Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_Ptr.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_HPP
11#define TEUCHOS_PTR_HPP
12
13
14#include "Teuchos_PtrDecl.hpp"
15#include "Teuchos_RCP.hpp"
16
17
18namespace Teuchos {
19
20
21namespace PtrPrivateUtilityPack {
22TEUCHOSCORE_LIB_DLL_EXPORT void throw_null( const std::string &type_name );
23} // namespace PtrPrivateUtilityPack
24
25
26template<class T> inline
27Ptr<T>::Ptr( ENull /*null_in*/ )
28 : ptr_(0)
29{}
30
31
32template<class T> inline
34 :ptr_(ptr_in)
35{}
36
37
38template<class T> inline
40 :ptr_(ptr_in.ptr_)
42 ,rcp_(ptr_in.access_rcp())
43#endif
44{}
45
46
47template<class T>
48template<class T2> inline
50 :ptr_(ptr_in.get())
52 ,rcp_(ptr_in.access_rcp())
53#endif
54{}
55
56
57template<class T> inline
59{
60 ptr_ = ptr_in.get();
61#ifdef TEUCHOS_DEBUG
62 rcp_ = ptr_in.rcp_;
63#endif
64 return *this;
65}
66
67
68template<class T> inline
70{
73 return ptr_;
74}
75
76
77template<class T> inline
79{
82 return *ptr_;
83}
84
85
86template<class T> inline
87T* Ptr<T>::get() const
88{
90 return ptr_;
91}
92
94template<class T> inline
96{
97 return get();
98}
99
100
101template<class T> inline
103{
104 if(!ptr_)
105 PtrPrivateUtilityPack::throw_null(TypeNameTraits<T>::name());
106 return *this;
107}
108
109
110template<class T> inline
111bool Ptr<T>::is_null () const {
112 return ptr_ == NULL;
113}
114
115
116template<class T> inline
117const Ptr<T> Ptr<T>::ptr() const
118{
119 return *this;
120}
121
122
123template<class T> inline
128
129
130template<class T> inline
132{
133#ifdef TEUCHOS_DEBUG
134 rcp_.access_private_node().assert_valid_ptr(*this);
135#endif
136}
137
139#ifdef TEUCHOS_DEBUG
140
142template<class T> inline
143Ptr<T>::Ptr( const RCP<T> &p )
144 : ptr_(p.getRawPtr()), rcp_(p)
145{}
146
147
148#endif // TEUCHOS_DEBUG
150
151} // namespace Teuchos
153
154template<class T>
155std::ostream& Teuchos::operator<<( std::ostream& out, const Ptr<T>& p )
156{
157 out
158 << TypeNameTraits<RCP<T> >::name() << "{"
159 << "ptr="<<(const void*)(p.get()) // I can't find any alternative to this C cast :-(
160 <<"}";
161 return out;
162}
163
164
165#endif // TEUCHOS_PTR_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.
const Ptr< T > & assert_not_null() const
Throws std::logic_error if this->get()==NULL, otherwise returns reference to *this.
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.
bool is_null() const
Return true if the wrapped raw pointer is NULL, else return false.
T * operator->() const
Pointer (->) access to members of underlying object.
T & operator*() const
Dereference the underlying object.
T * get() const
Get the raw C++ pointer to the underlying object.
const Ptr< T > ptr() const
Return a copy of *this.
Ptr(ENull null_in=null)
Default construct to NULL.
Smart reference counting pointer class for automatic garbage collection.
const RCP< T > & debug_assert_valid_ptr() const
Calls assert_valid_ptr() in a debug build.
T * get() const
Get the raw C++ pointer to the underlying object.
const RCP< T > & debug_assert_not_null() const
Calls assert_not_null() in a debug build.
Default traits class that just returns typeid(T).name().
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...