#include <CppCastingHelper.h>
Inheritance diagram for Patternist::CppCastingHelper< TSubClass >:

Public Member Functions | |
| template<typename TCastTarget> | |
| TCastTarget * | as () |
| template<typename TCastTarget> | |
| const TCastTarget * | as () const |
Protected Member Functions | |
| CppCastingHelper () | |
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:
dynamic_cast to verify that the static casts are properly done, such that sensible error messages are given when the casts are invalid. It also traps invalid casts which nevertheless happen to work on a particular platform/compiler/hardware architecture.CppCastingHelper is a template class where the TSubClass parameter must be the class inheriting CppCastingHelper. See Item or Expression for demonstration.
Definition at line 54 of file CppCastingHelper.h.
| 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.
| 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.
| 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.
1.5.1