DART  6.10.1
sub_ptr.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_SUB_PTR_HPP_
34 #define DART_COMMON_DETAIL_SUB_PTR_HPP_
35 
36 #include "dart/common/sub_ptr.hpp"
37 
38 namespace dart {
39 namespace common {
40 
41 //==============================================================================
42 template <class T>
43 sub_ptr<T>::sub_ptr() : mT(nullptr), mSubjectBase(nullptr)
44 {
45  // Do nothing
46 }
47 
48 //==============================================================================
49 template <class T>
50 sub_ptr<T>::sub_ptr(T* _ptr) : mT(nullptr), mSubjectBase(nullptr)
51 {
52  set(_ptr);
53 }
54 
55 //==============================================================================
56 template <class T>
57 sub_ptr<T>::sub_ptr(const sub_ptr& other) : mT(nullptr), mSubjectBase(nullptr)
58 {
59  set(other.get());
60 }
61 
62 //==============================================================================
63 template <class T>
65 {
66  set(_sp.get());
67  return *this;
68 }
69 
70 //==============================================================================
71 template <class T>
73 {
74  set(_ptr);
75  return *this;
76 }
77 
78 //==============================================================================
79 template <class T>
81 {
82  return mT;
83 }
84 
85 //==============================================================================
86 template <class T>
88 {
89  return *mT;
90 }
91 
92 //==============================================================================
93 template <class T>
95 {
96  return mT;
97 }
98 
99 //==============================================================================
100 template <class T>
101 T* sub_ptr<T>::get() const
102 {
103  return mT;
104 }
105 
106 //==============================================================================
107 template <class T>
108 void sub_ptr<T>::set(T* _ptr)
109 {
110  if (mT == _ptr)
111  return;
112 
113  removeSubject(mSubjectBase);
114  mSubjectBase = dynamic_cast<Subject*>(_ptr);
115  mT = _ptr;
116  addSubject(mSubjectBase);
117 }
118 
119 //==============================================================================
120 template <class T>
122 {
123  return mT != nullptr;
124 }
125 
126 //==============================================================================
127 template <class T>
129 {
130  if (_subject == mSubjectBase)
131  {
132  mT = nullptr;
133  mSubjectBase = nullptr;
134  }
135 }
136 
137 } // namespace common
138 } // namespace dart
139 
140 #endif // DART_COMMON_DETAIL_SUB_PTR_HPP_
The Subject class is a base class for any object that wants to report when it gets destroyed.
Definition: Subject.hpp:58
sub_ptr is a pointer to a Subject.
Definition: sub_ptr.hpp:47
T * operator->() const
Dereferencing operation.
Definition: sub_ptr.hpp:94
sub_ptr & operator=(const sub_ptr &_sp)
Change the Subject of this sub_ptr.
Definition: sub_ptr.hpp:64
void set(T *_ptr)
Set the subject of this sub_ptr.
Definition: sub_ptr.hpp:108
sub_ptr()
Default constructor.
Definition: sub_ptr.hpp:43
T * get() const
Get the Subject of this sub_ptr.
Definition: sub_ptr.hpp:101
void handleDestructionNotification(const Subject *_subject) override
Called by receiveDestructionNotification().
Definition: sub_ptr.hpp:128
T & operator*() const
Dereferencing operator.
Definition: sub_ptr.hpp:87
bool valid()
True if and only if this sub_ptr still points to a valid Subject.
Definition: sub_ptr.hpp:121
Definition: BulletCollisionDetector.cpp:65