Teuchos_RefCountPtrDecl.hpp

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 
00032 #include "Teuchos_any.hpp"
00033 
00034 #ifdef REFCOUNTPTR_INLINE_FUNCS
00035 #  define REFCOUNTPTR_INLINE inline
00036 #else
00037 #  define REFCOUNTPTR_INLINE
00038 #endif
00039 
00040 #ifdef TEUCHOS_DEBUG
00041 #  define TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
00042 #endif
00043 
00044 namespace Teuchos {
00045 
00046 namespace PrivateUtilityPack {
00047   class RefCountPtr_node;
00048 }
00049 
00055 enum ENull { null };
00056 
00061 enum EPrePostDestruction { PRE_DESTROY, POST_DESTROY };
00062 
00398 template<class T>
00399 class RefCountPtr {
00400 public:
00402   typedef T element_type;
00415   RefCountPtr( ENull null_arg = null );
00433   RefCountPtr(const RefCountPtr<T>& r_ptr);
00447   template<class T2>
00448   RefCountPtr(const RefCountPtr<T2>& r_ptr);
00460   ~RefCountPtr();
00478   RefCountPtr<T>& operator=(const RefCountPtr<T>& r_ptr);
00485   T* operator->() const;
00492   T& operator*() const;
00495   T* get() const;
00517   T* release();
00524   int count() const;
00543   void set_has_ownership();
00553   bool has_ownership() const;
00560   template<class T2>
00561   bool shares_resource(const RefCountPtr<T2>& r_ptr) const;
00563   const RefCountPtr<T>& assert_not_null() const;
00564 
00565 public: // Bad bad bad
00566 
00567   // //////////////////////////////////////
00568   // Private types
00569 
00570   typedef PrivateUtilityPack::RefCountPtr_node      node_t;
00571 
00572 private:
00573 
00574   // //////////////////////////////////////////////////////////////
00575   // Private data members
00576 
00577   T       *ptr_;  // NULL if this pointer is null
00578   node_t  *node_; // NULL if this pointer is null
00579 
00580 public:
00581 #ifndef DOXYGEN_COMPILE
00582   // These constructors should be private but I have not had good luck making
00583   // this portable (i.e. using friendship etc.) in the past
00584   RefCountPtr( T* p, bool has_ownership );
00585   template<class Dealloc_T>
00586   RefCountPtr( T* p, Dealloc_T dealloc, bool has_ownership );
00587   // This is a very bad breach of encapsulation that is needed since MS VC++ 5.0 will
00588   // not allow me to declare template functions as friends.
00589   RefCountPtr( T* p, node_t* node);
00590   T*&           access_ptr();
00591   node_t*&      access_node();
00592   node_t*       access_node() const;
00593 #endif
00594 
00595 };  // end class RefCountPtr<...>
00596 
00601 template<typename T>
00602 class TypeNameTraits<RefCountPtr<T> > {
00603 public:
00604   static std::string name() { return "RefCountPtr<"+TypeNameTraits<T>::name()+">"; }
00605 };
00606 
00607 
00613 template<class T>
00614 class DeallocDelete
00615 {
00616 public:
00618   typedef T ptr_t;
00620   void free( T* ptr ) { if(ptr) delete ptr; }
00621 };
00622 
00628 template<class T>
00629 class DeallocArrayDelete
00630 {
00631 public:
00633   typedef T ptr_t;
00635   void free( T* ptr ) { if(ptr) delete [] ptr; }
00636 };
00637 
00650 template<class T, class DeleteFunctor>
00651 class DeallocFunctorDelete
00652 {
00653 public:
00654   DeallocFunctorDelete( DeleteFunctor deleteFunctor ) : deleteFunctor_(deleteFunctor) {}
00655   typedef T ptr_t;
00656   void free( T* ptr ) { if(ptr) deleteFunctor_(ptr); }
00657 private:
00658   DeleteFunctor deleteFunctor_;
00659   DeallocFunctorDelete(); // Not defined and not to be called!
00660 };
00661 
00666 template<class T, class DeleteFunctor>
00667 DeallocFunctorDelete<T,DeleteFunctor>
00668 deallocFunctorDelete( DeleteFunctor deleteFunctor )
00669 {
00670   return DeallocFunctorDelete<T,DeleteFunctor>(deleteFunctor);
00671 }
00672 
00686 template<class T, class DeleteHandleFunctor>
00687 class DeallocFunctorHandleDelete
00688 {
00689 public:
00690   DeallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00691     : deleteHandleFunctor_(deleteHandleFunctor) {}
00692   typedef T ptr_t;
00693   void free( T* ptr ) { if(ptr) { T **hdl = &ptr; deleteHandleFunctor_(hdl); } }
00694 private:
00695   DeleteHandleFunctor deleteHandleFunctor_;
00696   DeallocFunctorHandleDelete(); // Not defined and not to be called!
00697 };
00698 
00703 template<class T, class DeleteHandleFunctor>
00704 DeallocFunctorHandleDelete<T,DeleteHandleFunctor>
00705 deallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00706 {
00707   return DeallocFunctorHandleDelete<T,DeleteHandleFunctor>(deleteHandleFunctor);
00708 }
00709 
00733 template<class T>
00734 RefCountPtr<T> rcp( T* p, bool owns_mem
00735 #ifndef __sun
00736   = true
00737 #endif
00738   );
00739 #ifdef __sun // RAB: 20040303: Sun needs to fix their compiler
00740 template<class T> inline RefCountPtr<T> rcp( T* p ) { return rcp(p,true); }
00741 #endif
00742 
00785 template<class T, class Dealloc_T>
00786 RefCountPtr<T> rcp( T* p, Dealloc_T dealloc, bool owns_mem );
00787 
00792 template<class T>
00793 bool is_null( const RefCountPtr<T> &p );
00794 
00799 template<class T>
00800 bool operator==( const RefCountPtr<T> &p, ENull );
00801 
00806 template<class T>
00807 bool operator!=( const RefCountPtr<T> &p, ENull );
00808 
00814 template<class T1, class T2>
00815 bool operator==( const RefCountPtr<T1> &p1, const RefCountPtr<T2> &p2 );
00816 
00822 template<class T1, class T2>
00823 bool operator!=( const RefCountPtr<T1> &p1, const RefCountPtr<T2> &p2 );
00824 
00834 template<class T2, class T1>
00835 RefCountPtr<T2> rcp_implicit_cast(const RefCountPtr<T1>& p1);
00836 
00847 template<class T2, class T1>
00848 RefCountPtr<T2> rcp_static_cast(const RefCountPtr<T1>& p1);
00849 
00856 template<class T2, class T1>
00857 RefCountPtr<T2> rcp_const_cast(const RefCountPtr<T1>& p1);
00858 
00881 template<class T2, class T1>
00882 RefCountPtr<T2> rcp_dynamic_cast(
00883   const RefCountPtr<T1>& p1
00884   ,bool throw_on_fail
00885 #ifndef __sun
00886   = false
00887 #endif
00888   );
00889 #ifdef __sun // RAB: 20041019: Sun needs to fix their compiler
00890 template<class T2, class T1> inline RefCountPtr<T2> rcp_dynamic_cast( const RefCountPtr<T1>& p1 )
00891 { return rcp_dynamic_cast<T2>(p1,false); }
00892 #endif
00893 
00950 template<class T1, class T2>
00951 void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p
00952                      ,EPrePostDestruction destroy_when
00953 #ifndef __sun
00954                      = POST_DESTROY
00955 #endif
00956                      ,bool force_unique
00957 #ifndef __sun
00958                      = true
00959 #endif
00960   );
00961 #ifdef __sun
00962 template<class T1, class T2>
00963 inline void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p )
00964 { set_extra_data( extra_data, name, p, POST_DESTROY, true ); }
00965 template<class T1, class T2>
00966 inline void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p, EPrePostDestruction destroy_when )
00967 { set_extra_data( extra_data, name, p, destroy_when, true ); }
00968 #endif
00969 
00988 template<class T1, class T2>
00989 T1& get_extra_data( RefCountPtr<T2>& p, const std::string& name );
00990 
01015 template<class T1, class T2>
01016 const T1& get_extra_data( const RefCountPtr<T2>& p, const std::string& name );
01017 
01041 template<class T1, class T2>
01042 T1* get_optional_extra_data( RefCountPtr<T2>& p, const std::string& name );
01043 
01072 template<class T1, class T2>
01073 const T1* get_optional_extra_data( const RefCountPtr<T2>& p, const std::string& name );
01074 
01085 template<class Dealloc_T, class T>
01086 Dealloc_T& get_dealloc( RefCountPtr<T>& p );
01087 
01105 template<class Dealloc_T, class T>
01106 const Dealloc_T& get_dealloc( const RefCountPtr<T>& p );
01107 
01122 template<class Dealloc_T, class T>
01123 Dealloc_T* get_optional_dealloc( RefCountPtr<T>& p );
01124 
01146 template<class Dealloc_T, class T>
01147 const Dealloc_T* get_optional_dealloc( const RefCountPtr<T>& p );
01148 
01156 template<class T>
01157 std::ostream& operator<<( std::ostream& out, const RefCountPtr<T>& p );
01158 
01159 } // end namespace Teuchos
01160 
01161 #endif  // TEUCHOS_REFCOUNTPTR_DECL_H

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