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