Teuchos_Ptr.hpp

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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 
00030 #ifndef TEUCHOS_PTR_HPP
00031 #define TEUCHOS_PTR_HPP
00032 
00033 
00034 #include "Teuchos_ENull.hpp"
00035 #include "Teuchos_TypeNameTraits.hpp"
00036 #include "Teuchos_dyn_cast.hpp"
00037 
00038 
00039 namespace Teuchos {
00040 
00041 
00091 template<class T>
00092 class Ptr {
00093 public:
00094 
00101   Ptr( ENull null_in = null );
00102 
00114   explicit Ptr( T *ptr );
00115 
00122   Ptr(const Ptr<T>& ptr);
00123 
00131   template<class T2>
00132   Ptr(const Ptr<T2>& ptr);
00133 
00140   Ptr<T>& operator=(const Ptr<T>& ptr);
00141 
00148   T* operator->() const;
00149 
00156   T& operator*() const;
00157 
00159   T* get() const;
00160 
00162   T* getRawPtr() const;
00163 
00167   const Ptr<T>& assert_not_null() const;
00168 
00169 private:
00170 
00171   T *ptr_;
00172 
00173 };
00174 
00175 
00180 template<typename T> inline
00181 Ptr<T> outArg( T& arg )
00182 {
00183   return Ptr<T>(&arg);
00184 }
00185 
00186 
00192 template<typename T> inline
00193 Ptr<const T> ptrInArg( T& arg )
00194 {
00195   return Ptr<const T>(&arg);
00196 }
00197 
00198 
00203 template<typename T> inline
00204 Ptr<T> optInArg( T& arg )
00205 {
00206   return Ptr<T>(&arg);
00207 }
00208 
00209 
00214 template<typename T> inline
00215 Ptr<const T> constOptInArg( T& arg )
00216 {
00217   return Ptr<const T>(&arg);
00218 }
00219 
00220 
00225 template<typename T> inline
00226 Ptr<T> ptrRef( T& arg )
00227 {
00228   return Ptr<T>(&arg);
00229 }
00230 
00231 
00236 template<typename T> inline
00237 Ptr<T> ptr( T* p )
00238 {
00239   return Ptr<T>(p);
00240 }
00241 
00242 
00251 template<typename T> inline
00252 Ptr<const T> constPtr( T& arg )
00253 {
00254   return Ptr<const T>(&arg);
00255 }
00256 
00257 
00258 // 2007/11/07: rabartl: ToDo: Add the casting functions
00259 // ptr_[const,dynamic,static]_cast(...) to allow conversions.
00260 
00261 
00266 template<class T> inline
00267 bool is_null( const Ptr<T> &p )
00268 {
00269   return p.get() == 0;
00270 }
00271 
00272 
00277 template<class T> inline
00278 bool operator==( const Ptr<T> &p, ENull )
00279 {
00280   return p.get() == 0;
00281 }
00282 
00283 
00288 template<class T>
00289 bool operator!=( const Ptr<T> &p, ENull )
00290 {
00291   return p.get() != 0;
00292 }
00293 
00294 
00299 template<class T1, class T2>
00300 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
00301 {
00302   return p1.get() == p2.get();
00303 }
00304 
00305 
00311 template<class T1, class T2>
00312 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 )
00313 {
00314   return p1.get() != p2.get();
00315 }
00316 
00317 
00329 template<class T2, class T1>
00330 Ptr<T2> ptr_implicit_cast(const Ptr<T1>& p1)
00331 {
00332   return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
00333 }
00334 
00335 
00349 template<class T2, class T1>
00350 Ptr<T2> ptr_static_cast(const Ptr<T1>& p1)
00351 {
00352   return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00353 }
00354 
00355 
00364 template<class T2, class T1>
00365 Ptr<T2> ptr_const_cast(const Ptr<T1>& p1)
00366 {
00367   return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00368 }
00369 
00370 
00396 template<class T2, class T1>
00397 Ptr<T2> ptr_dynamic_cast(
00398   const Ptr<T1>& p1, bool throw_on_fail = false
00399   )
00400 {
00401   if( p1.get() ) {
00402     T2 *check = NULL;
00403     if(throw_on_fail)
00404       check = &dyn_cast<T2>(*p1);
00405     else
00406       check = dynamic_cast<T2*>(p1.get());
00407     if(check) {
00408       return Ptr<T2>(check);
00409     }
00410   }
00411   return null;
00412 }
00413 
00414 
00419 template<typename T>
00420 class TypeNameTraits<Ptr<T> > {
00421 public:
00422   static std::string name() { return "Ptr<"+TypeNameTraits<T>::name()+">"; }
00423   static std::string concreteName( const Ptr<T>& t )
00424     { return name(); }
00425 };
00426 
00427 
00428 } // namespace Teuchos
00429 
00430 
00431 // /////////////////////////////////////////////////////////////////////////
00432 // Inline implementations below, not for the client to look at.
00433 
00434 
00435 namespace Teuchos {
00436 namespace PtrPrivateUtilityPack {
00437 void throw_null( const std::string &type_name );
00438 } // namespace PtrPrivateUtilityPack
00439 } // namespace Teuchos
00440 
00441 
00442 namespace Teuchos {
00443 
00444 
00445 template<class T> inline
00446 Ptr<T>::Ptr( ENull null_in )
00447   : ptr_(0)
00448 {}
00449 
00450 
00451 template<class T> inline
00452 Ptr<T>::Ptr( T *ptr )
00453   : ptr_(ptr)
00454 {}
00455 
00456 
00457 template<class T> inline
00458 Ptr<T>::Ptr(const Ptr<T>& ptr)
00459   :ptr_(ptr.ptr_)
00460 {}
00461 
00462 
00463 template<class T>
00464 template<class T2> inline
00465 Ptr<T>::Ptr(const Ptr<T2>& ptr)
00466   :ptr_(ptr.get())
00467 {}
00468 
00469 
00470 template<class T> inline
00471 Ptr<T>& Ptr<T>::operator=(const Ptr<T>& ptr)
00472 {
00473   ptr_ = ptr.get();
00474   return *this;
00475 }
00476 
00477 
00478 template<class T> inline
00479 T* Ptr<T>::operator->() const
00480 {
00481 #ifdef TEUCHOS_DEBUG
00482   assert_not_null();
00483 #endif
00484   return ptr_;
00485 }
00486 
00487 
00488 template<class T> inline
00489 T& Ptr<T>::operator*() const
00490 {
00491 #ifdef TEUCHOS_DEBUG
00492   assert_not_null();
00493 #endif
00494   return *ptr_;
00495 }
00496 
00497 
00498 template<class T> inline
00499 T* Ptr<T>::get() const
00500 {
00501   return ptr_;
00502 }
00503 
00504 
00505 template<class T> inline
00506 T* Ptr<T>::getRawPtr() const
00507 {
00508   return ptr_;
00509 }
00510 
00511 
00512 template<class T>
00513 const Ptr<T>& Ptr<T>::assert_not_null() const
00514 {
00515   if(!ptr_)
00516     PtrPrivateUtilityPack::throw_null(TypeNameTraits<T>::name());
00517   return *this;
00518 }
00519 
00520 
00521 } // namespace Teuchos
00522 
00523 
00524 #endif // TEUCHOS_PTR_HPP

Generated on Wed May 12 21:40:32 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7