RobotModuleMacros.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2020 CNRS-UM LIRMM, CNRS-AIST JRL, BIT
3  */
4 
5 #pragma once
6 
7 #include <mc_rbdyn/RobotModule.h>
8 
9 #ifndef MC_RTC_BUILD_STATIC
10 
11 # include <mc_rtc/version.h>
12 
13 /* Set of macros to assist with the writing of a RobotModule */
14 
22 # define ROBOT_MODULE_CHECK_VERSION(NAME) \
23  if(mc_rtc::MC_RTC_VERSION != mc_rtc::version()) \
24  { \
25  mc_rtc::log::error("{} was compiled with {} but mc_rtc is currently at version {}, you might experience subtle " \
26  "issues and should recompile your code", \
27  NAME, mc_rtc::MC_RTC_VERSION, mc_rtc::version()); \
28  }
29 
34 # define ROBOT_MODULE_COMMON(NAME) \
35  ROBOT_MODULE_API void MC_RTC_ROBOT_MODULE(std::vector<std::string> & names) \
36  { \
37  ROBOT_MODULE_CHECK_VERSION(NAME) \
38  names = {NAME}; \
39  } \
40  ROBOT_MODULE_API void destroy(mc_rbdyn::RobotModule * ptr) \
41  { \
42  delete ptr; \
43  }
44 
50 # define ROBOT_MODULE_DEFAULT_CONSTRUCTOR(NAME, TYPE) \
51  extern "C" \
52  { \
53  ROBOT_MODULE_COMMON(NAME) \
54  ROBOT_MODULE_API unsigned int create_args_required() \
55  { \
56  return 1; \
57  } \
58  ROBOT_MODULE_API mc_rbdyn::RobotModule * create(const std::string &) \
59  { \
60  return new TYPE(); \
61  } \
62  }
63 
68 # define ROBOT_MODULE_CANONIC_CONSTRUCTOR(NAME, TYPE) \
69  extern "C" \
70  { \
71  ROBOT_MODULE_COMMON(NAME) \
72  ROBOT_MODULE_API unsigned int create_args_required() \
73  { \
74  return 3; \
75  } \
76  ROBOT_MODULE_API mc_rbdyn::RobotModule * create(const std::string &, \
77  const std::string & path, \
78  const std::string & name) \
79  { \
80  return new TYPE(path, name); \
81  } \
82  }
83 
84 #else
85 
86 # include <mc_rbdyn/RobotLoader.h>
87 
88 # define ROBOT_MODULE_DEFAULT_CONSTRUCTOR(NAME, TYPE) \
89  namespace \
90  { \
91  static auto registered = []() \
92  { \
93  using fn_t = std::function<TYPE *()>; \
94  mc_rbdyn::RobotLoader::register_object(NAME, fn_t([]() { return new TYPE(); })); \
95  return true; \
96  }(); \
97  }
98 
99 # define ROBOT_MODULE_CANONIC_CONSTRUCTOR(NAME, TYPE) \
100  namespace \
101  { \
102  static auto registered = []() \
103  { \
104  using fn_t = std::function<TYPE *(const std::string &, const std::string &)>; \
105  mc_rbdyn::RobotLoader::register_object( \
106  NAME, fn_t([](const std::string & path, const std::string & name) { return new TYPE(path, name); })); \
107  return true; \
108  }(); \
109  }
110 
111 #endif
RobotLoader.h
RobotModule.h