DART  6.6.2
Cloneable.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2018, 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  set(other);
239  return *this;
240 }
241 
242 //==============================================================================
243 template <class Base, class OwnerT, class DataT,
244  void (*setData)(OwnerT*, const DataT&),
245  DataT (*getData)(const OwnerT*)>
247  ProxyCloneable&& other) -> ProxyCloneable&
248 {
249  set(other);
250  return *this;
251 }
252 
253 //==============================================================================
254 template <class Base, class OwnerT, class DataT,
255  void (*setData)(OwnerT*, const DataT&),
256  DataT (*getData)(const OwnerT*)>
258  const Data& data)
259 {
260  if(mOwner)
261  {
262  (*setData)(mOwner, data);
263  return;
264  }
265 
266  mData = make_unique<Data>(data);
267 }
268 
269 //==============================================================================
270 template <class Base, class OwnerT, class DataT,
271  void (*setData)(OwnerT*, const DataT&),
272  DataT (*getData)(const OwnerT*)>
274  Data&& data)
275 {
276  if(mOwner)
277  {
278  (*setData)(mOwner, data);
279  return;
280  }
281 
282  mData = dart::common::make_unique<Data>(std::move(data));
283 }
284 
285 //==============================================================================
286 template <class Base, class OwnerT, class DataT,
287  void (*setData)(OwnerT*, const DataT&),
288  DataT (*getData)(const OwnerT*)>
290  const ProxyCloneable& other)
291 {
292  set(other.get());
293 }
294 
295 //==============================================================================
296 template <class Base, class OwnerT, class DataT,
297  void (*setData)(OwnerT*, const DataT&),
298  DataT (*getData)(const OwnerT*)>
300  ProxyCloneable&& other)
301 {
302  set(other.get());
303 }
304 
305 //==============================================================================
306 template <class Base, class OwnerT, class DataT,
307  void (*setData)(OwnerT*, const DataT&),
308  DataT (*getData)(const OwnerT*)>
310 {
311  if(mOwner)
312  return (*getData)(mOwner);
313 
314  return *mData;
315 }
316 
317 //==============================================================================
318 template <class Base, class OwnerT, class DataT,
319  void (*setData)(OwnerT*, const DataT&),
320  DataT (*getData)(const OwnerT*)>
322 {
323  return mOwner;
324 }
325 
326 //==============================================================================
327 template <class Base, class OwnerT, class DataT,
328  void (*setData)(OwnerT*, const DataT&),
329  DataT (*getData)(const OwnerT*)>
331 {
332  return mOwner;
333 }
334 
335 //==============================================================================
336 template <class Base, class OwnerT, class DataT,
337  void (*setData)(OwnerT*, const DataT&),
338  DataT (*getData)(const OwnerT*)>
339 std::unique_ptr<Base> ProxyCloneable<
340  Base, OwnerT, DataT, setData, getData>::clone() const
341 {
342  return dart::common::make_unique<ProxyCloneable>(get());
343 }
344 
345 //==============================================================================
346 template <class Base, class OwnerT, class DataT,
347  void (*setData)(OwnerT*, const DataT&),
348  DataT (*getData)(const OwnerT*)>
350  const Base& other)
351 {
352  set(static_cast<const ProxyCloneable&>(other));
353 }
354 
355 //==============================================================================
356 template <typename MapType>
358  const CloneableMap& otherHolder)
359 {
360  copy(otherHolder);
361 }
362 
363 //==============================================================================
364 template <typename MapType>
366  CloneableMap&& otherHolder)
367 {
368  *this = std::move(otherHolder);
369 }
370 
371 //==============================================================================
372 template <typename MapType>
373 CloneableMap<MapType>::CloneableMap(const MapType& otherMap)
374 {
375  copy(otherMap);
376 }
377 
378 //==============================================================================
379 template <typename MapType>
381 {
382  *this = std::move(otherMap);
383 }
384 
385 //==============================================================================
386 template <typename MapType>
388  const CloneableMap& otherHolder)
389 {
390  copy(otherHolder);
391  return *this;
392 }
393 
394 //==============================================================================
395 template <typename MapType>
397  CloneableMap&& otherHolder)
398 {
399  mMap = std::move(otherHolder.mMap);
400 
401  return *this;
402 }
403 
404 //==============================================================================
405 template <typename MapType>
407  const MapType& otherMap)
408 {
409  copy(otherMap);
410  return *this;
411 }
412 
413 //==============================================================================
414 template <typename MapType>
416  MapType&& otherHolder)
417 {
418  mMap = std::move(otherHolder);
419 
420  return *this;
421 }
422 
423 //==============================================================================
424 template <typename MapType>
425 void CloneableMap<MapType>::copy(const CloneableMap& otherMap, bool merge)
426 {
427  copy(otherMap.getMap(), merge);
428 }
429 
430 //==============================================================================
431 template <typename MapType>
432 void CloneableMap<MapType>::copy(const MapType& otherMap, bool merge)
433 {
434  typename MapType::iterator receiver = mMap.begin();
435  typename MapType::const_iterator sender = otherMap.begin();
436 
437  while( otherMap.end() != sender )
438  {
439  if( mMap.end() == receiver )
440  {
441  // If we've reached the end of this CloneableMapHolder's map, then we
442  // should just add each entry
443  mMap[sender->first] = sender->second->clone();
444  ++sender;
445  }
446  else if( receiver->first == sender->first )
447  {
448  if(sender->second)
449  {
450  // If the sender has an object, we should copy it.
451  if(receiver->second)
452  // We should copy instead of cloning the incoming object when possible
453  // so we can avoid the memory allocation overhead of cloning.
454  receiver->second->copy(*sender->second);
455  else
456  receiver->second = sender->second->clone();
457  }
458  else if(!merge)
459  {
460  // If the sender has no object, we should clear this one.
461  receiver->second = nullptr;
462  }
463 
464  ++receiver;
465  ++sender;
466  }
467  else if( receiver->first < sender->first )
468  {
469  if(!merge)
470  {
471  // Clear this entry in the map, because it does not have an analog in
472  // the map that we are copying
473  receiver->second = nullptr;
474  }
475  ++receiver;
476  }
477  else
478  {
479  if(sender->second)
480  {
481  // If receiver has a higher value, then the receiving map does not
482  // contain an entry for this entry of the sending map, and therefore the
483  // entry must be created.
484  mMap[sender->first] = sender->second->clone();
485  }
486  ++sender;
487  }
488  }
489 
490  if(!merge)
491  {
492  while( mMap.end() != receiver )
493  {
494  mMap.erase(receiver++);
495  }
496  }
497 }
498 
499 //==============================================================================
500 template <typename MapType>
502 {
503  copy(otherMap, true);
504 }
505 
506 //==============================================================================
507 template <typename MapType>
508 void CloneableMap<MapType>::merge(const MapType& otherMap)
509 {
510  copy(otherMap, true);
511 }
512 
513 //==============================================================================
514 template <typename MapType>
516 {
517  return mMap;
518 }
519 
520 //==============================================================================
521 template <typename MapType>
522 const MapType& CloneableMap<MapType>::getMap() const
523 {
524  return mMap;
525 }
526 
527 //==============================================================================
528 template <typename T>
529 CloneableVector<T>::CloneableVector(const std::vector<T>& regularVector)
530  : mVector(regularVector)
531 {
532  // Do nothing
533 }
534 
535 //==============================================================================
536 template <typename T>
537 CloneableVector<T>::CloneableVector(std::vector<T>&& regularVector)
538 {
539  mVector = std::move(regularVector);
540 }
541 
542 //==============================================================================
543 template <typename T>
545 {
546  copy(other);
547 }
548 
549 //==============================================================================
550 template <typename T>
552 {
553  copy(other);
554  return this;
555 }
556 
557 //==============================================================================
558 template <typename T>
559 std::unique_ptr< CloneableVector<T> > CloneableVector<T>::clone() const
560 {
561  std::vector<T> clonedVector;
562  clonedVector.reserve(mVector.size());
563 
564  for(const T& entry : mVector)
565  clonedVector.push_back(entry->clone());
566 
567  return common::make_unique< CloneableVector<T> >( std::move(clonedVector) );
568 }
569 
570 //==============================================================================
571 template <typename T>
573 {
574  const std::vector<T>& other = anotherVector.getVector();
575  mVector.resize(other.size());
576 
577  for(std::size_t i=0; i < other.size(); ++i)
578  {
579  if(mVector[i] && other[i])
580  mVector[i]->copy(*other[i]);
581  else if(other[i])
582  mVector[i] = other[i]->clone();
583  else
584  mVector[i] = nullptr;
585  }
586 }
587 
588 //==============================================================================
589 template <typename T>
591 {
592  return mVector;
593 }
594 
595 //==============================================================================
596 template <typename T>
597 const std::vector<T>& CloneableVector<T>::getVector() const
598 {
599  return mVector;
600 }
601 
602 } // namespace common
603 } // namespace dart
604 
605 #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:501
MapType & getMap()
Get the map that is being held.
Definition: Cloneable.hpp:515
void copy(const CloneableMap &otherMap, bool merge=false)
Copy the contents of another cloneable map into this one.
Definition: Cloneable.hpp:425
CloneableMap & operator=(const CloneableMap &otherStates)
Assignment operator.
Definition: Cloneable.hpp:387
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:551
void copy(const CloneableVector< T > &anotherVector)
Copy the contents of another cloneable vector into this one.
Definition: Cloneable.hpp:572
std::unique_ptr< CloneableVector< T > > clone() const
Create a copy of this CloneableVector's contents.
Definition: Cloneable.hpp:559
std::vector< T > & getVector()
Get a reference to the std::vector that this class is wrapping.
Definition: Cloneable.hpp:590
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:309
DataT Data
Definition: Cloneable.hpp:143
std::multimap< dart::dynamics::Shape *, SimpleFrameShapeDnD * >::iterator iterator
Definition: Viewer.cpp:622
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43