Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_PtrDecl.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_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 
00177   inline bool is_null () const;
00178 
00182   inline const Ptr<T>& assert_not_null() const;
00183 
00185   inline const Ptr<T> ptr() const;
00186 
00188   inline Ptr<const T> getConst() const;
00189 
00190 private:
00191 
00192   T *ptr_;
00193 
00194 #ifdef TEUCHOS_DEBUG
00195   RCP<T> rcp_;
00196 #endif
00197 
00198   void debug_assert_not_null() const
00199     {
00200 #ifdef TEUCHOS_DEBUG
00201       assert_not_null();
00202 #endif
00203     }
00204 
00205   inline void debug_assert_valid_ptr() const;
00206 
00207 public: // Bad bad bad
00208 
00209 #ifdef TEUCHOS_DEBUG
00210   Ptr( const RCP<T> &p );
00211   T* access_private_ptr() const
00212     { return ptr_; }
00213   const RCP<T> access_rcp() const
00214     { return rcp_; }
00215 #endif
00216 
00217 
00218 };
00219 
00220 
00226 template<typename T> inline
00227 Ptr<T> outArg( T& arg )
00228 {
00229   return Ptr<T>(&arg);
00230 }
00231 
00232 
00238 template<typename T> inline
00239 Ptr<T> inOutArg( T& arg )
00240 {
00241   return Ptr<T>(&arg);
00242 }
00243 
00244 
00250 template<typename T> inline
00251 Ptr<T> inoutArg( T& arg )
00252 {
00253   return Ptr<T>(&arg);
00254 }
00255 
00256 
00262 template<typename T> inline
00263 Ptr<const T> ptrInArg( T& arg )
00264 {
00265   return Ptr<const T>(&arg);
00266 }
00267 
00268 
00274 template<typename T> inline
00275 Ptr<T> optInArg( T& arg )
00276 {
00277   return Ptr<T>(&arg);
00278 }
00279 
00280 
00286 template<typename T> inline
00287 Ptr<const T> constOptInArg( T& arg )
00288 {
00289   return Ptr<const T>(&arg);
00290 }
00291 
00292 
00297 template<typename T> inline
00298 Ptr<T> ptrFromRef( T& arg )
00299 {
00300   return Ptr<T>(&arg);
00301 }
00302 
00303 
00308 template<typename T> inline
00309 RCP<T> rcpFromPtr( const Ptr<T>& ptr )
00310 {
00311   if (is_null(ptr))
00312     return null;
00313 #ifdef TEUCHOS_DEBUG
00314   // In a debug build, just grab out the WEAK RCP and return it.  That way we
00315   // can get dangling reference checking without having to turn on more
00316   // expensive RCPNode tracing.
00317   if (!is_null(ptr.access_rcp()))
00318     return ptr.access_rcp();
00319 #endif
00320   return rcpFromRef(*ptr);
00321 }
00322 
00323 
00328 template<typename T> inline
00329 Ptr<T> ptr( T* p )
00330 {
00331   return Ptr<T>(p);
00332 }
00333 
00334 
00343 template<typename T> inline
00344 Ptr<const T> constPtr( T& arg )
00345 {
00346   return Ptr<const T>(&arg);
00347 }
00348 
00349 
00354 template<class T> inline
00355 bool is_null( const Ptr<T> &p )
00356 {
00357   return p.get() == 0;
00358 }
00359 
00360 
00365 template<class T> inline
00366 bool nonnull( const Ptr<T> &p )
00367 {
00368   return p.get() != 0;
00369 }
00370 
00371 
00376 template<class T> inline
00377 bool operator==( const Ptr<T> &p, ENull )
00378 {
00379   return p.get() == 0;
00380 }
00381 
00382 
00387 template<class T>
00388 bool operator!=( const Ptr<T> &p, ENull )
00389 {
00390   return p.get() != 0;
00391 }
00392 
00393 
00398 template<class T1, class T2>
00399 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
00400 {
00401   return p1.get() == p2.get();
00402 }
00403 
00404 
00410 template<class T1, class T2>
00411 bool operator!=( const Ptr<T1> &p1, const Ptr<T2> &p2 )
00412 {
00413   return p1.get() != p2.get();
00414 }
00415 
00416 
00428 template<class T2, class T1>
00429 Ptr<T2> ptr_implicit_cast(const Ptr<T1>& p1)
00430 {
00431   return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
00432 }
00433 
00434 
00448 template<class T2, class T1>
00449 Ptr<T2> ptr_static_cast(const Ptr<T1>& p1)
00450 {
00451   return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00452 }
00453 
00454 
00463 template<class T2, class T1>
00464 Ptr<T2> ptr_const_cast(const Ptr<T1>& p1)
00465 {
00466   return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00467 }
00468 
00469 
00495 template<class T2, class T1>
00496 Ptr<T2> ptr_dynamic_cast(
00497   const Ptr<T1>& p1, bool throw_on_fail = false
00498   )
00499 {
00500   if( p1.get() ) {
00501     T2 *check = NULL;
00502     if(throw_on_fail)
00503       check = &dyn_cast<T2>(*p1);
00504     else
00505       check = dynamic_cast<T2*>(p1.get());
00506     if(check) {
00507       return Ptr<T2>(check);
00508     }
00509   }
00510   return null;
00511 }
00512 
00513 
00521 template<class T>
00522 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p );
00523 
00524 
00525 } // namespace Teuchos
00526 
00527 
00528 #endif // TEUCHOS_PTR_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines