StatechartManager.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::Core::Statechart
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <condition_variable>
28 #include <list>
29 #include <mutex>
30 
33 
34 #include "State.h"
35 #include "StateBase.h"
36 #include "StateController.h"
37 
38 namespace armarx
39 {
40  class StatechartManager : virtual public IceUtil::Shared, virtual public Logging
41  {
42  public:
44  ~StatechartManager() override;
45 
46  /**
47  * @brief setToplevelState sets the toplevel state of this manager.<br/>
48  *
49  * This state will be started in start().
50  *
51  * @see start()
52  * @param newToplevelState Pointer to the toplevel state
53  * @param startParameters Parameters that should be given to the toplevel
54  * state on startup.
55  * @return returns true if the toplevelstate was successfully set.
56  * Returns false if the StatechartManager is already running.
57  */
58  bool setToplevelState(
59  const armarx::StatePtr& newToplevelState,
60  StringVariantContainerBaseMap startParameters = StringVariantContainerBaseMap());
61  StatePtr getToplevelState() const;
62 
64  {
68  };
69 
70  void start(bool enterToplevelState = true);
71  void shutdown();
72 
73  bool isRunning() const;
74  bool isShutdown() const;
76 
77  /**
78  * @brief wakeUp signals the event processing thread to wake up and
79  * check for new events or shutdown conditions
80  */
81  void wakeUp();
82 
83  bool addEvent(const EventPtr& newEvent, const StateControllerPtr& receivingState);
84 
85  template <typename EventType>
86  bool
87  addEvent(const StateControllerPtr& receivingState)
88  {
89  EventPtr event = new EventType(StateIceBasePtr::dynamicCast(receivingState)->stateName);
90  return addEvent(event, receivingState->__getParentState());
91  }
92 
93  protected:
94  virtual void processEvents();
95 
96  private:
97  struct EventProcessingData
98  {
99  EventPtr event;
100  StateControllerPtr receivingState;
101  };
102 
103  bool checkEvent(const EventProcessingData& eventData) const;
104 
106 
107 
108  std::list<EventProcessingData> eventQueue;
109 
110  mutable std::mutex eventQueueMutex;
111  std::condition_variable idleCondition;
112 
113 
114  StatechartManagerState managerState;
115  mutable HiddenTimedMutex managerStateMutex;
116 
117 
118  StatePtr toplevelState;
119  StringVariantContainerBaseMap startParameters;
120  };
121 
123 
124 } // namespace armarx
armarx::StatechartManager::addEvent
bool addEvent(const StateControllerPtr &receivingState)
Definition: StatechartManager.h:87
armarx::StatechartManager::processEvents
virtual void processEvents()
Definition: StatechartManager.cpp:218
armarx::StatechartManager::getToplevelState
StatePtr getToplevelState() const
Definition: StatechartManager.cpp:62
armarx::StatechartManager::eShutDown
@ eShutDown
Definition: StatechartManager.h:67
armarx::StatechartManager::eConstructed
@ eConstructed
Definition: StatechartManager.h:65
armarx::StatechartManager::getManagerState
StatechartManagerState getManagerState() const
Definition: StatechartManager.cpp:172
armarx::StatechartManager::eRunning
@ eRunning
Definition: StatechartManager.h:66
RunningTask.h
armarx::StatechartManager::wakeUp
void wakeUp()
wakeUp signals the event processing thread to wake up and check for new events or shutdown conditions
Definition: StatechartManager.cpp:179
armarx::StatechartManager::StatechartManager
StatechartManager()
Definition: StatechartManager.cpp:34
IceInternal::Handle< State >
StateController.h
armarx::StatechartManager::StatechartManagerState
StatechartManagerState
Definition: StatechartManager.h:63
armarx::StatechartManager::~StatechartManager
~StatechartManager() override
Definition: StatechartManager.cpp:42
State.h
armarx::StatechartManager::isShutdown
bool isShutdown() const
Definition: StatechartManager.cpp:159
armarx::StatechartManager::shutdown
void shutdown()
Definition: StatechartManager.cpp:102
Synchronization.h
armarx::StatechartManager::start
void start(bool enterToplevelState=true)
Definition: StatechartManager.cpp:68
armarx::StatechartManager::addEvent
bool addEvent(const EventPtr &newEvent, const StateControllerPtr &receivingState)
Definition: StatechartManager.cpp:185
StateBase.h
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:239
IceUtil::Handle< StatechartManager >
armarx::RunningTask::pointer_type
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
Definition: RunningTask.h:174
armarx::StatechartManager::setToplevelState
bool setToplevelState(const armarx::StatePtr &newToplevelState, StringVariantContainerBaseMap startParameters=StringVariantContainerBaseMap())
setToplevelState sets the toplevel state of this manager.
Definition: StatechartManager.cpp:48
armarx::StatechartManager::isRunning
bool isRunning() const
Definition: StatechartManager.cpp:146
armarx::StatechartManagerPtr
IceUtil::Handle< StatechartManager > StatechartManagerPtr
Definition: StatechartContext.h:49
armarx::StatePtr
IceInternal::Handle< State > StatePtr
Definition: State.h:44
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::StatechartManager
Definition: StatechartManager.h:40