1 | #ifndef FACT_EventImp
|
---|
2 | #define FACT_EventImp
|
---|
3 |
|
---|
4 | #include <string>
|
---|
5 | #include <vector>
|
---|
6 |
|
---|
7 | #include <boost/function.hpp>
|
---|
8 |
|
---|
9 | #include "Time.h"
|
---|
10 |
|
---|
11 | class EventImp
|
---|
12 | { int fTargetState; /// Target state of an event
|
---|
13 | std::vector<int> fAllowedStates; /// List of states in which this event is allowed
|
---|
14 |
|
---|
15 | /// http://www.boost.org/doc/libs/1_45_0/libs/bind/bind.html
|
---|
16 | boost::function<int(const EventImp &)> fFunction;
|
---|
17 |
|
---|
18 | public:
|
---|
19 | /// Constructor. Stores the target state given.
|
---|
20 | EventImp(int target=-1) : fTargetState(target) { }
|
---|
21 | /// Copy constructor
|
---|
22 | EventImp(const EventImp &cmd);
|
---|
23 |
|
---|
24 | // Description
|
---|
25 | virtual void SetDescription(const std::string &) { }
|
---|
26 | virtual std::string GetDescription() const { return ""; }
|
---|
27 |
|
---|
28 | // Function handling
|
---|
29 | EventImp &AssignFunction(const boost::function<int(const EventImp &)> &func) { fFunction = func; return *this; }
|
---|
30 | bool HasFunc() const { return !fFunction.empty(); }
|
---|
31 | int ExecFunc() const { return HasFunc() ? fFunction(*this) : -1; }
|
---|
32 |
|
---|
33 | // Configuration helper
|
---|
34 | EventImp &operator()(const boost::function<int(const EventImp &)> &func) { return AssignFunction(func); }
|
---|
35 | EventImp &operator()(const std::string str) { SetDescription(str); return *this; }
|
---|
36 | EventImp &operator()(const char *str) { SetDescription(str); return *this; }
|
---|
37 | EventImp &operator()(int state) { fAllowedStates.push_back(state); return *this; }
|
---|
38 |
|
---|
39 | // Print contents
|
---|
40 | virtual void Print(std::ostream &out, bool strip=false) const;
|
---|
41 | virtual void Print(bool strip=false) const;
|
---|
42 |
|
---|
43 | // Handling of the states
|
---|
44 | void AddAllowedState(int state);
|
---|
45 | void AddAllowedStates(const char *states);
|
---|
46 |
|
---|
47 | bool IsStateAllowed(int state) const;
|
---|
48 |
|
---|
49 | int GetTargetState() const { return fTargetState; }
|
---|
50 |
|
---|
51 | // virtual function to return the data as stored in the derived classes
|
---|
52 | virtual std::string GetName() const { return ""; }
|
---|
53 | virtual std::string GetFormat() const { return ""; }
|
---|
54 |
|
---|
55 | virtual const void *GetData() const { return 0; }
|
---|
56 | virtual int GetSize() const { return 0; }
|
---|
57 |
|
---|
58 | virtual Time GetTime() const { return Time::None; }
|
---|
59 |
|
---|
60 | // Getter for all the data contained (name, format, data and time)
|
---|
61 | short GetShort() const { return *reinterpret_cast<const short*>(GetData()); }
|
---|
62 | unsigned short GetUShort() const { return *reinterpret_cast<const unsigned short*>(GetData()); }
|
---|
63 | int GetInt() const { return *reinterpret_cast<const int*>(GetData()); }
|
---|
64 | unsigned int GetUInt() const { return *reinterpret_cast<const unsigned int*>(GetData()); }
|
---|
65 | float GetFloat() const { return *reinterpret_cast<const float*>(GetData()); }
|
---|
66 | double GetDouble() const { return *reinterpret_cast<const double*>(GetData()); }
|
---|
67 | const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
|
---|
68 | std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
|
---|
69 | std::string GetString() const;
|
---|
70 | };
|
---|
71 |
|
---|
72 | #endif
|
---|