Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_Ptr.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_HPP
44#define TEUCHOS_PTR_HPP
45
46
47#include "Teuchos_PtrDecl.hpp"
48#include "Teuchos_RCP.hpp"
49
50
51namespace Teuchos {
52
53
54namespace PtrPrivateUtilityPack {
55TEUCHOSCORE_LIB_DLL_EXPORT void throw_null( const std::string &type_name );
56} // namespace PtrPrivateUtilityPack
57
58
59template<class T> inline
60Ptr<T>::Ptr( ENull /*null_in*/ )
61 : ptr_(0)
62{}
63
64
65template<class T> inline
67 :ptr_(ptr_in)
68{}
69
70
71template<class T> inline
73 :ptr_(ptr_in.ptr_)
75 ,rcp_(ptr_in.access_rcp())
76#endif
77{}
78
79
80template<class T>
81template<class T2> inline
83 :ptr_(ptr_in.get())
85 ,rcp_(ptr_in.access_rcp())
86#endif
87{}
88
89
90template<class T> inline
92{
93 ptr_ = ptr_in.get();
94#ifdef TEUCHOS_DEBUG
95 rcp_ = ptr_in.rcp_;
96#endif
97 return *this;
98}
99
100
101template<class T> inline
103{
106 return ptr_;
107}
108
109
110template<class T> inline
112{
115 return *ptr_;
116}
117
118
119template<class T> inline
121{
123 return ptr_;
124}
125
127template<class T> inline
129{
130 return get();
131}
132
133
134template<class T> inline
136{
137 if(!ptr_)
138 PtrPrivateUtilityPack::throw_null(TypeNameTraits<T>::name());
139 return *this;
140}
141
142
143template<class T> inline
144bool Ptr<T>::is_null () const {
145 return ptr_ == NULL;
146}
147
148
149template<class T> inline
150const Ptr<T> Ptr<T>::ptr() const
151{
152 return *this;
153}
154
155
156template<class T> inline
161
162
163template<class T> inline
165{
166#ifdef TEUCHOS_DEBUG
167 rcp_.access_private_node().assert_valid_ptr(*this);
168#endif
169}
170
172#ifdef TEUCHOS_DEBUG
173
175template<class T> inline
176Ptr<T>::Ptr( const RCP<T> &p )
177 : ptr_(p.getRawPtr()), rcp_(p)
178{}
179
180
181#endif // TEUCHOS_DEBUG
183
184} // namespace Teuchos
186
187template<class T>
188std::ostream& Teuchos::operator<<( std::ostream& out, const Ptr<T>& p )
189{
190 out
191 << TypeNameTraits<RCP<T> >::name() << "{"
192 << "ptr="<<(const void*)(p.get()) // I can't find any alternative to this C cast :-(
193 <<"}";
194 return out;
195}
196
197
198#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.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
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,...