To help developers identify potential problems early in development, macros are provided to test for error conditions in functions (asserts) and objects (class invariants).
Casting is one well known source of hard-to-find errors. Casting discusses its use.
One method of catching errors early is to identify conditions that should be true at the beginning and end of functions, and raise errors if they are not.
Two mechanisms support this programming style.
asserts
class invariants
Two macros are supplied for asserting specific conditions in functions:
__ASSERT_ALWAYS
to catch run-time invalid input,
for both release and debug builds
__ASSERT_DEBUG
to catch programming errors, for
debug builds only
Class invariants are used to test that an object is in a valid state. They are used only in debug builds.
Define class invariants for non-trivial classes using
__DECLARE_TEST
. The class must supply functions that specify its
allowed stable states.
To ensures that the object is in a stable state prior to
executing the function, call the invariant at the start of all public functions
using __TEST_INVARIANT
.
For non-const functions, you can ensure that the object has been left in a stable state by also calling the invariant at the end of the function.
Casts, as in other operating systems, should be used with caution. If a cast seems to be needed, check that this does not reflect a design weakness.
The Symbian platform provides its own macros to encapsulate the C++ cast operators:
REINTERPRET_CAST
STATIC_CAST
CONST_CAST
MUTABLE_CAST
Prior to 6.0, GCC did not support the C++ casting operators, and these macros were, therefore, defined as simple C style casts for that compiler. These macros should have been used in preference to using the C++ operators explicitly.
From 6.0 onwards, GCC fully supports the C++ casting operators and the macros have been changed to reflect this in 6.1. This means that from 6.0 onwards, developers should use the native C++ casting operators rather than use the macros.
The more sophisticated C++ dynamic_cast
operator should not be used because EPOC does not enable or support Run Time Type Information (RTTI).