Graphite  Version 3
An experimental 3D geometry processing program
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 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/assert.h>
45 #include <geogram/basic/memory.h>
46 
52 namespace GEO {
53 
54  /************************************************************************/
55 
75  template <class T>
76  class SmartPointer {
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== (const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs) {
224  return lhs.get() == rhs.get();
225  }
226 
235  template <class T1, class T2>
236  inline bool operator!= (const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs) {
237  return lhs.get() != rhs.get();
238  }
239 
248  template <class T1, class T2>
249  inline bool operator< (const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs) {
250  return lhs.get() < rhs.get();
251  }
252 
261  template <class T1, class T2>
262  inline bool operator<= (const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs) {
263  return lhs.get() <= rhs.get();
264  }
265 
274  template <class T1, class T2>
275  inline bool operator> (const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs) {
276  return lhs.get() > rhs.get();
277  }
278 
287  template <class T1, class T2>
288  inline bool operator>= (const SmartPointer<T1>& lhs, const SmartPointer<T2>& rhs) {
289  return lhs.get() >= rhs.get();
290  }
291 }
292 
293 #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.
Definition: smart_pointer.h:76
void reset()
Resets pointer.
bool is_null() const
Check if stored pointer is null.
SmartPointer()
Creates an empty smart pointer.
Definition: smart_pointer.h:81
~SmartPointer()
Deletes a smart pointer.
SmartPointer< T > & operator=(T *ptr)
Assignment from a pointer.
T & operator*() const
Dereferences object.
T * get() const
Get pointer.
SmartPointer(const SmartPointer< T > &rhs)
Create a copy of a smart pointer.
SmartPointer(T *ptr)
Creates a smart pointer that owns a pointer.
Definition: smart_pointer.h:91
T * operator->() const
Dereferences object member.
Common include file, providing basic definitions. Should be included before anything else by all head...
Types and functions for memory manipulation.
bool operator!=(const aligned_allocator< T1, A1 > &, const aligned_allocator< T2, A2 > &)
Tests whether two aligned_allocators are different.
Definition: memory.h:617
bool operator==(const aligned_allocator< T1, A1 > &, const aligned_allocator< T2, A2 > &)
Tests whether two aligned_allocators are equal.
Definition: memory.h:606
Global Vorpaline namespace.