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 //
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 
177  inline bool is_null () const;
178 
182  inline const Ptr<T>& assert_not_null() const;
183 
185  inline const Ptr<T> ptr() const;
186 
188  inline Ptr<const T> getConst() const;
189 
190 private:
191 
192  T *ptr_;
193 
194 #ifdef TEUCHOS_DEBUG
195  RCP<T> rcp_;
196 #endif
197 
198  void debug_assert_not_null() const
199  {
200 #ifdef TEUCHOS_DEBUG
201  assert_not_null();
202 #endif
203  }
204 
205  inline void debug_assert_valid_ptr() const;
206 
207 public: // Bad bad bad
208 
209 #ifdef TEUCHOS_DEBUG
210  Ptr( const RCP<T> &p );
211  T* access_private_ptr() const
212  { return ptr_; }
213  const RCP<T> access_rcp() const
214  { return rcp_; }
215 #endif
216 
217 
218 };
219 
220 
226 template<typename T> inline
227 Ptr<T> outArg( T& arg )
228 {
229  return Ptr<T>(&arg);
230 }
231 
232 
238 template<typename T> inline
239 Ptr<T> inOutArg( T& arg )
240 {
241  return Ptr<T>(&arg);
242 }
243 
244 
250 template<typename T> inline
251 Ptr<T> inoutArg( T& arg )
252 {
253  return Ptr<T>(&arg);
254 }
255 
256 
262 template<typename T> inline
264 {
265  return Ptr<const T>(&arg);
266 }
267 
268 
274 template<typename T> inline
275 Ptr<T> optInArg( T& arg )
276 {
277  return Ptr<T>(&arg);
278 }
279 
280 
286 template<typename T> inline
288 {
289  return Ptr<const T>(&arg);
290 }
291 
292 
297 template<typename T> inline
299 {
300  return Ptr<T>(&arg);
301 }
302 
303 
308 template<typename T> inline
309 RCP<T> rcpFromPtr( const Ptr<T>& ptr )
310 {
311  if (is_null(ptr))
312  return null;
313 #ifdef TEUCHOS_DEBUG
314  // In a debug build, just grab out the WEAK RCP and return it. That way we
315  // can get dangling reference checking without having to turn on more
316  // expensive RCPNode tracing.
317  if (!is_null(ptr.access_rcp()))
318  return ptr.access_rcp();
319 #endif
320  return rcpFromRef(*ptr);
321 }
322 
323 
328 template<typename T> inline
329 Ptr<T> ptr( T* p )
330 {
331  return Ptr<T>(p);
332 }
333 
334 
343 template<typename T> inline
345 {
346  return Ptr<const T>(&arg);
347 }
348 
349 
354 template<class T> inline
355 bool is_null( const Ptr<T> &p )
356 {
357  return p.get() == 0;
358 }
359 
360 
365 template<class T> inline
366 bool nonnull( const Ptr<T> &p )
367 {
368  return p.get() != 0;
369 }
370 
371 
376 template<class T> inline
377 bool operator==( const Ptr<T> &p, ENull )
378 {
379  return p.get() == 0;
380 }
381 
382 
387 template<class T>
388 bool operator!=( const Ptr<T> &p, ENull )
389 {
390  return p.get() != 0;
391 }
392 
393 
398 template<class T1, class T2>
399 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
400 {
401  return p1.get() == p2.get();
402 }
403 
404 
410 template<class T1, class T2>
411 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 )
412 {
413  return p1.get() != p2.get();
414 }
415 
416 
428 template<class T2, class T1>
430 {
431  return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
432 }
433 
434 
448 template<class T2, class T1>
450 {
451  return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
452 }
453 
454 
463 template<class T2, class T1>
465 {
466  return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
467 }
468 
469 
495 template<class T2, class T1>
497  const Ptr<T1>& p1, bool throw_on_fail = false
498  )
499 {
500  if( p1.get() ) {
501  T2 *check = NULL;
502  if(throw_on_fail)
503  check = &dyn_cast<T2>(*p1);
504  else
505  check = dynamic_cast<T2*>(p1.get());
506  if(check) {
507  return Ptr<T2>(check);
508  }
509  }
510  return null;
511 }
512 
513 
521 template<class T>
522 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p );
523 
524 
525 } // namespace Teuchos
526 
527 
528 #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.
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-&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.
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
Ptr< T2 > ptr_const_cast(const Ptr< T1 > &p1)
Constant cast of underlying Ptr type from T1* to T2*.