FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
basic_shared_pointer.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (1 Mar. 2011)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 #include <cstddef>
19 
20 namespace cpp {
21 
22 template <typename T>
24 {
25 public:
26  BasicSharedPointer(T* data = NULL);
29 
30  T& operator*() const;
31  T* operator->() const;
32  T* data() const;
33 
34  bool isNull() const;
35 
37 
38 private:
39  void addRef();
40  void releaseRef();
41 
42  T* m_data;
43  unsigned* m_refCount;
44 };
45 
46 //
47 // Implementation
48 //
49 
67 template<typename T>
69  : m_data(data),
70  m_refCount(new unsigned(1))
71 {
72 }
73 
74 template<typename T>
76  : m_data(other.m_data),
77  m_refCount(other.m_refCount)
78 {
79  this->addRef();
80 }
81 
82 template<typename T>
84 {
85  this->releaseRef();
86 }
87 
88 template<typename T>
90 {
91  return *m_data;
92 }
93 
94 template<typename T>
96 {
97  return m_data;
98 }
99 
100 template<typename T>
102 {
103  return m_data;
104 }
105 
106 template<typename T>
108 {
109  return m_data == NULL;
110 }
111 
112 template<typename T>
114 {
115  if (this != &other) {
116  m_data = other.m_data;
117  m_refCount = other.m_refCount;
118  this->addRef();
119  }
120  return *this;
121 }
122 
123 template<typename T>
125 {
126  ++(*m_refCount);
127 }
128 
129 template<typename T>
130 void BasicSharedPointer<T>::releaseRef()
131 {
132  --(*m_refCount);
133  if (*m_refCount == 0) {
134  delete m_data;
135  delete m_refCount;
136  m_data = NULL;
137  m_refCount = NULL;
138  }
139 }
140 
141 } // namespace cpp
bool isNull() const
Definition: basic_shared_pointer.h:107
T * operator->() const
Definition: basic_shared_pointer.h:95
Provides basic sharing of a pointer.
Definition: basic_shared_pointer.h:23
Definition: basic_shared_pointer.h:20
BasicSharedPointer< T > & operator=(const BasicSharedPointer< T > &other)
Definition: basic_shared_pointer.h:113
BasicSharedPointer(T *data=NULL)
Definition: basic_shared_pointer.h:68
~BasicSharedPointer()
Definition: basic_shared_pointer.h:83
T & operator*() const
Definition: basic_shared_pointer.h:89
T * data() const
Definition: basic_shared_pointer.h:101