DART  6.7.3
AspectWithVersion.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2019, 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 
121 
125 
127  template <typename... BaseArgs>
129  const PropertiesData& properties, BaseArgs&&... args)
130  : Base(std::forward<BaseArgs>(args)...),
131  mProperties(properties)
132  {
133  // Do nothing
134  }
135 
136  // Documentation inherited
137  void setAspectProperties(const Aspect::Properties& someProperties) override final;
138 
139  // Documentation inherited
140  const Aspect::Properties* getAspectProperties() const override final;
141 
143  void setProperties(const PropertiesData& properties);
144 
146  const Properties& getProperties() const;
147 
148  // Documentation inherited
149  std::unique_ptr<Aspect> cloneAspect() const override;
150 
152  std::size_t incrementVersion();
153 
155  DART_DEPRECATED(6.2)
156  void notifyPropertiesUpdate();
157 
159  void notifyPropertiesUpdated();
160 
161 protected:
162 
164  Properties mProperties;
165 
166 };
167 
168 //==============================================================================
169 //
170 // These namespace-level definitions are required to enable ODR-use of static
171 // constexpr member variables.
172 //
173 // See this StackOverflow answer: http://stackoverflow.com/a/14396189/111426
174 //
175 template <class BaseT, class DerivedT, typename StateDataT,
176  class CompositeT, void (*updateState)(DerivedT*)>
177 constexpr void (*AspectWithState<
178  BaseT, DerivedT, StateDataT, CompositeT, updateState>::UpdateState)(
179  DerivedT*);
180 
181 //==============================================================================
182 template <class BaseT, class DerivedT, typename StateDataT,
183  class CompositeT, void (*updateState)(DerivedT*)>
184 AspectWithState<BaseT, DerivedT, StateDataT, CompositeT, updateState>::
185 AspectWithState(const StateDataT& state)
186  : BaseT(),
187  mState(state)
188 {
189  // Do nothing
190 }
191 
192 //==============================================================================
193 template <class BaseT, class DerivedT, typename StateData,
194  class CompositeT, void (*updateState)(DerivedT*)>
196 setAspectState(const Aspect::State& otherState)
197 {
198  setState(static_cast<const State&>(otherState));
199 }
200 
201 //==============================================================================
202 template <class BaseT, class DerivedT, typename StateData,
203  class CompositeT, void (*updateState)(DerivedT*)>
204 const Aspect::State*
206 getAspectState() const
207 {
208  return &mState;
209 }
210 
211 //==============================================================================
212 template <class BaseT, class DerivedT, typename StateData,
213  class CompositeT, void (*updateState)(DerivedT*)>
215 setState(const StateData& state)
216 {
217  static_cast<StateData&>(mState) = state;
218  UpdateState(static_cast<Derived*>(this));
219 }
220 
221 //==============================================================================
222 template <class BaseT, class DerivedT, typename StateDataT,
223  class CompositeT, void (*updateState)(DerivedT*)>
225 getState() const -> const State&
226 {
227  return mState;
228 }
229 
230 //==============================================================================
231 template <class BaseT, class DerivedT, typename StateData,
232  class CompositeT, void (*updateState)(DerivedT*)>
233 std::unique_ptr<Aspect>
235  cloneAspect() const
236 {
237  return common::make_unique<Derived>(mState);
238 }
239 
240 //==============================================================================
241 //
242 // These namespace-level definitions are required to enable ODR-use of static
243 // constexpr member variables.
244 //
245 // See this StackOverflow answer: http://stackoverflow.com/a/14396189/111426
246 //
247 template <class BaseT, class DerivedT, typename PropertiesDataT,
248  class CompositeT, void (*updateProperties)(DerivedT*)>
249 constexpr void (*AspectWithVersionedProperties<BaseT, DerivedT, PropertiesDataT,
250  CompositeT, updateProperties>::
251 UpdateProperties)(DerivedT*);
252 
253 //==============================================================================
254 template <class BaseT, class DerivedT, typename PropertiesDataT,
255  class CompositeT, void (*updateProperties)(DerivedT*)>
256 AspectWithVersionedProperties<BaseT, DerivedT, PropertiesDataT,
257  CompositeT, updateProperties>::
259  : BaseT(),
260  mProperties(properties)
261 {
262  // Do nothing
263 }
264 
265 //==============================================================================
266 template <class BaseT, class DerivedT, typename PropertiesData,
267  class CompositeT, void (*updateProperties)(DerivedT*)>
268 void AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
269  CompositeT, updateProperties>::
270 setAspectProperties(const Aspect::Properties& someProperties)
271 {
272  setProperties(static_cast<const Properties&>(someProperties));
273 }
274 
275 //==============================================================================
276 template <class BaseT, class DerivedT, typename PropertiesData,
277  class CompositeT, void (*updateProperties)(DerivedT*)>
278 const Aspect::Properties*
279 AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
280  CompositeT, updateProperties>::
281 getAspectProperties() const
282 {
283  return &mProperties;
284 }
285 
286 //==============================================================================
287 template <class BaseT, class DerivedT, typename PropertiesData,
288  class CompositeT, void (*updateProperties)(DerivedT*)>
289 void AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
290  CompositeT, updateProperties>::
291 setProperties(const PropertiesData& properties)
292 {
293  static_cast<PropertiesData&>(mProperties) = properties;
294  this->notifyPropertiesUpdated();
295 }
296 
297 //==============================================================================
298 template <class BaseT, class DerivedT, typename PropertiesData,
299  class CompositeT, void (*updateProperties)(DerivedT*)>
300 auto AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
301  CompositeT, updateProperties>::
302 getProperties() const -> const Properties&
303 {
304  return mProperties;
305 }
306 
307 //==============================================================================
308 template <class BaseT, class DerivedT, typename PropertiesData,
309  class CompositeT, void (*updateProperties)(DerivedT*)>
310 std::unique_ptr<Aspect>
311 AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
312  CompositeT, updateProperties>::
313 cloneAspect() const
314 {
315  return common::make_unique<Derived>(mProperties);
316 }
317 
318 //==============================================================================
319 template <class BaseT, class DerivedT, typename PropertiesData,
320  class CompositeT, void (*updateProperties)(DerivedT*)>
321 std::size_t AspectWithVersionedProperties<BaseT, DerivedT, PropertiesData,
322  CompositeT, updateProperties>::incrementVersion()
323 {
324  if(CompositeType* comp = this->getComposite())
325  return comp->incrementVersion();
326 
327  return 0;
328 }
329 
330 //==============================================================================
331 template <class BaseT, class DerivedT, typename PropertiesData,
332  class CompositeT, void (*updateProperties)(DerivedT*)>
334  BaseT, DerivedT, PropertiesData,
335  CompositeT, updateProperties>::notifyPropertiesUpdate()
336 {
337  notifyPropertiesUpdated();
338 }
339 
340 //==============================================================================
341 template <class BaseT, class DerivedT, typename PropertiesData,
342  class CompositeT, void (*updateProperties)(DerivedT*)>
344  BaseT, DerivedT, PropertiesData,
345  CompositeT, updateProperties>::notifyPropertiesUpdated()
346 {
347  UpdateProperties(static_cast<Derived*>(this));
348  this->incrementVersion();
349 }
350 
351 } // namespace detail
352 } // namespace common
353 } // namespace dart
354 
355 #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:322
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:128
const Aspect::Properties * getAspectProperties() const override final
Definition: AspectWithVersion.hpp:281
CompositeT CompositeType
Definition: AspectWithVersion.hpp:113
void setAspectProperties(const Aspect::Properties &someProperties) override final
Definition: AspectWithVersion.hpp:270
AspectWithVersionedProperties(const AspectWithVersionedProperties &)=delete
AspectWithVersionedProperties(const PropertiesData &properties=PropertiesData())
Construct using a PropertiesData instance.
Definition: AspectWithVersion.hpp:258
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:251
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43