NJointControllerRegistry.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::NJointController
17  * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
18  * @date 2017
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
28 
29 #include "NJointControllerBase.h"
30 
32 {
33  class ControllerManagement;
34 }
35 
36 namespace armarx
37 {
38  RobotUnit* getRobotUnit(RobotUnitModule::ControllerManagement* cmgr);
39 
41  {
42  private:
44  virtual NJointControllerBasePtr create(RobotUnitModule::ControllerManagement* cmngr,
45  const NJointControllerConfigPtr&,
47  bool deletable,
48  bool internal,
49  const std::string& instanceName) const = 0;
51  GenerateConfigDescription(const VirtualRobot::RobotPtr&,
52  const std::map<std::string, ConstControlDevicePtr>&,
53  const std::map<std::string, ConstSensorDevicePtr>&) const = 0;
54  virtual NJointControllerConfigPtr
55  GenerateConfigFromVariants(const StringVariantBaseMap&) const = 0;
56  virtual bool hasRemoteConfiguration() const = 0;
57 
58  protected:
59  static thread_local bool ConstructorIsRunning_;
60 
61  public:
62  virtual ~NJointControllerRegistryEntry() = default;
63 
64  static bool
66  {
67  return ConstructorIsRunning_;
68  }
69  };
70 
72 
73  template <class ControllerType>
75 
76  namespace detail
77  {
79  hasGenerateConfigDescription,
80  GenerateConfigDescription,
83  hasGenerateConfigFromVariants,
84  GenerateConfigFromVariants,
86  typename T::ConfigPtrT>);
87 
88  template <class NJointControllerT>
89  class NJointControllerRegistryEntryHelper : public NJointControllerRegistryEntry
90  {
93  "Either overload both GenerateConfigDescription and "
94  "GenerateConfigFromVariants, or none!");
95  static constexpr bool hasRemoteConfiguration_ =
97 
98  NJointControllerBasePtr
100  const NJointControllerConfigPtr& config,
101  const VirtualRobot::RobotPtr& rob,
102  bool deletable,
103  bool internal,
104  const std::string& instanceName) const final override
105  {
106  ARMARX_CHECK_EXPRESSION(cmngr) << "ControllerManagement module is NULL!";
107 
108  using ConfigPtrT = typename NJointControllerT::ConfigPtrT;
109  ConfigPtrT cfg = ConfigPtrT::dynamicCast(config);
111  << "The configuration is of the wrong type! it has to be an instance of: "
112  << GetTypeString<ConfigPtrT>();
113 
114  ARMARX_CHECK_EXPRESSION(!ConstructorIsRunning())
115  << "Two NJointControllers are created at the same time";
116  NJointControllerBasePtr ptr;
117  {
118  ConstructorIsRunning_ = true;
120  {
121  ConstructorIsRunning_ = false;
122  };
123  ptr = new NJointControllerT(getRobotUnit(cmngr), cfg, rob);
124  }
125  ptr->deletable = deletable;
126  ptr->internal = internal;
127  ptr->rtClassName_ = ptr->getClassName(Ice::emptyCurrent);
128  ptr->instanceName_ = instanceName;
129  return ptr;
130  }
131 
133  GenerateConfigDescription(
134  const VirtualRobot::RobotPtr& robot,
135  const std::map<std::string, ConstControlDevicePtr>& controlDevices,
136  const std::map<std::string, ConstSensorDevicePtr>& sensorDevices)
137  const final override
138  {
139  if constexpr (hasRemoteConfiguration_)
140  {
141  try
142  {
143  return NJointControllerT::GenerateConfigDescription(
144  robot, controlDevices, sensorDevices);
145  }
146  catch (Ice::UserException& e)
147  {
148  ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
149  << "::GenerateConfigDescription'"
150  << "\n---- file = " << e.ice_file()
151  << "\n---- line = " << e.ice_line()
152  << "\n---- id = " << e.ice_id() << "\n---- what:\n"
153  << e.what() << "\n---- stacktrace:\n"
154  << e.ice_stackTrace();
155  throw;
156  }
157  catch (std::exception& e)
158  {
159  ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
160  << "::GenerateConfigDescription'"
161  << "\n---- what:\n"
162  << e.what();
163  throw;
164  }
165  catch (...)
166  {
167  ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
168  << "::GenerateConfigDescription'";
169  throw;
170  }
171  }
172  else
173  {
174  ARMARX_CHECK_EXPRESSION(!"This function should never be called");
175  }
176  }
177 
178  NJointControllerConfigPtr
179  GenerateConfigFromVariants(const StringVariantBaseMap& variants) const final override
180  {
181  if constexpr (hasRemoteConfiguration_)
182  {
183  try
184  {
185  return NJointControllerT::GenerateConfigFromVariants(variants);
186  }
187  catch (Ice::UserException& e)
188  {
189  ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
190  << "::GenerateConfigFromVariants'"
191  << "\n---- file = " << e.ice_file()
192  << "\n---- line = " << e.ice_line()
193  << "\n---- id = " << e.ice_id() << "\n---- what:\n"
194  << e.what() << "\n---- stacktrace:\n"
195  << e.ice_stackTrace();
196  throw;
197  }
198  catch (std::exception& e)
199  {
200  ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
201  << "::GenerateConfigFromVariants'"
202  << "\n---- what:\n"
203  << e.what();
204  throw;
205  }
206  catch (...)
207  {
208  ARMARX_ERROR << "Exception calling '" << GetTypeString<NJointControllerT>()
209  << "::GenerateConfigFromVariants'";
210  throw;
211  }
212  }
213  else
214  {
215  ARMARX_CHECK_EXPRESSION(!"This function should never be called");
216  }
217  }
218 
219  bool
220  hasRemoteConfiguration() const final override
221  {
222  return hasRemoteConfiguration_;
223  }
224  };
225  } // namespace detail
226 
227  using NJointControllerRegistry = Registrar<std::unique_ptr<NJointControllerRegistryEntry>>;
228 
229  template <class ControllerType>
230  struct NJointControllerRegistration
231  {
232  NJointControllerRegistration(const std::string& name)
233  {
235  name,
236  std::unique_ptr<NJointControllerRegistryEntry>(
238  }
239  };
240 } // namespace armarx
241 
242 #define ARMARX_ASSERT_NJOINTCONTROLLER_HAS_CONSTRUCTION_GUI(T) \
243  static_assert( \
244  ::armarx::detail::hasGenerateConfigDescription<T>::value, \
245  #T " does not offer a construction gui (missing: static GenerateConfigDescription)"); \
246  static_assert( \
247  ::armarx::detail::hasGenerateConfigFromVariants<T>::value, \
248  #T " does not offer a construction gui (missing: static GenerateConfigFromVariants)")
armarx::NJointControllerRegistryEntry::~NJointControllerRegistryEntry
virtual ~NJointControllerRegistryEntry()=default
armarx::NJointControllerRegistryEntry::ConstructorIsRunning
static bool ConstructorIsRunning()
Definition: NJointControllerRegistry.h:65
armarx::NJointControllerRegistration
Definition: NJointControllerRegistry.h:74
armarx::NJointControllerRegistryEntry
Definition: NJointControllerRegistry.h:40
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
NJointControllerBase.h
OnScopeExit.h
armarx::NJointControllerRegistration::NJointControllerRegistration
NJointControllerRegistration(const std::string &name)
Definition: NJointControllerRegistry.h:232
armarx::Registrar::registerElement
static void registerElement(const KeyType &key, RegisteredType element)
Registers an element.
Definition: Registrar.h:105
TemplateMetaProgramming.h
armarx::detail::NJointControllerRegistryEntryHelper
Definition: NJointControllerBase.h:60
detail
Definition: OpenCVUtil.cpp:127
ARMARX_ON_SCOPE_EXIT
#define ARMARX_ON_SCOPE_EXIT
Executes given code when the enclosing scope is left.
Definition: OnScopeExit.h:112
armarx::NJointControllerBase::GenerateConfigFromVariantsFunctionSignature
ConfigPrtType(*)(const StringVariantBaseMap &) GenerateConfigFromVariantsFunctionSignature
Definition: NJointControllerBase.h:593
armarx::getRobotUnit
RobotUnit * getRobotUnit(RobotUnitModule::ControllerManagement *cmngr)
Definition: NJointController.cpp:35
IceInternal::Handle
Definition: forward_declarations.h:8
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::NJointControllerRegistry
Registrar< std::unique_ptr< NJointControllerRegistryEntry > > NJointControllerRegistry
Definition: NJointControllerRegistry.h:71
Registrar.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::WidgetDescription::WidgetPtr
::IceInternal::Handle<::armarx::WidgetDescription::Widget > WidgetPtr
Definition: NJointControllerBase.h:66
armarx::NJointControllerBase::GenerateConfigDescriptionFunctionSignature
WidgetDescription::WidgetPtr(*)(const VirtualRobot::RobotPtr &, const std::map< std::string, ConstControlDevicePtr > &controlDevices, const std::map< std::string, ConstSensorDevicePtr > &sensorDevices) GenerateConfigDescriptionFunctionSignature
Definition: NJointControllerBase.h:590
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::RobotUnitModule::ControllerManagement
This Module manages NJointControllers.
Definition: RobotUnitModuleControllerManagement.h:39
armarx::Registrar
Stores key object pairs.
Definition: Registrar.h:62
armarx::RobotUnitModule
Definition: ControlDevice.h:34
armarx::NJointControllerRegistryEntry::ConstructorIsRunning_
static thread_local bool ConstructorIsRunning_
Definition: NJointControllerRegistry.h:59
armarx::detail::ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK
ARMARX_META_MAKE_HAS_MEMBER_FNC_CHECK(hasGenerateConfigDescription, GenerateConfigDescription, NJointControllerBase::GenerateConfigDescriptionFunctionSignature)
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18