Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / ace / Refcounted_Auto_Ptr.h
blobe2cb12072b260fd88024634b5e9a0cee4ea91b49
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Refcounted_Auto_Ptr.h
7 * @author John Tucker <JTucker@infoglide.com>
8 */
9 //=============================================================================
11 #ifndef ACE_REFCOUNTED_AUTO_PTR_H
12 #define ACE_REFCOUNTED_AUTO_PTR_H
14 #include /**/ "ace/pre.h"
16 #include "ace/Atomic_Op.h"
17 #include <memory>
19 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 # pragma once
21 #endif /* ACE_LACKS_PRAGMA_ONCE */
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
25 // Forward decl.
26 template <class X, class ACE_LOCK> class ACE_Refcounted_Auto_Ptr_Rep;
27 template <class X, class ACE_LOCK> class ACE_Refcounted_Auto_Ptr;
29 /**
30 * @class ACE_Refcounted_Auto_Ptr
32 * @brief This class implements support for a reference counted unique_ptr.
33 * Assigning or copying instances of an ACE_Refcounted_Auto_Ptr
34 * will automatically increment the reference count. When the last
35 * instance that references a ACE_Refcounted_Auto_Ptr instance is
36 * destroyed or overwritten, it will invoke delete on its underlying
37 * pointer.
39 * The ACE_Refcounted_Auto_Ptr works by maintaining a reference to a
40 * separate representation object, ACE_Refcounted_Auto_Ptr_Rep. That
41 * separate representation object contains the reference count and
42 * the actual pointer value.
44 template <class X, class ACE_LOCK>
45 class ACE_Refcounted_Auto_Ptr
47 /// Used to define a proper boolean conversion for "if (sp) ..."
48 static void unspecified_bool(ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>***){}
49 typedef void (*unspecified_bool_type)(ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>***);
51 public:
52 /// Constructor that initializes an ACE_Refcounted_Auto_Ptr to
53 /// the specified pointer value.
54 explicit ACE_Refcounted_Auto_Ptr (X *p = nullptr);
56 /// Copy constructor binds the new ACE_Refcounted_Auto_Ptr to the
57 /// representation object referenced by @a r.
58 /// An ACE_Refcounted_Auto_Ptr_Rep is created if necessary.
59 ACE_Refcounted_Auto_Ptr (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r);
61 /// Destructor. Releases the reference to the underlying representation.
62 /// If the release of that reference causes its reference count to reach 0,
63 /// the representation object will also be destroyed.
64 virtual ~ACE_Refcounted_Auto_Ptr ();
66 /// Assignment operator that binds the current object and @a r to the same
67 /// ACE_Refcounted_Auto_Ptr_Rep. An ACE_Refcounted_Auto_Ptr_Rep
68 /// is created if necessary.
69 void operator = (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r);
71 /// Equality operator that returns @c true if both
72 /// ACE_Refcounted_Auto_Ptr objects point to the same underlying
73 /// representation. It does not compare the actual pointers.
74 /**
75 * @note It also returns @c true if both objects have just been
76 * instantiated and not used yet.
78 bool operator == (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const;
80 /// Inequality operator, which is the opposite of equality.
81 bool operator != (const ACE_Refcounted_Auto_Ptr<X, ACE_LOCK> &r) const;
83 /// Redirection operator
84 X *operator-> () const;
86 /// Accessor method.
87 X &operator *() const;
89 /// Check rep easily.
90 bool operator !() const;
92 /// Check rep easily.
93 operator unspecified_bool_type() const;
95 /// Releases the reference to the underlying representation object.
96 /// @retval The pointer value prior to releasing it.
97 X *release ();
99 /// Releases the current pointer value and then sets a new
100 /// pointer value specified by @a p.
101 void reset (X *p = nullptr);
103 /// Get the pointer value.
104 X *get () const;
106 /// Get the reference count value.
107 long count () const;
109 /// Returns @c true if this object does not contain a valid pointer.
110 bool null () const;
112 /// Declare the dynamic allocation hooks.
113 ACE_ALLOC_HOOK_DECLARE;
115 protected:
116 /// the ACE_Refcounted_Auto_Ptr_Rep
117 typedef ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> AUTO_REFCOUNTED_PTR_REP;
119 /// Protect operations on the ACE_Refcounted_Auto_Ptr.
120 AUTO_REFCOUNTED_PTR_REP *rep_;
124 * @class ACE_Refcounted_Auto_Ptr_Rep
126 * @brief An ACE_Refcounted_Auto_Ptr_Rep object encapsulates a pointer
127 * to an object of type X. It uses a lock object of type ACE_LOCK to protect
128 * access to the reference count.
130 * @internal ACE_Refcounted_Auto_Ptr_Rep is used internally by the
131 * ACE_Refcounted_Auto_Ptr class and is only accessible through it.
133 template <class X, class ACE_LOCK>
134 class ACE_Refcounted_Auto_Ptr_Rep
136 private:
137 friend class ACE_Refcounted_Auto_Ptr<X, ACE_LOCK>;
139 /// Get the pointer value.
140 X *get () const;
142 /// Get the reference count value.
143 long count () const;
145 /// Declare the dynamic allocation hooks.
146 ACE_ALLOC_HOOK_DECLARE;
148 // = Encapsulate reference count and object lifetime of instances.
150 /// Allocate a new ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> instance,
151 /// returning NULL if it cannot be created.
152 static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *internal_create (X *p);
154 /// Create a ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> and initialize
155 /// the reference count.
156 static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *create (X *p);
158 /// Increase the reference count on @a rep.
160 /// @retval @a rep if success, 0 if there's an error obtaining the lock
161 /// on @a rep.
162 static ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *attach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *&rep);
164 /// Decreases the reference count and and deletes rep if there are no
165 /// more references to rep.
167 /// Precondition (rep != 0)
168 static void detach (ACE_Refcounted_Auto_Ptr_Rep<X, ACE_LOCK> *&rep);
170 /// Pointer to the result.
171 std::unique_ptr<X> ptr_;
173 /// Reference count.
174 mutable ACE_Atomic_Op<ACE_LOCK, long> ref_count_;
176 private:
177 // = Constructor and destructor private.
178 ACE_Refcounted_Auto_Ptr_Rep (X *p = nullptr);
179 ~ACE_Refcounted_Auto_Ptr_Rep ();
182 ACE_END_VERSIONED_NAMESPACE_DECL
184 #include "ace/Refcounted_Auto_Ptr.inl"
186 #include "ace/Refcounted_Auto_Ptr.cpp"
188 #include /**/ "ace/post.h"
190 #endif /* ACE_REFCOUNTED_AUTO_PTR_H */