vsg 1.1.13
VulkanSceneGraph library
Loading...
Searching...
No Matches
observer_ptr.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
13</editor-fold> */
14
15#include <vsg/core/Auxiliary.h>
16
17namespace vsg
18{
19
22 template<class T>
23 class observer_ptr
24 {
25 public:
26 using element_type = T;
27
28 observer_ptr() :
29 _ptr(nullptr) {}
30
31 observer_ptr(const observer_ptr& rhs) :
32 _ptr(rhs._ptr),
33 _auxiliary(rhs._auxiliary)
34 {
35 }
36
37 template<class R>
38 explicit observer_ptr(R* ptr) :
39 _ptr(ptr),
40 _auxiliary(ptr ? ptr->getOrCreateAuxiliary() : nullptr)
41 {
42 }
43
44 template<class R>
45 explicit observer_ptr(const observer_ptr<R>& ptr) :
46 _ptr(ptr._ptr),
47 _auxiliary(ptr._auxiliary)
48 {
49 }
50
51 template<class R>
52 explicit observer_ptr(const vsg::ref_ptr<R>& ptr) :
53 _ptr(ptr.get()),
54 _auxiliary(ptr.valid() ? ptr->getOrCreateAuxiliary() : nullptr)
55 {
56 }
57
58 ~observer_ptr()
59 {
60 }
61
62 void reset()
63 {
64 _ptr = nullptr;
65 _auxiliary.reset();
66 }
67
68 template<class R>
69 observer_ptr& operator=(R* ptr)
70 {
71 _ptr = ptr;
72 _auxiliary = ptr ? ptr->getOrCreateAuxiliary() : nullptr;
73 return *this;
74 }
75
76 observer_ptr& operator=(const observer_ptr& rhs)
77 {
78 _ptr = rhs._ptr;
79 _auxiliary = rhs._auxiliary;
80 return *this;
81 }
82
83 template<class R>
84 observer_ptr& operator=(const observer_ptr<R>& rhs)
85 {
86 _ptr = rhs._ptr;
87 _auxiliary = rhs._auxiliary;
88 return *this;
89 }
90
91 template<class R>
92 observer_ptr& operator=(const vsg::ref_ptr<R>& rhs)
93 {
94 _ptr = rhs.get();
95 _auxiliary = rhs.valid() ? rhs->getOrCreateAuxiliary() : nullptr;
96 return *this;
97 }
98
99 template<class R>
100 bool operator<(const observer_ptr<R>& rhs) const { return (rhs._ptr < _ptr) || (rhs._ptr == _ptr && rhs._auxiliary < _auxiliary); }
101
102 template<class R>
103 bool operator==(const observer_ptr<R>& rhs) const { return (rhs._auxiliary == _auxiliary); }
104
105 template<class R>
106 bool operator!=(const observer_ptr<R>& rhs) const { return (rhs._auxiliary != _auxiliary); }
107
108 template<class R>
109 bool operator<(const R* rhs) const
110 {
111 if (rhs < _ptr)
112 return true;
113 if (_ptr == nullptr)
114 return false;
115 return rhs->getAuxiliary() < _auxiliary;
116 }
117
118 template<class R>
119 bool operator==(const R* rhs) const
120 {
121 if (rhs != _ptr)
122 return false;
123 if (rhs == nullptr)
124 return true;
125 return rhs->getAuxiliary() == _auxiliary;
126 }
127
128 template<class R>
129 bool operator!=(const R* rhs) const
130 {
131 if (rhs != _ptr)
132 return true;
133 if (rhs == nullptr)
134 return false;
135 return rhs->getAuxiliary() != _auxiliary;
136 }
137
138 template<class R>
139 bool operator<(const vsg::ref_ptr<R>& rhs) const
140 {
141 if (rhs.get() < _ptr)
142 return true;
143 if (_ptr == nullptr)
144 return false;
145 return rhs->getAuxiliary() < _auxiliary;
146 }
147
148 template<class R>
149 bool operator==(const vsg::ref_ptr<R>& rhs) const
150 {
151 if (rhs.get() != _ptr)
152 return false;
153 if (rhs == nullptr)
154 return true;
155 return rhs->getAuxiliary() == _auxiliary;
156 }
157
158 template<class R>
159 bool operator!=(const vsg::ref_ptr<R>& rhs) const
160 {
161 if (rhs.get() != _ptr)
162 return true;
163 if (rhs == nullptr)
164 return false;
165 return rhs->getAuxiliary() != _auxiliary;
166 }
167
168 bool valid() const noexcept { return _auxiliary.valid() && _auxiliary->getConnectedObject() != nullptr; }
169
170 explicit operator bool() const noexcept { return valid(); }
171
173 vsg::ref_ptr<T> ref_ptr() const { return vsg::ref_ptr<T>(*this); }
174
176 template<class R>
177 operator vsg::ref_ptr<R>() const
178 {
179 if (!_auxiliary) return vsg::ref_ptr<R>();
180
181 std::scoped_lock<std::mutex> guard(_auxiliary->getMutex());
182 if (_auxiliary->getConnectedObject() != nullptr)
183 return vsg::ref_ptr<R>(_ptr);
184 else
185 return {};
186 }
187
188 protected:
189 template<class R>
190 friend class observer_ptr;
191
192 T* _ptr;
193 vsg::ref_ptr<Auxiliary> _auxiliary;
194 };
195
196} // namespace vsg
Definition observer_ptr.h:24
vsg::ref_ptr< T > ref_ptr() const
convert observer_ptr into a ref_ptr so that Object pointed to can be safely accessed.
Definition observer_ptr.h:173
Definition ref_ptr.h:22