code::DerivedSmartPtr< BaseType, DerivedType > Class Template Reference

Inheritance diagram for code::DerivedSmartPtr< BaseType, DerivedType >:

code::SmartPtr< BaseType > List of all members.

Detailed Description

template<class BaseType, class DerivedType>
class code::DerivedSmartPtr< BaseType, DerivedType >

Makes it possible to return a derived type pointer when a base type pointer is required (usually by a virtual function signature).

All the functions are overloaded to return the derived type.

See also:
SmartPtr

InitSmartPtr

DerivedSmartPtr

InitDerivedSmartPtr

Author:
: Lakshman :
Id
code_DerivedSmartPtr.h,v 1.9 2009/06/02 20:17:39 lakshman Exp


Public Member Functions

 DerivedSmartPtr ()
 Default constructor, creates an invalid pointer; Do not dereference objects created using the default constructor.
 DerivedSmartPtr (DerivedType *p)
 Think of a DerivedSmartPtr as holding a pointer to DerivedType, i.e.
 DerivedSmartPtr (SmartPtr< DerivedType > orig)
 Think of a DerivedSmartPtr as holding a pointer to DerivedType, i.e.
 DerivedSmartPtr (SmartPtr< BaseType > orig)
 Think of a DerivedSmartPtr as holding a pointer to DerivedType, i.e.
SmartPtr< DerivedType > getDerivedType ()
 To obtain a derived object pointer.
bool operator< (const DerivedSmartPtr< BaseType, DerivedType > &other) const
 To enable easy mapping of SmartPtrs to STL containers, we overload the < operator.
DerivedType & operator * () const
 Like a regular pointer; Asserts to make sure the pointer is not NULL.
DerivedType * operator-> () const
 Like a regular pointer; Asserts to make sure the pointer is not NULL.
bool is_unique () const
 Is the pointed-to-object unique?
int copies () const
 Returns number of references to this SmartPtr.
bool is_ptr_valid () const
 Is the pointer that we are holding valid?
SmartPtr< const BaseType > const_ptr () const
 Will return a reference-counted pointer to const X, so that functions that promise not to change the object can ask for, and receive, SmartPtr<const X>.
bool operator< (const SmartPtr< BaseType > &other) const
 To enable easy mapping of SmartPtrs of ordered objects to STL containers, the < operator is overloaded to compare the objects being pointed to.
bool operator== (const SmartPtr< BaseType > &other) const
 To enable easy mapping of SmartPtrs of ordered objects to STL containers, the == operator is overloaded to compare the objects being pointed to.
bool is_stored_before (const SmartPtr< BaseType > &other) const
 In some cases, it makes no sense for the objects themselves to be ordered; yet, you might want simply unique instances.
SmartPtr< D > recast ()
 Recast this SmartPtr to the desired type.
SmartPtr< const D > recast () const

Public Attributes

BaseType * ptr
 Internal pointer storage.
int * numCopies
 Internal number of copies storage.


Constructor & Destructor Documentation

template<class BaseType, class DerivedType>
code::DerivedSmartPtr< BaseType, DerivedType >::DerivedSmartPtr (  )  [inline]

Default constructor, creates an invalid pointer; Do not dereference objects created using the default constructor.

template<class BaseType, class DerivedType>
code::DerivedSmartPtr< BaseType, DerivedType >::DerivedSmartPtr ( DerivedType *  p  )  [inline]

Think of a DerivedSmartPtr as holding a pointer to DerivedType, i.e.

as a SmartPtr< DerivedType >

A DerivedType* is a BaseType* also.

It is implemented as a subclass of BaseType to make virtual overloading possible.

template<class BaseType, class DerivedType>
code::DerivedSmartPtr< BaseType, DerivedType >::DerivedSmartPtr ( SmartPtr< DerivedType >  orig  )  [inline]

Think of a DerivedSmartPtr as holding a pointer to DerivedType, i.e.

as a SmartPtr< DerivedType >

A DerivedType* is a BaseType* also.

It is implemented as a subclass of BaseType to make virtual overloading possible.

template<class BaseType, class DerivedType>
code::DerivedSmartPtr< BaseType, DerivedType >::DerivedSmartPtr ( SmartPtr< BaseType >  orig  )  [inline]

Think of a DerivedSmartPtr as holding a pointer to DerivedType, i.e.

as a SmartPtr< DerivedType >

The dynamic downcast to DerivedType is done on access.


Member Function Documentation

SmartPtr<const BaseType > code::SmartPtr< BaseType >::const_ptr (  )  const [inline, inherited]

Will return a reference-counted pointer to const X, so that functions that promise not to change the object can ask for, and receive, SmartPtr<const X>.

int code::SmartPtr< BaseType >::copies (  )  const [inline, inherited]

Returns number of references to this SmartPtr.

template<class BaseType, class DerivedType>
SmartPtr< DerivedType > code::DerivedSmartPtr< BaseType, DerivedType >::getDerivedType (  )  [inline]

