#include <Teuchos_RCPDecl.hpp>
Public Types | |
| typedef T | element_type |
| | |
Public Member Functions | |
Constructors/destructors/initializers. | |
| RCP (ENull null_arg=null) | |
Initialize RCP<T> to NULL. | |
| RCP (T *p, bool has_ownership=false) | |
| Construct from a raw pointer. | |
| template<class Dealloc_T> | |
| RCP (T *p, Dealloc_T dealloc, bool has_ownership) | |
| Construct from a raw pointer and a custom deallocator. occur. | |
| RCP (const RCP< T > &r_ptr) | |
Initialize from another RCP<T> object. | |
| template<class T2> | |
| RCP (const RCP< T2 > &r_ptr) | |
Initialize from another RCP<T2> object (implicit conversion only). | |
| ~RCP () | |
| Removes a reference to a dynamically allocated object and possibly deletes the object if owned. | |
| RCP< T > & | operator= (const RCP< T > &r_ptr) |
| Copy the pointer to the referenced object and increment the reference count. | |
Object/Pointer Access Functions | |
| bool | is_null () const |
| Returns true if the underlying pointer is null. | |
| T * | operator-> () const |
Pointer (->) access to members of underlying object. | |
| T & | operator * () const |
| Dereference the underlying object. | |
| T * | get () const |
| Get the raw C++ pointer to the underlying object. | |
| 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. | |
Reference counting | |
| ERCPStrength | strength () const |
| Strength of the pointer. | |
| bool | is_valid_ptr () const |
| Return if the underlying object pointer is still valid or not. | |
| int | strong_count () const |
Return the number of active RCP<> objects that have a "strong" reference to the underlying reference-counted object. | |
| int | weak_count () const |
Return the number of active RCP<> objects that have a "weak" reference to the underlying reference-counted object. | |
| int | total_count () const |
| Total count (strong_count() + weak_count()). | |
| void | set_has_ownership () |
Give this and other RCP<> objects ownership of the referenced object this->get(). | |
| bool | has_ownership () const |
Returns true if this has ownership of object pointed to by this->get() in order to delete it. | |
| Ptr< T > | release () |
| Release the ownership of the underlying dynamically allocated object. | |
| RCP< T > | create_weak () const |
| Create a new weak RCP object from another RCP object. | |
| 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. | |
Assertions | |
| const RCP< T > & | assert_not_null () const |
Throws NullReferenceError if this->get()==NULL, otherwise returns reference to *this. | |
| const RCP< T > & | assert_valid_ptr () const |
| If the object pointer is non-null, assert that it is still valid. | |
Deprecated | |
| int | count () const |
Returns strong_count() [deprecated]. | |
Related Functions | |
| (Note that these are not member functions.) | |
| RCP< T > | rcp (T *p, bool owns_mem=true) |
Create a RCP object properly typed. | |
| RCP< T > | rcp (T *p, Dealloc_T dealloc, bool owns_mem) |
| Initialize from a raw pointer with a deallocation policy. | |
| Teuchos::RCP< T > | rcpFromRef (T &r) |
| Return a non-owning RCP object from a raw object reference. | |
| bool | is_null (const RCP< T > &p) |
Returns true if p.get()==NULL. | |
| bool | operator== (const RCP< T > &p, ENull) |
Returns true if p.get()==NULL. | |
| bool | operator!= (const RCP< T > &p, ENull) |
Returns true if p.get()!=NULL. | |
| 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. | |
| 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. | |
| RCP< T2 > | rcp_implicit_cast (const RCP< T1 > &p1) |
Implicit cast of underlying RCP type from T1* to T2*. | |
| RCP< T2 > | rcp_static_cast (const RCP< T1 > &p1) |
Static cast of underlying RCP type from T1* to T2*. | |
| RCP< T2 > | rcp_const_cast (const RCP< T1 > &p1) |
Constant cast of underlying RCP type from T1* to T2*. | |
| RCP< T2 > | rcp_dynamic_cast (const RCP< T1 > &p1, bool throw_on_fail=false) |
Dynamic cast of underlying RCP type from T1* to 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. | |
| 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. | |
| 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. | |
| 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. | |
| 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. | |
| const Dealloc_T & | get_dealloc (const RCP< T > &p) |
Return a const reference to the underlying deallocator object. | |
| Dealloc_T & | get_nonconst_dealloc (const RCP< T > &p) |
Return a non-const reference to the underlying deallocator object. | |
| Ptr< const Dealloc_T > | get_optional_dealloc (const RCP< T > &p) |
Return a pointer to the underlying const deallocator object if it exists. | |
| Ptr< Dealloc_T > | get_optional_nonconst_dealloc (const RCP< T > &p) |
Return a pointer to the underlying non-const deallocator object if it exists. | |
| const Embedded & | getEmbeddedObj (const RCP< T > &p) |
Get a const reference to an embedded object that was set by calling rcpWithEmbeddedObjPreDestroy(), rcpWithEmbeddedObjPostDestory(), or rcpWithEmbeddedObj(). | |
| Embedded & | getNonconstEmbeddedObj (const RCP< T > &p) |
Get a non-const reference to an embedded object that was set by calling rcpWithEmbeddedObjPreDestroy(), rcpWithEmbeddedObjPostDestory(), or rcpWithEmbeddedObj(). | |
| 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(). | |
| 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(). | |
| std::ostream & | operator<< (std::ostream &out, const RCP< T > &p) |
| Output stream inserter. | |
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.
class A { public: virtual ~A(){} virtual void f(){} }; class B1 : virtual public A {}; class B2 : virtual public A {}; class C : virtual public B1, virtual public B2 {}; class D {}; class E : public D {};
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).
RCP<> objects
RCP<> object using new
RCP<C> c_ptr = rcp(new C);
RCP<> object equipped with a specialized deallocator function : Teuchos::DeallocFunctorDelete
void someDeallocFunction(C* c_ptr); RCP<C> c_ptr = rcp(new deallocFunctorDelete<C>(someDeallocFunction),true);
RCP<> object to NULL
RCP<C> c_ptr;
or
RCP<C> c_ptr = null;
RCP<> object to an object {not} allocated with new
C c; RCP<C> c_ptr = rcpFromRef(c);
RCP<C> c_ptr = rcp(new C); // No cast RCP<A> a_ptr = c_ptr; // Cast to base class RCP<const A> ca_ptr = a_ptr; // Cast from non-const to const
RCP<C> c_ptr;
const RCP<C> c_ptr;
RCP<const C> c_ptr;
const RCP<const C> c_ptr;
RCP<> objects (using assignment operator)
RCP<A> a_ptr; a_ptr = rcp(new C());
RCP<A> a_ptr = rcp(new C()); a_ptr = null; // The C object will be deleted here
RCP<> object
RCP<A> a_ptr1; RCP<A> a_ptr2 = rcp(new C()); a_ptr1 = a_ptr2; // Now a_ptr1 and a_ptr2 point to same C object
Teuchos::RCP::operator*()
C &c_ref = *c_ptr;
NULL) : Teuchos::RCP::get()
C *c_rptr = c_ptr.get();
or
C *c_rptr = c_ptr.getRawPtr();
WARNING:: Avoid exposing raw C++ pointers!
NULL) : Teuchos::RCP::operator*()
C *c_rptr = &*c_ptr;
WARNING:: Avoid exposing raw C++ pointers!
Teuchos::RCP::operator->()
c_ptr->f();
Teuchos::RCP::get(), Teuchos::operator==(), Teuchos::operator!=()
if (a_ptr.is_null) std::cout << "a_ptr is not null!\n";
or
if (a_ptr != null) std::cout << "a_ptr is not null!\n";
or
if (!a_ptr.is_null()) std::cout << "a_ptr is null!\n";
or
if (a_ptr == null) std::cout << "a_ptr is null!\n";
or
if (is_null(a_ptr)) std::cout << "a_ptr is null!\n";
RCP<C> c_ptr = rcp(new C); // No cast RCP<A> a_ptr = rcp_implicit_cast<A>(c_ptr); // To base RCP<const A> ca_ptr = rcp_implicit_cast<const A>(a_ptr);// To const
const : rcp_const_cast()
RCP<const A> ca_ptr = rcp(new C); RCP<A> a_ptr = rcp_const_cast<A>(ca_ptr); // cast away const!
rcp_static_cast()
RCP<D> d_ptr = rcp(new E); RCP<E> e_ptr = rcp_static_cast<E>(d_ptr); // Unchecked, unsafe?
rcp_dynamic_cast()
RCP<A> a_ptr = rcp(new C); RCP<B1> b1_ptr = rcp_dynamic_cast<B1>(a_ptr); // Checked, safe! RCP<B2> b2_ptr = rcp_dynamic_cast<B2>(b1_ptr); // Checked, safe! RCP<C> c_ptr = rcp_dynamic_cast<C>(b2_ptr); // Checked, safe!
rcp_dynamic_cast()
RCP<A> a_ptr1 = rcp(new C); RCP<A> a_ptr2 = rcp(new A); RCP<B1> b1_ptr1 = rcp_dynamic_cast<B1>(a_ptr1, true); // Success! RCP<B1> b1_ptr2 = rcp_dynamic_cast<B1>(a_ptr2, true); // Throw std::bad_cast!
RCP<> object with a custom deallocator : rcp()TODO: Update this example!
Teuchos::get_dealloc()
const MyCustomDealloc<C>
&dealloc = get_dealloc<MyCustomDealloc<C> >(c_ptr);
Teuchos::get_optional_dealloc()
const Ptr<const MyCustomDealloc<C> > dealloc = get_optional_dealloc<MyCustomDealloc<C> >(c_ptr); if (!is_null(dealloc)) std::cout << "This deallocator exits!\n";
Teuchos::set_extra_data()
set_extra_data(rcp(new B1), "A:B1", inOutArg(a_ptr));
Teuchos::get_extra_data()
set_extra_data(rcp(new B1),"A:B1", inOutArg(a_ptr), PRE_DESTORY);
Teuchos::get_extra_data()
get_extra_data<RCP<B1> >(a_ptr, "A:B1")->f();
Teuchos::get_extra_data()
get_extra_data<RCP<B1> >(a_ptr, "A:B1") = rcp(new C);
Teuchos::get_optional_extra_data()
const Ptr<const RCP<B1> > b1 = get_optional_extra_data<RCP<B1> >(a_ptr, "A:B1"); if (!is_null(b1)) (*b1)->f();
|
|||||
|
|
|
||||||||||
|
Initialize Postconditons:
This allows clients to write code like: RCP<int> p = null; RCP<int> p; NULL |
|
||||||||||||||||
|
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 Postconditons:
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 |
|
||||||||||||||||||||||||
|
Construct from a raw pointer and a custom deallocator. occur.
|
|
||||||||||
|
Initialize from another
After construction, 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:
|
|
||||||||||||||
|
Initialize from another
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 Postconditons:
|
|
|||||||||
|
Removes a reference to a dynamically allocated object and possibly deletes the object if owned.
Deletes the object if |
|
||||||||||
|
Copy the pointer to the referenced object and increment the reference count.
If Postconditons:
Provides the "strong guarantee" in a debug build! |
|
|||||||||
|
Returns true if the underlying pointer is null.
|
|
|||||||||
|
Pointer ( Preconditions:
|
|
|||||||||
|
Dereference the underlying object. Preconditions:
|
|
|||||||||
|
Get the raw C++ pointer to the underlying object.
NOTE: Prefer to get the safer Ptr<T> object from |
|
|||||||||
|
Get the raw C++ pointer to the underlying object.
NOTE: Prefer to get the safer Ptr<T> object from |
|
|||||||||
|
Get a safer wrapper raw C++ pointer to the underlying object.
|
|
|||||||||
|
Strength of the pointer. Return values:
|
|
|||||||||
|
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 |
|
|||||||||
|
Return the number of active
|
|
|||||||||
|
Return the number of active
|
|
|||||||||
|
Total count (strong_count() + weak_count()).
|
|
|||||||||
|
Give
See ~RCP() above. This function does nothing if Postconditions:
|
|
|||||||||
|
Returns true if See ~RCP() above.
|
|
|||||||||
|
Release the ownership of the underlying dynamically allocated object.
WARNING! Never call 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
Note that this function does not have the exact same semantics as does Postconditions:
|
|
|||||||||
|
Create a new weak RCP object from another RCP object. ToDo: Explain this! Preconditons:
Postconditons:
|
|
||||||||||||||
|
Returns true if the smart pointers share the same underlying reference-counted object.
This method does more than just check if |
|
|||||||||
|
Throws
|
|
|||||||||
|
If the object pointer is non-null, assert that it is still valid.
If
If In this context, null is a valid object. |
|
|||||||||
|
Returns
|
|
||||||||||||||||
|
Create a
If the pointer |
|
||||||||||||||||||||
|
Initialize from a raw pointer with a deallocation policy.
Postconditions:
By default, |
|
||||||||||
|
Return a non-owning RCP object from a raw object reference.
|
|
||||||||||
|
Returns true if
|
|
||||||||||||||||
|
Returns true if
|
|
||||||||||||||||
|
Returns true if
|
|
||||||||||||||||
|
Return true if two
|
|
||||||||||||||||
|
Return true if two
|
|
||||||||||
|
Implicit cast of underlying
The function will compile only if ( 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
The function will compile only if (
This can safely be used for conversion down an inheritance hierarchy with polymorphic types only if |
|
||||||||||
|
Constant cast of underlying
This function will compile only if ( |
|
||||||||||||||||
|
Dynamic cast of underlying
|
|
||||||||||||||||||||||||||||
|
Set extra data associated with a
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 Preconditions:
Note, this function is made a non-member function to be consistent with the non-member |
|
||||||||||||||||
|
Get a const reference to extra data associated with a
Note, this function must be a non-member function since the client must manually select the first template argument. |
|
||||||||||||||||
|
Get a non-const reference to extra data associated with a
Note, this function must be a non-member function since the client must manually select the first template argument. |
|
||||||||||||||||
|
Get a pointer to const extra data (if it exists) associated with a
Postconditions:
Note, this function must be a non-member function since the client must manually select the first template argument. |
|
||||||||||||||||
|
Get a pointer to non-const extra data (if it exists) associated with a
Postconditions:
Note, this function must be a non-member function since the client must manually select the first template argument. |
|
||||||||||
|
Return a Preconditions:
|
|
||||||||||
|
Return a non- Preconditions:
|
|
||||||||||
|
Return a pointer to the underlying Preconditions:
Postconditions:
|
|
||||||||||
|
Return a pointer to the underlying non- Preconditions:
Postconditions:
|
|
||||||||||
|
Get a const reference to an embedded object that was set by calling
|
| ||||