source: trunk/FACT++/src/EventImp.h@ 10295

Last change on this file since 10295 was 10295, checked in by tbretz, 14 years ago
Added operator() for easy configuration; Added fDescription for convenience; Added GetString and GetVector member functions
File size: 2.9 KB
Line 
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
11class EventImp
12{
13 int fTargetState; /// Target state of an event
14 std::vector<int> fAllowedStates; /// List of states in which this event is allowed
15
16 std::string fDescription; /// A human readable description of the event
17
18 /// http://www.boost.org/doc/libs/1_45_0/libs/bind/bind.html
19 boost::function<int(const EventImp &)> fFunction;
20
21public:
22 /// Constructor. Stores the target state given.
23 EventImp(int target=-1) : fTargetState(target) { }
24 /// Copy constructor
25 EventImp(const EventImp &cmd);
26
27 // Description
28 EventImp &SetDescription(const std::string &str) { fDescription=str; return *this; }
29
30 // Function handling
31 EventImp &AssignFunction(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()(boost::function<int(const EventImp &)> func) { return AssignFunction(func); }
37 EventImp &operator()(const std::string str) { return SetDescription(str); }
38 EventImp &operator()(const char *str) { return SetDescription(str); }
39
40 // Print contents
41 virtual void Print(std::ostream &out) const;
42 virtual void Print() const;
43
44 // Handling of the states
45 void AddAllowedState(int state);
46 void AddAllowedStates(const char *states);
47
48 bool IsStateAllowed(int state) const;
49
50 int GetTargetState() const { return fTargetState; }
51
52 // virtual function to return the data as stored in the derived classes
53 virtual std::string GetName() const { return ""; }
54 virtual std::string GetFormat() const { return ""; }
55
56 virtual const void *GetData() const { return 0; }
57 virtual int GetSize() const { return 0; }
58
59 virtual Time GetTime() const { return Time::None; }
60
61 // Getter for all the data contained (name, format, data and time)
62 short GetShort() const { return *reinterpret_cast<const short*>(GetData()); }
63 unsigned short GetUShort() const { return *reinterpret_cast<const unsigned short*>(GetData()); }
64 int GetInt() const { return *reinterpret_cast<const int*>(GetData()); }
65 unsigned int GetUInt() const { return *reinterpret_cast<const unsigned int*>(GetData()); }
66 float GetFloat() const { return *reinterpret_cast<const float*>(GetData()); }
67 double GetDouble() const { return *reinterpret_cast<const double*>(GetData()); }
68 const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
69 std::string GetString() const { return std::string(GetText(), GetSize()); }
70 std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
71
72};
73
74#endif
Note: See TracBrowser for help on using the repository browser.