rcolyer.net
RC Lib  Version 202403231100
Ptr.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 Ptr.h
7 /// Provides a safe pointer class which throws exceptions.
8 /////////////////////////////////////////////////////////////////////
9 
10 #ifndef RC_PTR_H
11 #define RC_PTR_H
12 
13 
14 #include "Errors.h"
15 #include "Macros.h"
16 #include <iostream>
17 
18 
19 namespace RC {
20  /// @cond EXTERNAL
21  template <class T> class APtr;
22  template <class T> class RevPtr;
23  /// @endcond
24 
25  /// A safe pointer class that throws an RC::ErrorMsgNull if a null
26  /// dereference is attempted.
27  /** @see APtr
28  * @see RevPtr
29  */
30  template <class T>
31  class Ptr {
32  public:
33 
34  /// Default constructor assigning the value of the pointer.
35  /** @param t_ptr The new pointer value.
36  */
37  inline Ptr(T* t_ptr = NULL)
38  : t_ptr (t_ptr) {
39  }
40 
41  /// A constructor which obtains a non-reference-counted copy of an APtr.
42  /** @param other The APtr from which a pointer should be extracted.
43  */
44  template <class Tderived>
45  inline Ptr(const APtr<Tderived>& other) {
46  t_ptr = other;
47  }
48 
49  /// A constructor which obtains a non-revokable copy of a RevPtr.
50  /** @param other The RevPtr from which a pointer should be extracted.
51  */
52  template <class Tderived>
53  inline Ptr(const RevPtr<Tderived>& other) {
54  t_ptr = other;
55  }
56 
57  /// Copy constructor.
58  /** @param other The Ptr to copy.
59  */
60  template <class Tderived>
61  inline Ptr(const Ptr<Tderived>& other) {
62  t_ptr = other;
63  }
64 
65 
66  /// Deletes the object being pointed to and nulls the pointer.
67  inline void Delete() {
68  if (t_ptr != NULL) {
69  delete(t_ptr);
70  t_ptr = NULL;
71  }
72  }
73 
74  /// Returns a direct reference to the enclosed pointer.
75  /** The reference is read/write, so it can be used as a function
76  * parameter which updates the value of this pointer.
77  * @return A reference to the enclosed pointer.
78  */
79  inline T* Raw() const { return t_ptr; }
80 
81 
82 #include "PtrCommon.h"
83 
84 
85  protected:
86  /// @cond PROTECTED
87 
88  T *t_ptr;
89  /// @endcond
90  };
91 
92 
94 
95  /// Create a new Ptr<T> constructed with the given arguments.
96  template<class T, class... Args>
97  Ptr<T> MakePtr(Args&&... args) {
98  return Ptr<T>(new T(std::forward<Args>(args)...));
99  }
100 
101 }
102 
103 
104 #endif // RC_PTR_H
105 
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.
A reference counting ptr that auto-deletes what it points to when the last copy leaves scope or is ot...
Definition: APtr.h:39
A safe pointer class that throws an RC::ErrorMsgNull if a null dereference is attempted.
Definition: Ptr.h:31
T * Raw() const
Returns a direct reference to the enclosed pointer.
Definition: Ptr.h:79
void Delete()
Deletes the object being pointed to and nulls the pointer.
Definition: Ptr.h:67
Ptr(const APtr< Tderived > &other)
A constructor which obtains a non-reference-counted copy of an APtr.
Definition: Ptr.h:45
Ptr(const Ptr< Tderived > &other)
Copy constructor.
Definition: Ptr.h:61
Ptr(T *t_ptr=NULL)
Default constructor assigning the value of the pointer.
Definition: Ptr.h:37
Ptr(const RevPtr< Tderived > &other)
A constructor which obtains a non-revokable copy of a RevPtr.
Definition: Ptr.h:53
A reference counting pointer that revokes (NULLs) all copies when one set to AutoRevoke(true) leaves ...
Definition: RevPtr.h:45
Definition: APtr.h:25
Ptr< T > MakePtr(Args &&... args)
Create a new Ptr<T> constructed with the given arguments.
Definition: Ptr.h:97
email address
— (c) 2015