10 #ifndef TEUCHOS_RCP_HPP
11 #define TEUCHOS_RCP_HPP
27 #include "Teuchos_Ptr.hpp"
28 #include "Teuchos_Assert.hpp"
29 #include "Teuchos_Exceptions.hpp"
30 #include "Teuchos_dyn_cast.hpp"
43 RCPNode* RCP_createNewRCPNodeRawPtrNonowned( T* p )
45 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false);
51 RCPNode* RCP_createNewRCPNodeRawPtrNonownedUndefined( T* p )
53 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false, null);
59 RCPNode* RCP_createNewRCPNodeRawPtr( T* p,
bool has_ownership_in )
61 return new RCPNodeTmpl<T,DeallocDelete<T> >(p, DeallocDelete<T>(), has_ownership_in);
65 template<
class T,
class Dealloc_T>
67 RCPNode* RCP_createNewDeallocRCPNodeRawPtr(
68 T* p, Dealloc_T dealloc,
bool has_ownership_in
71 return new RCPNodeTmpl<T,Dealloc_T>(p, dealloc, has_ownership_in);
75 template<
class T,
class Dealloc_T>
77 RCPNode* RCP_createNewDeallocRCPNodeRawPtrUndefined(
78 T* p, Dealloc_T dealloc,
bool has_ownership_in
81 return new RCPNodeTmpl<T,Dealloc_T>(p, dealloc, has_ownership_in, null);
88 : ptr_(p), node_(node)
94 T* RCP<T>::access_private_ptr()
const
100 RCPNodeHandle& RCP<T>::nonconst_access_private_node()
106 const RCPNodeHandle& RCP<T>::access_private_node()
const
126 #ifndef TEUCHOS_DEBUG
127 , node_(RCP_createNewRCPNodeRawPtrNonowned(p))
133 if (existing_RCPNode) {
140 RCP_createNewRCPNodeRawPtrNonowned(p),
146 #endif // TEUCHOS_DEBUG
154 node_(RCP_createNewRCPNodeRawPtrNonownedUndefined(p))
162 #ifndef TEUCHOS_DEBUG
163 , node_(RCP_createNewRCPNodeRawPtr(p, has_ownership_in))
169 if (!has_ownership_in) {
172 if (existing_RCPNode) {
187 #endif // TEUCHOS_DEBUG
192 template<
class Dealloc_T>
196 #ifndef TEUCHOS_DEBUG
197 , node_(RCP_createNewDeallocRCPNodeRawPtr(p, dealloc, has_ownership_in))
205 RCPNodeThrowDeleter nodeDeleter(RCP_createNewDeallocRCPNodeRawPtr(p, dealloc, has_ownership_in));
213 #endif // TEUCHOS_DEBUG
218 template<
class Dealloc_T>
220 RCP<T>::RCP( T* p, Dealloc_T dealloc, ERCPUndefinedWithDealloc,
bool has_ownership_in )
222 #ifndef TEUCHOS_DEBUG
223 , node_(RCP_createNewDeallocRCPNodeRawPtrUndefined(p, dealloc, has_ownership_in))
233 p, dealloc, has_ownership_in));
241 #endif // TEUCHOS_DEBUG
248 : ptr_(r_ptr.ptr_), node_(r_ptr.node_)
255 : ptr_(r_ptr.ptr_), node_(std::move(r_ptr.node_))
266 node_(r_ptr.access_private_node())
275 node_(r_ptr.access_private_node())
294 RCP<T>(r_ptr).swap(*
this);
309 node_ = std::move(r_ptr.node_);
328 std::swap(r_ptr.ptr_, ptr_);
329 node_.swap(r_ptr.node_);
348 debug_assert_not_null();
349 debug_assert_valid_ptr();
358 debug_assert_not_null();
359 debug_assert_valid_ptr();
367 debug_assert_valid_ptr();
385 return Ptr<T>(this->create_weak());
387 return Ptr<T>(getRawPtr());
404 return rcp_implicit_cast<
const T>(*this);
423 return node_.strength();
432 return node_.is_valid_ptr();
441 return node_.strong_count();
449 return node_.weak_count();
457 return node_.total_count();
465 node_.has_ownership(
true);
473 return node_.has_ownership();
481 debug_assert_valid_ptr();
482 node_.has_ownership(
false);
491 debug_assert_valid_ptr();
492 return RCP<T>(ptr_, node_.create_weak());
500 debug_assert_valid_ptr();
501 return RCP<T>(ptr_, node_.create_strong());
504 #if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_THREAD_SAFE)
509 if (strength() == RCP_STRONG) {
510 return create_strong();
515 RCPNodeHandle attemptStrong = node_.create_strong_lock();
516 return RCP<T>( attemptStrong.is_node_null() ? 0 : ptr_, attemptStrong);
526 return node_.same_node(r_ptr.access_private_node());
541 throw_null_ptr_error(
typeName(*
this));
551 node_.assert_valid_ptr(*
this);
577 *
this =
rcp(p, has_ownership_in);
592 return RCP<T>(p, owns_mem);
596 template<
class T,
class Dealloc_T>
599 Teuchos::rcpWithDealloc( T* p, Dealloc_T dealloc,
bool owns_mem )
601 return RCP<T>(p, dealloc, owns_mem);
605 template<
class T,
class Dealloc_T>
608 Teuchos::rcpWithDeallocUndef( T* p, Dealloc_T dealloc,
bool owns_mem )
610 return RCP<T>(p, dealloc, RCP_UNDEFINED_WITH_DEALLOC, owns_mem);
616 Teuchos::rcpFromRef( T& r )
618 return RCP<T>(&r, RCP_WEAK_NO_DEALLOC);
624 Teuchos::rcpFromUndefRef( T& r )
626 return RCP<T>(&r, RCP_UNDEFINED_WEAK_NO_DEALLOC);
630 template<
class T,
class Embedded>
632 Teuchos::rcpWithEmbeddedObjPreDestroy(
633 T* p,
const Embedded &embedded,
bool owns_mem
637 p, embeddedObjDeallocDelete<T>(embedded,PRE_DESTROY), owns_mem
642 template<
class T,
class Embedded>
644 Teuchos::rcpWithEmbeddedObjPostDestroy(
645 T* p,
const Embedded &embedded,
bool owns_mem
648 return rcpWithDealloc( p, embeddedObjDeallocDelete<T>(embedded,POST_DESTROY), owns_mem );
652 template<
class T,
class Embedded>
654 Teuchos::rcpWithEmbeddedObj( T* p,
const Embedded &embedded,
bool owns_mem )
656 return rcpWithEmbeddedObjPostDestroy<T,Embedded>(p,embedded,owns_mem);
660 template<
class T,
class ParentT>
662 Teuchos::rcpWithInvertedObjOwnership(
const RCP<T> &child,
663 const RCP<ParentT> &parent)
665 using std::make_pair;
672 Teuchos::rcpCloneNode(
const RCP<T> &p)
699 bool Teuchos::operator==(
const RCP<T> &p,
ENull )
701 return p.get() == NULL;
707 bool Teuchos::operator!=(
const RCP<T> &p,
ENull )
709 return p.get() != NULL;
713 template<
class T1,
class T2>
715 bool Teuchos::operator==(
const RCP<T1> &p1,
const RCP<T2> &p2 )
717 return p1.access_private_node().same_node(p2.access_private_node());
721 template<
class T1,
class T2>
723 bool Teuchos::operator!=(
const RCP<T1> &p1,
const RCP<T2> &p2 )
725 return !p1.access_private_node().same_node(p2.access_private_node());
729 template<
class T2,
class T1>
732 Teuchos::rcp_implicit_cast(
const RCP<T1>& p1)
735 T2 *check = p1.
get();
736 return RCP<T2>(check, p1.access_private_node());
740 template<
class T2,
class T1>
743 Teuchos::rcp_static_cast(
const RCP<T1>& p1)
746 T2 *check =
static_cast<T2*
>(p1.get());
747 return RCP<T2>(check, p1.access_private_node());
751 template<
class T2,
class T1>
754 Teuchos::rcp_const_cast(
const RCP<T1>& p1)
757 T2 *check =
const_cast<T2*
>(p1.get());
758 return RCP<T2>(check, p1.access_private_node());
762 template<
class T2,
class T1>
765 Teuchos::rcp_dynamic_cast(
const RCP<T1>& p1,
bool throw_on_fail)
774 p =
dynamic_cast<T2*
>(p1.get());
777 return RCP<T2>(p, p1.access_private_node());
784 template<
class T1,
class T2>
786 void Teuchos::set_extra_data(
const T1 &extra_data,
const std::string& name,
789 p->assert_not_null();
790 p->nonconst_access_private_node().set_extra_data(
791 any(extra_data), name, destroy_when,
796 template<
class T1,
class T2>
798 const T1& Teuchos::get_extra_data(
const RCP<T2>& p,
const std::string& name )
802 p.access_private_node().get_extra_data(
803 TypeNameTraits<T1>::name(), name
809 template<
class T1,
class T2>
811 T1& Teuchos::get_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
815 p.nonconst_access_private_node().get_extra_data(
816 TypeNameTraits<T1>::name(), name
822 template<
class T1,
class T2>
825 Teuchos::get_optional_extra_data(
const RCP<T2>& p,
const std::string& name )
828 const any *extra_data = p.access_private_node().get_optional_extra_data(
829 TypeNameTraits<T1>::name(), name);
831 return Ptr<const T1>(&
any_cast<T1>(*extra_data));
836 template<
class T1,
class T2>
839 Teuchos::get_optional_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
842 any *extra_data = p.nonconst_access_private_node().get_optional_extra_data(
843 TypeNameTraits<T1>::name(), name);
845 return Ptr<T1>(&
any_cast<T1>(*extra_data));
850 template<
class Dealloc_T,
class T>
852 const Dealloc_T& Teuchos::get_dealloc(
const RCP<T>& p )
854 return get_nonconst_dealloc<Dealloc_T>(
const_cast<RCP<T>&
>(p));
858 template<
class Dealloc_T,
class T>
860 Dealloc_T& Teuchos::get_nonconst_dealloc(
const RCP<T>& p )
862 typedef RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T> requested_type;
864 RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T>
865 *dnode =
dynamic_cast<RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T>*
>(
866 p.access_private_node().node_ptr());
868 dnode==NULL, NullReferenceError
869 ,
"get_dealloc<" << TypeNameTraits<Dealloc_T>::name()
870 <<
"," << TypeNameTraits<T>::name() <<
">(p): "
871 <<
"Error, requested type \'" << TypeNameTraits<requested_type>::name()
872 <<
"\' does not match actual type of the node \'"
873 <<
typeName(*p.access_private_node().node_ptr()) <<
"!"
875 return dnode->get_nonconst_dealloc();
879 template<
class Dealloc_T,
class T>
882 Teuchos::get_optional_nonconst_dealloc(
const RCP<T>& p )
885 typedef RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T> RCPNT;
886 RCPNT *dnode =
dynamic_cast<RCPNT*
>(p.access_private_node().node_ptr());
888 return ptr(&dnode->get_nonconst_dealloc());
893 template<
class Dealloc_T,
class T>
896 Teuchos::get_optional_dealloc(
const RCP<T>& p )
898 return get_optional_nonconst_dealloc<Dealloc_T>(
const_cast<RCP<T>&
>(p));
902 template<
class TOrig,
class Embedded,
class T>
903 const Embedded& Teuchos::getEmbeddedObj(
const RCP<T>& p )
905 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
906 return get_dealloc<Dealloc_t>(p).getObj();
910 template<
class TOrig,
class Embedded,
class T>
911 Embedded& Teuchos::getNonconstEmbeddedObj(
const RCP<T>& p )
913 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
914 return get_nonconst_dealloc<Dealloc_t>(p).getNonconstObj();
918 template<
class TOrig,
class Embedded,
class T>
920 Teuchos::getOptionalEmbeddedObj(
const RCP<T>& p )
922 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
923 const Ptr<const Dealloc_t> dealloc = get_optional_dealloc<Dealloc_t>(p);
925 return ptr(&dealloc->getObj());
931 template<
class TOrig,
class Embedded,
class T>
933 Teuchos::getOptionalNonconstEmbeddedObj(
const RCP<T>& p )
935 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
936 const Ptr<Dealloc_t> dealloc = get_optional_nonconst_dealloc<Dealloc_t>(p);
938 return ptr(&dealloc->getNonconstObj());
944 template<
class ParentT,
class T>
948 typedef std::pair<RCP<T>, RCP<ParentT> > Pair_t;
949 Pair_t pair = getEmbeddedObj<T, Pair_t>(invertedChild);
955 std::ostream& Teuchos::operator<<( std::ostream& out, const RCP<T>& p )
959 <<
"ptr="<<(
const void*)(p.get())
960 <<
",node="<<p.access_private_node()
961 <<
",strong_count="<<p.strong_count()
962 <<
",weak_count="<<p.weak_count()
968 #endif // TEUCHOS_RCP_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<const T> version of *this.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
RCP< T > create_weak() const
Create a new weak RCP object from another (strong) RCP object.
bool has_ownership() const
Returns true if this has ownership of object pointed to by this->get() in order to delete it...
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
ValueType & any_cast(any &operand)
Used to extract the templated value held in Teuchos::any to a given value type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
T_To & dyn_cast(T_From &from)
Dynamic casting utility function meant to replace dynamic_cast<T&> by throwing a better documented er...
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.
void release()
Releaes the RCPNode pointer before the destructor is called.
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
ERCPStrength strength() const
Strength of the pointer.
std::string concreteTypeName(const T &t)
Template function for returning the type name of the actual concrete name of a passed-in object...
bool is_null(const ArrayRCP< T > &p)
Returns true if p.get()==NULL.
T * operator->() const
Pointer (->) access to members of underlying object.
RCP< T > rcpWithEmbeddedObj(T *p, const Embedded &embedded, bool owns_mem=true)
Create an RCP with and also put in an embedded object.
Node class to keep track of address and the reference count for a reference-counted utility class and...
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
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.
void set_has_ownership()
Give this and other RCP<> objects ownership of the referenced object this->get(). ...
void reset()
Reset to null.
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 & operator*() const
Dereference the underlying object.
ERCPStrength
Used to specify if the pointer is weak or strong.
RCP< T > & operator=(const RCP< T > &r_ptr)
Copy the pointer to the referenced object and increment the reference count.
const Ptr< T > & assert_not_null() const
Throws std::logic_error if this->get()==NULL, otherwise returns reference to *this.
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this.
static RCPNode * getExistingRCPNode(T *p)
Return a raw pointer to an existing owning RCPNode given the address to the underlying object if it e...
Ptr< T > release()
Release the ownership of the underlying dynamically allocated object.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
Provides std::map class for deficient platforms.
bool is_valid_ptr() const
Return if the underlying object pointer is still valid or not.
Handle class that manages the RCPNode's reference counting.
RCP< ParentT > getInvertedObjOwnershipParent(const RCP< T > &invertedChild)
Get the parent back from an inverted ownership RCP.
EPrePostDestruction
Used to specify a pre or post destruction of extra data.
Reference-counted pointer class and non-member templated function implementations.
Smart reference counting pointer class for automatic garbage collection.
Deletes a (non-owning) RCPNode but not it's underlying object in case of a throw. ...
RCP< T > rcpWithDealloc(T *p, Dealloc_T dealloc, bool owns_mem=true)
Initialize from a raw pointer with a deallocation policy.
Defines basic traits returning the name of a type in a portable and readable way. ...
int strong_count() const
Return the number of active RCP<> objects that have a "strong" reference to the underlying reference-...
void swap(RCPNodeHandle &node_ref)
Swap the contents of node_ref with *this.
int weak_count() const
Return the number of active RCP<> objects that have a "weak" reference to the underlying reference-co...
int total_count() const
Total count (strong_count() + weak_count()).
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
bool is_null() const
Returns true if the underlying pointer is null.