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

Last change on this file since 11287 was 11246, checked in by tbretz, 13 years ago
Made destructor virtual -- destructors of base classes should always be virtual.
File size: 3.5 KB
Line 
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
12class 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
20public:
21 /// Constructor. Stores the target state given.
22 EventImp(int target=-1) : fTargetState(target) { }
23 /// Copy constructor
24 EventImp(const EventImp &cmd);
25 virtual ~EventImp() {}
26
27 // Description
28 virtual void SetDescription(const std::string &) { }
29 virtual std::string GetDescription() const { return ""; }
30
31 // Function handling
32 EventImp &AssignFunction(const boost::function<int(const EventImp &)> &func) { fFunction = func; return *this; }
33 bool HasFunc() const { return !fFunction.empty(); }
34 int ExecFunc() const { return HasFunc() ? fFunction(*this) : -1; }
35
36 // Configuration helper
37 EventImp &operator()(const boost::function<int(const EventImp &)> &func) { return AssignFunction(func); }
38 EventImp &operator()(const std::string str) { SetDescription(str); return *this; }
39 EventImp &operator()(const char *str) { SetDescription(str); return *this; }
40 EventImp &operator()(int state) { fAllowedStates.push_back(state); return *this; }
41
42 // Print contents
43 virtual void Print(std::ostream &out, bool strip=false) const;
44 virtual void Print(bool strip=false) const;
45
46 // Handling of the states
47 void AddAllowedState(int state);
48 void AddAllowedStates(const char *states);
49
50 bool IsStateAllowed(int state) const;
51
52 int GetTargetState() const { return fTargetState; }
53
54 // virtual function to return the data as stored in the derived classes
55 virtual std::string GetName() const { return ""; }
56 virtual std::string GetFormat() const { return ""; }
57
58 virtual const void *GetData() const { return 0; }
59 virtual int GetSize() const { return 0; }
60
61 virtual Time GetTime() const { return Time::None; }
62
63 // Generalized access operators
64 template<typename T>
65 T Get(int offset=0) const
66 {
67 if (offset>=GetSize())
68 throw std::logic_error("EventImp::Get - offset out of range.");
69 return *reinterpret_cast<const T*>(GetText()+offset);
70 }
71
72 template<typename T>
73 const T *Ptr(int offset=0) const
74 {
75 if (offset>=GetSize())
76 throw std::logic_error("EventImp::Ptr - offset out of range.");
77 return reinterpret_cast<const T*>(GetText()+offset);
78 }
79
80 // Getter for all the data contained (name, format, data and time)
81 const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
82
83 bool GetBool() const { return Get<uint8_t>()!=0; }
84 int16_t GetShort() const { return Get<int16_t>(); }
85 uint16_t GetUShort() const { return Get<uint16_t>(); }
86 int32_t GetInt() const { return Get<int32_t>(); }
87 uint32_t GetUInt() const { return Get<uint32_t>(); }
88 int64_t GetXtra() const { return Get<int64_t>(); }
89 uint64_t GetUXtra() const { return Get<int64_t>(); }
90 float GetFloat() const { return Get<float>(); }
91 double GetDouble() const { return Get<double>(); }
92
93 std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
94 std::string GetString() const;
95};
96
97#endif
Note: See TracBrowser for help on using the repository browser.