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

Last change on this file since 14558 was 14005, checked in by tbretz, 12 years ago
replaced more 'const char *' by std::string&
File size: 3.6 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 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 std::function<int(const EventImp &)> fFunction;
17
18public:
19 /// Constructor. Stores the target state given.
20 EventImp() { }
21 /// Copy constructor
22 EventImp(const EventImp &cmd);
23 virtual ~EventImp() {}
24
25 // Description
26 virtual void SetDescription(const std::string &) { }
27 virtual std::string GetDescription() const { return ""; }
28
29 // Function handling
30 EventImp &AssignFunction(const std::function<int(const EventImp &)> &func=std::function<int(const EventImp &)>()) { fFunction = func; return *this; }
31 bool HasFunc() const { return (bool)fFunction; }
32 int ExecFunc() const { return fFunction ? fFunction(*this) : -1; }
33
34 // Configuration helper
35 EventImp &operator()(const std::function<int(const EventImp &)> &func) { return AssignFunction(func); }
36 EventImp &operator()(const std::string str) { SetDescription(str); return *this; }
37 EventImp &operator()(const char *str) { SetDescription(str); return *this; }
38 EventImp &operator()(int state) { fAllowedStates.push_back(state); return *this; }
39
40 // Print contents
41 virtual void Print(std::ostream &out, bool strip=false) const;
42 virtual void Print(bool strip=false) const;
43
44 // Handling of the states
45 void AddAllowedState(int state);
46 void AddAllowedStates(const std::string &states);
47
48 bool IsStateAllowed(int state) const;
49
50 // virtual function to return the data as stored in the derived classes
51 virtual std::string GetName() const { return ""; }
52 virtual std::string GetFormat() const { return ""; }
53
54 virtual const void *GetData() const { return 0; }
55 virtual size_t GetSize() const { return 0; }
56
57 virtual Time GetTime() const { return Time::None; }
58 virtual int GetQoS() const { return 0; }
59
60 std::string GetTimeAsStr(const char *fmt) const;
61 uint64_t GetJavaDate() const;
62
63 // Generalized access operators
64 template<typename T>
65 T Get(size_t 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(size_t 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 template<typename T>
81 const T &Ref(size_t offset=0) const
82 {
83 return *Ptr<T>(offset);
84 }
85
86 // Getter for all the data contained (name, format, data and time)
87 const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
88
89 bool GetBool() const { return Get<uint8_t>()!=0; }
90 int16_t GetShort() const { return Get<int16_t>(); }
91 uint16_t GetUShort() const { return Get<uint16_t>(); }
92 int32_t GetInt() const { return Get<int32_t>(); }
93 uint32_t GetUInt() const { return Get<uint32_t>(); }
94 int64_t GetXtra() const { return Get<int64_t>(); }
95 uint64_t GetUXtra() const { return Get<int64_t>(); }
96 float GetFloat() const { return Get<float>(); }
97 double GetDouble() const { return Get<double>(); }
98
99 std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
100 std::string GetString() const;
101};
102
103#endif
Note: See TracBrowser for help on using the repository browser.