DiaDes  0.1
DIAgnosis of Discrete-Event System
Event.hh
Go to the documentation of this file.
1 #ifndef __DIADES__PETRI__EVENT__HH__
2 #define __DIADES__PETRI__EVENT__HH__
3 
4 #include<iostream>
5 #include<unordered_map>
6 #include<vector>
7 #include<utils/Assertion.hh>
8 #include<utils/Substring.hh>
9 
10 using std::vector;
11 
12 namespace Diades
13 {
14  namespace Petri
15  {
16 
17 
22  class EventInvalid : public runtime_error, public domain_error
23  {
24  public:
28  EventInvalid(const string& whatArg) : runtime_error(whatArg), domain_error(whatArg){
29  ::std::cerr << "EXCEPTION Event -> " << whatArg << ::std::endl;
30  }
31  };
32 
33 
34 
35 
36  class Event;
37 
43  class EventData {
44  private:
45  string _type;
46  size_t _id;
47 
48  private:
54  EventData(const string & type, size_t id):_type(type),_id(id){}
55 
60  const string & type() const { return _type; }
61 
62 
63 
68  size_t id() const { return _id; }
69 
70  friend class Event;
71  };
72 
73  class EventFactory;
74 
82  class Event {
83  private:
85  public:
90  Event():_data(0){}
91 
92  private:
100  Event(const string & type,size_t id):_data(new EventData(type,id)){}
101 
102  public:
108  Event(const Event & event):_data(event._data){}
109 
110 
116  string type() const { if(isNull()) {return "Null";} return _data->type(); }
117 
122  bool isNull() const { return _data == 0; }
123 
129  bool operator == (const Event & e) const { return _data == e._data; }
130 
139  bool operator < (const Event & e) const { return _data < e._data; }
140 
149  bool operator > (const Event & e) const { return _data > e._data; }
150 
156  size_t id() const { if(_data == 0) { return 0; } return _data->id(); }
157 
164  friend std::ostream & operator << (std::ostream & os, const Event & e) { return os << e.type(); }
165 
166 
167  friend class EventFactory;
168  };
169  };
170 };
171 
172 namespace std
173 {
180  template<> struct hash<Diades::Petri::Event> {
181  size_t operator()(const Diades::Petri::Event & e) const {
182  return e.id();
183  }
184  };
185 };
186 
187 
188 namespace Diades
189 {
190  namespace Petri
191  {
192 
193 
202  {
203  public:
204 
209  static EventFactory * Instance();
210 
211  protected:
217 
218  public:
225  const Event & getEvent(const string & type)
226  {
227  unordered_map<string,Event>::const_iterator it = _mapping.find(type);
228  if(it == _mapping.end())
229  {
230  size_t newId = _events.size();
231  _events.push_back(Event(type,newId));
232  _mapping[type] = _events.back();
233  return _events.back();
234  }
235  return it->second;
236  }
237 
244  const Event & getEvent(size_t id) const
245  {
246  require(EventInvalid, _events.size() > id,"getEvent: invalid id");
247  return _events[id];
248  }
249 
250 
256  ~EventFactory();
257 
262  size_t numberOfCurrentEvents() const { return _events.size(); }
263 
264  private:
265  vector<Event> _events;
266  unordered_map<string,Event> _mapping;
268  };
269  };
270 };
271 
272 #endif
const Event & getEvent(size_t id) const
Definition: Event.hh:244
bool operator>(const Event &e) const
Definition: Event.hh:149
friend std::ostream & operator<<(std::ostream &os, const Event &e)
Definition: Event.hh:164
static EventFactory * _instance
Definition: Event.hh:267
const Event & getEvent(const string &type)
Definition: Event.hh:225
const string & type() const
Definition: Event.hh:60
unordered_map< string, Event > _mapping
Definition: Event.hh:266
STL namespace.
EventInvalid(const string &whatArg)
Definition: Event.hh:28
static EventFactory * Instance()
bool operator<(const Event &e) const
Definition: Event.hh:139
size_t id() const
Definition: Event.hh:156
#define require(Exception, expr, message)
Definition: Assertion.hh:56
Namespace of the Diades project.
size_t id() const
Definition: Event.hh:68
Event(const string &type, size_t id)
Definition: Event.hh:100
EventData * _data
Definition: Event.hh:84
Event(const Event &event)
Definition: Event.hh:108
vector< Event > _events
Definition: Event.hh:265
string type() const
Definition: Event.hh:116
EventData(const string &type, size_t id)
Definition: Event.hh:54
bool isNull() const
Definition: Event.hh:122
size_t operator()(const Diades::Petri::Event &e) const
Definition: Event.hh:181
bool operator==(const Event &e) const
Definition: Event.hh:129
size_t numberOfCurrentEvents() const
Definition: Event.hh:262