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
33Ptr<T>::Ptr( T *ptr_in )
34 :ptr_(ptr_in)
35{}
36
37
38template<class T> inline
39Ptr<T>::Ptr(const Ptr<T>& ptr_in)
40 :ptr_(ptr_in.ptr_)
41#ifdef TEUCHOS_DEBUG
42 ,rcp_(ptr_in.access_rcp())
43#endif
44{}
45
46
47template<class T>
48template<class T2> inline
49Ptr<T>::Ptr(const Ptr<T2>& ptr_in)
50 :ptr_(ptr_in.get())
51#ifdef TEUCHOS_DEBUG
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{
71 debug_assert_not_null();
72 debug_assert_valid_ptr();
73 return ptr_;
74}
75
76
77template<class T> inline
79{
80 debug_assert_not_null();
81 debug_assert_valid_ptr();
82 return *ptr_;
83}
84
85
86template<class T> inline
87T* Ptr<T>::get() const
88{
89 debug_assert_valid_ptr();
90 return ptr_;
91}
92
93
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
131void Ptr<T>::debug_assert_valid_ptr() const
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
152
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.
T & get(ParameterList &l, const std::string &name)
A shorter name for getParameter().
Ptr< T2 > ptr_implicit_cast(const Ptr< T1 > &p1)
Implicit 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.
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.
Default traits class that just returns typeid(T).name().
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...