Teuchos_RCPDecl.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_RCP_DECL_HPP
00030 #define TEUCHOS_RCP_DECL_HPP
00031 
00036 #include "Teuchos_any.hpp"
00037 
00038 #ifdef REFCOUNTPTR_INLINE_FUNCS
00039 #  define REFCOUNTPTR_INLINE inline
00040 #else
00041 #  define REFCOUNTPTR_INLINE
00042 #endif
00043 
00044 #ifdef TEUCHOS_DEBUG
00045 #  define TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
00046 #endif
00047 
00048 namespace Teuchos {
00049 
00050 namespace PrivateUtilityPack {
00051   class RCP_node;
00052 }
00053 
00059 enum ENull { null };
00060 
00065 enum EPrePostDestruction { PRE_DESTROY, POST_DESTROY };
00066 
00402 template<class T>
00403 class RCP {
00404 public:
00406   typedef T  element_type;
00419   RCP( ENull null_arg = null );
00433   explicit RCP( T* p, bool has_ownership = false );
00451   RCP(const RCP<T>& r_ptr);
00465   template<class T2>
00466   RCP(const RCP<T2>& r_ptr);
00478   ~RCP();
00496   RCP<T>& operator=(const RCP<T>& r_ptr);
00503   T* operator->() const;
00510   T& operator*() const;
00513   T* get() const;
00535   T* release();
00542   int count() const;
00561   void set_has_ownership();
00571   bool has_ownership() const;
00578   template<class T2>
00579   bool shares_resource(const RCP<T2>& r_ptr) const;
00581   const RCP<T>& assert_not_null() const;
00582 
00583 public: // Bad bad bad
00584 
00585   // //////////////////////////////////////
00586   // Private types
00587 
00588   typedef PrivateUtilityPack::RCP_node      node_t;
00589 
00590 private:
00591 
00592   // //////////////////////////////////////////////////////////////
00593   // Private data members
00594 
00595   T       *ptr_;  // NULL if this pointer is null
00596   node_t  *node_;  // NULL if this pointer is null
00597 
00598 public: // Bad bad bad
00599 #ifndef DOXYGEN_COMPILE
00600   // These constructors should be private but I have not had good luck making
00601   // this portable (i.e. using friendship etc.) in the past
00602   template<class Dealloc_T>
00603   RCP( T* p, Dealloc_T dealloc, bool has_ownership );
00604   // This is a very bad breach of encapsulation that is needed since MS VC++ 5.0 will
00605   // not allow me to declare template functions as friends.
00606   RCP( T* p, node_t* node);
00607   T*& access_ptr();
00608   node_t*& access_node();
00609   node_t* access_node() const;
00610 #endif
00611 
00612 };  // end class RCP<...>
00613 
00618 template<typename T>
00619 class TypeNameTraits<RCP<T> > {
00620 public:
00621   static std::string name() { return "RCP<"+TypeNameTraits<T>::name()+">"; }
00622 };
00623 
00624 
00630 template<class T>
00631 class DeallocDelete
00632 {
00633 public:
00635   typedef T ptr_t;
00637   void free( T* ptr ) { if(ptr) delete ptr; }
00638 };
00639 
00645 template<class T>
00646 class DeallocArrayDelete
00647 {
00648 public:
00650   typedef T ptr_t;
00652   void free( T* ptr ) { if(ptr) delete [] ptr; }
00653 };
00654 
00667 template<class T, class DeleteFunctor>
00668 class DeallocFunctorDelete
00669 {
00670 public:
00671   DeallocFunctorDelete( DeleteFunctor deleteFunctor ) : deleteFunctor_(deleteFunctor) {}
00672   typedef T ptr_t;
00673   void free( T* ptr ) { if(ptr) deleteFunctor_(ptr); }
00674 private:
00675   DeleteFunctor deleteFunctor_;
00676   DeallocFunctorDelete(); // Not defined and not to be called!
00677 };
00678 
00683 template<class T, class DeleteFunctor>
00684 DeallocFunctorDelete<T,DeleteFunctor>
00685 deallocFunctorDelete( DeleteFunctor deleteFunctor )
00686 {
00687   return DeallocFunctorDelete<T,DeleteFunctor>(deleteFunctor);
00688 }
00689 
00703 template<class T, class DeleteHandleFunctor>
00704 class DeallocFunctorHandleDelete
00705 {
00706 public:
00707   DeallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00708     : deleteHandleFunctor_(deleteHandleFunctor) {}
00709   typedef T ptr_t;
00710   void free( T* ptr ) { if(ptr) { T **hdl = &ptr; deleteHandleFunctor_(hdl); } }
00711 private:
00712   DeleteHandleFunctor deleteHandleFunctor_;
00713   DeallocFunctorHandleDelete(); // Not defined and not to be called!
00714 };
00715 
00720 template<class T, class DeleteHandleFunctor>
00721 DeallocFunctorHandleDelete<T,DeleteHandleFunctor>
00722 deallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00723 {
00724   return DeallocFunctorHandleDelete<T,DeleteHandleFunctor>(deleteHandleFunctor);
00725 }
00726 
00750 template<class T>
00751 RCP<T> rcp( T* p, bool owns_mem
00752 #ifndef __sun
00753   = true
00754 #endif
00755   );
00756 #ifdef __sun // RAB: 20040303: Sun needs to fix their compiler
00757 template<class T> inline RCP<T> rcp( T* p ) { return rcp(p,true); }
00758 #endif
00759 
00802 template<class T, class Dealloc_T>
00803 RCP<T> rcp( T* p, Dealloc_T dealloc, bool owns_mem );
00804 
00809 template<class T>
00810 bool is_null( const RCP<T> &p );
00811 
00816 template<class T>
00817 bool operator==( const RCP<T> &p, ENull );
00818 
00823 template<class T>
00824 bool operator!=( const RCP<T> &p, ENull );
00825 
00831 template<class T1, class T2>
00832 bool operator==( const RCP<T1> &p1, const RCP<T2> &p2 );
00833 
00839 template<class T1, class T2>
00840 bool operator!=( const RCP<T1> &p1, const RCP<T2> &p2 );
00841 
00851 template<class T2, class T1>
00852 RCP<T2> rcp_implicit_cast(const RCP<T1>& p1);
00853 
00864 template<class T2, class T1>
00865 RCP<T2> rcp_static_cast(const RCP<T1>& p1);
00866 
00873 template<class T2, class T1>
00874 RCP<T2> rcp_const_cast(const RCP<T1>& p1);
00875 
00898 template<class T2, class T1>
00899 RCP<T2> rcp_dynamic_cast(
00900   const RCP<T1>& p1
00901   ,bool throw_on_fail
00902 #ifndef __sun
00903   = false
00904 #endif
00905   );
00906 #ifdef __sun // RAB: 20041019: Sun needs to fix their compiler
00907 template<class T2, class T1> inline RCP<T2> rcp_dynamic_cast( const RCP<T1>& p1 )
00908 { return rcp_dynamic_cast<T2>(p1,false); }
00909 #endif
00910 
00967 template<class T1, class T2>
00968 void set_extra_data(
00969   const T1 &extra_data,
00970   const std::string& name, RCP<T2> *p,
00971   EPrePostDestruction destroy_when = POST_DESTROY,
00972   bool force_unique = true
00973   );
00974 
00993 template<class T1, class T2>
00994 T1& get_extra_data( RCP<T2>& p, const std::string& name );
00995 
01020 template<class T1, class T2>
01021 const T1& get_extra_data( const RCP<T2>& p, const std::string& name );
01022 
01046 template<class T1, class T2>
01047 T1* get_optional_extra_data( RCP<T2>& p, const std::string& name );
01048 
01077 template<class T1, class T2>
01078 const T1* get_optional_extra_data( const RCP<T2>& p, const std::string& name );
01079 
01090 template<class Dealloc_T, class T>
01091 Dealloc_T& get_dealloc( RCP<T>& p );
01092 
01110 template<class Dealloc_T, class T>
01111 const Dealloc_T& get_dealloc( const RCP<T>& p );
01112 
01127 template<class Dealloc_T, class T>
01128 Dealloc_T* get_optional_dealloc( RCP<T>& p );
01129 
01151 template<class Dealloc_T, class T>
01152 const Dealloc_T* get_optional_dealloc( const RCP<T>& p );
01153 
01161 template<class T>
01162 std::ostream& operator<<( std::ostream& out, const RCP<T>& p );
01163 
01180 void print_active_RCP_nodes(std::ostream &out);
01181 
01182 } // end namespace Teuchos
01183 
01184 #endif  // TEUCHOS_RCP_DECL_HPP

Generated on Tue Oct 20 12:45:26 2009 for Teuchos - Trilinos Tools Package by doxygen 1.4.7