Classes | |
| class | Teuchos::DeallocDelete< T > |
Deallocator for new which calls delete. More... | |
| class | Teuchos::DeallocArrayDelete< T > |
Deallocator for new [] which calls delete []. More... | |
| class | Teuchos::RefCountPtr< T > |
| Templated class for reference counted smart pointers (see description). More... | |
Enumerations | |
| enum | Teuchos::ENull { Teuchos::null } |
Used to initialize a RefCountPtr object to NULL using an implicit conversion! More... | |
| enum | Teuchos::EPrePostDestruction { Teuchos::PRE_DESTROY, Teuchos::POST_DESTROY } |
| Used to specify a pre or post destruction of extra data. More... | |
Functions | |
| template<class T> | |
| RefCountPtr< T > | Teuchos::rcp (T *p, bool owns_mem=true) |
Create a RefCountPtr object properly typed. | |
| template<class T, class Dealloc_T> | |
| RefCountPtr< T > | Teuchos::rcp (T *p, Dealloc_T dealloc, bool owns_mem) |
| Initialize from a raw pointer with a deallocation policy. | |
| template<class T2, class T1> | |
| RefCountPtr< T2 > | Teuchos::rcp_implicit_cast (const RefCountPtr< T1 > &p1) |
Implicit cast of underlying RefCountPtr type from T1* to T2*. | |
| template<class T2, class T1> | |
| RefCountPtr< T2 > | Teuchos::rcp_static_cast (const RefCountPtr< T1 > &p1) |
Static cast of underlying RefCountPtr type from T1* to T2*. | |
| template<class T2, class T1> | |
| RefCountPtr< T2 > | Teuchos::rcp_const_cast (const RefCountPtr< T1 > &p1) |
Constant cast of underlying RefCountPtr type from T1* to T2*. | |
| template<class T2, class T1> | |
| RefCountPtr< T2 > | Teuchos::rcp_dynamic_cast (const RefCountPtr< T1 > &p1, bool throw_on_fail=false) |
Dynamic cast of underlying RefCountPtr type from T1* to T2*. | |
| template<class T1, class T2> | |
| void | Teuchos::set_extra_data (const T1 &extra_data, const std::string &name, RefCountPtr< T2 > *p, bool force_unique=true, EPrePostDestruction destroy_when=POST_DESTROY) |
Set extra data associated with a RefCountPtr object. | |
| template<class T1, class T2> | |
| T1 & | Teuchos::get_extra_data (RefCountPtr< T2 > &p, const std::string &name) |
Get a non-const reference to extra data associated with a RefCountPtr object. | |
| template<class T1, class T2> | |
| const T1 & | Teuchos::get_extra_data (const RefCountPtr< T2 > &p, const std::string &name) |
Get a const reference to extra data associated with a RefCountPtr object. | |
| template<class Dealloc_T, class T> | |
| Dealloc_T & | Teuchos::get_dealloc (RefCountPtr< T > &p) |
Return a non-const reference to the underlying deallocator object. | |
| template<class Dealloc_T, class T> | |
| const Dealloc_T & | Teuchos::get_dealloc (const RefCountPtr< T > &p) |
Return a const reference to the underlying deallocator object. | |
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(){} A& operator=(const 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 _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]
RefCountPtr<C> c_ptr = rcp(new C[n],DeallocArrayDelete<C>(),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;
const RefCountPtr<C> c_ptr;
RefCountPtr<const C> c_ptr;
const RefCountPtr<const C> c_ptr;
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<> 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
RefCountPtr::operator*()
C &c_ref = *c_ptr;
NULL) : RefCountPtr::get()
C *c_rptr = c_ptr.get();
NULL) : RefCountPtr::operator*()
C *c_rptr = &*c_ptr;
RefCountPtr::operator->()
c_ptr->f();
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!
set_extra_data()
set_extra_data(rcp(new B1),"A:B1",&a_ptr);
get_extra_data()
set_extra_data(rcp(new B1),"A:B1",&a_ptr,PRE_DESTORY);
get_extra_data()
get_extra_data<RefCountPtr<B1> >(a_ptr,"A:B1")->f();
get_extra_data()
get_extra_data<RefCountPtr<B1> >(a_ptr,"A:B1") = rcp(new C);
|
|
Used to initialize a
Definition at line 315 of file Teuchos_RefCountPtrDecl.hpp. |
|
|
Used to specify a pre or post destruction of extra data.
Definition at line 318 of file Teuchos_RefCountPtrDecl.hpp. |
|
||||||||||||||||
|
Create a
If the pointer
Definition at line 314 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||||||
|
Initialize from a raw pointer with a deallocation policy.
Postconditions:
By default, Definition at line 322 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
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++. Definition at line 330 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
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 Definition at line 345 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Constant cast of underlying
This function will compile only if ( Definition at line 360 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
Dynamic cast of underlying
Definition at line 375 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||||||||||||||
|
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 Definition at line 395 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
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. Definition at line 403 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
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 Definition at line 411 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Return a non- Preconditions:
Definition at line 420 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Return a Preconditions:
Note that the Definition at line 437 of file Teuchos_RefCountPtr.hpp. |
1.3.9.1