|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
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 00172 inline Ptr<const T> getConst() const; 00173 00174 private: 00175 00176 T *ptr_; 00177 00178 #ifdef TEUCHOS_DEBUG 00179 RCP<T> rcp_; 00180 #endif 00181 00182 void debug_assert_not_null() const 00183 { 00184 #ifdef TEUCHOS_DEBUG 00185 assert_not_null(); 00186 #endif 00187 } 00188 00189 inline void debug_assert_valid_ptr() const; 00190 00191 public: // Bad bad bad 00192 00193 #ifdef TEUCHOS_DEBUG 00194 Ptr( const RCP<T> &p ); 00195 T* access_private_ptr() const 00196 { return ptr_; } 00197 const RCP<T> access_rcp() const 00198 { return rcp_; } 00199 #endif 00200 00201 00202 }; 00203 00204 00210 template<typename T> inline 00211 Ptr<T> outArg( T& arg ) 00212 { 00213 return Ptr<T>(&arg); 00214 } 00215 00216 00222 template<typename T> inline 00223 Ptr<T> inOutArg( T& arg ) 00224 { 00225 return Ptr<T>(&arg); 00226 } 00227 00228 00234 template<typename T> inline 00235 Ptr<const T> ptrInArg( T& arg ) 00236 { 00237 return Ptr<const T>(&arg); 00238 } 00239 00240 00246 template<typename T> inline 00247 Ptr<T> optInArg( T& arg ) 00248 { 00249 return Ptr<T>(&arg); 00250 } 00251 00252 00258 template<typename T> inline 00259 Ptr<const T> constOptInArg( T& arg ) 00260 { 00261 return Ptr<const T>(&arg); 00262 } 00263 00264 00269 template<typename T> inline 00270 Ptr<T> ptrFromRef( T& arg ) 00271 { 00272 return Ptr<T>(&arg); 00273 } 00274 00275 00280 template<typename T> inline 00281 RCP<T> rcpFromPtr( const Ptr<T>& ptr ) 00282 { 00283 if (is_null(ptr)) 00284 return null; 00285 #ifdef TEUCHOS_DEBUG 00286 // In a debug build, just grab out the WEAK RCP and return it. That way we 00287 // can get dangling reference checking without having to turn on more 00288 // expensive RCPNode tracing. 00289 if (!is_null(ptr.access_rcp())) 00290 return ptr.access_rcp(); 00291 #endif 00292 return rcpFromRef(*ptr); 00293 } 00294 00295 00300 template<typename T> inline 00301 Ptr<T> ptr( T* p ) 00302 { 00303 return Ptr<T>(p); 00304 } 00305 00306 00315 template<typename T> inline 00316 Ptr<const T> constPtr( T& arg ) 00317 { 00318 return Ptr<const T>(&arg); 00319 } 00320 00321 00326 template<class T> inline 00327 bool is_null( const Ptr<T> &p ) 00328 { 00329 return p.get() == 0; 00330 } 00331 00332 00337 template<class T> inline 00338 bool nonnull( const Ptr<T> &p ) 00339 { 00340 return p.get() != 0; 00341 } 00342 00343 00348 template<class T> inline 00349 bool operator==( const Ptr<T> &p, ENull ) 00350 { 00351 return p.get() == 0; 00352 } 00353 00354 00359 template<class T> 00360 bool operator!=( const Ptr<T> &p, ENull ) 00361 { 00362 return p.get() != 0; 00363 } 00364 00365 00370 template<class T1, class T2> 00371 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 ) 00372 { 00373 return p1.get() == p2.get(); 00374 } 00375 00376 00382 template<class T1, class T2> 00383 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 ) 00384 { 00385 return p1.get() != p2.get(); 00386 } 00387 00388 00400 template<class T2, class T1> 00401 Ptr<T2> ptr_implicit_cast(const Ptr<T1>& p1) 00402 { 00403 return Ptr<T2>(p1.get()); // Will only compile if conversion is legal! 00404 } 00405 00406 00420 template<class T2, class T1> 00421 Ptr<T2> ptr_static_cast(const Ptr<T1>& p1) 00422 { 00423 return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal! 00424 } 00425 00426 00435 template<class T2, class T1> 00436 Ptr<T2> ptr_const_cast(const Ptr<T1>& p1) 00437 { 00438 return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal! 00439 } 00440 00441 00467 template<class T2, class T1> 00468 Ptr<T2> ptr_dynamic_cast( 00469 const Ptr<T1>& p1, bool throw_on_fail = false 00470 ) 00471 { 00472 if( p1.get() ) { 00473 T2 *check = NULL; 00474 if(throw_on_fail) 00475 check = &dyn_cast<T2>(*p1); 00476 else 00477 check = dynamic_cast<T2*>(p1.get()); 00478 if(check) { 00479 return Ptr<T2>(check); 00480 } 00481 } 00482 return null; 00483 } 00484 00485 00493 template<class T> 00494 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p ); 00495 00496 00497 } // namespace Teuchos 00498 00499 00500 #endif // TEUCHOS_PTR_DECL_HPP
1.7.4