summaryrefslogtreecommitdiff
path: root/contrib/utility/Utility/ReferenceCounting/SmartPtr.tpp
blob: 6596c67f1b173e5cf1b677590f667f5049248dd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// file      : Utility/ReferenceCounting/SmartPtr.tpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

namespace Utility
{
  namespace ReferenceCounting
  {
    // c-tor's & d-tor

    template <typename T>
    SmartPtr<T>::
    SmartPtr () throw ()
        : ptr_ (0)
    {
    }

    template <typename T>
    SmartPtr<T>::
    SmartPtr (Type* ptr) throw ()
        : ptr_ (ptr)
    {
    }

    template <typename T>
    SmartPtr<T>::
    SmartPtr (SmartPtr<Type> const& s_ptr)
      throw (Interface::Exception, Interface::SystemException)
        : ptr_ (add_ref (s_ptr.in ()))
    {
    }

    template <typename T>
    template <typename Other>
    SmartPtr<T>::
    SmartPtr (SmartPtr<Other> const& s_ptr)
      throw (Interface::Exception, Interface::SystemException)
        : ptr_ (add_ref (s_ptr.in ()))
    {
    }


    template <typename T>
    SmartPtr<T>::
    ~SmartPtr () throw ()
    {
      // This is an additional catch-all layer to protect from
      // non-conformant Type.
      try
      {
        if (ptr_ != 0) ptr_->remove_ref ();
      }
      catch (...)
      {
      }
    }

    // operator=

    template <typename T>
    SmartPtr<T>& SmartPtr<T>::
    operator= (Type* ptr) throw ()
    {
      if (ptr_ != 0) ptr_->remove_ref ();
      ptr_ = ptr;
      return *this;
    }


    template <typename T>
    SmartPtr<T>& SmartPtr<T>::
    operator= (SmartPtr<Type> const& s_ptr)
      throw (Interface::Exception, Interface::SystemException)
    {
      Type* old_ptr (ptr_);
      Type* new_ptr (add_ref (s_ptr.in ())); // this can throw
      if (old_ptr != 0) old_ptr->remove_ref ();

      ptr_ = new_ptr; // commit

      return *this;
    }


    template <typename T>
    template <typename Other>
    SmartPtr<T>& SmartPtr<T>::
    operator= (SmartPtr<Other> const& s_ptr)
      throw (Interface::Exception, Interface::SystemException)
    {
      Type*  old_ptr (ptr_);
      Other* new_ptr (add_ref (s_ptr.in ())); // this can throw
      if (old_ptr != 0) old_ptr->remove_ref ();

      ptr_ = new_ptr; // commit

      return *this;
    }

    // conversions

    template <typename T>
    SmartPtr<T>::
    operator T* () const throw ()
    {
      return ptr_;
    }


    // accessors

    template <typename T>
    T* SmartPtr<T>::
    operator-> () const throw (NotInitialized)
    {
      if (ptr_ == 0)
      {
        throw NotInitialized(
          "Utility::ReferenceCounting::SmartPtr::operator-> : "
          "unable to dereference NULL pointer.");
      }
      return ptr_;
    }

    template <typename T>
    T* SmartPtr<T>::
    in () const throw ()
    {
      return ptr_;
    }

    template <typename T>
    T* SmartPtr<T>::
    retn() throw ()
    {
      Type* ret (ptr_);
      ptr_ = 0;
      return ret;
    }

    // Specialization of add_ref function for SmartPtr<T>
    template <typename T>
    T*
    add_ref (SmartPtr<T> const& ptr)
      throw (Interface::Exception, Interface::SystemException)
    {
      // delegate to generic implementation
      return add_ref (ptr.in ());
    }

    // Dynamic type conversion function for SmartPtr's
    template <typename D, typename S>
    D*
    smart_cast (SmartPtr<S> const& s)
      throw (Interface::Exception, Interface::SystemException)
    {
      return add_ref (dynamic_cast<D*>(s.in ()));
    }

    // Acquisition function
    template <typename T>
    SmartPtr<T>
    acquire (T* ptr) throw (Interface::Exception, Interface::SystemException)
    {
      return SmartPtr<T> (ptr);
    }
  }
}
//$Id$