aboutsummaryrefslogtreecommitdiffstats
path: root/gsl/gsl_assert
blob: 6d8760d71ba8cb239faf619f49f3b2d7c56e4f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// 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.
//
///////////////////////////////////////////////////////////////////////////////

#pragma once

#ifndef GSL_CONTRACTS_H
#define GSL_CONTRACTS_H

#include <exception>
#include <stdexcept>

//
// There are three configuration options for this GSL implementation's behavior
// when pre/post conditions on the GSL types are violated:
//
// 1. GSL_TERMINATE_ON_CONTRACT_VIOLATION: std::terminate will be called (default)
// 2. GSL_THROW_ON_CONTRACT_VIOLATION: a gsl::fail_fast exception will be thrown
// 3. GSL_UNENFORCED_ON_CONTRACT_VIOLATION: nothing happens
//
#if !(defined(GSL_THROW_ON_CONTRACT_VIOLATION) ^ defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) ^    \
      defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION))
#define GSL_TERMINATE_ON_CONTRACT_VIOLATION
#endif

#define GSL_STRINGIFY_DETAIL(x) #x
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)

#if defined(__clang__) || defined(__GNUC__)
#define GSL_LIKELY(x) __builtin_expect (!!(x), 1)
#define GSL_UNLIKELY(x) __builtin_expect (!!(x), 0)
#else
#define GSL_LIKELY(x) (x)
#define GSL_UNLIKELY(x) (x)
#endif

//
// GSL.assert: assertions
//

namespace gsl
{
struct fail_fast : public std::runtime_error
{
    explicit fail_fast(char const* const message) : std::runtime_error(message) {}
};
}

#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)

#define Expects(cond)                                                                              \
    if (GSL_UNLIKELY(!(cond)))                                                                     \
        throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__));
#define Ensures(cond)                                                                              \
    if (GSL_UNLIKELY(!(cond)))                                                                     \
        throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__                             \
                             ": " GSL_STRINGIFY(__LINE__));

#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)

#define Expects(cond)                                                                              \
    if (GSL_UNLIKELY(!(cond))) std::terminate();
#define Ensures(cond)                                                                              \
    if (GSL_UNLIKELY(!(cond))) std::terminate();

#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)

#define Expects(cond)
#define Ensures(cond)

#endif

#endif // GSL_CONTRACTS_H