diff options
author | Casey Carter <Casey@Carter.net> | 2017-02-08 02:59:37 +0300 |
---|---|---|
committer | Neil MacIntosh <neilmac@microsoft.com> | 2017-02-08 02:59:37 +0300 |
commit | 4e8f95b418ae96c802de57a2c041f16725a3d8ba (patch) | |
tree | cf4cae7cbd9f65a7968c01d9fbdc01968ece33d2 /include/gsl/gsl_assert | |
parent | 96eaf274f8c57829080100f7cd1d2e7744bae1ae (diff) |
Cleanup include structure, constexpr and noexcept compiler workarounds.
* Nest "gsl" directory inside a new "include" directory.
* Cleanup the _MSC_VER conditionals a bit; use constexpr on VS2017+.
* Don't #define noexcept on non-Microsoft implementations.
* Workaround VS2017 bug in multi_span. (Also implement == and != for static_bounds_dynamic_range_t because I'm an EoP semantic soundness snob.)
Fixes #441.
Diffstat (limited to 'include/gsl/gsl_assert')
-rw-r--r-- | include/gsl/gsl_assert | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/include/gsl/gsl_assert b/include/gsl/gsl_assert new file mode 100644 index 0000000..04f34b3 --- /dev/null +++ b/include/gsl/gsl_assert @@ -0,0 +1,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 |