rcolyer.net
RC Lib  Version 202403231100
PtrCommon.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////
2 //
3 // RC Library, (c) 2011-2015, Ryan A. Colyer
4 //
5 /// \file PtrCommon.h
6 /// Common components for the *Ptr.h files. Do not include this directly.
7 /////////////////////////////////////////////////////////////////////
8 
9  public:
10 
11  /// Throws RC::ErrorMsgNull if the pointer is null.
12  inline void Assert() const {
13  if ( IsNull() ) {
14  Throw_RC_Type(Null, "Null pointer");
15  }
16  }
17 
18  /// True if the pointer is non-NULL.
19  /** @return True if the pointer is non-NULL.
20  */
21  inline bool IsSet() const {
22  return (Raw() != NULL);
23  }
24 
25 
26  /// True if the pointer is NULL.
27  /** @return True if the pointer is NULL.
28  */
29  inline bool IsNull() const {
30  return (Raw() == NULL);
31  }
32 
33 
34  /// Dereferences the pointer, or throws an exception if null.
35  /** The exception thrown is RC::ErrorMsgNull.
36  * @return A reference to the dereferenced object.
37  */
38  inline T& operator* () { Assert(); return (*Raw()); }
39  /// Const version of operator*()
40  inline const T& operator* () const { Assert(); return (*Raw()); }
41  /// Provides access to the enclosed pointer, or throws RC::ErrorMsgNull
42  /// if null.
43  /** @return The enclosed pointer.
44  */
45  inline T* operator-> () { Assert(); return Raw(); }
46  /// Const version of operator->()
47  inline const T* operator-> () const { Assert(); return Raw(); }
48 
49  /// Implicitly casts to the enclosed pointer, without null checking.
50  inline operator T* () const { return Raw(); }
51 
52 
53  /// Dynamically casts to an RC::Ptr of the type used as a template
54  /// parameter on this function.
55  /** The Ptr is NULL if the cast fails. */
56  template<class Type>
57  inline Ptr<Type> Cast() { return dynamic_cast<Type*>(Raw()); }
58  /// Const version of Cast()
59  template<class Type>
60  inline const Ptr<Type> Cast() const { return dynamic_cast<Type*>(Raw()); }
61 
62  /// True if it can dynamically cast to this type.
63  template<class Type>
64  inline bool CanCast() const {
65  return Cast<const Type>().IsSet();
66  }
67 
68 
69  protected:
70  /// @cond PROTECTED
71  template<class Type, class Self>
72  inline static Type& As_Helper(Self& self) {
73  self.Assert();
74  Type* retptr = dynamic_cast<Type*>(self.Raw());
75  if (retptr == NULL) {
76  Throw_RC_Type(Cast, "Bad pointer cast");
77  }
78  return *retptr;
79  }
80  /// @endcond
81  public:
82 
83  /// Dynamically casts and dereferences to the type presented as a
84  /// template parameter, or throws ErrorMsgCast if it fails.
85  template<class Type>
86  inline Type& As() { return As_Helper<Type>(*this); }
87  /// Const version of As()
88  template<class Type>
89  inline const Type& As() const { return As_Helper<const Type>(*this); }
90 
91  /// Dereference as the default type.
92  /** @see operator() */
93  inline T& As() { Assert(); return (*Raw()); }
94  /// Const version of operator*()
95  inline const T& As() const { Assert(); return (*Raw()); }
96 
#define Throw_RC_Type(Type, err)
Use this to throw an RC:ErrorMsg subtype exception.
Definition: Errors.h:282
T & operator*()
Dereferences the pointer, or throws an exception if null.
Definition: PtrCommon.h:38
bool IsSet() const
True if the pointer is non-NULL.
Definition: PtrCommon.h:21
T * operator->()
Provides access to the enclosed pointer, or throws RC::ErrorMsgNull if null.
Definition: PtrCommon.h:45
Type & As()
Dynamically casts and dereferences to the type presented as a template parameter, or throws ErrorMsgC...
Definition: PtrCommon.h:86
Ptr< Type > Cast()
Dynamically casts to an RC::Ptr of the type used as a template parameter on this function.
Definition: PtrCommon.h:57
void Assert() const
Throws RC::ErrorMsgNull if the pointer is null.
Definition: PtrCommon.h:12
bool IsNull() const
True if the pointer is NULL.
Definition: PtrCommon.h:29
bool CanCast() const
True if it can dynamically cast to this type.
Definition: PtrCommon.h:64
T * Raw() const
Returns a direct reference to the enclosed pointer.
Definition: PtrSharedCommon.h:16
email address
— (c) 2015