DiaDes  0.1
DIAgnosis of Discrete-Event System
Event.hh
Go to the documentation of this file.
1 #ifndef __DIADES__AUTOMATA__EVENT__HH
2 #define __DIADES__AUTOMATA__EVENT__HH
3 
4 /*
5  * Event.hh
6  *
7  * Created on: 2 dec. 2008
8  * Author: ypencole
9  */
10 
11 
12 
13 #include<unordered_map>
14 #include<vector>
15 #include<iostream>
18 
19 
20 using std::istream;
21 using std::ostream;
22 using std::vector;
23 using std::unordered_map;
24 
25 namespace Diades
26 {
27  namespace Automata {
28 
29 
30  class EventFactory;
31 
32 
33 
38  typedef struct {
39  unsigned _id;
40  string _label;
41  string _nickname;
42  } EventData;
43 
44 
45 
53  class Event {
54  public:
55  static string typeName() { return "Automata::Event"; }
56 
62  typedef unsigned Id;
63 
64 
65 
66 
67 
71  typedef string Label;
72 
77  static Event nullEvent();
78 
83  static const Event::Label & nullEventLabel();
84 
85 
91  static Event getEvent(const Label & evtLabel);
92 
98  static Event getEvent(const Id & evtId);
99 
105  static void allocateEvents(size_t capacity);
106 
111  static void destroyEvents();
112 
113  public:
115 
116  private:
117  EventDataPointer _ptr; // pointer to the EventData
118 
124  Event(EventDataPointer data):_ptr(data) {}
125 
126  public:
127 // template<class Archive>
128 // void save(Archive & ar, const unsigned int version) const
129 // {
130 // if(isValid())
131 // {
132 // unsigned idt = id();
133 // ar & idt;
134 // string lab = label();
135 // ar << lab;
136 // lab = nickname();
137 // ar << lab;
138 // }
139 // else
140 // {
141 // string empty;
142 // unsigned zero = 0;
143 // ar & zero;
144 // ar & empty;
145 // ar & empty;
146 // }
147 // }
148 //
149 // template<class Archive>
150 // void load(Archive & ar, const unsigned int version);
151 //
152 // BOOST_SERIALIZATION_SPLIT_MEMBER();
153 
154  public:
159  Event():_ptr(nullptr){}
160 
166  Event(const Event & e):_ptr(e._ptr){}
167 
172  bool isValid() const { return _ptr != nullptr; }
173 
180  const Label & label() const;
181 
190  const Label & nickname() const {
191  require(Exception,isValid(),"nickname: the event is invalid.");
192  return _ptr->_nickname;
193  }
194 
195 
196 
197  void setNickname(const Label & nick)
198  {
199  _ptr->_nickname = nick;
200  }
201 
207  Id id() const {
208  require(Exception,isValid(),"id: the event is invalid.");
209  return _ptr->_id;
210  }
211 
217  Event & operator=(const Event & e) { if(this != &e) { _ptr = e._ptr; } return *this; }
218 
224  bool operator < (const Event & e) const {
225  if( (!isValid()) && (!e.isValid()))
226  {
227  return false;
228  }
229  if(!isValid())
230  {
231  return true;
232  }
233  if(!e.isValid())
234  {
235  return false;
236  }
237  return id() < e.id();
238  }
239 
245  bool operator == (const Event & e) const {
246  if(isValid() && e.isValid())
247  {
248  return ( id() == e.id() );
249  }
250  return (!isValid()) && (!e.isValid());
251  }
252 
258  bool operator != (const Event & e) const {
259  return !(*this == e);
260  }
261 
262 
263 
269  bool operator > (const Event & e) const {
270  return !( ((*this) < e) || ((*this) == e));
271  }
272 
277  int memoryUsage() const { return sizeof(_ptr); };
278 
283  int dataMemoryUsage() const { if(isValid()) { return sizeof(_ptr->_id) + sizeof(_ptr->_label) + sizeof(_ptr->_nickname); } return 0; }
284 
291  friend ostream & operator <<(ostream & os, const Event & e)
292  {
293  return os << e.label();
294  }
295 
302  friend istream & operator >>(istream & is, Event & e);
303 
304 
311  ostream & save(ostream & os) const
312  {
313  require(Exception,isValid(),"save: invalid event");
314  return os << id() << " " << label() << " " << nickname();
315  }
316 
322  istream & load(istream & is);
323 
324  friend class EventFactory;
325  };
326  };
327 };
328 
329 
330 namespace std
331 {
332  template<> struct hash<Diades::Automata::Event>
333  {
334  size_t operator()(const Diades::Automata::Event & e) const
335  {
336  if(e.isValid())
337  {
338  return e.id();
339  }
340  return 0;
341  }
342  };
343 };
344 
345 namespace Diades
346 {
347  namespace Automata
348  {
349 
350 
351 
357  {
358  public:
359  static string typeName() { return "Automata::EventFactory"; }
365  static void destroy();
366 
371  static void init(size_t capacity);
372 
377  static EventFactory * factory();
378 
387 
398  Event setEvent(Event::Id id, const Event::Label & label, const Event::Label & nick);
399 
404  Event nullEvent() const { return _events[0]; }
405 
412  Event getEvent(const Event::Label & label);
413 
418  ~EventFactory();
419 
424  const Event::Label & nullEventLabel() const
425  {
426  return _nullEventLabel;
427  }
428 
435  size_t capacity() const
436  {
437  return _events.capacity();
438  }
439 
440  protected:
441 
446  EventFactory();
447 
452  EventFactory(size_t capacity);
453 
454  private:
455  static EventFactory * _instance; // the unique instance of EventFactory
456  vector<Event> _events; // the set of created events _events[0] is the null event
457  unordered_map<Event::Label,Event> _mapping; // mapping to get the event from the label
459 
460  };
461 
462 
463 
464 // template<class Archive>
465 // void Event::load(Archive & ar, const unsigned int version)
466 // {
467 // Event::Id id;
468 // string label;
469 // string nickname;
470 // ar & id;
471 // ar & label;
472 // ar & nickname;
473 // if(label.empty())
474 // {
475 // _ptr = 0;
476 // }
477 // else
478 // {
479 // operator=(EventFactory::factory()->setEvent(id,label,nickname));
480 // }
481 // }
482 
483  };
484 
485 };
486 
487 
488 
489 
490 
491 #endif
492 
493 
const Label & label() const
ostream & save(ostream &os) const
Definition: Event.hh:311
Event getEvent(Event::Id id)
vector< Event > _events
Definition: Event.hh:456
const Label & nickname() const
Definition: Event.hh:190
STL namespace.
EventDataPointer _ptr
Definition: Event.hh:117
int memoryUsage() const
Definition: Event.hh:277
void load(Archive &ar, std::unordered_map< Key, HashFcn, EqualKey, Allocator > &t, const unsigned int)
static void init(size_t capacity)
Event::Label _nullEventLabel
Definition: Event.hh:458
DdAutFsm::EventPropertyId Event
Definition: TrimState.cc:139
Event setEvent(Event::Id id, const Event::Label &label, const Event::Label &nick)
static EventFactory * _instance
Definition: Event.hh:455
bool isValid() const
Definition: Event.hh:172
size_t operator()(const Diades::Automata::Event &e) const
Definition: Event.hh:334
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
static string typeName()
Definition: Event.hh:55
Event(EventDataPointer data)
Definition: Event.hh:124
Diades::Utils::Exception< Event > Exception
Definition: Event.hh:57
EventData * EventDataPointer
Definition: Event.hh:114
const Event::Label & nullEventLabel() const
Definition: Event.hh:424
int dataMemoryUsage() const
Definition: Event.hh:283
Event(const Event &e)
Definition: Event.hh:166
Event & operator=(const Event &e)
Definition: Event.hh:217
static EventFactory * factory()
ostream & operator<<(ostream &os, const DiagState &dstate)
size_t capacity() const
Definition: Event.hh:435
void setNickname(const Label &nick)
Definition: Event.hh:197
Diades::Utils::Exception< EventFactory > Exception
Definition: Event.hh:360
unordered_map< Event::Label, Event > _mapping
Definition: Event.hh:457
static string typeName()
Definition: Event.hh:359