DART 6.10.1
Loading...
Searching...
No Matches
AspectWithVersion.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2021, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef DART_COMMON_DETAIL_ASPECTWITHVERSION_HPP_
34#define DART_COMMON_DETAIL_ASPECTWITHVERSION_HPP_
35
39
40namespace dart {
41namespace common {
42namespace detail {
43
44//==============================================================================
47template <
48 class BaseT,
49 class DerivedT,
50 typename StateDataT,
51 class CompositeT = Composite,
52 void (*updateState)(DerivedT*) = &NoOp<DerivedT*> >
53class AspectWithState : public BaseT
54{
55public:
56 using Base = BaseT;
57 using Derived = DerivedT;
58 using StateData = StateDataT;
59 using CompositeType = CompositeT;
61 constexpr static void (*UpdateState)(Derived*) = updateState;
62
65
67
70
72 template <typename... BaseArgs>
73 AspectWithState(const StateData& state, BaseArgs&&... args)
74 : Base(std::forward<BaseArgs>(args)...), mState(state)
75 {
76 // Do nothing
77 }
78
79 // Documentation inherited
80 void setAspectState(const Aspect::State& otherState) override final;
81
82 // Documentation inherited
83 const Aspect::State* getAspectState() const override final;
84
86 void setState(const StateData& state);
87
89 const State& getState() const;
90
91 // Documentation inherited
92 std::unique_ptr<Aspect> cloneAspect() const override;
93
94protected:
96 State mState;
97};
98
99//==============================================================================
102template <
103 class BaseT,
104 class DerivedT,
105 typename PropertiesDataT,
106 class CompositeT = Composite,
107 void (*updateProperties)(DerivedT*) = &NoOp<DerivedT*> >
109{
110public:
111 using Base = BaseT;
112 using Derived = DerivedT;
113 using PropertiesData = PropertiesDataT;
114 using CompositeType = CompositeT;
116 constexpr static void (*UpdateProperties)(Derived*) = updateProperties;
117
119 Base,
120 Derived,
122 CompositeT,
123 updateProperties>;
124
126
130
132 template <typename... BaseArgs>
134 const PropertiesData& properties, BaseArgs&&... args)
135 : Base(std::forward<BaseArgs>(args)...), mProperties(properties)
136 {
137 // Do nothing
138 }
139
140 // Documentation inherited
142 const Aspect::Properties& someProperties) override final;
143
144 // Documentation inherited
145 const Aspect::Properties* getAspectProperties() const override final;
146
148 void setProperties(const PropertiesData& properties);
149
151 const Properties& getProperties() const;
152
153 // Documentation inherited
154 std::unique_ptr<Aspect> cloneAspect() const override;
155
157 std::size_t incrementVersion();
158
160 DART_DEPRECATED(6.2)
161 void notifyPropertiesUpdate();
162
164 void notifyPropertiesUpdated();
165
166protected:
168 Properties mProperties;
169};
170
171//==============================================================================
172//
173// These namespace-level definitions are required to enable ODR-use of static
174// constexpr member variables.
175//
176// See this StackOverflow answer: http://stackoverflow.com/a/14396189/111426
177//
178template <
179 class BaseT,
180 class DerivedT,
181 typename StateDataT,
182 class CompositeT,
183 void (*updateState)(DerivedT*)>
184constexpr void (
185 *AspectWithState<BaseT, DerivedT, StateDataT, CompositeT, updateState>::
186 UpdateState)(DerivedT*);
187
188//==============================================================================
189template <
190 class BaseT,
191 class DerivedT,
192 typename StateDataT,
193 class CompositeT,
194 void (*updateState)(DerivedT*)>
195AspectWithState<BaseT, DerivedT, StateDataT, CompositeT, updateState>::
196 AspectWithState(const StateDataT& state)
197 : BaseT(), mState(state)
198{
199 // Do nothing
200}
201
202//==============================================================================
203template <
204 class BaseT,
205 class DerivedT,
206 typename StateData,
207 class CompositeT,
208 void (*updateState)(DerivedT*)>
210 setAspectState(const Aspect::State& otherState)
211{
212 setState(static_cast<const State&>(otherState));
213}
214
215//==============================================================================
216template <
217 class BaseT,
218 class DerivedT,
219 typename StateData,
220 class CompositeT,
221 void (*updateState)(DerivedT*)>
222const Aspect::State*
224 getAspectState() const
225{
226 return &mState;
227}
228
229//==============================================================================
230template <
231 class BaseT,
232 class DerivedT,
233 typename StateData,
234 class CompositeT,
235 void (*updateState)(DerivedT*)>
237 setState(const StateData& state)
238{
239 static_cast<StateData&>(mState) = state;
240 UpdateState(static_cast<Derived*>(this));
241}
242
243//==============================================================================
244template <
245 class BaseT,
246 class DerivedT,
247 typename StateDataT,
248 class CompositeT,
249 void (*updateState)(DerivedT*)>
251 getState() const -> const State&
252{
253 return mState;
254}
255
256//==============================================================================
257template <
258 class BaseT,
259 class DerivedT,
260 typename StateData,
261 class CompositeT,
262 void (*updateState)(DerivedT*)>
263std::unique_ptr<Aspect>
265 cloneAspect() const
266{
267 return std::make_unique<Derived>(mState);
268}
269
270//==============================================================================
271//
272// These namespace-level definitions are required to enable ODR-use of static
273// constexpr member variables.
274//
275// See this StackOverflow answer: http://stackoverflow.com/a/14396189/111426
276//
277template <
278 class BaseT,
279 class DerivedT,
280 typename PropertiesDataT,
281 class CompositeT,
282 void (*updateProperties)(DerivedT*)>
284 BaseT,
285 DerivedT,
286 PropertiesDataT,
287 CompositeT,
288 updateProperties>::UpdateProperties)(DerivedT*);
289
290//==============================================================================
291template <
292 class BaseT,
293 class DerivedT,
294 typename PropertiesDataT,
295 class CompositeT,
296 void (*updateProperties)(DerivedT*)>
298 BaseT,
299 DerivedT,
300 PropertiesDataT,
301 CompositeT,
302 updateProperties>::AspectWithVersionedProperties(const PropertiesData&
304 : BaseT(), mProperties(properties)
305{
306 // Do nothing
307}
308
309//==============================================================================
310template <
311 class BaseT,
312 class DerivedT,
313 typename PropertiesData,
314 class CompositeT,
315 void (*updateProperties)(DerivedT*)>
317 BaseT,
318 DerivedT,
319 PropertiesData,
320 CompositeT,
321 updateProperties>::setAspectProperties(const Aspect::Properties&
322 someProperties)
323{
324 setProperties(static_cast<const Properties&>(someProperties));
325}
326
327//==============================================================================
328template <
329 class BaseT,
330 class DerivedT,
331 typename PropertiesData,
332 class CompositeT,
333 void (*updateProperties)(DerivedT*)>
335 BaseT,
336 DerivedT,
337 PropertiesData,
338 CompositeT,
339 updateProperties>::getAspectProperties() const
340{
341 return &mProperties;
342}
343
344//==============================================================================
345template <
346 class BaseT,
347 class DerivedT,
348 typename PropertiesData,
349 class CompositeT,
350 void (*updateProperties)(DerivedT*)>
352 BaseT,
353 DerivedT,
354 PropertiesData,
355 CompositeT,
356 updateProperties>::setProperties(const PropertiesData& properties)
357{
358 static_cast<PropertiesData&>(mProperties) = properties;
359 this->notifyPropertiesUpdated();
360}
361
362//==============================================================================
363template <
364 class BaseT,
365 class DerivedT,
366 typename PropertiesData,
367 class CompositeT,
368 void (*updateProperties)(DerivedT*)>
370 BaseT,
371 DerivedT,
372 PropertiesData,
373 CompositeT,
374 updateProperties>::getProperties() const -> const Properties&
375{
376 return mProperties;
377}
378
379//==============================================================================
380template <
381 class BaseT,
382 class DerivedT,
383 typename PropertiesData,
384 class CompositeT,
385 void (*updateProperties)(DerivedT*)>
386std::unique_ptr<Aspect> AspectWithVersionedProperties<
387 BaseT,
388 DerivedT,
389 PropertiesData,
390 CompositeT,
391 updateProperties>::cloneAspect() const
392{
393 return std::make_unique<Derived>(mProperties);
394}
395
396//==============================================================================
397template <
398 class BaseT,
399 class DerivedT,
400 typename PropertiesData,
401 class CompositeT,
402 void (*updateProperties)(DerivedT*)>
404 BaseT,
405 DerivedT,
406 PropertiesData,
407 CompositeT,
408 updateProperties>::incrementVersion()
409{
410 if (CompositeType* comp = this->getComposite())
411 return comp->incrementVersion();
412
413 return 0;
414}
415
416//==============================================================================
417template <
418 class BaseT,
419 class DerivedT,
420 typename PropertiesData,
421 class CompositeT,
422 void (*updateProperties)(DerivedT*)>
424 BaseT,
425 DerivedT,
426 PropertiesData,
427 CompositeT,
428 updateProperties>::notifyPropertiesUpdate()
429{
430 notifyPropertiesUpdated();
431}
432
433//==============================================================================
434template <
435 class BaseT,
436 class DerivedT,
437 typename PropertiesData,
438 class CompositeT,
439 void (*updateProperties)(DerivedT*)>
441 BaseT,
442 DerivedT,
443 PropertiesData,
444 CompositeT,
445 updateProperties>::notifyPropertiesUpdated()
446{
447 UpdateProperties(static_cast<Derived*>(this));
448 this->incrementVersion();
449}
450
451} // namespace detail
452} // namespace common
453} // namespace dart
454
455#endif // DART_COMMON_DETAIL_ASPECTWITHVERSION_HPP_
#define DART_DEPRECATED(version)
Definition Deprecated.hpp:51
BodyPropPtr properties
Definition SdfParser.cpp:80
If your Aspect has Properties, then that Properties class should inherit this Aspect::Properties clas...
Definition Aspect.hpp:86
If your Aspect has a State, then that State class should inherit this Aspect::State class.
Definition Aspect.hpp:65
Definition Aspect.hpp:48
Composite is a base class that should be virtually inherited by any class that wants to be able to ma...
Definition Composite.hpp:53
The MakeCloneable class is used to easily create an Cloneable (such as Node::State) which simply take...
Definition Cloneable.hpp:84
AspectWithProtectedState generates implementations of the State managing functions for an Aspect clas...
Definition AspectWithVersion.hpp:54
CompositeT CompositeType
Definition AspectWithVersion.hpp:59
void setAspectState(const Aspect::State &otherState) override final
Definition AspectWithVersion.hpp:210
AspectWithState(const StateData &state, BaseArgs &&... args)
Construct this Aspect and pass args into the constructor of the Base class.
Definition AspectWithVersion.hpp:73
AspectWithState(const AspectWithState &)=delete
DerivedT Derived
Definition AspectWithVersion.hpp:57
BaseT Base
Definition AspectWithVersion.hpp:56
StateDataT StateData
Definition AspectWithVersion.hpp:58
const Aspect::State * getAspectState() const override final
Definition AspectWithVersion.hpp:224
AspectWithState(const StateData &state=StateData())
Construct using a StateData instance.
AspectWithProtectedProperties generates implementations of the Property managing functions for an Asp...
Definition AspectWithVersion.hpp:109
std::size_t incrementVersion()
Increment the version of this Aspect and its Composite.
Definition AspectWithVersion.hpp:408
PropertiesDataT PropertiesData
Definition AspectWithVersion.hpp:113
AspectWithVersionedProperties(const PropertiesData &properties, BaseArgs &&... args)
Construct this Aspect and pass args into the constructor of the Base class.
Definition AspectWithVersion.hpp:133
const Aspect::Properties * getAspectProperties() const override final
Definition AspectWithVersion.hpp:339
CompositeT CompositeType
Definition AspectWithVersion.hpp:114
void setAspectProperties(const Aspect::Properties &someProperties) override final
Definition AspectWithVersion.hpp:321
AspectWithVersionedProperties(const AspectWithVersionedProperties &)=delete
AspectWithVersionedProperties(const PropertiesData &properties=PropertiesData())
Construct using a PropertiesData instance.
Definition AspectWithVersion.hpp:302
DerivedT Derived
Definition AspectWithVersion.hpp:112
BaseT Base
Definition AspectWithVersion.hpp:111
void NoOp(Args...)
NoOp is short for "no operation".
Definition NoOp.hpp:44
Definition BulletCollisionDetector.cpp:65
Definition SharedLibraryManager.hpp:46