ucommon
pointer.h
Go to the documentation of this file.
1 // Copyright (C) 2001-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2010 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 General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 // As a special exception to the GNU General Public License, permission is
19 // granted for additional uses of the text contained in its release
20 // of Common C++.
21 //
22 // The exception is that, if you link the Common C++ library with other
23 // files to produce an executable, this does not by itself cause the
24 // resulting executable to be covered by the GNU General Public License.
25 // Your use of that executable is in no way restricted on account of
26 // linking the Common C++ library code into it.
27 //
28 // This exception does not however invalidate any other reasons why
29 // the executable file might be covered by the GNU General Public License.
30 //
31 // This exception applies only to the code released under the
32 // name Common C++. If you copy code from other releases into a copy of
33 // Common C++, as the General Public License permits, the exception does
34 // not apply to the code that you add in this way. To avoid misleading
35 // anyone as to the status of such modified files, you must delete
36 // this exception notice from them.
37 //
38 // If you write modifications of your own for Common C++, it is your choice
39 // whether to permit this exception to apply to your modifications.
40 // If you do not wish that, delete this exception notice.
41 
47 #ifndef COMMONCPP_POINTER_H_
48 #define COMMONCPP_POINTER_H_
49 
50 #ifndef COMMONCPP_CONFIG_H_
51 #include <commoncpp/config.h>
52 #endif
53 
54 NAMESPACE_COMMONCPP
55 
62 template <class T>
63 class Pointer
64 {
65 protected:
66  unsigned *ptrCount;
67  T *ptrObject;
68 
69  void ptrDetach(void)
70  {
71  if(ptrCount && --(*ptrCount)==0) {
72  delete ptrObject;
73  delete ptrCount;
74  }
75  ptrObject = NULL;
76  ptrCount = NULL;
77  }
78 
79 public:
80  explicit Pointer(T* ptr = NULL) : ptrObject(ptr)
81  {
82  ptrCount = new unsigned;
83  *ptrCount = 1;
84  }
85 
86  Pointer(const Pointer<T> &ref)
87  {
88  ptrObject = ref.ptrObject;
89  ptrCount = ref.ptrCount;
90  ++(*ptrCount);
91  }
92 
93  inline virtual ~Pointer()
94  {ptrDetach();}
95 
96 
97  Pointer& operator=(const Pointer<T> &ref)
98  {
99  if(this != &ref) {
100  ptrDetach();
101  ptrObject = ref.ptrObject;
102  ptrCount = ref.ptrCount;
103  ++(*ptrCount);
104  }
105  return *this;
106  }
107 
108  inline T& operator*() const
109  {return *ptrObject;};
110 
111  inline T* getObject() const
112  {return ptrObject;};
113 
114  inline T* operator->() const
115  {return ptrObject;};
116 
117  inline bool operator!() const
118  {return (*ptrCount == 1);};
119 
120  inline int operator++() const
121  {return ++(*ptrCount);};
122 
123  int operator--() const
124  {
125  if(*ptrCount == 1) {
126  delete this;
127  return 0;
128  }
129  return --(*ptrCount);
130  }
131 };
132 
133 END_NAMESPACE
134 
135 #endif