ucommon
pointer.h
Go to the documentation of this file.
1 // Copyright (C) 2001-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception to the GNU General Public License, permission is
18 // granted for additional uses of the text contained in its release
19 // of Common C++.
20 //
21 // The exception is that, if you link the Common C++ library with other
22 // files to produce an executable, this does not by itself cause the
23 // resulting executable to be covered by the GNU General Public License.
24 // Your use of that executable is in no way restricted on account of
25 // linking the Common C++ library code into it.
26 //
27 // This exception does not however invalidate any other reasons why
28 // the executable file might be covered by the GNU General Public License.
29 //
30 // This exception applies only to the code released under the
31 // name Common C++. If you copy code from other releases into a copy of
32 // Common C++, as the General Public License permits, the exception does
33 // not apply to the code that you add in this way. To avoid misleading
34 // anyone as to the status of such modified files, you must delete
35 // this exception notice from them.
36 //
37 // If you write modifications of your own for Common C++, it is your choice
38 // whether to permit this exception to apply to your modifications.
39 // If you do not wish that, delete this exception notice.
40 
46 #ifndef COMMONCPP_POINTER_H_
47 #define COMMONCPP_POINTER_H_
48 
49 #ifndef COMMONCPP_CONFIG_H_
50 #include <commoncpp/config.h>
51 #endif
52 
53 namespace ost {
54 
61 template <class T>
62 class Pointer
63 {
64 protected:
65  unsigned *ptrCount;
66  T *ptrObject;
67 
68  inline void ptrDetach(void) {
69  if(ptrCount && --(*ptrCount)==0) {
70  delete ptrObject;
71  delete ptrCount;
72  }
73  ptrObject = NULL;
74  ptrCount = NULL;
75  }
76 
77 public:
78  inline explicit Pointer(T* ptr = NULL) : ptrObject(ptr) {
79  ptrCount = new unsigned;
80  *ptrCount = 1;
81  }
82 
83  inline Pointer(const Pointer<T> &ref) {
84  ptrObject = ref.ptrObject;
85  ptrCount = ref.ptrCount;
86  ++(*ptrCount);
87  }
88 
89  inline virtual ~Pointer()
90  {ptrDetach();}
91 
92 
93  inline Pointer& operator=(const Pointer<T> &ref) {
94  if(this != &ref) {
95  ptrDetach();
96  ptrObject = ref.ptrObject;
97  ptrCount = ref.ptrCount;
98  ++(*ptrCount);
99  }
100  return *this;
101  }
102 
103  inline T& operator*() const
104  {return *ptrObject;}
105 
106  inline T* getObject() const
107  {return ptrObject;}
108 
109  inline T* operator->() const
110  {return ptrObject;}
111 
112  inline bool operator!() const
113  {return (*ptrCount == 1);}
114 
115  inline int operator++() const
116  {return ++(*ptrCount);}
117 
118  inline int operator--() const {
119  if(*ptrCount == 1) {
120  delete this;
121  return 0;
122  }
123  return --(*ptrCount);
124  }
125 };
126 
127 } // namespace ost
128 
129 #endif
Definition: address.h:58
Used to create and manage referece counted pointers.
Definition: pointer.h:62