From 35798a0f39fe6523bb54de171dc8b3a8a072fd41 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 20 Jun 2025 10:24:33 +0200 Subject: [PATCH] refactor: `ostream.h` header file made deprecated --- example/avg_speed.cpp | 1 - example/capacitor_time_curve.cpp | 1 - example/clcpp_response.cpp | 1 - example/currency.cpp | 1 - example/hello_units.cpp | 1 - example/measurement.cpp | 1 - example/strong_angular_quantities.cpp | 1 - example/total_energy.cpp | 1 - src/core/CMakeLists.txt | 1 + src/core/include/mp-units/bits/ostream.h | 58 ++++++++++++++ src/core/include/mp-units/core.h | 1 - .../include/mp-units/framework/dimension.h | 18 ++++- .../include/mp-units/framework/quantity.h | 29 ++++++- src/core/include/mp-units/framework/unit.h | 17 +++- src/core/include/mp-units/ostream.h | 80 +------------------ test/runtime/atomic_test.cpp | 1 - test/runtime/distribution_test.cpp | 1 - test/runtime/fmt_test.cpp | 1 - test/runtime/linear_algebra_test.cpp | 1 - test/runtime/math_test.cpp | 1 - test/runtime/truncation_test.cpp | 1 - test_package/test_package.cpp | 1 - 22 files changed, 117 insertions(+), 102 deletions(-) create mode 100644 src/core/include/mp-units/bits/ostream.h diff --git a/example/avg_speed.cpp b/example/avg_speed.cpp index dc2d24d2..4fe5f729 100644 --- a/example/avg_speed.cpp +++ b/example/avg_speed.cpp @@ -34,7 +34,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include #include #include #include diff --git a/example/capacitor_time_curve.cpp b/example/capacitor_time_curve.cpp index 4cf363fe..d1c75757 100644 --- a/example/capacitor_time_curve.cpp +++ b/example/capacitor_time_curve.cpp @@ -29,7 +29,6 @@ import std; import mp_units; #else #include -#include #include #include #endif diff --git a/example/clcpp_response.cpp b/example/clcpp_response.cpp index f3b4c14e..9e3e8ade 100644 --- a/example/clcpp_response.cpp +++ b/example/clcpp_response.cpp @@ -25,7 +25,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include #include #include #include // IWYU pragma: keep diff --git a/example/currency.cpp b/example/currency.cpp index bedf7c05..f941ead3 100644 --- a/example/currency.cpp +++ b/example/currency.cpp @@ -35,7 +35,6 @@ import std; import mp_units.core; #else #include -#include #endif using namespace mp_units; diff --git a/example/hello_units.cpp b/example/hello_units.cpp index 6ada5c88..38f0a81f 100644 --- a/example/hello_units.cpp +++ b/example/hello_units.cpp @@ -36,7 +36,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include #include #include #include diff --git a/example/measurement.cpp b/example/measurement.cpp index f6624a5c..9c1e1327 100644 --- a/example/measurement.cpp +++ b/example/measurement.cpp @@ -35,7 +35,6 @@ import std; import mp_units; #else #include -#include #include #include #endif diff --git a/example/strong_angular_quantities.cpp b/example/strong_angular_quantities.cpp index 88b80abe..ca85fde1 100644 --- a/example/strong_angular_quantities.cpp +++ b/example/strong_angular_quantities.cpp @@ -28,7 +28,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include #include #include #endif diff --git a/example/total_energy.cpp b/example/total_energy.cpp index eced87bb..d6545087 100644 --- a/example/total_energy.cpp +++ b/example/total_energy.cpp @@ -30,7 +30,6 @@ import std; import mp_units; #else #include -#include #include #include #include diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index bf5d3892..8e93fcac 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -90,6 +90,7 @@ if(NOT ${projectPrefix}API_FREESTANDING) FILES include/mp-units/bits/fmt.h include/mp-units/bits/format.h + include/mp-units/bits/ostream.h include/mp-units/bits/requires_hosted.h include/mp-units/ext/format.h include/mp-units/cartesian_vector.h diff --git a/src/core/include/mp-units/bits/ostream.h b/src/core/include/mp-units/bits/ostream.h new file mode 100644 index 00000000..930dfdd4 --- /dev/null +++ b/src/core/include/mp-units/bits/ostream.h @@ -0,0 +1,58 @@ + +// 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 + +#include +// +#include + +#ifndef MP_UNITS_IN_MODULE_INTERFACE +#ifdef MP_UNITS_IMPORT_STD +import std; +#else +#include +#include +#endif +#endif + +namespace mp_units::detail { + +template&> F> +std::basic_ostream& to_stream(std::basic_ostream& os, const F& func) +{ + if (os.width()) { + // std::setw() applies to the whole output so it has to be first put into std::string + std::basic_ostringstream oss; + oss.flags(os.flags()); + oss.imbue(os.getloc()); + oss.precision(os.precision()); + func(oss); + return os << std::move(oss).str(); + } + + func(os); + return os; +} + +} // namespace mp_units::detail diff --git a/src/core/include/mp-units/core.h b/src/core/include/mp-units/core.h index 1674cc83..269507a7 100644 --- a/src/core/include/mp-units/core.h +++ b/src/core/include/mp-units/core.h @@ -30,7 +30,6 @@ #if MP_UNITS_HOSTED #include #include -#include #include #endif // IWYU pragma: end_exports diff --git a/src/core/include/mp-units/framework/dimension.h b/src/core/include/mp-units/framework/dimension.h index 65eed6ee..58b43fe6 100644 --- a/src/core/include/mp-units/framework/dimension.h +++ b/src/core/include/mp-units/framework/dimension.h @@ -33,6 +33,10 @@ #include #include #include +#if MP_UNITS_HOSTED +#include +#include +#endif #ifndef MP_UNITS_IN_MODULE_INTERFACE #include @@ -314,12 +318,22 @@ MP_UNITS_EXPORT template.view(); } +#if MP_UNITS_HOSTED + +MP_UNITS_EXPORT template +std::basic_ostream& operator<<(std::basic_ostream& os, D d) +{ + return detail::to_stream(os, [&](std::basic_ostream& oss) { + dimension_symbol_to(std::ostream_iterator(oss), d); + }); +} + +#endif // MP_UNITS_HOSTED + } // namespace mp_units #if MP_UNITS_HOSTED -#include - // // Grammar // diff --git a/src/core/include/mp-units/framework/quantity.h b/src/core/include/mp-units/framework/quantity.h index 34a4ce16..211d2239 100644 --- a/src/core/include/mp-units/framework/quantity.h +++ b/src/core/include/mp-units/framework/quantity.h @@ -36,6 +36,10 @@ #include #include #include +#if MP_UNITS_HOSTED +#include +#include +#endif #ifndef MP_UNITS_IN_MODULE_INTERFACE #include @@ -45,6 +49,9 @@ import std; #include // IWYU pragma: export #include #include +#if MP_UNITS_HOSTED +#include +#endif #endif #endif @@ -647,6 +654,25 @@ template explicit(quantity_like_traits::explicit_import) quantity(Q) -> quantity::reference, typename quantity_like_traits::rep>; +#if MP_UNITS_HOSTED + +template +std::basic_ostream& operator<<(std::basic_ostream& os, const quantity& q) + requires requires { os << q.numerical_value_ref_in(q.unit); } +{ + return detail::to_stream(os, [&](std::basic_ostream& oss) { + if constexpr (is_same_v || is_same_v) + // promote the value to int + oss << +q.numerical_value_ref_in(q.unit); + else + oss << q.numerical_value_ref_in(q.unit); + if constexpr (space_before_unit_symbol) oss << " "; + oss << q.unit; + }); +} + +#endif // MP_UNITS_HOSTED + MP_UNITS_EXPORT_END } // namespace mp_units @@ -737,9 +763,6 @@ public: #if MP_UNITS_HOSTED -#include -#include - // // Grammar // diff --git a/src/core/include/mp-units/framework/unit.h b/src/core/include/mp-units/framework/unit.h index 863f151a..7c5b7367 100644 --- a/src/core/include/mp-units/framework/unit.h +++ b/src/core/include/mp-units/framework/unit.h @@ -40,6 +40,10 @@ #include #include #include +#if MP_UNITS_HOSTED +#include +#include +#endif #ifndef MP_UNITS_IN_MODULE_INTERFACE #include @@ -913,12 +917,21 @@ MP_UNITS_EXPORT template.view(); } +#if MP_UNITS_HOSTED + +MP_UNITS_EXPORT template +std::basic_ostream& operator<<(std::basic_ostream& os, U u) +{ + return detail::to_stream( + os, [&](std::basic_ostream& oss) { unit_symbol_to(std::ostream_iterator(oss), u); }); +} + +#endif // MP_UNITS_HOSTED + } // namespace mp_units #if MP_UNITS_HOSTED -#include - // // Grammar // diff --git a/src/core/include/mp-units/ostream.h b/src/core/include/mp-units/ostream.h index 1b3f63ac..f4511c21 100644 --- a/src/core/include/mp-units/ostream.h +++ b/src/core/include/mp-units/ostream.h @@ -23,82 +23,4 @@ #pragma once -#include -// -#include -#include -#include - -#ifndef MP_UNITS_IN_MODULE_INTERFACE -#ifdef MP_UNITS_IMPORT_STD -import std; -#else -#include -#include -#endif -#endif - -namespace mp_units { - -namespace detail { - -template -void to_stream_impl(std::basic_ostream& os, D d) -{ - dimension_symbol_to(std::ostream_iterator(os), d); -} - -template -void to_stream_impl(std::basic_ostream& os, U u) -{ - unit_symbol_to(std::ostream_iterator(os), u); -} - -template -void to_stream_impl(std::basic_ostream& os, const quantity& q) - requires requires { os << q.numerical_value_ref_in(q.unit); } -{ - if constexpr (is_same_v || is_same_v) - // promote the value to int - os << +q.numerical_value_ref_in(q.unit); - else - os << q.numerical_value_ref_in(q.unit); - if constexpr (space_before_unit_symbol) os << " "; - to_stream_impl(os, get_unit(R)); -} - -template -std::basic_ostream& to_stream(std::basic_ostream& os, const T& val) - requires requires { detail::to_stream_impl(os, val); } -{ - if (os.width()) { - // std::setw() applies to the whole output so it has to be first put into std::string - std::basic_ostringstream oss; - oss.flags(os.flags()); - oss.imbue(os.getloc()); - oss.precision(os.precision()); - detail::to_stream_impl(oss, val); - return os << std::move(oss).str(); - } - - detail::to_stream_impl(os, val); - return os; -} - -template -constexpr bool is_mp_units_stream = requires(OStream os, T v) { detail::to_stream_impl(os, v); }; - -} // namespace detail - -MP_UNITS_EXPORT_BEGIN - -template -std::basic_ostream& operator<<(std::basic_ostream& os, const T& val) - requires detail::is_mp_units_stream, T> -{ - return detail::to_stream(os, val); -} - -MP_UNITS_EXPORT_END - -} // namespace mp_units +#warning "This header file is deprecated and does not have to be included anymore." diff --git a/test/runtime/atomic_test.cpp b/test/runtime/atomic_test.cpp index 5bca3157..f7b2540f 100644 --- a/test/runtime/atomic_test.cpp +++ b/test/runtime/atomic_test.cpp @@ -29,7 +29,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include // IWYU pragma: keep #include #include #endif diff --git a/test/runtime/distribution_test.cpp b/test/runtime/distribution_test.cpp index 19b237a0..322614ed 100644 --- a/test/runtime/distribution_test.cpp +++ b/test/runtime/distribution_test.cpp @@ -34,7 +34,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include // IWYU pragma: keep #include #include #include diff --git a/test/runtime/fmt_test.cpp b/test/runtime/fmt_test.cpp index ee04a871..14eea147 100644 --- a/test/runtime/fmt_test.cpp +++ b/test/runtime/fmt_test.cpp @@ -40,7 +40,6 @@ import std; import mp_units; #else #include -#include // IWYU pragma: keep #include #include #include diff --git a/test/runtime/linear_algebra_test.cpp b/test/runtime/linear_algebra_test.cpp index 34aa9ba2..420f392f 100644 --- a/test/runtime/linear_algebra_test.cpp +++ b/test/runtime/linear_algebra_test.cpp @@ -33,7 +33,6 @@ import std; import mp_units; #else #include -#include // IWYU pragma: keep #include #include #include diff --git a/test/runtime/math_test.cpp b/test/runtime/math_test.cpp index e6416d1d..3ef00134 100644 --- a/test/runtime/math_test.cpp +++ b/test/runtime/math_test.cpp @@ -32,7 +32,6 @@ import std; import mp_units; #else #include -#include // IWYU pragma: keep #include #include #include diff --git a/test/runtime/truncation_test.cpp b/test/runtime/truncation_test.cpp index 5f42ed57..9a0fb991 100644 --- a/test/runtime/truncation_test.cpp +++ b/test/runtime/truncation_test.cpp @@ -34,7 +34,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include // IWYU pragma: keep #include #endif diff --git a/test_package/test_package.cpp b/test_package/test_package.cpp index c52cfa57..f771bd51 100644 --- a/test_package/test_package.cpp +++ b/test_package/test_package.cpp @@ -30,7 +30,6 @@ import std; #ifdef MP_UNITS_MODULES import mp_units; #else -#include #include #include #endif