RobotsIO
Loading...
Searching...
No Matches
Parameters.h
1/*
2 * Copyright (C) 2019 Istituto Italiano di Tecnologia (IIT)
3 *
4 * This software may be modified and distributed under the terms of the
5 * BSD 3-Clause license. See the accompanying LICENSE file for details.
6 */
7
8#ifndef ROBOTSIO_PARAMETERS_H
9#define ROBOTSIO_PARAMETERS_H
10
11#include <RobotsIO/Utils/ParametersExtractor.h>
12#include <RobotsIO/Utils/ParametersFiller.h>
13
14#include <string>
15#include <stdexcept>
16#include <unordered_map>
17#include <vector>
18
19namespace RobotsIO {
20 namespace Utils {
21 class Parameters;
22 }
23}
24
25#define robots_io_field_storage(type) \
26 std::unordered_map<std::string, type > type##_data_; \
27
28#define robots_io_std_field_storage(type) \
29 std::unordered_map<std::string, std::type > type##_data_; \
30
31#define robots_io_field_getter(type) \
32 type get_##type(const std::string& name) const; \
33
34#define robots_io_std_field_getter(type) \
35 std::type get_##type(const std::string& name) const; \
36
37#define robots_io_field_setter(type) \
38 void set_##type(const std::string& name, const type& value); \
39
40#define robots_io_std_field_setter(type) \
41 void set_##type(const std::string& name, const std::type& value); \
42
43#define robots_io_declare_field(class_name, type, name) \
44 type name() const; \
45 \
46 void name(const type& value); \
47 \
48 class Field_##name : public Field \
49 { \
50 public: \
51 void extract_field(const class_name &parameters, RobotsIO::Utils::ParametersExtractor& extractor) const override; \
52 \
53 bool fill_field(class_name &parameters, const RobotsIO::Utils::ParametersFiller& extractor) override; \
54 \
55 }; \
56 \
57 friend class Field_##name;
58
59#define robots_io_declare_std_field(class_name, type, name) \
60 std::type name() const; \
61 \
62 void name(const std::type& value); \
63 \
64 class Field_##name : public Field \
65 { \
66 public: \
67 void extract_field(const class_name &parameters, RobotsIO::Utils::ParametersExtractor& extractor) const override; \
68 \
69 bool fill_field(class_name &parameters, const RobotsIO::Utils::ParametersFiller& extractor) override; \
70 \
71 }; \
72 \
73 friend class Field_##name;
74
75#define robots_io_accessor(class_name) \
76 class_name(const RobotsIO::Utils::ParametersFiller& filler); \
77 \
78 class Field \
79 { \
80 public: \
81 virtual void extract_field(const class_name& parameters, RobotsIO::Utils::ParametersExtractor& extractor) const = 0; \
82 \
83 virtual bool fill_field(class_name& parameters, const RobotsIO::Utils::ParametersFiller& filler) = 0; \
84 }; \
85 \
86 std::unordered_map<std::string, Field*> fields_; \
87 \
88 void extract_field(const std::string& key, RobotsIO::Utils::ParametersExtractor& extractor) const override; \
89 \
90 bool fill_field(const std::string& key, const RobotsIO::Utils::ParametersFiller& filler) override; \
91 \
92 std::vector<std::string> keys() const override;
93
94#define robots_io_declare_field_impl(class_name, type, name) \
95 type class_name::name() const \
96 { \
97 return get_##type(#name); \
98 } \
99 \
100 void class_name::name(const type& value) \
101 { \
102 if (fields_.find(#name) == fields_.end()) \
103 fields_[#name] = new Field_##name(); \
104 set_##type(#name, value); \
105 } \
106 \
107 void class_name::Field_##name::extract_field(const class_name& parameters, RobotsIO::Utils::ParametersExtractor& extractor) const \
108 { \
109 extractor.extract_field(#name, parameters.name()); \
110 } \
111 \
112 bool class_name::Field_##name::fill_field(class_name& parameters, const RobotsIO::Utils::ParametersFiller& filler) \
113 { \
114 bool is_value = false; \
115 type value; \
116 std::tie(is_value, value) = filler.fill_##type(#name); \
117 if (!is_value) \
118 return false; \
119 \
120 parameters.name(value); \
121 \
122 return true; \
123 } \
124
125#define robots_io_declare_std_field_impl(class_name, type, name) \
126 std::type class_name::name() const \
127 { \
128 return get_##type(#name); \
129 } \
130 \
131 void class_name::name(const std::type& value) \
132 { \
133 if (fields_.find(#name) == fields_.end()) \
134 fields_[#name] = new Field_##name(); \
135 set_##type(#name, value); \
136 } \
137 \
138 void class_name::Field_##name::extract_field(const class_name& parameters, RobotsIO::Utils::ParametersExtractor& extractor) const \
139 { \
140 extractor.extract_field(#name, parameters.name()); \
141 } \
142 \
143 bool class_name::Field_##name::fill_field(class_name& parameters, const RobotsIO::Utils::ParametersFiller& filler) \
144 { \
145 bool is_value = false; \
146 std::type value; \
147 std::tie(is_value, value) = filler.fill_##type(#name); \
148 if (!is_value) \
149 return false; \
150 \
151 parameters.name(value); \
152 \
153 return true; \
154 }
155
156#define robots_io_accessor_impl(class_name) \
157 class_name::class_name(const RobotsIO::Utils::ParametersFiller& filler) \
158 : class_name() \
159 { \
160 for (const std::string& key : keys()) \
161 if (!fill_field(key, filler)) \
162 { \
163 throw(std::runtime_error(std::string(#class_name) + "::ctor. Field " + key + " not available in provided filler.")); \
164 } \
165 } \
166 \
167 void class_name::extract_field(const std::string& key, RobotsIO::Utils::ParametersExtractor& extractor) const \
168 { \
169 fields_.at(key)->extract_field(*this, extractor); \
170 } \
171 \
172 bool class_name::fill_field(const std::string& key, const RobotsIO::Utils::ParametersFiller& filler) \
173 { \
174 return fields_.at(key)->fill_field(*this, filler); \
175 } \
176 \
177 std::vector<std::string> class_name::keys() const\
178 { \
179 std::vector<std::string> keys; \
180 for (const auto& field : fields_) \
181 keys.push_back(field.first); \
182 return keys; \
183 } \
184
186{
187public:
191 virtual bool fill_field(const std::string& key, const RobotsIO::Utils::ParametersFiller& filler) = 0;
192
196 virtual void extract_field(const std::string& key, RobotsIO::Utils::ParametersExtractor& extractor) const = 0;
197
201 virtual std::vector<std::string> keys() const = 0;
202
206 const Parameters* parameters() const;
207
208protected:
213
215
216 robots_io_field_getter(double);
217
218 robots_io_field_getter(int);
219
220 robots_io_field_getter(bool);
221
226
228
229 robots_io_field_setter(double);
230
231 robots_io_field_setter(int);
232
233 robots_io_field_setter(bool);
234
235private:
239 robots_io_std_field_storage(string);
240
241 robots_io_std_field_storage(size_t);
242
243 robots_io_field_storage(double);
244
245 robots_io_field_storage(int);
246
247 robots_io_field_storage(bool);
248};
249
250#endif /* ROBOTSIO_PARAMETERS_H */
virtual std::vector< std::string > keys() const =0
const Parameters * parameters() const
Definition: Parameters.cpp:67
virtual void extract_field(const std::string &key, RobotsIO::Utils::ParametersExtractor &extractor) const =0
virtual bool fill_field(const std::string &key, const RobotsIO::Utils::ParametersFiller &filler)=0