Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_PtrDecl.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_DECL_HPP
44 #define TEUCHOS_PTR_DECL_HPP
45 
46 
47 #include "Teuchos_RCPDecl.hpp"
48 #include "Teuchos_dyn_cast.hpp"
49 
50 
51 namespace Teuchos {
52 
53 
103 template<class T>
104 class Ptr {
105 public:
106 
113  inline Ptr( ENull null_in = null );
114 
126  inline explicit Ptr( T *ptr );
127 
134  inline Ptr(const Ptr<T>& ptr);
135 
143  template<class T2>
144  inline Ptr(const Ptr<T2>& ptr);
145 
152  Ptr<T>& operator=(const Ptr<T>& ptr);
153 
160  inline T* operator->() const;
161 
168  inline T& operator*() const;
169 
171  inline T* get() const;
172 
174  inline T* getRawPtr() const;
175 
179  inline const Ptr<T>& assert_not_null() const;
180 
182  inline const Ptr<T> ptr() const;
183 
185  inline Ptr<const T> getConst() const;
186 
187 private:
188 
189  T *ptr_;
190 
191 #ifdef TEUCHOS_DEBUG
192  RCP<T> rcp_;
193 #endif
194 
195  void debug_assert_not_null() const
196  {
197 #ifdef TEUCHOS_DEBUG
198  assert_not_null();
199 #endif
200  }
201 
202  inline void debug_assert_valid_ptr() const;
203 
204 public: // Bad bad bad
205 
206 #ifdef TEUCHOS_DEBUG
207  Ptr( const RCP<T> &p );
208  T* access_private_ptr() const
209  { return ptr_; }
210  const RCP<T> access_rcp() const
211  { return rcp_; }
212 #endif
213 
214 
215 };
216 
217 
223 template<typename T> inline
224 Ptr<T> outArg( T& arg )
225 {
226  return Ptr<T>(&arg);
227 }
228 
229 
235 template<typename T> inline
236 Ptr<T> inOutArg( T& arg )
237 {
238  return Ptr<T>(&arg);
239 }
240 
241 
247 template<typename T> inline
248 Ptr<T> inoutArg( T& arg )
249 {
250  return Ptr<T>(&arg);
251 }
252 
253 
259 template<typename T> inline
261 {
262  return Ptr<const T>(&arg);
263 }
264 
265 
271 template<typename T> inline
272 Ptr<T> optInArg( T& arg )
273 {
274  return Ptr<T>(&arg);
275 }
276 
277 
283 template<typename T> inline
285 {
286  return Ptr<const T>(&arg);
287 }
288 
289 
294 template<typename T> inline
296 {
297  return Ptr<T>(&arg);
298 }
299 
300 
305 template<typename T> inline
307 {
308  if (is_null(ptr))
309  return null;
310 #ifdef TEUCHOS_DEBUG
311  // In a debug build, just grab out the WEAK RCP and return it. That way we
312  // can get dangling reference checking without having to turn on more
313  // expensive RCPNode tracing.
314  if (!is_null(ptr.access_rcp()))
315  return ptr.access_rcp();
316 #endif
317  return rcpFromRef(*ptr);
318 }
319 
320 
325 template<typename T> inline
326 Ptr<T> ptr( T* p )
327 {
328  return Ptr<T>(p);
329 }
330 
331 
340 template<typename T> inline
342 {
343  return Ptr<const T>(&arg);
344 }
345 
346 
351 template<class T> inline
352 bool is_null( const Ptr<T> &p )
353 {
354  return p.get() == 0;
355 }
356 
357 
362 template<class T> inline
363 bool nonnull( const Ptr<T> &p )
364 {
365  return p.get() != 0;
366 }
367 
368 
373 template<class T> inline
374 bool operator==( const Ptr<T> &p, ENull )
375 {
376  return p.get() == 0;
377 }
378 
379 
384 template<class T>
385 bool operator!=( const Ptr<T> &p, ENull )
386 {
387  return p.get() != 0;
388 }
389 
390 
395 template<class T1, class T2>
396 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
397 {
398  return p1.get() == p2.get();
399 }
400 
401 
407 template<class T1, class T2>
408 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 )
409 {
410  return p1.get() != p2.get();
411 }
412 
413 
425 template<class T2, class T1>
427 {
428  return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
429 }
430 
431 
445 template<class T2, class T1>
447 {
448  return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
449 }
450 
451 
460 template<class T2, class T1>
462 {
463  return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
464 }
465 
466 
492 template<class T2, class T1>
494  const Ptr<T1>& p1, bool throw_on_fail = false
495  )
496 {
497  if( p1.get() ) {
498  T2 *check = NULL;
499  if(throw_on_fail)
500  check = &dyn_cast<T2>(*p1);
501  else
502  check = dynamic_cast<T2*>(p1.get());
503  if(check) {
504  return Ptr<T2>(check);
505  }
506  }
507  return null;
508 }
509 
510 
518 template<class T>
519 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p );
520 
521 
522 } // namespace Teuchos
523 
524 
525 #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.
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<T&> by throwing a better documented er...
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
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:60
T & operator*() const
Dereference the underlying object.
const Ptr< T > & assert_not_null() const
Throws std::logic_error if this->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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
RCP< T > rcpFromPtr(const Ptr< T > &ptr)
Create an RCP<T> from a Ptr<T> 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<const T> 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.
T * operator->() const
Pointer (->) access to members of underlying object.
Definition: Teuchos_Ptr.hpp:93
Ptr< T > & operator=(const Ptr< T > &ptr)
Shallow copy of the underlying pointer.
Definition: Teuchos_Ptr.hpp:85
Ptr< T2 > ptr_const_cast(const Ptr< T1 > &p1)
Constant cast of underlying Ptr type from T1* to T2*.