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 
00032 
00038 #include "Teuchos_RCPNode.hpp"
00039 #include "Teuchos_ENull.hpp"
00040 #include "Teuchos_NullIteratorTraits.hpp"
00041 
00042 
00043 #ifdef REFCOUNTPTR_INLINE_FUNCS
00044 #  define REFCOUNTPTR_INLINE inline
00045 #else
00046 #  define REFCOUNTPTR_INLINE
00047 #endif
00048 
00049 
00050 #ifdef TEUCHOS_DEBUG
00051 #  define TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
00052 #endif
00053 
00054 
00055 namespace Teuchos {
00056 
00057 
00059 template<class T> class Ptr;
00060 
00061 
00409 template<class T>
00410 class RCP {
00411 public:
00412 
00414   typedef T  element_type;
00415 
00418 
00440   RCP( ENull null_arg = null );
00441 
00457   explicit RCP( T* p, ENull null_arg );
00458 
00479   explicit RCP( T* p, bool has_ownership = true );
00480 
00503   template<class Dealloc_T>
00504   RCP( T* p, Dealloc_T dealloc, bool has_ownership );
00505 
00523   RCP(const RCP<T>& r_ptr);
00524 
00538   template<class T2>
00539   RCP(const RCP<T2>& r_ptr);
00540 
00552   ~RCP();
00553 
00573   RCP<T>& operator=(const RCP<T>& r_ptr);
00574 
00585   template<class T2>
00586   void reset(T2* p, bool has_ownership = true);
00587 
00589 
00592 
00594   bool is_null() const;
00595 
00602   T* operator->() const;
00603 
00610   T& operator*() const;
00611 
00616   T* get() const;
00617 
00622   T* getRawPtr() const;
00623 
00625   Ptr<T> ptr() const;
00626 
00628 
00631 
00643   ERCPStrength strength() const;
00644 
00654   bool is_valid_ptr() const;
00655 
00661   int strong_count() const;
00662 
00668   int weak_count() const;
00669 
00671   int total_count() const;
00672 
00691   void set_has_ownership();
00692 
00703   bool has_ownership() const;
00704 
00736   Ptr<T> release();
00737 
00754   RCP<T> create_weak() const;
00755 
00772   RCP<T> create_strong() const;
00773 
00781   template<class T2>
00782   bool shares_resource(const RCP<T2>& r_ptr) const;
00783 
00785 
00788 
00792   const RCP<T>& assert_not_null() const;
00793 
00803   const RCP<T>& assert_valid_ptr() const;
00804 
00806 
00809 
00811   int count() const;
00812 
00814 
00815 private:
00816 
00817   // //////////////////////////////////////////////////////////////
00818   // Private data members
00819 
00820   T *ptr_; // NULL if this pointer is null
00821   RCPNodeHandle node_; // NULL if this pointer is null
00822 
00823   const RCP<T>& debug_assert_not_null() const
00824     {
00825 #ifdef TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
00826       assert_not_null();
00827 #endif
00828       return *this;
00829     }
00830 
00831   const RCP<T>& debug_assert_valid_ptr() const
00832     {
00833 #ifdef TEUCHOS_DEBUG
00834       assert_valid_ptr();
00835 #endif
00836       return *this;
00837     }
00838 
00839 public: // Bad bad bad
00840 
00841 #ifndef DOXYGEN_COMPILE
00842 
00843   // WARNING: A general user should *never* call these functions!
00844   RCP( T* p, const RCPNodeHandle &node);
00845   T* access_private_ptr() const; // Does not throw
00846   RCPNodeHandle& nonconst_access_private_node(); // Does not thorw
00847   const RCPNodeHandle& access_private_node() const; // Does not thorw
00848 
00849 #endif
00850 
00851 };
00852 
00853 
00854 // 2008/09/22: rabartl: NOTE: I removed the TypeNameTraits<RCP<T> >
00855 // specialization since I want to be able to print the type name of an RCP
00856 // that does not have the type T fully defined!
00857 
00858 
00863 template<typename T>
00864 class NullIteratorTraits<RCP<T> > {
00865 public:
00866   static RCP<T> getNull() { return null; }
00867 };
00868 
00869 
00874 template<class T>
00875 class DeallocNull
00876 {
00877 public:
00879   typedef T ptr_t;
00881   void free( T* ptr ) { }
00882 };
00883 
00884 
00890 template<class T>
00891 class DeallocDelete
00892 {
00893 public:
00895   typedef T ptr_t;
00897   void free( T* ptr ) { if(ptr) delete ptr; }
00898 };
00899 
00900 
00906 template<class T>
00907 class DeallocArrayDelete
00908 {
00909 public:
00911   typedef T ptr_t;
00913   void free( T* ptr ) { if(ptr) delete [] ptr; }
00914 };
00915 
00916 
00929 template<class T, class DeleteFunctor>
00930 class DeallocFunctorDelete
00931 {
00932 public:
00933   DeallocFunctorDelete( DeleteFunctor deleteFunctor ) : deleteFunctor_(deleteFunctor) {}
00934   typedef T ptr_t;
00935   void free( T* ptr ) { if(ptr) deleteFunctor_(ptr); }
00936 private:
00937   DeleteFunctor deleteFunctor_;
00938   DeallocFunctorDelete(); // Not defined and not to be called!
00939 };
00940 
00941 
00946 template<class T, class DeleteFunctor>
00947 DeallocFunctorDelete<T,DeleteFunctor>
00948 deallocFunctorDelete( DeleteFunctor deleteFunctor )
00949 {
00950   return DeallocFunctorDelete<T,DeleteFunctor>(deleteFunctor);
00951 }
00952 
00953 
00967 template<class T, class DeleteHandleFunctor>
00968 class DeallocFunctorHandleDelete
00969 {
00970 public:
00971   DeallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00972     : deleteHandleFunctor_(deleteHandleFunctor) {}
00973   typedef T ptr_t;
00974   void free( T* ptr ) { if(ptr) { T **hdl = &ptr; deleteHandleFunctor_(hdl); } }
00975 private:
00976   DeleteHandleFunctor deleteHandleFunctor_;
00977   DeallocFunctorHandleDelete(); // Not defined and not to be called!
00978 };
00979 
00980 
00985 template<class T, class DeleteHandleFunctor>
00986 DeallocFunctorHandleDelete<T,DeleteHandleFunctor>
00987 deallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00988 {
00989   return DeallocFunctorHandleDelete<T,DeleteHandleFunctor>(deleteHandleFunctor);
00990 }
00991 
00992 
01001 template<class T, class Embedded, class Dealloc>
01002 class EmbeddedObjDealloc
01003 {
01004 public:
01005   typedef typename Dealloc::ptr_t ptr_t;
01006   EmbeddedObjDealloc(
01007     const Embedded &embedded, EPrePostDestruction prePostDestroy,
01008     Dealloc dealloc
01009     ) : embedded_(embedded), prePostDestroy_(prePostDestroy), dealloc_(dealloc)
01010     {}
01011   void setObj( const Embedded &embedded ) { embedded_ = embedded; }
01012   const Embedded& getObj() const { return embedded_; }
01013   Embedded& getNonconstObj() { return embedded_; }
01014   void free( T* ptr )
01015     {
01016       if (prePostDestroy_ == PRE_DESTROY)
01017         embedded_ = Embedded();
01018       dealloc_.free(ptr);
01019       if (prePostDestroy_ == POST_DESTROY)
01020         embedded_ = Embedded();
01021     }
01022 private:
01023   Embedded embedded_;
01024   EPrePostDestruction prePostDestroy_;
01025   Dealloc dealloc_;
01026   EmbeddedObjDealloc(); // Not defined and not to be called!
01027 };
01028 
01029 
01034 template<class T, class Embedded >
01035 EmbeddedObjDealloc<T,Embedded,DeallocDelete<T> >
01036 embeddedObjDeallocDelete(const Embedded &embedded, EPrePostDestruction prePostDestroy)
01037 {
01038   return EmbeddedObjDealloc<T,Embedded,DeallocDelete<T> >(
01039     embedded, prePostDestroy,DeallocDelete<T>());
01040 }
01041 
01042 
01065 template<class T>
01066 RCP<T> rcp( T* p, bool owns_mem = true );
01067 
01068 
01112 template<class T, class Dealloc_T>
01113 RCP<T> rcp( T* p, Dealloc_T dealloc, bool owns_mem );
01114 
01115 
01120 template<class T>
01121 Teuchos::RCP<T> rcpFromRef( T& r );
01122 
01123 
01124 /* \brief Create an RCP with and also put in an embedded object.
01125  *
01126  * In this case the embedded object is destroyed (by setting to Embedded())
01127  * before the object at <tt>*p</tt> is destroyed.
01128  *
01129  * The embedded object can be extracted using <tt>getEmbeddedObj()</tt> and
01130  * <tt>getNonconstEmbeddedObject()</tt>.
01131  *
01132  * \relates RCP
01133  */
01134 template<class T, class Embedded>
01135 RCP<T>
01136 rcpWithEmbeddedObjPreDestroy( T* p, const Embedded &embedded, bool owns_mem = true );
01137 
01138 
01139 /* \brief Create an RCP with and also put in an embedded object.
01140  *
01141  * In this case the embedded object is destroyed (by setting to Embedded())
01142  * after the object at <tt>*p</tt> is destroyed.
01143  *
01144  * The embedded object can be extracted using <tt>getEmbeddedObj()</tt> and
01145  * <tt>getNonconstEmbeddedObject()</tt>.
01146  *
01147  * \relates RCP
01148  */
01149 template<class T, class Embedded>
01150 RCP<T>
01151 rcpWithEmbeddedObjPostDestroy( T* p, const Embedded &embedded, bool owns_mem = true );
01152 
01153 
01154 /* \brief Create an RCP with and also put in an embedded object.
01155  *
01156  * This function should be called when it is not important when the embedded
01157  * object is destroyed (by setting to Embedded()) with respect to when
01158  * <tt>*p</tt> is destroyed.
01159  *
01160  * The embedded object can be extracted using <tt>getEmbeddedObj()</tt> and
01161  * <tt>getNonconstEmbeddedObject()</tt>.
01162  *
01163  * \relates RCP
01164  */
01165 template<class T, class Embedded>
01166 RCP<T>
01167 rcpWithEmbeddedObj( T* p, const Embedded &embedded, bool owns_mem = true );
01168 
01169 
01170 // 2007/10/25: rabartl: ToDo: put in versions of
01171 // rcpWithEmbedded[Pre,Post]DestoryWithDealloc(...) that also accept a general
01172 // deallocator!
01173 
01174 
01179 template<class T>
01180 bool is_null( const RCP<T> &p );
01181 
01182 
01187 template<class T>
01188 bool nonnull( const RCP<T> &p );
01189 
01190 
01195 template<class T>
01196 bool operator==( const RCP<T> &p, ENull );
01197 
01198 
01203 template<class T>
01204 bool operator!=( const RCP<T> &p, ENull );
01205 
01206 
01212 template<class T1, class T2>
01213 bool operator==( const RCP<T1> &p1, const RCP<T2> &p2 );
01214 
01215 
01221 template<class T1, class T2>
01222 bool operator!=( const RCP<T1> &p1, const RCP<T2> &p2 );
01223 
01224 
01234 template<class T2, class T1>
01235 RCP<T2> rcp_implicit_cast(const RCP<T1>& p1);
01236 
01237 
01248 template<class T2, class T1>
01249 RCP<T2> rcp_static_cast(const RCP<T1>& p1);
01250 
01251 
01258 template<class T2, class T1>
01259 RCP<T2> rcp_const_cast(const RCP<T1>& p1);
01260 
01261 
01285 template<class T2, class T1>
01286 RCP<T2> rcp_dynamic_cast(
01287   const RCP<T1>& p1, bool throw_on_fail = false
01288   );
01289 
01290 
01349 template<class T1, class T2>
01350 void set_extra_data( const T1 &extra_data, const std::string& name,
01351   const Ptr<RCP<T2> > &p, EPrePostDestruction destroy_when = POST_DESTROY,
01352   bool force_unique = true);
01353 
01373 template<class T1, class T2>
01374 const T1& get_extra_data( const RCP<T2>& p, const std::string& name );
01375 
01376 
01396 template<class T1, class T2>
01397 T1& get_nonconst_extra_data( RCP<T2>& p, const std::string& name );
01398 
01399 
01424 template<class T1, class T2>
01425 Ptr<const T1> get_optional_extra_data( const RCP<T2>& p, const std::string& name );
01426 
01427 
01452 template<class T1, class T2>
01453 Ptr<T1> get_optional_nonconst_extra_data( RCP<T2>& p, const std::string& name );
01454 
01455 
01467 template<class Dealloc_T, class T>
01468 const Dealloc_T& get_dealloc( const RCP<T>& p );
01469 
01470 
01482 template<class Dealloc_T, class T>
01483 Dealloc_T& get_nonconst_dealloc( const RCP<T>& p );
01484 
01485 
01500 template<class Dealloc_T, class T>
01501 Ptr<const Dealloc_T> get_optional_dealloc( const RCP<T>& p );
01502 
01503 
01518 template<class Dealloc_T, class T>
01519 Ptr<Dealloc_T> get_optional_nonconst_dealloc( const RCP<T>& p );
01520 
01521 
01528 template<class TOrig, class Embedded, class T>
01529 const Embedded& getEmbeddedObj( const RCP<T>& p );
01530 
01531 
01538 template<class TOrig, class Embedded, class T>
01539 Embedded& getNonconstEmbeddedObj( const RCP<T>& p );
01540 
01541 
01548 template<class TOrig, class Embedded, class T>
01549 Ptr<const Embedded> getOptionalEmbeddedObj( const RCP<T>& p );
01550 
01551 
01558 template<class TOrig, class Embedded, class T>
01559 Ptr<Embedded> getOptionalNonconstEmbeddedObj( const RCP<T>& p );
01560 
01561 
01569 template<class T>
01570 std::ostream& operator<<( std::ostream& out, const RCP<T>& p );
01571 
01572 
01573 } // end namespace Teuchos
01574 
01575 
01576 #endif  // TEUCHOS_RCP_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:14:00 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1