otsdaq  v2_05_02_indev
FiniteStateMachine.h
1 #ifndef _ots_FiniteStateMachine_h
2 #define _ots_FiniteStateMachine_h
3 
4 #include <toolbox/fsm/FiniteStateMachine.h>
5 #include <xoap/MessageReference.h>
6 
7 namespace ots
8 {
9 class FiniteStateMachine : public toolbox::fsm::FiniteStateMachine
10 {
11  public:
12  FiniteStateMachine(const std::string& stateMachineName);
13  ~FiniteStateMachine(void);
14 
15  using toolbox::fsm::FiniteStateMachine::addStateTransition;
16 
17  template<class OBJECT>
18  void addStateTransition(toolbox::fsm::State from,
19  toolbox::fsm::State to,
20  const std::string& input,
21  const std::string& transitionName,
22  OBJECT* obj,
23  void (OBJECT::*func)(toolbox::Event::Reference))
24 
25  {
26  stateTransitionNameTable_[from][input] = transitionName;
27  toolbox::fsm::FiniteStateMachine::addStateTransition(from, to, input, obj, func);
28  }
29 
30  template<class OBJECT>
31  void addStateTransition(toolbox::fsm::State from,
32  toolbox::fsm::State to,
33  const std::string& input,
34  const std::string& transitionName,
35  const std::string& transitionParameter,
36  OBJECT* obj,
37  void (OBJECT::*func)(toolbox::Event::Reference))
38 
39  {
40  stateTransitionParameterTable_[from][input] = transitionParameter;
41  addStateTransition(from, to, input, transitionName, obj, func);
42  }
43 
44  toolbox::fsm::State getProvenanceState(void);
45  toolbox::fsm::State getTransitionFinalState(const std::string& transition);
46 
47  std::string getProvenanceStateName(void);
48  std::string getCurrentStateName(void);
49  time_t getTimeInState(void);
50  std::string getCurrentTransitionName(const std::string& transition);
51  std::string getTransitionName(const toolbox::fsm::State from, const std::string& transition);
52  std::string getTransitionParameter(const toolbox::fsm::State from, const std::string& transition);
53  std::string getTransitionFinalStateName(const std::string& transition);
54  const std::string& getErrorMessage() const;
55  const std::string& getStateMachineName(void) const { return stateMachineName_; }
56  void setStateMachineName(const std::string& name) { stateMachineName_ = name; }
57 
58  const xoap::MessageReference& getCurrentMessage(void);
59 
60  bool execTransition(const std::string& transition);
61  bool execTransition(const std::string& transition, const xoap::MessageReference& message);
62  bool isInTransition(void);
63  void setInitialState(toolbox::fsm::State state);
64  void setErrorMessage(const std::string& errMessage, bool append = true);
65 
66  protected:
67  time_t stateEntranceTime_;
68 
69  // The volatile keyword indicates that a field might be modified by multiple
70  // concurrently executing threads. Fields that are declared volatile are not subject
71  // to compiler optimizations that assume access by a single thread. This ensures that
72  // the most up-to-date value is present in the field at all times. If you don't mark
73  // it volatile, the generated code might optimize the value into a registry and your
74  // thread will never see the change The atomicity has nothing to do with the
75  // visibility between threads... just because an operation is executed in one CPU
76  // cycle (atomic) it doesn't mean that the result will be visible to the other threads
77  // unless the value is marked volatile
78  volatile bool inTransition_;
79  toolbox::fsm::State provenanceState_;
80  std::map<toolbox::fsm::State, std::map<std::string, std::string, std::less<std::string> > > stateTransitionNameTable_;
81  std::map<toolbox::fsm::State, std::map<std::string, std::string, std::less<std::string> > > stateTransitionParameterTable_;
82 
83  xoap::MessageReference theMessage_;
84  std::string theErrorMessage_;
85  std::string stateMachineName_;
86 
87  private:
88 };
89 
90 } // namespace ots
91 
92 #endif