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 <type_traits>
27 
28 #include "State.h"
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 : virtual public State
40  {
41 
42  protected:
43  StateTemplate();
44 
45  ~StateTemplate() override
46  {
47  }
48 
49  public:
50  // this typedef is for later checking if the inheritance is correct (e.g. in addState)
51  using Type = StateType;
52  /*!
53  * @brief Creates a new state instance of the type of the template parameter.
54  * @param The name the new state should have.
55  * @return New state instance
56  */
57  static IceInternal::Handle<StateType> createInstance(std::string stateName = "");
58 
59  /*!
60  * @brief Creates a copy of this state.
61  *
62  * All substates and parameters are cloned as well.
63  * @return New state instance
64  */
65  StateBasePtr clone() const override;
66 
67  // /*!
68  // * @brief Creates a copy of this state.
69  // * @return New state instance
70  // */
71  // StateBasePtr createEmptyCopy() const override;
72  };
73 
74  ///////////////////////////////////////////////////////////////
75  ////// Implementations of StateTemplate
76  ///////////////////////////////////////////////////////////////
77 
78  template <class StateType>
80  {
81  static_assert(std::is_base_of_v<StateTemplate, StateType>,
82  "The template parameter of StateTemplate, must be a class that derives from "
83  "StateTemplate");
84 
85  // get the class name of this state
86  std::string className = GetTypeString<StateType>();
87 
88  if (className.size() > 1)
89  {
90  className = className.substr(0, className.size() - 1);
91  }
92 
93  setStateClassNameFromTypeName(className);
94  }
95 
96  template <class StateType>
99  {
100  IceInternal::Handle<StateType> ptr = new StateType();
101 
102  if (stateName.empty())
103  {
104  if (ptr->stateClassName.empty())
105  {
106  throw LocalException(
107  "StateName and StateClassName are empty - cannot create state instance");
108  }
109 
110  stateName = ptr->stateClassName;
111  }
112 
113  ptr->setTag(stateName);
114  ptr->stateName = stateName;
115  return ptr;
116  }
117 
118  template <class StateType>
121  {
122  StatePtr result = new StateType(*dynamic_cast<const StateType*>(this));
123  return result;
124  }
125 
126 } // namespace armarx
armarx::StateTemplate
Definition: State.h:38
armarx::StateTemplate::clone
StateBasePtr clone() const override
Creates a copy of this state.
Definition: StateTemplate.h:120
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::StateTemplate< StatechartClient >::Type
StateType Type
Definition: StateTemplate.h:51
State.h
KITProsthesis::ProsthesisState::State
State
Definition: KITProstheticHandInterface.ice:32
armarx::StateTemplate::~StateTemplate
~StateTemplate() override
Definition: StateTemplate.h:45
armarx::State
Definition: State.h:53
armarx::StateTemplate::StateTemplate
StateTemplate()
Definition: StateTemplate.h:79
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:98
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27