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_ASSUME(cond) 51// 52// Tell the optimizer that the predicate cond must hold. It is unspecified 53// whether or not cond is actually evaluated. 54// 55#ifdef _MSC_VER 56#define GSL_ASSUME(cond) __assume(cond) 57#elif defined(__clang__) 58#define GSL_ASSUME(cond) __builtin_assume(cond) 59#elif defined(__GNUC__) 60#define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable()) 61#else 62#define GSL_ASSUME(cond) static_cast<void>(!!(cond)) 63#endif 64 65// 66// GSL.assert: assertions 67// 68 69namespace gsl 70{ 71struct fail_fast : public std::logic_error 72{ 73 explicit fail_fast(char const* const message) : std::logic_error(message) {} 74}; 75} 76 77#if defined(GSL_THROW_ON_CONTRACT_VIOLATION) 78 79#define GSL_CONTRACT_CHECK(type, cond) \ 80 (GSL_LIKELY(cond) ? static_cast<void>(0) \ 81 : throw gsl::fail_fast("GSL: " type " failure at " __FILE__ \ 82 ": " GSL_STRINGIFY(__LINE__))) 83 84#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) 85 86#define GSL_CONTRACT_CHECK(type, cond) (GSL_LIKELY(cond) ? static_cast<void>(0) : std::terminate()) 87 88#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION) 89 90#define GSL_CONTRACT_CHECK(type, cond) GSL_ASSUME(cond) 91 92#endif 93 94#define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond) 95#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond) 96 97#endif // GSL_CONTRACTS_H 98