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_COMMONCPP
54 
61 template <class T>
62 class Pointer
63 {
64 protected:
65  unsigned *ptrCount;
66  T *ptrObject;
67 
68  void ptrDetach(void)
69  {
70  if(ptrCount && --(*ptrCount)==0) {
71  delete ptrObject;
72  delete ptrCount;
73  }
74  ptrObject = NULL;
75  ptrCount = NULL;
76  }
77 
78 public:
79  explicit Pointer(T* ptr = NULL) : ptrObject(ptr)
80  {
81  ptrCount = new unsigned;
82  *ptrCount = 1;
83  }
84 
85  Pointer(const Pointer<T> &ref)
86  {
87  ptrObject = ref.ptrObject;
88  ptrCount = ref.ptrCount;
89  ++(*ptrCount);
90  }
91 
92  inline virtual ~Pointer()
93  {ptrDetach();}
94 
95 
96  Pointer& operator=(const Pointer<T> &ref)
97  {
98  if(this != &ref) {
99  ptrDetach();
100  ptrObject = ref.ptrObject;
101  ptrCount = ref.ptrCount;
102  ++(*ptrCount);
103  }
104  return *this;
105  }
106 
107  inline T& operator*() const
108  {return *ptrObject;};
109 
110  inline T* getObject() const
111  {return ptrObject;};
112 
113  inline T* operator->() const
114  {return ptrObject;};
115 
116  inline bool operator!() const
117  {return (*ptrCount == 1);};
118 
119  inline int operator++() const
120  {return ++(*ptrCount);};
121 
122  int operator--() const
123  {
124  if(*ptrCount == 1) {
125  delete this;
126  return 0;
127  }
128  return --(*ptrCount);
129  }
130 };
131 
132 END_NAMESPACE
133 
134 #endif
Used to create and manage referece counted pointers.
Definition: pointer.h:62