#include <Teuchos_RefCountPtrDecl.hpp>
Public Types | |
| typedef T | element_type |
| | |
Public Member Functions | |
| RefCountPtr (ENull null_arg=null) | |
Initialize RefCountPtr<T> to NULL. | |
| RefCountPtr (const RefCountPtr< T > &r_ptr) | |
Initialize from another RefCountPtr<T> object. | |
| template<class T2> | |
| RefCountPtr (const RefCountPtr< T2 > &r_ptr) | |
Initialize from another RefCountPtr<T2> object (implicit conversion only). | |
| ~RefCountPtr () | |
| Removes a reference to a dynamically allocated object and possibly deletes the object if owned. | |
| RefCountPtr< T > & | operator= (const RefCountPtr< T > &r_ptr) |
| Copy the pointer to the referenced object and increment the reference count. | |
| 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 * | release () |
| Release the ownership of the underlying dynamically allocated object. | |
| int | count () const |
Return the number of RefCountPtr<> objects that have a reference to the underlying pointer that is being shared. | |
| void | set_has_ownership () |
Give this and other RefCountPtr<> 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. | |
| template<class T2> | |
| bool | shares_resource (const RefCountPtr< T2 > &r_ptr) const |
| Returns true if the smart pointers share the same underlying reference-counted object. | |
| const RefCountPtr< T > & | assert_not_null () const |
Throws std::logic_error if this->get()==NULL, otherwise returns reference to *this. | |
Related Functions | |
| (Note that these are not member functions.) | |
| RefCountPtr< T > | rcp (T *p, bool owns_mem=true) |
Create a RefCountPtr object properly typed. | |
| RefCountPtr< T > | rcp (T *p, Dealloc_T dealloc, bool owns_mem) |
| Initialize from a raw pointer with a deallocation policy. | |
| bool | is_null (const RefCountPtr< T > &p) |
Returns true if p.get()==NULL. | |
| bool | operator== (const RefCountPtr< T > &p, ENull) |
Returns true if p.get()==NULL. | |
| bool | operator!= (const RefCountPtr< T > &p, ENull) |
Returns true if p.get()!=NULL. | |
| bool | operator== (const RefCountPtr< T1 > &p1, const RefCountPtr< T2 > &p2) |
Return true if two RefCountPtr objects point to the same referenced-counted object and have the same node. | |
| bool | operator!= (const RefCountPtr< T1 > &p1, const RefCountPtr< T2 > &p2) |
Return true if two RefCountPtr objects do not point to the same referenced-counted object and have the same node. | |
| RefCountPtr< T2 > | rcp_implicit_cast (const RefCountPtr< T1 > &p1) |
Implicit cast of underlying RefCountPtr type from T1* to T2*. | |
| RefCountPtr< T2 > | rcp_static_cast (const RefCountPtr< T1 > &p1) |
Static cast of underlying RefCountPtr type from T1* to T2*. | |
| RefCountPtr< T2 > | rcp_const_cast (const RefCountPtr< T1 > &p1) |
Constant cast of underlying RefCountPtr type from T1* to T2*. | |
| RefCountPtr< T2 > | rcp_dynamic_cast (const RefCountPtr< T1 > &p1, bool throw_on_fail=false) |
Dynamic cast of underlying RefCountPtr type from T1* to T2*. | |
| void | set_extra_data (const T1 &extra_data, const std::string &name, RefCountPtr< T2 > *p, EPrePostDestruction destroy_when=POST_DESTROY, bool force_unique=true) |
Set extra data associated with a RefCountPtr object. | |
| T1 & | get_extra_data (RefCountPtr< T2 > &p, const std::string &name) |
Get a non-const reference to extra data associated with a RefCountPtr object. | |
| const T1 & | get_extra_data (const RefCountPtr< T2 > &p, const std::string &name) |
Get a const reference to extra data associated with a RefCountPtr object. | |
| T1 * | get_optional_extra_data (RefCountPtr< T2 > &p, const std::string &name) |
Get a pointer to non-const extra data (if it exists) associated with a RefCountPtr object. | |
| const T1 * | get_optional_extra_data (const RefCountPtr< T2 > &p, const std::string &name) |
Get a pointer to const extra data (if it exists) associated with a RefCountPtr object. | |
| Dealloc_T & | get_dealloc (RefCountPtr< T > &p) |
Return a non-const reference to the underlying deallocator object. | |
| const Dealloc_T & | get_dealloc (const RefCountPtr< T > &p) |
Return a const reference to the underlying deallocator object. | |
| Dealloc_T * | get_optional_dealloc (RefCountPtr< T > &p) |
Return a pointer to the underlying non-const deallocator object if it exists. | |
| const Dealloc_T * | get_optional_dealloc (const RefCountPtr< T > &p) |
Return a pointer to the underlying const deallocator object if it exists. | |
| std::ostream & | operator<< (std::ostream &out, const RefCountPtr< 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 RefCountPtr
Here we present a short, but fairly comprehensive, quick-start for the use of RefCountPtr<>. The use cases described here should cover the overwhelming majority of the use instances of RefCountPtr<> 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_REFCOUNTPTR_ASSERT_NONNULL, or TEUCHOS_DEBUG is defined at compile time).
RefCountPtr<> objects
RefCountPtr<> object using new
RefCountPtr<C> c_ptr = rcp(new C);
RefCountPtr<> object to an array allocated using new[n] : Teuchos::DeallocArrayDelete
RefCountPtr<C> c_ptr = rcp(new C[n],DeallocArrayDelete<C>(),true);
RefCountPtr<> object equipped with a specialized deallocator function : Teuchos::DeallocFunctorDelete
void someDeallocFunction(C* c_ptr); RefCountPtr<C> c_ptr = rcp(new deallocFunctorDelete<C>(someDeallocFunction),true);
RefCountPtr<> object to NULL
RefCountPtr<C> c_ptr;
RefCountPtr<C> c_ptr = null;
RefCountPtr<> object to an object {not} allocated with new
C c; RefCountPtr<C> c_ptr = rcp(&c,false);
RefCountPtr<C> c_ptr = rcp(new C); // No cast RefCountPtr<A> a_ptr = c_ptr; // Cast to base class RefCountPtr<const A> ca_ptr = a_ptr; // Cast from non-const to const
RefCountPtr<C> c_ptr;
const RefCountPtr<C> c_ptr;
RefCountPtr<const C> c_ptr;
const RefCountPtr<const C> c_ptr;
RefCountPtr<> objects (using assignment operator)
RefCountPtr<A> a_ptr; a_ptr = rcp(new C());
RefCountPtr<A> a_ptr = rcp(new C()); a_ptr = null; // The C object will be deleted here
RefCountPtr<> object
RefCountPtr<A> a_ptr1; RefCountPtr<A> a_ptr2 = rcp(new C()); a_ptr1 = a_ptr2; // Now a_ptr1 and a_ptr2 point to same C object
Teuchos::RefCountPtr::operator*()
C &c_ref = *c_ptr;
NULL) : Teuchos::RefCountPtr::get()
C *c_rptr = c_ptr.get();
NULL) : Teuchos::RefCountPtr::operator*()
C *c_rptr = &*c_ptr;
Teuchos::RefCountPtr::operator->()
c_ptr->f();
Teuchos::RefCountPtr::get(), Teuchos::operator==(), Teuchos::operator!=()
if( a_ptr.get() ) 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.get() ) 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";
RefCountPtr<C> c_ptr = rcp(new C); // No cast RefCountPtr<A> a_ptr = rcp_implicit_cast<A>(c_ptr); // To base RefCountPtr<const A> ca_ptr = rcp_implicit_cast<const A>(a_ptr);// To const
const : rcp_const_cast()
RefCountPtr<const A> ca_ptr = rcp(new C); RefCountPtr<A> a_ptr = rcp_const_cast<A>(ca_ptr); // cast away const!
rcp_static_cast()
RefCountPtr<D> d_ptr = rcp(new E); RefCountPtr<E> e_ptr = rcp_static_cast<E>(d_ptr); // Unchecked, unsafe?
rcp_dynamic_cast()
RefCountPtr<A> a_ptr = rcp(new C); RefCountPtr<B1> b1_ptr = rcp_dynamic_cast<B1>(a_ptr); // Checked, safe! RefCountPtr<B2> b2_ptr = rcp_dynamic_cast<B2>(b1_ptr); // Checked, safe! RefCountPtr<C> c_ptr = rcp_dynamic_cast<C>(b2_ptr); // Checked, safe!
rcp_dynamic_cast()
RefCountPtr<A> a_ptr1 = rcp(new C); RefCountPtr<A> a_ptr2 = rcp(new A); RefCountPtr<B1> b1_ptr1 = rcp_dynamic_cast<B1>(a_ptr1,true); // Success! RefCountPtr<B1> b1_ptr2 = rcp_dynamic_cast<B1>(a_ptr2,true); // Throw std::bad_cast!
RefCountPtr<> object with a custom deallocator : Teuchos::DeallocArrayDelete
RefCountPtr<C> c_ptr = rcp(new C[N],DeallocArrayDelete<C>(),true);
Teuchos::get_dealloc()
const DeallocArrayDelete<C>
&dealloc = get_dealloc<DeallocArrayDelete<C> >(c_ptr);
Teuchos::get_optional_dealloc()
const DeallocArrayDelete<C> *dealloc = get_optional_dealloc<DeallocArrayDelete<C> >(c_ptr); if(dealloc) std::cout << "This deallocator exits!\n";
Teuchos::set_extra_data()
set_extra_data(rcp(new B1),"A:B1",&a_ptr);
Teuchos::get_extra_data()
set_extra_data(rcp(new B1),"A:B1",&a_ptr,PRE_DESTORY);
Teuchos::get_extra_data()
get_extra_data<RefCountPtr<B1> >(a_ptr,"A:B1")->f();
Teuchos::get_extra_data()
get_extra_data<RefCountPtr<B1> >(a_ptr,"A:B1") = rcp(new C);
Teuchos::get_optional_extra_data()
const RefCountPtr<B1> *b1 = get_optional_extra_data<RefCountPtr<B1> >(a_ptr,"A:B1"); if(b1) (*b1)->f();
Definition at line 399 of file Teuchos_RefCountPtrDecl.hpp.
|
|||||
|
Definition at line 402 of file Teuchos_RefCountPtrDecl.hpp. |
|
||||||||||
|
Initialize This allows clients to write code like: RefCountPtr<int> p = null; RefCountPtr<int> p; NULL
Definition at line 198 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
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:
Definition at line 205 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||
|
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:
Definition at line 214 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Removes a reference to a dynamically allocated object and possibly deletes the object if owned.
Deletes the object if Definition at line 223 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Copy the pointer to the referenced object and increment the reference count.
If Postconditons:
Definition at line 236 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Pointer ( Preconditions:
Definition at line 254 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Dereference the underlying object. Preconditions:
Definition at line 263 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Get the raw C++ pointer to the underlying object.
Definition at line 272 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Release the ownership of the underlying dynamically allocated object.
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:
Definition at line 278 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Return the number of
Definition at line 286 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Give
See ~RefCountPtr() above. This function does nothing if Postconditions:
Definition at line 294 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Returns true if See ~RefCountPtr() above.
Definition at line 301 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||
|
Returns true if the smart pointers share the same underlying reference-counted object.
This method does more than just check if Definition at line 310 of file Teuchos_RefCountPtr.hpp. |
|
|||||||||
|
Throws
Definition at line 319 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
Create a
If the pointer |
|
||||||||||||||||||||
|
Initialize from a raw pointer with a deallocation policy.
Postconditions:
By default, |
|
||||||||||
|
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 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 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.
Also note that this const version is a false sense of security since a client can always copy a const |
|
||||||||||||||||
|
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. |
|
||||||||||||||||
|
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.
Also note that this const version is a false sense of security since a client can always copy a const |
|
||||||||||
|
Return a non- Preconditions:
|
|
||||||||||
|
Return a Preconditions:
Note that the |
|
||||||||||
|
Return a pointer to the underlying non- Preconditions:
Postconditions:
|
|
||||||||||
|
Return a pointer to the underlying Preconditions:
Postconditions:
Note that the |
|
||||||||||||||||
|
Output stream inserter. The implementation of this function just print pointer addresses and therefore puts not restrictions on the data types involved. |
1.3.9.1