User Manual, Developers Guide and API Documentation

Assure.hpp File Reference

#include <WNS/Exception.hpp>
#include <WNS/TypeInfo.hpp>
#include <cassert>
#include <sstream>
#include <iostream>
#include <string>
#include <csignal>

Include dependency graph for Assure.hpp:

Go to the source code of this file.

Namespaces

namespace  wns

Classes

class  wns::Assure
 Pseudo class grouping the assure functionality. More...
class  wns::Assure::Exception
 Thrown by assure. More...
class  wns::NullException
 Thrown by assureNotNull. More...

Defines

#define assure(expr, reason)
 An extended assert.
#define assureNotNull(pointer)
 Assure pointer is not NULL.
#define assureType(pointer, T)
 Assure "pointer" is of type T.
#define assureWithExceptionType(expr, reason, type)
 This assure allows to specifiy the exception type to be thrown.
#define WNS_ASSURE_THROWS_EXCEPTION   1


Define Documentation

#define assure ( expr,
reason   ) 

Value:

if (!(expr)) \
{ \
    std::stringstream s; \
    s.precision(10); \
    s<<reason; \
    wns::Assure::expression<wns::Assure::Exception>(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__, (s.str())); \
} \
else \
    void()
assure is very much like assert except for two differences:

1.) Additionally to the expression to be checked a reason in case the expression evaluates to "false" can be provided. Example:

 assure(container.find(x) != container.end(), "x is not contained in container");

2.) The assure is configurable at three levels:

  • Abort on error: It behaves like assert, the reason will NOT be visible
  • Don't check: The expression will not be evaluated and therefor not checked. This can e.g. be used when compiling optimized. Note, that it is therefor dangereous to use non-const (and even const) method of an object inside the assure expression as these may change the behaviour of the program in case you have the checks disabled.
The behavior of the macro must be configured at compile time. To throw on error you don't need to define anything - this is the defaul. To make assure behave like assert (abort on error) define WNS_ASSERT. To disable the checking define WNS_NDEBUG.

Note:
The WNS build system defines nothing if "dbg" is used as build flavour and defines WNS_NDEBUG if "opt" is used as build flavour.

Use WNS_ASSERT_ASSURE_EXCEPTION and WNS_ASSERT_ASSURE_NOT_NULL_EXCEPTION instead of CPPUNIT_ASSERT_THROW to prevent, that your test fails in opt flavours.

If you have other code, that depends on an exception thrown by an assure, enclose this code with an ifdef:

 #ifdef WNS_ASSURE_THROWS_EXCEPTION
         yourCode;
 #endif.

Definition at line 121 of file Assure.hpp.

#define assureNotNull ( pointer   ) 

Value:

assureWithExceptionType(NULL != pointer, \
               "Pointer " + std::string(#pointer) + " invalid (NULL)", wns::NullException)
Author:
Marc Schinnenburg <marc@schinnenburg.com>
This should be prefered over:
 assure (NULL != pointer);
since it adds additional information about the type of the pointer automatically.

Definition at line 175 of file Assure.hpp.

#define assureType ( pointer,
 ) 

Value:

assure(dynamic_cast<T>(pointer), \
       std::string(#pointer) + " is not of type " + wns::TypeInfo::create<T>().toString() + \
                   " but of Type: " + wns::TypeInfo::create(*pointer).toString()+"*")
Author:
Marc Schinnenburg <marc@schinnenburg.com>
 Baz* foo = getBaz();
 assureType(foo, Bar*);

Provides the following error message if pointer is NOT of type T:

foo is not of type wns::Bar* but of type wns::Baz* 

Definition at line 159 of file Assure.hpp.

#define assureWithExceptionType ( expr,
reason,
type   ) 

Value:

if (!(expr)) \
{ \
    std::stringstream s; \
    s.precision(10); \
    s<<reason; wns::Assure::expression<type>(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__, (s.str())); \
} else \
    void()

Definition at line 132 of file Assure.hpp.

#define WNS_ASSURE_THROWS_EXCEPTION   1

Definition at line 141 of file Assure.hpp.


Generated on Wed Feb 8 03:32:20 2012 for openWNS by  doxygen 1.5.5