diff --git a/docs/users_guide/use_cases/wide_compatibility.md b/docs/users_guide/use_cases/wide_compatibility.md index 85dd916d..b8e7d465 100644 --- a/docs/users_guide/use_cases/wide_compatibility.md +++ b/docs/users_guide/use_cases/wide_compatibility.md @@ -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 [MP_UNITS_API_STD_FORMAT](../../getting_started/installation_and_usage.md#MP_UNITS_API_STD_FORMAT) 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 +``` diff --git a/example/include/validated_type.h b/example/include/validated_type.h index 9a429e26..a0a7b91c 100644 --- a/example/include/validated_type.h +++ b/example/include/validated_type.h @@ -24,6 +24,7 @@ #include #include +#include #include // IWYU pragma: export #include #include diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 5ee3c155..733e3586 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -44,6 +44,7 @@ add_mp_units_module( include/mp-units/bits/text_tools.h include/mp-units/bits/type_list.h include/mp-units/ext/algorithm.h + include/mp-units/ext/contracts.h include/mp-units/ext/fixed_string.h include/mp-units/ext/prime.h include/mp-units/ext/type_name.h diff --git a/src/core/include/mp-units/bits/core_gmf.h b/src/core/include/mp-units/bits/core_gmf.h index 21d97e8f..08defcae 100644 --- a/src/core/include/mp-units/bits/core_gmf.h +++ b/src/core/include/mp-units/bits/core_gmf.h @@ -24,6 +24,7 @@ #include #include +#include #include #include #include diff --git a/src/core/include/mp-units/bits/fmt.h b/src/core/include/mp-units/bits/fmt.h index 8e84ac0b..bc3197e0 100644 --- a/src/core/include/mp-units/bits/fmt.h +++ b/src/core/include/mp-units/bits/fmt.h @@ -40,6 +40,7 @@ #include #include #include +#include #endif // most of the below code is based on/copied from fmtlib diff --git a/src/core/include/mp-units/bits/ratio.h b/src/core/include/mp-units/bits/ratio.h index bcf778dd..85e49afc 100644 --- a/src/core/include/mp-units/bits/ratio.h +++ b/src/core/include/mp-units/bits/ratio.h @@ -27,6 +27,7 @@ #include #ifndef MP_UNITS_IN_MODULE_INTERFACE +#include #include // IWYU pragma: export #include #include diff --git a/src/core/include/mp-units/compat_macros.h b/src/core/include/mp-units/compat_macros.h index 026f04e9..1d9aff34 100644 --- a/src/core/include/mp-units/compat_macros.h +++ b/src/core/include/mp-units/compat_macros.h @@ -101,8 +101,6 @@ MP_UNITS_DIAGNOSTIC_POP #if MP_UNITS_API_CONTRACTS == 2 || __has_include() -#include - #define MP_UNITS_EXPECTS(expr) gsl_Expects(expr) #define MP_UNITS_EXPECTS_DEBUG(expr) gsl_ExpectsDebug(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() -#include -#include - #define MP_UNITS_EXPECTS(expr) Expects(expr) #if defined NDEBUG #define MP_UNITS_EXPECTS_DEBUG(expr) static_cast(0) diff --git a/src/core/include/mp-units/ext/contracts.h b/src/core/include/mp-units/ext/contracts.h new file mode 100644 index 00000000..b274b021 --- /dev/null +++ b/src/core/include/mp-units/ext/contracts.h @@ -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 +#include + +#if MP_UNITS_API_CONTRACTS == 2 || __has_include() + +#if MP_UNITS_HOSTED +#include +#else +#include +#endif + +#elif MP_UNITS_API_CONTRACTS == 3 || __has_include() + +#if MP_UNITS_HOSTED +#include +#include +#else +#include +#endif + +#endif + +#endif // MP_UNITS_IN_MODULE_INTERFACE diff --git a/src/core/include/mp-units/ext/fixed_string.h b/src/core/include/mp-units/ext/fixed_string.h index 1cfe2369..2bfdb377 100644 --- a/src/core/include/mp-units/ext/fixed_string.h +++ b/src/core/include/mp-units/ext/fixed_string.h @@ -32,6 +32,7 @@ #include #ifndef MP_UNITS_IN_MODULE_INTERFACE +#include #include // IWYU pragma: export #include #include diff --git a/src/core/include/mp-units/framework/dimension.h b/src/core/include/mp-units/framework/dimension.h index 4aa746dd..63f409de 100644 --- a/src/core/include/mp-units/framework/dimension.h +++ b/src/core/include/mp-units/framework/dimension.h @@ -34,6 +34,7 @@ #include #ifndef MP_UNITS_IN_MODULE_INTERFACE +#include #include #include #include diff --git a/src/core/include/mp-units/framework/quantity.h b/src/core/include/mp-units/framework/quantity.h index f9625a9d..a87fa251 100644 --- a/src/core/include/mp-units/framework/quantity.h +++ b/src/core/include/mp-units/framework/quantity.h @@ -37,6 +37,7 @@ #include #ifndef MP_UNITS_IN_MODULE_INTERFACE +#include #include // IWYU pragma: export #include #endif diff --git a/src/core/include/mp-units/framework/symbol_text.h b/src/core/include/mp-units/framework/symbol_text.h index e89aab96..a266cb24 100644 --- a/src/core/include/mp-units/framework/symbol_text.h +++ b/src/core/include/mp-units/framework/symbol_text.h @@ -31,6 +31,7 @@ #include #ifndef MP_UNITS_IN_MODULE_INTERFACE +#include #include // IWYU pragma: export #include #include diff --git a/src/core/include/mp-units/framework/unit.h b/src/core/include/mp-units/framework/unit.h index c362afa1..e7786554 100644 --- a/src/core/include/mp-units/framework/unit.h +++ b/src/core/include/mp-units/framework/unit.h @@ -41,6 +41,7 @@ #include #ifndef MP_UNITS_IN_MODULE_INTERFACE +#include #include #include #include