• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1///////////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4//
5// This code is licensed under the MIT License (MIT).
6//
7// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13// THE SOFTWARE.
14//
15///////////////////////////////////////////////////////////////////////////////
16
17#pragma once
18
19#ifndef GSL_CONTRACTS_H
20#define GSL_CONTRACTS_H
21
22#include <exception>
23#include <stdexcept>
24
25//
26// There are three configuration options for this GSL implementation's behavior
27// when pre/post conditions on the GSL types are violated:
28//
29// 1. GSL_TERMINATE_ON_CONTRACT_VIOLATION: std::terminate will be called (default)
30// 2. GSL_THROW_ON_CONTRACT_VIOLATION: a gsl::fail_fast exception will be thrown
31// 3. GSL_UNENFORCED_ON_CONTRACT_VIOLATION: nothing happens
32//
33#if !(defined(GSL_THROW_ON_CONTRACT_VIOLATION) ^ defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) ^    \
34      defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION))
35#define GSL_TERMINATE_ON_CONTRACT_VIOLATION
36#endif
37
38#define GSL_STRINGIFY_DETAIL(x) #x
39#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
40
41#if defined(__clang__) || defined(__GNUC__)
42#define GSL_LIKELY(x) __builtin_expect (!!(x), 1)
43#define GSL_UNLIKELY(x) __builtin_expect (!!(x), 0)
44#else
45#define GSL_LIKELY(x) (x)
46#define GSL_UNLIKELY(x) (x)
47#endif
48
49//
50// GSL.assert: assertions
51//
52
53namespace gsl
54{
55struct fail_fast : public std::runtime_error
56{
57    explicit fail_fast(char const* const message) : std::runtime_error(message) {}
58};
59}
60
61#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
62
63#define Expects(cond)                                                                              \
64    if (GSL_UNLIKELY(!(cond)))                                                                     \
65        throw gsl::fail_fast("GSL: Precondition failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__));
66#define Ensures(cond)                                                                              \
67    if (GSL_UNLIKELY(!(cond)))                                                                     \
68        throw gsl::fail_fast("GSL: Postcondition failure at " __FILE__                             \
69                             ": " GSL_STRINGIFY(__LINE__));
70
71#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
72
73#define Expects(cond)                                                                              \
74    if (GSL_UNLIKELY(!(cond))) std::terminate();
75#define Ensures(cond)                                                                              \
76    if (GSL_UNLIKELY(!(cond))) std::terminate();
77
78#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
79
80#define Expects(cond)
81#define Ensures(cond)
82
83#endif
84
85#endif // GSL_CONTRACTS_H
86