IT++ Logo
events.h
Go to the documentation of this file.
1 
29 #ifndef EVENTS_H
30 #define EVENTS_H
31 
32 #include <itpp/base/itassert.h>
33 
34 #if (defined(_MSC_VER) && defined(ITPP_SHARED_LIB) && !(defined(itpp_EXPORTS) || defined(itpp_debug_EXPORTS)))
35 
36 #ifndef ITPP_PROTOCOL_EXCLUDED
37 #define ITPP_PROTOCOL_EXCLUDED
38 #pragma message( "PROTOCOL definitions are not available for MSVC shared builds" )
39 #endif
40 
41 #else
42 
43 
44 #include <queue>
45 #include <deque>
46 
47 namespace itpp
48 {
49 
51 
52 
53 // typedef long double Ttype; //!< 128-bit floating point time
54 typedef double Ttype;
55 // typedef long unsigned int Ttype; //!< 64-bit unsigned integer time
56 
57 class Event_Queue;
58 class Base_Event;
59 class Base_Signal;
60 
69 {
70 public:
71  friend class Base_Signal;
72 
73  friend class Event_Queue;
74 
75  friend struct Compare_Base_Event_Times;
76 
78  Base_Event(const Ttype delta_time) { // The event will occur in 'delta_time' time units from now!
79  it_assert(delta_time >= 0, "Only causal simulations are possible");
80  active = true;
81  delta_t = delta_time;
82  expire_t = 0; // Will be set correctly upon calling Event_Queue::add().
83  id = global_id++;
84  }
85 
87  virtual ~Base_Event() {}
88 
90  void cancel() { active = false; }
91 
92 protected:
94  virtual void exec(void) = 0;
100  bool active;
102  unsigned long long int id;
104  static unsigned long long int global_id;
105 };
106 
110  bool operator()(Base_Event *event1, Base_Event *event2) {
111  if (event1->expire_t == event2->expire_t) // Equal expire times.
112  return (event1->id > event2->id); // Base comparison on the event id.
113  else
114  return (event1->expire_t > event2->expire_t); // Different expire times. Regular comparison.
115  }
116 };
117 
127 {
128 public:
129  friend class Base_Signal;
130 
135 
137  static void add(Base_Event *e);
139  static Ttype now() {return t;}
141  static void start();
143  static void stop();
145  static void clear();
146 protected:
147  //static void cancel_all(Base_Signal *s);
148 private:
149  typedef std::deque<Base_Event*, std::allocator< Base_Event* > >::iterator Base_Event_Iterator;
150  static void _run();
151  static bool keep_running;
152  static Ttype t; // Current time.
153  static std::priority_queue < Base_Event*,
154  std::deque<Base_Event*, std::allocator<Base_Event*> >,
155  Compare_Base_Event_Times > event_queue; // Queue for the Events.
156 };
157 
163 template <class ObjectType>
164 class Event : public Base_Event
165 {
166 public:
168  Event(ObjectType *object_pointer, void (ObjectType::*object_function_pointer)(), const Ttype delta_time) : Base_Event(delta_time) {
169  po = object_pointer;
170  pm = object_function_pointer;
171  }
172 
174  virtual ~Event() {}
175 
177  virtual void exec(void) {(*po.*pm)(); }
178 
179 private:
180  void (ObjectType::*pm)(); // Pointer to class member function to be executed on event expire.
181  ObjectType *po; // Pointer to object who's member function is to be executed on event expire.
182 };
183 
189 template <class ObjectType, class DataType> class Data_Event : public Base_Event
190 {
191 public:
193  Data_Event(ObjectType *object_pointer,
194  void (ObjectType::*object_function_pointer)(DataType data),
195  DataType data, const Ttype delta_time) : Base_Event(delta_time) {
196  po = object_pointer;
197  pm = object_function_pointer;
198  u = data;
199  }
200 
202  virtual ~Data_Event() {}
203 
205  virtual void exec(void) {
206  (*po.*pm)(u);
207  }
208 
209 private:
210  void (ObjectType::*pm)(DataType data); // Pointer to class member function to be executed on event expire.
211  ObjectType* po; // Pointer to object who's member function is to be executed on event expire.
212  DataType u; // User data.
213 };
214 
216 
217 } // namespace itpp
218 
219 #endif
220 
221 #endif // #ifndef EVENTS_H
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:24 for IT++ by Doxygen 1.8.2