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