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 
29 
30 
31 #include "StateController.h"
32 #include "StateBase.h"
33 #include "State.h"
34 
35 #include <condition_variable>
36 #include <mutex>
37 #include <list>
38 
39 
40 namespace armarx
41 {
43  virtual public IceUtil::Shared,
44  virtual public Logging
45  {
46  public:
48  ~StatechartManager() override;
49 
50  /**
51  * @brief setToplevelState sets the toplevel state of this manager.<br/>
52  *
53  * This state will be started in start().
54  *
55  * @see start()
56  * @param newToplevelState Pointer to the toplevel state
57  * @param startParameters Parameters that should be given to the toplevel
58  * state on startup.
59  * @return returns true if the toplevelstate was successfully set.
60  * Returns false if the StatechartManager is already running.
61  */
62  bool setToplevelState(const armarx::StatePtr& newToplevelState, StringVariantContainerBaseMap startParameters = StringVariantContainerBaseMap());
63  StatePtr getToplevelState() const;
64 
66  {
70  };
71 
72  void start(bool enterToplevelState = true);
73  void shutdown();
74 
75  bool isRunning() const;
76  bool isShutdown() const;
78 
79  /**
80  * @brief wakeUp signals the event processing thread to wake up and
81  * check for new events or shutdown conditions
82  */
83  void wakeUp();
84 
85  bool addEvent(const EventPtr& newEvent, const StateControllerPtr& receivingState);
86  template <typename EventType>
87  bool 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  private:
96  struct EventProcessingData
97  {
98  EventPtr event;
99  StateControllerPtr receivingState;
100  };
101 
102  bool checkEvent(const EventProcessingData& eventData) const;
103 
105 
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 }
armarx::StatechartManager::addEvent
bool addEvent(const StateControllerPtr &receivingState)
Definition: StatechartManager.h:87
armarx::StatechartManager::processEvents
virtual void processEvents()
Definition: StatechartManager.cpp:210
armarx::StatechartManager::getToplevelState
StatePtr getToplevelState() const
Definition: StatechartManager.cpp:59
armarx::StatechartManager::eShutDown
@ eShutDown
Definition: StatechartManager.h:69
armarx::StatechartManager::eConstructed
@ eConstructed
Definition: StatechartManager.h:67
armarx::StatechartManager::getManagerState
StatechartManagerState getManagerState() const
Definition: StatechartManager.cpp:165
armarx::StatechartManager::eRunning
@ eRunning
Definition: StatechartManager.h:68
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:171
armarx::StatechartManager::StatechartManager
StatechartManager()
Definition: StatechartManager.cpp:34
IceInternal::Handle< State >
StateController.h
armarx::StatechartManager::StatechartManagerState
StatechartManagerState
Definition: StatechartManager.h:65
armarx::StatechartManager::~StatechartManager
~StatechartManager() override
Definition: StatechartManager.cpp:42
State.h
armarx::StatechartManager::isShutdown
bool isShutdown() const
Definition: StatechartManager.cpp:153
armarx::StatechartManager::shutdown
void shutdown()
Definition: StatechartManager.cpp:98
Synchronization.h
armarx::StatechartManager::start
void start(bool enterToplevelState=true)
Definition: StatechartManager.cpp:64
armarx::StatechartManager::addEvent
bool addEvent(const EventPtr &newEvent, const StateControllerPtr &receivingState)
Definition: StatechartManager.cpp:176
StateBase.h
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
IceUtil::Handle< StatechartManager >
armarx::RunningTask::pointer_type
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
Definition: RunningTask.h:178
armarx::StatechartManager::setToplevelState
bool setToplevelState(const armarx::StatePtr &newToplevelState, StringVariantContainerBaseMap startParameters=StringVariantContainerBaseMap())
setToplevelState sets the toplevel state of this manager.
Definition: StatechartManager.cpp:47
armarx::StatechartManager::isRunning
bool isRunning() const
Definition: StatechartManager.cpp:141
armarx::StatechartManagerPtr
IceUtil::Handle< StatechartManager > StatechartManagerPtr
Definition: StatechartContext.h:50
armarx::StatePtr
IceInternal::Handle< State > StatePtr
Definition: State.h:45
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StatechartManager
Definition: StatechartManager.h:42