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

Last change on this file since 16736 was 14564, checked in by tbretz, 12 years ago
Keep the info whether an event was empty or just zero size.
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 virtual bool IsEmpty() const { return GetData()==0; }
60
61 std::string GetTimeAsStr(const char *fmt) const;
62 uint64_t GetJavaDate() const;
63
64 // Generalized access operators
65 template<typename T>
66 T Get(size_t offset=0) const
67 {
68 if (offset>=GetSize())
69 throw std::logic_error("EventImp::Get - offset out of range.");
70 return *reinterpret_cast<const T*>(GetText()+offset);
71 }
72
73 template<typename T>
74 const T *Ptr(size_t offset=0) const
75 {
76 if (offset>=GetSize())
77 throw std::logic_error("EventImp::Ptr - offset out of range.");
78 return reinterpret_cast<const T*>(GetText()+offset);
79 }
80
81 template<typename T>
82 const T &Ref(size_t offset=0) const
83 {
84 return *Ptr<T>(offset);
85 }
86
87 // Getter for all the data contained (name, format, data and time)
88 const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
89
90 bool GetBool() const { return Get<uint8_t>()!=0; }
91 int16_t GetShort() const { return Get<int16_t>(); }
92 uint16_t GetUShort() const { return Get<uint16_t>(); }
93 int32_t GetInt() const { return Get<int32_t>(); }
94 uint32_t GetUInt() const { return Get<uint32_t>(); }
95 int64_t GetXtra() const { return Get<int64_t>(); }
96 uint64_t GetUXtra() const { return Get<int64_t>(); }
97 float GetFloat() const { return Get<float>(); }
98 double GetDouble() const { return Get<double>(); }
99
100 std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
101 std::string GetString() const;
102};
103
104#endif
Note: See TracBrowser for help on using the repository browser.