The Sparta Modeling Framework
Loading...
Searching...
No Matches
SpartaAssert.hpp
Go to the documentation of this file.
1// <SpartaAssert.hpp> -*- C++ -*-
2
3#pragma once
4
5#include <exception>
6#include <string>
7#include <iostream>
8#include <sstream>
10
11
27#define SPARTA_EXPECT_FALSE(x) __builtin_expect(!!(x), false)
28
38#define SPARTA_EXPECT_TRUE(x) __builtin_expect(!!(x), true)
39
40#ifndef DO_NOT_DOCUMENT
41
42#define ADD_FILE_INFORMATION(ex, file, line) \
43 ex << ": in file: '" << file << "', on line: " << std::dec << line;
44
45#define SPARTA_THROW_EXCEPTION(reason, file, line) \
46 sparta::SpartaException ex(reason); \
47 ADD_FILE_INFORMATION(ex, file, line) \
48 throw ex;
49
50#define SPARTA_ABORT(reason, file, line) \
51 std::stringstream msg(reason); \
52 ADD_FILE_INFORMATION(msg, file, line) \
53 std::cerr << msg.str() << std::endl; \
54 std::terminate();
55
56#define sparta_assert1(e) \
57 if(__builtin_expect(!(e), 0)) { SPARTA_THROW_EXCEPTION(#e, __FILE__, __LINE__) }
58
59#define sparta_assert2(e, insertions) \
60 if(__builtin_expect(!(e), 0)) { sparta::SpartaException ex(std::string(#e) + ": " ); \
61 ex << insertions; \
62 ADD_FILE_INFORMATION(ex, __FILE__, __LINE__); \
63 throw ex; }
64
65#define sparta_throw(message) \
66 { \
67 std::stringstream msg; \
68 msg << message; \
69 sparta::SpartaException ex(std::string("abort: ") + msg.str()); \
70 ADD_FILE_INFORMATION(ex, __FILE__, __LINE__); \
71 throw ex; \
72 }
73
74#define sparta_abort1(e) \
75 if(__builtin_expect(!(e), 0)) { SPARTA_ABORT(#e, __FILE__, __LINE__) }
76
77#define sparta_abort2(e, insertions) \
78 if(__builtin_expect(!(e), 0)) { std::stringstream msg(std::string(#e) + ": " ); \
79 msg << insertions; \
80 ADD_FILE_INFORMATION(msg, __FILE__, __LINE__); \
81 std::cerr << msg.str() << std::endl; \
82 std::terminate(); }
83
84#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
85#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
86#define sparta_assert_impl2(count, ...) sparta_assert##count(__VA_ARGS__)
87#define sparta_assert_impl(count, ...) sparta_assert_impl2(count, __VA_ARGS__)
88#define sparta_abort_impl2(count, ...) sparta_abort##count(__VA_ARGS__)
89#define sparta_abort_impl(count, ...) sparta_abort_impl2(count, __VA_ARGS__)
90
91// DO_NOT_DOCUMENT
92#endif
93
94#pragma GCC diagnostic push
95#pragma GCC diagnostic ignored "-Wvariadic-macros"
114#define sparta_assert(...) sparta_assert_impl(VA_NARGS(__VA_ARGS__), __VA_ARGS__)
115
116#pragma GCC diagnostic pop
117
123#define sparta_assert_errno(_cond) \
124 sparta_assert(_cond, std::string(std::strerror(errno)))
125
145#define sparta_abort(...) sparta_abort_impl(VA_NARGS(__VA_ARGS__), __VA_ARGS__)
Exception class for all of Sparta.