SchemaMacros.h
Go to the documentation of this file.
1 #include <mc_rtc/macros/pp_id.h>
2 
3 #define MC_RTC_SCHEMA(SchemaT, BaseT) \
4  \
5  using is_schema_t = std::true_type; \
6  \
7 protected: \
8  inline static mc_rtc::schema::Operations ops_; \
9  \
10  inline static size_t schema_size() noexcept \
11  { \
12  return ops_.values_count + BaseT::ops_.values_count; \
13  } \
14  inline void write_impl(mc_rtc::MessagePackBuilder & builder) const \
15  { \
16  BaseT::ops_.write(this, builder); \
17  ops_.write(this, builder); \
18  } \
19  \
20 public: \
21  inline void save(mc_rtc::Configuration & out) const \
22  { \
23  BaseT::ops_.save(this, out); \
24  ops_.save(this, out); \
25  } \
26  inline mc_rtc::Configuration toConfiguration() const \
27  { \
28  mc_rtc::Configuration out; \
29  save(out); \
30  return out; \
31  } \
32  inline void write(mc_rtc::MessagePackBuilder & builder) const \
33  { \
34  builder.start_map(schema_size()); \
35  write_impl(builder); \
36  builder.finish_map(); \
37  } \
38  inline std::string dump(bool pretty, bool yaml) const \
39  { \
40  mc_rtc::Configuration out; \
41  save(out); \
42  return out.dump(pretty, yaml); \
43  } \
44  inline void load(const mc_rtc::Configuration & in) \
45  { \
46  BaseT::ops_.load(this, in); \
47  ops_.load(this, in); \
48  } \
49  inline static SchemaT fromConfiguration(const mc_rtc::Configuration & in) \
50  { \
51  SchemaT out; \
52  out.load(in); \
53  return out; \
54  } \
55  inline void buildForm(mc_rtc::schema::Operations::FormElements & form) const \
56  { \
57  BaseT::ops_.buildForm(this, form); \
58  ops_.buildForm(this, form); \
59  } \
60  static inline void formToStd(const mc_rtc::Configuration & in, mc_rtc::Configuration & out) \
61  { \
62  BaseT::ops_.formToStd(in, out); \
63  ops_.formToStd(in, out); \
64  } \
65  template<typename Callback = std::function<void()>> \
66  inline void addToGUI( \
67  mc_rtc ::gui::StateBuilder & gui, const std::vector<std::string> & category, const std::string & name, \
68  Callback callback = []() {}) \
69  { \
70  auto form = mc_rtc::gui::Form(name, \
71  [this, callback](const mc_rtc::Configuration & in) \
72  { \
73  mc_rtc::Configuration cfg; \
74  formToStd(in, cfg); \
75  \
76  load(cfg); \
77  callback(); \
78  }); \
79  buildForm(form); \
80  gui.addElement(category, form); \
81  } \
82  inline bool operator==(const SchemaT & rhs) const \
83  { \
84  return BaseT::ops_.areEqual(this, &rhs) && ops_.areEqual(this, &rhs); \
85  } \
86  inline bool operator!=(const SchemaT & rhs) const \
87  { \
88  return !(*this == rhs); \
89  }
90 
91 #define MC_RTC_NEW_SCHEMA(SchemaT) MC_RTC_SCHEMA(SchemaT, mc_rtc::schema::details::EmptySchema)
92 
94 #define MC_RTC_SCHEMA_MEMBER(T, TYPE, NAME, DESCRIPTION, REQUIRED, DEFAULT, ...) \
95 public: \
96  TYPE NAME = mc_rtc::schema::details::get_default( \
97  DEFAULT, std::integral_constant<mc_rtc::schema::ValueFlag, REQUIRED>{}, ##__VA_ARGS__); \
98  \
99 private: \
100  inline static const bool NAME##_registered_ = \
101  T::ops_.registerValue(mc_rtc::schema::details::MemberPointerWrapper<&T::NAME>{}, #NAME, DESCRIPTION, \
102  std::integral_constant<mc_rtc::schema::ValueFlag, REQUIRED>{}, ##__VA_ARGS__); \
103  \
104 public:
105 
107 #define MC_RTC_SCHEMA_REQUIRED_MEMBER(T, TYPE, NAME, DESCRIPTION, DEFAULT, ...) \
108  MC_RTC_PP_ID(MC_RTC_SCHEMA_MEMBER(T, TYPE, NAME, DESCRIPTION, mc_rtc::schema::ValueFlag::All, DEFAULT, ##__VA_ARGS__))
109 
111 #define MC_RTC_SCHEMA_OPTIONAL_MEMBER(T, TYPE, NAME, DESCRIPTION, DEFAULT, ...) \
112  MC_RTC_PP_ID(MC_RTC_SCHEMA_MEMBER(T, TYPE, NAME, DESCRIPTION, mc_rtc::schema::ValueFlag::Interactive, DEFAULT, \
113  ##__VA_ARGS__))
114 
116 #define MC_RTC_SCHEMA_DEFAULT_MEMBER(T, TYPE, NAME, DESCRIPTION, REQUIRED, ...) \
117  MC_RTC_PP_ID(MC_RTC_SCHEMA_MEMBER(T, TYPE, NAME, DESCRIPTION, REQUIRED, mc_rtc::Default<TYPE>::value, ##__VA_ARGS__))
118 
120 #define MC_RTC_SCHEMA_REQUIRED_DEFAULT_MEMBER(T, TYPE, NAME, DESCRIPTION, ...) \
121  MC_RTC_PP_ID(MC_RTC_SCHEMA_DEFAULT_MEMBER(T, TYPE, NAME, DESCRIPTION, mc_rtc::schema::ValueFlag::All, ##__VA_ARGS__))
122 
124 #define MC_RTC_SCHEMA_OPTIONAL_DEFAULT_MEMBER(T, TYPE, NAME, DESCRIPTION, ...) \
125  MC_RTC_PP_ID( \
126  MC_RTC_SCHEMA_DEFAULT_MEMBER(T, TYPE, NAME, DESCRIPTION, mc_rtc::schema::ValueFlag::Interactive, ##__VA_ARGS__))
pp_id.h