Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_RCPDecl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_RCP_DECL_HPP
43 #define TEUCHOS_RCP_DECL_HPP
44 
45 
51 #include "Teuchos_RCPNode.hpp"
52 #include "Teuchos_ENull.hpp"
54 
55 
56 #ifdef REFCOUNTPTR_INLINE_FUNCS
57 # define REFCOUNTPTR_INLINE inline
58 #else
59 # define REFCOUNTPTR_INLINE
60 #endif
61 
62 
63 #ifdef TEUCHOS_DEBUG
64 # define TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
65 #endif
66 
67 
68 namespace Teuchos {
69 
70 
72 template<class T> class Ptr;
73 
74 
78 
79 
428 template<class T>
429 class RCP {
430 public:
431 
433  typedef T element_type;
434 
437 
459  inline RCP(ENull null_arg = null);
460 
481  inline explicit RCP( T* p, bool has_ownership = true );
482 
502  template<class Dealloc_T>
503  inline RCP(T* p, Dealloc_T dealloc, bool has_ownership);
504 
522  inline RCP(const RCP<T>& r_ptr);
523 
531  inline RCP(RCP<T>&& r_ptr);
532 
546  template<class T2>
547  inline RCP(const RCP<T2>& r_ptr);
548 
558  template<class T2>
559  inline RCP(const RCP<T2>& r_ptr, T* ptr);
560 
572  inline ~RCP();
573 
593  inline RCP<T>& operator=(const RCP<T>& r_ptr);
594 
604  inline RCP<T>& operator=(RCP<T>&& r_ptr);
605 
618  inline RCP<T>& operator=(ENull);
619 
621  inline void swap(RCP<T> &r_ptr);
622 
624 
627 
629  inline bool is_null() const;
630 
637  inline T* operator->() const;
638 
645  inline T& operator*() const;
646 
651  inline T* get() const;
652 
657  inline T* getRawPtr() const;
658 
660  inline Ptr<T> ptr() const;
661 
663  inline Ptr<T> operator()() const;
664 
666  inline explicit operator bool() const;
667 
669  inline RCP<const T> getConst() const;
670 
672 
675 
685  inline ERCPStrength strength() const;
686 
696  inline bool is_valid_ptr() const;
697 
703  inline int strong_count() const;
704 
710  inline int weak_count() const;
711 
713  inline int total_count() const;
714 
733  inline void set_has_ownership();
734 
745  inline bool has_ownership() const;
746 
778  inline Ptr<T> release();
779 
796  inline RCP<T> create_weak() const;
797 
814  inline RCP<T> create_strong() const;
815 
816 #if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_THREAD_SAFE)
817 
818  inline RCP<T> create_strong_thread_safe() const; // this format to be determined
819 #endif
820 
828  template<class T2>
829  inline bool shares_resource(const RCP<T2>& r_ptr) const;
830 
832 
835 
839  inline const RCP<T>& assert_not_null() const;
840 
850  inline const RCP<T>& assert_valid_ptr() const;
851 
853  inline const RCP<T>& debug_assert_not_null() const
854  {
855 #ifdef TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
856  assert_not_null();
857 #endif
858  return *this;
859  }
860 
862  inline const RCP<T>& debug_assert_valid_ptr() const
863  {
864 #ifdef TEUCHOS_DEBUG
866 #endif
867  return *this;
868  }
869 
871 
874 
876  inline void reset();
877 
888  template<class T2>
889  inline void reset(T2* p, bool has_ownership = true);
890 
892 
893 private:
894 
895  // //////////////////////////////////////////////////////////////
896  // Private data members
897 
898  T *ptr_; // NULL if this pointer is null
899  RCPNodeHandle node_; // NULL if this pointer is null
900 
901 public: // Bad bad bad
902 
903  // These constructors are put here because we don't want to confuse users
904  // who would otherwise see them.
905 
917  inline explicit RCP(T* p, ERCPWeakNoDealloc);
918 
929  inline explicit RCP(T* p, ERCPUndefinedWeakNoDealloc);
930 
937  template<class Dealloc_T>
938  inline RCP(T* p, Dealloc_T dealloc, ERCPUndefinedWithDealloc,
939  bool has_ownership = true);
940 
941 #ifndef DOXYGEN_COMPILE
942 
943  // WARNING: A general user should *never* call these functions!
944  inline RCP(T* p, const RCPNodeHandle &node);
945  inline T* access_private_ptr() const; // Does not throw
946  inline RCPNodeHandle& nonconst_access_private_node(); // Does not thorw
947  inline const RCPNodeHandle& access_private_node() const; // Does not thorw
948 
949 #endif
950 
951 };
952 
955 struct RCPComp {
957  template<class T1, class T2> inline
958  bool operator() (const RCP<T1> p1, const RCP<T2> p2) const{
959  return p1.get() < p2.get();
960  }
961 };
962 
965 struct RCPConstComp {
967  template<class T1, class T2> inline
968  bool operator() (const RCP<const T1> p1, const RCP<const T2> p2) const{
969  return p1.get() < p2.get();
970  }
971 };
972 
973 
974 
975 // 2008/09/22: rabartl: NOTE: I removed the TypeNameTraits<RCP<T> >
976 // specialization since I want to be able to print the type name of an RCP
977 // that does not have the type T fully defined!
978 
979 
984 template<typename T>
986 public:
987  static RCP<T> getNull() { return null; }
988 };
989 
990 
995 template<class T>
997 {
998 public:
1000  typedef T ptr_t;
1002  void free( T* ptr ) {
1003  (void) ptr; // silence "unused parameter" compiler warning
1004  }
1005 };
1006 
1007 
1013 template<class T>
1015 {
1016 public:
1018  typedef T ptr_t;
1020  void free( T* ptr ) { if(ptr) delete ptr; }
1021 };
1022 
1023 
1029 template<class T>
1031 {
1032 public:
1034  typedef T ptr_t;
1036  void free( T* ptr ) { if(ptr) delete [] ptr; }
1037 };
1038 
1039 
1052 template<class T, class DeleteFunctor>
1054 {
1055 public:
1056  DeallocFunctorDelete( DeleteFunctor deleteFunctor ) : deleteFunctor_(deleteFunctor) {}
1057  typedef T ptr_t;
1058  void free( T* ptr ) { if(ptr) deleteFunctor_(ptr); }
1059 private:
1060  DeleteFunctor deleteFunctor_;
1061  DeallocFunctorDelete(); // Not defined and not to be called!
1062 };
1063 
1064 
1069 template<class T, class DeleteFunctor>
1071 deallocFunctorDelete( DeleteFunctor deleteFunctor )
1072 {
1073  return DeallocFunctorDelete<T,DeleteFunctor>(deleteFunctor);
1074 }
1075 
1076 
1090 template<class T, class DeleteHandleFunctor>
1092 {
1093 public:
1094  DeallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
1095  : deleteHandleFunctor_(deleteHandleFunctor) {}
1096  typedef T ptr_t;
1097  void free( T* ptr ) { if(ptr) { T **hdl = &ptr; deleteHandleFunctor_(hdl); } }
1098 private:
1099  DeleteHandleFunctor deleteHandleFunctor_;
1100  DeallocFunctorHandleDelete(); // Not defined and not to be called!
1101 };
1102 
1103 
1108 template<class T, class DeleteHandleFunctor>
1110 deallocFunctorHandleDelete( DeleteHandleFunctor deleteHandleFunctor )
1111 {
1112  return DeallocFunctorHandleDelete<T,DeleteHandleFunctor>(deleteHandleFunctor);
1113 }
1114 
1115 
1124 template<class T, class Embedded, class Dealloc>
1126 {
1127 public:
1128  typedef typename Dealloc::ptr_t ptr_t;
1130  const Embedded &embedded, EPrePostDestruction prePostDestroy,
1131  Dealloc dealloc
1132  ) : embedded_(embedded), prePostDestroy_(prePostDestroy), dealloc_(dealloc)
1133  {}
1134  void setObj( const Embedded &embedded ) { embedded_ = embedded; }
1135  const Embedded& getObj() const { return embedded_; }
1136  Embedded& getNonconstObj() { return embedded_; }
1137  void free( T* ptr )
1138  {
1140  embedded_ = Embedded();
1141  dealloc_.free(ptr);
1143  embedded_ = Embedded();
1144  }
1145 private:
1146  Embedded embedded_;
1148  Dealloc dealloc_;
1149  EmbeddedObjDealloc(); // Not defined and not to be called!
1150 };
1151 
1152 
1157 template<class T, class Embedded >
1159 embeddedObjDeallocDelete(const Embedded &embedded, EPrePostDestruction prePostDestroy)
1160 {
1162  embedded, prePostDestroy,DeallocDelete<T>());
1163 }
1164 
1165 
1170 template<class T, class Embedded >
1172 embeddedObjDeallocArrayDelete(const Embedded &embedded, EPrePostDestruction prePostDestroy)
1173 {
1175  embedded, prePostDestroy,DeallocArrayDelete<T>());
1176 }
1177 
1178 
1201 template<class T> inline
1202 RCP<T> rcp(T* p, bool owns_mem = true);
1203 
1209 template <typename T, typename ... Args>
1210 inline auto make_rcp(Args&& ... args)
1211 {
1212  return Teuchos::rcp(
1213  new T(std::forward<Args>(args)...)
1214  );
1215 }
1216 
1260 template<class T, class Dealloc_T> inline
1261 RCP<T> rcpWithDealloc(T* p, Dealloc_T dealloc, bool owns_mem=true);
1262 
1263 
1265 template<class T, class Dealloc_T> inline
1266 TEUCHOS_DEPRECATED RCP<T> rcp( T* p, Dealloc_T dealloc, bool owns_mem )
1267 {
1268  return rcpWithDealloc(p, dealloc, owns_mem);
1269 }
1270 
1271 
1283 template<class T, class Dealloc_T> inline
1284 RCP<T> rcpWithDeallocUndef(T* p, Dealloc_T dealloc, bool owns_mem=true);
1285 
1286 
1296 template<class T> inline
1297 RCP<T> rcpFromRef(T& r);
1298 
1299 
1309 template<class T> inline
1310 RCP<T> rcpFromUndefRef(T& r);
1311 
1312 
1323 template<class T, class Embedded> inline
1324 RCP<T>
1325 rcpWithEmbeddedObjPreDestroy( T* p, const Embedded &embedded, bool owns_mem = true );
1326 
1327 
1338 template<class T, class Embedded> inline
1339 RCP<T>
1340 rcpWithEmbeddedObjPostDestroy( T* p, const Embedded &embedded, bool owns_mem = true );
1341 
1342 
1354 template<class T, class Embedded> inline
1355 RCP<T>
1356 rcpWithEmbeddedObj( T* p, const Embedded &embedded, bool owns_mem = true );
1357 
1358 
1359 // 2007/10/25: rabartl: ToDo: put in versions of
1360 // rcpWithEmbedded[Pre,Post]DestoryWithDealloc(...) that also accept a general
1361 // deallocator!
1362 
1363 
1373 template<class T, class ParentT>
1374 RCP<T> rcpWithInvertedObjOwnership(const RCP<T> &child, const RCP<ParentT> &parent);
1375 
1376 
1390 template<class T>
1391 RCP<T> rcpCloneNode(const RCP<T> &p);
1392 
1393 
1398 template<class T> inline
1399 bool is_null( const RCP<T> &p );
1400 
1401 
1406 template<class T> inline
1407 bool nonnull( const RCP<T> &p );
1408 
1409 
1414 template<class T> inline
1415 bool operator==( const RCP<T> &p, ENull );
1416 
1417 
1422 template<class T> inline
1423 bool operator!=( const RCP<T> &p, ENull );
1424 
1425 
1431 template<class T1, class T2> inline
1432 bool operator==( const RCP<T1> &p1, const RCP<T2> &p2 );
1433 
1434 
1440 template<class T1, class T2> inline
1441 bool operator!=( const RCP<T1> &p1, const RCP<T2> &p2 );
1442 
1443 
1453 template<class T2, class T1> inline
1454 RCP<T2> rcp_implicit_cast(const RCP<T1>& p1);
1455 
1456 
1467 template<class T2, class T1> inline
1468 RCP<T2> rcp_static_cast(const RCP<T1>& p1);
1469 
1470 
1477 template<class T2, class T1> inline
1478 RCP<T2> rcp_const_cast(const RCP<T1>& p1);
1479 
1480 
1504 template<class T2, class T1> inline
1505 RCP<T2> rcp_dynamic_cast(
1506  const RCP<T1>& p1, bool throw_on_fail = false
1507  );
1508 
1509 
1568 template<class T1, class T2>
1569 void set_extra_data( const T1 &extra_data, const std::string& name,
1570  const Ptr<RCP<T2> > &p, EPrePostDestruction destroy_when = POST_DESTROY,
1571  bool force_unique = true);
1572 
1592 template<class T1, class T2>
1593 const T1& get_extra_data( const RCP<T2>& p, const std::string& name );
1594 
1595 
1615 template<class T1, class T2>
1616 T1& get_nonconst_extra_data( RCP<T2>& p, const std::string& name );
1617 
1618 
1643 template<class T1, class T2>
1644 Ptr<const T1> get_optional_extra_data( const RCP<T2>& p, const std::string& name );
1645 
1646 
1671 template<class T1, class T2>
1672 Ptr<T1> get_optional_nonconst_extra_data( RCP<T2>& p, const std::string& name );
1673 
1674 
1686 template<class Dealloc_T, class T>
1687 const Dealloc_T& get_dealloc( const RCP<T>& p );
1688 
1689 
1701 template<class Dealloc_T, class T>
1702 Dealloc_T& get_nonconst_dealloc( const RCP<T>& p );
1703 
1704 
1719 template<class Dealloc_T, class T>
1720 Ptr<const Dealloc_T> get_optional_dealloc( const RCP<T>& p );
1721 
1722 
1737 template<class Dealloc_T, class T>
1738 Ptr<Dealloc_T> get_optional_nonconst_dealloc( const RCP<T>& p );
1739 
1740 
1747 template<class TOrig, class Embedded, class T>
1748 const Embedded& getEmbeddedObj( const RCP<T>& p );
1749 
1750 
1757 template<class TOrig, class Embedded, class T>
1758 Embedded& getNonconstEmbeddedObj( const RCP<T>& p );
1759 
1760 
1767 template<class TOrig, class Embedded, class T>
1768 Ptr<const Embedded> getOptionalEmbeddedObj( const RCP<T>& p );
1769 
1770 
1777 template<class TOrig, class Embedded, class T>
1778 Ptr<Embedded> getOptionalNonconstEmbeddedObj( const RCP<T>& p );
1779 
1780 
1786 template<class ParentT, class T>
1787 RCP<ParentT> getInvertedObjOwnershipParent(const RCP<T> &invertedChild);
1788 
1789 
1797 template<class T>
1798 std::ostream& operator<<( std::ostream& out, const RCP<T>& p );
1799 
1800 
1801 } // end namespace Teuchos
1802 
1803 
1804 #endif // TEUCHOS_RCP_DECL_HPP
~RCP()
Removes a reference to a dynamically allocated object and possibly deletes the object if owned...
RCP< const T > getConst() const
Return an RCP&lt;const T&gt; version of *this.
ERCPStrength
Used to specify if the pointer is weak or strong.
RCP(ENull null_arg=null)
Initialize RCP&lt;T&gt; to NULL.
DeallocFunctorHandleDelete< T, DeleteHandleFunctor > deallocFunctorHandleDelete(DeleteHandleFunctor deleteHandleFunctor)
A simple function used to create a functor deallocator object.
void free(T *ptr)
Deallocates a pointer ptr using delete ptr (required).
bool operator()(const RCP< T1 > p1, const RCP< T2 > p2) const
EPrePostDestruction prePostDestroy_
const RCP< T > & debug_assert_not_null() const
Calls assert_not_null() in a debug build.
RCP< T > create_weak() const
Create a new weak RCP object from another (strong) RCP object.
T ptr_t
Gives the type (required)
bool operator()(const RCP< const T1 > p1, const RCP< const T2 > p2) const
bool has_ownership() const
Returns true if this has ownership of object pointed to by this-&gt;get() in order to delete it...
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
Deallocator subclass that Allows any functor object (including a function pointer) to be used to free...
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
DeallocFunctorDelete(DeleteFunctor deleteFunctor)
void swap(RCP< T > &r_ptr)
Swap the contents with some other RCP object.
RCP< T > create_strong() const
Create a new strong RCP object from another (weak) RCP object.
T * get() const
Get the raw C++ pointer to the underlying object.
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
ERCPStrength strength() const
Strength of the pointer.
ERCPUndefinedWeakNoDealloc
A deallocator class that wraps a simple value object and delegates to another deallocator object...
T * operator->() const
Pointer (-&gt;) access to members of underlying object.
Struct for comparing two RCPs. Simply compares the raw pointers contained within the RCPs...
void free(T *ptr)
Deallocates a pointer ptr using delete ptr (required).
Struct for comparing two RCPs. Simply compares the raw pointers contained within the RCPs...
bool operator!=(const Allocator< T > &a_t, const Allocator< U > &a_u)
Return ! (a_t == a_u) (see above).
Policy class for deallocator that uses delete to delete a pointer which is used by RCP...
EmbeddedObjDealloc< T, Embedded, DeallocArrayDelete< T > > embeddedObjDeallocArrayDelete(const Embedded &embedded, EPrePostDestruction prePostDestroy)
Create a dealocator with an embedded object using delete [].
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
Deallocator class that uses delete [] to delete memory allocated uisng new []
const RCP< T > & assert_valid_ptr() const
If the object pointer is non-null, assert that it is still valid.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
EmbeddedObjDealloc(const Embedded &embedded, EPrePostDestruction prePostDestroy, Dealloc dealloc)
void set_has_ownership()
Give this and other RCP&lt;&gt; objects ownership of the referenced object this-&gt;get(). ...
void reset()
Reset to null.
const RCP< T > & debug_assert_valid_ptr() const
Calls assert_valid_ptr() in a debug build.
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
Ptr< T > operator()() const
Shorthand for ptr().
bool shares_resource(const RCP< T2 > &r_ptr) const
Returns true if the smart pointers share the same underlying reference-counted object.
T ptr_t
Gives the type (required)
T & operator*() const
Dereference the underlying object.
RCP< T > & operator=(const RCP< T > &r_ptr)
Copy the pointer to the referenced object and increment the reference count.
const Embedded & getObj() const
Base traits class for getting a properly initialized null pointer.
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this-&gt;get()==NULL, otherwise returns reference to *this.
Ptr< T > release()
Release the ownership of the underlying dynamically allocated object.
T ptr_t
Gives the type (required)
EmbeddedObjDealloc< T, Embedded, DeallocDelete< T > > embeddedObjDeallocDelete(const Embedded &embedded, EPrePostDestruction prePostDestroy)
Create a dealocator with an embedded object using delete.
bool is_valid_ptr() const
Return if the underlying object pointer is still valid or not.
EPrePostDestruction
Used to specify a pre or post destruction of extra data.
Handle class that manages the RCPNode&#39;s reference counting.
RCPNodeHandle node_
RCP< ParentT > getInvertedObjOwnershipParent(const RCP< T > &invertedChild)
Get the parent back from an inverted ownership RCP.
Policy class for deallocator for non-owned RCPs.
Deallocator subclass that Allows any functor object (including a function pointer) to be used to free...
DeallocFunctorDelete< T, DeleteFunctor > deallocFunctorDelete(DeleteFunctor deleteFunctor)
A simple function used to create a functor deallocator object.
Smart reference counting pointer class for automatic garbage collection.
bool operator==(BigUInt< n > const &a, BigUInt< n > const &b)
void setObj(const Embedded &embedded)
Reference-counted pointer node classes.
void free(T *ptr)
Deallocates a pointer ptr using delete [] ptr (required).
int strong_count() const
Return the number of active RCP&lt;&gt; objects that have a &quot;strong&quot; reference to the underlying reference-...
int weak_count() const
Return the number of active RCP&lt;&gt; objects that have a &quot;weak&quot; reference to the underlying reference-co...
int total_count() const
Total count (strong_count() + weak_count()).
DeallocFunctorHandleDelete(DeleteHandleFunctor deleteHandleFunctor)
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
auto make_rcp(Args &&...args)
bool is_null() const
Returns true if the underlying pointer is null.