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" 75 RCPNode* RCP_createNewRCPNodeRawPtrNonowned( T* p )
77 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false);
83 RCPNode* RCP_createNewRCPNodeRawPtrNonownedUndefined( T* p )
85 return new RCPNodeTmpl<T,DeallocNull<T> >(p, DeallocNull<T>(),
false, null);
91 RCPNode* RCP_createNewRCPNodeRawPtr( T* p,
bool has_ownership_in )
93 return new RCPNodeTmpl<T,DeallocDelete<T> >(p, DeallocDelete<T>(), has_ownership_in);
97 template<
class T,
class Dealloc_T>
99 RCPNode* RCP_createNewDeallocRCPNodeRawPtr(
100 T* p, Dealloc_T dealloc,
bool has_ownership_in
103 return new RCPNodeTmpl<T,Dealloc_T>(p, dealloc, has_ownership_in);
107 template<
class T,
class Dealloc_T>
109 RCPNode* 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)
126 T* RCP<T>::access_private_ptr()
const 132 RCPNodeHandle& RCP<T>::nonconst_access_private_node()
138 const RCPNodeHandle& RCP<T>::access_private_node()
const 158 #ifndef TEUCHOS_DEBUG
159 , node_(RCP_createNewRCPNodeRawPtrNonowned(p))
164 RCPNode* existing_RCPNode = RCPNodeTracer::getExistingRCPNode(p);
165 if (existing_RCPNode) {
172 RCP_createNewRCPNodeRawPtrNonowned(p),
178 #endif // TEUCHOS_DEBUG 186 node_(RCP_createNewRCPNodeRawPtrNonownedUndefined(p))
194 #ifndef TEUCHOS_DEBUG
195 , node_(RCP_createNewRCPNodeRawPtr(p, has_ownership_in))
201 if (!has_ownership_in) {
202 existing_RCPNode = RCPNodeTracer::getExistingRCPNode(p);
204 if (existing_RCPNode) {
219 #endif // TEUCHOS_DEBUG 224 template<
class Dealloc_T>
228 #ifndef TEUCHOS_DEBUG
229 , node_(RCP_createNewDeallocRCPNodeRawPtr(p, dealloc, has_ownership_in))
237 RCPNodeThrowDeleter nodeDeleter(RCP_createNewDeallocRCPNodeRawPtr(p, dealloc, has_ownership_in));
245 #endif // TEUCHOS_DEBUG 250 template<
class Dealloc_T>
252 RCP<T>::RCP( T* p, Dealloc_T dealloc, ERCPUndefinedWithDealloc,
bool has_ownership_in )
254 #ifndef TEUCHOS_DEBUG
255 , node_(RCP_createNewDeallocRCPNodeRawPtrUndefined(p, dealloc, has_ownership_in))
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())
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());
385 return Ptr<T>(getRawPtr());
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());
500 return node_.same_node(r_ptr.access_private_node());
551 *
this =
rcp(p, has_ownership_in);
559 return node_.
count();
574 return RCP<T>(p, owns_mem);
578 template<
class T,
class Dealloc_T>
581 Teuchos::rcpWithDealloc( T* p, Dealloc_T dealloc,
bool owns_mem )
583 return RCP<T>(p, dealloc, owns_mem);
587 template<
class T,
class Dealloc_T>
590 Teuchos::rcpWithDeallocUndef( T* p, Dealloc_T dealloc,
bool owns_mem )
592 return RCP<T>(p, dealloc, RCP_UNDEFINED_WITH_DEALLOC, owns_mem);
598 Teuchos::rcpFromRef( T& r )
600 return RCP<T>(&r, RCP_WEAK_NO_DEALLOC);
606 Teuchos::rcpFromUndefRef( T& r )
608 return RCP<T>(&r, RCP_UNDEFINED_WEAK_NO_DEALLOC);
612 template<
class T,
class Embedded>
614 Teuchos::rcpWithEmbeddedObjPreDestroy(
615 T* p,
const Embedded &embedded,
bool owns_mem
619 p, embeddedObjDeallocDelete<T>(embedded,PRE_DESTROY), owns_mem
624 template<
class T,
class Embedded>
626 Teuchos::rcpWithEmbeddedObjPostDestroy(
627 T* p,
const Embedded &embedded,
bool owns_mem
630 return rcpWithDealloc( p, embeddedObjDeallocDelete<T>(embedded,POST_DESTROY), owns_mem );
634 template<
class T,
class Embedded>
636 Teuchos::rcpWithEmbeddedObj( T* p,
const Embedded &embedded,
bool owns_mem )
638 return rcpWithEmbeddedObjPostDestroy<T,Embedded>(p,embedded,owns_mem);
642 template<
class T,
class ParentT>
644 Teuchos::rcpWithInvertedObjOwnership(
const RCP<T> &child,
645 const RCP<ParentT> &parent)
647 using std::make_pair;
654 Teuchos::rcpCloneNode(
const RCP<T> &p)
681 bool Teuchos::operator==(
const RCP<T> &p,
ENull )
683 return p.get() == NULL;
689 bool Teuchos::operator!=(
const RCP<T> &p,
ENull )
691 return p.get() != NULL;
695 template<
class T1,
class T2>
697 bool Teuchos::operator==(
const RCP<T1> &p1,
const RCP<T2> &p2 )
699 return p1.access_private_node().same_node(p2.access_private_node());
703 template<
class T1,
class T2>
705 bool Teuchos::operator!=(
const RCP<T1> &p1,
const RCP<T2> &p2 )
707 return !p1.access_private_node().same_node(p2.access_private_node());
711 template<
class T2,
class T1>
714 Teuchos::rcp_implicit_cast(
const RCP<T1>& p1)
717 T2 *check = p1.
get();
718 return RCP<T2>(check, p1.access_private_node());
722 template<
class T2,
class T1>
725 Teuchos::rcp_static_cast(
const RCP<T1>& p1)
728 T2 *check =
static_cast<T2*
>(p1.get());
729 return RCP<T2>(check, p1.access_private_node());
733 template<
class T2,
class T1>
736 Teuchos::rcp_const_cast(
const RCP<T1>& p1)
739 T2 *check =
const_cast<T2*
>(p1.get());
740 return RCP<T2>(check, p1.access_private_node());
744 template<
class T2,
class T1>
747 Teuchos::rcp_dynamic_cast(
const RCP<T1>& p1,
bool throw_on_fail)
756 p =
dynamic_cast<T2*
>(p1.get());
759 return RCP<T2>(p, p1.access_private_node());
766 template<
class T1,
class T2>
768 void Teuchos::set_extra_data(
const T1 &extra_data,
const std::string& name,
771 p->assert_not_null();
772 p->nonconst_access_private_node().set_extra_data(
773 any(extra_data), name, destroy_when,
778 template<
class T1,
class T2>
780 const T1& Teuchos::get_extra_data(
const RCP<T2>& p,
const std::string& name )
784 p.access_private_node().get_extra_data(
785 TypeNameTraits<T1>::name(), name
791 template<
class T1,
class T2>
793 T1& Teuchos::get_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
797 p.nonconst_access_private_node().get_extra_data(
798 TypeNameTraits<T1>::name(), name
804 template<
class T1,
class T2>
807 Teuchos::get_optional_extra_data(
const RCP<T2>& p,
const std::string& name )
810 const any *extra_data = p.access_private_node().get_optional_extra_data(
811 TypeNameTraits<T1>::name(), name);
813 return Ptr<const T1>(&
any_cast<T1>(*extra_data));
818 template<
class T1,
class T2>
821 Teuchos::get_optional_nonconst_extra_data( RCP<T2>& p,
const std::string& name )
824 any *extra_data = p.nonconst_access_private_node().get_optional_extra_data(
825 TypeNameTraits<T1>::name(), name);
827 return Ptr<T1>(&
any_cast<T1>(*extra_data));
832 template<
class Dealloc_T,
class T>
834 const Dealloc_T& Teuchos::get_dealloc(
const RCP<T>& p )
836 return get_nonconst_dealloc<Dealloc_T>(
const_cast<RCP<T>&
>(p));
840 template<
class Dealloc_T,
class T>
842 Dealloc_T& Teuchos::get_nonconst_dealloc(
const RCP<T>& p )
844 typedef RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T> requested_type;
846 RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T>
847 *dnode =
dynamic_cast<RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T>*
>(
848 p.access_private_node().node_ptr());
850 dnode==NULL, NullReferenceError
851 ,
"get_dealloc<" << TypeNameTraits<Dealloc_T>::name()
852 <<
"," << TypeNameTraits<T>::name() <<
">(p): " 853 <<
"Error, requested type \'" << TypeNameTraits<requested_type>::name()
854 <<
"\' does not match actual type of the node \'" 855 <<
typeName(*p.access_private_node().node_ptr()) <<
"!" 857 return dnode->get_nonconst_dealloc();
861 template<
class Dealloc_T,
class T>
864 Teuchos::get_optional_nonconst_dealloc(
const RCP<T>& p )
867 typedef RCPNodeTmpl<typename Dealloc_T::ptr_t,Dealloc_T> RCPNT;
868 RCPNT *dnode =
dynamic_cast<RCPNT*
>(p.access_private_node().node_ptr());
870 return ptr(&dnode->get_nonconst_dealloc());
875 template<
class Dealloc_T,
class T>
878 Teuchos::get_optional_dealloc(
const RCP<T>& p )
880 return get_optional_nonconst_dealloc<Dealloc_T>(
const_cast<RCP<T>&
>(p));
884 template<
class TOrig,
class Embedded,
class T>
885 const Embedded& Teuchos::getEmbeddedObj(
const RCP<T>& p )
887 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
888 return get_dealloc<Dealloc_t>(p).getObj();
892 template<
class TOrig,
class Embedded,
class T>
893 Embedded& Teuchos::getNonconstEmbeddedObj(
const RCP<T>& p )
895 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
896 return get_nonconst_dealloc<Dealloc_t>(p).getNonconstObj();
900 template<
class TOrig,
class Embedded,
class T>
902 Teuchos::getOptionalEmbeddedObj(
const RCP<T>& p )
904 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
905 const Ptr<const Dealloc_t> dealloc = get_optional_dealloc<Dealloc_t>(p);
907 return ptr(&dealloc->getObj());
913 template<
class TOrig,
class Embedded,
class T>
915 Teuchos::getOptionalNonconstEmbeddedObj(
const RCP<T>& p )
917 typedef EmbeddedObjDealloc<TOrig,Embedded,DeallocDelete<TOrig> > Dealloc_t;
918 const Ptr<Dealloc_t> dealloc = get_optional_nonconst_dealloc<Dealloc_t>(p);
920 return ptr(&dealloc->getNonconstObj());
926 template<
class ParentT,
class T>
930 typedef std::pair<RCP<T>, RCP<ParentT> > Pair_t;
931 Pair_t pair = getEmbeddedObj<T, Pair_t>(invertedChild);
937 std::ostream& Teuchos::operator<<( std::ostream& out, const RCP<T>& p )
941 <<
"ptr="<<(
const void*)(p.get())
942 <<
",node="<<p.access_private_node()
943 <<
",strong_count="<<p.strong_count()
944 <<
",weak_count="<<p.weak_count()
950 #endif // TEUCHOS_RCP_HPP const RCP< T > & assert_valid_ptr() const
If the object pointer is non-null, assert that it is still valid.
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...
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
int strong_count() const
Return the number of active RCP<> objects that have a "strong" reference to the underlying reference-...
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
T & get(ParameterList &l, const std::string &name)
A shorter name for getParameter().
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.
int total_count() const
Total count (strong_count() + weak_count()).
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.
TEUCHOS_DEPRECATED int count() const
Returns strong_count() [deprecated].
int weak_count() const
Return the number of active RCP<> objects that have a "weak" reference to the underlying reference-co...
T_To & dyn_cast(T_From &from)
Dynamic casting utility function meant to replace dynamic_cast<T&> by throwing a better documented er...
ERCPStrength strength() const
Strength of the pointer.
void release()
Releaes the RCPNode pointer before the destructor is called.
bool is_valid_ptr() const
Return if the underlying object pointer is still valid or not.
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
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 * get() const
Get the raw C++ pointer to the 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.
void swap(Array< T > &a1, Array< T > &a2)
Non-member swap (specializes default std version).
RCP< T2 > rcp_implicit_cast(const RCP< T1 > &p1)
Implicit cast of underlying RCP type from T1* to T2*.
Node class to keep track of address and the reference count for a reference-counted utility class and...
const Ptr< T > & assert_not_null() const
Throws std::logic_error if this->get()==NULL, otherwise returns reference to *this.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
ERCPStrength
Used to specify if the pointer is weak or strong.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
Provides std::map class for deficient platforms.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
bool has_ownership() const
Returns true if this has ownership of object pointed to by this->get() in order to delete it...
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.
TEUCHOSCORE_LIB_DLL_EXPORT void throw_null_ptr_error(const std::string &type_name)
Throw that a pointer passed into an RCP object is null.
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.
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
Defines basic traits returning the name of a type in a portable and readable way. ...
void swap(RCPNodeHandle &node_ref)
Swap the contents of node_ref with *this.
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.