Commit 5e300f34 authored by Gauthier Quesnel's avatar Gauthier Quesnel
Browse files

assert!

parent dc059b1a
Pipeline #218 passed with stage
in 50 seconds
/* Copyright (C) 2018 INRA
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef ORG_VLEPROJECT_BITS_ASSERT_HPP
#define ORG_VLEPROJECT_BITS_ASSERT_HPP
#include <exception>
#include <cstdio>
#define stringify_detail(x) #x
#define stringify(x) vle_stringify_detail(x)
#if defined(__clang__) || defined(__GNUC__)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (!!(x))
#define unlikely(x) (!!(x))
#endif
//
// In internal API, we prefer abort application when precondition,
// postcondition or assertion fail. For external API, user can select to use
// exception or return with message in terminal.
//
namespace bits {
namespace details {
void
fail_fast(const char* type,
const char* cond,
const char* file,
const char* line)
{
std::fprintf(stderr, "%s [%s] failure at %s: %s\n", type, cond, file, line);
std::terminate();
}
} // namespace details
} // namespace bits
#define BITS_CONTRACT_CHECK(type, cond) \
(likely(cond) ? static_cast<void>(0) \
: bits::details::fail_fast(type, cond, __FILE__, stringify(__LINE__)
#ifdef NDEBUG
#define expects(cond)
#define ensures(cond)
#define assert(cond)
#else
#define expects(cond) BITS_CONTRACT_CHECK("Precondition", cond)
#define ensures(cond) BITS_CONTRACT_CHECK("Postcondition", cond)
#define assert(cond) BITS_CONTRACT_CHECK("Assertion", cond)
#endif
#endif
\ No newline at end of file
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <tuple> #include <tuple>
#include <vector> #include <vector>
#include <bits/assert.hpp>
#include <bits/getopt.hpp> #include <bits/getopt.hpp>
#include <bits/tagged-union.hpp> #include <bits/tagged-union.hpp>
#include <bits/unit-test.hpp> #include <bits/unit-test.hpp>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment