]> sjero.net Git - qpsnr/blob - src/shared_ptr.h
Initial Commit of QPSNR (version 0.2.1)
[qpsnr] / src / shared_ptr.h
1 /*
2 *       qpsnr (C) 2010 E. Oriani, ema <AT> fastwebnet <DOT> it
3 *
4 *       This file is part of qpsnr.
5 *
6 *       qpsnr is free software: you can redistribute it and/or modify
7 *       it under the terms of the GNU General Public License as published by
8 *       the Free Software Foundation, either version 3 of the License, or
9 *       (at your option) any later version.
10 *
11 *       qpsnr is distributed in the hope that it will be useful,
12 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *       GNU General Public License for more details.
15 *
16 *       You should have received a copy of the GNU General Public License
17 *       along with qpsnr.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 *       myNZB (C) 2009 E. Oriani, ema <AT> fastwebnet <DOT> it
22 *
23 *       This file is part of myNZB.
24 *
25 *       myNZB is free software: you can redistribute it and/or modify
26 *       it under the terms of the GNU General Public License as published by
27 *       the Free Software Foundation, either version 3 of the License, or
28 *       (at your option) any later version.
29 *
30 *       myNZB is distributed in the hope that it will be useful,
31 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
32 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 *       GNU General Public License for more details.
34 *
35 *       You should have received a copy of the GNU General Public License
36 *       along with myNZB.  If not, see <http://www.gnu.org/licenses/>.
37 */
38
39 #ifndef _SHARED_PTR_H_
40 #define _SHARED_PTR_H_
41
42 template<typename T>
43 class shared_ptr
44 {
45 public:
46         typedef T               type;
47         typedef T*              ptr_type;
48         typedef unsigned int    count_type;
49         
50         shared_ptr() :
51         _ptr(0),
52         _count(0)
53         {
54         }
55
56         shared_ptr(const ptr_type& in) :
57         _ptr(in)
58         {
59                 _count = new (std::nothrow) count_type;
60                 if (!_count)
61                 {
62                         delete in;
63                         throw std::bad_alloc();
64                 }
65                 *_count = 1;
66         }
67         
68         shared_ptr(const shared_ptr& in)
69         {
70                 _ptr = in._ptr;
71                 _count = in._count;
72                 if (_count) *_count += 1;
73         }
74         
75         shared_ptr& operator=(const shared_ptr& in)
76         {
77                 if (_ptr != in._ptr)
78                 {
79                         if (_ptr)
80                         {
81                                 if (!--*_count)
82                                 {
83                                         delete _ptr;
84                                         delete _count;
85                                 }
86                         }
87                         _ptr = in._ptr;
88                         _count = in._count;
89                         if (_count) *_count += 1;
90                 }
91                 return *this;
92         }
93         
94         ~shared_ptr()
95         {
96                 if (_count)
97                 {
98                         if (!--*_count)
99                         {
100                                 delete _ptr;
101                                 delete _count;
102                         }
103                 }
104         }
105         
106         ptr_type get(void) const
107         {
108                 return _ptr;
109         }
110         
111         count_type get_count(void) const
112         {
113                 if (_count) return *_count;
114                 return 0;
115         }
116         
117         ptr_type operator->() const
118         {
119                 return _ptr;
120         }
121
122         type& operator*() const
123         {
124                 return *_ptr;
125         }
126         
127         template<typename U>
128         bool operator==(shared_ptr<U>& in)
129         {
130                 return _ptr == in._ptr;
131         }
132         
133         template<typename U>
134         bool operator!=(shared_ptr<U>& in)
135         {
136                 return _ptr != in._ptr;
137         }
138         
139         template<typename U>
140         bool operator<(shared_ptr<U>& in)
141         {
142                 return _ptr < in._ptr;
143         }
144 private:
145         ptr_type        _ptr;
146         count_type      *_count;
147 };
148
149 #endif //_SHARED_PTR_H_