DART  6.6.2
sub_ptr.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_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>
44  : mT(nullptr),
45  mSubjectBase(nullptr)
46 {
47  // Do nothing
48 }
49 
50 //==============================================================================
51 template <class T>
52 sub_ptr<T>::sub_ptr(T* _ptr) : mT(nullptr), mSubjectBase(nullptr)
53 {
54  set(_ptr);
55 }
56 
57 //==============================================================================
58 template <class T>
60 {
61  set(_sp.get());
62  return *this;
63 }
64 
65 //==============================================================================
66 template <class T>
68 {
69  set(_ptr);
70  return *this;
71 }
72 
73 //==============================================================================
74 template <class T>
76 {
77  return mT;
78 }
79 
80 //==============================================================================
81 template <class T>
83 {
84  return *mT;
85 }
86 
87 //==============================================================================
88 template <class T>
90 {
91  return mT;
92 }
93 
94 //==============================================================================
95 template <class T>
96 T* sub_ptr<T>::get() const
97 {
98  return mT;
99 }
100 
101 //==============================================================================
102 template <class T>
103 void sub_ptr<T>::set(T* _ptr)
104 {
105  if(mT == _ptr)
106  return;
107 
108  removeSubject(mSubjectBase);
109  mSubjectBase = dynamic_cast<Subject*>(_ptr);
110  mT = _ptr;
111  addSubject(mSubjectBase);
112 }
113 
114 //==============================================================================
115 template <class T>
117 {
118  return mT != nullptr;
119 }
120 
121 //==============================================================================
122 template <class T>
124 {
125  if(_subject == mSubjectBase)
126  {
127  mT = nullptr;
128  mSubjectBase = nullptr;
129  }
130 }
131 
132 } // namespace common
133 } // namespace dart
134 
135 #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:89
sub_ptr & operator=(const sub_ptr &_sp)
Change the Subject of this sub_ptr.
Definition: sub_ptr.hpp:59
void set(T *_ptr)
Set the subject of this sub_ptr.
Definition: sub_ptr.hpp:103
sub_ptr()
Default constructor.
Definition: sub_ptr.hpp:43
T * get() const
Get the Subject of this sub_ptr.
Definition: sub_ptr.hpp:96
void handleDestructionNotification(const Subject *_subject) override
Called by receiveDestructionNotification().
Definition: sub_ptr.hpp:123
T & operator*() const
Dereferencing operator.
Definition: sub_ptr.hpp:82
bool valid()
True if and only if this sub_ptr still points to a valid Subject.
Definition: sub_ptr.hpp:116
Definition: BulletCollisionDetector.cpp:63