OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimRefPtr.h
Go to the documentation of this file.
1 /* -*-c++-*- ossim - Copyright (C) since 2004 Garrett Potts
2  *
3  * This was taken directly from OpenSceneGraph and will retain OSGGPL license.
4  * This is basically an MIT.
5  *
6 */
7 #ifndef ossimRefPtr_HEADER
8 #define ossimRefPtr_HEADER
10 #include <stddef.h>
11 #include <cstddef>
12 
13 template<class T> class ossimRefPtr
14 {
15  public:
16  typedef T element_type;
17 
18  ossimRefPtr() :m_ptr(0) {}
19  ossimRefPtr(T* t):m_ptr(t) { if (m_ptr) m_ptr->ref(); }
20  ossimRefPtr(const ossimRefPtr& rp):m_ptr(rp.m_ptr) { if (m_ptr) m_ptr->ref(); }
21  ~ossimRefPtr() { if (m_ptr) m_ptr->unref(); m_ptr=0; }
22 
23  inline ossimRefPtr& operator = (const ossimRefPtr& rp)
24  {
25  if (m_ptr==rp.m_ptr)
26  return *this;
27  T* tmpPtr = m_ptr;
28  m_ptr = rp.m_ptr;
29  if (m_ptr)
30  m_ptr->ref();
31  // unref second to prevent any deletion of any object which might
32  // be referenced by the other object. i.e rp is child of the
33  // original _ptr.
34  if (tmpPtr)
35  tmpPtr->unref();
36  return *this;
37  }
38 
39  inline ossimRefPtr& operator = (T* ptr)
40  {
41  if (m_ptr==ptr)
42  return *this;
43  T* tmpPtr = m_ptr;
44  m_ptr = ptr;
45  if (m_ptr)
46  m_ptr->ref();
47  // unref second to prevent any deletion of any object which might
48  // be referenced by the other object. i.e rp is child of the
49  // original m_ptr.
50  if (tmpPtr)
51  tmpPtr->unref();
52  return *this;
53  }
54 
55  // comparison operators for ossimRefPtr.
56  inline bool operator < (const ossimRefPtr& rp) const { return (m_ptr<rp.m_ptr); }
57  inline bool operator > (const ossimRefPtr& rp) const { return (m_ptr>rp.m_ptr); }
58 
59  // comparion operator for const T*.
60  inline bool operator == (const T* ptr) const { return (m_ptr==ptr); }
61  inline bool operator != (const T* ptr) const { return (m_ptr!=ptr); }
62  inline bool operator < (const T* ptr) const { return (m_ptr<ptr); }
63  inline bool operator > (const T* ptr) const { return (m_ptr>ptr); }
64 
65  inline T& operator*() { return *m_ptr; }
66 
67  inline const T& operator*() const { return *m_ptr; }
68 
69  inline T* operator->() { return m_ptr; }
70 
71  inline const T* operator->() const { return m_ptr; }
72 
73  inline bool operator!() const { return m_ptr==0L; }
74 
75  inline bool valid() const { return m_ptr!=0L; }
76 
81  explicit operator bool() const { return m_ptr == 0 ? false : true; }
82 
83  inline T* get() { return m_ptr; }
84 
85  inline const T* get() const { return m_ptr; }
86 
91  inline T* take() { return release();}
92 
93  inline T* release() { T* tmp=m_ptr; if (m_ptr) m_ptr->unref_nodelete(); m_ptr=0; return tmp;}
94 
95  private:
96  T* m_ptr;
97 };
98 
99 // Copied from std::shared_ptr:
100 template<typename _Tp1, typename _Tp2> inline bool
101  operator==(const ossimRefPtr<_Tp1>& __a, const ossimRefPtr<_Tp2>& __b) noexcept
102  { return __a.get() == __b.get(); }
103 
104 template<typename _Tp> inline bool operator==(const ossimRefPtr<_Tp>& __a, std::nullptr_t) noexcept
105  { return !__a; }
106 
107 template<typename _Tp> inline bool operator==(std::nullptr_t, const ossimRefPtr<_Tp>& __a) noexcept
108  { return !__a; }
109 
110 template<typename _Tp1, typename _Tp2> inline bool
111  operator!=(const ossimRefPtr<_Tp1>& __a, const ossimRefPtr<_Tp2>& __b) noexcept
112  { return __a.get() != __b.get(); }
113 
114 template<typename _Tp> inline bool operator!=(const ossimRefPtr<_Tp>& __a, std::nullptr_t) noexcept
115  { return (bool)__a; }
116 
117 template<typename _Tp> inline bool operator!=(std::nullptr_t, const ossimRefPtr<_Tp>& __a) noexcept
118  { return (bool)__a; }
119 
120 
121 #endif
ossimRefPtr(T *t)
Definition: ossimRefPtr.h:19
bool operator!=(const ossimRefPtr< _Tp1 > &__a, const ossimRefPtr< _Tp2 > &__b) noexcept
Definition: ossimRefPtr.h:111
bool valid() const
Definition: ossimRefPtr.h:75
T & operator*()
Definition: ossimRefPtr.h:65
ossimRefPtr & operator=(const ossimRefPtr &rp)
Definition: ossimRefPtr.h:23
bool operator!() const
Definition: ossimRefPtr.h:73
bool operator<(const ossimRefPtr &rp) const
Definition: ossimRefPtr.h:56
bool operator==(const T *ptr) const
Definition: ossimRefPtr.h:60
const T & operator*() const
Definition: ossimRefPtr.h:67
T * take()
take control over the object pointed to by ref_ptr, unreference but do not delete even if ref count g...
Definition: ossimRefPtr.h:91
ossimRefPtr(const ossimRefPtr &rp)
Definition: ossimRefPtr.h:20
T * release()
Definition: ossimRefPtr.h:93
bool operator==(const ossimRefPtr< _Tp1 > &__a, const ossimRefPtr< _Tp2 > &__b) noexcept
Definition: ossimRefPtr.h:101
bool operator>(const ossimRefPtr &rp) const
Definition: ossimRefPtr.h:57
T * operator->()
Definition: ossimRefPtr.h:69
const T * operator->() const
Definition: ossimRefPtr.h:71
bool operator!=(const T *ptr) const
Definition: ossimRefPtr.h:61