|
Teuchos - Trilinos Tools Package
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 // 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_DECL_HPP 00044 #define TEUCHOS_PTR_DECL_HPP 00045 00046 00047 #include "Teuchos_RCPDecl.hpp" 00048 #include "Teuchos_dyn_cast.hpp" 00049 00050 00051 namespace Teuchos { 00052 00053 00103 template<class T> 00104 class Ptr { 00105 public: 00106 00113 inline Ptr( ENull null_in = null ); 00114 00126 inline explicit Ptr( T *ptr ); 00127 00134 inline Ptr(const Ptr<T>& ptr); 00135 00143 template<class T2> 00144 inline Ptr(const Ptr<T2>& ptr); 00145 00152 Ptr<T>& operator=(const Ptr<T>& ptr); 00153 00160 inline T* operator->() const; 00161 00168 inline T& operator*() const; 00169 00171 inline T* get() const; 00172 00174 inline T* getRawPtr() const; 00175 00179 inline const Ptr<T>& assert_not_null() const; 00180 00182 inline const Ptr<T> ptr() const; 00183 00185 inline Ptr<const T> getConst() const; 00186 00187 private: 00188 00189 T *ptr_; 00190 00191 #ifdef TEUCHOS_DEBUG 00192 RCP<T> rcp_; 00193 #endif 00194 00195 void debug_assert_not_null() const 00196 { 00197 #ifdef TEUCHOS_DEBUG 00198 assert_not_null(); 00199 #endif 00200 } 00201 00202 inline void debug_assert_valid_ptr() const; 00203 00204 public: // Bad bad bad 00205 00206 #ifdef TEUCHOS_DEBUG 00207 Ptr( const RCP<T> &p ); 00208 T* access_private_ptr() const 00209 { return ptr_; } 00210 const RCP<T> access_rcp() const 00211 { return rcp_; } 00212 #endif 00213 00214 00215 }; 00216 00217 00223 template<typename T> inline 00224 Ptr<T> outArg( T& arg ) 00225 { 00226 return Ptr<T>(&arg); 00227 } 00228 00229 00235 template<typename T> inline 00236 Ptr<T> inOutArg( T& arg ) 00237 { 00238 return Ptr<T>(&arg); 00239 } 00240 00241 00247 template<typename T> inline 00248 Ptr<T> inoutArg( T& arg ) 00249 { 00250 return Ptr<T>(&arg); 00251 } 00252 00253 00259 template<typename T> inline 00260 Ptr<const T> ptrInArg( T& arg ) 00261 { 00262 return Ptr<const T>(&arg); 00263 } 00264 00265 00271 template<typename T> inline 00272 Ptr<T> optInArg( T& arg ) 00273 { 00274 return Ptr<T>(&arg); 00275 } 00276 00277 00283 template<typename T> inline 00284 Ptr<const T> constOptInArg( T& arg ) 00285 { 00286 return Ptr<const T>(&arg); 00287 } 00288 00289 00294 template<typename T> inline 00295 Ptr<T> ptrFromRef( T& arg ) 00296 { 00297 return Ptr<T>(&arg); 00298 } 00299 00300 00305 template<typename T> inline 00306 RCP<T> rcpFromPtr( const Ptr<T>& ptr ) 00307 { 00308 if (is_null(ptr)) 00309 return null; 00310 #ifdef TEUCHOS_DEBUG 00311 // In a debug build, just grab out the WEAK RCP and return it. That way we 00312 // can get dangling reference checking without having to turn on more 00313 // expensive RCPNode tracing. 00314 if (!is_null(ptr.access_rcp())) 00315 return ptr.access_rcp(); 00316 #endif 00317 return rcpFromRef(*ptr); 00318 } 00319 00320 00325 template<typename T> inline 00326 Ptr<T> ptr( T* p ) 00327 { 00328 return Ptr<T>(p); 00329 } 00330 00331 00340 template<typename T> inline 00341 Ptr<const T> constPtr( T& arg ) 00342 { 00343 return Ptr<const T>(&arg); 00344 } 00345 00346 00351 template<class T> inline 00352 bool is_null( const Ptr<T> &p ) 00353 { 00354 return p.get() == 0; 00355 } 00356 00357 00362 template<class T> inline 00363 bool nonnull( const Ptr<T> &p ) 00364 { 00365 return p.get() != 0; 00366 } 00367 00368 00373 template<class T> inline 00374 bool operator==( const Ptr<T> &p, ENull ) 00375 { 00376 return p.get() == 0; 00377 } 00378 00379 00384 template<class T> 00385 bool operator!=( const Ptr<T> &p, ENull ) 00386 { 00387 return p.get() != 0; 00388 } 00389 00390 00395 template<class T1, class T2> 00396 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 ) 00397 { 00398 return p1.get() == p2.get(); 00399 } 00400 00401 00407 template<class T1, class T2> 00408 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 ) 00409 { 00410 return p1.get() != p2.get(); 00411 } 00412 00413 00425 template<class T2, class T1> 00426 Ptr<T2> ptr_implicit_cast(const Ptr<T1>& p1) 00427 { 00428 return Ptr<T2>(p1.get()); // Will only compile if conversion is legal! 00429 } 00430 00431 00445 template<class T2, class T1> 00446 Ptr<T2> ptr_static_cast(const Ptr<T1>& p1) 00447 { 00448 return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal! 00449 } 00450 00451 00460 template<class T2, class T1> 00461 Ptr<T2> ptr_const_cast(const Ptr<T1>& p1) 00462 { 00463 return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal! 00464 } 00465 00466 00492 template<class T2, class T1> 00493 Ptr<T2> ptr_dynamic_cast( 00494 const Ptr<T1>& p1, bool throw_on_fail = false 00495 ) 00496 { 00497 if( p1.get() ) { 00498 T2 *check = NULL; 00499 if(throw_on_fail) 00500 check = &dyn_cast<T2>(*p1); 00501 else 00502 check = dynamic_cast<T2*>(p1.get()); 00503 if(check) { 00504 return Ptr<T2>(check); 00505 } 00506 } 00507 return null; 00508 } 00509 00510 00518 template<class T> 00519 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p ); 00520 00521 00522 } // namespace Teuchos 00523 00524 00525 #endif // TEUCHOS_PTR_DECL_HPP
1.7.6.1