42#ifndef TEUCHOS_RCP_HPP
43#define TEUCHOS_RCP_HPP
59#include "Teuchos_Ptr.hpp"
60#include "Teuchos_Assert.hpp"
61#include "Teuchos_Exceptions.hpp"
62#include "Teuchos_dyn_cast.hpp"
75RCPNode* RCP_createNewRCPNodeRawPtrNonowned( T* p )
77 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false);
83RCPNode* RCP_createNewRCPNodeRawPtrNonownedUndefined( T* p )
85 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false, null);
91RCPNode* RCP_createNewRCPNodeRawPtr( T* p,
bool has_ownership_in )
93 return new RCPNodeTmpl<T,DeallocDelete<T> >(p, DeallocDelete<T>(), has_ownership_in);
97template<
class T,
class Dealloc_T>
99RCPNode* RCP_createNewDeallocRCPNodeRawPtr(
100 T* p, Dealloc_T dealloc,
bool has_ownership_in
103 return new RCPNodeTmpl<T,Dealloc_T>(p, dealloc, has_ownership_in);
107template<
class T,
class Dealloc_T>
109RCPNode* RCP_createNewDeallocRCPNodeRawPtrUndefined(
110 T* p, Dealloc_T dealloc,
bool has_ownership_in
113 return new RCPNodeTmpl<T,Dealloc_T>(p, dealloc, has_ownership_in, null);
120 : ptr_(p), node_(node)
126T* RCP<T>::access_private_ptr()
const
132RCPNodeHandle& RCP<T>::nonconst_access_private_node()
138const RCPNodeHandle& RCP<T>::access_private_node()
const
159 , node_(RCP_createNewRCPNodeRawPtrNonowned(
p))
172 RCP_createNewRCPNodeRawPtrNonowned(
p),
186 node_(RCP_createNewRCPNodeRawPtrNonownedUndefined(
p))
224template<
class Dealloc_T>
250template<
class Dealloc_T>
332 node_ = std::move(
r_ptr.node_);
351 std::swap(
r_ptr.ptr_, ptr_);
527#if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_THREAD_SAFE)
564 throw_null_ptr_error(
typeName(*
this));
619template<
class T,
class Dealloc_T>
628template<
class T,
class Dealloc_T>
639Teuchos::rcpFromRef(
T&
r )
641 return RCP<T>(&
r, RCP_WEAK_NO_DEALLOC);
647Teuchos::rcpFromUndefRef(
T&
r )
649 return RCP<T>(&
r, RCP_UNDEFINED_WEAK_NO_DEALLOC);
653template<
class T,
class Embedded>
655Teuchos::rcpWithEmbeddedObjPreDestroy(
659 return rcpWithDealloc(
665template<
class T,
class Embedded>
667Teuchos::rcpWithEmbeddedObjPostDestroy(
675template<
class T,
class Embedded>
683template<
class T,
class ParentT>
685Teuchos::rcpWithInvertedObjOwnership(
const RCP<T> &
child,
688 using std::make_pair;
695Teuchos::rcpCloneNode(
const RCP<T> &
p)
700 return rcpWithEmbeddedObj(&*
p,
p,
false);
730bool Teuchos::operator!=(
const RCP<T> &
p, ENull )
736template<
class T1,
class T2>
740 return p1.access_private_node().same_node(
p2.access_private_node());
744template<
class T1,
class T2>
746bool Teuchos::operator!=(
const RCP<T1> &p1,
const RCP<T2> &p2 )
748 return !p1.access_private_node().same_node(p2.access_private_node());
752template<
class T2,
class T1>
755Teuchos::rcp_implicit_cast(
const RCP<T1>& p1)
758 T2 *check = p1.
get();
759 return RCP<T2>(check, p1.access_private_node());
763template<
class T2,
class T1>
774template<
class T2,
class T1>
777Teuchos::rcp_const_cast(
const RCP<T1>&
p1)
785template<
class T2,
class T1>
807template<
class T1,
class T2>
809void Teuchos::set_extra_data(
const T1 &extra_data,
const std::string& name,
810 const Ptr<RCP<T2> > &p, EPrePostDestruction destroy_when,
bool force_unique )
814 any(extra_data), name, destroy_when,
819template<
class T1,
class T2>
821const T1& Teuchos::get_extra_data(
const RCP<T2>&
p,
const std::string& name )
832template<
class T1,
class T2>
834T1& Teuchos::get_nonconst_extra_data(
RCP<T2>&
p,
const std::string& name )
845template<
class T1,
class T2>
848Teuchos::get_optional_extra_data(
const RCP<T2>&
p,
const std::string& name )
859template<
class T1,
class T2>
862Teuchos::get_optional_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
873template<
class Dealloc_T,
class T>
881template<
class Dealloc_T,
class T>
889 p.access_private_node().node_ptr());
895 <<
"\' does not match actual type of the node \'"
896 <<
typeName(*
p.access_private_node().node_ptr()) <<
"!"
902template<
class Dealloc_T,
class T>
905Teuchos::get_optional_nonconst_dealloc(
const RCP<T>&
p )
916template<
class Dealloc_T,
class T>
919Teuchos::get_optional_dealloc(
const RCP<T>&
p )
925template<
class TOrig,
class Embedded,
class T>
933template<
class TOrig,
class Embedded,
class T>
941template<
class TOrig,
class Embedded,
class T>
943Teuchos::getOptionalEmbeddedObj(
const RCP<T>& p )
945 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
946 const Ptr<const Dealloc_t> dealloc = get_optional_dealloc<Dealloc_t>(p);
947 if (!is_null(dealloc)) {
948 return ptr(&dealloc->getObj());
954template<
class TOrig,
class Embedded,
class T>
956Teuchos::getOptionalNonconstEmbeddedObj(
const RCP<T>& p )
958 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
959 const Ptr<Dealloc_t> dealloc = get_optional_nonconst_dealloc<Dealloc_t>(p);
960 if (!is_null(dealloc)) {
961 return ptr(&dealloc->getNonconstObj());
967template<
class ParentT,
class T>
971 typedef std::pair<RCP<T>, RCP<ParentT> > Pair_t;
972 Pair_t pair = getEmbeddedObj<T, Pair_t>(invertedChild);
982 <<
"ptr="<<(
const void*)(p.get())
983 <<
",node="<<p.access_private_node()
984 <<
",strong_count="<<p.strong_count()
985 <<
",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.
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.
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.