Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Teuchos_PtrDecl.hpp
Go to the documentation of this file.
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
51namespace Teuchos {
52
53
103template<class T>
104class Ptr {
105public:
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
190private:
191
193
194#ifdef TEUCHOS_DEBUG
195 RCP<T> rcp_;
196#endif
197
199 {
200#ifdef TEUCHOS_DEBUG
202#endif
203 }
204
205 inline void debug_assert_valid_ptr() const;
206
207public: // 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
226template<typename T> inline
228{
229 return Ptr<T>(&arg);
230}
231
232
238template<typename T> inline
240{
241 return Ptr<T>(&arg);
242}
243
244
250template<typename T> inline
252{
253 return Ptr<T>(&arg);
254}
255
256
262template<typename T> inline
264{
265 return Ptr<const T>(&arg);
266}
267
268
274template<typename T> inline
276{
277 return Ptr<T>(&arg);
278}
279
280
286template<typename T> inline
288{
289 return Ptr<const T>(&arg);
290}
291
292
297template<typename T> inline
299{
300 return Ptr<T>(&arg);
301}
302
303
308template<typename T> inline
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
328template<typename T> inline
330{
331 return Ptr<T>(p);
332}
333
334
343template<typename T> inline
345{
346 return Ptr<const T>(&arg);
347}
348
349
354template<class T> inline
355bool is_null( const Ptr<T> &p )
356{
357 return p.get() == 0;
358}
359
360
365template<class T> inline
366bool nonnull( const Ptr<T> &p )
367{
368 return p.get() != 0;
369}
370
371
376template<class T> inline
377bool operator==( const Ptr<T> &p, ENull )
378{
379 return p.get() == 0;
380}
381
382
387template<class T>
388bool operator!=( const Ptr<T> &p, ENull )
389{
390 return p.get() != 0;
391}
392
393
398template<class T1, class T2>
399bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
400{
401 return p1.get() == p2.get();
402}
403
404
410template<class T1, class T2>
411bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 )
412{
413 return p1.get() != p2.get();
414}
415
416
428template<class T2, class T1>
430{
431 return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
432}
433
434
448template<class T2, class T1>
450{
451 return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
452}
453
454
463template<class T2, class T1>
465{
466 return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
467}
468
469
495template<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
521template<class T>
522std::ostream& operator<<( std::ostream& out, const Ptr<T>& p );
523
524
525} // namespace Teuchos
526
527
528#endif // TEUCHOS_PTR_DECL_HPP
Reference-counted pointer class and non-member templated function implementations.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Ptr< T2 > ptr_const_cast(const Ptr< T1 > &p1)
Constant 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*.
void debug_assert_not_null() const
Ptr< const T > ptrInArg(T &arg)
create a general Ptr input argument for a function call from a reference.
Ptr< T2 > ptr_dynamic_cast(const Ptr< T1 > &p1, bool throw_on_fail=false)
Dynamic cast of underlying Ptr type from T1* to T2*.
const Ptr< T > & assert_not_null() const
Throws std::logic_error if this->get()==NULL, otherwise returns reference to *this.
bool operator==(const Ptr< T > &p, ENull)
Returns true if p.get()==NULL.
bool nonnull(const Ptr< T > &p)
Returns true if p.get()!=NULL
Ptr< const T > getConst() const
Return a Ptr<const T> version of *this.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
Ptr< T > & operator=(const Ptr< T > &ptr)
Shallow copy of the underlying pointer.
Ptr< T > optInArg(T &arg)
create a non-persisting non-const optional input argument for a function call.
Ptr< const T > constPtr(T &arg)
Create a pointer from a const object given a non-const object reference.
void debug_assert_valid_ptr() const
bool is_null(const Ptr< T > &p)
Returns true if p.get()==NULL.
bool is_null() const
Return true if the wrapped raw pointer is NULL, else return false.
Ptr< T > ptrFromRef(T &arg)
Create a pointer to a object from an object reference.
Ptr< T > outArg(T &arg)
create a non-persisting (required or optional) output argument for a function call.
Ptr< T > inOutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call.
T * operator->() const
Pointer (->) access to members of underlying object.
bool operator!=(const Ptr< T > &p, ENull)
Returns true if p.get()!=NULL.
Ptr< T > inoutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call.
T & operator*() const
Dereference the underlying object.
bool operator==(const Ptr< T1 > &p1, const Ptr< T2 > &p2)
Return true if two Ptr objects point to the same object.
T * get() const
Get the raw C++ pointer to the underlying object.
Ptr< const T > constOptInArg(T &arg)
create a non-persisting const optional input argument for a function call.
bool operator!=(const Ptr< T1 > &p1, const Ptr< T2 > &p2)
Return true if two Ptr objects do not point to the same object.
Ptr< T2 > ptr_static_cast(const Ptr< T1 > &p1)
Static cast of underlying Ptr type from T1* to T2*.
const Ptr< T > ptr() const
Return a copy of *this.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
Ptr(ENull null_in=null)
Default construct to NULL.
std::ostream & operator<<(std::ostream &out, const Ptr< T > &p)
Output stream inserter.
RCP< T > rcpFromPtr(const Ptr< T > &ptr)
Create an RCP<T> from a Ptr<T> object.
Concrete serial communicator subclass.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.