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

Last change on this file since 13452 was 12784, checked in by tbretz, 13 years ago
Should be std::function not boost::function
File size: 3.4 KB
Line 
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
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 /// http://www.boost.org/doc/libs/1_45_0/libs/bind/bind.html
17 std::function<int(const EventImp &)> fFunction;
18
19public:
20 /// Constructor. Stores the target state given.
21 EventImp(int target=-1) : fTargetState(target) { }
22 /// Copy constructor
23 EventImp(const EventImp &cmd);
24 virtual ~EventImp() {}
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 std::function<int(const EventImp &)> &func) { fFunction = func; return *this; }
32 bool HasFunc() const { return (bool)fFunction; }
33 int ExecFunc() const { return fFunction ? fFunction(*this) : -1; }
34
35 // Configuration helper
36 EventImp &operator()(const std::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 // Generalized access operators
63 template<typename T>
64 T Get(int offset=0) const
65 {
66 if (offset>=GetSize())
67 throw std::logic_error("EventImp::Get - offset out of range.");
68 return *reinterpret_cast<const T*>(GetText()+offset);
69 }
70
71 template<typename T>
72 const T *Ptr(int offset=0) const
73 {
74 if (offset>=GetSize())
75 throw std::logic_error("EventImp::Ptr - offset out of range.");
76 return reinterpret_cast<const T*>(GetText()+offset);
77 }
78
79 // Getter for all the data contained (name, format, data and time)
80 const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
81
82 bool GetBool() const { return Get<uint8_t>()!=0; }
83 int16_t GetShort() const { return Get<int16_t>(); }
84 uint16_t GetUShort() const { return Get<uint16_t>(); }
85 int32_t GetInt() const { return Get<int32_t>(); }
86 uint32_t GetUInt() const { return Get<uint32_t>(); }
87 int64_t GetXtra() const { return Get<int64_t>(); }
88 uint64_t GetUXtra() const { return Get<int64_t>(); }
89 float GetFloat() const { return Get<float>(); }
90 double GetDouble() const { return Get<double>(); }
91
92 std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
93 std::string GetString() const;
94};
95
96#endif
Note: See TracBrowser for help on using the repository browser.