mc_rtc  2.16.0
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  { delete ptr; }
42 
48 # define ROBOT_MODULE_DEFAULT_CONSTRUCTOR(NAME, TYPE) \
49  extern "C" \
50  { \
51  ROBOT_MODULE_COMMON(NAME) \
52  ROBOT_MODULE_API unsigned int create_args_required() \
53  { return 1; } \
54  ROBOT_MODULE_API mc_rbdyn::RobotModule * create(const std::string &) \
55  { return new TYPE(); } \
56  }
57 
62 # define ROBOT_MODULE_CANONIC_CONSTRUCTOR(NAME, TYPE) \
63  extern "C" \
64  { \
65  ROBOT_MODULE_COMMON(NAME) \
66  ROBOT_MODULE_API unsigned int create_args_required() \
67  { return 3; } \
68  ROBOT_MODULE_API mc_rbdyn::RobotModule * create(const std::string &, \
69  const std::string & path, \
70  const std::string & name) \
71  { return new TYPE(path, name); } \
72  }
73 
74 #else
75 
76 # include <mc_rbdyn/RobotLoader.h>
77 
78 # define ROBOT_MODULE_DEFAULT_CONSTRUCTOR(NAME, TYPE) \
79  namespace \
80  { \
81  static auto registered = []() \
82  { \
83  using fn_t = std::function<TYPE *()>; \
84  mc_rbdyn::RobotLoader::register_object(NAME, fn_t([]() { return new TYPE(); })); \
85  return true; \
86  }(); \
87  }
88 
89 # define ROBOT_MODULE_CANONIC_CONSTRUCTOR(NAME, TYPE) \
90  namespace \
91  { \
92  static auto registered = []() \
93  { \
94  using fn_t = std::function<TYPE *(const std::string &, const std::string &)>; \
95  mc_rbdyn::RobotLoader::register_object( \
96  NAME, fn_t([](const std::string & path, const std::string & name) { return new TYPE(path, name); })); \
97  return true; \
98  }(); \
99  }
100 
101 #endif