State.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 ArmarXCore::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 #pragma once
25 
26 
27 #include <type_traits>
28 
32 
33 #include "StateUtil.h"
34 
35 namespace armarx
36 {
37  template <class StateType>
39 
40  class RemoteState;
42 
43  class State;
45 
46  /**
47  @class State
48  @ingroup StatechartGrp
49  Class that offers the main functionality needed to create a statechart.
50  Only functions from this class and StateUtility should be used to define
51  custom statecharts.
52  */
53  class State : virtual public StateUtility
54  {
55 
56  protected:
57  State();
58  State(const State& source);
59  State& operator=(const State& source);
60  ~State() override;
61 
62  public:
63  /*! @brief Function to initialize this state. Must be called in the highest
64  * level of the hierarchy - and only there.
65  * @note init() of substates is automatically called.
66  * @param context pointer to a StatechartContext, that holds the objects to
67  * communicate with other components like observers, kinematic unit etc.
68  */
69  bool init(StatechartContextInterface* context, StatechartManager* manager);
70 
71 
72  //! Returns an id to this state, that is guaranteed to be unique in this process.
73  unsigned int getId() const;
74 
75  /**
76  * @brief Getter for the global state identifier string. It contains all parent states
77  * to the top of the statechart including remote states.
78  * Example: RobotControl->Functional->VisualServo
79  */
80  std::string getGlobalIdString() const;
81 
82 
83  /*!
84  * @brief Getter for the initial state. The initial state is automatically entered,
85  * when this state is entered.
86  * @return Shared pointer to the initial state, NULL if not set.
87  * @see setInitState()
88  */
89  StateBasePtr getInitState() const;
90 
91  bool isUnbreakable() const;
92 
93  /*!
94  * @brief getter function to get the map of output parameters
95  * @return returns the reference to this state's output parameters (the complete map)
96  * @see getOutput, setOutput, getInput, getInputParameters
97  */
99 
100  /*!
101  * @brief getOutput can be used to access a specific input parameter.<br/>
102  *
103  * This version is for complex variant types.
104  * @throw InvalidTypeException
105  * @tparam T Type of the requested variant. The return type depends on this.
106  * @param key of the parameter
107  * @return A pointer to the requested value
108  **/
109  template <typename T>
110  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
111  getOutput(const std::string& key) const;
112 
113  /*!
114  * @brief getOutput can be used to access a specific input parameter.<br/>
115  *
116  * This version is for basic variant types like int, float, bool, string.
117  * @throw InvalidTypeException
118  * @tparam T Type of the requested variant. The return type depends on this.
119  * @param key of the parameter
120  * @return A copy of the requested value.
121  **/
122  template <typename T>
123  typename std::enable_if_t<!(std::is_base_of_v<VariantDataClass, T> ||
124  std::is_base_of_v<VariantContainerBase, T>),
125  T>
126  getOutput(const std::string& key) const;
127 
128 
129  public:
131 
132  /*! @brief Function to add a new substate to this state. Should be called in the defineState()-function.
133 
134  @param derivedState Template parameter that specifies the derived state class of the added state. Must be derived from StateTemplate or class State itself if no further functionality is required.
135  @param stateName String that describes this state. Should be unique in this hierarchy level.
136  @note Can only be called in defineSubstates() (exception otherwise).
137  @returns returns a shared pointer to the created state for convenience. The state is inserted into the std::vector substateList as well.
138  */
139  template <class derivedState>
140  IceInternal::Handle<derivedState> addState(std::string const& stateName = "");
141 
142  /*! @brief Function to add a new remote substate to this state.
143  *
144  * A Remote State is only a placeholder for the real state that is
145  * located in another application. Most functioncalls to a Remote State
146  * are redirected to the state in other application. The interface is
147  * the same as to a normal state.
148  * @param stateName String that describes this state. Must exist on the
149  * specified proxy, otherwise an exception is thrown. It is the name,
150  * that was set by setStateName(stateName) or addState(stateName).
151  * @param proxyName Name of the Ice Proxy, that offers the state
152  * @note Can only be called in defineSubstates() (exception otherwise).
153  * @returns returns a shared pointer to the created state for convenience.
154  * The state is inserted into the std::vector substateList as well.
155  */
156  virtual RemoteStatePtr
157  addRemoteState(std::string stateName, std::string proxyName, std::string instanceName = "");
158 
159  /*! @brief Function to add a new dynamic remote substate to this state.
160 
161  A Dynamic Remote State
162  is only a placeholder for the real state that is located in another
163  application. Most functioncalls to a Remote State are redirected to the
164  state in other application. The interface is the same as to a normal
165  state.<br/>
166  The differene to a Remote State is, that the location of
167  the real state is unspecified until the Dynamic Remote State-onEnter()
168  function is called by the system. The location of the real state must
169  be specified in the input parameters of the state.<br>
170  So the inputparameter map must contain a field 'proxyName' and a field
171  'stateName' with the corresponding values.
172  @note Can only be called in defineSubstates() (exception otherwise).
173  @returns returns a shared pointer to the created state for convenience.
174  The state is inserted into the std::vector substateList as well.
175  */
176  virtual RemoteStatePtr addDynamicRemoteState(std::string instanceName);
177 
178 
179  TransitionIceBase&
180  addTransition(EventPtr event,
181  StateIceBasePtr sourceState,
182  StateIceBasePtr destinationState,
183  ParameterMappingIceBasePtr mappingToNextStatesInput = nullptr,
184  ParameterMappingIceBasePtr mappingToParentStatesLocal = nullptr,
185  ParameterMappingIceBasePtr mappingToParentStatesOutput = nullptr);
186 
187  /*! @brief Function to add a new transition between to substates to this state.
188  @tparam EventType Type of the event that trigers this transition.
189  @param pSourceState Shared pointer to the state, that must be the active state before this transition. OnExit() is called there.
190  @param pDestination Shared pointer to the state, in which this transition will end. OnEnter() is called there.
191  @param pMapping Mapping from the outputDictionary of pSourceState and the eventDictionary to the inputDictionary of pDestinationState
192  @note Can only be called in defineSubstates() (exception otherwise).
193 
194  @return returns reference to added transition
195  */
196  template <class EventType>
197  TransitionIceBase&
198  addTransition(StateIceBasePtr sourceState,
199  StateIceBasePtr destinationState,
200  ParameterMappingIceBasePtr mappingToNextStatesInput = nullptr,
201  ParameterMappingIceBasePtr mappingToParentStatesLocal = nullptr,
202  ParameterMappingIceBasePtr mappingToParentStatesOutput = nullptr);
203 
204  /**
205  * @brief Function to add a new transition from all substates to @p destinationState.
206  * @note Can only be called in defineSubstates() (exception otherwise).
207  *
208  */
209  template <class EventType>
210  TransitionIceBase& addTransitionFromAllStates(
211  StateIceBasePtr destinationState,
212  ParameterMappingIceBasePtr mappingToNextStatesInput = nullptr,
213  ParameterMappingIceBasePtr mappingToParentStatesLocal = nullptr,
214  ParameterMappingIceBasePtr mappingToParentStatesOutput = nullptr);
215 
216  void inheritInputFromSubstate(std::string stateName);
217 
218  /*! @brief Adds a key,type-pair to the input parameters
219  @param key String to describe the parameter
220  @param type Variable that defines the type of this parameter
221  @param optional Flag to indicate whether or not this parameter must be set, when this state is entered
222  @return returns false if the key already exists. The old type resides.
223  @note Can only be called in defineParameters() (exception otherwise).
224 
225  */
226  bool addToInput(const std::string& key,
227  const ContainerType& type,
228  bool optional,
229  VariantContainerBasePtr defaultValue = VariantContainerBasePtr());
230 
231  /*! @brief Adds a new parameter list to the input parameters with a specific type.
232  @param key String to describe the parameter
233  @param type Variable that defines the type of this parameter
234  @param optional Flag to indicate whether or not this parameter must be set, when this state is entered
235  @return returns false if the key already exists. The old type resides.
236  @note Can only be called in defineParameters() (exception otherwise).
237  */
238  bool addToInput(const std::string& key,
239  VariantTypeId type,
240  bool optional,
241  VariantPtr defaultValue = VariantPtr());
242 
243  /*! @brief Adds a key,type-pair to the local parameters
244  @param key String to describe the parameter
245  @param type Variable that defines the type of this parameter
246  @param optional Flag to indicate whether or not this parameter must be set, when this state is entered
247  @return returns false if the key already exists. The old type resides.
248  @note Can only be called in defineParameters() (exception otherwise).
249  */
250  bool addToLocal(const std::string& key,
251  VariantTypeId type,
252  VariantPtr defaultValue = VariantPtr());
253 
254  /*! @brief Adds a new parameter list to the local parameters with a specific type.
255  @param key String to describe the parameter
256  @param type Variable that defines the type of this parameter
257  @param optional Flag to indicate whether or not this parameter must be set, when this state is entered
258  @return returns false if the key already exists. The old type resides.
259  @note Can only be called in defineParameters() (exception otherwise).
260  */
261  bool addToLocal(const std::string& key,
262  const ContainerType& type,
263  VariantContainerBasePtr defaultValue = VariantContainerBasePtr());
264 
265  /*! @brief Adds a key,type-pair to the output parameters
266  @param key String to describe the parameter
267  @param type Variable that defines the type of this parameter
268  @param optional Flag to indicate whether or not this parameter must be set, when this state is entered
269  @return returns false if the key already exists. The old type resides.
270  @note Can only be called in defineParameters() (exception otherwise).
271  */
272  bool addToOutput(const std::string& key, VariantTypeId type, bool optional);
273 
274  /*! @brief Adds a new parameter list to the output parameters with a specific type.
275  @param key String to describe the parameter
276  @param type Variable that defines the type of this parameter
277  @param optional Flag to indicate whether or not this parameter must be set, when this state is entered
278  @return returns false if the key already exists. The old type resides.
279  @note Can only be called in defineParameters() (exception otherwise).
280  */
281  bool addToOutput(const std::string& key, const ContainerType& type, bool optional);
282 
283 
284  //! @brief Generates a new copy of this state with the same statename, substates, transitions, overidden functions etc.
285  StateBasePtr clone() const override;
286  //! @brief Generates a new copy of this state with the same overidden functions. stateName, substates, transition etc. are not set.
287  StateBasePtr createEmptyCopy() const override;
288 
289  //**************************
290  // Getters
291  //**************************
292 
293 
294  /** @brief Returns a new copy of the inputparameters-dictionary, so that the
295  caller cannot modify them (const won't work due to pointers).
296 
297  These values are reset immediately after this state is left (onExit/onBreak
298  was called)
299  * @return new instance of the inputparameters-dictionary
300  **/
302 
303  /*!
304  * @brief getInput can be used to access a specific input parameter.<br/>
305  *
306  * This version is for complex variant types.
307  * @throw InvalidTypeException
308  * @tparam T Type of the requested variant. The return type depends on this.
309  * @param key of the parameter
310  * @return Since the user must not be able to modify the inputparameters
311  * and constant IceSharedPointers are not possible, a copy of the requested
312  * Variant is returned.
313  **/
314  template <typename T>
315  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
316  getInput(const std::string& key) const;
317 
318  /*!
319  * @brief getInput can be used to access a specific input parameter.<br/>
320  *
321  * This version is for basic variant types like int, float, bool, string.
322  * @throw InvalidTypeException
323  * @tparam T Type of the requested variant. The return type depends on this.
324  * @param key of the parameter
325  * @return Since the user must not be able to modify the inputparameters
326  * and constant IceSharedPointers are not possible, a copy of the requested
327  * Variant is returned.
328  **/
329  template <typename T>
330  typename std::enable_if_t<!(std::is_base_of_v<VariantDataClass, T> ||
331  std::is_base_of_v<VariantContainerBase, T>),
332  T>
333  getInput(const std::string& key) const;
334 
335  /*!
336  * @brief getInput can be used to access a specific input parameter.
337  *
338  * @throw InvalidTypeException
339  * @tparam T Type of the requested variant. The return type depends on this.
340  * @param key of the parameter
341  * @return Since the user must not be able to modify the inputparameters
342  * and constant IceSharedPointers are not possible, a copy of the requested
343  * Variant is returned.
344  */
345  VariantPtr getInput(const std::string& key) const;
346 
347 
348  /*!
349  * @brief Getter for the local parameter map.
350  *
351  * @return returns a reference to the local parameters map.
352  */
354 
355  /*!
356  * @brief getLocal can be used to access a specific input parameter.<br/>
357  *
358  * This version is for complex variant types.
359  * @throw InvalidTypeException
360  * @tparam T Type of the requested variant. The return type depends on this.
361  * @param key of the parameter
362  * @return A pointer to the requested value
363  **/
364  template <typename T>
365  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
366  getLocal(const std::string& key) const;
367 
368  /*!
369  * @brief getLocal can be used to access a specific input parameter.<br/>
370  *
371  * This version is for basic variant types like int, float, bool, string.
372  * @throw InvalidTypeException
373  * @tparam T Type of the requested variant. The return type depends on this.
374  * @param key of the parameter
375  * @return A copy of the requested value.
376  **/
377  template <typename T>
378  typename std::enable_if_t<!(std::is_base_of_v<VariantDataClass, T> ||
379  std::is_base_of_v<VariantContainerBase, T>),
380  T>
381  getLocal(const std::string& key) const;
382 
383 
384  VariantContainerBasePtr getLocalContainer(std::string const& key);
385 
386 
387  /**
388  * Checks whether a given input parameter is set or not.
389  * @param key Name of the input parameter
390  * @return returns true if the given parameter is set.
391  **/
392  bool isInputParameterSet(const std::string& key) const;
393  bool isLocalParameterSet(const std::string& key) const;
394  bool isOutputParameterSet(const std::string& key) const;
395 
396  //**************************
397  // Setters
398  //**************************
399  /*!
400  * @brief setInput() sets an input parameter.
401  *
402  * @param key of the parameter
403  * @param value of the parameter of any by the Variant supported type
404  */
405  void setInput(std::string const& key, const Variant& value);
406 
407  /*!
408  * @brief setInput() sets an input parameter <b>list</b>.
409  *
410  * @param key of the parameter
411  * @param value of the parameter of any by the Variant supported type
412  */
413  void setInput(std::string const& key, const VariantContainerBase& valueList);
414 
415 
416  /*!
417  * @brief setLocal() sets a local parameter.
418  *
419  * Local means, that this parameter
420  * can be written and read in this state's onEnter(), onBreak(), onEnter() and only
421  * there.
422  *
423  * @param key of the parameter
424  * @param value of the parameter of any by the Variant supported type
425  */
426  void setLocal(std::string const& key, const Variant& value);
427 
428  /*!
429  * @brief setLocal() sets a local parameter <b>list</b>.
430  *
431  * Local means, that this parameter
432  * can be written and read in this state's onEnter(), onBreak(), onEnter() and only
433  * there.
434  *
435  * @param key of the parameter
436  * @param value of the parameter of any by the Variant supported type
437  */
438  void setLocal(std::string const& key, const VariantContainerBase& valueList);
439 
440 
441  /*!
442  * @brief setOuput() sets an output parameter of this state.<br/>
443  *
444  * Can be called in onEnter(), onBreak(), onExit(). Following states
445  * can access this parameter.
446  *
447  * @param key of the parameter
448  * @param value of the parameter of any by the Variant supported type
449  */
450  void setOutput(std::string const& key, const Variant& value);
451 
452  /*!
453  * @brief setOuput() sets an output parameter LIST of this state.<br/>
454  *
455  * Can be called in onEnter(), onBreak(), onExit(). Following states
456  * can access this parameter.
457  *
458  * @param key of the parameter
459  * @param value of the parameter of any by the Variant supported type
460  */
461  void setOutput(std::string const& key, const VariantContainerBase& valueList);
462 
463 
464  /** @brief Sets the initial substate of this state.
465  * This initial state is automatically entered, when the parent state is entered.
466  * On the implicit transition a parameter mapping from the parent's input and
467  * local parameters is done. The mapping can be specified
468  * with setInitialStateMapping()
469  * @param initState: The state that should be the initial state
470  * @param initialStateMapping: A ParameterMapping that maps *this's input to
471  * the state's input in the first parameter(use mapFromParent()). This mapping is
472  * copied and then stored.
473  *
474  * @note Can only be called in defineSubstates() (exception otherwise).
475  *
476  */
478  ParameterMappingPtr initialStateMapping = ParameterMappingPtr());
479  /*!
480  * @brief With this function the state can be made unbreakable.
481  *
482  * Unbreakable means that a parent state cannot be left, while this state is active.
483  * @param set: Set to true if the state should be made unbreakable
484  */
485  void setUnbreakable(bool setUnbreakable = true);
486 
487  /**
488  * @brief setUseRunFunction can be used to configurate whether the thread
489  * with the async. run-function should be started/used.
490  *
491  * This should be set to false in time-critical states.
492  *
493  * @see StateBase::run(), _baseRun(), _startRun()
494  * @param useRunFuntion
495  */
496  void setUseRunFunction(bool useRunFuntion);
497 
498  /**
499  * @brief Use this function in the onEnter() function, if you want to avoid
500  * that the substates (i.e. the initial state) are entered.
501  */
502  void cancelSubstates();
503 
504  void setStateName(const std::string& newName);
505 
506  void setStateClassNameFromTypeName(const std::string& typeName);
507 
508  public:
509  // /*! @brief Function to jump directly into a state. Should only be called for statemachine-debugging.
510 
511  // @param stateName Name of the target state
512  // @param inputDictionary Dictionary for the state specified by @c stateName. Content is copied to the @c inputDictionary of @c stateName.
513  // */
514  // bool JumpToState(std::string stateName, StringVariantMap inputDictionary);
515 
516  StatePtr operator[](std::string const& stateName);
517 
518  template <class EventType, class StateType>
519  friend class FinalStateBase;
520  template <typename ContextType>
521  friend class RemoteStateOfferer;
522  friend class RemoteStateWrapper;
523  template <class StateType>
524  friend class StateTemplate;
525 
526  private:
527  struct Impl;
528  std::unique_ptr<Impl> simpl;
529  };
530 
531  ///////////////////////////////////////////////////////////////
532  ////// Implementations of State
533  ///////////////////////////////////////////////////////////////
534 
535  template <>
536  StatePtr State::addState<State>(std::string const& stateName);
537 
538  template <class derivedState>
540  State::addState(std::string const& stateName)
541  {
542  // if the compiler points to the next line, something with the template parameter has been done wrong. see the message-string in the macro for further information
543  static_assert(std::is_same_v<typename derivedState::Type, derivedState>,
544  "The template parameter 'derivedState' must be derived from "
545  "StateTemplate<derivedState> or the class 'State' itself! It must not "
546  "inherit from a class that inherits from StateTemplate!");
547 
548 
549  __checkPhase(eSubstatesDefinitions, __PRETTY_FUNCTION__);
550 
551  IceInternal::Handle<derivedState> pNewState = derivedState::createInstance(stateName);
552  // stateName = pNewState->getStateName();
553 
554  addState(pNewState);
555 
556  return pNewState;
557  }
558 
559  template <class EventType>
560  TransitionIceBase&
561  State::addTransition(StateIceBasePtr sourceState,
562  StateIceBasePtr destinationState,
563  ParameterMappingIceBasePtr mappingToNextStatesInput,
564  ParameterMappingIceBasePtr mappingToParentStatesLocal,
565  ParameterMappingIceBasePtr mappingToParentStatesOutput)
566  {
567  return addTransition(createEvent<EventType>(),
568  sourceState,
569  destinationState,
570  mappingToNextStatesInput,
571  mappingToParentStatesLocal,
572  mappingToParentStatesOutput);
573  }
574 
575  template <class EventType>
576  TransitionIceBase&
577  State::addTransitionFromAllStates(StateIceBasePtr destinationState,
578  ParameterMappingIceBasePtr mappingToNextStatesInput,
579  ParameterMappingIceBasePtr mappingToParentStatesLocal,
580  ParameterMappingIceBasePtr mappingToParentStatesOutput)
581  {
582  __checkPhase(eSubstatesDefinitions, __PRETTY_FUNCTION__);
583  TransitionIceBase t;
584 
585  assert(destinationState._ptr);
586  t.evt = new EventType(EVENTTOALL);
587  t.destinationState = destinationState;
588 
589  if (mappingToNextStatesInput)
590  {
591  t.mappingToNextStatesInput = PMPtr::dynamicCast(mappingToNextStatesInput)->clone();
592  }
593 
594  if (mappingToParentStatesLocal)
595  {
596  t.mappingToParentStatesLocal = PMPtr::dynamicCast(mappingToParentStatesLocal)->clone();
597  }
598 
599  if (mappingToParentStatesOutput)
600  {
601  t.mappingToParentStatesOutput =
602  PMPtr::dynamicCast(mappingToParentStatesOutput)->clone();
603  }
604 
605  t.fromAll = true;
606 
609  "Cannot insert a general transition on event '" + t.evt->eventName +
610  "' from any state to '" + destinationState->stateName + "' into state '" +
611  getStateName() +
612  "'! There already exists a transition to that state on that event.");
613 
614  transitions.push_back(t);
615  return *transitions.rbegin();
616  }
617 
618  template <typename T>
619  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
620  State::getInput(const std::string& key) const
621  {
622  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
623  VariantContainerBasePtr varContainer;
624  getParameterContainer(inputParameters, key, varContainer);
625  if (varContainer &&
626  !SingleVariantPtr::dynamicCast(
627  varContainer)) // we are only interested in real containers, not singlevariant containers
628  {
629  return IceInternal::Handle<T>::dynamicCast(varContainer);
630  }
631  else
632  {
633  VariantPtr var;
634  getParameter(inputParameters, key, var);
635  return IceInternal::Handle<T>::dynamicCast(var->get<T>());
636  }
637  }
638 
639  template <typename T>
640  typename std::enable_if_t<!(std::is_base_of_v<VariantDataClass, T> ||
641  std::is_base_of_v<VariantContainerBase, T>),
642  T>
643  State::getInput(const std::string& key) const
644  {
645  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
646  VariantPtr var;
647  getParameter(inputParameters, key, var);
648  return var->get<T>();
649  }
650 
651  template <typename T>
652  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
653  State::getLocal(const std::string& key) const
654  {
655  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
656  VariantContainerBasePtr varContainer;
657  getParameterContainer(localParameters, key, varContainer);
658  if (varContainer &&
659  !SingleVariantPtr::dynamicCast(
660  varContainer)) // we are only interested in real containers, not singlevariant containers
661  {
662  return IceInternal::Handle<T>::dynamicCast(varContainer);
663  }
664  else
665  {
666  VariantPtr var;
667  getParameter(localParameters, key, var);
668  return IceInternal::Handle<T>::dynamicCast(var->get<T>());
669  }
670  }
671 
672  template <typename T>
673  typename std::enable_if_t<!(std::is_base_of_v<VariantDataClass, T> ||
674  std::is_base_of_v<VariantContainerBase, T>),
675  T>
676  State::getLocal(const std::string& key) const
677  {
678  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
679  VariantPtr var;
680  getParameter(localParameters, key, var);
681  return var->get<T>();
682  }
683 
684  template <typename T>
685  typename std::enable_if_t<std::is_base_of_v<VariantDataClass, T>, IceInternal::Handle<T>>
686  State::getOutput(const std::string& key) const
687  {
688  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
689  VariantContainerBasePtr varContainer;
690  getParameterContainer(outputParameters, key, varContainer);
691  if (varContainer &&
692  !SingleVariantPtr::dynamicCast(
693  varContainer)) // we are only interested in real containers, not singlevariant containers
694  {
695  return IceInternal::Handle<T>::dynamicCast(varContainer);
696  }
697  else
698  {
699  VariantPtr var;
700  getParameter(outputParameters, key, var);
701  return IceInternal::Handle<T>::dynamicCast(var->get<T>());
702  }
703  }
704 
705  template <typename T>
706  typename std::enable_if_t<!(std::is_base_of_v<VariantDataClass, T> ||
707  std::is_base_of_v<VariantContainerBase, T>),
708  T>
709  State::getOutput(const std::string& key) const
710  {
711  __checkPhaseMin(eDefined, __PRETTY_FUNCTION__);
712  VariantPtr var;
713  getParameter(outputParameters, key, var);
714  return var->get<T>();
715  }
716 
717 
718 } // namespace armarx
armarx::State::isOutputParameterSet
bool isOutputParameterSet(const std::string &key) const
Definition: State.cpp:548
armarx::StateBase::getParameterContainer
void getParameterContainer(const StateParameterMap &paramMap, const std::string &key, VariantContainerBasePtr &valueContainer) const
Definition: StateBase.cpp:768
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:223
armarx::State::getOutputParameters
StateParameterMap & getOutputParameters() override
getter function to get the map of output parameters
Definition: State.cpp:119
armarx::State::Impl
Definition: State.cpp:50
SingleTypeVariantList.h
armarx::FinalStateBase
Definition: FinalState.h:48
armarx::RemoteStateWrapper
Definition: RemoteStateWrapper.h:50
armarx::State::isUnbreakable
bool isUnbreakable() const
Definition: State.cpp:113
armarx::StateTemplate
Definition: State.h:38
armarx::State::setOutput
void setOutput(std::string const &key, const Variant &value)
setOuput() sets an output parameter of this state.
Definition: State.cpp:482
armarx::State::setUseRunFunction
void setUseRunFunction(bool useRunFuntion)
setUseRunFunction can be used to configurate whether the thread with the async.
Definition: State.cpp:381
armarx::State::getInput
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getInput(const std::string &key) const
getInput can be used to access a specific input parameter.
Definition: State.h:620
armarx::StateBase::__checkPhaseMin
void __checkPhaseMin(StatePhase allowedType, const char *functionName) const
Definition: StateBase.cpp:230
armarx::ParameterMappingPtr
IceInternal::Handle< ParameterMapping > ParameterMappingPtr
Definition: ParameterMapping.h:41
armarx::State::~State
~State() override
Definition: State.cpp:82
armarx::StateBase::getStateName
std::string getStateName() const
getStateName
Definition: StateBase.cpp:545
armarx::StatechartContextInterface
Definition: StatechartContextInterface.h:45
armarx::State::init
bool init(StatechartContextInterface *context, StatechartManager *manager)
Function to initialize this state. Must be called in the highest level of the hierarchy - and only th...
Definition: State.cpp:87
armarx::State::setLocal
void setLocal(std::string const &key, const Variant &value)
setLocal() sets a local parameter.
Definition: State.cpp:464
armarx::exceptions::local::eStatechartLogicError
Definition: Exception.h:30
armarx::StateBase::eDefined
@ eDefined
Definition: StateBase.h:301
armarx::State::setStateName
void setStateName(const std::string &newName)
Definition: State.cpp:394
armarx::State::isInputParameterSet
bool isInputParameterSet(const std::string &key) const
Checks whether a given input parameter is set or not.
Definition: State.cpp:536
armarx::State::addState
StateBasePtr addState(StateBasePtr pNewState)
Definition: State.cpp:140
armarx::State::getGlobalIdString
std::string getGlobalIdString() const
Getter for the global state identifier string.
Definition: State.cpp:101
armarx::State::createEmptyCopy
StateBasePtr createEmptyCopy() const override
Generates a new copy of this state with the same overidden functions. stateName, substates,...
Definition: State.cpp:508
armarx::StateController::__checkExistenceOfTransition
bool __checkExistenceOfTransition(const TransitionIceBase &transition)
Definition: StateController.cpp:680
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::State::cancelSubstates
void cancelSubstates()
Use this function in the onEnter() function, if you want to avoid that the substates (i....
Definition: State.cpp:387
StringValueMap.h
armarx::VariantPtr
IceInternal::Handle< Variant > VariantPtr
Definition: Variant.h:41
armarx::State::setInitState
StateBasePtr setInitState(StateBasePtr initState, ParameterMappingPtr initialStateMapping=ParameterMappingPtr())
Sets the initial substate of this state.
Definition: State.cpp:354
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::RemoteStateOfferer
Class that holds states, which offer functionality for other states over Ice.
Definition: RemoteStateOfferer.h:203
armarx::State::getLocal
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getLocal(const std::string &key) const
getLocal can be used to access a specific input parameter.
Definition: State.h:653
armarx::State::addToLocal
bool addToLocal(const std::string &key, VariantTypeId type, VariantPtr defaultValue=VariantPtr())
Adds a key,type-pair to the local parameters.
Definition: State.cpp:345
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::State::getLocalParameters
StateParameterMap & getLocalParameters()
Getter for the local parameter map.
Definition: State.cpp:428
armarx::State::setUnbreakable
void setUnbreakable(bool setUnbreakable=true)
With this function the state can be made unbreakable.
Definition: State.cpp:374
armarx::State::addTransition
TransitionIceBase & addTransition(EventPtr event, StateIceBasePtr sourceState, StateIceBasePtr destinationState, ParameterMappingIceBasePtr mappingToNextStatesInput=nullptr, ParameterMappingIceBasePtr mappingToParentStatesLocal=nullptr, ParameterMappingIceBasePtr mappingToParentStatesOutput=nullptr)
Definition: State.cpp:250
magic_enum::detail::enable_if_t
typename enable_if_enum< std::is_enum_v< D > &&std::is_invocable_r_v< bool, BinaryPredicate, char_type, char_type >, R >::type enable_if_t
Definition: magic_enum.hpp:860
armarx::State::State
State()
Definition: State.cpp:57
armarx::RemoteState
This Statetype is used to create a state instance that represents a state that is located in another ...
Definition: RemoteState.h:62
armarx::State::addToInput
bool addToInput(const std::string &key, const ContainerType &type, bool optional, VariantContainerBasePtr defaultValue=VariantContainerBasePtr())
Adds a key,type-pair to the input parameters.
Definition: State.cpp:309
armarx::State::getOutput
std::enable_if_t< std::is_base_of_v< VariantDataClass, T >, IceInternal::Handle< T > > getOutput(const std::string &key) const
getOutput can be used to access a specific input parameter.
Definition: State.h:686
armarx::State::clone
StateBasePtr clone() const override
Generates a new copy of this state with the same statename, substates, transitions,...
Definition: State.cpp:500
armarx::State::getLocalContainer
VariantContainerBasePtr getLocalContainer(std::string const &key)
Definition: State.cpp:525
armarx::VariantTypeId
Ice::Int VariantTypeId
Definition: Variant.h:43
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::State
Definition: State.h:53
armarx::State::inheritInputFromSubstate
void inheritInputFromSubstate(std::string stateName)
Definition: State.cpp:292
armarx::StateUtility
Definition: StateUtil.h:46
armarx::StateBase::eSubstatesDefinitions
@ eSubstatesDefinitions
Definition: StateBase.h:299
armarx::State::addTransitionFromAllStates
TransitionIceBase & addTransitionFromAllStates(StateIceBasePtr destinationState, ParameterMappingIceBasePtr mappingToNextStatesInput=nullptr, ParameterMappingIceBasePtr mappingToParentStatesLocal=nullptr, ParameterMappingIceBasePtr mappingToParentStatesOutput=nullptr)
Function to add a new transition from all substates to destinationState.
Definition: State.h:577
armarx::State::addDynamicRemoteState
virtual RemoteStatePtr addDynamicRemoteState(std::string instanceName)
Function to add a new dynamic remote substate to this state.
Definition: State.cpp:213
armarx::State::getId
unsigned int getId() const
Returns an id to this state, that is guaranteed to be unique in this process.
Definition: State.cpp:95
armarx::State::setStateClassNameFromTypeName
void setStateClassNameFromTypeName(const std::string &typeName)
Definition: State.cpp:400
StateUtil.h
armarx::State::addRemoteState
virtual RemoteStatePtr addRemoteState(std::string stateName, std::string proxyName, std::string instanceName="")
Function to add a new remote substate to this state.
Definition: State.cpp:158
armarx::State::addToOutput
bool addToOutput(const std::string &key, VariantTypeId type, bool optional)
Adds a key,type-pair to the output parameters.
Definition: State.cpp:319
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx::State::getInitState
StateBasePtr getInitState() const
Getter for the initial state. The initial state is automatically entered, when this state is entered.
Definition: State.cpp:107
armarx::State::operator=
State & operator=(const State &source)
Definition: State.cpp:74
armarx::State::getInputParameters
StateParameterMap getInputParameters() override
Returns a new copy of the inputparameters-dictionary, so that the caller cannot modify them (const wo...
Definition: State.cpp:407
Variant.h
armarx::State::setInput
void setInput(std::string const &key, const Variant &value)
setInput() sets an input parameter.
Definition: State.cpp:434
armarx::StateBase::__checkPhase
void __checkPhase(StatePhase allowedType, const char *functionName) const
Helper function for checking if a function was called in valid position of the statechart.
Definition: StateBase.cpp:136
armarx::State::isLocalParameterSet
bool isLocalParameterSet(const std::string &key) const
Definition: State.cpp:542
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::StatechartManager
Definition: StatechartManager.h:40
armarx::StateBase::getParameter
void getParameter(const StateParameterMap &paramMap, const std::string &key, VariantPtr &value) const
Definition: StateBase.cpp:876
EVENTTOALL
#define EVENTTOALL
Definition: StateBase.h:42