Teuchos_PtrDecl.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_DECL_HPP
00031 #define TEUCHOS_PTR_DECL_HPP
00032 
00033 
00034 #include "Teuchos_RCPDecl.hpp"
00035 #include "Teuchos_dyn_cast.hpp"
00036 
00037 
00038 namespace Teuchos {
00039 
00040 
00090 template<class T>
00091 class Ptr {
00092 public:
00093 
00100   inline Ptr( ENull null_in = null );
00101 
00113   inline explicit Ptr( T *ptr );
00114 
00121   inline Ptr(const Ptr<T>& ptr);
00122 
00130   template<class T2>
00131   inline Ptr(const Ptr<T2>& ptr);
00132 
00139   Ptr<T>& operator=(const Ptr<T>& ptr);
00140 
00147   inline T* operator->() const;
00148 
00155   inline T& operator*() const;
00156 
00158   inline T* get() const;
00159 
00161   inline T* getRawPtr() const;
00162 
00166   inline const Ptr<T>& assert_not_null() const;
00167 
00169   inline const Ptr<T> ptr() const;
00170 
00171 private:
00172 
00173   T *ptr_;
00174 
00175 #ifdef TEUCHOS_DEBUG
00176   RCP<T> rcp_;
00177 #endif
00178 
00179   void debug_assert_not_null() const
00180     {
00181 #ifdef TEUCHOS_DEBUG
00182       assert_not_null();
00183 #endif
00184     }
00185 
00186   inline void debug_assert_valid_ptr() const;
00187 
00188 public: // Bad bad bad
00189 
00190 #ifdef TEUCHOS_DEBUG
00191   Ptr( const RCP<T> &p );
00192   T* access_private_ptr() const
00193     { return ptr_; }
00194   const RCP<T> access_rcp() const
00195     { return rcp_; }
00196 #endif
00197 
00198 
00199 };
00200 
00201 
00207 template<typename T> inline
00208 Ptr<T> outArg( T& arg )
00209 {
00210   return Ptr<T>(&arg);
00211 }
00212 
00213 
00219 template<typename T> inline
00220 Ptr<T> inOutArg( T& arg )
00221 {
00222   return Ptr<T>(&arg);
00223 }
00224 
00225 
00231 template<typename T> inline
00232 Ptr<const T> ptrInArg( T& arg )
00233 {
00234   return Ptr<const T>(&arg);
00235 }
00236 
00237 
00243 template<typename T> inline
00244 Ptr<T> optInArg( T& arg )
00245 {
00246   return Ptr<T>(&arg);
00247 }
00248 
00249 
00255 template<typename T> inline
00256 Ptr<const T> constOptInArg( T& arg )
00257 {
00258   return Ptr<const T>(&arg);
00259 }
00260 
00261 
00266 template<typename T> inline
00267 Ptr<T> ptrFromRef( T& arg )
00268 {
00269   return Ptr<T>(&arg);
00270 }
00271 
00272 
00277 template<typename T> inline
00278 RCP<T> rcpFromPtr( const Ptr<T>& ptr )
00279 {
00280   if (is_null(ptr))
00281     return null;
00282 #ifdef TEUCHOS_DEBUG
00283   // In a debug build, just grab out the WEAK RCP and return it.  That way we
00284   // can get dangling reference checking without having to turn on more
00285   // expensive RCPNode tracing.
00286   if (!is_null(ptr.access_rcp()))
00287     return ptr.access_rcp();
00288 #endif
00289   return rcpFromRef(*ptr);
00290 }
00291 
00292 
00297 template<typename T> inline
00298 Ptr<T> ptr( T* p )
00299 {
00300   return Ptr<T>(p);
00301 }
00302 
00303 
00312 template<typename T> inline
00313 Ptr<const T> constPtr( T& arg )
00314 {
00315   return Ptr<const T>(&arg);
00316 }
00317 
00318 
00323 template<class T> inline
00324 bool is_null( const Ptr<T> &p )
00325 {
00326   return p.get() == 0;
00327 }
00328 
00329 
00334 template<class T> inline
00335 bool nonnull( const Ptr<T> &p )
00336 {
00337   return p.get() != 0;
00338 }
00339 
00340 
00345 template<class T> inline
00346 bool operator==( const Ptr<T> &p, ENull )
00347 {
00348   return p.get() == 0;
00349 }
00350 
00351 
00356 template<class T>
00357 bool operator!=( const Ptr<T> &p, ENull )
00358 {
00359   return p.get() != 0;
00360 }
00361 
00362 
00367 template<class T1, class T2>
00368 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
00369 {
00370   return p1.get() == p2.get();
00371 }
00372 
00373 
00379 template<class T1, class T2>
00380 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 )
00381 {
00382   return p1.get() != p2.get();
00383 }
00384 
00385 
00397 template<class T2, class T1>
00398 Ptr<T2> ptr_implicit_cast(const Ptr<T1>& p1)
00399 {
00400   return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
00401 }
00402 
00403 
00417 template<class T2, class T1>
00418 Ptr<T2> ptr_static_cast(const Ptr<T1>& p1)
00419 {
00420   return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00421 }
00422 
00423 
00432 template<class T2, class T1>
00433 Ptr<T2> ptr_const_cast(const Ptr<T1>& p1)
00434 {
00435   return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00436 }
00437 
00438 
00464 template<class T2, class T1>
00465 Ptr<T2> ptr_dynamic_cast(
00466   const Ptr<T1>& p1, bool throw_on_fail = false
00467   )
00468 {
00469   if( p1.get() ) {
00470     T2 *check = NULL;
00471     if(throw_on_fail)
00472       check = &dyn_cast<T2>(*p1);
00473     else
00474       check = dynamic_cast<T2*>(p1.get());
00475     if(check) {
00476       return Ptr<T2>(check);
00477     }
00478   }
00479   return null;
00480 }
00481 
00482 
00490 template<class T>
00491 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p );
00492 
00493 
00494 } // namespace Teuchos
00495 
00496 
00497 #endif // TEUCHOS_PTR_DECL_HPP

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