LZW Compressor
 All Classes Files Functions Typedefs
SmartPtr.hh
1 // This is an example smart pointer implementation, which uses the
2 // FSBRefCountAllocator for efficiently allocating reference counters.
3 // See FSBAllocator.html for details.
4 
5 /*===========================================================================
6  This library is released under the MIT license.
7 
8 Copyright (c) 2008 Juha Nieminen
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16 
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
27 =============================================================================*/
28 
29 #ifndef INCLUDE_SMART_PTR_HH
30 #define INCLUDE_SMART_PTR_HH
31 
32 #ifndef SMART_PTR_USE_DEFAULT_ALLOCATOR_FOR_REF_COUNT
33 #include "FSBAllocator.hh"
34 #endif
35 
36 #include <memory>
37 
38 template<typename Data_t, typename Allocator = std::allocator<Data_t> >
39 class SmartPtr: private Allocator
40 {
41  public:
42  SmartPtr(const Allocator& = Allocator());
43  SmartPtr(Data_t*, const Allocator& = Allocator());
44  ~SmartPtr();
45  SmartPtr(const SmartPtr&);
46  SmartPtr& operator=(const SmartPtr&);
47 
48  Data_t* operator->();
49  const Data_t* operator->() const;
50 
51  Data_t& operator*();
52  const Data_t& operator*() const;
53 
54  Data_t* get() const;
55  operator bool() const { return data != 0; }
56  bool isShared() const;
57 
58 
59 
60  private:
61  Data_t* data;
62  size_t* refCount;
63 
64  void decRefCount()
65  {
66  if(refCount && --(*refCount) == 0)
67  {
68  Allocator::destroy(data);
69  Allocator::deallocate(data, 1);
70 
71 #ifdef SMART_PTR_USE_DEFAULT_ALLOCATOR_FOR_REF_COUNT
72  typename Allocator::template
73  rebind<size_t>::other(*this).deallocate(refCount, 1);
74 #else
75  FSBRefCountAllocator().deallocate(refCount, 1);
76 #endif
77  }
78  }
79 };
80 
81 template<typename Data_t, typename Allocator>
82 SmartPtr<Data_t, Allocator>::SmartPtr(const Allocator& a):
83  Allocator(a), data(0), refCount(0)
84 {}
85 
86 template<typename Data_t, typename Allocator>
87 SmartPtr<Data_t, Allocator>::SmartPtr(Data_t* d, const Allocator& a):
88  Allocator(a), data(d),
89 #ifdef SMART_PTR_USE_DEFAULT_ALLOCATOR_FOR_REF_COUNT
90  refCount(typename Allocator::template
91  rebind<size_t>::other(*this).allocate(1))
92 #else
93  refCount(FSBRefCountAllocator().allocate(1))
94 #endif
95 {
96  *refCount = 1;
97 }
98 
99 template<typename Data_t, typename Allocator>
101 {
102  decRefCount();
103 }
104 
105 template<typename Data_t, typename Allocator>
107  Allocator(rhs), data(rhs.data), refCount(rhs.refCount)
108 {
109  if(refCount) ++(*refCount);
110 }
111 
112 template<typename Data_t, typename Allocator>
115 {
116  if(data == rhs.data) return *this;
117 
118  decRefCount();
119  Allocator::operator=(rhs);
120  data = rhs.data;
121  refCount = rhs.refCount;
122  if(refCount) ++(*refCount);
123  return *this;
124 }
125 
126 template<typename Data_t, typename Allocator>
128 {
129  return data;
130 }
131 
132 template<typename Data_t, typename Allocator>
133 const Data_t* SmartPtr<Data_t, Allocator>::operator->() const
134 {
135  return data;
136 }
137 
138 template<typename Data_t, typename Allocator>
140 {
141  return *data;
142 }
143 
144 template<typename Data_t, typename Allocator>
145 const Data_t& SmartPtr<Data_t, Allocator>::operator*() const
146 {
147  return *data;
148 }
149 
150 template<typename Data_t, typename Allocator>
151 Data_t* SmartPtr<Data_t, Allocator>::get() const
152 {
153  return data;
154 }
155 
156 template<typename Data_t, typename Allocator>
158 {
159  return refCount && (*refCount) > 1;
160 }
161 
162 #endif