00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef METALIC_CONTRACT_HH
00029 # define METALIC_CONTRACT_HH
00030
00031 # include <mlc/config/system.hh>
00032
00033 # ifdef NDEBUG
00034
00035 # define assertion(expr) ((void) 0)
00036 # define invariant(expr) ((void) 0)
00037 # define precondition(expr) ((void) 0)
00038 # define postcondition(expr) ((void) 0)
00039
00040 # else // NDEBUG
00041
00042 # include <iostream>
00043 # include <cstdlib>
00044 # include <sstream>
00045 # include <stdexcept>
00046 # include <cassert>
00047
00048 namespace mlc
00049 {
00050
00051 # ifdef OLN_EXCEPTIONS
00052
00053
00054
00055
00056
00057
00058 inline void FailedCondition__( const char* condType,
00059 const char* condText,
00060 const char* fileName
00061 # ifndef RUNNING_TESTS
00062 ,int fileLine
00063 # endif
00064 )
00065 {
00066
00067 std::ostringstream err;
00068
00069 err << fileName << ':'
00070 # ifndef RUNNING_TESTS
00071 << fileLine
00072 # endif
00073 << ": "
00074 << condType << " `"
00075 << condText << "' failed.";
00076 # ifndef RUNNING_TESTS
00077 throw std::runtime_error(err.str());
00078 # else
00079 std::cerr << err.str();
00080 exit(1);
00081 # endif
00082 }
00083
00084 # else // no OLN_EXCEPTIONS
00085
00086 inline void FailedCondition__( const char* condType,
00087 const char* condText,
00088 const char* fileName
00089 # ifndef RUNNING_TESTS
00090 ,int fileLine
00091 # endif
00092 )
00093 {
00094
00095 std::cerr << fileName << ':'
00096 # ifndef RUNNING_TESTS
00097 << fileLine
00098 # endif
00099 << ": "
00100 << condType << " `"
00101 << condText << "' failed." << std::endl;
00102 # ifndef RUNNING_TESTS
00103 abort();
00104 # else
00105 exit(1);
00106 # endif
00107 }
00108
00109 # endif // OLN_EXCEPTIONS
00110
00111 }
00112
00113 # ifndef RUNNING_TESTS
00114 # define TestCondition__(condType,expr) \
00115 ((void) ((expr) ? 0 : (::mlc::FailedCondition__( #condType, #expr, \
00116 __FILE__, __LINE__ ), 0)))
00117 # else
00118 # define TestCondition__(condType,expr) \
00119 ((void) ((expr) ? 0 : (::mlc::FailedCondition__( #condType, #expr, \
00120 __FILE__ ), 0)))
00121 # endif
00122
00123
00124
00125
00126
00127 # ifdef OLN_EXCEPTIONS
00128 # define assertion(expr) TestCondition__(Assertion,expr)
00129 # define invariant(expr) TestCondition__(Invariant,expr)
00130 # define precondition(expr) TestCondition__(Precondition,expr)
00131 # define postcondition(expr) TestCondition__(Postcondition,expr)
00132 # else
00133 # define assertion(expr) assert(expr)
00134 # define invariant(expr) assert(expr)
00135 # define precondition(expr) assert(expr)
00136 # define postcondition(expr) assert(expr)
00137 # endif
00138
00139 # endif // NDEBUG
00140
00141 #endif // ! METALIC_CONTRACT_HH