4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen
This commit is contained in:
121
pitimer.h
121
pitimer.h
@@ -28,7 +28,7 @@
|
||||
|
||||
typedef void (*TimerEvent)(void * , int );
|
||||
|
||||
class PISystemTime {
|
||||
class PIP_EXPORT PISystemTime {
|
||||
public:
|
||||
PISystemTime() {seconds = nanoseconds = 0;}
|
||||
PISystemTime(long s, long ns) {seconds = s; nanoseconds = ns; checkOverflows();}
|
||||
@@ -71,29 +71,65 @@ private:
|
||||
inline PIByteArray & operator <<(PIByteArray & s, const PISystemTime & v) {s << v.seconds << v.nanoseconds; return s;}
|
||||
inline PIByteArray & operator >>(PIByteArray & s, PISystemTime & v) {s >> v.seconds >> v.nanoseconds; return s;}
|
||||
|
||||
struct PITime {
|
||||
struct PIP_EXPORT PITime {
|
||||
PITime() {hours = minutes = seconds = milliseconds = 0;}
|
||||
int milliseconds;
|
||||
int seconds;
|
||||
int minutes;
|
||||
int hours;
|
||||
PIString toString(const PIString & format = "h:mm:ss");
|
||||
PIString toString(const PIString & format = "h:mm:ss") const;
|
||||
};
|
||||
PIP_EXPORT bool operator ==(const PITime & t0, const PITime & t1);
|
||||
PIP_EXPORT bool operator <(const PITime & t0, const PITime & t1);
|
||||
PIP_EXPORT bool operator >(const PITime & t0, const PITime & t1);
|
||||
inline bool operator !=(const PITime & t0, const PITime & t1) {return !(t0 == t1);}
|
||||
inline bool operator <=(const PITime & t0, const PITime & t1) {return !(t0 > t1);}
|
||||
inline bool operator >=(const PITime & t0, const PITime & t1) {return !(t0 < t1);}
|
||||
|
||||
struct PIDate {
|
||||
struct PIP_EXPORT PIDate {
|
||||
PIDate() {year = month = day = 0;}
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
PIString toString(const PIString & format = "d.MM.yyyy");
|
||||
PIString toString(const PIString & format = "d.MM.yyyy") const;
|
||||
};
|
||||
PIP_EXPORT bool operator ==(const PIDate & t0, const PIDate & t1);
|
||||
PIP_EXPORT bool operator <(const PIDate & t0, const PIDate & t1);
|
||||
PIP_EXPORT bool operator >(const PIDate & t0, const PIDate & t1);
|
||||
inline bool operator !=(const PIDate & t0, const PIDate & t1) {return !(t0 == t1);}
|
||||
inline bool operator <=(const PIDate & t0, const PIDate & t1) {return !(t0 > t1);}
|
||||
inline bool operator >=(const PIDate & t0, const PIDate & t1) {return !(t0 < t1);}
|
||||
|
||||
struct PIDateTime {
|
||||
struct PIP_EXPORT PIDateTime {
|
||||
PIDateTime() {year = month = day = hours = minutes = seconds = 0;}
|
||||
PIDateTime(const PITime & time) {year = month = day = 0; hours = time.hours; minutes = time.minutes; seconds = time.seconds; milliseconds = time.milliseconds;}
|
||||
PIDateTime(const PIDate & date) {year = date.year; month = date.month; day = date.day; hours = minutes = seconds = milliseconds = 0;}
|
||||
PIDateTime(const PIDate & date, const PITime & time) {year = date.year; month = date.month; day = date.day; hours = time.hours; minutes = time.minutes; seconds = time.seconds; milliseconds = time.milliseconds;}
|
||||
int milliseconds;
|
||||
int seconds;
|
||||
int minutes;
|
||||
int hours;
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
PIString toString(const PIString & format = "h:mm:ss d.MM.yyyy");
|
||||
PIDateTime normalized() const {return PIDateTime::fromSecondSinceEpoch(toSecondSinceEpoch());}
|
||||
void normalize() {*this = normalized();}
|
||||
PIString toString(const PIString & format = "h:mm:ss d.MM.yyyy") const;
|
||||
time_t toSecondSinceEpoch() const;
|
||||
PISystemTime toSystemTime() const {return PISystemTime(int(toSecondSinceEpoch()), milliseconds * 1000000);}
|
||||
void operator +=(const PIDateTime & d1) {year += d1.year; month += d1.month; day += d1.day; hours += d1.hours; minutes += d1.minutes; seconds += d1.seconds; normalize();}
|
||||
void operator -=(const PIDateTime & d1) {year -= d1.year; month -= d1.month; day -= d1.day; hours -= d1.hours; minutes -= d1.minutes; seconds -= d1.seconds; normalize();}
|
||||
static PIDateTime fromSecondSinceEpoch(const time_t sec);
|
||||
static PIDateTime fromSystemTime(const PISystemTime & st) {PIDateTime dt = fromSecondSinceEpoch(st.seconds); dt.milliseconds = piClampi(st.nanoseconds / 1000000, 0, 999); return dt;}
|
||||
};
|
||||
inline PIDateTime operator +(const PIDateTime & d0, const PIDateTime & d1) {PIDateTime td = d0; td += d1; return td.normalized();}
|
||||
inline PIDateTime operator -(const PIDateTime & d0, const PIDateTime & d1) {PIDateTime td = d0; td -= d1; return td.normalized();}
|
||||
PIP_EXPORT bool operator ==(const PIDateTime & t0, const PIDateTime & t1);
|
||||
PIP_EXPORT bool operator <(const PIDateTime & t0, const PIDateTime & t1);
|
||||
PIP_EXPORT bool operator >(const PIDateTime & t0, const PIDateTime & t1);
|
||||
inline bool operator !=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 == t1);}
|
||||
inline bool operator <=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 > t1);}
|
||||
inline bool operator >=(const PIDateTime & t0, const PIDateTime & t1) {return !(t0 < t1);}
|
||||
|
||||
/// events:
|
||||
/// void timeout(void * data, int delimiter)
|
||||
@@ -108,13 +144,14 @@ struct PIDateTime {
|
||||
/// void clearDelimiters()
|
||||
/// void lock()
|
||||
/// void unlock()
|
||||
class PITimer
|
||||
class PIP_EXPORT PITimer
|
||||
#ifndef PIP_TIMER_RT
|
||||
: public PIThread
|
||||
#else
|
||||
: public PIObject
|
||||
#endif
|
||||
{
|
||||
PIOBJECT(PITimer)
|
||||
public:
|
||||
PITimer(TimerEvent slot = 0, void * data = 0, bool threaded = true);
|
||||
PITimer(bool threaded);
|
||||
@@ -122,53 +159,53 @@ public:
|
||||
|
||||
void setData(void * data_) {data = data_;}
|
||||
void setSlot(TimerEvent slot) {ret_func = slot;}
|
||||
double interval() const {return interval_;}
|
||||
#ifndef PIP_TIMER_RT
|
||||
# ifdef WINDOWS
|
||||
EVENT_HANDLER0(PITimer, void, reset) {t_st = GetCurrentTime();}
|
||||
EVENT_HANDLER1(PIThread, bool, start, int, timer_delay) {start(double(timer_delay)); return true;}
|
||||
EVENT_HANDLER1(PITimer, void, start, double, msecs);
|
||||
EVENT_HANDLER0(void, reset) {t_st = GetCurrentTime();}
|
||||
# elif defined(MAC_OS)
|
||||
EVENT_HANDLER0(PITimer, void, reset) {clock_get_time(__pi_mac_clock, &t_st);}
|
||||
EVENT_HANDLER1(PIThread, bool, start, int, timer_delay) {start(double(timer_delay)); return true;}
|
||||
EVENT_HANDLER1(PITimer, void, start, double, msecs);
|
||||
EVENT_HANDLER0(void, reset) {clock_get_time(__pi_mac_clock, &t_st);}
|
||||
# else
|
||||
EVENT_HANDLER0(PITimer, void, reset) {clock_gettime(0, &t_st);}
|
||||
EVENT_HANDLER1(PIThread, bool, start, int, timer_delay) {start(double(timer_delay)); return true;}
|
||||
EVENT_HANDLER1(PITimer, void, start, double, msecs);
|
||||
EVENT_HANDLER0(void, reset) {clock_gettime(0, &t_st);}
|
||||
# endif
|
||||
EVENT_HANDLER1(bool, start, int, timer_delay) {start(double(timer_delay)); return true;}
|
||||
EVENT_HANDLER1(void, start, double, msecs);
|
||||
EVENT_HANDLER0(void, stop) {running_ = false;}
|
||||
EVENT_HANDLER2(void, deferredStart, double, interval_msecs, double, delay_msecs);
|
||||
EVENT_HANDLER2(void, deferredStart, double, interval_msecs, const PIDateTime &, start_datetime);
|
||||
#else
|
||||
EVENT_HANDLER0(PITimer, void, reset) {clock_gettime(0, &t_st);}
|
||||
EVENT_HANDLER1(PITimer, void, start, double, msecs);
|
||||
EVENT_HANDLER2(PITimer, void, deferredStart, double, interval_msecs, double, delay_msecs);
|
||||
EVENT_HANDLER2(PITimer, void, deferredStart, double, interval_msecs, const PIDateTime &, start_datetime);
|
||||
EVENT_HANDLER0(PITimer, void, stop);
|
||||
EVENT_HANDLER0(PITimer, bool, waitForFinish) {return waitForFinish(-1);}
|
||||
EVENT_HANDLER1(PITimer, bool, waitForFinish, int, timeout_msecs);
|
||||
bool isRunning() const {return running;}
|
||||
EVENT_HANDLER0(void, reset) {clock_gettime(0, &t_st);}
|
||||
EVENT_HANDLER1(void, start, double, msecs);
|
||||
EVENT_HANDLER2(void, deferredStart, double, interval_msecs, double, delay_msecs);
|
||||
EVENT_HANDLER2(void, deferredStart, double, interval_msecs, const PIDateTime &, start_datetime);
|
||||
EVENT_HANDLER0(void, stop);
|
||||
EVENT_HANDLER0(bool, waitForFinish) {return waitForFinish(-1);}
|
||||
EVENT_HANDLER1(bool, waitForFinish, int, timeout_msecs);
|
||||
bool isRunning() const {return running_;}
|
||||
void needLockRun(bool need) {lockRun = need;}
|
||||
EVENT_HANDLER0(PITimer, void, lock) {mutex_.lock();}
|
||||
EVENT_HANDLER0(PITimer, void, unlock) {mutex_.unlock();}
|
||||
EVENT_HANDLER0(void, lock) {mutex_.lock();}
|
||||
EVENT_HANDLER0(void, unlock) {mutex_.unlock();}
|
||||
#endif
|
||||
void addDelimiter(int delim, TimerEvent slot = 0) {ret_funcs << TimerSlot(slot, delim);}
|
||||
void removeDelimiter(int delim) {for (int i = 0; i < ret_funcs.size_s(); ++i) if (ret_funcs[i].delim == delim) {ret_funcs.remove(i); i--;}}
|
||||
void removeDelimiter(TimerEvent slot) {for (int i = 0; i < ret_funcs.size_s(); ++i) if (ret_funcs[i].slot == slot) {ret_funcs.remove(i); i--;}}
|
||||
void removeDelimiter(int delim, TimerEvent slot) {for (int i = 0; i < ret_funcs.size_s(); ++i) if (ret_funcs[i].slot == slot && ret_funcs[i].delim == delim) {ret_funcs.remove(i); i--;}}
|
||||
EVENT_HANDLER0(PITimer, void, clearDelimiters) {ret_funcs.clear();}
|
||||
EVENT_HANDLER0(void, clearDelimiters) {ret_funcs.clear();}
|
||||
|
||||
double elapsed_n(); // nanoseconds
|
||||
double elapsed_u(); // microseconds
|
||||
double elapsed_m(); // miliseconds
|
||||
double elapsed_m(); // milliseconds
|
||||
double elapsed_s(); // seconds
|
||||
|
||||
double reset_time_n(); // nanoseconds
|
||||
double reset_time_u(); // microseconds
|
||||
double reset_time_m(); // miliseconds
|
||||
double reset_time_m(); // milliseconds
|
||||
double reset_time_s(); // seconds
|
||||
PISystemTime reset_time();
|
||||
|
||||
static double elapsed_system_n(); // nanoseconds
|
||||
static double elapsed_system_u(); // microseconds
|
||||
static double elapsed_system_m(); // miliseconds
|
||||
static double elapsed_system_m(); // milliseconds
|
||||
static double elapsed_system_s(); // seconds
|
||||
|
||||
|
||||
@@ -200,7 +237,7 @@ public:
|
||||
int ticks;
|
||||
#endif
|
||||
|
||||
EVENT2(PITimer, timeout, void * , data, int, delimiter)
|
||||
EVENT2(timeout, void * , data, int, delimiter)
|
||||
|
||||
protected:
|
||||
virtual void tick(void * data, int delimiter) {;}
|
||||
@@ -208,10 +245,12 @@ protected:
|
||||
private:
|
||||
#ifndef PIP_TIMER_RT
|
||||
void run();
|
||||
void end() {interval_ = 0.;}
|
||||
|
||||
PISystemTime st_time, inc_time;
|
||||
bool deferred_;
|
||||
#else
|
||||
bool running, threaded;
|
||||
bool threaded;
|
||||
volatile bool lockRun;
|
||||
PIMutex mutex_;
|
||||
int ti;
|
||||
@@ -219,6 +258,8 @@ private:
|
||||
timer_t timer;
|
||||
sigevent se;
|
||||
#endif
|
||||
bool running_;
|
||||
double interval_;
|
||||
|
||||
#ifdef WINDOWS
|
||||
long
|
||||
@@ -246,12 +287,12 @@ private:
|
||||
extern PITimer::TimerPool * pool;
|
||||
#endif
|
||||
|
||||
PITime currentTime();
|
||||
PIDate currentDate();
|
||||
PIDateTime currentDateTime();
|
||||
PISystemTime currentSystemTime();
|
||||
PIString time2string(const PITime & time, const PIString & format = "h:mm:ss"); // obsolete, use PITime.toString() instead
|
||||
PIString date2string(const PIDate & date, const PIString & format = "d.MM.yyyy"); // obsolete, use PITime.toString() instead
|
||||
PIString datetime2string(const PIDateTime & datetime, const PIString & format = "h:mm:ss d.MM.yyyy"); // obsolete, use PIDateTime.toString() instead
|
||||
PIP_EXPORT PITime currentTime();
|
||||
PIP_EXPORT PIDate currentDate();
|
||||
PIP_EXPORT PIDateTime currentDateTime();
|
||||
PIP_EXPORT PISystemTime currentSystemTime();
|
||||
PIP_EXPORT PIString time2string(const PITime & time, const PIString & format = "h:mm:ss"); // obsolete, use PITime.toString() instead
|
||||
PIP_EXPORT PIString date2string(const PIDate & date, const PIString & format = "d.MM.yyyy"); // obsolete, use PITime.toString() instead
|
||||
PIP_EXPORT PIString datetime2string(const PIDateTime & datetime, const PIString & format = "h:mm:ss d.MM.yyyy"); // obsolete, use PIDateTime.toString() instead
|
||||
|
||||
#endif // PITIMER_H
|
||||
|
||||
Reference in New Issue
Block a user