StateTemplate.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::
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2014
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 #pragma once
25 
26 #include "State.h"
27 
28 #include <type_traits>
29 
30 namespace armarx
31 {
32 
33  /**
34  @class StateTemplate
35  @ingroup StatechartGrp
36  Template class from which all states with additional functionality like an overriden onEnter() function must be derived.
37  */
38  template <class StateType>
39  class StateTemplate :
40  virtual public State
41  {
42 
43  protected:
44  StateTemplate();
45  ~StateTemplate() override {}
46  public:
47  // this typedef is for later checking if the inheritance is correct (e.g. in addState)
48  using Type = StateType;
49  /*!
50  * @brief Creates a new state instance of the type of the template parameter.
51  * @param The name the new state should have.
52  * @return New state instance
53  */
54  static IceInternal::Handle<StateType> createInstance(std::string stateName = "");
55 
56  /*!
57  * @brief Creates a copy of this state.
58  *
59  * All substates and parameters are cloned as well.
60  * @return New state instance
61  */
62  StateBasePtr clone() const override;
63 
64  // /*!
65  // * @brief Creates a copy of this state.
66  // * @return New state instance
67  // */
68  // StateBasePtr createEmptyCopy() const override;
69  };
70 
71 
72 
73  ///////////////////////////////////////////////////////////////
74  ////// Implementations of StateTemplate
75  ///////////////////////////////////////////////////////////////
76 
77  template <class StateType>
79  {
80  static_assert(std::is_base_of_v<StateTemplate, StateType>,
81  "The template parameter of StateTemplate, must be a class that derives from StateTemplate");
82 
83  // get the class name of this state
84  std::string className = GetTypeString<StateType>();
85 
86  if (className.size() > 1)
87  {
88  className = className.substr(0, className.size() - 1);
89  }
90 
91  setStateClassNameFromTypeName(className);
92  }
93 
94  template <class StateType>
96  {
97  IceInternal::Handle<StateType> ptr = new StateType();
98 
99  if (stateName.empty())
100  {
101  if (ptr->stateClassName.empty())
102  {
103  throw LocalException("StateName and StateClassName are empty - cannot create state instance");
104  }
105 
106  stateName = ptr->stateClassName;
107  }
108 
109  ptr->setTag(stateName);
110  ptr->stateName = stateName;
111  return ptr;
112  }
113 
114  template <class StateType>
116  {
117  StatePtr result = new StateType(*dynamic_cast<const StateType*>(this));
118  return result;
119  }
120 
121 }
armarx::StateTemplate
Definition: State.h:39
armarx::StateTemplate::clone
StateBasePtr clone() const override
Creates a copy of this state.
Definition: StateTemplate.h:115
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::StateTemplate< StatechartClient >::Type
StateType Type
Definition: StateTemplate.h:48
State.h
KITProsthesis::ProsthesisState::State
State
Definition: KITProstheticHandInterface.ice:32
armarx::StateTemplate::~StateTemplate
~StateTemplate() override
Definition: StateTemplate.h:45
armarx::State
Definition: State.h:54
armarx::StateTemplate::StateTemplate
StateTemplate()
Definition: StateTemplate.h:78
armarx::StateTemplate::createInstance
static IceInternal::Handle< StateType > createInstance(std::string stateName="")
Creates a new state instance of the type of the template parameter.
Definition: StateTemplate.h:95
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28