rcolyer.net
RC Lib  Version 202403231100
APtr.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////
2 //
3 // RC Library, (c) 2011-2014, Ryan A. Colyer
4 // Distributed under the Boost Software License, v1.0. (LICENSE.txt)
5 //
6 /// \file APtr.h
7 /// A reference counting ptr that is auto-deleted as the last copy
8 /// leaves scope.
9 /////////////////////////////////////////////////////////////////////
10 
11 #ifndef RC_APTR_H
12 #define RC_APTR_H
13 
14 #include "RCconfig.h"
15 #include "Types.h"
16 #include "Macros.h"
17 #include "Errors.h"
18 #include <iostream>
19 
20 #ifdef CPP11
21 #include <atomic>
22 #endif
23 
24 
25 namespace RC {
26  /// @cond EXTERNAL
27  template <class T> class Ptr;
28  /// @endcond
29 
30  /// A reference counting ptr that auto-deletes what it points to when the
31  /// last copy leaves scope or is otherwise destructed.
32  /** Use this for scope-controlled garbage collection. Like Ptr,
33  * dereferencing a null APtr throws ErrorMsgNull. For C++11 the reference
34  * counting is thread safe.
35  * @see Ptr
36  * @see RevPtr
37  */
38  template <class T>
39  class APtr {
40  public:
41 
42  /// Default constructor assigning the value of the pointer.
43  /** @param t_ptr The new pointer value.
44  */
45  inline APtr(T* t_ptr = NULL) {
46  helper = new PtrHelper(t_ptr, true);
47  }
48 
49  /// Copy constructor.
50  /** @param other The APtr to copy.
51  */
52  inline APtr(const APtr<T>& other) {
53  other.helper->Add();
54  helper = other.helper;
55  }
56 
57  /// A conversion constructor which creates a new APtr from a Ptr of the
58  /// same or a derived type.
59  /** @param other The Ptr from which the pointer should be used to make
60  * this APtr.
61  */
62  template <class Tderived>
63  inline APtr(const Ptr<Tderived>& other) {
64  helper = new PtrHelper(other, true);
65  }
66 
67  /// An assignment operator, which increases the shared reference count with
68  /// the source APtr.
69  /** @param other The source APtr to assign here.
70  */
71  inline APtr& operator= (const APtr<T> &other) {
72  other.helper->Add();
73 
74  PtrHelper* loc_helper = helper;
75  helper = other.helper;
76  loc_helper->Del();
77 
78  return (*this);
79  }
80 
81 
82  /// Destructor which decreases the shared reference count.
83  /** The object is deleted if the shared reference count reaches 0.
84  */
85  inline ~APtr() {
86  helper->Del();
87  }
88 
89 
90  /// Delete the object pointed to and set the shared pointer for all linked
91  /// APtr's to NULL.
92  /** Note: While this NULLs all the linked APtr's, it cannot affect
93  * raw pointers which have already been extracted or are in use when
94  * this is called. Be mindful of this in threading architecture design.
95  */
96  inline void Delete() {
97  helper->Delete();
98  }
99 
100 
101  /// Extract APtr<const T> and revokes ownership from this object.
103  APtr<const T> retptr(helper->t_ptr);
104  helper->Revoke();
105  return retptr;
106  }
107 
108 #include "PtrSharedCommon.h"
109 #include "PtrCommon.h"
110 
111 
112  protected:
113  /// @cond PROTECTED
114 
115  PtrHelper* helper;
116  /// @endcond
117  };
118 
119 
121 
122 
123  /// Create a new APtr<T> constructed with the given arguments.
124  template<class T, class... Args>
125  APtr<T> MakeAPtr(Args&&... args) {
126  return APtr<T>(new T(std::forward<Args>(args)...));
127  }
128 }
129 
130 
131 #endif // RC_APTR_H
132 
Provides informative exception handling.
Provides a set of convenience macros for metaprogramming and debugging.
#define RC_STREAM_RAWWRAP(Type)
Provides stream output for a class via Raw.
Definition: Macros.h:270
Common components for the *Ptr.h files. Do not include this directly.
Common components for the shared *Ptr.h files. Do not include directly.
The version information and configuration settings for RC Lib.
Provides typedefs and routines for working with primitives.
A reference counting ptr that auto-deletes what it points to when the last copy leaves scope or is ot...
Definition: APtr.h:39
APtr(const Ptr< Tderived > &other)
A conversion constructor which creates a new APtr from a Ptr of the same or a derived type.
Definition: APtr.h:63
APtr & operator=(const APtr< T > &other)
An assignment operator, which increases the shared reference count with the source APtr.
Definition: APtr.h:71
void Delete()
Delete the object pointed to and set the shared pointer for all linked APtr's to NULL.
Definition: APtr.h:96
APtr(const APtr< T > &other)
Copy constructor.
Definition: APtr.h:52
APtr< const T > ExtractConst()
Extract APtr<const T> and revokes ownership from this object.
Definition: APtr.h:102
~APtr()
Destructor which decreases the shared reference count.
Definition: APtr.h:85
APtr(T *t_ptr=NULL)
Default constructor assigning the value of the pointer.
Definition: APtr.h:45
A safe pointer class that throws an RC::ErrorMsgNull if a null dereference is attempted.
Definition: Ptr.h:31
Definition: APtr.h:25
APtr< T > MakeAPtr(Args &&... args)
Create a new APtr<T> constructed with the given arguments.
Definition: APtr.h:125
email address
— (c) 2015