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_DETAIL_CLONEABLE_HPP_
34 #define DART_COMMON_DETAIL_CLONEABLE_HPP_
35 
38 
39 namespace dart {
40 namespace common {
41 
42 //==============================================================================
43 template <class Base, class Mixin>
45 {
46  // Do nothing
47 }
48 
49 //==============================================================================
50 template <class Base, class Mixin>
51 template <typename ... Args>
53  : Mixin(std::forward<Args>(args)...)
54 {
55  // Do nothing
56 }
57 
58 //==============================================================================
59 template <class Base, class Mixin>
61  : Mixin(mixin)
62 {
63  // Do nothing
64 }
65 
66 //==============================================================================
67 template <class Base, class Mixin>
69  : Mixin(std::move(mixin))
70 {
71  // Do nothing
72 }
73 
74 //==============================================================================
75 template <class Base, class Mixin>
77  const MakeCloneable<Base, Mixin>& other)
78  : Mixin(other)
79 {
80  // Do nothing
81 }
82 
83 //==============================================================================
84 template <class Base, class Mixin>
86  : Mixin(other)
87 {
88  // Do nothing
89 }
90 
91 //==============================================================================
92 template <class Base, class Mixin>
94  const Mixin& mixin)
95 {
96  static_cast<Mixin&>(*this) = mixin;
97  return *this;
98 }
99 
100 //==============================================================================
101 template <class Base, class Mixin>
103 {
104  static_cast<Mixin&>(*this) = std::move(mixin);
105  return *this;
106 }
107 
108 //==============================================================================
109 template <class Base, class Mixin>
111  const MakeCloneable& other)
112 {
113  static_cast<Mixin&>(*this) = static_cast<const Mixin&>(other);
114  return *this;
115 }
116 
117 //==============================================================================
118 template <class Base, class Mixin>
120  MakeCloneable&& other)
121 {
122  static_cast<Mixin&>(*this) = std::move(static_cast<Mixin&&>(other));
123 }
124 
125 //==============================================================================
126 template <class Base, class Mixin>
127 std::unique_ptr<Base> MakeCloneable<Base, Mixin>::clone() const
128 {
129  return common::make_unique<MakeCloneable<Base, Mixin>>(*this);
130 }
131 
132 //==============================================================================
133 template <class Base, class Mixin>
134 void MakeCloneable<Base, Mixin>::copy(const Base& other)
135 {
136  *this = static_cast<const MakeCloneable<Base, Mixin>&>(other);
137 }
138 
139 //==============================================================================
140 template <class Base, class OwnerT, class DataT,
141  void (*setData)(OwnerT*, const DataT&),
142  DataT (*getData)(const OwnerT*)>
144  : mOwner(nullptr),
145  mData(make_unique<Data>())
146 {
147  // Do nothing
148 }
149 
150 //==============================================================================
151 template <class Base, class OwnerT, class DataT,
152  void (*setData)(OwnerT*, const DataT&),
153  DataT (*getData)(const OwnerT*)>
155  OwnerT* owner)
156  : mOwner(owner)
157 {
158  // Do nothing
159 }
160 
161 //==============================================================================
162 template <class Base, class OwnerT, class DataT,
163  void (*setData)(OwnerT*, const DataT&),
164  DataT (*getData)(const OwnerT*)>
165 template <typename... Args>
167  OwnerT* owner, Args&&... args)
168  : mOwner(owner)
169 {
170  set(Data(std::forward<Args>(args)...));
171 }
172 
173 //==============================================================================
174 template <class Base, class OwnerT, class DataT,
175  void (*setData)(OwnerT*, const DataT&),
176  DataT (*getData)(const OwnerT*)>
177 template <typename... Args>
179  Args&&... args)
180  : mOwner(nullptr),
181  mData(dart::common::make_unique<Data>(std::forward<Args>(args)...))
182 {
183  // Do nothing
184 }
185 
186 //==============================================================================
187 template <class Base, class OwnerT, class DataT,
188  void (*setData)(OwnerT*, const DataT&),
189  DataT (*getData)(const OwnerT*)>
191  const ProxyCloneable& other)
192  : mOwner(nullptr),
193  mData(nullptr)
194 {
195  set(other);
196 }
197 
198 //==============================================================================
199 template <class Base, class OwnerT, class DataT,
200  void (*setData)(OwnerT*, const DataT&),
201  DataT (*getData)(const OwnerT*)>
203  ProxyCloneable&& other)
204  : mOwner(nullptr)
205 {
206  set(other);
207 }
208 
209 //==============================================================================
210 template <class Base, class OwnerT, class DataT,
211  void (*setData)(OwnerT*, const DataT&),
212  DataT (*getData)(const OwnerT*)>
214  const Data& data) -> ProxyCloneable&
215 {
216  set(data);
217  return *this;
218 }
219 
220 //==============================================================================
221 template <class Base, class OwnerT, class DataT,
222  void (*setData)(OwnerT*, const DataT&),
223  DataT (*getData)(const OwnerT*)>
225  Data&& data) -> ProxyCloneable&
226 {
227  set(data);
228  return *this;
229 }
230 
231 //==============================================================================
232 template <class Base, class OwnerT, class DataT,
233  void (*setData)(OwnerT*, const DataT&),
234  DataT (*getData)(const OwnerT*)>
236  const ProxyCloneable& other) -> ProxyCloneable&
237 {
238  mOwner = other.mOwner;
239  set(other);
240  return *this;
241 }
242 
243 //==============================================================================
244 template <class Base, class OwnerT, class DataT,
245  void (*setData)(OwnerT*, const DataT&),
246  DataT (*getData)(const OwnerT*)>
248  ProxyCloneable&& other) -> ProxyCloneable&
249 {
250  mOwner = other.mOwner;
251  set(other);
252  return *this;
253 }
254 
255 //==============================================================================
256 template <class Base, class OwnerT, class DataT,
257  void (*setData)(OwnerT*, const DataT&),
258  DataT (*getData)(const OwnerT*)>
260  const Data& data)
261 {
262  if(mOwner)
263  {
264  (*setData)(mOwner, data);
265  return;
266  }
267 
268  mData = make_unique<Data>(data);
269 }
270 
271 //==============================================================================
272 template <class Base, class OwnerT, class DataT,
273  void (*setData)(OwnerT*, const DataT&),
274  DataT (*getData)(const OwnerT*)>
276  Data&& data)
277 {
278  if(mOwner)
279  {
280  (*setData)(mOwner, data);
281  return;
282  }
283 
284  mData = dart::common::make_unique<Data>(std::move(data));
285 }
286 
287 //==============================================================================
288 template <class Base, class OwnerT, class DataT,
289  void (*setData)(OwnerT*, const DataT&),
290  DataT (*getData)(const OwnerT*)>
292  const ProxyCloneable& other)
293 {
294  set(other.get());
295 }
296 
297 //==============================================================================
298 template <class Base, class OwnerT, class DataT,
299  void (*setData)(OwnerT*, const DataT&),
300  DataT (*getData)(const OwnerT*)>
302  ProxyCloneable&& other)
303 {
304  set(other.get());
305 }
306 
307 //==============================================================================
308 template <class Base, class OwnerT, class DataT,
309  void (*setData)(OwnerT*, const DataT&),
310  DataT (*getData)(const OwnerT*)>
312 {
313  if(mOwner)
314  return (*getData)(mOwner);
315 
316  return *mData;
317 }
318 
319 //==============================================================================
320 template <class Base, class OwnerT, class DataT,
321  void (*setData)(OwnerT*, const DataT&),
322  DataT (*getData)(const OwnerT*)>
324 {
325  return mOwner;
326 }
327 
328 //==============================================================================
329 template <class Base, class OwnerT, class DataT,
330  void (*setData)(OwnerT*, const DataT&),
331  DataT (*getData)(const OwnerT*)>
333 {
334  return mOwner;
335 }
336 
337 //==============================================================================
338 template <class Base, class OwnerT, class DataT,
339  void (*setData)(OwnerT*, const DataT&),
340  DataT (*getData)(const OwnerT*)>
341 std::unique_ptr<Base> ProxyCloneable<
342  Base, OwnerT, DataT, setData, getData>::clone() const
343 {
344  return dart::common::make_unique<ProxyCloneable>(get());
345 }
346 
347 //==============================================================================
348 template <class Base, class OwnerT, class DataT,
349  void (*setData)(OwnerT*, const DataT&),
350  DataT (*getData)(const OwnerT*)>
352  const Base& other)
353 {
354  set(static_cast<const ProxyCloneable&>(other));
355 }
356 
357 //==============================================================================
358 template <typename MapType>
360  const CloneableMap& otherHolder)
361 {
362  copy(otherHolder);
363 }
364 
365 //==============================================================================
366 template <typename MapType>
368  CloneableMap&& otherHolder)
369 {
370  *this = std::move(otherHolder);
371 }
372 
373 //==============================================================================
374 template <typename MapType>
375 CloneableMap<MapType>::CloneableMap(const MapType& otherMap)
376 {
377  copy(otherMap);
378 }
379 
380 //==============================================================================
381 template <typename MapType>
383 {
384  *this = std::move(otherMap);
385 }
386 
387 //==============================================================================
388 template <typename MapType>
390  const CloneableMap& otherHolder)
391 {
392  copy(otherHolder);
393  return *this;
394 }
395 
396 //==============================================================================
397 template <typename MapType>
399  CloneableMap&& otherHolder)
400 {
401  mMap = std::move(otherHolder.mMap);
402 
403  return *this;
404 }
405 
406 //==============================================================================
407 template <typename MapType>
409  const MapType& otherMap)
410 {
411  copy(otherMap);
412  return *this;
413 }
414 
415 //==============================================================================
416 template <typename MapType>
418  MapType&& otherHolder)
419 {
420  mMap = std::move(otherHolder);
421 
422  return *this;
423 }
424 
425 //==============================================================================
426 template <typename MapType>
427 void CloneableMap<MapType>::copy(const CloneableMap& otherMap, bool merge)
428 {
429  copy(otherMap.getMap(), merge);
430 }
431 
432 //==============================================================================
433 template <typename MapType>
434 void CloneableMap<MapType>::copy(const MapType& otherMap, bool merge)
435 {
436  typename MapType::iterator receiver = mMap.begin();
437  typename MapType::const_iterator sender = otherMap.begin();
438 
439  while( otherMap.end() != sender )
440  {
441  if( mMap.end() == receiver )
442  {
443  // If we've reached the end of this CloneableMapHolder's map, then we
444  // should just add each entry
445  mMap[sender->first] = sender->second->clone();
446  ++sender;
447  }
448  else if( receiver->first == sender->first )
449  {
450  if(sender->second)
451  {
452  // If the sender has an object, we should copy it.
453  if(receiver->second)
454  // We should copy instead of cloning the incoming object when possible
455  // so we can avoid the memory allocation overhead of cloning.
456  receiver->second->copy(*sender->second);
457  else
458  receiver->second = sender->second->clone();
459  }
460  else if(!merge)
461  {
462  // If the sender has no object, we should clear this one.
463  receiver->second = nullptr;
464  }
465 
466  ++receiver;
467  ++sender;
468  }
469  else if( receiver->first < sender->first )
470  {
471  if(!merge)
472  {
473  // Clear this entry in the map, because it does not have an analog in
474  // the map that we are copying
475  receiver->second = nullptr;
476  }
477  ++receiver;
478  }
479  else
480  {
481  if(sender->second)
482  {
483  // If receiver has a higher value, then the receiving map does not
484  // contain an entry for this entry of the sending map, and therefore the
485  // entry must be created.
486  mMap[sender->first] = sender->second->clone();
487  }
488  ++sender;
489  }
490  }
491 
492  if(!merge)
493  {
494  while( mMap.end() != receiver )
495  {
496  mMap.erase(receiver++);
497  }
498  }
499 }
500 
501 //==============================================================================
502 template <typename MapType>
504 {
505  copy(otherMap, true);
506 }
507 
508 //==============================================================================
509 template <typename MapType>
510 void CloneableMap<MapType>::merge(const MapType& otherMap)
511 {
512  copy(otherMap, true);
513 }
514 
515 //==============================================================================
516 template <typename MapType>
518 {
519  return mMap;
520 }
521 
522 //==============================================================================
523 template <typename MapType>
524 const MapType& CloneableMap<MapType>::getMap() const
525 {
526  return mMap;
527 }
528 
529 //==============================================================================
530 template <typename T>
531 CloneableVector<T>::CloneableVector(const std::vector<T>& regularVector)
532  : mVector(regularVector)
533 {
534  // Do nothing
535 }
536 
537 //==============================================================================
538 template <typename T>
539 CloneableVector<T>::CloneableVector(std::vector<T>&& regularVector)
540 {
541  mVector = std::move(regularVector);
542 }
543 
544 //==============================================================================
545 template <typename T>
547 {
548  copy(other);
549 }
550 
551 //==============================================================================
552 template <typename T>
554 {
555  copy(other);
556  return this;
557 }
558 
559 //==============================================================================
560 template <typename T>
561 std::unique_ptr< CloneableVector<T> > CloneableVector<T>::clone() const
562 {
563  std::vector<T> clonedVector;
564  clonedVector.reserve(mVector.size());
565 
566  for(const T& entry : mVector)
567  clonedVector.push_back(entry->clone());
568 
569  return common::make_unique< CloneableVector<T> >( std::move(clonedVector) );
570 }
571 
572 //==============================================================================
573 template <typename T>
575 {
576  const std::vector<T>& other = anotherVector.getVector();
577  mVector.resize(other.size());
578 
579  for(std::size_t i=0; i < other.size(); ++i)
580  {
581  if(mVector[i] && other[i])
582  mVector[i]->copy(*other[i]);
583  else if(other[i])
584  mVector[i] = other[i]->clone();
585  else
586  mVector[i] = nullptr;
587  }
588 }
589 
590 //==============================================================================
591 template <typename T>
593 {
594  return mVector;
595 }
596 
597 //==============================================================================
598 template <typename T>
599 const std::vector<T>& CloneableVector<T>::getVector() const
600 {
601  return mVector;
602 }
603 
604 } // namespace common
605 } // namespace dart
606 
607 #endif // DART_COMMON_DETAIL_CLONEABLE_HPP_
MapHolder is a templated wrapper class that is used to allow maps of Aspect::State and Aspect::Proper...
Definition: Cloneable.hpp:224
CloneableMap()=default
Default constructor.
void merge(const CloneableMap &otherMap)
Merge the contents of another cloneable map into this one.
Definition: Cloneable.hpp:503
MapType & getMap()
Get the map that is being held.
Definition: Cloneable.hpp:517
void copy(const CloneableMap &otherMap, bool merge=false)
Copy the contents of another cloneable map into this one.
Definition: Cloneable.hpp:427
CloneableMap & operator=(const CloneableMap &otherStates)
Assignment operator.
Definition: Cloneable.hpp:389
The CloneableVector type wraps a std::vector of an Cloneable type allowing it to be handled by an Clo...
Definition: Cloneable.hpp:293
CloneableVector & operator=(const CloneableVector &other)
Call copy(other) on this vector.
Definition: Cloneable.hpp:553
void copy(const CloneableVector< T > &anotherVector)
Copy the contents of another cloneable vector into this one.
Definition: Cloneable.hpp:574
std::unique_ptr< CloneableVector< T > > clone() const
Create a copy of this CloneableVector's contents.
Definition: Cloneable.hpp:561
std::vector< T > & getVector()
Get a reference to the std::vector that this class is wrapping.
Definition: Cloneable.hpp:592
CloneableVector()=default
Default constructor.
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
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
Data get() const
Get the Data of this ProxyCloneable.
Definition: Cloneable.hpp:311
DataT Data
Definition: Cloneable.hpp:143
OwnerT * mOwner
The ProxyCloneable will not store any data in mData if it has an Owner.
Definition: Cloneable.hpp:210
std::multimap< dart::dynamics::Shape *, SimpleFrameShapeDnD * >::iterator iterator
Definition: Viewer.cpp:622
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43