Geogram Version 1.9.7
A programming library of geometric algorithms
Loading...
Searching...
No Matches
smart_pointer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GEOGRAM_BASIC_SMART_POINTER
41#define GEOGRAM_BASIC_SMART_POINTER
42
46
52namespace GEO {
53
54 /************************************************************************/
55
75 template <class T>
77 public:
82 pointer_(nullptr) {
83 }
84
91 SmartPointer(T* ptr) :
92 pointer_(ptr) {
93 T::ref(pointer_);
94 }
95
103 pointer_(rhs) {
104 T::ref(pointer_);
105 }
106
114 T::unref(pointer_);
115 }
116
125 if(ptr != pointer_) {
126 T::unref(pointer_);
127 pointer_ = ptr;
128 T::ref(pointer_);
129 }
130 return *this;
131 }
132
141 T* rhs_p = rhs.get();
142 if(rhs_p != pointer_) {
143 T::unref(pointer_);
144 pointer_ = rhs_p;
145 T::ref(pointer_);
146 }
147 return *this;
148 }
149
157 void reset() {
158 T::unref(pointer_);
159 pointer_ = nullptr;
160 }
161
169 T* operator-> () const {
170 geo_assert(pointer_ != nullptr);
171 return pointer_;
172 }
173
181 T& operator* () const {
182 geo_assert(pointer_ != nullptr);
183 return *pointer_;
184 }
185
190 operator T* () const {
191 return pointer_;
192 }
193
198 T* get() const {
199 return pointer_;
200 }
201
206 bool is_null() const {
207 return pointer_ == nullptr;
208 }
209
210 private:
211 T* pointer_;
212 };
213
222 template <class T1, class T2>
223 inline bool operator== (
224 const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs
225 ) {
226 return lhs.get() == rhs.get();
227 }
228
237 template <class T1, class T2>
238 inline bool operator!= (
239 const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs
240 ) {
241 return lhs.get() != rhs.get();
242 }
243
252 template <class T1, class T2>
253 inline bool operator< (
254 const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs
255 ) {
256 return lhs.get() < rhs.get();
257 }
258
267 template <class T1, class T2>
268 inline bool operator<= (
269 const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs
270 ) {
271 return lhs.get() <= rhs.get();
272 }
273
282 template <class T1, class T2>
283 inline bool operator> (
284 const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs
285 ) {
286 return lhs.get() > rhs.get();
287 }
288
297 template <class T1, class T2>
298 inline bool operator>= (
299 const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs
300 ) {
301 return lhs.get() >= rhs.get();
302 }
303}
304
305#endif
Assertion checking mechanism.
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:149
A smart pointer with reference-counted copy semantics.
void reset()
Resets pointer.
bool is_null() const
Check if stored pointer is null.
SmartPointer()
Creates an empty smart pointer.
T * operator->() const
Dereferences object member.
~SmartPointer()
Deletes a smart pointer.
T & operator*() const
Dereferences object.
SmartPointer< T > & operator=(T *ptr)
Assignment from a pointer.
SmartPointer(const SmartPointer< T > &rhs)
Create a copy of a smart pointer.
T * get() const
Get pointer.
SmartPointer(T *ptr)
Creates a smart pointer that owns a pointer.
Common include file, providing basic definitions. Should be included before anything else by all head...
Types and functions for memory manipulation.
Global Vorpaline namespace.
Definition basic.h:55