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