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

Last change on this file since 10379 was 10371, checked in by tbretz, 14 years ago
Changed argeument to AssignFunction to a const reference.
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 /// http://www.boost.org/doc/libs/1_45_0/libs/bind/bind.html
17 boost::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
25 // Description
26 virtual void SetDescription(const std::string &) { }
27 virtual std::string GetDescription() const { return ""; }
28
29 // Function handling
30 EventImp &AssignFunction(const boost::function<int(const EventImp &)> &func) { fFunction = func; return *this; }
31 bool HasFunc() const { return !fFunction.empty(); }
32 int ExecFunc() const { return HasFunc() ? fFunction(*this) : -1; }
33
34 // Configuration helper
35 EventImp &operator()(const boost::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
39 // Print contents
40 virtual void Print(std::ostream &out) const;
41 virtual void Print() const;
42
43 // Handling of the states
44 void AddAllowedState(int state);
45 void AddAllowedStates(const char *states);
46
47 bool IsStateAllowed(int state) const;
48
49 int GetTargetState() const { return fTargetState; }
50
51 // virtual function to return the data as stored in the derived classes
52 virtual std::string GetName() const { return ""; }
53 virtual std::string GetFormat() const { return ""; }
54
55 virtual const void *GetData() const { return 0; }
56 virtual int GetSize() const { return 0; }
57
58 virtual Time GetTime() const { return Time::None; }
59
60 // Getter for all the data contained (name, format, data and time)
61 short GetShort() const { return *reinterpret_cast<const short*>(GetData()); }
62 unsigned short GetUShort() const { return *reinterpret_cast<const unsigned short*>(GetData()); }
63 int GetInt() const { return *reinterpret_cast<const int*>(GetData()); }
64 unsigned int GetUInt() const { return *reinterpret_cast<const unsigned int*>(GetData()); }
65 float GetFloat() const { return *reinterpret_cast<const float*>(GetData()); }
66 double GetDouble() const { return *reinterpret_cast<const double*>(GetData()); }
67 const char *GetText() const { return reinterpret_cast<const char*>(GetData()); }
68 std::string GetString() const { return std::string(GetText(), GetSize()); }
69 std::vector<char> GetVector() const { return std::vector<char>(GetText(), GetText()+GetSize()); }
70
71};
72
73#endif
Note: See TracBrowser for help on using the repository browser.