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 
00313 
00315 enum ENull { null };
00316 
00318 enum EPrePostDestruction { PRE_DESTROY, POST_DESTROY };
00319 
00321 template<class T>
00322 class DeallocDelete
00323 {
00324 public:
00326   typedef T ptr_t;
00328   void free( T* ptr ) { if(ptr) delete ptr; }
00329 };
00330 
00332 template<class T>
00333 class DeallocArrayDelete
00334 {
00335 public:
00337   typedef T ptr_t;
00339   void free( T* ptr ) { if(ptr) delete [] ptr; }
00340 };
00341 
00343 template<class T>
00344 class RefCountPtr {
00345 public:
00347   typedef T element_type;
00360   RefCountPtr( ENull null_arg = null );
00378   RefCountPtr(const RefCountPtr<T>& r_ptr);
00392   template<class T2>
00393   RefCountPtr(const RefCountPtr<T2>& r_ptr);
00404   ~RefCountPtr();
00421   RefCountPtr<T>& operator=(const RefCountPtr<T>& r_ptr);
00429   T* operator->() const;
00437   T& operator*() const;
00439 
00441   T* get() const;
00462   T* release();
00469   int count() const;
00488   void set_has_ownership();
00498   bool has_ownership() const;
00505   bool shares_resource(const RefCountPtr<T>& r_ptr) const;
00507   const RefCountPtr<T>& assert_not_null() const;
00508 
00509 private:
00510 
00511   // //////////////////////////////////////
00512   // Private types
00513 
00514   typedef PrivateUtilityPack::RefCountPtr_node      node_t;
00515 
00516   // //////////////////////////////////////////////////////////////
00517   // Private data members
00518 
00519   T       *ptr_;  // NULL if this pointer is null
00520   node_t  *node_; // NULL if this pointer is null
00521 
00522 public:
00523 #ifndef DOXYGEN_COMPILE
00524   // These constructors should be private but I have not had good luck making
00525   // this portable (i.e. using friendship etc.) in the past
00526   RefCountPtr( T* p, bool has_ownership );
00527   template<class Dealloc_T>
00528   RefCountPtr( T* p, Dealloc_T dealloc, bool has_ownership );
00529   // This is a very bad breach of encapsulation that is needed since MS VC++ 5.0 will
00530   // not allow me to declare template functions as friends.
00531   RefCountPtr( T* p, node_t* node);
00532   T*&           access_ptr();
00533   node_t*&      access_node();
00534   node_t*       access_node() const;
00535 #endif
00536 
00537 };  // end class RefCountPtr<...>
00538 
00560 template<class T>
00561 RefCountPtr<T> rcp( T* p, bool owns_mem
00562 #ifndef __sun
00563   = true
00564 #endif
00565   );
00566 #ifdef __sun // RAB: 20040303: Sun needs to fix their compiler
00567 template<class T> inline RefCountPtr<T> rcp( T* p ) { return rcp(p,true); }
00568 #endif
00569 
00610 template<class T, class Dealloc_T>
00611 RefCountPtr<T> rcp( T* p, Dealloc_T dealloc, bool owns_mem );
00612 
00620 template<class T2, class T1>
00621 RefCountPtr<T2> rcp_implicit_cast(const RefCountPtr<T1>& p1);
00622 
00631 template<class T2, class T1>
00632 RefCountPtr<T2> rcp_static_cast(const RefCountPtr<T1>& p1);
00633 
00638 template<class T2, class T1>
00639 RefCountPtr<T2> rcp_const_cast(const RefCountPtr<T1>& p1);
00640 
00661 template<class T2, class T1>
00662 RefCountPtr<T2> rcp_dynamic_cast(
00663   const RefCountPtr<T1>& p1
00664   ,bool throw_on_fail
00665 #ifndef __sun
00666   = false
00667 #endif
00668   );
00669 #ifdef __sun // RAB: 20041019: Sun needs to fix their compiler
00670 template<class T2, class T1> inline RefCountPtr<T2> rcp_dynamic_cast( const RefCountPtr<T1>& p1 )
00671 { return rcp_dynamic_cast<T2>(p1,false); }
00672 #endif
00673 
00733 template<class T1, class T2>
00734 void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p, bool force_unique
00735 #ifndef __sun
00736                      = true
00737 #endif
00738                      ,EPrePostDestruction destroy_when
00739 #ifndef __sun
00740                      = POST_DESTROY
00741 #endif
00742   );
00743 #ifdef __sun
00744 template<class T1, class T2>
00745 inline void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p )
00746 { set_extra_data( extra_data, name, p, true, POST_DESTROY ); }
00747 template<class T1, class T2>
00748 inline void set_extra_data( const T1 &extra_data, const std::string& name, RefCountPtr<T2> *p, bool force_unique )
00749 { set_extra_data( extra_data, name, p, force_unique, POST_DESTROY ); }
00750 #endif
00751 
00768 template<class T1, class T2>
00769 T1& get_extra_data( RefCountPtr<T2>& p, const std::string& name );
00770 
00793 template<class T1, class T2>
00794 const T1& get_extra_data( const RefCountPtr<T2>& p, const std::string& name );
00795 
00805 template<class Dealloc_T, class T>
00806 Dealloc_T& get_dealloc( RefCountPtr<T>& p );
00807 
00823 template<class Dealloc_T, class T>
00824 const Dealloc_T& get_dealloc( const RefCountPtr<T>& p );
00825 
00827 
00828 } // end namespace Teuchos
00829 
00830 #endif  // TEUCHOS_REFCOUNTPTR_DECL_H

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