From 9c3e10373e33431e8fd9e60c2e3041a88969dd3c Mon Sep 17 00:00:00 2001 From: metabeyond Date: Wed, 13 Nov 2024 23:17:07 +0800 Subject: [PATCH] Provides compatibilities for spdlog which internally uses fmt::format instead of std::format. --- CMakeLists.txt | 1 + Dependencies.cmake | 8 ++++- FeatureTests.cmake | 10 +++++++ feature-testing/std_format.cpp | 28 ++++++++++++++++++ include/essence/format_remediation.hpp | 41 +++++--------------------- src/CMakeLists.txt | 2 +- 6 files changed, 55 insertions(+), 35 deletions(-) create mode 100644 FeatureTests.cmake create mode 100644 feature-testing/std_format.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index bd778c1..815af0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ if(ES_STATIC_RUNTIME) endif() include(CTest) +include(FeatureTests.cmake) include(Dependencies.cmake) add_subdirectory(src) diff --git a/Dependencies.cmake b/Dependencies.cmake index e37ee21..ac83ac1 100644 --- a/Dependencies.cmake +++ b/Dependencies.cmake @@ -101,6 +101,12 @@ es_make_install_third_party_library( INSTALL_DIR ${CMAKE_INSTALL_PREFIX} ) +if(ES_HAVE_STD_FORMAT) + set(spdlog_extra_args -DSPDLOG_USE_STD_FORMAT=ON) +else() + set(spdlog_extra_args -DSPDLOG_FMT_EXTERNAL=ON) +endif() + es_make_install_third_party_library( spdlog REQUIRED @@ -110,9 +116,9 @@ es_make_install_third_party_library( GENERATOR ${CMAKE_GENERATOR} CMAKE_ARGS -DSPDLOG_BUILD_PIC=ON - -DSPDLOG_FMT_EXTERNAL=ON -DCMAKE_PREFIX_PATH=${CMAKE_INSTALL_PREFIX} ${extra_cmake_args} + ${spdlog_extra_args} SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/third-party/spdlog INSTALL_DIR ${CMAKE_INSTALL_PREFIX} ) diff --git a/FeatureTests.cmake b/FeatureTests.cmake new file mode 100644 index 0000000..3072cd7 --- /dev/null +++ b/FeatureTests.cmake @@ -0,0 +1,10 @@ +include_guard() + +try_compile( + ES_HAVE_STD_FORMAT + SOURCES ${CMAKE_SOURCE_DIR}/feature-testing/std_format.cpp + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON +) + +message(STATUS "ES_HAVE_STD_FORMAT - ${ES_HAVE_STD_FORMAT}") diff --git a/feature-testing/std_format.cpp b/feature-testing/std_format.cpp new file mode 100644 index 0000000..3fb16c9 --- /dev/null +++ b/feature-testing/std_format.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 The RefValue Project + * + * 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. + */ + +#include +#include + +int main() { + const auto str = std::format("{}", std::string_view{"Hello"}); +} diff --git a/include/essence/format_remediation.hpp b/include/essence/format_remediation.hpp index 0c7e02d..37f3831 100644 --- a/include/essence/format_remediation.hpp +++ b/include/essence/format_remediation.hpp @@ -36,8 +36,7 @@ #include #endif -#if __cpp_lib_format >= 201907L -#define ES_WITH_STD_FORMAT +#ifdef CPP_ESSENCE_USE_STD_FORMAT #define ES_FMT_NS std #include #else @@ -47,23 +46,13 @@ #endif namespace essence { -#ifdef ES_WITH_STD_FORMAT - using std::format; - using std::format_string; - using std::format_to; - using std::make_format_args; - using std::make_wformat_args; - using std::vformat; - using std::wformat_string; -#else - using fmt::format; - using fmt::format_string; - using fmt::format_to; - using fmt::make_format_args; - using fmt::make_wformat_args; - using fmt::vformat; - using fmt::wformat_string; -#endif + using ES_FMT_NS::format; + using ES_FMT_NS::format_string; + using ES_FMT_NS::format_to; + using ES_FMT_NS::make_format_args; + using ES_FMT_NS::make_wformat_args; + using ES_FMT_NS::vformat; + using ES_FMT_NS::wformat_string; /** * @brief Formats a group of arguments with a format string. @@ -153,18 +142,4 @@ struct ES_FMT_NS::formatter, CharT> } }; -// Provides compatibilities for spdlog which internally uses fmt::format instead of std::format. -#ifdef ES_WITH_STD_FORMAT -template -struct fmt::formatter, CharT> - : formatter, CharT> { - template - auto format(essence::basic_zstring_view str, FormatContext& ctx) const { - return formatter, CharT>::format( - std::basic_string_view{str}, ctx); - } -}; -#endif - #undef ES_FMT_NS -#undef ES_WITH_STD_FORMAT diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 60756c3..4f24124 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -171,7 +171,7 @@ target_include_directories( target_compile_definitions( ${target_name} PUBLIC - CPP_ESSENCE_HEADER_ONLY=0 + $<$:CPP_ESSENCE_USE_STD_FORMAT=1> _TURN_OFF_PLATFORM_STRING=1 # For cpprestsdk. )