DART  6.10.1
Cloneable.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_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:
57  Cloneable() = default;
58 
60  virtual ~Cloneable() = default;
61 
63  Cloneable(const Cloneable& doNotCopy) = delete;
64 
66  Cloneable& operator=(const Cloneable& doNotCopy) = delete;
67 
69  virtual std::unique_ptr<T> clone() const = 0;
70 
72  virtual void copy(const T& anotherCloneable) = 0;
73 };
74 
75 //==============================================================================
82 template <class Base, class Mixin>
83 class MakeCloneable : public Base, public Mixin
84 {
85 public:
86  using Data = Mixin;
87 
88  // To get byte-aligned Eigen vectors
89  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
90 
92 
93 
94  MakeCloneable();
95 
98  template <typename... Args>
99  MakeCloneable(Args&&... args);
100 
102  MakeCloneable(const Mixin& mixin);
103 
105  MakeCloneable(Mixin&& mixin);
106 
109 
112 
114  MakeCloneable& operator=(const Mixin& mixin);
115 
117  MakeCloneable& operator=(Mixin&& mixin);
118 
120  MakeCloneable& operator=(const MakeCloneable& other);
121 
124 
125  // Documentation inherited
126  std::unique_ptr<Base> clone() const override final;
127 
128  // Documentation inherited
129  void copy(const Base& other) override final;
130 };
131 
132 //==============================================================================
133 template <
134  class Base,
135  class OwnerT,
136  class DataT,
137  void (*setData)(OwnerT*, const DataT&),
138  DataT (*getData)(const OwnerT*)>
139 class ProxyCloneable : public Base
140 {
141 public:
142  using Data = DataT;
143  using Owner = OwnerT;
144 
146  ProxyCloneable();
147 
149  ProxyCloneable(OwnerT* owner);
150 
153  template <typename... Args>
154  ProxyCloneable(OwnerT* owner, Args&&... args);
155 
158  template <typename... Args>
159  ProxyCloneable(Args&&... args);
160 
162  ProxyCloneable(const ProxyCloneable& other);
163 
166 
168  ProxyCloneable& operator=(const Data& data);
169 
171  ProxyCloneable& operator=(Data&& other);
172 
174  ProxyCloneable& operator=(const ProxyCloneable& other);
175 
178 
180  void set(const Data& data);
181 
183  void set(Data&& data);
184 
186  void set(const ProxyCloneable& other);
187 
189  void set(ProxyCloneable&& other);
190 
192  Data get() const;
193 
195  OwnerT* getOwner();
196 
198  const OwnerT* getOwner() const;
199 
200  // Documentation inherited
201  std::unique_ptr<Base> clone() const override final;
202 
203  // Documentation inherited
204  void copy(const Base& other) override final;
205 
206 protected:
208  OwnerT* mOwner;
209 
212  std::unique_ptr<Data> mData;
213 };
214 
215 //==============================================================================
219 template <typename MapType>
221 {
222 public:
224  CloneableMap() = default;
225 
227  CloneableMap(const CloneableMap& otherStates);
228 
230  CloneableMap(CloneableMap&& otherStates);
231 
233  CloneableMap(const MapType& otherMap);
234 
236  CloneableMap(MapType&& otherMap);
237 
239  CloneableMap& operator=(const CloneableMap& otherStates);
240 
242  CloneableMap& operator=(CloneableMap&& otherStates);
243 
245  CloneableMap& operator=(const MapType& otherMap);
246 
248  CloneableMap& operator=(MapType&& otherMap);
249 
254  void copy(const CloneableMap& otherMap, bool merge = false);
255 
260  void copy(const MapType& otherMap, bool merge = false);
261 
265  void merge(const CloneableMap& otherMap);
266 
270  void merge(const MapType& otherMap);
271 
273  MapType& getMap();
274 
276  const MapType& getMap() const;
277 
278 protected:
280  MapType mMap;
281 };
282 
283 //==============================================================================
286 template <typename T>
288 {
289 public:
291  CloneableVector() = default;
292 
294  CloneableVector(const std::vector<T>& regularVector);
295 
297  CloneableVector(std::vector<T>&& regularVector);
298 
300  CloneableVector(const CloneableVector& other);
301 
304 
306  std::unique_ptr<CloneableVector<T> > clone() const;
307 
309  void copy(const CloneableVector<T>& anotherVector);
310 
312  std::vector<T>& getVector();
313 
315  const std::vector<T>& getVector() const;
316 
317 private:
319  std::vector<T> mVector;
320 };
321 
322 } // namespace common
323 } // namespace dart
324 
326 
327 #endif // DART_COMMON_CLONEABLE_HPP_
#define DART_DEFINE_ALIGNED_SHARED_OBJECT_CREATOR(class_name)
Definition: Memory.hpp:155
MapHolder is a templated wrapper class that is used to allow maps of Aspect::State and Aspect::Proper...
Definition: Cloneable.hpp:221
MapType mMap
A map containing the collection of States for the Aspect.
Definition: Cloneable.hpp:280
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:288
std::vector< T > mVector
The std::vector that this class is wrapping.
Definition: Cloneable.hpp:319
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:84
void copy(const Base &other) override final
Definition: Cloneable.hpp:133
Mixin Data
Definition: Cloneable.hpp:86
std::unique_ptr< Base > clone() const override final
Definition: Cloneable.hpp:126
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:92
Definition: Cloneable.hpp:140
OwnerT Owner
Definition: Cloneable.hpp:143
DataT Data
Definition: Cloneable.hpp:142
Definition: BulletCollisionDetector.cpp:65
Definition: SharedLibraryManager.hpp:46