-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimulator.h
More file actions
185 lines (161 loc) · 4.81 KB
/
Copy pathSimulator.h
File metadata and controls
185 lines (161 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Definition of Simulator class
// ECE8893, Final Project, Fall 2012
#ifndef __SIMULATOR_H__
#define __SIMULATOR_H__
#include "CommonDefs.h"
#include "Handler.h"
#include <vector>
#include <map>
#include <set>
class EventBase
{
public:
EventBase(Time_t t) : time(t) {}
public:
Time_t time; // Timestamp for the event
virtual void CallHandler() = 0; // All subclasses must implement this
};
template<typename T, typename OBJ>
class Event0 : public EventBase
{ // Event class with no arguments
// Type T is the object type for the event handler object
// Type OBJ is the actual event handler object
public:
Event0(double t, void (T::*f)(), OBJ* obj0)
: EventBase(t), handler(f), obj(obj0){}
void (T::*handler)(void);
OBJ* obj;
public:
void CallHandler();
};
template <typename T, typename OBJ>
void Event0<T, OBJ>::CallHandler()
{
(obj->*handler)();
}
// Event with one parameter on the callback function
template<typename T, typename OBJ, typename U1, typename T1>
class Event1 : public EventBase
{
public:
Event1(double t, void (T::*f)(U1), OBJ* obj0, T1 t1_0)
: EventBase(t), handler(f), obj(obj0), t1(t1_0){}
void (T::*handler)(U1);
OBJ* obj;
T1 t1;
public:
void CallHandler();
};
template <typename T, typename OBJ, typename U1, typename T1>
void Event1<T, OBJ, U1, T1>::CallHandler()
{
(obj->*handler)(t1);
}
template<typename T, typename OBJ,
typename U1, typename T1,
typename U2, typename T2>
class Event2 : public EventBase
{
public:
Event2(double t, void (T::*f)(U1, U2), OBJ* obj0, T1 t1_0, T2 t2_0)
: EventBase(t), handler(f), obj(obj0), t1(t1_0), t2(t2_0) {}
void (T::*handler)(U1, U2);
OBJ* obj;
T1 t1;
T2 t2;
public:
void CallHandler();
};
template <typename T, typename OBJ,
typename U1, typename T1,
typename U2, typename T2>
void Event2<T, OBJ, U1, T1, U2, T2>::CallHandler()
{
(obj->*handler)(t1, t2);
}
template <typename T, typename OBJ,
typename U1, typename T1,
typename U2, typename T2,
typename U3, typename T3>
class Event3 : public EventBase {
public:
Event3(double t, void (T::*f)(U1, U2, U3), OBJ *obj0, T1 t1_0, T2 t2_0, T3 t3_0)
: EventBase(t), handler(f), obj(obj0), t1(t1_0), t2(t2_0), t3(t3_0) {}
void (T::*handler)(U1, U2, U3);
OBJ* obj;
T1 t1;
T2 t2;
T3 t3;
public:
void CallHandler();
};
template <typename T, typename OBJ,
typename U1, typename T1,
typename U2, typename T2,
typename U3, typename T3>
void Event3<T,OBJ,U1,T1,U2,T2,U3,T3>::CallHandler() {
(obj->*handler)(t1,t2,t3);
}
// Now define the sorted set of events and the event comparator
class event_less
{
public:
event_less() { }
inline bool operator()(EventBase* const & l, const EventBase* const & r) const {
if(l->time < r->time) return true;
return false;
}
};
typedef std::multiset <EventBase*, event_less> EventSet_t;
class Simulator
{
public:
Simulator();
void Stop(); // Stop executing events and exit
static void Run(); // Starting executing events
static void StopAt(Time_t);
//static void Schedule(Handler*, Event*, Time_t); // Schedule a new future event
// Define the templated schedule functions
// This one has zero arguments on the handler callback
template <typename T, typename OBJ>
static void Schedule(double t, void(T::*handler)(void), OBJ* obj)
{
EventBase* ev = new Event0<T, OBJ>(t + Simulator::Now(), handler, obj);
events.insert(ev);
}
template <typename T, typename OBJ,
typename U1, typename T1>
void Schedule(double t, void(T::*handler)(U1), OBJ* obj, T1 t1)
{
EventBase* ev = new Event1<T, OBJ, U1, T1>(t + Simulator::Now(), handler, obj, t1);
events.insert(ev);
}
template <typename T, typename OBJ,
typename U1, typename T1,
typename U2, typename T2>
void Schedule(double t, void(T::*handler)(U1, U2), OBJ* obj, T1 t1, T2 t2)
{
EventBase* ev = new Event2<T, OBJ, U1, T1, U2, T2>(t + Simulator::Now(), handler, obj, t1, t2);
events.insert(ev);
}
template <typename T, typename OBJ,
typename U1, typename T1,
typename U2, typename T2,
typename U3, typename T3>
void Schedule(double t, void(T::*handler)(U1, U2, U3), OBJ* obj, T1 t1, T2 t2, T3 t3)
{
EventBase* ev = new Event3<T, OBJ, U1, T1, U2, T2, U3, T3>(t + Simulator::Now(), handler, obj, t1, t2, t3);
events.insert(ev);
}
static void ComputeRoutes(); // Compute routing tables at every node
// Inherited from Handler
void Handle(Event*, Time_t); // Handle the specified event
static Time_t Now(); // Return current simulation tiome
static Simulator* instance;
public:
// Your member variables here
static EventSet_t events;
static bool stopped;
static Time_t now;
};
#endif