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
38namespace 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 */
59 const armarx::StatePtr& newToplevelState,
60 StringVariantContainerBaseMap startParameters = StringVariantContainerBaseMap());
62
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
122 using StatechartManagerPtr = IceUtil::Handle<StatechartManager>;
123
124} // namespace armarx
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
void wakeUp()
wakeUp signals the event processing thread to wake up and check for new events or shutdown conditions
void start(bool enterToplevelState=true)
bool addEvent(const StateControllerPtr &receivingState)
bool setToplevelState(const armarx::StatePtr &newToplevelState, StringVariantContainerBaseMap startParameters=StringVariantContainerBaseMap())
setToplevelState sets the toplevel state of this manager.
bool addEvent(const EventPtr &newEvent, const StateControllerPtr &receivingState)
StatechartManagerState getManagerState() const
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< State > StatePtr
Definition State.h:44
IceInternal::Handle< Event > EventPtr
Typedef of EventPtr as IceInternal::Handle<Event> for convenience.
Definition Event.h:40
IceUtil::Handle< StatechartManager > StatechartManagerPtr
IceInternal::Handle< StateController > StateControllerPtr