Flow123d  release_3.0.0-1212-g8801db3
Public Member Functions | Public Attributes | Protected Attributes | List of all members
feal::Assert Class Reference

Class defining debugging messages. More...

#include <asserts.hh>

Collaboration diagram for feal::Assert:
Collaboration graph
[legend]

Public Member Functions

 Assert (const std::string &expression)
 Constructor. More...
 
 Assert (const Assert &other)
 Copy constructor. More...
 
 ~Assert ()
 Destructor. More...
 
template<typename T >
Assertadd_value (T var_current_val, const char *var_name)
 Adds name and value of variable. More...
 
Assertset_context (const char *file_name, const char *function, const int line)
 Stores values for printing out line number, function, etc. More...
 
void error (std::string error_msg="")
 Generate error with given message. More...
 
void warning (std::string warning_msg="")
 Generate warning with given message. More...
 

Public Attributes

Assert_FEAL_ASSERT_A
 clever macro A More...
 
Assert_FEAL_ASSERT_B
 clever macro B More...
 

Protected Attributes

Exc_assert exception_
 Exception object. More...
 
bool thrown_
 Flag marked if Assert thrown error, warning, ... More...
 

Detailed Description

Class defining debugging messages.

Allows define assert, warning etc. either only for debug mode or for release mode also.

Definition of asserts is designed using macros FEAL_ASSERT and FEAL_ASSERT_DBG. First macro is used for both modes, second is only for debug. Definition allows to printout given variables too. For simplifying are designed shorter names of macros ASSERT and ASSERT_DBG, these names can be used if thea aren't in conflicts with external libraries.

Examples of usage:

1) We expect empty stings 's1' and 's2', if condition is not satisfied exception will be thrown. Condition is indicated in first parentheses, variables designed for printout follow as (s1)(s2). Each variable must be defined in separate parentheses. The last step is calling of the appropriate assert type, in this case error(). This assert is performed for debug and release mode.

std::string s1, s2;
...
FEAL_ASSERT(s1.empty() && s2.empty())(s1)(s2).error("Both strings must be empty!");

2) Parameter of error method is optional.

FEAL_ASSERT(s1.empty() && s2.empty())(s1)(s2).error();

3) This example is same as previous, but assert is performed only for debug mode.

FEAL_ASSERT_DBG(s1.empty() && s2.empty())(s1)(s2).error("Both strings must be empty!");

4) Example is same as case 1). Assert type error is called automatically if any other is not listed. This case is not recommended, rather use explicitly calling of error() method.

FEAL_ASSERT(s1.empty() && s2.empty())(s1)(s2);

5) Example with same condition as all previous but with other type - warning. Any exception is not thrown, only warning is printed. Parameter of warning method is optional.

FEAL_ASSERT(s1.empty() && s2.empty())(s1)(s2).warning("Both strings should be empty!");

For simplifying we have defined several macros for comparsion of values:

All macros allow easier declaration of assert. Following example shows declarations of same cases with usage of different macros:

ASSERT_LT( idx, arr.size() ).error("Index out of array!");
FEAL_ASSERT( idx < arr.size() )(idx)(arr.size()).error("Index out of array!");

Definition at line 120 of file asserts.hh.

Constructor & Destructor Documentation

feal::Assert::Assert ( const std::string &  expression)
inline

Constructor.

Definition at line 123 of file asserts.hh.

feal::Assert::Assert ( const Assert other)
inline

Copy constructor.

Definition at line 133 of file asserts.hh.

feal::Assert::~Assert ( )

Destructor.

Definition at line 69 of file asserts.cc.

Member Function Documentation

template<typename T >
Assert& feal::Assert::add_value ( var_current_val,
const char *  var_name 
)
inline

Adds name and value of variable.

Definition at line 147 of file asserts.hh.

void feal::Assert::error ( std::string  error_msg = "")

Generate error with given message.

Definition at line 88 of file asserts.cc.

Assert & feal::Assert::set_context ( const char *  file_name,
const char *  function,
const int  line 
)

Stores values for printing out line number, function, etc.

Definition at line 78 of file asserts.cc.

void feal::Assert::warning ( std::string  warning_msg = "")

Generate warning with given message.

Definition at line 96 of file asserts.cc.

Member Data Documentation

Assert& feal::Assert::_FEAL_ASSERT_A

clever macro A

Definition at line 142 of file asserts.hh.

Assert& feal::Assert::_FEAL_ASSERT_B

clever macro B

Definition at line 143 of file asserts.hh.

Exc_assert feal::Assert::exception_
protected

Exception object.

Definition at line 166 of file asserts.hh.

bool feal::Assert::thrown_
protected

Flag marked if Assert thrown error, warning, ...

Definition at line 167 of file asserts.hh.


The documentation for this class was generated from the following files: