DART  6.7.3
Cloneable.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_CLONEABLE_HPP_
34 #define DART_COMMON_CLONEABLE_HPP_
35 
36 #include <memory>
37 #include <vector>
38 
39 #include "dart/common/Memory.hpp"
40 
41 namespace dart {
42 namespace common {
43 
44 //==============================================================================
52 template <class T>
53 class Cloneable
54 {
55 public:
56 
58  Cloneable() = default;
59 
61  virtual ~Cloneable() = default;
62 
64  Cloneable(const Cloneable& doNotCopy) = delete;
65 
67  Cloneable& operator=(const Cloneable& doNotCopy) = delete;
68 
70  virtual std::unique_ptr<T> clone() const = 0;
71 
73  virtual void copy(const T& anotherCloneable) = 0;
74 };
75 
76 //==============================================================================
83 template <class Base, class Mixin>
84 class MakeCloneable : public Base, public Mixin
85 {
86 public:
87 
88  using Data = Mixin;
89 
90  // To get byte-aligned Eigen vectors
91  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
92 
94 
95 
96  MakeCloneable();
97 
100  template <typename... Args>
101  MakeCloneable(Args&&... args);
102 
104  MakeCloneable(const Mixin& mixin);
105 
107  MakeCloneable(Mixin&& mixin);
108 
111 
114 
116  MakeCloneable& operator=(const Mixin& mixin);
117 
119  MakeCloneable& operator=(Mixin&& mixin);
120 
122  MakeCloneable& operator=(const MakeCloneable& other);
123 
126 
127  // Documentation inherited
128  std::unique_ptr<Base> clone() const override final;
129 
130  // Documentation inherited
131  void copy(const Base& other) override final;
132 
133 };
134 
135 //==============================================================================
136 template <class Base, class OwnerT, class DataT,
137  void (*setData)(OwnerT*, const DataT&),
138  DataT (*getData)(const OwnerT*)>
139 class ProxyCloneable : public Base
140 {
141 public:
142 
143  using Data = DataT;
144  using Owner = OwnerT;
145 
147  ProxyCloneable();
148 
150  ProxyCloneable(OwnerT* owner);
151 
154  template <typename... Args>
155  ProxyCloneable(OwnerT* owner, Args&&... args);
156 
159  template <typename... Args>
160  ProxyCloneable(Args&&... args);
161 
163  ProxyCloneable(const ProxyCloneable& other);
164 
167 
169  ProxyCloneable& operator=(const Data& data);
170 
172  ProxyCloneable& operator=(Data&& other);
173 
175  ProxyCloneable& operator=(const ProxyCloneable& other);
176 
179 
181  void set(const Data& data);
182 
184  void set(Data&& data);
185 
187  void set(const ProxyCloneable& other);
188 
190  void set(ProxyCloneable&& other);
191 
193  Data get() const;
194 
196  OwnerT* getOwner();
197 
199  const OwnerT* getOwner() const;
200 
201  // Documentation inherited
202  std::unique_ptr<Base> clone() const override final;
203 
204  // Documentation inherited
205  void copy(const Base& other) override final;
206 
207 protected:
208 
210  OwnerT* mOwner;
211 
214  std::unique_ptr<Data> mData;
215 
216 };
217 
218 //==============================================================================
222 template <typename MapType>
224 {
225 public:
226 
228  CloneableMap() = default;
229 
231  CloneableMap(const CloneableMap& otherStates);
232 
234  CloneableMap(CloneableMap&& otherStates);
235 
237  CloneableMap(const MapType& otherMap);
238 
240  CloneableMap(MapType&& otherMap);
241 
243  CloneableMap& operator=(const CloneableMap& otherStates);
244 
246  CloneableMap& operator=(CloneableMap&& otherStates);
247 
249  CloneableMap& operator=(const MapType& otherMap);
250 
252  CloneableMap& operator=(MapType&& otherMap);
253 
258  void copy(const CloneableMap& otherMap, bool merge=false);
259 
264  void copy(const MapType& otherMap, bool merge=false);
265 
269  void merge(const CloneableMap& otherMap);
270 
274  void merge(const MapType& otherMap);
275 
277  MapType& getMap();
278 
280  const MapType& getMap() const;
281 
282 protected:
283 
285  MapType mMap;
286 };
287 
288 //==============================================================================
291 template <typename T>
293 {
294 public:
295 
297  CloneableVector() = default;
298 
300  CloneableVector(const std::vector<T>& regularVector);
301 
303  CloneableVector(std::vector<T>&& regularVector);
304 
306  CloneableVector(const CloneableVector& other);
307 
310 
312  std::unique_ptr< CloneableVector<T> > clone() const;
313 
315  void copy(const CloneableVector<T>& anotherVector);
316 
318  std::vector<T>& getVector();
319 
321  const std::vector<T>& getVector() const;
322 
323 private:
324 
326  std::vector<T> mVector;
327 };
328 
329 } // namespace common
330 } // namespace dart
331 
333 
334 #endif // DART_COMMON_CLONEABLE_HPP_
#define DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR(class_name)
Definition: Memory.hpp:148
MapHolder is a templated wrapper class that is used to allow maps of Aspect::State and Aspect::Proper...
Definition: Cloneable.hpp:224
MapType mMap
A map containing the collection of States for the Aspect.
Definition: Cloneable.hpp:285
CloneableMap()=default
Default constructor.
The CloneableVector type wraps a std::vector of an Cloneable type allowing it to be handled by an Clo...
Definition: Cloneable.hpp:293
std::vector< T > mVector
The std::vector that this class is wrapping.
Definition: Cloneable.hpp:326
CloneableVector()=default
Default constructor.
Cloneable is a CRTP base class that provides an interface for easily creating data structures that ar...
Definition: Cloneable.hpp:54
virtual std::unique_ptr< T > clone() const =0
Implement this function to allow your Cloneable type to be copied safely.
Cloneable()=default
Default constructor.
Cloneable & operator=(const Cloneable &doNotCopy)=delete
Do not copy this class directly, use clone() or copy() instead.
Cloneable(const Cloneable &doNotCopy)=delete
Do not copy this class directly, use clone() or copy() instead.
virtual ~Cloneable()=default
Virtual destructor.
virtual void copy(const T &anotherCloneable)=0
Copy the contents of anotherCloneable into this one.
The MakeCloneable class is used to easily create an Cloneable (such as Node::State) which simply take...
Definition: Cloneable.hpp:85
void copy(const Base &other) override final
Definition: Cloneable.hpp:134
Mixin Data
Definition: Cloneable.hpp:88
std::unique_ptr< Base > clone() const override final
Definition: Cloneable.hpp:127
MakeCloneable()
Default constructor. Uses the default constructor of Mixin.
Definition: Cloneable.hpp:44
MakeCloneable & operator=(const Mixin &mixin)
Copy assignment operator that uses a Mixin instance.
Definition: Cloneable.hpp:93
Definition: Cloneable.hpp:140
OwnerT Owner
Definition: Cloneable.hpp:144
DataT Data
Definition: Cloneable.hpp:143
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43