DART  6.6.2
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 
36 #include "dart/common/Aspect.hpp"
39 
40 namespace dart {
41 namespace common {
42 namespace detail {
43 
44 //==============================================================================
47 template <class BaseT, class DerivedT, typename StateDataT,
48  class CompositeT = Composite,
49  void (*updateState)(DerivedT*) = &NoOp<DerivedT*> >
50 class AspectWithState : public BaseT
51 {
52 public:
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 
64  AspectWithState(const AspectWithState&) = delete;
65 
67  AspectWithState(const StateData& state = StateData());
68 
70  template <typename... BaseArgs>
71  AspectWithState(const StateData& state,
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 
94 protected:
95 
97  State mState;
98 };
99 
100 //==============================================================================
103 template <class BaseT, class DerivedT, typename PropertiesDataT,
104  class CompositeT = Composite,
105  void (*updateProperties)(DerivedT*) = &NoOp<DerivedT*> >
106 class AspectWithVersionedProperties : public BaseT
107 {
108 public:
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 
162 protected:
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 //
176 template <class BaseT, class DerivedT, typename StateDataT,
177  class CompositeT, void (*updateState)(DerivedT*)>
178 constexpr void (*AspectWithState<
179  BaseT, DerivedT, StateDataT, CompositeT, updateState>::UpdateState)(
180  DerivedT*);
181 
182 //==============================================================================
183 template <class BaseT, class DerivedT, typename StateDataT,
184  class CompositeT, void (*updateState)(DerivedT*)>
185 AspectWithState<BaseT, DerivedT, StateDataT, CompositeT, updateState>::
186 AspectWithState(const StateDataT& state)
187  : BaseT(),
188  mState(state)
189 {
190  // Do nothing
191 }
192 
193 //==============================================================================
194 template <class BaseT, class DerivedT, typename StateData,
195  class CompositeT, void (*updateState)(DerivedT*)>
197 setAspectState(const Aspect::State& otherState)
198 {
199  setState(static_cast<const State&>(otherState));
200 }
201 
202 //==============================================================================
203 template <class BaseT, class DerivedT, typename StateData,
204  class CompositeT, void (*updateState)(DerivedT*)>
205 const Aspect::State*
207 getAspectState() const
208 {
209  return &mState;
210 }
211 
212 //==============================================================================
213 template <class BaseT, class DerivedT, typename StateData,
214  class CompositeT, void (*updateState)(DerivedT*)>
216 setState(const StateData& state)
217 {
218  static_cast<StateData&>(mState) = state;
219  UpdateState(static_cast<Derived*>(this));
220 }
221 
222 //==============================================================================
223 template <class BaseT, class DerivedT, typename StateDataT,
224  class CompositeT, void (*updateState)(DerivedT*)>
226 getState() const -> const State&
227 {
228  return mState;
229 }
230 
231 //==============================================================================
232 template <class BaseT, class DerivedT, typename StateData,
233  class CompositeT, void (*updateState)(DerivedT*)>
234 std::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 //
248 template <class BaseT, class DerivedT, typename PropertiesDataT,
249  class CompositeT, void (*updateProperties)(DerivedT*)>
250 constexpr void (*AspectWithVersionedProperties<BaseT, DerivedT, PropertiesDataT,
251  CompositeT, updateProperties>::
252 UpdateProperties)(DerivedT*);
253 
254 //==============================================================================
255 template <class BaseT, class DerivedT, typename PropertiesDataT,
256  class CompositeT, void (*updateProperties)(DerivedT*)>
257 AspectWithVersionedProperties<BaseT, DerivedT, PropertiesDataT,
258  CompositeT, updateProperties>::
260  : BaseT(),
261  mProperties(properties)
262 {
263  // Do nothing
264 }
265 
266 //==============================================================================
267 template <class BaseT, class DerivedT, typename PropertiesData,
268  class CompositeT, void (*updateProperties)(DerivedT*)>
269 void AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
270  CompositeT, updateProperties>::
271 setAspectProperties(const Aspect::Properties& someProperties)
272 {
273  setProperties(static_cast<const Properties&>(someProperties));
274 }
275 
276 //==============================================================================
277 template <class BaseT, class DerivedT, typename PropertiesData,
278  class CompositeT, void (*updateProperties)(DerivedT*)>
279 const Aspect::Properties*
280 AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
281  CompositeT, updateProperties>::
282 getAspectProperties() const
283 {
284  return &mProperties;
285 }
286 
287 //==============================================================================
288 template <class BaseT, class DerivedT, typename PropertiesData,
289  class CompositeT, void (*updateProperties)(DerivedT*)>
290 void AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
291  CompositeT, updateProperties>::
292 setProperties(const PropertiesData& properties)
293 {
294  static_cast<PropertiesData&>(mProperties) = properties;
295  this->notifyPropertiesUpdated();
296 }
297 
298 //==============================================================================
299 template <class BaseT, class DerivedT, typename PropertiesData,
300  class CompositeT, void (*updateProperties)(DerivedT*)>
301 auto AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
302  CompositeT, updateProperties>::
303 getProperties() const -> const Properties&
304 {
305  return mProperties;
306 }
307 
308 //==============================================================================
309 template <class BaseT, class DerivedT, typename PropertiesData,
310  class CompositeT, void (*updateProperties)(DerivedT*)>
311 std::unique_ptr<Aspect>
312 AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
313  CompositeT, updateProperties>::
314 cloneAspect() const
315 {
316  return common::make_unique<Derived>(mProperties);
317 }
318 
319 //==============================================================================
320 template <class BaseT, class DerivedT, typename PropertiesData,
321  class CompositeT, void (*updateProperties)(DerivedT*)>
322 std::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 //==============================================================================
332 template <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 //==============================================================================
342 template <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
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
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
constexpr void(* AspectWithVersionedProperties)(DerivedT *)
Definition: AspectWithVersion.hpp:252
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43