vsg  1.1.0
VulkanSceneGraph library
observer_ptr.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission 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 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE 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 
17 namespace vsg
18 {
19 
22  template<class T>
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); }
101 
102  template<class R>
103  bool operator==(const observer_ptr<R>& rhs) const { return (rhs._ptr == _ptr); }
104 
105  template<class R>
106  bool operator!=(const observer_ptr<R>& rhs) const { return (rhs._ptr != _ptr); }
107 
108  template<class R>
109  bool operator<(const R* rhs) const { return (rhs < _ptr); }
110 
111  template<class R>
112  bool operator==(const R* rhs) const { return (rhs == _ptr); }
113 
114  template<class R>
115  bool operator!=(const R* rhs) const { return (rhs != _ptr); }
116 
117  bool valid() const noexcept { return _auxiliary.valid() && _auxiliary->getConnectedObject() != nullptr; }
118 
119  explicit operator bool() const noexcept { return valid(); }
120 
122  vsg::ref_ptr<T> ref_ptr() const { return vsg::ref_ptr<T>(*this); }
123 
125  template<class R>
126  operator vsg::ref_ptr<R>() const
127  {
128  if (!_auxiliary) return vsg::ref_ptr<R>();
129 
130  std::scoped_lock<std::mutex> guard(_auxiliary->getMutex());
131  if (_auxiliary->getConnectedObject() != nullptr)
132  return vsg::ref_ptr<R>(_ptr);
133  else
134  return {};
135  }
136 
137  protected:
138  template<class R>
139  friend class observer_ptr;
140 
141  T* _ptr;
142  vsg::ref_ptr<Auxiliary> _auxiliary;
143  };
144 
145 } // 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:122
Definition: ref_ptr.h:22