~ubuntu-branches/ubuntu/wily/cxxtools/wily-proposed

« back to all changes in this revision

Viewing changes to include/cxxtools/smartptr.h

  • Committer: Bazaar Package Importer
  • Author(s): Kari Pahula
  • Date: 2008-06-16 12:24:28 UTC
  • mfrom: (3.1.3 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616122428-7bllgyt1358u779r
Tags: 1.4.8-2
Made libcxxtools-dev depend on libcxxtools6, not libcxxtools5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * cxxtools - general purpose C++-toolbox
 
3
 * Copyright (C) 2005 Tommi Maekitalo
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#ifndef CXXTOOLS_SMARTPTR_H
 
21
#define CXXTOOLS_SMARTPTR_H
 
22
 
 
23
#include <cxxtools/atomicity.h>
 
24
 
 
25
namespace cxxtools
 
26
{
 
27
  template <typename objectType>
 
28
  class RefLinked
 
29
  {
 
30
      mutable const RefLinked* prev;
 
31
      mutable const RefLinked* next;
 
32
 
 
33
    protected:
 
34
      bool unlink(objectType* object)
 
35
      {
 
36
        bool ret = false;
 
37
        if (object)
 
38
        {
 
39
          if (next == this)
 
40
          {
 
41
            ret = true;
 
42
          }
 
43
          else
 
44
          {
 
45
            next->prev = prev;
 
46
            prev->next = next;
 
47
          }
 
48
          next = prev = this;
 
49
        }
 
50
        return ret;
 
51
      }
 
52
 
 
53
      void link(const RefLinked& ptr, objectType* object)
 
54
      {
 
55
        prev = &ptr;
 
56
        next = ptr.next;
 
57
        prev->next = this;
 
58
        next->prev = this;
 
59
      }
 
60
  };
 
61
 
 
62
  template <typename objectType>
 
63
  class InternalRefCounted
 
64
  {
 
65
    protected:
 
66
      bool unlink(objectType* object)
 
67
      {
 
68
        if (object)
 
69
          object->release();
 
70
        return false;
 
71
      }
 
72
 
 
73
      void link(const InternalRefCounted& ptr, objectType* object)
 
74
      {
 
75
        if (object)
 
76
          object->addRef();
 
77
      }
 
78
 
 
79
  };
 
80
 
 
81
  template <typename objectType>
 
82
  class ExternalRefCounted
 
83
  {
 
84
      atomic_t* refs;
 
85
 
 
86
    protected:
 
87
      ExternalRefCounted()
 
88
        : refs(0)  { }
 
89
 
 
90
      bool unlink(objectType* object)
 
91
      {
 
92
        if (object && atomicDecrement(*refs) <= 0)
 
93
        {
 
94
          delete refs;
 
95
          // no need to set refs to 0 since the pointer is either
 
96
          // destroyed or another object is linked in
 
97
          return true;
 
98
        }
 
99
        else
 
100
          return false;
 
101
      }
 
102
 
 
103
      void link(const ExternalRefCounted& ptr, objectType* object)
 
104
      {
 
105
        if (object)
 
106
        {
 
107
          if (ptr.refs == 0)
 
108
            refs = new atomic_t(1);
 
109
          else
 
110
          {
 
111
            refs = ptr.refs;
 
112
            atomicIncrement(*refs);
 
113
          }
 
114
        }
 
115
        else
 
116
          refs = 0;
 
117
      }
 
118
 
 
119
    public:
 
120
      atomic_t getRefs() const
 
121
        { return refs ? *refs : 0; }
 
122
  };
 
123
 
 
124
  template <typename objectType>
 
125
  class DefaultDestroyPolicy
 
126
  {
 
127
    protected:
 
128
      void destroy(objectType* ptr)
 
129
      { delete ptr; }
 
130
  };
 
131
 
 
132
  template <typename objectType>
 
133
  class ArrayDestroyPolicy
 
134
  {
 
135
    protected:
 
136
      void destroy(objectType* ptr)
 
137
      { delete[] ptr; }
 
138
  };
 
139
 
 
140
  /**
 
141
   * Policy-based smart-pointer-class.
 
142
   *
 
143
   * This class works like a pointer, but the destructor deletes the held
 
144
   * object if this is the last reference. The policy specifies, how the class
 
145
   * counts the references. There are 3 policies:
 
146
   *
 
147
   *   ExternalRefCounted: allocates a reference-count
 
148
   *
 
149
   *   InternalRefCounted: the pointed object needs to have a reference-counter
 
150
   *     with methods addRef() and release(). The release-method deletes the
 
151
   *     object, when the reference-count drops to 0.
 
152
   *
 
153
   *   RefLinked: all pointers to a object are linked
 
154
   *
 
155
   * The default policy is InternalRefCounted. Another class
 
156
   * cxxtools::RefCounted implements proper methods for the pointer, which
 
157
   * makes it straight-forward to use.
 
158
   *
 
159
   */
 
160
  template <typename objectType,
 
161
            template <class> class ownershipPolicy = InternalRefCounted,
 
162
            template <class> class destroyPolicy = DefaultDestroyPolicy>
 
163
  class SmartPtr : public ownershipPolicy<objectType>,
 
164
                   public destroyPolicy<objectType>
 
165
  {
 
166
      objectType* object;
 
167
      typedef ownershipPolicy<objectType> ownershipPolicyType;
 
168
      typedef destroyPolicy<objectType> destroyPolicyType;
 
169
 
 
170
    public:
 
171
      SmartPtr()
 
172
        : object(0)
 
173
        {}
 
174
      SmartPtr(objectType* ptr)
 
175
        : object(ptr)
 
176
        { ownershipPolicyType::link(*this, ptr); }
 
177
      SmartPtr(const SmartPtr& ptr)
 
178
        : object(ptr.object)
 
179
        { ownershipPolicyType::link(ptr, ptr.object); }
 
180
      ~SmartPtr()
 
181
        { if (ownershipPolicyType::unlink(object))
 
182
            destroy(object); }
 
183
 
 
184
      SmartPtr& operator= (const SmartPtr& ptr)
 
185
      {
 
186
        if (object != ptr.object)
 
187
        {
 
188
          if (ownershipPolicyType::unlink(object))
 
189
            destroy(object);
 
190
 
 
191
          object = ptr.object;
 
192
 
 
193
          ownershipPolicyType::link(ptr, object);
 
194
        }
 
195
        return *this;
 
196
      }
 
197
 
 
198
      /// The object can be dereferenced like the held object
 
199
      objectType* operator->() const              { return object; }
 
200
      /// The object can be dereferenced like the held object
 
201
      objectType& operator*() const               { return *object; }
 
202
 
 
203
      bool operator== (const objectType* p) const { return object == p; }
 
204
      bool operator!= (const objectType* p) const { return object != p; }
 
205
      bool operator< (const objectType* p) const  { return object < p; }
 
206
      bool operator! () const { return object == 0; }
 
207
      operator bool () const  { return object != 0; }
 
208
 
 
209
      objectType* getPointer()              { return object; }
 
210
      const objectType* getPointer() const  { return object; }
 
211
      operator objectType* ()               { return object; }
 
212
      operator const objectType* () const   { return object; }
 
213
  };
 
214
 
 
215
}
 
216
 
 
217
#endif // CXXTOOLS_SMARTPTR_H
 
218