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 > |
| More... | |
Enumerations | |
| enum | Teuchos::ENull |
Used to initialize a RefCountPtr object to NULL using an implicit conversion! More... | |
| enum | Teuchos::EPrePostDestruction |
| 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 T> | |
| bool | Teuchos::is_null (const RefCountPtr< T > &p) |
Returns true if p.get()==NULL. | |
| template<class T> | |
| bool | Teuchos::operator== (const RefCountPtr< T > &p, ENull) |
Returns true if p.get()==NULL. | |
| template<class T> | |
| bool | Teuchos::operator!= (const RefCountPtr< T > &p, ENull) |
Returns true if p.get()!=NULL. | |
| template<class T1, class T2> | |
| bool | Teuchos::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. | |
| template<class T1, class T2> | |
| bool | Teuchos::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. | |
| 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 T1, class T2> | |
| T1 * | Teuchos::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. | |
| template<class T1, class T2> | |
| const T1 * | Teuchos::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. | |
| 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. | |
| template<class Dealloc_T, class T> | |
| Dealloc_T * | Teuchos::get_optional_dealloc (RefCountPtr< T > &p) |
Return a pointer to the underlying non-const deallocator object if it exists. | |
| template<class Dealloc_T, class T> | |
| const Dealloc_T * | Teuchos::get_optional_dealloc (const RefCountPtr< T > &p) |
Return a pointer to the underlying const deallocator object if it exists. | |
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 _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 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();
|
|
Used to initialize a
Definition at line 396 of file Teuchos_RefCountPtrDecl.hpp. |
|
|
Used to specify a pre or post destruction of extra data.
Definition at line 399 of file Teuchos_RefCountPtrDecl.hpp. |
|
||||||||||||||||
|
Create a
If the pointer Definition at line 321 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||||||
|
Initialize from a raw pointer with a deallocation policy.
Postconditions:
By default, Definition at line 329 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Returns true if
Definition at line 336 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
Returns true if
Definition at line 343 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
Returns true if
Definition at line 350 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
Return true if two
Definition at line 357 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
Return true if two
Definition at line 364 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 372 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 387 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Constant cast of underlying
This function will compile only if ( Definition at line 402 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
Dynamic cast of underlying
Definition at line 417 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 437 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 445 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 453 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
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. Definition at line 461 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||||||||
|
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 Definition at line 471 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Return a non- Preconditions:
Definition at line 482 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Return a Preconditions:
Note that the Definition at line 499 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Return a pointer to the underlying non- Preconditions:
Postconditions:
Definition at line 507 of file Teuchos_RefCountPtr.hpp. |
|
||||||||||
|
Return a pointer to the underlying Preconditions:
Postconditions:
Note that the Definition at line 520 of file Teuchos_RefCountPtr.hpp. |
1.3.9.1