Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Ptr.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_PTR_HPP
11 #define TEUCHOS_PTR_HPP
12 
13 
14 #include "Teuchos_PtrDecl.hpp"
15 #include "Teuchos_RCP.hpp"
16 
17 
18 namespace Teuchos {
19 
20 
21 namespace PtrPrivateUtilityPack {
22 TEUCHOSCORE_LIB_DLL_EXPORT void throw_null( const std::string &type_name );
23 } // namespace PtrPrivateUtilityPack
24 
25 
26 template<class T> inline
27 Ptr<T>::Ptr( ENull /*null_in*/ )
28  : ptr_(0)
29 {}
30 
31 
32 template<class T> inline
33 Ptr<T>::Ptr( T *ptr_in )
34  :ptr_(ptr_in)
35 {}
36 
37 
38 template<class T> inline
39 Ptr<T>::Ptr(const Ptr<T>& ptr_in)
40  :ptr_(ptr_in.ptr_)
41 #ifdef TEUCHOS_DEBUG
42  ,rcp_(ptr_in.access_rcp())
43 #endif
44 {}
45 
46 
47 template<class T>
48 template<class T2> inline
49 Ptr<T>::Ptr(const Ptr<T2>& ptr_in)
50  :ptr_(ptr_in.get())
51 #ifdef TEUCHOS_DEBUG
52  ,rcp_(ptr_in.access_rcp())
53 #endif
54 {}
55 
56 
57 template<class T> inline
59 {
60  ptr_ = ptr_in.get();
61 #ifdef TEUCHOS_DEBUG
62  rcp_ = ptr_in.rcp_;
63 #endif
64  return *this;
65 }
66 
67 
68 template<class T> inline
70 {
71  debug_assert_not_null();
72  debug_assert_valid_ptr();
73  return ptr_;
74 }
75 
76 
77 template<class T> inline
79 {
80  debug_assert_not_null();
81  debug_assert_valid_ptr();
82  return *ptr_;
83 }
84 
85 
86 template<class T> inline
87 T* Ptr<T>::get() const
88 {
89  debug_assert_valid_ptr();
90  return ptr_;
91 }
92 
93 
94 template<class T> inline
96 {
97  return get();
98 }
99 
100 
101 template<class T> inline
103 {
104  if(!ptr_)
105  PtrPrivateUtilityPack::throw_null(TypeNameTraits<T>::name());
106  return *this;
107 }
108 
109 
110 template<class T> inline
111 bool Ptr<T>::is_null () const {
112  return ptr_ == NULL;
113 }
114 
115 
116 template<class T> inline
117 const Ptr<T> Ptr<T>::ptr() const
118 {
119  return *this;
120 }
121 
122 
123 template<class T> inline
125 {
126  return ptr_implicit_cast<const T>(*this);
127 }
128 
129 
130 template<class T> inline
132 {
133 #ifdef TEUCHOS_DEBUG
134  rcp_.access_private_node().assert_valid_ptr(*this);
135 #endif
136 }
137 
138 
139 #ifdef TEUCHOS_DEBUG
140 
141 
142 template<class T> inline
143 Ptr<T>::Ptr( const RCP<T> &p )
144  : ptr_(p.getRawPtr()), rcp_(p)
145 {}
146 
147 
148 #endif // TEUCHOS_DEBUG
149 
150 
151 } // namespace Teuchos
152 
153 
154 template<class T>
155 std::ostream& Teuchos::operator<<( std::ostream& out, const Ptr<T>& p )
156 {
157  out
158  << TypeNameTraits<RCP<T> >::name() << "{"
159  << "ptr="<<(const void*)(p.get()) // I can't find any alternative to this C cast :-(
160  <<"}";
161  return out;
162 }
163 
164 
165 #endif // TEUCHOS_PTR_HPP
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
Definition: Teuchos_Ptr.hpp:95
Ptr(ENull null_in=null)
Default construct to NULL.
Definition: Teuchos_Ptr.hpp:27
T & operator*() const
Dereference the underlying object.
Definition: Teuchos_Ptr.hpp:78
const Ptr< T > & assert_not_null() const
Throws std::logic_error if this-&gt;get()==NULL, otherwise returns reference to *this.
bool is_null() const
Return true if the wrapped raw pointer is NULL, else return false.
Default traits class that just returns typeid(T).name().
Ptr< const T > getConst() const
Return a Ptr&lt;const T&gt; version of *this.
Reference-counted pointer class and non-member templated function implementations.
const Ptr< T > ptr() const
Return a copy of *this.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
T * get() const
Get the raw C++ pointer to the underlying object.
Definition: Teuchos_Ptr.hpp:87
T * operator->() const
Pointer (-&gt;) access to members of underlying object.
Definition: Teuchos_Ptr.hpp:69
Ptr< T > & operator=(const Ptr< T > &ptr)
Shallow copy of the underlying pointer.
Definition: Teuchos_Ptr.hpp:58