44#ifndef LASS_GUARDIAN_OF_INCLUSION_UTIL_IMPL_ENFORCER_IMPL_H
45#define LASS_GUARDIAN_OF_INCLUSION_UTIL_IMPL_ENFORCER_IMPL_H
75 template <
typename T,
typename C>
76 static bool LASS_CALL wrong(
const T& t,
const C& )
95 template <
typename T,
typename C>
96 static bool LASS_CALL wrong(
const T& t,
const C& closure)
98 return !(t == closure);
115 template <
typename T,
typename C>
116 static bool LASS_CALL wrong(
const T& t,
const C& closure)
135 template <
typename T,
typename C>
136 static bool LASS_CALL wrong(
const T& t,
const C& closure)
154 template <
typename T,
typename C>
155 static bool LASS_CALL wrong(
const T& t,
const C& closure)
157 return t < C(0) || !(t < closure);
159 template <
typename C>
160 static bool LASS_CALL wrong(
size_t t,
const C& closure)
162 return !(t < closure);
177 template <
typename T,
typename C>
178 static bool LASS_CALL wrong(
const T& stream,
const C& )
180 return stream.fail();
187#ifdef LASS_HAS_GETLASTERROR
196struct WinAPIPredicate
198 template <
typename T,
typename C>
199 static bool LASS_CALL wrong(
const T& returnCode,
const C& )
201 return returnCode == 0 && lass_GetLastError() != 0;
222 template <
typename T,
typename C>
223 static void raise(
const T&,
const C&,
const std::string& message,
const char* locus)
238inline void raiserAddMessage(std::ostream& stream,
const std::string& message)
240 if (!message.empty())
242 stream <<
":\n" << message;
253 template <
typename T,
typename C>
254 static void raise(
const T& result,
const C&,
const std::string& message,
const char* locus)
256 std::ostringstream buffer;
257 buffer <<
"Expression " << locus <<
" resulted in a non-zero value '" << result <<
"'";
258 raiserAddMessage(buffer, message);
272 template <
typename T,
typename C>
273 static void raise(
const T&,
const C&,
const std::string& message,
const char* locus)
277 std::ostringstream buffer;
278 buffer <<
"Function call " << locus <<
" failed with: " <<
lass_strerror(errnum);
279 raiserAddMessage(buffer, message);
293 template <
typename T,
typename C>
294 static void raise(
const T&,
const C&,
const std::string& message,
const char* locus)
297 std::ostringstream buffer;
298 std::cerr <<
"[LASS RUN MSG] UNDEFINED BEHAVIOUR: "
299 <<
"Function call " << locus <<
" failed with errno: ("
301 raiserAddMessage(std::cerr, message);
302 std::cerr <<
".\n" << std::flush;
315 template <
typename T,
typename C>
316 static void raise(
const T& rc,
const C&,
const std::string& message,
const char* locus)
319 std::ostringstream buffer;
320 buffer <<
"Function call " << locus <<
" failed with return code: ("
322 raiserAddMessage(buffer, message);
336 template <
typename T,
typename C>
337 static void raise(
const T& rc,
const C&,
const std::string& message,
const char* locus)
339 std::cerr <<
"[LASS RUN MSG] UNDEFINED BEHAVIOUR: "
340 <<
"Function call " << locus <<
" failed with return code: ("
342 raiserAddMessage(std::cerr, message);
343 std::cerr << std::endl << std::flush;
355 template <
typename T,
typename C>
356 static void raise(
const T& hResult,
const C&,
const std::string& message,
const char* locus)
358 std::ostringstream buffer;
359 buffer <<
"Failure HRESULT '" << hResult <<
"' returned by " << locus;
360 raiserAddMessage(buffer, message);
367#ifdef LASS_HAS_GETLASTERROR
374struct LastErrorRaiser
376 template <
typename T,
typename C>
377 static void raise(
const T&,
const C&,
const std::string& message,
const char* locus)
379 std::ostringstream buffer;
380 const unsigned lastError = lass_GetLastError();
381 buffer <<
"Function call " << locus <<
" failed with last-error: ("
382 << lastError <<
") " << lass_FormatMessage(lastError);
383 raiserAddMessage(buffer, message);
393struct LastErrorWarner
395 template <
typename T,
typename C>
396 static void raise(
const T&,
const C&,
const std::string& message,
const char* locus)
398 const unsigned lastError = lass_GetLastError();
399 std::cerr <<
"Function call " << locus <<
" failed with last-error: ("
400 << lastError <<
") " << lass_FormatMessage(lastError);
401 raiserAddMessage(std::cerr, message);
402 std::cerr << std::endl << std::flush;
416 template <
typename T,
typename C>
417 static void raise(
const T& index,
const C& size,
const std::string& message,
const char* locus)
419 std::ostringstream buffer;
420 buffer <<
"Value '" << index <<
"' is out of range [0, " << size <<
") in '"
422 raiserAddMessage(buffer, message);
442 typename PredicateType,
450 Enforcer(T t, ClosureType closure,
const char* locus):
453 locus_(PredicateType::wrong(t, closure) ? locus : 0)
457 std::cerr <<
"[LASS RUN MSG] UNDEFINED BEHAVIOUR WARNING: "
458 "Enforcer did not get a locus!\n" << std::flush;
466 RaiserType::raise(t_, closure_, msg_, locus_);
471 template <
class MsgType>
472 Enforcer& operator()(
const MsgType& msg)
477 std::ostringstream ss;
487 ClosureType closure_;
489 const char*
const locus_;
492#ifndef LASS_ENFORCER_USE_AMBIGUITY_WORKAROUND
493# if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_GCC && LASS_COMPILER_VERSION < 40000
494# define LASS_ENFORCER_USE_AMBIGUITY_WORKAROUND
495# elif LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_SUNPRO
496# define LASS_ENFORCER_USE_AMBIGUITY_WORKAROUND
500#ifdef LASS_ENFORCER_USE_AMBIGUITY_WORKAROUND
502template <
typename PredicateType,
typename RaiserType>
505 template <
typename T,
typename C>
inline
507 T& t,
const C& closure,
const char* locus)
513 template <
typename T,
typename C>
inline
514 static Enforcer<PredicateType, RaiserType, const T&, const C&> make(
515 const T& t,
const C& closure,
const char* locus)
517 return Enforcer<PredicateType, RaiserType, const T&, const C&>(
522#define LASS_UTIL_IMPL_MAKE_ENFORCER(predicate, raiser, t, closure, locus)\
523 ::lass::util::impl::EnforcerMaker< predicate, raiser >::make(t, closure, locus)
537template <
typename PredicateType,
typename RaiserType,
typename T,
typename C>
539 T& t,
const C& closure,
const char* locus)
545template <
typename PredicateType,
typename RaiserType,
typename T,
typename C>
546inline Enforcer<PredicateType, RaiserType, const T&, const C&> makeEnforcer(
547 const T& t,
const C& closure,
const char* locus)
549 return Enforcer<PredicateType, RaiserType, const T&, const C&>(
553#define LASS_UTIL_IMPL_MAKE_ENFORCER(predicate, raiser, t, closure, locus)\
554 ::lass::util::impl::makeEnforcer< predicate, raiser >(t, closure, locus)
Exception thrown by enforcers.
Helper class of the enforce macro's.
Enforcer< PredicateType, RaiserType, T &, const C & > makeEnforcer(T &t, const C &closure, const char *locus)
helper function to create enforcers
const std::string lass_strerror(int errnum)
returns message associated to an CLIB error code
int lass_errno()
returns CLIB errno
general utility, debug facilities, ...
Library for Assembled Shared Sources.
Throws a run time exception for raisng LASS_ENFORCE_CLIB.
Throws a run time exception for raisng LASS_ENFORCE_CLIB_RC.
Throws a run time exception for raisng LASS_ENFORCE_CLIB_RC.
Prints warning to std::cerr for LASS_WARN_CLIB.
Throws an run time exception for raising LASS_ENFORCE_COM.
value must be equal to closure
value must be greater than or equal to closure
value must be in range [0, closure)
Throw a range error for LASS_ENFORCE_INDEX.
Predicate to enforce a stream to be a in good state.
Predicate for enforcers using operator!
value must be different than closure
Throws an run time exception for raising LASS_ENFORCE_ZERO.