senfassert.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_senfassert_
18 #define HH_SENF_Utils_senfassert_ 1
19 
20 // Custom includes
21 
22 //#include "senfassert.mpp"
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 
25 #ifndef SENF_DEBUG
26 
27 # include "IgnoreValue.hh"
28 # define SENF_ASSERT(x, comment)
29 # define SENF_ASSERT_EXPRESSION(expression, comment) senf::IGNORE( expression )
30 
31 #else
32 
33 #ifndef SENF_ASSERT_EXCEPTION
34 
35 # include <cassert>
36 # define SENF_ASSERT(x, comment) assert((x) && comment)
37 # define SENF_ASSERT_EXPRESSION(expression, comment) assert((expression) && comment)
38 
39 #else
40 
41 # include "Exception.hh"
42  namespace senf {
43  struct AssertException : public Exception
44  {
45  AssertException(std::string const & descr) {
46  (*this) << "Assertion failed at " << __FILE__ << ":" << __LINE__ << ": " << descr;
47  }
48  };
49  }
50 
51 # define SENF_ASSERT(x, comment) if (!(x)) throw senf::AssertException(comment);
52 # define SENF_ASSERT_EXPRESSION(expression, comment) if (!(expression)) throw senf::AssertException(comment);
53 
54 #endif
55 #endif
56 
57 
58 /* Lifted direct from:
59  Modern C++ Design: Generic Programming and Design Patterns Applied Section 2.1
60  by Andrei Alexandrescu
61 */
62 namespace senf {
63 namespace detail {
64  template<bool> class compile_time_check
65  {
66  public:
68  };
69 
70  template<> class compile_time_check<false>
71  {
72  };
73 }}
74 
75  /*
76  SENF_STATIC_ASSERT is only in operation when SENF_DEBUG is defined. It will test its first
77  argument at compile time and on failure report the error message of the second argument,
78  which must be a valid c++ classname. i.e. no spaces, punctuation or reserved keywords.
79  */
80 #ifdef SENF_DEBUG
81 # define SENF_STATIC_ASSERT(expr, msg) \
82  do { \
83  struct STATIC_ASSERT_FAILED_##msg {}; \
84  typedef senf::detail::compile_time_check< (expr) != 0 > tmplimpl; \
85  tmplimpl aTemp = tmplimpl(STATIC_ASSERT_FAILED_##msg()); \
86  (void)sizeof(aTemp); \
87  } while (0)
88 #else
89 # define SENF_STATIC_ASSERT(expr, msg)
90 #endif
91 
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 //#include "senfassert.cci"
94 //#include "senfassert.ct"
95 //#include "senfassert.cti"
96 #endif
97 
98 
99 // Local Variables:
100 // mode: c++
101 // fill-column: 100
102 // comment-column: 40
103 // c-file-style: "senf"
104 // indent-tabs-mode: nil
105 // ispell-local-dictionary: "american"
106 // compile-command: "scons -u test"
107 // End:
IgnoreValue public header.
Exception public header.