From af47898a6bf103ecef4da242292fc245c795b0fc Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Fri, 29 Dec 2023 17:19:24 +0100 Subject: [PATCH] refactor: `core-fmt` and `core-ostream` modules merged with `core` --- example/clcpp_response.cpp | 2 +- example/conversion_factor.cpp | 2 +- example/currency.cpp | 2 +- example/foot_pound_second.cpp | 2 +- example/glide_computer.cpp | 2 +- example/hello_units.cpp | 2 +- example/include/geographic.h | 2 +- example/include/validated_type.h | 2 +- example/kalman_filter/kalman.h | 2 +- example/si_constants.cpp | 2 +- example/spectroscopy_units.cpp | 2 +- example/storage_tank.cpp | 1 - example/unmanned_aerial_vehicle.cpp | 2 +- src/CMakeLists.txt | 4 +- src/core-fmt/CMakeLists.txt | 43 ---------- .../include/mp-units/compat_fmt_macros.h | 79 ------------------- src/core-fmt/mp-units-core-fmt.cpp | 13 --- src/core-io/CMakeLists.txt | 30 ------- src/core-io/mp-units-core-io.cpp | 13 --- src/core/CMakeLists.txt | 14 ++++ .../include/mp-units/bits/external/hacks.h | 47 +++++++++++ .../include/mp-units/bits/fmt.h | 2 +- src/core/include/mp-units/core.h | 2 + .../include/mp-units/format.h | 0 .../include/mp-units/ostream.h | 0 test/runtime/almost_equals.h | 2 +- test/runtime/fmt_test.cpp | 2 +- test/runtime/linear_algebra_test.cpp | 2 +- 28 files changed, 80 insertions(+), 198 deletions(-) delete mode 100644 src/core-fmt/CMakeLists.txt delete mode 100644 src/core-fmt/include/mp-units/compat_fmt_macros.h delete mode 100644 src/core-fmt/mp-units-core-fmt.cpp delete mode 100644 src/core-io/CMakeLists.txt delete mode 100644 src/core-io/mp-units-core-io.cpp rename src/{core-fmt => core}/include/mp-units/bits/fmt.h (99%) rename src/{core-fmt => core}/include/mp-units/format.h (100%) rename src/{core-io => core}/include/mp-units/ostream.h (100%) diff --git a/example/clcpp_response.cpp b/example/clcpp_response.cpp index 1bf0bbb5a8..98664b85c3 100644 --- a/example/clcpp_response.cpp +++ b/example/clcpp_response.cpp @@ -15,7 +15,7 @@ along with this program. If not, see http://www.gnu.org/licenses./ */ -#include +#include #include #ifdef MP_UNITS_MODULES import mp_units; diff --git a/example/conversion_factor.cpp b/example/conversion_factor.cpp index d6846f4dc0..e98dd694d8 100644 --- a/example/conversion_factor.cpp +++ b/example/conversion_factor.cpp @@ -15,7 +15,7 @@ along with this program. If not, see http://www.gnu.org/licenses./ */ -#include +#include #include #include #ifdef MP_UNITS_MODULES diff --git a/example/currency.cpp b/example/currency.cpp index 6552771116..cc935c5e3c 100644 --- a/example/currency.cpp +++ b/example/currency.cpp @@ -24,7 +24,7 @@ #include #include #ifdef MP_UNITS_MODULES -import mp_units.core_io; +import mp_units.core; #else #include #include diff --git a/example/foot_pound_second.cpp b/example/foot_pound_second.cpp index 63a303fc71..b14576b476 100644 --- a/example/foot_pound_second.cpp +++ b/example/foot_pound_second.cpp @@ -20,7 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include +#include #include #include #ifdef MP_UNITS_MODULES diff --git a/example/glide_computer.cpp b/example/glide_computer.cpp index c691d12d07..b834e917f6 100644 --- a/example/glide_computer.cpp +++ b/example/glide_computer.cpp @@ -21,7 +21,7 @@ // SOFTWARE. #include "glide_computer_lib.h" -#include +#include #include #include #include diff --git a/example/hello_units.cpp b/example/hello_units.cpp index e707cd99b9..9725926a40 100644 --- a/example/hello_units.cpp +++ b/example/hello_units.cpp @@ -25,7 +25,7 @@ // !!! renders correctly in the documentation "Examples" section. !!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -#include +#include #include #ifdef MP_UNITS_MODULES import mp_units; diff --git a/example/include/geographic.h b/example/include/geographic.h index 3d285bd8c4..9ee9491959 100644 --- a/example/include/geographic.h +++ b/example/include/geographic.h @@ -23,7 +23,7 @@ #pragma once #include "ranged_representation.h" -#include +#include #include #include #include diff --git a/example/include/validated_type.h b/example/include/validated_type.h index 9924fc4acb..019867dfc1 100644 --- a/example/include/validated_type.h +++ b/example/include/validated_type.h @@ -23,7 +23,7 @@ #pragma once #include -#include +#include #include #include #ifdef MP_UNITS_MODULES diff --git a/example/kalman_filter/kalman.h b/example/kalman_filter/kalman.h index 9115aae372..9ae5b46213 100644 --- a/example/kalman_filter/kalman.h +++ b/example/kalman_filter/kalman.h @@ -22,7 +22,7 @@ #pragma once -#include +#include #include #ifdef MP_UNITS_MODULES import mp_units; diff --git a/example/si_constants.cpp b/example/si_constants.cpp index e3d02d6eeb..2567210ad0 100644 --- a/example/si_constants.cpp +++ b/example/si_constants.cpp @@ -25,7 +25,7 @@ // !!! renders correctly in the documentation "Examples" section. !!! // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -#include +#include #include #ifdef MP_UNITS_MODULES import mp_units; diff --git a/example/spectroscopy_units.cpp b/example/spectroscopy_units.cpp index 71da66d59c..0badec0b54 100644 --- a/example/spectroscopy_units.cpp +++ b/example/spectroscopy_units.cpp @@ -20,7 +20,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include +#include #include #include #ifdef MP_UNITS_MODULES diff --git a/example/storage_tank.cpp b/example/storage_tank.cpp index a231738486..e1e457ceaf 100644 --- a/example/storage_tank.cpp +++ b/example/storage_tank.cpp @@ -20,7 +20,6 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -#include #include #include #include diff --git a/example/unmanned_aerial_vehicle.cpp b/example/unmanned_aerial_vehicle.cpp index 35978aaba9..05bf9309ff 100644 --- a/example/unmanned_aerial_vehicle.cpp +++ b/example/unmanned_aerial_vehicle.cpp @@ -21,7 +21,7 @@ // SOFTWARE. #include "geographic.h" -#include +#include #include #include #ifdef MP_UNITS_MODULES diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b3f877353d..51e484c900 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,13 +47,11 @@ if(${projectPrefix}AS_SYSTEM_HEADERS) endif() add_subdirectory(core) -add_subdirectory(core-fmt) -add_subdirectory(core-io) add_subdirectory(systems) # project-wide wrapper add_units_module( - mp-units DEPENDENCIES mp-units::core mp-units::core-io mp-units::core-fmt mp-units::systems + mp-units DEPENDENCIES mp-units::core mp-units::systems MODULE_INTERFACE_UNIT mp-units.cpp ) diff --git a/src/core-fmt/CMakeLists.txt b/src/core-fmt/CMakeLists.txt deleted file mode 100644 index 71a643daa2..0000000000 --- a/src/core-fmt/CMakeLists.txt +++ /dev/null @@ -1,43 +0,0 @@ -# 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. - -cmake_minimum_required(VERSION 3.19) - -option(${projectPrefix}USE_LIBFMT "Enables usage of libfmt instead of the one from 'std'" ON) -message(STATUS "${projectPrefix}USE_LIBFMT: ${${projectPrefix}USE_LIBFMT}") - -add_units_module( - core-fmt - DEPENDENCIES mp-units::core - HEADERS include/mp-units/bits/fmt.h include/mp-units/compat_fmt_macros.h include/mp-units/format.h - MODULE_INTERFACE_UNIT mp-units-core-fmt.cpp -) -target_compile_definitions( - mp-units-core-fmt ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}USE_LIBFMT=$ -) - -if(${projectPrefix}USE_LIBFMT) - if(NOT TARGET fmt::fmt) - find_package(fmt CONFIG REQUIRED) - endif() - target_link_libraries(mp-units-core-fmt ${${projectPrefix}TARGET_SCOPE} fmt::fmt) -endif() diff --git a/src/core-fmt/include/mp-units/compat_fmt_macros.h b/src/core-fmt/include/mp-units/compat_fmt_macros.h deleted file mode 100644 index 3ac40aad87..0000000000 --- a/src/core-fmt/include/mp-units/compat_fmt_macros.h +++ /dev/null @@ -1,79 +0,0 @@ -// 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. - -// Formatting library for C++ - the core API for char/UTF-8 -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#pragma once - -#include - -#ifndef MP_UNITS_USE_LIBFMT -#define MP_UNITS_USE_LIBFMT 1 -#endif - -#if MP_UNITS_USE_LIBFMT - -MP_UNITS_DIAGNOSTIC_PUSH -MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE -MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW -#include -MP_UNITS_DIAGNOSTIC_POP - -#define MP_UNITS_STD_FMT fmt -#define MP_UNITS_FMT_LOCALE(loc) (loc).template get() -#define MP_UNITS_FMT_TO_ARG_ID(arg) static_cast(arg) -#define MP_UNITS_FMT_FROM_ARG_ID(arg) static_cast(arg) - -// This re-uses code from fmt; -#if FMT_EXCEPTIONS -#if FMT_MSC_VERSION || defined(__NVCC__) -#define MP_UNITS_THROW(x) ::fmt::detail::do_throw(x) -#else -#define MP_UNITS_THROW(x) throw x -#endif -#else -#define MP_UNITS_THROW(x) \ - do { \ - FMT_ASSERT(false, (x).what()); \ - } while (false) -#endif - -#else - -#ifndef __cpp_lib_format -#error "std::formatting facility not supported" -#endif - -#include - -#define MP_UNITS_STD_FMT std -#define MP_UNITS_FMT_LOCALE(loc) loc -#define MP_UNITS_FMT_TO_ARG_ID(arg) arg -#define MP_UNITS_FMT_FROM_ARG_ID(arg) arg -#define MP_UNITS_THROW(arg) throw arg - -#endif diff --git a/src/core-fmt/mp-units-core-fmt.cpp b/src/core-fmt/mp-units-core-fmt.cpp deleted file mode 100644 index da3e62e892..0000000000 --- a/src/core-fmt/mp-units-core-fmt.cpp +++ /dev/null @@ -1,13 +0,0 @@ -module; - -#include -#include - -export module mp_units.core_fmt; - -export import mp_units.core; - -export -{ -#include -} diff --git a/src/core-io/CMakeLists.txt b/src/core-io/CMakeLists.txt deleted file mode 100644 index 6f9915302c..0000000000 --- a/src/core-io/CMakeLists.txt +++ /dev/null @@ -1,30 +0,0 @@ -# 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. - -cmake_minimum_required(VERSION 3.19) - -add_units_module( - core-io - DEPENDENCIES mp-units::core - HEADERS include/mp-units/ostream.h - MODULE_INTERFACE_UNIT mp-units-core-io.cpp -) diff --git a/src/core-io/mp-units-core-io.cpp b/src/core-io/mp-units-core-io.cpp deleted file mode 100644 index b31e780352..0000000000 --- a/src/core-io/mp-units-core-io.cpp +++ /dev/null @@ -1,13 +0,0 @@ -module; - -#include -#include - -export module mp_units.core_io; - -export import mp_units.core; - -export -{ -#include -} diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 46143809e0..023bb24552 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -42,6 +42,7 @@ add_units_module( include/mp-units/bits/core_gmf.h include/mp-units/bits/dimension_concepts.h include/mp-units/bits/expression_template.h + include/mp-units/bits/fmt.h include/mp-units/bits/get_associated_quantity.h include/mp-units/bits/get_common_base.h include/mp-units/bits/magnitude.h @@ -63,7 +64,9 @@ add_units_module( include/mp-units/core.h include/mp-units/customization_points.h include/mp-units/dimension.h + include/mp-units/format.h include/mp-units/math.h + include/mp-units/ostream.h include/mp-units/quantity.h include/mp-units/quantity_point.h include/mp-units/quantity_spec.h @@ -74,6 +77,17 @@ add_units_module( MODULE_INTERFACE_UNIT mp-units-core.cpp ) +target_compile_definitions( + mp-units-core ${${projectPrefix}TARGET_SCOPE} ${projectPrefix}USE_LIBFMT=$ +) + +if(${projectPrefix}USE_LIBFMT) + if(NOT TARGET fmt::fmt) + find_package(fmt CONFIG REQUIRED) + endif() + target_link_libraries(mp-units-core ${${projectPrefix}TARGET_SCOPE} fmt::fmt) +endif() + if(${projectPrefix}BUILD_CXX_MODULES) if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 18) diff --git a/src/core/include/mp-units/bits/external/hacks.h b/src/core/include/mp-units/bits/external/hacks.h index 6bf6f45df0..542ebcc663 100644 --- a/src/core/include/mp-units/bits/external/hacks.h +++ b/src/core/include/mp-units/bits/external/hacks.h @@ -99,3 +99,50 @@ #define MP_UNITS_CONSTRAINED_NTTP_WORKAROUND(X) X #endif + +#ifndef MP_UNITS_USE_LIBFMT +#define MP_UNITS_USE_LIBFMT 1 +#endif + +#if MP_UNITS_USE_LIBFMT + +MP_UNITS_DIAGNOSTIC_PUSH +MP_UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE +MP_UNITS_DIAGNOSTIC_IGNORE_SHADOW +#include +MP_UNITS_DIAGNOSTIC_POP + +#define MP_UNITS_STD_FMT fmt +#define MP_UNITS_FMT_LOCALE(loc) (loc).template get() +#define MP_UNITS_FMT_TO_ARG_ID(arg) static_cast(arg) +#define MP_UNITS_FMT_FROM_ARG_ID(arg) static_cast(arg) + +// This re-uses code from fmt; +#if FMT_EXCEPTIONS +#if FMT_MSC_VERSION || defined(__NVCC__) +#define MP_UNITS_THROW(x) ::fmt::detail::do_throw(x) +#else +#define MP_UNITS_THROW(x) throw x +#endif +#else +#define MP_UNITS_THROW(x) \ + do { \ + FMT_ASSERT(false, (x).what()); \ + } while (false) +#endif + +#else + +#ifndef __cpp_lib_format +#error "std::formatting facility not supported" +#endif + +#include + +#define MP_UNITS_STD_FMT std +#define MP_UNITS_FMT_LOCALE(loc) loc +#define MP_UNITS_FMT_TO_ARG_ID(arg) arg +#define MP_UNITS_FMT_FROM_ARG_ID(arg) arg +#define MP_UNITS_THROW(arg) throw arg + +#endif diff --git a/src/core-fmt/include/mp-units/bits/fmt.h b/src/core/include/mp-units/bits/fmt.h similarity index 99% rename from src/core-fmt/include/mp-units/bits/fmt.h rename to src/core/include/mp-units/bits/fmt.h index 28fa08ea35..1b56e7efc9 100644 --- a/src/core-fmt/include/mp-units/bits/fmt.h +++ b/src/core/include/mp-units/bits/fmt.h @@ -30,7 +30,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/src/core/include/mp-units/core.h b/src/core/include/mp-units/core.h index e92ceb28d5..d2d49551d4 100644 --- a/src/core/include/mp-units/core.h +++ b/src/core/include/mp-units/core.h @@ -27,7 +27,9 @@ #include #include #include +#include #include +#include #include #include #include diff --git a/src/core-fmt/include/mp-units/format.h b/src/core/include/mp-units/format.h similarity index 100% rename from src/core-fmt/include/mp-units/format.h rename to src/core/include/mp-units/format.h diff --git a/src/core-io/include/mp-units/ostream.h b/src/core/include/mp-units/ostream.h similarity index 100% rename from src/core-io/include/mp-units/ostream.h rename to src/core/include/mp-units/ostream.h diff --git a/test/runtime/almost_equals.h b/test/runtime/almost_equals.h index 5913c9901d..471f2b794c 100644 --- a/test/runtime/almost_equals.h +++ b/test/runtime/almost_equals.h @@ -21,7 +21,7 @@ // SOFTWARE. #include -#include +#include #include #ifdef MP_UNITS_MODULES import mp_units; diff --git a/test/runtime/fmt_test.cpp b/test/runtime/fmt_test.cpp index ef914a1ff7..71ea858893 100644 --- a/test/runtime/fmt_test.cpp +++ b/test/runtime/fmt_test.cpp @@ -22,7 +22,7 @@ #include #include -#include +#include #include #include #include diff --git a/test/runtime/linear_algebra_test.cpp b/test/runtime/linear_algebra_test.cpp index cbdb9c6c87..5779fbb99e 100644 --- a/test/runtime/linear_algebra_test.cpp +++ b/test/runtime/linear_algebra_test.cpp @@ -21,7 +21,7 @@ // SOFTWARE. #include -#include +#include #include #include #ifdef MP_UNITS_MODULES