BOSS 7.0.2
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/ers/ers-00-00-03/ers/Assertion.h
Go to the documentation of this file.
1/*
2 * Assertion.h
3 * ers
4 *
5 * Created by Matthias Wiesmann on 26.11.04.
6 * Copyright 2004 CERN. All rights reserved.
7 *
8 */
9
10/** \file Assertion.h
11 * This file defines the assertion class and the associated macros.
12 */
13
14#ifndef ERS_ASSERTION
15#define ERS_ASSERTION
16
17#include <stdio.h>
18#include "ers/Issue.h"
19
20namespace ers {
21
22/** This class represents an assertion in the code, it is an issue with two fields:
23 * \li A condition
24 * \li A message describing the condition
25 *
26 * If the condition is not verified, an Issue is trown.
27 * \author Matthias Wiesmann
28 * \version 1.0
29 * \brief This Issue represents a basic assertion
30 */
31
32 class Assertion: public ers::Issue {
33protected:
34 virtual std::string build_message(const char* condition_text, const std::string &msg, bool constant_expression=false) throw() ;
35 Assertion(const Context &context, severity_t s);
36 void setup(const char *condition, const std::string &message, bool constant_expression);
37 static const char* const MESSAGE_ELEMENTS[] ;
38public:
39 static const char* const ASSERT_CONDITION_KEY ;
40 static const char* const CLASS_NAME ;
41
42 virtual const char*get_class_name() const throw() ;
43 Assertion();
44 Assertion(const Context &context, severity_t s, const char*condition, const std::string &message, bool constant_expression=false) ;
45} ;
46
47/** This structure is simply used to trigger compile-time assertion errors
48 * The \c true template instanciation is implemented, but not the false, this means that if the template
49 * is instanciated with \c false, we get a compile-time error.
50 * \brief compile time error structure.
51 */
52
53template <bool> struct Compile_time_error ;
54
55/** \brief compile time error structure */
56
57template<> struct Compile_time_error<true> { };
58
59} // ers
60
61
62
63/** \def ERS_STATIC_ASSERT(expr) This macro inserts a compile-time assertion into the code.
64 * Compile time assertion can only be done on constant factors (i.e compile-time known quantities).
65 *
66 */
67
68#ifndef N_ERS_STATIC_ASSERT
69#define ERS_STATIC_ASSERT(expr) { ers::Compile_time_error <((expr) != 0)> ERROR_ASSERTION_FAILED ; (void) ERROR_ASSERTION_FAILED ; }
70#else
71#define ERS_STATIC_ASSERT(expr)
72#endif
73
74
75/** \def ERS_ASSERT(expr,msg,...) This macro inserts an assertion than checks condition e,
76 * if e is not true, then an issue of type ers::Asertion is thrown with message msg.
77 * The msg is actually a formatting string, like printf, that can be used with subsequent parameters.
78 * If the compiler is gcc, then the transient field of the assertion is set according to the transcience of the expression.
79 * This means that if the expression is detected by the compiler as being constant,
80 * \note This macro is disabled if the \c N_ERS_ASSERT macro is defined
81 */
82#ifndef N_ERS_ASSERT
83#ifdef __GNUC__
84#define ERS_ASSERT(expr,...) { if(!(expr)) { char assertion_buffer[256] ; snprintf(assertion_buffer,sizeof(assertion_buffer), __VA_ARGS__) ; ers::Assertion failed_assertion(ERS_HERE, ers::error, #expr,assertion_buffer,__builtin_constant_p(expr)) ; throw failed_assertion ; } }
85#else
86#define ERS_ASSERT(expr,...) { if(!(expr)) { char assertion_buffer[256] ; snprintf(assertion_buffer,sizeof(assertion_buffer), __VA_ARGS__) ; ers::Assertion failed_assertion(ERS_HERE, ers::error,#expr,assertion_buffer,false) ; throw failed_assertion ; } }
87#endif
88#else
89#define ERS_ASSERT(expr,...) ((void) (expr))
90#endif
91
92#endif
93
XmlRpcServer s
Definition: HelloServer.cpp:11
This Issue represents a basic assertion.
static const char *const MESSAGE_ELEMENTS[]
static const char *const CLASS_NAME
static const char *const ASSERT_CONDITION_KEY
void setup(const char *condition, const std::string &message, bool constant_expression)
virtual const char * get_class_name() const
Get key for class (used for serialisation)
virtual std::string build_message(const char *condition_text, const std::string &msg, bool constant_expression=false)
Source context for Issue.
const std::string & message() const
Message.
enum ers::_severity_t severity_t