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"
43RCPNode* RCP_createNewRCPNodeRawPtrNonowned( T* p )
45 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false);
51RCPNode* RCP_createNewRCPNodeRawPtrNonownedUndefined( T* p )
53 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false, null);
59RCPNode* RCP_createNewRCPNodeRawPtr( T* p,
bool has_ownership_in )
61 return new RCPNodeTmpl<T,DeallocDelete<T> >(p, DeallocDelete<T>(), has_ownership_in);
65template<
class T,
class Dealloc_T>
67RCPNode* RCP_createNewDeallocRCPNodeRawPtr(
68 T* p, Dealloc_T dealloc,
bool has_ownership_in
71 return new RCPNodeTmpl<T,Dealloc_T>(p, dealloc, has_ownership_in);
75template<
class T,
class Dealloc_T>
77RCPNode* 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)
94T* RCP<T>::access_private_ptr()
const
100RCPNodeHandle& RCP<T>::nonconst_access_private_node()
106const RCPNodeHandle& RCP<T>::access_private_node()
const
127 , node_(RCP_createNewRCPNodeRawPtrNonowned(
p))
140 RCP_createNewRCPNodeRawPtrNonowned(
p),
154 node_(RCP_createNewRCPNodeRawPtrNonownedUndefined(
p))
192template<
class Dealloc_T>
218template<
class Dealloc_T>
309 node_ = std::move(
r_ptr.node_);
328 std::swap(
r_ptr.ptr_, ptr_);
504#if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_THREAD_SAFE)
541 throw_null_ptr_error(
typeName(*
this));
596template<
class T,
class Dealloc_T>
605template<
class T,
class Dealloc_T>
616Teuchos::rcpFromRef(
T&
r )
618 return RCP<T>(&
r, RCP_WEAK_NO_DEALLOC);
624Teuchos::rcpFromUndefRef(
T&
r )
626 return RCP<T>(&
r, RCP_UNDEFINED_WEAK_NO_DEALLOC);
630template<
class T,
class Embedded>
632Teuchos::rcpWithEmbeddedObjPreDestroy(
636 return rcpWithDealloc(
642template<
class T,
class Embedded>
644Teuchos::rcpWithEmbeddedObjPostDestroy(
652template<
class T,
class Embedded>
660template<
class T,
class ParentT>
662Teuchos::rcpWithInvertedObjOwnership(
const RCP<T> &
child,
665 using std::make_pair;
672Teuchos::rcpCloneNode(
const RCP<T> &
p)
677 return rcpWithEmbeddedObj(&*
p,
p,
false);
699bool Teuchos::operator==(
const RCP<T> &p, ENull )
707bool Teuchos::operator!=(
const RCP<T> &
p, ENull )
713template<
class T1,
class T2>
717 return p1.access_private_node().same_node(
p2.access_private_node());
721template<
class T1,
class T2>
725 return !
p1.access_private_node().same_node(
p2.access_private_node());
729template<
class T2,
class T1>
732Teuchos::rcp_implicit_cast(
const RCP<T1>& p1)
735 T2 *check = p1.
get();
736 return RCP<T2>(check, p1.access_private_node());
740template<
class T2,
class T1>
743Teuchos::rcp_static_cast(
const RCP<T1>& p1)
751template<
class T2,
class T1>
754Teuchos::rcp_const_cast(
const RCP<T1>&
p1)
762template<
class T2,
class T1>
784template<
class T1,
class T2>
786void Teuchos::set_extra_data(
const T1 &extra_data,
const std::string& name,
791 any(extra_data), name, destroy_when,
796template<
class T1,
class T2>
798const T1& Teuchos::get_extra_data(
const RCP<T2>&
p,
const std::string& name )
809template<
class T1,
class T2>
811T1& Teuchos::get_nonconst_extra_data(
RCP<T2>&
p,
const std::string& name )
822template<
class T1,
class T2>
825Teuchos::get_optional_extra_data(
const RCP<T2>&
p,
const std::string& name )
836template<
class T1,
class T2>
839Teuchos::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);
850template<
class Dealloc_T,
class T>
858template<
class Dealloc_T,
class T>
866 p.access_private_node().node_ptr());
872 <<
"\' does not match actual type of the node \'"
873 <<
typeName(*
p.access_private_node().node_ptr()) <<
"!"
879template<
class Dealloc_T,
class T>
882Teuchos::get_optional_nonconst_dealloc(
const RCP<T>&
p )
893template<
class Dealloc_T,
class T>
896Teuchos::get_optional_dealloc(
const RCP<T>&
p )
902template<
class TOrig,
class Embedded,
class T>
910template<
class TOrig,
class Embedded,
class T>
918template<
class TOrig,
class Embedded,
class T>
920Teuchos::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);
924 if (!is_null(dealloc)) {
925 return ptr(&dealloc->getObj());
931template<
class TOrig,
class Embedded,
class T>
933Teuchos::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);
937 if (!is_null(dealloc)) {
938 return ptr(&dealloc->getNonconstObj());
944template<
class ParentT,
class T>
948 typedef std::pair<RCP<T>, RCP<ParentT> > Pair_t;
949 Pair_t pair = getEmbeddedObj<T, Pair_t>(invertedChild);
959 <<
"ptr="<<(
const void*)(p.get())
960 <<
",node="<<p.access_private_node()
961 <<
",strong_count="<<p.strong_count()
962 <<
",weak_count="<<p.weak_count()
Reference-counted pointer class and non-member templated function implementations.
Defines basic traits returning the name of a type in a portable and readable way.
Provides std::map class for deficient platforms.
Null reference error exception class.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Handle class that manages the RCPNode's reference counting.
RCPNodeHandle create_strong() const
Return a strong handle.
RCPNodeHandle create_weak() const
Return a weak handle.
int total_count() const
The sum of the weak and string counts.
RCPNodeHandle create_strong_lock() const
Return a strong handle if possible using thread safe atomics.
void assert_valid_ptr(const RCPType &rcp_obj) const
ERCPStrength strength() const
The strength of this handle.
void swap(RCPNodeHandle &node_ref)
Swap the contents of node_ref with *this.
int weak_count() const
The weak count for this RCPNode, or 0 if the node is NULL.
void has_ownership(bool has_ownership_in)
bool is_valid_ptr() const
Whether the underlying pointer is valid.
int strong_count() const
The strong count for this RCPNode, or 0 if the node is NULL.
bool same_node(const RCPNodeHandle &node2) const
Whether the RCPNode for which node2 is a handle is the same RCPNode as this object's RCPNode.
Deletes a (non-owning) RCPNode but not it's underlying object in case of a throw.
static RCPNode * getExistingRCPNode(T *p)
Return a raw pointer to an existing owning RCPNode given the address to the underlying object if it e...
Node class to keep track of address and the reference count for a reference-counted utility class and...
Smart reference counting pointer class for automatic garbage collection.
RCP< const T > getConst() const
Return an RCP<const T> version of *this.
RCP< T > create_weak() const
Create a new weak RCP object from another (strong) RCP object.
Ptr< T > release()
Release the ownership of the underlying dynamically allocated object.
void reset()
Reset to null.
void set_has_ownership()
Give this and other RCP<> objects ownership of the referenced object this->get().
~RCP()
Removes a reference to a dynamically allocated object and possibly deletes the object if owned.
const RCP< T > & assert_valid_ptr() const
If the object pointer is non-null, assert that it is still valid.
bool shares_resource(const RCP< T2 > &r_ptr) const
Returns true if the smart pointers share the same underlying reference-counted object.
Dealloc_T & get_nonconst_dealloc(const RCP< T > &p)
Return a non-const reference to the underlying deallocator object.
void swap(RCP< T > &r_ptr)
Swap the contents with some other RCP object.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object.
bool is_null() const
Returns true if the underlying pointer is null.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
const RCP< T > & assert_not_null() const
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this.
const RCP< T > & debug_assert_valid_ptr() const
Calls assert_valid_ptr() in a debug build.
Ptr< const T1 > get_optional_extra_data(const RCP< T2 > &p, const std::string &name)
Get a pointer to const extra data (if it exists) associated with a RCP object.
T * operator->() const
Pointer (->) access to members of underlying object.
bool is_valid_ptr() const
Return if the underlying object pointer is still valid or not.
const T1 & get_extra_data(const RCP< T2 > &p, const std::string &name)
Get a const reference to extra data associated with a RCP object.
RCP< T > create_strong() const
Create a new strong RCP object from another (weak) RCP object.
bool has_ownership() const
Returns true if this has ownership of object pointed to by this->get() in order to delete it.
T * get() const
Get the raw C++ pointer to the underlying object.
ERCPStrength strength() const
Strength of the pointer.
void set_extra_data(const T1 &extra_data, const std::string &name, const Ptr< RCP< T2 > > &p, EPrePostDestruction destroy_when=POST_DESTROY, bool force_unique=true)
Set extra data associated with a RCP object.
int weak_count() const
Return the number of active RCP<> objects that have a "weak" reference to the underlying reference-co...
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
const RCP< T > & debug_assert_not_null() const
Calls assert_not_null() in a debug build.
RCP< T > & operator=(const RCP< T > &r_ptr)
Copy the pointer to the referenced object and increment the reference count.
int total_count() const
Total count (strong_count() + weak_count()).
int strong_count() const
Return the number of active RCP<> objects that have a "strong" reference to the underlying reference-...
Ptr< T > operator()() const
Shorthand for ptr().
T & operator*() const
Dereference the underlying object.
Default traits class that just returns typeid(T).name().
Modified boost::any class, which is a container for a templated value.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
std::string concreteTypeName(const T &t)
Template function for returning the type name of the actual concrete name of a passed-in object.
ERCPStrength
Used to specify if the pointer is weak or strong.
EPrePostDestruction
Used to specify a pre or post destruction of extra data.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
RCP< ParentT > getInvertedObjOwnershipParent(const RCP< T > &invertedChild)
Get the parent back from an inverted ownership RCP.