refactor: text formatting library header files are no longer included in compat_macros.h

This commit is contained in:
Mateusz Pusz
2024-06-10 22:04:41 +02:00
parent ed53cf03f0
commit e90cffcbda
35 changed files with 101 additions and 43 deletions

View File

@@ -18,20 +18,20 @@ First, we either import the `mp_units` module or include the headers for:
- text formatting and stream output support - text formatting and stream output support
```cpp title="hello_units.cpp" linenums="1" ```cpp title="hello_units.cpp" linenums="1"
--8<-- "example/hello_units.cpp:28:40" --8<-- "example/hello_units.cpp:28:41"
``` ```
Also, to shorten the definitions, we "import" all the symbols from the `mp_units` namespace. Also, to shorten the definitions, we "import" all the symbols from the `mp_units` namespace.
```cpp title="hello_units.cpp" linenums="13" ```cpp title="hello_units.cpp" linenums="14"
--8<-- "example/hello_units.cpp:41:42" --8<-- "example/hello_units.cpp:42:43"
``` ```
Next, we define a simple function that calculates the average speed based on the provided Next, we define a simple function that calculates the average speed based on the provided
arguments of length and time: arguments of length and time:
```cpp title="hello_units.cpp" linenums="14" ```cpp title="hello_units.cpp" linenums="15"
--8<-- "example/hello_units.cpp:43:46" --8<-- "example/hello_units.cpp:44:47"
``` ```
The above function template takes any quantities implicitly convertible to `isq::length` The above function template takes any quantities implicitly convertible to `isq::length`
@@ -45,37 +45,37 @@ that its quantity type is implicitly convertible to `isq::speed`.
type is beneficial for users of such a function as it provides more information type is beneficial for users of such a function as it provides more information
of what to expect from a function than just using `auto`. of what to expect from a function than just using `auto`.
```cpp title="hello_units.cpp" linenums="18" ```cpp title="hello_units.cpp" linenums="19"
--8<-- "example/hello_units.cpp:48:51" --8<-- "example/hello_units.cpp:49:52"
``` ```
The above lines explicitly opt into using unit symbols from two systems of units. The above lines explicitly opt into using unit symbols from two systems of units.
As this introduces a lot of short identifiers into the current scope, it is not done As this introduces a lot of short identifiers into the current scope, it is not done
implicitly while including a header file. implicitly while including a header file.
```cpp title="hello_units.cpp" linenums="22" ```cpp title="hello_units.cpp" linenums="23"
--8<-- "example/hello_units.cpp:53:59" --8<-- "example/hello_units.cpp:54:60"
``` ```
- Lines `21` & `22` create a quantity of kind `isq::length / isq::time` with the numbers - Lines `23` & `24` create a quantity of kind `isq::length / isq::time` with the numbers
and units provided. Such quantities can be converted or assigned to any other quantity and units provided. Such quantities can be converted or assigned to any other quantity
with a matching kind. with a matching kind.
- Line `23` calls our function template with quantities of kind `isq::length` and - Line `25` calls our function template with quantities of kind `isq::length` and
`isq::time` and number and units provided. `isq::time` and number and units provided.
- Line `24` explicitly provides quantity types of the quantities passed to a function template. - Line `26` explicitly provides quantity types of the quantities passed to a function template.
This time, those will not be quantity kinds anymore and will have This time, those will not be quantity kinds anymore and will have
[more restrictive conversion rules](../framework_basics/simple_and_typed_quantities.md#quantity_cast-to-force-unsafe-conversions). [more restrictive conversion rules](../framework_basics/simple_and_typed_quantities.md#quantity_cast-to-force-unsafe-conversions).
- Line `25` changes the unit of a quantity `v3` to `m / s` in a - Line `27` changes the unit of a quantity `v3` to `m / s` in a
[value-preserving way](../framework_basics/value_conversions.md#value-preserving-conversions) [value-preserving way](../framework_basics/value_conversions.md#value-preserving-conversions)
(floating-point representations are considered to be value-preserving). (floating-point representations are considered to be value-preserving).
- Line `26` does a similar operation, but this time, it would also succeed for - Line `28` does a similar operation, but this time, it would also succeed for
[value-truncating cases](../framework_basics/value_conversions.md#value-truncating-conversions) [value-truncating cases](../framework_basics/value_conversions.md#value-truncating-conversions)
(if that was the case). (if that was the case).
- Line `27` does a [value-truncating conversion](../framework_basics/value_conversions.md#value-truncating-conversions) - Line `29` does a [value-truncating conversion](../framework_basics/value_conversions.md#value-truncating-conversions)
of changing the underlying representation type from `double` to `int`. of changing the underlying representation type from `double` to `int`.
```cpp title="hello_units.cpp" linenums="29" ```cpp title="hello_units.cpp" linenums="30"
--8<-- "example/hello_units.cpp:61" --8<-- "example/hello_units.cpp:62"
``` ```
The above presents [various ways to print a quantity](../framework_basics/text_output.md). The above presents [various ways to print a quantity](../framework_basics/text_output.md).

View File

@@ -13,7 +13,7 @@ how [Faster-than-lightspeed Constants](../framework_basics/faster_than_lightspee
work in practice. work in practice.
```cpp title="si_constants.cpp" linenums="1" ```cpp title="si_constants.cpp" linenums="1"
--8<-- "example/si_constants.cpp:28:39" --8<-- "example/si_constants.cpp:28:40"
``` ```
As always, we start with the inclusion of all the needed header files. After that, for As always, we start with the inclusion of all the needed header files. After that, for
@@ -21,8 +21,8 @@ the simplicity of this example, we
[hack the character of quantities](../framework_basics/character_of_a_quantity.md#hacking-the-character) [hack the character of quantities](../framework_basics/character_of_a_quantity.md#hacking-the-character)
to be able to express vector quantities with simple scalar types. to be able to express vector quantities with simple scalar types.
```cpp title="si_constants.cpp" linenums="13" ```cpp title="si_constants.cpp" linenums="14"
--8<-- "example/si_constants.cpp:41:" --8<-- "example/si_constants.cpp:42:"
``` ```
The main part of the example prints all of the SI-defining constants. While analyzing the output of The main part of the example prints all of the SI-defining constants. While analyzing the output of

View File

@@ -96,6 +96,7 @@ your code using **mp-units**:
```cpp ```cpp
#include <iostream> #include <iostream>
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
import mp_units; import mp_units;
@@ -158,6 +159,13 @@ This macro resolves to either the `std` or `fmt` namespace, depending on the val
[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.
To include the header files of the underlying text formatting framework, the following include
should be used:
```cpp
#include <mp-units/ext/format.h>
```
### Contracts ### Contracts
The mp-units library internally does contract checking by default. It can be disabled with a Conan The mp-units library internally does contract checking by default. It can be disabled with a Conan

View File

@@ -16,6 +16,7 @@
*/ */
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;

View File

@@ -16,6 +16,7 @@
*/ */
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <concepts> #include <concepts>
#include <iostream> #include <iostream>
#include <string> #include <string>

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <string_view> #include <string_view>

View File

@@ -24,6 +24,7 @@
#include "glide_computer_lib.h" #include "glide_computer_lib.h"
#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/format.h>
#include <array> #include <array>
#include <chrono> #include <chrono>
#include <concepts> #include <concepts>

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include "glide_computer_lib.h" #include "glide_computer_lib.h"
#include <mp-units/ext/format.h>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <numeric> #include <numeric>

View File

@@ -26,6 +26,7 @@
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -24,6 +24,7 @@
#include "ranged_representation.h" #include "ranged_representation.h"
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <compare> #include <compare>
#include <limits> #include <limits>
#include <numbers> #include <numbers>

View File

@@ -25,6 +25,7 @@
#include "validated_type.h" #include "validated_type.h"
#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/format.h>
#include <algorithm> #include <algorithm>
#include <concepts> #include <concepts>
#include <type_traits> #include <type_traits>

View File

@@ -25,6 +25,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 <mp-units/ext/contracts.h>
#include <mp-units/ext/format.h>
#include <compare> // IWYU pragma: export #include <compare> // IWYU pragma: export
#include <ostream> #include <ostream>
#include <utility> #include <utility>

View File

@@ -23,6 +23,7 @@
#pragma once #pragma once
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <algorithm> #include <algorithm>
#include <locale> #include <locale>
#include <tuple> #include <tuple>

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include "kalman.h" #include "kalman.h"
#include <mp-units/ext/format.h>
#include <array> #include <array>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -23,6 +23,7 @@
#include "kalman.h" #include "kalman.h"
#include <array> #include <array>
#include <iostream> #include <iostream>
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;
#else #else

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include "kalman.h" #include "kalman.h"
#include <mp-units/ext/format.h>
#include <array> #include <array>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include "kalman.h" #include "kalman.h"
#include <mp-units/ext/format.h>
#include <array> #include <array>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include "kalman.h" #include "kalman.h"
#include <mp-units/ext/format.h>
#include <array> #include <array>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -23,6 +23,7 @@
#include "kalman.h" #include "kalman.h"
#include <array> #include <array>
#include <iostream> #include <iostream>
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;
#else #else

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include "kalman.h" #include "kalman.h"
#include <mp-units/ext/format.h>
#include <array> #include <array>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include "kalman.h" #include "kalman.h"
#include <mp-units/ext/format.h>
#include <array> #include <array>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -26,6 +26,7 @@
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <iostream> #include <iostream>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <iostream> #include <iostream>
#include <tuple> #include <tuple>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES

View File

@@ -21,6 +21,7 @@
// SOFTWARE. // SOFTWARE.
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <cassert> #include <cassert>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>

View File

@@ -23,6 +23,7 @@
#include "geographic.h" #include "geographic.h"
#include "ranged_representation.h" #include "ranged_representation.h"
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
@@ -30,6 +31,7 @@
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;
#else #else
#include <mp-units/bits/fmt.h>
#include <mp-units/systems/international.h> #include <mp-units/systems/international.h>
#include <mp-units/systems/isq/space_and_time.h> #include <mp-units/systems/isq/space_and_time.h>
#include <mp-units/systems/si.h> #include <mp-units/systems/si.h>

View File

@@ -46,6 +46,7 @@ add_mp_units_module(
include/mp-units/ext/algorithm.h include/mp-units/ext/algorithm.h
include/mp-units/ext/contracts.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/format.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
include/mp-units/ext/type_traits.h include/mp-units/ext/type_traits.h

View File

@@ -45,23 +45,13 @@
#include <utility> #include <utility>
#if MP_UNITS_HOSTED #if MP_UNITS_HOSTED
#include <mp-units/ext/format.h>
#include <cmath> #include <cmath>
#include <locale> #include <locale>
#include <ostream> #include <ostream>
#include <random> #include <random>
#include <sstream> #include <sstream>
#include <string> #include <string>
#if MP_UNITS_USE_FMTLIB
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h>
MP_UNITS_DIAGNOSTIC_POP
#else
#include <format>
#endif
#endif #endif
#if __cpp_lib_text_encoding #if __cpp_lib_text_encoding

View File

@@ -41,6 +41,7 @@
#include <limits> #include <limits>
#include <string_view> #include <string_view>
#include <mp-units/ext/contracts.h> #include <mp-units/ext/contracts.h>
#include <mp-units/ext/format.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

@@ -83,18 +83,6 @@
#ifndef MP_UNITS_IN_MODULE_INTERFACE #ifndef MP_UNITS_IN_MODULE_INTERFACE
// IWYU pragma: begin_exports
#if MP_UNITS_USE_FMTLIB
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h>
MP_UNITS_DIAGNOSTIC_POP
#else
#include <format>
#endif
// IWYU pragma: end_exports
#endif #endif
#endif // MP_UNITS_HOSTED #endif // MP_UNITS_HOSTED

View File

@@ -0,0 +1,42 @@
// 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/requires_hosted.h>
//
#include <mp-units/bits/hacks.h>
#include <mp-units/compat_macros.h>
#if MP_UNITS_USE_FMTLIB
MP_UNITS_DIAGNOSTIC_PUSH
MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h>
MP_UNITS_DIAGNOSTIC_POP
#else
#include <format>
#endif
#endif

View File

@@ -22,6 +22,7 @@
#include <catch2/matchers/catch_matchers_templated.hpp> #include <catch2/matchers/catch_matchers_templated.hpp>
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <algorithm> #include <algorithm>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;

View File

@@ -25,6 +25,7 @@
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <sstream> #include <sstream>
#include <string_view> #include <string_view>
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;
#else #else

View File

@@ -24,6 +24,7 @@
#include <catch2/matchers/catch_matchers.hpp> #include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_exception.hpp> #include <catch2/matchers/catch_matchers_exception.hpp>
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <mp-units/ext/format.h>
#include <cstdint> #include <cstdint>
#include <iomanip> #include <iomanip>
#include <limits> #include <limits>

View File

@@ -24,6 +24,7 @@
#include <mp-units/compat_macros.h> #include <mp-units/compat_macros.h>
#include <matrix> #include <matrix>
#include <ostream> #include <ostream>
#include <mp-units/ext/format.h>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES
import mp_units; import mp_units;
#else #else

View File

@@ -24,6 +24,7 @@
#include <catch2/catch_template_test_macros.hpp> #include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp> #include <catch2/matchers/catch_matchers.hpp>
#include <mp-units/ext/format.h>
#include <limits> #include <limits>
#include <numbers> #include <numbers>
#ifdef MP_UNITS_MODULES #ifdef MP_UNITS_MODULES