Unravel Engine C++ Reference
Loading...
Searching...
No Matches
assert.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifndef CONTRACTS_H
4#define CONTRACTS_H
5
6#include <exception>
7#include <stdexcept>
8
9//
10// There are three configuration options for this implementation's behavior
11// when pre/post conditions on the types are violated:
12//
13// 1. TERMINATE_ON_CONTRACT_VIOLATION: std::terminate will be called (default)
14// 2. THROW_ON_CONTRACT_VIOLATION: a std::runtime_error exception will be thrown
15// 3. UNENFORCED_ON_CONTRACT_VIOLATION: nothing happens
16//
17#define THROW_ON_CONTRACT_VIOLATION
18
19#if !(defined(THROW_ON_CONTRACT_VIOLATION) ^ defined(TERMINATE_ON_CONTRACT_VIOLATION) ^ \
20 defined(UNENFORCED_ON_CONTRACT_VIOLATION))
21#define TERMINATE_ON_CONTRACT_VIOLATION
22#endif
23
24#define STRINGIFY_DETAIL(x) #x
25#define STRINGIFY(x) STRINGIFY_DETAIL(x)
26
27#if defined(THROW_ON_CONTRACT_VIOLATION)
28
29#define expects(cond) \
30 if(!(cond)) \
31 throw std::runtime_error("Precondition failure at " __FILE__ ": " STRINGIFY(__LINE__))
32#define ensures(cond) \
33 if(!(cond)) \
34 throw std::runtime_error("Postcondition failure at " __FILE__ ": " STRINGIFY(__LINE__))
35
36#elif defined(TERMINATE_ON_CONTRACT_VIOLATION)
37
38#define expects(cond) \
39 if(!(cond)) \
40 std::terminate()
41#define ensures(cond) \
42 if(!(cond)) \
43 std::terminate()
44
45#elif defined(UNENFORCED_ON_CONTRACT_VIOLATION)
46
47#define expects(cond)
48#define ensures(cond)
49
50#endif
51
52#endif // CONTRACTS_H