summaryrefslogtreecommitdiff
path: root/contrib/utility/Utility/ReferenceCounting/SmartPtr.hpp
blob: 682f0678ee150a1ce0d07c7944039727ee9ba0db (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
// file      : Utility/ReferenceCounting/SmartPtr.hpp
// author    : Boris Kolpackov <boris@kolpackov.net>
// copyright : Copyright (c) 2002-2003 Boris Kolpackov
// license   : http://kolpackov.net/license.html

#ifndef UTILITY_REFERENCE_COUNTING_SMART_PTR_HPP
#define UTILITY_REFERENCE_COUNTING_SMART_PTR_HPP

#include "Utility/ExH/Compound.hpp"
#include "Utility/ExH/Logic/DescriptiveException.hpp"

#include "Utility/ReferenceCounting/Interface.hpp"

namespace Utility
{
  namespace ReferenceCounting
  {
    template <typename T>
    class SmartPtr
    {
    public:
      typedef
      T
      Type;

      class NotInitialized_ {};
      typedef
      ExH::Compound<NotInitialized_, ExH::Logic::DescriptiveException>
      NotInitialized;

    public:
      // c-tor's

      SmartPtr () throw ();
      SmartPtr (Type* ptr) throw ();
      SmartPtr (SmartPtr<Type> const& s_ptr)
        throw (Interface::Exception, Interface::SystemException);

      template <typename Other>
      SmartPtr (SmartPtr<Other> const& s_ptr)
        throw (Interface::Exception, Interface::SystemException);

      // d-tor

      ~SmartPtr () throw ();

      // assignment & copy-assignment operators

      SmartPtr<Type>&
      operator= (Type* ptr) throw ();

      SmartPtr<Type>&
      operator= (SmartPtr<Type> const& s_ptr)
        throw (Interface::Exception, Interface::SystemException);

      template <typename Other>
      SmartPtr<Type>&
      operator= (SmartPtr<Other> const& s_ptr)
        throw (Interface::Exception, Interface::SystemException);

      //conversions

      operator Type* () const throw ();

      // accessors

      Type*
      operator-> () const throw (NotInitialized);

      Type*
      in () const throw ();

      Type*
      retn() throw ();

    private:
      Type* ptr_;
    };

    // Specialization of add_ref function for SmartPtr<T>
    template <typename T>
    T*
    add_ref (SmartPtr<T> const& ptr)
      throw (Interface::Exception, Interface::SystemException);


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

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

#include "Utility/ReferenceCounting/SmartPtr.tpp"

#endif  // UTILITY_REFERENCE_COUNTING_SMART_PTR_HPP
//$Id$