Teuchos_RefCountPtrDecl.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 // 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 #ifndef TEUCHOS_REFCOUNTPTR_DECL_H
00030 #define TEUCHOS_REFCOUNTPTR_DECL_H
00031 
00037 #include "Teuchos_any.hpp"
00038 
00039 #ifdef REFCOUNTPTR_INLINE_FUNCS
00040 #define REFCOUNTPTR_INLINE inline
00041 #else
00042 #define REFCOUNTPTR_INLINE
00043 #endif
00044 
00045 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00046 namespace MemMngPack {} // ToDo: Take out latter!
00047 #endif
00048 
00064 namespace Teuchos {
00065 
00066 namespace PrivateUtilityPack {
00067   class RefCountPtr_node;
00068 }
00069 
00394 
00396 enum ENull { null };
00397 
00399 enum EPrePostDestruction { PRE_DESTROY, POST_DESTROY };
00400 
00402 template<class T>
00403 class DeallocDelete
00404 {
00405 public:
00407   typedef T ptr_t;
00409   void free( T* ptr ) { if(ptr) delete ptr; }
00410 };
00411 
00413 template<class T>
00414 class DeallocArrayDelete
00415 {
00416 public:
00418   typedef T ptr_t;
00420   void free( T* ptr ) { if(ptr) delete [] ptr; }
00421 };
00422 
00424 template<class T>
00425 class RefCountPtr {
00426 public:
00428   typedef T element_type;
00441   RefCountPtr( ENull null_arg = null );
00459   RefCountPtr(const RefCountPtr<T>& r_ptr);
00473   template<class T2>
00474   RefCountPtr(const RefCountPtr<T2>& r_ptr);
00485   ~RefCountPtr();
00502   RefCountPtr<T>& operator=(const RefCountPtr<T>& r_ptr);
00510   T* operator->() const;
00518   T& operator*() const;
00521   T* get() const;
00542   T* release();
00549   int count() const;
00568   void set_has_ownership();
00578   bool has_ownership() const;
00585   bool shares_resource(const RefCountPtr<T>& r_ptr) const;
00587   const RefCountPtr<T>& assert_not_null() const;
00588 
00589 private:
00590 
00591   // //////////////////////////////////////
00592   // Private types
00593 
00594   typedef PrivateUtilityPack::RefCountPtr_node      node_t;
00595 
00596   // //////////////////////////////////////////////////////////////
00597   // Private data members
00598 
00599   T       *ptr_;  // NULL if this pointer is null
00600   node_t  *node_; // NULL if this pointer is null
00601 
00602 public:
00603 #ifndef DOXYGEN_COMPILE
00604   // These constructors should be private but I have not had good luck making
00605   // this portable (i.e. using friendship etc.) in the past
00606   RefCountPtr( T* p, bool has_ownership );
00607   template<class Dealloc_T>
00608   RefCountPtr( T* p, Dealloc_T dealloc, bool has_ownership );
00609   // This is a very bad breach of encapsulation that is needed since MS VC++ 5.0 will
00610   // not allow me to declare template functions as friends.
00611   RefCountPtr( T* p, node_t* node);
00612   T*&           access_ptr();
00613   node_t*&      access_node();
00614   node_t*       access_node() const;
00615 #endif
00616 
00617 };  // end class RefCountPtr<...>
00618 
00640 template<class T>
00641 RefCountPtr<T> rcp( T* p, bool owns_mem
00642 #ifndef __sun
00643   = true
00644 #endif
00645   );
00646 #ifdef __sun // RAB: 20040303: Sun needs to fix their compiler
00647 template<class T> inline RefCountPtr<T> rcp( T* p ) { return rcp(p,true); }
00648 #endif
00649 
00690 template<class T, class Dealloc_T>
00691 RefCountPtr<T> rcp( T* p, Dealloc_T dealloc, bool owns_mem );
00692 
00695 template<class T>
00696 bool is_null( const RefCountPtr<T> &p );
00697 
00700 template<class T>
00701 bool operator==( const RefCountPtr<T> &p, ENull );
00702 
00705 template<class T>
00706 bool operator!=( const RefCountPtr<T> &p, ENull );
00707 
00711 template<class T1, class T2>
00712 bool operator==( const RefCountPtr<T1> &p1, const RefCountPtr<T2> &p2 );
00713 
00717 template<class T1, class T2>
00718 bool operator!=( const RefCountPtr<T1> &p1, const RefCountPtr<T2> &p2 );
00719 
00727 template<class T2, class T1>
00728 RefCountPtr<T2> rcp_implicit_cast(const RefCountPtr<T1>& p1);
00729 
00738 template<class T2, class T1>
00739 RefCountPtr<T2> rcp_static_cast(const RefCountPtr<T1>& p1);
00740 
00745 template<class T2, class T1>
00746 RefCountPtr<T2> rcp_const_cast(const RefCountPtr<T1>& p1);
00747 
00768 template<class T2, class T1>
00769 RefCountPtr<T2> rcp_dynamic_cast(
00770   const RefCountPtr<T1>& p1
00771   ,bool throw_on_fail
00772 #ifndef __sun
00773   = false
00774 #endif
00775   );
00776 #ifdef __sun // RAB: 20041019: Sun needs to fix their compiler
00777 template<class T2, class T1> inline RefCountPtr<T2> rcp_dynamic_cast( const RefCountPtr<T1>& p1 )
00778 { return rcp_dynamic_cast<T2>(p1,false); }
00779 #endif
00780 
00835 template<class T1, class T2>
00836 void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p, bool force_unique
00837 #ifndef __sun
00838                      = true
00839 #endif
00840                      ,EPrePostDestruction destroy_when
00841 #ifndef __sun
00842                      = POST_DESTROY
00843 #endif
00844   );
00845 #ifdef __sun
00846 template<class T1, class T2>
00847 inline void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p )
00848 { set_extra_data( extra_data, name, p, true, POST_DESTROY ); }
00849 template<class T1, class T2>
00850 inline void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p, bool force_unique )
00851 { set_extra_data( extra_data, name, p, force_unique, POST_DESTROY ); }
00852 #endif
00853 
00870 template<class T1, class T2>
00871 T1& get_extra_data( RefCountPtr<T2>& p, const std::string& name );
00872 
00895 template<class T1, class T2>
00896 const T1& get_extra_data( const RefCountPtr<T2>& p, const std::string& name );
00897 
00919 template<class T1, class T2>
00920 T1* get_optional_extra_data( RefCountPtr<T2>& p, const std::string& name );
00921 
00948 template<class T1, class T2>
00949 const T1* get_optional_extra_data( const RefCountPtr<T2>& p, const std::string& name );
00950 
00960 template<class Dealloc_T, class T>
00961 Dealloc_T& get_dealloc( RefCountPtr<T>& p );
00962 
00978 template<class Dealloc_T, class T>
00979 const Dealloc_T& get_dealloc( const RefCountPtr<T>& p );
00980 
00993 template<class Dealloc_T, class T>
00994 Dealloc_T* get_optional_dealloc( RefCountPtr<T>& p );
00995 
01015 template<class Dealloc_T, class T>
01016 const Dealloc_T* get_optional_dealloc( const RefCountPtr<T>& p );
01017 
01019 
01020 } // end namespace Teuchos
01021 
01022 #endif  // TEUCHOS_REFCOUNTPTR_DECL_H

Generated on Thu Sep 18 12:39:10 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1