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_Ptr.hpp"
00040 #include "Teuchos_ENull.hpp"
00041 #include "Teuchos_NullIteratorTraits.hpp"
00042 
00043 
00044 #ifdef REFCOUNTPTR_INLINE_FUNCS
00045 #  define REFCOUNTPTR_INLINE inline
00046 #else
00047 #  define REFCOUNTPTR_INLINE
00048 #endif
00049 
00050 
00051 #ifdef TEUCHOS_DEBUG
00052 #  define TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
00053 #endif
00054 
00055 
00056 namespace Teuchos {
00057 
00058 
00404 template<class T>
00405 class RCP {
00406 public:
00408   typedef T  element_type;
00421   RCP( ENull null_arg = null );
00435   explicit RCP( T* p, bool has_ownership = false );
00453   RCP(const RCP<T>& r_ptr);
00467   template<class T2>
00468   RCP(const RCP<T2>& r_ptr);
00480   ~RCP();
00498   RCP<T>& operator=(const RCP<T>& r_ptr);
00505   T* operator->() const;
00512   T& operator*() const;
00517   T* get() const;
00522   T* getRawPtr() const;
00524   Ptr<T> ptr() const;
00555   Ptr<T> release();
00562   int count() const;
00581   void set_has_ownership();
00591   bool has_ownership() const;
00598   template<class T2>
00599   bool shares_resource(const RCP<T2>& r_ptr) const;
00601   const RCP<T>& assert_not_null() const;
00602 
00603 private:
00604 
00605   // //////////////////////////////////////////////////////////////
00606   // Private data members
00607 
00608   T *ptr_; // NULL if this pointer is null
00609   RCPNode *node_; // NULL if this pointer is null
00610 
00611 public: // Bad bad bad
00612 #ifndef DOXYGEN_COMPILE
00613   // These constructors should be private but I have not had good luck making
00614   // this portable (i.e. using friendship etc.) in the past
00615   template<class Dealloc_T>
00616   RCP( T* p, Dealloc_T dealloc, bool has_ownership );
00617   // This is a very bad breach of encapsulation that is needed since MS VC++ 5.0 will
00618   // not allow me to declare template functions as friends.
00619   RCP( T* p, RCPNode* node);
00620   T*& access_ptr();
00621   RCPNode*& access_node();
00622   RCPNode* access_node() const;
00623 #endif
00624 
00625 };  // end class RCP<...>
00626 
00627 
00632 template<typename T>
00633 class TypeNameTraits<RCP<T> > {
00634 public:
00635   static std::string name() { return "RCP<"+TypeNameTraits<T>::name()+">"; }
00636   static std::string concreteName( const RCP<T>& /*t*/ )
00637     { return name(); }
00638 };
00639 
00640 
00645 template<typename T>
00646 class NullIteratorTraits<RCP<T> > {
00647 public:
00648   static RCP<T> getNull() { return null; }
00649 };
00650 
00651 
00657 template<class T>
00658 class DeallocDelete
00659 {
00660 public:
00662   typedef T ptr_t;
00664   void free( T* ptr ) { if(ptr) delete ptr; }
00665 };
00666 
00667 
00673 template<class T>
00674 class DeallocArrayDelete
00675 {
00676 public:
00678   typedef T ptr_t;
00680   void free( T* ptr ) { if(ptr) delete [] ptr; }
00681 };
00682 
00683 
00696 template<class T, class DeleteFunctor>
00697 class DeallocFunctorDelete
00698 {
00699 public:
00700   DeallocFunctorDelete( DeleteFunctor deleteFunctor ) : deleteFunctor_(deleteFunctor) {}
00701   typedef T ptr_t;
00702   void free( T* ptr ) { if(ptr) deleteFunctor_(ptr); }
00703 private:
00704   DeleteFunctor deleteFunctor_;
00705   DeallocFunctorDelete(); // Not defined and not to be called!
00706 };
00707 
00708 
00713 template<class T, class DeleteFunctor>
00714 DeallocFunctorDelete<T,DeleteFunctor>
00715 deallocFunctorDelete( DeleteFunctor deleteFunctor )
00716 {
00717   return DeallocFunctorDelete<T,DeleteFunctor>(deleteFunctor);
00718 }
00719 
00720 
00734 template<class T, class DeleteHandleFunctor>
00735 class DeallocFunctorHandleDelete
00736 {
00737 public:
00738   DeallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00739     : deleteHandleFunctor_(deleteHandleFunctor) {}
00740   typedef T ptr_t;
00741   void free( T* ptr ) { if(ptr) { T **hdl = &ptr; deleteHandleFunctor_(hdl); } }
00742 private:
00743   DeleteHandleFunctor deleteHandleFunctor_;
00744   DeallocFunctorHandleDelete(); // Not defined and not to be called!
00745 };
00746 
00747 
00752 template<class T, class DeleteHandleFunctor>
00753 DeallocFunctorHandleDelete<T,DeleteHandleFunctor>
00754 deallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
00755 {
00756   return DeallocFunctorHandleDelete<T,DeleteHandleFunctor>(deleteHandleFunctor);
00757 }
00758 
00759 
00768 template<class T, class Embedded, class Dealloc>
00769 class EmbeddedObjDealloc
00770 {
00771 public:
00772   typedef typename Dealloc::ptr_t ptr_t;
00773   EmbeddedObjDealloc(
00774     const Embedded &embedded, EPrePostDestruction prePostDestroy,
00775     Dealloc dealloc
00776     ) : embedded_(embedded), prePostDestroy_(prePostDestroy), dealloc_(dealloc)
00777     {}
00778   void setObj( const Embedded &embedded ) { embedded_ = embedded; }
00779   const Embedded& getObj() const { return embedded_; }
00780   Embedded& getNonconstObj() { return embedded_; }
00781   void free( T* ptr )
00782     {
00783       if (prePostDestroy_ == PRE_DESTROY)
00784         embedded_ = Embedded();
00785       dealloc_.free(ptr);
00786       if (prePostDestroy_ == POST_DESTROY)
00787         embedded_ = Embedded();
00788     }
00789 private:
00790   Embedded embedded_;
00791   EPrePostDestruction prePostDestroy_;
00792   Dealloc dealloc_;
00793   EmbeddedObjDealloc(); // Not defined and not to be called!
00794 };
00795 
00796 
00801 template<class T, class Embedded >
00802 EmbeddedObjDealloc<T,Embedded,DeallocDelete<T> >
00803 embeddedObjDeallocDelete(const Embedded &embedded, EPrePostDestruction prePostDestroy)
00804 {
00805   return EmbeddedObjDealloc<T,Embedded,DeallocDelete<T> >(
00806     embedded, prePostDestroy,DeallocDelete<T>());
00807 }
00808 
00809 
00832 template<class T>
00833 RCP<T> rcp( T* p, bool owns_mem = true );
00834 
00835 
00879 template<class T, class Dealloc_T>
00880 RCP<T> rcp( T* p, Dealloc_T dealloc, bool owns_mem );
00881 
00882 
00887 template<class T>
00888 Teuchos::RCP<T> rcpFromRef( T& r );
00889 
00890 
00891 /* \brief Create an RCP with and also put in an embedded object.
00892  *
00893  * In this case the embedded object is destroyed (by setting to Embedded())
00894  * before the object at <tt>*p</tt> is destroyed.
00895  *
00896  * The embedded object can be extracted using <tt>getEmbeddedObj()</tt> and
00897  * <tt>getNonconstEmbeddedObject()</tt>.
00898  *
00899  * \relates RCP
00900  */
00901 template<class T, class Embedded>
00902 RCP<T>
00903 rcpWithEmbeddedObjPreDestroy( T* p, const Embedded &embedded, bool owns_mem = true );
00904 
00905 
00906 /* \brief Create an RCP with and also put in an embedded object.
00907  *
00908  * In this case the embedded object is destroyed (by setting to Embedded())
00909  * after the object at <tt>*p</tt> is destroyed.
00910  *
00911  * The embedded object can be extracted using <tt>getEmbeddedObj()</tt> and
00912  * <tt>getNonconstEmbeddedObject()</tt>.
00913  *
00914  * \relates RCP
00915  */
00916 template<class T, class Embedded>
00917 RCP<T>
00918 rcpWithEmbeddedObjPostDestroy( T* p, const Embedded &embedded, bool owns_mem = true );
00919 
00920 
00921 /* \brief Create an RCP with and also put in an embedded object.
00922  *
00923  * This function should be called when it is not important when the embedded
00924  * object is destroyed (by setting to Embedded()) with respect to when
00925  * <tt>*p</tt> is destroyed.
00926  *
00927  * The embedded object can be extracted using <tt>getEmbeddedObj()</tt> and
00928  * <tt>getNonconstEmbeddedObject()</tt>.
00929  *
00930  * \relates RCP
00931  */
00932 template<class T, class Embedded>
00933 RCP<T>
00934 rcpWithEmbeddedObj( T* p, const Embedded &embedded, bool owns_mem = true );
00935 
00936 
00937 // 2007/10/25: rabartl: ToDo: put in versions of
00938 // rcpWithEmbedded[Pre,Post]DestoryWithDealloc(...) that also accept a general
00939 // deallocator!
00940 
00941 
00946 template<class T>
00947 bool is_null( const RCP<T> &p );
00948 
00949 
00954 template<class T>
00955 bool operator==( const RCP<T> &p, ENull );
00956 
00957 
00962 template<class T>
00963 bool operator!=( const RCP<T> &p, ENull );
00964 
00965 
00971 template<class T1, class T2>
00972 bool operator==( const RCP<T1> &p1, const RCP<T2> &p2 );
00973 
00974 
00980 template<class T1, class T2>
00981 bool operator!=( const RCP<T1> &p1, const RCP<T2> &p2 );
00982 
00983 
00993 template<class T2, class T1>
00994 RCP<T2> rcp_implicit_cast(const RCP<T1>& p1);
00995 
00996 
01007 template<class T2, class T1>
01008 RCP<T2> rcp_static_cast(const RCP<T1>& p1);
01009 
01010 
01017 template<class T2, class T1>
01018 RCP<T2> rcp_const_cast(const RCP<T1>& p1);
01019 
01020 
01044 template<class T2, class T1>
01045 RCP<T2> rcp_dynamic_cast(
01046   const RCP<T1>& p1, bool throw_on_fail = false
01047   );
01048 
01049 
01108 template<class T1, class T2>
01109 void set_extra_data(
01110   const T1 &extra_data,
01111   const std::string& name, RCP<T2> *p,
01112   EPrePostDestruction destroy_when = POST_DESTROY,
01113   bool force_unique = true
01114   );
01115 // 2007/12/01: rabartl: Above, replace const RCP<T2> *p with const Ptr<RCP<T2>
01116 // > &p but leave the old deprecated version for backward compatibility!
01117 
01118 
01138 template<class T1, class T2>
01139 const T1& get_extra_data( const RCP<T2>& p, const std::string& name );
01140 
01141 
01161 template<class T1, class T2>
01162 T1& get_nonconst_extra_data( const RCP<T2>& p, const std::string& name );
01163 
01164 
01189 template<class T1, class T2>
01190 Ptr<const T1> get_optional_extra_data( const RCP<T2>& p, const std::string& name );
01191 
01192 
01217 template<class T1, class T2>
01218 Ptr<T1> get_optional_nonconst_extra_data( const RCP<T2>& p, const std::string& name );
01219 
01220 
01232 template<class Dealloc_T, class T>
01233 const Dealloc_T& get_dealloc( const RCP<T>& p );
01234 
01235 
01247 template<class Dealloc_T, class T>
01248 Dealloc_T& get_nonconst_dealloc( const RCP<T>& p );
01249 
01250 
01265 template<class Dealloc_T, class T>
01266 Ptr<const Dealloc_T> get_optional_dealloc( const RCP<T>& p );
01267 
01268 
01283 template<class Dealloc_T, class T>
01284 Ptr<Dealloc_T> get_optional_nonconst_dealloc( const RCP<T>& p );
01285 
01286 
01293 template<class TOrig, class Embedded, class T>
01294 const Embedded& getEmbeddedObj( const RCP<T>& p );
01295 
01296 
01303 template<class TOrig, class Embedded, class T>
01304 Embedded& getNonconstEmbeddedObj( const RCP<T>& p );
01305 
01306 
01314 template<class T>
01315 std::ostream& operator<<( std::ostream& out, const RCP<T>& p );
01316 
01317 
01318 } // end namespace Teuchos
01319 
01320 
01321 #endif  // TEUCHOS_RCP_DECL_HPP

Generated on Wed May 12 21:40:32 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7