BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/ers/include/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 "ers/Issue.h"
18#include <stdio.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 {
33 protected:
34 virtual std::string build_message( const char* condition_text, const std::string& msg,
35 bool constant_expression = false ) throw();
36 Assertion( const Context& context, severity_t s );
37 void setup( const char* condition, const std::string& message, bool constant_expression );
38 static const char* const MESSAGE_ELEMENTS[];
39
40 public:
41 static const char* const ASSERT_CONDITION_KEY;
42 static const char* const CLASS_NAME;
43
44 virtual const char* get_class_name() const throw();
45 Assertion();
46 Assertion( const Context& context, severity_t s, const char* condition,
47 const std::string& message, bool constant_expression = false );
48 };
49
50 /** This structure is simply used to trigger compile-time assertion errors
51 * The \c true template instanciation is implemented, but not the false, this means that if
52 * the template is instanciated with \c false, we get a compile-time error. \brief compile
53 * time error structure.
54 */
55
56 template <bool> struct Compile_time_error;
57
58 /** \brief compile time error structure */
59
60 template <> struct Compile_time_error<true> {};
61
62} // namespace ers
63
64/** \def ERS_STATIC_ASSERT(expr) This macro inserts a compile-time assertion into the code.
65 * Compile time assertion can only be done on constant factors (i.e compile-time known
66 * quantities).
67 *
68 */
69
70#ifndef N_ERS_STATIC_ASSERT
71# define ERS_STATIC_ASSERT( expr ) \
72 { \
73 ers::Compile_time_error<( ( expr ) != 0 )> ERROR_ASSERTION_FAILED; \
74 (void)ERROR_ASSERTION_FAILED; \
75 }
76#else
77# define ERS_STATIC_ASSERT( expr )
78#endif
79
80/** \def ERS_ASSERT(expr,msg,...) This macro inserts an assertion than checks condition e,
81 * if e is not true, then an issue of type ers::Asertion is thrown with message msg.
82 * The msg is actually a formatting string, like printf, that can be used with subsequent
83 * parameters. If the compiler is gcc, then the transient field of the assertion is set
84 * according to the transcience of the expression. This means that if the expression is
85 * detected by the compiler as being constant, \note This macro is disabled if the \c
86 * N_ERS_ASSERT macro is defined
87 */
88#ifndef N_ERS_ASSERT
89# ifdef __GNUC__
90# define ERS_ASSERT( expr, ... ) \
91 { \
92 if ( !( expr ) ) \
93 { \
94 char assertion_buffer[256]; \
95 snprintf( assertion_buffer, sizeof( assertion_buffer ), __VA_ARGS__ ); \
96 ers::Assertion failed_assertion( ERS_HERE, ers::error, #expr, assertion_buffer, \
97 __builtin_constant_p( expr ) ); \
98 throw failed_assertion; \
99 } \
100 }
101# else
102# define ERS_ASSERT( expr, ... ) \
103 { \
104 if ( !( expr ) ) \
105 { \
106 char assertion_buffer[256]; \
107 snprintf( assertion_buffer, sizeof( assertion_buffer ), __VA_ARGS__ ); \
108 ers::Assertion failed_assertion( ERS_HERE, ers::error, #expr, assertion_buffer, \
109 false ); \
110 throw failed_assertion; \
111 } \
112 }
113# endif
114#else
115# define ERS_ASSERT( expr, ... ) ( (void)( expr ) )
116#endif
117
118#endif
XmlRpcServer s
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)
Assertion(const Context &context, severity_t s)
Source context for Issue.
Root Issue class.
const std::string & message() const
Message.
enum ers::_severity_t severity_t
compile time error structure.