1 | // |
---|
2 | // You received this file as part of Finroc |
---|
3 | // A Framework for intelligent robot control |
---|
4 | // |
---|
5 | // Copyright (C) Finroc GbR (finroc.org) |
---|
6 | // |
---|
7 | // This program is free software; you can redistribute it and/or |
---|
8 | // modify it under the terms of the GNU General Public License |
---|
9 | // as published by the Free Software Foundation; either version 2 |
---|
10 | // of the License, or (at your option) any later version. |
---|
11 | // |
---|
12 | // This program is distributed in the hope that it will be useful, |
---|
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | // GNU General Public License for more details. |
---|
16 | // |
---|
17 | // You should have received a copy of the GNU General Public License |
---|
18 | // along with this program; if not, write to the Free Software |
---|
19 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | // |
---|
21 | //---------------------------------------------------------------------- |
---|
22 | /*!\file plugins/data_ports/common/tAbstractDataPortCreationInfo.h |
---|
23 | * |
---|
24 | * \author Max Reichardt |
---|
25 | * |
---|
26 | * \date 2012-10-29 |
---|
27 | * |
---|
28 | * \brief Contains tAbstractDataPortCreationInfo |
---|
29 | * |
---|
30 | * \b tAbstractDataPortCreationInfo |
---|
31 | * |
---|
32 | * This class bundles various parameters for the creation of data ports. |
---|
33 | * |
---|
34 | */ |
---|
35 | //---------------------------------------------------------------------- |
---|
36 | #ifndef __plugins__data_ports__common__tAbstractDataPortCreationInfo_h__ |
---|
37 | #define __plugins__data_ports__common__tAbstractDataPortCreationInfo_h__ |
---|
38 | |
---|
39 | //---------------------------------------------------------------------- |
---|
40 | // External includes (system with <>, local with "") |
---|
41 | //---------------------------------------------------------------------- |
---|
42 | #include "core/port/tAbstractPortCreationInfo.h" |
---|
43 | |
---|
44 | //---------------------------------------------------------------------- |
---|
45 | // Internal includes with "" |
---|
46 | //---------------------------------------------------------------------- |
---|
47 | #include "plugins/data_ports/tBounds.h" |
---|
48 | #include "plugins/data_ports/tQueueSettings.h" |
---|
49 | #include "plugins/data_ports/tUnit.h" |
---|
50 | |
---|
51 | //---------------------------------------------------------------------- |
---|
52 | // Namespace declaration |
---|
53 | //---------------------------------------------------------------------- |
---|
54 | namespace finroc |
---|
55 | { |
---|
56 | namespace data_ports |
---|
57 | { |
---|
58 | namespace common |
---|
59 | { |
---|
60 | |
---|
61 | //---------------------------------------------------------------------- |
---|
62 | // Forward declarations / typedefs / enums |
---|
63 | //---------------------------------------------------------------------- |
---|
64 | /*! Can be used to wrap lock order for tAbstractPortCreationInfo variadic template constructor */ |
---|
65 | struct tLockOrder |
---|
66 | { |
---|
67 | int wrapped; |
---|
68 | |
---|
69 | tLockOrder(int i) : wrapped(i) {} |
---|
70 | }; |
---|
71 | |
---|
72 | //---------------------------------------------------------------------- |
---|
73 | // Class declaration |
---|
74 | //---------------------------------------------------------------------- |
---|
75 | //! Bundle of port creation parameters |
---|
76 | /*! |
---|
77 | * This class bundles various parameters for the creation of data ports. |
---|
78 | * |
---|
79 | * Instead of providing suitable constructors for all types of sensible |
---|
80 | * combinations of the numerous (often optional) construction parameters, |
---|
81 | * there is only one constructor taking a single argument of this class. |
---|
82 | */ |
---|
83 | class tAbstractDataPortCreationInfo : public core::tAbstractPortCreationInfo |
---|
84 | { |
---|
85 | |
---|
86 | //---------------------------------------------------------------------- |
---|
87 | // Public fields and methods |
---|
88 | //---------------------------------------------------------------------- |
---|
89 | public: |
---|
90 | |
---|
91 | /*! SI Unit of port. NULL for no unit = provides raw numbers */ |
---|
92 | tUnit unit; |
---|
93 | |
---|
94 | /*! Input Queue size; value <= 0 means flexible size */ |
---|
95 | int max_queue_size; |
---|
96 | |
---|
97 | /*! Minimum Network update interval; value < 0 => default values */ |
---|
98 | int16_t min_net_update_interval; |
---|
99 | |
---|
100 | /*! config entry in config file */ |
---|
101 | tString config_entry; |
---|
102 | |
---|
103 | /*! |
---|
104 | * Creates port creation info with default values |
---|
105 | * (Typically, at least flags and name should be set to something sensible) |
---|
106 | */ |
---|
107 | tAbstractDataPortCreationInfo(); |
---|
108 | |
---|
109 | /*! |
---|
110 | * Constructor takes variadic argument list... just any properties you want to assign to port. |
---|
111 | * |
---|
112 | * The first string is interpreted as port name, the second possibly as config entry (relevant for parameters only). |
---|
113 | * A framework element pointer is interpreted as parent. |
---|
114 | * tFrameworkElementFlag arguments are interpreted as flags. |
---|
115 | * A tQueueSettings argument creates an input queue with the specified settings. |
---|
116 | * tBounds<T> are port's bounds. |
---|
117 | * tUnit argument is port's unit. |
---|
118 | * tAbstractPortCreationInfo argument is copied. This is only allowed as first argument. |
---|
119 | */ |
---|
120 | template <typename ARG1, typename ... TArgs> |
---|
121 | explicit tAbstractDataPortCreationInfo(const ARG1& arg1, const TArgs&... rest) : |
---|
122 | unit(), |
---|
123 | max_queue_size(-1), |
---|
124 | min_net_update_interval(-1), |
---|
125 | config_entry(), |
---|
126 | default_value(), |
---|
127 | bounds(), |
---|
128 | name_set(false) |
---|
129 | { |
---|
130 | ProcessFirstArg<ARG1>(arg1); |
---|
131 | ProcessArgs(rest...); |
---|
132 | } |
---|
133 | |
---|
134 | /*! |
---|
135 | * \return Have bounds for port been set? |
---|
136 | */ |
---|
137 | bool BoundsSet() const |
---|
138 | { |
---|
139 | return bounds.GetSize() > 0; |
---|
140 | } |
---|
141 | |
---|
142 | /*! |
---|
143 | * \return Has a default value been set? |
---|
144 | */ |
---|
145 | bool DefaultValueSet() const |
---|
146 | { |
---|
147 | return default_value.GetSize() > 0; |
---|
148 | } |
---|
149 | |
---|
150 | /*! |
---|
151 | * Derive method: Copy port creation info and change specified parameters |
---|
152 | * (Basically, the same arguments as in the constructor are possible) |
---|
153 | */ |
---|
154 | template <typename ... TArgs> |
---|
155 | tAbstractDataPortCreationInfo Derive(const TArgs&... rest) const |
---|
156 | { |
---|
157 | ProcessArgs(rest...); |
---|
158 | return *this; |
---|
159 | } |
---|
160 | |
---|
161 | /*! |
---|
162 | * \return Bounds (when their exact type is not known at compile time) |
---|
163 | */ |
---|
164 | const rrlib::serialization::tConstSource& GetBoundsGeneric() const |
---|
165 | { |
---|
166 | return bounds; |
---|
167 | } |
---|
168 | |
---|
169 | /*! |
---|
170 | * \return Bounds (when their exact type is not known at compile time) |
---|
171 | */ |
---|
172 | const rrlib::serialization::tConstSource& GetDefaultGeneric() const |
---|
173 | { |
---|
174 | return default_value; |
---|
175 | } |
---|
176 | |
---|
177 | /*! Various set methods for different port properties */ |
---|
178 | void Set(core::tFrameworkElement* parent) |
---|
179 | { |
---|
180 | this->parent = parent; |
---|
181 | } |
---|
182 | |
---|
183 | void Set(const char* c) |
---|
184 | { |
---|
185 | SetString(c); |
---|
186 | } |
---|
187 | |
---|
188 | void Set(const tString& s) |
---|
189 | { |
---|
190 | SetString(s); |
---|
191 | } |
---|
192 | |
---|
193 | void Set(const tQueueSettings& queue_settings) |
---|
194 | { |
---|
195 | max_queue_size = queue_settings.GetMaximumQueueLength(); |
---|
196 | flags |= core::tFrameworkElement::tFlag::HAS_QUEUE | core::tFrameworkElement::tFlag::USES_QUEUE; |
---|
197 | if (queue_settings.DequeueAllQueue()) |
---|
198 | { |
---|
199 | flags |= core::tFrameworkElement::tFlag::HAS_DEQUEUE_ALL_QUEUE; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | void Set(core::tFrameworkElement::tFlags flags) |
---|
204 | { |
---|
205 | this->flags |= flags; |
---|
206 | } |
---|
207 | |
---|
208 | void Set(const tUnit& unit) |
---|
209 | { |
---|
210 | this->unit = unit; |
---|
211 | } |
---|
212 | |
---|
213 | // This only catches int arguments - and should be used very rarely |
---|
214 | // void Set(const tLockOrder& lo) |
---|
215 | // { |
---|
216 | // this->lock_order = lo.wrapped; |
---|
217 | // } |
---|
218 | |
---|
219 | void Set(const rrlib::rtti::tType& dt) |
---|
220 | { |
---|
221 | this->data_type = dt; |
---|
222 | } |
---|
223 | |
---|
224 | /*! |
---|
225 | * Set bounds when type is not known at compile time |
---|
226 | * |
---|
227 | * \param min Minimum value |
---|
228 | * \param max Maximum value |
---|
229 | * \param out_of_bounds_action How to proceed if an incoming value is out of bounds |
---|
230 | */ |
---|
231 | void SetBoundsGeneric(const rrlib::rtti::tGenericObject& min, const rrlib::rtti::tGenericObject& max, |
---|
232 | tOutOfBoundsAction out_of_bounds_action = tOutOfBoundsAction::ADJUST_TO_RANGE) |
---|
233 | { |
---|
234 | rrlib::serialization::tOutputStream stream(bounds); |
---|
235 | // critical: needs to be the same serialization as used in tBounds |
---|
236 | min.Serialize(stream); |
---|
237 | max.Serialize(stream); |
---|
238 | stream << out_of_bounds_action; |
---|
239 | } |
---|
240 | |
---|
241 | void SetDefaultGeneric(const rrlib::rtti::tGenericObject& default_val) |
---|
242 | { |
---|
243 | rrlib::serialization::tOutputStream stream(default_value); |
---|
244 | default_val.Serialize(stream); |
---|
245 | } |
---|
246 | |
---|
247 | /*! |
---|
248 | * Set Port flag |
---|
249 | * |
---|
250 | * \param flag Flag to set |
---|
251 | * \param value Value to set flag to |
---|
252 | */ |
---|
253 | void SetFlag(uint flag, bool value); |
---|
254 | |
---|
255 | /*! |
---|
256 | * Removes default value from port creation info |
---|
257 | */ |
---|
258 | void UnsetDefaultValue() |
---|
259 | { |
---|
260 | default_value.Clear(); |
---|
261 | } |
---|
262 | |
---|
263 | //---------------------------------------------------------------------- |
---|
264 | // Protected classes and fields (used by derived class tPortCreationInfo also) |
---|
265 | //---------------------------------------------------------------------- |
---|
266 | protected: |
---|
267 | |
---|
268 | /*! |
---|
269 | * Class to store default values and bounds of arbitrary types (in serialized form) |
---|
270 | */ |
---|
271 | template <size_t INITIAL_SIZE> |
---|
272 | class tStorage : public rrlib::serialization::tStackMemoryBuffer<INITIAL_SIZE> |
---|
273 | { |
---|
274 | public: |
---|
275 | tStorage() : |
---|
276 | rrlib::serialization::tStackMemoryBuffer<INITIAL_SIZE>(5, true) |
---|
277 | {} |
---|
278 | |
---|
279 | tStorage(const tStorage& o) |
---|
280 | { |
---|
281 | this->CopyFrom(o); |
---|
282 | } |
---|
283 | |
---|
284 | tStorage& operator=(const tStorage& o) |
---|
285 | { |
---|
286 | this->CopyFrom(o); |
---|
287 | return *this; |
---|
288 | } |
---|
289 | }; |
---|
290 | |
---|
291 | /*! Storage for default value */ |
---|
292 | tStorage<150> default_value; |
---|
293 | |
---|
294 | /*! Storage for bounds */ |
---|
295 | tStorage<300> bounds; |
---|
296 | |
---|
297 | /*! Has name been set? (we do not check name for zero length, because ports without names may be created) */ |
---|
298 | bool name_set; |
---|
299 | |
---|
300 | |
---|
301 | /*! |
---|
302 | * Processes next string argument |
---|
303 | */ |
---|
304 | void SetString(const tString& s); |
---|
305 | |
---|
306 | //---------------------------------------------------------------------- |
---|
307 | // Private fields and methods |
---|
308 | //---------------------------------------------------------------------- |
---|
309 | private: |
---|
310 | |
---|
311 | /*! |
---|
312 | * Processes first argument (only here tPortCreationInfoBase argument is allowed) |
---|
313 | */ |
---|
314 | template <typename A> |
---|
315 | void ProcessFirstArg(const typename std::enable_if<std::is_base_of<tAbstractPortCreationInfo, A>::value, A>::type& a) |
---|
316 | { |
---|
317 | (*this) = a; |
---|
318 | } |
---|
319 | |
---|
320 | template <typename A> |
---|
321 | void ProcessFirstArg(const typename std::enable_if < !std::is_base_of<tAbstractPortCreationInfo, A>::value, A >::type& a) |
---|
322 | { |
---|
323 | ProcessArg<A>(a); |
---|
324 | } |
---|
325 | |
---|
326 | /*! Process constructor arguments */ |
---|
327 | void ProcessArgs() {} |
---|
328 | |
---|
329 | template <typename A, typename ... ARest> |
---|
330 | void ProcessArgs(const A& arg, const ARest&... args) |
---|
331 | { |
---|
332 | ProcessArg<A>(arg); |
---|
333 | ProcessArgs(args...); |
---|
334 | } |
---|
335 | |
---|
336 | /*! Process single constructor argument */ |
---|
337 | template <typename A> |
---|
338 | void ProcessArg(const A& arg) |
---|
339 | { |
---|
340 | // standard case |
---|
341 | Set(arg); |
---|
342 | } |
---|
343 | |
---|
344 | }; |
---|
345 | |
---|
346 | //---------------------------------------------------------------------- |
---|
347 | // End of namespace declaration |
---|
348 | //---------------------------------------------------------------------- |
---|
349 | } |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | #endif |
---|