Patternist::CppCastingHelper< TSubClass > Class Template Reference

Provides convenience methods for performing static casts between C++ classes. More...

#include <CppCastingHelper.h>

Inheritance diagram for Patternist::CppCastingHelper< TSubClass >:

Inheritance graph
[legend]
List of all members.

Public Member Functions

template<typename TCastTarget>
TCastTarget * as ()
template<typename TCastTarget>
const TCastTarget * as () const

Protected Member Functions

 CppCastingHelper ()

Detailed Description

template<typename TSubClass>
class Patternist::CppCastingHelper< TSubClass >

Provides convenience methods for performing static casts between C++ classes.

In Patternist, it is very common to do up-casts from Expression or Item, which typically involves writing messy code. Such an old-way cast looks like this:

 static_cast<const MyClass *>(myInstance.get())->myClassMember()

CppCastingHelper provides the convenience method as() for this, which is functionally equivalent to the above code, but simpler:

 myInstance->as<MyClass>()->myClassMember()

The as() function performs a static cast.

By using CppCastingHelper, this is achieved:

CppCastingHelper is a template class where the TSubClass parameter must be the class inheriting CppCastingHelper. See Item or Expression for demonstration.

Author:
Frans Englich <frans.englich@telia.com>

Definition at line 54 of file CppCastingHelper.h.


Constructor & Destructor Documentation

template<typename TSubClass>
Patternist::CppCastingHelper< TSubClass >::CppCastingHelper (  )  [inline, protected]

This constructor is protected because this class must be sub-classed.

Definition at line 104 of file CppCastingHelper.h.


Member Function Documentation

template<typename TSubClass>
template<typename TCastTarget>
TCastTarget* Patternist::CppCastingHelper< TSubClass >::as (  )  [inline]

Casts this instance to:

 TCastTarget *

and returns the result.

When compiled in debug mode, a dynamic_cast is attempted, in order to check the correctness of the cast.

Definition at line 92 of file CppCastingHelper.h.

template<typename TSubClass>
template<typename TCastTarget>
const TCastTarget* Patternist::CppCastingHelper< TSubClass >::as (  )  const [inline]

Casts this instance to:

 const TCastTarget *

and returns the result.

When compiled in debug mode, this function perform a dynamic_cast, in order to produce an informative message.

Definition at line 71 of file CppCastingHelper.h.


The documentation for this class was generated from the following file:
Generated on Thu Feb 8 14:54:32 2007 for Patternist by  doxygen 1.5.1