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 
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<const T> ptrInArg( T& arg )
00249 {
00250   return Ptr<const T>(&arg);
00251 }
00252 
00253 
00259 template<typename T> inline
00260 Ptr<T> optInArg( T& arg )
00261 {
00262   return Ptr<T>(&arg);
00263 }
00264 
00265 
00271 template<typename T> inline
00272 Ptr<const T> constOptInArg( T& arg )
00273 {
00274   return Ptr<const T>(&arg);
00275 }
00276 
00277 
00282 template<typename T> inline
00283 Ptr<T> ptrFromRef( T& arg )
00284 {
00285   return Ptr<T>(&arg);
00286 }
00287 
00288 
00293 template<typename T> inline
00294 RCP<T> rcpFromPtr( const Ptr<T>& ptr )
00295 {
00296   if (is_null(ptr))
00297     return null;
00298 #ifdef TEUCHOS_DEBUG
00299   // In a debug build, just grab out the WEAK RCP and return it.  That way we
00300   // can get dangling reference checking without having to turn on more
00301   // expensive RCPNode tracing.
00302   if (!is_null(ptr.access_rcp()))
00303     return ptr.access_rcp();
00304 #endif
00305   return rcpFromRef(*ptr);
00306 }
00307 
00308 
00313 template<typename T> inline
00314 Ptr<T> ptr( T* p )
00315 {
00316   return Ptr<T>(p);
00317 }
00318 
00319 
00328 template<typename T> inline
00329 Ptr<const T> constPtr( T& arg )
00330 {
00331   return Ptr<const T>(&arg);
00332 }
00333 
00334 
00339 template<class T> inline
00340 bool is_null( const Ptr<T> &p )
00341 {
00342   return p.get() == 0;
00343 }
00344 
00345 
00350 template<class T> inline
00351 bool nonnull( const Ptr<T> &p )
00352 {
00353   return p.get() != 0;
00354 }
00355 
00356 
00361 template<class T> inline
00362 bool operator==( const Ptr<T> &p, ENull )
00363 {
00364   return p.get() == 0;
00365 }
00366 
00367 
00372 template<class T>
00373 bool operator!=( const Ptr<T> &p, ENull )
00374 {
00375   return p.get() != 0;
00376 }
00377 
00378 
00383 template<class T1, class T2>
00384 bool operator==( const Ptr<T1> &p1, const Ptr<T2> &p2 )
00385 {
00386   return p1.get() == p2.get();
00387 }
00388 
00389 
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 
00413 template<class T2, class T1>
00414 Ptr<T2> ptr_implicit_cast(const Ptr<T1>& p1)
00415 {
00416   return Ptr<T2>(p1.get()); // Will only compile if conversion is legal!
00417 }
00418 
00419 
00433 template<class T2, class T1>
00434 Ptr<T2> ptr_static_cast(const Ptr<T1>& p1)
00435 {
00436   return Ptr<T2>(static_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00437 }
00438 
00439 
00448 template<class T2, class T1>
00449 Ptr<T2> ptr_const_cast(const Ptr<T1>& p1)
00450 {
00451   return Ptr<T2>(const_cast<T2*>(p1.get())); // Will only compile if conversion is legal!
00452 }
00453 
00454 
00480 template<class T2, class T1>
00481 Ptr<T2> ptr_dynamic_cast(
00482   const Ptr<T1>& p1, bool throw_on_fail = false
00483   )
00484 {
00485   if( p1.get() ) {
00486     T2 *check = NULL;
00487     if(throw_on_fail)
00488       check = &dyn_cast<T2>(*p1);
00489     else
00490       check = dynamic_cast<T2*>(p1.get());
00491     if(check) {
00492       return Ptr<T2>(check);
00493     }
00494   }
00495   return null;
00496 }
00497 
00498 
00506 template<class T>
00507 std::ostream& operator<<( std::ostream& out, const Ptr<T>& p );
00508 
00509 
00510 } // namespace Teuchos
00511 
00512 
00513 #endif // TEUCHOS_PTR_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines