FACT++  1.0
EventImp.h
Go to the documentation of this file.
1 #ifndef FACT_EventImp
2 #define FACT_EventImp
3 
4 #include <string>
5 #include <vector>
6 
7 #include <functional>
8 
9 #include "Time.h"
10 
11 class EventImp
12 {
13  std::vector<int> fAllowedStates;
14 
16  std::function<int(const EventImp &)> fFunction;
17 
18 public:
20  EventImp() { }
22  EventImp(const EventImp &cmd);
23  virtual ~EventImp() {}
24 
25  // Description
26  virtual void SetDescription(const std::string &) { }
27  virtual std::string GetDescription() const { return ""; }
28 
29  // Function handling
30  EventImp &AssignFunction(const std::function<int(const EventImp &)> &func=std::function<int(const EventImp &)>()) { fFunction = func; return *this; }
31  bool HasFunc() const { return (bool)fFunction; }
32  int ExecFunc() const { return fFunction ? fFunction(*this) : -1; }
33 
34  // Configuration helper
35  EventImp &operator()(const std::function<int(const EventImp &)> &func) { return AssignFunction(func); }
36  EventImp &operator()(const std::string str) { SetDescription(str); return *this; }
37  EventImp &operator()(const char *str) { SetDescription(str); return *this; }
38  EventImp &operator()(int state) { fAllowedStates.push_back(state); return *this; }
39 
40  // Print contents
41  virtual void Print(std::ostream &out, bool strip=false) const;
42  virtual void Print(bool strip=false) const;
43 
44  // Handling of the states
45  void AddAllowedState(int state);
46  void AddAllowedStates(const std::string &states);
47 
48  bool IsStateAllowed(int state) const;
49 
50  // virtual function to return the data as stored in the derived classes
51  virtual std::string GetName() const { return ""; }
52  virtual std::string GetFormat() const { return ""; }
53 
54  virtual const void *GetData() const { return 0; }
55  virtual size_t GetSize() const { return 0; }
56 
57  virtual Time GetTime() const { return Time::None; }
58  virtual int GetQoS() const { return 0; }
59  virtual bool IsEmpty() const { return GetData()==0; }
60 
61  std::string GetTimeAsStr(const char *fmt) const;
62  uint64_t GetJavaDate() const;
63 
64  // Generalized access operators
65  template<typename T>
66  T Get(size_t offset=0) const
67  {
68  if (offset>=GetSize())
69  throw std::logic_error("EventImp::Get - offset out of range.");
70  return *reinterpret_cast<const T*>(GetText()+offset);
71  }
72 
73  template<typename T>
74  const T *Ptr(size_t offset=0) const
75  {
76  if (offset>=GetSize())
77  throw std::logic_error("EventImp::Ptr - offset out of range.");
78  return reinterpret_cast<const T*>(GetText()+offset);
79  }
80 
81  template<typename T>
82  const T &Ref(size_t offset=0) const
83  {
84  return *Ptr<T>(offset);
85  }
86 
87  // Getter for all the data contained (name, format, data and time)
88  const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
89 
90  bool GetBool() const { return Get<uint8_t>()!=0; }
91  int16_t GetShort() const { return Get<int16_t>(); }
92  uint16_t GetUShort() const { return Get<uint16_t>(); }
93  int32_t GetInt() const { return Get<int32_t>(); }
94  uint32_t GetUInt() const { return Get<uint32_t>(); }
95  int64_t GetXtra() const { return Get<int64_t>(); }
96  uint64_t GetUXtra() const { return Get<int64_t>(); }
97  float GetFloat() const { return Get<float>(); }
98  double GetDouble() const { return Get<double>(); }
99 
100  std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
101  std::string GetString() const;
102 };
103 
104 #endif
void AddAllowedStates(const std::string &states)
Definition: EventImp.cc:151
virtual std::string GetDescription() const
Definition: EventImp.h:27
float GetFloat() const
Definition: EventImp.h:97
A general base-class describing events issues in a state machine.
Definition: EventImp.h:11
const char * GetText() const
Definition: EventImp.h:88
EventImp()
Constructor. Stores the target state given.
Definition: EventImp.h:20
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
EventImp & operator()(int state)
Definition: EventImp.h:38
char str[80]
Definition: test_client.c:7
std::vector< int > fAllowedStates
Definition: EventImp.h:13
bool HasFunc() const
Definition: EventImp.h:31
EventImp & operator()(const char *str)
Definition: EventImp.h:37
int32_t GetInt() const
Definition: EventImp.h:93
std::string GetString() const
Definition: EventImp.cc:194
EventImp & operator()(const std::function< int(const EventImp &)> &func)
Definition: EventImp.h:35
uint16_t GetUShort() const
Definition: EventImp.h:92
virtual void SetDescription(const std::string &)
Definition: EventImp.h:26
virtual void Print(std::ostream &out, bool strip=false) const
Definition: EventImp.cc:214
double GetDouble() const
Definition: EventImp.h:98
int ExecFunc() const
Definition: EventImp.h:32
virtual Time GetTime() const
Definition: EventImp.h:57
virtual std::string GetFormat() const
Definition: EventImp.h:52
int64_t GetXtra() const
Definition: EventImp.h:95
virtual int GetQoS() const
Definition: EventImp.h:58
bool IsStateAllowed(int state) const
Definition: EventImp.cc:174
static const Time None
A none-time, this can be used as a simple representation of an invalid time.
Definition: Time.h:34
void AddAllowedState(int state)
Definition: EventImp.cc:136
uint64_t GetJavaDate() const
Definition: EventImp.cc:303
std::string GetTimeAsStr(const char *fmt) const
Definition: EventImp.cc:298
EventImp & AssignFunction(const std::function< int(const EventImp &)> &func=std::function< int(const EventImp &)>())
Definition: EventImp.h:30
Warning states
Definition: smartfact.txt:92
uint32_t GetUInt() const
Definition: EventImp.h:94
virtual ~EventImp()
Definition: EventImp.h:23
uint64_t GetUXtra() const
Definition: EventImp.h:96
EventImp & operator()(const std::string str)
Definition: EventImp.h:36
virtual std::string GetName() const
Definition: EventImp.h:51
std::vector< char > GetVector() const
Definition: EventImp.h:100
bool GetBool() const
Definition: EventImp.h:90
int16_t GetShort() const
Definition: EventImp.h:91
T Get(size_t offset=0) const
Definition: EventImp.h:66
std::function< int(const EventImp &)> fFunction
List of states in which this event is allowed.
Definition: EventImp.h:16
virtual const void * GetData() const
Definition: EventImp.h:54
const T & Ref(size_t offset=0) const
Definition: EventImp.h:82
virtual bool IsEmpty() const
Definition: EventImp.h:59
const T * Ptr(size_t offset=0) const
Definition: EventImp.h:74
virtual size_t GetSize() const
Definition: EventImp.h:55