Teuchos - Trilinos Tools Package
Version of the Day
|
Smart reference counting pointer class for automatic garbage collection. More...
#include <Teuchos_RCPDecl.hpp>
Public Types | |
typedef T | element_type |
Public Member Functions | |
RCP (T *p, ERCPWeakNoDealloc) | |
Construct a non-owning RCP from a raw pointer to a type that is defined. More... | |
RCP (T *p, ERCPUndefinedWeakNoDealloc) | |
Construct a non-owning RCP from a raw pointer to a type that is not defined. More... | |
template<class Dealloc_T > | |
RCP (T *p, Dealloc_T dealloc, ERCPUndefinedWithDealloc, bool has_ownership=true) | |
Construct from a raw pointer and a custom deallocator for an undefined type. More... | |
Constructors/destructors/initializers. | |
RCP (ENull null_arg=null) | |
Initialize RCP<T> to NULL. More... | |
RCP (T *p, bool has_ownership=true) | |
Construct from a raw pointer. More... | |
template<class Dealloc_T > | |
RCP (T *p, Dealloc_T dealloc, bool has_ownership) | |
Construct from a raw pointer and a custom deallocator. More... | |
RCP (const RCP< T > &r_ptr) | |
Initialize from another RCP<T> object. More... | |
RCP (RCP< T > &&r_ptr) | |
Move constructor. More... | |
template<class T2 > | |
RCP (const RCP< T2 > &r_ptr) | |
Initialize from another RCP<T2> object (implicit conversion only). More... | |
~RCP () | |
Removes a reference to a dynamically allocated object and possibly deletes the object if owned. More... | |
RCP< T > & | operator= (const RCP< T > &r_ptr) |
Copy the pointer to the referenced object and increment the reference count. More... | |
RCP< T > & | operator= (RCP< T > &&r_ptr) |
Move assign. More... | |
RCP< T > & | operator= (ENull) |
Assign to null. More... | |
void | swap (RCP< T > &r_ptr) |
Swap the contents with some other RCP object. More... | |
Object/Pointer Access Functions | |
bool | is_null () const |
Returns true if the underlying pointer is null. More... | |
T * | operator-> () const |
Pointer (-> ) access to members of underlying object. More... | |
T & | operator* () const |
Dereference the underlying object. More... | |
T * | get () const |
Get the raw C++ pointer to the underlying object. More... | |
T * | getRawPtr () const |
Get the raw C++ pointer to the underlying object. More... | |
Ptr< T > | ptr () const |
Get a safer wrapper raw C++ pointer to the underlying object. More... | |
Ptr< T > | operator() () const |
Shorthand for ptr(). More... | |
RCP< const T > | getConst () const |
Return an RCP<const T> version of *this. More... | |
Reference counting | |
ERCPStrength | strength () const |
Strength of the pointer. More... | |
bool | is_valid_ptr () const |
Return if the underlying object pointer is still valid or not. More... | |
int | strong_count () const |
Return the number of active RCP<> objects that have a "strong" reference to the underlying reference-counted object. More... | |
int | weak_count () const |
Return the number of active RCP<> objects that have a "weak" reference to the underlying reference-counted object. More... | |
int | total_count () const |
Total count (strong_count() + weak_count()). More... | |
void | set_has_ownership () |
Give this and other RCP<> objects ownership of the referenced object this->get() . More... | |
bool | has_ownership () const |
Returns true if this has ownership of object pointed to by this->get() in order to delete it. More... | |
Ptr< T > | release () |
Release the ownership of the underlying dynamically allocated object. More... | |
RCP< T > | create_weak () const |
Create a new weak RCP object from another (strong) RCP object. More... | |
RCP< T > | create_strong () const |
Create a new strong RCP object from another (weak) RCP object. More... | |
template<class T2 > | |
bool | shares_resource (const RCP< T2 > &r_ptr) const |
Returns true if the smart pointers share the same underlying reference-counted object. More... | |
Assertions | |
const RCP< T > & | assert_not_null () const |
Throws NullReferenceError if this->get()==NULL , otherwise returns reference to *this . More... | |
const RCP< T > & | assert_valid_ptr () const |
If the object pointer is non-null, assert that it is still valid. More... | |
const RCP< T > & | debug_assert_not_null () const |
Calls assert_not_null() in a debug build. More... | |
const RCP< T > & | debug_assert_valid_ptr () const |
Calls assert_valid_ptr() in a debug build. More... | |
boost::shared_ptr compatiblity funtions. | |
void | reset () |
Reset to null. More... | |
template<class T2 > | |
void | reset (T2 *p, bool has_ownership=true) |
Reset the raw pointer with default ownership to delete. More... | |
TEUCHOS_DEPRECATED int | count () const |
Returns strong_count() [deprecated]. More... | |
Related Functions | |
(Note that these are not member functions.) | |
enum | ENull |
Used to initialize a RCP object to NULL using an implicit conversion! More... | |
template<typename T > | |
RCP< T > | rcpFromPtr (const Ptr< T > &ptr) |
Create an RCP<T> from a Ptr<T> object. More... | |
template<class T > | |
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. More... | |
template<class T > | |
boost::shared_ptr< T > | shared_pointer (const RCP< T > &rcp) |
Conversion function that takes in a Teuchos::RCP object and spits out a boost::shared_ptr object. More... | |
template<class T > | |
RCP< T > | rcp (T *p, bool owns_mem=true) |
Create a RCP object properly typed. More... | |
template<class T , class Dealloc_T > | |
RCP< T > | rcpWithDealloc (T *p, Dealloc_T dealloc, bool owns_mem=true) |
Initialize from a raw pointer with a deallocation policy. More... | |
template<class T , class Dealloc_T > | |
RCP< T > | rcpWithDeallocUndef (T *p, Dealloc_T dealloc, bool owns_mem=true) |
Initialize from a raw pointer with a deallocation policy for an undefined type. More... | |
template<class T > | |
RCP< T > | rcpFromRef (T &r) |
Return a non-owning weak RCP object from a raw object reference for a defined type. More... | |
template<class T > | |
RCP< T > | rcpFromUndefRef (T &r) |
Return a non-owning weak RCP object from a raw object reference for an undefined type. More... | |
template<class T , class Embedded > | |
RCP< T > | rcpWithEmbeddedObjPreDestroy (T *p, const Embedded &embedded, bool owns_mem=true) |
Create an RCP with and also put in an embedded object. More... | |
template<class T , class Embedded > | |
RCP< T > | rcpWithEmbeddedObjPostDestroy (T *p, const Embedded &embedded, bool owns_mem=true) |
Create an RCP with and also put in an embedded object. More... | |
template<class T , class Embedded > | |
RCP< T > | rcpWithEmbeddedObj (T *p, const Embedded &embedded, bool owns_mem=true) |
Create an RCP with and also put in an embedded object. More... | |
template<class T , class ParentT > | |
RCP< T > | rcpWithInvertedObjOwnership (const RCP< T > &child, const RCP< ParentT > &parent) |
Create a new RCP that inverts the ownership of parent and child. More... | |
template<class T > | |
RCP< T > | rcpCloneNode (const RCP< T > &p) |
Allocate a new RCP object with a new RCPNode with memory pointing to the initial node. More... | |
template<class T > | |
bool | is_null (const RCP< T > &p) |
Returns true if p.get()==NULL . More... | |
template<class T > | |
bool | nonnull (const RCP< T > &p) |
Returns true if p.get()!=NULL . More... | |
template<class T > | |
bool | operator== (const RCP< T > &p, ENull) |
Returns true if p.get()==NULL . More... | |
template<class T > | |
bool | operator!= (const RCP< T > &p, ENull) |
Returns true if p.get()!=NULL . More... | |
template<class T1 , class T2 > | |
bool | operator== (const RCP< T1 > &p1, const RCP< T2 > &p2) |
Return true if two RCP objects point to the same referenced-counted object and have the same node. More... | |
template<class T1 , class T2 > | |
bool | operator!= (const RCP< T1 > &p1, const RCP< T2 > &p2) |
Return true if two RCP objects do not point to the same referenced-counted object and have the same node. More... | |
template<class T2 , class T1 > | |
RCP< T2 > | rcp_implicit_cast (const RCP< T1 > &p1) |
Implicit cast of underlying RCP type from T1* to T2* . More... | |
template<class T2 , class T1 > | |
RCP< T2 > | rcp_static_cast (const RCP< T1 > &p1) |
Static cast of underlying RCP type from T1* to T2* . More... | |
template<class T2 , class T1 > | |
RCP< T2 > | rcp_const_cast (const RCP< T1 > &p1) |
Constant cast of underlying RCP type from T1* to T2* . More... | |
template<class T2 , class T1 > | |
RCP< T2 > | rcp_dynamic_cast (const RCP< T1 > &p1, bool throw_on_fail=false) |
Dynamic cast of underlying RCP type from T1* to T2* . More... | |
template<class T1 , class T2 > | |
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. More... | |
template<class T1 , class T2 > | |
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. More... | |
template<class T1 , class T2 > | |
T1 & | get_nonconst_extra_data (RCP< T2 > &p, const std::string &name) |
Get a non-const reference to extra data associated with a RCP object. More... | |
template<class T1 , class T2 > | |
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. More... | |
template<class T1 , class T2 > | |
Ptr< T1 > | get_optional_nonconst_extra_data (RCP< T2 > &p, const std::string &name) |
Get a pointer to non-const extra data (if it exists) associated with a RCP object. More... | |
template<class Dealloc_T , class T > | |
const Dealloc_T & | get_dealloc (const RCP< T > &p) |
Return a const reference to the underlying deallocator object. More... | |
template<class Dealloc_T , class T > | |
Dealloc_T & | get_nonconst_dealloc (const RCP< T > &p) |
Return a non-const reference to the underlying deallocator object. More... | |
template<class Dealloc_T , class T > | |
Ptr< const Dealloc_T > | get_optional_dealloc (const RCP< T > &p) |
Return a pointer to the underlying const deallocator object if it exists. More... | |
template<class Dealloc_T , class T > | |
Ptr< Dealloc_T > | get_optional_nonconst_dealloc (const RCP< T > &p) |
Return a pointer to the underlying non-const deallocator object if it exists. More... | |
template<class TOrig , class Embedded , class T > | |
const Embedded & | getEmbeddedObj (const RCP< T > &p) |
Get a const reference to an embedded object that was set by calling rcpWithEmbeddedObjPreDestroy() , rcpWithEmbeddedObjPostDestory() , or rcpWithEmbeddedObj() . More... | |
template<class TOrig , class Embedded , class T > | |
Embedded & | getNonconstEmbeddedObj (const RCP< T > &p) |
Get a non-const reference to an embedded object that was set by calling rcpWithEmbeddedObjPreDestroy() , rcpWithEmbeddedObjPostDestory() , or rcpWithEmbeddedObj() . More... | |
template<class TOrig , class Embedded , class T > | |
Ptr< const Embedded > | getOptionalEmbeddedObj (const RCP< T > &p) |
Get an optional Ptr to a const embedded object if it was set by calling rcpWithEmbeddedObjPreDestroy() , rcpWithEmbeddedObjPostDestory() , or rcpWithEmbeddedObj() . More... | |
template<class TOrig , class Embedded , class T > | |
Ptr< Embedded > | getOptionalNonconstEmbeddedObj (const RCP< T > &p) |
Get an optional Ptr to a non-const embedded object if it was set by calling rcpWithEmbeddedObjPreDestroy() , rcpWithEmbeddedObjPostDestory() , or rcpWithEmbeddedObj() . More... | |
template<class T > | |
std::ostream & | operator<< (std::ostream &out, const RCP< T > &p) |
Output stream inserter. More... | |
template<class T > | |
RCP< T > | rcp (const std::shared_ptr< T > &sptr) |
Conversion function that takes in a std::shared_ptr object and spits out a Teuchos::RCP object. More... | |
template<class T > | |
std::shared_ptr< T > | get_shared_ptr (const RCP< T > &rcp) |
Conversion function that takes in a Teuchos::RCP object and spits out a std::shared_ptr object. More... | |
Smart reference counting pointer class for automatic garbage collection.
For a carefully written discussion about what this class is and basic details on how to use it see the beginners guide.
Quickstart for RCP
Here we present a short, but fairly comprehensive, quick-start for the use of RCP<>
. The use cases described here should cover the overwhelming majority of the use instances of RCP<>
in a typical program.
The following class hierarchy will be used in the C++ examples given below.
All of the following code examples used in this quickstart are assumed to be in the namespace Teuchos
or have appropriate using Teuchos::...
declarations. This removes the need to explicitly use Teuchos::
to qualify classes, functions and other declarations from the Teuchos
namespace. Note that some of the runtime checks are denoted as "debug runtime checked" which means that checking will only be performed in a debug build (that is one where the macro TEUCHOS_DEBUG
is defined at compile time).
Creation of RCP<>
objects
Creating an RCP<>
object using new
Creating a RCP<>
object equipped with a specialized deallocator function : Teuchos::DeallocFunctorDelete
Initializing a RCP<>
object to NULL
or
Initializing a RCP<>
object to an object {not} allocated with new
Copy constructor (implicit casting)
Representing constantness and non-constantness
Non-constant pointer to non-constant object
Constant pointer to non-constant object
Non-Constant pointer to constant object
Constant pointer to constant object
Reinitialization of RCP<>
objects (using assignment operator)
Resetting from a raw pointer
Resetting to null
Assigning from a RCP<>
object
Accessing the reference-counted object
Access to object reference (debug runtime checked) : Teuchos::RCP::operator*()
Access to object pointer (unchecked, may return NULL
) : Teuchos::RCP::get()
or
WARNING:: Avoid exposing raw C++ pointers!
Access to object pointer (debug runtime checked, will not return NULL
) : Teuchos::RCP::operator*()
WARNING:: Avoid exposing raw C++ pointers!
Access of object's member (debug runtime checked) : Teuchos::RCP::operator->()
Testing for non-null : Teuchos::RCP::get()
, Teuchos::operator==()
, Teuchos::operator!=()
or
or
Testing for null
or
or
Casting
Implicit casting (see copy constructor above)
Using copy constructor (see above)
Using conversion function
Casting away const
: rcp_const_cast()
Static cast (no runtime check) : rcp_static_cast()
Dynamic cast (runtime checked, failed cast allowed) : rcp_dynamic_cast()
Dynamic cast (runtime checked, failed cast not allowed) : rcp_dynamic_cast()
Customized deallocators
Creating a RCP<>
object with a custom deallocator : rcp()
TODO: Update this example!
Access customized deallocator (runtime checked, throws on failure) : Teuchos::get_dealloc()
Access optional customized deallocator : Teuchos::get_optional_dealloc()
Managing extra data
Adding extra data (post destruction of extra data) : Teuchos::set_extra_data()
Adding extra data (pre destruction of extra data) : Teuchos::get_extra_data()
Retrieving extra data : Teuchos::get_extra_data()
Resetting extra data : Teuchos::get_extra_data()
Retrieving optional extra data : Teuchos::get_optional_extra_data()
Definition at line 429 of file Teuchos_RCPDecl.hpp.
typedef T Teuchos::RCP< T >::element_type |
Definition at line 433 of file Teuchos_RCPDecl.hpp.
|
inline |
Initialize RCP<T>
to NULL.
Postconditons:
this->get() == 0
this->strength() == RCP_STRONG
this->is_vali_ptr() == true
this->strong_count() == 0
this->weak_count() == 0
this->has_ownership() == false
This allows clients to write code like:
or
and construct to NULL
Definition at line 149 of file Teuchos_RCP.hpp.
|
inlineexplicit |
Construct from a raw pointer.
Note that this constructor is declared explicit so there is no implicit conversion from a raw pointer to an RCP allowed. If has_ownership==false
, then no attempt to delete the object will occur.
Postconditons:
this->get() == p
this->strength() == RCP_STRONG
this->is_vali_ptr() == true
this->strong_count() == 1
this->weak_count() == 0
this->has_ownership() == has_ownership
NOTE: It is recommended that this constructor never be called directly but only through a type-specific non-member constructor function or at least through the general non-member rcp()
function.
Definition at line 192 of file Teuchos_RCP.hpp.
|
inline |
Construct from a raw pointer and a custom deallocator.
p | [in] Pointer to the reference-counted object to be wrapped |
dealloc | [in] Deallocator policy object that will be copied by value and will perform the custom deallocation of the object pointed to by p when the last RCP object goes away. See the class DeallocDelete for the specfication and behavior of this policy interface. |
this->get() == p
this->strength() == RCP_STRONG
this->is_vali_ptr() == true
this->strong_count() == 1
this->weak_count() == 0
this->has_ownership() == has_ownership
get_dealloc<Delalloc_T>(*this)
returns a copy of the custom deallocator object dealloc>/tt>.
Definition at line 226 of file Teuchos_RCP.hpp.
|
inline |
Initialize from another RCP<T>
object.
After construction, this
and r_ptr
will reference the same object.
This form of the copy constructor is required even though the below more general templated version is sufficient since some compilers will generate this function automatically which will give an incorrect implementation.
Postconditons:
this->get() == r_ptr.get()
this->strong_count() == r_ptr.strong_count()
this->has_ownership() == r_ptr.has_ownership()
r_ptr.get() != NULL
then r_ptr.strong_count()
is incremented by 1 Definition at line 279 of file Teuchos_RCP.hpp.
|
inline |
Move constructor.
Postconditons:
*this
is an exact copy of r_ptr
before the call. r_ptr
is uninitialized Definition at line 286 of file Teuchos_RCP.hpp.
Initialize from another RCP<T2>
object (implicit conversion only).
This function allows the implicit conversion of smart pointer objects just like with raw C++ pointers. Note that this function will only compile if the statement T1 *ptr = r_ptr.get()
will compile.
Postconditons:
this->get() == r_ptr.get()
this->strong_count() == r_ptr.strong_count()
this->has_ownership() == r_ptr.has_ownership()
r_ptr.get() != NULL
then r_ptr.strong_count()
is incremented by 1 Definition at line 296 of file Teuchos_RCP.hpp.
|
inline |
Removes a reference to a dynamically allocated object and possibly deletes the object if owned.
Deletes the object if this->has_ownership() == true
and this->strong_count() == 1
. If this->strong_count() == 1
but this->has_ownership() == false
then the object is not deleted. If this->strong_count() > 1
then the internal reference count shared by all the other related RCP<...>
objects for this shared object is deincremented by one. If this->get() == NULL
then nothing happens.
Definition at line 304 of file Teuchos_RCP.hpp.
|
inlineexplicit |
Construct a non-owning RCP from a raw pointer to a type that is defined.
This version avoids adding a deallocator but still requires the type to be defined since it looks up the base object's address when doing RCPNode tracing.
NOTE: It is recommended that this constructor never be called directly but only through a type-specific non-member constructor function or at least through the general non-member rcpFromRef()
function.
Definition at line 156 of file Teuchos_RCP.hpp.
|
inlineexplicit |
Construct a non-owning RCP from a raw pointer to a type that is not defined.
This version avoids any type of compile-time queries of the type that would fail due to the type being undefined.
NOTE: It is recommended that this constructor never be called directly but only through a type-specific non-member constructor function or at least through the general non-member rcpFromUndefRef()
function.
Definition at line 184 of file Teuchos_RCP.hpp.
|
inline |
Construct from a raw pointer and a custom deallocator for an undefined type.
This version avoids any type of compile-time queries of the type that would fail due to the type being undefined.
Definition at line 252 of file Teuchos_RCP.hpp.
|
inline |
Copy the pointer to the referenced object and increment the reference count.
If this->has_ownership() == true
and this->strong_count() == 1
before this operation is called, then the object pointed to by this->get()
will be deleted (usually using delete
) prior to binding to the pointer (possibly NULL
) pointed to in r_ptr
. Assignment to self (i.e. this->get() == r_ptr.get()
) is harmless and this function does nothing.
Postconditons:
this->get() == r_ptr.get()
this->strong_count() == r_ptr.strong_count()
this->has_ownership() == r_ptr.has_ownership()
r_ptr.get() != NULL
then r_ptr.strong_count()
is incremented by 1 Provides the "strong guarantee" in a debug build!
Definition at line 310 of file Teuchos_RCP.hpp.
|
inline |
Move assign.
Postconditons:
*this
is an exact copy of r_ptr
before the call. r_ptr
is uninitialized Provides the "strong guarantee" in a debug build!
Definition at line 324 of file Teuchos_RCP.hpp.
|
inline |
Assign to null.
If this->has_ownership() == true
and this->strong_count() == 1
before this operation is called, then the object pointed to by this->get()
will be deleted (usually using delete
) prior to binding to the pointer (possibly NULL
) pointed to in r_ptr
.
Postconditons:
RCP(ENull)
Definition at line 340 of file Teuchos_RCP.hpp.
|
inline |
Swap the contents with some other RCP object.
Definition at line 349 of file Teuchos_RCP.hpp.
|
inline |
Returns true if the underlying pointer is null.
Definition at line 361 of file Teuchos_RCP.hpp.
|
inline |
Pointer (->
) access to members of underlying object.
Preconditions:
this->get() != NULL
(throws NullReferenceError
) Definition at line 369 of file Teuchos_RCP.hpp.
|
inline |
Dereference the underlying object.
Preconditions:
this->get() != NULL
(throws NullReferenceError
) Definition at line 379 of file Teuchos_RCP.hpp.
|
inline |
Get the raw C++ pointer to the underlying object.
NOTE: Prefer to get the safer Ptr<T> object from this->ptr()
!
Definition at line 388 of file Teuchos_RCP.hpp.
|
inline |
Get the raw C++ pointer to the underlying object.
NOTE: Prefer to get the safer Ptr<T> object from this->ptr()
!
Definition at line 397 of file Teuchos_RCP.hpp.
|
inline |
Get a safer wrapper raw C++ pointer to the underlying object.
Definition at line 405 of file Teuchos_RCP.hpp.
|
inline |
Shorthand for ptr().
Definition at line 417 of file Teuchos_RCP.hpp.
|
inline |
Return an RCP<const T> version of *this.
Definition at line 425 of file Teuchos_RCP.hpp.
|
inline |
Strength of the pointer.
Return values:
RCP_STRONG
: Underlying reference-counted object will be deleted when *this
is destroyed if strong_count()==1
. RCP_WEAK
: Underlying reference-counted object will not be deleted when *this
is destroyed if strong_count() > 0
. Definition at line 436 of file Teuchos_RCP.hpp.
|
inline |
Return if the underlying object pointer is still valid or not.
The underlying object will not be valid if the strong count has gone to zero but the weak count thas not.
NOTE: Null is a valid object pointer. If you want to know if there is a non-null object and it is valid then !is_null() && is_valid_ptr()
will be true
.
Definition at line 444 of file Teuchos_RCP.hpp.
|
inline |
Return the number of active RCP<>
objects that have a "strong" reference to the underlying reference-counted object.
this->get() == NULL
then this function returns 0. Definition at line 454 of file Teuchos_RCP.hpp.
|
inline |
Return the number of active RCP<>
objects that have a "weak" reference to the underlying reference-counted object.
this->get() == NULL
then this function returns 0. Definition at line 462 of file Teuchos_RCP.hpp.
|
inline |
Total count (strong_count() + weak_count()).
Definition at line 470 of file Teuchos_RCP.hpp.
|
inline |
Give this
and other RCP<>
objects ownership of the referenced object this->get()
.
See ~RCP() above. This function does nothing if this->get() == NULL
.
Postconditions:
this->get() == NULL
then this->has_ownership() == false
(always!). this->has_ownership() == true
Definition at line 478 of file Teuchos_RCP.hpp.
|
inline |
Returns true if this
has ownership of object pointed to by this->get()
in order to delete it.
See ~RCP() above.
== NULL
then this function always returns false
. Otherwise the value returned from this function depends on which function was called most recently, if any; set_has_ownership() (true
) or release() (false
). Definition at line 486 of file Teuchos_RCP.hpp.
|
inline |
Release the ownership of the underlying dynamically allocated object.
WARNING! Never call delete rcp.release().get()
as this can cause all kinds of segfaults. Instead, release your use of the shared object by simply assigning the RCP
object to Teuchos::null
.
This function should only be used as last result when all hell has broken loose and memory management control has broken down. This function is not to be used lightly!
After this function is called then the client is responsible for deallocating the shared object no matter how many ref_count_prt<T>
objects have a reference to it. If this->
get()== NULL
, then this call is meaningless.
Note that this function does not have the exact same semantics as does auto_ptr<T>::release()
. In auto_ptr<T>::release()
, this
is set to NULL
while here in RCP<T>:: release() only an ownership flag is set and *this
still points to the same object. It would be difficult to duplicate the behavior of auto_ptr<T>::release()
for this class.
Postconditions:
this->has_ownership() == false
this->get()
Definition at line 494 of file Teuchos_RCP.hpp.
|
inline |
Create a new weak RCP object from another (strong) RCP object.
ToDo: Explain this!
Preconditons:
returnVal.is_valid_ptr()==true
Postconditons:
returnVal.get() == this->get()
returnVal.strong_count() == this->strong_count()
returnVal.weak_count() == this->weak_count()+1
returnVal.strength() == RCP_WEAK
returnVal.has_ownership() == this->has_ownership()
Definition at line 504 of file Teuchos_RCP.hpp.
|
inline |
Create a new strong RCP object from another (weak) RCP object.
ToDo: Explain this!
Preconditons:
returnVal.is_valid_ptr()==true
Postconditons:
returnVal.get() == this->get()
returnVal.strong_count() == this->strong_count() + 1
returnVal.weak_count() == this->weak_count()
returnVal.strength() == RCP_STRONG
returnVal.has_ownership() == this->has_ownership()
Definition at line 513 of file Teuchos_RCP.hpp.
|
inline |
Returns true if the smart pointers share the same underlying reference-counted object.
This method does more than just check if this->get() == r_ptr.get()
. It also checks to see if the underlying reference counting machinary is the same.
Definition at line 539 of file Teuchos_RCP.hpp.
|
inline |
Throws NullReferenceError
if this->get()==NULL
, otherwise returns reference to *this
.
Definition at line 553 of file Teuchos_RCP.hpp.
|
inline |
If the object pointer is non-null, assert that it is still valid.
If is_null()==false && strong_count()==0
, this will throw DanglingReferenceErorr
with a great error message.
If is_null()==true
, then this will not throw any exception.
In this context, null is a valid object.
Definition at line 563 of file Teuchos_RCP.hpp.
|
inline |
Calls assert_not_null()
in a debug build.
Definition at line 838 of file Teuchos_RCPDecl.hpp.
|
inline |
Calls assert_valid_ptr()
in a debug build.
Definition at line 847 of file Teuchos_RCPDecl.hpp.
|
inline |
Reset to null.
Definition at line 576 of file Teuchos_RCP.hpp.
|
inline |
Reset the raw pointer with default ownership to delete.
Equivalent to calling:
Definition at line 590 of file Teuchos_RCP.hpp.
|
inline |
Returns strong_count()
[deprecated].
Definition at line 598 of file Teuchos_RCP.hpp.
|
related |
Used to initialize a RCP
object to NULL using an implicit conversion!
Definition at line 54 of file Teuchos_ENull.hpp.
Create an RCP<T> from a Ptr<T> object.
Definition at line 309 of file Teuchos_PtrDecl.hpp.
|
related |
Conversion function that takes in a boost::shared_ptr
object and spits out a Teuchos::RCP
object.
If the input boost::shared_ptr
already wraps a Teuchos::RCP
object, then that Teuchos::RCP
object will be copied and returned.
This function is not complicated, just look at its defintion below.
|
related |
Conversion function that takes in a Teuchos::RCP
object and spits out a boost::shared_ptr
object.
If the input Teuchos::RCP
already wraps a boost::shared_ptr
object, then that boost::shared_ptr
object will be copied and returned.
This function is not complicated, just look at its defintion below.
|
related |
Create a RCP
object properly typed.
p | [in] Pointer to an object to be reference counted. |
owns_mem | [in] If owns_mem==true then delete p will be called when the last reference to this object is removed. If owns_mem==false then nothing will happen to delete the the object pointed to by p when the last reference is removed. |
Preconditions:
owns_mem==true
then p
must have been created by calling new
to create the object since delete p
will be called eventually. If the pointer p
did not come from new
then either the client should use the version of rcp()
that that uses a deallocator policy object or should pass in owns_mem = false
.
|
related |
Initialize from a raw pointer with a deallocation policy.
p | [in] Raw C++ pointer that this will represent. |
dealloc | [in] Deallocator policy object (copied by value) that defines a function void Dealloc_T::free(T* p) that will free the underlying object. |
owns_mem | [in] If true then return is allowed to delete the underlying pointer by calling dealloc.free(p) . when all references have been removed. |
Preconditions:
void Dealloc_T::free(T* p)
exists. Postconditions:
return.get() == p
p == NULL
then return.count() == 0
return.has_ownership() == false
return.count() == 1
return.has_ownership() == owns_mem
By default, return
has ownership to delete the object pointed to by p
when return
is deleted (see ~RCP())
. If owns_mem==true
, it is vital that the address p
passed in is the same address that was returned by new
. With multiple inheritance this is not always the case. See the above discussion. This class is templated to accept a deallocator object that will free the pointer. The other functions use a default deallocator of type DeallocDelete
which has a method DeallocDelete::free()
which just calls delete p
.
|
related |
Initialize from a raw pointer with a deallocation policy for an undefined type.
p | [in] Raw C++ pointer that this will represent. |
dealloc | [in] Deallocator policy object (copied by value) that defines a function void Dealloc_T::free(T* p) that will free the underlying object. |
|
related |
Return a non-owning weak RCP object from a raw object reference for a defined type.
NOTE: When debug mode is turned on, in general, the type must be defined. If the type is undefined, then the function rcpFromUndefRef()
should be called instead.
|
related |
|
related |
Create an RCP with and also put in an embedded object.
In this case the embedded object is destroyed (by setting to Embedded()) before the object at *p
is destroyed.
The embedded object can be extracted using getEmbeddedObj()
and getNonconstEmbeddedObject()
.
|
related |
Create an RCP with and also put in an embedded object.
In this case the embedded object is destroyed (by setting to Embedded()) after the object at *p
is destroyed.
The embedded object can be extracted using getEmbeddedObj()
and getNonconstEmbeddedObject()
.
|
related |
Create an RCP with and also put in an embedded object.
This function should be called when it is not important when the embedded object is destroyed (by setting to Embedded()) with respect to when *p
is destroyed.
The embedded object can be extracted using getEmbeddedObj()
and getNonconstEmbeddedObject()
.
|
related |
Create a new RCP that inverts the ownership of parent and child.
This implements the "inverted object ownership" idiom.
NOTE: The parent can be retrieved using the function getInvertedObjOwnershipParent(...)
.
Allocate a new RCP object with a new RCPNode with memory pointing to the initial node.
The purpose of this function is to create a new "handle" to the underlying memory with its own seprate reference count. The new RCP object will have a new RCPNodeTmpl object that has a copy of the input RCP object embedded in it. This maintains the correct reference counting behaviors but now gives a private count. One would want to use rcpCloneNode(...) whenever it is important to keep a private reference count which is needed for some types of use cases.
|
related |
Returns true if p.get()==NULL
.
|
related |
Returns true if p.get()!=NULL
.
Returns true if p.get()==NULL
.
Returns true if p.get()!=NULL
.
|
related |
Return true if two RCP
objects point to the same referenced-counted object and have the same node.
|
related |
Return true if two RCP
objects do not point to the same referenced-counted object and have the same node.
Implicit cast of underlying RCP
type from T1*
to T2*
.
The function will compile only if (T2* p2 = p1.get();
) compiles.
This is to be used for conversions up an inheritance hierarchy and from non-const to const and any other standard implicit pointer conversions allowed by C++.
Static cast of underlying RCP
type from T1*
to T2*
.
The function will compile only if (static_cast<T2*>(p1.get());
) compiles.
This can safely be used for conversion down an inheritance hierarchy with polymorphic types only if dynamic_cast<T2>(p1.get()) == static_cast<T2>(p1.get())
. If not then you have to use rcp_dynamic_cast
<T2>(p1)
.
Constant cast of underlying RCP
type from T1*
to T2*
.
This function will compile only if (const_cast<T2*>(p1.get());
) compiles.
|
related |
Dynamic cast of underlying RCP
type from T1*
to T2*
.
p1 | [in] The smart pointer casting from |
throw_on_fail | [in] If true then if the cast fails (for p1.get()!=NULL) then a |
Postconditions:
If ( p1.get()!=NULL && throw_on_fail==true && dynamic_cast<T2*>(p1.get())==NULL ) == true
then an std::bad_cast
std::exception is thrown with a very informative error message.
If ( p1.get()!=NULL && dynamic_cast<T2*>(p1.get())!=NULL ) == true
then return.get() == dynamic_cast<T2*>(p1.get())
.
If ( p1.get()!=NULL && throw_on_fail==false && dynamic_cast<T2*>(p1.get())==NULL ) == true
then return.get() == NULL
.
If ( p1.get()==NULL ) == true
then return.get() == NULL
.
This function will compile only if (
dynamic_cast<T2*>(p1.get());
) compiles.
|
related |
Set extra data associated with a RCP
object.
extra_data | [in] Data object that will be set (copied) |
name | [in] The name given to the extra data. The value of name together with the data type T1 of the extra data must be unique from any other such data or the other data will be overwritten. |
p | [out] On output, will be updated with the input extra_data |
destroy_when | [in] Determines when extra_data will be destroyed in relation to the underlying reference-counted object. If destroy_when==PRE_DESTROY then extra_data will be deleted before the underlying reference-counted object. If destroy_when==POST_DESTROY (the default) then extra_data will be deleted after the underlying reference-counted object. |
force_unique | [in] Determines if this type and name pair must be unique in which case if an object with this same type and name already exists, then an std::exception will be thrown. The default is true for safety. |
If there is a call to this function with the same type of extra data T1
and same arguments p
and name
has already been made, then the current piece of extra data already set will be overwritten with extra_data
. However, if the type of the extra data T1
is different, then the extra data can be added and not overwrite existing extra data. This means that extra data is keyed on both the type and name. This helps to minimize the chance that clients will unexpectedly overwrite data by accident.
When the last RefcountPtr
object is removed and the reference-count node is deleted, then objects are deleted in the following order: (1) All of the extra data that where added with destroy_when==PRE_DESTROY
are first, (2) then the underlying reference-counted object is deleted, and (3) the rest of the extra data that was added with destroy_when==PRE_DESTROY
is then deleted. The order in which the objects are destroyed is not guaranteed. Therefore, clients should be careful not to add extra data that has deletion dependancies (instead consider using nested RCP objects as extra data which will guarantee the order of deletion).
Preconditions:
p->get() != NULL
(throws NullReferenceError
) T1
for extra_data
and the same std::string name
and force_unique==true
, then an std::invalid_argument
std::exception will be thrown. Note, this function is made a non-member function to be consistent with the non-member get_extra_data()
functions.
|
related |
Get a const reference to extra data associated with a RCP
object.
p | [in] Smart pointer object that extra data is being extraced from. |
name | [in] Name of the extra data. |
Preconditions:
p.get() != NULL
(throws NullReferenceError
) name
and T1
must have been used in a previous call to set_extra_data()
(throws std::invalid_argument
). Note, this function must be a non-member function since the client must manually select the first template argument.
|
related |
Get a non-const reference to extra data associated with a RCP
object.
p | [in] Smart pointer object that extra data is being extraced from. |
name | [in] Name of the extra data. |
Preconditions:
p.get() != NULL
(throws NullReferenceError
) name
and T1
must have been used in a previous call to set_extra_data()
(throws std::invalid_argument
). Note, this function must be a non-member function since the client must manually select the first template argument.
|
related |
Get a pointer to const extra data (if it exists) associated with a RCP
object.
p | [in] Smart pointer object that extra data is being extraced from. |
name | [in] Name of the extra data. |
Preconditions:
p.get() != NULL
(throws NullReferenceError
) Postconditions:
name
and T1
have been used in a previous call to set_extra_data()
then return !=NULL
and otherwise return == NULL
. Note, this function must be a non-member function since the client must manually select the first template argument.
|
related |
Get a pointer to non-const extra data (if it exists) associated with a RCP
object.
p | [in] Smart pointer object that extra data is being extraced from. |
name | [in] Name of the extra data. |
Preconditions:
p.get() != NULL
(throws NullReferenceError
) Postconditions:
name
and T1
have been used in a previous call to set_extra_data()
then return !=NULL
and otherwise return == NULL
. Note, this function must be a non-member function since the client must manually select the first template argument.
|
related |
Return a const
reference to the underlying deallocator object.
Preconditions:
p.get() != NULL
(throws NullReferenceError
) p
is same as Dealloc_T
(throws NullReferenceError
)
|
related |
Return a non-const
reference to the underlying deallocator object.
Preconditions:
p.get() != NULL
(throws NullReferenceError
) p
is same as Dealloc_T
(throws NullReferenceError
)
|
related |
Return a pointer to the underlying const
deallocator object if it exists.
Preconditions:
p.get() != NULL
(throws NullReferenceError
) Postconditions:
p
is same as Dealloc_T
then return!=NULL
, otherwise return==NULL
|
related |
Return a pointer to the underlying non-const
deallocator object if it exists.
Preconditions:
p.get() != NULL
(throws NullReferenceError
) Postconditions:
p
is same as Dealloc_T
then return!=NULL
, otherwise return==NULL
|
related |
Get a const reference to an embedded object that was set by calling rcpWithEmbeddedObjPreDestroy()
, rcpWithEmbeddedObjPostDestory()
, or rcpWithEmbeddedObj()
.
|
related |
Get a non-const reference to an embedded object that was set by calling rcpWithEmbeddedObjPreDestroy()
, rcpWithEmbeddedObjPostDestory()
, or rcpWithEmbeddedObj()
.
|
related |
Get an optional Ptr to a const embedded object if it was set by calling rcpWithEmbeddedObjPreDestroy()
, rcpWithEmbeddedObjPostDestory()
, or rcpWithEmbeddedObj()
.
|
related |
Get an optional Ptr to a non-const embedded object if it was set by calling rcpWithEmbeddedObjPreDestroy()
, rcpWithEmbeddedObjPostDestory()
, or rcpWithEmbeddedObj()
.
|
related |
Output stream inserter.
The implementation of this function just print pointer addresses and therefore puts no restrictions on the data types involved.
|
related |
Conversion function that takes in a std::shared_ptr
object and spits out a Teuchos::RCP
object.
If the input std::shared_ptr
already wraps a Teuchos::RCP
object, then that Teuchos::RCP
object will be copied and returned.
This function is not complicated, just look at its defintion below.
|
related |
Conversion function that takes in a Teuchos::RCP
object and spits out a std::shared_ptr
object.
If the input Teuchos::RCP
already wraps a std::shared_ptr
object, then that std::shared_ptr
object will be copied and returned.
This function is not complicated, just look at its defintion below.