Files
pip/pitimer.h
2012-01-16 01:01:49 +04:00

116 lines
3.4 KiB
C++

/*
PIP - Platform Independent Primitives
Timer
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PITIMER_H
#define PITIMER_H
#include <ctime>
#include <csignal>
#include "pithread.h"
#include "pistring.h"
#include "piobject.h"
typedef void (*TimerEvent)(void * , int );
struct PITime {
int seconds;
int minutes;
int hours;
};
struct PIDate {
int day;
int month;
int year; // since 1900
};
class PITimer
#ifdef WINDOWS
: public PIThread
#else
: public PIObject
#endif
{
public:
PITimer(TimerEvent slot = 0, void * data = 0);
~PITimer();
void setData(void * data_) {data = data_;}
void setSlot(TimerEvent slot) {ret_func = slot;}
#ifdef WINDOWS
void reset() {t_st = GetCurrentTime();}
#else
EVENT_HANDLER0(PITimer, void, reset) {clock_gettime(0, &t_st);}
EVENT_HANDLER1(PITimer, void, start, double, msecs);
EVENT_HANDLER0(PITimer, void, stop) {if (ti == 0) timer_delete(timer); ti = -1; running = false;}
EVENT_HANDLER0(PITimer, bool, waitForFinish) {return waitForFinish(-1);}
EVENT_HANDLER1(PITimer, 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();}
#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();}
double elapsed_n(); // nanoseconds
double elapsed_u(); // microseconds
double elapsed_m(); // miliseconds
double elapsed_s(); // seconds
private:
#ifdef WINDOWS
void run();
long int t_st, t_cur;
#else
static void timer_event(sigval e);
bool running;
volatile bool lockRun;
PIMutex mutex_;
int ti;
itimerspec spec;
timespec t_st, t_cur;
timer_t timer;
sigevent se;
#endif
struct TimerSlot {
TimerSlot(TimerEvent slot_ = 0, int delim_ = 1) {slot = slot_; delim = delim_; tick = 0;}
TimerEvent slot;
int delim;
int tick;
};
void * data;
TimerEvent ret_func;
PIVector<TimerSlot> ret_funcs;
};
PITime currentTime();
PIDate currentDate();
PIString time2string(const PITime & time, const PIString & format = "h:mm:ss");
PIString date2string(const PIDate & date, const PIString & format = "d.mm.yyyy");
#endif // PITIMER_H