42 #ifndef TEUCHOS_RCP_HPP
43 #define TEUCHOS_RCP_HPP
97 template<
class T,
class Dealloc_T>
100 T* p, Dealloc_T dealloc,
bool has_ownership_in
107 template<
class T,
class Dealloc_T>
110 T* p, Dealloc_T dealloc,
bool has_ownership_in
120 : ptr_(p), node_(node)
132 RCPNodeHandle& RCP<T>::nonconst_access_private_node()
138 const RCPNodeHandle& RCP<T>::access_private_node()
const
158 #ifndef TEUCHOS_DEBUG
165 if (existing_RCPNode) {
178 #endif // TEUCHOS_DEBUG
194 #ifndef TEUCHOS_DEBUG
201 if (!has_ownership_in) {
204 if (existing_RCPNode) {
219 #endif // TEUCHOS_DEBUG
224 template<
class Dealloc_T>
228 #ifndef TEUCHOS_DEBUG
245 #endif // TEUCHOS_DEBUG
250 template<
class Dealloc_T>
254 #ifndef TEUCHOS_DEBUG
265 p, dealloc, has_ownership_in));
273 #endif // TEUCHOS_DEBUG
280 : ptr_(r_ptr.ptr_), node_(r_ptr.node_)
289 node_(r_ptr.access_private_node())
308 RCP<T>(r_ptr).swap(*
this);
326 std::swap(r_ptr.
ptr_, ptr_);
327 node_.swap(r_ptr.
node_);
346 debug_assert_not_null();
347 debug_assert_valid_ptr();
356 debug_assert_not_null();
357 debug_assert_valid_ptr();
365 debug_assert_valid_ptr();
383 return Ptr<T>(this->create_weak());
402 return rcp_implicit_cast<
const T>(*this);
413 return node_.strength();
422 return node_.is_valid_ptr();
431 return node_.strong_count();
439 return node_.weak_count();
447 return node_.total_count();
455 node_.has_ownership(
true);
463 return node_.has_ownership();
471 debug_assert_valid_ptr();
472 node_.has_ownership(
false);
481 debug_assert_valid_ptr();
482 return RCP<T>(ptr_, node_.create_weak());
490 debug_assert_valid_ptr();
491 return RCP<T>(ptr_, node_.create_strong());
494 #if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_THREAD_SAFE)
500 return create_strong();
505 RCPNodeHandle attemptStrong = node_.create_strong_lock();
506 return RCP<T>( attemptStrong.is_node_null() ? 0 : ptr_, attemptStrong);
516 return node_.same_node(r_ptr.access_private_node());
531 throw_null_ptr_error(
typeName(*
this));
541 node_.assert_valid_ptr(*
this);
567 *
this =
rcp(p, has_ownership_in);
575 return node_.count();
590 return RCP<T>(p, owns_mem);
594 template<
class T,
class Dealloc_T>
597 Teuchos::rcpWithDealloc( T* p, Dealloc_T dealloc,
bool owns_mem )
599 return RCP<T>(p, dealloc, owns_mem);
603 template<
class T,
class Dealloc_T>
606 Teuchos::rcpWithDeallocUndef( T* p, Dealloc_T dealloc,
bool owns_mem )
614 Teuchos::rcpFromRef( T& r )
622 Teuchos::rcpFromUndefRef( T& r )
628 template<
class T,
class Embedded>
630 Teuchos::rcpWithEmbeddedObjPreDestroy(
631 T* p,
const Embedded &embedded,
bool owns_mem
635 p, embeddedObjDeallocDelete<T>(embedded,
PRE_DESTROY), owns_mem
640 template<
class T,
class Embedded>
642 Teuchos::rcpWithEmbeddedObjPostDestroy(
643 T* p,
const Embedded &embedded,
bool owns_mem
650 template<
class T,
class Embedded>
652 Teuchos::rcpWithEmbeddedObj( T* p,
const Embedded &embedded,
bool owns_mem )
654 return rcpWithEmbeddedObjPostDestroy<T,Embedded>(p,embedded,owns_mem);
658 template<
class T,
class ParentT>
660 Teuchos::rcpWithInvertedObjOwnership(
const RCP<T> &child,
661 const RCP<ParentT> &parent)
663 using std::make_pair;
670 Teuchos::rcpCloneNode(
const RCP<T> &p)
699 return p.get() == NULL;
707 return p.get() != NULL;
711 template<
class T1,
class T2>
715 return p1.access_private_node().same_node(p2.access_private_node());
719 template<
class T1,
class T2>
723 return !p1.access_private_node().same_node(p2.access_private_node());
727 template<
class T2,
class T1>
730 Teuchos::rcp_implicit_cast(
const RCP<T1>& p1)
733 T2 *check = p1.
get();
734 return RCP<T2>(check, p1.access_private_node());
738 template<
class T2,
class T1>
741 Teuchos::rcp_static_cast(
const RCP<T1>& p1)
744 T2 *check =
static_cast<T2*
>(p1.get());
745 return RCP<T2>(check, p1.access_private_node());
749 template<
class T2,
class T1>
752 Teuchos::rcp_const_cast(
const RCP<T1>& p1)
755 T2 *check =
const_cast<T2*
>(p1.get());
756 return RCP<T2>(check, p1.access_private_node());
760 template<
class T2,
class T1>
763 Teuchos::rcp_dynamic_cast(
const RCP<T1>& p1,
bool throw_on_fail)
772 p =
dynamic_cast<T2*
>(p1.get());
775 return RCP<T2>(p, p1.access_private_node());
782 template<
class T1,
class T2>
784 void Teuchos::set_extra_data(
const T1 &extra_data,
const std::string& name,
787 p->assert_not_null();
788 p->nonconst_access_private_node().set_extra_data(
789 any(extra_data), name, destroy_when,
794 template<
class T1,
class T2>
796 const T1& Teuchos::get_extra_data(
const RCP<T2>& p,
const std::string& name )
800 p.access_private_node().get_extra_data(
801 TypeNameTraits<T1>::name(), name
807 template<
class T1,
class T2>
809 T1& Teuchos::get_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
813 p.nonconst_access_private_node().get_extra_data(
814 TypeNameTraits<T1>::name(), name
820 template<
class T1,
class T2>
823 Teuchos::get_optional_extra_data(
const RCP<T2>& p,
const std::string& name )
826 const any *extra_data = p.access_private_node().get_optional_extra_data(
827 TypeNameTraits<T1>::name(), name);
829 return Ptr<const T1>(&
any_cast<T1>(*extra_data));
834 template<
class T1,
class T2>
837 Teuchos::get_optional_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
840 any *extra_data = p.nonconst_access_private_node().get_optional_extra_data(
841 TypeNameTraits<T1>::name(), name);
843 return Ptr<T1>(&
any_cast<T1>(*extra_data));
848 template<
class Dealloc_T,
class T>
850 const Dealloc_T& Teuchos::get_dealloc(
const RCP<T>& p )
852 return get_nonconst_dealloc<Dealloc_T>(
const_cast<RCP<T>&
>(p));
856 template<
class Dealloc_T,
class T>
858 Dealloc_T& Teuchos::get_nonconst_dealloc(
const RCP<T>& p )
860 typedef RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T> requested_type;
862 RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T>
863 *dnode =
dynamic_cast<RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T>*
>(
864 p.access_private_node().node_ptr());
866 dnode==NULL, NullReferenceError
867 ,
"get_dealloc<" << TypeNameTraits<Dealloc_T>::name()
868 <<
"," << TypeNameTraits<T>::name() <<
">(p): "
869 <<
"Error, requested type \'" << TypeNameTraits<requested_type>::name()
870 <<
"\' does not match actual type of the node \'"
871 <<
typeName(*p.access_private_node().node_ptr()) <<
"!"
873 return dnode->get_nonconst_dealloc();
877 template<
class Dealloc_T,
class T>
880 Teuchos::get_optional_nonconst_dealloc(
const RCP<T>& p )
883 typedef RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T> RCPNT;
884 RCPNT *dnode =
dynamic_cast<RCPNT*
>(p.access_private_node().node_ptr());
886 return ptr(&dnode->get_nonconst_dealloc());
891 template<
class Dealloc_T,
class T>
894 Teuchos::get_optional_dealloc(
const RCP<T>& p )
896 return get_optional_nonconst_dealloc<Dealloc_T>(
const_cast<RCP<T>&
>(p));
900 template<
class TOrig,
class Embedded,
class T>
901 const Embedded& Teuchos::getEmbeddedObj(
const RCP<T>& p )
903 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
904 return get_dealloc<Dealloc_t>(p).getObj();
908 template<
class TOrig,
class Embedded,
class T>
909 Embedded& Teuchos::getNonconstEmbeddedObj(
const RCP<T>& p )
911 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
912 return get_nonconst_dealloc<Dealloc_t>(p).getNonconstObj();
916 template<
class TOrig,
class Embedded,
class T>
918 Teuchos::getOptionalEmbeddedObj(
const RCP<T>& p )
920 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
921 const Ptr<const Dealloc_t> dealloc = get_optional_dealloc<Dealloc_t>(p);
923 return ptr(&dealloc->getObj());
929 template<
class TOrig,
class Embedded,
class T>
931 Teuchos::getOptionalNonconstEmbeddedObj(
const RCP<T>& p )
933 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
934 const Ptr<Dealloc_t> dealloc = get_optional_nonconst_dealloc<Dealloc_t>(p);
936 return ptr(&dealloc->getNonconstObj());
942 template<
class ParentT,
class T>
946 typedef std::pair<RCP<T>, RCP<ParentT> > Pair_t;
947 Pair_t pair = getEmbeddedObj<T, Pair_t>(invertedChild);
953 std::ostream& Teuchos::operator<<( std::ostream& out, const RCP<T>& p )
957 <<
"ptr="<<(
const void*)(p.get())
958 <<
",node="<<p.access_private_node()
959 <<
",strong_count="<<p.strong_count()
960 <<
",weak_count="<<p.weak_count()
966 #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.
ERCPStrength
Used to specify if the pointer is weak or strong.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
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.
RawPointerConversionTraits< Container >::Ptr_t getRawPtr(const Container &c)
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.
ERCPUndefinedWeakNoDealloc
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.
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...
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.
TEUCHOS_DEPRECATED int count() const
Returns strong_count() [deprecated].
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.
Templated implementation class of RCPNode that has the responsibility for deleting the reference-coun...
RCPNode * RCP_createNewRCPNodeRawPtrNonowned(T *p)
RCPNode * RCP_createNewDeallocRCPNodeRawPtrUndefined(T *p, Dealloc_T dealloc, bool has_ownership_in)
std::string concreteTypeName(const T &t)
Template function for returning the type name of the actual concrete name of a passed-in object...
void set_has_ownership()
Give this and other RCP<> objects ownership of the referenced object this->get(). ...
void reset()
Reset to null.
T_To & dyn_cast(T_From &from)
Dynamic casting utility function meant to replace dynamic_cast<T&> by throwing a better documented er...
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.
RCPNode * RCP_createNewRCPNodeRawPtr(T *p, bool has_ownership_in)
RCP< T > & operator=(const RCP< T > &r_ptr)
Copy the pointer to the referenced object and increment the reference count.
RCPNode * RCP_createNewRCPNodeRawPtrNonownedUndefined(T *p)
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.
RCPNode * RCP_createNewDeallocRCPNodeRawPtr(T *p, Dealloc_T dealloc, bool has_ownership_in)
EPrePostDestruction
Used to specify a pre or post destruction of extra data.
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.
Reference-counted pointer class and non-member templated function implementations.
Policy class for deallocator for non-owned RCPs.
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. ...
bool operator==(BigUInt< n > const &a, BigUInt< n > const &b)
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...
bool is_null() const
Returns true if the underlying pointer is null.