ObserverMacros.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 
8 
9 #include <mc_rtc/version.h>
10 
11 /* Set of macros to assist with the writing of an Observer */
12 
19 #define OBSERVER_MODULE_CHECK_VERSION(NAME) \
20  if(mc_rtc::MC_RTC_VERSION != mc_rtc::version()) \
21  { \
22  mc_rtc::log::error( \
23  "{} was compiled with {} but mc_rtc is currently at version {}, you might experience subtle issues and " \
24  "should recompile your code", \
25  NAME, mc_rtc::MC_RTC_VERSION, mc_rtc::version()); \
26  }
27 
28 #ifndef MC_RTC_BUILD_STATIC
29 
30 # define EXPORT_OBSERVER_MODULE(NAME, TYPE) \
31  extern "C" \
32  { \
33  OBSERVER_MODULE_API void MC_RTC_OBSERVER_MODULE(std::vector<std::string> & names) \
34  { \
35  OBSERVER_MODULE_CHECK_VERSION(NAME) \
36  names = {NAME}; \
37  } \
38  OBSERVER_MODULE_API void destroy(mc_observers::Observer * ptr) \
39  { \
40  delete ptr; \
41  } \
42  OBSERVER_MODULE_API unsigned int create_args_required() \
43  { \
44  return 2; \
45  } \
46  OBSERVER_MODULE_API mc_observers::Observer * create(const std::string & type, const double & dt) \
47  { \
48  return new TYPE{type, dt}; \
49  } \
50  }
51 
52 #else
53 
55 
56 # define EXPORT_OBSERVER_MODULE(NAME, TYPE) \
57  namespace \
58  { \
59  static auto registered = []() \
60  { \
61  using fn_t = std::function<TYPE *(const double &)>; \
62  mc_observers::ObserverLoader::register_object(NAME, fn_t([](const double & dt) { return new TYPE(NAME, dt); })); \
63  return true; \
64  }(); \
65  }
66 
67 #endif
ObserverLoader.h
Observer.h