mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
fbfdacb45f
This solution uses the approach of boost::asio to enabling usage of the library in environments where exception usage is either prohibited or not feasible (due to code size constraints). A function template gsl::throw_exception has been added, which in a normal environment just throws the exception. However, when GSL_TERMINATE_ON_CONTRACT_VIOLATION is defined the function is only declared by gsl and the definition of this function template must be supplied by the library's user. Closes: #468 Signed-off-by: Damian Jarek <damian.jarek93@gmail.com>
134 lines
3.9 KiB
Plaintext
134 lines
3.9 KiB
Plaintext
///////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// 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_ASSUME(cond)
|
|
//
|
|
// Tell the optimizer that the predicate cond must hold. It is unspecified
|
|
// whether or not cond is actually evaluated.
|
|
//
|
|
#ifdef _MSC_VER
|
|
#define GSL_ASSUME(cond) __assume(cond)
|
|
#elif defined(__clang__)
|
|
#define GSL_ASSUME(cond) __builtin_assume(cond)
|
|
#elif defined(__GNUC__)
|
|
#define GSL_ASSUME(cond) ((cond) ? static_cast<void>(0) : __builtin_unreachable())
|
|
#else
|
|
#define GSL_ASSUME(cond) static_cast<void>(!!(cond))
|
|
#endif
|
|
|
|
//
|
|
// GSL.assert: assertions
|
|
//
|
|
|
|
namespace gsl
|
|
{
|
|
struct fail_fast : public std::logic_error
|
|
{
|
|
explicit fail_fast(char const* const message) : std::logic_error(message) {}
|
|
};
|
|
namespace details
|
|
{
|
|
template <typename Exception>
|
|
[[noreturn]] void throw_exception(Exception&& exception);
|
|
|
|
} //namespace details
|
|
} //namespace gsl
|
|
|
|
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
|
|
|
|
namespace gsl
|
|
{
|
|
namespace details
|
|
{
|
|
template <typename Exception>
|
|
[[noreturn]] void throw_exception(Exception&& exception)
|
|
{
|
|
throw exception;
|
|
}
|
|
|
|
} //namespace details
|
|
} //namespace gsl
|
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) \
|
|
(GSL_LIKELY(cond) ? static_cast<void>(0) \
|
|
: gsl::throw_exception(gsl::fail_fast("GSL: " type " failure at " __FILE__ \
|
|
": " GSL_STRINGIFY(__LINE__))))
|
|
|
|
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
|
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) (GSL_LIKELY(cond) ? static_cast<void>(0) : std::terminate())
|
|
|
|
namespace gsl
|
|
{
|
|
namespace details
|
|
{
|
|
template <typename Exception>
|
|
[[noreturn]] void throw_exception(Exception&& exception)
|
|
{
|
|
static_cast<void>(exception);
|
|
std::terminate();
|
|
}
|
|
|
|
} //namespace details
|
|
} //namespace gsl
|
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) \
|
|
(GSL_LIKELY(cond) ? static_cast<void>(0) : std::terminate())
|
|
|
|
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
|
|
|
|
#define GSL_CONTRACT_CHECK(type, cond) GSL_ASSUME(cond)
|
|
|
|
#endif
|
|
|
|
#define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
|
|
#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
|
|
|
|
#endif // GSL_CONTRACTS_H
|