From f6aabecef493ea7da851a06d4ae813c7807cf066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20S=2E=20Ga=C3=9Fmann?= Date: Fri, 14 Apr 2023 10:43:25 +0200 Subject: [PATCH 1/3] feat: Add `` support for `string_ref` --- .github/workflows/unittests_linux.yml | 1 + .github/workflows/unittests_windows.yml | 1 + CMakeLists.txt | 8 +++ include/status-code/fmt_support.hpp | 85 +++++++++++++++++++++++ test/format.cpp | 89 +++++++++++++++++++++++++ 5 files changed, 184 insertions(+) create mode 100644 include/status-code/fmt_support.hpp create mode 100644 test/format.cpp diff --git a/.github/workflows/unittests_linux.yml b/.github/workflows/unittests_linux.yml index 5fc72e4..1a36cb6 100644 --- a/.github/workflows/unittests_linux.yml +++ b/.github/workflows/unittests_linux.yml @@ -37,6 +37,7 @@ jobs: run: | cd build bin/test-result + bin/test-cxx20-format bin/test-status-code bin/test-status-code-noexcept bin/test-status-code-not-posix diff --git a/.github/workflows/unittests_windows.yml b/.github/workflows/unittests_windows.yml index 6aacf8f..26b6203 100644 --- a/.github/workflows/unittests_windows.yml +++ b/.github/workflows/unittests_windows.yml @@ -30,6 +30,7 @@ jobs: run: | cd build bin/Release/test-result + bin/Release/test-cxx20-format bin/Release/test-status-code bin/Release/test-status-code-noexcept bin/Release/test-status-code-not-posix diff --git a/CMakeLists.txt b/CMakeLists.txt index 87aa94a..a001eb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ foreach(source "include/status-code/config.hpp" "include/status-code/error.hpp" "include/status-code/errored_status_code.hpp" + "include/status-code/fmt_support.hpp" "include/status-code/generic_code.hpp" "include/status-code/getaddrinfo_code.hpp" "include/status-code/http_status_code.hpp" @@ -233,6 +234,13 @@ if(NOT PROJECT_IS_DEPENDENCY) RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" ) add_test(NAME test-status-code-p0709a COMMAND $) + + add_executable(test-cxx20-format "test/format.cpp") + target_link_libraries(test-cxx20-format PRIVATE status-code) + set_target_properties(test-cxx20-format PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" + ) + add_test(NAME test-cxx20-format COMMAND $) if(WIN32) add_executable(generate-tables "utils/generate-tables.cpp") diff --git a/include/status-code/fmt_support.hpp b/include/status-code/fmt_support.hpp new file mode 100644 index 0000000..a9cde5e --- /dev/null +++ b/include/status-code/fmt_support.hpp @@ -0,0 +1,85 @@ +/* Proposed SG14 status_code +(C) 2023 Henrik Steffen Gaßmann +File Created: Apr 2023 + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License in the accompanying file +Licence.txt or at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file Licence.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +*/ + +#ifndef SYSTEM_ERROR2_FMT_SUPPORT_HPP +#define SYSTEM_ERROR2_FMT_SUPPORT_HPP + +#include "status_code_domain.hpp" + +SYSTEM_ERROR2_NAMESPACE_BEGIN + +namespace detail +{ + template class Formatter> struct string_ref_formatter : private Formatter + { + private: + using base = Formatter; + + public: + template constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator { return base::parse(ctx); } + + template auto format(const status_code_domain::string_ref &str, FormatContext &ctx) -> typename FormatContext::iterator + { + return base::format(StringView(str.data(), str.size()), ctx); + } + }; +} // namespace detail + +SYSTEM_ERROR2_NAMESPACE_END + +#if __cpp_lib_format >= 202106L +#include + +SYSTEM_ERROR2_NAMESPACE_BEGIN +namespace detail +{ + template using std_formatter = std::formatter; +} +SYSTEM_ERROR2_NAMESPACE_END + +template <> +struct std::formatter + : SYSTEM_ERROR2_NAMESPACE::detail::string_ref_formatter +{ +}; +#endif + +#if __has_include() +#include + +SYSTEM_ERROR2_NAMESPACE_BEGIN +namespace detail +{ + template using fmt_formatter = fmt::formatter; +} +SYSTEM_ERROR2_NAMESPACE_END + +template <> +struct fmt::formatter + : SYSTEM_ERROR2_NAMESPACE::detail::string_ref_formatter +{ +}; +#endif + +#endif diff --git a/test/format.cpp b/test/format.cpp new file mode 100644 index 0000000..8930c0f --- /dev/null +++ b/test/format.cpp @@ -0,0 +1,89 @@ +/* Proposed SG14 status_code testing +(C) 2015-2020 Niall Douglas +(C) 2023 Henrik Steffen Gaßmann +File Created: Apr 2023 + + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License in the accompanying file +Licence.txt or at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + + +Distributed under the Boost Software License, Version 1.0. +(See accompanying file Licence.txt or copy at +http://www.boost.org/LICENSE_1_0.txt) +*/ + +#include "status-code/fmt_support.hpp" +#include "status-code/generic_code.hpp" + +#if __cpp_lib_format >= 202106L + +#include +#include + +/* Most of this test suite was ported over from Boost.Outcome's +experimental-core-result-status.cpp +*/ + +#define BOOST_CHECK(expr) \ + if(!(expr)) \ + { \ + fprintf(stderr, #expr " failed at line %d\n", __LINE__); \ + retcode = 1; \ + } +#define BOOST_CHECK_THROW(expr, etype) \ + try \ + { \ + expr; \ + fprintf(stderr, #expr " failed to throw " #etype " at line %d\n", __LINE__); \ + retcode = 1; \ + } \ + catch(etype) \ + { \ + } \ + catch(...) \ + { \ + fprintf(stderr, #expr " failed to throw " #etype " at line %d\n", __LINE__); \ + retcode = 1; \ + } +#define BOOST_CHECK_NO_THROW(expr) \ + try \ + { \ + expr; \ + } \ + catch(...) \ + { \ + fprintf(stderr, #expr " failed due to throw at line %d\n", __LINE__); \ + retcode = 1; \ + } + +int main() +{ + using namespace std::string_view_literals; + using namespace SYSTEM_ERROR2_NAMESPACE; + int retcode = 0; + + { // format a string_ref + generic_code c{errc::bad_address}; + BOOST_CHECK(std::format("{}", c.message()) == "Bad address"sv); + } + + return retcode; +} + +#else +int main() +{ + return 0; +} +#endif From 6dc8fc7fbb5e71b5c34472df03aa0008e3c3c726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20S=2E=20Ga=C3=9Fmann?= Date: Fri, 14 Apr 2023 10:44:14 +0200 Subject: [PATCH 2/3] build: Prevent `PROJECT_IS_DEPENDENCY` shadowing --- CMakeLists.txt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a001eb8..c231fb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,10 +3,12 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR) project(status-code VERSION 1.0 LANGUAGES CXX) include(GNUInstallDirs) enable_testing() -if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) - set(PROJECT_IS_DEPENDENCY OFF) -else() - set(PROJECT_IS_DEPENDENCY ON) +if (NOT DEFINED PROJECT_IS_DEPENDENCY) # don't override cache variables + if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(PROJECT_IS_DEPENDENCY OFF) + else() + set(PROJECT_IS_DEPENDENCY ON) + endif() endif() # On MSVC very annoyingly cmake puts /EHsc into the global flags which means you From 71f601f395b93b77e56a4049d81cc78019112295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20S=2E=20Ga=C3=9Fmann?= Date: Fri, 14 Apr 2023 10:44:51 +0200 Subject: [PATCH 3/3] ci: Add C++20 target for VS2019 --- .github/workflows/unittests_windows.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/unittests_windows.yml b/.github/workflows/unittests_windows.yml index 26b6203..3b88431 100644 --- a/.github/workflows/unittests_windows.yml +++ b/.github/workflows/unittests_windows.yml @@ -13,6 +13,10 @@ jobs: WinVS2019: name: Windows VS2019 runs-on: windows-2019 + strategy: + fail-fast: false + matrix: + standard: [14, 20] steps: - uses: actions/checkout@v2 @@ -20,10 +24,8 @@ jobs: - name: CMake build tests Windows shell: bash run: | - mkdir build - cd build - cmake .. - cmake --build . --config Release + cmake -S . -B build -DCMAKE_CXX_STANDARD=${{ matrix.standard }} + cmake --build build --config Release - name: CMake run tests Windows shell: bash