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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
43 #ifndef TEUCHOS_PTR_HPP
44 #define TEUCHOS_PTR_HPP
45 
46 
47 #include "Teuchos_PtrDecl.hpp"
48 #include "Teuchos_RCP.hpp"
49 
50 
51 namespace Teuchos {
52 
53 
54 namespace PtrPrivateUtilityPack {
55 TEUCHOSCORE_LIB_DLL_EXPORT void throw_null( const std::string &type_name );
56 } // namespace PtrPrivateUtilityPack
57 
58 
59 template<class T> inline
60 Ptr<T>::Ptr( ENull /*null_in*/ )
61  : ptr_(0)
62 {}
63 
64 
65 template<class T> inline
66 Ptr<T>::Ptr( T *ptr_in )
67  :ptr_(ptr_in)
68 {}
69 
70 
71 template<class T> inline
72 Ptr<T>::Ptr(const Ptr<T>& ptr_in)
73  :ptr_(ptr_in.ptr_)
74 #ifdef TEUCHOS_DEBUG
75  ,rcp_(ptr_in.access_rcp())
76 #endif
77 {}
78 
79 
80 template<class T>
81 template<class T2> inline
82 Ptr<T>::Ptr(const Ptr<T2>& ptr_in)
83  :ptr_(ptr_in.get())
84 #ifdef TEUCHOS_DEBUG
85  ,rcp_(ptr_in.access_rcp())
86 #endif
87 {}
88 
89 
90 template<class T> inline
92 {
93  ptr_ = ptr_in.get();
94 #ifdef TEUCHOS_DEBUG
95  rcp_ = ptr_in.rcp_;
96 #endif
97  return *this;
98 }
99 
100 
101 template<class T> inline
103 {
104  debug_assert_not_null();
105  debug_assert_valid_ptr();
106  return ptr_;
107 }
108 
109 
110 template<class T> inline
112 {
113  debug_assert_not_null();
114  debug_assert_valid_ptr();
115  return *ptr_;
116 }
117 
118 
119 template<class T> inline
120 T* Ptr<T>::get() const
121 {
122  debug_assert_valid_ptr();
123  return ptr_;
124 }
125 
126 
127 template<class T> inline
129 {
130  return get();
131 }
132 
133 
134 template<class T> inline
136 {
137  if(!ptr_)
138  PtrPrivateUtilityPack::throw_null(TypeNameTraits<T>::name());
139  return *this;
140 }
141 
142 
143 template<class T> inline
144 bool Ptr<T>::is_null () const {
145  return ptr_ == NULL;
146 }
147 
148 
149 template<class T> inline
150 const Ptr<T> Ptr<T>::ptr() const
151 {
152  return *this;
153 }
154 
155 
156 template<class T> inline
158 {
159  return ptr_implicit_cast<const T>(*this);
160 }
161 
162 
163 template<class T> inline
165 {
166 #ifdef TEUCHOS_DEBUG
167  rcp_.access_private_node().assert_valid_ptr(*this);
168 #endif
169 }
170 
171 
172 #ifdef TEUCHOS_DEBUG
173 
174 
175 template<class T> inline
176 Ptr<T>::Ptr( const RCP<T> &p )
177  : ptr_(p.getRawPtr()), rcp_(p)
178 {}
179 
180 
181 #endif // TEUCHOS_DEBUG
182 
183 
184 } // namespace Teuchos
185 
186 
187 template<class T>
188 std::ostream& Teuchos::operator<<( std::ostream& out, const Ptr<T>& p )
189 {
190  out
191  << TypeNameTraits<RCP<T> >::name() << "{"
192  << "ptr="<<(const void*)(p.get()) // I can't find any alternative to this C cast :-(
193  <<"}";
194  return out;
195 }
196 
197 
198 #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.
Ptr(ENull null_in=null)
Default construct to NULL.
Definition: Teuchos_Ptr.hpp:60
T & operator*() const
Dereference the underlying object.
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.
T * operator->() const
Pointer (-&gt;) access to members of underlying object.
Ptr< T > & operator=(const Ptr< T > &ptr)
Shallow copy of the underlying pointer.
Definition: Teuchos_Ptr.hpp:91