DiaDes  0.1
DIAgnosis of Discrete-Event System
Timer.hh
Go to the documentation of this file.
1 #ifndef __DIADES__UTILS__TIMER__HH
2 #define __DIADES__UTILS__TIMER__HH
3 
4 #include <iostream>
5 #include <exception>
6 #include <stdexcept>
7 #include <string>
8 #include <map>
9 #include "Assertion.hh"
10 
11 using std::string;
12 using std::exception;
13 using std::runtime_error;
14 using std::domain_error;
15 using std::cerr;
16 using std::endl;
17 using std::cout;
18 using std::map;
19 
20 namespace Diades
21 {
22 namespace Utils
23 {
24 
25 
29 class TimerInvalid : public runtime_error, public domain_error
30 {
31 
32 public:
36  TimerInvalid(const string& whatArg) : runtime_error(whatArg), domain_error(whatArg)
37  {
38  cerr << "EXCEPTION Timer -> " << whatArg << endl;
39  }
40 };
41 
42 
43 
44 
48 class TimerTimeOut : public exception
49 {
50 
51 public:
55  TimerTimeOut(const string & timer) : exception()
56  {
57  cout << "TimeOut from timer " << timer << endl;
58  }
59 };
60 
61 
62 
63 
64 
65 
71 class Timer
72 {
73 private:
74 
75  string labl;
76  // label of the timer (use to throw the exception).
77 
78  long int timeOut;
79  // User time after which an exception should be thrown
80  // (millisecond) if timeOut = -1, the timer is not activated
81 
82 public:
83 
92  Timer(const string & label, long int delay);
93 
98  void checkTimeOut();
99 
100 
105  bool activated() const { return timeOut != -1; }
106 
107 
111  long int getTime() const;
112 
113 };
114 
115 
124 {
125 
126 private:
128  map<string,Timer *> timers;
129 
130 protected:
132 
133 
134 public:
135  static TimerFactory * Instance();
136 
142  void checkTimeOut();
143 
149  void createTimer(const string & label, long int delay);
150 
154  void stopTimer(const string & label);
155 
159  long int getTime(const string & label) const { return timers.find(label)->second->getTime(); }
160 
161 
162 };
163 
164 };
165 
166 };
167 #endif
map< string, Timer * > timers
Definition: Timer.hh:128
struct itimerval timer
TimerInvalid(const string &whatArg)
Definition: Timer.hh:36
long int getTime(const string &label) const
Definition: Timer.hh:159
long int timeOut
Definition: Timer.hh:78
TimerTimeOut(const string &timer)
Definition: Timer.hh:55
Namespace of the Diades project.
static TimerFactory * _instance
Definition: Timer.hh:127
bool activated() const
Definition: Timer.hh:105