To obtain a derived object pointer.

This is faster than keeping around the DerivedSmartPtr object.

bool code::SmartPtr< BaseType >::is_ptr_valid (  )  const [inline, inherited]

Is the pointer that we are holding valid?

Note that this is a test only on whether the pointer is valid. There is no test of whether the object is valid; Usually objects that require initialization, etc. will have a isValid() member function. To prevent typographical errors, the name of this function is different. Just because the pointer is valid doesn't mean that the object is valid.

Use this function to check the return value from a function that promises to return a SmartPtr to something. The SmartPtr will hold an invalid pointer on error.

Returns:
true if pointer is valid (no error) and false if the pointer is invalid (error).

Reimplemented in code::InitSmartPtr< BaseType >.

bool code::SmartPtr< BaseType >::is_stored_before ( const SmartPtr< BaseType > &  other  )  const [inline, inherited]

In some cases, it makes no sense for the objects themselves to be ordered; yet, you might want simply unique instances.

In that case, pointers need to be compared using is_stored_before()

When the objects themselves are ordered, you should be using the < operator.

bool code::SmartPtr< BaseType >::is_unique (  )  const [inline, inherited]

Is the pointed-to-object unique?

You can use this function to determine whether there is anybody else holding references to this object.

Returns:
true if the pointed-to-object is unique and false if there are one or more other modules/classes holding references to this object.

template<class BaseType, class DerivedType>
DerivedType& code::DerivedSmartPtr< BaseType, DerivedType >::operator * (  )  const [inline]

Like a regular pointer; Asserts to make sure the pointer is not NULL.

Reimplemented from code::SmartPtr< BaseType >.

template<class BaseType, class DerivedType>
DerivedType* code::DerivedSmartPtr< BaseType, DerivedType >::operator-> (  )  const [inline]

Like a regular pointer; Asserts to make sure the pointer is not NULL.

Reimplemented from code::SmartPtr< BaseType >.

bool code::SmartPtr< BaseType >::operator< ( const SmartPtr< BaseType > &  other  )  const [inline, inherited]

To enable easy mapping of SmartPtrs of ordered objects to STL containers, the < operator is overloaded to compare the objects being pointed to.

Hence, for this to be usable, the class X should have a < operator defined.

The behavior of the < and == operators is as follows:

  1. when neither pointer is valid, == would return true.
  2. when only one pointer is valid, the invalid pointer is always less than the valid pointer.
  3. when both pointers are valid, the objects pointed to are compared.

In some cases, it makes no sense for the objects themselves to be ordered; yet, you might want simply unique instances. In that case, pointers need to be compared. Use is_stored_before()

template<class BaseType, class DerivedType>
bool code::DerivedSmartPtr< BaseType, DerivedType >::operator< ( const DerivedSmartPtr< BaseType, DerivedType > &  other  )  const [inline]

To enable easy mapping of SmartPtrs to STL containers, we overload the < operator.

The meaning of < is to compare the objects being pointed to. Hence, for this to be usable, the class X should have a < operator defined.

bool code::SmartPtr< BaseType >::operator== ( const SmartPtr< BaseType > &  other  )  const [inline, inherited]

To enable easy mapping of SmartPtrs of ordered objects to STL containers, the == operator is overloaded to compare the objects being pointed to.

Hence, for this to be usable, the class X should have a == operator defined.

The behavior of the < and == operators is as follows:

  1. when neither pointer is valid, == would return true.
  2. when only one pointer is valid, the invalid pointer is always less than the valid pointer.
  3. when both pointers are valid, the objects pointed to are compared.

In some cases, it makes no sense for the objects themselves to be ordered; yet, you might want simply unique instances. In that case, pointers need to be compared. Use is_stored_before()

SmartPtr<const D> code::SmartPtr< BaseType >::recast (  )  const [inline, inherited]

SmartPtr<D> code::SmartPtr< BaseType >::recast (  )  [inline, inherited]

Recast this SmartPtr to the desired type.

An empty SmartPtr is returned if type conversion fails.

class A { ... }; class B: public A { ... }; class C: public B { ... }; class Unrelated { ... };

SmartPtr b = new C (); SmartPtr a = b.recast<A>(); // upcast SmartPtr<C> c = b.recast<C>(); // downcast assert (a.copies() == 3);

SmartPtr<Unrelated> u = b.recast<Unrelated>(); assert (!u.is_ptr_valid()); assert (a.copies() == 3);


Member Data Documentation

int* code::SmartPtr< BaseType >::numCopies [inherited]

Internal number of copies storage.

DerivedSmartPtr has access.

BaseType * code::SmartPtr< BaseType >::ptr [inherited]

Internal pointer storage.

DerivedSmartPtr has access.


Generated on Fri May 4 13:40:13 2012 for WDSS-IIw2 by  doxygen 1.4.7