Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_Ptr.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 
00043 #ifndef TEUCHOS_PTR_HPP
00044 #define TEUCHOS_PTR_HPP
00045 
00046 
00047 #include "Teuchos_PtrDecl.hpp"
00048 #include "Teuchos_RCP.hpp"
00049 
00050 
00051 namespace Teuchos {
00052 
00053 
00054 namespace PtrPrivateUtilityPack {
00055 TEUCHOSCORE_LIB_DLL_EXPORT void throw_null( const std::string &type_name );
00056 } // namespace PtrPrivateUtilityPack
00057 
00058 
00059 template<class T> inline
00060 Ptr<T>::Ptr( ENull /*null_in*/ )
00061   : ptr_(0)
00062 {}
00063 
00064 
00065 template<class T> inline
00066 Ptr<T>::Ptr( T *ptr_in )
00067   : ptr_(ptr_in)
00068 {}
00069 
00070 
00071 template<class T> inline
00072 Ptr<T>::Ptr(const Ptr<T>& ptr_in)
00073   :ptr_(ptr_in.ptr_)
00074 {}
00075 
00076 
00077 template<class T>
00078 template<class T2> inline
00079 Ptr<T>::Ptr(const Ptr<T2>& ptr_in)
00080   :ptr_(ptr_in.get())
00081 {}
00082 
00083 
00084 template<class T> inline
00085 Ptr<T>& Ptr<T>::operator=(const Ptr<T>& ptr_in)
00086 {
00087   ptr_ = ptr_in.get();
00088   return *this;
00089 }
00090 
00091 
00092 template<class T> inline
00093 T* Ptr<T>::operator->() const
00094 {
00095   debug_assert_not_null();
00096   debug_assert_valid_ptr();
00097   return ptr_;
00098 }
00099 
00100 
00101 template<class T> inline
00102 T& Ptr<T>::operator*() const
00103 {
00104   debug_assert_not_null();
00105   debug_assert_valid_ptr();
00106   return *ptr_;
00107 }
00108 
00109 
00110 template<class T> inline
00111 T* Ptr<T>::get() const
00112 {
00113   debug_assert_valid_ptr();
00114   return ptr_;
00115 }
00116 
00117 
00118 template<class T> inline
00119 T* Ptr<T>::getRawPtr() const
00120 {
00121   return get();
00122 }
00123 
00124 
00125 template<class T> inline
00126 const Ptr<T>& Ptr<T>::assert_not_null() const
00127 {
00128   if(!ptr_)
00129     PtrPrivateUtilityPack::throw_null(TypeNameTraits<T>::name());
00130   return *this;
00131 }
00132 
00133 
00134 template<class T> inline
00135 bool Ptr<T>::is_null () const {
00136   return ptr_ == NULL;
00137 }
00138 
00139 
00140 template<class T> inline
00141 const Ptr<T> Ptr<T>::ptr() const
00142 {
00143   return *this;
00144 }
00145 
00146 
00147 template<class T> inline
00148 Ptr<const T> Ptr<T>::getConst() const
00149 {
00150   return ptr_implicit_cast<const T>(*this);
00151 }
00152 
00153 
00154 template<class T> inline
00155 void Ptr<T>::debug_assert_valid_ptr() const
00156 {
00157 #ifdef TEUCHOS_DEBUG
00158   rcp_.access_private_node().assert_valid_ptr(*this);
00159 #endif
00160 }
00161 
00162 
00163 #ifdef TEUCHOS_DEBUG
00164 
00165 
00166 template<class T> inline
00167 Ptr<T>::Ptr( const RCP<T> &p )
00168   : ptr_(p.getRawPtr()), rcp_(p)
00169 {}
00170 
00171 
00172 #endif // TEUCHOS_DEBUG
00173 
00174 
00175 } // namespace Teuchos
00176 
00177 
00178 template<class T>
00179 std::ostream& Teuchos::operator<<( std::ostream& out, const Ptr<T>& p )
00180 {
00181   out
00182     << TypeNameTraits<RCP<T> >::name() << "{"
00183     << "ptr="<<(const void*)(p.get()) // I can't find any alternative to this C cast :-(
00184     <<"}";
00185   return out;
00186 }
00187 
00188 
00189 #endif // TEUCHOS_PTR_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines