refactor: contract support library header fles are no longer included in compat_macros.h

This commit is contained in:
Mateusz Pusz
2024-06-10 22:00:46 +02:00
parent 7fbc52dc93
commit ed53cf03f0
13 changed files with 79 additions and 5 deletions

View File

@@ -157,3 +157,23 @@ from additional features provided with the library).
This macro resolves to either the `std` or `fmt` namespace, depending on the value of This macro resolves to either the `std` or `fmt` namespace, depending on the value of
[MP_UNITS_API_STD_FORMAT](../../getting_started/installation_and_usage.md#MP_UNITS_API_STD_FORMAT) [MP_UNITS_API_STD_FORMAT](../../getting_started/installation_and_usage.md#MP_UNITS_API_STD_FORMAT)
CMake option. CMake option.
### Contracts
The mp-units library internally does contract checking by default. It can be disabled with a Conan
or CMake option. However, when enabled, it can use either [gsl-lite](https://github.com/gsl-lite/gsl-lite)
or [ms-gsl](https://github.com/microsoft/GSL). To write a code that is independent from the
underlying framework, the following preprocessor macros are exposed:
- `MP_UNITS_EXPECTS(expr)`
- `MP_UNITS_EXPECTS_DEBUG(expr)`
- `MP_UNITS_ASSERT(expr)`
- `MP_UNITS_ASSERT_DEBUG(expr)`
Their meaning is consistent with respective [gsl-lite](https://github.com/gsl-lite/gsl-lite?tab=readme-ov-file#contract-checking-configuration-macros).
Also, to include the header files of the underlying framework, the following include should be used:
```cpp
#include <mp-units/ext/contracts.h>
```

View File

@@ -24,6 +24,7 @@
#include <mp-units/bits/hacks.h> #include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export #include <compare> // IWYU pragma: export
#include <ostream> #include <ostream>
#include <utility> #include <utility>

View File

@@ -44,6 +44,7 @@ add_mp_units_module(
include/mp-units/bits/text_tools.h include/mp-units/bits/text_tools.h
include/mp-units/bits/type_list.h include/mp-units/bits/type_list.h
include/mp-units/ext/algorithm.h include/mp-units/ext/algorithm.h
include/mp-units/ext/contracts.h
include/mp-units/ext/fixed_string.h include/mp-units/ext/fixed_string.h
include/mp-units/ext/prime.h include/mp-units/ext/prime.h
include/mp-units/ext/type_name.h include/mp-units/ext/type_name.h

View File

@@ -24,6 +24,7 @@
#include <mp-units/bits/hacks.h> #include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/contracts.h>
#include <array> #include <array>
#include <compare> #include <compare>
#include <concepts> #include <concepts>

View File

@@ -40,6 +40,7 @@
#include <cstdint> #include <cstdint>
#include <limits> #include <limits>
#include <string_view> #include <string_view>
#include <mp-units/ext/contracts.h>
#endif #endif
// most of the below code is based on/copied from fmtlib // most of the below code is based on/copied from fmtlib

View File

@@ -27,6 +27,7 @@
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE #ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export #include <compare> // IWYU pragma: export
#include <cstdint> #include <cstdint>
#include <numeric> #include <numeric>

View File

@@ -101,8 +101,6 @@ MP_UNITS_DIAGNOSTIC_POP
#if MP_UNITS_API_CONTRACTS == 2 || __has_include(<gsl/gsl-lite.hpp>) #if MP_UNITS_API_CONTRACTS == 2 || __has_include(<gsl/gsl-lite.hpp>)
#include <gsl/gsl-lite.hpp>
#define MP_UNITS_EXPECTS(expr) gsl_Expects(expr) #define MP_UNITS_EXPECTS(expr) gsl_Expects(expr)
#define MP_UNITS_EXPECTS_DEBUG(expr) gsl_ExpectsDebug(expr) #define MP_UNITS_EXPECTS_DEBUG(expr) gsl_ExpectsDebug(expr)
#define MP_UNITS_ASSERT(expr) gsl_Assert(expr) #define MP_UNITS_ASSERT(expr) gsl_Assert(expr)
@@ -110,9 +108,6 @@ MP_UNITS_DIAGNOSTIC_POP
#elif MP_UNITS_API_CONTRACTS == 3 || __has_include(<gsl/gsl>) #elif MP_UNITS_API_CONTRACTS == 3 || __has_include(<gsl/gsl>)
#include <gsl/gsl>
#include <cassert>
#define MP_UNITS_EXPECTS(expr) Expects(expr) #define MP_UNITS_EXPECTS(expr) Expects(expr)
#if defined NDEBUG #if defined NDEBUG
#define MP_UNITS_EXPECTS_DEBUG(expr) static_cast<void>(0) #define MP_UNITS_EXPECTS_DEBUG(expr) static_cast<void>(0)

View File

@@ -0,0 +1,49 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// 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 MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h>
#if MP_UNITS_API_CONTRACTS == 2 || __has_include(<gsl/gsl-lite.hpp>)
#if MP_UNITS_HOSTED
#include <gsl/gsl-lite.hpp>
#else
#include <mp-units/bits/requires_hosted.h>
#endif
#elif MP_UNITS_API_CONTRACTS == 3 || __has_include(<gsl/gsl>)
#if MP_UNITS_HOSTED
#include <gsl/gsl>
#include <cassert>
#else
#include <mp-units/bits/requires_hosted.h>
#endif
#endif
#endif // MP_UNITS_IN_MODULE_INTERFACE

View File

@@ -32,6 +32,7 @@
#include <mp-units/ext/type_traits.h> #include <mp-units/ext/type_traits.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE #ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export #include <compare> // IWYU pragma: export
#include <cstddef> #include <cstddef>
#include <cstdlib> #include <cstdlib>

View File

@@ -34,6 +34,7 @@
#include <mp-units/framework/symbol_text.h> #include <mp-units/framework/symbol_text.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE #ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <iterator> #include <iterator>

View File

@@ -37,6 +37,7 @@
#include <mp-units/framework/unit_concepts.h> #include <mp-units/framework/unit_concepts.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE #ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export #include <compare> // IWYU pragma: export
#include <utility> #include <utility>
#endif #endif

View File

@@ -31,6 +31,7 @@
#include <mp-units/ext/fixed_string.h> #include <mp-units/ext/fixed_string.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE #ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <compare> // IWYU pragma: export #include <compare> // IWYU pragma: export
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>

View File

@@ -41,6 +41,7 @@
#include <mp-units/framework/unit_concepts.h> #include <mp-units/framework/unit_concepts.h>
#ifndef MP_UNITS_IN_MODULE_INTERFACE #ifndef MP_UNITS_IN_MODULE_INTERFACE
#include <mp-units/ext/contracts.h>
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <iterator> #include <iterator>