summaryrefslogtreecommitdiff
path: root/ACE/ace/Intrusive_Auto_Ptr.h
blob: 7757e6d5c0c69ff858475c6285a85c68e28faf6f (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
// -*- C++ -*-

//=============================================================================
/**
 *  @file    Intrusive_Auto_Ptr.h
 *
 *  @author Iliyan Jeliazkov <iliyan@ociweb.com>
 *
 *  @note Modeled on http://www.boost.org/boost/intrusive_ptr.hpp
 */
//=============================================================================

#ifndef ACE_INTRUSIVE_AUTO_PTR_H
#define ACE_INTRUSIVE_AUTO_PTR_H

#include /**/ "ace/pre.h"

#include "ace/Atomic_Op.h"

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

// Forward decl.
template <class X> class ACE_Intrusive_Auto_Ptr;

/**
 * @class ACE_Intrusive_Auto_Ptr
 *
 * @brief This class implements support for a reference counted
 * auto_ptr. It assumes reference counting abilities of the
 * parameterizing class.
 *
 * Assigning or copying instances of an ACE_Intrusive_Auto_Ptr will
 * automatically increment the reference count. When the last instance
 * that references a ACE_Intrusive_Auto_Ptr instance is destroyed or
 * overwritten, it will invoke delete on its underlying pointer.
 *
 * The ACE_Intrusive_Auto_Ptr works by maintaining a reference to a
 * separate representation object, ACE_Intrusive_Auto_Ptr_Rep. That
 * separate representation object contains the reference count and the
 * actual pointer value.
 */
template <class X>
class ACE_Intrusive_Auto_Ptr
{
protected:
  /// Used to define a proper boolean conversion for "if (sp) ..."
  static void unspecified_bool(ACE_Intrusive_Auto_Ptr<X>***){}
  typedef void (*unspecified_bool_type)(ACE_Intrusive_Auto_Ptr<X>***);

public:
  /// Enables "if (sp) ..."
  operator unspecified_bool_type() const
    {
        return rep_ == 0 ? 0: unspecified_bool;
    }

  /// Constructor that initializes an ACE_Intrusive_Auto_Ptr to
  /// the specified pointer value.
  ACE_Intrusive_Auto_Ptr (X *p = 0, bool addref = true);

  /// Copy constructor binds the new ACE_Intrusive_Auto_Ptr to the
  /// representation object referenced by @a r.
  /// An ACE_Intrusive_Auto_Ptr_Rep is created if necessary.
  ACE_Intrusive_Auto_Ptr (const ACE_Intrusive_Auto_Ptr<X> &r);

  // Derived class copy ctor
  template<class U> ACE_Intrusive_Auto_Ptr(const ACE_Intrusive_Auto_Ptr<U> & rhs);

  /// Destructor. Releases the reference to the underlying representation.
  /// If the release of that reference causes its reference count to reach 0,
  /// the representation object will also be destroyed.
  virtual ~ACE_Intrusive_Auto_Ptr ();

  /// Assignment operator that binds the current object and @a r to the same
  /// ACE_Intrusive_Auto_Ptr_Rep. An ACE_Intrusive_Auto_Ptr_Rep
  /// is created if necessary.
  void operator = (const ACE_Intrusive_Auto_Ptr<X> &r);

  /// Redirection operator
  X *operator-> () const;

  /// Accessor method.
  X &operator *() const;

  /// Releases the reference to the underlying representation object.
  /// @retval The pointer value prior to releasing it.
  X *release ();

  /// Releases the current pointer value and then sets a new
  /// pointer value specified by @a p.
  void reset (X *p = 0);

  /// Get the pointer value.
  X *get () const;

   /// Get the reference count value.
  long count () const;

  /// Declare the dynamic allocation hooks.
  ACE_ALLOC_HOOK_DECLARE;

protected:
  /// Protect operations on the ACE_Intrusive_Auto_Ptr.
  X *rep_;
};

  /// Equality operator that returns @c true if both
  /// ACE_Intrusive_Auto_Ptr objects point to the same underlying
  /// representation. It does not compare the actual pointers.
  /**
   * @note It also returns @c true if both objects have just been
   *       instantiated and not used yet.
   */
template<class T, class U>
bool operator==(ACE_Intrusive_Auto_Ptr<T> const & a, ACE_Intrusive_Auto_Ptr<U> const & b);

/// Inequality operator, which is the opposite of equality.
template<class T, class U>
bool operator!=(ACE_Intrusive_Auto_Ptr<T> const & a, ACE_Intrusive_Auto_Ptr<U> const & b);

template<class T, class U>
bool operator==(ACE_Intrusive_Auto_Ptr<T> const & a, U * b);

template<class T, class U>
bool operator!=(ACE_Intrusive_Auto_Ptr<T> & a, U * b);

template<class T, class U>
bool operator==(T * a, ACE_Intrusive_Auto_Ptr<U> const & b);

template<class T, class U>
bool operator!=(T * a, ACE_Intrusive_Auto_Ptr<U> const & b);

ACE_END_VERSIONED_NAMESPACE_DECL

#if defined (__ACE_INLINE__)
#include "ace/Intrusive_Auto_Ptr.inl"
#endif /* __ACE_INLINE __ */

#if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
#include "ace/Intrusive_Auto_Ptr.cpp"
#endif /* ACE_TEMPLATES_REQUIRE_SOURCE */

#if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
#pragma implementation ("Intrusive_Auto_Ptr.cpp")
#endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */

#include /**/ "ace/post.h"

#endif /* ACE_INTRUSIVE_AUTO_PTR_H */