Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_PtrDecl.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_DECL_HPP
11 #define TEUCHOS_PTR_DECL_HPP
12 
13 
14 #include "Teuchos_RCPDecl.hpp"
15 #include "Teuchos_dyn_cast.hpp"
16 
17 
18 namespace Teuchos {
19 
20 
70 template<class T>
71 class Ptr {
72 public:
73 
80  inline Ptr( ENull null_in = null );
81 
93  inline explicit Ptr( T *ptr );
94 
101  inline Ptr(const Ptr<T>& ptr);
102 
110  template<class T2>
111  inline Ptr(const Ptr<T2>& ptr);
112 
119  Ptr<T>& operator=(const Ptr<T>& ptr);
120 
127  inline T* operator->() const;
128 
135  inline T& operator*() const;
136 
138  inline T* get() const;
139 
141  inline T* getRawPtr() const;
142 
144  inline bool is_null () const;
145 
149  inline const Ptr<T>& assert_not_null() const;
150 
152  inline const Ptr<T> ptr() const;
153 
155  inline Ptr<const T> getConst() const;
156 
157 private:
158 
159  T *ptr_;
160 
161 #ifdef TEUCHOS_DEBUG
162  RCP<T> rcp_;
163 #endif
164 
165  void debug_assert_not_null() const
166  {
167 #ifdef TEUCHOS_DEBUG
168  assert_not_null();
169 #endif
170  }
171 
172  inline void debug_assert_valid_ptr() const;
173 
174 public: // Bad bad bad
175 
176 #ifdef TEUCHOS_DEBUG
177  Ptr( const RCP<T> &p );
178  T* access_private_ptr() const
179  { return ptr_; }
180  const RCP<T> access_rcp() const
181  { return rcp_; }
182 #endif
183 
184 
185 };
186 
187 
193 template<typename T> inline
194 Ptr<T> outArg( T& arg )
195 {
196  return Ptr<T>(&arg);
197 }
198 
199 
205 template<typename T> inline
206 Ptr<T> inOutArg( T& arg )
207 {
208  return Ptr<T>(&arg);
209 }
210 
211 
217 template<typename T> inline
218 Ptr<T> inoutArg( T& arg )
219 {
220  return Ptr<T>(&arg);
221 }
222 
223 
229 template<typename T> inline
231 {
232  return Ptr<const T>(&arg);
233 }
234 
235 
241 template<typename T> inline
242 Ptr<T> optInArg( T& arg )
243 {
244  return Ptr<T>(&arg);
245 }
246 
247 
253 template<typename T> inline
255 {
256  return Ptr<const T>(&arg);
257 }
258 
259 
264 template<typename T> inline
266 {
267  return Ptr<T>(&arg);
268 }
269 
270 
275 template<typename T> inline
276 RCP<T> rcpFromPtr( const Ptr<T>& ptr )
277 {
278  if (is_null(ptr))
279  return null;
280 #ifdef TEUCHOS_DEBUG
281  // In a debug build, just grab out the WEAK RCP and return it. That way we
282  // can get dangling reference checking without having to turn on more
283  // expensive RCPNode tracing.
284  if (!is_null(ptr.access_rcp()))
285  return ptr.access_rcp();
286 #endif
287  return rcpFromRef(*ptr);
288 }
289 
290 
295 template<typename T> inline
296 Ptr<T> ptr( T* p )
297 {
298  return Ptr<T>(p);
299 }
300 
301 
310 template<typename T> inline
312 {
313  return Ptr<const T>(&arg);
314 }
315 
316 
321 template<class T> inline
322 bool is_null( const Ptr<T> &p )
323 {
324  return p.get() == 0;
325 }
326 
327 
332 template<class T> inline
333 bool nonnull( const Ptr<T> &p )
334 {
335  return p.get() != 0;
336 }
337 
338 
343 template<class T> inline
344 bool operator==( const Ptr<T> &p, ENull )
345 {
346  return p.get() == 0;
347 }
348 
349 
354 template<class T>
355 bool operator!=( const Ptr<T> &p, ENull )
356 {
357  return p.get() != 0;
358 }
359 
360 
365 template<class T1, class T2>
366 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
367 {
368  return p1.get() == p2.get();
369 }
370 
371 
377 template<class T1, class T2>
378 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 )
379 {
380  return p1.get() != p2.get();
381 }
382 
383 
395 template<class T2, class T1>
397 {
398  return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
399 }
400 
401 
415 template<class T2, class T1>
417 {
418  return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
419 }
420 
421 
430 template<class T2, class T1>
432 {
433  return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
434 }
435 
436 
462 template<class T2, class T1>
464  const Ptr<T1>& p1, bool throw_on_fail = false
465  )
466 {
467  if( p1.get() ) {
468  T2 *check = NULL;
469  if(throw_on_fail)
470  check = &dyn_cast<T2>(*p1);
471  else
472  check = dynamic_cast<T2*>(p1.get());
473  if(check) {
474  return Ptr<T2>(check);
475  }
476  }
477  return null;
478 }
479 
480 
488 template<class T>
489 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p );
490 
491 
492 } // namespace Teuchos
493 
494 
495 #endif // TEUCHOS_PTR_DECL_HPP
Ptr< T > inOutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
bool is_null(const Ptr< T > &p)
Returns true if p.get()==NULL.
Ptr< T2 > ptr_static_cast(const Ptr< T1 > &p1)
Static cast of underlying Ptr type from T1* to T2*.
bool operator==(const Ptr< T1 > &p1, const Ptr< T2 > &p2)
Return true if two Ptr objects point to the same object.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
Ptr< T > optInArg(T &arg)
create a non-persisting non-const optional input argument for a function call.
T_To & dyn_cast(T_From &from)
Dynamic casting utility function meant to replace dynamic_cast&lt;T&amp;&gt; by throwing a better documented er...
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< T2 > ptr_dynamic_cast(const Ptr< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying Ptr type from T1* to T2*.
Ptr< T2 > ptr_implicit_cast(const Ptr< T1 > &p1)
Implicit cast of underlying Ptr type from T1* to T2*.
Ptr< T > ptrFromRef(T &arg)
Create a pointer to a object from an object reference.
Ptr< T > inoutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
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.
Ptr< const T > ptrInArg(T &arg)
create a general Ptr input argument for a function call from a reference.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
bool is_null() const
Return true if the wrapped raw pointer is NULL, else return false.
RCP< T > rcpFromPtr(const Ptr< T > &ptr)
Create an RCP&lt;T&gt; from a Ptr&lt;T&gt; object.
bool operator!=(const Ptr< T1 > &p1, const Ptr< T2 > &p2)
Return true if two Ptr objects do not point to the same object.
Reference-counted pointer class and non-member templated function implementations.
Smart reference counting pointer class for automatic garbage collection.
Ptr< const T > constPtr(T &arg)
Create a pointer from a const object given a non-const object reference.
bool nonnull(const Ptr< T > &p)
Returns true if p.get()!=NULL
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.
Ptr< const T > getConst() const
Return a Ptr&lt;const T&gt; version of *this.
Ptr< const T > constOptInArg(T &arg)
create a non-persisting const optional input argument for a function call.
const Ptr< T > ptr() const
Return a copy of *this.
bool operator!=(const Ptr< T > &p, ENull)
Returns true if p.get()!=NULL.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
bool operator==(const Ptr< T > &p, ENull)
Returns true if p.get()==NULL.
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
Ptr< T2 > ptr_const_cast(const Ptr< T1 > &p1)
Constant cast of underlying Ptr type from T1* to T2*.