DART 6.6.2
Loading...
Searching...
No Matches
AspectWithVersion.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2018, 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 <class BaseT, class DerivedT, typename StateDataT,
48 class CompositeT = Composite,
49 void (*updateState)(DerivedT*) = &NoOp<DerivedT*> >
50class AspectWithState : public BaseT
51{
52public:
53
54 using Base = BaseT;
55 using Derived = DerivedT;
56 using StateData = StateDataT;
57 using CompositeType = CompositeT;
59 constexpr static void (*UpdateState)(Derived*) = updateState;
60
62 Base, Derived, StateData, CompositeT, updateState>;
63
65
68
70 template <typename... BaseArgs>
72 BaseArgs&&... args)
73 : Base(std::forward<BaseArgs>(args)...),
74 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:
95
97 State mState;
98};
99
100//==============================================================================
103template <class BaseT, class DerivedT, typename PropertiesDataT,
104 class CompositeT = Composite,
105 void (*updateProperties)(DerivedT*) = &NoOp<DerivedT*> >
107{
108public:
109
110 using Base = BaseT;
111 using Derived = DerivedT;
112 using PropertiesData = PropertiesDataT;
113 using CompositeType = CompositeT;
115 constexpr static void (*UpdateProperties)(Derived*) = updateProperties;
116
118 Base, Derived, PropertiesData, CompositeT, updateProperties>;
119
122
126
128 template <typename... BaseArgs>
130 const PropertiesData& properties, BaseArgs&&... args)
131 : Base(std::forward<BaseArgs>(args)...),
132 mProperties(properties)
133 {
134 // Do nothing
135 }
136
137 // Documentation inherited
138 void setAspectProperties(const Aspect::Properties& someProperties) override final;
139
140 // Documentation inherited
141 const Aspect::Properties* getAspectProperties() const override final;
142
144 void setProperties(const PropertiesData& properties);
145
147 const Properties& getProperties() const;
148
149 // Documentation inherited
150 std::unique_ptr<Aspect> cloneAspect() const override;
151
153 std::size_t incrementVersion();
154
156 DART_DEPRECATED(6.2)
157 void notifyPropertiesUpdate();
158
160 void notifyPropertiesUpdated();
161
162protected:
163
165 Properties mProperties;
166
167};
168
169//==============================================================================
170//
171// These namespace-level definitions are required to enable ODR-use of static
172// constexpr member variables.
173//
174// See this StackOverflow answer: http://stackoverflow.com/a/14396189/111426
175//
176template <class BaseT, class DerivedT, typename StateDataT,
177 class CompositeT, void (*updateState)(DerivedT*)>
178constexpr void (*AspectWithState<
179 BaseT, DerivedT, StateDataT, CompositeT, updateState>::UpdateState)(
180 DerivedT*);
181
182//==============================================================================
183template <class BaseT, class DerivedT, typename StateDataT,
184 class CompositeT, void (*updateState)(DerivedT*)>
185AspectWithState<BaseT, DerivedT, StateDataT, CompositeT, updateState>::
186AspectWithState(const StateDataT& state)
187 : BaseT(),
188 mState(state)
189{
190 // Do nothing
191}
192
193//==============================================================================
194template <class BaseT, class DerivedT, typename StateData,
195 class CompositeT, void (*updateState)(DerivedT*)>
197setAspectState(const Aspect::State& otherState)
198{
199 setState(static_cast<const State&>(otherState));
200}
201
202//==============================================================================
203template <class BaseT, class DerivedT, typename StateData,
204 class CompositeT, void (*updateState)(DerivedT*)>
205const Aspect::State*
207getAspectState() const
208{
209 return &mState;
210}
211
212//==============================================================================
213template <class BaseT, class DerivedT, typename StateData,
214 class CompositeT, void (*updateState)(DerivedT*)>
216setState(const StateData& state)
217{
218 static_cast<StateData&>(mState) = state;
219 UpdateState(static_cast<Derived*>(this));
220}
221
222//==============================================================================
223template <class BaseT, class DerivedT, typename StateDataT,
224 class CompositeT, void (*updateState)(DerivedT*)>
226getState() const -> const State&
227{
228 return mState;
229}
230
231//==============================================================================
232template <class BaseT, class DerivedT, typename StateData,
233 class CompositeT, void (*updateState)(DerivedT*)>
234std::unique_ptr<Aspect>
236 cloneAspect() const
237{
238 return common::make_unique<Derived>(mState);
239}
240
241//==============================================================================
242//
243// These namespace-level definitions are required to enable ODR-use of static
244// constexpr member variables.
245//
246// See this StackOverflow answer: http://stackoverflow.com/a/14396189/111426
247//
248template <class BaseT, class DerivedT, typename PropertiesDataT,
249 class CompositeT, void (*updateProperties)(DerivedT*)>
250constexpr void (*AspectWithVersionedProperties<BaseT, DerivedT, PropertiesDataT,
251 CompositeT, updateProperties>::
252UpdateProperties)(DerivedT*);
253
254//==============================================================================
255template <class BaseT, class DerivedT, typename PropertiesDataT,
256 class CompositeT, void (*updateProperties)(DerivedT*)>
257AspectWithVersionedProperties<BaseT, DerivedT, PropertiesDataT,
258 CompositeT, updateProperties>::
260 : BaseT(),
261 mProperties(properties)
262{
263 // Do nothing
264}
265
266//==============================================================================
267template <class BaseT, class DerivedT, typename PropertiesData,
268 class CompositeT, void (*updateProperties)(DerivedT*)>
269void AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
270 CompositeT, updateProperties>::
271setAspectProperties(const Aspect::Properties& someProperties)
272{
273 setProperties(static_cast<const Properties&>(someProperties));
274}
275
276//==============================================================================
277template <class BaseT, class DerivedT, typename PropertiesData,
278 class CompositeT, void (*updateProperties)(DerivedT*)>
280AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
281 CompositeT, updateProperties>::
282getAspectProperties() const
283{
284 return &mProperties;
285}
286
287//==============================================================================
288template <class BaseT, class DerivedT, typename PropertiesData,
289 class CompositeT, void (*updateProperties)(DerivedT*)>
290void AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
291 CompositeT, updateProperties>::
292setProperties(const PropertiesData& properties)
293{
294 static_cast<PropertiesData&>(mProperties) = properties;
295 this->notifyPropertiesUpdated();
296}
297
298//==============================================================================
299template <class BaseT, class DerivedT, typename PropertiesData,
300 class CompositeT, void (*updateProperties)(DerivedT*)>
301auto AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
302 CompositeT, updateProperties>::
303getProperties() const -> const Properties&
304{
305 return mProperties;
306}
307
308//==============================================================================
309template <class BaseT, class DerivedT, typename PropertiesData,
310 class CompositeT, void (*updateProperties)(DerivedT*)>
311std::unique_ptr<Aspect>
312AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
313 CompositeT, updateProperties>::
314cloneAspect() const
315{
316 return common::make_unique<Derived>(mProperties);
317}
318
319//==============================================================================
320template <class BaseT, class DerivedT, typename PropertiesData,
321 class CompositeT, void (*updateProperties)(DerivedT*)>
322std::size_t AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
323 CompositeT, updateProperties>::incrementVersion()
324{
325 if(CompositeType* comp = this->getComposite())
326 return comp->incrementVersion();
327
328 return 0;
329}
330
331//==============================================================================
332template <class BaseT, class DerivedT, typename PropertiesData,
333 class CompositeT, void (*updateProperties)(DerivedT*)>
335 BaseT, DerivedT, PropertiesData,
336 CompositeT, updateProperties>::notifyPropertiesUpdate()
337{
338 notifyPropertiesUpdated();
339}
340
341//==============================================================================
342template <class BaseT, class DerivedT, typename PropertiesData,
343 class CompositeT, void (*updateProperties)(DerivedT*)>
345 BaseT, DerivedT, PropertiesData,
346 CompositeT, updateProperties>::notifyPropertiesUpdated()
347{
348 UpdateProperties(static_cast<Derived*>(this));
349 this->incrementVersion();
350}
351
352} // namespace detail
353} // namespace common
354} // namespace dart
355
356#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:83
If your Aspect has a State, then that State class should inherit this Aspect::State class.
Definition Aspect.hpp:64
Definition Aspect.hpp:47
Composite is a base class that should be virtually inherited by any class that wants to be able to ma...
Definition Composite.hpp:52
The MakeCloneable class is used to easily create an Cloneable (such as Node::State) which simply take...
Definition Cloneable.hpp:85
AspectWithProtectedState generates implementations of the State managing functions for an Aspect clas...
Definition AspectWithVersion.hpp:51
CompositeT CompositeType
Definition AspectWithVersion.hpp:57
void setAspectState(const Aspect::State &otherState) override final
Definition AspectWithVersion.hpp:197
AspectWithState(const StateData &state, BaseArgs &&... args)
Construct this Aspect and pass args into the constructor of the Base class.
Definition AspectWithVersion.hpp:71
AspectWithState(const AspectWithState &)=delete
DerivedT Derived
Definition AspectWithVersion.hpp:55
BaseT Base
Definition AspectWithVersion.hpp:54
StateDataT StateData
Definition AspectWithVersion.hpp:56
const Aspect::State * getAspectState() const override final
Definition AspectWithVersion.hpp:207
AspectWithState(const StateData &state=StateData())
Construct using a StateData instance.
AspectWithProtectedProperties generates implementations of the Property managing functions for an Asp...
Definition AspectWithVersion.hpp:107
std::size_t incrementVersion()
Increment the version of this Aspect and its Composite.
Definition AspectWithVersion.hpp:323
PropertiesDataT PropertiesData
Definition AspectWithVersion.hpp:112
AspectWithVersionedProperties(const PropertiesData &properties, BaseArgs &&... args)
Construct this Aspect and pass args into the constructor of the Base class.
Definition AspectWithVersion.hpp:129
const Aspect::Properties * getAspectProperties() const override final
Definition AspectWithVersion.hpp:282
CompositeT CompositeType
Definition AspectWithVersion.hpp:113
void setAspectProperties(const Aspect::Properties &someProperties) override final
Definition AspectWithVersion.hpp:271
AspectWithVersionedProperties(const AspectWithVersionedProperties &)=delete
AspectWithVersionedProperties(const PropertiesData &properties=PropertiesData())
Construct using a PropertiesData instance.
Definition AspectWithVersion.hpp:259
DerivedT Derived
Definition AspectWithVersion.hpp:111
BaseT Base
Definition AspectWithVersion.hpp:110
void NoOp(Args...)
NoOp is short for "no operation".
Definition NoOp.hpp:44
Definition BulletCollisionDetector.cpp:63
Definition SharedLibraryManager.hpp:43