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>
341 node_ = std::move(
r_ptr.node_);
360 std::swap(
r_ptr.ptr_, ptr_);
536#if defined(HAVE_TEUCHOSCORE_CXX11) && defined(HAVE_TEUCHOS_THREAD_SAFE)
573 throw_null_ptr_error(
typeName(*
this));
628template<
class T,
class Dealloc_T>
637template<
class T,
class Dealloc_T>
648Teuchos::rcpFromRef(
T&
r )
650 return RCP<T>(&
r, RCP_WEAK_NO_DEALLOC);
656Teuchos::rcpFromUndefRef(
T&
r )
658 return RCP<T>(&
r, RCP_UNDEFINED_WEAK_NO_DEALLOC);
662template<
class T,
class Embedded>
664Teuchos::rcpWithEmbeddedObjPreDestroy(
668 return rcpWithDealloc(
674template<
class T,
class Embedded>
676Teuchos::rcpWithEmbeddedObjPostDestroy(
684template<
class T,
class Embedded>
692template<
class T,
class ParentT>
694Teuchos::rcpWithInvertedObjOwnership(
const RCP<T> &
child,
697 using std::make_pair;
704Teuchos::rcpCloneNode(
const RCP<T> &
p)
709 return rcpWithEmbeddedObj(&*
p,
p,
false);
731bool Teuchos::operator==(
const RCP<T> &p, ENull )
739bool Teuchos::operator!=(
const RCP<T> &
p, ENull )
745template<
class T1,
class T2>
749 return p1.access_private_node().same_node(
p2.access_private_node());
753template<
class T1,
class T2>
757 return !
p1.access_private_node().same_node(
p2.access_private_node());
761template<
class T2,
class T1>
764Teuchos::rcp_implicit_cast(
const RCP<T1>& p1)
767 T2 *check = p1.
get();
768 return RCP<T2>(check, p1.access_private_node());
772template<
class T2,
class T1>
775Teuchos::rcp_static_cast(
const RCP<T1>& p1)
783template<
class T2,
class T1>
786Teuchos::rcp_const_cast(
const RCP<T1>&
p1)
794template<
class T2,
class T1>
816template<
class T1,
class T2>
818void Teuchos::set_extra_data(
const T1 &extra_data,
const std::string& name,
823 any(extra_data), name, destroy_when,
828template<
class T1,
class T2>
830const T1& Teuchos::get_extra_data(
const RCP<T2>&
p,
const std::string& name )
841template<
class T1,
class T2>
843T1& Teuchos::get_nonconst_extra_data(
RCP<T2>&
p,
const std::string& name )
854template<
class T1,
class T2>
857Teuchos::get_optional_extra_data(
const RCP<T2>&
p,
const std::string& name )
868template<
class T1,
class T2>
871Teuchos::get_optional_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
874 any *extra_data = p.nonconst_access_private_node().get_optional_extra_data(
875 TypeNameTraits<T1>::name(), name);
882template<
class Dealloc_T,
class T>
890template<
class Dealloc_T,
class T>
898 p.access_private_node().node_ptr());
904 <<
"\' does not match actual type of the node \'"
905 <<
typeName(*
p.access_private_node().node_ptr()) <<
"!"
911template<
class Dealloc_T,
class T>
914Teuchos::get_optional_nonconst_dealloc(
const RCP<T>&
p )
925template<
class Dealloc_T,
class T>
928Teuchos::get_optional_dealloc(
const RCP<T>&
p )
934template<
class TOrig,
class Embedded,
class T>
942template<
class TOrig,
class Embedded,
class T>
950template<
class TOrig,
class Embedded,
class T>
952Teuchos::getOptionalEmbeddedObj(
const RCP<T>& p )
954 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
955 const Ptr<const Dealloc_t> dealloc = get_optional_dealloc<Dealloc_t>(p);
956 if (!is_null(dealloc)) {
957 return ptr(&dealloc->getObj());
963template<
class TOrig,
class Embedded,
class T>
965Teuchos::getOptionalNonconstEmbeddedObj(
const RCP<T>& p )
967 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
968 const Ptr<Dealloc_t> dealloc = get_optional_nonconst_dealloc<Dealloc_t>(p);
970 return ptr(&dealloc->getNonconstObj());
976template<
class ParentT,
class T>
980 typedef std::pair<RCP<T>, RCP<ParentT> > Pair_t;
981 Pair_t pair = getEmbeddedObj<T, Pair_t>(invertedChild);
991 <<
"ptr="<<(
const void*)(p.get())
992 <<
",node="<<p.access_private_node()
993 <<
",strong_count="<<p.strong_count()
994 <<
",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 boost::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.
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.