37 #ifndef VIGRA_ERROR_HXX
38 #define VIGRA_ERROR_HXX
125 class ContractViolation :
public StdException
131 ContractViolation(
char const * prefix,
char const * message,
132 char const * file,
int line)
134 (*this) <<
"\n" << prefix <<
"\n" << message <<
"\n("
135 << file <<
":" << line <<
")\n";
138 ContractViolation(
char const * prefix,
char const * message)
140 (*this) <<
"\n" << prefix <<
"\n" << message <<
"\n";
143 ~ContractViolation() throw()
147 ContractViolation & operator<<(T
const & data)
149 std::ostringstream what;
155 virtual const char * what()
const throw()
159 return what_.c_str();
163 return "vigra::ContractViolation: error message was lost, sorry.";
171 class PreconditionViolation :
public ContractViolation
174 PreconditionViolation(
char const * message,
const char * file,
int line)
175 : ContractViolation(
"Precondition violation!", message, file, line)
178 PreconditionViolation(
char const * message)
179 : ContractViolation(
"Precondition violation!", message)
183 class PostconditionViolation :
public ContractViolation
186 PostconditionViolation(
char const * message,
const char * file,
int line)
187 : ContractViolation(
"Postcondition violation!", message, file, line)
190 PostconditionViolation(
char const * message)
191 : ContractViolation(
"Postcondition violation!", message)
195 class InvariantViolation :
public ContractViolation
198 InvariantViolation(
char const * message,
const char * file,
int line)
199 : ContractViolation(
"Invariant violation!", message, file, line)
202 InvariantViolation(
char const * message)
203 : ContractViolation(
"Invariant violation!", message)
210 void throw_invariant_error(
bool predicate,
char const * message,
char const * file,
int line)
213 throw vigra::InvariantViolation(message, file, line);
217 void throw_invariant_error(
bool predicate, std::string message,
char const * file,
int line)
220 throw vigra::InvariantViolation(message.c_str(), file, line);
224 void throw_precondition_error(
bool predicate,
char const * message,
char const * file,
int line)
227 throw vigra::PreconditionViolation(message, file, line);
231 void throw_precondition_error(
bool predicate, std::string message,
char const * file,
int line)
234 throw vigra::PreconditionViolation(message.c_str(), file, line);
238 void throw_postcondition_error(
bool predicate,
char const * message,
char const * file,
int line)
241 throw vigra::PostconditionViolation(message, file, line);
245 void throw_postcondition_error(
bool predicate, std::string message,
char const * file,
int line)
248 throw vigra::PostconditionViolation(message.c_str(), file, line);
252 void throw_runtime_error(
char const * message,
char const * file,
int line)
254 std::ostringstream what;
255 what <<
"\n" << message <<
"\n(" << file <<
":" << line <<
")\n";
256 throw std::runtime_error(what.str());
260 void throw_runtime_error(std::string message,
char const * file,
int line)
262 std::ostringstream what;
263 what <<
"\n" << message <<
"\n(" << file <<
":" << line <<
")\n";
264 throw std::runtime_error(what.str());
267 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
269 #define vigra_assert(PREDICATE, MESSAGE) vigra_precondition(PREDICATE, MESSAGE)
271 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
273 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE, __FILE__, __LINE__)
275 #define vigra_fail(MESSAGE) vigra::throw_runtime_error(MESSAGE, __FILE__, __LINE__)
280 void throw_invariant_error(
bool predicate,
char const * message)
283 throw vigra::InvariantViolation(message);
287 void throw_precondition_error(
bool predicate,
char const * message)
290 throw vigra::PreconditionViolation(message);
294 void throw_postcondition_error(
bool predicate,
char const * message)
297 throw vigra::PostconditionViolation(message);
301 void throw_invariant_error(
bool predicate, std::string message)
304 throw vigra::InvariantViolation(message.c_str());
308 void throw_precondition_error(
bool predicate, std::string message)
311 throw vigra::PreconditionViolation(message.c_str());
315 void throw_postcondition_error(
bool predicate, std::string message)
318 throw vigra::PostconditionViolation(message.c_str());
321 #define vigra_precondition(PREDICATE, MESSAGE) vigra::throw_precondition_error((PREDICATE), MESSAGE)
323 #define vigra_assert(PREDICATE, MESSAGE)
325 #define vigra_postcondition(PREDICATE, MESSAGE) vigra::throw_postcondition_error((PREDICATE), MESSAGE)
327 #define vigra_invariant(PREDICATE, MESSAGE) vigra::throw_invariant_error((PREDICATE), MESSAGE)
329 #define vigra_fail(MESSAGE) throw std::runtime_error(MESSAGE)
335 #endif // VIGRA_ERROR_HXX