From 8979697933753dcb4a3093a013661e67d4e2b361 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 27 Dec 2024 13:35:57 +0200 Subject: [PATCH 001/156] switch to the latest `add_cyphal_library` on Nunavut --- .github/workflows/tests.yml | 4 ++-- .gitmodules | 6 ------ src/daemon/engine/CMakeLists.txt | 11 +++++------ submodules/public_regulated_data_types | 1 - submodules/pydsdl | 1 - 5 files changed, 7 insertions(+), 16 deletions(-) delete mode 160000 submodules/public_regulated_data_types delete mode 160000 submodules/pydsdl diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 34dfe7d..3c8d4da 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -18,7 +18,7 @@ jobs: steps: - uses: actions/checkout@v4 with: - submodules: true + submodules: recursive - run: | wget https://apt.llvm.org/llvm.sh chmod +x llvm.sh @@ -53,7 +53,7 @@ jobs: steps: - uses: actions/checkout@v4 with: - submodules: true + submodules: recursive - run: | sudo apt update -y && sudo apt upgrade -y sudo apt install gcc-multilib g++-multilib ninja-build diff --git a/.gitmodules b/.gitmodules index 03878cc..0b13fe0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -9,12 +9,6 @@ path = submodules/nunavut url = https://github.com/OpenCyphal/nunavut.git branch = 3.0.preview -[submodule "submodules/pydsdl"] - path = submodules/pydsdl - url = https://github.com/OpenCyphal/pydsdl.git -[submodule "submodules/public_regulated_data_types"] - path = submodules/public_regulated_data_types - url = https://github.com/UAVCAN/public_regulated_data_types [submodule "submodules/cetl"] path = submodules/cetl url = https://github.com/OpenCyphal/CETL.git diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 83fdd44..6ff0a9a 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -12,11 +12,11 @@ find_package("Nunavut" 3.0 REQUIRED) # Define type generation and header library all in one go. # set(dsdl_types_in_engine # List all the DSDL types used in the engine - ${submodules_dir}/public_regulated_data_types/uavcan:node/430.GetInfo.1.0.dsdl - ${submodules_dir}/public_regulated_data_types/uavcan:node/435.ExecuteCommand.1.3.dsdl - ${submodules_dir}/public_regulated_data_types/uavcan:node/7509.Heartbeat.1.0.dsdl - ${submodules_dir}/public_regulated_data_types/uavcan:register/384.Access.1.0.dsdl - ${submodules_dir}/public_regulated_data_types/uavcan:register/385.List.1.0.dsdl + uavcan/node/430.GetInfo.1.0.dsdl + uavcan/node/435.ExecuteCommand.1.3.dsdl + uavcan/node/7509.Heartbeat.1.0.dsdl + uavcan/register/384.Access.1.0.dsdl + uavcan/register/385.List.1.0.dsdl ) add_cyphal_library( NAME engine @@ -25,7 +25,6 @@ add_cyphal_library( LANGUAGE cpp LANGUAGE_STANDARD cetl++14-17 OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled - PYDSDL_PATH ${submodules_dir}/pydsdl OUT_LIBRARY_TARGET dsdl_transpiled_headers ) diff --git a/submodules/public_regulated_data_types b/submodules/public_regulated_data_types deleted file mode 160000 index f9f6790..0000000 --- a/submodules/public_regulated_data_types +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f9f67906cc0ca5d7c1b429924852f6b28f313cbf diff --git a/submodules/pydsdl b/submodules/pydsdl deleted file mode 160000 index 97fb20c..0000000 --- a/submodules/pydsdl +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 97fb20cfe34b506c88bd27c6b766a70645e386f7 From 831d23e8a7b4e7fe5d872d73bc680489553e155a Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 27 Dec 2024 14:59:36 +0200 Subject: [PATCH 002/156] fix PLATFORM_OS_TYPE --- CMakeLists.txt | 13 ++-- CMakePresets.json | 68 ++++++++----------- README.md | 15 ++-- src/daemon/engine/CMakeLists.txt | 4 -- src/daemon/engine/platform/defines.hpp | 8 +-- .../epoll_single_threaded_executor.hpp | 10 +-- 6 files changed, 53 insertions(+), 65 deletions(-) rename src/daemon/engine/platform/{debian => linux}/epoll_single_threaded_executor.hpp (96%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 46e9e0f..25acf4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,9 @@ if (NOT NO_STATIC_ANALYSIS) set(CMAKE_CXX_CLANG_TIDY ${clang_tidy}) endif () +# Pull in Nunavut's cmake integration +find_package("Nunavut" 3.0 REQUIRED) + # Forward the revision information to the compiler so that we could expose it at runtime. This is entirely optional. execute_process( COMMAND git rev-parse --short=16 HEAD @@ -67,11 +70,11 @@ add_definitions( -DVCS_REVISION_ID=0x${vcs_revision_id}ULL -DNODE_NAME="org.opencyphal.ocvsmd" ) -if (DEFINED PLATFORM_LINUX_TYPE) - if(${PLATFORM_LINUX_TYPE} STREQUAL "bsd") - add_definitions(-DPLATFORM_LINUX_TYPE_BSD) - elseif(${PLATFORM_LINUX_TYPE} STREQUAL "debian") - add_definitions(-DPLATFORM_LINUX_TYPE_DEBIAN) +if (DEFINED PLATFORM_OS_TYPE) + if(${PLATFORM_OS_TYPE} STREQUAL "bsd") + add_definitions(-DPLATFORM_OS_TYPE_BSD) + elseif(${PLATFORM_OS_TYPE} STREQUAL "linux") + add_definitions(-DPLATFORM_OS_TYPE_LINUX) endif() endif() diff --git a/CMakePresets.json b/CMakePresets.json index 845b9f1..3f63272 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -26,47 +26,35 @@ } }, { - "name": "config-linux-debian", + "name": "config-linux", "hidden": true, "cacheVariables": { - "PLATFORM_LINUX_TYPE": "debian", - "PLATFORM_OS_TYPE": "generic" + "PLATFORM_OS_TYPE": "linux" } }, { - "name": "config-linux-bsd", + "name": "config-bsd", "hidden": true, "cacheVariables": { - "PLATFORM_LINUX_TYPE": "bsd", - "PLATFORM_OS_TYPE": "generic" + "PLATFORM_OS_TYPE": "bsd" } }, { - "name": "config-linux-darwin", - "hidden": true, - "inherits": [ - "config-linux-bsd" - ], - "cacheVariables": { - "PLATFORM_OS_TYPE": "Darwin" - } - }, - { - "name": "OCVSMD-Debian", - "displayName": "Debian Linux OCVSMD", - "description": "Configures OCVSMD for Linux distributions derived from Debian.", + "name": "OCVSMD-Linux", + "displayName": "Linux OCVSMD", + "description": "Configures OCVSMD for Linux.", "inherits": [ "config-common", - "config-linux-debian" + "config-linux" ] }, { - "name": "OCVSMD-Darwin", - "displayName": "Darwin Linux OCVSMD", - "description": "Configures OCVSMD for MacOS", + "name": "OCVSMD-BSD", + "displayName": "BSD OCVSMD", + "description": "Configures OCVSMD for BSD", "inherits": [ "config-common", - "config-linux-darwin" + "config-bsd" ], "cacheVariables": { "CMAKE_C_COMPILER": "clang", @@ -76,40 +64,40 @@ ], "buildPresets": [ { - "name": "OCVSMD-Debian-Debug", - "displayName": "Debian Linux OCVSMD (Debug)", - "description": "Builds OCVSMD for Debian Linux distributions", - "configurePreset": "OCVSMD-Debian", + "name": "OCVSMD-Linux-Debug", + "displayName": "Linux OCVSMD (Debug)", + "description": "Builds OCVSMD for Linux", + "configurePreset": "OCVSMD-Linux", "configuration": "Debug", "targets": [ "ocvsmd" ] }, { - "name": "OCVSMD-Debian-Release", - "displayName": "Debian Linux OCVSMD (Release)", - "description": "Builds OCVSMD for Debian Linux distributions", - "configurePreset": "OCVSMD-Debian", + "name": "OCVSMD-Linux-Release", + "displayName": "Linux OCVSMD (Release)", + "description": "Builds OCVSMD for Linux", + "configurePreset": "OCVSMD-Linux", "configuration": "Release", "targets": [ "ocvsmd" ] }, { - "name": "OCVSMD-Darwin-Debug", - "displayName": "Darwin Linux OCVSMD (Debug)", - "description": "Builds OCVSMD for MacOS", - "configurePreset": "OCVSMD-Darwin", + "name": "OCVSMD-BSD-Debug", + "displayName": "BSD OCVSMD (Debug)", + "description": "Builds OCVSMD for BSD", + "configurePreset": "OCVSMD-BSD", "configuration": "Debug", "targets": [ "ocvsmd" ] }, { - "name": "OCVSMD-Darwin-Release", - "displayName": "Darwin Linux OCVSMD (Release)", - "description": "Builds OCVSMD for MacOS", - "configurePreset": "OCVSMD-Darwin", + "name": "OCVSMD-BSD-Release", + "displayName": "BSD OCVSMD (Release)", + "description": "Builds OCVSMD for BSD", + "configurePreset": "OCVSMD-BSD", "configuration": "Release", "targets": [ "ocvsmd" diff --git a/README.md b/README.md index 363afad..81c6d8c 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,23 @@ # OCVSMD - Open Cyphal Vehicle System Management Daemon ### Build -- Change directory to the project root: +- Change directory to the project root; init submodules: ``` cd ocvsmd + git submodule update --init --recursive ``` Then one of the two presets depending on your system: -- `Demo-Debian` – Debian-based Linux distros like Ubuntu. -- `Demo-Darwin` – MacOS +- `Demo-Linux` – Linux distros like Ubuntu. +- `Demo-BSD` – BSD based like MacOS. ###### Debug ```bash - cmake --preset OCVSMD-Debian - cmake --build --preset OCVSMD-Debian-Debug + cmake --preset OCVSMD-Linux + cmake --build --preset OCVSMD-Linux-Debug ``` ###### Release ```bash - cmake --preset OCVSMD-Debian - cmake --build --preset OCVSMD-Debian-Release + cmake --preset OCVSMD-Linux + cmake --build --preset OCVSMD-Linux-Release ``` ### Installing diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 6ff0a9a..58d39ae 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -5,10 +5,6 @@ cmake_minimum_required(VERSION 3.27) - -# Pull in Nunavut's cmake integration -find_package("Nunavut" 3.0 REQUIRED) - # Define type generation and header library all in one go. # set(dsdl_types_in_engine # List all the DSDL types used in the engine diff --git a/src/daemon/engine/platform/defines.hpp b/src/daemon/engine/platform/defines.hpp index 85dbb52..4b04d09 100644 --- a/src/daemon/engine/platform/defines.hpp +++ b/src/daemon/engine/platform/defines.hpp @@ -6,10 +6,10 @@ #ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_DEFINES_HPP_INCLUDED #define OCVSMD_DAEMON_ENGINE_PLATFORM_DEFINES_HPP_INCLUDED -#ifdef PLATFORM_LINUX_TYPE_BSD +#ifdef PLATFORM_OS_TYPE_BSD # include "bsd/kqueue_single_threaded_executor.hpp" #else -# include "debian/epoll_single_threaded_executor.hpp" +# include "linux/epoll_single_threaded_executor.hpp" #endif namespace ocvsmd @@ -21,10 +21,10 @@ namespace engine namespace platform { -#ifdef PLATFORM_LINUX_TYPE_BSD +#ifdef PLATFORM_OS_TYPE_BSD using SingleThreadedExecutor = bsd::KqueueSingleThreadedExecutor; #else -using SingleThreadedExecutor = debian::EpollSingleThreadedExecutor; +using SingleThreadedExecutor = Linux::EpollSingleThreadedExecutor; #endif } // namespace platform diff --git a/src/daemon/engine/platform/debian/epoll_single_threaded_executor.hpp b/src/daemon/engine/platform/linux/epoll_single_threaded_executor.hpp similarity index 96% rename from src/daemon/engine/platform/debian/epoll_single_threaded_executor.hpp rename to src/daemon/engine/platform/linux/epoll_single_threaded_executor.hpp index 867c550..56be665 100644 --- a/src/daemon/engine/platform/debian/epoll_single_threaded_executor.hpp +++ b/src/daemon/engine/platform/linux/epoll_single_threaded_executor.hpp @@ -3,8 +3,8 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_DEBIAN_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED -#define OCVSMD_DAEMON_ENGINE_PLATFORM_DEBIAN_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED #include "platform/posix_executor_extension.hpp" #include "platform/posix_platform_error.hpp" @@ -39,7 +39,7 @@ namespace engine { namespace platform { -namespace debian +namespace Linux { /// @brief Defines Linux platform specific single-threaded executor based on `epoll` mechanism. @@ -255,10 +255,10 @@ class EpollSingleThreadedExecutor final : public libcyphal::platform::SingleThre }; // EpollSingleThreadedExecutor -} // namespace debian +} // namespace Linux } // namespace platform } // namespace engine } // namespace daemon } // namespace ocvsmd -#endif // OCVSMD_DAEMON_ENGINE_PLATFORM_DEBIAN_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#endif // OCVSMD_DAEMON_ENGINE_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED From 7ce6fae673b3944db390219730de345ed5560b03 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 27 Dec 2024 17:44:29 +0200 Subject: [PATCH 003/156] added nunavut to the common lib --- .github/workflows/tests.yml | 8 +-- include/ocvsmd/sdk/daemon.hpp | 4 +- src/cli/main.cpp | 6 ++- src/common/CMakeLists.txt | 20 +++++++ .../dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl | 4 ++ src/common/dsdl_helpers.hpp | 54 +++++++++++++++++++ src/common/ipc/unix_socket_client.cpp | 37 +++++++++---- src/common/ipc/unix_socket_client.hpp | 13 +++-- src/daemon/engine/CMakeLists.txt | 6 +-- src/sdk/CMakeLists.txt | 3 ++ src/sdk/daemon.cpp | 25 ++++++--- 11 files changed, 150 insertions(+), 30 deletions(-) create mode 100644 src/common/dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl create mode 100644 src/common/dsdl_helpers.hpp diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 3c8d4da..1159444 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,8 +28,8 @@ jobs: sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-$LLVM_VERSION 50 clang-tidy --version - run: | - cmake --preset OCVSMD-Debian -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} - cmake --build --preset OCVSMD-Debian-Debug + cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} + cmake --build --preset OCVSMD-Linux-Debug - uses: actions/upload-artifact@v4 if: always() with: @@ -58,8 +58,8 @@ jobs: sudo apt update -y && sudo apt upgrade -y sudo apt install gcc-multilib g++-multilib ninja-build - run: | - cmake --preset OCVSMD-Debian -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} - cmake --build --preset OCVSMD-Debian-Release + cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} + cmake --build --preset OCVSMD-Linux-Release - uses: actions/upload-artifact@v4 if: always() with: diff --git a/include/ocvsmd/sdk/daemon.hpp b/include/ocvsmd/sdk/daemon.hpp index 769b5b3..722607d 100644 --- a/include/ocvsmd/sdk/daemon.hpp +++ b/include/ocvsmd/sdk/daemon.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_SDK_DAEMON_HPP_INCLUDED #define OCVSMD_SDK_DAEMON_HPP_INCLUDED +#include + #include namespace ocvsmd @@ -18,7 +20,7 @@ namespace sdk class Daemon { public: - static std::unique_ptr make(); + static std::unique_ptr make(cetl::pmr::memory_resource& memory); Daemon(Daemon&&) = delete; Daemon(const Daemon&) = delete; diff --git a/src/cli/main.cpp b/src/cli/main.cpp index ea488e4..5aabf76 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -5,12 +5,16 @@ #include +#include + int main(const int argc, const char** const argv) { (void) argc; (void) argv; - if (auto daemon = ocvsmd::sdk::Daemon::make()) + auto& memory = *cetl::pmr::new_delete_resource(); + + if (auto daemon = ocvsmd::sdk::Daemon::make(memory)) { daemon->send_messages(); } diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d07d99e..a05f87e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -5,10 +5,27 @@ cmake_minimum_required(VERSION 3.27) +set(dsdl_ocvsmd_dir ${CMAKE_CURRENT_SOURCE_DIR}/dsdl/ocvsmd) +file(GLOB_RECURSE dsdl_ocvsmd_files CONFIGURE_DEPENDS ${dsdl_ocvsmd_dir}/*.dsdl) + +add_cyphal_library( + NAME common + DSDL_FILES ${dsdl_ocvsmd_files} + DSDL_NAMESPACES ${dsdl_ocvsmd_dir} + ALLOW_EXPERIMENTAL_LANGUAGES + LANGUAGE cpp + LANGUAGE_STANDARD cetl++14-17 + OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled + OUT_LIBRARY_TARGET common_transpiled +) + add_library(ocvsmd_common ipc/unix_socket_client.cpp ipc/unix_socket_server.cpp ) +target_link_libraries(ocvsmd_common + PUBLIC ${common_transpiled} +) target_include_directories(ocvsmd_common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) @@ -16,3 +33,6 @@ target_include_directories(ocvsmd_common SYSTEM PUBLIC ${submodules_dir}/cetl/include PUBLIC ${submodules_dir}/libcyphal/include ) +add_dependencies(ocvsmd_common + ${common_transpiled} +) diff --git a/src/common/dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl b/src/common/dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl new file mode 100644 index 0000000..57c6163 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl @@ -0,0 +1,4 @@ +int8[<8] some_stuff + +# reserve twice as much as we need. +@extent _offset_.max * 2 diff --git a/src/common/dsdl_helpers.hpp b/src/common/dsdl_helpers.hpp new file mode 100644 index 0000000..0de87b2 --- /dev/null +++ b/src/common/dsdl_helpers.hpp @@ -0,0 +1,54 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED +#define OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED + +#include +#include + +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ + +template +static auto tryPerformOnSerialized(const Message& message, // + const cetl::pmr::memory_resource& memory, + Action&& action) -> std::enable_if_t +{ + // Not in use b/c we use stack buffer for small messages. + (void) memory; + + // Try to serialize the message to raw payload buffer. + // + // Next nolint b/c we use a buffer to serialize the message, so no need to zero it (and performance better). + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + std::array buffer; + // + const auto result_size = serialize( // + message, + // Next nolint & NOSONAR are currently unavoidable. + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + {reinterpret_cast(buffer.data()), BufferSize}); // NOSONAR cpp:S3630, + + if (!result_size) + { + return result_size.error(); + } + + const cetl::span bytes{buffer.data(), result_size.value()}; + return std::forward(action)(bytes); +} + +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED diff --git a/src/common/ipc/unix_socket_client.cpp b/src/common/ipc/unix_socket_client.cpp index 0a2e8a7..52e51e8 100644 --- a/src/common/ipc/unix_socket_client.cpp +++ b/src/common/ipc/unix_socket_client.cpp @@ -5,9 +5,15 @@ #include "unix_socket_client.hpp" +#include "dsdl_helpers.hpp" #include "platform/posix_utils.hpp" +#include +#include + #include +#include +#include #include #include @@ -28,8 +34,9 @@ namespace common namespace ipc { -UnixSocketClient::UnixSocketClient(std::string socket_path) - : socket_path_{std::move(socket_path)} +UnixSocketClient::UnixSocketClient(cetl::pmr::memory_resource& memory, std::string socket_path) + : memory_{memory} + , socket_path_{std::move(socket_path)} , client_fd_{-1} { } @@ -82,16 +89,24 @@ bool UnixSocketClient::connect_to_server() return true; } -void UnixSocketClient::send_message(const std::string& message) const +void UnixSocketClient::send_message(const dsdl::Foo_1_0& foo_message) const { - if (const auto err = platform::posixSyscallError([this, &message] { - // - return ::write(client_fd_, message.c_str(), message.size()); - })) - { - std::cerr << "Failed to write: " << ::strerror(err) << "\n"; - return; - } + using Failure = cetl::variant; + + tryPerformOnSerialized(foo_message, memory_, [this](const cetl::span msg_bytes) { + // + if (const auto err = platform::posixSyscallError([this, msg_bytes] { + // + return ::write(client_fd_, msg_bytes.data(), msg_bytes.size()); + })) + { + std::cerr << "Failed to write: " << ::strerror(err) << "\n"; + } + return 0; + }); constexpr std::size_t buf_size = 256; std::array buffer{}; diff --git a/src/common/ipc/unix_socket_client.hpp b/src/common/ipc/unix_socket_client.hpp index fdc1b8e..9312983 100644 --- a/src/common/ipc/unix_socket_client.hpp +++ b/src/common/ipc/unix_socket_client.hpp @@ -6,6 +6,10 @@ #ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED #define OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED +#include "ocvsmd/common/dsdl/Foo_1_0.hpp" + +#include + #include namespace ocvsmd @@ -18,7 +22,7 @@ namespace ipc class UnixSocketClient final { public: - explicit UnixSocketClient(std::string socket_path); + UnixSocketClient(cetl::pmr::memory_resource& memory, std::string socket_path); UnixSocketClient(UnixSocketClient&&) = delete; UnixSocketClient(const UnixSocketClient&) = delete; @@ -28,11 +32,12 @@ class UnixSocketClient final ~UnixSocketClient(); bool connect_to_server(); - void send_message(const std::string& message) const; + void send_message(const dsdl::Foo_1_0& foo_message) const; private: - std::string socket_path_; - int client_fd_; + cetl::pmr::memory_resource& memory_; + std::string socket_path_; + int client_fd_; }; // UnixSocketClient diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 58d39ae..9d26746 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -21,7 +21,7 @@ add_cyphal_library( LANGUAGE cpp LANGUAGE_STANDARD cetl++14-17 OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled - OUT_LIBRARY_TARGET dsdl_transpiled_headers + OUT_LIBRARY_TARGET engine_transpiled ) add_library(udpard @@ -38,7 +38,7 @@ add_library(ocvsmd_engine ) target_link_libraries(ocvsmd_engine PUBLIC udpard - PUBLIC ${dsdl_transpiled_headers} + PUBLIC ${engine_transpiled} PUBLIC ocvsmd_common ) target_include_directories(ocvsmd_engine @@ -49,5 +49,5 @@ target_include_directories(ocvsmd_engine SYSTEM PUBLIC ${submodules_dir}/libcyphal/include ) add_dependencies(ocvsmd_engine - ${dsdl_transpiled_headers} + ${engine_transpiled} ) diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index 8ca17b8..8ab797e 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -14,3 +14,6 @@ target_link_libraries(ocvsmd_sdk target_include_directories(ocvsmd_sdk PUBLIC ${include_dir} ) +target_include_directories(ocvsmd_sdk SYSTEM + PUBLIC ${submodules_dir}/cetl/include +) diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 486ef0e..7ea4719 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -6,10 +6,13 @@ #include #include "ipc/unix_socket_client.hpp" +#include "ocvsmd/common/dsdl/Foo_1_0.hpp" + +#include #include -#include #include +#include namespace ocvsmd { @@ -21,6 +24,11 @@ namespace class DaemonImpl final : public Daemon { public: + explicit DaemonImpl(cetl::pmr::memory_resource& memory) + : memory_{memory} + { + } + bool connect() { return ipc_client_.connect_to_server(); @@ -28,22 +36,27 @@ class DaemonImpl final : public Daemon void send_messages() const override { - ipc_client_.send_message("Hello, world!"); + common::dsdl::Foo_1_0 foo_message{&memory_}; + foo_message.some_stuff.push_back('A'); // NOLINT + ipc_client_.send_message(foo_message); ::sleep(1); - ipc_client_.send_message("Goodbye, world!"); + + foo_message.some_stuff.push_back('Z'); // NOLINT + ipc_client_.send_message(foo_message); ::sleep(1); } private: - common::ipc::UnixSocketClient ipc_client_{"/var/run/ocvsmd/local.sock"}; + cetl::pmr::memory_resource& memory_; + common::ipc::UnixSocketClient ipc_client_{memory_, "/var/run/ocvsmd/local.sock"}; }; // DaemonImpl } // namespace -std::unique_ptr Daemon::make() +std::unique_ptr Daemon::make(cetl::pmr::memory_resource& memory) { - auto daemon = std::make_unique(); + auto daemon = std::make_unique(memory); if (!daemon->connect()) { return nullptr; From 2585693b7893bc911a1559afab8b4181a0aceb9c Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 30 Dec 2024 17:22:06 +0200 Subject: [PATCH 004/156] added nunavut to the common lib --- src/common/dsdl_helpers.hpp | 43 ++++-- src/common/ipc/unix_socket_base.hpp | 188 ++++++++++++++++++++++++++ src/common/ipc/unix_socket_client.cpp | 52 +------ src/common/ipc/unix_socket_client.hpp | 22 +-- src/common/ipc/unix_socket_server.cpp | 120 ++++++++-------- src/common/ipc/unix_socket_server.hpp | 42 ++++-- src/daemon/engine/application.cpp | 10 +- src/sdk/daemon.cpp | 8 +- 8 files changed, 326 insertions(+), 159 deletions(-) create mode 100644 src/common/ipc/unix_socket_base.hpp diff --git a/src/common/dsdl_helpers.hpp b/src/common/dsdl_helpers.hpp index 0de87b2..f34c39b 100644 --- a/src/common/dsdl_helpers.hpp +++ b/src/common/dsdl_helpers.hpp @@ -6,12 +6,13 @@ #ifndef OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED #define OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED -#include #include #include +#include #include #include +#include #include namespace ocvsmd @@ -19,32 +20,46 @@ namespace ocvsmd namespace common { -template -static auto tryPerformOnSerialized(const Message& message, // - const cetl::pmr::memory_resource& memory, - Action&& action) -> std::enable_if_t +template +static auto tryDeserializePayload(const cetl::span payload, Message& out_message) { - // Not in use b/c we use stack buffer for small messages. - (void) memory; + return deserialize(out_message, {payload.data(), payload.size()}); +} +template +static auto tryPerformOnSerialized(const Message& message, Action&& action) -> std::enable_if_t +{ // Try to serialize the message to raw payload buffer. // // Next nolint b/c we use a buffer to serialize the message, so no need to zero it (and performance better). // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) - std::array buffer; + std::array buffer; // - const auto result_size = serialize( // - message, - // Next nolint & NOSONAR are currently unavoidable. - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - {reinterpret_cast(buffer.data()), BufferSize}); // NOSONAR cpp:S3630, + const auto result_size = serialize(message, {buffer.data(), buffer.size()}); + if (!result_size) + { + return result_size.error(); + } + const cetl::span bytes{buffer.data(), result_size.value()}; + return std::forward(action)(bytes); +} + +template +static auto tryPerformOnSerialized(const Message& message, Action&& action) -> std::enable_if_t +{ + // Try to serialize the message to raw payload buffer. + // + using ArrayOfBytes = std::array; + const std::unique_ptr buffer{new ArrayOfBytes}; + // + const auto result_size = serialize(message, {buffer->data(), buffer->size()}); if (!result_size) { return result_size.error(); } - const cetl::span bytes{buffer.data(), result_size.value()}; + const cetl::span bytes{buffer->data(), result_size.value()}; return std::forward(action)(bytes); } diff --git a/src/common/ipc/unix_socket_base.hpp b/src/common/ipc/unix_socket_base.hpp new file mode 100644 index 0000000..f335db2 --- /dev/null +++ b/src/common/ipc/unix_socket_base.hpp @@ -0,0 +1,188 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_BASE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_UNIX_SOCKET_BASE_HPP_INCLUDED + +#include "dsdl_helpers.hpp" +#include "platform/posix_utils.hpp" + +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +class UnixSocketBase +{ +public: + // Either POSIX errno or a Nunavut one. + using Failure = cetl::variant; + +protected: + template + static Failure writeMessage(const int output_fd, const Message& message) + { + constexpr std::size_t BufferSize = Message::_traits_::SerializationBufferSizeBytes; + constexpr bool IsOnStack = BufferSize <= MsgSmallPayloadSize; + + return tryPerformOnSerialized( // + message, + [output_fd](const auto msg_bytes_span) { + // + // 1. Write the message header. + if (const int err = platform::posixSyscallError([output_fd, msg_bytes_span] { + // + const MsgHeader msg_header{.signature = MsgSignature, + .size = static_cast(msg_bytes_span.size())}; + return ::write(output_fd, &msg_header, sizeof(msg_header)); + })) + { + return err; + } + // 2. Write the message payload. + return platform::posixSyscallError([output_fd, msg_bytes_span] { + // + return ::write(output_fd, msg_bytes_span.data(), msg_bytes_span.size()); + }); + }); + } + + template + static int readAndActOnMessage(const int input_fd, Action&& action) + { + // 1. Read and validate the message header. + // + std::size_t msg_size = 0; + { + MsgHeader msg_header; + ssize_t bytes_read = 0; + if (const auto err = platform::posixSyscallError([input_fd, &msg_header, &bytes_read] { + // + return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); + })) + { + ::syslog(LOG_ERR, "Failed to read message header (fd=%d): %s", input_fd, ::strerror(err)); + return err; + } + + if ((bytes_read != sizeof(msg_header)) || (msg_header.signature != MsgSignature) // + || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) + { + return EINVAL; + } + + msg_size = msg_header.size; + } + + // 2. Read message payload. + // + auto read_and_act = [input_fd, act = std::forward(action)](const cetl::span buf_span) { + // + ssize_t read = 0; + if (const auto err = platform::posixSyscallError([input_fd, buf_span, &read] { + // + return read = ::read(input_fd, buf_span.data(), buf_span.size()); + })) + { + ::syslog(LOG_ERR, "Failed to read message payload (fd=%d): %s", input_fd, ::strerror(err)); + return err; + } + if (read != buf_span.size()) + { + return EINVAL; + } + + const cetl::span const_buf_span{buf_span}; + return std::forward(act)(const_buf_span); + }; + if (msg_size <= MsgSmallPayloadSize) // on stack buffer? + { + std::array buffer; + return read_and_act({buffer.data(), msg_size}); + } + const std::unique_ptr buffer{new std::uint8_t[msg_size]}; + return read_and_act({buffer.get(), msg_size}); + } + + template + static Failure readMessage(const int input_fd, Message& message) + { + // 1. Read and validate the message header. + // + std::size_t msg_size = 0; + { + MsgHeader msg_header; + ssize_t bytes_read = 0; + if (const auto err = platform::posixSyscallError([input_fd, &msg_header, &bytes_read] { + // + return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); + })) + { + return err; + } + + // 2. Validate message header. + if ((bytes_read != sizeof(msg_header)) || (msg_header.signature != MsgSignature) // + || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) + { + return EINVAL; + } + + msg_size = msg_header.size; + } + + // 2. Deserialize message payload. + // + auto read_payload_and_deser_msg = [input_fd, &message](const cetl::span buf_span) { + // + ssize_t read = 0; + if (const auto err = platform::posixSyscallError([input_fd, buf_span, &read] { + // + return read = ::read(input_fd, buf_span.data(), buf_span.size()); + })) + { + return err; + } + if (read != buf_span.size()) + { + return EINVAL; + } + return tryDeserializePayload({buf_span.data(), buf_span.size()}, message); + }; + if (msg_size <= MsgSmallPayloadSize) // on stack buffer? + { + std::array buffer; + return read_payload_and_deser_msg({buffer.data(), msg_size}); + } + const std::unique_ptr buffer{new std::uint8_t[msg_size]}; + return read_payload_and_deser_msg({buffer.get(), msg_size}); + } + +private: + struct MsgHeader + { + std::uint32_t signature; + std::uint32_t size; + }; + + static constexpr std::size_t MsgSmallPayloadSize = 256; + static constexpr std::uint32_t MsgSignature = 0x5356434F; // 'OCVS' + static constexpr std::size_t MsgMaxSize = 1ULL << 20ULL; // 1 MB + +}; // UnixSocketBase + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_UNIX_SOCKET_BASE_HPP_INCLUDED diff --git a/src/common/ipc/unix_socket_client.cpp b/src/common/ipc/unix_socket_client.cpp index 52e51e8..0e77af5 100644 --- a/src/common/ipc/unix_socket_client.cpp +++ b/src/common/ipc/unix_socket_client.cpp @@ -5,24 +5,16 @@ #include "unix_socket_client.hpp" -#include "dsdl_helpers.hpp" #include "platform/posix_utils.hpp" -#include -#include - #include -#include -#include #include -#include #include #include #include #include #include -#include #include #include #include @@ -34,9 +26,8 @@ namespace common namespace ipc { -UnixSocketClient::UnixSocketClient(cetl::pmr::memory_resource& memory, std::string socket_path) - : memory_{memory} - , socket_path_{std::move(socket_path)} +UnixSocketClient::UnixSocketClient(std::string socket_path) + : socket_path_{std::move(socket_path)} , client_fd_{-1} { } @@ -52,7 +43,7 @@ UnixSocketClient::~UnixSocketClient() } } -bool UnixSocketClient::connect_to_server() +bool UnixSocketClient::connectToServer() { CETL_DEBUG_ASSERT(client_fd_ == -1, ""); @@ -89,43 +80,6 @@ bool UnixSocketClient::connect_to_server() return true; } -void UnixSocketClient::send_message(const dsdl::Foo_1_0& foo_message) const -{ - using Failure = cetl::variant; - - tryPerformOnSerialized(foo_message, memory_, [this](const cetl::span msg_bytes) { - // - if (const auto err = platform::posixSyscallError([this, msg_bytes] { - // - return ::write(client_fd_, msg_bytes.data(), msg_bytes.size()); - })) - { - std::cerr << "Failed to write: " << ::strerror(err) << "\n"; - } - return 0; - }); - - constexpr std::size_t buf_size = 256; - std::array buffer{}; - ssize_t bytes_read = 0; - if (const auto err = platform::posixSyscallError([this, &bytes_read, &buffer] { - // - return bytes_read = ::read(client_fd_, buffer.data(), buffer.size() - 1); - })) - { - std::cerr << "Failed to read: " << ::strerror(err) << "\n"; - return; - } - if (bytes_read > 0) - { - buffer[bytes_read] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) - std::cout << "Received: " << buffer.data() << "\n"; - } -} - } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/unix_socket_client.hpp b/src/common/ipc/unix_socket_client.hpp index 9312983..99a7a46 100644 --- a/src/common/ipc/unix_socket_client.hpp +++ b/src/common/ipc/unix_socket_client.hpp @@ -6,9 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED #define OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED -#include "ocvsmd/common/dsdl/Foo_1_0.hpp" - -#include +#include "unix_socket_base.hpp" #include @@ -19,10 +17,10 @@ namespace common namespace ipc { -class UnixSocketClient final +class UnixSocketClient final : public UnixSocketBase { public: - UnixSocketClient(cetl::pmr::memory_resource& memory, std::string socket_path); + explicit UnixSocketClient(std::string socket_path); UnixSocketClient(UnixSocketClient&&) = delete; UnixSocketClient(const UnixSocketClient&) = delete; @@ -31,13 +29,17 @@ class UnixSocketClient final ~UnixSocketClient(); - bool connect_to_server(); - void send_message(const dsdl::Foo_1_0& foo_message) const; + bool connectToServer(); + + template + Failure sendMessage(const Message& message) const + { + return writeMessage(client_fd_, message); + } private: - cetl::pmr::memory_resource& memory_; - std::string socket_path_; - int client_fd_; + std::string socket_path_; + int client_fd_; }; // UnixSocketClient diff --git a/src/common/ipc/unix_socket_server.cpp b/src/common/ipc/unix_socket_server.cpp index ec76b1f..22a59cb 100644 --- a/src/common/ipc/unix_socket_server.cpp +++ b/src/common/ipc/unix_socket_server.cpp @@ -16,10 +16,11 @@ #include #include #include -#include +#include #include #include #include +#include #include #include #include @@ -39,21 +40,22 @@ constexpr int MaxConnections = 5; class ClientContextImpl final : public detail::ClientContext { public: - explicit ClientContextImpl(const int client_fd) - : client_fd_{client_fd} + explicit ClientContextImpl(const UnixSocketServer::ClientId id, const int fd) + : id_{id} + , fd_{fd} { CETL_DEBUG_ASSERT(client_fd != -1, ""); - std::cout << "New client connection on fd=" << client_fd << ".\n"; + ::syslog(LOG_NOTICE, "New client connection on fd=%d (id=%zu).", fd, id); } ~ClientContextImpl() override { - std::cout << "Closing connection on fd=" << client_fd_ << ".\n"; + ::syslog(LOG_NOTICE, "Closing client connection on fd=%d (id=%zu).", fd_, id_); platform::posixSyscallError([this] { // - return ::close(client_fd_); + return ::close(fd_); }); } @@ -62,19 +64,15 @@ class ClientContextImpl final : public detail::ClientContext ClientContextImpl& operator=(ClientContextImpl&&) = delete; ClientContextImpl& operator=(const ClientContextImpl&) = delete; - int getFd() const + void setCallback(libcyphal::IExecutor::Callback::Any&& fd_callback) { - return client_fd_; - } - - void setCallback(libcyphal::IExecutor::Callback::Any&& callback) - { - callback_ = std::move(callback); + fd_callback_ = std::move(fd_callback); } private: - const int client_fd_; - libcyphal::IExecutor::Callback::Any callback_; + const UnixSocketServer::ClientId id_; + const int fd_; + libcyphal::IExecutor::Callback::Any fd_callback_; }; // ClientContextImpl @@ -85,6 +83,7 @@ UnixSocketServer::UnixSocketServer(libcyphal::IExecutor& executor, std::string s , socket_path_{std::move(socket_path)} , server_fd_{-1} , posix_executor_ext_{cetl::rtti_cast(&executor_)} + , client_id_counter_{0} { CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); } @@ -100,16 +99,19 @@ UnixSocketServer::~UnixSocketServer() } } -bool UnixSocketServer::start() +bool UnixSocketServer::start(std::function&& client_event_handler) { CETL_DEBUG_ASSERT(server_fd_ == -1, ""); + CETL_DEBUG_ASSERT(client_event_handler, ""); + + client_event_handler_ = std::move(client_event_handler); if (const auto err = platform::posixSyscallError([this] { // return server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); })) { - std::cerr << "Failed to create socket: " << ::strerror(err) << "\n"; + ::syslog(LOG_ERR, "Failed to create server socket: %s", ::strerror(err)); return false; } @@ -130,7 +132,7 @@ bool UnixSocketServer::start() offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { - std::cerr << "Failed to bind socket: " << ::strerror(err) << "\n"; + ::syslog(LOG_ERR, "Failed to bind server socket: %s", ::strerror(err)); return false; } @@ -139,14 +141,21 @@ bool UnixSocketServer::start() return ::listen(server_fd_, MaxConnections); })) { - std::cerr << "Failed to listen on socket: " << ::strerror(err) << "\n"; + ::syslog(LOG_ERR, "Failed to listen on server socket: %s", ::strerror(err)); return false; } + accept_callback_ = posix_executor_ext_->registerAwaitableCallback( // + [this](const auto&) { + // + handle_accept(); + }, + platform::IPosixExecutorExtension::Trigger::Readable{server_fd_}); + return true; } -void UnixSocketServer::accept() +void UnixSocketServer::handle_accept() { CETL_DEBUG_ASSERT(server_fd_ != -1, ""); @@ -156,75 +165,52 @@ void UnixSocketServer::accept() return client_fd = ::accept(server_fd_, nullptr, nullptr); })) { - std::cerr << "Failed to accept connection: " << ::strerror(err) << "\n"; + ::syslog(LOG_WARNING, "Failed to accept client connection: %s", ::strerror(err)); return; } - handle_client_connection(client_fd); -} - -void UnixSocketServer::handle_client_connection(const int client_fd) -{ CETL_DEBUG_ASSERT(client_fd != -1, ""); CETL_DEBUG_ASSERT(client_contexts_.find(client_fd) == client_contexts_.end(), ""); - auto client_context = std::make_unique(client_fd); + const ClientId new_client_id = ++client_id_counter_; + auto client_context = std::make_unique(new_client_id, client_fd); + // client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( - [this, client_fd](const auto&) { + [this, new_client_id, client_fd](const auto&) { // - handle_client_request(client_fd); + handle_client_request(new_client_id, client_fd); }, platform::IPosixExecutorExtension::Trigger::Readable{client_fd})); client_contexts_.emplace(client_fd, std::move(client_context)); + client_event_handler_(ClientEvent::Connected{new_client_id}); } -void UnixSocketServer::handle_client_request(const int client_fd) +void UnixSocketServer::handle_client_request(const ClientId client_id, const int client_fd) { - CETL_DEBUG_ASSERT(client_fd != -1, ""); - - constexpr std::size_t buf_size = 256; - std::array buffer{}; - ssize_t bytes_read = 0; - if (const auto err = platform::posixSyscallError([client_fd, &bytes_read, &buffer] { + if (const auto err = readAndActOnMessage(client_fd, [this, client_fd](const auto payload) { // - return bytes_read = ::read(client_fd, buffer.data(), buffer.size() - 1); + return client_event_handler_(ClientEvent::Message{.client_id = client_fd, .payload = payload}); })) { - std::cerr << "Failed to read: " << ::strerror(err) << "\n"; - return; - } - if (bytes_read == 0) - { - // EOF which means the client has closed the connection. - client_contexts_.erase(client_fd); - return; - } + if (err == -1) + { + ::syslog(LOG_DEBUG, "End of client stream - closing connection (id=%zu, fd=%d).", client_id, client_fd); + } + else + { + ::syslog(LOG_WARNING, + "Failed to handle client request - closing connection (id=%zu, fd=%d): %s", + client_id, + client_fd, + ::strerror(err)); + } - buffer[bytes_read] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) - std::cout << "Received: " << buffer.data() << "\n"; - - // Echo back - // - if (const auto err = platform::posixSyscallError([client_fd, bytes_read, &buffer] { - // - return ::write(client_fd, buffer.data(), bytes_read); - })) - { - std::cerr << "Failed to write: " << ::strerror(err) << "\n"; + client_contexts_.erase(client_fd); + client_event_handler_(ClientEvent::Disconnected{client_id}); } } -CETL_NODISCARD libcyphal::IExecutor::Callback::Any UnixSocketServer::registerListenCallback( - libcyphal::IExecutor::Callback::Function&& function) const -{ - CETL_DEBUG_ASSERT(udp_handle_.fd >= 0, ""); - - return posix_executor_ext_->registerAwaitableCallback( // - std::move(function), - platform::IPosixExecutorExtension::Trigger::Readable{server_fd_}); -} - } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/unix_socket_server.hpp b/src/common/ipc/unix_socket_server.hpp index e6149f0..d9d0439 100644 --- a/src/common/ipc/unix_socket_server.hpp +++ b/src/common/ipc/unix_socket_server.hpp @@ -7,10 +7,13 @@ #define OCVSMD_COMMON_IPC_UNIX_SOCKET_SERVER_HPP_INCLUDED #include "platform/posix_executor_extension.hpp" +#include "unix_socket_base.hpp" -#include +#include #include +#include +#include #include #include #include @@ -40,9 +43,31 @@ class ClientContext } // namespace detail -class UnixSocketServer final +class UnixSocketServer final : public UnixSocketBase { public: + using ClientId = std::size_t; + + struct ClientEvent + { + struct Message + { + ClientId client_id; + cetl::span payload; + }; + struct Connected + { + ClientId client_id; + }; + struct Disconnected + { + ClientId client_id; + }; + + using Var = cetl::variant; + + }; // ClientEvent + UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path); UnixSocketServer(UnixSocketServer&&) = delete; @@ -52,22 +77,21 @@ class UnixSocketServer final ~UnixSocketServer(); - bool start(); - - CETL_NODISCARD libcyphal::IExecutor::Callback::Any registerListenCallback( - libcyphal::IExecutor::Callback::Function&& function) const; - - void accept(); + bool start(std::function&& client_event_handler); private: + void handle_accept(); void handle_client_connection(const int client_fd); - void handle_client_request(const int client_fd); + void handle_client_request(const ClientId client_id, const int client_fd); libcyphal::IExecutor& executor_; const std::string socket_path_; int server_fd_; platform::IPosixExecutorExtension* const posix_executor_ext_; + ClientId client_id_counter_; + std::function client_event_handler_; std::unordered_map> client_contexts_; + libcyphal::IExecutor::Callback::Any accept_callback_; }; // UnixSocketServer diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index ee52ac6..609c02b 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -57,16 +57,14 @@ cetl::optional Application::init() .setSoftwareVcsRevisionId(VCS_REVISION_ID) .setUniqueId(getUniqueId()); - if (!ipc_server_.start()) + if (!ipc_server_.start([](const auto& client_event) { + // + (void) client_event; + })) { return "Failed to start IPC server."; } - ipc_server_callback_ = ipc_server_.registerListenCallback([this](const auto&) { - // - ipc_server_.accept(); - }); - return cetl::nullopt; } diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 7ea4719..48d300d 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -31,24 +31,24 @@ class DaemonImpl final : public Daemon bool connect() { - return ipc_client_.connect_to_server(); + return ipc_client_.connectToServer(); } void send_messages() const override { common::dsdl::Foo_1_0 foo_message{&memory_}; foo_message.some_stuff.push_back('A'); // NOLINT - ipc_client_.send_message(foo_message); + ipc_client_.sendMessage(foo_message); ::sleep(1); foo_message.some_stuff.push_back('Z'); // NOLINT - ipc_client_.send_message(foo_message); + ipc_client_.sendMessage(foo_message); ::sleep(1); } private: cetl::pmr::memory_resource& memory_; - common::ipc::UnixSocketClient ipc_client_{memory_, "/var/run/ocvsmd/local.sock"}; + common::ipc::UnixSocketClient ipc_client_{"/var/run/ocvsmd/local.sock"}; }; // DaemonImpl From 8aea2158dd6f8bcd3048c7eca7f52911955f69fc Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 31 Dec 2024 14:09:53 +0200 Subject: [PATCH 005/156] build fixes --- src/common/ipc/unix_socket_base.hpp | 7 ++++++- src/common/ipc/unix_socket_server.cpp | 16 ++++++++++------ src/common/ipc/unix_socket_server.hpp | 20 ++++++++++++++++---- src/daemon/engine/application.cpp | 25 +++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/common/ipc/unix_socket_base.hpp b/src/common/ipc/unix_socket_base.hpp index f335db2..356d7fc 100644 --- a/src/common/ipc/unix_socket_base.hpp +++ b/src/common/ipc/unix_socket_base.hpp @@ -75,6 +75,11 @@ class UnixSocketBase return err; } + if (bytes_read == 0) + { + return -1; // EOF + } + if ((bytes_read != sizeof(msg_header)) || (msg_header.signature != MsgSignature) // || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) { @@ -103,7 +108,7 @@ class UnixSocketBase } const cetl::span const_buf_span{buf_span}; - return std::forward(act)(const_buf_span); + return act(const_buf_span); }; if (msg_size <= MsgSmallPayloadSize) // on stack buffer? { diff --git a/src/common/ipc/unix_socket_server.cpp b/src/common/ipc/unix_socket_server.cpp index 22a59cb..dba27e9 100644 --- a/src/common/ipc/unix_socket_server.cpp +++ b/src/common/ipc/unix_socket_server.cpp @@ -83,7 +83,7 @@ UnixSocketServer::UnixSocketServer(libcyphal::IExecutor& executor, std::string s , socket_path_{std::move(socket_path)} , server_fd_{-1} , posix_executor_ext_{cetl::rtti_cast(&executor_)} - , client_id_counter_{0} + , unique_client_id_counter_{0} { CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); } @@ -172,7 +172,7 @@ void UnixSocketServer::handle_accept() CETL_DEBUG_ASSERT(client_fd != -1, ""); CETL_DEBUG_ASSERT(client_contexts_.find(client_fd) == client_contexts_.end(), ""); - const ClientId new_client_id = ++client_id_counter_; + const ClientId new_client_id = ++unique_client_id_counter_; auto client_context = std::make_unique(new_client_id, client_fd); // client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( @@ -182,15 +182,17 @@ void UnixSocketServer::handle_accept() }, platform::IPosixExecutorExtension::Trigger::Readable{client_fd})); - client_contexts_.emplace(client_fd, std::move(client_context)); + client_id_to_fd_[new_client_id] = client_fd; + client_fd_to_context_.emplace(client_fd, std::move(client_context)); + client_event_handler_(ClientEvent::Connected{new_client_id}); } void UnixSocketServer::handle_client_request(const ClientId client_id, const int client_fd) { - if (const auto err = readAndActOnMessage(client_fd, [this, client_fd](const auto payload) { + if (const auto err = readAndActOnMessage(client_fd, [this, client_id](const auto payload) { // - return client_event_handler_(ClientEvent::Message{.client_id = client_fd, .payload = payload}); + return client_event_handler_(ClientEvent::Message{.client_id = client_id, .payload = payload}); })) { if (err == -1) @@ -206,7 +208,9 @@ void UnixSocketServer::handle_client_request(const ClientId client_id, const int ::strerror(err)); } - client_contexts_.erase(client_fd); + client_id_to_fd_.erase(client_id); + client_fd_to_context_.erase(client_fd); + client_event_handler_(ClientEvent::Disconnected{client_id}); } } diff --git a/src/common/ipc/unix_socket_server.hpp b/src/common/ipc/unix_socket_server.hpp index d9d0439..856250c 100644 --- a/src/common/ipc/unix_socket_server.hpp +++ b/src/common/ipc/unix_socket_server.hpp @@ -52,8 +52,8 @@ class UnixSocketServer final : public UnixSocketBase { struct Message { - ClientId client_id; - cetl::span payload; + ClientId client_id; + cetl::span payload; }; struct Connected { @@ -79,6 +79,17 @@ class UnixSocketServer final : public UnixSocketBase bool start(std::function&& client_event_handler); + template + Failure sendMessage(const ClientId client_id, const Message& message) const + { + const auto id_and_fd = client_id_to_fd_.find(client_id); + if (id_and_fd == client_id_to_fd_.end()) + { + return EINVAL; + } + return writeMessage(id_and_fd->second, message); + } + private: void handle_accept(); void handle_client_connection(const int client_fd); @@ -88,9 +99,10 @@ class UnixSocketServer final : public UnixSocketBase const std::string socket_path_; int server_fd_; platform::IPosixExecutorExtension* const posix_executor_ext_; - ClientId client_id_counter_; + ClientId unique_client_id_counter_; std::function client_event_handler_; - std::unordered_map> client_contexts_; + std::unordered_map> client_fd_to_context_; + std::unordered_map client_id_to_fd_; libcyphal::IExecutor::Callback::Any accept_callback_; }; // UnixSocketServer diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 609c02b..2f158be 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -4,8 +4,10 @@ // #include "application.hpp" +#include "ocvsmd/common/dsdl/Foo_1_0.hpp" #include +#include #include #include @@ -57,9 +59,28 @@ cetl::optional Application::init() .setSoftwareVcsRevisionId(VCS_REVISION_ID) .setUniqueId(getUniqueId()); - if (!ipc_server_.start([](const auto& client_event) { + if (!ipc_server_.start([this](const auto& client_event) { // - (void) client_event; + using ClientEvent = common::ipc::UnixSocketServer::ClientEvent; + + cetl::visit( // + cetl::make_overloaded( + [](const ClientEvent::Connected& connected) { + // + ::syslog(LOG_DEBUG, "Client connected (%zu).", connected.client_id); + }, + [this](const ClientEvent::Message& message) { + // + ::syslog(LOG_DEBUG, "Client msg (%zu).", message.client_id); + const common::dsdl::Foo_1_0 foo_message{&memory_}; + (void) ipc_server_.sendMessage(message.client_id, foo_message); + }, + [](const ClientEvent::Disconnected& disconnected) { + // + ::syslog(LOG_DEBUG, "Client disconnected (%zu).", disconnected.client_id); + }), + client_event); + return 0; })) { return "Failed to start IPC server."; From 428fb4a5f3ce3ade6b3d49a912bd86375ef48499 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 31 Dec 2024 14:58:58 +0200 Subject: [PATCH 006/156] remove de/serialization from socket client/server --- src/common/ipc/unix_socket_base.hpp | 109 ++++++-------------------- src/common/ipc/unix_socket_client.hpp | 7 +- src/common/ipc/unix_socket_server.cpp | 4 +- src/common/ipc/unix_socket_server.hpp | 7 +- src/daemon/engine/application.cpp | 5 +- src/sdk/daemon.cpp | 9 +-- 6 files changed, 38 insertions(+), 103 deletions(-) diff --git a/src/common/ipc/unix_socket_base.hpp b/src/common/ipc/unix_socket_base.hpp index 356d7fc..21a31fc 100644 --- a/src/common/ipc/unix_socket_base.hpp +++ b/src/common/ipc/unix_socket_base.hpp @@ -9,10 +9,15 @@ #include "dsdl_helpers.hpp" #include "platform/posix_utils.hpp" -#include +#include +#include +#include #include +#include +#include #include +#include #include namespace ocvsmd @@ -24,43 +29,31 @@ namespace ipc class UnixSocketBase { -public: - // Either POSIX errno or a Nunavut one. - using Failure = cetl::variant; - protected: - template - static Failure writeMessage(const int output_fd, const Message& message) + static int sendMessage(const int output_fd, const cetl::span payload) { - constexpr std::size_t BufferSize = Message::_traits_::SerializationBufferSizeBytes; - constexpr bool IsOnStack = BufferSize <= MsgSmallPayloadSize; - - return tryPerformOnSerialized( // - message, - [output_fd](const auto msg_bytes_span) { + // 1. Write the message header. + if (const int err = platform::posixSyscallError([output_fd, payload] { // - // 1. Write the message header. - if (const int err = platform::posixSyscallError([output_fd, msg_bytes_span] { - // - const MsgHeader msg_header{.signature = MsgSignature, - .size = static_cast(msg_bytes_span.size())}; - return ::write(output_fd, &msg_header, sizeof(msg_header)); - })) - { - return err; - } - // 2. Write the message payload. - return platform::posixSyscallError([output_fd, msg_bytes_span] { - // - return ::write(output_fd, msg_bytes_span.data(), msg_bytes_span.size()); - }); - }); + const MsgHeader msg_header{.signature = MsgSignature, + .size = static_cast(payload.size())}; + return ::write(output_fd, &msg_header, sizeof(msg_header)); + })) + { + return err; + } + + // 2. Write the message payload. + return platform::posixSyscallError([output_fd, payload] { + // + return ::write(output_fd, payload.data(), payload.size()); + }); } template - static int readAndActOnMessage(const int input_fd, Action&& action) + static int receiveMessage(const int input_fd, Action&& action) { - // 1. Read and validate the message header. + // 1. Receive and validate the message header. // std::size_t msg_size = 0; { @@ -119,60 +112,6 @@ class UnixSocketBase return read_and_act({buffer.get(), msg_size}); } - template - static Failure readMessage(const int input_fd, Message& message) - { - // 1. Read and validate the message header. - // - std::size_t msg_size = 0; - { - MsgHeader msg_header; - ssize_t bytes_read = 0; - if (const auto err = platform::posixSyscallError([input_fd, &msg_header, &bytes_read] { - // - return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); - })) - { - return err; - } - - // 2. Validate message header. - if ((bytes_read != sizeof(msg_header)) || (msg_header.signature != MsgSignature) // - || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) - { - return EINVAL; - } - - msg_size = msg_header.size; - } - - // 2. Deserialize message payload. - // - auto read_payload_and_deser_msg = [input_fd, &message](const cetl::span buf_span) { - // - ssize_t read = 0; - if (const auto err = platform::posixSyscallError([input_fd, buf_span, &read] { - // - return read = ::read(input_fd, buf_span.data(), buf_span.size()); - })) - { - return err; - } - if (read != buf_span.size()) - { - return EINVAL; - } - return tryDeserializePayload({buf_span.data(), buf_span.size()}, message); - }; - if (msg_size <= MsgSmallPayloadSize) // on stack buffer? - { - std::array buffer; - return read_payload_and_deser_msg({buffer.data(), msg_size}); - } - const std::unique_ptr buffer{new std::uint8_t[msg_size]}; - return read_payload_and_deser_msg({buffer.get(), msg_size}); - } - private: struct MsgHeader { diff --git a/src/common/ipc/unix_socket_client.hpp b/src/common/ipc/unix_socket_client.hpp index 99a7a46..e25deae 100644 --- a/src/common/ipc/unix_socket_client.hpp +++ b/src/common/ipc/unix_socket_client.hpp @@ -8,6 +8,8 @@ #include "unix_socket_base.hpp" +#include + #include namespace ocvsmd @@ -31,10 +33,9 @@ class UnixSocketClient final : public UnixSocketBase bool connectToServer(); - template - Failure sendMessage(const Message& message) const + int sendMessage(const cetl::span payload) const { - return writeMessage(client_fd_, message); + return UnixSocketBase::sendMessage(client_fd_, payload); } private: diff --git a/src/common/ipc/unix_socket_server.cpp b/src/common/ipc/unix_socket_server.cpp index dba27e9..4ac929a 100644 --- a/src/common/ipc/unix_socket_server.cpp +++ b/src/common/ipc/unix_socket_server.cpp @@ -13,7 +13,6 @@ #include #include -#include #include #include #include @@ -21,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -190,7 +188,7 @@ void UnixSocketServer::handle_accept() void UnixSocketServer::handle_client_request(const ClientId client_id, const int client_fd) { - if (const auto err = readAndActOnMessage(client_fd, [this, client_id](const auto payload) { + if (const auto err = receiveMessage(client_fd, [this, client_id](const auto payload) { // return client_event_handler_(ClientEvent::Message{.client_id = client_id, .payload = payload}); })) diff --git a/src/common/ipc/unix_socket_server.hpp b/src/common/ipc/unix_socket_server.hpp index 856250c..c3d8d16 100644 --- a/src/common/ipc/unix_socket_server.hpp +++ b/src/common/ipc/unix_socket_server.hpp @@ -10,8 +10,10 @@ #include "unix_socket_base.hpp" #include +#include #include +#include #include #include #include @@ -79,15 +81,14 @@ class UnixSocketServer final : public UnixSocketBase bool start(std::function&& client_event_handler); - template - Failure sendMessage(const ClientId client_id, const Message& message) const + int sendMessage(const ClientId client_id, const cetl::span payload) const { const auto id_and_fd = client_id_to_fd_.find(client_id); if (id_and_fd == client_id_to_fd_.end()) { return EINVAL; } - return writeMessage(id_and_fd->second, message); + return UnixSocketBase::sendMessage(id_and_fd->second, payload); } private: diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 2f158be..2a1f836 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -4,7 +4,6 @@ // #include "application.hpp" -#include "ocvsmd/common/dsdl/Foo_1_0.hpp" #include #include @@ -17,6 +16,7 @@ #include #include #include +#include #include namespace ocvsmd @@ -72,8 +72,7 @@ cetl::optional Application::init() [this](const ClientEvent::Message& message) { // ::syslog(LOG_DEBUG, "Client msg (%zu).", message.client_id); - const common::dsdl::Foo_1_0 foo_message{&memory_}; - (void) ipc_server_.sendMessage(message.client_id, foo_message); + (void) ipc_server_.sendMessage(message.client_id, message.payload); }, [](const ClientEvent::Disconnected& disconnected) { // diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 48d300d..83e592f 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -6,10 +6,10 @@ #include #include "ipc/unix_socket_client.hpp" -#include "ocvsmd/common/dsdl/Foo_1_0.hpp" #include +#include #include #include #include @@ -36,13 +36,10 @@ class DaemonImpl final : public Daemon void send_messages() const override { - common::dsdl::Foo_1_0 foo_message{&memory_}; - foo_message.some_stuff.push_back('A'); // NOLINT - ipc_client_.sendMessage(foo_message); + ipc_client_.sendMessage({reinterpret_cast("Abc"), 3}); // NOLINT ::sleep(1); - foo_message.some_stuff.push_back('Z'); // NOLINT - ipc_client_.sendMessage(foo_message); + ipc_client_.sendMessage({reinterpret_cast("xyZ"), 3}); // NOLINT ::sleep(1); } From f8ac79eeead58da493f975cd3ea57dcc27583716 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 31 Dec 2024 18:29:36 +0200 Subject: [PATCH 007/156] remove de/serialization from socket client/server --- .../bsd/kqueue_single_threaded_executor.hpp | 16 ++-- .../ocvsmd}/platform/defines.hpp | 12 +-- .../linux/epoll_single_threaded_executor.hpp | 18 ++-- .../platform/posix_executor_extension.hpp | 9 +- .../ocvsmd}/platform/posix_platform_error.hpp | 12 +-- .../ocvsmd}/platform/posix_utils.hpp | 9 +- include/ocvsmd/sdk/daemon.hpp | 5 +- src/cli/main.cpp | 84 +++++++++++++++++-- src/common/CMakeLists.txt | 1 + src/common/ipc/unix_socket_base.hpp | 8 +- src/common/ipc/unix_socket_client.cpp | 59 +++++++++++-- src/common/ipc/unix_socket_client.hpp | 32 ++++++- src/common/ipc/unix_socket_server.cpp | 31 ++++--- src/common/ipc/unix_socket_server.hpp | 5 +- src/daemon/engine/application.cpp | 12 ++- src/daemon/engine/application.hpp | 4 +- .../engine/platform/udp/udp_sockets.hpp | 20 ++--- src/daemon/main.cpp | 64 ++++++++------ src/sdk/CMakeLists.txt | 1 + src/sdk/daemon.cpp | 42 ++++++---- 20 files changed, 293 insertions(+), 151 deletions(-) rename {src/daemon/engine => include/ocvsmd}/platform/bsd/kqueue_single_threaded_executor.hpp (95%) rename {src/daemon/engine => include/ocvsmd}/platform/defines.hpp (66%) rename {src/daemon/engine => include/ocvsmd}/platform/linux/epoll_single_threaded_executor.hpp (93%) rename {src/common => include/ocvsmd}/platform/posix_executor_extension.hpp (88%) rename {src/daemon/engine => include/ocvsmd}/platform/posix_platform_error.hpp (79%) rename {src/common => include/ocvsmd}/platform/posix_utils.hpp (68%) diff --git a/src/daemon/engine/platform/bsd/kqueue_single_threaded_executor.hpp b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp similarity index 95% rename from src/daemon/engine/platform/bsd/kqueue_single_threaded_executor.hpp rename to include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp index d3d512d..5524437 100644 --- a/src/daemon/engine/platform/bsd/kqueue_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp @@ -3,11 +3,11 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_BSD_KQUEUE_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED -#define OCVSMD_DAEMON_ENGINE_PLATFORM_BSD_KQUEUE_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#ifndef OCVSMD_PLATFORM_BSD_KQUEUE_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#define OCVSMD_PLATFORM_BSD_KQUEUE_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED -#include "platform/posix_executor_extension.hpp" -#include "platform/posix_platform_error.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_platform_error.hpp" #include #include @@ -35,10 +35,6 @@ namespace ocvsmd { -namespace daemon -{ -namespace engine -{ namespace platform { namespace bsd @@ -273,8 +269,6 @@ class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThr } // namespace bsd } // namespace platform -} // namespace engine -} // namespace daemon } // namespace ocvsmd -#endif // OCVSMD_DAEMON_ENGINE_PLATFORM_BSD_KQUEUE_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#endif // OCVSMD_PLATFORM_BSD_KQUEUE_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED diff --git a/src/daemon/engine/platform/defines.hpp b/include/ocvsmd/platform/defines.hpp similarity index 66% rename from src/daemon/engine/platform/defines.hpp rename to include/ocvsmd/platform/defines.hpp index 4b04d09..98a9746 100644 --- a/src/daemon/engine/platform/defines.hpp +++ b/include/ocvsmd/platform/defines.hpp @@ -3,8 +3,8 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_DEFINES_HPP_INCLUDED -#define OCVSMD_DAEMON_ENGINE_PLATFORM_DEFINES_HPP_INCLUDED +#ifndef OCVSMD_PLATFORM_DEFINES_HPP_INCLUDED +#define OCVSMD_PLATFORM_DEFINES_HPP_INCLUDED #ifdef PLATFORM_OS_TYPE_BSD # include "bsd/kqueue_single_threaded_executor.hpp" @@ -14,10 +14,6 @@ namespace ocvsmd { -namespace daemon -{ -namespace engine -{ namespace platform { @@ -28,8 +24,6 @@ using SingleThreadedExecutor = Linux::EpollSingleThreadedExecutor; #endif } // namespace platform -} // namespace engine -} // namespace daemon } // namespace ocvsmd -#endif // OCVSMD_DAEMON_ENGINE_PLATFORM_DEFINES_HPP_INCLUDED +#endif // OCVSMD_PLATFORM_DEFINES_HPP_INCLUDED diff --git a/src/daemon/engine/platform/linux/epoll_single_threaded_executor.hpp b/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp similarity index 93% rename from src/daemon/engine/platform/linux/epoll_single_threaded_executor.hpp rename to include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp index 56be665..02ccbb9 100644 --- a/src/daemon/engine/platform/linux/epoll_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp @@ -3,11 +3,11 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED -#define OCVSMD_DAEMON_ENGINE_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#ifndef OCVSMD_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#define OCVSMD_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED -#include "platform/posix_executor_extension.hpp" -#include "platform/posix_platform_error.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_platform_error.hpp" #include #include @@ -33,10 +33,6 @@ namespace ocvsmd { -namespace daemon -{ -namespace engine -{ namespace platform { namespace Linux @@ -45,7 +41,7 @@ namespace Linux /// @brief Defines Linux platform specific single-threaded executor based on `epoll` mechanism. /// class EpollSingleThreadedExecutor final : public libcyphal::platform::SingleThreadedExecutor, - public common::platform::IPosixExecutorExtension + public IPosixExecutorExtension { public: EpollSingleThreadedExecutor() @@ -257,8 +253,6 @@ class EpollSingleThreadedExecutor final : public libcyphal::platform::SingleThre } // namespace Linux } // namespace platform -} // namespace engine -} // namespace daemon } // namespace ocvsmd -#endif // OCVSMD_DAEMON_ENGINE_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED +#endif // OCVSMD_PLATFORM_LINUX_EPOLL_SINGLE_THREADED_EXECUTOR_HPP_INCLUDED diff --git a/src/common/platform/posix_executor_extension.hpp b/include/ocvsmd/platform/posix_executor_extension.hpp similarity index 88% rename from src/common/platform/posix_executor_extension.hpp rename to include/ocvsmd/platform/posix_executor_extension.hpp index 80149ba..dc78087 100644 --- a/src/common/platform/posix_executor_extension.hpp +++ b/include/ocvsmd/platform/posix_executor_extension.hpp @@ -3,8 +3,8 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_PLATFORM_POSIX_EXECUTOR_EXTENSION_HPP_INCLUDED -#define OCVSMD_COMMON_PLATFORM_POSIX_EXECUTOR_EXTENSION_HPP_INCLUDED +#ifndef OCVSMD_PLATFORM_POSIX_EXECUTOR_EXTENSION_HPP_INCLUDED +#define OCVSMD_PLATFORM_POSIX_EXECUTOR_EXTENSION_HPP_INCLUDED #include #include @@ -14,8 +14,6 @@ namespace ocvsmd { -namespace common -{ namespace platform { @@ -69,7 +67,6 @@ class IPosixExecutorExtension }; // IPosixExecutorExtension } // namespace platform -} // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_PLATFORM_POSIX_EXECUTOR_EXTENSION_HPP_INCLUDED +#endif // OCVSMD_PLATFORM_POSIX_EXECUTOR_EXTENSION_HPP_INCLUDED diff --git a/src/daemon/engine/platform/posix_platform_error.hpp b/include/ocvsmd/platform/posix_platform_error.hpp similarity index 79% rename from src/daemon/engine/platform/posix_platform_error.hpp rename to include/ocvsmd/platform/posix_platform_error.hpp index c038555..5cce104 100644 --- a/src/daemon/engine/platform/posix_platform_error.hpp +++ b/include/ocvsmd/platform/posix_platform_error.hpp @@ -3,8 +3,8 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_POSIX_PLATFORM_ERROR_HPP_INCLUDED -#define OCVSMD_DAEMON_ENGINE_PLATFORM_POSIX_PLATFORM_ERROR_HPP_INCLUDED +#ifndef OCVSMD_PLATFORM_POSIX_PLATFORM_ERROR_HPP_INCLUDED +#define OCVSMD_PLATFORM_POSIX_PLATFORM_ERROR_HPP_INCLUDED #include #include @@ -13,10 +13,6 @@ namespace ocvsmd { -namespace daemon -{ -namespace engine -{ namespace platform { @@ -51,8 +47,6 @@ class PosixPlatformError final : public libcyphal::transport::IPlatformError }; // PosixPlatformError } // namespace platform -} // namespace engine -} // namespace daemon } // namespace ocvsmd -#endif // OCVSMD_DAEMON_ENGINE_PLATFORM_POSIX_PLATFORM_ERROR_HPP_INCLUDED +#endif // OCVSMD_PLATFORM_POSIX_PLATFORM_ERROR_HPP_INCLUDED diff --git a/src/common/platform/posix_utils.hpp b/include/ocvsmd/platform/posix_utils.hpp similarity index 68% rename from src/common/platform/posix_utils.hpp rename to include/ocvsmd/platform/posix_utils.hpp index 566db41..ad28adb 100644 --- a/src/common/platform/posix_utils.hpp +++ b/include/ocvsmd/platform/posix_utils.hpp @@ -3,15 +3,13 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_PLATFORM_POSIX_UTILS_HPP_INCLUDED -#define OCVSMD_COMMON_PLATFORM_POSIX_UTILS_HPP_INCLUDED +#ifndef OCVSMD_PLATFORM_POSIX_UTILS_HPP_INCLUDED +#define OCVSMD_PLATFORM_POSIX_UTILS_HPP_INCLUDED #include namespace ocvsmd { -namespace common -{ namespace platform { @@ -30,7 +28,6 @@ int posixSyscallError(const Call& call) } } // namespace platform -} // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_PLATFORM_POSIX_UTILS_HPP_INCLUDED +#endif // OCVSMD_PLATFORM_POSIX_UTILS_HPP_INCLUDED diff --git a/include/ocvsmd/sdk/daemon.hpp b/include/ocvsmd/sdk/daemon.hpp index 722607d..cfdfb89 100644 --- a/include/ocvsmd/sdk/daemon.hpp +++ b/include/ocvsmd/sdk/daemon.hpp @@ -7,6 +7,7 @@ #define OCVSMD_SDK_DAEMON_HPP_INCLUDED #include +#include #include @@ -20,7 +21,7 @@ namespace sdk class Daemon { public: - static std::unique_ptr make(cetl::pmr::memory_resource& memory); + static std::unique_ptr make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor); Daemon(Daemon&&) = delete; Daemon(const Daemon&) = delete; @@ -29,8 +30,6 @@ class Daemon virtual ~Daemon() = default; - virtual void send_messages() const = 0; - protected: Daemon() = default; diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 5aabf76..2e68862 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -3,21 +3,93 @@ // SPDX-License-Identifier: MIT // +#include #include #include +#include -int main(const int argc, const char** const argv) +#include +#include +#include +#include +#include +#include + +namespace +{ + +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +volatile sig_atomic_t g_running = 1; + +void signal_handler(const int sig) +{ + switch (sig) + { + case SIGINT: + case SIGTERM: + g_running = 0; + break; + default: + break; + } +} + +void setup_signal_handlers() { - (void) argc; - (void) argv; + struct sigaction sigbreak + {}; + sigbreak.sa_handler = &signal_handler; + ::sigaction(SIGINT, &sigbreak, nullptr); + ::sigaction(SIGTERM, &sigbreak, nullptr); +} + +} // namespace - auto& memory = *cetl::pmr::new_delete_resource(); +int main(const int, const char** const) +{ + using std::chrono_literals::operator""s; - if (auto daemon = ocvsmd::sdk::Daemon::make(memory)) + setup_signal_handlers(); + + ::openlog("ocvsmd-cli", LOG_PID, LOG_USER); + ::syslog(LOG_NOTICE, "ocvsmd cli started."); // NOLINT *-vararg { - daemon->send_messages(); + auto& memory = *cetl::pmr::new_delete_resource(); + ocvsmd::platform::SingleThreadedExecutor executor; + + const auto daemon = ocvsmd::sdk::Daemon::make(memory, executor); + if (!daemon) + { + std::cerr << "Failed to create daemon.\n"; + return EXIT_FAILURE; + } + + while (g_running != 0) + { + const auto spin_result = executor.spinOnce(); + + // Poll awaitable resources but awake at least once per second. + libcyphal::Duration timeout{1s}; + if (spin_result.next_exec_time.has_value()) + { + timeout = std::min(timeout, spin_result.next_exec_time.value() - executor.now()); + } + + // TODO: Don't ignore polling failures; come up with a strategy to handle them. + // Probably we should log it, break the loop, + // and exit with a failure code (b/c it is a critical and unexpected error). + auto maybe_poll_failure = executor.pollAwaitableResourcesFor(cetl::make_optional(timeout)); + (void) maybe_poll_failure; + } + + if (g_running == 0) + { + ::syslog(LOG_NOTICE, "Received termination signal."); + } } + ::syslog(LOG_NOTICE, "ocvsmd cli terminated."); // NOLINT *-vararg + ::closelog(); return 0; } diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index a05f87e..ebd2119 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -27,6 +27,7 @@ target_link_libraries(ocvsmd_common PUBLIC ${common_transpiled} ) target_include_directories(ocvsmd_common + PUBLIC ${include_dir} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) target_include_directories(ocvsmd_common SYSTEM diff --git a/src/common/ipc/unix_socket_base.hpp b/src/common/ipc/unix_socket_base.hpp index 21a31fc..a9f1f6c 100644 --- a/src/common/ipc/unix_socket_base.hpp +++ b/src/common/ipc/unix_socket_base.hpp @@ -7,7 +7,7 @@ #define OCVSMD_COMMON_IPC_UNIX_SOCKET_BASE_HPP_INCLUDED #include "dsdl_helpers.hpp" -#include "platform/posix_utils.hpp" +#include "ocvsmd/platform/posix_utils.hpp" #include @@ -58,13 +58,13 @@ class UnixSocketBase std::size_t msg_size = 0; { MsgHeader msg_header; - ssize_t bytes_read = 0; + ssize_t bytes_read = 0; if (const auto err = platform::posixSyscallError([input_fd, &msg_header, &bytes_read] { // return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); })) { - ::syslog(LOG_ERR, "Failed to read message header (fd=%d): %s", input_fd, ::strerror(err)); + ::syslog(LOG_ERR, "Failed to read message header (fd=%d): %s", input_fd, std::strerror(err)); return err; } @@ -92,7 +92,7 @@ class UnixSocketBase return read = ::read(input_fd, buf_span.data(), buf_span.size()); })) { - ::syslog(LOG_ERR, "Failed to read message payload (fd=%d): %s", input_fd, ::strerror(err)); + ::syslog(LOG_ERR, "Failed to read message payload (fd=%d): %s", input_fd, std::strerror(err)); return err; } if (read != buf_span.size()) diff --git a/src/common/ipc/unix_socket_client.cpp b/src/common/ipc/unix_socket_client.cpp index 0e77af5..46d1c8d 100644 --- a/src/common/ipc/unix_socket_client.cpp +++ b/src/common/ipc/unix_socket_client.cpp @@ -5,13 +5,17 @@ #include "unix_socket_client.hpp" -#include "platform/posix_utils.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_utils.hpp" #include +#include +#include #include #include #include +#include #include #include #include @@ -26,10 +30,12 @@ namespace common namespace ipc { -UnixSocketClient::UnixSocketClient(std::string socket_path) +UnixSocketClient::UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path) : socket_path_{std::move(socket_path)} , client_fd_{-1} + , posix_executor_ext_{cetl::rtti_cast(&executor)} { + CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); } UnixSocketClient::~UnixSocketClient() @@ -43,17 +49,20 @@ UnixSocketClient::~UnixSocketClient() } } -bool UnixSocketClient::connectToServer() +int UnixSocketClient::start(std::function&& server_event_handler) { CETL_DEBUG_ASSERT(client_fd_ == -1, ""); + CETL_DEBUG_ASSERT(server_event_handler, ""); + + server_event_handler_ = std::move(server_event_handler); if (const auto err = platform::posixSyscallError([this] { // - return client_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); + return client_fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); })) { - std::cerr << "Failed to create socket: " << ::strerror(err) << "\n"; - return false; + std::cerr << "Failed to create socket: " << std::strerror(err) << "\n"; + return err; } sockaddr_un addr{}; @@ -73,11 +82,43 @@ bool UnixSocketClient::connectToServer() offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { - std::cerr << "Failed to connect to server: " << ::strerror(err) << "\n"; - return false; + std::cerr << "Failed to connect to server: " << std::strerror(err) << "\n"; + return err; } - return true; + socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // + [this](const auto&) { + // + handle_socket(); + }, + platform::IPosixExecutorExtension::Trigger::Readable{client_fd_}); + + server_event_handler_(ServerEvent::Connected{}); + return 0; +} + +void UnixSocketClient::handle_socket() +{ + if (const auto err = receiveMessage(client_fd_, [this](const auto payload) { + // + return server_event_handler_(ServerEvent::Message{payload}); + })) + { + if (err == -1) + { + ::syslog(LOG_DEBUG, "End of server stream - closing connection."); + } + else + { + ::syslog(LOG_WARNING, "Failed to handle server response - closing connection: %s", std::strerror(err)); + } + + socket_callback_.reset(); + ::close(client_fd_); + client_fd_ = -1; + + server_event_handler_(ServerEvent::Disconnected{}); + } } } // namespace ipc diff --git a/src/common/ipc/unix_socket_client.hpp b/src/common/ipc/unix_socket_client.hpp index e25deae..3639b1a 100644 --- a/src/common/ipc/unix_socket_client.hpp +++ b/src/common/ipc/unix_socket_client.hpp @@ -6,10 +6,14 @@ #ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED #define OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED +#include "ocvsmd/platform/posix_executor_extension.hpp" #include "unix_socket_base.hpp" +#include #include +#include +#include #include namespace ocvsmd @@ -22,7 +26,22 @@ namespace ipc class UnixSocketClient final : public UnixSocketBase { public: - explicit UnixSocketClient(std::string socket_path); + struct ServerEvent + { + struct Message + { + cetl::span payload; + }; + struct Connected + {}; + struct Disconnected + {}; + + using Var = cetl::variant; + + }; // ServerEvent + + UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path); UnixSocketClient(UnixSocketClient&&) = delete; UnixSocketClient(const UnixSocketClient&) = delete; @@ -31,7 +50,7 @@ class UnixSocketClient final : public UnixSocketBase ~UnixSocketClient(); - bool connectToServer(); + int start(std::function&& server_event_handler); int sendMessage(const cetl::span payload) const { @@ -39,8 +58,13 @@ class UnixSocketClient final : public UnixSocketBase } private: - std::string socket_path_; - int client_fd_; + void handle_socket(); + + std::string socket_path_; + int client_fd_; + platform::IPosixExecutorExtension* const posix_executor_ext_; + libcyphal::IExecutor::Callback::Any socket_callback_; + std::function server_event_handler_; }; // UnixSocketClient diff --git a/src/common/ipc/unix_socket_server.cpp b/src/common/ipc/unix_socket_server.cpp index 4ac929a..49384ec 100644 --- a/src/common/ipc/unix_socket_server.cpp +++ b/src/common/ipc/unix_socket_server.cpp @@ -5,8 +5,8 @@ #include "unix_socket_server.hpp" -#include "platform/posix_executor_extension.hpp" -#include "platform/posix_utils.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_utils.hpp" #include #include @@ -77,10 +77,9 @@ class ClientContextImpl final : public detail::ClientContext } // namespace UnixSocketServer::UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path) - : executor_{executor} - , socket_path_{std::move(socket_path)} + : socket_path_{std::move(socket_path)} , server_fd_{-1} - , posix_executor_ext_{cetl::rtti_cast(&executor_)} + , posix_executor_ext_{cetl::rtti_cast(&executor)} , unique_client_id_counter_{0} { CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); @@ -97,7 +96,7 @@ UnixSocketServer::~UnixSocketServer() } } -bool UnixSocketServer::start(std::function&& client_event_handler) +int UnixSocketServer::start(std::function&& client_event_handler) { CETL_DEBUG_ASSERT(server_fd_ == -1, ""); CETL_DEBUG_ASSERT(client_event_handler, ""); @@ -109,8 +108,8 @@ bool UnixSocketServer::start(std::function&& clien return server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); })) { - ::syslog(LOG_ERR, "Failed to create server socket: %s", ::strerror(err)); - return false; + ::syslog(LOG_ERR, "Failed to create server socket: %s", std::strerror(err)); + return err; } sockaddr_un addr{}; @@ -130,8 +129,8 @@ bool UnixSocketServer::start(std::function&& clien offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { - ::syslog(LOG_ERR, "Failed to bind server socket: %s", ::strerror(err)); - return false; + ::syslog(LOG_ERR, "Failed to bind server socket: %s", std::strerror(err)); + return err; } if (const auto err = platform::posixSyscallError([this] { @@ -139,8 +138,8 @@ bool UnixSocketServer::start(std::function&& clien return ::listen(server_fd_, MaxConnections); })) { - ::syslog(LOG_ERR, "Failed to listen on server socket: %s", ::strerror(err)); - return false; + ::syslog(LOG_ERR, "Failed to listen on server socket: %s", std::strerror(err)); + return err; } accept_callback_ = posix_executor_ext_->registerAwaitableCallback( // @@ -150,7 +149,7 @@ bool UnixSocketServer::start(std::function&& clien }, platform::IPosixExecutorExtension::Trigger::Readable{server_fd_}); - return true; + return 0; } void UnixSocketServer::handle_accept() @@ -163,7 +162,7 @@ void UnixSocketServer::handle_accept() return client_fd = ::accept(server_fd_, nullptr, nullptr); })) { - ::syslog(LOG_WARNING, "Failed to accept client connection: %s", ::strerror(err)); + ::syslog(LOG_WARNING, "Failed to accept client connection: %s", std::strerror(err)); return; } @@ -190,7 +189,7 @@ void UnixSocketServer::handle_client_request(const ClientId client_id, const int { if (const auto err = receiveMessage(client_fd, [this, client_id](const auto payload) { // - return client_event_handler_(ClientEvent::Message{.client_id = client_id, .payload = payload}); + return client_event_handler_(ClientEvent::Message{client_id, payload}); })) { if (err == -1) @@ -203,7 +202,7 @@ void UnixSocketServer::handle_client_request(const ClientId client_id, const int "Failed to handle client request - closing connection (id=%zu, fd=%d): %s", client_id, client_fd, - ::strerror(err)); + std::strerror(err)); } client_id_to_fd_.erase(client_id); diff --git a/src/common/ipc/unix_socket_server.hpp b/src/common/ipc/unix_socket_server.hpp index c3d8d16..7d4f0df 100644 --- a/src/common/ipc/unix_socket_server.hpp +++ b/src/common/ipc/unix_socket_server.hpp @@ -6,7 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_SERVER_HPP_INCLUDED #define OCVSMD_COMMON_IPC_UNIX_SOCKET_SERVER_HPP_INCLUDED -#include "platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" #include "unix_socket_base.hpp" #include @@ -79,7 +79,7 @@ class UnixSocketServer final : public UnixSocketBase ~UnixSocketServer(); - bool start(std::function&& client_event_handler); + int start(std::function&& client_event_handler); int sendMessage(const ClientId client_id, const cetl::span payload) const { @@ -96,7 +96,6 @@ class UnixSocketServer final : public UnixSocketBase void handle_client_connection(const int client_fd); void handle_client_request(const ClientId client_id, const int client_fd); - libcyphal::IExecutor& executor_; const std::string socket_path_; int server_fd_; platform::IPosixExecutorExtension* const posix_executor_ext_; diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 2a1f836..798a79b 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -59,15 +59,21 @@ cetl::optional Application::init() .setSoftwareVcsRevisionId(VCS_REVISION_ID) .setUniqueId(getUniqueId()); - if (!ipc_server_.start([this](const auto& client_event) { + if (const auto err = ipc_server_.start([this](const auto& client_event) { // using ClientEvent = common::ipc::UnixSocketServer::ClientEvent; cetl::visit( // cetl::make_overloaded( - [](const ClientEvent::Connected& connected) { + [this](const ClientEvent::Connected& connected) { // ::syslog(LOG_DEBUG, "Client connected (%zu).", connected.client_id); + (void) ipc_server_.sendMessage( // + connected.client_id, + {reinterpret_cast("Status1"), 7}); // NOLINT + (void) ipc_server_.sendMessage( // + connected.client_id, + {reinterpret_cast("Status2"), 7}); // NOLINT }, [this](const ClientEvent::Message& message) { // @@ -82,7 +88,7 @@ cetl::optional Application::init() return 0; })) { - return "Failed to start IPC server."; + return std::string("Failed to start IPC server: ") + std::strerror(err); } return cetl::nullopt; diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/application.hpp index 6dfbc18..201c61e 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/application.hpp @@ -7,7 +7,7 @@ #define OCVSMD_DAEMON_ENGINE_APPLICATION_HPP_INCLUDED #include "cyphal/udp_transport_bag.hpp" -#include "platform/defines.hpp" +#include "ocvsmd/platform/defines.hpp" #include @@ -40,7 +40,7 @@ class Application static UniqueId getUniqueId(); - platform::SingleThreadedExecutor executor_; + ocvsmd::platform::SingleThreadedExecutor executor_; cetl::pmr::memory_resource& memory_{*cetl::pmr::get_default_resource()}; cyphal::UdpTransportBag udp_transport_bag_{memory_, executor_}; cetl::optional presentation_; diff --git a/src/daemon/engine/platform/udp/udp_sockets.hpp b/src/daemon/engine/platform/udp/udp_sockets.hpp index 8405269..30f12e0 100644 --- a/src/daemon/engine/platform/udp/udp_sockets.hpp +++ b/src/daemon/engine/platform/udp/udp_sockets.hpp @@ -6,8 +6,8 @@ #ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_UDP_SOCKETS_HPP_INCLUDED #define OCVSMD_DAEMON_ENGINE_PLATFORM_UDP_SOCKETS_HPP_INCLUDED -#include "platform/posix_executor_extension.hpp" -#include "platform/posix_platform_error.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_platform_error.hpp" #include "udp.h" #include @@ -50,7 +50,7 @@ class UdpTxSocket final : public libcyphal::transport::udp::ITxSocket const auto result = ::udpTxInit(&handle, ::udpParseIfaceAddress(iface_address)); if (result < 0) { - return libcyphal::transport::PlatformError{PosixPlatformError{-result}}; + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-result}}; } auto tx_socket = libcyphal::makeUniquePtr(memory, executor, handle); @@ -99,7 +99,7 @@ class UdpTxSocket final : public libcyphal::transport::udp::ITxSocket payload_fragments[0].data()); if (result < 0) { - return libcyphal::transport::PlatformError{PosixPlatformError{-result}}; + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-result}}; } return SendResult::Success{result == 1}; @@ -108,7 +108,7 @@ class UdpTxSocket final : public libcyphal::transport::udp::ITxSocket CETL_NODISCARD libcyphal::IExecutor::Callback::Any registerCallback( libcyphal::IExecutor::Callback::Function&& function) override { - auto* const posix_executor_ext = cetl::rtti_cast(&executor_); + auto* const posix_executor_ext = cetl::rtti_cast(&executor_); if (nullptr == posix_executor_ext) { return {}; @@ -117,7 +117,7 @@ class UdpTxSocket final : public libcyphal::transport::udp::ITxSocket CETL_DEBUG_ASSERT(udp_handle_.fd >= 0, ""); return posix_executor_ext->registerAwaitableCallback( // std::move(function), - common::platform::IPosixExecutorExtension::Trigger::Writable{udp_handle_.fd}); + ocvsmd::platform::IPosixExecutorExtension::Trigger::Writable{udp_handle_.fd}); } // MARK: Data members: @@ -143,7 +143,7 @@ class UdpRxSocket final : public libcyphal::transport::udp::IRxSocket ::udpRxInit(&handle, ::udpParseIfaceAddress(address.c_str()), endpoint.ip_address, endpoint.udp_port); if (result < 0) { - return libcyphal::transport::PlatformError{PosixPlatformError{-result}}; + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-result}}; } auto rx_socket = libcyphal::makeUniquePtr(memory, executor, handle, memory); @@ -192,7 +192,7 @@ class UdpRxSocket final : public libcyphal::transport::udp::IRxSocket const std::int16_t result = ::udpRxReceive(&udp_handle_, &inout_size, buffer.data()); if (result < 0) { - return libcyphal::transport::PlatformError{PosixPlatformError{-result}}; + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-result}}; } if (result == 0) { @@ -214,7 +214,7 @@ class UdpRxSocket final : public libcyphal::transport::udp::IRxSocket CETL_NODISCARD libcyphal::IExecutor::Callback::Any registerCallback( libcyphal::IExecutor::Callback::Function&& function) override { - auto* const posix_executor_ext = cetl::rtti_cast(&executor_); + auto* const posix_executor_ext = cetl::rtti_cast(&executor_); if (nullptr == posix_executor_ext) { return {}; @@ -223,7 +223,7 @@ class UdpRxSocket final : public libcyphal::transport::udp::IRxSocket CETL_DEBUG_ASSERT(udp_handle_.fd >= 0, ""); return posix_executor_ext->registerAwaitableCallback( // std::move(function), - common::platform::IPosixExecutorExtension::Trigger::Readable{udp_handle_.fd}); + ocvsmd::platform::IPosixExecutorExtension::Trigger::Readable{udp_handle_.fd}); } // MARK: Data members: diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index f95e72b..3c0052a 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -8,12 +8,12 @@ #include #include #include -#include #include #include #include #include #include +#include #include #include #include @@ -27,21 +27,30 @@ namespace const auto* const s_init_complete = "init_complete"; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -volatile int s_running = 1; +volatile sig_atomic_t g_running = 1; -extern "C" void handle_signal(const int sig) +extern "C" void signal_handler(const int sig) { switch (sig) { case SIGTERM: case SIGINT: - s_running = 0; + g_running = 0; break; default: break; } } +void setup_signal_handlers() +{ + struct sigaction sigbreak + {}; + sigbreak.sa_handler = &signal_handler; + ::sigaction(SIGINT, &sigbreak, nullptr); + ::sigaction(SIGTERM, &sigbreak, nullptr); +} + bool write_string(const int fd, const char* const str) { const auto str_len = strlen(str); @@ -50,7 +59,7 @@ bool write_string(const int fd, const char* const str) void exit_with_failure(const int fd, const char* const msg) { - const char* const err_txt = strerror(errno); + const char* const err_txt = std::strerror(errno); write_string(fd, msg); write_string(fd, err_txt); ::exit(EXIT_FAILURE); @@ -61,7 +70,7 @@ void step_01_close_all_file_descriptors(std::array& pipe_fds) rlimit rlimit_files{}; if (getrlimit(RLIMIT_NOFILE, &rlimit_files) != 0) { - const char* const err_txt = strerror(errno); + const char* const err_txt = std::strerror(errno); std::cerr << "Failed to getrlimit(RLIMIT_NOFILE): " << err_txt << "\n"; ::exit(EXIT_FAILURE); } @@ -75,7 +84,7 @@ void step_01_close_all_file_descriptors(std::array& pipe_fds) // if (::pipe(pipe_fds.data()) == -1) { - const char* const err_txt = ::strerror(errno); + const char* const err_txt = std::strerror(errno); std::cerr << "Failed to create pipe: " << err_txt << "\n"; ::exit(EXIT_FAILURE); } @@ -83,9 +92,7 @@ void step_01_close_all_file_descriptors(std::array& pipe_fds) void step_02_03_setup_signal_handlers() { - // Catch termination signals - (void) ::signal(SIGTERM, handle_signal); - (void) ::signal(SIGINT, handle_signal); + setup_signal_handlers(); } void step_04_sanitize_environment() @@ -99,7 +106,7 @@ bool step_05_fork_to_background(std::array& pipe_fds) const pid_t parent_pid = fork(); if (parent_pid < 0) { - const char* const err_txt = ::strerror(errno); + const char* const err_txt = std::strerror(errno); std::cerr << "Failed to fork: " << err_txt << "\n"; ::exit(EXIT_FAILURE); } @@ -235,7 +242,7 @@ void step_15_exit_org_process(int& pipe_read_fd) const auto res = ::read(pipe_read_fd, msg_from_child.data(), msg_from_child.size() - 1); if (res == -1) { - const char* const err_txt = ::strerror(errno); + const char* const err_txt = std::strerror(errno); std::cerr << "Failed to read pipe: " << err_txt << "\n"; ::exit(EXIT_FAILURE); } @@ -312,24 +319,33 @@ int main(const int argc, const char** const argv) pipe_write_fd = daemonize(); // We are in a child process now! } - - Application application; - if (const auto failure_str = application.init()) - { - write_string(pipe_write_fd, "Failed to init application: "); - write_string(pipe_write_fd, failure_str.value().c_str()); - ::exit(EXIT_FAILURE); - } - if (should_daemonize) + else { - step_14_notify_init_complete(pipe_write_fd); + setup_signal_handlers(); } - ::openlog("ocvsmd", LOG_PID, LOG_DAEMON); + ::openlog("ocvsmd", LOG_PID, should_daemonize ? LOG_DAEMON : LOG_USER); ::syslog(LOG_NOTICE, "ocvsmd daemon started."); // NOLINT *-vararg + { + Application application; + if (const auto failure_str = application.init()) + { + write_string(pipe_write_fd, "Failed to init application: "); + write_string(pipe_write_fd, failure_str.value().c_str()); + ::exit(EXIT_FAILURE); + } + if (should_daemonize) + { + step_14_notify_init_complete(pipe_write_fd); + } - application.runWhile([] { return s_running == 1; }); + application.runWhile([] { return g_running == 1; }); + if (g_running == 0) + { + ::syslog(LOG_NOTICE, "Received termination signal."); + } + } ::syslog(LOG_NOTICE, "ocvsmd daemon terminated."); // NOLINT *-vararg ::closelog(); diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index 8ab797e..18125e6 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -16,4 +16,5 @@ target_include_directories(ocvsmd_sdk ) target_include_directories(ocvsmd_sdk SYSTEM PUBLIC ${submodules_dir}/cetl/include + PUBLIC ${submodules_dir}/libcyphal/include ) diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 83e592f..2dee2bf 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -8,6 +8,8 @@ #include "ipc/unix_socket_client.hpp" #include +#include +#include #include #include @@ -24,41 +26,53 @@ namespace class DaemonImpl final : public Daemon { public: - explicit DaemonImpl(cetl::pmr::memory_resource& memory) + DaemonImpl(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) : memory_{memory} + , executor_{executor} { } bool connect() { - return ipc_client_.connectToServer(); - } - - void send_messages() const override - { - ipc_client_.sendMessage({reinterpret_cast("Abc"), 3}); // NOLINT - ::sleep(1); + return 0 == ipc_client_.start([](const auto& server_event) { + // + using ServerEvent = common::ipc::UnixSocketClient::ServerEvent; - ipc_client_.sendMessage({reinterpret_cast("xyZ"), 3}); // NOLINT - ::sleep(1); + cetl::visit( // + cetl::make_overloaded( + [](const ServerEvent::Connected&) { + // + ::syslog(LOG_DEBUG, "Server connected."); + }, + [](const ServerEvent::Message& message) { + // + ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", message.payload.size()); + }, + [](const ServerEvent::Disconnected&) { + // + ::syslog(LOG_DEBUG, "Server disconnected."); + }), + server_event); + return 0; + }); } private: cetl::pmr::memory_resource& memory_; - common::ipc::UnixSocketClient ipc_client_{"/var/run/ocvsmd/local.sock"}; + libcyphal::IExecutor& executor_; + common::ipc::UnixSocketClient ipc_client_{executor_, "/var/run/ocvsmd/local.sock"}; }; // DaemonImpl } // namespace -std::unique_ptr Daemon::make(cetl::pmr::memory_resource& memory) +std::unique_ptr Daemon::make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) { - auto daemon = std::make_unique(memory); + auto daemon = std::make_unique(memory, executor); if (!daemon->connect()) { return nullptr; } - return std::move(daemon); } From d73339fc21c947c63811de2b9afb8cc6079ad0c9 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 2 Jan 2025 10:22:55 +0200 Subject: [PATCH 008/156] format --- src/sdk/daemon.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 2dee2bf..f6dd690 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -36,23 +36,23 @@ class DaemonImpl final : public Daemon { return 0 == ipc_client_.start([](const auto& server_event) { // - using ServerEvent = common::ipc::UnixSocketClient::ServerEvent; + using ServerEvent = common::ipc::UnixSocketClient::ServerEvent; - cetl::visit( // - cetl::make_overloaded( - [](const ServerEvent::Connected&) { - // - ::syslog(LOG_DEBUG, "Server connected."); - }, - [](const ServerEvent::Message& message) { - // - ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", message.payload.size()); - }, - [](const ServerEvent::Disconnected&) { - // - ::syslog(LOG_DEBUG, "Server disconnected."); - }), - server_event); + cetl::visit( // + cetl::make_overloaded( + [](const ServerEvent::Connected&) { + // + ::syslog(LOG_DEBUG, "Server connected."); + }, + [](const ServerEvent::Message& message) { + // + ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", message.payload.size()); + }, + [](const ServerEvent::Disconnected&) { + // + ::syslog(LOG_DEBUG, "Server disconnected."); + }), + server_event); return 0; }); } From db718d635404ee6b15276e76a29051b13a2e18ef Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 2 Jan 2025 10:33:59 +0200 Subject: [PATCH 009/156] nolints --- src/cli/main.cpp | 2 +- src/common/ipc/unix_socket_base.hpp | 2 ++ src/common/ipc/unix_socket_client.cpp | 2 ++ src/common/ipc/unix_socket_server.cpp | 8 ++++++++ src/daemon/engine/application.cpp | 4 ++++ src/daemon/main.cpp | 2 +- src/sdk/daemon.cpp | 3 +++ 7 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 2e68862..76d570d 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -85,7 +85,7 @@ int main(const int, const char** const) if (g_running == 0) { - ::syslog(LOG_NOTICE, "Received termination signal."); + ::syslog(LOG_NOTICE, "Received termination signal."); // NOLINT *-vararg } } ::syslog(LOG_NOTICE, "ocvsmd cli terminated."); // NOLINT *-vararg diff --git a/src/common/ipc/unix_socket_base.hpp b/src/common/ipc/unix_socket_base.hpp index a9f1f6c..49e101e 100644 --- a/src/common/ipc/unix_socket_base.hpp +++ b/src/common/ipc/unix_socket_base.hpp @@ -64,6 +64,7 @@ class UnixSocketBase return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); })) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_ERR, "Failed to read message header (fd=%d): %s", input_fd, std::strerror(err)); return err; } @@ -92,6 +93,7 @@ class UnixSocketBase return read = ::read(input_fd, buf_span.data(), buf_span.size()); })) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_ERR, "Failed to read message payload (fd=%d): %s", input_fd, std::strerror(err)); return err; } diff --git a/src/common/ipc/unix_socket_client.cpp b/src/common/ipc/unix_socket_client.cpp index 46d1c8d..d6b5037 100644 --- a/src/common/ipc/unix_socket_client.cpp +++ b/src/common/ipc/unix_socket_client.cpp @@ -106,10 +106,12 @@ void UnixSocketClient::handle_socket() { if (err == -1) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "End of server stream - closing connection."); } else { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_WARNING, "Failed to handle server response - closing connection: %s", std::strerror(err)); } diff --git a/src/common/ipc/unix_socket_server.cpp b/src/common/ipc/unix_socket_server.cpp index 49384ec..0a1260e 100644 --- a/src/common/ipc/unix_socket_server.cpp +++ b/src/common/ipc/unix_socket_server.cpp @@ -44,11 +44,13 @@ class ClientContextImpl final : public detail::ClientContext { CETL_DEBUG_ASSERT(client_fd != -1, ""); + // NOLINTNEXTLINE *-vararg ::syslog(LOG_NOTICE, "New client connection on fd=%d (id=%zu).", fd, id); } ~ClientContextImpl() override { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_NOTICE, "Closing client connection on fd=%d (id=%zu).", fd_, id_); platform::posixSyscallError([this] { @@ -108,6 +110,7 @@ int UnixSocketServer::start(std::function&& client return server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); })) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_ERR, "Failed to create server socket: %s", std::strerror(err)); return err; } @@ -129,6 +132,7 @@ int UnixSocketServer::start(std::function&& client offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_ERR, "Failed to bind server socket: %s", std::strerror(err)); return err; } @@ -138,6 +142,7 @@ int UnixSocketServer::start(std::function&& client return ::listen(server_fd_, MaxConnections); })) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_ERR, "Failed to listen on server socket: %s", std::strerror(err)); return err; } @@ -162,6 +167,7 @@ void UnixSocketServer::handle_accept() return client_fd = ::accept(server_fd_, nullptr, nullptr); })) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_WARNING, "Failed to accept client connection: %s", std::strerror(err)); return; } @@ -194,10 +200,12 @@ void UnixSocketServer::handle_client_request(const ClientId client_id, const int { if (err == -1) { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "End of client stream - closing connection (id=%zu, fd=%d).", client_id, client_fd); } else { + // NOLINTNEXTLINE *-vararg ::syslog(LOG_WARNING, "Failed to handle client request - closing connection (id=%zu, fd=%d): %s", client_id, diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 798a79b..976ed74 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -67,6 +68,7 @@ cetl::optional Application::init() cetl::make_overloaded( [this](const ClientEvent::Connected& connected) { // + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client connected (%zu).", connected.client_id); (void) ipc_server_.sendMessage( // connected.client_id, @@ -77,11 +79,13 @@ cetl::optional Application::init() }, [this](const ClientEvent::Message& message) { // + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client msg (%zu).", message.client_id); (void) ipc_server_.sendMessage(message.client_id, message.payload); }, [](const ClientEvent::Disconnected& disconnected) { // + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client disconnected (%zu).", disconnected.client_id); }), client_event); diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 3c0052a..f4b87fa 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -343,7 +343,7 @@ int main(const int argc, const char** const argv) if (g_running == 0) { - ::syslog(LOG_NOTICE, "Received termination signal."); + ::syslog(LOG_NOTICE, "Received termination signal."); // NOLINT *-vararg } } ::syslog(LOG_NOTICE, "ocvsmd daemon terminated."); // NOLINT *-vararg diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index f6dd690..cc8f301 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -42,14 +42,17 @@ class DaemonImpl final : public Daemon cetl::make_overloaded( [](const ServerEvent::Connected&) { // + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server connected."); }, [](const ServerEvent::Message& message) { // + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", message.payload.size()); }, [](const ServerEvent::Disconnected&) { // + // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server disconnected."); }), server_event); From 29450b39139780bc105e6aa8f6091f81e811995f Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 2 Jan 2025 10:40:57 +0200 Subject: [PATCH 010/156] clang-tidy fixes --- src/daemon/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index f4b87fa..2661940 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -8,12 +8,12 @@ #include #include #include +#include #include #include #include #include #include -#include #include #include #include From 2cf24a520cdb7e4cf6a9b5d2ffdf4d8ee674aa2d Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 2 Jan 2025 10:47:47 +0200 Subject: [PATCH 011/156] clang-tidy fixes --- src/cli/main.cpp | 3 ++- src/daemon/main.cpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 76d570d..163a1c8 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -12,8 +12,9 @@ #include #include #include +#include #include -#include +#include // NOLINT #include namespace diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 2661940..fd64c98 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -8,12 +8,12 @@ #include #include #include -#include #include #include #include #include #include +#include // NOLINT #include #include #include From b51f03650a9325936316f224734272f5b90bcf28 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 11:36:08 +0200 Subject: [PATCH 012/156] add gtests --- .github/workflows/tests.yml | 2 ++ CMakeLists.txt | 10 +++---- src/cli/main.cpp | 1 - src/common/CMakeLists.txt | 1 + src/common/ipc/client_router.cpp | 35 +++++++++++++++++++++++ src/common/ipc/client_router.hpp | 39 ++++++++++++++++++++++++++ src/sdk/daemon.cpp | 2 -- test/CMakeLists.txt | 5 ++++ test/common/CMakeLists.txt | 11 ++++++++ test/common/ipc/test_client_router.cpp | 33 ++++++++++++++++++++++ test/daemon/CMakeLists.txt | 2 ++ test/daemon/engine/CMakeLists.txt | 17 +++++++++++ test/daemon/engine/test_xxx.cpp | 29 +++++++++++++++++++ test/sdk/CMakeLists.txt | 6 ++++ 14 files changed, 184 insertions(+), 9 deletions(-) create mode 100644 src/common/ipc/client_router.cpp create mode 100644 src/common/ipc/client_router.hpp create mode 100644 test/common/ipc/test_client_router.cpp create mode 100644 test/daemon/engine/CMakeLists.txt create mode 100644 test/daemon/engine/test_xxx.cpp create mode 100644 test/sdk/CMakeLists.txt diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1159444..c5e4965 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -30,6 +30,7 @@ jobs: - run: | cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Debug + ctest --preset OCVSMD-Linux-Debug - uses: actions/upload-artifact@v4 if: always() with: @@ -60,6 +61,7 @@ jobs: - run: | cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Release + ctest --preset OCVSMD-Linux-Release - uses: actions/upload-artifact@v4 if: always() with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 25acf4b..cfbde1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,6 @@ project(ocvsmd HOMEPAGE_URL https://github.com/OpenCyphal-Garage/opencyphal-vehicle-system-management-daemon ) -enable_testing() - set(NO_STATIC_ANALYSIS OFF CACHE BOOL "disable static analysis") set(CMAKE_CXX_STANDARD 14) @@ -71,12 +69,12 @@ add_definitions( -DNODE_NAME="org.opencyphal.ocvsmd" ) if (DEFINED PLATFORM_OS_TYPE) - if(${PLATFORM_OS_TYPE} STREQUAL "bsd") + if (${PLATFORM_OS_TYPE} STREQUAL "bsd") add_definitions(-DPLATFORM_OS_TYPE_BSD) - elseif(${PLATFORM_OS_TYPE} STREQUAL "linux") + elseif (${PLATFORM_OS_TYPE} STREQUAL "linux") add_definitions(-DPLATFORM_OS_TYPE_LINUX) - endif() -endif() + endif () +endif () add_subdirectory(src) add_subdirectory(test) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 163a1c8..c8f9a7b 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -11,7 +11,6 @@ #include #include -#include #include #include #include // NOLINT diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index ebd2119..aa05153 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -20,6 +20,7 @@ add_cyphal_library( ) add_library(ocvsmd_common + ipc/client_router.cpp ipc/unix_socket_client.cpp ipc/unix_socket_server.cpp ) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp new file mode 100644 index 0000000..bcea1e4 --- /dev/null +++ b/src/common/ipc/client_router.cpp @@ -0,0 +1,35 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "client_router.hpp" + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace +{ + +class ClientRouterImpl final : public ClientRouter +{ +public: + ClientRouterImpl() = default; + +}; // ClientRouterImpl + +} // namespace + +std::unique_ptr ClientRouter::make() +{ + return std::make_unique(); +} + +} // namespace ipc +} // namespace common +} // namespace ocvsmd diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp new file mode 100644 index 0000000..c1cc348 --- /dev/null +++ b/src/common/ipc/client_router.hpp @@ -0,0 +1,39 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +class ClientRouter +{ +public: + static std::unique_ptr make(); + + ClientRouter(ClientRouter&&) = delete; + ClientRouter(const ClientRouter&) = delete; + ClientRouter& operator=(ClientRouter&&) = delete; + ClientRouter& operator=(const ClientRouter&) = delete; + + virtual ~ClientRouter() = default; + +protected: + ClientRouter() = default; + +}; // ClientRouter + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index cc8f301..44f40e6 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -11,9 +11,7 @@ #include #include -#include #include -#include #include namespace ocvsmd diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 09243f0..647bbb5 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,6 +5,8 @@ cmake_minimum_required(VERSION 3.27) +enable_testing() + include(FetchContent) FetchContent_Declare( googletest @@ -16,6 +18,9 @@ block() FetchContent_MakeAvailable(googletest) endblock() +include(GoogleTest) + add_subdirectory(common) add_subdirectory(daemon) add_subdirectory(cli) +add_subdirectory(sdk) diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index 2b27a6c..63ea7ee 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -4,3 +4,14 @@ # cmake_minimum_required(VERSION 3.27) + +add_executable(common_tests + ipc/test_client_router.cpp +) +target_link_libraries(common_tests + ocvsmd_common + GTest::gmock + GTest::gtest_main +) + +gtest_discover_tests(common_tests) diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp new file mode 100644 index 0000000..118c36b --- /dev/null +++ b/test/common/ipc/test_client_router.cpp @@ -0,0 +1,33 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "ipc/client_router.hpp" + +#include +#include + +namespace +{ + +using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in the unit tests. + +using testing::NotNull; + +class TestClientRouter : public testing::Test +{ +protected: + void SetUp() override {} + void TearDown() override {} +}; + +// MARK: - Tests: + +TEST_F(TestClientRouter, make) +{ + const auto router = ClientRouter::make(); + EXPECT_THAT(router, NotNull()); +} + +} // namespace diff --git a/test/daemon/CMakeLists.txt b/test/daemon/CMakeLists.txt index 2b27a6c..572acc6 100644 --- a/test/daemon/CMakeLists.txt +++ b/test/daemon/CMakeLists.txt @@ -4,3 +4,5 @@ # cmake_minimum_required(VERSION 3.27) + +add_subdirectory(engine) diff --git a/test/daemon/engine/CMakeLists.txt b/test/daemon/engine/CMakeLists.txt new file mode 100644 index 0000000..e11f45d --- /dev/null +++ b/test/daemon/engine/CMakeLists.txt @@ -0,0 +1,17 @@ +# +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: MIT +# + +cmake_minimum_required(VERSION 3.27) + +add_executable(engine_tests + test_xxx.cpp +) +target_link_libraries(engine_tests + ocvsmd_engine + GTest::gmock + GTest::gtest_main +) + +gtest_discover_tests(engine_tests) diff --git a/test/daemon/engine/test_xxx.cpp b/test/daemon/engine/test_xxx.cpp new file mode 100644 index 0000000..ed7c944 --- /dev/null +++ b/test/daemon/engine/test_xxx.cpp @@ -0,0 +1,29 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include +#include + +namespace +{ + +class TestXxx : public testing::Test +{ +protected: + void SetUp() override {} + void TearDown() override {} +}; + +// MARK: - Tests: + +TEST_F(TestXxx, make) +{ + // Expect two strings not to be equal. + EXPECT_STRNE("hello", "world"); + // Expect equality. + EXPECT_THAT(7 * 6, 42); +} + +} // namespace diff --git a/test/sdk/CMakeLists.txt b/test/sdk/CMakeLists.txt new file mode 100644 index 0000000..2b27a6c --- /dev/null +++ b/test/sdk/CMakeLists.txt @@ -0,0 +1,6 @@ +# +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: MIT +# + +cmake_minimum_required(VERSION 3.27) From 0e5b6bd8812dad92ea96ebc0952fb5d6b8131dd1 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 11:45:37 +0200 Subject: [PATCH 013/156] added test presets --- CMakePresets.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CMakePresets.json b/CMakePresets.json index 3f63272..fcfaff2 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -103,5 +103,21 @@ "ocvsmd" ] } + ], + "testPresets": [ + { + "name": "OCVSMD-Debug", + "displayName": "Test OCVSMD (Debug)", + "description": "Tests OCVSMD", + "configurePreset": "OCVSMD-Linux", + "configuration": "Debug" + }, + { + "name": "OCVSMD-Release", + "displayName": "Test OCVSMD (Release)", + "description": "Tests OCVSMD", + "configurePreset": "OCVSMD-Linux", + "configuration": "Release" + } ] } From fff42736d144ba08b701ec826e207f5634929606 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 11:47:26 +0200 Subject: [PATCH 014/156] fix test presets --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c5e4965..35d1c39 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -30,7 +30,7 @@ jobs: - run: | cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Debug - ctest --preset OCVSMD-Linux-Debug + ctest --preset OCVSMD-Debug - uses: actions/upload-artifact@v4 if: always() with: @@ -61,7 +61,7 @@ jobs: - run: | cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Release - ctest --preset OCVSMD-Linux-Release + ctest --preset OCVSMD-Release - uses: actions/upload-artifact@v4 if: always() with: From 259753d618492eed6e0a9591489e2ce084d398cb Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 13:22:00 +0200 Subject: [PATCH 015/156] fix test presets --- CMakeLists.txt | 2 ++ CMakePresets.json | 20 ++++---------------- test/CMakeLists.txt | 2 -- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cfbde1d..c04c3a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,8 @@ project(ocvsmd HOMEPAGE_URL https://github.com/OpenCyphal-Garage/opencyphal-vehicle-system-management-daemon ) +enable_testing() + set(NO_STATIC_ANALYSIS OFF CACHE BOOL "disable static analysis") set(CMAKE_CXX_STANDARD 14) diff --git a/CMakePresets.json b/CMakePresets.json index fcfaff2..86bd397 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -68,40 +68,28 @@ "displayName": "Linux OCVSMD (Debug)", "description": "Builds OCVSMD for Linux", "configurePreset": "OCVSMD-Linux", - "configuration": "Debug", - "targets": [ - "ocvsmd" - ] + "configuration": "Debug" }, { "name": "OCVSMD-Linux-Release", "displayName": "Linux OCVSMD (Release)", "description": "Builds OCVSMD for Linux", "configurePreset": "OCVSMD-Linux", - "configuration": "Release", - "targets": [ - "ocvsmd" - ] + "configuration": "Release" }, { "name": "OCVSMD-BSD-Debug", "displayName": "BSD OCVSMD (Debug)", "description": "Builds OCVSMD for BSD", "configurePreset": "OCVSMD-BSD", - "configuration": "Debug", - "targets": [ - "ocvsmd" - ] + "configuration": "Debug" }, { "name": "OCVSMD-BSD-Release", "displayName": "BSD OCVSMD (Release)", "description": "Builds OCVSMD for BSD", "configurePreset": "OCVSMD-BSD", - "configuration": "Release", - "targets": [ - "ocvsmd" - ] + "configuration": "Release" } ], "testPresets": [ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 647bbb5..1c742a0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,8 +5,6 @@ cmake_minimum_required(VERSION 3.27) -enable_testing() - include(FetchContent) FetchContent_Declare( googletest From c994efb688d0622f18908b728e6a4b6644786a6a Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 13:26:49 +0200 Subject: [PATCH 016/156] try unit test failure --- test/common/ipc/test_client_router.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 118c36b..6077969 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -13,7 +13,7 @@ namespace using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in the unit tests. -using testing::NotNull; +using testing::IsNull; class TestClientRouter : public testing::Test { @@ -27,7 +27,7 @@ class TestClientRouter : public testing::Test TEST_F(TestClientRouter, make) { const auto router = ClientRouter::make(); - EXPECT_THAT(router, NotNull()); + EXPECT_THAT(router, IsNull()); } } // namespace From b8fb6d655d034dff6cb43a9aa51d8bbcaa671a93 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 13:34:10 +0200 Subject: [PATCH 017/156] fix unit test failure --- test/common/ipc/test_client_router.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 6077969..118c36b 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -13,7 +13,7 @@ namespace using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in the unit tests. -using testing::IsNull; +using testing::NotNull; class TestClientRouter : public testing::Test { @@ -27,7 +27,7 @@ class TestClientRouter : public testing::Test TEST_F(TestClientRouter, make) { const auto router = ClientRouter::make(); - EXPECT_THAT(router, IsNull()); + EXPECT_THAT(router, NotNull()); } } // namespace From 9cd3dd4791d57353abccec7dad21bf77f774aa68 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 14:49:18 +0200 Subject: [PATCH 018/156] added `ClientPipe` and `ClientRoute` interfaces --- CMakeLists.txt | 8 +-- src/common/ipc/client_pipe.hpp | 67 ++++++++++++++++++++++++++ src/common/ipc/client_router.cpp | 17 +++++-- src/common/ipc/client_router.hpp | 6 ++- src/common/ipc/unix_socket_client.cpp | 12 ++--- src/common/ipc/unix_socket_client.hpp | 38 +++++---------- src/sdk/daemon.cpp | 12 ++--- test/CMakeLists.txt | 2 + test/common/ipc/client_pipe_mock.hpp | 53 ++++++++++++++++++++ test/common/ipc/test_client_router.cpp | 15 +++++- test/unique_ptr_refwrapper.hpp | 42 ++++++++++++++++ 11 files changed, 224 insertions(+), 48 deletions(-) create mode 100644 src/common/ipc/client_pipe.hpp create mode 100644 test/common/ipc/client_pipe_mock.hpp create mode 100644 test/unique_ptr_refwrapper.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c04c3a4..c9d72e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,10 +32,10 @@ if (NOT clang_format) message(STATUS "Could not locate clang-format") else () file(GLOB_RECURSE format_files - ${include_dir}/**/*.hpp - ${src_dir}/**/*.[ch] - ${src_dir}/**/*.[ch]pp - ${test_dir}/**/*.[ch]pp + ${include_dir}/*.hpp + ${src_dir}/*.[ch] + ${src_dir}/*.[ch]pp + ${test_dir}/*.[ch]pp ) message(STATUS "Using clang-format: ${clang_format}") add_custom_target(format COMMAND ${clang_format} -i -fallback-style=none -style=file --verbose ${format_files}) diff --git a/src/common/ipc/client_pipe.hpp b/src/common/ipc/client_pipe.hpp new file mode 100644 index 0000000..65ac123 --- /dev/null +++ b/src/common/ipc/client_pipe.hpp @@ -0,0 +1,67 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED + +#include +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +class ClientPipe +{ +public: + using Ptr = std::unique_ptr; + + using Payload = cetl::span; + + struct Event + { + struct Connected + {}; + struct Disconnected + {}; + struct Message + { + Payload payload; + + }; // Message + + using Var = cetl::variant; + + }; // Event + + using EventHandler = std::function; + + ClientPipe(ClientPipe&&) = delete; + ClientPipe(const ClientPipe&) = delete; + ClientPipe& operator=(ClientPipe&&) = delete; + ClientPipe& operator=(const ClientPipe&) = delete; + + virtual ~ClientPipe() = default; + + virtual int start(EventHandler event_handler) = 0; + virtual int sendMessage(const Payload payload) = 0; + +protected: + ClientPipe() = default; + +}; // ClientPipe + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index bcea1e4..4e97891 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -3,9 +3,13 @@ // SPDX-License-Identifier: MIT // +#include "client_pipe.hpp" #include "client_router.hpp" +#include + #include +#include namespace ocvsmd { @@ -19,15 +23,22 @@ namespace class ClientRouterImpl final : public ClientRouter { public: - ClientRouterImpl() = default; + explicit ClientRouterImpl(ClientPipe::Ptr client_pipe) + : client_pipe_{std::move(client_pipe)} + { + CETL_DEBUG_ASSERT(client_pipe_, ""); + } + +private: + ClientPipe::Ptr client_pipe_; }; // ClientRouterImpl } // namespace -std::unique_ptr ClientRouter::make() +ClientRouter::Ptr ClientRouter::make(ClientPipe::Ptr client_pipe) { - return std::make_unique(); + return std::make_unique(std::move(client_pipe)); } } // namespace ipc diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index c1cc348..138b7e9 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED #define OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED +#include "client_pipe.hpp" + #include namespace ocvsmd @@ -18,7 +20,9 @@ namespace ipc class ClientRouter { public: - static std::unique_ptr make(); + using Ptr = std::unique_ptr; + + static Ptr make(ClientPipe::Ptr client_pipe); ClientRouter(ClientRouter&&) = delete; ClientRouter(const ClientRouter&) = delete; diff --git a/src/common/ipc/unix_socket_client.cpp b/src/common/ipc/unix_socket_client.cpp index d6b5037..a0c1a7b 100644 --- a/src/common/ipc/unix_socket_client.cpp +++ b/src/common/ipc/unix_socket_client.cpp @@ -49,12 +49,12 @@ UnixSocketClient::~UnixSocketClient() } } -int UnixSocketClient::start(std::function&& server_event_handler) +int UnixSocketClient::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(client_fd_ == -1, ""); - CETL_DEBUG_ASSERT(server_event_handler, ""); + CETL_DEBUG_ASSERT(event_handler_, ""); - server_event_handler_ = std::move(server_event_handler); + event_handler_ = std::move(event_handler); if (const auto err = platform::posixSyscallError([this] { // @@ -93,7 +93,7 @@ int UnixSocketClient::start(std::function&& server }, platform::IPosixExecutorExtension::Trigger::Readable{client_fd_}); - server_event_handler_(ServerEvent::Connected{}); + event_handler_(Event::Connected{}); return 0; } @@ -101,7 +101,7 @@ void UnixSocketClient::handle_socket() { if (const auto err = receiveMessage(client_fd_, [this](const auto payload) { // - return server_event_handler_(ServerEvent::Message{payload}); + return event_handler_(Event::Message{payload}); })) { if (err == -1) @@ -119,7 +119,7 @@ void UnixSocketClient::handle_socket() ::close(client_fd_); client_fd_ = -1; - server_event_handler_(ServerEvent::Disconnected{}); + event_handler_(Event::Disconnected{}); } } diff --git a/src/common/ipc/unix_socket_client.hpp b/src/common/ipc/unix_socket_client.hpp index 3639b1a..6dfa537 100644 --- a/src/common/ipc/unix_socket_client.hpp +++ b/src/common/ipc/unix_socket_client.hpp @@ -6,14 +6,13 @@ #ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED #define OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED +#include "client_pipe.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" #include "unix_socket_base.hpp" -#include #include #include -#include #include namespace ocvsmd @@ -23,24 +22,9 @@ namespace common namespace ipc { -class UnixSocketClient final : public UnixSocketBase +class UnixSocketClient final : public UnixSocketBase, public ClientPipe { public: - struct ServerEvent - { - struct Message - { - cetl::span payload; - }; - struct Connected - {}; - struct Disconnected - {}; - - using Var = cetl::variant; - - }; // ServerEvent - UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path); UnixSocketClient(UnixSocketClient&&) = delete; @@ -48,11 +32,13 @@ class UnixSocketClient final : public UnixSocketBase UnixSocketClient& operator=(UnixSocketClient&&) = delete; UnixSocketClient& operator=(const UnixSocketClient&) = delete; - ~UnixSocketClient(); + ~UnixSocketClient() override; + + // ClientPipe - int start(std::function&& server_event_handler); + int start(EventHandler event_handler) override; - int sendMessage(const cetl::span payload) const + int sendMessage(const Payload payload) override { return UnixSocketBase::sendMessage(client_fd_, payload); } @@ -60,11 +46,11 @@ class UnixSocketClient final : public UnixSocketBase private: void handle_socket(); - std::string socket_path_; - int client_fd_; - platform::IPosixExecutorExtension* const posix_executor_ext_; - libcyphal::IExecutor::Callback::Any socket_callback_; - std::function server_event_handler_; + std::string socket_path_; + int client_fd_; + platform::IPosixExecutorExtension* const posix_executor_ext_; + libcyphal::IExecutor::Callback::Any socket_callback_; + std::function event_handler_; }; // UnixSocketClient diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 44f40e6..cd71a75 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -32,28 +32,28 @@ class DaemonImpl final : public Daemon bool connect() { - return 0 == ipc_client_.start([](const auto& server_event) { + return 0 == ipc_client_.start([](const auto& event) { // - using ServerEvent = common::ipc::UnixSocketClient::ServerEvent; + using Event = common::ipc::UnixSocketClient::Event; cetl::visit( // cetl::make_overloaded( - [](const ServerEvent::Connected&) { + [](const Event::Connected&) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server connected."); }, - [](const ServerEvent::Message& message) { + [](const Event::Message& message) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", message.payload.size()); }, - [](const ServerEvent::Disconnected&) { + [](const Event::Disconnected&) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server disconnected."); }), - server_event); + event); return 0; }); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1c742a0..6f25600 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -18,6 +18,8 @@ endblock() include(GoogleTest) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + add_subdirectory(common) add_subdirectory(daemon) add_subdirectory(cli) diff --git a/test/common/ipc/client_pipe_mock.hpp b/test/common/ipc/client_pipe_mock.hpp new file mode 100644 index 0000000..cd62c26 --- /dev/null +++ b/test/common/ipc/client_pipe_mock.hpp @@ -0,0 +1,53 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_CLIENT_PIPE_MOCK_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_CLIENT_PIPE_MOCK_HPP_INCLUDED + +#include "ipc/client_pipe.hpp" +#include "unique_ptr_refwrapper.hpp" + +#include + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +class ClientPipeMock : public ClientPipe +{ +public: + struct RefWrapper final : UniquePtrRefWrapper + { + using UniquePtrRefWrapper::UniquePtrRefWrapper; + + // MARK: ClientPipe + + int start(EventHandler event_handler) override + { + return reference().start(event_handler); + } + int sendMessage(const Payload payload) override + { + return reference().sendMessage(payload); + } + + }; // RefWrapper + + MOCK_METHOD(void, deinit, (), (const)); + MOCK_METHOD(int, start, (EventHandler event_handler), (override)); + MOCK_METHOD(int, sendMessage, (const Payload payload), (override)); + +}; // ClientPipeMock + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_CLIENT_PIPE_MOCK_HPP_INCLUDED diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 118c36b..c2dfedd 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -3,17 +3,22 @@ // SPDX-License-Identifier: MIT // +#include "client_pipe_mock.hpp" #include "ipc/client_router.hpp" #include #include +#include +#include + namespace { using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in the unit tests. using testing::NotNull; +using testing::StrictMock; class TestClientRouter : public testing::Test { @@ -26,8 +31,14 @@ class TestClientRouter : public testing::Test TEST_F(TestClientRouter, make) { - const auto router = ClientRouter::make(); - EXPECT_THAT(router, NotNull()); + StrictMock client_pipe_mock; + { + auto client_pipe = std::make_unique(client_pipe_mock); + const auto client_router = ClientRouter::make(std::move(client_pipe)); + EXPECT_THAT(client_router, NotNull()); + + EXPECT_CALL(client_pipe_mock, deinit()).Times(1); + } } } // namespace diff --git a/test/unique_ptr_refwrapper.hpp b/test/unique_ptr_refwrapper.hpp new file mode 100644 index 0000000..8415bd4 --- /dev/null +++ b/test/unique_ptr_refwrapper.hpp @@ -0,0 +1,42 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED +#define OCVSMD_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED + +namespace ocvsmd +{ + +template +struct UniquePtrRefWrapper : Interface +{ + explicit UniquePtrRefWrapper(Reference& reference) + : reference_{reference} + { + } + + UniquePtrRefWrapper(const UniquePtrRefWrapper& other) = delete; + UniquePtrRefWrapper(UniquePtrRefWrapper&&) noexcept = delete; + UniquePtrRefWrapper& operator=(const UniquePtrRefWrapper&) = delete; + UniquePtrRefWrapper& operator=(UniquePtrRefWrapper&&) noexcept = delete; + + virtual ~UniquePtrRefWrapper() + { + reference_.deinit(); + } + + Reference& reference() + { + return reference_; + } + +private: + Reference& reference_; + +}; // UniquePtrRefWrapper + +} // namespace ocvsmd + +#endif // OCVSMD_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED From 150c9ca86e00e74c8636e164c29656fd64a54ba2 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 15:39:56 +0200 Subject: [PATCH 019/156] added `ServerPipe` and `ServerRouter` interfaces --- src/common/CMakeLists.txt | 5 +- src/common/ipc/client_router.cpp | 9 ++- src/common/ipc/client_router.hpp | 4 +- src/common/ipc/{ => pipe}/client_pipe.hpp | 4 +- src/common/ipc/pipe/server_pipe.hpp | 77 +++++++++++++++++++ .../ipc/{ => pipe}/unix_socket_base.hpp | 12 +++ .../ipc/{ => pipe}/unix_socket_client.cpp | 4 +- .../ipc/{ => pipe}/unix_socket_client.hpp | 5 +- .../ipc/{ => pipe}/unix_socket_server.cpp | 14 ++-- .../ipc/{ => pipe}/unix_socket_server.hpp | 55 +++++-------- src/common/ipc/server_router.cpp | 47 +++++++++++ src/common/ipc/server_router.hpp | 43 +++++++++++ src/daemon/engine/application.cpp | 12 +-- src/daemon/engine/application.hpp | 4 +- src/sdk/daemon.cpp | 10 +-- test/common/CMakeLists.txt | 1 + .../ipc/{ => pipe}/client_pipe_mock.hpp | 5 +- test/common/ipc/pipe/server_pipe_mock.hpp | 56 ++++++++++++++ test/common/ipc/test_client_router.cpp | 7 +- test/common/ipc/test_server_router.cpp | 45 +++++++++++ 20 files changed, 348 insertions(+), 71 deletions(-) rename src/common/ipc/{ => pipe}/client_pipe.hpp (97%) create mode 100644 src/common/ipc/pipe/server_pipe.hpp rename src/common/ipc/{ => pipe}/unix_socket_base.hpp (91%) rename src/common/ipc/{ => pipe}/unix_socket_client.cpp (98%) rename src/common/ipc/{ => pipe}/unix_socket_client.hpp (94%) rename src/common/ipc/{ => pipe}/unix_socket_server.cpp (94%) rename src/common/ipc/{ => pipe}/unix_socket_server.hpp (55%) create mode 100644 src/common/ipc/server_router.cpp create mode 100644 src/common/ipc/server_router.hpp rename test/common/ipc/{ => pipe}/client_pipe_mock.hpp (94%) create mode 100644 test/common/ipc/pipe/server_pipe_mock.hpp create mode 100644 test/common/ipc/test_server_router.cpp diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index aa05153..ed60fdb 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -21,8 +21,9 @@ add_cyphal_library( add_library(ocvsmd_common ipc/client_router.cpp - ipc/unix_socket_client.cpp - ipc/unix_socket_server.cpp + ipc/pipe/unix_socket_client.cpp + ipc/pipe/unix_socket_server.cpp + ipc/server_router.cpp ) target_link_libraries(ocvsmd_common PUBLIC ${common_transpiled} diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 4e97891..1a54366 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -3,9 +3,10 @@ // SPDX-License-Identifier: MIT // -#include "client_pipe.hpp" #include "client_router.hpp" +#include "pipe/client_pipe.hpp" + #include #include @@ -23,20 +24,20 @@ namespace class ClientRouterImpl final : public ClientRouter { public: - explicit ClientRouterImpl(ClientPipe::Ptr client_pipe) + explicit ClientRouterImpl(pipe::ClientPipe::Ptr client_pipe) : client_pipe_{std::move(client_pipe)} { CETL_DEBUG_ASSERT(client_pipe_, ""); } private: - ClientPipe::Ptr client_pipe_; + pipe::ClientPipe::Ptr client_pipe_; }; // ClientRouterImpl } // namespace -ClientRouter::Ptr ClientRouter::make(ClientPipe::Ptr client_pipe) +ClientRouter::Ptr ClientRouter::make(pipe::ClientPipe::Ptr client_pipe) { return std::make_unique(std::move(client_pipe)); } diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index 138b7e9..a7deb02 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -6,7 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED #define OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED -#include "client_pipe.hpp" +#include "pipe/client_pipe.hpp" #include @@ -22,7 +22,7 @@ class ClientRouter public: using Ptr = std::unique_ptr; - static Ptr make(ClientPipe::Ptr client_pipe); + static Ptr make(pipe::ClientPipe::Ptr client_pipe); ClientRouter(ClientRouter&&) = delete; ClientRouter(const ClientRouter&) = delete; diff --git a/src/common/ipc/client_pipe.hpp b/src/common/ipc/pipe/client_pipe.hpp similarity index 97% rename from src/common/ipc/client_pipe.hpp rename to src/common/ipc/pipe/client_pipe.hpp index 65ac123..9e0edb0 100644 --- a/src/common/ipc/client_pipe.hpp +++ b/src/common/ipc/pipe/client_pipe.hpp @@ -19,7 +19,8 @@ namespace common { namespace ipc { - +namespace pipe +{ class ClientPipe { public: @@ -60,6 +61,7 @@ class ClientPipe }; // ClientPipe +} // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/pipe/server_pipe.hpp b/src/common/ipc/pipe/server_pipe.hpp new file mode 100644 index 0000000..77b7f04 --- /dev/null +++ b/src/common/ipc/pipe/server_pipe.hpp @@ -0,0 +1,77 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED + +#include +#include + +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class ServerPipe +{ +public: + using Ptr = std::unique_ptr; + + using ClientId = std::size_t; + using Payload = cetl::span; + + struct Event + { + struct Connected + { + ClientId client_id; + }; + struct Disconnected + { + ClientId client_id; + }; + struct Message + { + ClientId client_id; + Payload payload; + + }; // Message + + using Var = cetl::variant; + + }; // Event + + using EventHandler = std::function; + + ServerPipe(ServerPipe&&) = delete; + ServerPipe(const ServerPipe&) = delete; + ServerPipe& operator=(ServerPipe&&) = delete; + ServerPipe& operator=(const ServerPipe&) = delete; + + virtual ~ServerPipe() = default; + + virtual int start(EventHandler event_handler) = 0; + virtual int sendMessage(const ClientId client_id, const Payload payload) = 0; + +protected: + ServerPipe() = default; + +}; // ServerPipe + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED diff --git a/src/common/ipc/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp similarity index 91% rename from src/common/ipc/unix_socket_base.hpp rename to src/common/ipc/pipe/unix_socket_base.hpp index 49e101e..ce4bb68 100644 --- a/src/common/ipc/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -26,10 +26,21 @@ namespace common { namespace ipc { +namespace pipe +{ class UnixSocketBase { +public: + UnixSocketBase(UnixSocketBase&&) = delete; + UnixSocketBase(const UnixSocketBase&) = delete; + UnixSocketBase& operator=(UnixSocketBase&&) = delete; + UnixSocketBase& operator=(const UnixSocketBase&) = delete; + protected: + UnixSocketBase() = default; + ~UnixSocketBase() = default; + static int sendMessage(const int output_fd, const cetl::span payload) { // 1. Write the message header. @@ -127,6 +138,7 @@ class UnixSocketBase }; // UnixSocketBase +} // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp similarity index 98% rename from src/common/ipc/unix_socket_client.cpp rename to src/common/ipc/pipe/unix_socket_client.cpp index a0c1a7b..be4d11a 100644 --- a/src/common/ipc/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -29,6 +28,8 @@ namespace common { namespace ipc { +namespace pipe +{ UnixSocketClient::UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path) : socket_path_{std::move(socket_path)} @@ -123,6 +124,7 @@ void UnixSocketClient::handle_socket() } } +} // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp similarity index 94% rename from src/common/ipc/unix_socket_client.hpp rename to src/common/ipc/pipe/unix_socket_client.hpp index 6dfa537..7e58acf 100644 --- a/src/common/ipc/unix_socket_client.hpp +++ b/src/common/ipc/pipe/unix_socket_client.hpp @@ -21,6 +21,8 @@ namespace common { namespace ipc { +namespace pipe +{ class UnixSocketClient final : public UnixSocketBase, public ClientPipe { @@ -50,10 +52,11 @@ class UnixSocketClient final : public UnixSocketBase, public ClientPipe int client_fd_; platform::IPosixExecutorExtension* const posix_executor_ext_; libcyphal::IExecutor::Callback::Any socket_callback_; - std::function event_handler_; + EventHandler event_handler_; }; // UnixSocketClient +} // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp similarity index 94% rename from src/common/ipc/unix_socket_server.cpp rename to src/common/ipc/pipe/unix_socket_server.cpp index 0a1260e..514fed1 100644 --- a/src/common/ipc/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -30,6 +29,8 @@ namespace common { namespace ipc { +namespace pipe +{ namespace { @@ -98,12 +99,12 @@ UnixSocketServer::~UnixSocketServer() } } -int UnixSocketServer::start(std::function&& client_event_handler) +int UnixSocketServer::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(server_fd_ == -1, ""); CETL_DEBUG_ASSERT(client_event_handler, ""); - client_event_handler_ = std::move(client_event_handler); + event_handler_ = std::move(event_handler); if (const auto err = platform::posixSyscallError([this] { // @@ -188,14 +189,14 @@ void UnixSocketServer::handle_accept() client_id_to_fd_[new_client_id] = client_fd; client_fd_to_context_.emplace(client_fd, std::move(client_context)); - client_event_handler_(ClientEvent::Connected{new_client_id}); + event_handler_(Event::Connected{new_client_id}); } void UnixSocketServer::handle_client_request(const ClientId client_id, const int client_fd) { if (const auto err = receiveMessage(client_fd, [this, client_id](const auto payload) { // - return client_event_handler_(ClientEvent::Message{client_id, payload}); + return event_handler_(Event::Message{client_id, payload}); })) { if (err == -1) @@ -216,10 +217,11 @@ void UnixSocketServer::handle_client_request(const ClientId client_id, const int client_id_to_fd_.erase(client_id); client_fd_to_context_.erase(client_fd); - client_event_handler_(ClientEvent::Disconnected{client_id}); + event_handler_(Event::Disconnected{client_id}); } } +} // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp similarity index 55% rename from src/common/ipc/unix_socket_server.hpp rename to src/common/ipc/pipe/unix_socket_server.hpp index 7d4f0df..86c3f93 100644 --- a/src/common/ipc/unix_socket_server.hpp +++ b/src/common/ipc/pipe/unix_socket_server.hpp @@ -7,15 +7,13 @@ #define OCVSMD_COMMON_IPC_UNIX_SOCKET_SERVER_HPP_INCLUDED #include "ocvsmd/platform/posix_executor_extension.hpp" +#include "server_pipe.hpp" #include "unix_socket_base.hpp" #include -#include #include #include -#include -#include #include #include #include @@ -26,12 +24,16 @@ namespace common { namespace ipc { +namespace pipe +{ namespace detail { class ClientContext { public: + using Ptr = std::unique_ptr; + ClientContext() = default; ClientContext(ClientContext&&) = delete; @@ -45,31 +47,9 @@ class ClientContext } // namespace detail -class UnixSocketServer final : public UnixSocketBase +class UnixSocketServer final : public UnixSocketBase, public ServerPipe { public: - using ClientId = std::size_t; - - struct ClientEvent - { - struct Message - { - ClientId client_id; - cetl::span payload; - }; - struct Connected - { - ClientId client_id; - }; - struct Disconnected - { - ClientId client_id; - }; - - using Var = cetl::variant; - - }; // ClientEvent - UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path); UnixSocketServer(UnixSocketServer&&) = delete; @@ -77,11 +57,11 @@ class UnixSocketServer final : public UnixSocketBase UnixSocketServer& operator=(UnixSocketServer&&) = delete; UnixSocketServer& operator=(const UnixSocketServer&) = delete; - ~UnixSocketServer(); + ~UnixSocketServer() override; - int start(std::function&& client_event_handler); + int start(EventHandler event_handler) override; - int sendMessage(const ClientId client_id, const cetl::span payload) const + int sendMessage(const ClientId client_id, const Payload payload) override { const auto id_and_fd = client_id_to_fd_.find(client_id); if (id_and_fd == client_id_to_fd_.end()) @@ -96,17 +76,18 @@ class UnixSocketServer final : public UnixSocketBase void handle_client_connection(const int client_fd); void handle_client_request(const ClientId client_id, const int client_fd); - const std::string socket_path_; - int server_fd_; - platform::IPosixExecutorExtension* const posix_executor_ext_; - ClientId unique_client_id_counter_; - std::function client_event_handler_; - std::unordered_map> client_fd_to_context_; - std::unordered_map client_id_to_fd_; - libcyphal::IExecutor::Callback::Any accept_callback_; + const std::string socket_path_; + int server_fd_; + platform::IPosixExecutorExtension* const posix_executor_ext_; + ClientId unique_client_id_counter_; + EventHandler event_handler_; + std::unordered_map client_fd_to_context_; + std::unordered_map client_id_to_fd_; + libcyphal::IExecutor::Callback::Any accept_callback_; }; // UnixSocketServer +} // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp new file mode 100644 index 0000000..8ca5a17 --- /dev/null +++ b/src/common/ipc/server_router.cpp @@ -0,0 +1,47 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "server_router.hpp" + +#include "pipe/server_pipe.hpp" + +#include + +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace +{ + +class ServerRouterImpl final : public ServerRouter +{ +public: + explicit ServerRouterImpl(pipe::ServerPipe::Ptr server_pipe) + : server_pipe_{std::move(server_pipe)} + { + CETL_DEBUG_ASSERT(server_pipe_, ""); + } + +private: + pipe::ServerPipe::Ptr server_pipe_; + +}; // ClientRouterImpl + +} // namespace + +ServerRouter::Ptr ServerRouter::make(pipe::ServerPipe::Ptr server_pipe) +{ + return std::make_unique(std::move(server_pipe)); +} + +} // namespace ipc +} // namespace common +} // namespace ocvsmd diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp new file mode 100644 index 0000000..dff8bd4 --- /dev/null +++ b/src/common/ipc/server_router.hpp @@ -0,0 +1,43 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_SERVER_ROUTER_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_SERVER_ROUTER_HPP_INCLUDED + +#include "pipe/server_pipe.hpp" + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +class ServerRouter +{ +public: + using Ptr = std::unique_ptr; + + static Ptr make(pipe::ServerPipe::Ptr server_pipe); + + ServerRouter(ServerRouter&&) = delete; + ServerRouter(const ServerRouter&) = delete; + ServerRouter& operator=(ServerRouter&&) = delete; + ServerRouter& operator=(const ServerRouter&) = delete; + + virtual ~ServerRouter() = default; + +protected: + ServerRouter() = default; + +}; // ServerRouter + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_SERVER_ROUTER_HPP_INCLUDED diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 976ed74..8cee730 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -60,13 +60,13 @@ cetl::optional Application::init() .setSoftwareVcsRevisionId(VCS_REVISION_ID) .setUniqueId(getUniqueId()); - if (const auto err = ipc_server_.start([this](const auto& client_event) { + if (const auto err = ipc_server_.start([this](const auto& event) { // - using ClientEvent = common::ipc::UnixSocketServer::ClientEvent; + using Event = common::ipc::pipe::ServerPipe::Event; cetl::visit( // cetl::make_overloaded( - [this](const ClientEvent::Connected& connected) { + [this](const Event::Connected& connected) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client connected (%zu).", connected.client_id); @@ -77,18 +77,18 @@ cetl::optional Application::init() connected.client_id, {reinterpret_cast("Status2"), 7}); // NOLINT }, - [this](const ClientEvent::Message& message) { + [this](const Event::Message& message) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client msg (%zu).", message.client_id); (void) ipc_server_.sendMessage(message.client_id, message.payload); }, - [](const ClientEvent::Disconnected& disconnected) { + [](const Event::Disconnected& disconnected) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client disconnected (%zu).", disconnected.client_id); }), - client_event); + event); return 0; })) { diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/application.hpp index 201c61e..96f826a 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/application.hpp @@ -9,7 +9,7 @@ #include "cyphal/udp_transport_bag.hpp" #include "ocvsmd/platform/defines.hpp" -#include +#include #include #include @@ -46,7 +46,7 @@ class Application cetl::optional presentation_; cetl::optional node_; - common::ipc::UnixSocketServer ipc_server_{executor_, "/var/run/ocvsmd/local.sock"}; + common::ipc::pipe::UnixSocketServer ipc_server_{executor_, "/var/run/ocvsmd/local.sock"}; libcyphal::IExecutor::Callback::Any ipc_server_callback_; }; // Application diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index cd71a75..d69ef82 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -5,7 +5,7 @@ #include -#include "ipc/unix_socket_client.hpp" +#include "ipc/pipe/unix_socket_client.hpp" #include #include @@ -34,7 +34,7 @@ class DaemonImpl final : public Daemon { return 0 == ipc_client_.start([](const auto& event) { // - using Event = common::ipc::UnixSocketClient::Event; + using Event = common::ipc::pipe::ClientPipe::Event; cetl::visit( // cetl::make_overloaded( @@ -59,9 +59,9 @@ class DaemonImpl final : public Daemon } private: - cetl::pmr::memory_resource& memory_; - libcyphal::IExecutor& executor_; - common::ipc::UnixSocketClient ipc_client_{executor_, "/var/run/ocvsmd/local.sock"}; + cetl::pmr::memory_resource& memory_; + libcyphal::IExecutor& executor_; + common::ipc::pipe::UnixSocketClient ipc_client_{executor_, "/var/run/ocvsmd/local.sock"}; }; // DaemonImpl diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index 63ea7ee..8aff3d4 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -7,6 +7,7 @@ cmake_minimum_required(VERSION 3.27) add_executable(common_tests ipc/test_client_router.cpp + ipc/test_server_router.cpp ) target_link_libraries(common_tests ocvsmd_common diff --git a/test/common/ipc/client_pipe_mock.hpp b/test/common/ipc/pipe/client_pipe_mock.hpp similarity index 94% rename from test/common/ipc/client_pipe_mock.hpp rename to test/common/ipc/pipe/client_pipe_mock.hpp index cd62c26..ecd7913 100644 --- a/test/common/ipc/client_pipe_mock.hpp +++ b/test/common/ipc/pipe/client_pipe_mock.hpp @@ -6,7 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_CLIENT_PIPE_MOCK_HPP_INCLUDED #define OCVSMD_COMMON_IPC_CLIENT_PIPE_MOCK_HPP_INCLUDED -#include "ipc/client_pipe.hpp" +#include "ipc/pipe/client_pipe.hpp" #include "unique_ptr_refwrapper.hpp" #include @@ -19,6 +19,8 @@ namespace common { namespace ipc { +namespace pipe +{ class ClientPipeMock : public ClientPipe { @@ -46,6 +48,7 @@ class ClientPipeMock : public ClientPipe }; // ClientPipeMock +} // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/test/common/ipc/pipe/server_pipe_mock.hpp b/test/common/ipc/pipe/server_pipe_mock.hpp new file mode 100644 index 0000000..234c0b7 --- /dev/null +++ b/test/common/ipc/pipe/server_pipe_mock.hpp @@ -0,0 +1,56 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_SERVER_PIPE_MOCK_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_SERVER_PIPE_MOCK_HPP_INCLUDED + +#include "ipc/pipe/server_pipe.hpp" +#include "unique_ptr_refwrapper.hpp" + +#include + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class ServerPipeMock : public ServerPipe +{ +public: + struct RefWrapper final : UniquePtrRefWrapper + { + using UniquePtrRefWrapper::UniquePtrRefWrapper; + + // MARK: ServerPipe + + int start(EventHandler event_handler) override + { + return reference().start(event_handler); + } + int sendMessage(const ClientId client_id, const Payload payload) override + { + return reference().sendMessage(client_id, payload); + } + + }; // RefWrapper + + MOCK_METHOD(void, deinit, (), (const)); + MOCK_METHOD(int, start, (EventHandler event_handler), (override)); + MOCK_METHOD(int, sendMessage, (const ClientId client_id, const Payload payload), (override)); + +}; // ServerPipeMock + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_SERVER_PIPE_MOCK_HPP_INCLUDED diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index c2dfedd..1b9817d 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -3,9 +3,10 @@ // SPDX-License-Identifier: MIT // -#include "client_pipe_mock.hpp" #include "ipc/client_router.hpp" +#include "pipe/client_pipe_mock.hpp" + #include #include @@ -31,9 +32,9 @@ class TestClientRouter : public testing::Test TEST_F(TestClientRouter, make) { - StrictMock client_pipe_mock; + StrictMock client_pipe_mock; { - auto client_pipe = std::make_unique(client_pipe_mock); + auto client_pipe = std::make_unique(client_pipe_mock); const auto client_router = ClientRouter::make(std::move(client_pipe)); EXPECT_THAT(client_router, NotNull()); diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp new file mode 100644 index 0000000..368998c --- /dev/null +++ b/test/common/ipc/test_server_router.cpp @@ -0,0 +1,45 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "ipc/server_router.hpp" + +#include "pipe/server_pipe_mock.hpp" + +#include +#include + +#include +#include + +namespace +{ + +using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in the unit tests. + +using testing::NotNull; +using testing::StrictMock; + +class TestServerRouter : public testing::Test +{ +protected: + void SetUp() override {} + void TearDown() override {} +}; + +// MARK: - Tests: + +TEST_F(TestServerRouter, make) +{ + StrictMock server_pipe_mock; + { + auto server_pipe = std::make_unique(server_pipe_mock); + const auto server_router = ServerRouter::make(std::move(server_pipe)); + EXPECT_THAT(server_router, NotNull()); + + EXPECT_CALL(server_pipe_mock, deinit()).Times(1); + } +} + +} // namespace From 3814a483df77668b66bbf21dc44a83d54a0a40ad Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 3 Jan 2025 19:27:34 +0200 Subject: [PATCH 020/156] added `Channel` and `Gateway` entities --- src/common/ipc/channel.hpp | 121 +++++++++++++++++++++ src/common/ipc/client_router.cpp | 82 +++++++++++++- src/common/ipc/client_router.hpp | 20 +++- src/common/ipc/gateway.hpp | 70 ++++++++++++ src/common/ipc/pipe/client_pipe.hpp | 15 +-- src/common/ipc/pipe/server_pipe.hpp | 14 +-- src/common/ipc/pipe/unix_socket_base.hpp | 15 ++- src/common/ipc/pipe/unix_socket_client.hpp | 15 ++- src/common/ipc/pipe/unix_socket_server.cpp | 8 +- src/common/ipc/pipe/unix_socket_server.hpp | 15 ++- src/common/ipc/server_router.hpp | 8 +- test/common/ipc/test_client_router.cpp | 19 ++++ 12 files changed, 351 insertions(+), 51 deletions(-) create mode 100644 src/common/ipc/channel.hpp create mode 100644 src/common/ipc/gateway.hpp diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp new file mode 100644 index 0000000..1e57ef2 --- /dev/null +++ b/src/common/ipc/channel.hpp @@ -0,0 +1,121 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED + +#include "dsdl_helpers.hpp" +#include "gateway.hpp" + +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +class AnyChannel +{ +public: + struct Connected + {}; + + struct Disconnected + {}; + + template + using EventVar = cetl::variant; + + template + using EventHandler = std::function&)>; + +protected: + AnyChannel() = default; + +}; // AnyChannel + +template +class Channel final : public AnyChannel +{ +public: + Channel(Channel&& other) noexcept + : gateway_{std::move(other.gateway_)} + , event_handler_{std::move(other.event_handler_)} + { + setupEventHandler(); + } + + Channel& operator=(Channel&& other) noexcept + { + if (this != &other) + { + gateway_ = std::move(other.gateway_); + event_handler_ = std::move(other.event_handler_); + setupEventHandler(); + } + return *this; + } + + Channel(const Channel&) = delete; + Channel& operator=(const Channel&) = delete; + + ~Channel() + { + gateway_->setEventHandler(nullptr); + event_handler_ = nullptr; + } + + void send(const Output& output) + { + constexpr std::size_t BufferSize = Output::; + constexpr bool IsOnStack = BufferSize <= MsgSmallPayloadSize; + + return tryPerformOnSerialized( // + output, + [this](const auto payload) { + // + gateway_->send(payload); + }); + } + +private: + friend class ClientRouter; + + Channel(detail::Gateway::Ptr gateway, EventHandler event_handler) + : gateway_{std::move(gateway)} + , event_handler_{std::move(event_handler)} + { + CETL_DEBUG_ASSERT(gateway_, ""); + CETL_DEBUG_ASSERT(event_handler_, ""); + + setupEventHandler(); + } + + void setupEventHandler() + { + gateway_->setEventHandler([this](const auto& event) { + // + event_handler_(event); + }); + } + + static constexpr std::size_t MsgSmallPayloadSize = 256; + + detail::Gateway::Ptr gateway_; + EventHandler event_handler_; + +}; // Channel + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 1a54366..7b19d5e 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -5,11 +5,14 @@ #include "client_router.hpp" +#include "gateway.hpp" #include "pipe/client_pipe.hpp" #include +#include #include +#include #include namespace ocvsmd @@ -26,12 +29,89 @@ class ClientRouterImpl final : public ClientRouter public: explicit ClientRouterImpl(pipe::ClientPipe::Ptr client_pipe) : client_pipe_{std::move(client_pipe)} + , next_tag_{0} { CETL_DEBUG_ASSERT(client_pipe_, ""); } + // ClientRouter + + CETL_NODISCARD detail::Gateway::Ptr makeGateway() override + { + const Tag new_tag = ++next_tag_; + auto gateway = GatewayImpl::create(new_tag, *this); + tag_to_gateway_[new_tag] = gateway; + return gateway; + } + private: - pipe::ClientPipe::Ptr client_pipe_; + using Tag = std::uint64_t; + + class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway + { + struct Private + { + explicit Private() = default; + }; + + public: + static std::shared_ptr create(const Tag tag, ClientRouterImpl& router) + { + return std::make_shared(Private(), tag, router); + } + + GatewayImpl(Private, const Tag tag, ClientRouterImpl& router) + : tag_{tag} + , router_{router} + { + } + + GatewayImpl(const GatewayImpl&) = delete; + GatewayImpl(GatewayImpl&&) noexcept = delete; + GatewayImpl& operator=(const GatewayImpl&) = delete; + GatewayImpl& operator=(GatewayImpl&&) noexcept = delete; + + ~GatewayImpl() + { + setEventHandler(nullptr); + } + + void send(const Payload payload) override + { + router_.client_pipe_->sendMessage(payload); + } + + void event(const Event::Var& event) override + { + if (event_handler_) + { + event_handler_(event); + } + } + + void setEventHandler(EventHandler event_handler) override + { + if (event_handler) + { + event_handler_ = std::move(event_handler); + router_.tag_to_gateway_[tag_] = shared_from_this(); + } + else + { + router_.tag_to_gateway_.erase(tag_); + } + } + + private: + const Tag tag_; + ClientRouterImpl& router_; + EventHandler event_handler_; + + }; // GatewayImpl + + pipe::ClientPipe::Ptr client_pipe_; + Tag next_tag_; + std::unordered_map tag_to_gateway_; }; // ClientRouterImpl diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index a7deb02..85ff339 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -6,8 +6,12 @@ #ifndef OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED #define OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED +#include "channel.hpp" +#include "gateway.hpp" #include "pipe/client_pipe.hpp" +#include + #include namespace ocvsmd @@ -24,16 +28,24 @@ class ClientRouter static Ptr make(pipe::ClientPipe::Ptr client_pipe); - ClientRouter(ClientRouter&&) = delete; - ClientRouter(const ClientRouter&) = delete; - ClientRouter& operator=(ClientRouter&&) = delete; - ClientRouter& operator=(const ClientRouter&) = delete; + ClientRouter(const ClientRouter&) = delete; + ClientRouter(ClientRouter&&) noexcept = delete; + ClientRouter& operator=(const ClientRouter&) = delete; + ClientRouter& operator=(ClientRouter&&) noexcept = delete; virtual ~ClientRouter() = default; + template + CETL_NODISCARD Channel makeChannel(AnyChannel::EventHandler event_handler) + { + return Channel{makeGateway(), event_handler}; + } + protected: ClientRouter() = default; + CETL_NODISCARD virtual detail::Gateway::Ptr makeGateway() = 0; + }; // ClientRouter } // namespace ipc diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp new file mode 100644 index 0000000..475857d --- /dev/null +++ b/src/common/ipc/gateway.hpp @@ -0,0 +1,70 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED + +#include +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace detail +{ + +class Gateway +{ +public: + using Ptr = std::shared_ptr; + + using Payload = cetl::span; + + struct Event + { + struct Connected + {}; + struct Disconnected + {}; + struct Message + { + Payload payload; + + }; // Message + + using Var = cetl::variant; + + }; // Event + + using EventHandler = std::function; + + Gateway(const Gateway&) = delete; + Gateway(Gateway&&) noexcept = delete; + Gateway& operator=(const Gateway&) = delete; + Gateway& operator=(Gateway&&) noexcept = delete; + + virtual void send(const Payload payload) = 0; + virtual void event(const Event::Var& event) = 0; + virtual void setEventHandler(EventHandler event_handler) = 0; + +protected: + Gateway() = default; + ~Gateway() = default; + +}; // Gateway + +} // namespace detail +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED diff --git a/src/common/ipc/pipe/client_pipe.hpp b/src/common/ipc/pipe/client_pipe.hpp index 9e0edb0..384c952 100644 --- a/src/common/ipc/pipe/client_pipe.hpp +++ b/src/common/ipc/pipe/client_pipe.hpp @@ -3,8 +3,8 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED +#ifndef OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED #include #include @@ -21,6 +21,7 @@ namespace ipc { namespace pipe { + class ClientPipe { public: @@ -46,10 +47,10 @@ class ClientPipe using EventHandler = std::function; - ClientPipe(ClientPipe&&) = delete; - ClientPipe(const ClientPipe&) = delete; - ClientPipe& operator=(ClientPipe&&) = delete; - ClientPipe& operator=(const ClientPipe&) = delete; + ClientPipe(const ClientPipe&) = delete; + ClientPipe(ClientPipe&&) noexcept = delete; + ClientPipe& operator=(const ClientPipe&) = delete; + ClientPipe& operator=(ClientPipe&&) noexcept = delete; virtual ~ClientPipe() = default; @@ -66,4 +67,4 @@ class ClientPipe } // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_IPC_CLIENT_PIPE_HPP_INCLUDED +#endif // OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED diff --git a/src/common/ipc/pipe/server_pipe.hpp b/src/common/ipc/pipe/server_pipe.hpp index 77b7f04..686c1d7 100644 --- a/src/common/ipc/pipe/server_pipe.hpp +++ b/src/common/ipc/pipe/server_pipe.hpp @@ -3,8 +3,8 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED +#ifndef OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED #include #include @@ -54,10 +54,10 @@ class ServerPipe using EventHandler = std::function; - ServerPipe(ServerPipe&&) = delete; - ServerPipe(const ServerPipe&) = delete; - ServerPipe& operator=(ServerPipe&&) = delete; - ServerPipe& operator=(const ServerPipe&) = delete; + ServerPipe(const ServerPipe&) = delete; + ServerPipe(ServerPipe&&) noexcept = delete; + ServerPipe& operator=(const ServerPipe&) = delete; + ServerPipe& operator=(ServerPipe&&) noexcept = delete; virtual ~ServerPipe() = default; @@ -74,4 +74,4 @@ class ServerPipe } // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED +#endif // OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp index ce4bb68..29bd1d8 100644 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -3,10 +3,9 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_BASE_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_UNIX_SOCKET_BASE_HPP_INCLUDED +#ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED -#include "dsdl_helpers.hpp" #include "ocvsmd/platform/posix_utils.hpp" #include @@ -32,10 +31,10 @@ namespace pipe class UnixSocketBase { public: - UnixSocketBase(UnixSocketBase&&) = delete; - UnixSocketBase(const UnixSocketBase&) = delete; - UnixSocketBase& operator=(UnixSocketBase&&) = delete; - UnixSocketBase& operator=(const UnixSocketBase&) = delete; + UnixSocketBase(const UnixSocketBase&) = delete; + UnixSocketBase(UnixSocketBase&&) noexcept = delete; + UnixSocketBase& operator=(const UnixSocketBase&) = delete; + UnixSocketBase& operator=(UnixSocketBase&&) noexcept = delete; protected: UnixSocketBase() = default; @@ -143,4 +142,4 @@ class UnixSocketBase } // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_IPC_UNIX_SOCKET_BASE_HPP_INCLUDED +#endif // OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED diff --git a/src/common/ipc/pipe/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp index 7e58acf..b73f2f7 100644 --- a/src/common/ipc/pipe/unix_socket_client.hpp +++ b/src/common/ipc/pipe/unix_socket_client.hpp @@ -3,14 +3,13 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED +#ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED #include "client_pipe.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" #include "unix_socket_base.hpp" -#include #include #include @@ -29,10 +28,10 @@ class UnixSocketClient final : public UnixSocketBase, public ClientPipe public: UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path); - UnixSocketClient(UnixSocketClient&&) = delete; - UnixSocketClient(const UnixSocketClient&) = delete; - UnixSocketClient& operator=(UnixSocketClient&&) = delete; - UnixSocketClient& operator=(const UnixSocketClient&) = delete; + UnixSocketClient(const UnixSocketClient&) = delete; + UnixSocketClient(UnixSocketClient&&) noexcept = delete; + UnixSocketClient& operator=(const UnixSocketClient&) = delete; + UnixSocketClient& operator=(UnixSocketClient&&) noexcept = delete; ~UnixSocketClient() override; @@ -61,4 +60,4 @@ class UnixSocketClient final : public UnixSocketBase, public ClientPipe } // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED +#endif // OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index 514fed1..702147f 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -60,10 +60,10 @@ class ClientContextImpl final : public detail::ClientContext }); } - ClientContextImpl(ClientContextImpl&&) = delete; - ClientContextImpl(const ClientContextImpl&) = delete; - ClientContextImpl& operator=(ClientContextImpl&&) = delete; - ClientContextImpl& operator=(const ClientContextImpl&) = delete; + ClientContextImpl(const ClientContextImpl&) = delete; + ClientContextImpl(ClientContextImpl&&) noexcept = delete; + ClientContextImpl& operator=(const ClientContextImpl&) = delete; + ClientContextImpl& operator=(ClientContextImpl&&) noexcept = delete; void setCallback(libcyphal::IExecutor::Callback::Any&& fd_callback) { diff --git a/src/common/ipc/pipe/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp index 86c3f93..06b1003 100644 --- a/src/common/ipc/pipe/unix_socket_server.hpp +++ b/src/common/ipc/pipe/unix_socket_server.hpp @@ -3,14 +3,13 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_SERVER_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_UNIX_SOCKET_SERVER_HPP_INCLUDED +#ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED #include "ocvsmd/platform/posix_executor_extension.hpp" #include "server_pipe.hpp" #include "unix_socket_base.hpp" -#include #include #include @@ -36,10 +35,10 @@ class ClientContext ClientContext() = default; - ClientContext(ClientContext&&) = delete; - ClientContext(const ClientContext&) = delete; - ClientContext& operator=(ClientContext&&) = delete; - ClientContext& operator=(const ClientContext&) = delete; + ClientContext(const ClientContext&) = delete; + ClientContext(ClientContext&&) noexcept = delete; + ClientContext& operator=(const ClientContext&) = delete; + ClientContext& operator=(ClientContext&&) noexcept = delete; virtual ~ClientContext() = default; @@ -92,4 +91,4 @@ class UnixSocketServer final : public UnixSocketBase, public ServerPipe } // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_IPC_UNIX_SOCKET_SERVER_HPP_INCLUDED +#endif // OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index dff8bd4..a7f4218 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -24,10 +24,10 @@ class ServerRouter static Ptr make(pipe::ServerPipe::Ptr server_pipe); - ServerRouter(ServerRouter&&) = delete; - ServerRouter(const ServerRouter&) = delete; - ServerRouter& operator=(ServerRouter&&) = delete; - ServerRouter& operator=(const ServerRouter&) = delete; + ServerRouter(const ServerRouter&) = delete; + ServerRouter(ServerRouter&&) noexcept = delete; + ServerRouter& operator=(const ServerRouter&) = delete; + ServerRouter& operator=(ServerRouter&&) noexcept = delete; virtual ~ServerRouter() = default; diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 1b9817d..ab89584 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -42,4 +42,23 @@ TEST_F(TestClientRouter, make) } } +TEST_F(TestClientRouter, makeChannel) +{ + StrictMock client_pipe_mock; + { + auto client_pipe = std::make_unique(client_pipe_mock); + const auto client_router = ClientRouter::make(std::move(client_pipe)); + EXPECT_THAT(client_router, NotNull()); + + using Data = cetl::span; + + auto ch = client_router->makeChannel([](const auto&) {}); + // + // const std::array data{1, 2, 3, 4}; + // ch.send(data); + + EXPECT_CALL(client_pipe_mock, deinit()).Times(1); + } +} + } // namespace From 4b5d628070ff68c420353d39c700c1cac81c8e79 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 6 Jan 2025 19:35:25 +0200 Subject: [PATCH 021/156] implemented client side Route-s --- .../dsdl/ocvsmd/common/ipc/Route.1.0.dsdl | 7 + .../common/ipc/RouteChannelMsg.1.0.dsdl | 5 + .../ocvsmd/common/ipc/RouteConnect.1.0.dsdl | 4 + .../ExecCmd.1.0.dsdl} | 0 src/common/dsdl_helpers.hpp | 23 +- src/common/ipc/channel.hpp | 76 ++++-- src/common/ipc/client_router.cpp | 103 ++++++++- src/common/ipc/client_router.hpp | 8 +- src/daemon/engine/application.cpp | 2 + src/sdk/daemon.cpp | 1 + test/cetl_gtest_helpers.hpp | 42 ++++ test/common/ipc/pipe/client_pipe_mock.hpp | 7 + test/common/ipc/test_client_router.cpp | 216 ++++++++++++++++-- test/tracking_memory_resource.hpp | 136 +++++++++++ 14 files changed, 580 insertions(+), 50 deletions(-) create mode 100644 src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl rename src/common/dsdl/ocvsmd/common/{dsdl/Foo.1.0.dsdl => node_command/ExecCmd.1.0.dsdl} (100%) create mode 100644 test/cetl_gtest_helpers.hpp create mode 100644 test/tracking_memory_resource.hpp diff --git a/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl new file mode 100644 index 0000000..d9269ca --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl @@ -0,0 +1,7 @@ +@union + +uavcan.primitive.Empty.1.0 empty +RouteConnect.1.0 connect +RouteChannelMsg.1.0 channel_msg + +@sealed diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl new file mode 100644 index 0000000..1702795 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl @@ -0,0 +1,5 @@ +uint64 tag +uint64 type_id + +# reserve twice as much as we need. +@extent _offset_.max * 2 diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl new file mode 100644 index 0000000..7ad4358 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl @@ -0,0 +1,4 @@ +uavcan.node.Version.1.0 version + +# reserve twice as much as we need. +@extent _offset_.max * 2 diff --git a/src/common/dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl b/src/common/dsdl/ocvsmd/common/node_command/ExecCmd.1.0.dsdl similarity index 100% rename from src/common/dsdl/ocvsmd/common/dsdl/Foo.1.0.dsdl rename to src/common/dsdl/ocvsmd/common/node_command/ExecCmd.1.0.dsdl diff --git a/src/common/dsdl_helpers.hpp b/src/common/dsdl_helpers.hpp index f34c39b..b1439ec 100644 --- a/src/common/dsdl_helpers.hpp +++ b/src/common/dsdl_helpers.hpp @@ -26,6 +26,25 @@ static auto tryDeserializePayload(const cetl::span payload, return deserialize(out_message, {payload.data(), payload.size()}); } +template +static int tryPerformOnSerialized(const Message& message, Action&& action) +{ + // Try to serialize the message to raw payload buffer. + // + // Next nolint b/c we use a buffer to serialize the message, so no need to zero it (and performance better). + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + std::array buffer; + // + const auto result_size = serialize(message, {buffer.data(), buffer.size()}); + if (!result_size) + { + return EINVAL; + } + + const cetl::span bytes{buffer.data(), result_size.value()}; + return std::forward(action)(bytes); +} + template static auto tryPerformOnSerialized(const Message& message, Action&& action) -> std::enable_if_t { @@ -38,7 +57,7 @@ static auto tryPerformOnSerialized(const Message& message, Action&& action) -> s const auto result_size = serialize(message, {buffer.data(), buffer.size()}); if (!result_size) { - return result_size.error(); + return Result{result_size.error()}; } const cetl::span bytes{buffer.data(), result_size.value()}; @@ -56,7 +75,7 @@ static auto tryPerformOnSerialized(const Message& message, Action&& action) -> s const auto result_size = serialize(message, {buffer->data(), buffer->size()}); if (!result_size) { - return result_size.error(); + return Result{result_size.error()}; } const cetl::span bytes{buffer->data(), result_size.value()}; diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 1e57ef2..2506cec 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -9,6 +9,8 @@ #include "dsdl_helpers.hpp" #include "gateway.hpp" +#include + #include #include @@ -42,30 +44,26 @@ class AnyChannel }; // AnyChannel -template +template class Channel final : public AnyChannel { public: + using Input = Input_; + using Output = Output_; + using EventVar = EventVar; + using EventHandler = EventHandler; + Channel(Channel&& other) noexcept - : gateway_{std::move(other.gateway_)} + : memory_{other.memory_} + , gateway_{std::move(other.gateway_)} , event_handler_{std::move(other.event_handler_)} { setupEventHandler(); } - Channel& operator=(Channel&& other) noexcept - { - if (this != &other) - { - gateway_ = std::move(other.gateway_); - event_handler_ = std::move(other.event_handler_); - setupEventHandler(); - } - return *this; - } - - Channel(const Channel&) = delete; - Channel& operator=(const Channel&) = delete; + Channel(const Channel&) = delete; + Channel& operator=(const Channel&) = delete; + Channel& operator=(Channel&& other) noexcept = delete; ~Channel() { @@ -73,24 +71,29 @@ class Channel final : public AnyChannel event_handler_ = nullptr; } - void send(const Output& output) + using SendFailure = nunavut::support::Error; + using SendResult = cetl::optional; + + SendResult send(const Output& output) { - constexpr std::size_t BufferSize = Output::; + constexpr std::size_t BufferSize = Output::_traits_::SerializationBufferSizeBytes; constexpr bool IsOnStack = BufferSize <= MsgSmallPayloadSize; - return tryPerformOnSerialized( // + return tryPerformOnSerialized( // output, [this](const auto payload) { // gateway_->send(payload); + return cetl::nullopt; }); } private: friend class ClientRouter; - Channel(detail::Gateway::Ptr gateway, EventHandler event_handler) - : gateway_{std::move(gateway)} + Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, EventHandler event_handler) + : memory_{memory} + , gateway_{std::move(gateway)} , event_handler_{std::move(event_handler)} { CETL_DEBUG_ASSERT(gateway_, ""); @@ -101,16 +104,41 @@ class Channel final : public AnyChannel void setupEventHandler() { - gateway_->setEventHandler([this](const auto& event) { + gateway_->setEventHandler([this](const detail::Gateway::Event::Var& gateway_event_var) { // - event_handler_(event); + cetl::visit( + [this](const auto& gateway_event) { + // + handleGatewayEvent(gateway_event); + }, + gateway_event_var); }); } + void handleGatewayEvent(const detail::Gateway::Event::Message& gateway_message) + { + Input input{&memory_}; + if (tryDeserializePayload(gateway_message.payload, input)) + { + event_handler_(input); + } + } + + void handleGatewayEvent(const detail::Gateway::Event::Connected) + { + event_handler_(Connected{}); + } + + void handleGatewayEvent(const detail::Gateway::Event::Disconnected) + { + event_handler_(Disconnected{}); + } + static constexpr std::size_t MsgSmallPayloadSize = 256; - detail::Gateway::Ptr gateway_; - EventHandler event_handler_; + cetl::pmr::memory_resource& memory_; + detail::Gateway::Ptr gateway_; + EventHandler event_handler_; }; // Channel diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 7b19d5e..5336dc2 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -5,11 +5,20 @@ #include "client_router.hpp" +#include "dsdl_helpers.hpp" #include "gateway.hpp" #include "pipe/client_pipe.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/Route_1_0.hpp" +#include "uavcan/primitive/Empty_1_0.hpp" + #include +#include +#include +#include #include #include #include @@ -27,8 +36,9 @@ namespace class ClientRouterImpl final : public ClientRouter { public: - explicit ClientRouterImpl(pipe::ClientPipe::Ptr client_pipe) - : client_pipe_{std::move(client_pipe)} + ClientRouterImpl(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) + : memory_{memory} + , client_pipe_{std::move(client_pipe)} , next_tag_{0} { CETL_DEBUG_ASSERT(client_pipe_, ""); @@ -36,6 +46,24 @@ class ClientRouterImpl final : public ClientRouter // ClientRouter + cetl::pmr::memory_resource& memory() override + { + return memory_; + } + + void start() override + { + client_pipe_->start([this](const auto& pipe_event_var) { + // + return cetl::visit( + [this](const auto& pipe_event) { + // + return handlePipeEvent(pipe_event); + }, + pipe_event_var); + }); + } + CETL_NODISCARD detail::Gateway::Ptr makeGateway() override { const Tag new_tag = ++next_tag_; @@ -109,6 +137,73 @@ class ClientRouterImpl final : public ClientRouter }; // GatewayImpl + int handlePipeEvent(const pipe::ClientPipe::Event::Connected) + { + Route_1_0 route{&memory_}; + auto& route_conn = route.set_connect(); + route_conn.version.major = VERSION_MAJOR; + route_conn.version.minor = VERSION_MINOR; + + return tryPerformOnSerialized(route, [this](const auto payload) { + // + return client_pipe_->sendMessage(payload); + }); + } + + int handlePipeEvent(const pipe::ClientPipe::Event::Message& msg) + { + Route_1_0 route_msg{&memory_}; + const auto result_size = tryDeserializePayload(msg.payload, route_msg); + if (!result_size.has_value()) + { + return EINVAL; + } + + const auto remaining_payload = msg.payload.subspan(result_size.value()); + + cetl::visit(cetl::make_overloaded( + // + [this](const uavcan::primitive::Empty_1_0&) {}, + [this](const RouteConnect_1_0& route_conn) { + // + handleRouteConnect(route_conn); + }, + [this, remaining_payload](const RouteChannelMsg_1_0& route_channel) { + // + handleRouteChannelMsg(route_channel, remaining_payload); + }), + route_msg.union_value); + + return 0; + } + + int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) + { + for (auto& pair : tag_to_gateway_) + { + pair.second->event(detail::Gateway::Event::Disconnected{}); + } + return 0; + } + + void handleRouteConnect(const RouteConnect_1_0&) + { + for (auto& pair : tag_to_gateway_) + { + pair.second->event(detail::Gateway::Event::Connected{}); + } + } + + void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_channel_msg, pipe::ClientPipe::Payload payload) + { + const auto it = tag_to_gateway_.find(route_channel_msg.tag); + if (it != tag_to_gateway_.end()) + { + it->second->event(detail::Gateway::Event::Message{payload}); + } + } + + cetl::pmr::memory_resource& memory_; pipe::ClientPipe::Ptr client_pipe_; Tag next_tag_; std::unordered_map tag_to_gateway_; @@ -117,9 +212,9 @@ class ClientRouterImpl final : public ClientRouter } // namespace -ClientRouter::Ptr ClientRouter::make(pipe::ClientPipe::Ptr client_pipe) +ClientRouter::Ptr ClientRouter::make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) { - return std::make_unique(std::move(client_pipe)); + return std::make_unique(memory, std::move(client_pipe)); } } // namespace ipc diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index 85ff339..3e814eb 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -11,6 +11,7 @@ #include "pipe/client_pipe.hpp" #include +#include #include @@ -26,7 +27,7 @@ class ClientRouter public: using Ptr = std::unique_ptr; - static Ptr make(pipe::ClientPipe::Ptr client_pipe); + static Ptr make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe); ClientRouter(const ClientRouter&) = delete; ClientRouter(ClientRouter&&) noexcept = delete; @@ -35,10 +36,13 @@ class ClientRouter virtual ~ClientRouter() = default; + virtual void start() = 0; + virtual cetl::pmr::memory_resource& memory() = 0; + template CETL_NODISCARD Channel makeChannel(AnyChannel::EventHandler event_handler) { - return Channel{makeGateway(), event_handler}; + return Channel{memory(), makeGateway(), event_handler}; } protected: diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 8cee730..3af01a1 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -5,6 +5,8 @@ #include "application.hpp" +#include "ipc/pipe/server_pipe.hpp" + #include #include #include diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index d69ef82..c7d3e20 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -5,6 +5,7 @@ #include +#include "ipc/pipe/client_pipe.hpp" #include "ipc/pipe/unix_socket_client.hpp" #include diff --git a/test/cetl_gtest_helpers.hpp b/test/cetl_gtest_helpers.hpp new file mode 100644 index 0000000..26809c6 --- /dev/null +++ b/test/cetl_gtest_helpers.hpp @@ -0,0 +1,42 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_CETL_GTEST_HELPERS_HPP_INCLUDED +#define OCVSMD_CETL_GTEST_HELPERS_HPP_INCLUDED + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +// MARK: - GTest Printers: + +namespace cetl +{ +namespace pf20 +{ + +template +inline std::ostream& operator<<(std::ostream& os, const cetl::span& items) +{ + os << "{size=" << items.size() << ", data=["; + for (const auto& item : items) + { + os << testing::PrintToString(item) << ", "; + } + return os << ")}"; +} + +} // namespace pf20 +} // namespace cetl + +#endif // OCVSMD_CETL_GTEST_HELPERS_HPP_INCLUDED diff --git a/test/common/ipc/pipe/client_pipe_mock.hpp b/test/common/ipc/pipe/client_pipe_mock.hpp index ecd7913..5b5d75f 100644 --- a/test/common/ipc/pipe/client_pipe_mock.hpp +++ b/test/common/ipc/pipe/client_pipe_mock.hpp @@ -33,6 +33,7 @@ class ClientPipeMock : public ClientPipe int start(EventHandler event_handler) override { + reference().event_handler_ = event_handler; return reference().start(event_handler); } int sendMessage(const Payload payload) override @@ -46,6 +47,12 @@ class ClientPipeMock : public ClientPipe MOCK_METHOD(int, start, (EventHandler event_handler), (override)); MOCK_METHOD(int, sendMessage, (const Payload payload), (override)); + // MARK: Data members: + + // NOLINTBEGIN + EventHandler event_handler_; + // NOLINTEND + }; // ClientPipeMock } // namespace pipe diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index ab89584..e345c99 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -5,27 +5,102 @@ #include "ipc/client_router.hpp" +#include "cetl_gtest_helpers.hpp" // NOLINT(misc-include-cleaner) +#include "ipc/channel.hpp" +#include "ipc/pipe/client_pipe.hpp" #include "pipe/client_pipe_mock.hpp" +#include "tracking_memory_resource.hpp" + +#include "ocvsmd/common/ipc/Route_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" + +#include +#include #include #include +#include +#include #include #include +#include namespace { using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in the unit tests. +using testing::_; +using testing::IsTrue; +using testing::Return; +using testing::IsEmpty; +using testing::IsFalse; using testing::NotNull; using testing::StrictMock; +using testing::ElementsAre; +using testing::VariantWith; +using testing::MockFunction; + +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) class TestClientRouter : public testing::Test { protected: - void SetUp() override {} - void TearDown() override {} + void SetUp() override + { + cetl::pmr::set_default_resource(&mr_); + } + + void TearDown() override + { + EXPECT_THAT(mr_.allocations, IsEmpty()); + EXPECT_THAT(mr_.total_allocated_bytes, mr_.total_deallocated_bytes); + } + + template + void withRouteConnect(const RouteConnect_1_0& connect, Action action) + { + using ocvsmd::common::tryPerformOnSerialized; + + Route_1_0 route{&mr_}; + route.set_connect(connect); + + tryPerformOnSerialized(route, [&](const auto payload) { + // + action(payload); + return 0; + }); + } + + template + void withRouteChannelMsg(const std::uint64_t tag, const Message& message, Action action) + { + using ocvsmd::common::tryPerformOnSerialized; + + Route_1_0 route{&mr_}; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = tag; + + tryPerformOnSerialized(route, [&](const auto prefix) { + // + return tryPerformOnSerialized(message, [&](const auto suffix) { + // + std::vector buffer; + std::copy(prefix.begin(), prefix.end(), std::back_inserter(buffer)); + std::copy(suffix.begin(), suffix.end(), std::back_inserter(buffer)); + action(cetl::span{buffer.data(), buffer.size()}); + return 0; + }); + }); + } + + // MARK: Data members: + + // NOLINTBEGIN + ocvsmd::TrackingMemoryResource mr_; + // NOLINTEND }; // MARK: - Tests: @@ -33,32 +108,137 @@ class TestClientRouter : public testing::Test TEST_F(TestClientRouter, make) { StrictMock client_pipe_mock; - { - auto client_pipe = std::make_unique(client_pipe_mock); - const auto client_router = ClientRouter::make(std::move(client_pipe)); - EXPECT_THAT(client_router, NotNull()); - EXPECT_CALL(client_pipe_mock, deinit()).Times(1); - } + auto client_pipe = std::make_unique(client_pipe_mock); + const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + ASSERT_THAT(client_router, NotNull()); + EXPECT_THAT(client_pipe_mock.event_handler_, IsFalse()); + + EXPECT_CALL(client_pipe_mock, deinit()).Times(1); +} + +TEST_F(TestClientRouter, start) +{ + StrictMock client_pipe_mock; + + auto client_pipe = std::make_unique(client_pipe_mock); + const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + ASSERT_THAT(client_router, NotNull()); + EXPECT_THAT(client_pipe_mock.event_handler_, IsFalse()); + + EXPECT_CALL(client_pipe_mock, start(_)).Times(1); + client_router->start(); + EXPECT_THAT(client_pipe_mock.event_handler_, IsTrue()); + + EXPECT_CALL(client_pipe_mock, deinit()).Times(1); } TEST_F(TestClientRouter, makeChannel) { + using Msg = ocvsmd::common::ipc::Route_1_0; + + StrictMock client_pipe_mock; + EXPECT_CALL(client_pipe_mock, deinit()).Times(1); + + auto client_pipe = std::make_unique(client_pipe_mock); + const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + ASSERT_THAT(client_router, NotNull()); + + EXPECT_CALL(client_pipe_mock, start(_)).Times(1); + client_router->start(); + + const auto channel = client_router->makeChannel([](const auto&) {}); + (void) channel; +} + +TEST_F(TestClientRouter, makeChannel_send) +{ + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + StrictMock client_pipe_mock; - { - auto client_pipe = std::make_unique(client_pipe_mock); - const auto client_router = ClientRouter::make(std::move(client_pipe)); - EXPECT_THAT(client_router, NotNull()); + EXPECT_CALL(client_pipe_mock, deinit()).Times(1); + + auto client_pipe = std::make_unique(client_pipe_mock); + const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + ASSERT_THAT(client_router, NotNull()); + + EXPECT_CALL(client_pipe_mock, start(_)).Times(1); + client_router->start(); + + auto channel = client_router->makeChannel([](const auto&) {}); - using Data = cetl::span; + Msg msg{&mr_}; + + EXPECT_CALL(client_pipe_mock, sendMessage(ElementsAre(0))).WillOnce(Return(0)); + channel.send(msg); + + msg.some_stuff.push_back(-1); + msg.some_stuff.push_back('X'); + EXPECT_CALL(client_pipe_mock, sendMessage(ElementsAre(2, 0xFF, 'X'))).WillOnce(Return(0)); + channel.send(msg); +} - auto ch = client_router->makeChannel([](const auto&) {}); +TEST_F(TestClientRouter, makeChannel_receive_events) +{ + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + ; + using Channel = Channel; + + StrictMock client_pipe_mock; + EXPECT_CALL(client_pipe_mock, deinit()).Times(1); + + auto client_pipe = std::make_unique(client_pipe_mock); + const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + ASSERT_THAT(client_router, NotNull()); + + EXPECT_CALL(client_pipe_mock, start(_)).Times(1); + client_router->start(); + + StrictMock> ch1_event_mock; + StrictMock> ch2_event_mock; + + const auto channel1 = client_router->makeChannel(ch1_event_mock.AsStdFunction()); + (void) channel1; + const auto channel2 = client_router->makeChannel(ch2_event_mock.AsStdFunction()); + (void) channel2; + + // Emulate that we've got pipe connected - `RouteConnect` should be sent to the server. + // + EXPECT_CALL(client_pipe_mock, sendMessage(_)).WillOnce(Return(0)); // RouteConnect -> server + client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Connected{}); + + // Emulate that server responded with its `RouteConnect` - all channels should be notified. + // + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); + withRouteConnect(RouteConnect_1_0{{1, 2, &mr_}, &mr_}, [&](const auto payload) { // - // const std::array data{1, 2, 3, 4}; - // ch.send(data); + client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); + }); - EXPECT_CALL(client_pipe_mock, deinit()).Times(1); - } + // Emulate that server posted `RouteChannelMsg` on tag #1. + // + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + withRouteChannelMsg(1, Channel::Input{&mr_}, [&](const auto payload) { + // + client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); + }); + + // Emulate that server posted `RouteChannelMsg` on tag #2. + // + EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); + withRouteChannelMsg(2, Channel::Input{&mr_}, [&](const auto payload) { + // + client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); + }); + + // Emulate that the pipe is disconnected - all channels should be notified. + // + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); + client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Disconnected{}); } +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + } // namespace diff --git a/test/tracking_memory_resource.hpp b/test/tracking_memory_resource.hpp new file mode 100644 index 0000000..d450207 --- /dev/null +++ b/test/tracking_memory_resource.hpp @@ -0,0 +1,136 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_TRACKING_MEMORY_RESOURCE_HPP_INCLUDED +#define OCVSMD_TRACKING_MEMORY_RESOURCE_HPP_INCLUDED + +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ + +class TrackingMemoryResource final : public cetl::pmr::memory_resource +{ +public: + struct Allocation final + { + std::size_t size; + void* pointer; + + friend void PrintTo(const Allocation& alloc, std::ostream* os) + { + *os << "\n{ptr=0x" << std::hex << alloc.pointer << ", size=" << std::dec << alloc.size << "}"; + } + }; + + // NOLINTBEGIN + std::vector allocations{}; + std::size_t allocated_bytes{0}; + std::size_t max_allocated_bytes{0}; + std::size_t total_allocated_bytes{0}; + std::size_t total_deallocated_bytes{0}; + cetl::pmr::memory_resource* memory_{cetl::pmr::new_delete_resource()}; + // NOLINTEND + +private: + // MARK: cetl::pmr::memory_resource + + void* do_allocate(std::size_t size_bytes, std::size_t alignment) override + { + if (alignment > alignof(std::max_align_t)) + { +#if defined(__cpp_exceptions) + throw std::bad_alloc(); +#endif + return nullptr; + } + + void* ptr = memory_->allocate(size_bytes, alignment); + + total_allocated_bytes += size_bytes; + allocations.push_back({size_bytes, ptr}); + + allocated_bytes += size_bytes; + max_allocated_bytes = std::max(max_allocated_bytes, allocated_bytes); + + return ptr; + } + + void do_deallocate(void* ptr, std::size_t size_bytes, std::size_t alignment) override + { + CETL_DEBUG_ASSERT((nullptr != ptr) || (0 == size_bytes), ""); + + if (nullptr != ptr) + { + auto prev_alloc = std::find_if(allocations.cbegin(), allocations.cend(), [ptr](const auto& alloc) { + return alloc.pointer == ptr; + }); + CETL_DEBUG_ASSERT(prev_alloc != allocations.cend(), ""); + if (prev_alloc != allocations.cend()) + { + CETL_DEBUG_ASSERT(prev_alloc->size == size_bytes, ""); + allocations.erase(prev_alloc); + } + total_deallocated_bytes += size_bytes; + } + + memory_->deallocate(ptr, size_bytes, alignment); + + allocated_bytes -= size_bytes; + } + +#if (__cplusplus < CETL_CPP_STANDARD_17) + + void* do_reallocate(void* ptr, + std::size_t old_size_bytes, + std::size_t new_size_bytes, + std::size_t alignment) override + { + CETL_DEBUG_ASSERT((nullptr != ptr) || (0 == old_size_bytes), ""); + + if (nullptr != ptr) + { + auto prev_alloc = std::find_if(allocations.cbegin(), allocations.cend(), [ptr](const auto& alloc) { + return alloc.pointer == ptr; + }); + CETL_DEBUG_ASSERT(prev_alloc != allocations.cend(), ""); + if (prev_alloc != allocations.cend()) + { + CETL_DEBUG_ASSERT(prev_alloc->size == old_size_bytes, ""); + allocations.erase(prev_alloc); + } + total_allocated_bytes -= old_size_bytes; + } + + auto* const new_ptr = memory_->reallocate(ptr, old_size_bytes, new_size_bytes, alignment); + + total_allocated_bytes += new_size_bytes; + allocations.push_back({new_size_bytes, new_ptr}); + + allocated_bytes -= old_size_bytes; + allocated_bytes += new_size_bytes; + max_allocated_bytes = std::max(max_allocated_bytes, allocated_bytes); + + return new_ptr; + } + +#endif + + bool do_is_equal(const cetl::pmr::memory_resource& rhs) const noexcept override + { + return (&rhs == this); + } + +}; // TrackingMemoryResource + +} // namespace ocvsmd + +#endif // OCVSMD_TRACKING_MEMORY_RESOURCE_HPP_INCLUDED From 12a254180dc05b0cef60e206d2ea7189baaedda0 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 6 Jan 2025 20:45:22 +0200 Subject: [PATCH 022/156] added pipe types --- src/common/ipc/client_router.cpp | 19 ++++++-- src/common/ipc/gateway.hpp | 8 ++-- src/common/ipc/pipe/client_pipe.hpp | 8 ++-- src/common/ipc/pipe/pipe_types.hpp | 30 +++++++++++++ src/common/ipc/pipe/server_pipe.hpp | 7 +-- src/common/ipc/pipe/unix_socket_base.hpp | 38 +++++++++++----- src/common/ipc/pipe/unix_socket_client.hpp | 5 ++- src/common/ipc/pipe/unix_socket_server.hpp | 5 ++- src/daemon/engine/application.cpp | 50 +++++++++++----------- test/common/ipc/pipe/client_pipe_mock.hpp | 10 ++--- test/common/ipc/pipe/server_pipe_mock.hpp | 10 ++--- test/common/ipc/test_client_router.cpp | 5 ++- 12 files changed, 129 insertions(+), 66 deletions(-) create mode 100644 src/common/ipc/pipe/pipe_types.hpp diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 5336dc2..546e7ef 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -8,6 +8,7 @@ #include "dsdl_helpers.hpp" #include "gateway.hpp" #include "pipe/client_pipe.hpp" +#include "pipe/pipe_types.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" #include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" @@ -18,6 +19,7 @@ #include #include +#include #include #include #include @@ -104,9 +106,17 @@ class ClientRouterImpl final : public ClientRouter setEventHandler(nullptr); } - void send(const Payload payload) override + void send(const pipe::Payload payload) override { - router_.client_pipe_->sendMessage(payload); + Route_1_0 route{&router_.memory_}; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = tag_; + + tryPerformOnSerialized(route, [this, payload](const auto prefix) { + // + std::array fragments{prefix, payload}; + return router_.client_pipe_->sendMessage(fragments); + }); } void event(const Event::Var& event) override @@ -146,7 +156,8 @@ class ClientRouterImpl final : public ClientRouter return tryPerformOnSerialized(route, [this](const auto payload) { // - return client_pipe_->sendMessage(payload); + std::array payloads{payload}; + return client_pipe_->sendMessage(payloads); }); } @@ -194,7 +205,7 @@ class ClientRouterImpl final : public ClientRouter } } - void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_channel_msg, pipe::ClientPipe::Payload payload) + void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_channel_msg, pipe::Payload payload) { const auto it = tag_to_gateway_.find(route_channel_msg.tag); if (it != tag_to_gateway_.end()) diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index 475857d..c957a3d 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED #define OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED +#include "pipe/pipe_types.hpp" + #include #include @@ -27,8 +29,6 @@ class Gateway public: using Ptr = std::shared_ptr; - using Payload = cetl::span; - struct Event { struct Connected @@ -37,7 +37,7 @@ class Gateway {}; struct Message { - Payload payload; + pipe::Payload payload; }; // Message @@ -52,7 +52,7 @@ class Gateway Gateway& operator=(const Gateway&) = delete; Gateway& operator=(Gateway&&) noexcept = delete; - virtual void send(const Payload payload) = 0; + virtual void send(const pipe::Payload payload) = 0; virtual void event(const Event::Var& event) = 0; virtual void setEventHandler(EventHandler event_handler) = 0; diff --git a/src/common/ipc/pipe/client_pipe.hpp b/src/common/ipc/pipe/client_pipe.hpp index 384c952..51ffef6 100644 --- a/src/common/ipc/pipe/client_pipe.hpp +++ b/src/common/ipc/pipe/client_pipe.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED +#include "pipe_types.hpp" + #include #include @@ -27,8 +29,6 @@ class ClientPipe public: using Ptr = std::unique_ptr; - using Payload = cetl::span; - struct Event { struct Connected @@ -54,8 +54,8 @@ class ClientPipe virtual ~ClientPipe() = default; - virtual int start(EventHandler event_handler) = 0; - virtual int sendMessage(const Payload payload) = 0; + virtual int start(EventHandler event_handler) = 0; + virtual int sendMessage(const Payloads payloads) = 0; protected: ClientPipe() = default; diff --git a/src/common/ipc/pipe/pipe_types.hpp b/src/common/ipc/pipe/pipe_types.hpp new file mode 100644 index 0000000..ba7a672 --- /dev/null +++ b/src/common/ipc/pipe/pipe_types.hpp @@ -0,0 +1,30 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_PIPE_TYPES_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_TYPES_HPP_INCLUDED + +#include + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +using Payload = cetl::span; +using Payloads = cetl::span; + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_PIPE_TYPES_HPP_INCLUDED diff --git a/src/common/ipc/pipe/server_pipe.hpp b/src/common/ipc/pipe/server_pipe.hpp index 686c1d7..d2ca336 100644 --- a/src/common/ipc/pipe/server_pipe.hpp +++ b/src/common/ipc/pipe/server_pipe.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED +#include "pipe_types.hpp" + #include #include @@ -29,7 +31,6 @@ class ServerPipe using Ptr = std::unique_ptr; using ClientId = std::size_t; - using Payload = cetl::span; struct Event { @@ -61,8 +62,8 @@ class ServerPipe virtual ~ServerPipe() = default; - virtual int start(EventHandler event_handler) = 0; - virtual int sendMessage(const ClientId client_id, const Payload payload) = 0; + virtual int start(EventHandler event_handler) = 0; + virtual int sendMessage(const ClientId client_id, const Payloads payloads) = 0; protected: ServerPipe() = default; diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp index 29bd1d8..e487499 100644 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -7,6 +7,7 @@ #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED #include "ocvsmd/platform/posix_utils.hpp" +#include "pipe_types.hpp" #include @@ -15,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -40,24 +42,40 @@ class UnixSocketBase UnixSocketBase() = default; ~UnixSocketBase() = default; - static int sendMessage(const int output_fd, const cetl::span payload) + static int sendMessage(const int output_fd, const Payloads payloads) { - // 1. Write the message header. - if (const int err = platform::posixSyscallError([output_fd, payload] { + // 1. Write the message header (signature and total size of the following fragments). + // + const std::size_t total_size = std::accumulate( // + payloads.begin(), + payloads.end(), + 0ULL, + [](const std::size_t acc, const Payload payload) { + // + return acc + payload.size(); + }); + if (const int err = platform::posixSyscallError([output_fd, total_size] { // - const MsgHeader msg_header{.signature = MsgSignature, - .size = static_cast(payload.size())}; + const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; return ::write(output_fd, &msg_header, sizeof(msg_header)); })) { return err; } - // 2. Write the message payload. - return platform::posixSyscallError([output_fd, payload] { - // - return ::write(output_fd, payload.data(), payload.size()); - }); + // 2. Write the message payload fragments. + // + for (const auto payload : payloads) + { + if (const int err = platform::posixSyscallError([output_fd, payload] { + // + return ::write(output_fd, payload.data(), payload.size()); + })) + { + return err; + } + } + return 0; } template diff --git a/src/common/ipc/pipe/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp index b73f2f7..5349a60 100644 --- a/src/common/ipc/pipe/unix_socket_client.hpp +++ b/src/common/ipc/pipe/unix_socket_client.hpp @@ -8,6 +8,7 @@ #include "client_pipe.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" +#include "pipe_types.hpp" #include "unix_socket_base.hpp" #include @@ -39,9 +40,9 @@ class UnixSocketClient final : public UnixSocketBase, public ClientPipe int start(EventHandler event_handler) override; - int sendMessage(const Payload payload) override + int sendMessage(const Payloads payloads) override { - return UnixSocketBase::sendMessage(client_fd_, payload); + return UnixSocketBase::sendMessage(client_fd_, payloads); } private: diff --git a/src/common/ipc/pipe/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp index 06b1003..9f0a21d 100644 --- a/src/common/ipc/pipe/unix_socket_server.hpp +++ b/src/common/ipc/pipe/unix_socket_server.hpp @@ -7,6 +7,7 @@ #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED #include "ocvsmd/platform/posix_executor_extension.hpp" +#include "pipe_types.hpp" #include "server_pipe.hpp" #include "unix_socket_base.hpp" @@ -60,14 +61,14 @@ class UnixSocketServer final : public UnixSocketBase, public ServerPipe int start(EventHandler event_handler) override; - int sendMessage(const ClientId client_id, const Payload payload) override + int sendMessage(const ClientId client_id, const Payloads payloads) override { const auto id_and_fd = client_id_to_fd_.find(client_id); if (id_and_fd == client_id_to_fd_.end()) { return EINVAL; } - return UnixSocketBase::sendMessage(id_and_fd->second, payload); + return UnixSocketBase::sendMessage(id_and_fd->second, payloads); } private: diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 3af01a1..5dcac72 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -66,31 +66,31 @@ cetl::optional Application::init() // using Event = common::ipc::pipe::ServerPipe::Event; - cetl::visit( // - cetl::make_overloaded( - [this](const Event::Connected& connected) { - // - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Client connected (%zu).", connected.client_id); - (void) ipc_server_.sendMessage( // - connected.client_id, - {reinterpret_cast("Status1"), 7}); // NOLINT - (void) ipc_server_.sendMessage( // - connected.client_id, - {reinterpret_cast("Status2"), 7}); // NOLINT - }, - [this](const Event::Message& message) { - // - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Client msg (%zu).", message.client_id); - (void) ipc_server_.sendMessage(message.client_id, message.payload); - }, - [](const Event::Disconnected& disconnected) { - // - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Client disconnected (%zu).", disconnected.client_id); - }), - event); + // cetl::visit( // + // cetl::make_overloaded( + // [this](const Event::Connected& connected) { + // // + // // NOLINTNEXTLINE *-vararg + // ::syslog(LOG_DEBUG, "Client connected (%zu).", connected.client_id); + // (void) ipc_server_.sendMessage( // + // connected.client_id, + // {{reinterpret_cast("Status1"), 7}, 1}); // NOLINT + // (void) ipc_server_.sendMessage( // + // connected.client_id, + // {reinterpret_cast("Status2"), 7}); // NOLINT + // }, + // [this](const Event::Message& message) { + // // + // // NOLINTNEXTLINE *-vararg + // ::syslog(LOG_DEBUG, "Client msg (%zu).", message.client_id); + // (void) ipc_server_.sendMessage(message.client_id, message.payload); + // }, + // [](const Event::Disconnected& disconnected) { + // // + // // NOLINTNEXTLINE *-vararg + // ::syslog(LOG_DEBUG, "Client disconnected (%zu).", disconnected.client_id); + // }), + // event); return 0; })) { diff --git a/test/common/ipc/pipe/client_pipe_mock.hpp b/test/common/ipc/pipe/client_pipe_mock.hpp index 5b5d75f..e934ed0 100644 --- a/test/common/ipc/pipe/client_pipe_mock.hpp +++ b/test/common/ipc/pipe/client_pipe_mock.hpp @@ -7,12 +7,12 @@ #define OCVSMD_COMMON_IPC_CLIENT_PIPE_MOCK_HPP_INCLUDED #include "ipc/pipe/client_pipe.hpp" + +#include "ipc/pipe/pipe_types.hpp" #include "unique_ptr_refwrapper.hpp" #include -#include - namespace ocvsmd { namespace common @@ -36,16 +36,16 @@ class ClientPipeMock : public ClientPipe reference().event_handler_ = event_handler; return reference().start(event_handler); } - int sendMessage(const Payload payload) override + int sendMessage(const Payloads payloads) override { - return reference().sendMessage(payload); + return reference().sendMessage(payloads); } }; // RefWrapper MOCK_METHOD(void, deinit, (), (const)); MOCK_METHOD(int, start, (EventHandler event_handler), (override)); - MOCK_METHOD(int, sendMessage, (const Payload payload), (override)); + MOCK_METHOD(int, sendMessage, (const Payloads payloads), (override)); // MARK: Data members: diff --git a/test/common/ipc/pipe/server_pipe_mock.hpp b/test/common/ipc/pipe/server_pipe_mock.hpp index 234c0b7..31b2f43 100644 --- a/test/common/ipc/pipe/server_pipe_mock.hpp +++ b/test/common/ipc/pipe/server_pipe_mock.hpp @@ -7,12 +7,12 @@ #define OCVSMD_COMMON_IPC_SERVER_PIPE_MOCK_HPP_INCLUDED #include "ipc/pipe/server_pipe.hpp" + +#include "ipc/pipe/pipe_types.hpp" #include "unique_ptr_refwrapper.hpp" #include -#include - namespace ocvsmd { namespace common @@ -35,16 +35,16 @@ class ServerPipeMock : public ServerPipe { return reference().start(event_handler); } - int sendMessage(const ClientId client_id, const Payload payload) override + int sendMessage(const ClientId client_id, const Payloads payloads) override { - return reference().sendMessage(client_id, payload); + return reference().sendMessage(client_id, payloads); } }; // RefWrapper MOCK_METHOD(void, deinit, (), (const)); MOCK_METHOD(int, start, (EventHandler event_handler), (override)); - MOCK_METHOD(int, sendMessage, (const ClientId client_id, const Payload payload), (override)); + MOCK_METHOD(int, sendMessage, (const ClientId client_id, const Payloads payloads), (override)); }; // ServerPipeMock diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index e345c99..b2d159a 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -35,6 +35,7 @@ using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in th using testing::_; using testing::IsTrue; using testing::Return; +using testing::SizeIs; using testing::IsEmpty; using testing::IsFalse; using testing::NotNull; @@ -169,12 +170,12 @@ TEST_F(TestClientRouter, makeChannel_send) Msg msg{&mr_}; - EXPECT_CALL(client_pipe_mock, sendMessage(ElementsAre(0))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, sendMessage(SizeIs(1))).WillOnce(Return(0)); channel.send(msg); msg.some_stuff.push_back(-1); msg.some_stuff.push_back('X'); - EXPECT_CALL(client_pipe_mock, sendMessage(ElementsAre(2, 0xFF, 'X'))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, sendMessage(ElementsAre(ElementsAre(2, 0xFF, 'X')))).WillOnce(Return(0)); channel.send(msg); } From 286eeb143c72b3c01d87f40a1be21c044a129a4d Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 6 Jan 2025 20:54:06 +0200 Subject: [PATCH 023/156] fix unit test --- test/common/ipc/test_client_router.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index b2d159a..f7c3d31 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -170,12 +170,12 @@ TEST_F(TestClientRouter, makeChannel_send) Msg msg{&mr_}; - EXPECT_CALL(client_pipe_mock, sendMessage(SizeIs(1))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, sendMessage(SizeIs(2))).WillOnce(Return(0)); channel.send(msg); msg.some_stuff.push_back(-1); msg.some_stuff.push_back('X'); - EXPECT_CALL(client_pipe_mock, sendMessage(ElementsAre(ElementsAre(2, 0xFF, 'X')))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, sendMessage(SizeIs(2))).WillOnce(Return(0)); channel.send(msg); } From 890ec251ae96d557820e2a4c6c30a6b04c6c848b Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 7 Jan 2025 10:28:51 +0200 Subject: [PATCH 024/156] added msg type id --- src/common/ipc/channel.hpp | 22 +++++++++++++++++----- src/common/ipc/client_router.cpp | 3 ++- src/common/ipc/gateway.hpp | 8 +++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 2506cec..7fef3a5 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -33,11 +34,19 @@ class AnyChannel struct Disconnected {}; - template - using EventVar = cetl::variant; + template + using EventVar = cetl::variant; - template - using EventHandler = std::function&)>; + template + using EventHandler = std::function&)>; + + template + static detail::MsgTypeId getTypeId() noexcept + { + const cetl::string_view type_name{Message::_traits_::FullNameAndVersion()}; + const libcyphal::common::CRC64WE crc64{type_name.cbegin(), type_name.cend()}; + return crc64.get(); + } protected: AnyChannel() = default; @@ -57,6 +66,7 @@ class Channel final : public AnyChannel : memory_{other.memory_} , gateway_{std::move(other.gateway_)} , event_handler_{std::move(other.event_handler_)} + , output_type_id_{other.output_type_id_} { setupEventHandler(); } @@ -83,7 +93,7 @@ class Channel final : public AnyChannel output, [this](const auto payload) { // - gateway_->send(payload); + gateway_->send(output_type_id_, payload); return cetl::nullopt; }); } @@ -95,6 +105,7 @@ class Channel final : public AnyChannel : memory_{memory} , gateway_{std::move(gateway)} , event_handler_{std::move(event_handler)} + , output_type_id_{getTypeId()} { CETL_DEBUG_ASSERT(gateway_, ""); CETL_DEBUG_ASSERT(event_handler_, ""); @@ -139,6 +150,7 @@ class Channel final : public AnyChannel cetl::pmr::memory_resource& memory_; detail::Gateway::Ptr gateway_; EventHandler event_handler_; + detail::MsgTypeId output_type_id_; }; // Channel diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 546e7ef..6a05312 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -106,11 +106,12 @@ class ClientRouterImpl final : public ClientRouter setEventHandler(nullptr); } - void send(const pipe::Payload payload) override + void send(const detail::MsgTypeId type_id, const pipe::Payload payload) override { Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag_; + channel_msg.type_id = type_id; tryPerformOnSerialized(route, [this, payload](const auto prefix) { // diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index c957a3d..baaeb2f 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -24,6 +24,8 @@ namespace ipc namespace detail { +using MsgTypeId = std::uint64_t; + class Gateway { public: @@ -52,9 +54,9 @@ class Gateway Gateway& operator=(const Gateway&) = delete; Gateway& operator=(Gateway&&) noexcept = delete; - virtual void send(const pipe::Payload payload) = 0; - virtual void event(const Event::Var& event) = 0; - virtual void setEventHandler(EventHandler event_handler) = 0; + virtual void send(const MsgTypeId type_id, const pipe::Payload payload) = 0; + virtual void event(const Event::Var& event) = 0; + virtual void setEventHandler(EventHandler event_handler) = 0; protected: Gateway() = default; From 561e27280cdcd12015da370143b28f2cfeb3fa9e Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 7 Jan 2025 14:12:53 +0200 Subject: [PATCH 025/156] added server router --- src/common/ipc/channel.hpp | 35 +++- src/common/ipc/client_router.cpp | 72 ++++++-- src/common/ipc/gateway.hpp | 3 +- src/common/ipc/server_router.cpp | 215 +++++++++++++++++++++- src/common/ipc/server_router.hpp | 30 ++- test/common/ipc/pipe/server_pipe_mock.hpp | 7 + test/common/ipc/test_client_router.cpp | 30 +-- test/common/ipc/test_server_router.cpp | 147 ++++++++++++++- 8 files changed, 487 insertions(+), 52 deletions(-) diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 7fef3a5..09d3cf0 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -77,8 +77,14 @@ class Channel final : public AnyChannel ~Channel() { - gateway_->setEventHandler(nullptr); - event_handler_ = nullptr; + if (gateway_) + { + gateway_->setEventHandler(nullptr); + } + if (event_handler_) + { + event_handler_ = nullptr; + } } using SendFailure = nunavut::support::Error; @@ -98,8 +104,14 @@ class Channel final : public AnyChannel }); } + void setEventHandler(EventHandler event_handler) + { + event_handler_ = std::move(event_handler); + } + private: friend class ClientRouter; + friend class ServerRouter; Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, EventHandler event_handler) : memory_{memory} @@ -128,21 +140,30 @@ class Channel final : public AnyChannel void handleGatewayEvent(const detail::Gateway::Event::Message& gateway_message) { - Input input{&memory_}; - if (tryDeserializePayload(gateway_message.payload, input)) + if (event_handler_) { - event_handler_(input); + Input input{&memory_}; + if (tryDeserializePayload(gateway_message.payload, input)) + { + event_handler_(input); + } } } void handleGatewayEvent(const detail::Gateway::Event::Connected) { - event_handler_(Connected{}); + if (event_handler_) + { + event_handler_(Connected{}); + } } void handleGatewayEvent(const detail::Gateway::Event::Disconnected) { - event_handler_(Disconnected{}); + if (event_handler_) + { + event_handler_(Disconnected{}); + } } static constexpr std::size_t MsgSmallPayloadSize = 256; diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 6a05312..1201ac1 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace ocvsmd { @@ -148,8 +149,33 @@ class ClientRouterImpl final : public ClientRouter }; // GatewayImpl - int handlePipeEvent(const pipe::ClientPipe::Event::Connected) + template + void forEachGateway(Action action) const { + // Calling an action might indirectly modify the map, so we first + // collect strong pointers to gateways into a local collection. + // + std::vector gateways; + gateways.reserve(tag_to_gateway_.size()); + for (const auto& pair : tag_to_gateway_) + { + const auto gateway = pair.second.lock(); + if (gateway) + { + gateways.push_back(gateway); + } + } + + for (const auto& gateway : gateways) + { + action(gateway); + } + } + + int handlePipeEvent(const pipe::ClientPipe::Event::Connected) const + { + // TODO: log client pipe connection + Route_1_0 route{&memory_}; auto& route_conn = route.set_connect(); route_conn.version.major = VERSION_MAJOR; @@ -189,36 +215,46 @@ class ClientRouterImpl final : public ClientRouter return 0; } - int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) + int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) const { - for (auto& pair : tag_to_gateway_) - { - pair.second->event(detail::Gateway::Event::Disconnected{}); - } + // TODO: log client pipe disconnection + + forEachGateway([](const auto& gateway) { + // + gateway->event(detail::Gateway::Event::Disconnected{}); + }); return 0; } - void handleRouteConnect(const RouteConnect_1_0&) + void handleRouteConnect(const RouteConnect_1_0&) const { - for (auto& pair : tag_to_gateway_) - { - pair.second->event(detail::Gateway::Event::Connected{}); - } + // TODO: log server route connection + + forEachGateway([](const auto& gateway) { + // + gateway->event(detail::Gateway::Event::Connected{}); + }); } void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_channel_msg, pipe::Payload payload) { - const auto it = tag_to_gateway_.find(route_channel_msg.tag); - if (it != tag_to_gateway_.end()) + const auto tag_it = tag_to_gateway_.find(route_channel_msg.tag); + if (tag_it != tag_to_gateway_.end()) { - it->second->event(detail::Gateway::Event::Message{payload}); + const auto gateway = tag_it->second.lock(); + if (gateway) + { + gateway->event(detail::Gateway::Event::Message{payload}); + } } + + // TODO: log unsolicited message } - cetl::pmr::memory_resource& memory_; - pipe::ClientPipe::Ptr client_pipe_; - Tag next_tag_; - std::unordered_map tag_to_gateway_; + cetl::pmr::memory_resource& memory_; + pipe::ClientPipe::Ptr client_pipe_; + Tag next_tag_; + std::unordered_map tag_to_gateway_; }; // ClientRouterImpl diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index baaeb2f..6ddb5bd 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -29,7 +29,8 @@ using MsgTypeId = std::uint64_t; class Gateway { public: - using Ptr = std::shared_ptr; + using Ptr = std::shared_ptr; + using WeakPtr = std::weak_ptr; struct Event { diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 8ca5a17..a543ef6 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -5,11 +5,25 @@ #include "server_router.hpp" +#include "dsdl_helpers.hpp" +#include "gateway.hpp" +#include "pipe/pipe_types.hpp" #include "pipe/server_pipe.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/Route_1_0.hpp" +#include "uavcan/primitive/Empty_1_0.hpp" + #include +#include +#include +#include +#include +#include #include +#include #include namespace ocvsmd @@ -24,22 +38,213 @@ namespace class ServerRouterImpl final : public ServerRouter { public: - explicit ServerRouterImpl(pipe::ServerPipe::Ptr server_pipe) - : server_pipe_{std::move(server_pipe)} + explicit ServerRouterImpl(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe) + : memory_{memory} + , server_pipe_{std::move(server_pipe)} { CETL_DEBUG_ASSERT(server_pipe_, ""); } + // ServerRouter + + cetl::pmr::memory_resource& memory() override + { + return memory_; + } + + void start() override + { + server_pipe_->start([this](const auto& pipe_event_var) { + // + return cetl::visit( + [this](const auto& pipe_event) { + // + return handlePipeEvent(pipe_event); + }, + pipe_event_var); + }); + } + + void registerChannelFactory( // + const detail::MsgTypeId input_type_id, + TypeErasedChannelFactory channel_factory) override + { + type_id_to_channel_factory_[input_type_id] = std::move(channel_factory); + } + private: - pipe::ServerPipe::Ptr server_pipe_; + using Tag = std::uint64_t; + using ClientId = pipe::ServerPipe::ClientId; + + class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway + { + struct Private + { + explicit Private() = default; + }; + + public: + static std::shared_ptr create(const Tag tag, ServerRouterImpl& router, const ClientId client_id) + { + return std::make_shared(Private(), tag, router, client_id); + } + + GatewayImpl(Private, const Tag tag, ServerRouterImpl& router, const ClientId client_id) + : tag_{tag} + , router_{router} + , client_id_{client_id} + { + } + + GatewayImpl(const GatewayImpl&) = delete; + GatewayImpl(GatewayImpl&&) noexcept = delete; + GatewayImpl& operator=(const GatewayImpl&) = delete; + GatewayImpl& operator=(GatewayImpl&&) noexcept = delete; + + ~GatewayImpl() + { + setEventHandler(nullptr); + } + + void send(const detail::MsgTypeId type_id, const pipe::Payload payload) override + { + Route_1_0 route{&router_.memory_}; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = tag_; + channel_msg.type_id = type_id; + + tryPerformOnSerialized(route, [this, payload](const auto prefix) { + // + std::array fragments{prefix, payload}; + return router_.server_pipe_->sendMessage(client_id_, fragments); + }); + } + + void event(const Event::Var& event) override + { + if (event_handler_) + { + event_handler_(event); + } + } + + void setEventHandler(EventHandler event_handler) override + { + if (event_handler) + { + event_handler_ = std::move(event_handler); + router_.tag_to_gateway_[tag_] = shared_from_this(); + } + else + { + router_.tag_to_gateway_.erase(tag_); + } + } + + private: + const Tag tag_; + ServerRouterImpl& router_; + const ClientId client_id_; + EventHandler event_handler_; + + }; // GatewayImpl + + static int handlePipeEvent(const pipe::ServerPipe::Event::Connected) + { + // TODO: Implement! + return 0; + } + + int handlePipeEvent(const pipe::ServerPipe::Event::Message& msg) + { + Route_1_0 route_msg{&memory_}; + const auto result_size = tryDeserializePayload(msg.payload, route_msg); + if (!result_size.has_value()) + { + return EINVAL; + } + + const auto remaining_payload = msg.payload.subspan(result_size.value()); + + cetl::visit(cetl::make_overloaded( + // + [this](const uavcan::primitive::Empty_1_0&) {}, + [this, &msg](const RouteConnect_1_0& route_conn) { + // + handleRouteConnect(msg.client_id, route_conn); + }, + [this, &msg, remaining_payload](const RouteChannelMsg_1_0& route_channel) { + // + handleRouteChannelMsg(msg.client_id, route_channel, remaining_payload); + }), + route_msg.union_value); + + return 0; + + return 0; + } + + static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected) + { + // TODO: Implement! disconnected for all gateways which belong to the corresponding client id + return 0; + } + + void handleRouteConnect(const ClientId client_id, const RouteConnect_1_0&) const + { + // TODO: log client route connection + + Route_1_0 route{&memory_}; + auto& route_conn = route.set_connect(); + route_conn.version.major = VERSION_MAJOR; + route_conn.version.minor = VERSION_MINOR; + + tryPerformOnSerialized(route, [this, client_id](const auto payload) { + // + std::array payloads{payload}; + return server_pipe_->sendMessage(client_id, payloads); + }); + } + + void handleRouteChannelMsg(const ClientId client_id, + const RouteChannelMsg_1_0& route_channel_msg, + pipe::Payload payload) + { + const auto tag_it = tag_to_gateway_.find(route_channel_msg.tag); + if (tag_it != tag_to_gateway_.end()) + { + auto gateway = tag_it->second.lock(); + if (gateway) + { + gateway->event(detail::Gateway::Event::Message{payload}); + } + return; + } + + const auto ch_factory_it = type_id_to_channel_factory_.find(route_channel_msg.type_id); + if (ch_factory_it != type_id_to_channel_factory_.end()) + { + auto gateway = GatewayImpl::create(route_channel_msg.tag, *this, client_id); + + tag_to_gateway_[route_channel_msg.tag] = gateway; + ch_factory_it->second(gateway); + } + + // TODO: log unsolicited message + } + + cetl::pmr::memory_resource& memory_; + pipe::ServerPipe::Ptr server_pipe_; + std::unordered_map type_id_to_channel_factory_; + std::unordered_map tag_to_gateway_; }; // ClientRouterImpl } // namespace -ServerRouter::Ptr ServerRouter::make(pipe::ServerPipe::Ptr server_pipe) +ServerRouter::Ptr ServerRouter::make(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe) { - return std::make_unique(std::move(server_pipe)); + return std::make_unique(memory, std::move(server_pipe)); } } // namespace ipc diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index a7f4218..b159cf6 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -6,8 +6,13 @@ #ifndef OCVSMD_COMMON_IPC_SERVER_ROUTER_HPP_INCLUDED #define OCVSMD_COMMON_IPC_SERVER_ROUTER_HPP_INCLUDED +#include "channel.hpp" +#include "gateway.hpp" #include "pipe/server_pipe.hpp" +#include + +#include #include namespace ocvsmd @@ -22,7 +27,7 @@ class ServerRouter public: using Ptr = std::unique_ptr; - static Ptr make(pipe::ServerPipe::Ptr server_pipe); + static Ptr make(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe); ServerRouter(const ServerRouter&) = delete; ServerRouter(ServerRouter&&) noexcept = delete; @@ -31,9 +36,32 @@ class ServerRouter virtual ~ServerRouter() = default; + virtual void start() = 0; + virtual cetl::pmr::memory_resource& memory() = 0; + + template + using NewChannelHandler = std::function&& new_channel)>; + + template + void registerChannel(NewChannelHandler new_channel_handler) + { + registerChannelFactory( // + AnyChannel::getTypeId(), + [this, new_ch_handler = std::move(new_channel_handler)](detail::Gateway::Ptr gateway) { + // + new_ch_handler(Channel{memory(), gateway, nullptr}); + }); + } + protected: + using TypeErasedChannelFactory = std::function; + ServerRouter() = default; + virtual void registerChannelFactory( // + const detail::MsgTypeId input_type_id, + TypeErasedChannelFactory channel_factory) = 0; + }; // ServerRouter } // namespace ipc diff --git a/test/common/ipc/pipe/server_pipe_mock.hpp b/test/common/ipc/pipe/server_pipe_mock.hpp index 31b2f43..2a4a28b 100644 --- a/test/common/ipc/pipe/server_pipe_mock.hpp +++ b/test/common/ipc/pipe/server_pipe_mock.hpp @@ -33,6 +33,7 @@ class ServerPipeMock : public ServerPipe int start(EventHandler event_handler) override { + reference().event_handler_ = event_handler; return reference().start(event_handler); } int sendMessage(const ClientId client_id, const Payloads payloads) override @@ -46,6 +47,12 @@ class ServerPipeMock : public ServerPipe MOCK_METHOD(int, start, (EventHandler event_handler), (override)); MOCK_METHOD(int, sendMessage, (const ClientId client_id, const Payloads payloads), (override)); + // MARK: Data members: + + // NOLINTBEGIN + EventHandler event_handler_; + // NOLINTEND + }; // ServerPipeMock } // namespace pipe diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index f7c3d31..9f62836 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -11,8 +11,8 @@ #include "pipe/client_pipe_mock.hpp" #include "tracking_memory_resource.hpp" -#include "ocvsmd/common/ipc/Route_1_0.hpp" #include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/Route_1_0.hpp" #include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" #include @@ -83,6 +83,7 @@ class TestClientRouter : public testing::Test Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag; + channel_msg.type_id = AnyChannel::getTypeId(); tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -110,8 +111,9 @@ TEST_F(TestClientRouter, make) { StrictMock client_pipe_mock; - auto client_pipe = std::make_unique(client_pipe_mock); - const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + const auto client_router = ClientRouter::make( // + mr_, + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_THAT(client_pipe_mock.event_handler_, IsFalse()); @@ -122,8 +124,9 @@ TEST_F(TestClientRouter, start) { StrictMock client_pipe_mock; - auto client_pipe = std::make_unique(client_pipe_mock); - const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + const auto client_router = ClientRouter::make( // + mr_, + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_THAT(client_pipe_mock.event_handler_, IsFalse()); @@ -136,13 +139,14 @@ TEST_F(TestClientRouter, start) TEST_F(TestClientRouter, makeChannel) { - using Msg = ocvsmd::common::ipc::Route_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; StrictMock client_pipe_mock; EXPECT_CALL(client_pipe_mock, deinit()).Times(1); - auto client_pipe = std::make_unique(client_pipe_mock); - const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + const auto client_router = ClientRouter::make( // + mr_, + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); @@ -159,8 +163,9 @@ TEST_F(TestClientRouter, makeChannel_send) StrictMock client_pipe_mock; EXPECT_CALL(client_pipe_mock, deinit()).Times(1); - auto client_pipe = std::make_unique(client_pipe_mock); - const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + const auto client_router = ClientRouter::make( // + mr_, + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); @@ -188,8 +193,9 @@ TEST_F(TestClientRouter, makeChannel_receive_events) StrictMock client_pipe_mock; EXPECT_CALL(client_pipe_mock, deinit()).Times(1); - auto client_pipe = std::make_unique(client_pipe_mock); - const auto client_router = ClientRouter::make(mr_, std::move(client_pipe)); + const auto client_router = ClientRouter::make( // + mr_, + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 368998c..7705722 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -5,27 +5,83 @@ #include "ipc/server_router.hpp" +#include "ipc/channel.hpp" +#include "ipc/pipe/server_pipe.hpp" #include "pipe/server_pipe_mock.hpp" +#include "tracking_memory_resource.hpp" + +#include "ocvsmd/common/ipc/Route_1_0.hpp" +#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" + +#include #include #include +#include +#include #include #include +#include namespace { using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in the unit tests. +using testing::_; +using testing::IsTrue; +using testing::IsEmpty; +using testing::IsFalse; using testing::NotNull; using testing::StrictMock; +using testing::VariantWith; +using testing::MockFunction; + +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) class TestServerRouter : public testing::Test { protected: - void SetUp() override {} - void TearDown() override {} + void SetUp() override + { + cetl::pmr::set_default_resource(&mr_); + } + + void TearDown() override + { + EXPECT_THAT(mr_.allocations, IsEmpty()); + EXPECT_THAT(mr_.total_allocated_bytes, mr_.total_deallocated_bytes); + } + + template + void withRouteChannelMsg(const std::uint64_t tag, const Message& message, Action action) + { + using ocvsmd::common::tryPerformOnSerialized; + + Route_1_0 route{&mr_}; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = tag; + channel_msg.type_id = AnyChannel::getTypeId(); + + tryPerformOnSerialized(route, [&](const auto prefix) { + // + return tryPerformOnSerialized(message, [&](const auto suffix) { + // + std::vector buffer; + std::copy(prefix.begin(), prefix.end(), std::back_inserter(buffer)); + std::copy(suffix.begin(), suffix.end(), std::back_inserter(buffer)); + action(cetl::span{buffer.data(), buffer.size()}); + return 0; + }); + }); + } + + // MARK: Data members: + + // NOLINTBEGIN + ocvsmd::TrackingMemoryResource mr_; + // NOLINTEND }; // MARK: - Tests: @@ -33,13 +89,88 @@ class TestServerRouter : public testing::Test TEST_F(TestServerRouter, make) { StrictMock server_pipe_mock; - { - auto server_pipe = std::make_unique(server_pipe_mock); - const auto server_router = ServerRouter::make(std::move(server_pipe)); - EXPECT_THAT(server_router, NotNull()); + EXPECT_CALL(server_pipe_mock, deinit()).Times(1); - EXPECT_CALL(server_pipe_mock, deinit()).Times(1); - } + const auto server_router = ServerRouter::make( // + mr_, + std::make_unique(server_pipe_mock)); + ASSERT_THAT(server_router, NotNull()); + EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); +} + +TEST_F(TestServerRouter, start) +{ + StrictMock server_pipe_mock; + EXPECT_CALL(server_pipe_mock, deinit()).Times(1); + + const auto server_router = ServerRouter::make( // + mr_, + std::make_unique(server_pipe_mock)); + ASSERT_THAT(server_router, NotNull()); + EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); + + EXPECT_CALL(server_pipe_mock, start(_)).Times(1); + server_router->start(); + EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); } +TEST_F(TestServerRouter, registerChannel) +{ + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + + StrictMock server_pipe_mock; + EXPECT_CALL(server_pipe_mock, deinit()).Times(1); + + const auto server_router = ServerRouter::make( // + mr_, + std::make_unique(server_pipe_mock)); + ASSERT_THAT(server_router, NotNull()); + EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); + + EXPECT_CALL(server_pipe_mock, start(_)).Times(1); + server_router->start(); + EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); + + server_router->registerChannel([](auto&&) {}); +} + +TEST_F(TestServerRouter, channel_send) +{ + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Channel = Channel; + + StrictMock server_pipe_mock; + EXPECT_CALL(server_pipe_mock, deinit()).Times(1); + + const auto server_router = ServerRouter::make( // + mr_, + std::make_unique(server_pipe_mock)); + ASSERT_THAT(server_router, NotNull()); + EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); + + EXPECT_CALL(server_pipe_mock, start(_)).Times(1); + server_router->start(); + EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); + + StrictMock> ch1_event_mock; + + cetl::optional maybe_channel; + server_router->registerChannel([&](auto&& ch) { + // + ch.setEventHandler(ch1_event_mock.AsStdFunction()); + maybe_channel.emplace(std::forward(ch)); + }); + EXPECT_THAT(maybe_channel.has_value(), IsFalse()); + + // Emulate that server posted `RouteChannelMsg` on 1/42 tag/client. + // + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + withRouteChannelMsg(1, Channel::Input{&mr_}, [&](const auto payload) { + // + server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); + }); +} + +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + } // namespace From 4d334137ceea9f9235fb9b4407517cacb74d90e1 Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 7 Jan 2025 16:56:27 +0200 Subject: [PATCH 026/156] deliver initial ch msg --- src/common/ipc/server_router.cpp | 2 +- src/common/ipc/server_router.hpp | 15 ++++++++++----- test/common/ipc/test_server_router.cpp | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index a543ef6..a63a0cb 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -227,7 +227,7 @@ class ServerRouterImpl final : public ServerRouter auto gateway = GatewayImpl::create(route_channel_msg.tag, *this, client_id); tag_to_gateway_[route_channel_msg.tag] = gateway; - ch_factory_it->second(gateway); + ch_factory_it->second(gateway, payload); } // TODO: log unsolicited message diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index b159cf6..fc4601c 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -8,6 +8,7 @@ #include "channel.hpp" #include "gateway.hpp" +#include "pipe/pipe_types.hpp" #include "pipe/server_pipe.hpp" #include @@ -40,21 +41,25 @@ class ServerRouter virtual cetl::pmr::memory_resource& memory() = 0; template - using NewChannelHandler = std::function&& new_channel)>; + using NewChannelHandler = std::function&& new_channel, const Input& input)>; template void registerChannel(NewChannelHandler new_channel_handler) { registerChannelFactory( // AnyChannel::getTypeId(), - [this, new_ch_handler = std::move(new_channel_handler)](detail::Gateway::Ptr gateway) { - // - new_ch_handler(Channel{memory(), gateway, nullptr}); + [this, new_ch_handler = std::move(new_channel_handler)](detail::Gateway::Ptr gateway, + const pipe::Payload payload) { + Input input{&memory()}; + if (tryDeserializePayload(payload, input)) + { + new_ch_handler(Channel{memory(), gateway, nullptr}, input); + } }); } protected: - using TypeErasedChannelFactory = std::function; + using TypeErasedChannelFactory = std::function; ServerRouter() = default; diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 7705722..0fada25 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -131,7 +131,7 @@ TEST_F(TestServerRouter, registerChannel) server_router->start(); EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); - server_router->registerChannel([](auto&&) {}); + server_router->registerChannel([](auto&&, const auto&) {}); } TEST_F(TestServerRouter, channel_send) @@ -155,20 +155,29 @@ TEST_F(TestServerRouter, channel_send) StrictMock> ch1_event_mock; cetl::optional maybe_channel; - server_router->registerChannel([&](auto&& ch) { + server_router->registerChannel([&](auto&& ch, const auto& input) { // ch.setEventHandler(ch1_event_mock.AsStdFunction()); maybe_channel.emplace(std::forward(ch)); + ch1_event_mock.Call(input); }); EXPECT_THAT(maybe_channel.has_value(), IsFalse()); - // Emulate that server posted `RouteChannelMsg` on 1/42 tag/client. + // Emulate that client posted initial `RouteChannelMsg` on 1/42 tag/client pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); withRouteChannelMsg(1, Channel::Input{&mr_}, [&](const auto payload) { // server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); }); + + // Emulate that client posted one more `RouteChannelMsg` on the same 1/42 tag/client pair. + // + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + withRouteChannelMsg(2, Channel::Input{&mr_}, [&](const auto payload) { + // + server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); + }); } // NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) From 9a756267a1cfd410dc9522417eb61a981c69350d Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 7 Jan 2025 19:07:36 +0200 Subject: [PATCH 027/156] introduce `Endpoint` entity --- CMakePresets.json | 3 +- src/common/ipc/channel.hpp | 21 +--- src/common/ipc/client_router.cpp | 112 +++++++++++------ src/common/ipc/client_router.hpp | 4 +- src/common/ipc/pipe/unix_socket_server.cpp | 6 +- src/common/ipc/server_router.cpp | 134 ++++++++++++++------- src/common/ipc/server_router.hpp | 2 +- 7 files changed, 181 insertions(+), 101 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 86bd397..f7d7bf3 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -22,7 +22,8 @@ "CMAKE_CROSS_CONFIGS": "all", "CMAKE_DEFAULT_BUILD_TYPE": "Release", "CMAKE_DEFAULT_CONFIGS": "Release", - "CMAKE_PREFIX_PATH": "${sourceDir}/submodules/nunavut" + "CMAKE_PREFIX_PATH": "${sourceDir}/submodules/nunavut", + "CMAKE_CXX_FLAGS": "-DCETL_ENABLE_DEBUG_ASSERT=1" } }, { diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 09d3cf0..e0d21a3 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -71,22 +71,12 @@ class Channel final : public AnyChannel setupEventHandler(); } + ~Channel() = default; + Channel(const Channel&) = delete; Channel& operator=(const Channel&) = delete; Channel& operator=(Channel&& other) noexcept = delete; - ~Channel() - { - if (gateway_) - { - gateway_->setEventHandler(nullptr); - } - if (event_handler_) - { - event_handler_ = nullptr; - } - } - using SendFailure = nunavut::support::Error; using SendResult = cetl::optional; @@ -107,22 +97,19 @@ class Channel final : public AnyChannel void setEventHandler(EventHandler event_handler) { event_handler_ = std::move(event_handler); + setupEventHandler(); } private: friend class ClientRouter; friend class ServerRouter; - Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, EventHandler event_handler) + Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway) : memory_{memory} , gateway_{std::move(gateway)} - , event_handler_{std::move(event_handler)} , output_type_id_{getTypeId()} { CETL_DEBUG_ASSERT(gateway_, ""); - CETL_DEBUG_ASSERT(event_handler_, ""); - - setupEventHandler(); } void setupEventHandler() diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 1201ac1..2c93c34 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -42,7 +43,7 @@ class ClientRouterImpl final : public ClientRouter ClientRouterImpl(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) : memory_{memory} , client_pipe_{std::move(client_pipe)} - , next_tag_{0} + , next_unique_tag_{0} { CETL_DEBUG_ASSERT(client_pipe_, ""); } @@ -69,14 +70,45 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD detail::Gateway::Ptr makeGateway() override { - const Tag new_tag = ++next_tag_; - auto gateway = GatewayImpl::create(new_tag, *this); - tag_to_gateway_[new_tag] = gateway; - return gateway; + const Endpoint endpoint{++next_unique_tag_}; + return GatewayImpl::create(*this, endpoint); } private: - using Tag = std::uint64_t; + struct Endpoint final + { + using Tag = std::uint64_t; + + explicit Endpoint(const Tag tag) noexcept + : tag_{tag} + { + } + + Tag getTag() const noexcept + { + return tag_; + } + + // Hasher + + bool operator==(const Endpoint& other) const noexcept + { + return tag_ == other.tag_; + } + + struct Hasher final + { + std::size_t operator()(const Endpoint& endpoint) const noexcept + { + return std::hash{}(endpoint.tag_); + } + + }; // Hasher + + private: + const Tag tag_; + + }; // Endpoint class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway { @@ -86,14 +118,14 @@ class ClientRouterImpl final : public ClientRouter }; public: - static std::shared_ptr create(const Tag tag, ClientRouterImpl& router) + static std::shared_ptr create(ClientRouterImpl& router, const Endpoint& endpoint) { - return std::make_shared(Private(), tag, router); + return std::make_shared(Private(), router, endpoint); } - GatewayImpl(Private, const Tag tag, ClientRouterImpl& router) - : tag_{tag} - , router_{router} + GatewayImpl(Private, ClientRouterImpl& router, const Endpoint& endpoint) + : router_{router} + , endpoint_{endpoint} { } @@ -104,14 +136,14 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { - setEventHandler(nullptr); + router_.unregisterGateway(endpoint_); } void send(const detail::MsgTypeId type_id, const pipe::Payload payload) override { Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = tag_; + channel_msg.tag = endpoint_.getTag(); channel_msg.type_id = type_id; tryPerformOnSerialized(route, [this, payload](const auto prefix) { @@ -131,24 +163,29 @@ class ClientRouterImpl final : public ClientRouter void setEventHandler(EventHandler event_handler) override { - if (event_handler) - { - event_handler_ = std::move(event_handler); - router_.tag_to_gateway_[tag_] = shared_from_this(); - } - else - { - router_.tag_to_gateway_.erase(tag_); - } + router_.registerGateway(endpoint_, shared_from_this()); + event_handler_ = std::move(event_handler); } private: - const Tag tag_; ClientRouterImpl& router_; + const Endpoint endpoint_; EventHandler event_handler_; }; // GatewayImpl + using EndpointToWeakGateway = std::unordered_map; + + void registerGateway(const Endpoint& endpoint, detail::Gateway::WeakPtr gateway) + { + endpoint_to_gateway_[endpoint] = std::move(gateway); + } + + void unregisterGateway(const Endpoint& endpoint) + { + endpoint_to_gateway_.erase(endpoint); + } + template void forEachGateway(Action action) const { @@ -156,10 +193,10 @@ class ClientRouterImpl final : public ClientRouter // collect strong pointers to gateways into a local collection. // std::vector gateways; - gateways.reserve(tag_to_gateway_.size()); - for (const auto& pair : tag_to_gateway_) + gateways.reserve(endpoint_to_gateway_.size()); + for (const auto& ep_to_gw : endpoint_to_gateway_) { - const auto gateway = pair.second.lock(); + const auto gateway = ep_to_gw.second.lock(); if (gateway) { gateways.push_back(gateway); @@ -197,7 +234,8 @@ class ClientRouterImpl final : public ClientRouter return EINVAL; } - const auto remaining_payload = msg.payload.subspan(result_size.value()); + // Cut routing stuff from the payload - remaining is the actual message payload. + const auto msg_payload = msg.payload.subspan(result_size.value()); cetl::visit(cetl::make_overloaded( // @@ -206,9 +244,9 @@ class ClientRouterImpl final : public ClientRouter // handleRouteConnect(route_conn); }, - [this, remaining_payload](const RouteChannelMsg_1_0& route_channel) { + [this, msg_payload](const RouteChannelMsg_1_0& route_channel) { // - handleRouteChannelMsg(route_channel, remaining_payload); + handleRouteChannelMsg(route_channel, msg_payload); }), route_msg.union_value); @@ -238,10 +276,12 @@ class ClientRouterImpl final : public ClientRouter void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_channel_msg, pipe::Payload payload) { - const auto tag_it = tag_to_gateway_.find(route_channel_msg.tag); - if (tag_it != tag_to_gateway_.end()) + const Endpoint endpoint{route_channel_msg.tag}; + + const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); + if (ep_to_gw != endpoint_to_gateway_.end()) { - const auto gateway = tag_it->second.lock(); + const auto gateway = ep_to_gw->second.lock(); if (gateway) { gateway->event(detail::Gateway::Event::Message{payload}); @@ -251,10 +291,10 @@ class ClientRouterImpl final : public ClientRouter // TODO: log unsolicited message } - cetl::pmr::memory_resource& memory_; - pipe::ClientPipe::Ptr client_pipe_; - Tag next_tag_; - std::unordered_map tag_to_gateway_; + cetl::pmr::memory_resource& memory_; + pipe::ClientPipe::Ptr client_pipe_; + Endpoint::Tag next_unique_tag_; + EndpointToWeakGateway endpoint_to_gateway_; }; // ClientRouterImpl diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index 3e814eb..ee8f972 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -42,7 +42,9 @@ class ClientRouter template CETL_NODISCARD Channel makeChannel(AnyChannel::EventHandler event_handler) { - return Channel{memory(), makeGateway(), event_handler}; + auto channel = Channel{memory(), makeGateway()}; + channel.setEventHandler(event_handler); + return channel; } protected: diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index 702147f..06e7cbb 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -43,7 +43,7 @@ class ClientContextImpl final : public detail::ClientContext : id_{id} , fd_{fd} { - CETL_DEBUG_ASSERT(client_fd != -1, ""); + CETL_DEBUG_ASSERT(fd_ != -1, ""); // NOLINTNEXTLINE *-vararg ::syslog(LOG_NOTICE, "New client connection on fd=%d (id=%zu).", fd, id); @@ -102,7 +102,7 @@ UnixSocketServer::~UnixSocketServer() int UnixSocketServer::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(server_fd_ == -1, ""); - CETL_DEBUG_ASSERT(client_event_handler, ""); + CETL_DEBUG_ASSERT(event_handler, ""); event_handler_ = std::move(event_handler); @@ -174,7 +174,7 @@ void UnixSocketServer::handle_accept() } CETL_DEBUG_ASSERT(client_fd != -1, ""); - CETL_DEBUG_ASSERT(client_contexts_.find(client_fd) == client_contexts_.end(), ""); + CETL_DEBUG_ASSERT(client_fd_to_context_.find(client_fd) == client_fd_to_context_.end(), ""); const ClientId new_client_id = ++unique_client_id_counter_; auto client_context = std::make_unique(new_client_id, client_fd); diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index a63a0cb..90f8122 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -73,8 +74,50 @@ class ServerRouterImpl final : public ServerRouter } private: - using Tag = std::uint64_t; - using ClientId = pipe::ServerPipe::ClientId; + struct Endpoint final + { + using Tag = std::uint64_t; + using ClientId = pipe::ServerPipe::ClientId; + + Endpoint(const Tag tag, ClientId client_id) noexcept + : tag_{tag} + , client_id_{client_id} + { + } + + Tag getTag() const noexcept + { + return tag_; + } + + ClientId getClientId() const noexcept + { + return client_id_; + } + + // Hasher + + bool operator==(const Endpoint& other) const noexcept + { + return tag_ == other.tag_ && client_id_ == other.client_id_; + } + + struct Hasher final + { + std::size_t operator()(const Endpoint& endpoint) const noexcept + { + const std::size_t h1 = std::hash{}(endpoint.tag_); + const std::size_t h2 = std::hash{}(endpoint.client_id_); + return h1 ^ (h2 << 1ULL); + } + + }; // Hasher + + private: + const Tag tag_; + const ClientId client_id_; + + }; // Endpoint class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway { @@ -84,15 +127,14 @@ class ServerRouterImpl final : public ServerRouter }; public: - static std::shared_ptr create(const Tag tag, ServerRouterImpl& router, const ClientId client_id) + static std::shared_ptr create(ServerRouterImpl& router, const Endpoint& endpoint) { - return std::make_shared(Private(), tag, router, client_id); + return std::make_shared(Private(), router, endpoint); } - GatewayImpl(Private, const Tag tag, ServerRouterImpl& router, const ClientId client_id) - : tag_{tag} - , router_{router} - , client_id_{client_id} + GatewayImpl(Private, ServerRouterImpl& router, const Endpoint& endpoint) + : router_{router} + , endpoint_{endpoint} { } @@ -103,20 +145,20 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { - setEventHandler(nullptr); + router_.unregisterGateway(endpoint_); } void send(const detail::MsgTypeId type_id, const pipe::Payload payload) override { Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = tag_; + channel_msg.tag = endpoint_.getTag(); channel_msg.type_id = type_id; tryPerformOnSerialized(route, [this, payload](const auto prefix) { // std::array fragments{prefix, payload}; - return router_.server_pipe_->sendMessage(client_id_, fragments); + return router_.server_pipe_->sendMessage(endpoint_.getClientId(), fragments); }); } @@ -130,25 +172,30 @@ class ServerRouterImpl final : public ServerRouter void setEventHandler(EventHandler event_handler) override { - if (event_handler) - { - event_handler_ = std::move(event_handler); - router_.tag_to_gateway_[tag_] = shared_from_this(); - } - else - { - router_.tag_to_gateway_.erase(tag_); - } + router_.registerGateway(endpoint_, shared_from_this()); + event_handler_ = std::move(event_handler); } private: - const Tag tag_; ServerRouterImpl& router_; - const ClientId client_id_; + const Endpoint endpoint_; EventHandler event_handler_; }; // GatewayImpl + using TypeIdToChannelFactory = std::unordered_map; + using EndpointToWeakGateway = std::unordered_map; + + void registerGateway(const Endpoint& endpoint, detail::Gateway::WeakPtr gateway) + { + endpoint_to_gateway_[endpoint] = std::move(gateway); + } + + void unregisterGateway(const Endpoint& endpoint) + { + endpoint_to_gateway_.erase(endpoint); + } + static int handlePipeEvent(const pipe::ServerPipe::Event::Connected) { // TODO: Implement! @@ -164,7 +211,8 @@ class ServerRouterImpl final : public ServerRouter return EINVAL; } - const auto remaining_payload = msg.payload.subspan(result_size.value()); + // Cut routing stuff from the payload - remaining is the actual message payload. + const auto msg_payload = msg.payload.subspan(result_size.value()); cetl::visit(cetl::make_overloaded( // @@ -173,9 +221,9 @@ class ServerRouterImpl final : public ServerRouter // handleRouteConnect(msg.client_id, route_conn); }, - [this, &msg, remaining_payload](const RouteChannelMsg_1_0& route_channel) { + [this, &msg, msg_payload](const RouteChannelMsg_1_0& route_channel) { // - handleRouteChannelMsg(msg.client_id, route_channel, remaining_payload); + handleRouteChannelMsg(msg.client_id, route_channel, msg_payload); }), route_msg.union_value); @@ -190,7 +238,7 @@ class ServerRouterImpl final : public ServerRouter return 0; } - void handleRouteConnect(const ClientId client_id, const RouteConnect_1_0&) const + void handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_1_0&) const { // TODO: log client route connection @@ -206,37 +254,39 @@ class ServerRouterImpl final : public ServerRouter }); } - void handleRouteChannelMsg(const ClientId client_id, - const RouteChannelMsg_1_0& route_channel_msg, - pipe::Payload payload) + void handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, + const RouteChannelMsg_1_0& route_channel_msg, + pipe::Payload msg_payload) { - const auto tag_it = tag_to_gateway_.find(route_channel_msg.tag); - if (tag_it != tag_to_gateway_.end()) + const Endpoint endpoint{route_channel_msg.tag, client_id}; + + const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); + if (ep_to_gw != endpoint_to_gateway_.end()) { - auto gateway = tag_it->second.lock(); + auto gateway = ep_to_gw->second.lock(); if (gateway) { - gateway->event(detail::Gateway::Event::Message{payload}); + gateway->event(detail::Gateway::Event::Message{msg_payload}); } return; } - const auto ch_factory_it = type_id_to_channel_factory_.find(route_channel_msg.type_id); - if (ch_factory_it != type_id_to_channel_factory_.end()) + const auto ti_to_cf = type_id_to_channel_factory_.find(route_channel_msg.type_id); + if (ti_to_cf != type_id_to_channel_factory_.end()) { - auto gateway = GatewayImpl::create(route_channel_msg.tag, *this, client_id); + auto gateway = GatewayImpl::create(*this, endpoint); - tag_to_gateway_[route_channel_msg.tag] = gateway; - ch_factory_it->second(gateway, payload); + endpoint_to_gateway_[endpoint] = gateway; + ti_to_cf->second(gateway, msg_payload); } // TODO: log unsolicited message } - cetl::pmr::memory_resource& memory_; - pipe::ServerPipe::Ptr server_pipe_; - std::unordered_map type_id_to_channel_factory_; - std::unordered_map tag_to_gateway_; + cetl::pmr::memory_resource& memory_; + pipe::ServerPipe::Ptr server_pipe_; + EndpointToWeakGateway endpoint_to_gateway_; + TypeIdToChannelFactory type_id_to_channel_factory_; }; // ClientRouterImpl diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index fc4601c..8cee191 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -53,7 +53,7 @@ class ServerRouter Input input{&memory()}; if (tryDeserializePayload(payload, input)) { - new_ch_handler(Channel{memory(), gateway, nullptr}, input); + new_ch_handler(Channel{memory(), gateway}, input); } }); } From c402dc729e5f16c25f9bc12cf8b4b6e359b09737 Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 7 Jan 2025 19:30:47 +0200 Subject: [PATCH 028/156] simplify channel api --- src/common/ipc/channel.hpp | 9 +++++---- src/common/ipc/client_router.cpp | 6 +++--- src/common/ipc/client_router.hpp | 6 ++---- test/common/ipc/test_client_router.cpp | 13 +++++++------ 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index e0d21a3..969b11a 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -68,7 +68,7 @@ class Channel final : public AnyChannel , event_handler_{std::move(other.event_handler_)} , output_type_id_{other.output_type_id_} { - setupEventHandler(); + setupEventHandling(); } ~Channel() = default; @@ -94,10 +94,11 @@ class Channel final : public AnyChannel }); } - void setEventHandler(EventHandler event_handler) + Channel& setEventHandler(EventHandler event_handler) { event_handler_ = std::move(event_handler); - setupEventHandler(); + setupEventHandling(); + return *this; } private: @@ -112,7 +113,7 @@ class Channel final : public AnyChannel CETL_DEBUG_ASSERT(gateway_, ""); } - void setupEventHandler() + void setupEventHandling() { gateway_->setEventHandler([this](const detail::Gateway::Event::Var& gateway_event_var) { // diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 2c93c34..ff9582e 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -43,7 +43,7 @@ class ClientRouterImpl final : public ClientRouter ClientRouterImpl(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) : memory_{memory} , client_pipe_{std::move(client_pipe)} - , next_unique_tag_{0} + , last_unique_tag_{0} { CETL_DEBUG_ASSERT(client_pipe_, ""); } @@ -70,7 +70,7 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD detail::Gateway::Ptr makeGateway() override { - const Endpoint endpoint{++next_unique_tag_}; + const Endpoint endpoint{++last_unique_tag_}; return GatewayImpl::create(*this, endpoint); } @@ -293,7 +293,7 @@ class ClientRouterImpl final : public ClientRouter cetl::pmr::memory_resource& memory_; pipe::ClientPipe::Ptr client_pipe_; - Endpoint::Tag next_unique_tag_; + Endpoint::Tag last_unique_tag_; EndpointToWeakGateway endpoint_to_gateway_; }; // ClientRouterImpl diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index ee8f972..38e1ec3 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -40,11 +40,9 @@ class ClientRouter virtual cetl::pmr::memory_resource& memory() = 0; template - CETL_NODISCARD Channel makeChannel(AnyChannel::EventHandler event_handler) + CETL_NODISCARD Channel makeChannel() { - auto channel = Channel{memory(), makeGateway()}; - channel.setEventHandler(event_handler); - return channel; + return Channel{memory(), makeGateway()}; } protected: diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 9f62836..b6781f2 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -152,7 +152,7 @@ TEST_F(TestClientRouter, makeChannel) EXPECT_CALL(client_pipe_mock, start(_)).Times(1); client_router->start(); - const auto channel = client_router->makeChannel([](const auto&) {}); + const auto channel = client_router->makeChannel(); (void) channel; } @@ -171,7 +171,7 @@ TEST_F(TestClientRouter, makeChannel_send) EXPECT_CALL(client_pipe_mock, start(_)).Times(1); client_router->start(); - auto channel = client_router->makeChannel([](const auto&) {}); + auto channel = client_router->makeChannel(); Msg msg{&mr_}; @@ -204,10 +204,11 @@ TEST_F(TestClientRouter, makeChannel_receive_events) StrictMock> ch1_event_mock; StrictMock> ch2_event_mock; - const auto channel1 = client_router->makeChannel(ch1_event_mock.AsStdFunction()); - (void) channel1; - const auto channel2 = client_router->makeChannel(ch2_event_mock.AsStdFunction()); - (void) channel2; + auto channel1 = client_router->makeChannel(); + channel1.setEventHandler(ch1_event_mock.AsStdFunction()); + + auto channel2 = client_router->makeChannel(); + channel2.setEventHandler(ch2_event_mock.AsStdFunction()); // Emulate that we've got pipe connected - `RouteConnect` should be sent to the server. // From 829372ecb4c3c192756d4f04a32f15495e7ca21d Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 7 Jan 2025 20:29:53 +0200 Subject: [PATCH 029/156] type id -> service id --- .../common/ipc/RouteChannelMsg.1.0.dsdl | 2 +- src/common/ipc/channel.hpp | 21 +++++++++------ src/common/ipc/client_router.cpp | 8 +++--- src/common/ipc/client_router.hpp | 5 ++-- src/common/ipc/gateway.hpp | 8 +++--- src/common/ipc/pipe/client_pipe.hpp | 1 - src/common/ipc/server_router.cpp | 27 +++++++++---------- src/common/ipc/server_router.hpp | 17 +++++++----- test/common/ipc/test_client_router.cpp | 15 ++++++----- test/common/ipc/test_server_router.cpp | 19 +++++++------ 10 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl index 1702795..5b431ad 100644 --- a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl @@ -1,5 +1,5 @@ uint64 tag -uint64 type_id +uint64 service_id # reserve twice as much as we need. @extent _offset_.max * 2 diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 969b11a..b5639b6 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -40,11 +40,16 @@ class AnyChannel template using EventHandler = std::function&)>; + /// Builds a service ID from either the service name (if not empty), or message type name. + /// template - static detail::MsgTypeId getTypeId() noexcept + static detail::ServiceId getServiceId(const cetl::string_view service_name) noexcept { - const cetl::string_view type_name{Message::_traits_::FullNameAndVersion()}; - const libcyphal::common::CRC64WE crc64{type_name.cbegin(), type_name.cend()}; + const cetl::string_view srv_or_msg_name = !service_name.empty() // + ? service_name + : Message::_traits_::FullNameAndVersion(); + + const libcyphal::common::CRC64WE crc64{srv_or_msg_name.cbegin(), srv_or_msg_name.cend()}; return crc64.get(); } @@ -65,8 +70,8 @@ class Channel final : public AnyChannel Channel(Channel&& other) noexcept : memory_{other.memory_} , gateway_{std::move(other.gateway_)} + , service_id_{other.service_id_} , event_handler_{std::move(other.event_handler_)} - , output_type_id_{other.output_type_id_} { setupEventHandling(); } @@ -89,7 +94,7 @@ class Channel final : public AnyChannel output, [this](const auto payload) { // - gateway_->send(output_type_id_, payload); + gateway_->send(service_id_, payload); return cetl::nullopt; }); } @@ -105,10 +110,10 @@ class Channel final : public AnyChannel friend class ClientRouter; friend class ServerRouter; - Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway) + Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, const detail::ServiceId service_id) : memory_{memory} , gateway_{std::move(gateway)} - , output_type_id_{getTypeId()} + , service_id_{service_id} { CETL_DEBUG_ASSERT(gateway_, ""); } @@ -158,8 +163,8 @@ class Channel final : public AnyChannel cetl::pmr::memory_resource& memory_; detail::Gateway::Ptr gateway_; + detail::ServiceId service_id_; EventHandler event_handler_; - detail::MsgTypeId output_type_id_; }; // Channel diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index ff9582e..6db6b96 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -139,12 +139,12 @@ class ClientRouterImpl final : public ClientRouter router_.unregisterGateway(endpoint_); } - void send(const detail::MsgTypeId type_id, const pipe::Payload payload) override + void send(const detail::ServiceId service_id, const pipe::Payload payload) override { Route_1_0 route{&router_.memory_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.getTag(); - channel_msg.type_id = type_id; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = endpoint_.getTag(); + channel_msg.service_id = service_id; tryPerformOnSerialized(route, [this, payload](const auto prefix) { // diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index 38e1ec3..87e6aae 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -40,9 +40,10 @@ class ClientRouter virtual cetl::pmr::memory_resource& memory() = 0; template - CETL_NODISCARD Channel makeChannel() + CETL_NODISCARD Channel makeChannel(cetl::string_view service_name = "") { - return Channel{memory(), makeGateway()}; + const auto service_id = AnyChannel::getServiceId(service_name); + return Channel{memory(), makeGateway(), service_id}; } protected: diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index 6ddb5bd..c406577 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -24,7 +24,7 @@ namespace ipc namespace detail { -using MsgTypeId = std::uint64_t; +using ServiceId = std::uint64_t; class Gateway { @@ -55,9 +55,9 @@ class Gateway Gateway& operator=(const Gateway&) = delete; Gateway& operator=(Gateway&&) noexcept = delete; - virtual void send(const MsgTypeId type_id, const pipe::Payload payload) = 0; - virtual void event(const Event::Var& event) = 0; - virtual void setEventHandler(EventHandler event_handler) = 0; + virtual void send(const ServiceId service_id, const pipe::Payload payload) = 0; + virtual void event(const Event::Var& event) = 0; + virtual void setEventHandler(EventHandler event_handler) = 0; protected: Gateway() = default; diff --git a/src/common/ipc/pipe/client_pipe.hpp b/src/common/ipc/pipe/client_pipe.hpp index 51ffef6..2bc503e 100644 --- a/src/common/ipc/pipe/client_pipe.hpp +++ b/src/common/ipc/pipe/client_pipe.hpp @@ -11,7 +11,6 @@ #include #include -#include #include #include diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 90f8122..cc8d4df 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -66,11 +66,10 @@ class ServerRouterImpl final : public ServerRouter }); } - void registerChannelFactory( // - const detail::MsgTypeId input_type_id, - TypeErasedChannelFactory channel_factory) override + void registerChannelFactory(const detail::ServiceId service_id, // + TypeErasedChannelFactory channel_factory) override { - type_id_to_channel_factory_[input_type_id] = std::move(channel_factory); + service_id_to_channel_factory_[service_id] = std::move(channel_factory); } private: @@ -148,12 +147,12 @@ class ServerRouterImpl final : public ServerRouter router_.unregisterGateway(endpoint_); } - void send(const detail::MsgTypeId type_id, const pipe::Payload payload) override + void send(const detail::ServiceId service_id, const pipe::Payload payload) override { Route_1_0 route{&router_.memory_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.getTag(); - channel_msg.type_id = type_id; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = endpoint_.getTag(); + channel_msg.service_id = service_id; tryPerformOnSerialized(route, [this, payload](const auto prefix) { // @@ -183,8 +182,8 @@ class ServerRouterImpl final : public ServerRouter }; // GatewayImpl - using TypeIdToChannelFactory = std::unordered_map; - using EndpointToWeakGateway = std::unordered_map; + using ServiceIdToChannelFactory = std::unordered_map; + using EndpointToWeakGateway = std::unordered_map; void registerGateway(const Endpoint& endpoint, detail::Gateway::WeakPtr gateway) { @@ -271,13 +270,13 @@ class ServerRouterImpl final : public ServerRouter return; } - const auto ti_to_cf = type_id_to_channel_factory_.find(route_channel_msg.type_id); - if (ti_to_cf != type_id_to_channel_factory_.end()) + const auto si_to_cf = service_id_to_channel_factory_.find(route_channel_msg.service_id); + if (si_to_cf != service_id_to_channel_factory_.end()) { auto gateway = GatewayImpl::create(*this, endpoint); endpoint_to_gateway_[endpoint] = gateway; - ti_to_cf->second(gateway, msg_payload); + si_to_cf->second(gateway, msg_payload); } // TODO: log unsolicited message @@ -286,7 +285,7 @@ class ServerRouterImpl final : public ServerRouter cetl::pmr::memory_resource& memory_; pipe::ServerPipe::Ptr server_pipe_; EndpointToWeakGateway endpoint_to_gateway_; - TypeIdToChannelFactory type_id_to_channel_factory_; + ServiceIdToChannelFactory service_id_to_channel_factory_; }; // ClientRouterImpl diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index 8cee191..5bef6b3 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -44,16 +44,20 @@ class ServerRouter using NewChannelHandler = std::function&& new_channel, const Input& input)>; template - void registerChannel(NewChannelHandler new_channel_handler) + void registerChannel(const cetl::string_view service_name, NewChannelHandler handler) { + CETL_DEBUG_ASSERT(handler, ""); + + const auto service_id = AnyChannel::getServiceId(service_name); + registerChannelFactory( // - AnyChannel::getTypeId(), - [this, new_ch_handler = std::move(new_channel_handler)](detail::Gateway::Ptr gateway, + service_id, + [this, service_id, new_ch_handler = std::move(handler)](detail::Gateway::Ptr gateway, const pipe::Payload payload) { Input input{&memory()}; if (tryDeserializePayload(payload, input)) { - new_ch_handler(Channel{memory(), gateway}, input); + new_ch_handler(Channel{memory(), gateway, service_id}, input); } }); } @@ -63,9 +67,8 @@ class ServerRouter ServerRouter() = default; - virtual void registerChannelFactory( // - const detail::MsgTypeId input_type_id, - TypeErasedChannelFactory channel_factory) = 0; + virtual void registerChannelFactory(const detail::ServiceId service_id, + TypeErasedChannelFactory channel_factory) = 0; }; // ServerRouter diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index b6781f2..7da64d8 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -76,14 +76,17 @@ class TestClientRouter : public testing::Test } template - void withRouteChannelMsg(const std::uint64_t tag, const Message& message, Action action) + void withRouteChannelMsg(const cetl::string_view service_name, + const std::uint64_t tag, + const Message& message, + Action action) { using ocvsmd::common::tryPerformOnSerialized; Route_1_0 route{&mr_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = tag; - channel_msg.type_id = AnyChannel::getTypeId(); + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = tag; + channel_msg.service_id = AnyChannel::getServiceId(service_name); tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -227,7 +230,7 @@ TEST_F(TestClientRouter, makeChannel_receive_events) // Emulate that server posted `RouteChannelMsg` on tag #1. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg(1, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 1, Channel::Input{&mr_}, [&](const auto payload) { // client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); }); @@ -235,7 +238,7 @@ TEST_F(TestClientRouter, makeChannel_receive_events) // Emulate that server posted `RouteChannelMsg` on tag #2. // EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg(2, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 2, Channel::Input{&mr_}, [&](const auto payload) { // client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); }); diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 0fada25..cb3a0b7 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -55,14 +55,17 @@ class TestServerRouter : public testing::Test } template - void withRouteChannelMsg(const std::uint64_t tag, const Message& message, Action action) + void withRouteChannelMsg(const cetl::string_view service_name, + const std::uint64_t tag, + const Message& message, + Action action) { using ocvsmd::common::tryPerformOnSerialized; Route_1_0 route{&mr_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = tag; - channel_msg.type_id = AnyChannel::getTypeId(); + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = tag; + channel_msg.service_id = AnyChannel::getServiceId(service_name); tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -131,7 +134,7 @@ TEST_F(TestServerRouter, registerChannel) server_router->start(); EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); - server_router->registerChannel([](auto&&, const auto&) {}); + server_router->registerChannel("", [](auto&&, const auto&) {}); } TEST_F(TestServerRouter, channel_send) @@ -155,7 +158,7 @@ TEST_F(TestServerRouter, channel_send) StrictMock> ch1_event_mock; cetl::optional maybe_channel; - server_router->registerChannel([&](auto&& ch, const auto& input) { + server_router->registerChannel("", [&](auto&& ch, const auto& input) { // ch.setEventHandler(ch1_event_mock.AsStdFunction()); maybe_channel.emplace(std::forward(ch)); @@ -166,7 +169,7 @@ TEST_F(TestServerRouter, channel_send) // Emulate that client posted initial `RouteChannelMsg` on 1/42 tag/client pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg(1, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 1, Channel::Input{&mr_}, [&](const auto payload) { // server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); }); @@ -174,7 +177,7 @@ TEST_F(TestServerRouter, channel_send) // Emulate that client posted one more `RouteChannelMsg` on the same 1/42 tag/client pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg(2, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 2, Channel::Input{&mr_}, [&](const auto payload) { // server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); }); From e3feb814060a99565b151e657ba0678d14a9d8a6 Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 7 Jan 2025 22:42:59 +0200 Subject: [PATCH 030/156] client <-> daemon --- src/common/ipc/pipe/unix_socket_client.cpp | 2 +- src/daemon/engine/application.cpp | 59 ++++++++-------------- src/daemon/engine/application.hpp | 7 +-- src/sdk/daemon.cpp | 51 ++++++++++++------- 4 files changed, 57 insertions(+), 62 deletions(-) diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp index be4d11a..b734288 100644 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -53,7 +53,7 @@ UnixSocketClient::~UnixSocketClient() int UnixSocketClient::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(client_fd_ == -1, ""); - CETL_DEBUG_ASSERT(event_handler_, ""); + CETL_DEBUG_ASSERT(event_handler, ""); event_handler_ = std::move(event_handler); diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 5dcac72..8ebcc9f 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -5,18 +5,21 @@ #include "application.hpp" -#include "ipc/pipe/server_pipe.hpp" +#include "ipc/channel.hpp" +#include "ipc/pipe/unix_socket_server.hpp" +#include "ipc/server_router.hpp" + +#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" #include -#include #include #include #include #include -#include #include #include +#include #include #include #include @@ -62,40 +65,22 @@ cetl::optional Application::init() .setSoftwareVcsRevisionId(VCS_REVISION_ID) .setUniqueId(getUniqueId()); - if (const auto err = ipc_server_.start([this](const auto& event) { - // - using Event = common::ipc::pipe::ServerPipe::Event; - - // cetl::visit( // - // cetl::make_overloaded( - // [this](const Event::Connected& connected) { - // // - // // NOLINTNEXTLINE *-vararg - // ::syslog(LOG_DEBUG, "Client connected (%zu).", connected.client_id); - // (void) ipc_server_.sendMessage( // - // connected.client_id, - // {{reinterpret_cast("Status1"), 7}, 1}); // NOLINT - // (void) ipc_server_.sendMessage( // - // connected.client_id, - // {reinterpret_cast("Status2"), 7}); // NOLINT - // }, - // [this](const Event::Message& message) { - // // - // // NOLINTNEXTLINE *-vararg - // ::syslog(LOG_DEBUG, "Client msg (%zu).", message.client_id); - // (void) ipc_server_.sendMessage(message.client_id, message.payload); - // }, - // [](const Event::Disconnected& disconnected) { - // // - // // NOLINTNEXTLINE *-vararg - // ::syslog(LOG_DEBUG, "Client disconnected (%zu).", disconnected.client_id); - // }), - // event); - return 0; - })) - { - return std::string("Failed to start IPC server: ") + std::strerror(err); - } + using ServerPipe = common::ipc::pipe::UnixSocketServer; + + auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); + ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); + + using ExecCmd = common::node_command::ExecCmd_1_0; + using ExecCmdChannel = common::ipc::Channel; + using Ch = ExecCmdChannel; + ipc_router_->registerChannel("daemon", [this](auto&& ch, const auto& request) { + // + // NOLINTNEXTLINE *-vararg + ::syslog(LOG_DEBUG, "Client msg (%zu).", request.some_stuff.size()); + ch.send(request); + }); + + ipc_router_->start(); return cetl::nullopt; } diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/application.hpp index 96f826a..775fc36 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/application.hpp @@ -9,12 +9,11 @@ #include "cyphal/udp_transport_bag.hpp" #include "ocvsmd/platform/defines.hpp" -#include +#include #include #include #include -#include #include #include @@ -45,9 +44,7 @@ class Application cyphal::UdpTransportBag udp_transport_bag_{memory_, executor_}; cetl::optional presentation_; cetl::optional node_; - - common::ipc::pipe::UnixSocketServer ipc_server_{executor_, "/var/run/ocvsmd/local.sock"}; - libcyphal::IExecutor::Callback::Any ipc_server_callback_; + common::ipc::ServerRouter::Ptr ipc_router_; }; // Application diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index c7d3e20..efcc14e 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -5,9 +5,13 @@ #include +#include "ipc/channel.hpp" +#include "ipc/client_router.hpp" #include "ipc/pipe/client_pipe.hpp" #include "ipc/pipe/unix_socket_client.hpp" +#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" + #include #include #include @@ -27,42 +31,54 @@ class DaemonImpl final : public Daemon public: DaemonImpl(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) : memory_{memory} - , executor_{executor} { + using ClientPipe = common::ipc::pipe::UnixSocketClient; + + auto client_pipe = std::make_unique(executor, "/var/run/ocvsmd/local.sock"); + ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); } - bool connect() + void start() { - return 0 == ipc_client_.start([](const auto& event) { - // - using Event = common::ipc::pipe::ClientPipe::Event; + ipc_router_->start(); + using Ch = ExecCmdChannel; + auto ch = ipc_router_->makeChannel("daemon"); + ch.setEventHandler([this](const auto& event_var) { + // cetl::visit( // cetl::make_overloaded( - [](const Event::Connected&) { + [this](const Ch::Connected&) { // // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Server connected."); + ::syslog(LOG_DEBUG, "Ch connected."); + ExecCmd cmd{&memory_}; + cmd.some_stuff.push_back('A'); + cmd.some_stuff.push_back('Z'); + ipc_exec_cmd_channel_->send(cmd); }, - [](const Event::Message& message) { + [](const Ch::Input& input) { // // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", message.payload.size()); + ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", input.some_stuff.size()); }, - [](const Event::Disconnected&) { + [](const Ch::Disconnected&) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server disconnected."); }), - event); - return 0; + event_var); }); + ipc_exec_cmd_channel_.emplace(std::move(ch)); } private: - cetl::pmr::memory_resource& memory_; - libcyphal::IExecutor& executor_; - common::ipc::pipe::UnixSocketClient ipc_client_{executor_, "/var/run/ocvsmd/local.sock"}; + using ExecCmd = common::node_command::ExecCmd_1_0; + using ExecCmdChannel = common::ipc::Channel; + + cetl::pmr::memory_resource& memory_; + common::ipc::ClientRouter::Ptr ipc_router_; + cetl::optional ipc_exec_cmd_channel_; }; // DaemonImpl @@ -71,10 +87,7 @@ class DaemonImpl final : public Daemon std::unique_ptr Daemon::make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) { auto daemon = std::make_unique(memory, executor); - if (!daemon->connect()) - { - return nullptr; - } + daemon->start(); return std::move(daemon); } From c3cdbcfa39e41f7f80d8740e0c52bf1e9c1b8afc Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 8 Jan 2025 00:52:27 +0200 Subject: [PATCH 031/156] `final`-s --- src/common/ipc/channel.hpp | 14 ++++---------- src/common/ipc/client_router.cpp | 3 +++ src/common/ipc/gateway.hpp | 8 ++++---- src/common/ipc/pipe/client_pipe.hpp | 8 ++++---- src/common/ipc/pipe/server_pipe.hpp | 8 ++++---- src/common/ipc/pipe/unix_socket_base.hpp | 2 +- src/common/ipc/pipe/unix_socket_server.cpp | 8 ++++---- src/common/ipc/pipe/unix_socket_server.hpp | 5 ++--- src/common/ipc/server_router.cpp | 5 +++-- src/daemon/engine/application.cpp | 16 ++++++++++------ src/daemon/engine/application.hpp | 8 ++++++++ src/sdk/daemon.cpp | 17 +++++++++-------- 12 files changed, 56 insertions(+), 46 deletions(-) diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index b5639b6..2a7b18e 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -28,18 +28,12 @@ namespace ipc class AnyChannel { public: - struct Connected + struct Connected final {}; - struct Disconnected + struct Disconnected final {}; - template - using EventVar = cetl::variant; - - template - using EventHandler = std::function&)>; - /// Builds a service ID from either the service name (if not empty), or message type name. /// template @@ -64,8 +58,8 @@ class Channel final : public AnyChannel public: using Input = Input_; using Output = Output_; - using EventVar = EventVar; - using EventHandler = EventHandler; + using EventVar = cetl::variant; + using EventHandler = std::function; Channel(Channel&& other) noexcept : memory_{other.memory_} diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 6db6b96..05899d2 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -127,6 +128,7 @@ class ClientRouterImpl final : public ClientRouter : router_{router} , endpoint_{endpoint} { + ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag()); } GatewayImpl(const GatewayImpl&) = delete; @@ -137,6 +139,7 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { router_.unregisterGateway(endpoint_); + ::syslog(LOG_DEBUG, "~Gateway(tag=%zu).", endpoint_.getTag()); } void send(const detail::ServiceId service_id, const pipe::Payload payload) override diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index c406577..31be919 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -32,13 +32,13 @@ class Gateway using Ptr = std::shared_ptr; using WeakPtr = std::weak_ptr; - struct Event + struct Event final { - struct Connected + struct Connected final {}; - struct Disconnected + struct Disconnected final {}; - struct Message + struct Message final { pipe::Payload payload; diff --git a/src/common/ipc/pipe/client_pipe.hpp b/src/common/ipc/pipe/client_pipe.hpp index 2bc503e..4e36cc0 100644 --- a/src/common/ipc/pipe/client_pipe.hpp +++ b/src/common/ipc/pipe/client_pipe.hpp @@ -28,13 +28,13 @@ class ClientPipe public: using Ptr = std::unique_ptr; - struct Event + struct Event final { - struct Connected + struct Connected final {}; - struct Disconnected + struct Disconnected final {}; - struct Message + struct Message final { Payload payload; diff --git a/src/common/ipc/pipe/server_pipe.hpp b/src/common/ipc/pipe/server_pipe.hpp index d2ca336..790688b 100644 --- a/src/common/ipc/pipe/server_pipe.hpp +++ b/src/common/ipc/pipe/server_pipe.hpp @@ -32,17 +32,17 @@ class ServerPipe using ClientId = std::size_t; - struct Event + struct Event final { - struct Connected + struct Connected final { ClientId client_id; }; - struct Disconnected + struct Disconnected final { ClientId client_id; }; - struct Message + struct Message final { ClientId client_id; Payload payload; diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp index e487499..6cb421c 100644 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -143,7 +143,7 @@ class UnixSocketBase } private: - struct MsgHeader + struct MsgHeader final { std::uint32_t signature; std::uint32_t size; diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index 06e7cbb..cc4da6f 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -151,14 +151,14 @@ int UnixSocketServer::start(EventHandler event_handler) accept_callback_ = posix_executor_ext_->registerAwaitableCallback( // [this](const auto&) { // - handle_accept(); + handleAccept(); }, platform::IPosixExecutorExtension::Trigger::Readable{server_fd_}); return 0; } -void UnixSocketServer::handle_accept() +void UnixSocketServer::handleAccept() { CETL_DEBUG_ASSERT(server_fd_ != -1, ""); @@ -182,7 +182,7 @@ void UnixSocketServer::handle_accept() client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( [this, new_client_id, client_fd](const auto&) { // - handle_client_request(new_client_id, client_fd); + handleClientRequest(new_client_id, client_fd); }, platform::IPosixExecutorExtension::Trigger::Readable{client_fd})); @@ -192,7 +192,7 @@ void UnixSocketServer::handle_accept() event_handler_(Event::Connected{new_client_id}); } -void UnixSocketServer::handle_client_request(const ClientId client_id, const int client_fd) +void UnixSocketServer::handleClientRequest(const ClientId client_id, const int client_fd) { if (const auto err = receiveMessage(client_fd, [this, client_id](const auto payload) { // diff --git a/src/common/ipc/pipe/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp index 9f0a21d..b241c1f 100644 --- a/src/common/ipc/pipe/unix_socket_server.hpp +++ b/src/common/ipc/pipe/unix_socket_server.hpp @@ -72,9 +72,8 @@ class UnixSocketServer final : public UnixSocketBase, public ServerPipe } private: - void handle_accept(); - void handle_client_connection(const int client_fd); - void handle_client_request(const ClientId client_id, const int client_fd); + void handleAccept(); + void handleClientRequest(const ClientId client_id, const int client_fd); const std::string socket_path_; int server_fd_; diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index cc8d4df..b125bf8 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -135,6 +136,7 @@ class ServerRouterImpl final : public ServerRouter : router_{router} , endpoint_{endpoint} { + ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag()); } GatewayImpl(const GatewayImpl&) = delete; @@ -145,6 +147,7 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { router_.unregisterGateway(endpoint_); + ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); } void send(const detail::ServiceId service_id, const pipe::Payload payload) override @@ -274,8 +277,6 @@ class ServerRouterImpl final : public ServerRouter if (si_to_cf != service_id_to_channel_factory_.end()) { auto gateway = GatewayImpl::create(*this, endpoint); - - endpoint_to_gateway_[endpoint] = gateway; si_to_cf->second(gateway, msg_payload); } diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 8ebcc9f..5b8a368 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -9,8 +9,6 @@ #include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" -#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" - #include #include #include @@ -68,16 +66,22 @@ cetl::optional Application::init() using ServerPipe = common::ipc::pipe::UnixSocketServer; auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); - ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); + ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); - using ExecCmd = common::node_command::ExecCmd_1_0; - using ExecCmdChannel = common::ipc::Channel; using Ch = ExecCmdChannel; ipc_router_->registerChannel("daemon", [this](auto&& ch, const auto& request) { // // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Client msg (%zu).", request.some_stuff.size()); + ::syslog(LOG_DEBUG, "Client initial msg (%zu).", request.some_stuff.size()); ch.send(request); + ch.setEventHandler([this](const auto&) { + // + ::syslog(LOG_DEBUG, "Client nested msg"); + ExecCmd r1{&memory_}; + ipc_exec_cmd_ch_->send(r1); + ipc_exec_cmd_ch_.reset(); + }); + ipc_exec_cmd_ch_.emplace(std::move(ch)); }); ipc_router_->start(); diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/application.hpp index 775fc36..0213854 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/application.hpp @@ -9,6 +9,9 @@ #include "cyphal/udp_transport_bag.hpp" #include "ocvsmd/platform/defines.hpp" +#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" + +#include #include #include @@ -35,6 +38,11 @@ class Application void runWhile(const std::function& loop_predicate); private: + // TODO: temp stuff + using ExecCmd = common::node_command::ExecCmd_1_0; + using ExecCmdChannel = common::ipc::Channel; + cetl::optional ipc_exec_cmd_ch_; + using UniqueId = uavcan::node::GetInfo::Response_1_0::_traits_::TypeOf::unique_id; static UniqueId getUniqueId(); diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index efcc14e..4141912 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -35,16 +35,16 @@ class DaemonImpl final : public Daemon using ClientPipe = common::ipc::pipe::UnixSocketClient; auto client_pipe = std::make_unique(executor, "/var/run/ocvsmd/local.sock"); - ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); + ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); } void start() { ipc_router_->start(); - using Ch = ExecCmdChannel; - auto ch = ipc_router_->makeChannel("daemon"); - ch.setEventHandler([this](const auto& event_var) { + using Ch = ExecCmdChannel; + auto channel = ipc_router_->makeChannel("daemon"); + channel.setEventHandler([this](const auto& event_var) { // cetl::visit( // cetl::make_overloaded( @@ -55,12 +55,13 @@ class DaemonImpl final : public Daemon ExecCmd cmd{&memory_}; cmd.some_stuff.push_back('A'); cmd.some_stuff.push_back('Z'); - ipc_exec_cmd_channel_->send(cmd); + ipc_exec_cmd_ch_->send(cmd); }, - [](const Ch::Input& input) { + [this](const Ch::Input& input) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", input.some_stuff.size()); + ipc_exec_cmd_ch_->send(input); }, [](const Ch::Disconnected&) { // @@ -69,7 +70,7 @@ class DaemonImpl final : public Daemon }), event_var); }); - ipc_exec_cmd_channel_.emplace(std::move(ch)); + ipc_exec_cmd_ch_.emplace(std::move(channel)); } private: @@ -78,7 +79,7 @@ class DaemonImpl final : public Daemon cetl::pmr::memory_resource& memory_; common::ipc::ClientRouter::Ptr ipc_router_; - cetl::optional ipc_exec_cmd_channel_; + cetl::optional ipc_exec_cmd_ch_; }; // DaemonImpl From 5e16615390b5296aae113d310d3338999fc28c08 Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 8 Jan 2025 03:23:39 +0200 Subject: [PATCH 032/156] `setEventHandler` -> `subscribe` --- .../common/ipc/RouteChannelMsg.1.0.dsdl | 3 +- src/common/ipc/channel.hpp | 82 +++++++++---------- src/common/ipc/client_router.cpp | 33 +++++--- src/common/ipc/gateway.hpp | 3 +- src/common/ipc/server_router.cpp | 49 +++++++---- src/daemon/engine/application.cpp | 2 +- src/sdk/daemon.cpp | 2 +- test/common/ipc/test_client_router.cpp | 12 +-- test/common/ipc/test_server_router.cpp | 10 ++- 9 files changed, 113 insertions(+), 83 deletions(-) diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl index 5b431ad..47d7a02 100644 --- a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl @@ -1,5 +1,6 @@ -uint64 tag uint64 service_id +uint64 tag +uint64 sequence # reserve twice as much as we need. @extent _offset_.max * 2 diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 2a7b18e..2ac621c 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -56,8 +56,9 @@ template class Channel final : public AnyChannel { public: - using Input = Input_; - using Output = Output_; + using Input = Input_; + using Output = Output_; + using EventVar = cetl::variant; using EventHandler = std::function; @@ -65,9 +66,7 @@ class Channel final : public AnyChannel : memory_{other.memory_} , gateway_{std::move(other.gateway_)} , service_id_{other.service_id_} - , event_handler_{std::move(other.event_handler_)} { - setupEventHandling(); } ~Channel() = default; @@ -93,64 +92,60 @@ class Channel final : public AnyChannel }); } - Channel& setEventHandler(EventHandler event_handler) + void subscribe(EventHandler event_handler) { - event_handler_ = std::move(event_handler); - setupEventHandling(); - return *this; + if (event_handler) + { + gateway_->subscribe( + [adapter = Adapter{memory_, std::move(event_handler)}](const GatewayEvent::Var& ge_var) { + // + cetl::visit(adapter, ge_var); + }); + } + else + { + gateway_->subscribe(nullptr); + } } private: friend class ClientRouter; friend class ServerRouter; - Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, const detail::ServiceId service_id) - : memory_{memory} - , gateway_{std::move(gateway)} - , service_id_{service_id} - { - CETL_DEBUG_ASSERT(gateway_, ""); - } + using GatewayEvent = detail::Gateway::Event; - void setupEventHandling() + struct Adapter final { - gateway_->setEventHandler([this](const detail::Gateway::Event::Var& gateway_event_var) { - // - cetl::visit( - [this](const auto& gateway_event) { - // - handleGatewayEvent(gateway_event); - }, - gateway_event_var); - }); - } + cetl::pmr::memory_resource& memory; // NOLINT + EventHandler ch_event_handler; // NOLINT - void handleGatewayEvent(const detail::Gateway::Event::Message& gateway_message) - { - if (event_handler_) + void operator()(const GatewayEvent::Connected&) const + { + ch_event_handler(Connected{}); + } + + void operator()(const GatewayEvent::Message& gateway_msg) const { - Input input{&memory_}; - if (tryDeserializePayload(gateway_message.payload, input)) + Input input{&memory}; + if (tryDeserializePayload(gateway_msg.payload, input)) { - event_handler_(input); + ch_event_handler(input); } } - } - void handleGatewayEvent(const detail::Gateway::Event::Connected) - { - if (event_handler_) + void operator()(const GatewayEvent::Disconnected&) const { - event_handler_(Connected{}); + ch_event_handler(Disconnected{}); } - } - void handleGatewayEvent(const detail::Gateway::Event::Disconnected) + }; // Adapter + + Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, const detail::ServiceId service_id) + : memory_{memory} + , gateway_{std::move(gateway)} + , service_id_{service_id} { - if (event_handler_) - { - event_handler_(Disconnected{}); - } + CETL_DEBUG_ASSERT(gateway_, ""); } static constexpr std::size_t MsgSmallPayloadSize = 256; @@ -158,7 +153,6 @@ class Channel final : public AnyChannel cetl::pmr::memory_resource& memory_; detail::Gateway::Ptr gateway_; detail::ServiceId service_id_; - EventHandler event_handler_; }; // Channel diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 05899d2..59ab04d 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -127,6 +127,7 @@ class ClientRouterImpl final : public ClientRouter GatewayImpl(Private, ClientRouterImpl& router, const Endpoint& endpoint) : router_{router} , endpoint_{endpoint} + , sequence_{0} { ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag()); } @@ -145,9 +146,11 @@ class ClientRouterImpl final : public ClientRouter void send(const detail::ServiceId service_id, const pipe::Payload payload) override { Route_1_0 route{&router_.memory_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.getTag(); + + auto& channel_msg = route.set_channel_msg(); channel_msg.service_id = service_id; + channel_msg.tag = endpoint_.getTag(); + channel_msg.sequence = sequence_++; tryPerformOnSerialized(route, [this, payload](const auto prefix) { // @@ -164,15 +167,24 @@ class ClientRouterImpl final : public ClientRouter } } - void setEventHandler(EventHandler event_handler) override + void subscribe(EventHandler event_handler) override { - router_.registerGateway(endpoint_, shared_from_this()); - event_handler_ = std::move(event_handler); + if (event_handler) + { + event_handler_ = std::move(event_handler); + router_.registerGateway(endpoint_, shared_from_this()); + } + else + { + event_handler_ = nullptr; + router_.unregisterGateway(endpoint_); + } } private: ClientRouterImpl& router_; const Endpoint endpoint_; + std::uint64_t sequence_; EventHandler event_handler_; }; // GatewayImpl @@ -247,9 +259,9 @@ class ClientRouterImpl final : public ClientRouter // handleRouteConnect(route_conn); }, - [this, msg_payload](const RouteChannelMsg_1_0& route_channel) { + [this, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { // - handleRouteChannelMsg(route_channel, msg_payload); + handleRouteChannelMsg(route_ch_msg, msg_payload); }), route_msg.union_value); @@ -277,9 +289,9 @@ class ClientRouterImpl final : public ClientRouter }); } - void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_channel_msg, pipe::Payload payload) + void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_ch_msg, pipe::Payload payload) { - const Endpoint endpoint{route_channel_msg.tag}; + const Endpoint endpoint{route_ch_msg.tag}; const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); if (ep_to_gw != endpoint_to_gateway_.end()) @@ -287,7 +299,8 @@ class ClientRouterImpl final : public ClientRouter const auto gateway = ep_to_gw->second.lock(); if (gateway) { - gateway->event(detail::Gateway::Event::Message{payload}); + gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, payload}); + return; } } diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index 31be919..74b54ba 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -40,6 +40,7 @@ class Gateway {}; struct Message final { + std::uint64_t sequence; pipe::Payload payload; }; // Message @@ -57,7 +58,7 @@ class Gateway virtual void send(const ServiceId service_id, const pipe::Payload payload) = 0; virtual void event(const Event::Var& event) = 0; - virtual void setEventHandler(EventHandler event_handler) = 0; + virtual void subscribe(EventHandler event_handler) = 0; protected: Gateway() = default; diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index b125bf8..9c66b2b 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -135,6 +135,7 @@ class ServerRouterImpl final : public ServerRouter GatewayImpl(Private, ServerRouterImpl& router, const Endpoint& endpoint) : router_{router} , endpoint_{endpoint} + , sequence_{0} { ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag()); } @@ -153,9 +154,11 @@ class ServerRouterImpl final : public ServerRouter void send(const detail::ServiceId service_id, const pipe::Payload payload) override { Route_1_0 route{&router_.memory_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.getTag(); + + auto& channel_msg = route.set_channel_msg(); channel_msg.service_id = service_id; + channel_msg.tag = endpoint_.getTag(); + channel_msg.sequence = sequence_++; tryPerformOnSerialized(route, [this, payload](const auto prefix) { // @@ -172,15 +175,24 @@ class ServerRouterImpl final : public ServerRouter } } - void setEventHandler(EventHandler event_handler) override + void subscribe(EventHandler event_handler) override { - router_.registerGateway(endpoint_, shared_from_this()); - event_handler_ = std::move(event_handler); + if (event_handler) + { + event_handler_ = std::move(event_handler); + router_.registerGateway(endpoint_, shared_from_this()); + } + else + { + event_handler_ = nullptr; + router_.unregisterGateway(endpoint_); + } } private: ServerRouterImpl& router_; const Endpoint endpoint_; + std::uint64_t sequence_; EventHandler event_handler_; }; // GatewayImpl @@ -223,9 +235,9 @@ class ServerRouterImpl final : public ServerRouter // handleRouteConnect(msg.client_id, route_conn); }, - [this, &msg, msg_payload](const RouteChannelMsg_1_0& route_channel) { + [this, &msg, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { // - handleRouteChannelMsg(msg.client_id, route_channel, msg_payload); + handleRouteChannelMsg(msg.client_id, route_ch_msg, msg_payload); }), route_msg.union_value); @@ -245,7 +257,8 @@ class ServerRouterImpl final : public ServerRouter // TODO: log client route connection Route_1_0 route{&memory_}; - auto& route_conn = route.set_connect(); + + auto& route_conn = route.set_connect(); route_conn.version.major = VERSION_MAJOR; route_conn.version.minor = VERSION_MINOR; @@ -257,10 +270,10 @@ class ServerRouterImpl final : public ServerRouter } void handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, - const RouteChannelMsg_1_0& route_channel_msg, + const RouteChannelMsg_1_0& route_ch_msg, pipe::Payload msg_payload) { - const Endpoint endpoint{route_channel_msg.tag, client_id}; + const Endpoint endpoint{route_ch_msg.tag, client_id}; const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); if (ep_to_gw != endpoint_to_gateway_.end()) @@ -268,16 +281,20 @@ class ServerRouterImpl final : public ServerRouter auto gateway = ep_to_gw->second.lock(); if (gateway) { - gateway->event(detail::Gateway::Event::Message{msg_payload}); + gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_payload}); + return; } - return; } - const auto si_to_cf = service_id_to_channel_factory_.find(route_channel_msg.service_id); - if (si_to_cf != service_id_to_channel_factory_.end()) + // Only the very first message in the sequence is considered to trigger channel factory. + if (route_ch_msg.sequence == 0) { - auto gateway = GatewayImpl::create(*this, endpoint); - si_to_cf->second(gateway, msg_payload); + const auto si_to_cf = service_id_to_channel_factory_.find(route_ch_msg.service_id); + if (si_to_cf != service_id_to_channel_factory_.end()) + { + auto gateway = GatewayImpl::create(*this, endpoint); + si_to_cf->second(gateway, msg_payload); + } } // TODO: log unsolicited message diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 5b8a368..dc511e1 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -74,7 +74,7 @@ cetl::optional Application::init() // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client initial msg (%zu).", request.some_stuff.size()); ch.send(request); - ch.setEventHandler([this](const auto&) { + ch.subscribe([this](const auto&) { // ::syslog(LOG_DEBUG, "Client nested msg"); ExecCmd r1{&memory_}; diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 4141912..367447f 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -44,7 +44,7 @@ class DaemonImpl final : public Daemon using Ch = ExecCmdChannel; auto channel = ipc_router_->makeChannel("daemon"); - channel.setEventHandler([this](const auto& event_var) { + channel.subscribe([this](const auto& event_var) { // cetl::visit( // cetl::make_overloaded( diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 7da64d8..c8739ba 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -79,14 +79,16 @@ class TestClientRouter : public testing::Test void withRouteChannelMsg(const cetl::string_view service_name, const std::uint64_t tag, const Message& message, + const std::uint64_t sequence, Action action) { using ocvsmd::common::tryPerformOnSerialized; Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = tag; channel_msg.service_id = AnyChannel::getServiceId(service_name); + channel_msg.tag = tag; + channel_msg.sequence = sequence; tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -208,10 +210,10 @@ TEST_F(TestClientRouter, makeChannel_receive_events) StrictMock> ch2_event_mock; auto channel1 = client_router->makeChannel(); - channel1.setEventHandler(ch1_event_mock.AsStdFunction()); + channel1.subscribe(ch1_event_mock.AsStdFunction()); auto channel2 = client_router->makeChannel(); - channel2.setEventHandler(ch2_event_mock.AsStdFunction()); + channel2.subscribe(ch2_event_mock.AsStdFunction()); // Emulate that we've got pipe connected - `RouteConnect` should be sent to the server. // @@ -230,7 +232,7 @@ TEST_F(TestClientRouter, makeChannel_receive_events) // Emulate that server posted `RouteChannelMsg` on tag #1. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 1, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 1, Channel::Input{&mr_}, 0, [&](const auto payload) { // client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); }); @@ -238,7 +240,7 @@ TEST_F(TestClientRouter, makeChannel_receive_events) // Emulate that server posted `RouteChannelMsg` on tag #2. // EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 2, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 2, Channel::Input{&mr_}, 0, [&](const auto payload) { // client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); }); diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index cb3a0b7..806ea59 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -58,14 +58,16 @@ class TestServerRouter : public testing::Test void withRouteChannelMsg(const cetl::string_view service_name, const std::uint64_t tag, const Message& message, + const std::uint64_t sequence, Action action) { using ocvsmd::common::tryPerformOnSerialized; Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = tag; channel_msg.service_id = AnyChannel::getServiceId(service_name); + channel_msg.tag = tag; + channel_msg.sequence = sequence; tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -160,7 +162,7 @@ TEST_F(TestServerRouter, channel_send) cetl::optional maybe_channel; server_router->registerChannel("", [&](auto&& ch, const auto& input) { // - ch.setEventHandler(ch1_event_mock.AsStdFunction()); + ch.subscribe(ch1_event_mock.AsStdFunction()); maybe_channel.emplace(std::forward(ch)); ch1_event_mock.Call(input); }); @@ -169,7 +171,7 @@ TEST_F(TestServerRouter, channel_send) // Emulate that client posted initial `RouteChannelMsg` on 1/42 tag/client pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 1, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 1, Channel::Input{&mr_}, 0, [&](const auto payload) { // server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); }); @@ -177,7 +179,7 @@ TEST_F(TestServerRouter, channel_send) // Emulate that client posted one more `RouteChannelMsg` on the same 1/42 tag/client pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 2, Channel::Input{&mr_}, [&](const auto payload) { + withRouteChannelMsg("", 1, Channel::Input{&mr_}, 1, [&](const auto payload) { // server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); }); From 64d815c9787c7f4df9c9c884297b1745fe531830 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 8 Jan 2025 11:38:42 +0200 Subject: [PATCH 033/156] make `Channel` default move constructible/assignable --- src/common/ipc/channel.hpp | 22 ++++++++-------------- src/daemon/engine/application.cpp | 4 ++-- src/sdk/daemon.cpp | 2 +- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 2ac621c..10e1fa7 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -62,18 +62,12 @@ class Channel final : public AnyChannel using EventVar = cetl::variant; using EventHandler = std::function; - Channel(Channel&& other) noexcept - : memory_{other.memory_} - , gateway_{std::move(other.gateway_)} - , service_id_{other.service_id_} - { - } - - ~Channel() = default; + ~Channel() = default; + Channel(Channel&& other) noexcept = default; + Channel& operator=(Channel&& other) noexcept = default; - Channel(const Channel&) = delete; - Channel& operator=(const Channel&) = delete; - Channel& operator=(Channel&& other) noexcept = delete; + Channel(const Channel&) = delete; + Channel& operator=(const Channel&) = delete; using SendFailure = nunavut::support::Error; using SendResult = cetl::optional; @@ -150,9 +144,9 @@ class Channel final : public AnyChannel static constexpr std::size_t MsgSmallPayloadSize = 256; - cetl::pmr::memory_resource& memory_; - detail::Gateway::Ptr gateway_; - detail::ServiceId service_id_; + std::reference_wrapper memory_; + detail::Gateway::Ptr gateway_; + detail::ServiceId service_id_; }; // Channel diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index dc511e1..eaf8827 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -69,7 +69,7 @@ cetl::optional Application::init() ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); using Ch = ExecCmdChannel; - ipc_router_->registerChannel("daemon", [this](auto&& ch, const auto& request) { + ipc_router_->registerChannel("daemon", [this](Ch&& ch, const auto& request) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client initial msg (%zu).", request.some_stuff.size()); @@ -81,7 +81,7 @@ cetl::optional Application::init() ipc_exec_cmd_ch_->send(r1); ipc_exec_cmd_ch_.reset(); }); - ipc_exec_cmd_ch_.emplace(std::move(ch)); + ipc_exec_cmd_ch_ = std::move(ch); }); ipc_router_->start(); diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 367447f..294f21f 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -70,7 +70,7 @@ class DaemonImpl final : public Daemon }), event_var); }); - ipc_exec_cmd_ch_.emplace(std::move(channel)); + ipc_exec_cmd_ch_ = std::move(channel); } private: From 418b8fb7f31803c4981febfb4190760a5bf2d795 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 8 Jan 2025 11:59:02 +0200 Subject: [PATCH 034/156] template `Ch` param --- src/common/ipc/client_router.hpp | 8 ++++---- src/common/ipc/server_router.hpp | 14 +++++++------- src/daemon/engine/application.cpp | 2 +- src/sdk/daemon.cpp | 9 ++++----- test/common/ipc/test_client_router.cpp | 17 +++++++++-------- test/common/ipc/test_server_router.cpp | 9 +++++---- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index 87e6aae..d26cb22 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -39,11 +39,11 @@ class ClientRouter virtual void start() = 0; virtual cetl::pmr::memory_resource& memory() = 0; - template - CETL_NODISCARD Channel makeChannel(cetl::string_view service_name = "") + template + CETL_NODISCARD Ch makeChannel(cetl::string_view service_name = "") { - const auto service_id = AnyChannel::getServiceId(service_name); - return Channel{memory(), makeGateway(), service_id}; + const auto service_id = AnyChannel::getServiceId(service_name); + return Ch{memory(), makeGateway(), service_id}; } protected: diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index 5bef6b3..4b74925 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -40,24 +40,24 @@ class ServerRouter virtual void start() = 0; virtual cetl::pmr::memory_resource& memory() = 0; - template - using NewChannelHandler = std::function&& new_channel, const Input& input)>; + template + using NewChannelHandler = std::function; - template - void registerChannel(const cetl::string_view service_name, NewChannelHandler handler) + template + void registerChannel(const cetl::string_view service_name, NewChannelHandler handler) { CETL_DEBUG_ASSERT(handler, ""); - const auto service_id = AnyChannel::getServiceId(service_name); + const auto service_id = AnyChannel::getServiceId(service_name); registerChannelFactory( // service_id, [this, service_id, new_ch_handler = std::move(handler)](detail::Gateway::Ptr gateway, const pipe::Payload payload) { - Input input{&memory()}; + typename Ch::Input input{&memory()}; if (tryDeserializePayload(payload, input)) { - new_ch_handler(Channel{memory(), gateway, service_id}, input); + new_ch_handler(Ch{memory(), gateway, service_id}, input); } }); } diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index eaf8827..a48f58a 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -69,7 +69,7 @@ cetl::optional Application::init() ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); using Ch = ExecCmdChannel; - ipc_router_->registerChannel("daemon", [this](Ch&& ch, const auto& request) { + ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto& request) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client initial msg (%zu).", request.some_stuff.size()); diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 294f21f..d48b3dd 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -42,13 +42,12 @@ class DaemonImpl final : public Daemon { ipc_router_->start(); - using Ch = ExecCmdChannel; - auto channel = ipc_router_->makeChannel("daemon"); + auto channel = ipc_router_->makeChannel("daemon"); channel.subscribe([this](const auto& event_var) { // cetl::visit( // cetl::make_overloaded( - [this](const Ch::Connected&) { + [this](const ExecCmdChannel::Connected&) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Ch connected."); @@ -57,13 +56,13 @@ class DaemonImpl final : public Daemon cmd.some_stuff.push_back('Z'); ipc_exec_cmd_ch_->send(cmd); }, - [this](const Ch::Input& input) { + [this](const ExecCmdChannel::Input& input) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", input.some_stuff.size()); ipc_exec_cmd_ch_->send(input); }, - [](const Ch::Disconnected&) { + [](const ExecCmdChannel::Disconnected&) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server disconnected."); diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index c8739ba..1c9ac2e 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -144,7 +144,8 @@ TEST_F(TestClientRouter, start) TEST_F(TestClientRouter, makeChannel) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Channel = Channel; StrictMock client_pipe_mock; EXPECT_CALL(client_pipe_mock, deinit()).Times(1); @@ -157,13 +158,14 @@ TEST_F(TestClientRouter, makeChannel) EXPECT_CALL(client_pipe_mock, start(_)).Times(1); client_router->start(); - const auto channel = client_router->makeChannel(); + const auto channel = client_router->makeChannel(); (void) channel; } TEST_F(TestClientRouter, makeChannel_send) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Channel = Channel; StrictMock client_pipe_mock; EXPECT_CALL(client_pipe_mock, deinit()).Times(1); @@ -176,7 +178,7 @@ TEST_F(TestClientRouter, makeChannel_send) EXPECT_CALL(client_pipe_mock, start(_)).Times(1); client_router->start(); - auto channel = client_router->makeChannel(); + auto channel = client_router->makeChannel(); Msg msg{&mr_}; @@ -191,8 +193,7 @@ TEST_F(TestClientRouter, makeChannel_send) TEST_F(TestClientRouter, makeChannel_receive_events) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; - ; + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; using Channel = Channel; StrictMock client_pipe_mock; @@ -209,10 +210,10 @@ TEST_F(TestClientRouter, makeChannel_receive_events) StrictMock> ch1_event_mock; StrictMock> ch2_event_mock; - auto channel1 = client_router->makeChannel(); + auto channel1 = client_router->makeChannel(); channel1.subscribe(ch1_event_mock.AsStdFunction()); - auto channel2 = client_router->makeChannel(); + auto channel2 = client_router->makeChannel(); channel2.subscribe(ch2_event_mock.AsStdFunction()); // Emulate that we've got pipe connected - `RouteConnect` should be sent to the server. diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 806ea59..8e66425 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -121,7 +121,8 @@ TEST_F(TestServerRouter, start) TEST_F(TestServerRouter, registerChannel) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Channel = Channel; StrictMock server_pipe_mock; EXPECT_CALL(server_pipe_mock, deinit()).Times(1); @@ -136,7 +137,7 @@ TEST_F(TestServerRouter, registerChannel) server_router->start(); EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); - server_router->registerChannel("", [](auto&&, const auto&) {}); + server_router->registerChannel("", [](auto&&, const auto&) {}); } TEST_F(TestServerRouter, channel_send) @@ -160,10 +161,10 @@ TEST_F(TestServerRouter, channel_send) StrictMock> ch1_event_mock; cetl::optional maybe_channel; - server_router->registerChannel("", [&](auto&& ch, const auto& input) { + server_router->registerChannel("", [&](Channel&& ch, const auto& input) { // ch.subscribe(ch1_event_mock.AsStdFunction()); - maybe_channel.emplace(std::forward(ch)); + maybe_channel.emplace(std::move(ch)); ch1_event_mock.Call(input); }); EXPECT_THAT(maybe_channel.has_value(), IsFalse()); From 6041ff872fe77fcb3637374967d66f6d98c9995a Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 8 Jan 2025 17:27:21 +0200 Subject: [PATCH 035/156] more CETL_NODISCARD --- include/ocvsmd/sdk/daemon.hpp | 5 +- .../dsdl/ocvsmd/common/ipc/Route.1.0.dsdl | 1 + .../common/ipc/RouteChannelEnd.1.0.dsdl | 5 + src/common/dsdl_helpers.hpp | 19 +-- src/common/ipc/channel.hpp | 23 ++-- src/common/ipc/client_router.cpp | 115 +++++++++++++----- src/common/ipc/client_router.hpp | 6 +- src/common/ipc/gateway.hpp | 7 +- src/common/ipc/pipe/client_pipe.hpp | 6 +- src/common/ipc/pipe/server_pipe.hpp | 7 +- src/common/ipc/pipe/unix_socket_base.hpp | 5 +- src/common/ipc/pipe/unix_socket_client.cpp | 2 +- src/common/ipc/pipe/unix_socket_client.hpp | 7 +- src/common/ipc/pipe/unix_socket_server.cpp | 4 +- src/common/ipc/pipe/unix_socket_server.hpp | 7 +- src/common/ipc/server_router.cpp | 94 +++++++++----- src/common/ipc/server_router.hpp | 6 +- src/daemon/engine/application.cpp | 13 +- src/sdk/daemon.cpp | 30 +++-- test/common/ipc/pipe/client_pipe_mock.hpp | 6 +- test/common/ipc/pipe/server_pipe_mock.hpp | 7 +- test/common/ipc/test_client_router.cpp | 91 +++++++------- test/common/ipc/test_server_router.cpp | 50 ++++---- 23 files changed, 316 insertions(+), 200 deletions(-) create mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl diff --git a/include/ocvsmd/sdk/daemon.hpp b/include/ocvsmd/sdk/daemon.hpp index cfdfb89..fc71750 100644 --- a/include/ocvsmd/sdk/daemon.hpp +++ b/include/ocvsmd/sdk/daemon.hpp @@ -6,6 +6,7 @@ #ifndef OCVSMD_SDK_DAEMON_HPP_INCLUDED #define OCVSMD_SDK_DAEMON_HPP_INCLUDED +#include #include #include @@ -21,7 +22,9 @@ namespace sdk class Daemon { public: - static std::unique_ptr make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor); + CETL_NODISCARD static std::unique_ptr make( // + cetl::pmr::memory_resource& memory, + libcyphal::IExecutor& executor); Daemon(Daemon&&) = delete; Daemon(const Daemon&) = delete; diff --git a/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl index d9269ca..95d22ae 100644 --- a/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl +++ b/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl @@ -3,5 +3,6 @@ uavcan.primitive.Empty.1.0 empty RouteConnect.1.0 connect RouteChannelMsg.1.0 channel_msg +RouteChannelEnd.1.0 channel_end @sealed diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl new file mode 100644 index 0000000..ebf9b13 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl @@ -0,0 +1,5 @@ +uint64 tag +int32 error_code + +# reserve twice as much as we need. +@extent _offset_.max * 2 diff --git a/src/common/dsdl_helpers.hpp b/src/common/dsdl_helpers.hpp index b1439ec..316c3cf 100644 --- a/src/common/dsdl_helpers.hpp +++ b/src/common/dsdl_helpers.hpp @@ -6,6 +6,7 @@ #ifndef OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED #define OCVSMD_COMMON_DSDL_HELPERS_HPP_INCLUDED +#include #include #include @@ -21,13 +22,13 @@ namespace common { template -static auto tryDeserializePayload(const cetl::span payload, Message& out_message) +CETL_NODISCARD static auto tryDeserializePayload(const cetl::span payload, Message& out_message) { return deserialize(out_message, {payload.data(), payload.size()}); } template -static int tryPerformOnSerialized(const Message& message, Action&& action) +CETL_NODISCARD static int tryPerformOnSerialized(const Message& message, Action&& action) { // Try to serialize the message to raw payload buffer. // @@ -45,8 +46,9 @@ static int tryPerformOnSerialized(const Message& message, Action&& action) return std::forward(action)(bytes); } -template -static auto tryPerformOnSerialized(const Message& message, Action&& action) -> std::enable_if_t +template +CETL_NODISCARD static auto tryPerformOnSerialized(const Message& message, + Action&& action) -> std::enable_if_t { // Try to serialize the message to raw payload buffer. // @@ -57,15 +59,16 @@ static auto tryPerformOnSerialized(const Message& message, Action&& action) -> s const auto result_size = serialize(message, {buffer.data(), buffer.size()}); if (!result_size) { - return Result{result_size.error()}; + return EINVAL; } const cetl::span bytes{buffer.data(), result_size.value()}; return std::forward(action)(bytes); } -template -static auto tryPerformOnSerialized(const Message& message, Action&& action) -> std::enable_if_t +template +CETL_NODISCARD static auto tryPerformOnSerialized(const Message& message, + Action&& action) -> std::enable_if_t { // Try to serialize the message to raw payload buffer. // @@ -75,7 +78,7 @@ static auto tryPerformOnSerialized(const Message& message, Action&& action) -> s const auto result_size = serialize(message, {buffer->data(), buffer->size()}); if (!result_size) { - return Result{result_size.error()}; + return EINVAL; } const cetl::span bytes{buffer->data(), result_size.value()}; diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 10e1fa7..69f076e 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -11,6 +11,7 @@ #include +#include #include #include @@ -37,7 +38,7 @@ class AnyChannel /// Builds a service ID from either the service name (if not empty), or message type name. /// template - static detail::ServiceId getServiceId(const cetl::string_view service_name) noexcept + CETL_NODISCARD static detail::ServiceId getServiceId(const cetl::string_view service_name) noexcept { const cetl::string_view srv_or_msg_name = !service_name.empty() // ? service_name @@ -69,20 +70,16 @@ class Channel final : public AnyChannel Channel(const Channel&) = delete; Channel& operator=(const Channel&) = delete; - using SendFailure = nunavut::support::Error; - using SendResult = cetl::optional; - - SendResult send(const Output& output) + CETL_NODISCARD int send(const Output& output) { constexpr std::size_t BufferSize = Output::_traits_::SerializationBufferSizeBytes; constexpr bool IsOnStack = BufferSize <= MsgSmallPayloadSize; - return tryPerformOnSerialized( // + return tryPerformOnSerialized( // output, [this](const auto payload) { // - gateway_->send(service_id_, payload); - return cetl::nullopt; + return gateway_->send(service_id_, payload); }); } @@ -90,11 +87,11 @@ class Channel final : public AnyChannel { if (event_handler) { - gateway_->subscribe( - [adapter = Adapter{memory_, std::move(event_handler)}](const GatewayEvent::Var& ge_var) { - // - cetl::visit(adapter, ge_var); - }); + auto adapter = Adapter{memory_, std::move(event_handler)}; + gateway_->subscribe([adapter = std::move(adapter)](const GatewayEvent::Var& ge_var) { + // + cetl::visit(adapter, ge_var); + }); } else { diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 59ab04d..4ab1579 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -10,6 +10,7 @@ #include "pipe/client_pipe.hpp" #include "pipe/pipe_types.hpp" +#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" #include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" @@ -19,7 +20,6 @@ #include #include -#include #include #include #include @@ -45,20 +45,21 @@ class ClientRouterImpl final : public ClientRouter : memory_{memory} , client_pipe_{std::move(client_pipe)} , last_unique_tag_{0} + , is_connected_{false} { CETL_DEBUG_ASSERT(client_pipe_, ""); } // ClientRouter - cetl::pmr::memory_resource& memory() override + CETL_NODISCARD cetl::pmr::memory_resource& memory() override { return memory_; } - void start() override + CETL_NODISCARD int start() override { - client_pipe_->start([this](const auto& pipe_event_var) { + return client_pipe_->start([this](const auto& pipe_event_var) { // return cetl::visit( [this](const auto& pipe_event) { @@ -85,21 +86,21 @@ class ClientRouterImpl final : public ClientRouter { } - Tag getTag() const noexcept + CETL_NODISCARD Tag getTag() const noexcept { return tag_; } // Hasher - bool operator==(const Endpoint& other) const noexcept + CETL_NODISCARD bool operator==(const Endpoint& other) const noexcept { return tag_ == other.tag_; } struct Hasher final { - std::size_t operator()(const Endpoint& endpoint) const noexcept + CETL_NODISCARD std::size_t operator()(const Endpoint& endpoint) const noexcept { return std::hash{}(endpoint.tag_); } @@ -119,7 +120,7 @@ class ClientRouterImpl final : public ClientRouter }; public: - static std::shared_ptr create(ClientRouterImpl& router, const Endpoint& endpoint) + CETL_NODISCARD static std::shared_ptr create(ClientRouterImpl& router, const Endpoint& endpoint) { return std::make_shared(Private(), router, endpoint); } @@ -139,12 +140,19 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { - router_.unregisterGateway(endpoint_); + router_.unregisterGateway(endpoint_, true); ::syslog(LOG_DEBUG, "~Gateway(tag=%zu).", endpoint_.getTag()); } - void send(const detail::ServiceId service_id, const pipe::Payload payload) override + // detail::Gateway + + CETL_NODISCARD int send(const detail::ServiceId service_id, const pipe::Payload payload) override { + if (!router_.is_connected_) + { + return ENOTCONN; + } + Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); @@ -152,10 +160,9 @@ class ClientRouterImpl final : public ClientRouter channel_msg.tag = endpoint_.getTag(); channel_msg.sequence = sequence_++; - tryPerformOnSerialized(route, [this, payload](const auto prefix) { + return tryPerformOnSerialized(route, [this, payload](const auto prefix) { // - std::array fragments{prefix, payload}; - return router_.client_pipe_->sendMessage(fragments); + return router_.client_pipe_->send({{prefix, payload}}); }); } @@ -172,7 +179,7 @@ class ClientRouterImpl final : public ClientRouter if (event_handler) { event_handler_ = std::move(event_handler); - router_.registerGateway(endpoint_, shared_from_this()); + router_.registerGateway(endpoint_, *this); } else { @@ -191,14 +198,39 @@ class ClientRouterImpl final : public ClientRouter using EndpointToWeakGateway = std::unordered_map; - void registerGateway(const Endpoint& endpoint, detail::Gateway::WeakPtr gateway) + CETL_NODISCARD bool isConnected(const Endpoint&) const noexcept { - endpoint_to_gateway_[endpoint] = std::move(gateway); + return is_connected_; + } + + void registerGateway(const Endpoint& endpoint, GatewayImpl& gateway) + { + endpoint_to_gateway_[endpoint] = gateway.shared_from_this(); + if (is_connected_) + { + gateway.event(detail::Gateway::Event::Connected{}); + } } - void unregisterGateway(const Endpoint& endpoint) + void unregisterGateway(const Endpoint& endpoint, const bool is_disposed = false) { endpoint_to_gateway_.erase(endpoint); + + // Notify "remote" router about the gateway disposal. + // The router will deliver "disconnected" event to the counterpart gateway (if it exists). + // + if (is_disposed && isConnected(endpoint)) + { + Route_1_0 route{&memory_}; + auto& channel_end = route.set_channel_end(); + // channel_end.version.minor = VERSION_MINOR; + + const int result = tryPerformOnSerialized(route, [this](const auto payload) { + // + return client_pipe_->send({{payload}}); + }); + (void) result; + } } template @@ -224,7 +256,7 @@ class ClientRouterImpl final : public ClientRouter } } - int handlePipeEvent(const pipe::ClientPipe::Event::Connected) const + CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Connected) const { // TODO: log client pipe connection @@ -233,14 +265,13 @@ class ClientRouterImpl final : public ClientRouter route_conn.version.major = VERSION_MAJOR; route_conn.version.minor = VERSION_MINOR; - return tryPerformOnSerialized(route, [this](const auto payload) { + return tryPerformOnSerialized(route, [this](const auto payload) { // - std::array payloads{payload}; - return client_pipe_->sendMessage(payloads); + return client_pipe_->send({{payload}}); }); } - int handlePipeEvent(const pipe::ClientPipe::Event::Message& msg) + CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Message& msg) { Route_1_0 route_msg{&memory_}; const auto result_size = tryDeserializePayload(msg.payload, route_msg); @@ -262,31 +293,49 @@ class ClientRouterImpl final : public ClientRouter [this, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { // handleRouteChannelMsg(route_ch_msg, msg_payload); + }, + [this, msg_payload](const RouteChannelEnd_1_0& route_ch_end) { + // + handleRouteChannelEnd(route_ch_end); }), route_msg.union_value); return 0; } - int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) const + CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) { // TODO: log client pipe disconnection - forEachGateway([](const auto& gateway) { + if (is_connected_) + { + is_connected_ = false; + + // The whole router is disconnected, so we need to notify all local gateways. // - gateway->event(detail::Gateway::Event::Disconnected{}); - }); + forEachGateway([](const auto& gateway) { + // + gateway->event(detail::Gateway::Event::Disconnected{}); + }); + } return 0; } - void handleRouteConnect(const RouteConnect_1_0&) const + void handleRouteConnect(const RouteConnect_1_0&) { // TODO: log server route connection - forEachGateway([](const auto& gateway) { + if (!is_connected_) + { + is_connected_ = true; + + // We've got connection response from the server, so we need to notify all local gateways. // - gateway->event(detail::Gateway::Event::Connected{}); - }); + forEachGateway([](const auto& gateway) { + // + gateway->event(detail::Gateway::Event::Connected{}); + }); + } } void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_ch_msg, pipe::Payload payload) @@ -307,16 +356,20 @@ class ClientRouterImpl final : public ClientRouter // TODO: log unsolicited message } + void handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end) {} + cetl::pmr::memory_resource& memory_; pipe::ClientPipe::Ptr client_pipe_; Endpoint::Tag last_unique_tag_; EndpointToWeakGateway endpoint_to_gateway_; + bool is_connected_; }; // ClientRouterImpl } // namespace -ClientRouter::Ptr ClientRouter::make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) +CETL_NODISCARD ClientRouter::Ptr ClientRouter::make(cetl::pmr::memory_resource& memory, + pipe::ClientPipe::Ptr client_pipe) { return std::make_unique(memory, std::move(client_pipe)); } diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index d26cb22..5f58086 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -27,7 +27,7 @@ class ClientRouter public: using Ptr = std::unique_ptr; - static Ptr make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe); + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe); ClientRouter(const ClientRouter&) = delete; ClientRouter(ClientRouter&&) noexcept = delete; @@ -36,8 +36,8 @@ class ClientRouter virtual ~ClientRouter() = default; - virtual void start() = 0; - virtual cetl::pmr::memory_resource& memory() = 0; + CETL_NODISCARD virtual int start() = 0; + CETL_NODISCARD virtual cetl::pmr::memory_resource& memory() = 0; template CETL_NODISCARD Ch makeChannel(cetl::string_view service_name = "") diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index 74b54ba..bf933f3 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -8,6 +8,7 @@ #include "pipe/pipe_types.hpp" +#include #include #include @@ -56,9 +57,9 @@ class Gateway Gateway& operator=(const Gateway&) = delete; Gateway& operator=(Gateway&&) noexcept = delete; - virtual void send(const ServiceId service_id, const pipe::Payload payload) = 0; - virtual void event(const Event::Var& event) = 0; - virtual void subscribe(EventHandler event_handler) = 0; + CETL_NODISCARD virtual int send(const ServiceId service_id, const pipe::Payload payload) = 0; + virtual void event(const Event::Var& event) = 0; + virtual void subscribe(EventHandler event_handler) = 0; protected: Gateway() = default; diff --git a/src/common/ipc/pipe/client_pipe.hpp b/src/common/ipc/pipe/client_pipe.hpp index 4e36cc0..78cb76e 100644 --- a/src/common/ipc/pipe/client_pipe.hpp +++ b/src/common/ipc/pipe/client_pipe.hpp @@ -8,8 +8,8 @@ #include "pipe_types.hpp" +#include #include -#include #include #include @@ -53,8 +53,8 @@ class ClientPipe virtual ~ClientPipe() = default; - virtual int start(EventHandler event_handler) = 0; - virtual int sendMessage(const Payloads payloads) = 0; + CETL_NODISCARD virtual int start(EventHandler event_handler) = 0; + CETL_NODISCARD virtual int send(const Payloads payloads) = 0; protected: ClientPipe() = default; diff --git a/src/common/ipc/pipe/server_pipe.hpp b/src/common/ipc/pipe/server_pipe.hpp index 790688b..2c352a8 100644 --- a/src/common/ipc/pipe/server_pipe.hpp +++ b/src/common/ipc/pipe/server_pipe.hpp @@ -8,11 +8,10 @@ #include "pipe_types.hpp" +#include #include -#include #include -#include #include #include @@ -62,8 +61,8 @@ class ServerPipe virtual ~ServerPipe() = default; - virtual int start(EventHandler event_handler) = 0; - virtual int sendMessage(const ClientId client_id, const Payloads payloads) = 0; + CETL_NODISCARD virtual int start(EventHandler event_handler) = 0; + CETL_NODISCARD virtual int send(const ClientId client_id, const Payloads payloads) = 0; protected: ServerPipe() = default; diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp index 6cb421c..21ac003 100644 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -9,6 +9,7 @@ #include "ocvsmd/platform/posix_utils.hpp" #include "pipe_types.hpp" +#include #include #include @@ -42,7 +43,7 @@ class UnixSocketBase UnixSocketBase() = default; ~UnixSocketBase() = default; - static int sendMessage(const int output_fd, const Payloads payloads) + CETL_NODISCARD static int send(const int output_fd, const Payloads payloads) { // 1. Write the message header (signature and total size of the following fragments). // @@ -79,7 +80,7 @@ class UnixSocketBase } template - static int receiveMessage(const int input_fd, Action&& action) + CETL_NODISCARD static int receiveMessage(const int input_fd, Action&& action) { // 1. Receive and validate the message header. // diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp index b734288..5df5056 100644 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -50,7 +50,7 @@ UnixSocketClient::~UnixSocketClient() } } -int UnixSocketClient::start(EventHandler event_handler) +CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(client_fd_ == -1, ""); CETL_DEBUG_ASSERT(event_handler, ""); diff --git a/src/common/ipc/pipe/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp index 5349a60..4c06655 100644 --- a/src/common/ipc/pipe/unix_socket_client.hpp +++ b/src/common/ipc/pipe/unix_socket_client.hpp @@ -11,6 +11,7 @@ #include "pipe_types.hpp" #include "unix_socket_base.hpp" +#include #include #include @@ -38,11 +39,11 @@ class UnixSocketClient final : public UnixSocketBase, public ClientPipe // ClientPipe - int start(EventHandler event_handler) override; + CETL_NODISCARD int start(EventHandler event_handler) override; - int sendMessage(const Payloads payloads) override + CETL_NODISCARD int send(const Payloads payloads) override { - return UnixSocketBase::sendMessage(client_fd_, payloads); + return UnixSocketBase::send(client_fd_, payloads); } private: diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index cc4da6f..794a681 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -39,7 +39,7 @@ constexpr int MaxConnections = 5; class ClientContextImpl final : public detail::ClientContext { public: - explicit ClientContextImpl(const UnixSocketServer::ClientId id, const int fd) + ClientContextImpl(const UnixSocketServer::ClientId id, const int fd) : id_{id} , fd_{fd} { @@ -99,7 +99,7 @@ UnixSocketServer::~UnixSocketServer() } } -int UnixSocketServer::start(EventHandler event_handler) +CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(server_fd_ == -1, ""); CETL_DEBUG_ASSERT(event_handler, ""); diff --git a/src/common/ipc/pipe/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp index b241c1f..5be3d57 100644 --- a/src/common/ipc/pipe/unix_socket_server.hpp +++ b/src/common/ipc/pipe/unix_socket_server.hpp @@ -11,6 +11,7 @@ #include "server_pipe.hpp" #include "unix_socket_base.hpp" +#include #include #include @@ -59,16 +60,16 @@ class UnixSocketServer final : public UnixSocketBase, public ServerPipe ~UnixSocketServer() override; - int start(EventHandler event_handler) override; + CETL_NODISCARD int start(EventHandler event_handler) override; - int sendMessage(const ClientId client_id, const Payloads payloads) override + CETL_NODISCARD int send(const ClientId client_id, const Payloads payloads) override { const auto id_and_fd = client_id_to_fd_.find(client_id); if (id_and_fd == client_id_to_fd_.end()) { return EINVAL; } - return UnixSocketBase::sendMessage(id_and_fd->second, payloads); + return UnixSocketBase::send(id_and_fd->second, payloads); } private: diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 9c66b2b..551c575 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -19,13 +19,13 @@ #include #include -#include #include #include #include #include #include #include +#include #include namespace ocvsmd @@ -40,7 +40,7 @@ namespace class ServerRouterImpl final : public ServerRouter { public: - explicit ServerRouterImpl(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe) + ServerRouterImpl(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe) : memory_{memory} , server_pipe_{std::move(server_pipe)} { @@ -49,14 +49,14 @@ class ServerRouterImpl final : public ServerRouter // ServerRouter - cetl::pmr::memory_resource& memory() override + CETL_NODISCARD cetl::pmr::memory_resource& memory() override { return memory_; } - void start() override + CETL_NODISCARD int start() override { - server_pipe_->start([this](const auto& pipe_event_var) { + return server_pipe_->start([this](const auto& pipe_event_var) { // return cetl::visit( [this](const auto& pipe_event) { @@ -85,26 +85,26 @@ class ServerRouterImpl final : public ServerRouter { } - Tag getTag() const noexcept + CETL_NODISCARD Tag getTag() const noexcept { return tag_; } - ClientId getClientId() const noexcept + CETL_NODISCARD ClientId getClientId() const noexcept { return client_id_; } // Hasher - bool operator==(const Endpoint& other) const noexcept + CETL_NODISCARD bool operator==(const Endpoint& other) const noexcept { return tag_ == other.tag_ && client_id_ == other.client_id_; } struct Hasher final { - std::size_t operator()(const Endpoint& endpoint) const noexcept + CETL_NODISCARD std::size_t operator()(const Endpoint& endpoint) const noexcept { const std::size_t h1 = std::hash{}(endpoint.tag_); const std::size_t h2 = std::hash{}(endpoint.client_id_); @@ -127,7 +127,7 @@ class ServerRouterImpl final : public ServerRouter }; public: - static std::shared_ptr create(ServerRouterImpl& router, const Endpoint& endpoint) + CETL_NODISCARD static std::shared_ptr create(ServerRouterImpl& router, const Endpoint& endpoint) { return std::make_shared(Private(), router, endpoint); } @@ -147,12 +147,19 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { - router_.unregisterGateway(endpoint_); + router_.unregisterGateway(endpoint_, true); ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); } - void send(const detail::ServiceId service_id, const pipe::Payload payload) override + // detail::Gateway + + CETL_NODISCARD int send(const detail::ServiceId service_id, const pipe::Payload payload) override { + if (!router_.isConnected(endpoint_)) + { + return ENOTCONN; + } + Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); @@ -160,10 +167,9 @@ class ServerRouterImpl final : public ServerRouter channel_msg.tag = endpoint_.getTag(); channel_msg.sequence = sequence_++; - tryPerformOnSerialized(route, [this, payload](const auto prefix) { + return tryPerformOnSerialized(route, [this, payload](const auto prefix) { // - std::array fragments{prefix, payload}; - return router_.server_pipe_->sendMessage(endpoint_.getClientId(), fragments); + return router_.server_pipe_->send(endpoint_.getClientId(), {{prefix, payload}}); }); } @@ -180,12 +186,12 @@ class ServerRouterImpl final : public ServerRouter if (event_handler) { event_handler_ = std::move(event_handler); - router_.registerGateway(endpoint_, shared_from_this()); + router_.registerGateway(endpoint_, *this); } else { event_handler_ = nullptr; - router_.unregisterGateway(endpoint_); + router_.unregisterGateway(endpoint_, false); } } @@ -200,23 +206,39 @@ class ServerRouterImpl final : public ServerRouter using ServiceIdToChannelFactory = std::unordered_map; using EndpointToWeakGateway = std::unordered_map; - void registerGateway(const Endpoint& endpoint, detail::Gateway::WeakPtr gateway) + CETL_NODISCARD bool isConnected(const Endpoint& endpoint) const noexcept { - endpoint_to_gateway_[endpoint] = std::move(gateway); + return connected_client_ids_.find(endpoint.getClientId()) != connected_client_ids_.end(); } - void unregisterGateway(const Endpoint& endpoint) + void registerGateway(const Endpoint& endpoint, GatewayImpl& gateway) + { + endpoint_to_gateway_[endpoint] = gateway.shared_from_this(); + if (isConnected(endpoint)) + { + gateway.event(detail::Gateway::Event::Connected{}); + } + } + + void unregisterGateway(const Endpoint& endpoint, const bool is_disposed = false) { endpoint_to_gateway_.erase(endpoint); + + // Notify "remote" router about the gateway disposal. + // The router will deliver "disconnected" event to the counterpart gateway (if it exists). + // + if (is_disposed && isConnected(endpoint)) + { + } } - static int handlePipeEvent(const pipe::ServerPipe::Event::Connected) + CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Connected conn) { - // TODO: Implement! + connected_client_ids_.insert(conn.client_id); return 0; } - int handlePipeEvent(const pipe::ServerPipe::Event::Message& msg) + CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Message& msg) { Route_1_0 route_msg{&memory_}; const auto result_size = tryDeserializePayload(msg.payload, route_msg); @@ -238,6 +260,10 @@ class ServerRouterImpl final : public ServerRouter [this, &msg, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { // handleRouteChannelMsg(msg.client_id, route_ch_msg, msg_payload); + }, + [this, &msg, msg_payload](const RouteChannelEnd_1_0& route_ch_end) { + // + handleRouteChannelEnd(msg.client_id, route_ch_end); }), route_msg.union_value); @@ -246,7 +272,7 @@ class ServerRouterImpl final : public ServerRouter return 0; } - static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected) + CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected) { // TODO: Implement! disconnected for all gateways which belong to the corresponding client id return 0; @@ -262,11 +288,11 @@ class ServerRouterImpl final : public ServerRouter route_conn.version.major = VERSION_MAJOR; route_conn.version.minor = VERSION_MINOR; - tryPerformOnSerialized(route, [this, client_id](const auto payload) { + const int result = tryPerformOnSerialized(route, [this, client_id](const auto payload) { // - std::array payloads{payload}; - return server_pipe_->sendMessage(client_id, payloads); + return server_pipe_->send(client_id, {{payload}}); }); + (void) result; } void handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, @@ -300,16 +326,20 @@ class ServerRouterImpl final : public ServerRouter // TODO: log unsolicited message } - cetl::pmr::memory_resource& memory_; - pipe::ServerPipe::Ptr server_pipe_; - EndpointToWeakGateway endpoint_to_gateway_; - ServiceIdToChannelFactory service_id_to_channel_factory_; + void handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end) {} + + cetl::pmr::memory_resource& memory_; + pipe::ServerPipe::Ptr server_pipe_; + EndpointToWeakGateway endpoint_to_gateway_; + ServiceIdToChannelFactory service_id_to_channel_factory_; + std::unordered_set connected_client_ids_; }; // ClientRouterImpl } // namespace -ServerRouter::Ptr ServerRouter::make(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe) +CETL_NODISCARD ServerRouter::Ptr ServerRouter::make(cetl::pmr::memory_resource& memory, + pipe::ServerPipe::Ptr server_pipe) { return std::make_unique(memory, std::move(server_pipe)); } diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index 4b74925..abbbd4c 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -28,7 +28,7 @@ class ServerRouter public: using Ptr = std::unique_ptr; - static Ptr make(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe); + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe); ServerRouter(const ServerRouter&) = delete; ServerRouter(ServerRouter&&) noexcept = delete; @@ -37,8 +37,8 @@ class ServerRouter virtual ~ServerRouter() = default; - virtual void start() = 0; - virtual cetl::pmr::memory_resource& memory() = 0; + CETL_NODISCARD virtual int start() = 0; + CETL_NODISCARD virtual cetl::pmr::memory_resource& memory() = 0; template using NewChannelHandler = std::function; diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index a48f58a..cfbdc24 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -73,18 +73,23 @@ cetl::optional Application::init() // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Client initial msg (%zu).", request.some_stuff.size()); - ch.send(request); + const int result = ch.send(request); + (void) result; ch.subscribe([this](const auto&) { // ::syslog(LOG_DEBUG, "Client nested msg"); - ExecCmd r1{&memory_}; - ipc_exec_cmd_ch_->send(r1); + ExecCmd r1{&memory_}; + const int res1 = ipc_exec_cmd_ch_->send(r1); + (void) res1; ipc_exec_cmd_ch_.reset(); }); ipc_exec_cmd_ch_ = std::move(ch); }); - ipc_router_->start(); + if (0 != ipc_router_->start()) + { + return "Failed to start IPC router."; + } return cetl::nullopt; } diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index d48b3dd..44a6ec3 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -12,6 +12,7 @@ #include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" +#include #include #include #include @@ -38,9 +39,15 @@ class DaemonImpl final : public Daemon ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); } - void start() + CETL_NODISCARD int start() { - ipc_router_->start(); + const int result = ipc_router_->start(); + if (result != 0) + { + // NOLINTNEXTLINE *-vararg + ::syslog(LOG_ERR, "Failed to start IPC router: %d", result); + return result; + } auto channel = ipc_router_->makeChannel("daemon"); channel.subscribe([this](const auto& event_var) { @@ -54,13 +61,15 @@ class DaemonImpl final : public Daemon ExecCmd cmd{&memory_}; cmd.some_stuff.push_back('A'); cmd.some_stuff.push_back('Z'); - ipc_exec_cmd_ch_->send(cmd); + const int result = ipc_exec_cmd_ch_->send(cmd); + (void) result; }, [this](const ExecCmdChannel::Input& input) { // // NOLINTNEXTLINE *-vararg ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", input.some_stuff.size()); - ipc_exec_cmd_ch_->send(input); + const int result = ipc_exec_cmd_ch_->send(input); + (void) result; }, [](const ExecCmdChannel::Disconnected&) { // @@ -70,6 +79,8 @@ class DaemonImpl final : public Daemon event_var); }); ipc_exec_cmd_ch_ = std::move(channel); + + return 0; } private: @@ -84,11 +95,16 @@ class DaemonImpl final : public Daemon } // namespace -std::unique_ptr Daemon::make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) +CETL_NODISCARD std::unique_ptr Daemon::make( // + cetl::pmr::memory_resource& memory, + libcyphal::IExecutor& executor) { auto daemon = std::make_unique(memory, executor); - daemon->start(); - return std::move(daemon); + if (daemon->start()) + { + return nullptr; + } + return daemon; } } // namespace sdk diff --git a/test/common/ipc/pipe/client_pipe_mock.hpp b/test/common/ipc/pipe/client_pipe_mock.hpp index e934ed0..5a7c014 100644 --- a/test/common/ipc/pipe/client_pipe_mock.hpp +++ b/test/common/ipc/pipe/client_pipe_mock.hpp @@ -36,16 +36,16 @@ class ClientPipeMock : public ClientPipe reference().event_handler_ = event_handler; return reference().start(event_handler); } - int sendMessage(const Payloads payloads) override + int send(const Payloads payloads) override { - return reference().sendMessage(payloads); + return reference().send(payloads); } }; // RefWrapper MOCK_METHOD(void, deinit, (), (const)); MOCK_METHOD(int, start, (EventHandler event_handler), (override)); - MOCK_METHOD(int, sendMessage, (const Payloads payloads), (override)); + MOCK_METHOD(int, send, (const Payloads payloads), (override)); // MARK: Data members: diff --git a/test/common/ipc/pipe/server_pipe_mock.hpp b/test/common/ipc/pipe/server_pipe_mock.hpp index 2a4a28b..ae39878 100644 --- a/test/common/ipc/pipe/server_pipe_mock.hpp +++ b/test/common/ipc/pipe/server_pipe_mock.hpp @@ -36,16 +36,17 @@ class ServerPipeMock : public ServerPipe reference().event_handler_ = event_handler; return reference().start(event_handler); } - int sendMessage(const ClientId client_id, const Payloads payloads) override + + int send(const ClientId client_id, const Payloads payloads) override { - return reference().sendMessage(client_id, payloads); + return reference().send(client_id, payloads); } }; // RefWrapper MOCK_METHOD(void, deinit, (), (const)); MOCK_METHOD(int, start, (EventHandler event_handler), (override)); - MOCK_METHOD(int, sendMessage, (const ClientId client_id, const Payloads payloads), (override)); + MOCK_METHOD(int, send, (const ClientId client_id, const Payloads payloads), (override)); // MARK: Data members: diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 1c9ac2e..70a0af7 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -8,6 +8,7 @@ #include "cetl_gtest_helpers.hpp" // NOLINT(misc-include-cleaner) #include "ipc/channel.hpp" #include "ipc/pipe/client_pipe.hpp" +#include "ipc/pipe/pipe_types.hpp" #include "pipe/client_pipe_mock.hpp" #include "tracking_memory_resource.hpp" @@ -21,6 +22,7 @@ #include #include +#include #include #include #include @@ -68,39 +70,54 @@ class TestClientRouter : public testing::Test Route_1_0 route{&mr_}; route.set_connect(connect); - tryPerformOnSerialized(route, [&](const auto payload) { + const int result = tryPerformOnSerialized(route, [&](const auto payload) { // action(payload); return 0; }); + EXPECT_THAT(result, 0); } - template - void withRouteChannelMsg(const cetl::string_view service_name, - const std::uint64_t tag, - const Message& message, - const std::uint64_t sequence, - Action action) + void emulateRouteConnect(pipe::ClientPipeMock& client_pipe_mock) + { + // client RouteConnect -> server + EXPECT_CALL(client_pipe_mock, send(_)).WillOnce(Return(0)); + client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Connected{}); + + // Server -> client RouteConnect + withRouteConnect(RouteConnect_1_0{{1, 2, &mr_}, &mr_}, [&](const auto payload) { + // + client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); + }); + } + + template + void emulateRouteChannelMsg(pipe::ClientPipeMock& client_pipe_mock, + const std::uint64_t tag, + const Msg& msg, + const std::uint64_t seq, + const cetl::string_view service_name = "") { using ocvsmd::common::tryPerformOnSerialized; Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.service_id = AnyChannel::getServiceId(service_name); + channel_msg.service_id = AnyChannel::getServiceId(service_name); channel_msg.tag = tag; - channel_msg.sequence = sequence; + channel_msg.sequence = seq; - tryPerformOnSerialized(route, [&](const auto prefix) { + const int result = tryPerformOnSerialized(route, [&](const auto prefix) { // - return tryPerformOnSerialized(message, [&](const auto suffix) { + return tryPerformOnSerialized(msg, [&](const auto suffix) { // std::vector buffer; std::copy(prefix.begin(), prefix.end(), std::back_inserter(buffer)); std::copy(suffix.begin(), suffix.end(), std::back_inserter(buffer)); - action(cetl::span{buffer.data(), buffer.size()}); - return 0; + const pipe::Payload payload{buffer.data(), buffer.size()}; + return client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); }); }); + EXPECT_THAT(result, 0); } // MARK: Data members: @@ -136,7 +153,7 @@ TEST_F(TestClientRouter, start) EXPECT_THAT(client_pipe_mock.event_handler_, IsFalse()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); - client_router->start(); + EXPECT_THAT(client_router->start(), 0); EXPECT_THAT(client_pipe_mock.event_handler_, IsTrue()); EXPECT_CALL(client_pipe_mock, deinit()).Times(1); @@ -156,7 +173,7 @@ TEST_F(TestClientRouter, makeChannel) ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); - client_router->start(); + EXPECT_THAT(client_router->start(), 0); const auto channel = client_router->makeChannel(); (void) channel; @@ -176,19 +193,17 @@ TEST_F(TestClientRouter, makeChannel_send) ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); - client_router->start(); + EXPECT_THAT(client_router->start(), 0); auto channel = client_router->makeChannel(); - Msg msg{&mr_}; + const Msg msg{&mr_}; + EXPECT_THAT(channel.send(msg), ENOTCONN); - EXPECT_CALL(client_pipe_mock, sendMessage(SizeIs(2))).WillOnce(Return(0)); - channel.send(msg); + emulateRouteConnect(client_pipe_mock); - msg.some_stuff.push_back(-1); - msg.some_stuff.push_back('X'); - EXPECT_CALL(client_pipe_mock, sendMessage(SizeIs(2))).WillOnce(Return(0)); - channel.send(msg); + EXPECT_CALL(client_pipe_mock, send(SizeIs(2))).WillOnce(Return(0)); + EXPECT_THAT(channel.send(msg), 0); } TEST_F(TestClientRouter, makeChannel_receive_events) @@ -205,7 +220,7 @@ TEST_F(TestClientRouter, makeChannel_receive_events) ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); - client_router->start(); + EXPECT_THAT(client_router->start(), 0); StrictMock> ch1_event_mock; StrictMock> ch2_event_mock; @@ -214,37 +229,23 @@ TEST_F(TestClientRouter, makeChannel_receive_events) channel1.subscribe(ch1_event_mock.AsStdFunction()); auto channel2 = client_router->makeChannel(); - channel2.subscribe(ch2_event_mock.AsStdFunction()); - - // Emulate that we've got pipe connected - `RouteConnect` should be sent to the server. - // - EXPECT_CALL(client_pipe_mock, sendMessage(_)).WillOnce(Return(0)); // RouteConnect -> server - client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Connected{}); - // Emulate that server responded with its `RouteConnect` - all channels should be notified. - // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + emulateRouteConnect(client_pipe_mock); + EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); - withRouteConnect(RouteConnect_1_0{{1, 2, &mr_}, &mr_}, [&](const auto payload) { - // - client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); - }); + channel2.subscribe(ch2_event_mock.AsStdFunction()); // Emulate that server posted `RouteChannelMsg` on tag #1. // - EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 1, Channel::Input{&mr_}, 0, [&](const auto payload) { - // - client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); - }); + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(2); + emulateRouteChannelMsg(client_pipe_mock, 1, Channel::Input{&mr_}, 0); + emulateRouteChannelMsg(client_pipe_mock, 1, Channel::Input{&mr_}, 1); // Emulate that server posted `RouteChannelMsg` on tag #2. // EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 2, Channel::Input{&mr_}, 0, [&](const auto payload) { - // - client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); - }); + emulateRouteChannelMsg(client_pipe_mock, 2, Channel::Input{&mr_}, 0); // Emulate that the pipe is disconnected - all channels should be notified. // diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 8e66425..92937f5 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -6,6 +6,7 @@ #include "ipc/server_router.hpp" #include "ipc/channel.hpp" +#include "ipc/pipe/pipe_types.hpp" #include "ipc/pipe/server_pipe.hpp" #include "pipe/server_pipe_mock.hpp" #include "tracking_memory_resource.hpp" @@ -54,32 +55,34 @@ class TestServerRouter : public testing::Test EXPECT_THAT(mr_.total_allocated_bytes, mr_.total_deallocated_bytes); } - template - void withRouteChannelMsg(const cetl::string_view service_name, - const std::uint64_t tag, - const Message& message, - const std::uint64_t sequence, - Action action) + template + void emulateRouteChannelMsg(pipe::ServerPipeMock& server_pipe_mock, + const pipe::ServerPipe::ClientId client_id, + const std::uint64_t tag, + const Msg& msg, + const std::uint64_t seq, + const cetl::string_view service_name = "") { using ocvsmd::common::tryPerformOnSerialized; Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.service_id = AnyChannel::getServiceId(service_name); + channel_msg.service_id = AnyChannel::getServiceId(service_name); channel_msg.tag = tag; - channel_msg.sequence = sequence; + channel_msg.sequence = seq; - tryPerformOnSerialized(route, [&](const auto prefix) { + const int result = tryPerformOnSerialized(route, [&](const auto prefix) { // - return tryPerformOnSerialized(message, [&](const auto suffix) { + return tryPerformOnSerialized(msg, [&](const auto suffix) { // std::vector buffer; std::copy(prefix.begin(), prefix.end(), std::back_inserter(buffer)); std::copy(suffix.begin(), suffix.end(), std::back_inserter(buffer)); - action(cetl::span{buffer.data(), buffer.size()}); - return 0; + const pipe::Payload payload{buffer.data(), buffer.size()}; + return server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{client_id, payload}); }); }); + EXPECT_THAT(result, 0); } // MARK: Data members: @@ -115,7 +118,7 @@ TEST_F(TestServerRouter, start) EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); EXPECT_CALL(server_pipe_mock, start(_)).Times(1); - server_router->start(); + EXPECT_THAT(server_router->start(), 0); EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); } @@ -134,7 +137,7 @@ TEST_F(TestServerRouter, registerChannel) EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); EXPECT_CALL(server_pipe_mock, start(_)).Times(1); - server_router->start(); + EXPECT_THAT(server_router->start(), 0); EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); server_router->registerChannel("", [](auto&&, const auto&) {}); @@ -155,7 +158,7 @@ TEST_F(TestServerRouter, channel_send) EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); EXPECT_CALL(server_pipe_mock, start(_)).Times(1); - server_router->start(); + EXPECT_THAT(server_router->start(), 0); EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); StrictMock> ch1_event_mock; @@ -164,26 +167,21 @@ TEST_F(TestServerRouter, channel_send) server_router->registerChannel("", [&](Channel&& ch, const auto& input) { // ch.subscribe(ch1_event_mock.AsStdFunction()); - maybe_channel.emplace(std::move(ch)); + maybe_channel = std::move(ch); ch1_event_mock.Call(input); }); EXPECT_THAT(maybe_channel.has_value(), IsFalse()); - // Emulate that client posted initial `RouteChannelMsg` on 1/42 tag/client pair. + // Emulate that client posted initial `RouteChannelMsg` on 42/1 client/tag pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 1, Channel::Input{&mr_}, 0, [&](const auto payload) { - // - server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); - }); + emulateRouteChannelMsg(server_pipe_mock, 42, 1, Channel::Input{&mr_}, 0); + EXPECT_THAT(maybe_channel.has_value(), IsTrue()); - // Emulate that client posted one more `RouteChannelMsg` on the same 1/42 tag/client pair. + // Emulate that client posted one more `RouteChannelMsg` on the same 42/1 client/tag pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - withRouteChannelMsg("", 1, Channel::Input{&mr_}, 1, [&](const auto payload) { - // - server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{42, payload}); - }); + emulateRouteChannelMsg(server_pipe_mock, 42, 1, Channel::Input{&mr_}, 1); } // NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) From d63bb58e6a30839ec7e3c84bb28df6a3553c5afe Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 8 Jan 2025 18:10:34 +0200 Subject: [PATCH 036/156] minor fix --- src/common/ipc/client_router.cpp | 5 +++-- src/daemon/engine/application.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 4ab1579..695adba 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -222,8 +222,9 @@ class ClientRouterImpl final : public ClientRouter if (is_disposed && isConnected(endpoint)) { Route_1_0 route{&memory_}; - auto& channel_end = route.set_channel_end(); - // channel_end.version.minor = VERSION_MINOR; + auto& channel_end = route.set_channel_end(); + channel_end.tag = endpoint.getTag(); + channel_end.error_code = 0; const int result = tryPerformOnSerialized(route, [this](const auto payload) { // diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index cfbdc24..c5b8a34 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -75,7 +75,8 @@ cetl::optional Application::init() ::syslog(LOG_DEBUG, "Client initial msg (%zu).", request.some_stuff.size()); const int result = ch.send(request); (void) result; - ch.subscribe([this](const auto&) { + ipc_exec_cmd_ch_ = std::move(ch); + ipc_exec_cmd_ch_->subscribe([this](const auto&) { // ::syslog(LOG_DEBUG, "Client nested msg"); ExecCmd r1{&memory_}; @@ -83,7 +84,6 @@ cetl::optional Application::init() (void) res1; ipc_exec_cmd_ch_.reset(); }); - ipc_exec_cmd_ch_ = std::move(ch); }); if (0 != ipc_router_->start()) From 7f51127c2ad8f5898cb21b3cd790360600dc9c8d Mon Sep 17 00:00:00 2001 From: Sergei Date: Thu, 9 Jan 2025 03:05:58 +0200 Subject: [PATCH 037/156] added various gtest matcher and printers --- .../common/ipc/RouteChannelMsg.1.0.dsdl | 2 +- src/common/ipc/client_router.cpp | 2 +- src/common/ipc/server_router.cpp | 2 +- test/common/ipc/ipc_gtest_helpers.hpp | 193 ++++++++++++++++++ test/common/ipc/test_client_router.cpp | 17 +- test/common/ipc/test_server_router.cpp | 31 ++- 6 files changed, 233 insertions(+), 14 deletions(-) create mode 100644 test/common/ipc/ipc_gtest_helpers.hpp diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl index 47d7a02..0b6df88 100644 --- a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl @@ -1,6 +1,6 @@ -uint64 service_id uint64 tag uint64 sequence +uint64 service_id # reserve twice as much as we need. @extent _offset_.max * 2 diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 695adba..74453fe 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -156,9 +156,9 @@ class ClientRouterImpl final : public ClientRouter Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.service_id = service_id; channel_msg.tag = endpoint_.getTag(); channel_msg.sequence = sequence_++; + channel_msg.service_id = service_id; return tryPerformOnSerialized(route, [this, payload](const auto prefix) { // diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 551c575..9a4e26e 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -163,9 +163,9 @@ class ServerRouterImpl final : public ServerRouter Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.service_id = service_id; channel_msg.tag = endpoint_.getTag(); channel_msg.sequence = sequence_++; + channel_msg.service_id = service_id; return tryPerformOnSerialized(route, [this, payload](const auto prefix) { // diff --git a/test/common/ipc/ipc_gtest_helpers.hpp b/test/common/ipc/ipc_gtest_helpers.hpp new file mode 100644 index 0000000..814d0f2 --- /dev/null +++ b/test/common/ipc/ipc_gtest_helpers.hpp @@ -0,0 +1,193 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_GTEST_HELPERS_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_GTEST_HELPERS_HPP_INCLUDED + +#include "dsdl_helpers.hpp" +#include "ipc/pipe/pipe_types.hpp" + +#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/Route_1_0.hpp" + +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include + +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +// MARK: - GTest Printers: + +inline void PrintTo(const uavcan::primitive::Empty_1_0&, std::ostream* os) +{ + *os << "Empty_1_0"; +} + +inline void PrintTo(const uavcan::node::Version_1_0& ver, std::ostream* os) +{ + *os << "Version_1_0{'" << static_cast(ver.major) << "." << static_cast(ver.minor) << "'}"; +} + +inline void PrintTo(const RouteConnect_1_0& conn, std::ostream* os) +{ + *os << "RouteConnect_1_0{ver="; + PrintTo(conn.version, os); + *os << "}"; +} + +inline void PrintTo(const RouteChannelMsg_1_0& msg, std::ostream* os) +{ + *os << "RouteChannelMsg_1_0{tag=" << msg.tag << ", seq=" << msg.sequence << ", srv=0x" << std::hex << msg.service_id + << "}"; +} + +inline void PrintTo(const RouteChannelEnd_1_0& msg, std::ostream* os) +{ + *os << "RouteChannelEnd_1_0{tag=" << msg.tag << ", err=" << msg.error_code << "}"; +} + +inline void PrintTo(const Route_1_0& route, std::ostream* os) +{ + *os << "Route_1_0{"; + cetl::visit([os](const auto& v) { PrintTo(v, os); }, route.union_value); + *os << "}"; +} + +// MARK: - Equitable-s for matching: + +inline bool operator==(const RouteConnect_1_0& lhs, const RouteConnect_1_0& rhs) +{ + return lhs.version.major == rhs.version.major && lhs.version.minor == rhs.version.minor; +} + +inline bool operator==(const RouteChannelMsg_1_0& lhs, const RouteChannelMsg_1_0& rhs) +{ + return lhs.tag == rhs.tag && lhs.sequence == rhs.sequence && lhs.service_id == rhs.service_id; +} + +inline bool operator==(const RouteChannelEnd_1_0& lhs, const RouteChannelEnd_1_0& rhs) +{ + return lhs.tag == rhs.tag && lhs.error_code == rhs.error_code; +} + +// MARK: - GTest Matchers: + +template +class PayloadMatcher +{ +public: + explicit PayloadMatcher(testing::Matcher matcher, + cetl::pmr::memory_resource& memory) + : matcher_(std::move(matcher)) + , memory_{memory} + + { + } + + bool MatchAndExplain(const pipe::Payload& payload, testing::MatchResultListener* listener) const + { + T msg{&memory_}; + const auto result = tryDeserializePayload(payload, msg); + if (!result) + { + if (listener->IsInterested()) + { + *listener << "Failed to deserialize the payload."; + } + return false; + } + + const bool match = matcher_.MatchAndExplain(msg.union_value, listener); + if (!match && listener->IsInterested()) + { + *listener << ".\n Payload: "; + *listener << testing::PrintToString(msg); + } + return match; + } + + bool MatchAndExplain(const pipe::Payloads& payloads, testing::MatchResultListener* listener) const + { + std::vector flatten; + for (const auto& payload : payloads) + { + flatten.insert(flatten.end(), payload.begin(), payload.end()); + } + return MatchAndExplain({flatten.data(), flatten.size()}, listener); + } + + void DescribeTo(std::ostream* os) const + { + *os << "is a variant<> with value of type '" << "GetTypeName()" + << "' and the value "; + matcher_.DescribeTo(os); + } + + void DescribeNegationTo(std::ostream* os) const + { + *os << "is a variant<> with value of type other than '" << "GetTypeName()" + << "' or the value "; + matcher_.DescribeNegationTo(os); + } + +private: + const testing::Matcher matcher_; + cetl::pmr::memory_resource& memory_; + +}; // PayloadMatcher + +template +testing::PolymorphicMatcher> PayloadWith( + + const testing::Matcher& matcher, + cetl::pmr::memory_resource& memory) +{ + return testing::MakePolymorphicMatcher(PayloadMatcher(matcher, memory)); +} + +inline auto PayloadRouteConnectEq(cetl::pmr::memory_resource& mr, + const std::uint8_t ver_major = VERSION_MAJOR, + const std::uint8_t ver_minor = VERSION_MINOR) +{ + const RouteConnect_1_0 connect{{ver_major, ver_minor, &mr}, &mr}; + return PayloadWith(testing::VariantWith(connect), mr); +} + +template +auto PayloadOfRouteChannel(cetl::pmr::memory_resource& mr, + const std::uint64_t tag, + const std::uint64_t seq, + const cetl::string_view srv_name = "") +{ + const RouteChannelMsg_1_0 msg{tag, seq, AnyChannel::getServiceId(srv_name), &mr}; + return PayloadWith(testing::VariantWith(msg), mr); +} + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +#endif // OCVSMD_COMMON_IPC_GTEST_HELPERS_HPP_INCLUDED diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 70a0af7..46a5be8 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -9,15 +9,17 @@ #include "ipc/channel.hpp" #include "ipc/pipe/client_pipe.hpp" #include "ipc/pipe/pipe_types.hpp" +#include "ipc_gtest_helpers.hpp" #include "pipe/client_pipe_mock.hpp" #include "tracking_memory_resource.hpp" +#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" #include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" #include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" #include -#include #include #include @@ -26,7 +28,6 @@ #include #include #include -#include #include namespace @@ -81,7 +82,7 @@ class TestClientRouter : public testing::Test void emulateRouteConnect(pipe::ClientPipeMock& client_pipe_mock) { // client RouteConnect -> server - EXPECT_CALL(client_pipe_mock, send(_)).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, send(PayloadRouteConnectEq(mr_))).WillOnce(Return(0)); client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Connected{}); // Server -> client RouteConnect @@ -102,9 +103,9 @@ class TestClientRouter : public testing::Test Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.service_id = AnyChannel::getServiceId(service_name); channel_msg.tag = tag; channel_msg.sequence = seq; + channel_msg.service_id = AnyChannel::getServiceId(service_name); const int result = tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -202,8 +203,14 @@ TEST_F(TestClientRouter, makeChannel_send) emulateRouteConnect(client_pipe_mock); - EXPECT_CALL(client_pipe_mock, send(SizeIs(2))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, 1, 0))).WillOnce(Return(0)); EXPECT_THAT(channel.send(msg), 0); + + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, 1, 1))).WillOnce(Return(0)); + EXPECT_THAT(channel.send(msg), 0); + + EXPECT_CALL(client_pipe_mock, send(PayloadWith(VariantWith(_), mr_))) + .WillOnce(Return(0)); } TEST_F(TestClientRouter, makeChannel_receive_events) diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 92937f5..eab8fdf 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -8,9 +8,11 @@ #include "ipc/channel.hpp" #include "ipc/pipe/pipe_types.hpp" #include "ipc/pipe/server_pipe.hpp" +#include "ipc_gtest_helpers.hpp" #include "pipe/server_pipe_mock.hpp" #include "tracking_memory_resource.hpp" +#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" #include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" @@ -32,6 +34,7 @@ using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in th using testing::_; using testing::IsTrue; +using testing::Return; using testing::IsEmpty; using testing::IsFalse; using testing::NotNull; @@ -55,9 +58,14 @@ class TestServerRouter : public testing::Test EXPECT_THAT(mr_.total_allocated_bytes, mr_.total_deallocated_bytes); } + static void emulatePipeConnect(const pipe::ServerPipe::ClientId client_id, pipe::ServerPipeMock& server_pipe_mock) + { + server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Connected{client_id}); + } + template - void emulateRouteChannelMsg(pipe::ServerPipeMock& server_pipe_mock, - const pipe::ServerPipe::ClientId client_id, + void emulateRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, + pipe::ServerPipeMock& server_pipe_mock, const std::uint64_t tag, const Msg& msg, const std::uint64_t seq, @@ -67,9 +75,9 @@ class TestServerRouter : public testing::Test Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.service_id = AnyChannel::getServiceId(service_name); channel_msg.tag = tag; channel_msg.sequence = seq; + channel_msg.service_id = AnyChannel::getServiceId(service_name); const int result = tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -172,16 +180,27 @@ TEST_F(TestServerRouter, channel_send) }); EXPECT_THAT(maybe_channel.has_value(), IsFalse()); + // Emulate that client #42 is connected. + // + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + emulatePipeConnect(42, server_pipe_mock); + // Emulate that client posted initial `RouteChannelMsg` on 42/1 client/tag pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - emulateRouteChannelMsg(server_pipe_mock, 42, 1, Channel::Input{&mr_}, 0); - EXPECT_THAT(maybe_channel.has_value(), IsTrue()); + emulateRouteChannelMsg(42, server_pipe_mock, 1, Channel::Input{&mr_}, 0); + ASSERT_THAT(maybe_channel.has_value(), IsTrue()); // Emulate that client posted one more `RouteChannelMsg` on the same 42/1 client/tag pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - emulateRouteChannelMsg(server_pipe_mock, 42, 1, Channel::Input{&mr_}, 1); + emulateRouteChannelMsg(42, server_pipe_mock, 1, Channel::Input{&mr_}, 1); + + EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, 1, 0))).WillOnce(Return(0)); + EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); + + EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, 1, 1))).WillOnce(Return(0)); + EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); } // NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) From 672e197a88d14658eccd02cba63b676df95b3142 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 9 Jan 2025 16:06:22 +0200 Subject: [PATCH 038/156] introduce `ErrorCode` --- src/common/ipc/channel.hpp | 17 ++- src/common/ipc/client_router.cpp | 169 ++++++++++++--------- src/common/ipc/gateway.hpp | 21 +-- src/common/ipc/ipc_types.hpp | 40 +++++ src/common/ipc/pipe/client_pipe.hpp | 2 +- src/common/ipc/pipe/pipe_types.hpp | 30 ---- src/common/ipc/pipe/server_pipe.hpp | 2 +- src/common/ipc/pipe/unix_socket_base.hpp | 2 +- src/common/ipc/pipe/unix_socket_client.hpp | 2 +- src/common/ipc/pipe/unix_socket_server.hpp | 2 +- src/common/ipc/server_router.cpp | 14 +- src/common/ipc/server_router.hpp | 6 +- src/sdk/daemon.cpp | 8 +- test/common/ipc/ipc_gtest_helpers.hpp | 12 +- test/common/ipc/pipe/client_pipe_mock.hpp | 2 +- test/common/ipc/pipe/server_pipe_mock.hpp | 2 +- test/common/ipc/test_client_router.cpp | 33 ++-- test/common/ipc/test_server_router.cpp | 19 ++- 18 files changed, 218 insertions(+), 165 deletions(-) create mode 100644 src/common/ipc/ipc_types.hpp delete mode 100644 src/common/ipc/pipe/pipe_types.hpp diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 69f076e..8b4f76d 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -8,13 +8,13 @@ #include "dsdl_helpers.hpp" #include "gateway.hpp" - -#include +#include "ipc_types.hpp" #include #include #include +#include #include #include #include @@ -32,8 +32,11 @@ class AnyChannel struct Connected final {}; - struct Disconnected final - {}; + struct Completed final + { + /// Channel completion error code. Zero means success. + ErrorCode error_code; + }; /// Builds a service ID from either the service name (if not empty), or message type name. /// @@ -60,7 +63,7 @@ class Channel final : public AnyChannel using Input = Input_; using Output = Output_; - using EventVar = cetl::variant; + using EventVar = cetl::variant; using EventHandler = std::function; ~Channel() = default; @@ -124,9 +127,9 @@ class Channel final : public AnyChannel } } - void operator()(const GatewayEvent::Disconnected&) const + void operator()(const GatewayEvent::Completed& completed) const { - ch_event_handler(Disconnected{}); + ch_event_handler(Completed{completed.error_code}); } }; // Adapter diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 74453fe..5de3a03 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -7,8 +7,8 @@ #include "dsdl_helpers.hpp" #include "gateway.hpp" +#include "ipc_types.hpp" #include "pipe/client_pipe.hpp" -#include "pipe/pipe_types.hpp" #include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" @@ -44,7 +44,7 @@ class ClientRouterImpl final : public ClientRouter ClientRouterImpl(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) : memory_{memory} , client_pipe_{std::move(client_pipe)} - , last_unique_tag_{0} + , next_tag_{0} , is_connected_{false} { CETL_DEBUG_ASSERT(client_pipe_, ""); @@ -72,8 +72,11 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD detail::Gateway::Ptr makeGateway() override { - const Endpoint endpoint{++last_unique_tag_}; - return GatewayImpl::create(*this, endpoint); + const Endpoint endpoint{next_tag_++}; + + auto gateway = GatewayImpl::create(*this, endpoint); + endpoint_to_gateway_[endpoint] = gateway; + return gateway; } private: @@ -128,7 +131,7 @@ class ClientRouterImpl final : public ClientRouter GatewayImpl(Private, ClientRouterImpl& router, const Endpoint& endpoint) : router_{router} , endpoint_{endpoint} - , sequence_{0} + , next_sequence_{0} { ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag()); } @@ -140,24 +143,27 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { - router_.unregisterGateway(endpoint_, true); - ::syslog(LOG_DEBUG, "~Gateway(tag=%zu).", endpoint_.getTag()); + ::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.getTag(), next_sequence_); + + // `next_sequence_ == 0` means that this gateway was never used for sending messages, + // and so remote router never knew about it (its tag) - no need to post "ChEnd" event. + router_.onGatewayDisposal(endpoint_, next_sequence_ > 0); } // detail::Gateway - CETL_NODISCARD int send(const detail::ServiceId service_id, const pipe::Payload payload) override + CETL_NODISCARD int send(const detail::ServiceId service_id, const Payload payload) override { if (!router_.is_connected_) { - return ENOTCONN; + return static_cast(ErrorCode::NotConnected); } Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = endpoint_.getTag(); - channel_msg.sequence = sequence_++; + channel_msg.sequence = next_sequence_++; channel_msg.service_id = service_id; return tryPerformOnSerialized(route, [this, payload](const auto prefix) { @@ -176,22 +182,14 @@ class ClientRouterImpl final : public ClientRouter void subscribe(EventHandler event_handler) override { - if (event_handler) - { - event_handler_ = std::move(event_handler); - router_.registerGateway(endpoint_, *this); - } - else - { - event_handler_ = nullptr; - router_.unregisterGateway(endpoint_); - } + event_handler_ = std::move(event_handler); + router_.onGatewaySubscription(endpoint_); } private: ClientRouterImpl& router_; const Endpoint endpoint_; - std::uint64_t sequence_; + std::uint64_t next_sequence_; EventHandler event_handler_; }; // GatewayImpl @@ -203,60 +201,83 @@ class ClientRouterImpl final : public ClientRouter return is_connected_; } - void registerGateway(const Endpoint& endpoint, GatewayImpl& gateway) + template + void findRegisteredGateway(const Endpoint endpoint, Action&& action) + { + const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); + if (ep_to_gw != endpoint_to_gateway_.end()) + { + const auto gateway = ep_to_gw->second.lock(); + if (gateway) + { + std::forward(action)(*gateway, ep_to_gw); + } + } + } + + template + void forEachRegisteredGateway(Action action) + { + // Calling an action might indirectly modify the map, so we first + // collect strong pointers to gateways into a local collection. + // + std::vector gateway_ptrs; + gateway_ptrs.reserve(endpoint_to_gateway_.size()); + for (const auto& ep_to_gw : endpoint_to_gateway_) + { + const auto gateway_ptr = ep_to_gw.second.lock(); + if (gateway_ptr) + { + gateway_ptrs.push_back(gateway_ptr); + } + } + + for (const auto& gateway_ptr : gateway_ptrs) + { + action(*gateway_ptr); + } + } + + void onGatewaySubscription(const Endpoint endpoint) { - endpoint_to_gateway_[endpoint] = gateway.shared_from_this(); if (is_connected_) { - gateway.event(detail::Gateway::Event::Connected{}); + findRegisteredGateway(endpoint, [](auto& gateway, auto) { + // + gateway.event(detail::Gateway::Event::Connected{}); + }); } } - void unregisterGateway(const Endpoint& endpoint, const bool is_disposed = false) + /// Unregisters the gateway associated with the given endpoint. + /// + /// Called on the gateway disposal (correspondingly on its channel destruction). + /// The "dying" gateway might wish to notify the remote router about its disposal. + /// The router fulfills the wish if the gateway was registered and the router is connected. + /// + void onGatewayDisposal(const Endpoint& endpoint, const bool send_ch_end) { - endpoint_to_gateway_.erase(endpoint); + const bool was_registered = (endpoint_to_gateway_.erase(endpoint) > 0); - // Notify "remote" router about the gateway disposal. - // The router will deliver "disconnected" event to the counterpart gateway (if it exists). + // Notify "remote" router about the gateway disposal (aka channel completion). + // The router will propagate "ChEnd" event to the counterpart gateway (if it's registered). // - if (is_disposed && isConnected(endpoint)) + if (was_registered && send_ch_end && isConnected(endpoint)) { Route_1_0 route{&memory_}; auto& channel_end = route.set_channel_end(); channel_end.tag = endpoint.getTag(); - channel_end.error_code = 0; + channel_end.error_code = 0; // No error b/c it's a normal channel completion. const int result = tryPerformOnSerialized(route, [this](const auto payload) { // return client_pipe_->send({{payload}}); }); + // Best efforts strategy - gateway anyway is gone, so nowhere to report. (void) result; } } - template - void forEachGateway(Action action) const - { - // Calling an action might indirectly modify the map, so we first - // collect strong pointers to gateways into a local collection. - // - std::vector gateways; - gateways.reserve(endpoint_to_gateway_.size()); - for (const auto& ep_to_gw : endpoint_to_gateway_) - { - const auto gateway = ep_to_gw.second.lock(); - if (gateway) - { - gateways.push_back(gateway); - } - } - - for (const auto& gateway : gateways) - { - action(gateway); - } - } - CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Connected) const { // TODO: log client pipe connection @@ -295,7 +316,7 @@ class ClientRouterImpl final : public ClientRouter // handleRouteChannelMsg(route_ch_msg, msg_payload); }, - [this, msg_payload](const RouteChannelEnd_1_0& route_ch_end) { + [this](const RouteChannelEnd_1_0& route_ch_end) { // handleRouteChannelEnd(route_ch_end); }), @@ -306,40 +327,42 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) { - // TODO: log client pipe disconnection - if (is_connected_) { is_connected_ = false; - // The whole router is disconnected, so we need to notify all local gateways. + // The whole router is disconnected, so we need to unregister and notify all gateways. // - forEachGateway([](const auto& gateway) { - // - gateway->event(detail::Gateway::Event::Disconnected{}); - }); + EndpointToWeakGateway local_gateways; + std::swap(local_gateways, endpoint_to_gateway_); + for (const auto& ep_to_gw : local_gateways) + { + const auto gateway = ep_to_gw.second.lock(); + if (gateway) + { + gateway->event(detail::Gateway::Event::Completed{ErrorCode::Disconnected}); + } + } } return 0; } void handleRouteConnect(const RouteConnect_1_0&) { - // TODO: log server route connection - if (!is_connected_) { is_connected_ = true; // We've got connection response from the server, so we need to notify all local gateways. // - forEachGateway([](const auto& gateway) { + forEachRegisteredGateway([](auto& gateway) { // - gateway->event(detail::Gateway::Event::Connected{}); + gateway.event(detail::Gateway::Event::Connected{}); }); } } - void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_ch_msg, pipe::Payload payload) + void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_ch_msg, const Payload payload) { const Endpoint endpoint{route_ch_msg.tag}; @@ -357,11 +380,21 @@ class ClientRouterImpl final : public ClientRouter // TODO: log unsolicited message } - void handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end) {} + void handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end) + { + const Endpoint endpoint{route_ch_end.tag}; + const auto error_code = static_cast(route_ch_end.error_code); + + findRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto it) { + // + endpoint_to_gateway_.erase(it); + gateway.event(detail::Gateway::Event::Completed{error_code}); + }); + } cetl::pmr::memory_resource& memory_; pipe::ClientPipe::Ptr client_pipe_; - Endpoint::Tag last_unique_tag_; + Endpoint::Tag next_tag_; EndpointToWeakGateway endpoint_to_gateway_; bool is_connected_; diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index bf933f3..671a945 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -6,7 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED #define OCVSMD_COMMON_IPC_GATEWAY_HPP_INCLUDED -#include "pipe/pipe_types.hpp" +#include "ipc_types.hpp" #include #include @@ -37,16 +37,17 @@ class Gateway { struct Connected final {}; - struct Disconnected final - {}; + struct Completed final + { + ErrorCode error_code; + }; struct Message final { std::uint64_t sequence; - pipe::Payload payload; - - }; // Message + Payload payload; + }; - using Var = cetl::variant; + using Var = cetl::variant; }; // Event @@ -57,9 +58,9 @@ class Gateway Gateway& operator=(const Gateway&) = delete; Gateway& operator=(Gateway&&) noexcept = delete; - CETL_NODISCARD virtual int send(const ServiceId service_id, const pipe::Payload payload) = 0; - virtual void event(const Event::Var& event) = 0; - virtual void subscribe(EventHandler event_handler) = 0; + CETL_NODISCARD virtual int send(const ServiceId service_id, const Payload payload) = 0; + virtual void event(const Event::Var& event) = 0; + virtual void subscribe(EventHandler event_handler) = 0; protected: Gateway() = default; diff --git a/src/common/ipc/ipc_types.hpp b/src/common/ipc/ipc_types.hpp new file mode 100644 index 0000000..6fd92c4 --- /dev/null +++ b/src/common/ipc/ipc_types.hpp @@ -0,0 +1,40 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_TYPES_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_TYPES_HPP_INCLUDED + +#include + +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +/// Defines some common error codes of IPC operations. +/// +/// Maps to `errno` values, hence `int` inheritance and zero on success. +/// +enum class ErrorCode : int // NOLINT +{ + Success = 0, + NotConnected = ENOTCONN, + Disconnected = ESHUTDOWN, + +}; // ErrorCode + +using Payload = cetl::span; +using Payloads = cetl::span; + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_TYPES_HPP_INCLUDED diff --git a/src/common/ipc/pipe/client_pipe.hpp b/src/common/ipc/pipe/client_pipe.hpp index 78cb76e..9e48b7a 100644 --- a/src/common/ipc/pipe/client_pipe.hpp +++ b/src/common/ipc/pipe/client_pipe.hpp @@ -6,7 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_CLIENT_PIPE_HPP_INCLUDED -#include "pipe_types.hpp" +#include "ipc/ipc_types.hpp" #include #include diff --git a/src/common/ipc/pipe/pipe_types.hpp b/src/common/ipc/pipe/pipe_types.hpp deleted file mode 100644 index ba7a672..0000000 --- a/src/common/ipc/pipe/pipe_types.hpp +++ /dev/null @@ -1,30 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#ifndef OCVSMD_COMMON_IPC_PIPE_TYPES_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_TYPES_HPP_INCLUDED - -#include - -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -using Payload = cetl::span; -using Payloads = cetl::span; - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd - -#endif // OCVSMD_COMMON_IPC_PIPE_TYPES_HPP_INCLUDED diff --git a/src/common/ipc/pipe/server_pipe.hpp b/src/common/ipc/pipe/server_pipe.hpp index 2c352a8..1346f26 100644 --- a/src/common/ipc/pipe/server_pipe.hpp +++ b/src/common/ipc/pipe/server_pipe.hpp @@ -6,7 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED -#include "pipe_types.hpp" +#include "ipc/ipc_types.hpp" #include #include diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp index 21ac003..423e361 100644 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -6,8 +6,8 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED +#include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_utils.hpp" -#include "pipe_types.hpp" #include #include diff --git a/src/common/ipc/pipe/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp index 4c06655..26f1e27 100644 --- a/src/common/ipc/pipe/unix_socket_client.hpp +++ b/src/common/ipc/pipe/unix_socket_client.hpp @@ -7,8 +7,8 @@ #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED #include "client_pipe.hpp" +#include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" -#include "pipe_types.hpp" #include "unix_socket_base.hpp" #include diff --git a/src/common/ipc/pipe/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp index 5be3d57..57e4a4f 100644 --- a/src/common/ipc/pipe/unix_socket_server.hpp +++ b/src/common/ipc/pipe/unix_socket_server.hpp @@ -6,8 +6,8 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED +#include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" -#include "pipe_types.hpp" #include "server_pipe.hpp" #include "unix_socket_base.hpp" diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 9a4e26e..7bf638e 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -7,7 +7,7 @@ #include "dsdl_helpers.hpp" #include "gateway.hpp" -#include "pipe/pipe_types.hpp" +#include "ipc_types.hpp" #include "pipe/server_pipe.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" @@ -135,7 +135,7 @@ class ServerRouterImpl final : public ServerRouter GatewayImpl(Private, ServerRouterImpl& router, const Endpoint& endpoint) : router_{router} , endpoint_{endpoint} - , sequence_{0} + , next_sequence_{0} { ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag()); } @@ -153,18 +153,18 @@ class ServerRouterImpl final : public ServerRouter // detail::Gateway - CETL_NODISCARD int send(const detail::ServiceId service_id, const pipe::Payload payload) override + CETL_NODISCARD int send(const detail::ServiceId service_id, const Payload payload) override { if (!router_.isConnected(endpoint_)) { - return ENOTCONN; + return static_cast(ErrorCode::NotConnected); } Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = endpoint_.getTag(); - channel_msg.sequence = sequence_++; + channel_msg.sequence = next_sequence_++; channel_msg.service_id = service_id; return tryPerformOnSerialized(route, [this, payload](const auto prefix) { @@ -198,7 +198,7 @@ class ServerRouterImpl final : public ServerRouter private: ServerRouterImpl& router_; const Endpoint endpoint_; - std::uint64_t sequence_; + std::uint64_t next_sequence_; EventHandler event_handler_; }; // GatewayImpl @@ -297,7 +297,7 @@ class ServerRouterImpl final : public ServerRouter void handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, const RouteChannelMsg_1_0& route_ch_msg, - pipe::Payload msg_payload) + const Payload msg_payload) { const Endpoint endpoint{route_ch_msg.tag, client_id}; diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index abbbd4c..326252f 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -8,7 +8,7 @@ #include "channel.hpp" #include "gateway.hpp" -#include "pipe/pipe_types.hpp" +#include "ipc_types.hpp" #include "pipe/server_pipe.hpp" #include @@ -53,7 +53,7 @@ class ServerRouter registerChannelFactory( // service_id, [this, service_id, new_ch_handler = std::move(handler)](detail::Gateway::Ptr gateway, - const pipe::Payload payload) { + const Payload payload) { typename Ch::Input input{&memory()}; if (tryDeserializePayload(payload, input)) { @@ -63,7 +63,7 @@ class ServerRouter } protected: - using TypeErasedChannelFactory = std::function; + using TypeErasedChannelFactory = std::function; ServerRouter() = default; diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 44a6ec3..8a696d2 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -57,7 +57,7 @@ class DaemonImpl final : public Daemon [this](const ExecCmdChannel::Connected&) { // // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Ch connected."); + ::syslog(LOG_DEBUG, "🟢 Ch connected."); ExecCmd cmd{&memory_}; cmd.some_stuff.push_back('A'); cmd.some_stuff.push_back('Z'); @@ -67,14 +67,14 @@ class DaemonImpl final : public Daemon [this](const ExecCmdChannel::Input& input) { // // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Server msg (%zu bytes).", input.some_stuff.size()); + ::syslog(LOG_DEBUG, "🔵 Ch Msg (%zu bytes).", input.some_stuff.size()); const int result = ipc_exec_cmd_ch_->send(input); (void) result; }, - [](const ExecCmdChannel::Disconnected&) { + [](const ExecCmdChannel::Completed& completed) { // // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Server disconnected."); + ::syslog(LOG_DEBUG, "🔴 Ch Completed (err=%d).", completed.error_code); }), event_var); }); diff --git a/test/common/ipc/ipc_gtest_helpers.hpp b/test/common/ipc/ipc_gtest_helpers.hpp index 814d0f2..68dfd59 100644 --- a/test/common/ipc/ipc_gtest_helpers.hpp +++ b/test/common/ipc/ipc_gtest_helpers.hpp @@ -7,7 +7,7 @@ #define OCVSMD_COMMON_IPC_GTEST_HELPERS_HPP_INCLUDED #include "dsdl_helpers.hpp" -#include "ipc/pipe/pipe_types.hpp" +#include "ipc/ipc_types.hpp" #include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" @@ -105,7 +105,7 @@ class PayloadMatcher { } - bool MatchAndExplain(const pipe::Payload& payload, testing::MatchResultListener* listener) const + bool MatchAndExplain(const Payload& payload, testing::MatchResultListener* listener) const { T msg{&memory_}; const auto result = tryDeserializePayload(payload, msg); @@ -127,7 +127,7 @@ class PayloadMatcher return match; } - bool MatchAndExplain(const pipe::Payloads& payloads, testing::MatchResultListener* listener) const + bool MatchAndExplain(const Payloads& payloads, testing::MatchResultListener* listener) const { std::vector flatten; for (const auto& payload : payloads) @@ -139,15 +139,13 @@ class PayloadMatcher void DescribeTo(std::ostream* os) const { - *os << "is a variant<> with value of type '" << "GetTypeName()" - << "' and the value "; + *os << "is a variant<> with value of type '" << "GetTypeName()" << "' and the value "; matcher_.DescribeTo(os); } void DescribeNegationTo(std::ostream* os) const { - *os << "is a variant<> with value of type other than '" << "GetTypeName()" - << "' or the value "; + *os << "is a variant<> with value of type other than '" << "GetTypeName()" << "' or the value "; matcher_.DescribeNegationTo(os); } diff --git a/test/common/ipc/pipe/client_pipe_mock.hpp b/test/common/ipc/pipe/client_pipe_mock.hpp index 5a7c014..0ac1f57 100644 --- a/test/common/ipc/pipe/client_pipe_mock.hpp +++ b/test/common/ipc/pipe/client_pipe_mock.hpp @@ -8,7 +8,7 @@ #include "ipc/pipe/client_pipe.hpp" -#include "ipc/pipe/pipe_types.hpp" +#include "ipc/ipc_types.hpp" #include "unique_ptr_refwrapper.hpp" #include diff --git a/test/common/ipc/pipe/server_pipe_mock.hpp b/test/common/ipc/pipe/server_pipe_mock.hpp index ae39878..deb1186 100644 --- a/test/common/ipc/pipe/server_pipe_mock.hpp +++ b/test/common/ipc/pipe/server_pipe_mock.hpp @@ -8,7 +8,7 @@ #include "ipc/pipe/server_pipe.hpp" -#include "ipc/pipe/pipe_types.hpp" +#include "ipc/ipc_types.hpp" #include "unique_ptr_refwrapper.hpp" #include diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 46a5be8..78c9739 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -7,8 +7,8 @@ #include "cetl_gtest_helpers.hpp" // NOLINT(misc-include-cleaner) #include "ipc/channel.hpp" +#include "ipc/ipc_types.hpp" #include "ipc/pipe/client_pipe.hpp" -#include "ipc/pipe/pipe_types.hpp" #include "ipc_gtest_helpers.hpp" #include "pipe/client_pipe_mock.hpp" #include "tracking_memory_resource.hpp" @@ -96,7 +96,7 @@ class TestClientRouter : public testing::Test void emulateRouteChannelMsg(pipe::ClientPipeMock& client_pipe_mock, const std::uint64_t tag, const Msg& msg, - const std::uint64_t seq, + std::uint64_t& seq, const cetl::string_view service_name = "") { using ocvsmd::common::tryPerformOnSerialized; @@ -104,7 +104,7 @@ class TestClientRouter : public testing::Test Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag; - channel_msg.sequence = seq; + channel_msg.sequence = seq++; channel_msg.service_id = AnyChannel::getServiceId(service_name); const int result = tryPerformOnSerialized(route, [&](const auto prefix) { @@ -114,7 +114,7 @@ class TestClientRouter : public testing::Test std::vector buffer; std::copy(prefix.begin(), prefix.end(), std::back_inserter(buffer)); std::copy(suffix.begin(), suffix.end(), std::back_inserter(buffer)); - const pipe::Payload payload{buffer.data(), buffer.size()}; + const Payload payload{buffer.data(), buffer.size()}; return client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); }); }); @@ -199,14 +199,16 @@ TEST_F(TestClientRouter, makeChannel_send) auto channel = client_router->makeChannel(); const Msg msg{&mr_}; - EXPECT_THAT(channel.send(msg), ENOTCONN); + EXPECT_THAT(channel.send(msg), static_cast(ErrorCode::NotConnected)); emulateRouteConnect(client_pipe_mock); - EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, 1, 0))).WillOnce(Return(0)); + const std::uint64_t tag = 0; + std::uint64_t seq = 0; + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); EXPECT_THAT(channel.send(msg), 0); - EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, 1, 1))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); EXPECT_THAT(channel.send(msg), 0); EXPECT_CALL(client_pipe_mock, send(PayloadWith(VariantWith(_), mr_))) @@ -243,21 +245,24 @@ TEST_F(TestClientRouter, makeChannel_receive_events) EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); channel2.subscribe(ch2_event_mock.AsStdFunction()); - // Emulate that server posted `RouteChannelMsg` on tag #1. + // Emulate that server posted `RouteChannelMsg` on tag #0. // + std::uint64_t tag = 0; + std::uint64_t seq = 0; EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(2); - emulateRouteChannelMsg(client_pipe_mock, 1, Channel::Input{&mr_}, 0); - emulateRouteChannelMsg(client_pipe_mock, 1, Channel::Input{&mr_}, 1); + emulateRouteChannelMsg(client_pipe_mock, tag, Channel::Input{&mr_}, seq); + emulateRouteChannelMsg(client_pipe_mock, tag, Channel::Input{&mr_}, seq); - // Emulate that server posted `RouteChannelMsg` on tag #2. + // Emulate that server posted `RouteChannelMsg` on tag #1. // + tag = 1, seq = 0; EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); - emulateRouteChannelMsg(client_pipe_mock, 2, Channel::Input{&mr_}, 0); + emulateRouteChannelMsg(client_pipe_mock, tag, Channel::Input{&mr_}, seq); // Emulate that the pipe is disconnected - all channels should be notified. // - EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); + EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); + EXPECT_CALL(ch2_event_mock, Call(VariantWith(_))).Times(1); client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Disconnected{}); } diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index eab8fdf..7389d23 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -6,7 +6,7 @@ #include "ipc/server_router.hpp" #include "ipc/channel.hpp" -#include "ipc/pipe/pipe_types.hpp" +#include "ipc/ipc_types.hpp" #include "ipc/pipe/server_pipe.hpp" #include "ipc_gtest_helpers.hpp" #include "pipe/server_pipe_mock.hpp" @@ -68,7 +68,7 @@ class TestServerRouter : public testing::Test pipe::ServerPipeMock& server_pipe_mock, const std::uint64_t tag, const Msg& msg, - const std::uint64_t seq, + std::uint64_t& seq, const cetl::string_view service_name = "") { using ocvsmd::common::tryPerformOnSerialized; @@ -76,7 +76,7 @@ class TestServerRouter : public testing::Test Route_1_0 route{&mr_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag; - channel_msg.sequence = seq; + channel_msg.sequence = seq++; channel_msg.service_id = AnyChannel::getServiceId(service_name); const int result = tryPerformOnSerialized(route, [&](const auto prefix) { @@ -86,7 +86,7 @@ class TestServerRouter : public testing::Test std::vector buffer; std::copy(prefix.begin(), prefix.end(), std::back_inserter(buffer)); std::copy(suffix.begin(), suffix.end(), std::back_inserter(buffer)); - const pipe::Payload payload{buffer.data(), buffer.size()}; + const Payload payload{buffer.data(), buffer.size()}; return server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{client_id, payload}); }); }); @@ -187,19 +187,22 @@ TEST_F(TestServerRouter, channel_send) // Emulate that client posted initial `RouteChannelMsg` on 42/1 client/tag pair. // + const std::uint64_t tag = 1; + std::uint64_t seq = 0; EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - emulateRouteChannelMsg(42, server_pipe_mock, 1, Channel::Input{&mr_}, 0); + emulateRouteChannelMsg(42, server_pipe_mock, tag, Channel::Input{&mr_}, seq); ASSERT_THAT(maybe_channel.has_value(), IsTrue()); // Emulate that client posted one more `RouteChannelMsg` on the same 42/1 client/tag pair. // EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - emulateRouteChannelMsg(42, server_pipe_mock, 1, Channel::Input{&mr_}, 1); + emulateRouteChannelMsg(42, server_pipe_mock, tag, Channel::Input{&mr_}, seq); - EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, 1, 0))).WillOnce(Return(0)); + seq = 0; + EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); - EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, 1, 1))).WillOnce(Return(0)); + EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); } From 3ff632557651a1a479cd25f84291cb383f327b1a Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 9 Jan 2025 16:18:15 +0200 Subject: [PATCH 039/156] `Disconnected` on unregistered gateway send --- src/common/ipc/client_router.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 5de3a03..237215d 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -154,10 +154,14 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD int send(const detail::ServiceId service_id, const Payload payload) override { - if (!router_.is_connected_) + if (!router_.isConnected(endpoint_)) { return static_cast(ErrorCode::NotConnected); } + if (!router_.isRegisteredGateway(endpoint_)) + { + return static_cast(ErrorCode::Disconnected); + } Route_1_0 route{&router_.memory_}; @@ -201,8 +205,13 @@ class ClientRouterImpl final : public ClientRouter return is_connected_; } + CETL_NODISCARD bool isRegisteredGateway(const Endpoint& endpoint) const noexcept + { + return endpoint_to_gateway_.find(endpoint) != endpoint_to_gateway_.end(); + } + template - void findRegisteredGateway(const Endpoint endpoint, Action&& action) + void findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) { const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); if (ep_to_gw != endpoint_to_gateway_.end()) @@ -242,7 +251,7 @@ class ClientRouterImpl final : public ClientRouter { if (is_connected_) { - findRegisteredGateway(endpoint, [](auto& gateway, auto) { + findAndActOnRegisteredGateway(endpoint, [](auto& gateway, auto) { // gateway.event(detail::Gateway::Event::Connected{}); }); @@ -385,9 +394,9 @@ class ClientRouterImpl final : public ClientRouter const Endpoint endpoint{route_ch_end.tag}; const auto error_code = static_cast(route_ch_end.error_code); - findRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto it) { + findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { // - endpoint_to_gateway_.erase(it); + endpoint_to_gateway_.erase(found_it); gateway.event(detail::Gateway::Event::Completed{error_code}); }); } From cc119cb114a2dbf9a8bded30e1806a1a81943e34 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 9 Jan 2025 17:31:18 +0200 Subject: [PATCH 040/156] corrected send flow for client/server routers --- src/common/ipc/client_router.cpp | 2 +- src/common/ipc/server_router.cpp | 76 +++++++++++++++++++------- test/common/ipc/ipc_gtest_helpers.hpp | 10 +++- test/common/ipc/test_client_router.cpp | 12 ++-- test/common/ipc/test_server_router.cpp | 34 +++++++----- 5 files changed, 92 insertions(+), 42 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 237215d..48e102c 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -262,7 +262,7 @@ class ClientRouterImpl final : public ClientRouter /// /// Called on the gateway disposal (correspondingly on its channel destruction). /// The "dying" gateway might wish to notify the remote router about its disposal. - /// The router fulfills the wish if the gateway was registered and the router is connected. + /// This local router fulfills the wish if the gateway was registered and the router is connected. /// void onGatewayDisposal(const Endpoint& endpoint, const bool send_ch_end) { diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 7bf638e..60c4b96 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -147,8 +147,9 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { - router_.unregisterGateway(endpoint_, true); ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); + + router_.onGatewayDisposal(endpoint_); } // detail::Gateway @@ -159,6 +160,10 @@ class ServerRouterImpl final : public ServerRouter { return static_cast(ErrorCode::NotConnected); } + if (!router_.isRegisteredGateway(endpoint_)) + { + return static_cast(ErrorCode::Disconnected); + } Route_1_0 route{&router_.memory_}; @@ -183,16 +188,8 @@ class ServerRouterImpl final : public ServerRouter void subscribe(EventHandler event_handler) override { - if (event_handler) - { - event_handler_ = std::move(event_handler); - router_.registerGateway(endpoint_, *this); - } - else - { - event_handler_ = nullptr; - router_.unregisterGateway(endpoint_, false); - } + event_handler_ = std::move(event_handler); + router_.onGatewaySubscription(endpoint_); } private: @@ -211,24 +208,62 @@ class ServerRouterImpl final : public ServerRouter return connected_client_ids_.find(endpoint.getClientId()) != connected_client_ids_.end(); } - void registerGateway(const Endpoint& endpoint, GatewayImpl& gateway) + CETL_NODISCARD bool isRegisteredGateway(const Endpoint& endpoint) const noexcept + { + return endpoint_to_gateway_.find(endpoint) != endpoint_to_gateway_.end(); + } + + template + void findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) + { + const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); + if (ep_to_gw != endpoint_to_gateway_.end()) + { + const auto gateway = ep_to_gw->second.lock(); + if (gateway) + { + std::forward(action)(*gateway, ep_to_gw); + } + } + } + + void onGatewaySubscription(const Endpoint endpoint) { - endpoint_to_gateway_[endpoint] = gateway.shared_from_this(); if (isConnected(endpoint)) { - gateway.event(detail::Gateway::Event::Connected{}); + findAndActOnRegisteredGateway(endpoint, [](auto& gateway, auto) { + // + gateway.event(detail::Gateway::Event::Connected{}); + }); } } - void unregisterGateway(const Endpoint& endpoint, const bool is_disposed = false) + /// Unregisters the gateway associated with the given endpoint. + /// + /// Called on the gateway disposal (correspondingly on its channel destruction). + /// The "dying" gateway wishes to notify the remote client router about its disposal. + /// This local router fulfills the wish if the gateway was registered and the client router is connected. + /// + void onGatewayDisposal(const Endpoint& endpoint) { - endpoint_to_gateway_.erase(endpoint); + const bool was_registered = (endpoint_to_gateway_.erase(endpoint) > 0); - // Notify "remote" router about the gateway disposal. - // The router will deliver "disconnected" event to the counterpart gateway (if it exists). + // Notify remote client router about the gateway disposal (aka channel completion). + // The router will propagate "ChEnd" event to the counterpart gateway (if it's registered). // - if (is_disposed && isConnected(endpoint)) + if (was_registered && isConnected(endpoint)) { + Route_1_0 route{&memory_}; + auto& channel_end = route.set_channel_end(); + channel_end.tag = endpoint.getTag(); + channel_end.error_code = 0; // No error b/c it's a normal channel completion. + + const int result = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) { + // + return server_pipe_->send(endpoint.getClientId(), {{payload}}); + }); + // Best efforts strategy - gateway anyway is gone, so nowhere to report. + (void) result; } } @@ -318,7 +353,8 @@ class ServerRouterImpl final : public ServerRouter const auto si_to_cf = service_id_to_channel_factory_.find(route_ch_msg.service_id); if (si_to_cf != service_id_to_channel_factory_.end()) { - auto gateway = GatewayImpl::create(*this, endpoint); + auto gateway = GatewayImpl::create(*this, endpoint); + endpoint_to_gateway_[endpoint] = gateway; si_to_cf->second(gateway, msg_payload); } } diff --git a/test/common/ipc/ipc_gtest_helpers.hpp b/test/common/ipc/ipc_gtest_helpers.hpp index 68dfd59..78a9baa 100644 --- a/test/common/ipc/ipc_gtest_helpers.hpp +++ b/test/common/ipc/ipc_gtest_helpers.hpp @@ -164,7 +164,7 @@ testing::PolymorphicMatcher> PayloadWith( return testing::MakePolymorphicMatcher(PayloadMatcher(matcher, memory)); } -inline auto PayloadRouteConnectEq(cetl::pmr::memory_resource& mr, +inline auto PayloadOfRouteConnect(cetl::pmr::memory_resource& mr, const std::uint8_t ver_major = VERSION_MAJOR, const std::uint8_t ver_minor = VERSION_MINOR) { @@ -182,6 +182,14 @@ auto PayloadOfRouteChannel(cetl::pmr::memory_resource& mr, return PayloadWith(testing::VariantWith(msg), mr); } +inline auto PayloadOfRouteChannelEnd(cetl::pmr::memory_resource& mr, // + const std::uint64_t tag, + const ErrorCode error_code) +{ + const RouteChannelEnd_1_0 ch_end{{tag, static_cast(error_code), &mr}, &mr}; + return PayloadWith(testing::VariantWith(ch_end), mr); +} + } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 78c9739..e37e5ed 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -13,7 +13,6 @@ #include "pipe/client_pipe_mock.hpp" #include "tracking_memory_resource.hpp" -#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" #include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" @@ -82,7 +81,8 @@ class TestClientRouter : public testing::Test void emulateRouteConnect(pipe::ClientPipeMock& client_pipe_mock) { // client RouteConnect -> server - EXPECT_CALL(client_pipe_mock, send(PayloadRouteConnectEq(mr_))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteConnect(mr_))) // + .WillOnce(Return(0)); client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Connected{}); // Server -> client RouteConnect @@ -205,13 +205,15 @@ TEST_F(TestClientRouter, makeChannel_send) const std::uint64_t tag = 0; std::uint64_t seq = 0; - EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))) // + .WillOnce(Return(0)); EXPECT_THAT(channel.send(msg), 0); - EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))) // + .WillOnce(Return(0)); EXPECT_THAT(channel.send(msg), 0); - EXPECT_CALL(client_pipe_mock, send(PayloadWith(VariantWith(_), mr_))) + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannelEnd(mr_, tag, ErrorCode::Success))) // .WillOnce(Return(0)); } diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 7389d23..cbf9bda 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -12,7 +12,6 @@ #include "pipe/server_pipe_mock.hpp" #include "tracking_memory_resource.hpp" -#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" #include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" @@ -169,40 +168,45 @@ TEST_F(TestServerRouter, channel_send) EXPECT_THAT(server_router->start(), 0); EXPECT_THAT(server_pipe_mock.event_handler_, IsTrue()); - StrictMock> ch1_event_mock; + StrictMock> ch_event_mock; cetl::optional maybe_channel; server_router->registerChannel("", [&](Channel&& ch, const auto& input) { // - ch.subscribe(ch1_event_mock.AsStdFunction()); + ch.subscribe(ch_event_mock.AsStdFunction()); maybe_channel = std::move(ch); - ch1_event_mock.Call(input); + ch_event_mock.Call(input); }); EXPECT_THAT(maybe_channel.has_value(), IsFalse()); // Emulate that client #42 is connected. // - EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - emulatePipeConnect(42, server_pipe_mock); + constexpr std::uint64_t cl_id = 42; + EXPECT_CALL(ch_event_mock, Call(VariantWith(_))).Times(1); + emulatePipeConnect(cl_id, server_pipe_mock); - // Emulate that client posted initial `RouteChannelMsg` on 42/1 client/tag pair. + // Emulate that client posted initial `RouteChannelMsg` on 42/7 client/tag pair. // - const std::uint64_t tag = 1; + const std::uint64_t tag = 7; std::uint64_t seq = 0; - EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - emulateRouteChannelMsg(42, server_pipe_mock, tag, Channel::Input{&mr_}, seq); + EXPECT_CALL(ch_event_mock, Call(VariantWith(_))).Times(1); + emulateRouteChannelMsg(cl_id, server_pipe_mock, tag, Channel::Input{&mr_}, seq); ASSERT_THAT(maybe_channel.has_value(), IsTrue()); + EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannelEnd(mr_, tag, ErrorCode::Success))) // + .WillOnce(Return(0)); - // Emulate that client posted one more `RouteChannelMsg` on the same 42/1 client/tag pair. + // Emulate that client posted one more `RouteChannelMsg` on the same 42/7 client/tag pair. // - EXPECT_CALL(ch1_event_mock, Call(VariantWith(_))).Times(1); - emulateRouteChannelMsg(42, server_pipe_mock, tag, Channel::Input{&mr_}, seq); + EXPECT_CALL(ch_event_mock, Call(VariantWith(_))).Times(1); + emulateRouteChannelMsg(cl_id, server_pipe_mock, tag, Channel::Input{&mr_}, seq); seq = 0; - EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); + EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannel(mr_, tag, seq++))) // + .WillOnce(Return(0)); EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); - EXPECT_CALL(server_pipe_mock, send(42, PayloadOfRouteChannel(mr_, tag, seq++))).WillOnce(Return(0)); + EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannel(mr_, tag, seq++))) // + .WillOnce(Return(0)); EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); } From b0ebbb320a585bfbe92de288f66186d912d37986 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 9 Jan 2025 19:28:40 +0200 Subject: [PATCH 041/156] impl handleRouteChannelEnd --- .../dsdl/ocvsmd/common/ipc/Route.1.0.dsdl | 2 +- .../ocvsmd/common/ipc/RouteConnect.0.1.dsdl | 4 ++ .../ocvsmd/common/ipc/RouteConnect.1.0.dsdl | 4 -- src/common/ipc/client_router.cpp | 31 +++++++--- src/common/ipc/server_router.cpp | 62 ++++++++++++++----- src/daemon/engine/application.cpp | 6 +- src/sdk/daemon.cpp | 4 +- test/common/ipc/ipc_gtest_helpers.hpp | 17 ++--- test/common/ipc/test_client_router.cpp | 35 +++++------ test/common/ipc/test_server_router.cpp | 24 ++++++- 10 files changed, 122 insertions(+), 67 deletions(-) create mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl delete mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl diff --git a/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl index 95d22ae..5cd25a5 100644 --- a/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl +++ b/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl @@ -1,7 +1,7 @@ @union uavcan.primitive.Empty.1.0 empty -RouteConnect.1.0 connect +RouteConnect.0.1 connect RouteChannelMsg.1.0 channel_msg RouteChannelEnd.1.0 channel_end diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl new file mode 100644 index 0000000..8ce664c --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl @@ -0,0 +1,4 @@ +uavcan.node.Version.1.0 version +int32 error_code + +@extent 64 diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl deleted file mode 100644 index 7ad4358..0000000 --- a/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.1.0.dsdl +++ /dev/null @@ -1,4 +0,0 @@ -uavcan.node.Version.1.0 version - -# reserve twice as much as we need. -@extent _offset_.max * 2 diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 48e102c..413fb47 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -12,7 +12,7 @@ #include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" -#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" #include "uavcan/primitive/Empty_1_0.hpp" @@ -133,7 +133,7 @@ class ClientRouterImpl final : public ClientRouter , endpoint_{endpoint} , next_sequence_{0} { - ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag()); + ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag()); // NOLINT } GatewayImpl(const GatewayImpl&) = delete; @@ -143,7 +143,7 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { - ::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.getTag(), next_sequence_); + ::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.getTag(), next_sequence_); // NOLINT // `next_sequence_ == 0` means that this gateway was never used for sending messages, // and so remote router never knew about it (its tag) - no need to post "ChEnd" event. @@ -278,24 +278,27 @@ class ClientRouterImpl final : public ClientRouter channel_end.tag = endpoint.getTag(); channel_end.error_code = 0; // No error b/c it's a normal channel completion. - const int result = tryPerformOnSerialized(route, [this](const auto payload) { + const int error = tryPerformOnSerialized(route, [this](const auto payload) { // return client_pipe_->send({{payload}}); }); // Best efforts strategy - gateway anyway is gone, so nowhere to report. - (void) result; + (void) error; } } CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Connected) const { - // TODO: log client pipe connection + ::syslog(LOG_DEBUG, "Pipe is connected."); // NOLINT + // It's not enough to consider the server route connected by the pipe event. + // We gonna initiate `RouteConnect` negotiation (see `handleRouteConnect`). + // Route_1_0 route{&memory_}; auto& route_conn = route.set_connect(); route_conn.version.major = VERSION_MAJOR; route_conn.version.minor = VERSION_MINOR; - + // return tryPerformOnSerialized(route, [this](const auto payload) { // return client_pipe_->send({{payload}}); @@ -317,7 +320,7 @@ class ClientRouterImpl final : public ClientRouter cetl::visit(cetl::make_overloaded( // [this](const uavcan::primitive::Empty_1_0&) {}, - [this](const RouteConnect_1_0& route_conn) { + [this](const RouteConnect_0_1& route_conn) { // handleRouteConnect(route_conn); }, @@ -336,6 +339,8 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) { + ::syslog(LOG_DEBUG, "Pipe is disconnected."); // NOLINT + if (is_connected_) { is_connected_ = false; @@ -356,8 +361,14 @@ class ClientRouterImpl final : public ClientRouter return 0; } - void handleRouteConnect(const RouteConnect_1_0&) + void handleRouteConnect(const RouteConnect_0_1& rt_conn) { + ::syslog(LOG_DEBUG, // NOLINT + "Route connect response (ver='%d.%d', err=%d).", + static_cast(rt_conn.version.major), + static_cast(rt_conn.version.minor), + static_cast(rt_conn.error_code)); + if (!is_connected_) { is_connected_ = true; @@ -391,6 +402,8 @@ class ClientRouterImpl final : public ClientRouter void handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end) { + ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT + const Endpoint endpoint{route_ch_end.tag}; const auto error_code = static_cast(route_ch_end.error_code); diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 60c4b96..2e5dece 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -11,7 +11,7 @@ #include "pipe/server_pipe.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" -#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" #include "uavcan/primitive/Empty_1_0.hpp" @@ -137,7 +137,7 @@ class ServerRouterImpl final : public ServerRouter , endpoint_{endpoint} , next_sequence_{0} { - ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag()); + ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag()); // NOLINT } GatewayImpl(const GatewayImpl&) = delete; @@ -147,7 +147,7 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { - ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); + ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); // NOLINT router_.onGatewayDisposal(endpoint_); } @@ -258,18 +258,22 @@ class ServerRouterImpl final : public ServerRouter channel_end.tag = endpoint.getTag(); channel_end.error_code = 0; // No error b/c it's a normal channel completion. - const int result = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) { + const int error = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) { // return server_pipe_->send(endpoint.getClientId(), {{payload}}); }); // Best efforts strategy - gateway anyway is gone, so nowhere to report. - (void) result; + (void) error; } } - CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Connected conn) + CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Connected& pipe_conn) { - connected_client_ids_.insert(conn.client_id); + ::syslog(LOG_DEBUG, "Pipe is connected (cl=%zu).", pipe_conn.client_id); // NOLINT + + // It's not enough to consider the client router connected by the pipe event. + // We gonna wait for `RouteConnect` negotiation (see `handleRouteConnect`). + // But for now everything is fine. return 0; } @@ -288,7 +292,7 @@ class ServerRouterImpl final : public ServerRouter cetl::visit(cetl::make_overloaded( // [this](const uavcan::primitive::Empty_1_0&) {}, - [this, &msg](const RouteConnect_1_0& route_conn) { + [this, &msg](const RouteConnect_0_1& route_conn) { // handleRouteConnect(msg.client_id, route_conn); }, @@ -307,27 +311,39 @@ class ServerRouterImpl final : public ServerRouter return 0; } - CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected) + CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected& disconn) { + ::syslog(LOG_DEBUG, "Pipe is disconnected (cl=%zu).", disconn.client_id); // NOLINT + // TODO: Implement! disconnected for all gateways which belong to the corresponding client id return 0; } - void handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_1_0&) const + void handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_0_1& rt_conn) { - // TODO: log client route connection + ::syslog(LOG_DEBUG, // NOLINT + "Route connect request (cl=%zu, ver='%d.%d', err=%d).", + client_id, + static_cast(rt_conn.version.major), + static_cast(rt_conn.version.minor), + static_cast(rt_conn.error_code)); Route_1_0 route{&memory_}; - - auto& route_conn = route.set_connect(); + auto& route_conn = route.set_connect(); route_conn.version.major = VERSION_MAJOR; route_conn.version.minor = VERSION_MINOR; - - const int result = tryPerformOnSerialized(route, [this, client_id](const auto payload) { + // In the future, we might have version comparison logic here, + // and potentially refuse the connection if the versions are incompatible. + route_conn.error_code = 0; + // + const int error = tryPerformOnSerialized(route, [this, client_id](const auto payload) { // return server_pipe_->send(client_id, {{payload}}); }); - (void) result; + if (0 != error) + { + connected_client_ids_.insert(client_id); + } } void handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, @@ -362,7 +378,19 @@ class ServerRouterImpl final : public ServerRouter // TODO: log unsolicited message } - void handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end) {} + void handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end) + { + ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT + + const Endpoint endpoint{route_ch_end.tag, client_id}; + const auto error_code = static_cast(route_ch_end.error_code); + + findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { + // + endpoint_to_gateway_.erase(found_it); + gateway.event(detail::Gateway::Event::Completed{error_code}); + }); + } cetl::pmr::memory_resource& memory_; pipe::ServerPipe::Ptr server_pipe_; diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index c5b8a34..9932388 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -78,9 +78,9 @@ cetl::optional Application::init() ipc_exec_cmd_ch_ = std::move(ch); ipc_exec_cmd_ch_->subscribe([this](const auto&) { // - ::syslog(LOG_DEBUG, "Client nested msg"); - ExecCmd r1{&memory_}; - const int res1 = ipc_exec_cmd_ch_->send(r1); + ::syslog(LOG_DEBUG, "Client nested msg"); // NOLINT + const ExecCmd r1{&memory_}; + const int res1 = ipc_exec_cmd_ch_->send(r1); (void) res1; ipc_exec_cmd_ch_.reset(); }); diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 8a696d2..198abee 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -74,7 +74,7 @@ class DaemonImpl final : public Daemon [](const ExecCmdChannel::Completed& completed) { // // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "🔴 Ch Completed (err=%d).", completed.error_code); + ::syslog(LOG_DEBUG, "🔴 Ch Completed (err=%d).", static_cast(completed.error_code)); }), event_var); }); @@ -100,7 +100,7 @@ CETL_NODISCARD std::unique_ptr Daemon::make( // libcyphal::IExecutor& executor) { auto daemon = std::make_unique(memory, executor); - if (daemon->start()) + if (0 != daemon->start()) { return nullptr; } diff --git a/test/common/ipc/ipc_gtest_helpers.hpp b/test/common/ipc/ipc_gtest_helpers.hpp index 78a9baa..e746b6f 100644 --- a/test/common/ipc/ipc_gtest_helpers.hpp +++ b/test/common/ipc/ipc_gtest_helpers.hpp @@ -11,7 +11,7 @@ #include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" -#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" #include @@ -49,9 +49,9 @@ inline void PrintTo(const uavcan::node::Version_1_0& ver, std::ostream* os) *os << "Version_1_0{'" << static_cast(ver.major) << "." << static_cast(ver.minor) << "'}"; } -inline void PrintTo(const RouteConnect_1_0& conn, std::ostream* os) +inline void PrintTo(const RouteConnect_0_1& conn, std::ostream* os) { - *os << "RouteConnect_1_0{ver="; + *os << "RouteConnect_0_1{ver="; PrintTo(conn.version, os); *os << "}"; } @@ -76,7 +76,7 @@ inline void PrintTo(const Route_1_0& route, std::ostream* os) // MARK: - Equitable-s for matching: -inline bool operator==(const RouteConnect_1_0& lhs, const RouteConnect_1_0& rhs) +inline bool operator==(const RouteConnect_0_1& lhs, const RouteConnect_0_1& rhs) { return lhs.version.major == rhs.version.major && lhs.version.minor == rhs.version.minor; } @@ -165,11 +165,12 @@ testing::PolymorphicMatcher> PayloadWith( } inline auto PayloadOfRouteConnect(cetl::pmr::memory_resource& mr, - const std::uint8_t ver_major = VERSION_MAJOR, - const std::uint8_t ver_minor = VERSION_MINOR) + const std::uint8_t ver_major = VERSION_MAJOR, + const std::uint8_t ver_minor = VERSION_MINOR, + ErrorCode error_code = ErrorCode::Success) { - const RouteConnect_1_0 connect{{ver_major, ver_minor, &mr}, &mr}; - return PayloadWith(testing::VariantWith(connect), mr); + const RouteConnect_0_1 route_conn{{ver_major, ver_minor, &mr}, static_cast(error_code), &mr}; + return PayloadWith(testing::VariantWith(route_conn), mr); } template diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index e37e5ed..0362692 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -14,7 +14,7 @@ #include "tracking_memory_resource.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" -#include "ocvsmd/common/ipc/RouteConnect_1_0.hpp" +#include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" #include "ocvsmd/common/ipc/Route_1_0.hpp" #include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" @@ -37,12 +37,10 @@ using namespace ocvsmd::common::ipc; // NOLINT This our main concern here in th using testing::_; using testing::IsTrue; using testing::Return; -using testing::SizeIs; using testing::IsEmpty; using testing::IsFalse; using testing::NotNull; using testing::StrictMock; -using testing::ElementsAre; using testing::VariantWith; using testing::MockFunction; @@ -62,34 +60,29 @@ class TestClientRouter : public testing::Test EXPECT_THAT(mr_.total_allocated_bytes, mr_.total_deallocated_bytes); } - template - void withRouteConnect(const RouteConnect_1_0& connect, Action action) + void emulateRouteConnect(pipe::ClientPipeMock& client_pipe_mock, + const std::uint8_t ver_major = VERSION_MAJOR, // NOLINT + const std::uint8_t ver_minor = VERSION_MINOR, + ErrorCode error_code = ErrorCode::Success) { using ocvsmd::common::tryPerformOnSerialized; - Route_1_0 route{&mr_}; - route.set_connect(connect); - - const int result = tryPerformOnSerialized(route, [&](const auto payload) { - // - action(payload); - return 0; - }); - EXPECT_THAT(result, 0); - } - - void emulateRouteConnect(pipe::ClientPipeMock& client_pipe_mock) - { // client RouteConnect -> server EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteConnect(mr_))) // .WillOnce(Return(0)); client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Connected{}); - // Server -> client RouteConnect - withRouteConnect(RouteConnect_1_0{{1, 2, &mr_}, &mr_}, [&](const auto payload) { + Route_1_0 route{&mr_}; + auto& rt_conn = route.set_connect(); + rt_conn.version.major = ver_major; + rt_conn.version.minor = ver_minor; + rt_conn.error_code = static_cast(error_code); + // + const int result = tryPerformOnSerialized(route, [&](const auto payload) { // - client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); + return client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Message{payload}); }); + EXPECT_THAT(result, 0); } template diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index cbf9bda..7ef639d 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -57,9 +57,29 @@ class TestServerRouter : public testing::Test EXPECT_THAT(mr_.total_allocated_bytes, mr_.total_deallocated_bytes); } - static void emulatePipeConnect(const pipe::ServerPipe::ClientId client_id, pipe::ServerPipeMock& server_pipe_mock) + void emulateRouteConnect(const pipe::ServerPipe::ClientId client_id, + pipe::ServerPipeMock& server_pipe_mock, + const std::uint8_t ver_major = VERSION_MAJOR, // NOLINT + const std::uint8_t ver_minor = VERSION_MINOR, + const ErrorCode error_code = ErrorCode::Success) { + using ocvsmd::common::tryPerformOnSerialized; + server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Connected{client_id}); + + Route_1_0 route{&mr_}; + auto& rt_conn = route.set_connect(); + rt_conn.version.major = ver_major; + rt_conn.version.minor = ver_minor; + rt_conn.error_code = static_cast(error_code); + // + EXPECT_CALL(server_pipe_mock, send(client_id, PayloadOfRouteConnect(mr_))) // + .WillOnce(Return(0)); + const int result = tryPerformOnSerialized(route, [&](const auto payload) { + // + return server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Message{client_id, payload}); + }); + EXPECT_THAT(result, 0); } template @@ -183,7 +203,7 @@ TEST_F(TestServerRouter, channel_send) // constexpr std::uint64_t cl_id = 42; EXPECT_CALL(ch_event_mock, Call(VariantWith(_))).Times(1); - emulatePipeConnect(cl_id, server_pipe_mock); + emulateRouteConnect(cl_id, server_pipe_mock); // Emulate that client posted initial `RouteChannelMsg` on 42/7 client/tag pair. // From ff41f401f3c3453efca205a73437f7ceb53b4a26 Mon Sep 17 00:00:00 2001 From: Sergei Date: Thu, 9 Jan 2025 23:13:20 +0200 Subject: [PATCH 042/156] print cmake version --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 35d1c39..6419ab0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,6 +28,7 @@ jobs: sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-$LLVM_VERSION 50 clang-tidy --version - run: | + cmake --version cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Debug ctest --preset OCVSMD-Debug @@ -59,6 +60,7 @@ jobs: sudo apt update -y && sudo apt upgrade -y sudo apt install gcc-multilib g++-multilib ninja-build - run: | + cmake --version cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Release ctest --preset OCVSMD-Release From 2d420ddb24884fee3cb93fe390dfa0fa05e1abce Mon Sep 17 00:00:00 2001 From: Sergei Date: Thu, 9 Jan 2025 23:51:19 +0200 Subject: [PATCH 043/156] fixed bug in handleRouteConnect --- src/common/ipc/server_router.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 2e5dece..2eac98d 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -300,7 +300,7 @@ class ServerRouterImpl final : public ServerRouter // handleRouteChannelMsg(msg.client_id, route_ch_msg, msg_payload); }, - [this, &msg, msg_payload](const RouteChannelEnd_1_0& route_ch_end) { + [this, &msg](const RouteChannelEnd_1_0& route_ch_end) { // handleRouteChannelEnd(msg.client_id, route_ch_end); }), @@ -340,7 +340,7 @@ class ServerRouterImpl final : public ServerRouter // return server_pipe_->send(client_id, {{payload}}); }); - if (0 != error) + if (0 == error) { connected_client_ids_.insert(client_id); } From 00651a255fda27d6beb3da6d1ed9bfde9865af19 Mon Sep 17 00:00:00 2001 From: Sergei Date: Fri, 10 Jan 2025 08:48:10 +0200 Subject: [PATCH 044/156] minor fix --- src/common/ipc/server_router.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 2eac98d..ae301a6 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -307,8 +307,6 @@ class ServerRouterImpl final : public ServerRouter route_msg.union_value); return 0; - - return 0; } CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected& disconn) From 10aa6512343966170fb45ab4b1b35d5b50425c64 Mon Sep 17 00:00:00 2001 From: Sergei Date: Fri, 10 Jan 2025 10:46:24 +0200 Subject: [PATCH 045/156] fix potential throwing at destructor of gateways --- src/common/ipc/client_router.cpp | 9 ++++++--- src/common/ipc/ipc_types.hpp | 18 ++++++++++++++++++ src/common/ipc/server_router.cpp | 5 ++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 413fb47..481ab11 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -145,9 +145,12 @@ class ClientRouterImpl final : public ClientRouter { ::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.getTag(), next_sequence_); // NOLINT - // `next_sequence_ == 0` means that this gateway was never used for sending messages, - // and so remote router never knew about it (its tag) - no need to post "ChEnd" event. - router_.onGatewayDisposal(endpoint_, next_sequence_ > 0); + performWithoutThrowing([this] { + // + // `next_sequence_ == 0` means that this gateway was never used for sending messages, + // and so remote router never knew about it (its tag) - no need to post "ChEnd" event. + router_.onGatewayDisposal(endpoint_, next_sequence_ > 0); + }); } // detail::Gateway diff --git a/src/common/ipc/ipc_types.hpp b/src/common/ipc/ipc_types.hpp index 6fd92c4..de88e96 100644 --- a/src/common/ipc/ipc_types.hpp +++ b/src/common/ipc/ipc_types.hpp @@ -10,6 +10,7 @@ #include #include +#include namespace ocvsmd { @@ -33,6 +34,23 @@ enum class ErrorCode : int // NOLINT using Payload = cetl::span; using Payloads = cetl::span; +template +void performWithoutThrowing(Action&& action) noexcept +{ +#if defined(__cpp_exceptions) + try +#endif + { + std::forward(action)(); + } +#if defined(__cpp_exceptions) + catch (...) + { + ::syslog(LOG_WARNING, "Unexpected exception is caught!"); // NOLINT + } +#endif +} + } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index ae301a6..7ac6142 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -149,7 +149,10 @@ class ServerRouterImpl final : public ServerRouter { ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); // NOLINT - router_.onGatewayDisposal(endpoint_); + performWithoutThrowing([this] { + // + router_.onGatewayDisposal(endpoint_); + }); } // detail::Gateway From d8d2b82be3c333b40644a4fcbf2f1f18ae410c99 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 14:08:20 +0200 Subject: [PATCH 046/156] more error handling --- src/common/ipc/channel.hpp | 18 ++++-- src/common/ipc/client_router.cpp | 96 +++++++++++++++++--------------- src/common/ipc/gateway.hpp | 4 +- src/common/ipc/server_router.cpp | 93 +++++++++++++++++-------------- 4 files changed, 116 insertions(+), 95 deletions(-) diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index 8b4f76d..d3a4bb1 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -93,7 +93,7 @@ class Channel final : public AnyChannel auto adapter = Adapter{memory_, std::move(event_handler)}; gateway_->subscribe([adapter = std::move(adapter)](const GatewayEvent::Var& ge_var) { // - cetl::visit(adapter, ge_var); + return cetl::visit(adapter, ge_var); }); } else @@ -113,23 +113,29 @@ class Channel final : public AnyChannel cetl::pmr::memory_resource& memory; // NOLINT EventHandler ch_event_handler; // NOLINT - void operator()(const GatewayEvent::Connected&) const + CETL_NODISCARD int operator()(const GatewayEvent::Connected&) const { ch_event_handler(Connected{}); + return 0; } - void operator()(const GatewayEvent::Message& gateway_msg) const + CETL_NODISCARD int operator()(const GatewayEvent::Message& gateway_msg) const { Input input{&memory}; - if (tryDeserializePayload(gateway_msg.payload, input)) + if (!tryDeserializePayload(gateway_msg.payload, input)) { - ch_event_handler(input); + // Invalid message payload. + return EINVAL; } + + ch_event_handler(input); + return 0; } - void operator()(const GatewayEvent::Completed& completed) const + CETL_NODISCARD int operator()(const GatewayEvent::Completed& completed) const { ch_event_handler(Completed{completed.error_code}); + return 0; } }; // Adapter diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 481ab11..a28054d 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -179,12 +179,10 @@ class ClientRouterImpl final : public ClientRouter }); } - void event(const Event::Var& event) override + CETL_NODISCARD int event(const Event::Var& event) override { - if (event_handler_) - { - event_handler_(event); - } + // It's fine to be not subscribed to events. + return (event_handler_) ? event_handler_(event) : 0; } void subscribe(EventHandler event_handler) override @@ -214,17 +212,19 @@ class ClientRouterImpl final : public ClientRouter } template - void findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) + CETL_NODISCARD int findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) { const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); if (ep_to_gw != endpoint_to_gateway_.end()) { - const auto gateway = ep_to_gw->second.lock(); - if (gateway) + if (const auto gateway = ep_to_gw->second.lock()) { - std::forward(action)(*gateway, ep_to_gw); + return std::forward(action)(*gateway, ep_to_gw); } } + + // It's fine and expected to have no gateway registered for the given endpoint. + return 0; } template @@ -237,8 +237,7 @@ class ClientRouterImpl final : public ClientRouter gateway_ptrs.reserve(endpoint_to_gateway_.size()); for (const auto& ep_to_gw : endpoint_to_gateway_) { - const auto gateway_ptr = ep_to_gw.second.lock(); - if (gateway_ptr) + if (const auto gateway_ptr = ep_to_gw.second.lock()) { gateway_ptrs.push_back(gateway_ptr); } @@ -254,10 +253,12 @@ class ClientRouterImpl final : public ClientRouter { if (is_connected_) { - findAndActOnRegisteredGateway(endpoint, [](auto& gateway, auto) { + const int err = findAndActOnRegisteredGateway(endpoint, [](auto& gateway, auto) { // - gateway.event(detail::Gateway::Event::Connected{}); + return gateway.event(detail::Gateway::Event::Connected{}); }); + // Best efforts strategy. + (void) err; } } @@ -314,30 +315,34 @@ class ClientRouterImpl final : public ClientRouter const auto result_size = tryDeserializePayload(msg.payload, route_msg); if (!result_size.has_value()) { + // Invalid message payload. return EINVAL; } // Cut routing stuff from the payload - remaining is the actual message payload. const auto msg_payload = msg.payload.subspan(result_size.value()); - cetl::visit(cetl::make_overloaded( - // - [this](const uavcan::primitive::Empty_1_0&) {}, - [this](const RouteConnect_0_1& route_conn) { - // - handleRouteConnect(route_conn); - }, - [this, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { - // - handleRouteChannelMsg(route_ch_msg, msg_payload); - }, - [this](const RouteChannelEnd_1_0& route_ch_end) { - // - handleRouteChannelEnd(route_ch_end); - }), - route_msg.union_value); - - return 0; + return cetl::visit( // + cetl::make_overloaded( // + [this](const uavcan::primitive::Empty_1_0&) { + // + // Unexpected message, but we can't remove it + // b/c Nunavut generated code needs a default case. + return EINVAL; + }, + [this](const RouteConnect_0_1& route_conn) { + // + return handleRouteConnect(route_conn); + }, + [this, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { + // + return handleRouteChannelMsg(route_ch_msg, msg_payload); + }, + [this](const RouteChannelEnd_1_0& route_ch_end) { + // + return handleRouteChannelEnd(route_ch_end); + }), + route_msg.union_value); } CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) @@ -354,17 +359,18 @@ class ClientRouterImpl final : public ClientRouter std::swap(local_gateways, endpoint_to_gateway_); for (const auto& ep_to_gw : local_gateways) { - const auto gateway = ep_to_gw.second.lock(); - if (gateway) + if (const auto gateway = ep_to_gw.second.lock()) { - gateway->event(detail::Gateway::Event::Completed{ErrorCode::Disconnected}); + const int err = gateway->event(detail::Gateway::Event::Completed{ErrorCode::Disconnected}); + // Best efforts strategy. + (void) err; } } } return 0; } - void handleRouteConnect(const RouteConnect_0_1& rt_conn) + CETL_NODISCARD int handleRouteConnect(const RouteConnect_0_1& rt_conn) { ::syslog(LOG_DEBUG, // NOLINT "Route connect response (ver='%d.%d', err=%d).", @@ -383,37 +389,39 @@ class ClientRouterImpl final : public ClientRouter gateway.event(detail::Gateway::Event::Connected{}); }); } + + // It's fine to be already connected. + return 0; } - void handleRouteChannelMsg(const RouteChannelMsg_1_0& route_ch_msg, const Payload payload) + CETL_NODISCARD int handleRouteChannelMsg(const RouteChannelMsg_1_0& route_ch_msg, const Payload payload) { const Endpoint endpoint{route_ch_msg.tag}; const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); if (ep_to_gw != endpoint_to_gateway_.end()) { - const auto gateway = ep_to_gw->second.lock(); - if (gateway) + if (const auto gateway = ep_to_gw->second.lock()) { - gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, payload}); - return; + return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, payload}); } } - // TODO: log unsolicited message + // Nothing to do here with unsolicited messages - just ignore them. + return 0; } - void handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end) + CETL_NODISCARD int handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end) { ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT const Endpoint endpoint{route_ch_end.tag}; const auto error_code = static_cast(route_ch_end.error_code); - findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { + return findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { // endpoint_to_gateway_.erase(found_it); - gateway.event(detail::Gateway::Event::Completed{error_code}); + return gateway.event(detail::Gateway::Event::Completed{error_code}); }); } diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index 671a945..39f9495 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -51,7 +51,7 @@ class Gateway }; // Event - using EventHandler = std::function; + using EventHandler = std::function; Gateway(const Gateway&) = delete; Gateway(Gateway&&) noexcept = delete; @@ -59,7 +59,7 @@ class Gateway Gateway& operator=(Gateway&&) noexcept = delete; CETL_NODISCARD virtual int send(const ServiceId service_id, const Payload payload) = 0; - virtual void event(const Event::Var& event) = 0; + CETL_NODISCARD virtual int event(const Event::Var& event) = 0; virtual void subscribe(EventHandler event_handler) = 0; protected: diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 7ac6142..783e002 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -181,12 +181,10 @@ class ServerRouterImpl final : public ServerRouter }); } - void event(const Event::Var& event) override + CETL_NODISCARD int event(const Event::Var& event) override { - if (event_handler_) - { - event_handler_(event); - } + // It's fine to be not subscribed to events. + return (event_handler_) ? event_handler_(event) : 0; } void subscribe(EventHandler event_handler) override @@ -217,27 +215,31 @@ class ServerRouterImpl final : public ServerRouter } template - void findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) + CETL_NODISCARD int findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) { const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); if (ep_to_gw != endpoint_to_gateway_.end()) { - const auto gateway = ep_to_gw->second.lock(); - if (gateway) + if (const auto gateway = ep_to_gw->second.lock()) { - std::forward(action)(*gateway, ep_to_gw); + return std::forward(action)(*gateway, ep_to_gw); } } + + // It's fine and expected to have no gateway registered for the given endpoint. + return 0; } void onGatewaySubscription(const Endpoint endpoint) { if (isConnected(endpoint)) { - findAndActOnRegisteredGateway(endpoint, [](auto& gateway, auto) { + const int err = findAndActOnRegisteredGateway(endpoint, [](auto& gateway, auto) { // - gateway.event(detail::Gateway::Event::Connected{}); + return gateway.event(detail::Gateway::Event::Connected{}); }); + // Best efforts strategy. + (void) err; } } @@ -286,30 +288,34 @@ class ServerRouterImpl final : public ServerRouter const auto result_size = tryDeserializePayload(msg.payload, route_msg); if (!result_size.has_value()) { + // Invalid message payload. return EINVAL; } // Cut routing stuff from the payload - remaining is the actual message payload. const auto msg_payload = msg.payload.subspan(result_size.value()); - cetl::visit(cetl::make_overloaded( - // - [this](const uavcan::primitive::Empty_1_0&) {}, - [this, &msg](const RouteConnect_0_1& route_conn) { - // - handleRouteConnect(msg.client_id, route_conn); - }, - [this, &msg, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { - // - handleRouteChannelMsg(msg.client_id, route_ch_msg, msg_payload); - }, - [this, &msg](const RouteChannelEnd_1_0& route_ch_end) { - // - handleRouteChannelEnd(msg.client_id, route_ch_end); - }), - route_msg.union_value); - - return 0; + return cetl::visit( // + cetl::make_overloaded( // + [this](const uavcan::primitive::Empty_1_0&) { + // + // Unexpected message, but we can't remove it + // b/c Nunavut generated code needs a default case. + return EINVAL; + }, + [this, &msg](const RouteConnect_0_1& route_conn) { + // + return handleRouteConnect(msg.client_id, route_conn); + }, + [this, &msg, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { + // + return handleRouteChannelMsg(msg.client_id, route_ch_msg, msg_payload); + }, + [this, &msg](const RouteChannelEnd_1_0& route_ch_end) { + // + return handleRouteChannelEnd(msg.client_id, route_ch_end); + }), + route_msg.union_value); } CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected& disconn) @@ -320,7 +326,7 @@ class ServerRouterImpl final : public ServerRouter return 0; } - void handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_0_1& rt_conn) + CETL_NODISCARD int handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_0_1& rt_conn) { ::syslog(LOG_DEBUG, // NOLINT "Route connect request (cl=%zu, ver='%d.%d', err=%d).", @@ -337,30 +343,29 @@ class ServerRouterImpl final : public ServerRouter // and potentially refuse the connection if the versions are incompatible. route_conn.error_code = 0; // - const int error = tryPerformOnSerialized(route, [this, client_id](const auto payload) { + const int err = tryPerformOnSerialized(route, [this, client_id](const auto payload) { // return server_pipe_->send(client_id, {{payload}}); }); - if (0 == error) + if (0 == err) { connected_client_ids_.insert(client_id); } + return err; } - void handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, - const RouteChannelMsg_1_0& route_ch_msg, - const Payload msg_payload) + CETL_NODISCARD int handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, + const RouteChannelMsg_1_0& route_ch_msg, + const Payload msg_payload) { const Endpoint endpoint{route_ch_msg.tag, client_id}; const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); if (ep_to_gw != endpoint_to_gateway_.end()) { - auto gateway = ep_to_gw->second.lock(); - if (gateway) + if (auto gateway = ep_to_gw->second.lock()) { - gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_payload}); - return; + return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_payload}); } } @@ -376,20 +381,22 @@ class ServerRouterImpl final : public ServerRouter } } - // TODO: log unsolicited message + // Nothing to do here with unsolicited messages - just ignore them. + return 0; } - void handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_1_0& route_ch_end) + CETL_NODISCARD int handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, + const RouteChannelEnd_1_0& route_ch_end) { ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT const Endpoint endpoint{route_ch_end.tag, client_id}; const auto error_code = static_cast(route_ch_end.error_code); - findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { + return findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { // endpoint_to_gateway_.erase(found_it); - gateway.event(detail::Gateway::Event::Completed{error_code}); + return gateway.event(detail::Gateway::Event::Completed{error_code}); }); } From ec6849296804dbef6ae79d7551f9bd168ffc60a3 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 14:26:36 +0200 Subject: [PATCH 047/156] fix build --- src/common/ipc/client_router.cpp | 9 ++++----- src/common/ipc/server_router.cpp | 3 +-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index a28054d..b00d559 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -257,8 +257,7 @@ class ClientRouterImpl final : public ClientRouter // return gateway.event(detail::Gateway::Event::Connected{}); }); - // Best efforts strategy. - (void) err; + (void) err; // Best efforts strategy. } } @@ -362,8 +361,7 @@ class ClientRouterImpl final : public ClientRouter if (const auto gateway = ep_to_gw.second.lock()) { const int err = gateway->event(detail::Gateway::Event::Completed{ErrorCode::Disconnected}); - // Best efforts strategy. - (void) err; + (void) err; // Best efforts strategy. } } } @@ -386,7 +384,8 @@ class ClientRouterImpl final : public ClientRouter // forEachRegisteredGateway([](auto& gateway) { // - gateway.event(detail::Gateway::Event::Connected{}); + const int err = gateway.event(detail::Gateway::Event::Connected{}); + (void) err; // Best efforts strategy. }); } diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 783e002..4fa654c 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -238,8 +238,7 @@ class ServerRouterImpl final : public ServerRouter // return gateway.event(detail::Gateway::Event::Connected{}); }); - // Best efforts strategy. - (void) err; + (void) err; // Best efforts strategy. } } From 968249dd41159f6ad6fd3b9301e626168a600d4c Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 16:03:30 +0200 Subject: [PATCH 048/156] Implemented "disconnected" for all gateways which belong to the corresponding client id --- src/common/ipc/client_router.cpp | 84 +++++------- src/common/ipc/server_router.cpp | 212 +++++++++++++++++-------------- 2 files changed, 144 insertions(+), 152 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index b00d559..bf59da2 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -75,7 +75,7 @@ class ClientRouterImpl final : public ClientRouter const Endpoint endpoint{next_tag_++}; auto gateway = GatewayImpl::create(*this, endpoint); - endpoint_to_gateway_[endpoint] = gateway; + map_of_gateways_[endpoint.tag] = gateway; return gateway; } @@ -84,36 +84,8 @@ class ClientRouterImpl final : public ClientRouter { using Tag = std::uint64_t; - explicit Endpoint(const Tag tag) noexcept - : tag_{tag} - { - } - - CETL_NODISCARD Tag getTag() const noexcept - { - return tag_; - } - - // Hasher - - CETL_NODISCARD bool operator==(const Endpoint& other) const noexcept - { - return tag_ == other.tag_; - } - - struct Hasher final - { - CETL_NODISCARD std::size_t operator()(const Endpoint& endpoint) const noexcept - { - return std::hash{}(endpoint.tag_); - } - - }; // Hasher - - private: - const Tag tag_; - - }; // Endpoint + const Tag tag; + }; class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway { @@ -133,7 +105,7 @@ class ClientRouterImpl final : public ClientRouter , endpoint_{endpoint} , next_sequence_{0} { - ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.getTag()); // NOLINT + ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.tag); // NOLINT } GatewayImpl(const GatewayImpl&) = delete; @@ -143,7 +115,7 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { - ::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.getTag(), next_sequence_); // NOLINT + ::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.tag, next_sequence_); // NOLINT performWithoutThrowing([this] { // @@ -169,7 +141,7 @@ class ClientRouterImpl final : public ClientRouter Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.getTag(); + channel_msg.tag = endpoint_.tag; channel_msg.sequence = next_sequence_++; channel_msg.service_id = service_id; @@ -199,7 +171,7 @@ class ClientRouterImpl final : public ClientRouter }; // GatewayImpl - using EndpointToWeakGateway = std::unordered_map; + using MapOfWeakGateways = std::unordered_map; CETL_NODISCARD bool isConnected(const Endpoint&) const noexcept { @@ -208,18 +180,18 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD bool isRegisteredGateway(const Endpoint& endpoint) const noexcept { - return endpoint_to_gateway_.find(endpoint) != endpoint_to_gateway_.end(); + return map_of_gateways_.find(endpoint.tag) != map_of_gateways_.end(); } template - CETL_NODISCARD int findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) + CETL_NODISCARD int findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) const { - const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); - if (ep_to_gw != endpoint_to_gateway_.end()) + const auto tag_to_gw = map_of_gateways_.find(endpoint.tag); + if (tag_to_gw != map_of_gateways_.end()) { - if (const auto gateway = ep_to_gw->second.lock()) + if (const auto gateway = tag_to_gw->second.lock()) { - return std::forward(action)(*gateway, ep_to_gw); + return std::forward(action)(*gateway, tag_to_gw); } } @@ -234,10 +206,10 @@ class ClientRouterImpl final : public ClientRouter // collect strong pointers to gateways into a local collection. // std::vector gateway_ptrs; - gateway_ptrs.reserve(endpoint_to_gateway_.size()); - for (const auto& ep_to_gw : endpoint_to_gateway_) + gateway_ptrs.reserve(map_of_gateways_.size()); + for (const auto& tag_to_gw : map_of_gateways_) { - if (const auto gateway_ptr = ep_to_gw.second.lock()) + if (const auto gateway_ptr = tag_to_gw.second.lock()) { gateway_ptrs.push_back(gateway_ptr); } @@ -269,7 +241,7 @@ class ClientRouterImpl final : public ClientRouter /// void onGatewayDisposal(const Endpoint& endpoint, const bool send_ch_end) { - const bool was_registered = (endpoint_to_gateway_.erase(endpoint) > 0); + const bool was_registered = (map_of_gateways_.erase(endpoint.tag) > 0); // Notify "remote" router about the gateway disposal (aka channel completion). // The router will propagate "ChEnd" event to the counterpart gateway (if it's registered). @@ -278,7 +250,7 @@ class ClientRouterImpl final : public ClientRouter { Route_1_0 route{&memory_}; auto& channel_end = route.set_channel_end(); - channel_end.tag = endpoint.getTag(); + channel_end.tag = endpoint.tag; channel_end.error_code = 0; // No error b/c it's a normal channel completion. const int error = tryPerformOnSerialized(route, [this](const auto payload) { @@ -354,17 +326,19 @@ class ClientRouterImpl final : public ClientRouter // The whole router is disconnected, so we need to unregister and notify all gateways. // - EndpointToWeakGateway local_gateways; - std::swap(local_gateways, endpoint_to_gateway_); - for (const auto& ep_to_gw : local_gateways) + MapOfWeakGateways local_map_of_gateways; + std::swap(local_map_of_gateways, map_of_gateways_); + for (const auto& tag_to_gw : local_map_of_gateways) { - if (const auto gateway = ep_to_gw.second.lock()) + if (const auto gateway = tag_to_gw.second.lock()) { const int err = gateway->event(detail::Gateway::Event::Completed{ErrorCode::Disconnected}); (void) err; // Best efforts strategy. } } } + + // It's fine to be already disconnected. return 0; } @@ -397,10 +371,10 @@ class ClientRouterImpl final : public ClientRouter { const Endpoint endpoint{route_ch_msg.tag}; - const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); - if (ep_to_gw != endpoint_to_gateway_.end()) + const auto tag_to_gw = map_of_gateways_.find(endpoint.tag); + if (tag_to_gw != map_of_gateways_.end()) { - if (const auto gateway = ep_to_gw->second.lock()) + if (const auto gateway = tag_to_gw->second.lock()) { return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, payload}); } @@ -419,7 +393,7 @@ class ClientRouterImpl final : public ClientRouter return findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { // - endpoint_to_gateway_.erase(found_it); + map_of_gateways_.erase(found_it); return gateway.event(detail::Gateway::Event::Completed{error_code}); }); } @@ -427,8 +401,8 @@ class ClientRouterImpl final : public ClientRouter cetl::pmr::memory_resource& memory_; pipe::ClientPipe::Ptr client_pipe_; Endpoint::Tag next_tag_; - EndpointToWeakGateway endpoint_to_gateway_; bool is_connected_; + MapOfWeakGateways map_of_gateways_; }; // ClientRouterImpl diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 4fa654c..dabdbc3 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -79,45 +79,9 @@ class ServerRouterImpl final : public ServerRouter using Tag = std::uint64_t; using ClientId = pipe::ServerPipe::ClientId; - Endpoint(const Tag tag, ClientId client_id) noexcept - : tag_{tag} - , client_id_{client_id} - { - } - - CETL_NODISCARD Tag getTag() const noexcept - { - return tag_; - } - - CETL_NODISCARD ClientId getClientId() const noexcept - { - return client_id_; - } - - // Hasher - - CETL_NODISCARD bool operator==(const Endpoint& other) const noexcept - { - return tag_ == other.tag_ && client_id_ == other.client_id_; - } - - struct Hasher final - { - CETL_NODISCARD std::size_t operator()(const Endpoint& endpoint) const noexcept - { - const std::size_t h1 = std::hash{}(endpoint.tag_); - const std::size_t h2 = std::hash{}(endpoint.client_id_); - return h1 ^ (h2 << 1ULL); - } - - }; // Hasher - - private: - const Tag tag_; - const ClientId client_id_; - - }; // Endpoint + const Tag tag; + const ClientId client_id; + }; class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway { @@ -137,7 +101,7 @@ class ServerRouterImpl final : public ServerRouter , endpoint_{endpoint} , next_sequence_{0} { - ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.getClientId(), endpoint.getTag()); // NOLINT + ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.client_id, endpoint.tag); // NOLINT } GatewayImpl(const GatewayImpl&) = delete; @@ -147,7 +111,7 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { - ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.getClientId(), endpoint_.getTag()); // NOLINT + ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.client_id, endpoint_.tag); // NOLINT performWithoutThrowing([this] { // @@ -171,13 +135,13 @@ class ServerRouterImpl final : public ServerRouter Route_1_0 route{&router_.memory_}; auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.getTag(); + channel_msg.tag = endpoint_.tag; channel_msg.sequence = next_sequence_++; channel_msg.service_id = service_id; return tryPerformOnSerialized(route, [this, payload](const auto prefix) { // - return router_.server_pipe_->send(endpoint_.getClientId(), {{prefix, payload}}); + return router_.server_pipe_->send(endpoint_.client_id, {{prefix, payload}}); }); } @@ -202,27 +166,41 @@ class ServerRouterImpl final : public ServerRouter }; // GatewayImpl using ServiceIdToChannelFactory = std::unordered_map; - using EndpointToWeakGateway = std::unordered_map; + using MapOfWeakGateways = std::unordered_map; + using ClientIdToMapOfGateways = std::unordered_map; CETL_NODISCARD bool isConnected(const Endpoint& endpoint) const noexcept { - return connected_client_ids_.find(endpoint.getClientId()) != connected_client_ids_.end(); + const auto cl_to_gws = client_id_to_map_of_gateways_.find(endpoint.client_id); + return cl_to_gws != client_id_to_map_of_gateways_.end(); } CETL_NODISCARD bool isRegisteredGateway(const Endpoint& endpoint) const noexcept { - return endpoint_to_gateway_.find(endpoint) != endpoint_to_gateway_.end(); + const auto cl_to_gws = client_id_to_map_of_gateways_.find(endpoint.client_id); + if (cl_to_gws != client_id_to_map_of_gateways_.end()) + { + const auto& map_of_gws = cl_to_gws->second; + return map_of_gws.find(endpoint.tag) != map_of_gws.end(); + } + return false; } template - CETL_NODISCARD int findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) + CETL_NODISCARD int findAndActOnRegisteredGateway(const Endpoint endpoint, Action&& action) const { - const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); - if (ep_to_gw != endpoint_to_gateway_.end()) + const auto cl_to_gws = client_id_to_map_of_gateways_.find(endpoint.client_id); + if (cl_to_gws != client_id_to_map_of_gateways_.end()) { - if (const auto gateway = ep_to_gw->second.lock()) + const auto& map_of_gws = cl_to_gws->second; + + const auto tag_to_gw = map_of_gws.find(endpoint.tag); + if (tag_to_gw != map_of_gws.end()) { - return std::forward(action)(*gateway, ep_to_gw); + if (const auto gateway = tag_to_gw->second.lock()) + { + return std::forward(action)(*gateway, tag_to_gw); + } } } @@ -250,24 +228,29 @@ class ServerRouterImpl final : public ServerRouter /// void onGatewayDisposal(const Endpoint& endpoint) { - const bool was_registered = (endpoint_to_gateway_.erase(endpoint) > 0); - - // Notify remote client router about the gateway disposal (aka channel completion). - // The router will propagate "ChEnd" event to the counterpart gateway (if it's registered). - // - if (was_registered && isConnected(endpoint)) + const auto cl_to_gws = client_id_to_map_of_gateways_.find(endpoint.client_id); + if (cl_to_gws != client_id_to_map_of_gateways_.end()) { - Route_1_0 route{&memory_}; - auto& channel_end = route.set_channel_end(); - channel_end.tag = endpoint.getTag(); - channel_end.error_code = 0; // No error b/c it's a normal channel completion. + auto& map_of_gws = cl_to_gws->second; + const bool was_registered = (map_of_gws.erase(endpoint.tag) > 0); - const int error = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) { - // - return server_pipe_->send(endpoint.getClientId(), {{payload}}); - }); - // Best efforts strategy - gateway anyway is gone, so nowhere to report. - (void) error; + // Notify remote client router about the gateway disposal (aka channel completion). + // The router will propagate "ChEnd" event to the counterpart gateway (if it's registered). + // + if (was_registered && isConnected(endpoint)) + { + Route_1_0 route{&memory_}; + auto& channel_end = route.set_channel_end(); + channel_end.tag = endpoint.tag; + channel_end.error_code = 0; // No error b/c it's a normal channel completion. + + const int error = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) { + // + return server_pipe_->send(endpoint.client_id, {{payload}}); + }); + // Best efforts strategy - gateway anyway is gone, so nowhere to report. + (void) error; + } } } @@ -317,11 +300,29 @@ class ServerRouterImpl final : public ServerRouter route_msg.union_value); } - CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected& disconn) + CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected& disconn) { ::syslog(LOG_DEBUG, "Pipe is disconnected (cl=%zu).", disconn.client_id); // NOLINT - // TODO: Implement! disconnected for all gateways which belong to the corresponding client id + const auto cl_to_gws = client_id_to_map_of_gateways_.find(disconn.client_id); + if (cl_to_gws != client_id_to_map_of_gateways_.end()) + { + const auto local_map_of_gateways = std::move(cl_to_gws->second); + client_id_to_map_of_gateways_.erase(cl_to_gws); + + // The whole client router is disconnected, so we need to unregister and notify all its gateways. + // + for (const auto& tag_to_gw : local_map_of_gateways) + { + if (const auto gateway = tag_to_gw.second.lock()) + { + const int err = gateway->event(detail::Gateway::Event::Completed{ErrorCode::Disconnected}); + (void) err; // Best efforts strategy. + } + } + } + + // It's fine for a client to be already disconnected. return 0; } @@ -348,7 +349,7 @@ class ServerRouterImpl final : public ServerRouter }); if (0 == err) { - connected_client_ids_.insert(client_id); + client_id_to_map_of_gateways_.insert({client_id, MapOfWeakGateways{}}); } return err; } @@ -357,26 +358,33 @@ class ServerRouterImpl final : public ServerRouter const RouteChannelMsg_1_0& route_ch_msg, const Payload msg_payload) { - const Endpoint endpoint{route_ch_msg.tag, client_id}; - - const auto ep_to_gw = endpoint_to_gateway_.find(endpoint); - if (ep_to_gw != endpoint_to_gateway_.end()) + const auto cl_to_gws = client_id_to_map_of_gateways_.find(client_id); + if (cl_to_gws != client_id_to_map_of_gateways_.end()) { - if (auto gateway = ep_to_gw->second.lock()) + auto& map_of_gws = cl_to_gws->second; + + const auto tag_to_gw = map_of_gws.find(route_ch_msg.tag); + if (tag_to_gw != map_of_gws.end()) { - return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_payload}); + if (auto gateway = tag_to_gw->second.lock()) + { + return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_payload}); + } } - } - // Only the very first message in the sequence is considered to trigger channel factory. - if (route_ch_msg.sequence == 0) - { - const auto si_to_cf = service_id_to_channel_factory_.find(route_ch_msg.service_id); - if (si_to_cf != service_id_to_channel_factory_.end()) + // Only the very first message in the sequence is considered to trigger channel factory. + if (route_ch_msg.sequence == 0) { - auto gateway = GatewayImpl::create(*this, endpoint); - endpoint_to_gateway_[endpoint] = gateway; - si_to_cf->second(gateway, msg_payload); + const auto si_to_ch_factory = service_id_to_channel_factory_.find(route_ch_msg.service_id); + if (si_to_ch_factory != service_id_to_channel_factory_.end()) + { + const Endpoint endpoint{route_ch_msg.tag, client_id}; + + auto gateway = GatewayImpl::create(*this, endpoint); + map_of_gws[route_ch_msg.tag] = gateway; + + si_to_ch_factory->second(gateway, msg_payload); + } } } @@ -389,21 +397,31 @@ class ServerRouterImpl final : public ServerRouter { ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT - const Endpoint endpoint{route_ch_end.tag, client_id}; - const auto error_code = static_cast(route_ch_end.error_code); + const auto cl_to_gws = client_id_to_map_of_gateways_.find(client_id); + if (cl_to_gws != client_id_to_map_of_gateways_.end()) + { + auto& map_of_gws = cl_to_gws->second; - return findAndActOnRegisteredGateway(endpoint, [this, error_code](auto& gateway, auto found_it) { - // - endpoint_to_gateway_.erase(found_it); - return gateway.event(detail::Gateway::Event::Completed{error_code}); - }); + const Endpoint endpoint{route_ch_end.tag, client_id}; + const auto error_code = static_cast(route_ch_end.error_code); + + return findAndActOnRegisteredGateway( // + endpoint, + [this, error_code, &map_of_gws](auto& gateway, auto found_it) { + // + map_of_gws.erase(found_it); + return gateway.event(detail::Gateway::Event::Completed{error_code}); + }); + } + + // It's fine for a client to be already disconnected. + return 0; } - cetl::pmr::memory_resource& memory_; - pipe::ServerPipe::Ptr server_pipe_; - EndpointToWeakGateway endpoint_to_gateway_; - ServiceIdToChannelFactory service_id_to_channel_factory_; - std::unordered_set connected_client_ids_; + cetl::pmr::memory_resource& memory_; + pipe::ServerPipe::Ptr server_pipe_; + ClientIdToMapOfGateways client_id_to_map_of_gateways_; + ServiceIdToChannelFactory service_id_to_channel_factory_; }; // ClientRouterImpl From 2f851a30c52d6a18964b2c4a91dde9685c228bbc Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 16:13:25 +0200 Subject: [PATCH 049/156] fix build --- src/common/ipc/server_router.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index dabdbc3..3c70c2c 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -20,12 +20,10 @@ #include #include -#include #include #include #include #include -#include #include namespace ocvsmd From 0c6218f88e21c17069c2925296b2538f53f3847f Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 17:25:57 +0200 Subject: [PATCH 050/156] added `Shutdown` error code --- src/common/ipc/client_router.cpp | 2 +- src/common/ipc/ipc_types.hpp | 3 ++- src/common/ipc/server_router.cpp | 2 +- src/daemon/engine/application.cpp | 40 +++++++++++++++++++++++-------- src/sdk/daemon.cpp | 32 +++++++++++++++---------- 5 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index bf59da2..189549e 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -135,7 +135,7 @@ class ClientRouterImpl final : public ClientRouter } if (!router_.isRegisteredGateway(endpoint_)) { - return static_cast(ErrorCode::Disconnected); + return static_cast(ErrorCode::Shutdown); } Route_1_0 route{&router_.memory_}; diff --git a/src/common/ipc/ipc_types.hpp b/src/common/ipc/ipc_types.hpp index de88e96..1e7041d 100644 --- a/src/common/ipc/ipc_types.hpp +++ b/src/common/ipc/ipc_types.hpp @@ -27,7 +27,8 @@ enum class ErrorCode : int // NOLINT { Success = 0, NotConnected = ENOTCONN, - Disconnected = ESHUTDOWN, + Disconnected = ECONNRESET, + Shutdown = ESHUTDOWN, }; // ErrorCode diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 3c70c2c..4975dc7 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -127,7 +127,7 @@ class ServerRouterImpl final : public ServerRouter } if (!router_.isRegisteredGateway(endpoint_)) { - return static_cast(ErrorCode::Disconnected); + return static_cast(ErrorCode::Shutdown); } Route_1_0 route{&router_.memory_}; diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 9932388..2b7c2bd 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -71,18 +71,38 @@ cetl::optional Application::init() using Ch = ExecCmdChannel; ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto& request) { // - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "Client initial msg (%zu).", request.some_stuff.size()); - const int result = ch.send(request); - (void) result; + ::syslog(LOG_DEBUG, "D << 🆕 Ch created."); // NOLINT + ::syslog(LOG_DEBUG, "D << 🔵 Ch ininital msg."); // NOLINT + ipc_exec_cmd_ch_ = std::move(ch); - ipc_exec_cmd_ch_->subscribe([this](const auto&) { + ipc_exec_cmd_ch_->subscribe([this](const auto& event_var) { // - ::syslog(LOG_DEBUG, "Client nested msg"); // NOLINT - const ExecCmd r1{&memory_}; - const int res1 = ipc_exec_cmd_ch_->send(r1); - (void) res1; - ipc_exec_cmd_ch_.reset(); + cetl::visit( // + cetl::make_overloaded( + [this](const ExecCmdChannel::Connected&) { + // + ::syslog(LOG_DEBUG, "D << 🟢 Ch connected."); // NOLINT + + ::syslog(LOG_DEBUG, "D >> 🔵 Ch Msg."); // NOLINT + ExecCmd cmd{&memory_}; + const int result = ipc_exec_cmd_ch_->send(cmd); + (void) result; + }, + [this](const ExecCmdChannel::Input& input) { + // + ::syslog(LOG_DEBUG, "D << 🔵 Ch Msg."); // NOLINT + + ::syslog(LOG_DEBUG, "D >> 🔵 Ch Msg."); // NOLINT + const int result = ipc_exec_cmd_ch_->send(input); + (void) result; + }, + [this](const ExecCmdChannel::Completed& completed) { + // + // NOLINTNEXTLINE + ::syslog(LOG_DEBUG, "D << 🔴 Ch Completed (err=%d).", static_cast(completed.error_code)); + ipc_exec_cmd_ch_.reset(); + }), + event_var); }); }); diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 198abee..88402b4 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -49,36 +49,42 @@ class DaemonImpl final : public Daemon return result; } - auto channel = ipc_router_->makeChannel("daemon"); - channel.subscribe([this](const auto& event_var) { + ipc_exec_cmd_ch_ = ipc_router_->makeChannel("daemon"); + ::syslog(LOG_DEBUG, "C << 🆕 Ch created."); // NOLINT + ipc_exec_cmd_ch_->subscribe([this](const auto& event_var) { // cetl::visit( // cetl::make_overloaded( [this](const ExecCmdChannel::Connected&) { // - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "🟢 Ch connected."); + ::syslog(LOG_DEBUG, "C << 🟢 Ch connected."); // NOLINT + ExecCmd cmd{&memory_}; cmd.some_stuff.push_back('A'); cmd.some_stuff.push_back('Z'); + ::syslog(LOG_DEBUG, "C >> 🔵 Ch Msg."); // NOLINT const int result = ipc_exec_cmd_ch_->send(cmd); (void) result; }, [this](const ExecCmdChannel::Input& input) { // - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "🔵 Ch Msg (%zu bytes).", input.some_stuff.size()); - const int result = ipc_exec_cmd_ch_->send(input); - (void) result; + ::syslog(LOG_DEBUG, "C << 🔵 Ch Msg."); // NOLINT + + if (countdown_--) + { + ::syslog(LOG_DEBUG, "C >> 🔵 Ch Msg."); // NOLINT + const int result = ipc_exec_cmd_ch_->send(input); + (void) result; + } }, - [](const ExecCmdChannel::Completed& completed) { + [this](const ExecCmdChannel::Completed& completed) { // - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "🔴 Ch Completed (err=%d).", static_cast(completed.error_code)); + // NOLINTNEXTLINE + ::syslog(LOG_DEBUG, "C << 🔴 Ch Completed (err=%d).", static_cast(completed.error_code)); + ipc_exec_cmd_ch_.reset(); }), event_var); }); - ipc_exec_cmd_ch_ = std::move(channel); return 0; } @@ -91,6 +97,8 @@ class DaemonImpl final : public Daemon common::ipc::ClientRouter::Ptr ipc_router_; cetl::optional ipc_exec_cmd_ch_; + int countdown_{2}; + }; // DaemonImpl } // namespace From aaecd152f6ad1cf00052ac4f16c9f3395b08cb5d Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 17:44:05 +0200 Subject: [PATCH 051/156] build fix --- src/daemon/engine/application.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 2b7c2bd..1d3fa7b 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -69,7 +69,7 @@ cetl::optional Application::init() ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); using Ch = ExecCmdChannel; - ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto& request) { + ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto&) { // ::syslog(LOG_DEBUG, "D << 🆕 Ch created."); // NOLINT ::syslog(LOG_DEBUG, "D << 🔵 Ch ininital msg."); // NOLINT @@ -84,8 +84,8 @@ cetl::optional Application::init() ::syslog(LOG_DEBUG, "D << 🟢 Ch connected."); // NOLINT ::syslog(LOG_DEBUG, "D >> 🔵 Ch Msg."); // NOLINT - ExecCmd cmd{&memory_}; - const int result = ipc_exec_cmd_ch_->send(cmd); + const ExecCmd cmd{&memory_}; + const int result = ipc_exec_cmd_ch_->send(cmd); (void) result; }, [this](const ExecCmdChannel::Input& input) { From 2cb01e776630e5739978515b493666b54b6a2aa6 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 18:56:01 +0200 Subject: [PATCH 052/156] added `payload_size` field --- .../ipc/{Route.1.0.dsdl => Route.0.1.dsdl} | 4 +- .../common/ipc/RouteChannelEnd.0.1.dsdl | 4 ++ .../common/ipc/RouteChannelEnd.1.0.dsdl | 5 -- .../common/ipc/RouteChannelMsg.0.1.dsdl | 6 +++ .../common/ipc/RouteChannelMsg.1.0.dsdl | 6 --- .../{ExecCmd.1.0.dsdl => ExecCmd.0.1.dsdl} | 0 src/common/ipc/client_router.cpp | 41 +++++++------- src/common/ipc/server_router.cpp | 43 +++++++-------- src/daemon/engine/application.cpp | 19 ++++--- src/daemon/engine/application.hpp | 4 +- src/sdk/daemon.cpp | 15 +++--- test/common/ipc/ipc_gtest_helpers.hpp | 54 +++++++++++-------- test/common/ipc/test_client_router.cpp | 20 +++---- test/common/ipc/test_server_router.cpp | 21 ++++---- 14 files changed, 129 insertions(+), 113 deletions(-) rename src/common/dsdl/ocvsmd/common/ipc/{Route.1.0.dsdl => Route.0.1.dsdl} (53%) create mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.0.1.dsdl delete mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.0.1.dsdl delete mode 100644 src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl rename src/common/dsdl/ocvsmd/common/node_command/{ExecCmd.1.0.dsdl => ExecCmd.0.1.dsdl} (100%) diff --git a/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/Route.0.1.dsdl similarity index 53% rename from src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl rename to src/common/dsdl/ocvsmd/common/ipc/Route.0.1.dsdl index 5cd25a5..ff3ea0b 100644 --- a/src/common/dsdl/ocvsmd/common/ipc/Route.1.0.dsdl +++ b/src/common/dsdl/ocvsmd/common/ipc/Route.0.1.dsdl @@ -2,7 +2,7 @@ uavcan.primitive.Empty.1.0 empty RouteConnect.0.1 connect -RouteChannelMsg.1.0 channel_msg -RouteChannelEnd.1.0 channel_end +RouteChannelMsg.0.1 channel_msg +RouteChannelEnd.0.1 channel_end @sealed diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.0.1.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.0.1.dsdl new file mode 100644 index 0000000..afe81eb --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.0.1.dsdl @@ -0,0 +1,4 @@ +uint64 tag +int32 error_code + +@extent 32 * 8 diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl deleted file mode 100644 index ebf9b13..0000000 --- a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelEnd.1.0.dsdl +++ /dev/null @@ -1,5 +0,0 @@ -uint64 tag -int32 error_code - -# reserve twice as much as we need. -@extent _offset_.max * 2 diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.0.1.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.0.1.dsdl new file mode 100644 index 0000000..0fb7fce --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.0.1.dsdl @@ -0,0 +1,6 @@ +uint64 tag +uint64 sequence +uint64 service_id +uint64 payload_size + +@extent 64 * 8 diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl deleted file mode 100644 index 0b6df88..0000000 --- a/src/common/dsdl/ocvsmd/common/ipc/RouteChannelMsg.1.0.dsdl +++ /dev/null @@ -1,6 +0,0 @@ -uint64 tag -uint64 sequence -uint64 service_id - -# reserve twice as much as we need. -@extent _offset_.max * 2 diff --git a/src/common/dsdl/ocvsmd/common/node_command/ExecCmd.1.0.dsdl b/src/common/dsdl/ocvsmd/common/node_command/ExecCmd.0.1.dsdl similarity index 100% rename from src/common/dsdl/ocvsmd/common/node_command/ExecCmd.1.0.dsdl rename to src/common/dsdl/ocvsmd/common/node_command/ExecCmd.0.1.dsdl diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 189549e..a1fada9 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -10,10 +10,10 @@ #include "ipc_types.hpp" #include "pipe/client_pipe.hpp" -#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" -#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" +#include "ocvsmd/common/ipc/RouteChannelEnd_0_1.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_0_1.hpp" #include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" -#include "ocvsmd/common/ipc/Route_1_0.hpp" +#include "ocvsmd/common/ipc/Route_0_1.hpp" #include "uavcan/primitive/Empty_1_0.hpp" #include @@ -138,12 +138,13 @@ class ClientRouterImpl final : public ClientRouter return static_cast(ErrorCode::Shutdown); } - Route_1_0 route{&router_.memory_}; + Route_0_1 route{&router_.memory_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.tag; - channel_msg.sequence = next_sequence_++; - channel_msg.service_id = service_id; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = endpoint_.tag; + channel_msg.sequence = next_sequence_++; + channel_msg.service_id = service_id; + channel_msg.payload_size = payload.size(); return tryPerformOnSerialized(route, [this, payload](const auto prefix) { // @@ -248,7 +249,7 @@ class ClientRouterImpl final : public ClientRouter // if (was_registered && send_ch_end && isConnected(endpoint)) { - Route_1_0 route{&memory_}; + Route_0_1 route{&memory_}; auto& channel_end = route.set_channel_end(); channel_end.tag = endpoint.tag; channel_end.error_code = 0; // No error b/c it's a normal channel completion. @@ -269,7 +270,7 @@ class ClientRouterImpl final : public ClientRouter // It's not enough to consider the server route connected by the pipe event. // We gonna initiate `RouteConnect` negotiation (see `handleRouteConnect`). // - Route_1_0 route{&memory_}; + Route_0_1 route{&memory_}; auto& route_conn = route.set_connect(); route_conn.version.major = VERSION_MAJOR; route_conn.version.minor = VERSION_MINOR; @@ -282,7 +283,7 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Message& msg) { - Route_1_0 route_msg{&memory_}; + Route_0_1 route_msg{&memory_}; const auto result_size = tryDeserializePayload(msg.payload, route_msg); if (!result_size.has_value()) { @@ -290,9 +291,6 @@ class ClientRouterImpl final : public ClientRouter return EINVAL; } - // Cut routing stuff from the payload - remaining is the actual message payload. - const auto msg_payload = msg.payload.subspan(result_size.value()); - return cetl::visit( // cetl::make_overloaded( // [this](const uavcan::primitive::Empty_1_0&) { @@ -305,11 +303,11 @@ class ClientRouterImpl final : public ClientRouter // return handleRouteConnect(route_conn); }, - [this, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { + [this, &msg](const RouteChannelMsg_0_1& route_ch_msg) { // - return handleRouteChannelMsg(route_ch_msg, msg_payload); + return handleRouteChannelMsg(route_ch_msg, msg.payload); }, - [this](const RouteChannelEnd_1_0& route_ch_end) { + [this](const RouteChannelEnd_0_1& route_ch_end) { // return handleRouteChannelEnd(route_ch_end); }), @@ -367,8 +365,11 @@ class ClientRouterImpl final : public ClientRouter return 0; } - CETL_NODISCARD int handleRouteChannelMsg(const RouteChannelMsg_1_0& route_ch_msg, const Payload payload) + CETL_NODISCARD int handleRouteChannelMsg(const RouteChannelMsg_0_1& route_ch_msg, const Payload payload) { + // Cut routing stuff from the payload - remaining is the real message payload. + const auto msg_real_payload = payload.subspan(payload.size() - route_ch_msg.payload_size); + const Endpoint endpoint{route_ch_msg.tag}; const auto tag_to_gw = map_of_gateways_.find(endpoint.tag); @@ -376,7 +377,7 @@ class ClientRouterImpl final : public ClientRouter { if (const auto gateway = tag_to_gw->second.lock()) { - return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, payload}); + return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_real_payload}); } } @@ -384,7 +385,7 @@ class ClientRouterImpl final : public ClientRouter return 0; } - CETL_NODISCARD int handleRouteChannelEnd(const RouteChannelEnd_1_0& route_ch_end) + CETL_NODISCARD int handleRouteChannelEnd(const RouteChannelEnd_0_1& route_ch_end) { ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 4975dc7..cfb57ed 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -10,9 +10,9 @@ #include "ipc_types.hpp" #include "pipe/server_pipe.hpp" -#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_0_1.hpp" #include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" -#include "ocvsmd/common/ipc/Route_1_0.hpp" +#include "ocvsmd/common/ipc/Route_0_1.hpp" #include "uavcan/primitive/Empty_1_0.hpp" #include @@ -130,12 +130,13 @@ class ServerRouterImpl final : public ServerRouter return static_cast(ErrorCode::Shutdown); } - Route_1_0 route{&router_.memory_}; + Route_0_1 route{&router_.memory_}; - auto& channel_msg = route.set_channel_msg(); - channel_msg.tag = endpoint_.tag; - channel_msg.sequence = next_sequence_++; - channel_msg.service_id = service_id; + auto& channel_msg = route.set_channel_msg(); + channel_msg.tag = endpoint_.tag; + channel_msg.sequence = next_sequence_++; + channel_msg.service_id = service_id; + channel_msg.payload_size = payload.size(); return tryPerformOnSerialized(route, [this, payload](const auto prefix) { // @@ -237,7 +238,7 @@ class ServerRouterImpl final : public ServerRouter // if (was_registered && isConnected(endpoint)) { - Route_1_0 route{&memory_}; + Route_0_1 route{&memory_}; auto& channel_end = route.set_channel_end(); channel_end.tag = endpoint.tag; channel_end.error_code = 0; // No error b/c it's a normal channel completion. @@ -264,7 +265,7 @@ class ServerRouterImpl final : public ServerRouter CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Message& msg) { - Route_1_0 route_msg{&memory_}; + Route_0_1 route_msg{&memory_}; const auto result_size = tryDeserializePayload(msg.payload, route_msg); if (!result_size.has_value()) { @@ -272,9 +273,6 @@ class ServerRouterImpl final : public ServerRouter return EINVAL; } - // Cut routing stuff from the payload - remaining is the actual message payload. - const auto msg_payload = msg.payload.subspan(result_size.value()); - return cetl::visit( // cetl::make_overloaded( // [this](const uavcan::primitive::Empty_1_0&) { @@ -287,11 +285,11 @@ class ServerRouterImpl final : public ServerRouter // return handleRouteConnect(msg.client_id, route_conn); }, - [this, &msg, msg_payload](const RouteChannelMsg_1_0& route_ch_msg) { + [this, &msg](const RouteChannelMsg_0_1& route_ch_msg) { // - return handleRouteChannelMsg(msg.client_id, route_ch_msg, msg_payload); + return handleRouteChannelMsg(msg.client_id, route_ch_msg, msg.payload); }, - [this, &msg](const RouteChannelEnd_1_0& route_ch_end) { + [this, &msg](const RouteChannelEnd_0_1& route_ch_end) { // return handleRouteChannelEnd(msg.client_id, route_ch_end); }), @@ -333,7 +331,7 @@ class ServerRouterImpl final : public ServerRouter static_cast(rt_conn.version.minor), static_cast(rt_conn.error_code)); - Route_1_0 route{&memory_}; + Route_0_1 route{&memory_}; auto& route_conn = route.set_connect(); route_conn.version.major = VERSION_MAJOR; route_conn.version.minor = VERSION_MINOR; @@ -353,9 +351,12 @@ class ServerRouterImpl final : public ServerRouter } CETL_NODISCARD int handleRouteChannelMsg(const pipe::ServerPipe::ClientId client_id, - const RouteChannelMsg_1_0& route_ch_msg, - const Payload msg_payload) + const RouteChannelMsg_0_1& route_ch_msg, + const Payload payload) { + // Cut routing stuff from the payload - remaining is the real message payload. + const auto msg_real_payload = payload.subspan(payload.size() - route_ch_msg.payload_size); + const auto cl_to_gws = client_id_to_map_of_gateways_.find(client_id); if (cl_to_gws != client_id_to_map_of_gateways_.end()) { @@ -366,7 +367,7 @@ class ServerRouterImpl final : public ServerRouter { if (auto gateway = tag_to_gw->second.lock()) { - return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_payload}); + return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_real_payload}); } } @@ -381,7 +382,7 @@ class ServerRouterImpl final : public ServerRouter auto gateway = GatewayImpl::create(*this, endpoint); map_of_gws[route_ch_msg.tag] = gateway; - si_to_ch_factory->second(gateway, msg_payload); + si_to_ch_factory->second(gateway, msg_real_payload); } } } @@ -391,7 +392,7 @@ class ServerRouterImpl final : public ServerRouter } CETL_NODISCARD int handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, - const RouteChannelEnd_1_0& route_ch_end) + const RouteChannelEnd_0_1& route_ch_end) { ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 1d3fa7b..9ff7423 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -69,10 +69,10 @@ cetl::optional Application::init() ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); using Ch = ExecCmdChannel; - ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto&) { + ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto& request) { // - ::syslog(LOG_DEBUG, "D << 🆕 Ch created."); // NOLINT - ::syslog(LOG_DEBUG, "D << 🔵 Ch ininital msg."); // NOLINT + ::syslog(LOG_DEBUG, "D << 🆕 Ch created."); // NOLINT + ::syslog(LOG_DEBUG, "D << 🔵 Ch ininital Msg='%s'.", request.some_stuff.data()); // NOLINT ipc_exec_cmd_ch_ = std::move(ch); ipc_exec_cmd_ch_->subscribe([this](const auto& event_var) { @@ -83,16 +83,19 @@ cetl::optional Application::init() // ::syslog(LOG_DEBUG, "D << 🟢 Ch connected."); // NOLINT - ::syslog(LOG_DEBUG, "D >> 🔵 Ch Msg."); // NOLINT - const ExecCmd cmd{&memory_}; - const int result = ipc_exec_cmd_ch_->send(cmd); + ::syslog(LOG_DEBUG, "D >> 🔵 Ch 'SR' msg."); // NOLINT + ExecCmd cmd{&memory_}; + cmd.some_stuff.push_back('S'); + cmd.some_stuff.push_back('R'); + cmd.some_stuff.push_back('\0'); + const int result = ipc_exec_cmd_ch_->send(cmd); (void) result; }, [this](const ExecCmdChannel::Input& input) { // - ::syslog(LOG_DEBUG, "D << 🔵 Ch Msg."); // NOLINT + ::syslog(LOG_DEBUG, "D << 🔵 Ch Msg='%s'.", input.some_stuff.data()); // NOLINT - ::syslog(LOG_DEBUG, "D >> 🔵 Ch Msg."); // NOLINT + ::syslog(LOG_DEBUG, "D >> 🔵 Ch '%s' msg.", input.some_stuff.data()); // NOLINT const int result = ipc_exec_cmd_ch_->send(input); (void) result; }, diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/application.hpp index 0213854..2a37da9 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/application.hpp @@ -9,7 +9,7 @@ #include "cyphal/udp_transport_bag.hpp" #include "ocvsmd/platform/defines.hpp" -#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" +#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" #include #include @@ -39,7 +39,7 @@ class Application private: // TODO: temp stuff - using ExecCmd = common::node_command::ExecCmd_1_0; + using ExecCmd = common::node_command::ExecCmd_0_1; using ExecCmdChannel = common::ipc::Channel; cetl::optional ipc_exec_cmd_ch_; diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 88402b4..6e6ce55 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -10,7 +10,7 @@ #include "ipc/pipe/client_pipe.hpp" #include "ipc/pipe/unix_socket_client.hpp" -#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" +#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" #include #include @@ -60,19 +60,20 @@ class DaemonImpl final : public Daemon ::syslog(LOG_DEBUG, "C << 🟢 Ch connected."); // NOLINT ExecCmd cmd{&memory_}; - cmd.some_stuff.push_back('A'); - cmd.some_stuff.push_back('Z'); - ::syslog(LOG_DEBUG, "C >> 🔵 Ch Msg."); // NOLINT + cmd.some_stuff.push_back('C'); + cmd.some_stuff.push_back('L'); + cmd.some_stuff.push_back('\0'); + ::syslog(LOG_DEBUG, "C >> 🔵 Ch 'CL' msg."); // NOLINT const int result = ipc_exec_cmd_ch_->send(cmd); (void) result; }, [this](const ExecCmdChannel::Input& input) { // - ::syslog(LOG_DEBUG, "C << 🔵 Ch Msg."); // NOLINT + ::syslog(LOG_DEBUG, "C << 🔵 Ch Msg='%s'.", input.some_stuff.data()); // NOLINT if (countdown_--) { - ::syslog(LOG_DEBUG, "C >> 🔵 Ch Msg."); // NOLINT + ::syslog(LOG_DEBUG, "C >> 🔵 Ch '%s' msg.", input.some_stuff.data()); // NOLINT const int result = ipc_exec_cmd_ch_->send(input); (void) result; } @@ -90,7 +91,7 @@ class DaemonImpl final : public Daemon } private: - using ExecCmd = common::node_command::ExecCmd_1_0; + using ExecCmd = common::node_command::ExecCmd_0_1; using ExecCmdChannel = common::ipc::Channel; cetl::pmr::memory_resource& memory_; diff --git a/test/common/ipc/ipc_gtest_helpers.hpp b/test/common/ipc/ipc_gtest_helpers.hpp index e746b6f..ce81073 100644 --- a/test/common/ipc/ipc_gtest_helpers.hpp +++ b/test/common/ipc/ipc_gtest_helpers.hpp @@ -9,10 +9,10 @@ #include "dsdl_helpers.hpp" #include "ipc/ipc_types.hpp" -#include "ocvsmd/common/ipc/RouteChannelEnd_1_0.hpp" -#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" +#include "ocvsmd/common/ipc/RouteChannelEnd_0_1.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_0_1.hpp" #include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" -#include "ocvsmd/common/ipc/Route_1_0.hpp" +#include "ocvsmd/common/ipc/Route_0_1.hpp" #include #include @@ -56,20 +56,20 @@ inline void PrintTo(const RouteConnect_0_1& conn, std::ostream* os) *os << "}"; } -inline void PrintTo(const RouteChannelMsg_1_0& msg, std::ostream* os) +inline void PrintTo(const RouteChannelMsg_0_1& msg, std::ostream* os) { *os << "RouteChannelMsg_1_0{tag=" << msg.tag << ", seq=" << msg.sequence << ", srv=0x" << std::hex << msg.service_id - << "}"; + << ", payload_size=" << msg.payload_size << "}"; } -inline void PrintTo(const RouteChannelEnd_1_0& msg, std::ostream* os) +inline void PrintTo(const RouteChannelEnd_0_1& msg, std::ostream* os) { - *os << "RouteChannelEnd_1_0{tag=" << msg.tag << ", err=" << msg.error_code << "}"; + *os << "RouteChannelEnd_0_1{tag=" << msg.tag << ", err=" << msg.error_code << "}"; } -inline void PrintTo(const Route_1_0& route, std::ostream* os) +inline void PrintTo(const Route_0_1& route, std::ostream* os) { - *os << "Route_1_0{"; + *os << "Route_0_1{"; cetl::visit([os](const auto& v) { PrintTo(v, os); }, route.union_value); *os << "}"; } @@ -81,12 +81,13 @@ inline bool operator==(const RouteConnect_0_1& lhs, const RouteConnect_0_1& rhs) return lhs.version.major == rhs.version.major && lhs.version.minor == rhs.version.minor; } -inline bool operator==(const RouteChannelMsg_1_0& lhs, const RouteChannelMsg_1_0& rhs) +inline bool operator==(const RouteChannelMsg_0_1& lhs, const RouteChannelMsg_0_1& rhs) { - return lhs.tag == rhs.tag && lhs.sequence == rhs.sequence && lhs.service_id == rhs.service_id; + return lhs.tag == rhs.tag && lhs.sequence == rhs.sequence && lhs.service_id == rhs.service_id && + lhs.payload_size == rhs.payload_size; } -inline bool operator==(const RouteChannelEnd_1_0& lhs, const RouteChannelEnd_1_0& rhs) +inline bool operator==(const RouteChannelEnd_0_1& lhs, const RouteChannelEnd_0_1& rhs) { return lhs.tag == rhs.tag && lhs.error_code == rhs.error_code; } @@ -170,25 +171,34 @@ inline auto PayloadOfRouteConnect(cetl::pmr::memory_resource& mr, ErrorCode error_code = ErrorCode::Success) { const RouteConnect_0_1 route_conn{{ver_major, ver_minor, &mr}, static_cast(error_code), &mr}; - return PayloadWith(testing::VariantWith(route_conn), mr); + return PayloadWith(testing::VariantWith(route_conn), mr); } template -auto PayloadOfRouteChannel(cetl::pmr::memory_resource& mr, - const std::uint64_t tag, - const std::uint64_t seq, - const cetl::string_view srv_name = "") -{ - const RouteChannelMsg_1_0 msg{tag, seq, AnyChannel::getServiceId(srv_name), &mr}; - return PayloadWith(testing::VariantWith(msg), mr); +auto PayloadOfRouteChannelMsg(const Msg& msg, + cetl::pmr::memory_resource& mr, + const std::uint64_t tag, + const std::uint64_t seq, + const cetl::string_view srv_name = "") +{ + RouteChannelMsg_0_1 route_ch_msg{tag, seq, AnyChannel::getServiceId(srv_name), 0, &mr}; + EXPECT_THAT(tryPerformOnSerialized( // + msg, + [&route_ch_msg](const auto payload) { + // + route_ch_msg.payload_size = payload.size(); + return 0; + }), + 0); + return PayloadWith(testing::VariantWith(route_ch_msg), mr); } inline auto PayloadOfRouteChannelEnd(cetl::pmr::memory_resource& mr, // const std::uint64_t tag, const ErrorCode error_code) { - const RouteChannelEnd_1_0 ch_end{{tag, static_cast(error_code), &mr}, &mr}; - return PayloadWith(testing::VariantWith(ch_end), mr); + const RouteChannelEnd_0_1 ch_end{{tag, static_cast(error_code), &mr}, &mr}; + return PayloadWith(testing::VariantWith(ch_end), mr); } } // namespace ipc diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 0362692..5c5cf67 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -13,10 +13,10 @@ #include "pipe/client_pipe_mock.hpp" #include "tracking_memory_resource.hpp" -#include "ocvsmd/common/ipc/RouteChannelMsg_1_0.hpp" +#include "ocvsmd/common/ipc/RouteChannelMsg_0_1.hpp" #include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" -#include "ocvsmd/common/ipc/Route_1_0.hpp" -#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" +#include "ocvsmd/common/ipc/Route_0_1.hpp" +#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" #include @@ -72,7 +72,7 @@ class TestClientRouter : public testing::Test .WillOnce(Return(0)); client_pipe_mock.event_handler_(pipe::ClientPipe::Event::Connected{}); - Route_1_0 route{&mr_}; + Route_0_1 route{&mr_}; auto& rt_conn = route.set_connect(); rt_conn.version.major = ver_major; rt_conn.version.minor = ver_minor; @@ -94,7 +94,7 @@ class TestClientRouter : public testing::Test { using ocvsmd::common::tryPerformOnSerialized; - Route_1_0 route{&mr_}; + Route_0_1 route{&mr_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag; channel_msg.sequence = seq++; @@ -155,7 +155,7 @@ TEST_F(TestClientRouter, start) TEST_F(TestClientRouter, makeChannel) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_0_1; using Channel = Channel; StrictMock client_pipe_mock; @@ -175,7 +175,7 @@ TEST_F(TestClientRouter, makeChannel) TEST_F(TestClientRouter, makeChannel_send) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_0_1; using Channel = Channel; StrictMock client_pipe_mock; @@ -198,11 +198,11 @@ TEST_F(TestClientRouter, makeChannel_send) const std::uint64_t tag = 0; std::uint64_t seq = 0; - EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))) // + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannelMsg(msg, mr_, tag, seq++))) // .WillOnce(Return(0)); EXPECT_THAT(channel.send(msg), 0); - EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannel(mr_, tag, seq++))) // + EXPECT_CALL(client_pipe_mock, send(PayloadOfRouteChannelMsg(msg, mr_, tag, seq++))) // .WillOnce(Return(0)); EXPECT_THAT(channel.send(msg), 0); @@ -212,7 +212,7 @@ TEST_F(TestClientRouter, makeChannel_send) TEST_F(TestClientRouter, makeChannel_receive_events) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_0_1; using Channel = Channel; StrictMock client_pipe_mock; diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 7ef639d..a011ce4 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -12,8 +12,8 @@ #include "pipe/server_pipe_mock.hpp" #include "tracking_memory_resource.hpp" -#include "ocvsmd/common/ipc/Route_1_0.hpp" -#include "ocvsmd/common/node_command/ExecCmd_1_0.hpp" +#include "ocvsmd/common/ipc/Route_0_1.hpp" +#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" #include @@ -67,7 +67,7 @@ class TestServerRouter : public testing::Test server_pipe_mock.event_handler_(pipe::ServerPipe::Event::Connected{client_id}); - Route_1_0 route{&mr_}; + Route_0_1 route{&mr_}; auto& rt_conn = route.set_connect(); rt_conn.version.major = ver_major; rt_conn.version.minor = ver_minor; @@ -92,7 +92,7 @@ class TestServerRouter : public testing::Test { using ocvsmd::common::tryPerformOnSerialized; - Route_1_0 route{&mr_}; + Route_0_1 route{&mr_}; auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag; channel_msg.sequence = seq++; @@ -151,7 +151,7 @@ TEST_F(TestServerRouter, start) TEST_F(TestServerRouter, registerChannel) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_0_1; using Channel = Channel; StrictMock server_pipe_mock; @@ -172,7 +172,7 @@ TEST_F(TestServerRouter, registerChannel) TEST_F(TestServerRouter, channel_send) { - using Msg = ocvsmd::common::node_command::ExecCmd_1_0; + using Msg = ocvsmd::common::node_command::ExecCmd_0_1; using Channel = Channel; StrictMock server_pipe_mock; @@ -221,13 +221,14 @@ TEST_F(TestServerRouter, channel_send) emulateRouteChannelMsg(cl_id, server_pipe_mock, tag, Channel::Input{&mr_}, seq); seq = 0; - EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannel(mr_, tag, seq++))) // + const Channel::Output msg{&mr_}; + EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannelMsg(msg, mr_, tag, seq++))) // .WillOnce(Return(0)); - EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); + EXPECT_THAT(maybe_channel->send(msg), 0); - EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannel(mr_, tag, seq++))) // + EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannelMsg(msg, mr_, tag, seq++))) // .WillOnce(Return(0)); - EXPECT_THAT(maybe_channel->send(Channel::Output{&mr_}), 0); + EXPECT_THAT(maybe_channel->send(msg), 0); } // NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) From 77a1a121ac814f2c53881720b71d43ca022ad6db Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 10 Jan 2025 19:03:35 +0200 Subject: [PATCH 053/156] fix build --- src/common/ipc/client_router.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index a1fada9..c0591f8 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -21,7 +21,6 @@ #include #include -#include #include #include #include From a067fae6c1e6b530166ddf0a1e9d03bf0b57a93e Mon Sep 17 00:00:00 2001 From: Sergei Date: Sat, 11 Jan 2025 01:18:27 +0200 Subject: [PATCH 054/156] added spdlog 1.15.0 --- CMakeLists.txt | 2 +- include/spdlog/async.h | 100 + include/spdlog/async_logger-inl.h | 84 + include/spdlog/async_logger.h | 74 + include/spdlog/cfg/argv.h | 40 + include/spdlog/cfg/env.h | 36 + include/spdlog/cfg/helpers-inl.h | 107 + include/spdlog/cfg/helpers.h | 29 + include/spdlog/common-inl.h | 68 + include/spdlog/common.h | 406 ++ include/spdlog/details/backtracer-inl.h | 63 + include/spdlog/details/backtracer.h | 45 + include/spdlog/details/circular_q.h | 115 + include/spdlog/details/console_globals.h | 28 + include/spdlog/details/file_helper-inl.h | 153 + include/spdlog/details/file_helper.h | 61 + include/spdlog/details/fmt_helper.h | 141 + include/spdlog/details/log_msg-inl.h | 44 + include/spdlog/details/log_msg.h | 40 + include/spdlog/details/log_msg_buffer-inl.h | 54 + include/spdlog/details/log_msg_buffer.h | 32 + include/spdlog/details/mpmc_blocking_q.h | 177 + include/spdlog/details/null_mutex.h | 35 + include/spdlog/details/os-inl.h | 606 +++ include/spdlog/details/os.h | 127 + include/spdlog/details/periodic_worker-inl.h | 26 + include/spdlog/details/periodic_worker.h | 58 + include/spdlog/details/registry-inl.h | 261 + include/spdlog/details/registry.h | 129 + include/spdlog/details/synchronous_factory.h | 22 + include/spdlog/details/tcp_client-windows.h | 135 + include/spdlog/details/tcp_client.h | 127 + include/spdlog/details/thread_pool-inl.h | 127 + include/spdlog/details/thread_pool.h | 117 + include/spdlog/details/udp_client-windows.h | 98 + include/spdlog/details/udp_client.h | 81 + include/spdlog/details/windows_include.h | 11 + include/spdlog/fmt/bin_to_hex.h | 224 + include/spdlog/fmt/bundled/args.h | 228 + include/spdlog/fmt/bundled/base.h | 3077 ++++++++++++ include/spdlog/fmt/bundled/chrono.h | 2432 +++++++++ include/spdlog/fmt/bundled/color.h | 612 +++ include/spdlog/fmt/bundled/compile.h | 529 ++ include/spdlog/fmt/bundled/core.h | 5 + include/spdlog/fmt/bundled/fmt.license.rst | 27 + include/spdlog/fmt/bundled/format-inl.h | 1928 +++++++ include/spdlog/fmt/bundled/format.h | 4427 +++++++++++++++++ include/spdlog/fmt/bundled/locale.h | 2 + include/spdlog/fmt/bundled/os.h | 439 ++ include/spdlog/fmt/bundled/ostream.h | 211 + include/spdlog/fmt/bundled/printf.h | 656 +++ include/spdlog/fmt/bundled/ranges.h | 882 ++++ include/spdlog/fmt/bundled/std.h | 699 +++ include/spdlog/fmt/bundled/xchar.h | 322 ++ include/spdlog/fmt/chrono.h | 23 + include/spdlog/fmt/compile.h | 23 + include/spdlog/fmt/fmt.h | 31 + include/spdlog/fmt/ostr.h | 23 + include/spdlog/fmt/ranges.h | 23 + include/spdlog/fmt/std.h | 24 + include/spdlog/fmt/xchar.h | 23 + include/spdlog/formatter.h | 17 + include/spdlog/fwd.h | 18 + include/spdlog/logger-inl.h | 198 + include/spdlog/logger.h | 379 ++ include/spdlog/mdc.h | 50 + include/spdlog/pattern_formatter-inl.h | 1338 +++++ include/spdlog/pattern_formatter.h | 118 + include/spdlog/sinks/android_sink.h | 137 + include/spdlog/sinks/ansicolor_sink-inl.h | 135 + include/spdlog/sinks/ansicolor_sink.h | 115 + include/spdlog/sinks/base_sink-inl.h | 59 + include/spdlog/sinks/base_sink.h | 51 + include/spdlog/sinks/basic_file_sink-inl.h | 48 + include/spdlog/sinks/basic_file_sink.h | 66 + include/spdlog/sinks/callback_sink.h | 56 + include/spdlog/sinks/daily_file_sink.h | 254 + include/spdlog/sinks/dist_sink.h | 81 + include/spdlog/sinks/dup_filter_sink.h | 92 + include/spdlog/sinks/hourly_file_sink.h | 193 + include/spdlog/sinks/kafka_sink.h | 119 + include/spdlog/sinks/mongo_sink.h | 108 + include/spdlog/sinks/msvc_sink.h | 68 + include/spdlog/sinks/null_sink.h | 41 + include/spdlog/sinks/ostream_sink.h | 43 + include/spdlog/sinks/qt_sinks.h | 304 ++ include/spdlog/sinks/ringbuffer_sink.h | 67 + include/spdlog/sinks/rotating_file_sink-inl.h | 150 + include/spdlog/sinks/rotating_file_sink.h | 90 + include/spdlog/sinks/sink-inl.h | 22 + include/spdlog/sinks/sink.h | 34 + include/spdlog/sinks/stdout_color_sinks-inl.h | 38 + include/spdlog/sinks/stdout_color_sinks.h | 49 + include/spdlog/sinks/stdout_sinks-inl.h | 127 + include/spdlog/sinks/stdout_sinks.h | 84 + include/spdlog/sinks/syslog_sink.h | 104 + include/spdlog/sinks/systemd_sink.h | 121 + include/spdlog/sinks/tcp_sink.h | 75 + include/spdlog/sinks/udp_sink.h | 69 + include/spdlog/sinks/win_eventlog_sink.h | 260 + include/spdlog/sinks/wincolor_sink-inl.h | 172 + include/spdlog/sinks/wincolor_sink.h | 82 + include/spdlog/spdlog-inl.h | 92 + include/spdlog/spdlog.h | 352 ++ include/spdlog/stopwatch.h | 66 + include/spdlog/tweakme.h | 141 + include/spdlog/version.h | 11 + 107 files changed, 27022 insertions(+), 1 deletion(-) create mode 100644 include/spdlog/async.h create mode 100644 include/spdlog/async_logger-inl.h create mode 100644 include/spdlog/async_logger.h create mode 100644 include/spdlog/cfg/argv.h create mode 100644 include/spdlog/cfg/env.h create mode 100644 include/spdlog/cfg/helpers-inl.h create mode 100644 include/spdlog/cfg/helpers.h create mode 100644 include/spdlog/common-inl.h create mode 100644 include/spdlog/common.h create mode 100644 include/spdlog/details/backtracer-inl.h create mode 100644 include/spdlog/details/backtracer.h create mode 100644 include/spdlog/details/circular_q.h create mode 100644 include/spdlog/details/console_globals.h create mode 100644 include/spdlog/details/file_helper-inl.h create mode 100644 include/spdlog/details/file_helper.h create mode 100644 include/spdlog/details/fmt_helper.h create mode 100644 include/spdlog/details/log_msg-inl.h create mode 100644 include/spdlog/details/log_msg.h create mode 100644 include/spdlog/details/log_msg_buffer-inl.h create mode 100644 include/spdlog/details/log_msg_buffer.h create mode 100644 include/spdlog/details/mpmc_blocking_q.h create mode 100644 include/spdlog/details/null_mutex.h create mode 100644 include/spdlog/details/os-inl.h create mode 100644 include/spdlog/details/os.h create mode 100644 include/spdlog/details/periodic_worker-inl.h create mode 100644 include/spdlog/details/periodic_worker.h create mode 100644 include/spdlog/details/registry-inl.h create mode 100644 include/spdlog/details/registry.h create mode 100644 include/spdlog/details/synchronous_factory.h create mode 100644 include/spdlog/details/tcp_client-windows.h create mode 100644 include/spdlog/details/tcp_client.h create mode 100644 include/spdlog/details/thread_pool-inl.h create mode 100644 include/spdlog/details/thread_pool.h create mode 100644 include/spdlog/details/udp_client-windows.h create mode 100644 include/spdlog/details/udp_client.h create mode 100644 include/spdlog/details/windows_include.h create mode 100644 include/spdlog/fmt/bin_to_hex.h create mode 100644 include/spdlog/fmt/bundled/args.h create mode 100644 include/spdlog/fmt/bundled/base.h create mode 100644 include/spdlog/fmt/bundled/chrono.h create mode 100644 include/spdlog/fmt/bundled/color.h create mode 100644 include/spdlog/fmt/bundled/compile.h create mode 100644 include/spdlog/fmt/bundled/core.h create mode 100644 include/spdlog/fmt/bundled/fmt.license.rst create mode 100644 include/spdlog/fmt/bundled/format-inl.h create mode 100644 include/spdlog/fmt/bundled/format.h create mode 100644 include/spdlog/fmt/bundled/locale.h create mode 100644 include/spdlog/fmt/bundled/os.h create mode 100644 include/spdlog/fmt/bundled/ostream.h create mode 100644 include/spdlog/fmt/bundled/printf.h create mode 100644 include/spdlog/fmt/bundled/ranges.h create mode 100644 include/spdlog/fmt/bundled/std.h create mode 100644 include/spdlog/fmt/bundled/xchar.h create mode 100644 include/spdlog/fmt/chrono.h create mode 100644 include/spdlog/fmt/compile.h create mode 100644 include/spdlog/fmt/fmt.h create mode 100644 include/spdlog/fmt/ostr.h create mode 100644 include/spdlog/fmt/ranges.h create mode 100644 include/spdlog/fmt/std.h create mode 100644 include/spdlog/fmt/xchar.h create mode 100644 include/spdlog/formatter.h create mode 100644 include/spdlog/fwd.h create mode 100644 include/spdlog/logger-inl.h create mode 100644 include/spdlog/logger.h create mode 100644 include/spdlog/mdc.h create mode 100644 include/spdlog/pattern_formatter-inl.h create mode 100644 include/spdlog/pattern_formatter.h create mode 100644 include/spdlog/sinks/android_sink.h create mode 100644 include/spdlog/sinks/ansicolor_sink-inl.h create mode 100644 include/spdlog/sinks/ansicolor_sink.h create mode 100644 include/spdlog/sinks/base_sink-inl.h create mode 100644 include/spdlog/sinks/base_sink.h create mode 100644 include/spdlog/sinks/basic_file_sink-inl.h create mode 100644 include/spdlog/sinks/basic_file_sink.h create mode 100644 include/spdlog/sinks/callback_sink.h create mode 100644 include/spdlog/sinks/daily_file_sink.h create mode 100644 include/spdlog/sinks/dist_sink.h create mode 100644 include/spdlog/sinks/dup_filter_sink.h create mode 100644 include/spdlog/sinks/hourly_file_sink.h create mode 100644 include/spdlog/sinks/kafka_sink.h create mode 100644 include/spdlog/sinks/mongo_sink.h create mode 100644 include/spdlog/sinks/msvc_sink.h create mode 100644 include/spdlog/sinks/null_sink.h create mode 100644 include/spdlog/sinks/ostream_sink.h create mode 100644 include/spdlog/sinks/qt_sinks.h create mode 100644 include/spdlog/sinks/ringbuffer_sink.h create mode 100644 include/spdlog/sinks/rotating_file_sink-inl.h create mode 100644 include/spdlog/sinks/rotating_file_sink.h create mode 100644 include/spdlog/sinks/sink-inl.h create mode 100644 include/spdlog/sinks/sink.h create mode 100644 include/spdlog/sinks/stdout_color_sinks-inl.h create mode 100644 include/spdlog/sinks/stdout_color_sinks.h create mode 100644 include/spdlog/sinks/stdout_sinks-inl.h create mode 100644 include/spdlog/sinks/stdout_sinks.h create mode 100644 include/spdlog/sinks/syslog_sink.h create mode 100644 include/spdlog/sinks/systemd_sink.h create mode 100644 include/spdlog/sinks/tcp_sink.h create mode 100644 include/spdlog/sinks/udp_sink.h create mode 100644 include/spdlog/sinks/win_eventlog_sink.h create mode 100644 include/spdlog/sinks/wincolor_sink-inl.h create mode 100644 include/spdlog/sinks/wincolor_sink.h create mode 100644 include/spdlog/spdlog-inl.h create mode 100644 include/spdlog/spdlog.h create mode 100644 include/spdlog/stopwatch.h create mode 100644 include/spdlog/tweakme.h create mode 100644 include/spdlog/version.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c9d72e5..82ce964 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ if (NOT clang_format) message(STATUS "Could not locate clang-format") else () file(GLOB_RECURSE format_files - ${include_dir}/*.hpp + ${include_dir}/ocvsmd/*.hpp ${src_dir}/*.[ch] ${src_dir}/*.[ch]pp ${test_dir}/*.[ch]pp diff --git a/include/spdlog/async.h b/include/spdlog/async.h new file mode 100644 index 0000000..e96abd1 --- /dev/null +++ b/include/spdlog/async.h @@ -0,0 +1,100 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// +// Async logging using global thread pool +// All loggers created here share same global thread pool. +// Each log message is pushed to a queue along with a shared pointer to the +// logger. +// If a logger deleted while having pending messages in the queue, it's actual +// destruction will defer +// until all its messages are processed by the thread pool. +// This is because each message in the queue holds a shared_ptr to the +// originating logger. + +#include +#include +#include + +#include +#include +#include + +namespace spdlog { + +namespace details { +static const size_t default_async_q_size = 8192; +} + +// async logger factory - creates async loggers backed with thread pool. +// if a global thread pool doesn't already exist, create it with default queue +// size of 8192 items and single thread. +template +struct async_factory_impl { + template + static std::shared_ptr create(std::string logger_name, SinkArgs &&...args) { + auto ®istry_inst = details::registry::instance(); + + // create global thread pool if not already exists.. + + auto &mutex = registry_inst.tp_mutex(); + std::lock_guard tp_lock(mutex); + auto tp = registry_inst.get_tp(); + if (tp == nullptr) { + tp = std::make_shared(details::default_async_q_size, 1U); + registry_inst.set_tp(tp); + } + + auto sink = std::make_shared(std::forward(args)...); + auto new_logger = std::make_shared(std::move(logger_name), std::move(sink), + std::move(tp), OverflowPolicy); + registry_inst.initialize_logger(new_logger); + return new_logger; + } +}; + +using async_factory = async_factory_impl; +using async_factory_nonblock = async_factory_impl; + +template +inline std::shared_ptr create_async(std::string logger_name, + SinkArgs &&...sink_args) { + return async_factory::create(std::move(logger_name), + std::forward(sink_args)...); +} + +template +inline std::shared_ptr create_async_nb(std::string logger_name, + SinkArgs &&...sink_args) { + return async_factory_nonblock::create(std::move(logger_name), + std::forward(sink_args)...); +} + +// set global thread pool. +inline void init_thread_pool(size_t q_size, + size_t thread_count, + std::function on_thread_start, + std::function on_thread_stop) { + auto tp = std::make_shared(q_size, thread_count, on_thread_start, + on_thread_stop); + details::registry::instance().set_tp(std::move(tp)); +} + +inline void init_thread_pool(size_t q_size, + size_t thread_count, + std::function on_thread_start) { + init_thread_pool(q_size, thread_count, on_thread_start, [] {}); +} + +inline void init_thread_pool(size_t q_size, size_t thread_count) { + init_thread_pool( + q_size, thread_count, [] {}, [] {}); +} + +// get the global thread pool. +inline std::shared_ptr thread_pool() { + return details::registry::instance().get_tp(); +} +} // namespace spdlog diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h new file mode 100644 index 0000000..1e79479 --- /dev/null +++ b/include/spdlog/async_logger-inl.h @@ -0,0 +1,84 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +#include +#include + +SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name, + sinks_init_list sinks_list, + std::weak_ptr tp, + async_overflow_policy overflow_policy) + : async_logger(std::move(logger_name), + sinks_list.begin(), + sinks_list.end(), + std::move(tp), + overflow_policy) {} + +SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name, + sink_ptr single_sink, + std::weak_ptr tp, + async_overflow_policy overflow_policy) + : async_logger( + std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {} + +// send the log message to the thread pool +SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){ + SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){ + pool_ptr->post_log(shared_from_this(), msg, overflow_policy_); +} +else { + throw_spdlog_ex("async log: thread pool doesn't exist anymore"); +} +} +SPDLOG_LOGGER_CATCH(msg.source) +} + +// send flush request to the thread pool +SPDLOG_INLINE void spdlog::async_logger::flush_(){ + SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){ + pool_ptr->post_flush(shared_from_this(), overflow_policy_); +} +else { + throw_spdlog_ex("async flush: thread pool doesn't exist anymore"); +} +} +SPDLOG_LOGGER_CATCH(source_loc()) +} + +// +// backend functions - called from the thread pool to do the actual job +// +SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { + for (auto &sink : sinks_) { + if (sink->should_log(msg.level)) { + SPDLOG_TRY { sink->log(msg); } + SPDLOG_LOGGER_CATCH(msg.source) + } + } + + if (should_flush_(msg)) { + backend_flush_(); + } +} + +SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { + for (auto &sink : sinks_) { + SPDLOG_TRY { sink->flush(); } + SPDLOG_LOGGER_CATCH(source_loc()) + } +} + +SPDLOG_INLINE std::shared_ptr spdlog::async_logger::clone(std::string new_name) { + auto cloned = std::make_shared(*this); + cloned->name_ = std::move(new_name); + return cloned; +} diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h new file mode 100644 index 0000000..846c4c6 --- /dev/null +++ b/include/spdlog/async_logger.h @@ -0,0 +1,74 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// Fast asynchronous logger. +// Uses pre allocated queue. +// Creates a single back thread to pop messages from the queue and log them. +// +// Upon each log write the logger: +// 1. Checks if its log level is enough to log the message +// 2. Push a new copy of the message to a queue (or block the caller until +// space is available in the queue) +// Upon destruction, logs all remaining messages in the queue before +// destructing.. + +#include + +namespace spdlog { + +// Async overflow policy - block by default. +enum class async_overflow_policy { + block, // Block until message can be enqueued + overrun_oldest, // Discard oldest message in the queue if full when trying to + // add new item. + discard_new // Discard new message if the queue is full when trying to add new item. +}; + +namespace details { +class thread_pool; +} + +class SPDLOG_API async_logger final : public std::enable_shared_from_this, + public logger { + friend class details::thread_pool; + +public: + template + async_logger(std::string logger_name, + It begin, + It end, + std::weak_ptr tp, + async_overflow_policy overflow_policy = async_overflow_policy::block) + : logger(std::move(logger_name), begin, end), + thread_pool_(std::move(tp)), + overflow_policy_(overflow_policy) {} + + async_logger(std::string logger_name, + sinks_init_list sinks_list, + std::weak_ptr tp, + async_overflow_policy overflow_policy = async_overflow_policy::block); + + async_logger(std::string logger_name, + sink_ptr single_sink, + std::weak_ptr tp, + async_overflow_policy overflow_policy = async_overflow_policy::block); + + std::shared_ptr clone(std::string new_name) override; + +protected: + void sink_it_(const details::log_msg &msg) override; + void flush_() override; + void backend_sink_it_(const details::log_msg &incoming_log_msg); + void backend_flush_(); + +private: + std::weak_ptr thread_pool_; + async_overflow_policy overflow_policy_; +}; +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "async_logger-inl.h" +#endif diff --git a/include/spdlog/cfg/argv.h b/include/spdlog/cfg/argv.h new file mode 100644 index 0000000..7de2f83 --- /dev/null +++ b/include/spdlog/cfg/argv.h @@ -0,0 +1,40 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once +#include +#include + +// +// Init log levels using each argv entry that starts with "SPDLOG_LEVEL=" +// +// set all loggers to debug level: +// example.exe "SPDLOG_LEVEL=debug" + +// set logger1 to trace level +// example.exe "SPDLOG_LEVEL=logger1=trace" + +// turn off all logging except for logger1 and logger2: +// example.exe "SPDLOG_LEVEL=off,logger1=debug,logger2=info" + +namespace spdlog { +namespace cfg { + +// search for SPDLOG_LEVEL= in the args and use it to init the levels +inline void load_argv_levels(int argc, const char **argv) { + const std::string spdlog_level_prefix = "SPDLOG_LEVEL="; + for (int i = 1; i < argc; i++) { + std::string arg = argv[i]; + if (arg.find(spdlog_level_prefix) == 0) { + auto levels_string = arg.substr(spdlog_level_prefix.size()); + helpers::load_levels(levels_string); + } + } +} + +inline void load_argv_levels(int argc, char **argv) { + load_argv_levels(argc, const_cast(argv)); +} + +} // namespace cfg +} // namespace spdlog diff --git a/include/spdlog/cfg/env.h b/include/spdlog/cfg/env.h new file mode 100644 index 0000000..6e55414 --- /dev/null +++ b/include/spdlog/cfg/env.h @@ -0,0 +1,36 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once +#include +#include +#include + +// +// Init levels and patterns from env variables SPDLOG_LEVEL +// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger). +// Note - fallback to "info" level on unrecognized levels +// +// Examples: +// +// set global level to debug: +// export SPDLOG_LEVEL=debug +// +// turn off all logging except for logger1: +// export SPDLOG_LEVEL="*=off,logger1=debug" +// + +// turn off all logging except for logger1 and logger2: +// export SPDLOG_LEVEL="off,logger1=debug,logger2=info" + +namespace spdlog { +namespace cfg { +inline void load_env_levels() { + auto env_val = details::os::getenv("SPDLOG_LEVEL"); + if (!env_val.empty()) { + helpers::load_levels(env_val); + } +} + +} // namespace cfg +} // namespace spdlog diff --git a/include/spdlog/cfg/helpers-inl.h b/include/spdlog/cfg/helpers-inl.h new file mode 100644 index 0000000..93650a2 --- /dev/null +++ b/include/spdlog/cfg/helpers-inl.h @@ -0,0 +1,107 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include +#include + +#include +#include +#include +#include + +namespace spdlog { +namespace cfg { +namespace helpers { + +// inplace convert to lowercase +inline std::string &to_lower_(std::string &str) { + std::transform(str.begin(), str.end(), str.begin(), [](char ch) { + return static_cast((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); + }); + return str; +} + +// inplace trim spaces +inline std::string &trim_(std::string &str) { + const char *spaces = " \n\r\t"; + str.erase(str.find_last_not_of(spaces) + 1); + str.erase(0, str.find_first_not_of(spaces)); + return str; +} + +// return (name,value) trimmed pair from given "name=value" string. +// return empty string on missing parts +// "key=val" => ("key", "val") +// " key = val " => ("key", "val") +// "key=" => ("key", "") +// "val" => ("", "val") + +inline std::pair extract_kv_(char sep, const std::string &str) { + auto n = str.find(sep); + std::string k, v; + if (n == std::string::npos) { + v = str; + } else { + k = str.substr(0, n); + v = str.substr(n + 1); + } + return std::make_pair(trim_(k), trim_(v)); +} + +// return vector of key/value pairs from sequence of "K1=V1,K2=V2,.." +// "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...} +inline std::unordered_map extract_key_vals_(const std::string &str) { + std::string token; + std::istringstream token_stream(str); + std::unordered_map rv{}; + while (std::getline(token_stream, token, ',')) { + if (token.empty()) { + continue; + } + auto kv = extract_kv_('=', token); + rv[kv.first] = kv.second; + } + return rv; +} + +SPDLOG_INLINE void load_levels(const std::string &input) { + if (input.empty() || input.size() > 512) { + return; + } + + auto key_vals = extract_key_vals_(input); + std::unordered_map levels; + level::level_enum global_level = level::info; + bool global_level_found = false; + + for (auto &name_level : key_vals) { + auto &logger_name = name_level.first; + auto level_name = to_lower_(name_level.second); + auto level = level::from_str(level_name); + // ignore unrecognized level names + if (level == level::off && level_name != "off") { + continue; + } + if (logger_name.empty()) // no logger name indicate global level + { + global_level_found = true; + global_level = level; + } else { + levels[logger_name] = level; + } + } + + details::registry::instance().set_levels(std::move(levels), + global_level_found ? &global_level : nullptr); +} + +} // namespace helpers +} // namespace cfg +} // namespace spdlog diff --git a/include/spdlog/cfg/helpers.h b/include/spdlog/cfg/helpers.h new file mode 100644 index 0000000..c023818 --- /dev/null +++ b/include/spdlog/cfg/helpers.h @@ -0,0 +1,29 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +namespace spdlog { +namespace cfg { +namespace helpers { +// +// Init levels from given string +// +// Examples: +// +// set global level to debug: "debug" +// turn off all logging except for logger1: "off,logger1=debug" +// turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info" +// +SPDLOG_API void load_levels(const std::string &txt); +} // namespace helpers + +} // namespace cfg +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "helpers-inl.h" +#endif // SPDLOG_HEADER_ONLY diff --git a/include/spdlog/common-inl.h b/include/spdlog/common-inl.h new file mode 100644 index 0000000..a8a0453 --- /dev/null +++ b/include/spdlog/common-inl.h @@ -0,0 +1,68 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +namespace spdlog { +namespace level { + +#if __cplusplus >= 201703L +constexpr +#endif + static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES; + +static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES; + +SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT { + return level_string_views[l]; +} + +SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT { + return short_level_names[l]; +} + +SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT { + auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name); + if (it != std::end(level_string_views)) + return static_cast(std::distance(std::begin(level_string_views), it)); + + // check also for "warn" and "err" before giving up.. + if (name == "warn") { + return level::warn; + } + if (name == "err") { + return level::err; + } + return level::off; +} +} // namespace level + +SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg) + : msg_(std::move(msg)) {} + +SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) { +#ifdef SPDLOG_USE_STD_FORMAT + msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what(); +#else + memory_buf_t outbuf; + fmt::format_system_error(outbuf, last_errno, msg.c_str()); + msg_ = fmt::to_string(outbuf); +#endif +} + +SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT { return msg_.c_str(); } + +SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) { + SPDLOG_THROW(spdlog_ex(msg, last_errno)); +} + +SPDLOG_INLINE void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); } + +} // namespace spdlog diff --git a/include/spdlog/common.h b/include/spdlog/common.h new file mode 100644 index 0000000..71ffd24 --- /dev/null +++ b/include/spdlog/common.h @@ -0,0 +1,406 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef SPDLOG_USE_STD_FORMAT + #include + #if __cpp_lib_format >= 202207L + #include + #else + #include + #endif +#endif + +#ifdef SPDLOG_COMPILED_LIB + #undef SPDLOG_HEADER_ONLY + #if defined(SPDLOG_SHARED_LIB) + #if defined(_WIN32) + #ifdef spdlog_EXPORTS + #define SPDLOG_API __declspec(dllexport) + #else // !spdlog_EXPORTS + #define SPDLOG_API __declspec(dllimport) + #endif + #else // !defined(_WIN32) + #define SPDLOG_API __attribute__((visibility("default"))) + #endif + #else // !defined(SPDLOG_SHARED_LIB) + #define SPDLOG_API + #endif + #define SPDLOG_INLINE +#else // !defined(SPDLOG_COMPILED_LIB) + #define SPDLOG_API + #define SPDLOG_HEADER_ONLY + #define SPDLOG_INLINE inline +#endif // #ifdef SPDLOG_COMPILED_LIB + +#include + +#if !defined(SPDLOG_USE_STD_FORMAT) && \ + FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8 + #define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string) + #define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string) + #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) + #include + #endif +#else + #define SPDLOG_FMT_RUNTIME(format_string) format_string + #define SPDLOG_FMT_STRING(format_string) format_string +#endif + +// visual studio up to 2013 does not support noexcept nor constexpr +#if defined(_MSC_VER) && (_MSC_VER < 1900) + #define SPDLOG_NOEXCEPT _NOEXCEPT + #define SPDLOG_CONSTEXPR +#else + #define SPDLOG_NOEXCEPT noexcept + #define SPDLOG_CONSTEXPR constexpr +#endif + +// If building with std::format, can just use constexpr, otherwise if building with fmt +// SPDLOG_CONSTEXPR_FUNC needs to be set the same as FMT_CONSTEXPR to avoid situations where +// a constexpr function in spdlog could end up calling a non-constexpr function in fmt +// depending on the compiler +// If fmt determines it can't use constexpr, we should inline the function instead +#ifdef SPDLOG_USE_STD_FORMAT + #define SPDLOG_CONSTEXPR_FUNC constexpr +#else // Being built with fmt + #if FMT_USE_CONSTEXPR + #define SPDLOG_CONSTEXPR_FUNC FMT_CONSTEXPR + #else + #define SPDLOG_CONSTEXPR_FUNC inline + #endif +#endif + +#if defined(__GNUC__) || defined(__clang__) + #define SPDLOG_DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) + #define SPDLOG_DEPRECATED __declspec(deprecated) +#else + #define SPDLOG_DEPRECATED +#endif + +// disable thread local on msvc 2013 +#ifndef SPDLOG_NO_TLS + #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt) + #define SPDLOG_NO_TLS 1 + #endif +#endif + +#ifndef SPDLOG_FUNCTION + #define SPDLOG_FUNCTION static_cast(__FUNCTION__) +#endif + +#ifdef SPDLOG_NO_EXCEPTIONS + #define SPDLOG_TRY + #define SPDLOG_THROW(ex) \ + do { \ + printf("spdlog fatal error: %s\n", ex.what()); \ + std::abort(); \ + } while (0) + #define SPDLOG_CATCH_STD +#else + #define SPDLOG_TRY try + #define SPDLOG_THROW(ex) throw(ex) + #define SPDLOG_CATCH_STD \ + catch (const std::exception &) { \ + } +#endif + +namespace spdlog { + +class formatter; + +namespace sinks { +class sink; +} + +#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) +using filename_t = std::wstring; + // allow macro expansion to occur in SPDLOG_FILENAME_T + #define SPDLOG_FILENAME_T_INNER(s) L##s + #define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s) +#else +using filename_t = std::string; + #define SPDLOG_FILENAME_T(s) s +#endif + +using log_clock = std::chrono::system_clock; +using sink_ptr = std::shared_ptr; +using sinks_init_list = std::initializer_list; +using err_handler = std::function; +#ifdef SPDLOG_USE_STD_FORMAT +namespace fmt_lib = std; + +using string_view_t = std::string_view; +using memory_buf_t = std::string; + +template + #if __cpp_lib_format >= 202207L +using format_string_t = std::format_string; + #else +using format_string_t = std::string_view; + #endif + +template +struct is_convertible_to_basic_format_string + : std::integral_constant>::value> {}; + + #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +using wstring_view_t = std::wstring_view; +using wmemory_buf_t = std::wstring; + +template + #if __cpp_lib_format >= 202207L +using wformat_string_t = std::wformat_string; + #else +using wformat_string_t = std::wstring_view; + #endif + #endif + #define SPDLOG_BUF_TO_STRING(x) x +#else // use fmt lib instead of std::format +namespace fmt_lib = fmt; + +using string_view_t = fmt::basic_string_view; +using memory_buf_t = fmt::basic_memory_buffer; + +template +using format_string_t = fmt::format_string; + +template +using remove_cvref_t = typename std::remove_cv::type>::type; + +template + #if FMT_VERSION >= 90101 +using fmt_runtime_string = fmt::runtime_format_string; + #else +using fmt_runtime_string = fmt::basic_runtime; + #endif + +// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the +// condition from basic_format_string here, in addition, fmt::basic_runtime is only +// convertible to basic_format_string but not basic_string_view +template +struct is_convertible_to_basic_format_string + : std::integral_constant>::value || + std::is_same, fmt_runtime_string>::value> { +}; + + #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +using wstring_view_t = fmt::basic_string_view; +using wmemory_buf_t = fmt::basic_memory_buffer; + +template +using wformat_string_t = fmt::wformat_string; + #endif + #define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x) +#endif + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + #ifndef _WIN32 + #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows + #endif // _WIN32 +#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + +template +struct is_convertible_to_any_format_string + : std::integral_constant::value || + is_convertible_to_basic_format_string::value> {}; + +#if defined(SPDLOG_NO_ATOMIC_LEVELS) +using level_t = details::null_atomic_int; +#else +using level_t = std::atomic; +#endif + +#define SPDLOG_LEVEL_TRACE 0 +#define SPDLOG_LEVEL_DEBUG 1 +#define SPDLOG_LEVEL_INFO 2 +#define SPDLOG_LEVEL_WARN 3 +#define SPDLOG_LEVEL_ERROR 4 +#define SPDLOG_LEVEL_CRITICAL 5 +#define SPDLOG_LEVEL_OFF 6 + +#if !defined(SPDLOG_ACTIVE_LEVEL) + #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO +#endif + +// Log level enum +namespace level { +enum level_enum : int { + trace = SPDLOG_LEVEL_TRACE, + debug = SPDLOG_LEVEL_DEBUG, + info = SPDLOG_LEVEL_INFO, + warn = SPDLOG_LEVEL_WARN, + err = SPDLOG_LEVEL_ERROR, + critical = SPDLOG_LEVEL_CRITICAL, + off = SPDLOG_LEVEL_OFF, + n_levels +}; + +#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5) +#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5) +#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4) +#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7) +#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5) +#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8) +#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3) + +#if !defined(SPDLOG_LEVEL_NAMES) + #define SPDLOG_LEVEL_NAMES \ + { \ + SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, \ + SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, \ + SPDLOG_LEVEL_NAME_OFF \ + } +#endif + +#if !defined(SPDLOG_SHORT_LEVEL_NAMES) + + #define SPDLOG_SHORT_LEVEL_NAMES \ + { "T", "D", "I", "W", "E", "C", "O" } +#endif + +SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT; +SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT; +SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT; + +} // namespace level + +// +// Color mode used by sinks with color support. +// +enum class color_mode { always, automatic, never }; + +// +// Pattern time - specific time getting to use for pattern_formatter. +// local time by default +// +enum class pattern_time_type { + local, // log localtime + utc // log utc +}; + +// +// Log exception +// +class SPDLOG_API spdlog_ex : public std::exception { +public: + explicit spdlog_ex(std::string msg); + spdlog_ex(const std::string &msg, int last_errno); + const char *what() const SPDLOG_NOEXCEPT override; + +private: + std::string msg_; +}; + +[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno); +[[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg); + +struct source_loc { + SPDLOG_CONSTEXPR source_loc() = default; + SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in) + : filename{filename_in}, + line{line_in}, + funcname{funcname_in} {} + + SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line <= 0; } + const char *filename{nullptr}; + int line{0}; + const char *funcname{nullptr}; +}; + +struct file_event_handlers { + file_event_handlers() + : before_open(nullptr), + after_open(nullptr), + before_close(nullptr), + after_close(nullptr) {} + + std::function before_open; + std::function after_open; + std::function before_close; + std::function after_close; +}; + +namespace details { + +// to_string_view + +SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t &buf) + SPDLOG_NOEXCEPT { + return spdlog::string_view_t{buf.data(), buf.size()}; +} + +SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(spdlog::string_view_t str) + SPDLOG_NOEXCEPT { + return str; +} + +#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) + SPDLOG_NOEXCEPT { + return spdlog::wstring_view_t{buf.data(), buf.size()}; +} + +SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) + SPDLOG_NOEXCEPT { + return str; +} +#endif + +#if defined(SPDLOG_USE_STD_FORMAT) && __cpp_lib_format >= 202207L +template +SPDLOG_CONSTEXPR_FUNC std::basic_string_view to_string_view( + std::basic_format_string fmt) SPDLOG_NOEXCEPT { + return fmt.get(); +} +#endif + +// make_unique support for pre c++14 +#if __cplusplus >= 201402L // C++14 and beyond +using std::enable_if_t; +using std::make_unique; +#else +template +using enable_if_t = typename std::enable_if::type; + +template +std::unique_ptr make_unique(Args &&...args) { + static_assert(!std::is_array::value, "arrays not supported"); + return std::unique_ptr(new T(std::forward(args)...)); +} +#endif + +// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) +template ::value, int> = 0> +constexpr T conditional_static_cast(U value) { + return static_cast(value); +} + +template ::value, int> = 0> +constexpr T conditional_static_cast(U value) { + return value; +} + +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "common-inl.h" +#endif diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h new file mode 100644 index 0000000..43d1002 --- /dev/null +++ b/include/spdlog/details/backtracer-inl.h @@ -0,0 +1,63 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif +namespace spdlog { +namespace details { +SPDLOG_INLINE backtracer::backtracer(const backtracer &other) { + std::lock_guard lock(other.mutex_); + enabled_ = other.enabled(); + messages_ = other.messages_; +} + +SPDLOG_INLINE backtracer::backtracer(backtracer &&other) SPDLOG_NOEXCEPT { + std::lock_guard lock(other.mutex_); + enabled_ = other.enabled(); + messages_ = std::move(other.messages_); +} + +SPDLOG_INLINE backtracer &backtracer::operator=(backtracer other) { + std::lock_guard lock(mutex_); + enabled_ = other.enabled(); + messages_ = std::move(other.messages_); + return *this; +} + +SPDLOG_INLINE void backtracer::enable(size_t size) { + std::lock_guard lock{mutex_}; + enabled_.store(true, std::memory_order_relaxed); + messages_ = circular_q{size}; +} + +SPDLOG_INLINE void backtracer::disable() { + std::lock_guard lock{mutex_}; + enabled_.store(false, std::memory_order_relaxed); +} + +SPDLOG_INLINE bool backtracer::enabled() const { return enabled_.load(std::memory_order_relaxed); } + +SPDLOG_INLINE void backtracer::push_back(const log_msg &msg) { + std::lock_guard lock{mutex_}; + messages_.push_back(log_msg_buffer{msg}); +} + +SPDLOG_INLINE bool backtracer::empty() const { + std::lock_guard lock{mutex_}; + return messages_.empty(); +} + +// pop all items in the q and apply the given fun on each of them. +SPDLOG_INLINE void backtracer::foreach_pop(std::function fun) { + std::lock_guard lock{mutex_}; + while (!messages_.empty()) { + auto &front_msg = messages_.front(); + fun(front_msg); + messages_.pop_front(); + } +} +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/backtracer.h b/include/spdlog/details/backtracer.h new file mode 100644 index 0000000..541339c --- /dev/null +++ b/include/spdlog/details/backtracer.h @@ -0,0 +1,45 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +#include +#include +#include + +// Store log messages in circular buffer. +// Useful for storing debug data in case of error/warning happens. + +namespace spdlog { +namespace details { +class SPDLOG_API backtracer { + mutable std::mutex mutex_; + std::atomic enabled_{false}; + circular_q messages_; + +public: + backtracer() = default; + backtracer(const backtracer &other); + + backtracer(backtracer &&other) SPDLOG_NOEXCEPT; + backtracer &operator=(backtracer other); + + void enable(size_t size); + void disable(); + bool enabled() const; + void push_back(const log_msg &msg); + bool empty() const; + + // pop all items in the q and apply the given fun on each of them. + void foreach_pop(std::function fun); +}; + +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "backtracer-inl.h" +#endif diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h new file mode 100644 index 0000000..29e9d25 --- /dev/null +++ b/include/spdlog/details/circular_q.h @@ -0,0 +1,115 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +// circular q view of std::vector. +#pragma once + +#include +#include + +#include "spdlog/common.h" + +namespace spdlog { +namespace details { +template +class circular_q { + size_t max_items_ = 0; + typename std::vector::size_type head_ = 0; + typename std::vector::size_type tail_ = 0; + size_t overrun_counter_ = 0; + std::vector v_; + +public: + using value_type = T; + + // empty ctor - create a disabled queue with no elements allocated at all + circular_q() = default; + + explicit circular_q(size_t max_items) + : max_items_(max_items + 1) // one item is reserved as marker for full q + , + v_(max_items_) {} + + circular_q(const circular_q &) = default; + circular_q &operator=(const circular_q &) = default; + + // move cannot be default, + // since we need to reset head_, tail_, etc to zero in the moved object + circular_q(circular_q &&other) SPDLOG_NOEXCEPT { copy_moveable(std::move(other)); } + + circular_q &operator=(circular_q &&other) SPDLOG_NOEXCEPT { + copy_moveable(std::move(other)); + return *this; + } + + // push back, overrun (oldest) item if no room left + void push_back(T &&item) { + if (max_items_ > 0) { + v_[tail_] = std::move(item); + tail_ = (tail_ + 1) % max_items_; + + if (tail_ == head_) // overrun last item if full + { + head_ = (head_ + 1) % max_items_; + ++overrun_counter_; + } + } + } + + // Return reference to the front item. + // If there are no elements in the container, the behavior is undefined. + const T &front() const { return v_[head_]; } + + T &front() { return v_[head_]; } + + // Return number of elements actually stored + size_t size() const { + if (tail_ >= head_) { + return tail_ - head_; + } else { + return max_items_ - (head_ - tail_); + } + } + + // Return const reference to item by index. + // If index is out of range 0…size()-1, the behavior is undefined. + const T &at(size_t i) const { + assert(i < size()); + return v_[(head_ + i) % max_items_]; + } + + // Pop item from front. + // If there are no elements in the container, the behavior is undefined. + void pop_front() { head_ = (head_ + 1) % max_items_; } + + bool empty() const { return tail_ == head_; } + + bool full() const { + // head is ahead of the tail by 1 + if (max_items_ > 0) { + return ((tail_ + 1) % max_items_) == head_; + } + return false; + } + + size_t overrun_counter() const { return overrun_counter_; } + + void reset_overrun_counter() { overrun_counter_ = 0; } + +private: + // copy from other&& and reset it to disabled state + void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT { + max_items_ = other.max_items_; + head_ = other.head_; + tail_ = other.tail_; + overrun_counter_ = other.overrun_counter_; + v_ = std::move(other.v_); + + // put &&other in disabled, but valid state + other.max_items_ = 0; + other.head_ = other.tail_ = 0; + other.overrun_counter_ = 0; + } +}; +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/console_globals.h b/include/spdlog/details/console_globals.h new file mode 100644 index 0000000..9c55210 --- /dev/null +++ b/include/spdlog/details/console_globals.h @@ -0,0 +1,28 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +namespace spdlog { +namespace details { + +struct console_mutex { + using mutex_t = std::mutex; + static mutex_t &mutex() { + static mutex_t s_mutex; + return s_mutex; + } +}; + +struct console_nullmutex { + using mutex_t = null_mutex; + static mutex_t &mutex() { + static mutex_t s_mutex; + return s_mutex; + } +}; +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h new file mode 100644 index 0000000..8742b96 --- /dev/null +++ b/include/spdlog/details/file_helper-inl.h @@ -0,0 +1,153 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace spdlog { +namespace details { + +SPDLOG_INLINE file_helper::file_helper(const file_event_handlers &event_handlers) + : event_handlers_(event_handlers) {} + +SPDLOG_INLINE file_helper::~file_helper() { close(); } + +SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { + close(); + filename_ = fname; + + auto *mode = SPDLOG_FILENAME_T("ab"); + auto *trunc_mode = SPDLOG_FILENAME_T("wb"); + + if (event_handlers_.before_open) { + event_handlers_.before_open(filename_); + } + for (int tries = 0; tries < open_tries_; ++tries) { + // create containing folder if not exists already. + os::create_dir(os::dir_name(fname)); + if (truncate) { + // Truncate by opening-and-closing a tmp file in "wb" mode, always + // opening the actual log-we-write-to in "ab" mode, since that + // interacts more politely with eternal processes that might + // rotate/truncate the file underneath us. + std::FILE *tmp; + if (os::fopen_s(&tmp, fname, trunc_mode)) { + continue; + } + std::fclose(tmp); + } + if (!os::fopen_s(&fd_, fname, mode)) { + if (event_handlers_.after_open) { + event_handlers_.after_open(filename_, fd_); + } + return; + } + + details::os::sleep_for_millis(open_interval_); + } + + throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", + errno); +} + +SPDLOG_INLINE void file_helper::reopen(bool truncate) { + if (filename_.empty()) { + throw_spdlog_ex("Failed re opening file - was not opened before"); + } + this->open(filename_, truncate); +} + +SPDLOG_INLINE void file_helper::flush() { + if (std::fflush(fd_) != 0) { + throw_spdlog_ex("Failed flush to file " + os::filename_to_str(filename_), errno); + } +} + +SPDLOG_INLINE void file_helper::sync() { + if (!os::fsync(fd_)) { + throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno); + } +} + +SPDLOG_INLINE void file_helper::close() { + if (fd_ != nullptr) { + if (event_handlers_.before_close) { + event_handlers_.before_close(filename_, fd_); + } + + std::fclose(fd_); + fd_ = nullptr; + + if (event_handlers_.after_close) { + event_handlers_.after_close(filename_); + } + } +} + +SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf) { + if (fd_ == nullptr) return; + size_t msg_size = buf.size(); + auto data = buf.data(); + + if (!details::os::fwrite_bytes(data, msg_size, fd_)) { + throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno); + } +} + +SPDLOG_INLINE size_t file_helper::size() const { + if (fd_ == nullptr) { + throw_spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_)); + } + return os::filesize(fd_); +} + +SPDLOG_INLINE const filename_t &file_helper::filename() const { return filename_; } + +// +// return file path and its extension: +// +// "mylog.txt" => ("mylog", ".txt") +// "mylog" => ("mylog", "") +// "mylog." => ("mylog.", "") +// "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") +// +// the starting dot in filenames is ignored (hidden files): +// +// ".mylog" => (".mylog". "") +// "my_folder/.mylog" => ("my_folder/.mylog", "") +// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") +SPDLOG_INLINE std::tuple file_helper::split_by_extension( + const filename_t &fname) { + auto ext_index = fname.rfind('.'); + + // no valid extension found - return whole path and empty string as + // extension + if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1) { + return std::make_tuple(fname, filename_t()); + } + + // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile" + auto folder_index = fname.find_last_of(details::os::folder_seps_filename); + if (folder_index != filename_t::npos && folder_index >= ext_index - 1) { + return std::make_tuple(fname, filename_t()); + } + + // finally - return a valid base and extension tuple + return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index)); +} + +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h new file mode 100644 index 0000000..f0e5d18 --- /dev/null +++ b/include/spdlog/details/file_helper.h @@ -0,0 +1,61 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +namespace spdlog { +namespace details { + +// Helper class for file sinks. +// When failing to open a file, retry several times(5) with a delay interval(10 ms). +// Throw spdlog_ex exception on errors. + +class SPDLOG_API file_helper { +public: + file_helper() = default; + explicit file_helper(const file_event_handlers &event_handlers); + + file_helper(const file_helper &) = delete; + file_helper &operator=(const file_helper &) = delete; + ~file_helper(); + + void open(const filename_t &fname, bool truncate = false); + void reopen(bool truncate); + void flush(); + void sync(); + void close(); + void write(const memory_buf_t &buf); + size_t size() const; + const filename_t &filename() const; + + // + // return file path and its extension: + // + // "mylog.txt" => ("mylog", ".txt") + // "mylog" => ("mylog", "") + // "mylog." => ("mylog.", "") + // "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") + // + // the starting dot in filenames is ignored (hidden files): + // + // ".mylog" => (".mylog". "") + // "my_folder/.mylog" => ("my_folder/.mylog", "") + // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") + static std::tuple split_by_extension(const filename_t &fname); + +private: + const int open_tries_ = 5; + const unsigned int open_interval_ = 10; + std::FILE *fd_{nullptr}; + filename_t filename_; + file_event_handlers event_handlers_; +}; +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "file_helper-inl.h" +#endif diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h new file mode 100644 index 0000000..6130600 --- /dev/null +++ b/include/spdlog/details/fmt_helper.h @@ -0,0 +1,141 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +#pragma once + +#include +#include +#include +#include +#include + +#ifdef SPDLOG_USE_STD_FORMAT + #include + #include +#endif + +// Some fmt helpers to efficiently format and pad ints and strings +namespace spdlog { +namespace details { +namespace fmt_helper { + +inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) { + auto *buf_ptr = view.data(); + dest.append(buf_ptr, buf_ptr + view.size()); +} + +#ifdef SPDLOG_USE_STD_FORMAT +template +inline void append_int(T n, memory_buf_t &dest) { + // Buffer should be large enough to hold all digits (digits10 + 1) and a sign + SPDLOG_CONSTEXPR const auto BUF_SIZE = std::numeric_limits::digits10 + 2; + char buf[BUF_SIZE]; + + auto [ptr, ec] = std::to_chars(buf, buf + BUF_SIZE, n, 10); + if (ec == std::errc()) { + dest.append(buf, ptr); + } else { + throw_spdlog_ex("Failed to format int", static_cast(ec)); + } +} +#else +template +inline void append_int(T n, memory_buf_t &dest) { + fmt::format_int i(n); + dest.append(i.data(), i.data() + i.size()); +} +#endif + +template +SPDLOG_CONSTEXPR_FUNC unsigned int count_digits_fallback(T n) { + // taken from fmt: https://github.com/fmtlib/fmt/blob/8.0.1/include/fmt/format.h#L899-L912 + unsigned int count = 1; + for (;;) { + // Integer division is slow so do it for a group of four digits instead + // of for every digit. The idea comes from the talk by Alexandrescu + // "Three Optimization Tips for C++". See speed-test for a comparison. + if (n < 10) return count; + if (n < 100) return count + 1; + if (n < 1000) return count + 2; + if (n < 10000) return count + 3; + n /= 10000u; + count += 4; + } +} + +template +inline unsigned int count_digits(T n) { + using count_type = + typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type; +#ifdef SPDLOG_USE_STD_FORMAT + return count_digits_fallback(static_cast(n)); +#else + return static_cast(fmt:: + // fmt 7.0.0 renamed the internal namespace to detail. + // See: https://github.com/fmtlib/fmt/issues/1538 + #if FMT_VERSION < 70000 + internal + #else + detail + #endif + ::count_digits(static_cast(n))); +#endif +} + +inline void pad2(int n, memory_buf_t &dest) { + if (n >= 0 && n < 100) // 0-99 + { + dest.push_back(static_cast('0' + n / 10)); + dest.push_back(static_cast('0' + n % 10)); + } else // unlikely, but just in case, let fmt deal with it + { + fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n); + } +} + +template +inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) { + static_assert(std::is_unsigned::value, "pad_uint must get unsigned T"); + for (auto digits = count_digits(n); digits < width; digits++) { + dest.push_back('0'); + } + append_int(n, dest); +} + +template +inline void pad3(T n, memory_buf_t &dest) { + static_assert(std::is_unsigned::value, "pad3 must get unsigned T"); + if (n < 1000) { + dest.push_back(static_cast(n / 100 + '0')); + n = n % 100; + dest.push_back(static_cast((n / 10) + '0')); + dest.push_back(static_cast((n % 10) + '0')); + } else { + append_int(n, dest); + } +} + +template +inline void pad6(T n, memory_buf_t &dest) { + pad_uint(n, 6, dest); +} + +template +inline void pad9(T n, memory_buf_t &dest) { + pad_uint(n, 9, dest); +} + +// return fraction of a second of the given time_point. +// e.g. +// fraction(tp) -> will return the millis part of the second +template +inline ToDuration time_fraction(log_clock::time_point tp) { + using std::chrono::duration_cast; + using std::chrono::seconds; + auto duration = tp.time_since_epoch(); + auto secs = duration_cast(duration); + return duration_cast(duration) - duration_cast(secs); +} + +} // namespace fmt_helper +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/log_msg-inl.h b/include/spdlog/details/log_msg-inl.h new file mode 100644 index 0000000..aa3a957 --- /dev/null +++ b/include/spdlog/details/log_msg-inl.h @@ -0,0 +1,44 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include + +namespace spdlog { +namespace details { + +SPDLOG_INLINE log_msg::log_msg(spdlog::log_clock::time_point log_time, + spdlog::source_loc loc, + string_view_t a_logger_name, + spdlog::level::level_enum lvl, + spdlog::string_view_t msg) + : logger_name(a_logger_name), + level(lvl), + time(log_time) +#ifndef SPDLOG_NO_THREAD_ID + , + thread_id(os::thread_id()) +#endif + , + source(loc), + payload(msg) { +} + +SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, + string_view_t a_logger_name, + spdlog::level::level_enum lvl, + spdlog::string_view_t msg) + : log_msg(os::now(), loc, a_logger_name, lvl, msg) {} + +SPDLOG_INLINE log_msg::log_msg(string_view_t a_logger_name, + spdlog::level::level_enum lvl, + spdlog::string_view_t msg) + : log_msg(os::now(), source_loc{}, a_logger_name, lvl, msg) {} + +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h new file mode 100644 index 0000000..87df1e8 --- /dev/null +++ b/include/spdlog/details/log_msg.h @@ -0,0 +1,40 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +namespace spdlog { +namespace details { +struct SPDLOG_API log_msg { + log_msg() = default; + log_msg(log_clock::time_point log_time, + source_loc loc, + string_view_t logger_name, + level::level_enum lvl, + string_view_t msg); + log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg); + log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg); + log_msg(const log_msg &other) = default; + log_msg &operator=(const log_msg &other) = default; + + string_view_t logger_name; + level::level_enum level{level::off}; + log_clock::time_point time; + size_t thread_id{0}; + + // wrapping the formatted text with color (updated by pattern_formatter). + mutable size_t color_range_start{0}; + mutable size_t color_range_end{0}; + + source_loc source; + string_view_t payload; +}; +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "log_msg-inl.h" +#endif diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h new file mode 100644 index 0000000..2eb2428 --- /dev/null +++ b/include/spdlog/details/log_msg_buffer-inl.h @@ -0,0 +1,54 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +namespace spdlog { +namespace details { + +SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg &orig_msg) + : log_msg{orig_msg} { + buffer.append(logger_name.begin(), logger_name.end()); + buffer.append(payload.begin(), payload.end()); + update_string_views(); +} + +SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) + : log_msg{other} { + buffer.append(logger_name.begin(), logger_name.end()); + buffer.append(payload.begin(), payload.end()); + update_string_views(); +} + +SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT + : log_msg{other}, + buffer{std::move(other.buffer)} { + update_string_views(); +} + +SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(const log_msg_buffer &other) { + log_msg::operator=(other); + buffer.clear(); + buffer.append(other.buffer.data(), other.buffer.data() + other.buffer.size()); + update_string_views(); + return *this; +} + +SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT { + log_msg::operator=(other); + buffer = std::move(other.buffer); + update_string_views(); + return *this; +} + +SPDLOG_INLINE void log_msg_buffer::update_string_views() { + logger_name = string_view_t{buffer.data(), logger_name.size()}; + payload = string_view_t{buffer.data() + logger_name.size(), payload.size()}; +} + +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h new file mode 100644 index 0000000..1143b3b --- /dev/null +++ b/include/spdlog/details/log_msg_buffer.h @@ -0,0 +1,32 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include + +namespace spdlog { +namespace details { + +// Extend log_msg with internal buffer to store its payload. +// This is needed since log_msg holds string_views that points to stack data. + +class SPDLOG_API log_msg_buffer : public log_msg { + memory_buf_t buffer; + void update_string_views(); + +public: + log_msg_buffer() = default; + explicit log_msg_buffer(const log_msg &orig_msg); + log_msg_buffer(const log_msg_buffer &other); + log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT; + log_msg_buffer &operator=(const log_msg_buffer &other); + log_msg_buffer &operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT; +}; + +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "log_msg_buffer-inl.h" +#endif diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h new file mode 100644 index 0000000..5848cca --- /dev/null +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -0,0 +1,177 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// multi producer-multi consumer blocking queue. +// enqueue(..) - will block until room found to put the new message. +// enqueue_nowait(..) - will return immediately with false if no room left in +// the queue. +// dequeue_for(..) - will block until the queue is not empty or timeout have +// passed. + +#include + +#include +#include +#include + +namespace spdlog { +namespace details { + +template +class mpmc_blocking_queue { +public: + using item_type = T; + explicit mpmc_blocking_queue(size_t max_items) + : q_(max_items) {} + +#ifndef __MINGW32__ + // try to enqueue and block if no room left + void enqueue(T &&item) { + { + std::unique_lock lock(queue_mutex_); + pop_cv_.wait(lock, [this] { return !this->q_.full(); }); + q_.push_back(std::move(item)); + } + push_cv_.notify_one(); + } + + // enqueue immediately. overrun oldest message in the queue if no room left. + void enqueue_nowait(T &&item) { + { + std::unique_lock lock(queue_mutex_); + q_.push_back(std::move(item)); + } + push_cv_.notify_one(); + } + + void enqueue_if_have_room(T &&item) { + bool pushed = false; + { + std::unique_lock lock(queue_mutex_); + if (!q_.full()) { + q_.push_back(std::move(item)); + pushed = true; + } + } + + if (pushed) { + push_cv_.notify_one(); + } else { + ++discard_counter_; + } + } + + // dequeue with a timeout. + // Return true, if succeeded dequeue item, false otherwise + bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) { + { + std::unique_lock lock(queue_mutex_); + if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) { + return false; + } + popped_item = std::move(q_.front()); + q_.pop_front(); + } + pop_cv_.notify_one(); + return true; + } + + // blocking dequeue without a timeout. + void dequeue(T &popped_item) { + { + std::unique_lock lock(queue_mutex_); + push_cv_.wait(lock, [this] { return !this->q_.empty(); }); + popped_item = std::move(q_.front()); + q_.pop_front(); + } + pop_cv_.notify_one(); + } + +#else + // apparently mingw deadlocks if the mutex is released before cv.notify_one(), + // so release the mutex at the very end each function. + + // try to enqueue and block if no room left + void enqueue(T &&item) { + std::unique_lock lock(queue_mutex_); + pop_cv_.wait(lock, [this] { return !this->q_.full(); }); + q_.push_back(std::move(item)); + push_cv_.notify_one(); + } + + // enqueue immediately. overrun oldest message in the queue if no room left. + void enqueue_nowait(T &&item) { + std::unique_lock lock(queue_mutex_); + q_.push_back(std::move(item)); + push_cv_.notify_one(); + } + + void enqueue_if_have_room(T &&item) { + bool pushed = false; + std::unique_lock lock(queue_mutex_); + if (!q_.full()) { + q_.push_back(std::move(item)); + pushed = true; + } + + if (pushed) { + push_cv_.notify_one(); + } else { + ++discard_counter_; + } + } + + // dequeue with a timeout. + // Return true, if succeeded dequeue item, false otherwise + bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) { + std::unique_lock lock(queue_mutex_); + if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) { + return false; + } + popped_item = std::move(q_.front()); + q_.pop_front(); + pop_cv_.notify_one(); + return true; + } + + // blocking dequeue without a timeout. + void dequeue(T &popped_item) { + std::unique_lock lock(queue_mutex_); + push_cv_.wait(lock, [this] { return !this->q_.empty(); }); + popped_item = std::move(q_.front()); + q_.pop_front(); + pop_cv_.notify_one(); + } + +#endif + + size_t overrun_counter() { + std::lock_guard lock(queue_mutex_); + return q_.overrun_counter(); + } + + size_t discard_counter() { return discard_counter_.load(std::memory_order_relaxed); } + + size_t size() { + std::lock_guard lock(queue_mutex_); + return q_.size(); + } + + void reset_overrun_counter() { + std::lock_guard lock(queue_mutex_); + q_.reset_overrun_counter(); + } + + void reset_discard_counter() { discard_counter_.store(0, std::memory_order_relaxed); } + +private: + std::mutex queue_mutex_; + std::condition_variable push_cv_; + std::condition_variable pop_cv_; + spdlog::details::circular_q q_; + std::atomic discard_counter_{0}; +}; +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/null_mutex.h b/include/spdlog/details/null_mutex.h new file mode 100644 index 0000000..e3b3220 --- /dev/null +++ b/include/spdlog/details/null_mutex.h @@ -0,0 +1,35 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +// null, no cost dummy "mutex" and dummy "atomic" int + +namespace spdlog { +namespace details { +struct null_mutex { + void lock() const {} + void unlock() const {} +}; + +struct null_atomic_int { + int value; + null_atomic_int() = default; + + explicit null_atomic_int(int new_value) + : value(new_value) {} + + int load(std::memory_order = std::memory_order_relaxed) const { return value; } + + void store(int new_value, std::memory_order = std::memory_order_relaxed) { value = new_value; } + + int exchange(int new_value, std::memory_order = std::memory_order_relaxed) { + std::swap(new_value, value); + return new_value; // return value before the call + } +}; + +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h new file mode 100644 index 0000000..8ee4230 --- /dev/null +++ b/include/spdlog/details/os-inl.h @@ -0,0 +1,606 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 + #include + #include // for FlushFileBuffers + #include // for _get_osfhandle, _isatty, _fileno + #include // for _get_pid + + #ifdef __MINGW32__ + #include + #endif + + #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES) + #include + #include + #endif + + #include // for _mkdir/_wmkdir + +#else // unix + + #include + #include + + #ifdef __linux__ + #include //Use gettid() syscall under linux to get thread id + + #elif defined(_AIX) + #include // for pthread_getthrds_np + + #elif defined(__DragonFly__) || defined(__FreeBSD__) + #include // for pthread_getthreadid_np + + #elif defined(__NetBSD__) + #include // for _lwp_self + + #elif defined(__sun) + #include // for thr_self + #endif + +#endif // unix + +#if defined __APPLE__ + #include +#endif + +#ifndef __has_feature // Clang - feature checking macros. + #define __has_feature(x) 0 // Compatibility with non-clang compilers. +#endif + +namespace spdlog { +namespace details { +namespace os { + +SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT { +#if defined __linux__ && defined SPDLOG_CLOCK_COARSE + timespec ts; + ::clock_gettime(CLOCK_REALTIME_COARSE, &ts); + return std::chrono::time_point( + std::chrono::duration_cast( + std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec))); + +#else + return log_clock::now(); +#endif +} +SPDLOG_INLINE std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT { +#ifdef _WIN32 + std::tm tm; + ::localtime_s(&tm, &time_tt); +#else + std::tm tm; + ::localtime_r(&time_tt, &tm); +#endif + return tm; +} + +SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT { + std::time_t now_t = ::time(nullptr); + return localtime(now_t); +} + +SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT { +#ifdef _WIN32 + std::tm tm; + ::gmtime_s(&tm, &time_tt); +#else + std::tm tm; + ::gmtime_r(&time_tt, &tm); +#endif + return tm; +} + +SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT { + std::time_t now_t = ::time(nullptr); + return gmtime(now_t); +} + +// fopen_s on non windows for writing +SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { +#ifdef _WIN32 + #ifdef SPDLOG_WCHAR_FILENAMES + *fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); + #else + *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); + #endif + #if defined(SPDLOG_PREVENT_CHILD_FD) + if (*fp != nullptr) { + auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(*fp))); + if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) { + ::fclose(*fp); + *fp = nullptr; + } + } + #endif +#else // unix + #if defined(SPDLOG_PREVENT_CHILD_FD) + const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC; + const int fd = + ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644)); + if (fd == -1) { + return true; + } + *fp = ::fdopen(fd, mode.c_str()); + if (*fp == nullptr) { + ::close(fd); + } + #else + *fp = ::fopen((filename.c_str()), mode.c_str()); + #endif +#endif + + return *fp == nullptr; +} + +SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT { +#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) + return ::_wremove(filename.c_str()); +#else + return std::remove(filename.c_str()); +#endif +} + +SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT { + return path_exists(filename) ? remove(filename) : 0; +} + +SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT { +#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) + return ::_wrename(filename1.c_str(), filename2.c_str()); +#else + return std::rename(filename1.c_str(), filename2.c_str()); +#endif +} + +// Return true if path exists (file or directory) +SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT { +#ifdef _WIN32 + struct _stat buffer; + #ifdef SPDLOG_WCHAR_FILENAMES + return (::_wstat(filename.c_str(), &buffer) == 0); + #else + return (::_stat(filename.c_str(), &buffer) == 0); + #endif +#else // common linux/unix all have the stat system call + struct stat buffer; + return (::stat(filename.c_str(), &buffer) == 0); +#endif +} + +#ifdef _MSC_VER + // avoid warning about unreachable statement at the end of filesize() + #pragma warning(push) + #pragma warning(disable : 4702) +#endif + +// Return file size according to open FILE* object +SPDLOG_INLINE size_t filesize(FILE *f) { + if (f == nullptr) { + throw_spdlog_ex("Failed getting file size. fd is null"); + } +#if defined(_WIN32) && !defined(__CYGWIN__) + int fd = ::_fileno(f); + #if defined(_WIN64) // 64 bits + __int64 ret = ::_filelengthi64(fd); + if (ret >= 0) { + return static_cast(ret); + } + + #else // windows 32 bits + long ret = ::_filelength(fd); + if (ret >= 0) { + return static_cast(ret); + } + #endif + +#else // unix + // OpenBSD and AIX doesn't compile with :: before the fileno(..) + #if defined(__OpenBSD__) || defined(_AIX) + int fd = fileno(f); + #else + int fd = ::fileno(f); + #endif + // 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated) + #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && \ + (defined(__LP64__) || defined(_LP64)) + struct stat64 st; + if (::fstat64(fd, &st) == 0) { + return static_cast(st.st_size); + } + #else // other unix or linux 32 bits or cygwin + struct stat st; + if (::fstat(fd, &st) == 0) { + return static_cast(st.st_size); + } + #endif +#endif + throw_spdlog_ex("Failed getting file size from fd", errno); + return 0; // will not be reached. +} + +#ifdef _MSC_VER + #pragma warning(pop) +#endif + +// Return utc offset in minutes or throw spdlog_ex on failure +SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) { +#ifdef _WIN32 + #if _WIN32_WINNT < _WIN32_WINNT_WS08 + TIME_ZONE_INFORMATION tzinfo; + auto rv = ::GetTimeZoneInformation(&tzinfo); + #else + DYNAMIC_TIME_ZONE_INFORMATION tzinfo; + auto rv = ::GetDynamicTimeZoneInformation(&tzinfo); + #endif + if (rv == TIME_ZONE_ID_INVALID) throw_spdlog_ex("Failed getting timezone info. ", errno); + + int offset = -tzinfo.Bias; + if (tm.tm_isdst) { + offset -= tzinfo.DaylightBias; + } else { + offset -= tzinfo.StandardBias; + } + return offset; +#else + + #if defined(sun) || defined(__sun) || defined(_AIX) || \ + (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \ + (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE)) + // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris + struct helper { + static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(), + const std::tm &gmtm = details::os::gmtime()) { + int local_year = localtm.tm_year + (1900 - 1); + int gmt_year = gmtm.tm_year + (1900 - 1); + + long int days = ( + // difference in day of year + localtm.tm_yday - + gmtm.tm_yday + + // + intervening leap days + + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) + + ((local_year / 100 >> 2) - (gmt_year / 100 >> 2)) + + // + difference in years * 365 */ + + static_cast(local_year - gmt_year) * 365); + + long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour); + long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min); + long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec); + + return secs; + } + }; + + auto offset_seconds = helper::calculate_gmt_offset(tm); + #else + auto offset_seconds = tm.tm_gmtoff; + #endif + + return static_cast(offset_seconds / 60); +#endif +} + +// Return current thread id as size_t +// It exists because the std::this_thread::get_id() is much slower(especially +// under VS 2013) +SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT { +#ifdef _WIN32 + return static_cast(::GetCurrentThreadId()); +#elif defined(__linux__) + #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) + #define SYS_gettid __NR_gettid + #endif + return static_cast(::syscall(SYS_gettid)); +#elif defined(_AIX) + struct __pthrdsinfo buf; + int reg_size = 0; + pthread_t pt = pthread_self(); + int retval = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &buf, sizeof(buf), NULL, ®_size); + int tid = (!retval) ? buf.__pi_tid : 0; + return static_cast(tid); +#elif defined(__DragonFly__) || defined(__FreeBSD__) + return static_cast(::pthread_getthreadid_np()); +#elif defined(__NetBSD__) + return static_cast(::_lwp_self()); +#elif defined(__OpenBSD__) + return static_cast(::getthrid()); +#elif defined(__sun) + return static_cast(::thr_self()); +#elif __APPLE__ + uint64_t tid; + // There is no pthread_threadid_np prior to Mac OS X 10.6, and it is not supported on any PPC, + // including 10.6.8 Rosetta. __POWERPC__ is Apple-specific define encompassing ppc and ppc64. + #ifdef MAC_OS_X_VERSION_MAX_ALLOWED + { + #if (MAC_OS_X_VERSION_MAX_ALLOWED < 1060) || defined(__POWERPC__) + tid = pthread_mach_thread_np(pthread_self()); + #elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060 + if (&pthread_threadid_np) { + pthread_threadid_np(nullptr, &tid); + } else { + tid = pthread_mach_thread_np(pthread_self()); + } + #else + pthread_threadid_np(nullptr, &tid); + #endif + } + #else + pthread_threadid_np(nullptr, &tid); + #endif + return static_cast(tid); +#else // Default to standard C++11 (other Unix) + return static_cast(std::hash()(std::this_thread::get_id())); +#endif +} + +// Return current thread id as size_t (from thread local storage) +SPDLOG_INLINE size_t thread_id() SPDLOG_NOEXCEPT { +#if defined(SPDLOG_NO_TLS) + return _thread_id(); +#else // cache thread id in tls + static thread_local const size_t tid = _thread_id(); + return tid; +#endif +} + +// This is avoid msvc issue in sleep_for that happens if the clock changes. +// See https://github.com/gabime/spdlog/issues/609 +SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT { +#if defined(_WIN32) + ::Sleep(milliseconds); +#else + std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); +#endif +} + +// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined) +#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) +SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) { + memory_buf_t buf; + wstr_to_utf8buf(filename, buf); + return SPDLOG_BUF_TO_STRING(buf); +} +#else +SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) { return filename; } +#endif + +SPDLOG_INLINE int pid() SPDLOG_NOEXCEPT { +#ifdef _WIN32 + return conditional_static_cast(::GetCurrentProcessId()); +#else + return conditional_static_cast(::getpid()); +#endif +} + +// Determine if the terminal supports colors +// Based on: https://github.com/agauniyal/rang/ +SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT { +#ifdef _WIN32 + return true; +#else + + static const bool result = []() { + const char *env_colorterm_p = std::getenv("COLORTERM"); + if (env_colorterm_p != nullptr) { + return true; + } + + static constexpr std::array terms = { + {"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys", + "putty", "rxvt", "screen", "vt100", "xterm", "alacritty", "vt102"}}; + + const char *env_term_p = std::getenv("TERM"); + if (env_term_p == nullptr) { + return false; + } + + return std::any_of(terms.begin(), terms.end(), [&](const char *term) { + return std::strstr(env_term_p, term) != nullptr; + }); + }(); + + return result; +#endif +} + +// Determine if the terminal attached +// Source: https://github.com/agauniyal/rang/ +SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT { +#ifdef _WIN32 + return ::_isatty(_fileno(file)) != 0; +#else + return ::isatty(fileno(file)) != 0; +#endif +} + +#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) +SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { + if (wstr.size() > static_cast((std::numeric_limits::max)()) / 4 - 1) { + throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8"); + } + + int wstr_size = static_cast(wstr.size()); + if (wstr_size == 0) { + target.resize(0); + return; + } + + int result_size = static_cast(target.capacity()); + if ((wstr_size + 1) * 4 > result_size) { + result_size = + ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL); + } + + if (result_size > 0) { + target.resize(result_size); + result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), + result_size, NULL, NULL); + + if (result_size > 0) { + target.resize(result_size); + return; + } + } + + throw_spdlog_ex( + fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); +} + +SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { + if (str.size() > static_cast((std::numeric_limits::max)()) - 1) { + throw_spdlog_ex("UTF-8 string is too big to be converted to UTF-16"); + } + + int str_size = static_cast(str.size()); + if (str_size == 0) { + target.resize(0); + return; + } + + // find the size to allocate for the result buffer + int result_size = + ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0); + + if (result_size > 0) { + target.resize(result_size); + result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), + result_size); + if (result_size > 0) { + assert(result_size == target.size()); + return; + } + } + + throw_spdlog_ex( + fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError())); +} +#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && + // defined(_WIN32) + +// return true on success +static SPDLOG_INLINE bool mkdir_(const filename_t &path) { +#ifdef _WIN32 + #ifdef SPDLOG_WCHAR_FILENAMES + return ::_wmkdir(path.c_str()) == 0; + #else + return ::_mkdir(path.c_str()) == 0; + #endif +#else + return ::mkdir(path.c_str(), mode_t(0755)) == 0; +#endif +} + +// create the given directory - and all directories leading to it +// return true on success or if the directory already exists +SPDLOG_INLINE bool create_dir(const filename_t &path) { + if (path_exists(path)) { + return true; + } + + if (path.empty()) { + return false; + } + + size_t search_offset = 0; + do { + auto token_pos = path.find_first_of(folder_seps_filename, search_offset); + // treat the entire path as a folder if no folder separator not found + if (token_pos == filename_t::npos) { + token_pos = path.size(); + } + + auto subdir = path.substr(0, token_pos); +#ifdef _WIN32 + // if subdir is just a drive letter, add a slash e.g. "c:"=>"c:\", + // otherwise path_exists(subdir) returns false (issue #3079) + const bool is_drive = subdir.length() == 2 && subdir[1] == ':'; + if (is_drive) { + subdir += '\\'; + token_pos++; + } +#endif + + if (!subdir.empty() && !path_exists(subdir) && !mkdir_(subdir)) { + return false; // return error if failed creating dir + } + search_offset = token_pos + 1; + } while (search_offset < path.size()); + + return true; +} + +// Return directory name from given path or empty string +// "abc/file" => "abc" +// "abc/" => "abc" +// "abc" => "" +// "abc///" => "abc//" +SPDLOG_INLINE filename_t dir_name(const filename_t &path) { + auto pos = path.find_last_of(folder_seps_filename); + return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; +} + +std::string SPDLOG_INLINE getenv(const char *field) { +#if defined(_MSC_VER) + #if defined(__cplusplus_winrt) + return std::string{}; // not supported under uwp + #else + size_t len = 0; + char buf[128]; + bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0; + return ok ? buf : std::string{}; + #endif +#else // revert to getenv + char *buf = ::getenv(field); + return buf ? buf : std::string{}; +#endif +} + +// Do fsync by FILE handlerpointer +// Return true on success +SPDLOG_INLINE bool fsync(FILE *fp) { +#ifdef _WIN32 + return FlushFileBuffers(reinterpret_cast(_get_osfhandle(_fileno(fp)))) != 0; +#else + return ::fsync(fileno(fp)) == 0; +#endif +} + +// Do non-locking fwrite if possible by the os or use the regular locking fwrite +// Return true on success. +SPDLOG_INLINE bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) { + #if defined(_WIN32) && defined(SPDLOG_FWRITE_UNLOCKED) + return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes; + #elif defined(SPDLOG_FWRITE_UNLOCKED) + return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes; + #else + return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; + #endif +} + +} // namespace os +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h new file mode 100644 index 0000000..5fd12ba --- /dev/null +++ b/include/spdlog/details/os.h @@ -0,0 +1,127 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include // std::time_t +#include + +namespace spdlog { +namespace details { +namespace os { + +SPDLOG_API spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT; + +SPDLOG_API std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT; + +SPDLOG_API std::tm localtime() SPDLOG_NOEXCEPT; + +SPDLOG_API std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT; + +SPDLOG_API std::tm gmtime() SPDLOG_NOEXCEPT; + +// eol definition +#if !defined(SPDLOG_EOL) + #ifdef _WIN32 + #define SPDLOG_EOL "\r\n" + #else + #define SPDLOG_EOL "\n" + #endif +#endif + +SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL; + +// folder separator +#if !defined(SPDLOG_FOLDER_SEPS) + #ifdef _WIN32 + #define SPDLOG_FOLDER_SEPS "\\/" + #else + #define SPDLOG_FOLDER_SEPS "/" + #endif +#endif + +SPDLOG_CONSTEXPR static const char folder_seps[] = SPDLOG_FOLDER_SEPS; +SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] = + SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS); + +// fopen_s on non windows for writing +SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode); + +// Remove filename. return 0 on success +SPDLOG_API int remove(const filename_t &filename) SPDLOG_NOEXCEPT; + +// Remove file if exists. return 0 on success +// Note: Non atomic (might return failure to delete if concurrently deleted by other process/thread) +SPDLOG_API int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT; + +SPDLOG_API int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT; + +// Return if file exists. +SPDLOG_API bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT; + +// Return file size according to open FILE* object +SPDLOG_API size_t filesize(FILE *f); + +// Return utc offset in minutes or throw spdlog_ex on failure +SPDLOG_API int utc_minutes_offset(const std::tm &tm = details::os::localtime()); + +// Return current thread id as size_t +// It exists because the std::this_thread::get_id() is much slower(especially +// under VS 2013) +SPDLOG_API size_t _thread_id() SPDLOG_NOEXCEPT; + +// Return current thread id as size_t (from thread local storage) +SPDLOG_API size_t thread_id() SPDLOG_NOEXCEPT; + +// This is avoid msvc issue in sleep_for that happens if the clock changes. +// See https://github.com/gabime/spdlog/issues/609 +SPDLOG_API void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT; + +SPDLOG_API std::string filename_to_str(const filename_t &filename); + +SPDLOG_API int pid() SPDLOG_NOEXCEPT; + +// Determine if the terminal supports colors +// Source: https://github.com/agauniyal/rang/ +SPDLOG_API bool is_color_terminal() SPDLOG_NOEXCEPT; + +// Determine if the terminal attached +// Source: https://github.com/agauniyal/rang/ +SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; + +#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) +SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); + +SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target); +#endif + +// Return directory name from given path or empty string +// "abc/file" => "abc" +// "abc/" => "abc" +// "abc" => "" +// "abc///" => "abc//" +SPDLOG_API filename_t dir_name(const filename_t &path); + +// Create a dir from the given path. +// Return true if succeeded or if this dir already exists. +SPDLOG_API bool create_dir(const filename_t &path); + +// non thread safe, cross platform getenv/getenv_s +// return empty string if field not found +SPDLOG_API std::string getenv(const char *field); + +// Do fsync by FILE objectpointer. +// Return true on success. +SPDLOG_API bool fsync(FILE *fp); + +// Do non-locking fwrite if possible by the os or use the regular locking fwrite +// Return true on success. +SPDLOG_API bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp); + +} // namespace os +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "os-inl.h" +#endif diff --git a/include/spdlog/details/periodic_worker-inl.h b/include/spdlog/details/periodic_worker-inl.h new file mode 100644 index 0000000..18f11fb --- /dev/null +++ b/include/spdlog/details/periodic_worker-inl.h @@ -0,0 +1,26 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +namespace spdlog { +namespace details { + +// stop the worker thread and join it +SPDLOG_INLINE periodic_worker::~periodic_worker() { + if (worker_thread_.joinable()) { + { + std::lock_guard lock(mutex_); + active_ = false; + } + cv_.notify_one(); + worker_thread_.join(); + } +} + +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/periodic_worker.h b/include/spdlog/details/periodic_worker.h new file mode 100644 index 0000000..d647b66 --- /dev/null +++ b/include/spdlog/details/periodic_worker.h @@ -0,0 +1,58 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// periodic worker thread - periodically executes the given callback function. +// +// RAII over the owned thread: +// creates the thread on construction. +// stops and joins the thread on destruction (if the thread is executing a callback, wait for it +// to finish first). + +#include +#include +#include +#include +#include +namespace spdlog { +namespace details { + +class SPDLOG_API periodic_worker { +public: + template + periodic_worker(const std::function &callback_fun, + std::chrono::duration interval) { + active_ = (interval > std::chrono::duration::zero()); + if (!active_) { + return; + } + + worker_thread_ = std::thread([this, callback_fun, interval]() { + for (;;) { + std::unique_lock lock(this->mutex_); + if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; })) { + return; // active_ == false, so exit this thread + } + callback_fun(); + } + }); + } + std::thread &get_thread() { return worker_thread_; } + periodic_worker(const periodic_worker &) = delete; + periodic_worker &operator=(const periodic_worker &) = delete; + // stop the worker thread and join it + ~periodic_worker(); + +private: + bool active_; + std::thread worker_thread_; + std::mutex mutex_; + std::condition_variable cv_; +}; +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "periodic_worker-inl.h" +#endif diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h new file mode 100644 index 0000000..f447848 --- /dev/null +++ b/include/spdlog/details/registry-inl.h @@ -0,0 +1,261 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include +#include +#include + +#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER + // support for the default stdout color logger + #ifdef _WIN32 + #include + #else + #include + #endif +#endif // SPDLOG_DISABLE_DEFAULT_LOGGER + +#include +#include +#include +#include +#include + +namespace spdlog { +namespace details { + +SPDLOG_INLINE registry::registry() + : formatter_(new pattern_formatter()) { +#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER + // create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows). + #ifdef _WIN32 + auto color_sink = std::make_shared(); + #else + auto color_sink = std::make_shared(); + #endif + + const char *default_logger_name = ""; + default_logger_ = std::make_shared(default_logger_name, std::move(color_sink)); + loggers_[default_logger_name] = default_logger_; + +#endif // SPDLOG_DISABLE_DEFAULT_LOGGER +} + +SPDLOG_INLINE registry::~registry() = default; + +SPDLOG_INLINE void registry::register_logger(std::shared_ptr new_logger) { + std::lock_guard lock(logger_map_mutex_); + register_logger_(std::move(new_logger)); +} + +SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr new_logger) { + std::lock_guard lock(logger_map_mutex_); + new_logger->set_formatter(formatter_->clone()); + + if (err_handler_) { + new_logger->set_error_handler(err_handler_); + } + + // set new level according to previously configured level or default level + auto it = log_levels_.find(new_logger->name()); + auto new_level = it != log_levels_.end() ? it->second : global_log_level_; + new_logger->set_level(new_level); + + new_logger->flush_on(flush_level_); + + if (backtrace_n_messages_ > 0) { + new_logger->enable_backtrace(backtrace_n_messages_); + } + + if (automatic_registration_) { + register_logger_(std::move(new_logger)); + } +} + +SPDLOG_INLINE std::shared_ptr registry::get(const std::string &logger_name) { + std::lock_guard lock(logger_map_mutex_); + auto found = loggers_.find(logger_name); + return found == loggers_.end() ? nullptr : found->second; +} + +SPDLOG_INLINE std::shared_ptr registry::default_logger() { + std::lock_guard lock(logger_map_mutex_); + return default_logger_; +} + +// Return raw ptr to the default logger. +// To be used directly by the spdlog default api (e.g. spdlog::info) +// This make the default API faster, but cannot be used concurrently with set_default_logger(). +// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. +SPDLOG_INLINE logger *registry::get_default_raw() { return default_logger_.get(); } + +// set default logger. +// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map. +SPDLOG_INLINE void registry::set_default_logger(std::shared_ptr new_default_logger) { + std::lock_guard lock(logger_map_mutex_); + if (new_default_logger != nullptr) { + loggers_[new_default_logger->name()] = new_default_logger; + } + default_logger_ = std::move(new_default_logger); +} + +SPDLOG_INLINE void registry::set_tp(std::shared_ptr tp) { + std::lock_guard lock(tp_mutex_); + tp_ = std::move(tp); +} + +SPDLOG_INLINE std::shared_ptr registry::get_tp() { + std::lock_guard lock(tp_mutex_); + return tp_; +} + +// Set global formatter. Each sink in each logger will get a clone of this object +SPDLOG_INLINE void registry::set_formatter(std::unique_ptr formatter) { + std::lock_guard lock(logger_map_mutex_); + formatter_ = std::move(formatter); + for (auto &l : loggers_) { + l.second->set_formatter(formatter_->clone()); + } +} + +SPDLOG_INLINE void registry::enable_backtrace(size_t n_messages) { + std::lock_guard lock(logger_map_mutex_); + backtrace_n_messages_ = n_messages; + + for (auto &l : loggers_) { + l.second->enable_backtrace(n_messages); + } +} + +SPDLOG_INLINE void registry::disable_backtrace() { + std::lock_guard lock(logger_map_mutex_); + backtrace_n_messages_ = 0; + for (auto &l : loggers_) { + l.second->disable_backtrace(); + } +} + +SPDLOG_INLINE void registry::set_level(level::level_enum log_level) { + std::lock_guard lock(logger_map_mutex_); + for (auto &l : loggers_) { + l.second->set_level(log_level); + } + global_log_level_ = log_level; +} + +SPDLOG_INLINE void registry::flush_on(level::level_enum log_level) { + std::lock_guard lock(logger_map_mutex_); + for (auto &l : loggers_) { + l.second->flush_on(log_level); + } + flush_level_ = log_level; +} + +SPDLOG_INLINE void registry::set_error_handler(err_handler handler) { + std::lock_guard lock(logger_map_mutex_); + for (auto &l : loggers_) { + l.second->set_error_handler(handler); + } + err_handler_ = std::move(handler); +} + +SPDLOG_INLINE void registry::apply_all( + const std::function)> &fun) { + std::lock_guard lock(logger_map_mutex_); + for (auto &l : loggers_) { + fun(l.second); + } +} + +SPDLOG_INLINE void registry::flush_all() { + std::lock_guard lock(logger_map_mutex_); + for (auto &l : loggers_) { + l.second->flush(); + } +} + +SPDLOG_INLINE void registry::drop(const std::string &logger_name) { + std::lock_guard lock(logger_map_mutex_); + auto is_default_logger = default_logger_ && default_logger_->name() == logger_name; + loggers_.erase(logger_name); + if (is_default_logger) { + default_logger_.reset(); + } +} + +SPDLOG_INLINE void registry::drop_all() { + std::lock_guard lock(logger_map_mutex_); + loggers_.clear(); + default_logger_.reset(); +} + +// clean all resources and threads started by the registry +SPDLOG_INLINE void registry::shutdown() { + { + std::lock_guard lock(flusher_mutex_); + periodic_flusher_.reset(); + } + + drop_all(); + + { + std::lock_guard lock(tp_mutex_); + tp_.reset(); + } +} + +SPDLOG_INLINE std::recursive_mutex ®istry::tp_mutex() { return tp_mutex_; } + +SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registration) { + std::lock_guard lock(logger_map_mutex_); + automatic_registration_ = automatic_registration; +} + +SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *global_level) { + std::lock_guard lock(logger_map_mutex_); + log_levels_ = std::move(levels); + auto global_level_requested = global_level != nullptr; + global_log_level_ = global_level_requested ? *global_level : global_log_level_; + + for (auto &logger : loggers_) { + auto logger_entry = log_levels_.find(logger.first); + if (logger_entry != log_levels_.end()) { + logger.second->set_level(logger_entry->second); + } else if (global_level_requested) { + logger.second->set_level(*global_level); + } + } +} + +SPDLOG_INLINE registry ®istry::instance() { + static registry s_instance; + return s_instance; +} + +SPDLOG_INLINE void registry::apply_logger_env_levels(std::shared_ptr new_logger) { + std::lock_guard lock(logger_map_mutex_); + auto it = log_levels_.find(new_logger->name()); + auto new_level = it != log_levels_.end() ? it->second : global_log_level_; + new_logger->set_level(new_level); +} + +SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name) { + if (loggers_.find(logger_name) != loggers_.end()) { + throw_spdlog_ex("logger with name '" + logger_name + "' already exists"); + } +} + +SPDLOG_INLINE void registry::register_logger_(std::shared_ptr new_logger) { + auto logger_name = new_logger->name(); + throw_if_exists_(logger_name); + loggers_[logger_name] = std::move(new_logger); +} + +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h new file mode 100644 index 0000000..8afcbd6 --- /dev/null +++ b/include/spdlog/details/registry.h @@ -0,0 +1,129 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// Loggers registry of unique name->logger pointer +// An attempt to create a logger with an already existing name will result with spdlog_ex exception. +// If user requests a non existing logger, nullptr will be returned +// This class is thread safe + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace spdlog { +class logger; + +namespace details { +class thread_pool; + +class SPDLOG_API registry { +public: + using log_levels = std::unordered_map; + registry(const registry &) = delete; + registry &operator=(const registry &) = delete; + + void register_logger(std::shared_ptr new_logger); + void initialize_logger(std::shared_ptr new_logger); + std::shared_ptr get(const std::string &logger_name); + std::shared_ptr default_logger(); + + // Return raw ptr to the default logger. + // To be used directly by the spdlog default api (e.g. spdlog::info) + // This make the default API faster, but cannot be used concurrently with set_default_logger(). + // e.g do not call set_default_logger() from one thread while calling spdlog::info() from + // another. + logger *get_default_raw(); + + // set default logger and add it to the registry if not registered already. + // default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map. + // Note: Make sure to unregister it when no longer needed or before calling again with a new + // logger. + void set_default_logger(std::shared_ptr new_default_logger); + + void set_tp(std::shared_ptr tp); + + std::shared_ptr get_tp(); + + // Set global formatter. Each sink in each logger will get a clone of this object + void set_formatter(std::unique_ptr formatter); + + void enable_backtrace(size_t n_messages); + + void disable_backtrace(); + + void set_level(level::level_enum log_level); + + void flush_on(level::level_enum log_level); + + template + void flush_every(std::chrono::duration interval) { + std::lock_guard lock(flusher_mutex_); + auto clbk = [this]() { this->flush_all(); }; + periodic_flusher_ = details::make_unique(clbk, interval); + } + + std::unique_ptr &get_flusher() { + std::lock_guard lock(flusher_mutex_); + return periodic_flusher_; + } + + void set_error_handler(err_handler handler); + + void apply_all(const std::function)> &fun); + + void flush_all(); + + void drop(const std::string &logger_name); + + void drop_all(); + + // clean all resources and threads started by the registry + void shutdown(); + + std::recursive_mutex &tp_mutex(); + + void set_automatic_registration(bool automatic_registration); + + // set levels for all existing/future loggers. global_level can be null if should not set. + void set_levels(log_levels levels, level::level_enum *global_level); + + static registry &instance(); + + void apply_logger_env_levels(std::shared_ptr new_logger); + +private: + registry(); + ~registry(); + + void throw_if_exists_(const std::string &logger_name); + void register_logger_(std::shared_ptr new_logger); + bool set_level_from_cfg_(logger *logger); + std::mutex logger_map_mutex_, flusher_mutex_; + std::recursive_mutex tp_mutex_; + std::unordered_map> loggers_; + log_levels log_levels_; + std::unique_ptr formatter_; + spdlog::level::level_enum global_log_level_ = level::info; + level::level_enum flush_level_ = level::off; + err_handler err_handler_; + std::shared_ptr tp_; + std::unique_ptr periodic_flusher_; + std::shared_ptr default_logger_; + bool automatic_registration_ = true; + size_t backtrace_n_messages_ = 0; +}; + +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "registry-inl.h" +#endif diff --git a/include/spdlog/details/synchronous_factory.h b/include/spdlog/details/synchronous_factory.h new file mode 100644 index 0000000..4bd5a51 --- /dev/null +++ b/include/spdlog/details/synchronous_factory.h @@ -0,0 +1,22 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "registry.h" + +namespace spdlog { + +// Default logger factory- creates synchronous loggers +class logger; + +struct synchronous_factory { + template + static std::shared_ptr create(std::string logger_name, SinkArgs &&...args) { + auto sink = std::make_shared(std::forward(args)...); + auto new_logger = std::make_shared(std::move(logger_name), std::move(sink)); + details::registry::instance().initialize_logger(new_logger); + return new_logger; + } +}; +} // namespace spdlog diff --git a/include/spdlog/details/tcp_client-windows.h b/include/spdlog/details/tcp_client-windows.h new file mode 100644 index 0000000..bf8f7b8 --- /dev/null +++ b/include/spdlog/details/tcp_client-windows.h @@ -0,0 +1,135 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#define WIN32_LEAN_AND_MEAN +// tcp client helper +#include +#include + +#include +#include +#include +#include +#include +#include + +#pragma comment(lib, "Ws2_32.lib") +#pragma comment(lib, "Mswsock.lib") +#pragma comment(lib, "AdvApi32.lib") + +namespace spdlog { +namespace details { +class tcp_client { + SOCKET socket_ = INVALID_SOCKET; + + static void init_winsock_() { + WSADATA wsaData; + auto rv = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (rv != 0) { + throw_winsock_error_("WSAStartup failed", ::WSAGetLastError()); + } + } + + static void throw_winsock_error_(const std::string &msg, int last_error) { + char buf[512]; + ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, + last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, + (sizeof(buf) / sizeof(char)), NULL); + + throw_spdlog_ex(fmt_lib::format("tcp_sink - {}: {}", msg, buf)); + } + +public: + tcp_client() { init_winsock_(); } + + ~tcp_client() { + close(); + ::WSACleanup(); + } + + bool is_connected() const { return socket_ != INVALID_SOCKET; } + + void close() { + ::closesocket(socket_); + socket_ = INVALID_SOCKET; + } + + SOCKET fd() const { return socket_; } + + // try to connect or throw on failure + void connect(const std::string &host, int port) { + if (is_connected()) { + close(); + } + struct addrinfo hints {}; + ZeroMemory(&hints, sizeof(hints)); + + hints.ai_family = AF_UNSPEC; // To work with IPv4, IPv6, and so on + hints.ai_socktype = SOCK_STREAM; // TCP + hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value + hints.ai_protocol = 0; + + auto port_str = std::to_string(port); + struct addrinfo *addrinfo_result; + auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); + int last_error = 0; + if (rv != 0) { + last_error = ::WSAGetLastError(); + WSACleanup(); + throw_winsock_error_("getaddrinfo failed", last_error); + } + + // Try each address until we successfully connect(2). + + for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) { + socket_ = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if (socket_ == INVALID_SOCKET) { + last_error = ::WSAGetLastError(); + WSACleanup(); + continue; + } + if (::connect(socket_, rp->ai_addr, (int)rp->ai_addrlen) == 0) { + break; + } else { + last_error = ::WSAGetLastError(); + close(); + } + } + ::freeaddrinfo(addrinfo_result); + if (socket_ == INVALID_SOCKET) { + WSACleanup(); + throw_winsock_error_("connect failed", last_error); + } + + // set TCP_NODELAY + int enable_flag = 1; + ::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&enable_flag), + sizeof(enable_flag)); + } + + // Send exactly n_bytes of the given data. + // On error close the connection and throw. + void send(const char *data, size_t n_bytes) { + size_t bytes_sent = 0; + while (bytes_sent < n_bytes) { + const int send_flags = 0; + auto write_result = + ::send(socket_, data + bytes_sent, (int)(n_bytes - bytes_sent), send_flags); + if (write_result == SOCKET_ERROR) { + int last_error = ::WSAGetLastError(); + close(); + throw_winsock_error_("send failed", last_error); + } + + if (write_result == 0) // (probably should not happen but in any case..) + { + break; + } + bytes_sent += static_cast(write_result); + } + } +}; +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/tcp_client.h b/include/spdlog/details/tcp_client.h new file mode 100644 index 0000000..9d3c40d --- /dev/null +++ b/include/spdlog/details/tcp_client.h @@ -0,0 +1,127 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifdef _WIN32 + #error include tcp_client-windows.h instead +#endif + +// tcp client helper +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace spdlog { +namespace details { +class tcp_client { + int socket_ = -1; + +public: + bool is_connected() const { return socket_ != -1; } + + void close() { + if (is_connected()) { + ::close(socket_); + socket_ = -1; + } + } + + int fd() const { return socket_; } + + ~tcp_client() { close(); } + + // try to connect or throw on failure + void connect(const std::string &host, int port) { + close(); + struct addrinfo hints {}; + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; // To work with IPv4, IPv6, and so on + hints.ai_socktype = SOCK_STREAM; // TCP + hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value + hints.ai_protocol = 0; + + auto port_str = std::to_string(port); + struct addrinfo *addrinfo_result; + auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); + if (rv != 0) { + throw_spdlog_ex(fmt_lib::format("::getaddrinfo failed: {}", gai_strerror(rv))); + } + + // Try each address until we successfully connect(2). + int last_errno = 0; + for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) { +#if defined(SOCK_CLOEXEC) + const int flags = SOCK_CLOEXEC; +#else + const int flags = 0; +#endif + socket_ = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol); + if (socket_ == -1) { + last_errno = errno; + continue; + } + rv = ::connect(socket_, rp->ai_addr, rp->ai_addrlen); + if (rv == 0) { + break; + } + last_errno = errno; + ::close(socket_); + socket_ = -1; + } + ::freeaddrinfo(addrinfo_result); + if (socket_ == -1) { + throw_spdlog_ex("::connect failed", last_errno); + } + + // set TCP_NODELAY + int enable_flag = 1; + ::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&enable_flag), + sizeof(enable_flag)); + + // prevent sigpipe on systems where MSG_NOSIGNAL is not available +#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) + ::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast(&enable_flag), + sizeof(enable_flag)); +#endif + +#if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) + #error "tcp_sink would raise SIGPIPE since neither SO_NOSIGPIPE nor MSG_NOSIGNAL are available" +#endif + } + + // Send exactly n_bytes of the given data. + // On error close the connection and throw. + void send(const char *data, size_t n_bytes) { + size_t bytes_sent = 0; + while (bytes_sent < n_bytes) { +#if defined(MSG_NOSIGNAL) + const int send_flags = MSG_NOSIGNAL; +#else + const int send_flags = 0; +#endif + auto write_result = + ::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags); + if (write_result < 0) { + close(); + throw_spdlog_ex("write(2) failed", errno); + } + + if (write_result == 0) // (probably should not happen but in any case..) + { + break; + } + bytes_sent += static_cast(write_result); + } + } +}; +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h new file mode 100644 index 0000000..17e01c0 --- /dev/null +++ b/include/spdlog/details/thread_pool-inl.h @@ -0,0 +1,127 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +namespace spdlog { +namespace details { + +SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, + size_t threads_n, + std::function on_thread_start, + std::function on_thread_stop) + : q_(q_max_items) { + if (threads_n == 0 || threads_n > 1000) { + throw_spdlog_ex( + "spdlog::thread_pool(): invalid threads_n param (valid " + "range is 1-1000)"); + } + for (size_t i = 0; i < threads_n; i++) { + threads_.emplace_back([this, on_thread_start, on_thread_stop] { + on_thread_start(); + this->thread_pool::worker_loop_(); + on_thread_stop(); + }); + } +} + +SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, + size_t threads_n, + std::function on_thread_start) + : thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {} + +SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n) + : thread_pool( + q_max_items, threads_n, [] {}, [] {}) {} + +// message all threads to terminate gracefully join them +SPDLOG_INLINE thread_pool::~thread_pool() { + SPDLOG_TRY { + for (size_t i = 0; i < threads_.size(); i++) { + post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block); + } + + for (auto &t : threads_) { + t.join(); + } + } + SPDLOG_CATCH_STD +} + +void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, + const details::log_msg &msg, + async_overflow_policy overflow_policy) { + async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg); + post_async_msg_(std::move(async_m), overflow_policy); +} + +void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, + async_overflow_policy overflow_policy) { + post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); +} + +size_t SPDLOG_INLINE thread_pool::overrun_counter() { return q_.overrun_counter(); } + +void SPDLOG_INLINE thread_pool::reset_overrun_counter() { q_.reset_overrun_counter(); } + +size_t SPDLOG_INLINE thread_pool::discard_counter() { return q_.discard_counter(); } + +void SPDLOG_INLINE thread_pool::reset_discard_counter() { q_.reset_discard_counter(); } + +size_t SPDLOG_INLINE thread_pool::queue_size() { return q_.size(); } + +void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, + async_overflow_policy overflow_policy) { + if (overflow_policy == async_overflow_policy::block) { + q_.enqueue(std::move(new_msg)); + } else if (overflow_policy == async_overflow_policy::overrun_oldest) { + q_.enqueue_nowait(std::move(new_msg)); + } else { + assert(overflow_policy == async_overflow_policy::discard_new); + q_.enqueue_if_have_room(std::move(new_msg)); + } +} + +void SPDLOG_INLINE thread_pool::worker_loop_() { + while (process_next_msg_()) { + } +} + +// process next message in the queue +// return true if this thread should still be active (while no terminate msg +// was received) +bool SPDLOG_INLINE thread_pool::process_next_msg_() { + async_msg incoming_async_msg; + q_.dequeue(incoming_async_msg); + + switch (incoming_async_msg.msg_type) { + case async_msg_type::log: { + incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg); + return true; + } + case async_msg_type::flush: { + incoming_async_msg.worker_ptr->backend_flush_(); + return true; + } + + case async_msg_type::terminate: { + return false; + } + + default: { + assert(false); + } + } + + return true; +} + +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h new file mode 100644 index 0000000..f22b078 --- /dev/null +++ b/include/spdlog/details/thread_pool.h @@ -0,0 +1,117 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace spdlog { +class async_logger; + +namespace details { + +using async_logger_ptr = std::shared_ptr; + +enum class async_msg_type { log, flush, terminate }; + +// Async msg to move to/from the queue +// Movable only. should never be copied +struct async_msg : log_msg_buffer { + async_msg_type msg_type{async_msg_type::log}; + async_logger_ptr worker_ptr; + + async_msg() = default; + ~async_msg() = default; + + // should only be moved in or out of the queue.. + async_msg(const async_msg &) = delete; + +// support for vs2013 move +#if defined(_MSC_VER) && _MSC_VER <= 1800 + async_msg(async_msg &&other) + : log_msg_buffer(std::move(other)), + msg_type(other.msg_type), + worker_ptr(std::move(other.worker_ptr)) {} + + async_msg &operator=(async_msg &&other) { + *static_cast(this) = std::move(other); + msg_type = other.msg_type; + worker_ptr = std::move(other.worker_ptr); + return *this; + } +#else // (_MSC_VER) && _MSC_VER <= 1800 + async_msg(async_msg &&) = default; + async_msg &operator=(async_msg &&) = default; +#endif + + // construct from log_msg with given type + async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m) + : log_msg_buffer{m}, + msg_type{the_type}, + worker_ptr{std::move(worker)} {} + + async_msg(async_logger_ptr &&worker, async_msg_type the_type) + : log_msg_buffer{}, + msg_type{the_type}, + worker_ptr{std::move(worker)} {} + + explicit async_msg(async_msg_type the_type) + : async_msg{nullptr, the_type} {} +}; + +class SPDLOG_API thread_pool { +public: + using item_type = async_msg; + using q_type = details::mpmc_blocking_queue; + + thread_pool(size_t q_max_items, + size_t threads_n, + std::function on_thread_start, + std::function on_thread_stop); + thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start); + thread_pool(size_t q_max_items, size_t threads_n); + + // message all threads to terminate gracefully and join them + ~thread_pool(); + + thread_pool(const thread_pool &) = delete; + thread_pool &operator=(thread_pool &&) = delete; + + void post_log(async_logger_ptr &&worker_ptr, + const details::log_msg &msg, + async_overflow_policy overflow_policy); + void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy); + size_t overrun_counter(); + void reset_overrun_counter(); + size_t discard_counter(); + void reset_discard_counter(); + size_t queue_size(); + +private: + q_type q_; + + std::vector threads_; + + void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy); + void worker_loop_(); + + // process next message in the queue + // return true if this thread should still be active (while no terminate msg + // was received) + bool process_next_msg_(); +}; + +} // namespace details +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "thread_pool-inl.h" +#endif diff --git a/include/spdlog/details/udp_client-windows.h b/include/spdlog/details/udp_client-windows.h new file mode 100644 index 0000000..8b7c223 --- /dev/null +++ b/include/spdlog/details/udp_client-windows.h @@ -0,0 +1,98 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// Helper RAII over winsock udp client socket. +// Will throw on construction if socket creation failed. + +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(_MSC_VER) + #pragma comment(lib, "Ws2_32.lib") + #pragma comment(lib, "Mswsock.lib") + #pragma comment(lib, "AdvApi32.lib") +#endif + +namespace spdlog { +namespace details { +class udp_client { + static constexpr int TX_BUFFER_SIZE = 1024 * 10; + SOCKET socket_ = INVALID_SOCKET; + sockaddr_in addr_ = {}; + + static void init_winsock_() { + WSADATA wsaData; + auto rv = ::WSAStartup(MAKEWORD(2, 2), &wsaData); + if (rv != 0) { + throw_winsock_error_("WSAStartup failed", ::WSAGetLastError()); + } + } + + static void throw_winsock_error_(const std::string &msg, int last_error) { + char buf[512]; + ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, + last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, + (sizeof(buf) / sizeof(char)), NULL); + + throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf)); + } + + void cleanup_() { + if (socket_ != INVALID_SOCKET) { + ::closesocket(socket_); + } + socket_ = INVALID_SOCKET; + ::WSACleanup(); + } + +public: + udp_client(const std::string &host, uint16_t port) { + init_winsock_(); + + addr_.sin_family = PF_INET; + addr_.sin_port = htons(port); + addr_.sin_addr.s_addr = INADDR_ANY; + if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1) { + int last_error = ::WSAGetLastError(); + ::WSACleanup(); + throw_winsock_error_("error: Invalid address!", last_error); + } + + socket_ = ::socket(PF_INET, SOCK_DGRAM, 0); + if (socket_ == INVALID_SOCKET) { + int last_error = ::WSAGetLastError(); + ::WSACleanup(); + throw_winsock_error_("error: Create Socket failed", last_error); + } + + int option_value = TX_BUFFER_SIZE; + if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, + reinterpret_cast(&option_value), sizeof(option_value)) < 0) { + int last_error = ::WSAGetLastError(); + cleanup_(); + throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error); + } + } + + ~udp_client() { cleanup_(); } + + SOCKET fd() const { return socket_; } + + void send(const char *data, size_t n_bytes) { + socklen_t tolen = sizeof(struct sockaddr); + if (::sendto(socket_, data, static_cast(n_bytes), 0, (struct sockaddr *)&addr_, + tolen) == -1) { + throw_spdlog_ex("sendto(2) failed", errno); + } + } +}; +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/udp_client.h b/include/spdlog/details/udp_client.h new file mode 100644 index 0000000..95826f5 --- /dev/null +++ b/include/spdlog/details/udp_client.h @@ -0,0 +1,81 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// Helper RAII over unix udp client socket. +// Will throw on construction if the socket creation failed. + +#ifdef _WIN32 + #error "include udp_client-windows.h instead" +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace spdlog { +namespace details { + +class udp_client { + static constexpr int TX_BUFFER_SIZE = 1024 * 10; + int socket_ = -1; + struct sockaddr_in sockAddr_; + + void cleanup_() { + if (socket_ != -1) { + ::close(socket_); + socket_ = -1; + } + } + +public: + udp_client(const std::string &host, uint16_t port) { + socket_ = ::socket(PF_INET, SOCK_DGRAM, 0); + if (socket_ < 0) { + throw_spdlog_ex("error: Create Socket Failed!"); + } + + int option_value = TX_BUFFER_SIZE; + if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, + reinterpret_cast(&option_value), sizeof(option_value)) < 0) { + cleanup_(); + throw_spdlog_ex("error: setsockopt(SO_SNDBUF) Failed!"); + } + + sockAddr_.sin_family = AF_INET; + sockAddr_.sin_port = htons(port); + + if (::inet_aton(host.c_str(), &sockAddr_.sin_addr) == 0) { + cleanup_(); + throw_spdlog_ex("error: Invalid address!"); + } + + ::memset(sockAddr_.sin_zero, 0x00, sizeof(sockAddr_.sin_zero)); + } + + ~udp_client() { cleanup_(); } + + int fd() const { return socket_; } + + // Send exactly n_bytes of the given data. + // On error close the connection and throw. + void send(const char *data, size_t n_bytes) { + ssize_t toslen = 0; + socklen_t tolen = sizeof(struct sockaddr); + if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == + -1) { + throw_spdlog_ex("sendto(2) failed", errno); + } + } +}; +} // namespace details +} // namespace spdlog diff --git a/include/spdlog/details/windows_include.h b/include/spdlog/details/windows_include.h new file mode 100644 index 0000000..bbab59b --- /dev/null +++ b/include/spdlog/details/windows_include.h @@ -0,0 +1,11 @@ +#pragma once + +#ifndef NOMINMAX + #define NOMINMAX // prevent windows redefining min/max +#endif + +#ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN +#endif + +#include diff --git a/include/spdlog/fmt/bin_to_hex.h b/include/spdlog/fmt/bin_to_hex.h new file mode 100644 index 0000000..c2998d5 --- /dev/null +++ b/include/spdlog/fmt/bin_to_hex.h @@ -0,0 +1,224 @@ +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once + +#include +#include + +#if defined(__has_include) + #if __has_include() + #include + #endif +#endif + +#if __cpp_lib_span >= 202002L + #include +#endif + +// +// Support for logging binary data as hex +// format flags, any combination of the following: +// {:X} - print in uppercase. +// {:s} - don't separate each byte with space. +// {:p} - don't print the position on each line start. +// {:n} - don't split the output to lines. +// {:a} - show ASCII if :n is not set + +// +// Examples: +// +// std::vector v(200, 0x0b); +// logger->info("Some buffer {}", spdlog::to_hex(v)); +// char buf[128]; +// logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf))); +// logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf), 16)); + +namespace spdlog { +namespace details { + +template +class dump_info { +public: + dump_info(It range_begin, It range_end, size_t size_per_line) + : begin_(range_begin), + end_(range_end), + size_per_line_(size_per_line) {} + + // do not use begin() and end() to avoid collision with fmt/ranges + It get_begin() const { return begin_; } + It get_end() const { return end_; } + size_t size_per_line() const { return size_per_line_; } + +private: + It begin_, end_; + size_t size_per_line_; +}; +} // namespace details + +// create a dump_info that wraps the given container +template +inline details::dump_info to_hex(const Container &container, + size_t size_per_line = 32) { + static_assert(sizeof(typename Container::value_type) == 1, + "sizeof(Container::value_type) != 1"); + using Iter = typename Container::const_iterator; + return details::dump_info(std::begin(container), std::end(container), size_per_line); +} + +#if __cpp_lib_span >= 202002L + +template +inline details::dump_info::iterator> to_hex( + const std::span &container, size_t size_per_line = 32) { + using Container = std::span; + static_assert(sizeof(typename Container::value_type) == 1, + "sizeof(Container::value_type) != 1"); + using Iter = typename Container::iterator; + return details::dump_info(std::begin(container), std::end(container), size_per_line); +} + +#endif + +// create dump_info from ranges +template +inline details::dump_info to_hex(const It range_begin, + const It range_end, + size_t size_per_line = 32) { + return details::dump_info(range_begin, range_end, size_per_line); +} + +} // namespace spdlog + +namespace +#ifdef SPDLOG_USE_STD_FORMAT + std +#else + fmt +#endif +{ + +template +struct formatter, char> { + const char delimiter = ' '; + bool put_newlines = true; + bool put_delimiters = true; + bool use_uppercase = false; + bool put_positions = true; // position on start of each line + bool show_ascii = false; + + // parse the format string flags + template + SPDLOG_CONSTEXPR_FUNC auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { + auto it = ctx.begin(); + while (it != ctx.end() && *it != '}') { + switch (*it) { + case 'X': + use_uppercase = true; + break; + case 's': + put_delimiters = false; + break; + case 'p': + put_positions = false; + break; + case 'n': + put_newlines = false; + show_ascii = false; + break; + case 'a': + if (put_newlines) { + show_ascii = true; + } + break; + } + + ++it; + } + return it; + } + + // format the given bytes range as hex + template + auto format(const spdlog::details::dump_info &the_range, FormatContext &ctx) const + -> decltype(ctx.out()) { + SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF"; + SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef"; + const char *hex_chars = use_uppercase ? hex_upper : hex_lower; + +#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION < 60000 + auto inserter = ctx.begin(); +#else + auto inserter = ctx.out(); +#endif + + int size_per_line = static_cast(the_range.size_per_line()); + auto start_of_line = the_range.get_begin(); + for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) { + auto ch = static_cast(*i); + + if (put_newlines && + (i == the_range.get_begin() || i - start_of_line >= size_per_line)) { + if (show_ascii && i != the_range.get_begin()) { + *inserter++ = delimiter; + *inserter++ = delimiter; + for (auto j = start_of_line; j < i; j++) { + auto pc = static_cast(*j); + *inserter++ = std::isprint(pc) ? static_cast(*j) : '.'; + } + } + + put_newline(inserter, static_cast(i - the_range.get_begin())); + + // put first byte without delimiter in front of it + *inserter++ = hex_chars[(ch >> 4) & 0x0f]; + *inserter++ = hex_chars[ch & 0x0f]; + start_of_line = i; + continue; + } + + if (put_delimiters && i != the_range.get_begin()) { + *inserter++ = delimiter; + } + + *inserter++ = hex_chars[(ch >> 4) & 0x0f]; + *inserter++ = hex_chars[ch & 0x0f]; + } + if (show_ascii) // add ascii to last line + { + if (the_range.get_end() - the_range.get_begin() > size_per_line) { + auto blank_num = size_per_line - (the_range.get_end() - start_of_line); + while (blank_num-- > 0) { + *inserter++ = delimiter; + *inserter++ = delimiter; + if (put_delimiters) { + *inserter++ = delimiter; + } + } + } + *inserter++ = delimiter; + *inserter++ = delimiter; + for (auto j = start_of_line; j != the_range.get_end(); j++) { + auto pc = static_cast(*j); + *inserter++ = std::isprint(pc) ? static_cast(*j) : '.'; + } + } + return inserter; + } + + // put newline(and position header) + template + void put_newline(It inserter, std::size_t pos) const { +#ifdef _WIN32 + *inserter++ = '\r'; +#endif + *inserter++ = '\n'; + + if (put_positions) { + spdlog::fmt_lib::format_to(inserter, SPDLOG_FMT_STRING("{:04X}: "), pos); + } + } +}; +} // namespace std diff --git a/include/spdlog/fmt/bundled/args.h b/include/spdlog/fmt/bundled/args.h new file mode 100644 index 0000000..31a60e8 --- /dev/null +++ b/include/spdlog/fmt/bundled/args.h @@ -0,0 +1,228 @@ +// Formatting library for C++ - dynamic argument lists +// +// Copyright (c) 2012 - present, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_ARGS_H_ +#define FMT_ARGS_H_ + +#ifndef FMT_MODULE +# include // std::reference_wrapper +# include // std::unique_ptr +# include +#endif + +#include "format.h" // std_string_view + +FMT_BEGIN_NAMESPACE + +namespace detail { + +template struct is_reference_wrapper : std::false_type {}; +template +struct is_reference_wrapper> : std::true_type {}; + +template auto unwrap(const T& v) -> const T& { return v; } +template +auto unwrap(const std::reference_wrapper& v) -> const T& { + return static_cast(v); +} + +// node is defined outside dynamic_arg_list to workaround a C2504 bug in MSVC +// 2022 (v17.10.0). +// +// Workaround for clang's -Wweak-vtables. Unlike for regular classes, for +// templates it doesn't complain about inability to deduce single translation +// unit for placing vtable. So node is made a fake template. +template struct node { + virtual ~node() = default; + std::unique_ptr> next; +}; + +class dynamic_arg_list { + template struct typed_node : node<> { + T value; + + template + FMT_CONSTEXPR typed_node(const Arg& arg) : value(arg) {} + + template + FMT_CONSTEXPR typed_node(const basic_string_view& arg) + : value(arg.data(), arg.size()) {} + }; + + std::unique_ptr> head_; + + public: + template auto push(const Arg& arg) -> const T& { + auto new_node = std::unique_ptr>(new typed_node(arg)); + auto& value = new_node->value; + new_node->next = std::move(head_); + head_ = std::move(new_node); + return value; + } +}; +} // namespace detail + +/** + * A dynamic list of formatting arguments with storage. + * + * It can be implicitly converted into `fmt::basic_format_args` for passing + * into type-erased formatting functions such as `fmt::vformat`. + */ +template +class dynamic_format_arg_store +#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 + // Workaround a GCC template argument substitution bug. + : public basic_format_args +#endif +{ + private: + using char_type = typename Context::char_type; + + template struct need_copy { + static constexpr detail::type mapped_type = + detail::mapped_type_constant::value; + + enum { + value = !(detail::is_reference_wrapper::value || + std::is_same>::value || + std::is_same>::value || + (mapped_type != detail::type::cstring_type && + mapped_type != detail::type::string_type && + mapped_type != detail::type::custom_type)) + }; + }; + + template + using stored_type = conditional_t< + std::is_convertible>::value && + !detail::is_reference_wrapper::value, + std::basic_string, T>; + + // Storage of basic_format_arg must be contiguous. + std::vector> data_; + std::vector> named_info_; + + // Storage of arguments not fitting into basic_format_arg must grow + // without relocation because items in data_ refer to it. + detail::dynamic_arg_list dynamic_args_; + + friend class basic_format_args; + + auto get_types() const -> unsigned long long { + return detail::is_unpacked_bit | data_.size() | + (named_info_.empty() + ? 0ULL + : static_cast(detail::has_named_args_bit)); + } + + auto data() const -> const basic_format_arg* { + return named_info_.empty() ? data_.data() : data_.data() + 1; + } + + template void emplace_arg(const T& arg) { + data_.emplace_back(detail::make_arg(arg)); + } + + template + void emplace_arg(const detail::named_arg& arg) { + if (named_info_.empty()) { + constexpr const detail::named_arg_info* zero_ptr{nullptr}; + data_.insert(data_.begin(), {zero_ptr, 0}); + } + data_.emplace_back(detail::make_arg(detail::unwrap(arg.value))); + auto pop_one = [](std::vector>* data) { + data->pop_back(); + }; + std::unique_ptr>, decltype(pop_one)> + guard{&data_, pop_one}; + named_info_.push_back({arg.name, static_cast(data_.size() - 2u)}); + data_[0].value_.named_args = {named_info_.data(), named_info_.size()}; + guard.release(); + } + + public: + constexpr dynamic_format_arg_store() = default; + + /** + * Adds an argument into the dynamic store for later passing to a formatting + * function. + * + * Note that custom types and string types (but not string views) are copied + * into the store dynamically allocating memory if necessary. + * + * **Example**: + * + * fmt::dynamic_format_arg_store store; + * store.push_back(42); + * store.push_back("abc"); + * store.push_back(1.5f); + * std::string result = fmt::vformat("{} and {} and {}", store); + */ + template void push_back(const T& arg) { + if (detail::const_check(need_copy::value)) + emplace_arg(dynamic_args_.push>(arg)); + else + emplace_arg(detail::unwrap(arg)); + } + + /** + * Adds a reference to the argument into the dynamic store for later passing + * to a formatting function. + * + * **Example**: + * + * fmt::dynamic_format_arg_store store; + * char band[] = "Rolling Stones"; + * store.push_back(std::cref(band)); + * band[9] = 'c'; // Changing str affects the output. + * std::string result = fmt::vformat("{}", store); + * // result == "Rolling Scones" + */ + template void push_back(std::reference_wrapper arg) { + static_assert( + need_copy::value, + "objects of built-in types and string views are always copied"); + emplace_arg(arg.get()); + } + + /** + * Adds named argument into the dynamic store for later passing to a + * formatting function. `std::reference_wrapper` is supported to avoid + * copying of the argument. The name is always copied into the store. + */ + template + void push_back(const detail::named_arg& arg) { + const char_type* arg_name = + dynamic_args_.push>(arg.name).c_str(); + if (detail::const_check(need_copy::value)) { + emplace_arg( + fmt::arg(arg_name, dynamic_args_.push>(arg.value))); + } else { + emplace_arg(fmt::arg(arg_name, arg.value)); + } + } + + /// Erase all elements from the store. + void clear() { + data_.clear(); + named_info_.clear(); + dynamic_args_ = detail::dynamic_arg_list(); + } + + /// Reserves space to store at least `new_cap` arguments including + /// `new_cap_named` named arguments. + void reserve(size_t new_cap, size_t new_cap_named) { + FMT_ASSERT(new_cap >= new_cap_named, + "Set of arguments includes set of named arguments"); + data_.reserve(new_cap); + named_info_.reserve(new_cap_named); + } +}; + +FMT_END_NAMESPACE + +#endif // FMT_ARGS_H_ diff --git a/include/spdlog/fmt/bundled/base.h b/include/spdlog/fmt/bundled/base.h new file mode 100644 index 0000000..6276494 --- /dev/null +++ b/include/spdlog/fmt/bundled/base.h @@ -0,0 +1,3077 @@ +// Formatting library for C++ - the base API for char/UTF-8 +// +// Copyright (c) 2012 - present, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_BASE_H_ +#define FMT_BASE_H_ + +#if defined(FMT_IMPORT_STD) && !defined(FMT_MODULE) +# define FMT_MODULE +#endif + +#ifndef FMT_MODULE +# include // CHAR_BIT +# include // FILE +# include // strlen + +// is also included transitively from . +# include // std::byte +# include // std::enable_if +#endif + +// The fmt library version in the form major * 10000 + minor * 100 + patch. +#define FMT_VERSION 110002 + +// Detect compiler versions. +#if defined(__clang__) && !defined(__ibmxl__) +# define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) +#else +# define FMT_CLANG_VERSION 0 +#endif +#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) +# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) +#else +# define FMT_GCC_VERSION 0 +#endif +#if defined(__ICL) +# define FMT_ICC_VERSION __ICL +#elif defined(__INTEL_COMPILER) +# define FMT_ICC_VERSION __INTEL_COMPILER +#else +# define FMT_ICC_VERSION 0 +#endif +#if defined(_MSC_VER) +# define FMT_MSC_VERSION _MSC_VER +#else +# define FMT_MSC_VERSION 0 +#endif + +// Detect standard library versions. +#ifdef _GLIBCXX_RELEASE +# define FMT_GLIBCXX_RELEASE _GLIBCXX_RELEASE +#else +# define FMT_GLIBCXX_RELEASE 0 +#endif +#ifdef _LIBCPP_VERSION +# define FMT_LIBCPP_VERSION _LIBCPP_VERSION +#else +# define FMT_LIBCPP_VERSION 0 +#endif + +#ifdef _MSVC_LANG +# define FMT_CPLUSPLUS _MSVC_LANG +#else +# define FMT_CPLUSPLUS __cplusplus +#endif + +// Detect __has_*. +#ifdef __has_feature +# define FMT_HAS_FEATURE(x) __has_feature(x) +#else +# define FMT_HAS_FEATURE(x) 0 +#endif +#ifdef __has_include +# define FMT_HAS_INCLUDE(x) __has_include(x) +#else +# define FMT_HAS_INCLUDE(x) 0 +#endif +#ifdef __has_cpp_attribute +# define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) +#else +# define FMT_HAS_CPP_ATTRIBUTE(x) 0 +#endif + +#define FMT_HAS_CPP14_ATTRIBUTE(attribute) \ + (FMT_CPLUSPLUS >= 201402L && FMT_HAS_CPP_ATTRIBUTE(attribute)) + +#define FMT_HAS_CPP17_ATTRIBUTE(attribute) \ + (FMT_CPLUSPLUS >= 201703L && FMT_HAS_CPP_ATTRIBUTE(attribute)) + +// Detect C++14 relaxed constexpr. +#ifdef FMT_USE_CONSTEXPR +// Use the provided definition. +#elif FMT_GCC_VERSION >= 600 && FMT_CPLUSPLUS >= 201402L +// GCC only allows throw in constexpr since version 6: +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67371. +# define FMT_USE_CONSTEXPR 1 +#elif FMT_ICC_VERSION +# define FMT_USE_CONSTEXPR 0 // https://github.com/fmtlib/fmt/issues/1628 +#elif FMT_HAS_FEATURE(cxx_relaxed_constexpr) || FMT_MSC_VERSION >= 1912 +# define FMT_USE_CONSTEXPR 1 +#else +# define FMT_USE_CONSTEXPR 0 +#endif +#if FMT_USE_CONSTEXPR +# define FMT_CONSTEXPR constexpr +#else +# define FMT_CONSTEXPR +#endif + +// Detect consteval, C++20 constexpr extensions and std::is_constant_evaluated. +#if !defined(__cpp_lib_is_constant_evaluated) +# define FMT_USE_CONSTEVAL 0 +#elif FMT_CPLUSPLUS < 201709L +# define FMT_USE_CONSTEVAL 0 +#elif FMT_GLIBCXX_RELEASE && FMT_GLIBCXX_RELEASE < 10 +# define FMT_USE_CONSTEVAL 0 +#elif FMT_LIBCPP_VERSION && FMT_LIBCPP_VERSION < 10000 +# define FMT_USE_CONSTEVAL 0 +#elif defined(__apple_build_version__) && __apple_build_version__ < 14000029L +# define FMT_USE_CONSTEVAL 0 // consteval is broken in Apple clang < 14. +#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1929 +# define FMT_USE_CONSTEVAL 0 // consteval is broken in MSVC VS2019 < 16.10. +#elif defined(__cpp_consteval) +# define FMT_USE_CONSTEVAL 1 +#elif FMT_GCC_VERSION >= 1002 || FMT_CLANG_VERSION >= 1101 +# define FMT_USE_CONSTEVAL 1 +#else +# define FMT_USE_CONSTEVAL 0 +#endif +#if FMT_USE_CONSTEVAL +# define FMT_CONSTEVAL consteval +# define FMT_CONSTEXPR20 constexpr +#else +# define FMT_CONSTEVAL +# define FMT_CONSTEXPR20 +#endif + +#if defined(FMT_USE_NONTYPE_TEMPLATE_ARGS) +// Use the provided definition. +#elif defined(__NVCOMPILER) +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 +#elif FMT_GCC_VERSION >= 903 && FMT_CPLUSPLUS >= 201709L +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 +#elif defined(__cpp_nontype_template_args) && \ + __cpp_nontype_template_args >= 201911L +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 +#elif FMT_CLANG_VERSION >= 1200 && FMT_CPLUSPLUS >= 202002L +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 +#else +# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 +#endif + +#ifdef FMT_USE_CONCEPTS +// Use the provided definition. +#elif defined(__cpp_concepts) +# define FMT_USE_CONCEPTS 1 +#else +# define FMT_USE_CONCEPTS 0 +#endif + +// Check if exceptions are disabled. +#ifdef FMT_EXCEPTIONS +// Use the provided definition. +#elif defined(__GNUC__) && !defined(__EXCEPTIONS) +# define FMT_EXCEPTIONS 0 +#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS +# define FMT_EXCEPTIONS 0 +#else +# define FMT_EXCEPTIONS 1 +#endif +#if FMT_EXCEPTIONS +# define FMT_TRY try +# define FMT_CATCH(x) catch (x) +#else +# define FMT_TRY if (true) +# define FMT_CATCH(x) if (false) +#endif + +#if FMT_HAS_CPP17_ATTRIBUTE(fallthrough) +# define FMT_FALLTHROUGH [[fallthrough]] +#elif defined(__clang__) +# define FMT_FALLTHROUGH [[clang::fallthrough]] +#elif FMT_GCC_VERSION >= 700 && \ + (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 520) +# define FMT_FALLTHROUGH [[gnu::fallthrough]] +#else +# define FMT_FALLTHROUGH +#endif + +// Disable [[noreturn]] on MSVC/NVCC because of bogus unreachable code warnings. +#if FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VERSION && !defined(__NVCC__) +# define FMT_NORETURN [[noreturn]] +#else +# define FMT_NORETURN +#endif + +#ifndef FMT_NODISCARD +# if FMT_HAS_CPP17_ATTRIBUTE(nodiscard) +# define FMT_NODISCARD [[nodiscard]] +# else +# define FMT_NODISCARD +# endif +#endif + +#ifdef FMT_DEPRECATED +// Use the provided definition. +#elif FMT_HAS_CPP14_ATTRIBUTE(deprecated) +# define FMT_DEPRECATED [[deprecated]] +#else +# define FMT_DEPRECATED /* deprecated */ +#endif + +#ifdef FMT_INLINE +// Use the provided definition. +#elif FMT_GCC_VERSION || FMT_CLANG_VERSION +# define FMT_ALWAYS_INLINE inline __attribute__((always_inline)) +#else +# define FMT_ALWAYS_INLINE inline +#endif +// A version of FMT_INLINE to prevent code bloat in debug mode. +#ifdef NDEBUG +# define FMT_INLINE FMT_ALWAYS_INLINE +#else +# define FMT_INLINE inline +#endif + +#if FMT_GCC_VERSION || FMT_CLANG_VERSION +# define FMT_VISIBILITY(value) __attribute__((visibility(value))) +#else +# define FMT_VISIBILITY(value) +#endif + +#ifndef FMT_GCC_PRAGMA +// Workaround a _Pragma bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59884 +// and an nvhpc warning: https://github.com/fmtlib/fmt/pull/2582. +# if FMT_GCC_VERSION >= 504 && !defined(__NVCOMPILER) +# define FMT_GCC_PRAGMA(arg) _Pragma(arg) +# else +# define FMT_GCC_PRAGMA(arg) +# endif +#endif + +// GCC < 5 requires this-> in decltype. +#if FMT_GCC_VERSION && FMT_GCC_VERSION < 500 +# define FMT_DECLTYPE_THIS this-> +#else +# define FMT_DECLTYPE_THIS +#endif + +#if FMT_MSC_VERSION +# define FMT_MSC_WARNING(...) __pragma(warning(__VA_ARGS__)) +# define FMT_UNCHECKED_ITERATOR(It) \ + using _Unchecked_type = It // Mark iterator as checked. +#else +# define FMT_MSC_WARNING(...) +# define FMT_UNCHECKED_ITERATOR(It) using unchecked_type = It +#endif + +#ifndef FMT_BEGIN_NAMESPACE +# define FMT_BEGIN_NAMESPACE \ + namespace fmt { \ + inline namespace v11 { +# define FMT_END_NAMESPACE \ + } \ + } +#endif + +#ifndef FMT_EXPORT +# define FMT_EXPORT +# define FMT_BEGIN_EXPORT +# define FMT_END_EXPORT +#endif + +#if !defined(FMT_HEADER_ONLY) && defined(_WIN32) +# if defined(FMT_LIB_EXPORT) +# define FMT_API __declspec(dllexport) +# elif defined(FMT_SHARED) +# define FMT_API __declspec(dllimport) +# endif +#elif defined(FMT_LIB_EXPORT) || defined(FMT_SHARED) +# define FMT_API FMT_VISIBILITY("default") +#endif +#ifndef FMT_API +# define FMT_API +#endif + +#ifndef FMT_UNICODE +# define FMT_UNICODE 1 +#endif + +// Check if rtti is available. +#ifndef FMT_USE_RTTI +// __RTTI is for EDG compilers. _CPPRTTI is for MSVC. +# if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \ + defined(__INTEL_RTTI__) || defined(__RTTI) +# define FMT_USE_RTTI 1 +# else +# define FMT_USE_RTTI 0 +# endif +#endif + +#define FMT_FWD(...) static_cast(__VA_ARGS__) + +// Enable minimal optimizations for more compact code in debug mode. +FMT_GCC_PRAGMA("GCC push_options") +#if !defined(__OPTIMIZE__) && !defined(__CUDACC__) +FMT_GCC_PRAGMA("GCC optimize(\"Og\")") +#endif + +FMT_BEGIN_NAMESPACE + +// Implementations of enable_if_t and other metafunctions for older systems. +template +using enable_if_t = typename std::enable_if::type; +template +using conditional_t = typename std::conditional::type; +template using bool_constant = std::integral_constant; +template +using remove_reference_t = typename std::remove_reference::type; +template +using remove_const_t = typename std::remove_const::type; +template +using remove_cvref_t = typename std::remove_cv>::type; +template struct type_identity { + using type = T; +}; +template using type_identity_t = typename type_identity::type; +template +using make_unsigned_t = typename std::make_unsigned::type; +template +using underlying_t = typename std::underlying_type::type; + +#if FMT_GCC_VERSION && FMT_GCC_VERSION < 500 +// A workaround for gcc 4.8 to make void_t work in a SFINAE context. +template struct void_t_impl { + using type = void; +}; +template using void_t = typename void_t_impl::type; +#else +template using void_t = void; +#endif + +struct monostate { + constexpr monostate() {} +}; + +// An enable_if helper to be used in template parameters which results in much +// shorter symbols: https://godbolt.org/z/sWw4vP. Extra parentheses are needed +// to workaround a bug in MSVC 2019 (see #1140 and #1186). +#ifdef FMT_DOC +# define FMT_ENABLE_IF(...) +#else +# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0 +#endif + +// This is defined in base.h instead of format.h to avoid injecting in std. +// It is a template to avoid undesirable implicit conversions to std::byte. +#ifdef __cpp_lib_byte +template ::value)> +inline auto format_as(T b) -> unsigned char { + return static_cast(b); +} +#endif + +namespace detail { +// Suppresses "unused variable" warnings with the method described in +// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/. +// (void)var does not work on many Intel compilers. +template FMT_CONSTEXPR void ignore_unused(const T&...) {} + +constexpr auto is_constant_evaluated(bool default_value = false) noexcept + -> bool { +// Workaround for incompatibility between libstdc++ consteval-based +// std::is_constant_evaluated() implementation and clang-14: +// https://github.com/fmtlib/fmt/issues/3247. +#if FMT_CPLUSPLUS >= 202002L && FMT_GLIBCXX_RELEASE >= 12 && \ + (FMT_CLANG_VERSION >= 1400 && FMT_CLANG_VERSION < 1500) + ignore_unused(default_value); + return __builtin_is_constant_evaluated(); +#elif defined(__cpp_lib_is_constant_evaluated) + ignore_unused(default_value); + return std::is_constant_evaluated(); +#else + return default_value; +#endif +} + +// Suppresses "conditional expression is constant" warnings. +template constexpr auto const_check(T value) -> T { return value; } + +FMT_NORETURN FMT_API void assert_fail(const char* file, int line, + const char* message); + +#if defined(FMT_ASSERT) +// Use the provided definition. +#elif defined(NDEBUG) +// FMT_ASSERT is not empty to avoid -Wempty-body. +# define FMT_ASSERT(condition, message) \ + fmt::detail::ignore_unused((condition), (message)) +#else +# define FMT_ASSERT(condition, message) \ + ((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \ + ? (void)0 \ + : fmt::detail::assert_fail(__FILE__, __LINE__, (message))) +#endif + +#ifdef FMT_USE_INT128 +// Do nothing. +#elif defined(__SIZEOF_INT128__) && !defined(__NVCC__) && \ + !(FMT_CLANG_VERSION && FMT_MSC_VERSION) +# define FMT_USE_INT128 1 +using int128_opt = __int128_t; // An optional native 128-bit integer. +using uint128_opt = __uint128_t; +template inline auto convert_for_visit(T value) -> T { + return value; +} +#else +# define FMT_USE_INT128 0 +#endif +#if !FMT_USE_INT128 +enum class int128_opt {}; +enum class uint128_opt {}; +// Reduce template instantiations. +template auto convert_for_visit(T) -> monostate { return {}; } +#endif + +// Casts a nonnegative integer to unsigned. +template +FMT_CONSTEXPR auto to_unsigned(Int value) -> make_unsigned_t { + FMT_ASSERT(std::is_unsigned::value || value >= 0, "negative value"); + return static_cast>(value); +} + +// A heuristic to detect std::string and std::[experimental::]string_view. +// It is mainly used to avoid dependency on <[experimental/]string_view>. +template +struct is_std_string_like : std::false_type {}; +template +struct is_std_string_like().find_first_of( + typename T::value_type(), 0))>> + : std::is_convertible().data()), + const typename T::value_type*> {}; + +// Returns true iff the literal encoding is UTF-8. +constexpr auto is_utf8_enabled() -> bool { + // Avoid an MSVC sign extension bug: https://github.com/fmtlib/fmt/pull/2297. + using uchar = unsigned char; + return sizeof("\u00A7") == 3 && uchar("\u00A7"[0]) == 0xC2 && + uchar("\u00A7"[1]) == 0xA7; +} +constexpr auto use_utf8() -> bool { + return !FMT_MSC_VERSION || is_utf8_enabled(); +} + +static_assert(!FMT_UNICODE || use_utf8(), + "Unicode support requires compiling with /utf-8"); + +template FMT_CONSTEXPR auto length(const Char* s) -> size_t { + size_t len = 0; + while (*s++) ++len; + return len; +} + +template +FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n) + -> int { + if (!is_constant_evaluated() && sizeof(Char) == 1) return memcmp(s1, s2, n); + for (; n != 0; ++s1, ++s2, --n) { + if (*s1 < *s2) return -1; + if (*s1 > *s2) return 1; + } + return 0; +} + +namespace adl { +using namespace std; + +template +auto invoke_back_inserter() + -> decltype(back_inserter(std::declval())); +} // namespace adl + +template +struct is_back_insert_iterator : std::false_type {}; + +template +struct is_back_insert_iterator< + It, bool_constant()), + It>::value>> : std::true_type {}; + +// Extracts a reference to the container from *insert_iterator. +template +inline auto get_container(OutputIt it) -> typename OutputIt::container_type& { + struct accessor : OutputIt { + accessor(OutputIt base) : OutputIt(base) {} + using OutputIt::container; + }; + return *accessor(it).container; +} +} // namespace detail + +// Checks whether T is a container with contiguous storage. +template struct is_contiguous : std::false_type {}; + +/** + * An implementation of `std::basic_string_view` for pre-C++17. It provides a + * subset of the API. `fmt::basic_string_view` is used for format strings even + * if `std::basic_string_view` is available to prevent issues when a library is + * compiled with a different `-std` option than the client code (which is not + * recommended). + */ +FMT_EXPORT +template class basic_string_view { + private: + const Char* data_; + size_t size_; + + public: + using value_type = Char; + using iterator = const Char*; + + constexpr basic_string_view() noexcept : data_(nullptr), size_(0) {} + + /// Constructs a string reference object from a C string and a size. + constexpr basic_string_view(const Char* s, size_t count) noexcept + : data_(s), size_(count) {} + + constexpr basic_string_view(std::nullptr_t) = delete; + + /// Constructs a string reference object from a C string. + FMT_CONSTEXPR20 + basic_string_view(const Char* s) + : data_(s), + size_(detail::const_check(std::is_same::value && + !detail::is_constant_evaluated(false)) + ? strlen(reinterpret_cast(s)) + : detail::length(s)) {} + + /// Constructs a string reference from a `std::basic_string` or a + /// `std::basic_string_view` object. + template ::value&& std::is_same< + typename S::value_type, Char>::value)> + FMT_CONSTEXPR basic_string_view(const S& s) noexcept + : data_(s.data()), size_(s.size()) {} + + /// Returns a pointer to the string data. + constexpr auto data() const noexcept -> const Char* { return data_; } + + /// Returns the string size. + constexpr auto size() const noexcept -> size_t { return size_; } + + constexpr auto begin() const noexcept -> iterator { return data_; } + constexpr auto end() const noexcept -> iterator { return data_ + size_; } + + constexpr auto operator[](size_t pos) const noexcept -> const Char& { + return data_[pos]; + } + + FMT_CONSTEXPR void remove_prefix(size_t n) noexcept { + data_ += n; + size_ -= n; + } + + FMT_CONSTEXPR auto starts_with(basic_string_view sv) const noexcept + -> bool { + return size_ >= sv.size_ && detail::compare(data_, sv.data_, sv.size_) == 0; + } + FMT_CONSTEXPR auto starts_with(Char c) const noexcept -> bool { + return size_ >= 1 && *data_ == c; + } + FMT_CONSTEXPR auto starts_with(const Char* s) const -> bool { + return starts_with(basic_string_view(s)); + } + + // Lexicographically compare this string reference to other. + FMT_CONSTEXPR auto compare(basic_string_view other) const -> int { + size_t str_size = size_ < other.size_ ? size_ : other.size_; + int result = detail::compare(data_, other.data_, str_size); + if (result == 0) + result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1); + return result; + } + + FMT_CONSTEXPR friend auto operator==(basic_string_view lhs, + basic_string_view rhs) -> bool { + return lhs.compare(rhs) == 0; + } + friend auto operator!=(basic_string_view lhs, basic_string_view rhs) -> bool { + return lhs.compare(rhs) != 0; + } + friend auto operator<(basic_string_view lhs, basic_string_view rhs) -> bool { + return lhs.compare(rhs) < 0; + } + friend auto operator<=(basic_string_view lhs, basic_string_view rhs) -> bool { + return lhs.compare(rhs) <= 0; + } + friend auto operator>(basic_string_view lhs, basic_string_view rhs) -> bool { + return lhs.compare(rhs) > 0; + } + friend auto operator>=(basic_string_view lhs, basic_string_view rhs) -> bool { + return lhs.compare(rhs) >= 0; + } +}; + +FMT_EXPORT +using string_view = basic_string_view; + +/// Specifies if `T` is a character type. Can be specialized by users. +FMT_EXPORT +template struct is_char : std::false_type {}; +template <> struct is_char : std::true_type {}; + +namespace detail { + +// Constructs fmt::basic_string_view from types implicitly convertible +// to it, deducing Char. Explicitly convertible types such as the ones returned +// from FMT_STRING are intentionally excluded. +template ::value)> +constexpr auto to_string_view(const Char* s) -> basic_string_view { + return s; +} +template ::value)> +constexpr auto to_string_view(const T& s) + -> basic_string_view { + return s; +} +template +constexpr auto to_string_view(basic_string_view s) + -> basic_string_view { + return s; +} + +template +struct has_to_string_view : std::false_type {}; +// detail:: is intentional since to_string_view is not an extension point. +template +struct has_to_string_view< + T, void_t()))>> + : std::true_type {}; + +template struct string_literal { + static constexpr Char value[sizeof...(C)] = {C...}; + constexpr operator basic_string_view() const { + return {value, sizeof...(C)}; + } +}; +#if FMT_CPLUSPLUS < 201703L +template +constexpr Char string_literal::value[sizeof...(C)]; +#endif + +enum class type { + none_type, + // Integer types should go first, + int_type, + uint_type, + long_long_type, + ulong_long_type, + int128_type, + uint128_type, + bool_type, + char_type, + last_integer_type = char_type, + // followed by floating-point types. + float_type, + double_type, + long_double_type, + last_numeric_type = long_double_type, + cstring_type, + string_type, + pointer_type, + custom_type +}; + +// Maps core type T to the corresponding type enum constant. +template +struct type_constant : std::integral_constant {}; + +#define FMT_TYPE_CONSTANT(Type, constant) \ + template \ + struct type_constant \ + : std::integral_constant {} + +FMT_TYPE_CONSTANT(int, int_type); +FMT_TYPE_CONSTANT(unsigned, uint_type); +FMT_TYPE_CONSTANT(long long, long_long_type); +FMT_TYPE_CONSTANT(unsigned long long, ulong_long_type); +FMT_TYPE_CONSTANT(int128_opt, int128_type); +FMT_TYPE_CONSTANT(uint128_opt, uint128_type); +FMT_TYPE_CONSTANT(bool, bool_type); +FMT_TYPE_CONSTANT(Char, char_type); +FMT_TYPE_CONSTANT(float, float_type); +FMT_TYPE_CONSTANT(double, double_type); +FMT_TYPE_CONSTANT(long double, long_double_type); +FMT_TYPE_CONSTANT(const Char*, cstring_type); +FMT_TYPE_CONSTANT(basic_string_view, string_type); +FMT_TYPE_CONSTANT(const void*, pointer_type); + +constexpr auto is_integral_type(type t) -> bool { + return t > type::none_type && t <= type::last_integer_type; +} +constexpr auto is_arithmetic_type(type t) -> bool { + return t > type::none_type && t <= type::last_numeric_type; +} + +constexpr auto set(type rhs) -> int { return 1 << static_cast(rhs); } +constexpr auto in(type t, int set) -> bool { + return ((set >> static_cast(t)) & 1) != 0; +} + +// Bitsets of types. +enum { + sint_set = + set(type::int_type) | set(type::long_long_type) | set(type::int128_type), + uint_set = set(type::uint_type) | set(type::ulong_long_type) | + set(type::uint128_type), + bool_set = set(type::bool_type), + char_set = set(type::char_type), + float_set = set(type::float_type) | set(type::double_type) | + set(type::long_double_type), + string_set = set(type::string_type), + cstring_set = set(type::cstring_type), + pointer_set = set(type::pointer_type) +}; +} // namespace detail + +/// Reports a format error at compile time or, via a `format_error` exception, +/// at runtime. +// This function is intentionally not constexpr to give a compile-time error. +FMT_NORETURN FMT_API void report_error(const char* message); + +FMT_DEPRECATED FMT_NORETURN inline void throw_format_error( + const char* message) { + report_error(message); +} + +/// String's character (code unit) type. +template ()))> +using char_t = typename V::value_type; + +/** + * Parsing context consisting of a format string range being parsed and an + * argument counter for automatic indexing. + * You can use the `format_parse_context` type alias for `char` instead. + */ +FMT_EXPORT +template class basic_format_parse_context { + private: + basic_string_view format_str_; + int next_arg_id_; + + FMT_CONSTEXPR void do_check_arg_id(int id); + + public: + using char_type = Char; + using iterator = const Char*; + + explicit constexpr basic_format_parse_context( + basic_string_view format_str, int next_arg_id = 0) + : format_str_(format_str), next_arg_id_(next_arg_id) {} + + /// Returns an iterator to the beginning of the format string range being + /// parsed. + constexpr auto begin() const noexcept -> iterator { + return format_str_.begin(); + } + + /// Returns an iterator past the end of the format string range being parsed. + constexpr auto end() const noexcept -> iterator { return format_str_.end(); } + + /// Advances the begin iterator to `it`. + FMT_CONSTEXPR void advance_to(iterator it) { + format_str_.remove_prefix(detail::to_unsigned(it - begin())); + } + + /// Reports an error if using the manual argument indexing; otherwise returns + /// the next argument index and switches to the automatic indexing. + FMT_CONSTEXPR auto next_arg_id() -> int { + if (next_arg_id_ < 0) { + report_error("cannot switch from manual to automatic argument indexing"); + return 0; + } + int id = next_arg_id_++; + do_check_arg_id(id); + return id; + } + + /// Reports an error if using the automatic argument indexing; otherwise + /// switches to the manual indexing. + FMT_CONSTEXPR void check_arg_id(int id) { + if (next_arg_id_ > 0) { + report_error("cannot switch from automatic to manual argument indexing"); + return; + } + next_arg_id_ = -1; + do_check_arg_id(id); + } + FMT_CONSTEXPR void check_arg_id(basic_string_view) { + next_arg_id_ = -1; + } + FMT_CONSTEXPR void check_dynamic_spec(int arg_id); +}; + +FMT_EXPORT +using format_parse_context = basic_format_parse_context; + +namespace detail { +// A parse context with extra data used only in compile-time checks. +template +class compile_parse_context : public basic_format_parse_context { + private: + int num_args_; + const type* types_; + using base = basic_format_parse_context; + + public: + explicit FMT_CONSTEXPR compile_parse_context( + basic_string_view format_str, int num_args, const type* types, + int next_arg_id = 0) + : base(format_str, next_arg_id), num_args_(num_args), types_(types) {} + + constexpr auto num_args() const -> int { return num_args_; } + constexpr auto arg_type(int id) const -> type { return types_[id]; } + + FMT_CONSTEXPR auto next_arg_id() -> int { + int id = base::next_arg_id(); + if (id >= num_args_) report_error("argument not found"); + return id; + } + + FMT_CONSTEXPR void check_arg_id(int id) { + base::check_arg_id(id); + if (id >= num_args_) report_error("argument not found"); + } + using base::check_arg_id; + + FMT_CONSTEXPR void check_dynamic_spec(int arg_id) { + detail::ignore_unused(arg_id); + if (arg_id < num_args_ && types_ && !is_integral_type(types_[arg_id])) + report_error("width/precision is not integer"); + } +}; + +/// A contiguous memory buffer with an optional growing ability. It is an +/// internal class and shouldn't be used directly, only via `memory_buffer`. +template class buffer { + private: + T* ptr_; + size_t size_; + size_t capacity_; + + using grow_fun = void (*)(buffer& buf, size_t capacity); + grow_fun grow_; + + protected: + // Don't initialize ptr_ since it is not accessed to save a few cycles. + FMT_MSC_WARNING(suppress : 26495) + FMT_CONSTEXPR20 buffer(grow_fun grow, size_t sz) noexcept + : size_(sz), capacity_(sz), grow_(grow) {} + + constexpr buffer(grow_fun grow, T* p = nullptr, size_t sz = 0, + size_t cap = 0) noexcept + : ptr_(p), size_(sz), capacity_(cap), grow_(grow) {} + + FMT_CONSTEXPR20 ~buffer() = default; + buffer(buffer&&) = default; + + /// Sets the buffer data and capacity. + FMT_CONSTEXPR void set(T* buf_data, size_t buf_capacity) noexcept { + ptr_ = buf_data; + capacity_ = buf_capacity; + } + + public: + using value_type = T; + using const_reference = const T&; + + buffer(const buffer&) = delete; + void operator=(const buffer&) = delete; + + auto begin() noexcept -> T* { return ptr_; } + auto end() noexcept -> T* { return ptr_ + size_; } + + auto begin() const noexcept -> const T* { return ptr_; } + auto end() const noexcept -> const T* { return ptr_ + size_; } + + /// Returns the size of this buffer. + constexpr auto size() const noexcept -> size_t { return size_; } + + /// Returns the capacity of this buffer. + constexpr auto capacity() const noexcept -> size_t { return capacity_; } + + /// Returns a pointer to the buffer data (not null-terminated). + FMT_CONSTEXPR auto data() noexcept -> T* { return ptr_; } + FMT_CONSTEXPR auto data() const noexcept -> const T* { return ptr_; } + + /// Clears this buffer. + void clear() { size_ = 0; } + + // Tries resizing the buffer to contain `count` elements. If T is a POD type + // the new elements may not be initialized. + FMT_CONSTEXPR void try_resize(size_t count) { + try_reserve(count); + size_ = count <= capacity_ ? count : capacity_; + } + + // Tries increasing the buffer capacity to `new_capacity`. It can increase the + // capacity by a smaller amount than requested but guarantees there is space + // for at least one additional element either by increasing the capacity or by + // flushing the buffer if it is full. + FMT_CONSTEXPR void try_reserve(size_t new_capacity) { + if (new_capacity > capacity_) grow_(*this, new_capacity); + } + + FMT_CONSTEXPR void push_back(const T& value) { + try_reserve(size_ + 1); + ptr_[size_++] = value; + } + + /// Appends data to the end of the buffer. + template void append(const U* begin, const U* end) { + while (begin != end) { + auto count = to_unsigned(end - begin); + try_reserve(size_ + count); + auto free_cap = capacity_ - size_; + if (free_cap < count) count = free_cap; + // A loop is faster than memcpy on small sizes. + T* out = ptr_ + size_; + for (size_t i = 0; i < count; ++i) out[i] = begin[i]; + size_ += count; + begin += count; + } + } + + template FMT_CONSTEXPR auto operator[](Idx index) -> T& { + return ptr_[index]; + } + template + FMT_CONSTEXPR auto operator[](Idx index) const -> const T& { + return ptr_[index]; + } +}; + +struct buffer_traits { + explicit buffer_traits(size_t) {} + auto count() const -> size_t { return 0; } + auto limit(size_t size) -> size_t { return size; } +}; + +class fixed_buffer_traits { + private: + size_t count_ = 0; + size_t limit_; + + public: + explicit fixed_buffer_traits(size_t limit) : limit_(limit) {} + auto count() const -> size_t { return count_; } + auto limit(size_t size) -> size_t { + size_t n = limit_ > count_ ? limit_ - count_ : 0; + count_ += size; + return size < n ? size : n; + } +}; + +// A buffer that writes to an output iterator when flushed. +template +class iterator_buffer : public Traits, public buffer { + private: + OutputIt out_; + enum { buffer_size = 256 }; + T data_[buffer_size]; + + static FMT_CONSTEXPR void grow(buffer& buf, size_t) { + if (buf.size() == buffer_size) static_cast(buf).flush(); + } + + void flush() { + auto size = this->size(); + this->clear(); + const T* begin = data_; + const T* end = begin + this->limit(size); + while (begin != end) *out_++ = *begin++; + } + + public: + explicit iterator_buffer(OutputIt out, size_t n = buffer_size) + : Traits(n), buffer(grow, data_, 0, buffer_size), out_(out) {} + iterator_buffer(iterator_buffer&& other) noexcept + : Traits(other), + buffer(grow, data_, 0, buffer_size), + out_(other.out_) {} + ~iterator_buffer() { + // Don't crash if flush fails during unwinding. + FMT_TRY { flush(); } + FMT_CATCH(...) {} + } + + auto out() -> OutputIt { + flush(); + return out_; + } + auto count() const -> size_t { return Traits::count() + this->size(); } +}; + +template +class iterator_buffer : public fixed_buffer_traits, + public buffer { + private: + T* out_; + enum { buffer_size = 256 }; + T data_[buffer_size]; + + static FMT_CONSTEXPR void grow(buffer& buf, size_t) { + if (buf.size() == buf.capacity()) + static_cast(buf).flush(); + } + + void flush() { + size_t n = this->limit(this->size()); + if (this->data() == out_) { + out_ += n; + this->set(data_, buffer_size); + } + this->clear(); + } + + public: + explicit iterator_buffer(T* out, size_t n = buffer_size) + : fixed_buffer_traits(n), buffer(grow, out, 0, n), out_(out) {} + iterator_buffer(iterator_buffer&& other) noexcept + : fixed_buffer_traits(other), + buffer(static_cast(other)), + out_(other.out_) { + if (this->data() != out_) { + this->set(data_, buffer_size); + this->clear(); + } + } + ~iterator_buffer() { flush(); } + + auto out() -> T* { + flush(); + return out_; + } + auto count() const -> size_t { + return fixed_buffer_traits::count() + this->size(); + } +}; + +template class iterator_buffer : public buffer { + public: + explicit iterator_buffer(T* out, size_t = 0) + : buffer([](buffer&, size_t) {}, out, 0, ~size_t()) {} + + auto out() -> T* { return &*this->end(); } +}; + +// A buffer that writes to a container with the contiguous storage. +template +class iterator_buffer< + OutputIt, + enable_if_t::value && + is_contiguous::value, + typename OutputIt::container_type::value_type>> + : public buffer { + private: + using container_type = typename OutputIt::container_type; + using value_type = typename container_type::value_type; + container_type& container_; + + static FMT_CONSTEXPR void grow(buffer& buf, size_t capacity) { + auto& self = static_cast(buf); + self.container_.resize(capacity); + self.set(&self.container_[0], capacity); + } + + public: + explicit iterator_buffer(container_type& c) + : buffer(grow, c.size()), container_(c) {} + explicit iterator_buffer(OutputIt out, size_t = 0) + : iterator_buffer(get_container(out)) {} + + auto out() -> OutputIt { return back_inserter(container_); } +}; + +// A buffer that counts the number of code units written discarding the output. +template class counting_buffer : public buffer { + private: + enum { buffer_size = 256 }; + T data_[buffer_size]; + size_t count_ = 0; + + static FMT_CONSTEXPR void grow(buffer& buf, size_t) { + if (buf.size() != buffer_size) return; + static_cast(buf).count_ += buf.size(); + buf.clear(); + } + + public: + counting_buffer() : buffer(grow, data_, 0, buffer_size) {} + + auto count() -> size_t { return count_ + this->size(); } +}; +} // namespace detail + +template +FMT_CONSTEXPR void basic_format_parse_context::do_check_arg_id(int id) { + // Argument id is only checked at compile-time during parsing because + // formatting has its own validation. + if (detail::is_constant_evaluated() && + (!FMT_GCC_VERSION || FMT_GCC_VERSION >= 1200)) { + using context = detail::compile_parse_context; + if (id >= static_cast(this)->num_args()) + report_error("argument not found"); + } +} + +template +FMT_CONSTEXPR void basic_format_parse_context::check_dynamic_spec( + int arg_id) { + if (detail::is_constant_evaluated() && + (!FMT_GCC_VERSION || FMT_GCC_VERSION >= 1200)) { + using context = detail::compile_parse_context; + static_cast(this)->check_dynamic_spec(arg_id); + } +} + +FMT_EXPORT template class basic_format_arg; +FMT_EXPORT template class basic_format_args; +FMT_EXPORT template class dynamic_format_arg_store; + +// A formatter for objects of type T. +FMT_EXPORT +template +struct formatter { + // A deleted default constructor indicates a disabled formatter. + formatter() = delete; +}; + +// Specifies if T has an enabled formatter specialization. A type can be +// formattable even if it doesn't have a formatter e.g. via a conversion. +template +using has_formatter = + std::is_constructible>; + +// An output iterator that appends to a buffer. It is used instead of +// back_insert_iterator to reduce symbol sizes and avoid dependency. +template class basic_appender { + private: + detail::buffer* buffer_; + + friend auto get_container(basic_appender app) -> detail::buffer& { + return *app.buffer_; + } + + public: + using iterator_category = int; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = T*; + using reference = T&; + using container_type = detail::buffer; + FMT_UNCHECKED_ITERATOR(basic_appender); + + FMT_CONSTEXPR basic_appender(detail::buffer& buf) : buffer_(&buf) {} + + auto operator=(T c) -> basic_appender& { + buffer_->push_back(c); + return *this; + } + auto operator*() -> basic_appender& { return *this; } + auto operator++() -> basic_appender& { return *this; } + auto operator++(int) -> basic_appender { return *this; } +}; + +using appender = basic_appender; + +namespace detail { +template +struct is_back_insert_iterator> : std::true_type {}; + +template +struct locking : std::true_type {}; +template +struct locking>::nonlocking>> + : std::false_type {}; + +template FMT_CONSTEXPR inline auto is_locking() -> bool { + return locking::value; +} +template +FMT_CONSTEXPR inline auto is_locking() -> bool { + return locking::value || is_locking(); +} + +// An optimized version of std::copy with the output value type (T). +template ::value)> +auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt { + get_container(out).append(begin, end); + return out; +} + +template ::value)> +FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt { + while (begin != end) *out++ = static_cast(*begin++); + return out; +} + +template +FMT_CONSTEXPR auto copy(basic_string_view s, OutputIt out) -> OutputIt { + return copy(s.begin(), s.end(), out); +} + +template +constexpr auto has_const_formatter_impl(T*) + -> decltype(typename Context::template formatter_type().format( + std::declval(), std::declval()), + true) { + return true; +} +template +constexpr auto has_const_formatter_impl(...) -> bool { + return false; +} +template +constexpr auto has_const_formatter() -> bool { + return has_const_formatter_impl(static_cast(nullptr)); +} + +template +struct is_buffer_appender : std::false_type {}; +template +struct is_buffer_appender< + It, bool_constant< + is_back_insert_iterator::value && + std::is_base_of, + typename It::container_type>::value>> + : std::true_type {}; + +// Maps an output iterator to a buffer. +template ::value)> +auto get_buffer(OutputIt out) -> iterator_buffer { + return iterator_buffer(out); +} +template ::value)> +auto get_buffer(OutputIt out) -> buffer& { + return get_container(out); +} + +template +auto get_iterator(Buf& buf, OutputIt) -> decltype(buf.out()) { + return buf.out(); +} +template +auto get_iterator(buffer&, OutputIt out) -> OutputIt { + return out; +} + +struct view {}; + +template struct named_arg : view { + const Char* name; + const T& value; + named_arg(const Char* n, const T& v) : name(n), value(v) {} +}; + +template struct named_arg_info { + const Char* name; + int id; +}; + +template struct is_named_arg : std::false_type {}; +template struct is_statically_named_arg : std::false_type {}; + +template +struct is_named_arg> : std::true_type {}; + +template constexpr auto count() -> size_t { return B ? 1 : 0; } +template constexpr auto count() -> size_t { + return (B1 ? 1 : 0) + count(); +} + +template constexpr auto count_named_args() -> size_t { + return count::value...>(); +} + +template +constexpr auto count_statically_named_args() -> size_t { + return count::value...>(); +} + +struct unformattable {}; +struct unformattable_char : unformattable {}; +struct unformattable_pointer : unformattable {}; + +template struct string_value { + const Char* data; + size_t size; +}; + +template struct named_arg_value { + const named_arg_info* data; + size_t size; +}; + +template struct custom_value { + using parse_context = typename Context::parse_context_type; + void* value; + void (*format)(void* arg, parse_context& parse_ctx, Context& ctx); +}; + +// A formatting argument value. +template class value { + public: + using char_type = typename Context::char_type; + + union { + monostate no_value; + int int_value; + unsigned uint_value; + long long long_long_value; + unsigned long long ulong_long_value; + int128_opt int128_value; + uint128_opt uint128_value; + bool bool_value; + char_type char_value; + float float_value; + double double_value; + long double long_double_value; + const void* pointer; + string_value string; + custom_value custom; + named_arg_value named_args; + }; + + constexpr FMT_ALWAYS_INLINE value() : no_value() {} + constexpr FMT_ALWAYS_INLINE value(int val) : int_value(val) {} + constexpr FMT_ALWAYS_INLINE value(unsigned val) : uint_value(val) {} + constexpr FMT_ALWAYS_INLINE value(long long val) : long_long_value(val) {} + constexpr FMT_ALWAYS_INLINE value(unsigned long long val) + : ulong_long_value(val) {} + FMT_ALWAYS_INLINE value(int128_opt val) : int128_value(val) {} + FMT_ALWAYS_INLINE value(uint128_opt val) : uint128_value(val) {} + constexpr FMT_ALWAYS_INLINE value(float val) : float_value(val) {} + constexpr FMT_ALWAYS_INLINE value(double val) : double_value(val) {} + FMT_ALWAYS_INLINE value(long double val) : long_double_value(val) {} + constexpr FMT_ALWAYS_INLINE value(bool val) : bool_value(val) {} + constexpr FMT_ALWAYS_INLINE value(char_type val) : char_value(val) {} + FMT_CONSTEXPR FMT_ALWAYS_INLINE value(const char_type* val) { + string.data = val; + if (is_constant_evaluated()) string.size = {}; + } + FMT_CONSTEXPR FMT_ALWAYS_INLINE value(basic_string_view val) { + string.data = val.data(); + string.size = val.size(); + } + FMT_ALWAYS_INLINE value(const void* val) : pointer(val) {} + FMT_ALWAYS_INLINE value(const named_arg_info* args, size_t size) + : named_args{args, size} {} + + template FMT_CONSTEXPR20 FMT_ALWAYS_INLINE value(T& val) { + using value_type = remove_const_t; + // T may overload operator& e.g. std::vector::reference in libc++. +#if defined(__cpp_if_constexpr) + if constexpr (std::is_same::value) + custom.value = const_cast(&val); +#endif + if (!is_constant_evaluated()) + custom.value = const_cast(&reinterpret_cast(val)); + // Get the formatter type through the context to allow different contexts + // have different extension points, e.g. `formatter` for `format` and + // `printf_formatter` for `printf`. + custom.format = format_custom_arg< + value_type, typename Context::template formatter_type>; + } + value(unformattable); + value(unformattable_char); + value(unformattable_pointer); + + private: + // Formats an argument of a custom type, such as a user-defined class. + template + static void format_custom_arg(void* arg, + typename Context::parse_context_type& parse_ctx, + Context& ctx) { + auto f = Formatter(); + parse_ctx.advance_to(f.parse(parse_ctx)); + using qualified_type = + conditional_t(), const T, T>; + // format must be const for compatibility with std::format and compilation. + const auto& cf = f; + ctx.advance_to(cf.format(*static_cast(arg), ctx)); + } +}; + +// To minimize the number of types we need to deal with, long is translated +// either to int or to long long depending on its size. +enum { long_short = sizeof(long) == sizeof(int) }; +using long_type = conditional_t; +using ulong_type = conditional_t; + +template struct format_as_result { + template ::value || std::is_class::value)> + static auto map(U*) -> remove_cvref_t()))>; + static auto map(...) -> void; + + using type = decltype(map(static_cast(nullptr))); +}; +template using format_as_t = typename format_as_result::type; + +template +struct has_format_as + : bool_constant, void>::value> {}; + +#define FMT_MAP_API FMT_CONSTEXPR FMT_ALWAYS_INLINE + +// Maps formatting arguments to core types. +// arg_mapper reports errors by returning unformattable instead of using +// static_assert because it's used in the is_formattable trait. +template struct arg_mapper { + using char_type = typename Context::char_type; + + FMT_MAP_API auto map(signed char val) -> int { return val; } + FMT_MAP_API auto map(unsigned char val) -> unsigned { return val; } + FMT_MAP_API auto map(short val) -> int { return val; } + FMT_MAP_API auto map(unsigned short val) -> unsigned { return val; } + FMT_MAP_API auto map(int val) -> int { return val; } + FMT_MAP_API auto map(unsigned val) -> unsigned { return val; } + FMT_MAP_API auto map(long val) -> long_type { return val; } + FMT_MAP_API auto map(unsigned long val) -> ulong_type { return val; } + FMT_MAP_API auto map(long long val) -> long long { return val; } + FMT_MAP_API auto map(unsigned long long val) -> unsigned long long { + return val; + } + FMT_MAP_API auto map(int128_opt val) -> int128_opt { return val; } + FMT_MAP_API auto map(uint128_opt val) -> uint128_opt { return val; } + FMT_MAP_API auto map(bool val) -> bool { return val; } + + template ::value || + std::is_same::value)> + FMT_MAP_API auto map(T val) -> char_type { + return val; + } + template ::value || +#ifdef __cpp_char8_t + std::is_same::value || +#endif + std::is_same::value || + std::is_same::value) && + !std::is_same::value, + int> = 0> + FMT_MAP_API auto map(T) -> unformattable_char { + return {}; + } + + FMT_MAP_API auto map(float val) -> float { return val; } + FMT_MAP_API auto map(double val) -> double { return val; } + FMT_MAP_API auto map(long double val) -> long double { return val; } + + FMT_MAP_API auto map(char_type* val) -> const char_type* { return val; } + FMT_MAP_API auto map(const char_type* val) -> const char_type* { return val; } + template , + FMT_ENABLE_IF(std::is_same::value && + !std::is_pointer::value)> + FMT_MAP_API auto map(const T& val) -> basic_string_view { + return to_string_view(val); + } + template , + FMT_ENABLE_IF(!std::is_same::value && + !std::is_pointer::value)> + FMT_MAP_API auto map(const T&) -> unformattable_char { + return {}; + } + + FMT_MAP_API auto map(void* val) -> const void* { return val; } + FMT_MAP_API auto map(const void* val) -> const void* { return val; } + FMT_MAP_API auto map(volatile void* val) -> const void* { + return const_cast(val); + } + FMT_MAP_API auto map(const volatile void* val) -> const void* { + return const_cast(val); + } + FMT_MAP_API auto map(std::nullptr_t val) -> const void* { return val; } + + // Use SFINAE instead of a const T* parameter to avoid a conflict with the + // array overload. + template < + typename T, + FMT_ENABLE_IF( + std::is_pointer::value || std::is_member_pointer::value || + std::is_function::type>::value || + (std::is_array::value && + !std::is_convertible::value))> + FMT_CONSTEXPR auto map(const T&) -> unformattable_pointer { + return {}; + } + + template ::value)> + FMT_MAP_API auto map(const T (&values)[N]) -> const T (&)[N] { + return values; + } + + // Only map owning types because mapping views can be unsafe. + template , + FMT_ENABLE_IF(std::is_arithmetic::value)> + FMT_MAP_API auto map(const T& val) -> decltype(FMT_DECLTYPE_THIS map(U())) { + return map(format_as(val)); + } + + template > + struct formattable : bool_constant() || + (has_formatter::value && + !std::is_const::value)> {}; + + template ::value)> + FMT_MAP_API auto do_map(T& val) -> T& { + return val; + } + template ::value)> + FMT_MAP_API auto do_map(T&) -> unformattable { + return {}; + } + + // is_fundamental is used to allow formatters for extended FP types. + template , + FMT_ENABLE_IF( + (std::is_class::value || std::is_enum::value || + std::is_union::value || std::is_fundamental::value) && + !has_to_string_view::value && !is_char::value && + !is_named_arg::value && !std::is_integral::value && + !std::is_arithmetic>::value)> + FMT_MAP_API auto map(T& val) -> decltype(FMT_DECLTYPE_THIS do_map(val)) { + return do_map(val); + } + + template ::value)> + FMT_MAP_API auto map(const T& named_arg) + -> decltype(FMT_DECLTYPE_THIS map(named_arg.value)) { + return map(named_arg.value); + } + + auto map(...) -> unformattable { return {}; } +}; + +// A type constant after applying arg_mapper. +template +using mapped_type_constant = + type_constant().map(std::declval())), + typename Context::char_type>; + +enum { packed_arg_bits = 4 }; +// Maximum number of arguments with packed types. +enum { max_packed_args = 62 / packed_arg_bits }; +enum : unsigned long long { is_unpacked_bit = 1ULL << 63 }; +enum : unsigned long long { has_named_args_bit = 1ULL << 62 }; + +template +struct is_output_iterator : std::false_type {}; + +template <> struct is_output_iterator : std::true_type {}; + +template +struct is_output_iterator< + It, T, void_t()++ = std::declval())>> + : std::true_type {}; + +// A type-erased reference to an std::locale to avoid a heavy include. +class locale_ref { + private: + const void* locale_; // A type-erased pointer to std::locale. + + public: + constexpr locale_ref() : locale_(nullptr) {} + template explicit locale_ref(const Locale& loc); + + explicit operator bool() const noexcept { return locale_ != nullptr; } + + template auto get() const -> Locale; +}; + +template constexpr auto encode_types() -> unsigned long long { + return 0; +} + +template +constexpr auto encode_types() -> unsigned long long { + return static_cast(mapped_type_constant::value) | + (encode_types() << packed_arg_bits); +} + +template +constexpr unsigned long long make_descriptor() { + return NUM_ARGS <= max_packed_args ? encode_types() + : is_unpacked_bit | NUM_ARGS; +} + +// This type is intentionally undefined, only used for errors. +template +#if FMT_CLANG_VERSION && FMT_CLANG_VERSION <= 1500 +// https://github.com/fmtlib/fmt/issues/3796 +struct type_is_unformattable_for { +}; +#else +struct type_is_unformattable_for; +#endif + +template +FMT_CONSTEXPR auto make_arg(T& val) -> value { + using arg_type = remove_cvref_t().map(val))>; + + // Use enum instead of constexpr because the latter may generate code. + enum { + formattable_char = !std::is_same::value + }; + static_assert(formattable_char, "Mixing character types is disallowed."); + + // Formatting of arbitrary pointers is disallowed. If you want to format a + // pointer cast it to `void*` or `const void*`. In particular, this forbids + // formatting of `[const] volatile char*` printed as bool by iostreams. + enum { + formattable_pointer = !std::is_same::value + }; + static_assert(formattable_pointer, + "Formatting of non-void pointers is disallowed."); + + enum { formattable = !std::is_same::value }; +#if defined(__cpp_if_constexpr) + if constexpr (!formattable) + type_is_unformattable_for _; +#endif + static_assert( + formattable, + "Cannot format an argument. To make type T formattable provide a " + "formatter specialization: https://fmt.dev/latest/api.html#udt"); + return {arg_mapper().map(val)}; +} + +template +FMT_CONSTEXPR auto make_arg(T& val) -> basic_format_arg { + auto arg = basic_format_arg(); + arg.type_ = mapped_type_constant::value; + arg.value_ = make_arg(val); + return arg; +} + +template +FMT_CONSTEXPR inline auto make_arg(T& val) -> basic_format_arg { + return make_arg(val); +} + +template +using arg_t = conditional_t, + basic_format_arg>; + +template ::value)> +void init_named_arg(named_arg_info*, int& arg_index, int&, const T&) { + ++arg_index; +} +template ::value)> +void init_named_arg(named_arg_info* named_args, int& arg_index, + int& named_arg_index, const T& arg) { + named_args[named_arg_index++] = {arg.name, arg_index++}; +} + +// An array of references to arguments. It can be implicitly converted to +// `fmt::basic_format_args` for passing into type-erased formatting functions +// such as `fmt::vformat`. +template +struct format_arg_store { + // args_[0].named_args points to named_args to avoid bloating format_args. + // +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning. + static constexpr size_t ARGS_ARR_SIZE = 1 + (NUM_ARGS != 0 ? NUM_ARGS : +1); + + arg_t args[ARGS_ARR_SIZE]; + named_arg_info named_args[NUM_NAMED_ARGS]; + + template + FMT_MAP_API format_arg_store(T&... values) + : args{{named_args, NUM_NAMED_ARGS}, + make_arg(values)...} { + using dummy = int[]; + int arg_index = 0, named_arg_index = 0; + (void)dummy{ + 0, + (init_named_arg(named_args, arg_index, named_arg_index, values), 0)...}; + } + + format_arg_store(format_arg_store&& rhs) { + args[0] = {named_args, NUM_NAMED_ARGS}; + for (size_t i = 1; i < ARGS_ARR_SIZE; ++i) args[i] = rhs.args[i]; + for (size_t i = 0; i < NUM_NAMED_ARGS; ++i) + named_args[i] = rhs.named_args[i]; + } + + format_arg_store(const format_arg_store& rhs) = delete; + format_arg_store& operator=(const format_arg_store& rhs) = delete; + format_arg_store& operator=(format_arg_store&& rhs) = delete; +}; + +// A specialization of format_arg_store without named arguments. +// It is a plain struct to reduce binary size in debug mode. +template +struct format_arg_store { + // +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning. + arg_t args[NUM_ARGS != 0 ? NUM_ARGS : +1]; +}; + +} // namespace detail +FMT_BEGIN_EXPORT + +// A formatting argument. Context is a template parameter for the compiled API +// where output can be unbuffered. +template class basic_format_arg { + private: + detail::value value_; + detail::type type_; + + template + friend FMT_CONSTEXPR auto detail::make_arg(T& value) + -> basic_format_arg; + + friend class basic_format_args; + friend class dynamic_format_arg_store; + + using char_type = typename Context::char_type; + + template + friend struct detail::format_arg_store; + + basic_format_arg(const detail::named_arg_info* args, size_t size) + : value_(args, size) {} + + public: + class handle { + public: + explicit handle(detail::custom_value custom) : custom_(custom) {} + + void format(typename Context::parse_context_type& parse_ctx, + Context& ctx) const { + custom_.format(custom_.value, parse_ctx, ctx); + } + + private: + detail::custom_value custom_; + }; + + constexpr basic_format_arg() : type_(detail::type::none_type) {} + + constexpr explicit operator bool() const noexcept { + return type_ != detail::type::none_type; + } + + auto type() const -> detail::type { return type_; } + + auto is_integral() const -> bool { return detail::is_integral_type(type_); } + auto is_arithmetic() const -> bool { + return detail::is_arithmetic_type(type_); + } + + /** + * Visits an argument dispatching to the appropriate visit method based on + * the argument type. For example, if the argument type is `double` then + * `vis(value)` will be called with the value of type `double`. + */ + template + FMT_CONSTEXPR FMT_INLINE auto visit(Visitor&& vis) const -> decltype(vis(0)) { + switch (type_) { + case detail::type::none_type: + break; + case detail::type::int_type: + return vis(value_.int_value); + case detail::type::uint_type: + return vis(value_.uint_value); + case detail::type::long_long_type: + return vis(value_.long_long_value); + case detail::type::ulong_long_type: + return vis(value_.ulong_long_value); + case detail::type::int128_type: + return vis(detail::convert_for_visit(value_.int128_value)); + case detail::type::uint128_type: + return vis(detail::convert_for_visit(value_.uint128_value)); + case detail::type::bool_type: + return vis(value_.bool_value); + case detail::type::char_type: + return vis(value_.char_value); + case detail::type::float_type: + return vis(value_.float_value); + case detail::type::double_type: + return vis(value_.double_value); + case detail::type::long_double_type: + return vis(value_.long_double_value); + case detail::type::cstring_type: + return vis(value_.string.data); + case detail::type::string_type: + using sv = basic_string_view; + return vis(sv(value_.string.data, value_.string.size)); + case detail::type::pointer_type: + return vis(value_.pointer); + case detail::type::custom_type: + return vis(typename basic_format_arg::handle(value_.custom)); + } + return vis(monostate()); + } + + auto format_custom(const char_type* parse_begin, + typename Context::parse_context_type& parse_ctx, + Context& ctx) -> bool { + if (type_ != detail::type::custom_type) return false; + parse_ctx.advance_to(parse_begin); + value_.custom.format(value_.custom.value, parse_ctx, ctx); + return true; + } +}; + +template +FMT_DEPRECATED FMT_CONSTEXPR auto visit_format_arg( + Visitor&& vis, const basic_format_arg& arg) -> decltype(vis(0)) { + return arg.visit(static_cast(vis)); +} + +/** + * A view of a collection of formatting arguments. To avoid lifetime issues it + * should only be used as a parameter type in type-erased functions such as + * `vformat`: + * + * void vlog(fmt::string_view fmt, fmt::format_args args); // OK + * fmt::format_args args = fmt::make_format_args(); // Dangling reference + */ +template class basic_format_args { + public: + using size_type = int; + using format_arg = basic_format_arg; + + private: + // A descriptor that contains information about formatting arguments. + // If the number of arguments is less or equal to max_packed_args then + // argument types are passed in the descriptor. This reduces binary code size + // per formatting function call. + unsigned long long desc_; + union { + // If is_packed() returns true then argument values are stored in values_; + // otherwise they are stored in args_. This is done to improve cache + // locality and reduce compiled code size since storing larger objects + // may require more code (at least on x86-64) even if the same amount of + // data is actually copied to stack. It saves ~10% on the bloat test. + const detail::value* values_; + const format_arg* args_; + }; + + constexpr auto is_packed() const -> bool { + return (desc_ & detail::is_unpacked_bit) == 0; + } + constexpr auto has_named_args() const -> bool { + return (desc_ & detail::has_named_args_bit) != 0; + } + + FMT_CONSTEXPR auto type(int index) const -> detail::type { + int shift = index * detail::packed_arg_bits; + unsigned int mask = (1 << detail::packed_arg_bits) - 1; + return static_cast((desc_ >> shift) & mask); + } + + public: + constexpr basic_format_args() : desc_(0), args_(nullptr) {} + + /// Constructs a `basic_format_args` object from `format_arg_store`. + template + constexpr FMT_ALWAYS_INLINE basic_format_args( + const detail::format_arg_store& + store) + : desc_(DESC), values_(store.args + (NUM_NAMED_ARGS != 0 ? 1 : 0)) {} + + template detail::max_packed_args)> + constexpr basic_format_args( + const detail::format_arg_store& + store) + : desc_(DESC), args_(store.args + (NUM_NAMED_ARGS != 0 ? 1 : 0)) {} + + /// Constructs a `basic_format_args` object from `dynamic_format_arg_store`. + constexpr basic_format_args(const dynamic_format_arg_store& store) + : desc_(store.get_types()), args_(store.data()) {} + + /// Constructs a `basic_format_args` object from a dynamic list of arguments. + constexpr basic_format_args(const format_arg* args, int count) + : desc_(detail::is_unpacked_bit | detail::to_unsigned(count)), + args_(args) {} + + /// Returns the argument with the specified id. + FMT_CONSTEXPR auto get(int id) const -> format_arg { + format_arg arg; + if (!is_packed()) { + if (id < max_size()) arg = args_[id]; + return arg; + } + if (static_cast(id) >= detail::max_packed_args) return arg; + arg.type_ = type(id); + if (arg.type_ == detail::type::none_type) return arg; + arg.value_ = values_[id]; + return arg; + } + + template + auto get(basic_string_view name) const -> format_arg { + int id = get_id(name); + return id >= 0 ? get(id) : format_arg(); + } + + template + FMT_CONSTEXPR auto get_id(basic_string_view name) const -> int { + if (!has_named_args()) return -1; + const auto& named_args = + (is_packed() ? values_[-1] : args_[-1].value_).named_args; + for (size_t i = 0; i < named_args.size; ++i) { + if (named_args.data[i].name == name) return named_args.data[i].id; + } + return -1; + } + + auto max_size() const -> int { + unsigned long long max_packed = detail::max_packed_args; + return static_cast(is_packed() ? max_packed + : desc_ & ~detail::is_unpacked_bit); + } +}; + +// A formatting context. +class context { + private: + appender out_; + basic_format_args args_; + detail::locale_ref loc_; + + public: + /// The character type for the output. + using char_type = char; + + using iterator = appender; + using format_arg = basic_format_arg; + using parse_context_type = basic_format_parse_context; + template using formatter_type = formatter; + + /// Constructs a `basic_format_context` object. References to the arguments + /// are stored in the object so make sure they have appropriate lifetimes. + FMT_CONSTEXPR context(iterator out, basic_format_args ctx_args, + detail::locale_ref loc = {}) + : out_(out), args_(ctx_args), loc_(loc) {} + context(context&&) = default; + context(const context&) = delete; + void operator=(const context&) = delete; + + FMT_CONSTEXPR auto arg(int id) const -> format_arg { return args_.get(id); } + auto arg(string_view name) -> format_arg { return args_.get(name); } + FMT_CONSTEXPR auto arg_id(string_view name) -> int { + return args_.get_id(name); + } + auto args() const -> const basic_format_args& { return args_; } + + // Returns an iterator to the beginning of the output range. + FMT_CONSTEXPR auto out() -> iterator { return out_; } + + // Advances the begin iterator to `it`. + void advance_to(iterator) {} + + FMT_CONSTEXPR auto locale() -> detail::locale_ref { return loc_; } +}; + +template class generic_context; + +// Longer aliases for C++20 compatibility. +template +using basic_format_context = + conditional_t::value, context, + generic_context>; +using format_context = context; + +template +using buffered_context = basic_format_context, Char>; + +template +using is_formattable = bool_constant>() + .map(std::declval()))>::value>; + +#if FMT_USE_CONCEPTS +template +concept formattable = is_formattable, Char>::value; +#endif + +/** + * Constructs an object that stores references to arguments and can be + * implicitly converted to `format_args`. `Context` can be omitted in which case + * it defaults to `format_context`. See `arg` for lifetime considerations. + */ +// Take arguments by lvalue references to avoid some lifetime issues, e.g. +// auto args = make_format_args(std::string()); +template (), + unsigned long long DESC = detail::make_descriptor(), + FMT_ENABLE_IF(NUM_NAMED_ARGS == 0)> +constexpr FMT_ALWAYS_INLINE auto make_format_args(T&... args) + -> detail::format_arg_store { + return {{detail::make_arg( + args)...}}; +} + +#ifndef FMT_DOC +template (), + unsigned long long DESC = + detail::make_descriptor() | + static_cast(detail::has_named_args_bit), + FMT_ENABLE_IF(NUM_NAMED_ARGS != 0)> +constexpr auto make_format_args(T&... args) + -> detail::format_arg_store { + return {args...}; +} +#endif + +/** + * Returns a named argument to be used in a formatting function. + * It should only be used in a call to a formatting function or + * `dynamic_format_arg_store::push_back`. + * + * **Example**: + * + * fmt::print("The answer is {answer}.", fmt::arg("answer", 42)); + */ +template +inline auto arg(const Char* name, const T& arg) -> detail::named_arg { + static_assert(!detail::is_named_arg(), "nested named arguments"); + return {name, arg}; +} +FMT_END_EXPORT + +/// An alias for `basic_format_args`. +// A separate type would result in shorter symbols but break ABI compatibility +// between clang and gcc on ARM (#1919). +FMT_EXPORT using format_args = basic_format_args; + +// We cannot use enum classes as bit fields because of a gcc bug, so we put them +// in namespaces instead (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61414). +// Additionally, if an underlying type is specified, older gcc incorrectly warns +// that the type is too small. Both bugs are fixed in gcc 9.3. +#if FMT_GCC_VERSION && FMT_GCC_VERSION < 903 +# define FMT_ENUM_UNDERLYING_TYPE(type) +#else +# define FMT_ENUM_UNDERLYING_TYPE(type) : type +#endif +namespace align { +enum type FMT_ENUM_UNDERLYING_TYPE(unsigned char){none, left, right, center, + numeric}; +} +using align_t = align::type; +namespace sign { +enum type FMT_ENUM_UNDERLYING_TYPE(unsigned char){none, minus, plus, space}; +} +using sign_t = sign::type; + +namespace detail { + +template +using unsigned_char = typename conditional_t::value, + std::make_unsigned, + type_identity>::type; + +// Character (code unit) type is erased to prevent template bloat. +struct fill_t { + private: + enum { max_size = 4 }; + char data_[max_size] = {' '}; + unsigned char size_ = 1; + + public: + template + FMT_CONSTEXPR void operator=(basic_string_view s) { + auto size = s.size(); + size_ = static_cast(size); + if (size == 1) { + unsigned uchar = static_cast>(s[0]); + data_[0] = static_cast(uchar); + data_[1] = static_cast(uchar >> 8); + return; + } + FMT_ASSERT(size <= max_size, "invalid fill"); + for (size_t i = 0; i < size; ++i) data_[i] = static_cast(s[i]); + } + + FMT_CONSTEXPR void operator=(char c) { + data_[0] = c; + size_ = 1; + } + + constexpr auto size() const -> size_t { return size_; } + + template constexpr auto get() const -> Char { + using uchar = unsigned char; + return static_cast(static_cast(data_[0]) | + (static_cast(data_[1]) << 8)); + } + + template ::value)> + constexpr auto data() const -> const Char* { + return data_; + } + template ::value)> + constexpr auto data() const -> const Char* { + return nullptr; + } +}; +} // namespace detail + +enum class presentation_type : unsigned char { + // Common specifiers: + none = 0, + debug = 1, // '?' + string = 2, // 's' (string, bool) + + // Integral, bool and character specifiers: + dec = 3, // 'd' + hex, // 'x' or 'X' + oct, // 'o' + bin, // 'b' or 'B' + chr, // 'c' + + // String and pointer specifiers: + pointer = 3, // 'p' + + // Floating-point specifiers: + exp = 1, // 'e' or 'E' (1 since there is no FP debug presentation) + fixed, // 'f' or 'F' + general, // 'g' or 'G' + hexfloat // 'a' or 'A' +}; + +// Format specifiers for built-in and string types. +struct format_specs { + int width; + int precision; + presentation_type type; + align_t align : 4; + sign_t sign : 3; + bool upper : 1; // An uppercase version e.g. 'X' for 'x'. + bool alt : 1; // Alternate form ('#'). + bool localized : 1; + detail::fill_t fill; + + constexpr format_specs() + : width(0), + precision(-1), + type(presentation_type::none), + align(align::none), + sign(sign::none), + upper(false), + alt(false), + localized(false) {} +}; + +namespace detail { + +enum class arg_id_kind { none, index, name }; + +// An argument reference. +template struct arg_ref { + FMT_CONSTEXPR arg_ref() : kind(arg_id_kind::none), val() {} + + FMT_CONSTEXPR explicit arg_ref(int index) + : kind(arg_id_kind::index), val(index) {} + FMT_CONSTEXPR explicit arg_ref(basic_string_view name) + : kind(arg_id_kind::name), val(name) {} + + FMT_CONSTEXPR auto operator=(int idx) -> arg_ref& { + kind = arg_id_kind::index; + val.index = idx; + return *this; + } + + arg_id_kind kind; + union value { + FMT_CONSTEXPR value(int idx = 0) : index(idx) {} + FMT_CONSTEXPR value(basic_string_view n) : name(n) {} + + int index; + basic_string_view name; + } val; +}; + +// Format specifiers with width and precision resolved at formatting rather +// than parsing time to allow reusing the same parsed specifiers with +// different sets of arguments (precompilation of format strings). +template struct dynamic_format_specs : format_specs { + arg_ref width_ref; + arg_ref precision_ref; +}; + +// Converts a character to ASCII. Returns '\0' on conversion failure. +template ::value)> +constexpr auto to_ascii(Char c) -> char { + return c <= 0xff ? static_cast(c) : '\0'; +} + +// Returns the number of code units in a code point or 1 on error. +template +FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { + if (const_check(sizeof(Char) != 1)) return 1; + auto c = static_cast(*begin); + return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 0x3) + 1; +} + +// Return the result via the out param to workaround gcc bug 77539. +template +FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr& out) -> bool { + for (out = first; out != last; ++out) { + if (*out == value) return true; + } + return false; +} + +template <> +inline auto find(const char* first, const char* last, char value, + const char*& out) -> bool { + out = + static_cast(memchr(first, value, to_unsigned(last - first))); + return out != nullptr; +} + +// Parses the range [begin, end) as an unsigned integer. This function assumes +// that the range is non-empty and the first character is a digit. +template +FMT_CONSTEXPR auto parse_nonnegative_int(const Char*& begin, const Char* end, + int error_value) noexcept -> int { + FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', ""); + unsigned value = 0, prev = 0; + auto p = begin; + do { + prev = value; + value = value * 10 + unsigned(*p - '0'); + ++p; + } while (p != end && '0' <= *p && *p <= '9'); + auto num_digits = p - begin; + begin = p; + int digits10 = static_cast(sizeof(int) * CHAR_BIT * 3 / 10); + if (num_digits <= digits10) return static_cast(value); + // Check for overflow. + unsigned max = INT_MAX; + return num_digits == digits10 + 1 && + prev * 10ull + unsigned(p[-1] - '0') <= max + ? static_cast(value) + : error_value; +} + +FMT_CONSTEXPR inline auto parse_align(char c) -> align_t { + switch (c) { + case '<': + return align::left; + case '>': + return align::right; + case '^': + return align::center; + } + return align::none; +} + +template constexpr auto is_name_start(Char c) -> bool { + return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_'; +} + +template +FMT_CONSTEXPR auto do_parse_arg_id(const Char* begin, const Char* end, + Handler&& handler) -> const Char* { + Char c = *begin; + if (c >= '0' && c <= '9') { + int index = 0; + if (c != '0') + index = parse_nonnegative_int(begin, end, INT_MAX); + else + ++begin; + if (begin == end || (*begin != '}' && *begin != ':')) + report_error("invalid format string"); + else + handler.on_index(index); + return begin; + } + if (!is_name_start(c)) { + report_error("invalid format string"); + return begin; + } + auto it = begin; + do { + ++it; + } while (it != end && (is_name_start(*it) || ('0' <= *it && *it <= '9'))); + handler.on_name({begin, to_unsigned(it - begin)}); + return it; +} + +template +FMT_CONSTEXPR auto parse_arg_id(const Char* begin, const Char* end, + Handler&& handler) -> const Char* { + FMT_ASSERT(begin != end, ""); + Char c = *begin; + if (c != '}' && c != ':') return do_parse_arg_id(begin, end, handler); + handler.on_auto(); + return begin; +} + +template struct dynamic_spec_id_handler { + basic_format_parse_context& ctx; + arg_ref& ref; + + FMT_CONSTEXPR void on_auto() { + int id = ctx.next_arg_id(); + ref = arg_ref(id); + ctx.check_dynamic_spec(id); + } + FMT_CONSTEXPR void on_index(int id) { + ref = arg_ref(id); + ctx.check_arg_id(id); + ctx.check_dynamic_spec(id); + } + FMT_CONSTEXPR void on_name(basic_string_view id) { + ref = arg_ref(id); + ctx.check_arg_id(id); + } +}; + +// Parses [integer | "{" [arg_id] "}"]. +template +FMT_CONSTEXPR auto parse_dynamic_spec(const Char* begin, const Char* end, + int& value, arg_ref& ref, + basic_format_parse_context& ctx) + -> const Char* { + FMT_ASSERT(begin != end, ""); + if ('0' <= *begin && *begin <= '9') { + int val = parse_nonnegative_int(begin, end, -1); + if (val != -1) + value = val; + else + report_error("number is too big"); + } else if (*begin == '{') { + ++begin; + auto handler = dynamic_spec_id_handler{ctx, ref}; + if (begin != end) begin = parse_arg_id(begin, end, handler); + if (begin != end && *begin == '}') return ++begin; + report_error("invalid format string"); + } + return begin; +} + +template +FMT_CONSTEXPR auto parse_precision(const Char* begin, const Char* end, + int& value, arg_ref& ref, + basic_format_parse_context& ctx) + -> const Char* { + ++begin; + if (begin == end || *begin == '}') { + report_error("invalid precision"); + return begin; + } + return parse_dynamic_spec(begin, end, value, ref, ctx); +} + +enum class state { start, align, sign, hash, zero, width, precision, locale }; + +// Parses standard format specifiers. +template +FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end, + dynamic_format_specs& specs, + basic_format_parse_context& ctx, + type arg_type) -> const Char* { + auto c = '\0'; + if (end - begin > 1) { + auto next = to_ascii(begin[1]); + c = parse_align(next) == align::none ? to_ascii(*begin) : '\0'; + } else { + if (begin == end) return begin; + c = to_ascii(*begin); + } + + struct { + state current_state = state::start; + FMT_CONSTEXPR void operator()(state s, bool valid = true) { + if (current_state >= s || !valid) + report_error("invalid format specifier"); + current_state = s; + } + } enter_state; + + using pres = presentation_type; + constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; + struct { + const Char*& begin; + dynamic_format_specs& specs; + type arg_type; + + FMT_CONSTEXPR auto operator()(pres pres_type, int set) -> const Char* { + if (!in(arg_type, set)) { + if (arg_type == type::none_type) return begin; + report_error("invalid format specifier"); + } + specs.type = pres_type; + return begin + 1; + } + } parse_presentation_type{begin, specs, arg_type}; + + for (;;) { + switch (c) { + case '<': + case '>': + case '^': + enter_state(state::align); + specs.align = parse_align(c); + ++begin; + break; + case '+': + case '-': + case ' ': + if (arg_type == type::none_type) return begin; + enter_state(state::sign, in(arg_type, sint_set | float_set)); + switch (c) { + case '+': + specs.sign = sign::plus; + break; + case '-': + specs.sign = sign::minus; + break; + case ' ': + specs.sign = sign::space; + break; + } + ++begin; + break; + case '#': + if (arg_type == type::none_type) return begin; + enter_state(state::hash, is_arithmetic_type(arg_type)); + specs.alt = true; + ++begin; + break; + case '0': + enter_state(state::zero); + if (!is_arithmetic_type(arg_type)) { + if (arg_type == type::none_type) return begin; + report_error("format specifier requires numeric argument"); + } + if (specs.align == align::none) { + // Ignore 0 if align is specified for compatibility with std::format. + specs.align = align::numeric; + specs.fill = '0'; + } + ++begin; + break; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '{': + enter_state(state::width); + begin = parse_dynamic_spec(begin, end, specs.width, specs.width_ref, ctx); + break; + case '.': + if (arg_type == type::none_type) return begin; + enter_state(state::precision, + in(arg_type, float_set | string_set | cstring_set)); + begin = parse_precision(begin, end, specs.precision, specs.precision_ref, + ctx); + break; + case 'L': + if (arg_type == type::none_type) return begin; + enter_state(state::locale, is_arithmetic_type(arg_type)); + specs.localized = true; + ++begin; + break; + case 'd': + return parse_presentation_type(pres::dec, integral_set); + case 'X': + specs.upper = true; + FMT_FALLTHROUGH; + case 'x': + return parse_presentation_type(pres::hex, integral_set); + case 'o': + return parse_presentation_type(pres::oct, integral_set); + case 'B': + specs.upper = true; + FMT_FALLTHROUGH; + case 'b': + return parse_presentation_type(pres::bin, integral_set); + case 'E': + specs.upper = true; + FMT_FALLTHROUGH; + case 'e': + return parse_presentation_type(pres::exp, float_set); + case 'F': + specs.upper = true; + FMT_FALLTHROUGH; + case 'f': + return parse_presentation_type(pres::fixed, float_set); + case 'G': + specs.upper = true; + FMT_FALLTHROUGH; + case 'g': + return parse_presentation_type(pres::general, float_set); + case 'A': + specs.upper = true; + FMT_FALLTHROUGH; + case 'a': + return parse_presentation_type(pres::hexfloat, float_set); + case 'c': + if (arg_type == type::bool_type) report_error("invalid format specifier"); + return parse_presentation_type(pres::chr, integral_set); + case 's': + return parse_presentation_type(pres::string, + bool_set | string_set | cstring_set); + case 'p': + return parse_presentation_type(pres::pointer, pointer_set | cstring_set); + case '?': + return parse_presentation_type(pres::debug, + char_set | string_set | cstring_set); + case '}': + return begin; + default: { + if (*begin == '}') return begin; + // Parse fill and alignment. + auto fill_end = begin + code_point_length(begin); + if (end - fill_end <= 0) { + report_error("invalid format specifier"); + return begin; + } + if (*begin == '{') { + report_error("invalid fill character '{'"); + return begin; + } + auto align = parse_align(to_ascii(*fill_end)); + enter_state(state::align, align != align::none); + specs.fill = + basic_string_view(begin, to_unsigned(fill_end - begin)); + specs.align = align; + begin = fill_end + 1; + } + } + if (begin == end) return begin; + c = to_ascii(*begin); + } +} + +template +FMT_CONSTEXPR auto parse_replacement_field(const Char* begin, const Char* end, + Handler&& handler) -> const Char* { + struct id_adapter { + Handler& handler; + int arg_id; + + FMT_CONSTEXPR void on_auto() { arg_id = handler.on_arg_id(); } + FMT_CONSTEXPR void on_index(int id) { arg_id = handler.on_arg_id(id); } + FMT_CONSTEXPR void on_name(basic_string_view id) { + arg_id = handler.on_arg_id(id); + } + }; + + ++begin; + if (begin == end) return handler.on_error("invalid format string"), end; + if (*begin == '}') { + handler.on_replacement_field(handler.on_arg_id(), begin); + } else if (*begin == '{') { + handler.on_text(begin, begin + 1); + } else { + auto adapter = id_adapter{handler, 0}; + begin = parse_arg_id(begin, end, adapter); + Char c = begin != end ? *begin : Char(); + if (c == '}') { + handler.on_replacement_field(adapter.arg_id, begin); + } else if (c == ':') { + begin = handler.on_format_specs(adapter.arg_id, begin + 1, end); + if (begin == end || *begin != '}') + return handler.on_error("unknown format specifier"), end; + } else { + return handler.on_error("missing '}' in format string"), end; + } + } + return begin + 1; +} + +template +FMT_CONSTEXPR void parse_format_string(basic_string_view format_str, + Handler&& handler) { + auto begin = format_str.data(); + auto end = begin + format_str.size(); + if (end - begin < 32) { + // Use a simple loop instead of memchr for small strings. + const Char* p = begin; + while (p != end) { + auto c = *p++; + if (c == '{') { + handler.on_text(begin, p - 1); + begin = p = parse_replacement_field(p - 1, end, handler); + } else if (c == '}') { + if (p == end || *p != '}') + return handler.on_error("unmatched '}' in format string"); + handler.on_text(begin, p); + begin = ++p; + } + } + handler.on_text(begin, end); + return; + } + struct writer { + FMT_CONSTEXPR void operator()(const Char* from, const Char* to) { + if (from == to) return; + for (;;) { + const Char* p = nullptr; + if (!find(from, to, Char('}'), p)) + return handler_.on_text(from, to); + ++p; + if (p == to || *p != '}') + return handler_.on_error("unmatched '}' in format string"); + handler_.on_text(from, p); + from = p + 1; + } + } + Handler& handler_; + } write = {handler}; + while (begin != end) { + // Doing two passes with memchr (one for '{' and another for '}') is up to + // 2.5x faster than the naive one-pass implementation on big format strings. + const Char* p = begin; + if (*begin != '{' && !find(begin + 1, end, Char('{'), p)) + return write(begin, end); + write(begin, p); + begin = parse_replacement_field(p, end, handler); + } +} + +template ::value> struct strip_named_arg { + using type = T; +}; +template struct strip_named_arg { + using type = remove_cvref_t; +}; + +template +FMT_VISIBILITY("hidden") // Suppress an ld warning on macOS (#3769). +FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx) + -> decltype(ctx.begin()) { + using char_type = typename ParseContext::char_type; + using context = buffered_context; + using mapped_type = conditional_t< + mapped_type_constant::value != type::custom_type, + decltype(arg_mapper().map(std::declval())), + typename strip_named_arg::type>; +#if defined(__cpp_if_constexpr) + if constexpr (std::is_default_constructible< + formatter>::value) { + return formatter().parse(ctx); + } else { + type_is_unformattable_for _; + return ctx.begin(); + } +#else + return formatter().parse(ctx); +#endif +} + +// Checks char specs and returns true iff the presentation type is char-like. +FMT_CONSTEXPR inline auto check_char_specs(const format_specs& specs) -> bool { + if (specs.type != presentation_type::none && + specs.type != presentation_type::chr && + specs.type != presentation_type::debug) { + return false; + } + if (specs.align == align::numeric || specs.sign != sign::none || specs.alt) + report_error("invalid format specifier for char"); + return true; +} + +#if FMT_USE_NONTYPE_TEMPLATE_ARGS +template +constexpr auto get_arg_index_by_name(basic_string_view name) -> int { + if constexpr (is_statically_named_arg()) { + if (name == T::name) return N; + } + if constexpr (sizeof...(Args) > 0) + return get_arg_index_by_name(name); + (void)name; // Workaround an MSVC bug about "unused" parameter. + return -1; +} +#endif + +template +FMT_CONSTEXPR auto get_arg_index_by_name(basic_string_view name) -> int { +#if FMT_USE_NONTYPE_TEMPLATE_ARGS + if constexpr (sizeof...(Args) > 0) + return get_arg_index_by_name<0, Args...>(name); +#endif + (void)name; + return -1; +} + +template class format_string_checker { + private: + using parse_context_type = compile_parse_context; + static constexpr int num_args = sizeof...(Args); + + // Format specifier parsing function. + // In the future basic_format_parse_context will replace compile_parse_context + // here and will use is_constant_evaluated and downcasting to access the data + // needed for compile-time checks: https://godbolt.org/z/GvWzcTjh1. + using parse_func = const Char* (*)(parse_context_type&); + + type types_[num_args > 0 ? static_cast(num_args) : 1]; + parse_context_type context_; + parse_func parse_funcs_[num_args > 0 ? static_cast(num_args) : 1]; + + public: + explicit FMT_CONSTEXPR format_string_checker(basic_string_view fmt) + : types_{mapped_type_constant>::value...}, + context_(fmt, num_args, types_), + parse_funcs_{&parse_format_specs...} {} + + FMT_CONSTEXPR void on_text(const Char*, const Char*) {} + + FMT_CONSTEXPR auto on_arg_id() -> int { return context_.next_arg_id(); } + FMT_CONSTEXPR auto on_arg_id(int id) -> int { + return context_.check_arg_id(id), id; + } + FMT_CONSTEXPR auto on_arg_id(basic_string_view id) -> int { +#if FMT_USE_NONTYPE_TEMPLATE_ARGS + auto index = get_arg_index_by_name(id); + if (index < 0) on_error("named argument is not found"); + return index; +#else + (void)id; + on_error("compile-time checks for named arguments require C++20 support"); + return 0; +#endif + } + + FMT_CONSTEXPR void on_replacement_field(int id, const Char* begin) { + on_format_specs(id, begin, begin); // Call parse() on empty specs. + } + + FMT_CONSTEXPR auto on_format_specs(int id, const Char* begin, const Char*) + -> const Char* { + context_.advance_to(begin); + // id >= 0 check is a workaround for gcc 10 bug (#2065). + return id >= 0 && id < num_args ? parse_funcs_[id](context_) : begin; + } + + FMT_NORETURN FMT_CONSTEXPR void on_error(const char* message) { + report_error(message); + } +}; + +// A base class for compile-time strings. +struct compile_string {}; + +template +using is_compile_string = std::is_base_of; + +// Reports a compile-time error if S is not a valid format string. +template ::value)> +FMT_ALWAYS_INLINE void check_format_string(const S&) { +#ifdef FMT_ENFORCE_COMPILE_STRING + static_assert(is_compile_string::value, + "FMT_ENFORCE_COMPILE_STRING requires all format strings to use " + "FMT_STRING."); +#endif +} +template ::value)> +void check_format_string(S format_str) { + using char_t = typename S::char_type; + FMT_CONSTEXPR auto s = basic_string_view(format_str); + using checker = format_string_checker...>; + FMT_CONSTEXPR bool error = (parse_format_string(s, checker(s)), true); + ignore_unused(error); +} + +// Report truncation to prevent silent data loss. +inline void report_truncation(bool truncated) { + if (truncated) report_error("output is truncated"); +} + +// Use vformat_args and avoid type_identity to keep symbols short and workaround +// a GCC <= 4.8 bug. +template struct vformat_args { + using type = basic_format_args>; +}; +template <> struct vformat_args { + using type = format_args; +}; + +template +void vformat_to(buffer& buf, basic_string_view fmt, + typename vformat_args::type args, locale_ref loc = {}); + +FMT_API void vprint_mojibake(FILE*, string_view, format_args, bool = false); +#ifndef _WIN32 +inline void vprint_mojibake(FILE*, string_view, format_args, bool) {} +#endif + +template struct native_formatter { + private: + dynamic_format_specs specs_; + + public: + using nonlocking = void; + + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* { + if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); + auto end = parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, TYPE); + if (const_check(TYPE == type::char_type)) check_char_specs(specs_); + return end; + } + + template + FMT_CONSTEXPR void set_debug_format(bool set = true) { + specs_.type = set ? presentation_type::debug : presentation_type::none; + } + + template + FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const + -> decltype(ctx.out()); +}; +} // namespace detail + +FMT_BEGIN_EXPORT + +// A formatter specialization for natively supported types. +template +struct formatter::value != + detail::type::custom_type>> + : detail::native_formatter::value> { +}; + +template struct runtime_format_string { + basic_string_view str; +}; + +/// A compile-time format string. +template class basic_format_string { + private: + basic_string_view str_; + + public: + template < + typename S, + FMT_ENABLE_IF( + std::is_convertible>::value || + (detail::is_compile_string::value && + std::is_constructible, const S&>::value))> + FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_format_string(const S& s) : str_(s) { + static_assert( + detail::count< + (std::is_base_of>::value && + std::is_reference::value)...>() == 0, + "passing views as lvalues is disallowed"); +#if FMT_USE_CONSTEVAL + if constexpr (detail::count_named_args() == + detail::count_statically_named_args()) { + using checker = + detail::format_string_checker...>; + detail::parse_format_string(str_, checker(s)); + } +#else + detail::check_format_string(s); +#endif + } + basic_format_string(runtime_format_string fmt) : str_(fmt.str) {} + + FMT_ALWAYS_INLINE operator basic_string_view() const { return str_; } + auto get() const -> basic_string_view { return str_; } +}; + +#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 +// Workaround broken conversion on older gcc. +template using format_string = string_view; +inline auto runtime(string_view s) -> string_view { return s; } +#else +template +using format_string = basic_format_string...>; +/** + * Creates a runtime format string. + * + * **Example**: + * + * // Check format string at runtime instead of compile-time. + * fmt::print(fmt::runtime("{:d}"), "I am not a number"); + */ +inline auto runtime(string_view s) -> runtime_format_string<> { return {{s}}; } +#endif + +/// Formats a string and writes the output to `out`. +template , + char>::value)> +auto vformat_to(OutputIt&& out, string_view fmt, format_args args) + -> remove_cvref_t { + auto&& buf = detail::get_buffer(out); + detail::vformat_to(buf, fmt, args, {}); + return detail::get_iterator(buf, out); +} + +/** + * Formats `args` according to specifications in `fmt`, writes the result to + * the output iterator `out` and returns the iterator past the end of the output + * range. `format_to` does not append a terminating null character. + * + * **Example**: + * + * auto out = std::vector(); + * fmt::format_to(std::back_inserter(out), "{}", 42); + */ +template , + char>::value)> +FMT_INLINE auto format_to(OutputIt&& out, format_string fmt, T&&... args) + -> remove_cvref_t { + return vformat_to(FMT_FWD(out), fmt, fmt::make_format_args(args...)); +} + +template struct format_to_n_result { + /// Iterator past the end of the output range. + OutputIt out; + /// Total (not truncated) output size. + size_t size; +}; + +template ::value)> +auto vformat_to_n(OutputIt out, size_t n, string_view fmt, format_args args) + -> format_to_n_result { + using traits = detail::fixed_buffer_traits; + auto buf = detail::iterator_buffer(out, n); + detail::vformat_to(buf, fmt, args, {}); + return {buf.out(), buf.count()}; +} + +/** + * Formats `args` according to specifications in `fmt`, writes up to `n` + * characters of the result to the output iterator `out` and returns the total + * (not truncated) output size and the iterator past the end of the output + * range. `format_to_n` does not append a terminating null character. + */ +template ::value)> +FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string fmt, + T&&... args) -> format_to_n_result { + return vformat_to_n(out, n, fmt, fmt::make_format_args(args...)); +} + +template +struct format_to_result { + /// Iterator pointing to just after the last successful write in the range. + OutputIt out; + /// Specifies if the output was truncated. + bool truncated; + + FMT_CONSTEXPR operator OutputIt&() & { + detail::report_truncation(truncated); + return out; + } + FMT_CONSTEXPR operator const OutputIt&() const& { + detail::report_truncation(truncated); + return out; + } + FMT_CONSTEXPR operator OutputIt&&() && { + detail::report_truncation(truncated); + return static_cast(out); + } +}; + +template +auto vformat_to(char (&out)[N], string_view fmt, format_args args) + -> format_to_result { + auto result = vformat_to_n(out, N, fmt, args); + return {result.out, result.size > N}; +} + +template +FMT_INLINE auto format_to(char (&out)[N], format_string fmt, T&&... args) + -> format_to_result { + auto result = fmt::format_to_n(out, N, fmt, static_cast(args)...); + return {result.out, result.size > N}; +} + +/// Returns the number of chars in the output of `format(fmt, args...)`. +template +FMT_NODISCARD FMT_INLINE auto formatted_size(format_string fmt, + T&&... args) -> size_t { + auto buf = detail::counting_buffer<>(); + detail::vformat_to(buf, fmt, fmt::make_format_args(args...), {}); + return buf.count(); +} + +FMT_API void vprint(string_view fmt, format_args args); +FMT_API void vprint(FILE* f, string_view fmt, format_args args); +FMT_API void vprint_buffered(FILE* f, string_view fmt, format_args args); +FMT_API void vprintln(FILE* f, string_view fmt, format_args args); + +/** + * Formats `args` according to specifications in `fmt` and writes the output + * to `stdout`. + * + * **Example**: + * + * fmt::print("The answer is {}.", 42); + */ +template +FMT_INLINE void print(format_string fmt, T&&... args) { + const auto& vargs = fmt::make_format_args(args...); + if (!detail::use_utf8()) return detail::vprint_mojibake(stdout, fmt, vargs); + return detail::is_locking() ? vprint_buffered(stdout, fmt, vargs) + : vprint(fmt, vargs); +} + +/** + * Formats `args` according to specifications in `fmt` and writes the + * output to the file `f`. + * + * **Example**: + * + * fmt::print(stderr, "Don't {}!", "panic"); + */ +template +FMT_INLINE void print(FILE* f, format_string fmt, T&&... args) { + const auto& vargs = fmt::make_format_args(args...); + if (!detail::use_utf8()) return detail::vprint_mojibake(f, fmt, vargs); + return detail::is_locking() ? vprint_buffered(f, fmt, vargs) + : vprint(f, fmt, vargs); +} + +/// Formats `args` according to specifications in `fmt` and writes the output +/// to the file `f` followed by a newline. +template +FMT_INLINE void println(FILE* f, format_string fmt, T&&... args) { + const auto& vargs = fmt::make_format_args(args...); + return detail::use_utf8() ? vprintln(f, fmt, vargs) + : detail::vprint_mojibake(f, fmt, vargs, true); +} + +/// Formats `args` according to specifications in `fmt` and writes the output +/// to `stdout` followed by a newline. +template +FMT_INLINE void println(format_string fmt, T&&... args) { + return fmt::println(stdout, fmt, static_cast(args)...); +} + +FMT_END_EXPORT +FMT_GCC_PRAGMA("GCC pop_options") +FMT_END_NAMESPACE + +#ifdef FMT_HEADER_ONLY +# include "format.h" +#endif +#endif // FMT_BASE_H_ diff --git a/include/spdlog/fmt/bundled/chrono.h b/include/spdlog/fmt/bundled/chrono.h new file mode 100644 index 0000000..c93123f --- /dev/null +++ b/include/spdlog/fmt/bundled/chrono.h @@ -0,0 +1,2432 @@ +// Formatting library for C++ - chrono support +// +// Copyright (c) 2012 - present, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_CHRONO_H_ +#define FMT_CHRONO_H_ + +#ifndef FMT_MODULE +# include +# include +# include // std::isfinite +# include // std::memcpy +# include +# include +# include +# include +# include +#endif + +#include "format.h" + +FMT_BEGIN_NAMESPACE + +// Check if std::chrono::local_t is available. +#ifndef FMT_USE_LOCAL_TIME +# ifdef __cpp_lib_chrono +# define FMT_USE_LOCAL_TIME (__cpp_lib_chrono >= 201907L) +# else +# define FMT_USE_LOCAL_TIME 0 +# endif +#endif + +// Check if std::chrono::utc_timestamp is available. +#ifndef FMT_USE_UTC_TIME +# ifdef __cpp_lib_chrono +# define FMT_USE_UTC_TIME (__cpp_lib_chrono >= 201907L) +# else +# define FMT_USE_UTC_TIME 0 +# endif +#endif + +// Enable tzset. +#ifndef FMT_USE_TZSET +// UWP doesn't provide _tzset. +# if FMT_HAS_INCLUDE("winapifamily.h") +# include +# endif +# if defined(_WIN32) && (!defined(WINAPI_FAMILY) || \ + (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) +# define FMT_USE_TZSET 1 +# else +# define FMT_USE_TZSET 0 +# endif +#endif + +// Enable safe chrono durations, unless explicitly disabled. +#ifndef FMT_SAFE_DURATION_CAST +# define FMT_SAFE_DURATION_CAST 1 +#endif +#if FMT_SAFE_DURATION_CAST + +// For conversion between std::chrono::durations without undefined +// behaviour or erroneous results. +// This is a stripped down version of duration_cast, for inclusion in fmt. +// See https://github.com/pauldreik/safe_duration_cast +// +// Copyright Paul Dreik 2019 +namespace safe_duration_cast { + +template ::value && + std::numeric_limits::is_signed == + std::numeric_limits::is_signed)> +FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) + -> To { + ec = 0; + using F = std::numeric_limits; + using T = std::numeric_limits; + static_assert(F::is_integer, "From must be integral"); + static_assert(T::is_integer, "To must be integral"); + + // A and B are both signed, or both unsigned. + if (detail::const_check(F::digits <= T::digits)) { + // From fits in To without any problem. + } else { + // From does not always fit in To, resort to a dynamic check. + if (from < (T::min)() || from > (T::max)()) { + // outside range. + ec = 1; + return {}; + } + } + return static_cast(from); +} + +/// Converts From to To, without loss. If the dynamic value of from +/// can't be converted to To without loss, ec is set. +template ::value && + std::numeric_limits::is_signed != + std::numeric_limits::is_signed)> +FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) + -> To { + ec = 0; + using F = std::numeric_limits; + using T = std::numeric_limits; + static_assert(F::is_integer, "From must be integral"); + static_assert(T::is_integer, "To must be integral"); + + if (detail::const_check(F::is_signed && !T::is_signed)) { + // From may be negative, not allowed! + if (fmt::detail::is_negative(from)) { + ec = 1; + return {}; + } + // From is positive. Can it always fit in To? + if (detail::const_check(F::digits > T::digits) && + from > static_cast(detail::max_value())) { + ec = 1; + return {}; + } + } + + if (detail::const_check(!F::is_signed && T::is_signed && + F::digits >= T::digits) && + from > static_cast(detail::max_value())) { + ec = 1; + return {}; + } + return static_cast(from); // Lossless conversion. +} + +template ::value)> +FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) + -> To { + ec = 0; + return from; +} // function + +// clang-format off +/** + * converts From to To if possible, otherwise ec is set. + * + * input | output + * ---------------------------------|--------------- + * NaN | NaN + * Inf | Inf + * normal, fits in output | converted (possibly lossy) + * normal, does not fit in output | ec is set + * subnormal | best effort + * -Inf | -Inf + */ +// clang-format on +template ::value)> +FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To { + ec = 0; + using T = std::numeric_limits; + static_assert(std::is_floating_point::value, "From must be floating"); + static_assert(std::is_floating_point::value, "To must be floating"); + + // catch the only happy case + if (std::isfinite(from)) { + if (from >= T::lowest() && from <= (T::max)()) { + return static_cast(from); + } + // not within range. + ec = 1; + return {}; + } + + // nan and inf will be preserved + return static_cast(from); +} // function + +template ::value)> +FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To { + ec = 0; + static_assert(std::is_floating_point::value, "From must be floating"); + return from; +} + +/// Safe duration cast between integral durations +template ::value), + FMT_ENABLE_IF(std::is_integral::value)> +auto safe_duration_cast(std::chrono::duration from, + int& ec) -> To { + using From = std::chrono::duration; + ec = 0; + // the basic idea is that we need to convert from count() in the from type + // to count() in the To type, by multiplying it with this: + struct Factor + : std::ratio_divide {}; + + static_assert(Factor::num > 0, "num must be positive"); + static_assert(Factor::den > 0, "den must be positive"); + + // the conversion is like this: multiply from.count() with Factor::num + // /Factor::den and convert it to To::rep, all this without + // overflow/underflow. let's start by finding a suitable type that can hold + // both To, From and Factor::num + using IntermediateRep = + typename std::common_type::type; + + // safe conversion to IntermediateRep + IntermediateRep count = + lossless_integral_conversion(from.count(), ec); + if (ec) return {}; + // multiply with Factor::num without overflow or underflow + if (detail::const_check(Factor::num != 1)) { + const auto max1 = detail::max_value() / Factor::num; + if (count > max1) { + ec = 1; + return {}; + } + const auto min1 = + (std::numeric_limits::min)() / Factor::num; + if (detail::const_check(!std::is_unsigned::value) && + count < min1) { + ec = 1; + return {}; + } + count *= Factor::num; + } + + if (detail::const_check(Factor::den != 1)) count /= Factor::den; + auto tocount = lossless_integral_conversion(count, ec); + return ec ? To() : To(tocount); +} + +/// Safe duration_cast between floating point durations +template ::value), + FMT_ENABLE_IF(std::is_floating_point::value)> +auto safe_duration_cast(std::chrono::duration from, + int& ec) -> To { + using From = std::chrono::duration; + ec = 0; + if (std::isnan(from.count())) { + // nan in, gives nan out. easy. + return To{std::numeric_limits::quiet_NaN()}; + } + // maybe we should also check if from is denormal, and decide what to do about + // it. + + // +-inf should be preserved. + if (std::isinf(from.count())) { + return To{from.count()}; + } + + // the basic idea is that we need to convert from count() in the from type + // to count() in the To type, by multiplying it with this: + struct Factor + : std::ratio_divide {}; + + static_assert(Factor::num > 0, "num must be positive"); + static_assert(Factor::den > 0, "den must be positive"); + + // the conversion is like this: multiply from.count() with Factor::num + // /Factor::den and convert it to To::rep, all this without + // overflow/underflow. let's start by finding a suitable type that can hold + // both To, From and Factor::num + using IntermediateRep = + typename std::common_type::type; + + // force conversion of From::rep -> IntermediateRep to be safe, + // even if it will never happen be narrowing in this context. + IntermediateRep count = + safe_float_conversion(from.count(), ec); + if (ec) { + return {}; + } + + // multiply with Factor::num without overflow or underflow + if (detail::const_check(Factor::num != 1)) { + constexpr auto max1 = detail::max_value() / + static_cast(Factor::num); + if (count > max1) { + ec = 1; + return {}; + } + constexpr auto min1 = std::numeric_limits::lowest() / + static_cast(Factor::num); + if (count < min1) { + ec = 1; + return {}; + } + count *= static_cast(Factor::num); + } + + // this can't go wrong, right? den>0 is checked earlier. + if (detail::const_check(Factor::den != 1)) { + using common_t = typename std::common_type::type; + count /= static_cast(Factor::den); + } + + // convert to the to type, safely + using ToRep = typename To::rep; + + const ToRep tocount = safe_float_conversion(count, ec); + if (ec) { + return {}; + } + return To{tocount}; +} +} // namespace safe_duration_cast +#endif + +// Prevents expansion of a preceding token as a function-style macro. +// Usage: f FMT_NOMACRO() +#define FMT_NOMACRO + +namespace detail { +template struct null {}; +inline auto localtime_r FMT_NOMACRO(...) -> null<> { return null<>(); } +inline auto localtime_s(...) -> null<> { return null<>(); } +inline auto gmtime_r(...) -> null<> { return null<>(); } +inline auto gmtime_s(...) -> null<> { return null<>(); } + +// It is defined here and not in ostream.h because the latter has expensive +// includes. +template class formatbuf : public Streambuf { + private: + using char_type = typename Streambuf::char_type; + using streamsize = decltype(std::declval().sputn(nullptr, 0)); + using int_type = typename Streambuf::int_type; + using traits_type = typename Streambuf::traits_type; + + buffer& buffer_; + + public: + explicit formatbuf(buffer& buf) : buffer_(buf) {} + + protected: + // The put area is always empty. This makes the implementation simpler and has + // the advantage that the streambuf and the buffer are always in sync and + // sputc never writes into uninitialized memory. A disadvantage is that each + // call to sputc always results in a (virtual) call to overflow. There is no + // disadvantage here for sputn since this always results in a call to xsputn. + + auto overflow(int_type ch) -> int_type override { + if (!traits_type::eq_int_type(ch, traits_type::eof())) + buffer_.push_back(static_cast(ch)); + return ch; + } + + auto xsputn(const char_type* s, streamsize count) -> streamsize override { + buffer_.append(s, s + count); + return count; + } +}; + +inline auto get_classic_locale() -> const std::locale& { + static const auto& locale = std::locale::classic(); + return locale; +} + +template struct codecvt_result { + static constexpr const size_t max_size = 32; + CodeUnit buf[max_size]; + CodeUnit* end; +}; + +template +void write_codecvt(codecvt_result& out, string_view in_buf, + const std::locale& loc) { +#if FMT_CLANG_VERSION +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wdeprecated" + auto& f = std::use_facet>(loc); +# pragma clang diagnostic pop +#else + auto& f = std::use_facet>(loc); +#endif + auto mb = std::mbstate_t(); + const char* from_next = nullptr; + auto result = f.in(mb, in_buf.begin(), in_buf.end(), from_next, + std::begin(out.buf), std::end(out.buf), out.end); + if (result != std::codecvt_base::ok) + FMT_THROW(format_error("failed to format time")); +} + +template +auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc) + -> OutputIt { + if (detail::use_utf8() && loc != get_classic_locale()) { + // char16_t and char32_t codecvts are broken in MSVC (linkage errors) and + // gcc-4. +#if FMT_MSC_VERSION != 0 || \ + (defined(__GLIBCXX__) && \ + (!defined(_GLIBCXX_USE_DUAL_ABI) || _GLIBCXX_USE_DUAL_ABI == 0)) + // The _GLIBCXX_USE_DUAL_ABI macro is always defined in libstdc++ from gcc-5 + // and newer. + using code_unit = wchar_t; +#else + using code_unit = char32_t; +#endif + + using unit_t = codecvt_result; + unit_t unit; + write_codecvt(unit, in, loc); + // In UTF-8 is used one to four one-byte code units. + auto u = + to_utf8>(); + if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)})) + FMT_THROW(format_error("failed to format time")); + return copy(u.c_str(), u.c_str() + u.size(), out); + } + return copy(in.data(), in.data() + in.size(), out); +} + +template ::value)> +auto write_tm_str(OutputIt out, string_view sv, const std::locale& loc) + -> OutputIt { + codecvt_result unit; + write_codecvt(unit, sv, loc); + return copy(unit.buf, unit.end, out); +} + +template ::value)> +auto write_tm_str(OutputIt out, string_view sv, const std::locale& loc) + -> OutputIt { + return write_encoded_tm_str(out, sv, loc); +} + +template +inline void do_write(buffer& buf, const std::tm& time, + const std::locale& loc, char format, char modifier) { + auto&& format_buf = formatbuf>(buf); + auto&& os = std::basic_ostream(&format_buf); + os.imbue(loc); + const auto& facet = std::use_facet>(loc); + auto end = facet.put(os, os, Char(' '), &time, format, modifier); + if (end.failed()) FMT_THROW(format_error("failed to format time")); +} + +template ::value)> +auto write(OutputIt out, const std::tm& time, const std::locale& loc, + char format, char modifier = 0) -> OutputIt { + auto&& buf = get_buffer(out); + do_write(buf, time, loc, format, modifier); + return get_iterator(buf, out); +} + +template ::value)> +auto write(OutputIt out, const std::tm& time, const std::locale& loc, + char format, char modifier = 0) -> OutputIt { + auto&& buf = basic_memory_buffer(); + do_write(buf, time, loc, format, modifier); + return write_encoded_tm_str(out, string_view(buf.data(), buf.size()), loc); +} + +template +struct is_same_arithmetic_type + : public std::integral_constant::value && + std::is_integral::value) || + (std::is_floating_point::value && + std::is_floating_point::value)> { +}; + +template < + typename To, typename FromRep, typename FromPeriod, + FMT_ENABLE_IF(is_same_arithmetic_type::value)> +auto fmt_duration_cast(std::chrono::duration from) -> To { +#if FMT_SAFE_DURATION_CAST + // Throwing version of safe_duration_cast is only available for + // integer to integer or float to float casts. + int ec; + To to = safe_duration_cast::safe_duration_cast(from, ec); + if (ec) FMT_THROW(format_error("cannot format duration")); + return to; +#else + // Standard duration cast, may overflow. + return std::chrono::duration_cast(from); +#endif +} + +template < + typename To, typename FromRep, typename FromPeriod, + FMT_ENABLE_IF(!is_same_arithmetic_type::value)> +auto fmt_duration_cast(std::chrono::duration from) -> To { + // Mixed integer <-> float cast is not supported by safe_duration_cast. + return std::chrono::duration_cast(from); +} + +template +auto to_time_t( + std::chrono::time_point time_point) + -> std::time_t { + // Cannot use std::chrono::system_clock::to_time_t since this would first + // require a cast to std::chrono::system_clock::time_point, which could + // overflow. + return fmt_duration_cast>( + time_point.time_since_epoch()) + .count(); +} +} // namespace detail + +FMT_BEGIN_EXPORT + +/** + * Converts given time since epoch as `std::time_t` value into calendar time, + * expressed in local time. Unlike `std::localtime`, this function is + * thread-safe on most platforms. + */ +inline auto localtime(std::time_t time) -> std::tm { + struct dispatcher { + std::time_t time_; + std::tm tm_; + + dispatcher(std::time_t t) : time_(t) {} + + auto run() -> bool { + using namespace fmt::detail; + return handle(localtime_r(&time_, &tm_)); + } + + auto handle(std::tm* tm) -> bool { return tm != nullptr; } + + auto handle(detail::null<>) -> bool { + using namespace fmt::detail; + return fallback(localtime_s(&tm_, &time_)); + } + + auto fallback(int res) -> bool { return res == 0; } + +#if !FMT_MSC_VERSION + auto fallback(detail::null<>) -> bool { + using namespace fmt::detail; + std::tm* tm = std::localtime(&time_); + if (tm) tm_ = *tm; + return tm != nullptr; + } +#endif + }; + dispatcher lt(time); + // Too big time values may be unsupported. + if (!lt.run()) FMT_THROW(format_error("time_t value out of range")); + return lt.tm_; +} + +#if FMT_USE_LOCAL_TIME +template +inline auto localtime(std::chrono::local_time time) -> std::tm { + return localtime( + detail::to_time_t(std::chrono::current_zone()->to_sys(time))); +} +#endif + +/** + * Converts given time since epoch as `std::time_t` value into calendar time, + * expressed in Coordinated Universal Time (UTC). Unlike `std::gmtime`, this + * function is thread-safe on most platforms. + */ +inline auto gmtime(std::time_t time) -> std::tm { + struct dispatcher { + std::time_t time_; + std::tm tm_; + + dispatcher(std::time_t t) : time_(t) {} + + auto run() -> bool { + using namespace fmt::detail; + return handle(gmtime_r(&time_, &tm_)); + } + + auto handle(std::tm* tm) -> bool { return tm != nullptr; } + + auto handle(detail::null<>) -> bool { + using namespace fmt::detail; + return fallback(gmtime_s(&tm_, &time_)); + } + + auto fallback(int res) -> bool { return res == 0; } + +#if !FMT_MSC_VERSION + auto fallback(detail::null<>) -> bool { + std::tm* tm = std::gmtime(&time_); + if (tm) tm_ = *tm; + return tm != nullptr; + } +#endif + }; + auto gt = dispatcher(time); + // Too big time values may be unsupported. + if (!gt.run()) FMT_THROW(format_error("time_t value out of range")); + return gt.tm_; +} + +template +inline auto gmtime( + std::chrono::time_point time_point) + -> std::tm { + return gmtime(detail::to_time_t(time_point)); +} + +namespace detail { + +// Writes two-digit numbers a, b and c separated by sep to buf. +// The method by Pavel Novikov based on +// https://johnnylee-sde.github.io/Fast-unsigned-integer-to-time-string/. +inline void write_digit2_separated(char* buf, unsigned a, unsigned b, + unsigned c, char sep) { + unsigned long long digits = + a | (b << 24) | (static_cast(c) << 48); + // Convert each value to BCD. + // We have x = a * 10 + b and we want to convert it to BCD y = a * 16 + b. + // The difference is + // y - x = a * 6 + // a can be found from x: + // a = floor(x / 10) + // then + // y = x + a * 6 = x + floor(x / 10) * 6 + // floor(x / 10) is (x * 205) >> 11 (needs 16 bits). + digits += (((digits * 205) >> 11) & 0x000f00000f00000f) * 6; + // Put low nibbles to high bytes and high nibbles to low bytes. + digits = ((digits & 0x00f00000f00000f0) >> 4) | + ((digits & 0x000f00000f00000f) << 8); + auto usep = static_cast(sep); + // Add ASCII '0' to each digit byte and insert separators. + digits |= 0x3030003030003030 | (usep << 16) | (usep << 40); + + constexpr const size_t len = 8; + if (const_check(is_big_endian())) { + char tmp[len]; + std::memcpy(tmp, &digits, len); + std::reverse_copy(tmp, tmp + len, buf); + } else { + std::memcpy(buf, &digits, len); + } +} + +template +FMT_CONSTEXPR inline auto get_units() -> const char* { + if (std::is_same::value) return "as"; + if (std::is_same::value) return "fs"; + if (std::is_same::value) return "ps"; + if (std::is_same::value) return "ns"; + if (std::is_same::value) return "µs"; + if (std::is_same::value) return "ms"; + if (std::is_same::value) return "cs"; + if (std::is_same::value) return "ds"; + if (std::is_same>::value) return "s"; + if (std::is_same::value) return "das"; + if (std::is_same::value) return "hs"; + if (std::is_same::value) return "ks"; + if (std::is_same::value) return "Ms"; + if (std::is_same::value) return "Gs"; + if (std::is_same::value) return "Ts"; + if (std::is_same::value) return "Ps"; + if (std::is_same::value) return "Es"; + if (std::is_same>::value) return "min"; + if (std::is_same>::value) return "h"; + if (std::is_same>::value) return "d"; + return nullptr; +} + +enum class numeric_system { + standard, + // Alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale. + alternative +}; + +// Glibc extensions for formatting numeric values. +enum class pad_type { + // Pad a numeric result string with zeros (the default). + zero, + // Do not pad a numeric result string. + none, + // Pad a numeric result string with spaces. + space, +}; + +template +auto write_padding(OutputIt out, pad_type pad, int width) -> OutputIt { + if (pad == pad_type::none) return out; + return detail::fill_n(out, width, pad == pad_type::space ? ' ' : '0'); +} + +template +auto write_padding(OutputIt out, pad_type pad) -> OutputIt { + if (pad != pad_type::none) *out++ = pad == pad_type::space ? ' ' : '0'; + return out; +} + +// Parses a put_time-like format string and invokes handler actions. +template +FMT_CONSTEXPR auto parse_chrono_format(const Char* begin, const Char* end, + Handler&& handler) -> const Char* { + if (begin == end || *begin == '}') return begin; + if (*begin != '%') FMT_THROW(format_error("invalid format")); + auto ptr = begin; + while (ptr != end) { + pad_type pad = pad_type::zero; + auto c = *ptr; + if (c == '}') break; + if (c != '%') { + ++ptr; + continue; + } + if (begin != ptr) handler.on_text(begin, ptr); + ++ptr; // consume '%' + if (ptr == end) FMT_THROW(format_error("invalid format")); + c = *ptr; + switch (c) { + case '_': + pad = pad_type::space; + ++ptr; + break; + case '-': + pad = pad_type::none; + ++ptr; + break; + } + if (ptr == end) FMT_THROW(format_error("invalid format")); + c = *ptr++; + switch (c) { + case '%': + handler.on_text(ptr - 1, ptr); + break; + case 'n': { + const Char newline[] = {'\n'}; + handler.on_text(newline, newline + 1); + break; + } + case 't': { + const Char tab[] = {'\t'}; + handler.on_text(tab, tab + 1); + break; + } + // Year: + case 'Y': + handler.on_year(numeric_system::standard); + break; + case 'y': + handler.on_short_year(numeric_system::standard); + break; + case 'C': + handler.on_century(numeric_system::standard); + break; + case 'G': + handler.on_iso_week_based_year(); + break; + case 'g': + handler.on_iso_week_based_short_year(); + break; + // Day of the week: + case 'a': + handler.on_abbr_weekday(); + break; + case 'A': + handler.on_full_weekday(); + break; + case 'w': + handler.on_dec0_weekday(numeric_system::standard); + break; + case 'u': + handler.on_dec1_weekday(numeric_system::standard); + break; + // Month: + case 'b': + case 'h': + handler.on_abbr_month(); + break; + case 'B': + handler.on_full_month(); + break; + case 'm': + handler.on_dec_month(numeric_system::standard); + break; + // Day of the year/month: + case 'U': + handler.on_dec0_week_of_year(numeric_system::standard, pad); + break; + case 'W': + handler.on_dec1_week_of_year(numeric_system::standard, pad); + break; + case 'V': + handler.on_iso_week_of_year(numeric_system::standard, pad); + break; + case 'j': + handler.on_day_of_year(); + break; + case 'd': + handler.on_day_of_month(numeric_system::standard, pad); + break; + case 'e': + handler.on_day_of_month(numeric_system::standard, pad_type::space); + break; + // Hour, minute, second: + case 'H': + handler.on_24_hour(numeric_system::standard, pad); + break; + case 'I': + handler.on_12_hour(numeric_system::standard, pad); + break; + case 'M': + handler.on_minute(numeric_system::standard, pad); + break; + case 'S': + handler.on_second(numeric_system::standard, pad); + break; + // Other: + case 'c': + handler.on_datetime(numeric_system::standard); + break; + case 'x': + handler.on_loc_date(numeric_system::standard); + break; + case 'X': + handler.on_loc_time(numeric_system::standard); + break; + case 'D': + handler.on_us_date(); + break; + case 'F': + handler.on_iso_date(); + break; + case 'r': + handler.on_12_hour_time(); + break; + case 'R': + handler.on_24_hour_time(); + break; + case 'T': + handler.on_iso_time(); + break; + case 'p': + handler.on_am_pm(); + break; + case 'Q': + handler.on_duration_value(); + break; + case 'q': + handler.on_duration_unit(); + break; + case 'z': + handler.on_utc_offset(numeric_system::standard); + break; + case 'Z': + handler.on_tz_name(); + break; + // Alternative representation: + case 'E': { + if (ptr == end) FMT_THROW(format_error("invalid format")); + c = *ptr++; + switch (c) { + case 'Y': + handler.on_year(numeric_system::alternative); + break; + case 'y': + handler.on_offset_year(); + break; + case 'C': + handler.on_century(numeric_system::alternative); + break; + case 'c': + handler.on_datetime(numeric_system::alternative); + break; + case 'x': + handler.on_loc_date(numeric_system::alternative); + break; + case 'X': + handler.on_loc_time(numeric_system::alternative); + break; + case 'z': + handler.on_utc_offset(numeric_system::alternative); + break; + default: + FMT_THROW(format_error("invalid format")); + } + break; + } + case 'O': + if (ptr == end) FMT_THROW(format_error("invalid format")); + c = *ptr++; + switch (c) { + case 'y': + handler.on_short_year(numeric_system::alternative); + break; + case 'm': + handler.on_dec_month(numeric_system::alternative); + break; + case 'U': + handler.on_dec0_week_of_year(numeric_system::alternative, pad); + break; + case 'W': + handler.on_dec1_week_of_year(numeric_system::alternative, pad); + break; + case 'V': + handler.on_iso_week_of_year(numeric_system::alternative, pad); + break; + case 'd': + handler.on_day_of_month(numeric_system::alternative, pad); + break; + case 'e': + handler.on_day_of_month(numeric_system::alternative, pad_type::space); + break; + case 'w': + handler.on_dec0_weekday(numeric_system::alternative); + break; + case 'u': + handler.on_dec1_weekday(numeric_system::alternative); + break; + case 'H': + handler.on_24_hour(numeric_system::alternative, pad); + break; + case 'I': + handler.on_12_hour(numeric_system::alternative, pad); + break; + case 'M': + handler.on_minute(numeric_system::alternative, pad); + break; + case 'S': + handler.on_second(numeric_system::alternative, pad); + break; + case 'z': + handler.on_utc_offset(numeric_system::alternative); + break; + default: + FMT_THROW(format_error("invalid format")); + } + break; + default: + FMT_THROW(format_error("invalid format")); + } + begin = ptr; + } + if (begin != ptr) handler.on_text(begin, ptr); + return ptr; +} + +template struct null_chrono_spec_handler { + FMT_CONSTEXPR void unsupported() { + static_cast(this)->unsupported(); + } + FMT_CONSTEXPR void on_year(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_short_year(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_offset_year() { unsupported(); } + FMT_CONSTEXPR void on_century(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_iso_week_based_year() { unsupported(); } + FMT_CONSTEXPR void on_iso_week_based_short_year() { unsupported(); } + FMT_CONSTEXPR void on_abbr_weekday() { unsupported(); } + FMT_CONSTEXPR void on_full_weekday() { unsupported(); } + FMT_CONSTEXPR void on_dec0_weekday(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_dec1_weekday(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_abbr_month() { unsupported(); } + FMT_CONSTEXPR void on_full_month() { unsupported(); } + FMT_CONSTEXPR void on_dec_month(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_dec0_week_of_year(numeric_system, pad_type) { + unsupported(); + } + FMT_CONSTEXPR void on_dec1_week_of_year(numeric_system, pad_type) { + unsupported(); + } + FMT_CONSTEXPR void on_iso_week_of_year(numeric_system, pad_type) { + unsupported(); + } + FMT_CONSTEXPR void on_day_of_year() { unsupported(); } + FMT_CONSTEXPR void on_day_of_month(numeric_system, pad_type) { + unsupported(); + } + FMT_CONSTEXPR void on_24_hour(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_12_hour(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_minute(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_second(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_datetime(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_loc_date(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_loc_time(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_us_date() { unsupported(); } + FMT_CONSTEXPR void on_iso_date() { unsupported(); } + FMT_CONSTEXPR void on_12_hour_time() { unsupported(); } + FMT_CONSTEXPR void on_24_hour_time() { unsupported(); } + FMT_CONSTEXPR void on_iso_time() { unsupported(); } + FMT_CONSTEXPR void on_am_pm() { unsupported(); } + FMT_CONSTEXPR void on_duration_value() { unsupported(); } + FMT_CONSTEXPR void on_duration_unit() { unsupported(); } + FMT_CONSTEXPR void on_utc_offset(numeric_system) { unsupported(); } + FMT_CONSTEXPR void on_tz_name() { unsupported(); } +}; + +struct tm_format_checker : null_chrono_spec_handler { + FMT_NORETURN void unsupported() { FMT_THROW(format_error("no format")); } + + template + FMT_CONSTEXPR void on_text(const Char*, const Char*) {} + FMT_CONSTEXPR void on_year(numeric_system) {} + FMT_CONSTEXPR void on_short_year(numeric_system) {} + FMT_CONSTEXPR void on_offset_year() {} + FMT_CONSTEXPR void on_century(numeric_system) {} + FMT_CONSTEXPR void on_iso_week_based_year() {} + FMT_CONSTEXPR void on_iso_week_based_short_year() {} + FMT_CONSTEXPR void on_abbr_weekday() {} + FMT_CONSTEXPR void on_full_weekday() {} + FMT_CONSTEXPR void on_dec0_weekday(numeric_system) {} + FMT_CONSTEXPR void on_dec1_weekday(numeric_system) {} + FMT_CONSTEXPR void on_abbr_month() {} + FMT_CONSTEXPR void on_full_month() {} + FMT_CONSTEXPR void on_dec_month(numeric_system) {} + FMT_CONSTEXPR void on_dec0_week_of_year(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_dec1_week_of_year(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_iso_week_of_year(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_day_of_year() {} + FMT_CONSTEXPR void on_day_of_month(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_second(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_datetime(numeric_system) {} + FMT_CONSTEXPR void on_loc_date(numeric_system) {} + FMT_CONSTEXPR void on_loc_time(numeric_system) {} + FMT_CONSTEXPR void on_us_date() {} + FMT_CONSTEXPR void on_iso_date() {} + FMT_CONSTEXPR void on_12_hour_time() {} + FMT_CONSTEXPR void on_24_hour_time() {} + FMT_CONSTEXPR void on_iso_time() {} + FMT_CONSTEXPR void on_am_pm() {} + FMT_CONSTEXPR void on_utc_offset(numeric_system) {} + FMT_CONSTEXPR void on_tz_name() {} +}; + +inline auto tm_wday_full_name(int wday) -> const char* { + static constexpr const char* full_name_list[] = { + "Sunday", "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday"}; + return wday >= 0 && wday <= 6 ? full_name_list[wday] : "?"; +} +inline auto tm_wday_short_name(int wday) -> const char* { + static constexpr const char* short_name_list[] = {"Sun", "Mon", "Tue", "Wed", + "Thu", "Fri", "Sat"}; + return wday >= 0 && wday <= 6 ? short_name_list[wday] : "???"; +} + +inline auto tm_mon_full_name(int mon) -> const char* { + static constexpr const char* full_name_list[] = { + "January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December"}; + return mon >= 0 && mon <= 11 ? full_name_list[mon] : "?"; +} +inline auto tm_mon_short_name(int mon) -> const char* { + static constexpr const char* short_name_list[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", + }; + return mon >= 0 && mon <= 11 ? short_name_list[mon] : "???"; +} + +template +struct has_member_data_tm_gmtoff : std::false_type {}; +template +struct has_member_data_tm_gmtoff> + : std::true_type {}; + +template +struct has_member_data_tm_zone : std::false_type {}; +template +struct has_member_data_tm_zone> + : std::true_type {}; + +#if FMT_USE_TZSET +inline void tzset_once() { + static bool init = []() -> bool { + _tzset(); + return true; + }(); + ignore_unused(init); +} +#endif + +// Converts value to Int and checks that it's in the range [0, upper). +template ::value)> +inline auto to_nonnegative_int(T value, Int upper) -> Int { + if (!std::is_unsigned::value && + (value < 0 || to_unsigned(value) > to_unsigned(upper))) { + FMT_THROW(fmt::format_error("chrono value is out of range")); + } + return static_cast(value); +} +template ::value)> +inline auto to_nonnegative_int(T value, Int upper) -> Int { + auto int_value = static_cast(value); + if (int_value < 0 || value > static_cast(upper)) + FMT_THROW(format_error("invalid value")); + return int_value; +} + +constexpr auto pow10(std::uint32_t n) -> long long { + return n == 0 ? 1 : 10 * pow10(n - 1); +} + +// Counts the number of fractional digits in the range [0, 18] according to the +// C++20 spec. If more than 18 fractional digits are required then returns 6 for +// microseconds precision. +template () / 10)> +struct count_fractional_digits { + static constexpr int value = + Num % Den == 0 ? N : count_fractional_digits::value; +}; + +// Base case that doesn't instantiate any more templates +// in order to avoid overflow. +template +struct count_fractional_digits { + static constexpr int value = (Num % Den == 0) ? N : 6; +}; + +// Format subseconds which are given as an integer type with an appropriate +// number of digits. +template +void write_fractional_seconds(OutputIt& out, Duration d, int precision = -1) { + constexpr auto num_fractional_digits = + count_fractional_digits::value; + + using subsecond_precision = std::chrono::duration< + typename std::common_type::type, + std::ratio<1, detail::pow10(num_fractional_digits)>>; + + const auto fractional = d - fmt_duration_cast(d); + const auto subseconds = + std::chrono::treat_as_floating_point< + typename subsecond_precision::rep>::value + ? fractional.count() + : fmt_duration_cast(fractional).count(); + auto n = static_cast>(subseconds); + const int num_digits = detail::count_digits(n); + + int leading_zeroes = (std::max)(0, num_fractional_digits - num_digits); + if (precision < 0) { + FMT_ASSERT(!std::is_floating_point::value, ""); + if (std::ratio_less::value) { + *out++ = '.'; + out = detail::fill_n(out, leading_zeroes, '0'); + out = format_decimal(out, n, num_digits).end; + } + } else if (precision > 0) { + *out++ = '.'; + leading_zeroes = (std::min)(leading_zeroes, precision); + int remaining = precision - leading_zeroes; + out = detail::fill_n(out, leading_zeroes, '0'); + if (remaining < num_digits) { + int num_truncated_digits = num_digits - remaining; + n /= to_unsigned(detail::pow10(to_unsigned(num_truncated_digits))); + if (n) { + out = format_decimal(out, n, remaining).end; + } + return; + } + if (n) { + out = format_decimal(out, n, num_digits).end; + remaining -= num_digits; + } + out = detail::fill_n(out, remaining, '0'); + } +} + +// Format subseconds which are given as a floating point type with an +// appropriate number of digits. We cannot pass the Duration here, as we +// explicitly need to pass the Rep value in the chrono_formatter. +template +void write_floating_seconds(memory_buffer& buf, Duration duration, + int num_fractional_digits = -1) { + using rep = typename Duration::rep; + FMT_ASSERT(std::is_floating_point::value, ""); + + auto val = duration.count(); + + if (num_fractional_digits < 0) { + // For `std::round` with fallback to `round`: + // On some toolchains `std::round` is not available (e.g. GCC 6). + using namespace std; + num_fractional_digits = + count_fractional_digits::value; + if (num_fractional_digits < 6 && static_cast(round(val)) != val) + num_fractional_digits = 6; + } + + fmt::format_to(std::back_inserter(buf), FMT_STRING("{:.{}f}"), + std::fmod(val * static_cast(Duration::period::num) / + static_cast(Duration::period::den), + static_cast(60)), + num_fractional_digits); +} + +template +class tm_writer { + private: + static constexpr int days_per_week = 7; + + const std::locale& loc_; + const bool is_classic_; + OutputIt out_; + const Duration* subsecs_; + const std::tm& tm_; + + auto tm_sec() const noexcept -> int { + FMT_ASSERT(tm_.tm_sec >= 0 && tm_.tm_sec <= 61, ""); + return tm_.tm_sec; + } + auto tm_min() const noexcept -> int { + FMT_ASSERT(tm_.tm_min >= 0 && tm_.tm_min <= 59, ""); + return tm_.tm_min; + } + auto tm_hour() const noexcept -> int { + FMT_ASSERT(tm_.tm_hour >= 0 && tm_.tm_hour <= 23, ""); + return tm_.tm_hour; + } + auto tm_mday() const noexcept -> int { + FMT_ASSERT(tm_.tm_mday >= 1 && tm_.tm_mday <= 31, ""); + return tm_.tm_mday; + } + auto tm_mon() const noexcept -> int { + FMT_ASSERT(tm_.tm_mon >= 0 && tm_.tm_mon <= 11, ""); + return tm_.tm_mon; + } + auto tm_year() const noexcept -> long long { return 1900ll + tm_.tm_year; } + auto tm_wday() const noexcept -> int { + FMT_ASSERT(tm_.tm_wday >= 0 && tm_.tm_wday <= 6, ""); + return tm_.tm_wday; + } + auto tm_yday() const noexcept -> int { + FMT_ASSERT(tm_.tm_yday >= 0 && tm_.tm_yday <= 365, ""); + return tm_.tm_yday; + } + + auto tm_hour12() const noexcept -> int { + const auto h = tm_hour(); + const auto z = h < 12 ? h : h - 12; + return z == 0 ? 12 : z; + } + + // POSIX and the C Standard are unclear or inconsistent about what %C and %y + // do if the year is negative or exceeds 9999. Use the convention that %C + // concatenated with %y yields the same output as %Y, and that %Y contains at + // least 4 characters, with more only if necessary. + auto split_year_lower(long long year) const noexcept -> int { + auto l = year % 100; + if (l < 0) l = -l; // l in [0, 99] + return static_cast(l); + } + + // Algorithm: https://en.wikipedia.org/wiki/ISO_week_date. + auto iso_year_weeks(long long curr_year) const noexcept -> int { + const auto prev_year = curr_year - 1; + const auto curr_p = + (curr_year + curr_year / 4 - curr_year / 100 + curr_year / 400) % + days_per_week; + const auto prev_p = + (prev_year + prev_year / 4 - prev_year / 100 + prev_year / 400) % + days_per_week; + return 52 + ((curr_p == 4 || prev_p == 3) ? 1 : 0); + } + auto iso_week_num(int tm_yday, int tm_wday) const noexcept -> int { + return (tm_yday + 11 - (tm_wday == 0 ? days_per_week : tm_wday)) / + days_per_week; + } + auto tm_iso_week_year() const noexcept -> long long { + const auto year = tm_year(); + const auto w = iso_week_num(tm_yday(), tm_wday()); + if (w < 1) return year - 1; + if (w > iso_year_weeks(year)) return year + 1; + return year; + } + auto tm_iso_week_of_year() const noexcept -> int { + const auto year = tm_year(); + const auto w = iso_week_num(tm_yday(), tm_wday()); + if (w < 1) return iso_year_weeks(year - 1); + if (w > iso_year_weeks(year)) return 1; + return w; + } + + void write1(int value) { + *out_++ = static_cast('0' + to_unsigned(value) % 10); + } + void write2(int value) { + const char* d = digits2(to_unsigned(value) % 100); + *out_++ = *d++; + *out_++ = *d; + } + void write2(int value, pad_type pad) { + unsigned int v = to_unsigned(value) % 100; + if (v >= 10) { + const char* d = digits2(v); + *out_++ = *d++; + *out_++ = *d; + } else { + out_ = detail::write_padding(out_, pad); + *out_++ = static_cast('0' + v); + } + } + + void write_year_extended(long long year) { + // At least 4 characters. + int width = 4; + if (year < 0) { + *out_++ = '-'; + year = 0 - year; + --width; + } + uint32_or_64_or_128_t n = to_unsigned(year); + const int num_digits = count_digits(n); + if (width > num_digits) + out_ = detail::fill_n(out_, width - num_digits, '0'); + out_ = format_decimal(out_, n, num_digits).end; + } + void write_year(long long year) { + if (year >= 0 && year < 10000) { + write2(static_cast(year / 100)); + write2(static_cast(year % 100)); + } else { + write_year_extended(year); + } + } + + void write_utc_offset(long offset, numeric_system ns) { + if (offset < 0) { + *out_++ = '-'; + offset = -offset; + } else { + *out_++ = '+'; + } + offset /= 60; + write2(static_cast(offset / 60)); + if (ns != numeric_system::standard) *out_++ = ':'; + write2(static_cast(offset % 60)); + } + template ::value)> + void format_utc_offset_impl(const T& tm, numeric_system ns) { + write_utc_offset(tm.tm_gmtoff, ns); + } + template ::value)> + void format_utc_offset_impl(const T& tm, numeric_system ns) { +#if defined(_WIN32) && defined(_UCRT) +# if FMT_USE_TZSET + tzset_once(); +# endif + long offset = 0; + _get_timezone(&offset); + if (tm.tm_isdst) { + long dstbias = 0; + _get_dstbias(&dstbias); + offset += dstbias; + } + write_utc_offset(-offset, ns); +#else + if (ns == numeric_system::standard) return format_localized('z'); + + // Extract timezone offset from timezone conversion functions. + std::tm gtm = tm; + std::time_t gt = std::mktime(>m); + std::tm ltm = gmtime(gt); + std::time_t lt = std::mktime(<m); + long offset = gt - lt; + write_utc_offset(offset, ns); +#endif + } + + template ::value)> + void format_tz_name_impl(const T& tm) { + if (is_classic_) + out_ = write_tm_str(out_, tm.tm_zone, loc_); + else + format_localized('Z'); + } + template ::value)> + void format_tz_name_impl(const T&) { + format_localized('Z'); + } + + void format_localized(char format, char modifier = 0) { + out_ = write(out_, tm_, loc_, format, modifier); + } + + public: + tm_writer(const std::locale& loc, OutputIt out, const std::tm& tm, + const Duration* subsecs = nullptr) + : loc_(loc), + is_classic_(loc_ == get_classic_locale()), + out_(out), + subsecs_(subsecs), + tm_(tm) {} + + auto out() const -> OutputIt { return out_; } + + FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) { + out_ = copy(begin, end, out_); + } + + void on_abbr_weekday() { + if (is_classic_) + out_ = write(out_, tm_wday_short_name(tm_wday())); + else + format_localized('a'); + } + void on_full_weekday() { + if (is_classic_) + out_ = write(out_, tm_wday_full_name(tm_wday())); + else + format_localized('A'); + } + void on_dec0_weekday(numeric_system ns) { + if (is_classic_ || ns == numeric_system::standard) return write1(tm_wday()); + format_localized('w', 'O'); + } + void on_dec1_weekday(numeric_system ns) { + if (is_classic_ || ns == numeric_system::standard) { + auto wday = tm_wday(); + write1(wday == 0 ? days_per_week : wday); + } else { + format_localized('u', 'O'); + } + } + + void on_abbr_month() { + if (is_classic_) + out_ = write(out_, tm_mon_short_name(tm_mon())); + else + format_localized('b'); + } + void on_full_month() { + if (is_classic_) + out_ = write(out_, tm_mon_full_name(tm_mon())); + else + format_localized('B'); + } + + void on_datetime(numeric_system ns) { + if (is_classic_) { + on_abbr_weekday(); + *out_++ = ' '; + on_abbr_month(); + *out_++ = ' '; + on_day_of_month(numeric_system::standard, pad_type::space); + *out_++ = ' '; + on_iso_time(); + *out_++ = ' '; + on_year(numeric_system::standard); + } else { + format_localized('c', ns == numeric_system::standard ? '\0' : 'E'); + } + } + void on_loc_date(numeric_system ns) { + if (is_classic_) + on_us_date(); + else + format_localized('x', ns == numeric_system::standard ? '\0' : 'E'); + } + void on_loc_time(numeric_system ns) { + if (is_classic_) + on_iso_time(); + else + format_localized('X', ns == numeric_system::standard ? '\0' : 'E'); + } + void on_us_date() { + char buf[8]; + write_digit2_separated(buf, to_unsigned(tm_mon() + 1), + to_unsigned(tm_mday()), + to_unsigned(split_year_lower(tm_year())), '/'); + out_ = copy(std::begin(buf), std::end(buf), out_); + } + void on_iso_date() { + auto year = tm_year(); + char buf[10]; + size_t offset = 0; + if (year >= 0 && year < 10000) { + copy2(buf, digits2(static_cast(year / 100))); + } else { + offset = 4; + write_year_extended(year); + year = 0; + } + write_digit2_separated(buf + 2, static_cast(year % 100), + to_unsigned(tm_mon() + 1), to_unsigned(tm_mday()), + '-'); + out_ = copy(std::begin(buf) + offset, std::end(buf), out_); + } + + void on_utc_offset(numeric_system ns) { format_utc_offset_impl(tm_, ns); } + void on_tz_name() { format_tz_name_impl(tm_); } + + void on_year(numeric_system ns) { + if (is_classic_ || ns == numeric_system::standard) + return write_year(tm_year()); + format_localized('Y', 'E'); + } + void on_short_year(numeric_system ns) { + if (is_classic_ || ns == numeric_system::standard) + return write2(split_year_lower(tm_year())); + format_localized('y', 'O'); + } + void on_offset_year() { + if (is_classic_) return write2(split_year_lower(tm_year())); + format_localized('y', 'E'); + } + + void on_century(numeric_system ns) { + if (is_classic_ || ns == numeric_system::standard) { + auto year = tm_year(); + auto upper = year / 100; + if (year >= -99 && year < 0) { + // Zero upper on negative year. + *out_++ = '-'; + *out_++ = '0'; + } else if (upper >= 0 && upper < 100) { + write2(static_cast(upper)); + } else { + out_ = write(out_, upper); + } + } else { + format_localized('C', 'E'); + } + } + + void on_dec_month(numeric_system ns) { + if (is_classic_ || ns == numeric_system::standard) + return write2(tm_mon() + 1); + format_localized('m', 'O'); + } + + void on_dec0_week_of_year(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) + return write2((tm_yday() + days_per_week - tm_wday()) / days_per_week, + pad); + format_localized('U', 'O'); + } + void on_dec1_week_of_year(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) { + auto wday = tm_wday(); + write2((tm_yday() + days_per_week - + (wday == 0 ? (days_per_week - 1) : (wday - 1))) / + days_per_week, + pad); + } else { + format_localized('W', 'O'); + } + } + void on_iso_week_of_year(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) + return write2(tm_iso_week_of_year(), pad); + format_localized('V', 'O'); + } + + void on_iso_week_based_year() { write_year(tm_iso_week_year()); } + void on_iso_week_based_short_year() { + write2(split_year_lower(tm_iso_week_year())); + } + + void on_day_of_year() { + auto yday = tm_yday() + 1; + write1(yday / 100); + write2(yday % 100); + } + void on_day_of_month(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) + return write2(tm_mday(), pad); + format_localized('d', 'O'); + } + + void on_24_hour(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) + return write2(tm_hour(), pad); + format_localized('H', 'O'); + } + void on_12_hour(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) + return write2(tm_hour12(), pad); + format_localized('I', 'O'); + } + void on_minute(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) + return write2(tm_min(), pad); + format_localized('M', 'O'); + } + + void on_second(numeric_system ns, pad_type pad) { + if (is_classic_ || ns == numeric_system::standard) { + write2(tm_sec(), pad); + if (subsecs_) { + if (std::is_floating_point::value) { + auto buf = memory_buffer(); + write_floating_seconds(buf, *subsecs_); + if (buf.size() > 1) { + // Remove the leading "0", write something like ".123". + out_ = std::copy(buf.begin() + 1, buf.end(), out_); + } + } else { + write_fractional_seconds(out_, *subsecs_); + } + } + } else { + // Currently no formatting of subseconds when a locale is set. + format_localized('S', 'O'); + } + } + + void on_12_hour_time() { + if (is_classic_) { + char buf[8]; + write_digit2_separated(buf, to_unsigned(tm_hour12()), + to_unsigned(tm_min()), to_unsigned(tm_sec()), ':'); + out_ = copy(std::begin(buf), std::end(buf), out_); + *out_++ = ' '; + on_am_pm(); + } else { + format_localized('r'); + } + } + void on_24_hour_time() { + write2(tm_hour()); + *out_++ = ':'; + write2(tm_min()); + } + void on_iso_time() { + on_24_hour_time(); + *out_++ = ':'; + on_second(numeric_system::standard, pad_type::zero); + } + + void on_am_pm() { + if (is_classic_) { + *out_++ = tm_hour() < 12 ? 'A' : 'P'; + *out_++ = 'M'; + } else { + format_localized('p'); + } + } + + // These apply to chrono durations but not tm. + void on_duration_value() {} + void on_duration_unit() {} +}; + +struct chrono_format_checker : null_chrono_spec_handler { + bool has_precision_integral = false; + + FMT_NORETURN void unsupported() { FMT_THROW(format_error("no date")); } + + template + FMT_CONSTEXPR void on_text(const Char*, const Char*) {} + FMT_CONSTEXPR void on_day_of_year() {} + FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_second(numeric_system, pad_type) {} + FMT_CONSTEXPR void on_12_hour_time() {} + FMT_CONSTEXPR void on_24_hour_time() {} + FMT_CONSTEXPR void on_iso_time() {} + FMT_CONSTEXPR void on_am_pm() {} + FMT_CONSTEXPR void on_duration_value() const { + if (has_precision_integral) { + FMT_THROW(format_error("precision not allowed for this argument type")); + } + } + FMT_CONSTEXPR void on_duration_unit() {} +}; + +template ::value&& has_isfinite::value)> +inline auto isfinite(T) -> bool { + return true; +} + +template ::value)> +inline auto mod(T x, int y) -> T { + return x % static_cast(y); +} +template ::value)> +inline auto mod(T x, int y) -> T { + return std::fmod(x, static_cast(y)); +} + +// If T is an integral type, maps T to its unsigned counterpart, otherwise +// leaves it unchanged (unlike std::make_unsigned). +template ::value> +struct make_unsigned_or_unchanged { + using type = T; +}; + +template struct make_unsigned_or_unchanged { + using type = typename std::make_unsigned::type; +}; + +template ::value)> +inline auto get_milliseconds(std::chrono::duration d) + -> std::chrono::duration { + // this may overflow and/or the result may not fit in the + // target type. +#if FMT_SAFE_DURATION_CAST + using CommonSecondsType = + typename std::common_type::type; + const auto d_as_common = fmt_duration_cast(d); + const auto d_as_whole_seconds = + fmt_duration_cast(d_as_common); + // this conversion should be nonproblematic + const auto diff = d_as_common - d_as_whole_seconds; + const auto ms = + fmt_duration_cast>(diff); + return ms; +#else + auto s = fmt_duration_cast(d); + return fmt_duration_cast(d - s); +#endif +} + +template ::value)> +auto format_duration_value(OutputIt out, Rep val, int) -> OutputIt { + return write(out, val); +} + +template ::value)> +auto format_duration_value(OutputIt out, Rep val, int precision) -> OutputIt { + auto specs = format_specs(); + specs.precision = precision; + specs.type = + precision >= 0 ? presentation_type::fixed : presentation_type::general; + return write(out, val, specs); +} + +template +auto copy_unit(string_view unit, OutputIt out, Char) -> OutputIt { + return std::copy(unit.begin(), unit.end(), out); +} + +template +auto copy_unit(string_view unit, OutputIt out, wchar_t) -> OutputIt { + // This works when wchar_t is UTF-32 because units only contain characters + // that have the same representation in UTF-16 and UTF-32. + utf8_to_utf16 u(unit); + return std::copy(u.c_str(), u.c_str() + u.size(), out); +} + +template +auto format_duration_unit(OutputIt out) -> OutputIt { + if (const char* unit = get_units()) + return copy_unit(string_view(unit), out, Char()); + *out++ = '['; + out = write(out, Period::num); + if (const_check(Period::den != 1)) { + *out++ = '/'; + out = write(out, Period::den); + } + *out++ = ']'; + *out++ = 's'; + return out; +} + +class get_locale { + private: + union { + std::locale locale_; + }; + bool has_locale_ = false; + + public: + get_locale(bool localized, locale_ref loc) : has_locale_(localized) { +#ifndef FMT_STATIC_THOUSANDS_SEPARATOR + if (localized) + ::new (&locale_) std::locale(loc.template get()); +#endif + } + ~get_locale() { + if (has_locale_) locale_.~locale(); + } + operator const std::locale&() const { + return has_locale_ ? locale_ : get_classic_locale(); + } +}; + +template +struct chrono_formatter { + FormatContext& context; + OutputIt out; + int precision; + bool localized = false; + // rep is unsigned to avoid overflow. + using rep = + conditional_t::value && sizeof(Rep) < sizeof(int), + unsigned, typename make_unsigned_or_unchanged::type>; + rep val; + using seconds = std::chrono::duration; + seconds s; + using milliseconds = std::chrono::duration; + bool negative; + + using char_type = typename FormatContext::char_type; + using tm_writer_type = tm_writer; + + chrono_formatter(FormatContext& ctx, OutputIt o, + std::chrono::duration d) + : context(ctx), + out(o), + val(static_cast(d.count())), + negative(false) { + if (d.count() < 0) { + val = 0 - val; + negative = true; + } + + // this may overflow and/or the result may not fit in the + // target type. + // might need checked conversion (rep!=Rep) + s = fmt_duration_cast(std::chrono::duration(val)); + } + + // returns true if nan or inf, writes to out. + auto handle_nan_inf() -> bool { + if (isfinite(val)) { + return false; + } + if (isnan(val)) { + write_nan(); + return true; + } + // must be +-inf + if (val > 0) { + write_pinf(); + } else { + write_ninf(); + } + return true; + } + + auto days() const -> Rep { return static_cast(s.count() / 86400); } + auto hour() const -> Rep { + return static_cast(mod((s.count() / 3600), 24)); + } + + auto hour12() const -> Rep { + Rep hour = static_cast(mod((s.count() / 3600), 12)); + return hour <= 0 ? 12 : hour; + } + + auto minute() const -> Rep { + return static_cast(mod((s.count() / 60), 60)); + } + auto second() const -> Rep { return static_cast(mod(s.count(), 60)); } + + auto time() const -> std::tm { + auto time = std::tm(); + time.tm_hour = to_nonnegative_int(hour(), 24); + time.tm_min = to_nonnegative_int(minute(), 60); + time.tm_sec = to_nonnegative_int(second(), 60); + return time; + } + + void write_sign() { + if (negative) { + *out++ = '-'; + negative = false; + } + } + + void write(Rep value, int width, pad_type pad = pad_type::zero) { + write_sign(); + if (isnan(value)) return write_nan(); + uint32_or_64_or_128_t n = + to_unsigned(to_nonnegative_int(value, max_value())); + int num_digits = detail::count_digits(n); + if (width > num_digits) { + out = detail::write_padding(out, pad, width - num_digits); + } + out = format_decimal(out, n, num_digits).end; + } + + void write_nan() { std::copy_n("nan", 3, out); } + void write_pinf() { std::copy_n("inf", 3, out); } + void write_ninf() { std::copy_n("-inf", 4, out); } + + template + void format_tm(const tm& time, Callback cb, Args... args) { + if (isnan(val)) return write_nan(); + get_locale loc(localized, context.locale()); + auto w = tm_writer_type(loc, out, time); + (w.*cb)(args...); + out = w.out(); + } + + void on_text(const char_type* begin, const char_type* end) { + std::copy(begin, end, out); + } + + // These are not implemented because durations don't have date information. + void on_abbr_weekday() {} + void on_full_weekday() {} + void on_dec0_weekday(numeric_system) {} + void on_dec1_weekday(numeric_system) {} + void on_abbr_month() {} + void on_full_month() {} + void on_datetime(numeric_system) {} + void on_loc_date(numeric_system) {} + void on_loc_time(numeric_system) {} + void on_us_date() {} + void on_iso_date() {} + void on_utc_offset(numeric_system) {} + void on_tz_name() {} + void on_year(numeric_system) {} + void on_short_year(numeric_system) {} + void on_offset_year() {} + void on_century(numeric_system) {} + void on_iso_week_based_year() {} + void on_iso_week_based_short_year() {} + void on_dec_month(numeric_system) {} + void on_dec0_week_of_year(numeric_system, pad_type) {} + void on_dec1_week_of_year(numeric_system, pad_type) {} + void on_iso_week_of_year(numeric_system, pad_type) {} + void on_day_of_month(numeric_system, pad_type) {} + + void on_day_of_year() { + if (handle_nan_inf()) return; + write(days(), 0); + } + + void on_24_hour(numeric_system ns, pad_type pad) { + if (handle_nan_inf()) return; + + if (ns == numeric_system::standard) return write(hour(), 2, pad); + auto time = tm(); + time.tm_hour = to_nonnegative_int(hour(), 24); + format_tm(time, &tm_writer_type::on_24_hour, ns, pad); + } + + void on_12_hour(numeric_system ns, pad_type pad) { + if (handle_nan_inf()) return; + + if (ns == numeric_system::standard) return write(hour12(), 2, pad); + auto time = tm(); + time.tm_hour = to_nonnegative_int(hour12(), 12); + format_tm(time, &tm_writer_type::on_12_hour, ns, pad); + } + + void on_minute(numeric_system ns, pad_type pad) { + if (handle_nan_inf()) return; + + if (ns == numeric_system::standard) return write(minute(), 2, pad); + auto time = tm(); + time.tm_min = to_nonnegative_int(minute(), 60); + format_tm(time, &tm_writer_type::on_minute, ns, pad); + } + + void on_second(numeric_system ns, pad_type pad) { + if (handle_nan_inf()) return; + + if (ns == numeric_system::standard) { + if (std::is_floating_point::value) { + auto buf = memory_buffer(); + write_floating_seconds(buf, std::chrono::duration(val), + precision); + if (negative) *out++ = '-'; + if (buf.size() < 2 || buf[1] == '.') { + out = detail::write_padding(out, pad); + } + out = std::copy(buf.begin(), buf.end(), out); + } else { + write(second(), 2, pad); + write_fractional_seconds( + out, std::chrono::duration(val), precision); + } + return; + } + auto time = tm(); + time.tm_sec = to_nonnegative_int(second(), 60); + format_tm(time, &tm_writer_type::on_second, ns, pad); + } + + void on_12_hour_time() { + if (handle_nan_inf()) return; + format_tm(time(), &tm_writer_type::on_12_hour_time); + } + + void on_24_hour_time() { + if (handle_nan_inf()) { + *out++ = ':'; + handle_nan_inf(); + return; + } + + write(hour(), 2); + *out++ = ':'; + write(minute(), 2); + } + + void on_iso_time() { + on_24_hour_time(); + *out++ = ':'; + if (handle_nan_inf()) return; + on_second(numeric_system::standard, pad_type::zero); + } + + void on_am_pm() { + if (handle_nan_inf()) return; + format_tm(time(), &tm_writer_type::on_am_pm); + } + + void on_duration_value() { + if (handle_nan_inf()) return; + write_sign(); + out = format_duration_value(out, val, precision); + } + + void on_duration_unit() { + out = format_duration_unit(out); + } +}; + +} // namespace detail + +#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907 +using weekday = std::chrono::weekday; +using day = std::chrono::day; +using month = std::chrono::month; +using year = std::chrono::year; +using year_month_day = std::chrono::year_month_day; +#else +// A fallback version of weekday. +class weekday { + private: + unsigned char value_; + + public: + weekday() = default; + constexpr explicit weekday(unsigned wd) noexcept + : value_(static_cast(wd != 7 ? wd : 0)) {} + constexpr auto c_encoding() const noexcept -> unsigned { return value_; } +}; + +class day { + private: + unsigned char value_; + + public: + day() = default; + constexpr explicit day(unsigned d) noexcept + : value_(static_cast(d)) {} + constexpr explicit operator unsigned() const noexcept { return value_; } +}; + +class month { + private: + unsigned char value_; + + public: + month() = default; + constexpr explicit month(unsigned m) noexcept + : value_(static_cast(m)) {} + constexpr explicit operator unsigned() const noexcept { return value_; } +}; + +class year { + private: + int value_; + + public: + year() = default; + constexpr explicit year(int y) noexcept : value_(y) {} + constexpr explicit operator int() const noexcept { return value_; } +}; + +class year_month_day { + private: + fmt::year year_; + fmt::month month_; + fmt::day day_; + + public: + year_month_day() = default; + constexpr year_month_day(const year& y, const month& m, const day& d) noexcept + : year_(y), month_(m), day_(d) {} + constexpr auto year() const noexcept -> fmt::year { return year_; } + constexpr auto month() const noexcept -> fmt::month { return month_; } + constexpr auto day() const noexcept -> fmt::day { return day_; } +}; +#endif + +template +struct formatter : private formatter { + private: + bool localized_ = false; + bool use_tm_formatter_ = false; + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(), end = ctx.end(); + if (it != end && *it == 'L') { + ++it; + localized_ = true; + return it; + } + use_tm_formatter_ = it != end && *it != '}'; + return use_tm_formatter_ ? formatter::parse(ctx) : it; + } + + template + auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) { + auto time = std::tm(); + time.tm_wday = static_cast(wd.c_encoding()); + if (use_tm_formatter_) return formatter::format(time, ctx); + detail::get_locale loc(localized_, ctx.locale()); + auto w = detail::tm_writer(loc, ctx.out(), time); + w.on_abbr_weekday(); + return w.out(); + } +}; + +template +struct formatter : private formatter { + private: + bool use_tm_formatter_ = false; + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(), end = ctx.end(); + use_tm_formatter_ = it != end && *it != '}'; + return use_tm_formatter_ ? formatter::parse(ctx) : it; + } + + template + auto format(day d, FormatContext& ctx) const -> decltype(ctx.out()) { + auto time = std::tm(); + time.tm_mday = static_cast(static_cast(d)); + if (use_tm_formatter_) return formatter::format(time, ctx); + detail::get_locale loc(false, ctx.locale()); + auto w = detail::tm_writer(loc, ctx.out(), time); + w.on_day_of_month(detail::numeric_system::standard, detail::pad_type::zero); + return w.out(); + } +}; + +template +struct formatter : private formatter { + private: + bool localized_ = false; + bool use_tm_formatter_ = false; + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(), end = ctx.end(); + if (it != end && *it == 'L') { + ++it; + localized_ = true; + return it; + } + use_tm_formatter_ = it != end && *it != '}'; + return use_tm_formatter_ ? formatter::parse(ctx) : it; + } + + template + auto format(month m, FormatContext& ctx) const -> decltype(ctx.out()) { + auto time = std::tm(); + time.tm_mon = static_cast(static_cast(m)) - 1; + if (use_tm_formatter_) return formatter::format(time, ctx); + detail::get_locale loc(localized_, ctx.locale()); + auto w = detail::tm_writer(loc, ctx.out(), time); + w.on_abbr_month(); + return w.out(); + } +}; + +template +struct formatter : private formatter { + private: + bool use_tm_formatter_ = false; + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(), end = ctx.end(); + use_tm_formatter_ = it != end && *it != '}'; + return use_tm_formatter_ ? formatter::parse(ctx) : it; + } + + template + auto format(year y, FormatContext& ctx) const -> decltype(ctx.out()) { + auto time = std::tm(); + time.tm_year = static_cast(y) - 1900; + if (use_tm_formatter_) return formatter::format(time, ctx); + detail::get_locale loc(false, ctx.locale()); + auto w = detail::tm_writer(loc, ctx.out(), time); + w.on_year(detail::numeric_system::standard); + return w.out(); + } +}; + +template +struct formatter : private formatter { + private: + bool use_tm_formatter_ = false; + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(), end = ctx.end(); + use_tm_formatter_ = it != end && *it != '}'; + return use_tm_formatter_ ? formatter::parse(ctx) : it; + } + + template + auto format(year_month_day val, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto time = std::tm(); + time.tm_year = static_cast(val.year()) - 1900; + time.tm_mon = static_cast(static_cast(val.month())) - 1; + time.tm_mday = static_cast(static_cast(val.day())); + if (use_tm_formatter_) return formatter::format(time, ctx); + detail::get_locale loc(true, ctx.locale()); + auto w = detail::tm_writer(loc, ctx.out(), time); + w.on_iso_date(); + return w.out(); + } +}; + +template +struct formatter, Char> { + private: + format_specs specs_; + detail::arg_ref width_ref_; + detail::arg_ref precision_ref_; + bool localized_ = false; + basic_string_view format_str_; + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(), end = ctx.end(); + if (it == end || *it == '}') return it; + + it = detail::parse_align(it, end, specs_); + if (it == end) return it; + + it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx); + if (it == end) return it; + + auto checker = detail::chrono_format_checker(); + if (*it == '.') { + checker.has_precision_integral = !std::is_floating_point::value; + it = detail::parse_precision(it, end, specs_.precision, precision_ref_, + ctx); + } + if (it != end && *it == 'L') { + localized_ = true; + ++it; + } + end = detail::parse_chrono_format(it, end, checker); + format_str_ = {it, detail::to_unsigned(end - it)}; + return end; + } + + template + auto format(std::chrono::duration d, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto specs = specs_; + auto precision = specs.precision; + specs.precision = -1; + auto begin = format_str_.begin(), end = format_str_.end(); + // As a possible future optimization, we could avoid extra copying if width + // is not specified. + auto buf = basic_memory_buffer(); + auto out = std::back_inserter(buf); + detail::handle_dynamic_spec(specs.width, width_ref_, + ctx); + detail::handle_dynamic_spec(precision, + precision_ref_, ctx); + if (begin == end || *begin == '}') { + out = detail::format_duration_value(out, d.count(), precision); + detail::format_duration_unit(out); + } else { + using chrono_formatter = + detail::chrono_formatter; + auto f = chrono_formatter(ctx, out, d); + f.precision = precision; + f.localized = localized_; + detail::parse_chrono_format(begin, end, f); + } + return detail::write( + ctx.out(), basic_string_view(buf.data(), buf.size()), specs); + } +}; + +template +struct formatter, + Char> : formatter { + FMT_CONSTEXPR formatter() { + this->format_str_ = detail::string_literal{}; + } + + template + auto format(std::chrono::time_point val, + FormatContext& ctx) const -> decltype(ctx.out()) { + std::tm tm = gmtime(val); + using period = typename Duration::period; + if (detail::const_check( + period::num == 1 && period::den == 1 && + !std::is_floating_point::value)) { + return formatter::format(tm, ctx); + } + Duration epoch = val.time_since_epoch(); + Duration subsecs = detail::fmt_duration_cast( + epoch - detail::fmt_duration_cast(epoch)); + if (subsecs.count() < 0) { + auto second = + detail::fmt_duration_cast(std::chrono::seconds(1)); + if (tm.tm_sec != 0) + --tm.tm_sec; + else + tm = gmtime(val - second); + subsecs += detail::fmt_duration_cast(std::chrono::seconds(1)); + } + return formatter::do_format(tm, ctx, &subsecs); + } +}; + +#if FMT_USE_LOCAL_TIME +template +struct formatter, Char> + : formatter { + FMT_CONSTEXPR formatter() { + this->format_str_ = detail::string_literal{}; + } + + template + auto format(std::chrono::local_time val, FormatContext& ctx) const + -> decltype(ctx.out()) { + using period = typename Duration::period; + if (period::num != 1 || period::den != 1 || + std::is_floating_point::value) { + const auto epoch = val.time_since_epoch(); + const auto subsecs = detail::fmt_duration_cast( + epoch - detail::fmt_duration_cast(epoch)); + + return formatter::do_format(localtime(val), ctx, &subsecs); + } + + return formatter::format(localtime(val), ctx); + } +}; +#endif + +#if FMT_USE_UTC_TIME +template +struct formatter, + Char> + : formatter, + Char> { + template + auto format(std::chrono::time_point val, + FormatContext& ctx) const -> decltype(ctx.out()) { + return formatter< + std::chrono::time_point, + Char>::format(std::chrono::utc_clock::to_sys(val), ctx); + } +}; +#endif + +template struct formatter { + private: + format_specs specs_; + detail::arg_ref width_ref_; + + protected: + basic_string_view format_str_; + + template + auto do_format(const std::tm& tm, FormatContext& ctx, + const Duration* subsecs) const -> decltype(ctx.out()) { + auto specs = specs_; + auto buf = basic_memory_buffer(); + auto out = std::back_inserter(buf); + detail::handle_dynamic_spec(specs.width, width_ref_, + ctx); + + auto loc_ref = ctx.locale(); + detail::get_locale loc(static_cast(loc_ref), loc_ref); + auto w = + detail::tm_writer(loc, out, tm, subsecs); + detail::parse_chrono_format(format_str_.begin(), format_str_.end(), w); + return detail::write( + ctx.out(), basic_string_view(buf.data(), buf.size()), specs); + } + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(), end = ctx.end(); + if (it == end || *it == '}') return it; + + it = detail::parse_align(it, end, specs_); + if (it == end) return it; + + it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx); + if (it == end) return it; + + end = detail::parse_chrono_format(it, end, detail::tm_format_checker()); + // Replace the default format_str only if the new spec is not empty. + if (end != it) format_str_ = {it, detail::to_unsigned(end - it)}; + return end; + } + + template + auto format(const std::tm& tm, FormatContext& ctx) const + -> decltype(ctx.out()) { + return do_format(tm, ctx, nullptr); + } +}; + +FMT_END_EXPORT +FMT_END_NAMESPACE + +#endif // FMT_CHRONO_H_ diff --git a/include/spdlog/fmt/bundled/color.h b/include/spdlog/fmt/bundled/color.h new file mode 100644 index 0000000..f0e9dd9 --- /dev/null +++ b/include/spdlog/fmt/bundled/color.h @@ -0,0 +1,612 @@ +// Formatting library for C++ - color support +// +// Copyright (c) 2018 - present, Victor Zverovich and fmt contributors +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_COLOR_H_ +#define FMT_COLOR_H_ + +#include "format.h" + +FMT_BEGIN_NAMESPACE +FMT_BEGIN_EXPORT + +enum class color : uint32_t { + alice_blue = 0xF0F8FF, // rgb(240,248,255) + antique_white = 0xFAEBD7, // rgb(250,235,215) + aqua = 0x00FFFF, // rgb(0,255,255) + aquamarine = 0x7FFFD4, // rgb(127,255,212) + azure = 0xF0FFFF, // rgb(240,255,255) + beige = 0xF5F5DC, // rgb(245,245,220) + bisque = 0xFFE4C4, // rgb(255,228,196) + black = 0x000000, // rgb(0,0,0) + blanched_almond = 0xFFEBCD, // rgb(255,235,205) + blue = 0x0000FF, // rgb(0,0,255) + blue_violet = 0x8A2BE2, // rgb(138,43,226) + brown = 0xA52A2A, // rgb(165,42,42) + burly_wood = 0xDEB887, // rgb(222,184,135) + cadet_blue = 0x5F9EA0, // rgb(95,158,160) + chartreuse = 0x7FFF00, // rgb(127,255,0) + chocolate = 0xD2691E, // rgb(210,105,30) + coral = 0xFF7F50, // rgb(255,127,80) + cornflower_blue = 0x6495ED, // rgb(100,149,237) + cornsilk = 0xFFF8DC, // rgb(255,248,220) + crimson = 0xDC143C, // rgb(220,20,60) + cyan = 0x00FFFF, // rgb(0,255,255) + dark_blue = 0x00008B, // rgb(0,0,139) + dark_cyan = 0x008B8B, // rgb(0,139,139) + dark_golden_rod = 0xB8860B, // rgb(184,134,11) + dark_gray = 0xA9A9A9, // rgb(169,169,169) + dark_green = 0x006400, // rgb(0,100,0) + dark_khaki = 0xBDB76B, // rgb(189,183,107) + dark_magenta = 0x8B008B, // rgb(139,0,139) + dark_olive_green = 0x556B2F, // rgb(85,107,47) + dark_orange = 0xFF8C00, // rgb(255,140,0) + dark_orchid = 0x9932CC, // rgb(153,50,204) + dark_red = 0x8B0000, // rgb(139,0,0) + dark_salmon = 0xE9967A, // rgb(233,150,122) + dark_sea_green = 0x8FBC8F, // rgb(143,188,143) + dark_slate_blue = 0x483D8B, // rgb(72,61,139) + dark_slate_gray = 0x2F4F4F, // rgb(47,79,79) + dark_turquoise = 0x00CED1, // rgb(0,206,209) + dark_violet = 0x9400D3, // rgb(148,0,211) + deep_pink = 0xFF1493, // rgb(255,20,147) + deep_sky_blue = 0x00BFFF, // rgb(0,191,255) + dim_gray = 0x696969, // rgb(105,105,105) + dodger_blue = 0x1E90FF, // rgb(30,144,255) + fire_brick = 0xB22222, // rgb(178,34,34) + floral_white = 0xFFFAF0, // rgb(255,250,240) + forest_green = 0x228B22, // rgb(34,139,34) + fuchsia = 0xFF00FF, // rgb(255,0,255) + gainsboro = 0xDCDCDC, // rgb(220,220,220) + ghost_white = 0xF8F8FF, // rgb(248,248,255) + gold = 0xFFD700, // rgb(255,215,0) + golden_rod = 0xDAA520, // rgb(218,165,32) + gray = 0x808080, // rgb(128,128,128) + green = 0x008000, // rgb(0,128,0) + green_yellow = 0xADFF2F, // rgb(173,255,47) + honey_dew = 0xF0FFF0, // rgb(240,255,240) + hot_pink = 0xFF69B4, // rgb(255,105,180) + indian_red = 0xCD5C5C, // rgb(205,92,92) + indigo = 0x4B0082, // rgb(75,0,130) + ivory = 0xFFFFF0, // rgb(255,255,240) + khaki = 0xF0E68C, // rgb(240,230,140) + lavender = 0xE6E6FA, // rgb(230,230,250) + lavender_blush = 0xFFF0F5, // rgb(255,240,245) + lawn_green = 0x7CFC00, // rgb(124,252,0) + lemon_chiffon = 0xFFFACD, // rgb(255,250,205) + light_blue = 0xADD8E6, // rgb(173,216,230) + light_coral = 0xF08080, // rgb(240,128,128) + light_cyan = 0xE0FFFF, // rgb(224,255,255) + light_golden_rod_yellow = 0xFAFAD2, // rgb(250,250,210) + light_gray = 0xD3D3D3, // rgb(211,211,211) + light_green = 0x90EE90, // rgb(144,238,144) + light_pink = 0xFFB6C1, // rgb(255,182,193) + light_salmon = 0xFFA07A, // rgb(255,160,122) + light_sea_green = 0x20B2AA, // rgb(32,178,170) + light_sky_blue = 0x87CEFA, // rgb(135,206,250) + light_slate_gray = 0x778899, // rgb(119,136,153) + light_steel_blue = 0xB0C4DE, // rgb(176,196,222) + light_yellow = 0xFFFFE0, // rgb(255,255,224) + lime = 0x00FF00, // rgb(0,255,0) + lime_green = 0x32CD32, // rgb(50,205,50) + linen = 0xFAF0E6, // rgb(250,240,230) + magenta = 0xFF00FF, // rgb(255,0,255) + maroon = 0x800000, // rgb(128,0,0) + medium_aquamarine = 0x66CDAA, // rgb(102,205,170) + medium_blue = 0x0000CD, // rgb(0,0,205) + medium_orchid = 0xBA55D3, // rgb(186,85,211) + medium_purple = 0x9370DB, // rgb(147,112,219) + medium_sea_green = 0x3CB371, // rgb(60,179,113) + medium_slate_blue = 0x7B68EE, // rgb(123,104,238) + medium_spring_green = 0x00FA9A, // rgb(0,250,154) + medium_turquoise = 0x48D1CC, // rgb(72,209,204) + medium_violet_red = 0xC71585, // rgb(199,21,133) + midnight_blue = 0x191970, // rgb(25,25,112) + mint_cream = 0xF5FFFA, // rgb(245,255,250) + misty_rose = 0xFFE4E1, // rgb(255,228,225) + moccasin = 0xFFE4B5, // rgb(255,228,181) + navajo_white = 0xFFDEAD, // rgb(255,222,173) + navy = 0x000080, // rgb(0,0,128) + old_lace = 0xFDF5E6, // rgb(253,245,230) + olive = 0x808000, // rgb(128,128,0) + olive_drab = 0x6B8E23, // rgb(107,142,35) + orange = 0xFFA500, // rgb(255,165,0) + orange_red = 0xFF4500, // rgb(255,69,0) + orchid = 0xDA70D6, // rgb(218,112,214) + pale_golden_rod = 0xEEE8AA, // rgb(238,232,170) + pale_green = 0x98FB98, // rgb(152,251,152) + pale_turquoise = 0xAFEEEE, // rgb(175,238,238) + pale_violet_red = 0xDB7093, // rgb(219,112,147) + papaya_whip = 0xFFEFD5, // rgb(255,239,213) + peach_puff = 0xFFDAB9, // rgb(255,218,185) + peru = 0xCD853F, // rgb(205,133,63) + pink = 0xFFC0CB, // rgb(255,192,203) + plum = 0xDDA0DD, // rgb(221,160,221) + powder_blue = 0xB0E0E6, // rgb(176,224,230) + purple = 0x800080, // rgb(128,0,128) + rebecca_purple = 0x663399, // rgb(102,51,153) + red = 0xFF0000, // rgb(255,0,0) + rosy_brown = 0xBC8F8F, // rgb(188,143,143) + royal_blue = 0x4169E1, // rgb(65,105,225) + saddle_brown = 0x8B4513, // rgb(139,69,19) + salmon = 0xFA8072, // rgb(250,128,114) + sandy_brown = 0xF4A460, // rgb(244,164,96) + sea_green = 0x2E8B57, // rgb(46,139,87) + sea_shell = 0xFFF5EE, // rgb(255,245,238) + sienna = 0xA0522D, // rgb(160,82,45) + silver = 0xC0C0C0, // rgb(192,192,192) + sky_blue = 0x87CEEB, // rgb(135,206,235) + slate_blue = 0x6A5ACD, // rgb(106,90,205) + slate_gray = 0x708090, // rgb(112,128,144) + snow = 0xFFFAFA, // rgb(255,250,250) + spring_green = 0x00FF7F, // rgb(0,255,127) + steel_blue = 0x4682B4, // rgb(70,130,180) + tan = 0xD2B48C, // rgb(210,180,140) + teal = 0x008080, // rgb(0,128,128) + thistle = 0xD8BFD8, // rgb(216,191,216) + tomato = 0xFF6347, // rgb(255,99,71) + turquoise = 0x40E0D0, // rgb(64,224,208) + violet = 0xEE82EE, // rgb(238,130,238) + wheat = 0xF5DEB3, // rgb(245,222,179) + white = 0xFFFFFF, // rgb(255,255,255) + white_smoke = 0xF5F5F5, // rgb(245,245,245) + yellow = 0xFFFF00, // rgb(255,255,0) + yellow_green = 0x9ACD32 // rgb(154,205,50) +}; // enum class color + +enum class terminal_color : uint8_t { + black = 30, + red, + green, + yellow, + blue, + magenta, + cyan, + white, + bright_black = 90, + bright_red, + bright_green, + bright_yellow, + bright_blue, + bright_magenta, + bright_cyan, + bright_white +}; + +enum class emphasis : uint8_t { + bold = 1, + faint = 1 << 1, + italic = 1 << 2, + underline = 1 << 3, + blink = 1 << 4, + reverse = 1 << 5, + conceal = 1 << 6, + strikethrough = 1 << 7, +}; + +// rgb is a struct for red, green and blue colors. +// Using the name "rgb" makes some editors show the color in a tooltip. +struct rgb { + FMT_CONSTEXPR rgb() : r(0), g(0), b(0) {} + FMT_CONSTEXPR rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {} + FMT_CONSTEXPR rgb(uint32_t hex) + : r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b(hex & 0xFF) {} + FMT_CONSTEXPR rgb(color hex) + : r((uint32_t(hex) >> 16) & 0xFF), + g((uint32_t(hex) >> 8) & 0xFF), + b(uint32_t(hex) & 0xFF) {} + uint8_t r; + uint8_t g; + uint8_t b; +}; + +namespace detail { + +// color is a struct of either a rgb color or a terminal color. +struct color_type { + FMT_CONSTEXPR color_type() noexcept : is_rgb(), value{} {} + FMT_CONSTEXPR color_type(color rgb_color) noexcept : is_rgb(true), value{} { + value.rgb_color = static_cast(rgb_color); + } + FMT_CONSTEXPR color_type(rgb rgb_color) noexcept : is_rgb(true), value{} { + value.rgb_color = (static_cast(rgb_color.r) << 16) | + (static_cast(rgb_color.g) << 8) | rgb_color.b; + } + FMT_CONSTEXPR color_type(terminal_color term_color) noexcept + : is_rgb(), value{} { + value.term_color = static_cast(term_color); + } + bool is_rgb; + union color_union { + uint8_t term_color; + uint32_t rgb_color; + } value; +}; +} // namespace detail + +/// A text style consisting of foreground and background colors and emphasis. +class text_style { + public: + FMT_CONSTEXPR text_style(emphasis em = emphasis()) noexcept + : set_foreground_color(), set_background_color(), ems(em) {} + + FMT_CONSTEXPR auto operator|=(const text_style& rhs) -> text_style& { + if (!set_foreground_color) { + set_foreground_color = rhs.set_foreground_color; + foreground_color = rhs.foreground_color; + } else if (rhs.set_foreground_color) { + if (!foreground_color.is_rgb || !rhs.foreground_color.is_rgb) + report_error("can't OR a terminal color"); + foreground_color.value.rgb_color |= rhs.foreground_color.value.rgb_color; + } + + if (!set_background_color) { + set_background_color = rhs.set_background_color; + background_color = rhs.background_color; + } else if (rhs.set_background_color) { + if (!background_color.is_rgb || !rhs.background_color.is_rgb) + report_error("can't OR a terminal color"); + background_color.value.rgb_color |= rhs.background_color.value.rgb_color; + } + + ems = static_cast(static_cast(ems) | + static_cast(rhs.ems)); + return *this; + } + + friend FMT_CONSTEXPR auto operator|(text_style lhs, const text_style& rhs) + -> text_style { + return lhs |= rhs; + } + + FMT_CONSTEXPR auto has_foreground() const noexcept -> bool { + return set_foreground_color; + } + FMT_CONSTEXPR auto has_background() const noexcept -> bool { + return set_background_color; + } + FMT_CONSTEXPR auto has_emphasis() const noexcept -> bool { + return static_cast(ems) != 0; + } + FMT_CONSTEXPR auto get_foreground() const noexcept -> detail::color_type { + FMT_ASSERT(has_foreground(), "no foreground specified for this style"); + return foreground_color; + } + FMT_CONSTEXPR auto get_background() const noexcept -> detail::color_type { + FMT_ASSERT(has_background(), "no background specified for this style"); + return background_color; + } + FMT_CONSTEXPR auto get_emphasis() const noexcept -> emphasis { + FMT_ASSERT(has_emphasis(), "no emphasis specified for this style"); + return ems; + } + + private: + FMT_CONSTEXPR text_style(bool is_foreground, + detail::color_type text_color) noexcept + : set_foreground_color(), set_background_color(), ems() { + if (is_foreground) { + foreground_color = text_color; + set_foreground_color = true; + } else { + background_color = text_color; + set_background_color = true; + } + } + + friend FMT_CONSTEXPR auto fg(detail::color_type foreground) noexcept + -> text_style; + + friend FMT_CONSTEXPR auto bg(detail::color_type background) noexcept + -> text_style; + + detail::color_type foreground_color; + detail::color_type background_color; + bool set_foreground_color; + bool set_background_color; + emphasis ems; +}; + +/// Creates a text style from the foreground (text) color. +FMT_CONSTEXPR inline auto fg(detail::color_type foreground) noexcept + -> text_style { + return text_style(true, foreground); +} + +/// Creates a text style from the background color. +FMT_CONSTEXPR inline auto bg(detail::color_type background) noexcept + -> text_style { + return text_style(false, background); +} + +FMT_CONSTEXPR inline auto operator|(emphasis lhs, emphasis rhs) noexcept + -> text_style { + return text_style(lhs) | rhs; +} + +namespace detail { + +template struct ansi_color_escape { + FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color, + const char* esc) noexcept { + // If we have a terminal color, we need to output another escape code + // sequence. + if (!text_color.is_rgb) { + bool is_background = esc == string_view("\x1b[48;2;"); + uint32_t value = text_color.value.term_color; + // Background ASCII codes are the same as the foreground ones but with + // 10 more. + if (is_background) value += 10u; + + size_t index = 0; + buffer[index++] = static_cast('\x1b'); + buffer[index++] = static_cast('['); + + if (value >= 100u) { + buffer[index++] = static_cast('1'); + value %= 100u; + } + buffer[index++] = static_cast('0' + value / 10u); + buffer[index++] = static_cast('0' + value % 10u); + + buffer[index++] = static_cast('m'); + buffer[index++] = static_cast('\0'); + return; + } + + for (int i = 0; i < 7; i++) { + buffer[i] = static_cast(esc[i]); + } + rgb color(text_color.value.rgb_color); + to_esc(color.r, buffer + 7, ';'); + to_esc(color.g, buffer + 11, ';'); + to_esc(color.b, buffer + 15, 'm'); + buffer[19] = static_cast(0); + } + FMT_CONSTEXPR ansi_color_escape(emphasis em) noexcept { + uint8_t em_codes[num_emphases] = {}; + if (has_emphasis(em, emphasis::bold)) em_codes[0] = 1; + if (has_emphasis(em, emphasis::faint)) em_codes[1] = 2; + if (has_emphasis(em, emphasis::italic)) em_codes[2] = 3; + if (has_emphasis(em, emphasis::underline)) em_codes[3] = 4; + if (has_emphasis(em, emphasis::blink)) em_codes[4] = 5; + if (has_emphasis(em, emphasis::reverse)) em_codes[5] = 7; + if (has_emphasis(em, emphasis::conceal)) em_codes[6] = 8; + if (has_emphasis(em, emphasis::strikethrough)) em_codes[7] = 9; + + size_t index = 0; + for (size_t i = 0; i < num_emphases; ++i) { + if (!em_codes[i]) continue; + buffer[index++] = static_cast('\x1b'); + buffer[index++] = static_cast('['); + buffer[index++] = static_cast('0' + em_codes[i]); + buffer[index++] = static_cast('m'); + } + buffer[index++] = static_cast(0); + } + FMT_CONSTEXPR operator const Char*() const noexcept { return buffer; } + + FMT_CONSTEXPR auto begin() const noexcept -> const Char* { return buffer; } + FMT_CONSTEXPR20 auto end() const noexcept -> const Char* { + return buffer + basic_string_view(buffer).size(); + } + + private: + static constexpr size_t num_emphases = 8; + Char buffer[7u + 3u * num_emphases + 1u]; + + static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out, + char delimiter) noexcept { + out[0] = static_cast('0' + c / 100); + out[1] = static_cast('0' + c / 10 % 10); + out[2] = static_cast('0' + c % 10); + out[3] = static_cast(delimiter); + } + static FMT_CONSTEXPR auto has_emphasis(emphasis em, emphasis mask) noexcept + -> bool { + return static_cast(em) & static_cast(mask); + } +}; + +template +FMT_CONSTEXPR auto make_foreground_color(detail::color_type foreground) noexcept + -> ansi_color_escape { + return ansi_color_escape(foreground, "\x1b[38;2;"); +} + +template +FMT_CONSTEXPR auto make_background_color(detail::color_type background) noexcept + -> ansi_color_escape { + return ansi_color_escape(background, "\x1b[48;2;"); +} + +template +FMT_CONSTEXPR auto make_emphasis(emphasis em) noexcept + -> ansi_color_escape { + return ansi_color_escape(em); +} + +template inline void reset_color(buffer& buffer) { + auto reset_color = string_view("\x1b[0m"); + buffer.append(reset_color.begin(), reset_color.end()); +} + +template struct styled_arg : detail::view { + const T& value; + text_style style; + styled_arg(const T& v, text_style s) : value(v), style(s) {} +}; + +template +void vformat_to( + buffer& buf, const text_style& ts, basic_string_view format_str, + basic_format_args>> args) { + bool has_style = false; + if (ts.has_emphasis()) { + has_style = true; + auto emphasis = detail::make_emphasis(ts.get_emphasis()); + buf.append(emphasis.begin(), emphasis.end()); + } + if (ts.has_foreground()) { + has_style = true; + auto foreground = detail::make_foreground_color(ts.get_foreground()); + buf.append(foreground.begin(), foreground.end()); + } + if (ts.has_background()) { + has_style = true; + auto background = detail::make_background_color(ts.get_background()); + buf.append(background.begin(), background.end()); + } + detail::vformat_to(buf, format_str, args, {}); + if (has_style) detail::reset_color(buf); +} + +} // namespace detail + +inline void vprint(FILE* f, const text_style& ts, string_view fmt, + format_args args) { + auto buf = memory_buffer(); + detail::vformat_to(buf, ts, fmt, args); + print(f, FMT_STRING("{}"), string_view(buf.begin(), buf.size())); +} + +/** + * Formats a string and prints it to the specified file stream using ANSI + * escape sequences to specify text formatting. + * + * **Example**: + * + * fmt::print(fmt::emphasis::bold | fg(fmt::color::red), + * "Elapsed time: {0:.2f} seconds", 1.23); + */ +template +void print(FILE* f, const text_style& ts, format_string fmt, + T&&... args) { + vprint(f, ts, fmt, fmt::make_format_args(args...)); +} + +/** + * Formats a string and prints it to stdout using ANSI escape sequences to + * specify text formatting. + * + * **Example**: + * + * fmt::print(fmt::emphasis::bold | fg(fmt::color::red), + * "Elapsed time: {0:.2f} seconds", 1.23); + */ +template +void print(const text_style& ts, format_string fmt, T&&... args) { + return print(stdout, ts, fmt, std::forward(args)...); +} + +inline auto vformat(const text_style& ts, string_view fmt, format_args args) + -> std::string { + auto buf = memory_buffer(); + detail::vformat_to(buf, ts, fmt, args); + return fmt::to_string(buf); +} + +/** + * Formats arguments and returns the result as a string using ANSI escape + * sequences to specify text formatting. + * + * **Example**: + * + * ``` + * #include + * std::string message = fmt::format(fmt::emphasis::bold | fg(fmt::color::red), + * "The answer is {}", 42); + * ``` + */ +template +inline auto format(const text_style& ts, format_string fmt, T&&... args) + -> std::string { + return fmt::vformat(ts, fmt, fmt::make_format_args(args...)); +} + +/// Formats a string with the given text_style and writes the output to `out`. +template ::value)> +auto vformat_to(OutputIt out, const text_style& ts, string_view fmt, + format_args args) -> OutputIt { + auto&& buf = detail::get_buffer(out); + detail::vformat_to(buf, ts, fmt, args); + return detail::get_iterator(buf, out); +} + +/** + * Formats arguments with the given text style, writes the result to the output + * iterator `out` and returns the iterator past the end of the output range. + * + * **Example**: + * + * std::vector out; + * fmt::format_to(std::back_inserter(out), + * fmt::emphasis::bold | fg(fmt::color::red), "{}", 42); + */ +template ::value)> +inline auto format_to(OutputIt out, const text_style& ts, + format_string fmt, T&&... args) -> OutputIt { + return vformat_to(out, ts, fmt, fmt::make_format_args(args...)); +} + +template +struct formatter, Char> : formatter { + template + auto format(const detail::styled_arg& arg, FormatContext& ctx) const + -> decltype(ctx.out()) { + const auto& ts = arg.style; + const auto& value = arg.value; + auto out = ctx.out(); + + bool has_style = false; + if (ts.has_emphasis()) { + has_style = true; + auto emphasis = detail::make_emphasis(ts.get_emphasis()); + out = std::copy(emphasis.begin(), emphasis.end(), out); + } + if (ts.has_foreground()) { + has_style = true; + auto foreground = + detail::make_foreground_color(ts.get_foreground()); + out = std::copy(foreground.begin(), foreground.end(), out); + } + if (ts.has_background()) { + has_style = true; + auto background = + detail::make_background_color(ts.get_background()); + out = std::copy(background.begin(), background.end(), out); + } + out = formatter::format(value, ctx); + if (has_style) { + auto reset_color = string_view("\x1b[0m"); + out = std::copy(reset_color.begin(), reset_color.end(), out); + } + return out; + } +}; + +/** + * Returns an argument that will be formatted using ANSI escape sequences, + * to be used in a formatting function. + * + * **Example**: + * + * fmt::print("Elapsed time: {0:.2f} seconds", + * fmt::styled(1.23, fmt::fg(fmt::color::green) | + * fmt::bg(fmt::color::blue))); + */ +template +FMT_CONSTEXPR auto styled(const T& value, text_style ts) + -> detail::styled_arg> { + return detail::styled_arg>{value, ts}; +} + +FMT_END_EXPORT +FMT_END_NAMESPACE + +#endif // FMT_COLOR_H_ diff --git a/include/spdlog/fmt/bundled/compile.h b/include/spdlog/fmt/bundled/compile.h new file mode 100644 index 0000000..b2afc2c --- /dev/null +++ b/include/spdlog/fmt/bundled/compile.h @@ -0,0 +1,529 @@ +// Formatting library for C++ - experimental format string compilation +// +// Copyright (c) 2012 - present, Victor Zverovich and fmt contributors +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_COMPILE_H_ +#define FMT_COMPILE_H_ + +#ifndef FMT_MODULE +# include // std::back_inserter +#endif + +#include "format.h" + +FMT_BEGIN_NAMESPACE + +// A compile-time string which is compiled into fast formatting code. +FMT_EXPORT class compiled_string {}; + +namespace detail { + +template +FMT_CONSTEXPR inline auto copy(InputIt begin, InputIt end, counting_iterator it) + -> counting_iterator { + return it + (end - begin); +} + +template +struct is_compiled_string : std::is_base_of {}; + +/** + * Converts a string literal `s` into a format string that will be parsed at + * compile time and converted into efficient formatting code. Requires C++17 + * `constexpr if` compiler support. + * + * **Example**: + * + * // Converts 42 into std::string using the most efficient method and no + * // runtime format string processing. + * std::string s = fmt::format(FMT_COMPILE("{}"), 42); + */ +#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) +# define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::compiled_string, explicit) +#else +# define FMT_COMPILE(s) FMT_STRING(s) +#endif + +#if FMT_USE_NONTYPE_TEMPLATE_ARGS +template Str> +struct udl_compiled_string : compiled_string { + using char_type = Char; + explicit constexpr operator basic_string_view() const { + return {Str.data, N - 1}; + } +}; +#endif + +template +auto first(const T& value, const Tail&...) -> const T& { + return value; +} + +#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) +template struct type_list {}; + +// Returns a reference to the argument at index N from [first, rest...]. +template +constexpr const auto& get([[maybe_unused]] const T& first, + [[maybe_unused]] const Args&... rest) { + static_assert(N < 1 + sizeof...(Args), "index is out of bounds"); + if constexpr (N == 0) + return first; + else + return detail::get(rest...); +} + +template +constexpr int get_arg_index_by_name(basic_string_view name, + type_list) { + return get_arg_index_by_name(name); +} + +template struct get_type_impl; + +template struct get_type_impl> { + using type = + remove_cvref_t(std::declval()...))>; +}; + +template +using get_type = typename get_type_impl::type; + +template struct is_compiled_format : std::false_type {}; + +template struct text { + basic_string_view data; + using char_type = Char; + + template + constexpr OutputIt format(OutputIt out, const Args&...) const { + return write(out, data); + } +}; + +template +struct is_compiled_format> : std::true_type {}; + +template +constexpr text make_text(basic_string_view s, size_t pos, + size_t size) { + return {{&s[pos], size}}; +} + +template struct code_unit { + Char value; + using char_type = Char; + + template + constexpr OutputIt format(OutputIt out, const Args&...) const { + *out++ = value; + return out; + } +}; + +// This ensures that the argument type is convertible to `const T&`. +template +constexpr const T& get_arg_checked(const Args&... args) { + const auto& arg = detail::get(args...); + if constexpr (detail::is_named_arg>()) { + return arg.value; + } else { + return arg; + } +} + +template +struct is_compiled_format> : std::true_type {}; + +// A replacement field that refers to argument N. +template struct field { + using char_type = Char; + + template + constexpr OutputIt format(OutputIt out, const Args&... args) const { + const T& arg = get_arg_checked(args...); + if constexpr (std::is_convertible>::value) { + auto s = basic_string_view(arg); + return copy(s.begin(), s.end(), out); + } + return write(out, arg); + } +}; + +template +struct is_compiled_format> : std::true_type {}; + +// A replacement field that refers to argument with name. +template struct runtime_named_field { + using char_type = Char; + basic_string_view name; + + template + constexpr static bool try_format_argument( + OutputIt& out, + // [[maybe_unused]] due to unused-but-set-parameter warning in GCC 7,8,9 + [[maybe_unused]] basic_string_view arg_name, const T& arg) { + if constexpr (is_named_arg::type>::value) { + if (arg_name == arg.name) { + out = write(out, arg.value); + return true; + } + } + return false; + } + + template + constexpr OutputIt format(OutputIt out, const Args&... args) const { + bool found = (try_format_argument(out, name, args) || ...); + if (!found) { + FMT_THROW(format_error("argument with specified name is not found")); + } + return out; + } +}; + +template +struct is_compiled_format> : std::true_type {}; + +// A replacement field that refers to argument N and has format specifiers. +template struct spec_field { + using char_type = Char; + formatter fmt; + + template + constexpr FMT_INLINE OutputIt format(OutputIt out, + const Args&... args) const { + const auto& vargs = + fmt::make_format_args>(args...); + basic_format_context ctx(out, vargs); + return fmt.format(get_arg_checked(args...), ctx); + } +}; + +template +struct is_compiled_format> : std::true_type {}; + +template struct concat { + L lhs; + R rhs; + using char_type = typename L::char_type; + + template + constexpr OutputIt format(OutputIt out, const Args&... args) const { + out = lhs.format(out, args...); + return rhs.format(out, args...); + } +}; + +template +struct is_compiled_format> : std::true_type {}; + +template +constexpr concat make_concat(L lhs, R rhs) { + return {lhs, rhs}; +} + +struct unknown_format {}; + +template +constexpr size_t parse_text(basic_string_view str, size_t pos) { + for (size_t size = str.size(); pos != size; ++pos) { + if (str[pos] == '{' || str[pos] == '}') break; + } + return pos; +} + +template +constexpr auto compile_format_string(S fmt); + +template +constexpr auto parse_tail(T head, S fmt) { + if constexpr (POS != basic_string_view(fmt).size()) { + constexpr auto tail = compile_format_string(fmt); + if constexpr (std::is_same, + unknown_format>()) + return tail; + else + return make_concat(head, tail); + } else { + return head; + } +} + +template struct parse_specs_result { + formatter fmt; + size_t end; + int next_arg_id; +}; + +enum { manual_indexing_id = -1 }; + +template +constexpr parse_specs_result parse_specs(basic_string_view str, + size_t pos, int next_arg_id) { + str.remove_prefix(pos); + auto ctx = + compile_parse_context(str, max_value(), nullptr, next_arg_id); + auto f = formatter(); + auto end = f.parse(ctx); + return {f, pos + fmt::detail::to_unsigned(end - str.data()), + next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()}; +} + +template struct arg_id_handler { + arg_ref arg_id; + + constexpr int on_auto() { + FMT_ASSERT(false, "handler cannot be used with automatic indexing"); + return 0; + } + constexpr int on_index(int id) { + arg_id = arg_ref(id); + return 0; + } + constexpr int on_name(basic_string_view id) { + arg_id = arg_ref(id); + return 0; + } +}; + +template struct parse_arg_id_result { + arg_ref arg_id; + const Char* arg_id_end; +}; + +template +constexpr auto parse_arg_id(const Char* begin, const Char* end) { + auto handler = arg_id_handler{arg_ref{}}; + auto arg_id_end = parse_arg_id(begin, end, handler); + return parse_arg_id_result{handler.arg_id, arg_id_end}; +} + +template struct field_type { + using type = remove_cvref_t; +}; + +template +struct field_type::value>> { + using type = remove_cvref_t; +}; + +template +constexpr auto parse_replacement_field_then_tail(S fmt) { + using char_type = typename S::char_type; + constexpr auto str = basic_string_view(fmt); + constexpr char_type c = END_POS != str.size() ? str[END_POS] : char_type(); + if constexpr (c == '}') { + return parse_tail( + field::type, ARG_INDEX>(), fmt); + } else if constexpr (c != ':') { + FMT_THROW(format_error("expected ':'")); + } else { + constexpr auto result = parse_specs::type>( + str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID); + if constexpr (result.end >= str.size() || str[result.end] != '}') { + FMT_THROW(format_error("expected '}'")); + return 0; + } else { + return parse_tail( + spec_field::type, ARG_INDEX>{ + result.fmt}, + fmt); + } + } +} + +// Compiles a non-empty format string and returns the compiled representation +// or unknown_format() on unrecognized input. +template +constexpr auto compile_format_string(S fmt) { + using char_type = typename S::char_type; + constexpr auto str = basic_string_view(fmt); + if constexpr (str[POS] == '{') { + if constexpr (POS + 1 == str.size()) + FMT_THROW(format_error("unmatched '{' in format string")); + if constexpr (str[POS + 1] == '{') { + return parse_tail(make_text(str, POS, 1), fmt); + } else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') { + static_assert(ID != manual_indexing_id, + "cannot switch from manual to automatic argument indexing"); + constexpr auto next_id = + ID != manual_indexing_id ? ID + 1 : manual_indexing_id; + return parse_replacement_field_then_tail, Args, + POS + 1, ID, next_id>(fmt); + } else { + constexpr auto arg_id_result = + parse_arg_id(str.data() + POS + 1, str.data() + str.size()); + constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data(); + constexpr char_type c = + arg_id_end_pos != str.size() ? str[arg_id_end_pos] : char_type(); + static_assert(c == '}' || c == ':', "missing '}' in format string"); + if constexpr (arg_id_result.arg_id.kind == arg_id_kind::index) { + static_assert( + ID == manual_indexing_id || ID == 0, + "cannot switch from automatic to manual argument indexing"); + constexpr auto arg_index = arg_id_result.arg_id.val.index; + return parse_replacement_field_then_tail, + Args, arg_id_end_pos, + arg_index, manual_indexing_id>( + fmt); + } else if constexpr (arg_id_result.arg_id.kind == arg_id_kind::name) { + constexpr auto arg_index = + get_arg_index_by_name(arg_id_result.arg_id.val.name, Args{}); + if constexpr (arg_index >= 0) { + constexpr auto next_id = + ID != manual_indexing_id ? ID + 1 : manual_indexing_id; + return parse_replacement_field_then_tail< + decltype(get_type::value), Args, arg_id_end_pos, + arg_index, next_id>(fmt); + } else if constexpr (c == '}') { + return parse_tail( + runtime_named_field{arg_id_result.arg_id.val.name}, + fmt); + } else if constexpr (c == ':') { + return unknown_format(); // no type info for specs parsing + } + } + } + } else if constexpr (str[POS] == '}') { + if constexpr (POS + 1 == str.size()) + FMT_THROW(format_error("unmatched '}' in format string")); + return parse_tail(make_text(str, POS, 1), fmt); + } else { + constexpr auto end = parse_text(str, POS + 1); + if constexpr (end - POS > 1) { + return parse_tail(make_text(str, POS, end - POS), fmt); + } else { + return parse_tail(code_unit{str[POS]}, fmt); + } + } +} + +template ::value)> +constexpr auto compile(S fmt) { + constexpr auto str = basic_string_view(fmt); + if constexpr (str.size() == 0) { + return detail::make_text(str, 0, 0); + } else { + constexpr auto result = + detail::compile_format_string, 0, 0>(fmt); + return result; + } +} +#endif // defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) +} // namespace detail + +FMT_BEGIN_EXPORT + +#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) + +template ::value)> +FMT_INLINE std::basic_string format(const CompiledFormat& cf, + const Args&... args) { + auto s = std::basic_string(); + cf.format(std::back_inserter(s), args...); + return s; +} + +template ::value)> +constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf, + const Args&... args) { + return cf.format(out, args...); +} + +template ::value)> +FMT_INLINE std::basic_string format(const S&, + Args&&... args) { + if constexpr (std::is_same::value) { + constexpr auto str = basic_string_view(S()); + if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') { + const auto& first = detail::first(args...); + if constexpr (detail::is_named_arg< + remove_cvref_t>::value) { + return fmt::to_string(first.value); + } else { + return fmt::to_string(first); + } + } + } + constexpr auto compiled = detail::compile(S()); + if constexpr (std::is_same, + detail::unknown_format>()) { + return fmt::format( + static_cast>(S()), + std::forward(args)...); + } else { + return fmt::format(compiled, std::forward(args)...); + } +} + +template ::value)> +FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) { + constexpr auto compiled = detail::compile(S()); + if constexpr (std::is_same, + detail::unknown_format>()) { + return fmt::format_to( + out, static_cast>(S()), + std::forward(args)...); + } else { + return fmt::format_to(out, compiled, std::forward(args)...); + } +} +#endif + +template ::value)> +auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args) + -> format_to_n_result { + using traits = detail::fixed_buffer_traits; + auto buf = detail::iterator_buffer(out, n); + fmt::format_to(std::back_inserter(buf), fmt, std::forward(args)...); + return {buf.out(), buf.count()}; +} + +template ::value)> +FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args) + -> size_t { + return fmt::format_to(detail::counting_iterator(), fmt, args...).count(); +} + +template ::value)> +void print(std::FILE* f, const S& fmt, const Args&... args) { + memory_buffer buffer; + fmt::format_to(std::back_inserter(buffer), fmt, args...); + detail::print(f, {buffer.data(), buffer.size()}); +} + +template ::value)> +void print(const S& fmt, const Args&... args) { + print(stdout, fmt, args...); +} + +#if FMT_USE_NONTYPE_TEMPLATE_ARGS +inline namespace literals { +template constexpr auto operator""_cf() { + using char_t = remove_cvref_t; + return detail::udl_compiled_string(); +} +} // namespace literals +#endif + +FMT_END_EXPORT +FMT_END_NAMESPACE + +#endif // FMT_COMPILE_H_ diff --git a/include/spdlog/fmt/bundled/core.h b/include/spdlog/fmt/bundled/core.h new file mode 100644 index 0000000..8ca735f --- /dev/null +++ b/include/spdlog/fmt/bundled/core.h @@ -0,0 +1,5 @@ +// This file is only provided for compatibility and may be removed in future +// versions. Use fmt/base.h if you don't need fmt::format and fmt/format.h +// otherwise. + +#include "format.h" diff --git a/include/spdlog/fmt/bundled/fmt.license.rst b/include/spdlog/fmt/bundled/fmt.license.rst new file mode 100644 index 0000000..f0ec3db --- /dev/null +++ b/include/spdlog/fmt/bundled/fmt.license.rst @@ -0,0 +1,27 @@ +Copyright (c) 2012 - present, Victor Zverovich + +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. + +--- Optional exception to the license --- + +As an exception, if, as a result of your compiling your source code, portions +of this Software are embedded into a machine-executable object form of such +source code, you may redistribute such embedded portions in such object form +without including the above copyright and permission notices. diff --git a/include/spdlog/fmt/bundled/format-inl.h b/include/spdlog/fmt/bundled/format-inl.h new file mode 100644 index 0000000..a887483 --- /dev/null +++ b/include/spdlog/fmt/bundled/format-inl.h @@ -0,0 +1,1928 @@ +// Formatting library for C++ - implementation +// +// Copyright (c) 2012 - 2016, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_FORMAT_INL_H_ +#define FMT_FORMAT_INL_H_ + +#ifndef FMT_MODULE +# include +# include // errno +# include +# include +# include + +# if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) +# include +# endif +#endif + +#if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE) +# include // _isatty +#endif + +#include "format.h" + +FMT_BEGIN_NAMESPACE +namespace detail { + +FMT_FUNC void assert_fail(const char* file, int line, const char* message) { + // Use unchecked std::fprintf to avoid triggering another assertion when + // writing to stderr fails + std::fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message); + // Chosen instead of std::abort to satisfy Clang in CUDA mode during device + // code pass. + std::terminate(); +} + +FMT_FUNC void format_error_code(detail::buffer& out, int error_code, + string_view message) noexcept { + // Report error code making sure that the output fits into + // inline_buffer_size to avoid dynamic memory allocation and potential + // bad_alloc. + out.try_resize(0); + static const char SEP[] = ": "; + static const char ERROR_STR[] = "error "; + // Subtract 2 to account for terminating null characters in SEP and ERROR_STR. + size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2; + auto abs_value = static_cast>(error_code); + if (detail::is_negative(error_code)) { + abs_value = 0 - abs_value; + ++error_code_size; + } + error_code_size += detail::to_unsigned(detail::count_digits(abs_value)); + auto it = appender(out); + if (message.size() <= inline_buffer_size - error_code_size) + fmt::format_to(it, FMT_STRING("{}{}"), message, SEP); + fmt::format_to(it, FMT_STRING("{}{}"), ERROR_STR, error_code); + FMT_ASSERT(out.size() <= inline_buffer_size, ""); +} + +FMT_FUNC void report_error(format_func func, int error_code, + const char* message) noexcept { + memory_buffer full_message; + func(full_message, error_code, message); + // Don't use fwrite_fully because the latter may throw. + if (std::fwrite(full_message.data(), full_message.size(), 1, stderr) > 0) + std::fputc('\n', stderr); +} + +// A wrapper around fwrite that throws on error. +inline void fwrite_fully(const void* ptr, size_t count, FILE* stream) { + size_t written = std::fwrite(ptr, 1, count, stream); + if (written < count) + FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); +} + +#ifndef FMT_STATIC_THOUSANDS_SEPARATOR +template +locale_ref::locale_ref(const Locale& loc) : locale_(&loc) { + static_assert(std::is_same::value, ""); +} + +template auto locale_ref::get() const -> Locale { + static_assert(std::is_same::value, ""); + return locale_ ? *static_cast(locale_) : std::locale(); +} + +template +FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result { + auto& facet = std::use_facet>(loc.get()); + auto grouping = facet.grouping(); + auto thousands_sep = grouping.empty() ? Char() : facet.thousands_sep(); + return {std::move(grouping), thousands_sep}; +} +template +FMT_FUNC auto decimal_point_impl(locale_ref loc) -> Char { + return std::use_facet>(loc.get()) + .decimal_point(); +} +#else +template +FMT_FUNC auto thousands_sep_impl(locale_ref) -> thousands_sep_result { + return {"\03", FMT_STATIC_THOUSANDS_SEPARATOR}; +} +template FMT_FUNC Char decimal_point_impl(locale_ref) { + return '.'; +} +#endif + +FMT_FUNC auto write_loc(appender out, loc_value value, + const format_specs& specs, locale_ref loc) -> bool { +#ifdef FMT_STATIC_THOUSANDS_SEPARATOR + value.visit(loc_writer<>{ + out, specs, std::string(1, FMT_STATIC_THOUSANDS_SEPARATOR), "\3", "."}); + return true; +#else + auto locale = loc.get(); + // We cannot use the num_put facet because it may produce output in + // a wrong encoding. + using facet = format_facet; + if (std::has_facet(locale)) + return std::use_facet(locale).put(out, value, specs); + return facet(locale).put(out, value, specs); +#endif +} +} // namespace detail + +FMT_FUNC void report_error(const char* message) { + FMT_THROW(format_error(message)); +} + +template typename Locale::id format_facet::id; + +#ifndef FMT_STATIC_THOUSANDS_SEPARATOR +template format_facet::format_facet(Locale& loc) { + auto& numpunct = std::use_facet>(loc); + grouping_ = numpunct.grouping(); + if (!grouping_.empty()) separator_ = std::string(1, numpunct.thousands_sep()); +} + +template <> +FMT_API FMT_FUNC auto format_facet::do_put( + appender out, loc_value val, const format_specs& specs) const -> bool { + return val.visit( + detail::loc_writer<>{out, specs, separator_, grouping_, decimal_point_}); +} +#endif + +FMT_FUNC auto vsystem_error(int error_code, string_view fmt, format_args args) + -> std::system_error { + auto ec = std::error_code(error_code, std::generic_category()); + return std::system_error(ec, vformat(fmt, args)); +} + +namespace detail { + +template +inline auto operator==(basic_fp x, basic_fp y) -> bool { + return x.f == y.f && x.e == y.e; +} + +// Compilers should be able to optimize this into the ror instruction. +FMT_CONSTEXPR inline auto rotr(uint32_t n, uint32_t r) noexcept -> uint32_t { + r &= 31; + return (n >> r) | (n << (32 - r)); +} +FMT_CONSTEXPR inline auto rotr(uint64_t n, uint32_t r) noexcept -> uint64_t { + r &= 63; + return (n >> r) | (n << (64 - r)); +} + +// Implementation of Dragonbox algorithm: https://github.com/jk-jeon/dragonbox. +namespace dragonbox { +// Computes upper 64 bits of multiplication of a 32-bit unsigned integer and a +// 64-bit unsigned integer. +inline auto umul96_upper64(uint32_t x, uint64_t y) noexcept -> uint64_t { + return umul128_upper64(static_cast(x) << 32, y); +} + +// Computes lower 128 bits of multiplication of a 64-bit unsigned integer and a +// 128-bit unsigned integer. +inline auto umul192_lower128(uint64_t x, uint128_fallback y) noexcept + -> uint128_fallback { + uint64_t high = x * y.high(); + uint128_fallback high_low = umul128(x, y.low()); + return {high + high_low.high(), high_low.low()}; +} + +// Computes lower 64 bits of multiplication of a 32-bit unsigned integer and a +// 64-bit unsigned integer. +inline auto umul96_lower64(uint32_t x, uint64_t y) noexcept -> uint64_t { + return x * y; +} + +// Various fast log computations. +inline auto floor_log10_pow2_minus_log10_4_over_3(int e) noexcept -> int { + FMT_ASSERT(e <= 2936 && e >= -2985, "too large exponent"); + return (e * 631305 - 261663) >> 21; +} + +FMT_INLINE_VARIABLE constexpr struct { + uint32_t divisor; + int shift_amount; +} div_small_pow10_infos[] = {{10, 16}, {100, 16}}; + +// Replaces n by floor(n / pow(10, N)) returning true if and only if n is +// divisible by pow(10, N). +// Precondition: n <= pow(10, N + 1). +template +auto check_divisibility_and_divide_by_pow10(uint32_t& n) noexcept -> bool { + // The numbers below are chosen such that: + // 1. floor(n/d) = floor(nm / 2^k) where d=10 or d=100, + // 2. nm mod 2^k < m if and only if n is divisible by d, + // where m is magic_number, k is shift_amount + // and d is divisor. + // + // Item 1 is a common technique of replacing division by a constant with + // multiplication, see e.g. "Division by Invariant Integers Using + // Multiplication" by Granlund and Montgomery (1994). magic_number (m) is set + // to ceil(2^k/d) for large enough k. + // The idea for item 2 originates from Schubfach. + constexpr auto info = div_small_pow10_infos[N - 1]; + FMT_ASSERT(n <= info.divisor * 10, "n is too large"); + constexpr uint32_t magic_number = + (1u << info.shift_amount) / info.divisor + 1; + n *= magic_number; + const uint32_t comparison_mask = (1u << info.shift_amount) - 1; + bool result = (n & comparison_mask) < magic_number; + n >>= info.shift_amount; + return result; +} + +// Computes floor(n / pow(10, N)) for small n and N. +// Precondition: n <= pow(10, N + 1). +template auto small_division_by_pow10(uint32_t n) noexcept -> uint32_t { + constexpr auto info = div_small_pow10_infos[N - 1]; + FMT_ASSERT(n <= info.divisor * 10, "n is too large"); + constexpr uint32_t magic_number = + (1u << info.shift_amount) / info.divisor + 1; + return (n * magic_number) >> info.shift_amount; +} + +// Computes floor(n / 10^(kappa + 1)) (float) +inline auto divide_by_10_to_kappa_plus_1(uint32_t n) noexcept -> uint32_t { + // 1374389535 = ceil(2^37/100) + return static_cast((static_cast(n) * 1374389535) >> 37); +} +// Computes floor(n / 10^(kappa + 1)) (double) +inline auto divide_by_10_to_kappa_plus_1(uint64_t n) noexcept -> uint64_t { + // 2361183241434822607 = ceil(2^(64+7)/1000) + return umul128_upper64(n, 2361183241434822607ull) >> 7; +} + +// Various subroutines using pow10 cache +template struct cache_accessor; + +template <> struct cache_accessor { + using carrier_uint = float_info::carrier_uint; + using cache_entry_type = uint64_t; + + static auto get_cached_power(int k) noexcept -> uint64_t { + FMT_ASSERT(k >= float_info::min_k && k <= float_info::max_k, + "k is out of range"); + static constexpr const uint64_t pow10_significands[] = { + 0x81ceb32c4b43fcf5, 0xa2425ff75e14fc32, 0xcad2f7f5359a3b3f, + 0xfd87b5f28300ca0e, 0x9e74d1b791e07e49, 0xc612062576589ddb, + 0xf79687aed3eec552, 0x9abe14cd44753b53, 0xc16d9a0095928a28, + 0xf1c90080baf72cb2, 0x971da05074da7bef, 0xbce5086492111aeb, + 0xec1e4a7db69561a6, 0x9392ee8e921d5d08, 0xb877aa3236a4b44a, + 0xe69594bec44de15c, 0x901d7cf73ab0acda, 0xb424dc35095cd810, + 0xe12e13424bb40e14, 0x8cbccc096f5088cc, 0xafebff0bcb24aaff, + 0xdbe6fecebdedd5bf, 0x89705f4136b4a598, 0xabcc77118461cefd, + 0xd6bf94d5e57a42bd, 0x8637bd05af6c69b6, 0xa7c5ac471b478424, + 0xd1b71758e219652c, 0x83126e978d4fdf3c, 0xa3d70a3d70a3d70b, + 0xcccccccccccccccd, 0x8000000000000000, 0xa000000000000000, + 0xc800000000000000, 0xfa00000000000000, 0x9c40000000000000, + 0xc350000000000000, 0xf424000000000000, 0x9896800000000000, + 0xbebc200000000000, 0xee6b280000000000, 0x9502f90000000000, + 0xba43b74000000000, 0xe8d4a51000000000, 0x9184e72a00000000, + 0xb5e620f480000000, 0xe35fa931a0000000, 0x8e1bc9bf04000000, + 0xb1a2bc2ec5000000, 0xde0b6b3a76400000, 0x8ac7230489e80000, + 0xad78ebc5ac620000, 0xd8d726b7177a8000, 0x878678326eac9000, + 0xa968163f0a57b400, 0xd3c21bcecceda100, 0x84595161401484a0, + 0xa56fa5b99019a5c8, 0xcecb8f27f4200f3a, 0x813f3978f8940985, + 0xa18f07d736b90be6, 0xc9f2c9cd04674edf, 0xfc6f7c4045812297, + 0x9dc5ada82b70b59e, 0xc5371912364ce306, 0xf684df56c3e01bc7, + 0x9a130b963a6c115d, 0xc097ce7bc90715b4, 0xf0bdc21abb48db21, + 0x96769950b50d88f5, 0xbc143fa4e250eb32, 0xeb194f8e1ae525fe, + 0x92efd1b8d0cf37bf, 0xb7abc627050305ae, 0xe596b7b0c643c71a, + 0x8f7e32ce7bea5c70, 0xb35dbf821ae4f38c, 0xe0352f62a19e306f}; + return pow10_significands[k - float_info::min_k]; + } + + struct compute_mul_result { + carrier_uint result; + bool is_integer; + }; + struct compute_mul_parity_result { + bool parity; + bool is_integer; + }; + + static auto compute_mul(carrier_uint u, + const cache_entry_type& cache) noexcept + -> compute_mul_result { + auto r = umul96_upper64(u, cache); + return {static_cast(r >> 32), + static_cast(r) == 0}; + } + + static auto compute_delta(const cache_entry_type& cache, int beta) noexcept + -> uint32_t { + return static_cast(cache >> (64 - 1 - beta)); + } + + static auto compute_mul_parity(carrier_uint two_f, + const cache_entry_type& cache, + int beta) noexcept + -> compute_mul_parity_result { + FMT_ASSERT(beta >= 1, ""); + FMT_ASSERT(beta < 64, ""); + + auto r = umul96_lower64(two_f, cache); + return {((r >> (64 - beta)) & 1) != 0, + static_cast(r >> (32 - beta)) == 0}; + } + + static auto compute_left_endpoint_for_shorter_interval_case( + const cache_entry_type& cache, int beta) noexcept -> carrier_uint { + return static_cast( + (cache - (cache >> (num_significand_bits() + 2))) >> + (64 - num_significand_bits() - 1 - beta)); + } + + static auto compute_right_endpoint_for_shorter_interval_case( + const cache_entry_type& cache, int beta) noexcept -> carrier_uint { + return static_cast( + (cache + (cache >> (num_significand_bits() + 1))) >> + (64 - num_significand_bits() - 1 - beta)); + } + + static auto compute_round_up_for_shorter_interval_case( + const cache_entry_type& cache, int beta) noexcept -> carrier_uint { + return (static_cast( + cache >> (64 - num_significand_bits() - 2 - beta)) + + 1) / + 2; + } +}; + +template <> struct cache_accessor { + using carrier_uint = float_info::carrier_uint; + using cache_entry_type = uint128_fallback; + + static auto get_cached_power(int k) noexcept -> uint128_fallback { + FMT_ASSERT(k >= float_info::min_k && k <= float_info::max_k, + "k is out of range"); + + static constexpr const uint128_fallback pow10_significands[] = { +#if FMT_USE_FULL_CACHE_DRAGONBOX + {0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b}, + {0x9faacf3df73609b1, 0x77b191618c54e9ad}, + {0xc795830d75038c1d, 0xd59df5b9ef6a2418}, + {0xf97ae3d0d2446f25, 0x4b0573286b44ad1e}, + {0x9becce62836ac577, 0x4ee367f9430aec33}, + {0xc2e801fb244576d5, 0x229c41f793cda740}, + {0xf3a20279ed56d48a, 0x6b43527578c11110}, + {0x9845418c345644d6, 0x830a13896b78aaaa}, + {0xbe5691ef416bd60c, 0x23cc986bc656d554}, + {0xedec366b11c6cb8f, 0x2cbfbe86b7ec8aa9}, + {0x94b3a202eb1c3f39, 0x7bf7d71432f3d6aa}, + {0xb9e08a83a5e34f07, 0xdaf5ccd93fb0cc54}, + {0xe858ad248f5c22c9, 0xd1b3400f8f9cff69}, + {0x91376c36d99995be, 0x23100809b9c21fa2}, + {0xb58547448ffffb2d, 0xabd40a0c2832a78b}, + {0xe2e69915b3fff9f9, 0x16c90c8f323f516d}, + {0x8dd01fad907ffc3b, 0xae3da7d97f6792e4}, + {0xb1442798f49ffb4a, 0x99cd11cfdf41779d}, + {0xdd95317f31c7fa1d, 0x40405643d711d584}, + {0x8a7d3eef7f1cfc52, 0x482835ea666b2573}, + {0xad1c8eab5ee43b66, 0xda3243650005eed0}, + {0xd863b256369d4a40, 0x90bed43e40076a83}, + {0x873e4f75e2224e68, 0x5a7744a6e804a292}, + {0xa90de3535aaae202, 0x711515d0a205cb37}, + {0xd3515c2831559a83, 0x0d5a5b44ca873e04}, + {0x8412d9991ed58091, 0xe858790afe9486c3}, + {0xa5178fff668ae0b6, 0x626e974dbe39a873}, + {0xce5d73ff402d98e3, 0xfb0a3d212dc81290}, + {0x80fa687f881c7f8e, 0x7ce66634bc9d0b9a}, + {0xa139029f6a239f72, 0x1c1fffc1ebc44e81}, + {0xc987434744ac874e, 0xa327ffb266b56221}, + {0xfbe9141915d7a922, 0x4bf1ff9f0062baa9}, + {0x9d71ac8fada6c9b5, 0x6f773fc3603db4aa}, + {0xc4ce17b399107c22, 0xcb550fb4384d21d4}, + {0xf6019da07f549b2b, 0x7e2a53a146606a49}, + {0x99c102844f94e0fb, 0x2eda7444cbfc426e}, + {0xc0314325637a1939, 0xfa911155fefb5309}, + {0xf03d93eebc589f88, 0x793555ab7eba27cb}, + {0x96267c7535b763b5, 0x4bc1558b2f3458df}, + {0xbbb01b9283253ca2, 0x9eb1aaedfb016f17}, + {0xea9c227723ee8bcb, 0x465e15a979c1cadd}, + {0x92a1958a7675175f, 0x0bfacd89ec191eca}, + {0xb749faed14125d36, 0xcef980ec671f667c}, + {0xe51c79a85916f484, 0x82b7e12780e7401b}, + {0x8f31cc0937ae58d2, 0xd1b2ecb8b0908811}, + {0xb2fe3f0b8599ef07, 0x861fa7e6dcb4aa16}, + {0xdfbdcece67006ac9, 0x67a791e093e1d49b}, + {0x8bd6a141006042bd, 0xe0c8bb2c5c6d24e1}, + {0xaecc49914078536d, 0x58fae9f773886e19}, + {0xda7f5bf590966848, 0xaf39a475506a899f}, + {0x888f99797a5e012d, 0x6d8406c952429604}, + {0xaab37fd7d8f58178, 0xc8e5087ba6d33b84}, + {0xd5605fcdcf32e1d6, 0xfb1e4a9a90880a65}, + {0x855c3be0a17fcd26, 0x5cf2eea09a550680}, + {0xa6b34ad8c9dfc06f, 0xf42faa48c0ea481f}, + {0xd0601d8efc57b08b, 0xf13b94daf124da27}, + {0x823c12795db6ce57, 0x76c53d08d6b70859}, + {0xa2cb1717b52481ed, 0x54768c4b0c64ca6f}, + {0xcb7ddcdda26da268, 0xa9942f5dcf7dfd0a}, + {0xfe5d54150b090b02, 0xd3f93b35435d7c4d}, + {0x9efa548d26e5a6e1, 0xc47bc5014a1a6db0}, + {0xc6b8e9b0709f109a, 0x359ab6419ca1091c}, + {0xf867241c8cc6d4c0, 0xc30163d203c94b63}, + {0x9b407691d7fc44f8, 0x79e0de63425dcf1e}, + {0xc21094364dfb5636, 0x985915fc12f542e5}, + {0xf294b943e17a2bc4, 0x3e6f5b7b17b2939e}, + {0x979cf3ca6cec5b5a, 0xa705992ceecf9c43}, + {0xbd8430bd08277231, 0x50c6ff782a838354}, + {0xece53cec4a314ebd, 0xa4f8bf5635246429}, + {0x940f4613ae5ed136, 0x871b7795e136be9a}, + {0xb913179899f68584, 0x28e2557b59846e40}, + {0xe757dd7ec07426e5, 0x331aeada2fe589d0}, + {0x9096ea6f3848984f, 0x3ff0d2c85def7622}, + {0xb4bca50b065abe63, 0x0fed077a756b53aa}, + {0xe1ebce4dc7f16dfb, 0xd3e8495912c62895}, + {0x8d3360f09cf6e4bd, 0x64712dd7abbbd95d}, + {0xb080392cc4349dec, 0xbd8d794d96aacfb4}, + {0xdca04777f541c567, 0xecf0d7a0fc5583a1}, + {0x89e42caaf9491b60, 0xf41686c49db57245}, + {0xac5d37d5b79b6239, 0x311c2875c522ced6}, + {0xd77485cb25823ac7, 0x7d633293366b828c}, + {0x86a8d39ef77164bc, 0xae5dff9c02033198}, + {0xa8530886b54dbdeb, 0xd9f57f830283fdfd}, + {0xd267caa862a12d66, 0xd072df63c324fd7c}, + {0x8380dea93da4bc60, 0x4247cb9e59f71e6e}, + {0xa46116538d0deb78, 0x52d9be85f074e609}, + {0xcd795be870516656, 0x67902e276c921f8c}, + {0x806bd9714632dff6, 0x00ba1cd8a3db53b7}, + {0xa086cfcd97bf97f3, 0x80e8a40eccd228a5}, + {0xc8a883c0fdaf7df0, 0x6122cd128006b2ce}, + {0xfad2a4b13d1b5d6c, 0x796b805720085f82}, + {0x9cc3a6eec6311a63, 0xcbe3303674053bb1}, + {0xc3f490aa77bd60fc, 0xbedbfc4411068a9d}, + {0xf4f1b4d515acb93b, 0xee92fb5515482d45}, + {0x991711052d8bf3c5, 0x751bdd152d4d1c4b}, + {0xbf5cd54678eef0b6, 0xd262d45a78a0635e}, + {0xef340a98172aace4, 0x86fb897116c87c35}, + {0x9580869f0e7aac0e, 0xd45d35e6ae3d4da1}, + {0xbae0a846d2195712, 0x8974836059cca10a}, + {0xe998d258869facd7, 0x2bd1a438703fc94c}, + {0x91ff83775423cc06, 0x7b6306a34627ddd0}, + {0xb67f6455292cbf08, 0x1a3bc84c17b1d543}, + {0xe41f3d6a7377eeca, 0x20caba5f1d9e4a94}, + {0x8e938662882af53e, 0x547eb47b7282ee9d}, + {0xb23867fb2a35b28d, 0xe99e619a4f23aa44}, + {0xdec681f9f4c31f31, 0x6405fa00e2ec94d5}, + {0x8b3c113c38f9f37e, 0xde83bc408dd3dd05}, + {0xae0b158b4738705e, 0x9624ab50b148d446}, + {0xd98ddaee19068c76, 0x3badd624dd9b0958}, + {0x87f8a8d4cfa417c9, 0xe54ca5d70a80e5d7}, + {0xa9f6d30a038d1dbc, 0x5e9fcf4ccd211f4d}, + {0xd47487cc8470652b, 0x7647c32000696720}, + {0x84c8d4dfd2c63f3b, 0x29ecd9f40041e074}, + {0xa5fb0a17c777cf09, 0xf468107100525891}, + {0xcf79cc9db955c2cc, 0x7182148d4066eeb5}, + {0x81ac1fe293d599bf, 0xc6f14cd848405531}, + {0xa21727db38cb002f, 0xb8ada00e5a506a7d}, + {0xca9cf1d206fdc03b, 0xa6d90811f0e4851d}, + {0xfd442e4688bd304a, 0x908f4a166d1da664}, + {0x9e4a9cec15763e2e, 0x9a598e4e043287ff}, + {0xc5dd44271ad3cdba, 0x40eff1e1853f29fe}, + {0xf7549530e188c128, 0xd12bee59e68ef47d}, + {0x9a94dd3e8cf578b9, 0x82bb74f8301958cf}, + {0xc13a148e3032d6e7, 0xe36a52363c1faf02}, + {0xf18899b1bc3f8ca1, 0xdc44e6c3cb279ac2}, + {0x96f5600f15a7b7e5, 0x29ab103a5ef8c0ba}, + {0xbcb2b812db11a5de, 0x7415d448f6b6f0e8}, + {0xebdf661791d60f56, 0x111b495b3464ad22}, + {0x936b9fcebb25c995, 0xcab10dd900beec35}, + {0xb84687c269ef3bfb, 0x3d5d514f40eea743}, + {0xe65829b3046b0afa, 0x0cb4a5a3112a5113}, + {0x8ff71a0fe2c2e6dc, 0x47f0e785eaba72ac}, + {0xb3f4e093db73a093, 0x59ed216765690f57}, + {0xe0f218b8d25088b8, 0x306869c13ec3532d}, + {0x8c974f7383725573, 0x1e414218c73a13fc}, + {0xafbd2350644eeacf, 0xe5d1929ef90898fb}, + {0xdbac6c247d62a583, 0xdf45f746b74abf3a}, + {0x894bc396ce5da772, 0x6b8bba8c328eb784}, + {0xab9eb47c81f5114f, 0x066ea92f3f326565}, + {0xd686619ba27255a2, 0xc80a537b0efefebe}, + {0x8613fd0145877585, 0xbd06742ce95f5f37}, + {0xa798fc4196e952e7, 0x2c48113823b73705}, + {0xd17f3b51fca3a7a0, 0xf75a15862ca504c6}, + {0x82ef85133de648c4, 0x9a984d73dbe722fc}, + {0xa3ab66580d5fdaf5, 0xc13e60d0d2e0ebbb}, + {0xcc963fee10b7d1b3, 0x318df905079926a9}, + {0xffbbcfe994e5c61f, 0xfdf17746497f7053}, + {0x9fd561f1fd0f9bd3, 0xfeb6ea8bedefa634}, + {0xc7caba6e7c5382c8, 0xfe64a52ee96b8fc1}, + {0xf9bd690a1b68637b, 0x3dfdce7aa3c673b1}, + {0x9c1661a651213e2d, 0x06bea10ca65c084f}, + {0xc31bfa0fe5698db8, 0x486e494fcff30a63}, + {0xf3e2f893dec3f126, 0x5a89dba3c3efccfb}, + {0x986ddb5c6b3a76b7, 0xf89629465a75e01d}, + {0xbe89523386091465, 0xf6bbb397f1135824}, + {0xee2ba6c0678b597f, 0x746aa07ded582e2d}, + {0x94db483840b717ef, 0xa8c2a44eb4571cdd}, + {0xba121a4650e4ddeb, 0x92f34d62616ce414}, + {0xe896a0d7e51e1566, 0x77b020baf9c81d18}, + {0x915e2486ef32cd60, 0x0ace1474dc1d122f}, + {0xb5b5ada8aaff80b8, 0x0d819992132456bb}, + {0xe3231912d5bf60e6, 0x10e1fff697ed6c6a}, + {0x8df5efabc5979c8f, 0xca8d3ffa1ef463c2}, + {0xb1736b96b6fd83b3, 0xbd308ff8a6b17cb3}, + {0xddd0467c64bce4a0, 0xac7cb3f6d05ddbdf}, + {0x8aa22c0dbef60ee4, 0x6bcdf07a423aa96c}, + {0xad4ab7112eb3929d, 0x86c16c98d2c953c7}, + {0xd89d64d57a607744, 0xe871c7bf077ba8b8}, + {0x87625f056c7c4a8b, 0x11471cd764ad4973}, + {0xa93af6c6c79b5d2d, 0xd598e40d3dd89bd0}, + {0xd389b47879823479, 0x4aff1d108d4ec2c4}, + {0x843610cb4bf160cb, 0xcedf722a585139bb}, + {0xa54394fe1eedb8fe, 0xc2974eb4ee658829}, + {0xce947a3da6a9273e, 0x733d226229feea33}, + {0x811ccc668829b887, 0x0806357d5a3f5260}, + {0xa163ff802a3426a8, 0xca07c2dcb0cf26f8}, + {0xc9bcff6034c13052, 0xfc89b393dd02f0b6}, + {0xfc2c3f3841f17c67, 0xbbac2078d443ace3}, + {0x9d9ba7832936edc0, 0xd54b944b84aa4c0e}, + {0xc5029163f384a931, 0x0a9e795e65d4df12}, + {0xf64335bcf065d37d, 0x4d4617b5ff4a16d6}, + {0x99ea0196163fa42e, 0x504bced1bf8e4e46}, + {0xc06481fb9bcf8d39, 0xe45ec2862f71e1d7}, + {0xf07da27a82c37088, 0x5d767327bb4e5a4d}, + {0x964e858c91ba2655, 0x3a6a07f8d510f870}, + {0xbbe226efb628afea, 0x890489f70a55368c}, + {0xeadab0aba3b2dbe5, 0x2b45ac74ccea842f}, + {0x92c8ae6b464fc96f, 0x3b0b8bc90012929e}, + {0xb77ada0617e3bbcb, 0x09ce6ebb40173745}, + {0xe55990879ddcaabd, 0xcc420a6a101d0516}, + {0x8f57fa54c2a9eab6, 0x9fa946824a12232e}, + {0xb32df8e9f3546564, 0x47939822dc96abfa}, + {0xdff9772470297ebd, 0x59787e2b93bc56f8}, + {0x8bfbea76c619ef36, 0x57eb4edb3c55b65b}, + {0xaefae51477a06b03, 0xede622920b6b23f2}, + {0xdab99e59958885c4, 0xe95fab368e45ecee}, + {0x88b402f7fd75539b, 0x11dbcb0218ebb415}, + {0xaae103b5fcd2a881, 0xd652bdc29f26a11a}, + {0xd59944a37c0752a2, 0x4be76d3346f04960}, + {0x857fcae62d8493a5, 0x6f70a4400c562ddc}, + {0xa6dfbd9fb8e5b88e, 0xcb4ccd500f6bb953}, + {0xd097ad07a71f26b2, 0x7e2000a41346a7a8}, + {0x825ecc24c873782f, 0x8ed400668c0c28c9}, + {0xa2f67f2dfa90563b, 0x728900802f0f32fb}, + {0xcbb41ef979346bca, 0x4f2b40a03ad2ffba}, + {0xfea126b7d78186bc, 0xe2f610c84987bfa9}, + {0x9f24b832e6b0f436, 0x0dd9ca7d2df4d7ca}, + {0xc6ede63fa05d3143, 0x91503d1c79720dbc}, + {0xf8a95fcf88747d94, 0x75a44c6397ce912b}, + {0x9b69dbe1b548ce7c, 0xc986afbe3ee11abb}, + {0xc24452da229b021b, 0xfbe85badce996169}, + {0xf2d56790ab41c2a2, 0xfae27299423fb9c4}, + {0x97c560ba6b0919a5, 0xdccd879fc967d41b}, + {0xbdb6b8e905cb600f, 0x5400e987bbc1c921}, + {0xed246723473e3813, 0x290123e9aab23b69}, + {0x9436c0760c86e30b, 0xf9a0b6720aaf6522}, + {0xb94470938fa89bce, 0xf808e40e8d5b3e6a}, + {0xe7958cb87392c2c2, 0xb60b1d1230b20e05}, + {0x90bd77f3483bb9b9, 0xb1c6f22b5e6f48c3}, + {0xb4ecd5f01a4aa828, 0x1e38aeb6360b1af4}, + {0xe2280b6c20dd5232, 0x25c6da63c38de1b1}, + {0x8d590723948a535f, 0x579c487e5a38ad0f}, + {0xb0af48ec79ace837, 0x2d835a9df0c6d852}, + {0xdcdb1b2798182244, 0xf8e431456cf88e66}, + {0x8a08f0f8bf0f156b, 0x1b8e9ecb641b5900}, + {0xac8b2d36eed2dac5, 0xe272467e3d222f40}, + {0xd7adf884aa879177, 0x5b0ed81dcc6abb10}, + {0x86ccbb52ea94baea, 0x98e947129fc2b4ea}, + {0xa87fea27a539e9a5, 0x3f2398d747b36225}, + {0xd29fe4b18e88640e, 0x8eec7f0d19a03aae}, + {0x83a3eeeef9153e89, 0x1953cf68300424ad}, + {0xa48ceaaab75a8e2b, 0x5fa8c3423c052dd8}, + {0xcdb02555653131b6, 0x3792f412cb06794e}, + {0x808e17555f3ebf11, 0xe2bbd88bbee40bd1}, + {0xa0b19d2ab70e6ed6, 0x5b6aceaeae9d0ec5}, + {0xc8de047564d20a8b, 0xf245825a5a445276}, + {0xfb158592be068d2e, 0xeed6e2f0f0d56713}, + {0x9ced737bb6c4183d, 0x55464dd69685606c}, + {0xc428d05aa4751e4c, 0xaa97e14c3c26b887}, + {0xf53304714d9265df, 0xd53dd99f4b3066a9}, + {0x993fe2c6d07b7fab, 0xe546a8038efe402a}, + {0xbf8fdb78849a5f96, 0xde98520472bdd034}, + {0xef73d256a5c0f77c, 0x963e66858f6d4441}, + {0x95a8637627989aad, 0xdde7001379a44aa9}, + {0xbb127c53b17ec159, 0x5560c018580d5d53}, + {0xe9d71b689dde71af, 0xaab8f01e6e10b4a7}, + {0x9226712162ab070d, 0xcab3961304ca70e9}, + {0xb6b00d69bb55c8d1, 0x3d607b97c5fd0d23}, + {0xe45c10c42a2b3b05, 0x8cb89a7db77c506b}, + {0x8eb98a7a9a5b04e3, 0x77f3608e92adb243}, + {0xb267ed1940f1c61c, 0x55f038b237591ed4}, + {0xdf01e85f912e37a3, 0x6b6c46dec52f6689}, + {0x8b61313bbabce2c6, 0x2323ac4b3b3da016}, + {0xae397d8aa96c1b77, 0xabec975e0a0d081b}, + {0xd9c7dced53c72255, 0x96e7bd358c904a22}, + {0x881cea14545c7575, 0x7e50d64177da2e55}, + {0xaa242499697392d2, 0xdde50bd1d5d0b9ea}, + {0xd4ad2dbfc3d07787, 0x955e4ec64b44e865}, + {0x84ec3c97da624ab4, 0xbd5af13bef0b113f}, + {0xa6274bbdd0fadd61, 0xecb1ad8aeacdd58f}, + {0xcfb11ead453994ba, 0x67de18eda5814af3}, + {0x81ceb32c4b43fcf4, 0x80eacf948770ced8}, + {0xa2425ff75e14fc31, 0xa1258379a94d028e}, + {0xcad2f7f5359a3b3e, 0x096ee45813a04331}, + {0xfd87b5f28300ca0d, 0x8bca9d6e188853fd}, + {0x9e74d1b791e07e48, 0x775ea264cf55347e}, + {0xc612062576589dda, 0x95364afe032a819e}, + {0xf79687aed3eec551, 0x3a83ddbd83f52205}, + {0x9abe14cd44753b52, 0xc4926a9672793543}, + {0xc16d9a0095928a27, 0x75b7053c0f178294}, + {0xf1c90080baf72cb1, 0x5324c68b12dd6339}, + {0x971da05074da7bee, 0xd3f6fc16ebca5e04}, + {0xbce5086492111aea, 0x88f4bb1ca6bcf585}, + {0xec1e4a7db69561a5, 0x2b31e9e3d06c32e6}, + {0x9392ee8e921d5d07, 0x3aff322e62439fd0}, + {0xb877aa3236a4b449, 0x09befeb9fad487c3}, + {0xe69594bec44de15b, 0x4c2ebe687989a9b4}, + {0x901d7cf73ab0acd9, 0x0f9d37014bf60a11}, + {0xb424dc35095cd80f, 0x538484c19ef38c95}, + {0xe12e13424bb40e13, 0x2865a5f206b06fba}, + {0x8cbccc096f5088cb, 0xf93f87b7442e45d4}, + {0xafebff0bcb24aafe, 0xf78f69a51539d749}, + {0xdbe6fecebdedd5be, 0xb573440e5a884d1c}, + {0x89705f4136b4a597, 0x31680a88f8953031}, + {0xabcc77118461cefc, 0xfdc20d2b36ba7c3e}, + {0xd6bf94d5e57a42bc, 0x3d32907604691b4d}, + {0x8637bd05af6c69b5, 0xa63f9a49c2c1b110}, + {0xa7c5ac471b478423, 0x0fcf80dc33721d54}, + {0xd1b71758e219652b, 0xd3c36113404ea4a9}, + {0x83126e978d4fdf3b, 0x645a1cac083126ea}, + {0xa3d70a3d70a3d70a, 0x3d70a3d70a3d70a4}, + {0xcccccccccccccccc, 0xcccccccccccccccd}, + {0x8000000000000000, 0x0000000000000000}, + {0xa000000000000000, 0x0000000000000000}, + {0xc800000000000000, 0x0000000000000000}, + {0xfa00000000000000, 0x0000000000000000}, + {0x9c40000000000000, 0x0000000000000000}, + {0xc350000000000000, 0x0000000000000000}, + {0xf424000000000000, 0x0000000000000000}, + {0x9896800000000000, 0x0000000000000000}, + {0xbebc200000000000, 0x0000000000000000}, + {0xee6b280000000000, 0x0000000000000000}, + {0x9502f90000000000, 0x0000000000000000}, + {0xba43b74000000000, 0x0000000000000000}, + {0xe8d4a51000000000, 0x0000000000000000}, + {0x9184e72a00000000, 0x0000000000000000}, + {0xb5e620f480000000, 0x0000000000000000}, + {0xe35fa931a0000000, 0x0000000000000000}, + {0x8e1bc9bf04000000, 0x0000000000000000}, + {0xb1a2bc2ec5000000, 0x0000000000000000}, + {0xde0b6b3a76400000, 0x0000000000000000}, + {0x8ac7230489e80000, 0x0000000000000000}, + {0xad78ebc5ac620000, 0x0000000000000000}, + {0xd8d726b7177a8000, 0x0000000000000000}, + {0x878678326eac9000, 0x0000000000000000}, + {0xa968163f0a57b400, 0x0000000000000000}, + {0xd3c21bcecceda100, 0x0000000000000000}, + {0x84595161401484a0, 0x0000000000000000}, + {0xa56fa5b99019a5c8, 0x0000000000000000}, + {0xcecb8f27f4200f3a, 0x0000000000000000}, + {0x813f3978f8940984, 0x4000000000000000}, + {0xa18f07d736b90be5, 0x5000000000000000}, + {0xc9f2c9cd04674ede, 0xa400000000000000}, + {0xfc6f7c4045812296, 0x4d00000000000000}, + {0x9dc5ada82b70b59d, 0xf020000000000000}, + {0xc5371912364ce305, 0x6c28000000000000}, + {0xf684df56c3e01bc6, 0xc732000000000000}, + {0x9a130b963a6c115c, 0x3c7f400000000000}, + {0xc097ce7bc90715b3, 0x4b9f100000000000}, + {0xf0bdc21abb48db20, 0x1e86d40000000000}, + {0x96769950b50d88f4, 0x1314448000000000}, + {0xbc143fa4e250eb31, 0x17d955a000000000}, + {0xeb194f8e1ae525fd, 0x5dcfab0800000000}, + {0x92efd1b8d0cf37be, 0x5aa1cae500000000}, + {0xb7abc627050305ad, 0xf14a3d9e40000000}, + {0xe596b7b0c643c719, 0x6d9ccd05d0000000}, + {0x8f7e32ce7bea5c6f, 0xe4820023a2000000}, + {0xb35dbf821ae4f38b, 0xdda2802c8a800000}, + {0xe0352f62a19e306e, 0xd50b2037ad200000}, + {0x8c213d9da502de45, 0x4526f422cc340000}, + {0xaf298d050e4395d6, 0x9670b12b7f410000}, + {0xdaf3f04651d47b4c, 0x3c0cdd765f114000}, + {0x88d8762bf324cd0f, 0xa5880a69fb6ac800}, + {0xab0e93b6efee0053, 0x8eea0d047a457a00}, + {0xd5d238a4abe98068, 0x72a4904598d6d880}, + {0x85a36366eb71f041, 0x47a6da2b7f864750}, + {0xa70c3c40a64e6c51, 0x999090b65f67d924}, + {0xd0cf4b50cfe20765, 0xfff4b4e3f741cf6d}, + {0x82818f1281ed449f, 0xbff8f10e7a8921a5}, + {0xa321f2d7226895c7, 0xaff72d52192b6a0e}, + {0xcbea6f8ceb02bb39, 0x9bf4f8a69f764491}, + {0xfee50b7025c36a08, 0x02f236d04753d5b5}, + {0x9f4f2726179a2245, 0x01d762422c946591}, + {0xc722f0ef9d80aad6, 0x424d3ad2b7b97ef6}, + {0xf8ebad2b84e0d58b, 0xd2e0898765a7deb3}, + {0x9b934c3b330c8577, 0x63cc55f49f88eb30}, + {0xc2781f49ffcfa6d5, 0x3cbf6b71c76b25fc}, + {0xf316271c7fc3908a, 0x8bef464e3945ef7b}, + {0x97edd871cfda3a56, 0x97758bf0e3cbb5ad}, + {0xbde94e8e43d0c8ec, 0x3d52eeed1cbea318}, + {0xed63a231d4c4fb27, 0x4ca7aaa863ee4bde}, + {0x945e455f24fb1cf8, 0x8fe8caa93e74ef6b}, + {0xb975d6b6ee39e436, 0xb3e2fd538e122b45}, + {0xe7d34c64a9c85d44, 0x60dbbca87196b617}, + {0x90e40fbeea1d3a4a, 0xbc8955e946fe31ce}, + {0xb51d13aea4a488dd, 0x6babab6398bdbe42}, + {0xe264589a4dcdab14, 0xc696963c7eed2dd2}, + {0x8d7eb76070a08aec, 0xfc1e1de5cf543ca3}, + {0xb0de65388cc8ada8, 0x3b25a55f43294bcc}, + {0xdd15fe86affad912, 0x49ef0eb713f39ebf}, + {0x8a2dbf142dfcc7ab, 0x6e3569326c784338}, + {0xacb92ed9397bf996, 0x49c2c37f07965405}, + {0xd7e77a8f87daf7fb, 0xdc33745ec97be907}, + {0x86f0ac99b4e8dafd, 0x69a028bb3ded71a4}, + {0xa8acd7c0222311bc, 0xc40832ea0d68ce0d}, + {0xd2d80db02aabd62b, 0xf50a3fa490c30191}, + {0x83c7088e1aab65db, 0x792667c6da79e0fb}, + {0xa4b8cab1a1563f52, 0x577001b891185939}, + {0xcde6fd5e09abcf26, 0xed4c0226b55e6f87}, + {0x80b05e5ac60b6178, 0x544f8158315b05b5}, + {0xa0dc75f1778e39d6, 0x696361ae3db1c722}, + {0xc913936dd571c84c, 0x03bc3a19cd1e38ea}, + {0xfb5878494ace3a5f, 0x04ab48a04065c724}, + {0x9d174b2dcec0e47b, 0x62eb0d64283f9c77}, + {0xc45d1df942711d9a, 0x3ba5d0bd324f8395}, + {0xf5746577930d6500, 0xca8f44ec7ee3647a}, + {0x9968bf6abbe85f20, 0x7e998b13cf4e1ecc}, + {0xbfc2ef456ae276e8, 0x9e3fedd8c321a67f}, + {0xefb3ab16c59b14a2, 0xc5cfe94ef3ea101f}, + {0x95d04aee3b80ece5, 0xbba1f1d158724a13}, + {0xbb445da9ca61281f, 0x2a8a6e45ae8edc98}, + {0xea1575143cf97226, 0xf52d09d71a3293be}, + {0x924d692ca61be758, 0x593c2626705f9c57}, + {0xb6e0c377cfa2e12e, 0x6f8b2fb00c77836d}, + {0xe498f455c38b997a, 0x0b6dfb9c0f956448}, + {0x8edf98b59a373fec, 0x4724bd4189bd5ead}, + {0xb2977ee300c50fe7, 0x58edec91ec2cb658}, + {0xdf3d5e9bc0f653e1, 0x2f2967b66737e3ee}, + {0x8b865b215899f46c, 0xbd79e0d20082ee75}, + {0xae67f1e9aec07187, 0xecd8590680a3aa12}, + {0xda01ee641a708de9, 0xe80e6f4820cc9496}, + {0x884134fe908658b2, 0x3109058d147fdcde}, + {0xaa51823e34a7eede, 0xbd4b46f0599fd416}, + {0xd4e5e2cdc1d1ea96, 0x6c9e18ac7007c91b}, + {0x850fadc09923329e, 0x03e2cf6bc604ddb1}, + {0xa6539930bf6bff45, 0x84db8346b786151d}, + {0xcfe87f7cef46ff16, 0xe612641865679a64}, + {0x81f14fae158c5f6e, 0x4fcb7e8f3f60c07f}, + {0xa26da3999aef7749, 0xe3be5e330f38f09e}, + {0xcb090c8001ab551c, 0x5cadf5bfd3072cc6}, + {0xfdcb4fa002162a63, 0x73d9732fc7c8f7f7}, + {0x9e9f11c4014dda7e, 0x2867e7fddcdd9afb}, + {0xc646d63501a1511d, 0xb281e1fd541501b9}, + {0xf7d88bc24209a565, 0x1f225a7ca91a4227}, + {0x9ae757596946075f, 0x3375788de9b06959}, + {0xc1a12d2fc3978937, 0x0052d6b1641c83af}, + {0xf209787bb47d6b84, 0xc0678c5dbd23a49b}, + {0x9745eb4d50ce6332, 0xf840b7ba963646e1}, + {0xbd176620a501fbff, 0xb650e5a93bc3d899}, + {0xec5d3fa8ce427aff, 0xa3e51f138ab4cebf}, + {0x93ba47c980e98cdf, 0xc66f336c36b10138}, + {0xb8a8d9bbe123f017, 0xb80b0047445d4185}, + {0xe6d3102ad96cec1d, 0xa60dc059157491e6}, + {0x9043ea1ac7e41392, 0x87c89837ad68db30}, + {0xb454e4a179dd1877, 0x29babe4598c311fc}, + {0xe16a1dc9d8545e94, 0xf4296dd6fef3d67b}, + {0x8ce2529e2734bb1d, 0x1899e4a65f58660d}, + {0xb01ae745b101e9e4, 0x5ec05dcff72e7f90}, + {0xdc21a1171d42645d, 0x76707543f4fa1f74}, + {0x899504ae72497eba, 0x6a06494a791c53a9}, + {0xabfa45da0edbde69, 0x0487db9d17636893}, + {0xd6f8d7509292d603, 0x45a9d2845d3c42b7}, + {0x865b86925b9bc5c2, 0x0b8a2392ba45a9b3}, + {0xa7f26836f282b732, 0x8e6cac7768d7141f}, + {0xd1ef0244af2364ff, 0x3207d795430cd927}, + {0x8335616aed761f1f, 0x7f44e6bd49e807b9}, + {0xa402b9c5a8d3a6e7, 0x5f16206c9c6209a7}, + {0xcd036837130890a1, 0x36dba887c37a8c10}, + {0x802221226be55a64, 0xc2494954da2c978a}, + {0xa02aa96b06deb0fd, 0xf2db9baa10b7bd6d}, + {0xc83553c5c8965d3d, 0x6f92829494e5acc8}, + {0xfa42a8b73abbf48c, 0xcb772339ba1f17fa}, + {0x9c69a97284b578d7, 0xff2a760414536efc}, + {0xc38413cf25e2d70d, 0xfef5138519684abb}, + {0xf46518c2ef5b8cd1, 0x7eb258665fc25d6a}, + {0x98bf2f79d5993802, 0xef2f773ffbd97a62}, + {0xbeeefb584aff8603, 0xaafb550ffacfd8fb}, + {0xeeaaba2e5dbf6784, 0x95ba2a53f983cf39}, + {0x952ab45cfa97a0b2, 0xdd945a747bf26184}, + {0xba756174393d88df, 0x94f971119aeef9e5}, + {0xe912b9d1478ceb17, 0x7a37cd5601aab85e}, + {0x91abb422ccb812ee, 0xac62e055c10ab33b}, + {0xb616a12b7fe617aa, 0x577b986b314d600a}, + {0xe39c49765fdf9d94, 0xed5a7e85fda0b80c}, + {0x8e41ade9fbebc27d, 0x14588f13be847308}, + {0xb1d219647ae6b31c, 0x596eb2d8ae258fc9}, + {0xde469fbd99a05fe3, 0x6fca5f8ed9aef3bc}, + {0x8aec23d680043bee, 0x25de7bb9480d5855}, + {0xada72ccc20054ae9, 0xaf561aa79a10ae6b}, + {0xd910f7ff28069da4, 0x1b2ba1518094da05}, + {0x87aa9aff79042286, 0x90fb44d2f05d0843}, + {0xa99541bf57452b28, 0x353a1607ac744a54}, + {0xd3fa922f2d1675f2, 0x42889b8997915ce9}, + {0x847c9b5d7c2e09b7, 0x69956135febada12}, + {0xa59bc234db398c25, 0x43fab9837e699096}, + {0xcf02b2c21207ef2e, 0x94f967e45e03f4bc}, + {0x8161afb94b44f57d, 0x1d1be0eebac278f6}, + {0xa1ba1ba79e1632dc, 0x6462d92a69731733}, + {0xca28a291859bbf93, 0x7d7b8f7503cfdcff}, + {0xfcb2cb35e702af78, 0x5cda735244c3d43f}, + {0x9defbf01b061adab, 0x3a0888136afa64a8}, + {0xc56baec21c7a1916, 0x088aaa1845b8fdd1}, + {0xf6c69a72a3989f5b, 0x8aad549e57273d46}, + {0x9a3c2087a63f6399, 0x36ac54e2f678864c}, + {0xc0cb28a98fcf3c7f, 0x84576a1bb416a7de}, + {0xf0fdf2d3f3c30b9f, 0x656d44a2a11c51d6}, + {0x969eb7c47859e743, 0x9f644ae5a4b1b326}, + {0xbc4665b596706114, 0x873d5d9f0dde1fef}, + {0xeb57ff22fc0c7959, 0xa90cb506d155a7eb}, + {0x9316ff75dd87cbd8, 0x09a7f12442d588f3}, + {0xb7dcbf5354e9bece, 0x0c11ed6d538aeb30}, + {0xe5d3ef282a242e81, 0x8f1668c8a86da5fb}, + {0x8fa475791a569d10, 0xf96e017d694487bd}, + {0xb38d92d760ec4455, 0x37c981dcc395a9ad}, + {0xe070f78d3927556a, 0x85bbe253f47b1418}, + {0x8c469ab843b89562, 0x93956d7478ccec8f}, + {0xaf58416654a6babb, 0x387ac8d1970027b3}, + {0xdb2e51bfe9d0696a, 0x06997b05fcc0319f}, + {0x88fcf317f22241e2, 0x441fece3bdf81f04}, + {0xab3c2fddeeaad25a, 0xd527e81cad7626c4}, + {0xd60b3bd56a5586f1, 0x8a71e223d8d3b075}, + {0x85c7056562757456, 0xf6872d5667844e4a}, + {0xa738c6bebb12d16c, 0xb428f8ac016561dc}, + {0xd106f86e69d785c7, 0xe13336d701beba53}, + {0x82a45b450226b39c, 0xecc0024661173474}, + {0xa34d721642b06084, 0x27f002d7f95d0191}, + {0xcc20ce9bd35c78a5, 0x31ec038df7b441f5}, + {0xff290242c83396ce, 0x7e67047175a15272}, + {0x9f79a169bd203e41, 0x0f0062c6e984d387}, + {0xc75809c42c684dd1, 0x52c07b78a3e60869}, + {0xf92e0c3537826145, 0xa7709a56ccdf8a83}, + {0x9bbcc7a142b17ccb, 0x88a66076400bb692}, + {0xc2abf989935ddbfe, 0x6acff893d00ea436}, + {0xf356f7ebf83552fe, 0x0583f6b8c4124d44}, + {0x98165af37b2153de, 0xc3727a337a8b704b}, + {0xbe1bf1b059e9a8d6, 0x744f18c0592e4c5d}, + {0xeda2ee1c7064130c, 0x1162def06f79df74}, + {0x9485d4d1c63e8be7, 0x8addcb5645ac2ba9}, + {0xb9a74a0637ce2ee1, 0x6d953e2bd7173693}, + {0xe8111c87c5c1ba99, 0xc8fa8db6ccdd0438}, + {0x910ab1d4db9914a0, 0x1d9c9892400a22a3}, + {0xb54d5e4a127f59c8, 0x2503beb6d00cab4c}, + {0xe2a0b5dc971f303a, 0x2e44ae64840fd61e}, + {0x8da471a9de737e24, 0x5ceaecfed289e5d3}, + {0xb10d8e1456105dad, 0x7425a83e872c5f48}, + {0xdd50f1996b947518, 0xd12f124e28f7771a}, + {0x8a5296ffe33cc92f, 0x82bd6b70d99aaa70}, + {0xace73cbfdc0bfb7b, 0x636cc64d1001550c}, + {0xd8210befd30efa5a, 0x3c47f7e05401aa4f}, + {0x8714a775e3e95c78, 0x65acfaec34810a72}, + {0xa8d9d1535ce3b396, 0x7f1839a741a14d0e}, + {0xd31045a8341ca07c, 0x1ede48111209a051}, + {0x83ea2b892091e44d, 0x934aed0aab460433}, + {0xa4e4b66b68b65d60, 0xf81da84d56178540}, + {0xce1de40642e3f4b9, 0x36251260ab9d668f}, + {0x80d2ae83e9ce78f3, 0xc1d72b7c6b42601a}, + {0xa1075a24e4421730, 0xb24cf65b8612f820}, + {0xc94930ae1d529cfc, 0xdee033f26797b628}, + {0xfb9b7cd9a4a7443c, 0x169840ef017da3b2}, + {0x9d412e0806e88aa5, 0x8e1f289560ee864f}, + {0xc491798a08a2ad4e, 0xf1a6f2bab92a27e3}, + {0xf5b5d7ec8acb58a2, 0xae10af696774b1dc}, + {0x9991a6f3d6bf1765, 0xacca6da1e0a8ef2a}, + {0xbff610b0cc6edd3f, 0x17fd090a58d32af4}, + {0xeff394dcff8a948e, 0xddfc4b4cef07f5b1}, + {0x95f83d0a1fb69cd9, 0x4abdaf101564f98f}, + {0xbb764c4ca7a4440f, 0x9d6d1ad41abe37f2}, + {0xea53df5fd18d5513, 0x84c86189216dc5ee}, + {0x92746b9be2f8552c, 0x32fd3cf5b4e49bb5}, + {0xb7118682dbb66a77, 0x3fbc8c33221dc2a2}, + {0xe4d5e82392a40515, 0x0fabaf3feaa5334b}, + {0x8f05b1163ba6832d, 0x29cb4d87f2a7400f}, + {0xb2c71d5bca9023f8, 0x743e20e9ef511013}, + {0xdf78e4b2bd342cf6, 0x914da9246b255417}, + {0x8bab8eefb6409c1a, 0x1ad089b6c2f7548f}, + {0xae9672aba3d0c320, 0xa184ac2473b529b2}, + {0xda3c0f568cc4f3e8, 0xc9e5d72d90a2741f}, + {0x8865899617fb1871, 0x7e2fa67c7a658893}, + {0xaa7eebfb9df9de8d, 0xddbb901b98feeab8}, + {0xd51ea6fa85785631, 0x552a74227f3ea566}, + {0x8533285c936b35de, 0xd53a88958f872760}, + {0xa67ff273b8460356, 0x8a892abaf368f138}, + {0xd01fef10a657842c, 0x2d2b7569b0432d86}, + {0x8213f56a67f6b29b, 0x9c3b29620e29fc74}, + {0xa298f2c501f45f42, 0x8349f3ba91b47b90}, + {0xcb3f2f7642717713, 0x241c70a936219a74}, + {0xfe0efb53d30dd4d7, 0xed238cd383aa0111}, + {0x9ec95d1463e8a506, 0xf4363804324a40ab}, + {0xc67bb4597ce2ce48, 0xb143c6053edcd0d6}, + {0xf81aa16fdc1b81da, 0xdd94b7868e94050b}, + {0x9b10a4e5e9913128, 0xca7cf2b4191c8327}, + {0xc1d4ce1f63f57d72, 0xfd1c2f611f63a3f1}, + {0xf24a01a73cf2dccf, 0xbc633b39673c8ced}, + {0x976e41088617ca01, 0xd5be0503e085d814}, + {0xbd49d14aa79dbc82, 0x4b2d8644d8a74e19}, + {0xec9c459d51852ba2, 0xddf8e7d60ed1219f}, + {0x93e1ab8252f33b45, 0xcabb90e5c942b504}, + {0xb8da1662e7b00a17, 0x3d6a751f3b936244}, + {0xe7109bfba19c0c9d, 0x0cc512670a783ad5}, + {0x906a617d450187e2, 0x27fb2b80668b24c6}, + {0xb484f9dc9641e9da, 0xb1f9f660802dedf7}, + {0xe1a63853bbd26451, 0x5e7873f8a0396974}, + {0x8d07e33455637eb2, 0xdb0b487b6423e1e9}, + {0xb049dc016abc5e5f, 0x91ce1a9a3d2cda63}, + {0xdc5c5301c56b75f7, 0x7641a140cc7810fc}, + {0x89b9b3e11b6329ba, 0xa9e904c87fcb0a9e}, + {0xac2820d9623bf429, 0x546345fa9fbdcd45}, + {0xd732290fbacaf133, 0xa97c177947ad4096}, + {0x867f59a9d4bed6c0, 0x49ed8eabcccc485e}, + {0xa81f301449ee8c70, 0x5c68f256bfff5a75}, + {0xd226fc195c6a2f8c, 0x73832eec6fff3112}, + {0x83585d8fd9c25db7, 0xc831fd53c5ff7eac}, + {0xa42e74f3d032f525, 0xba3e7ca8b77f5e56}, + {0xcd3a1230c43fb26f, 0x28ce1bd2e55f35ec}, + {0x80444b5e7aa7cf85, 0x7980d163cf5b81b4}, + {0xa0555e361951c366, 0xd7e105bcc3326220}, + {0xc86ab5c39fa63440, 0x8dd9472bf3fefaa8}, + {0xfa856334878fc150, 0xb14f98f6f0feb952}, + {0x9c935e00d4b9d8d2, 0x6ed1bf9a569f33d4}, + {0xc3b8358109e84f07, 0x0a862f80ec4700c9}, + {0xf4a642e14c6262c8, 0xcd27bb612758c0fb}, + {0x98e7e9cccfbd7dbd, 0x8038d51cb897789d}, + {0xbf21e44003acdd2c, 0xe0470a63e6bd56c4}, + {0xeeea5d5004981478, 0x1858ccfce06cac75}, + {0x95527a5202df0ccb, 0x0f37801e0c43ebc9}, + {0xbaa718e68396cffd, 0xd30560258f54e6bb}, + {0xe950df20247c83fd, 0x47c6b82ef32a206a}, + {0x91d28b7416cdd27e, 0x4cdc331d57fa5442}, + {0xb6472e511c81471d, 0xe0133fe4adf8e953}, + {0xe3d8f9e563a198e5, 0x58180fddd97723a7}, + {0x8e679c2f5e44ff8f, 0x570f09eaa7ea7649}, + {0xb201833b35d63f73, 0x2cd2cc6551e513db}, + {0xde81e40a034bcf4f, 0xf8077f7ea65e58d2}, + {0x8b112e86420f6191, 0xfb04afaf27faf783}, + {0xadd57a27d29339f6, 0x79c5db9af1f9b564}, + {0xd94ad8b1c7380874, 0x18375281ae7822bd}, + {0x87cec76f1c830548, 0x8f2293910d0b15b6}, + {0xa9c2794ae3a3c69a, 0xb2eb3875504ddb23}, + {0xd433179d9c8cb841, 0x5fa60692a46151ec}, + {0x849feec281d7f328, 0xdbc7c41ba6bcd334}, + {0xa5c7ea73224deff3, 0x12b9b522906c0801}, + {0xcf39e50feae16bef, 0xd768226b34870a01}, + {0x81842f29f2cce375, 0xe6a1158300d46641}, + {0xa1e53af46f801c53, 0x60495ae3c1097fd1}, + {0xca5e89b18b602368, 0x385bb19cb14bdfc5}, + {0xfcf62c1dee382c42, 0x46729e03dd9ed7b6}, + {0x9e19db92b4e31ba9, 0x6c07a2c26a8346d2}, + {0xc5a05277621be293, 0xc7098b7305241886}, + {0xf70867153aa2db38, 0xb8cbee4fc66d1ea8}, + {0x9a65406d44a5c903, 0x737f74f1dc043329}, + {0xc0fe908895cf3b44, 0x505f522e53053ff3}, + {0xf13e34aabb430a15, 0x647726b9e7c68ff0}, + {0x96c6e0eab509e64d, 0x5eca783430dc19f6}, + {0xbc789925624c5fe0, 0xb67d16413d132073}, + {0xeb96bf6ebadf77d8, 0xe41c5bd18c57e890}, + {0x933e37a534cbaae7, 0x8e91b962f7b6f15a}, + {0xb80dc58e81fe95a1, 0x723627bbb5a4adb1}, + {0xe61136f2227e3b09, 0xcec3b1aaa30dd91d}, + {0x8fcac257558ee4e6, 0x213a4f0aa5e8a7b2}, + {0xb3bd72ed2af29e1f, 0xa988e2cd4f62d19e}, + {0xe0accfa875af45a7, 0x93eb1b80a33b8606}, + {0x8c6c01c9498d8b88, 0xbc72f130660533c4}, + {0xaf87023b9bf0ee6a, 0xeb8fad7c7f8680b5}, + {0xdb68c2ca82ed2a05, 0xa67398db9f6820e2}, +#else + {0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b}, + {0xce5d73ff402d98e3, 0xfb0a3d212dc81290}, + {0xa6b34ad8c9dfc06f, 0xf42faa48c0ea481f}, + {0x86a8d39ef77164bc, 0xae5dff9c02033198}, + {0xd98ddaee19068c76, 0x3badd624dd9b0958}, + {0xafbd2350644eeacf, 0xe5d1929ef90898fb}, + {0x8df5efabc5979c8f, 0xca8d3ffa1ef463c2}, + {0xe55990879ddcaabd, 0xcc420a6a101d0516}, + {0xb94470938fa89bce, 0xf808e40e8d5b3e6a}, + {0x95a8637627989aad, 0xdde7001379a44aa9}, + {0xf1c90080baf72cb1, 0x5324c68b12dd6339}, + {0xc350000000000000, 0x0000000000000000}, + {0x9dc5ada82b70b59d, 0xf020000000000000}, + {0xfee50b7025c36a08, 0x02f236d04753d5b5}, + {0xcde6fd5e09abcf26, 0xed4c0226b55e6f87}, + {0xa6539930bf6bff45, 0x84db8346b786151d}, + {0x865b86925b9bc5c2, 0x0b8a2392ba45a9b3}, + {0xd910f7ff28069da4, 0x1b2ba1518094da05}, + {0xaf58416654a6babb, 0x387ac8d1970027b3}, + {0x8da471a9de737e24, 0x5ceaecfed289e5d3}, + {0xe4d5e82392a40515, 0x0fabaf3feaa5334b}, + {0xb8da1662e7b00a17, 0x3d6a751f3b936244}, + {0x95527a5202df0ccb, 0x0f37801e0c43ebc9}, + {0xf13e34aabb430a15, 0x647726b9e7c68ff0} +#endif + }; + +#if FMT_USE_FULL_CACHE_DRAGONBOX + return pow10_significands[k - float_info::min_k]; +#else + static constexpr const uint64_t powers_of_5_64[] = { + 0x0000000000000001, 0x0000000000000005, 0x0000000000000019, + 0x000000000000007d, 0x0000000000000271, 0x0000000000000c35, + 0x0000000000003d09, 0x000000000001312d, 0x000000000005f5e1, + 0x00000000001dcd65, 0x00000000009502f9, 0x0000000002e90edd, + 0x000000000e8d4a51, 0x0000000048c27395, 0x000000016bcc41e9, + 0x000000071afd498d, 0x0000002386f26fc1, 0x000000b1a2bc2ec5, + 0x000003782dace9d9, 0x00001158e460913d, 0x000056bc75e2d631, + 0x0001b1ae4d6e2ef5, 0x000878678326eac9, 0x002a5a058fc295ed, + 0x00d3c21bcecceda1, 0x0422ca8b0a00a425, 0x14adf4b7320334b9}; + + static const int compression_ratio = 27; + + // Compute base index. + int cache_index = (k - float_info::min_k) / compression_ratio; + int kb = cache_index * compression_ratio + float_info::min_k; + int offset = k - kb; + + // Get base cache. + uint128_fallback base_cache = pow10_significands[cache_index]; + if (offset == 0) return base_cache; + + // Compute the required amount of bit-shift. + int alpha = floor_log2_pow10(kb + offset) - floor_log2_pow10(kb) - offset; + FMT_ASSERT(alpha > 0 && alpha < 64, "shifting error detected"); + + // Try to recover the real cache. + uint64_t pow5 = powers_of_5_64[offset]; + uint128_fallback recovered_cache = umul128(base_cache.high(), pow5); + uint128_fallback middle_low = umul128(base_cache.low(), pow5); + + recovered_cache += middle_low.high(); + + uint64_t high_to_middle = recovered_cache.high() << (64 - alpha); + uint64_t middle_to_low = recovered_cache.low() << (64 - alpha); + + recovered_cache = + uint128_fallback{(recovered_cache.low() >> alpha) | high_to_middle, + ((middle_low.low() >> alpha) | middle_to_low)}; + FMT_ASSERT(recovered_cache.low() + 1 != 0, ""); + return {recovered_cache.high(), recovered_cache.low() + 1}; +#endif + } + + struct compute_mul_result { + carrier_uint result; + bool is_integer; + }; + struct compute_mul_parity_result { + bool parity; + bool is_integer; + }; + + static auto compute_mul(carrier_uint u, + const cache_entry_type& cache) noexcept + -> compute_mul_result { + auto r = umul192_upper128(u, cache); + return {r.high(), r.low() == 0}; + } + + static auto compute_delta(cache_entry_type const& cache, int beta) noexcept + -> uint32_t { + return static_cast(cache.high() >> (64 - 1 - beta)); + } + + static auto compute_mul_parity(carrier_uint two_f, + const cache_entry_type& cache, + int beta) noexcept + -> compute_mul_parity_result { + FMT_ASSERT(beta >= 1, ""); + FMT_ASSERT(beta < 64, ""); + + auto r = umul192_lower128(two_f, cache); + return {((r.high() >> (64 - beta)) & 1) != 0, + ((r.high() << beta) | (r.low() >> (64 - beta))) == 0}; + } + + static auto compute_left_endpoint_for_shorter_interval_case( + const cache_entry_type& cache, int beta) noexcept -> carrier_uint { + return (cache.high() - + (cache.high() >> (num_significand_bits() + 2))) >> + (64 - num_significand_bits() - 1 - beta); + } + + static auto compute_right_endpoint_for_shorter_interval_case( + const cache_entry_type& cache, int beta) noexcept -> carrier_uint { + return (cache.high() + + (cache.high() >> (num_significand_bits() + 1))) >> + (64 - num_significand_bits() - 1 - beta); + } + + static auto compute_round_up_for_shorter_interval_case( + const cache_entry_type& cache, int beta) noexcept -> carrier_uint { + return ((cache.high() >> (64 - num_significand_bits() - 2 - beta)) + + 1) / + 2; + } +}; + +FMT_FUNC auto get_cached_power(int k) noexcept -> uint128_fallback { + return cache_accessor::get_cached_power(k); +} + +// Various integer checks +template +auto is_left_endpoint_integer_shorter_interval(int exponent) noexcept -> bool { + const int case_shorter_interval_left_endpoint_lower_threshold = 2; + const int case_shorter_interval_left_endpoint_upper_threshold = 3; + return exponent >= case_shorter_interval_left_endpoint_lower_threshold && + exponent <= case_shorter_interval_left_endpoint_upper_threshold; +} + +// Remove trailing zeros from n and return the number of zeros removed (float) +FMT_INLINE int remove_trailing_zeros(uint32_t& n, int s = 0) noexcept { + FMT_ASSERT(n != 0, ""); + // Modular inverse of 5 (mod 2^32): (mod_inv_5 * 5) mod 2^32 = 1. + constexpr uint32_t mod_inv_5 = 0xcccccccd; + constexpr uint32_t mod_inv_25 = 0xc28f5c29; // = mod_inv_5 * mod_inv_5 + + while (true) { + auto q = rotr(n * mod_inv_25, 2); + if (q > max_value() / 100) break; + n = q; + s += 2; + } + auto q = rotr(n * mod_inv_5, 1); + if (q <= max_value() / 10) { + n = q; + s |= 1; + } + return s; +} + +// Removes trailing zeros and returns the number of zeros removed (double) +FMT_INLINE int remove_trailing_zeros(uint64_t& n) noexcept { + FMT_ASSERT(n != 0, ""); + + // This magic number is ceil(2^90 / 10^8). + constexpr uint64_t magic_number = 12379400392853802749ull; + auto nm = umul128(n, magic_number); + + // Is n is divisible by 10^8? + if ((nm.high() & ((1ull << (90 - 64)) - 1)) == 0 && nm.low() < magic_number) { + // If yes, work with the quotient... + auto n32 = static_cast(nm.high() >> (90 - 64)); + // ... and use the 32 bit variant of the function + int s = remove_trailing_zeros(n32, 8); + n = n32; + return s; + } + + // If n is not divisible by 10^8, work with n itself. + constexpr uint64_t mod_inv_5 = 0xcccccccccccccccd; + constexpr uint64_t mod_inv_25 = 0x8f5c28f5c28f5c29; // mod_inv_5 * mod_inv_5 + + int s = 0; + while (true) { + auto q = rotr(n * mod_inv_25, 2); + if (q > max_value() / 100) break; + n = q; + s += 2; + } + auto q = rotr(n * mod_inv_5, 1); + if (q <= max_value() / 10) { + n = q; + s |= 1; + } + + return s; +} + +// The main algorithm for shorter interval case +template +FMT_INLINE decimal_fp shorter_interval_case(int exponent) noexcept { + decimal_fp ret_value; + // Compute k and beta + const int minus_k = floor_log10_pow2_minus_log10_4_over_3(exponent); + const int beta = exponent + floor_log2_pow10(-minus_k); + + // Compute xi and zi + using cache_entry_type = typename cache_accessor::cache_entry_type; + const cache_entry_type cache = cache_accessor::get_cached_power(-minus_k); + + auto xi = cache_accessor::compute_left_endpoint_for_shorter_interval_case( + cache, beta); + auto zi = cache_accessor::compute_right_endpoint_for_shorter_interval_case( + cache, beta); + + // If the left endpoint is not an integer, increase it + if (!is_left_endpoint_integer_shorter_interval(exponent)) ++xi; + + // Try bigger divisor + ret_value.significand = zi / 10; + + // If succeed, remove trailing zeros if necessary and return + if (ret_value.significand * 10 >= xi) { + ret_value.exponent = minus_k + 1; + ret_value.exponent += remove_trailing_zeros(ret_value.significand); + return ret_value; + } + + // Otherwise, compute the round-up of y + ret_value.significand = + cache_accessor::compute_round_up_for_shorter_interval_case(cache, + beta); + ret_value.exponent = minus_k; + + // When tie occurs, choose one of them according to the rule + if (exponent >= float_info::shorter_interval_tie_lower_threshold && + exponent <= float_info::shorter_interval_tie_upper_threshold) { + ret_value.significand = ret_value.significand % 2 == 0 + ? ret_value.significand + : ret_value.significand - 1; + } else if (ret_value.significand < xi) { + ++ret_value.significand; + } + return ret_value; +} + +template auto to_decimal(T x) noexcept -> decimal_fp { + // Step 1: integer promotion & Schubfach multiplier calculation. + + using carrier_uint = typename float_info::carrier_uint; + using cache_entry_type = typename cache_accessor::cache_entry_type; + auto br = bit_cast(x); + + // Extract significand bits and exponent bits. + const carrier_uint significand_mask = + (static_cast(1) << num_significand_bits()) - 1; + carrier_uint significand = (br & significand_mask); + int exponent = + static_cast((br & exponent_mask()) >> num_significand_bits()); + + if (exponent != 0) { // Check if normal. + exponent -= exponent_bias() + num_significand_bits(); + + // Shorter interval case; proceed like Schubfach. + // In fact, when exponent == 1 and significand == 0, the interval is + // regular. However, it can be shown that the end-results are anyway same. + if (significand == 0) return shorter_interval_case(exponent); + + significand |= (static_cast(1) << num_significand_bits()); + } else { + // Subnormal case; the interval is always regular. + if (significand == 0) return {0, 0}; + exponent = + std::numeric_limits::min_exponent - num_significand_bits() - 1; + } + + const bool include_left_endpoint = (significand % 2 == 0); + const bool include_right_endpoint = include_left_endpoint; + + // Compute k and beta. + const int minus_k = floor_log10_pow2(exponent) - float_info::kappa; + const cache_entry_type cache = cache_accessor::get_cached_power(-minus_k); + const int beta = exponent + floor_log2_pow10(-minus_k); + + // Compute zi and deltai. + // 10^kappa <= deltai < 10^(kappa + 1) + const uint32_t deltai = cache_accessor::compute_delta(cache, beta); + const carrier_uint two_fc = significand << 1; + + // For the case of binary32, the result of integer check is not correct for + // 29711844 * 2^-82 + // = 6.1442653300000000008655037797566933477355632930994033813476... * 10^-18 + // and 29711844 * 2^-81 + // = 1.2288530660000000001731007559513386695471126586198806762695... * 10^-17, + // and they are the unique counterexamples. However, since 29711844 is even, + // this does not cause any problem for the endpoints calculations; it can only + // cause a problem when we need to perform integer check for the center. + // Fortunately, with these inputs, that branch is never executed, so we are + // fine. + const typename cache_accessor::compute_mul_result z_mul = + cache_accessor::compute_mul((two_fc | 1) << beta, cache); + + // Step 2: Try larger divisor; remove trailing zeros if necessary. + + // Using an upper bound on zi, we might be able to optimize the division + // better than the compiler; we are computing zi / big_divisor here. + decimal_fp ret_value; + ret_value.significand = divide_by_10_to_kappa_plus_1(z_mul.result); + uint32_t r = static_cast(z_mul.result - float_info::big_divisor * + ret_value.significand); + + if (r < deltai) { + // Exclude the right endpoint if necessary. + if (r == 0 && (z_mul.is_integer & !include_right_endpoint)) { + --ret_value.significand; + r = float_info::big_divisor; + goto small_divisor_case_label; + } + } else if (r > deltai) { + goto small_divisor_case_label; + } else { + // r == deltai; compare fractional parts. + const typename cache_accessor::compute_mul_parity_result x_mul = + cache_accessor::compute_mul_parity(two_fc - 1, cache, beta); + + if (!(x_mul.parity | (x_mul.is_integer & include_left_endpoint))) + goto small_divisor_case_label; + } + ret_value.exponent = minus_k + float_info::kappa + 1; + + // We may need to remove trailing zeros. + ret_value.exponent += remove_trailing_zeros(ret_value.significand); + return ret_value; + + // Step 3: Find the significand with the smaller divisor. + +small_divisor_case_label: + ret_value.significand *= 10; + ret_value.exponent = minus_k + float_info::kappa; + + uint32_t dist = r - (deltai / 2) + (float_info::small_divisor / 2); + const bool approx_y_parity = + ((dist ^ (float_info::small_divisor / 2)) & 1) != 0; + + // Is dist divisible by 10^kappa? + const bool divisible_by_small_divisor = + check_divisibility_and_divide_by_pow10::kappa>(dist); + + // Add dist / 10^kappa to the significand. + ret_value.significand += dist; + + if (!divisible_by_small_divisor) return ret_value; + + // Check z^(f) >= epsilon^(f). + // We have either yi == zi - epsiloni or yi == (zi - epsiloni) - 1, + // where yi == zi - epsiloni if and only if z^(f) >= epsilon^(f). + // Since there are only 2 possibilities, we only need to care about the + // parity. Also, zi and r should have the same parity since the divisor + // is an even number. + const auto y_mul = cache_accessor::compute_mul_parity(two_fc, cache, beta); + + // If z^(f) >= epsilon^(f), we might have a tie when z^(f) == epsilon^(f), + // or equivalently, when y is an integer. + if (y_mul.parity != approx_y_parity) + --ret_value.significand; + else if (y_mul.is_integer & (ret_value.significand % 2 != 0)) + --ret_value.significand; + return ret_value; +} +} // namespace dragonbox +} // namespace detail + +template <> struct formatter { + FMT_CONSTEXPR auto parse(format_parse_context& ctx) + -> format_parse_context::iterator { + return ctx.begin(); + } + + auto format(const detail::bigint& n, format_context& ctx) const + -> format_context::iterator { + auto out = ctx.out(); + bool first = true; + for (auto i = n.bigits_.size(); i > 0; --i) { + auto value = n.bigits_[i - 1u]; + if (first) { + out = fmt::format_to(out, FMT_STRING("{:x}"), value); + first = false; + continue; + } + out = fmt::format_to(out, FMT_STRING("{:08x}"), value); + } + if (n.exp_ > 0) + out = fmt::format_to(out, FMT_STRING("p{}"), + n.exp_ * detail::bigint::bigit_bits); + return out; + } +}; + +FMT_FUNC detail::utf8_to_utf16::utf8_to_utf16(string_view s) { + for_each_codepoint(s, [this](uint32_t cp, string_view) { + if (cp == invalid_code_point) FMT_THROW(std::runtime_error("invalid utf8")); + if (cp <= 0xFFFF) { + buffer_.push_back(static_cast(cp)); + } else { + cp -= 0x10000; + buffer_.push_back(static_cast(0xD800 + (cp >> 10))); + buffer_.push_back(static_cast(0xDC00 + (cp & 0x3FF))); + } + return true; + }); + buffer_.push_back(0); +} + +FMT_FUNC void format_system_error(detail::buffer& out, int error_code, + const char* message) noexcept { + FMT_TRY { + auto ec = std::error_code(error_code, std::generic_category()); + detail::write(appender(out), std::system_error(ec, message).what()); + return; + } + FMT_CATCH(...) {} + format_error_code(out, error_code, message); +} + +FMT_FUNC void report_system_error(int error_code, + const char* message) noexcept { + report_error(format_system_error, error_code, message); +} + +FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string { + // Don't optimize the "{}" case to keep the binary size small and because it + // can be better optimized in fmt::format anyway. + auto buffer = memory_buffer(); + detail::vformat_to(buffer, fmt, args); + return to_string(buffer); +} + +namespace detail { + +template struct span { + T* data; + size_t size; +}; + +template auto flockfile(F* f) -> decltype(_lock_file(f)) { + _lock_file(f); +} +template auto funlockfile(F* f) -> decltype(_unlock_file(f)) { + _unlock_file(f); +} + +#ifndef getc_unlocked +template auto getc_unlocked(F* f) -> decltype(_fgetc_nolock(f)) { + return _fgetc_nolock(f); +} +#endif + +template +struct has_flockfile : std::false_type {}; + +template +struct has_flockfile()))>> + : std::true_type {}; + +// A FILE wrapper. F is FILE defined as a template parameter to make system API +// detection work. +template class file_base { + public: + F* file_; + + public: + file_base(F* file) : file_(file) {} + operator F*() const { return file_; } + + // Reads a code unit from the stream. + auto get() -> int { + int result = getc_unlocked(file_); + if (result == EOF && ferror(file_) != 0) + FMT_THROW(system_error(errno, FMT_STRING("getc failed"))); + return result; + } + + // Puts the code unit back into the stream buffer. + void unget(char c) { + if (ungetc(c, file_) == EOF) + FMT_THROW(system_error(errno, FMT_STRING("ungetc failed"))); + } + + void flush() { fflush(this->file_); } +}; + +// A FILE wrapper for glibc. +template class glibc_file : public file_base { + private: + enum { + line_buffered = 0x200, // _IO_LINE_BUF + unbuffered = 2 // _IO_UNBUFFERED + }; + + public: + using file_base::file_base; + + auto is_buffered() const -> bool { + return (this->file_->_flags & unbuffered) == 0; + } + + void init_buffer() { + if (this->file_->_IO_write_ptr) return; + // Force buffer initialization by placing and removing a char in a buffer. + putc_unlocked(0, this->file_); + --this->file_->_IO_write_ptr; + } + + // Returns the file's read buffer. + auto get_read_buffer() const -> span { + auto ptr = this->file_->_IO_read_ptr; + return {ptr, to_unsigned(this->file_->_IO_read_end - ptr)}; + } + + // Returns the file's write buffer. + auto get_write_buffer() const -> span { + auto ptr = this->file_->_IO_write_ptr; + return {ptr, to_unsigned(this->file_->_IO_buf_end - ptr)}; + } + + void advance_write_buffer(size_t size) { this->file_->_IO_write_ptr += size; } + + bool needs_flush() const { + if ((this->file_->_flags & line_buffered) == 0) return false; + char* end = this->file_->_IO_write_end; + return memchr(end, '\n', to_unsigned(this->file_->_IO_write_ptr - end)); + } + + void flush() { fflush_unlocked(this->file_); } +}; + +// A FILE wrapper for Apple's libc. +template class apple_file : public file_base { + private: + enum { + line_buffered = 1, // __SNBF + unbuffered = 2 // __SLBF + }; + + public: + using file_base::file_base; + + auto is_buffered() const -> bool { + return (this->file_->_flags & unbuffered) == 0; + } + + void init_buffer() { + if (this->file_->_p) return; + // Force buffer initialization by placing and removing a char in a buffer. + putc_unlocked(0, this->file_); + --this->file_->_p; + ++this->file_->_w; + } + + auto get_read_buffer() const -> span { + return {reinterpret_cast(this->file_->_p), + to_unsigned(this->file_->_r)}; + } + + auto get_write_buffer() const -> span { + return {reinterpret_cast(this->file_->_p), + to_unsigned(this->file_->_bf._base + this->file_->_bf._size - + this->file_->_p)}; + } + + void advance_write_buffer(size_t size) { + this->file_->_p += size; + this->file_->_w -= size; + } + + bool needs_flush() const { + if ((this->file_->_flags & line_buffered) == 0) return false; + return memchr(this->file_->_p + this->file_->_w, '\n', + to_unsigned(-this->file_->_w)); + } +}; + +// A fallback FILE wrapper. +template class fallback_file : public file_base { + private: + char next_; // The next unconsumed character in the buffer. + bool has_next_ = false; + + public: + using file_base::file_base; + + auto is_buffered() const -> bool { return false; } + auto needs_flush() const -> bool { return false; } + void init_buffer() {} + + auto get_read_buffer() const -> span { + return {&next_, has_next_ ? 1u : 0u}; + } + + auto get_write_buffer() const -> span { return {nullptr, 0}; } + + void advance_write_buffer(size_t) {} + + auto get() -> int { + has_next_ = false; + return file_base::get(); + } + + void unget(char c) { + file_base::unget(c); + next_ = c; + has_next_ = true; + } +}; + +#ifndef FMT_USE_FALLBACK_FILE +# define FMT_USE_FALLBACK_FILE 1 +#endif + +template +auto get_file(F* f, int) -> apple_file { + return f; +} +template +inline auto get_file(F* f, int) -> glibc_file { + return f; +} + +inline auto get_file(FILE* f, ...) -> fallback_file { return f; } + +using file_ref = decltype(get_file(static_cast(nullptr), 0)); + +template +class file_print_buffer : public buffer { + public: + explicit file_print_buffer(F*) : buffer(nullptr, size_t()) {} +}; + +template +class file_print_buffer::value>> + : public buffer { + private: + file_ref file_; + + static void grow(buffer& base, size_t) { + auto& self = static_cast(base); + self.file_.advance_write_buffer(self.size()); + if (self.file_.get_write_buffer().size == 0) self.file_.flush(); + auto buf = self.file_.get_write_buffer(); + FMT_ASSERT(buf.size > 0, ""); + self.set(buf.data, buf.size); + self.clear(); + } + + public: + explicit file_print_buffer(F* f) : buffer(grow, size_t()), file_(f) { + flockfile(f); + file_.init_buffer(); + auto buf = file_.get_write_buffer(); + set(buf.data, buf.size); + } + ~file_print_buffer() { + file_.advance_write_buffer(size()); + bool flush = file_.needs_flush(); + F* f = file_; // Make funlockfile depend on the template parameter F + funlockfile(f); // for the system API detection to work. + if (flush) fflush(file_); + } +}; + +#if !defined(_WIN32) || defined(FMT_USE_WRITE_CONSOLE) +FMT_FUNC auto write_console(int, string_view) -> bool { return false; } +#else +using dword = conditional_t; +extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( // + void*, const void*, dword, dword*, void*); + +FMT_FUNC bool write_console(int fd, string_view text) { + auto u16 = utf8_to_utf16(text); + return WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), u16.c_str(), + static_cast(u16.size()), nullptr, nullptr) != 0; +} +#endif + +#ifdef _WIN32 +// Print assuming legacy (non-Unicode) encoding. +FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args, + bool newline) { + auto buffer = memory_buffer(); + detail::vformat_to(buffer, fmt, args); + if (newline) buffer.push_back('\n'); + fwrite_fully(buffer.data(), buffer.size(), f); +} +#endif + +FMT_FUNC void print(std::FILE* f, string_view text) { +#if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE) + int fd = _fileno(f); + if (_isatty(fd)) { + std::fflush(f); + if (write_console(fd, text)) return; + } +#endif + fwrite_fully(text.data(), text.size(), f); +} +} // namespace detail + +FMT_FUNC void vprint_buffered(std::FILE* f, string_view fmt, format_args args) { + auto buffer = memory_buffer(); + detail::vformat_to(buffer, fmt, args); + detail::print(f, {buffer.data(), buffer.size()}); +} + +FMT_FUNC void vprint(std::FILE* f, string_view fmt, format_args args) { + if (!detail::file_ref(f).is_buffered() || !detail::has_flockfile<>()) + return vprint_buffered(f, fmt, args); + auto&& buffer = detail::file_print_buffer<>(f); + return detail::vformat_to(buffer, fmt, args); +} + +FMT_FUNC void vprintln(std::FILE* f, string_view fmt, format_args args) { + auto buffer = memory_buffer(); + detail::vformat_to(buffer, fmt, args); + buffer.push_back('\n'); + detail::print(f, {buffer.data(), buffer.size()}); +} + +FMT_FUNC void vprint(string_view fmt, format_args args) { + vprint(stdout, fmt, args); +} + +namespace detail { + +struct singleton { + unsigned char upper; + unsigned char lower_count; +}; + +inline auto is_printable(uint16_t x, const singleton* singletons, + size_t singletons_size, + const unsigned char* singleton_lowers, + const unsigned char* normal, size_t normal_size) + -> bool { + auto upper = x >> 8; + auto lower_start = 0; + for (size_t i = 0; i < singletons_size; ++i) { + auto s = singletons[i]; + auto lower_end = lower_start + s.lower_count; + if (upper < s.upper) break; + if (upper == s.upper) { + for (auto j = lower_start; j < lower_end; ++j) { + if (singleton_lowers[j] == (x & 0xff)) return false; + } + } + lower_start = lower_end; + } + + auto xsigned = static_cast(x); + auto current = true; + for (size_t i = 0; i < normal_size; ++i) { + auto v = static_cast(normal[i]); + auto len = (v & 0x80) != 0 ? (v & 0x7f) << 8 | normal[++i] : v; + xsigned -= len; + if (xsigned < 0) break; + current = !current; + } + return current; +} + +// This code is generated by support/printable.py. +FMT_FUNC auto is_printable(uint32_t cp) -> bool { + static constexpr singleton singletons0[] = { + {0x00, 1}, {0x03, 5}, {0x05, 6}, {0x06, 3}, {0x07, 6}, {0x08, 8}, + {0x09, 17}, {0x0a, 28}, {0x0b, 25}, {0x0c, 20}, {0x0d, 16}, {0x0e, 13}, + {0x0f, 4}, {0x10, 3}, {0x12, 18}, {0x13, 9}, {0x16, 1}, {0x17, 5}, + {0x18, 2}, {0x19, 3}, {0x1a, 7}, {0x1c, 2}, {0x1d, 1}, {0x1f, 22}, + {0x20, 3}, {0x2b, 3}, {0x2c, 2}, {0x2d, 11}, {0x2e, 1}, {0x30, 3}, + {0x31, 2}, {0x32, 1}, {0xa7, 2}, {0xa9, 2}, {0xaa, 4}, {0xab, 8}, + {0xfa, 2}, {0xfb, 5}, {0xfd, 4}, {0xfe, 3}, {0xff, 9}, + }; + static constexpr unsigned char singletons0_lower[] = { + 0xad, 0x78, 0x79, 0x8b, 0x8d, 0xa2, 0x30, 0x57, 0x58, 0x8b, 0x8c, 0x90, + 0x1c, 0x1d, 0xdd, 0x0e, 0x0f, 0x4b, 0x4c, 0xfb, 0xfc, 0x2e, 0x2f, 0x3f, + 0x5c, 0x5d, 0x5f, 0xb5, 0xe2, 0x84, 0x8d, 0x8e, 0x91, 0x92, 0xa9, 0xb1, + 0xba, 0xbb, 0xc5, 0xc6, 0xc9, 0xca, 0xde, 0xe4, 0xe5, 0xff, 0x00, 0x04, + 0x11, 0x12, 0x29, 0x31, 0x34, 0x37, 0x3a, 0x3b, 0x3d, 0x49, 0x4a, 0x5d, + 0x84, 0x8e, 0x92, 0xa9, 0xb1, 0xb4, 0xba, 0xbb, 0xc6, 0xca, 0xce, 0xcf, + 0xe4, 0xe5, 0x00, 0x04, 0x0d, 0x0e, 0x11, 0x12, 0x29, 0x31, 0x34, 0x3a, + 0x3b, 0x45, 0x46, 0x49, 0x4a, 0x5e, 0x64, 0x65, 0x84, 0x91, 0x9b, 0x9d, + 0xc9, 0xce, 0xcf, 0x0d, 0x11, 0x29, 0x45, 0x49, 0x57, 0x64, 0x65, 0x8d, + 0x91, 0xa9, 0xb4, 0xba, 0xbb, 0xc5, 0xc9, 0xdf, 0xe4, 0xe5, 0xf0, 0x0d, + 0x11, 0x45, 0x49, 0x64, 0x65, 0x80, 0x84, 0xb2, 0xbc, 0xbe, 0xbf, 0xd5, + 0xd7, 0xf0, 0xf1, 0x83, 0x85, 0x8b, 0xa4, 0xa6, 0xbe, 0xbf, 0xc5, 0xc7, + 0xce, 0xcf, 0xda, 0xdb, 0x48, 0x98, 0xbd, 0xcd, 0xc6, 0xce, 0xcf, 0x49, + 0x4e, 0x4f, 0x57, 0x59, 0x5e, 0x5f, 0x89, 0x8e, 0x8f, 0xb1, 0xb6, 0xb7, + 0xbf, 0xc1, 0xc6, 0xc7, 0xd7, 0x11, 0x16, 0x17, 0x5b, 0x5c, 0xf6, 0xf7, + 0xfe, 0xff, 0x80, 0x0d, 0x6d, 0x71, 0xde, 0xdf, 0x0e, 0x0f, 0x1f, 0x6e, + 0x6f, 0x1c, 0x1d, 0x5f, 0x7d, 0x7e, 0xae, 0xaf, 0xbb, 0xbc, 0xfa, 0x16, + 0x17, 0x1e, 0x1f, 0x46, 0x47, 0x4e, 0x4f, 0x58, 0x5a, 0x5c, 0x5e, 0x7e, + 0x7f, 0xb5, 0xc5, 0xd4, 0xd5, 0xdc, 0xf0, 0xf1, 0xf5, 0x72, 0x73, 0x8f, + 0x74, 0x75, 0x96, 0x2f, 0x5f, 0x26, 0x2e, 0x2f, 0xa7, 0xaf, 0xb7, 0xbf, + 0xc7, 0xcf, 0xd7, 0xdf, 0x9a, 0x40, 0x97, 0x98, 0x30, 0x8f, 0x1f, 0xc0, + 0xc1, 0xce, 0xff, 0x4e, 0x4f, 0x5a, 0x5b, 0x07, 0x08, 0x0f, 0x10, 0x27, + 0x2f, 0xee, 0xef, 0x6e, 0x6f, 0x37, 0x3d, 0x3f, 0x42, 0x45, 0x90, 0x91, + 0xfe, 0xff, 0x53, 0x67, 0x75, 0xc8, 0xc9, 0xd0, 0xd1, 0xd8, 0xd9, 0xe7, + 0xfe, 0xff, + }; + static constexpr singleton singletons1[] = { + {0x00, 6}, {0x01, 1}, {0x03, 1}, {0x04, 2}, {0x08, 8}, {0x09, 2}, + {0x0a, 5}, {0x0b, 2}, {0x0e, 4}, {0x10, 1}, {0x11, 2}, {0x12, 5}, + {0x13, 17}, {0x14, 1}, {0x15, 2}, {0x17, 2}, {0x19, 13}, {0x1c, 5}, + {0x1d, 8}, {0x24, 1}, {0x6a, 3}, {0x6b, 2}, {0xbc, 2}, {0xd1, 2}, + {0xd4, 12}, {0xd5, 9}, {0xd6, 2}, {0xd7, 2}, {0xda, 1}, {0xe0, 5}, + {0xe1, 2}, {0xe8, 2}, {0xee, 32}, {0xf0, 4}, {0xf8, 2}, {0xf9, 2}, + {0xfa, 2}, {0xfb, 1}, + }; + static constexpr unsigned char singletons1_lower[] = { + 0x0c, 0x27, 0x3b, 0x3e, 0x4e, 0x4f, 0x8f, 0x9e, 0x9e, 0x9f, 0x06, 0x07, + 0x09, 0x36, 0x3d, 0x3e, 0x56, 0xf3, 0xd0, 0xd1, 0x04, 0x14, 0x18, 0x36, + 0x37, 0x56, 0x57, 0x7f, 0xaa, 0xae, 0xaf, 0xbd, 0x35, 0xe0, 0x12, 0x87, + 0x89, 0x8e, 0x9e, 0x04, 0x0d, 0x0e, 0x11, 0x12, 0x29, 0x31, 0x34, 0x3a, + 0x45, 0x46, 0x49, 0x4a, 0x4e, 0x4f, 0x64, 0x65, 0x5c, 0xb6, 0xb7, 0x1b, + 0x1c, 0x07, 0x08, 0x0a, 0x0b, 0x14, 0x17, 0x36, 0x39, 0x3a, 0xa8, 0xa9, + 0xd8, 0xd9, 0x09, 0x37, 0x90, 0x91, 0xa8, 0x07, 0x0a, 0x3b, 0x3e, 0x66, + 0x69, 0x8f, 0x92, 0x6f, 0x5f, 0xee, 0xef, 0x5a, 0x62, 0x9a, 0x9b, 0x27, + 0x28, 0x55, 0x9d, 0xa0, 0xa1, 0xa3, 0xa4, 0xa7, 0xa8, 0xad, 0xba, 0xbc, + 0xc4, 0x06, 0x0b, 0x0c, 0x15, 0x1d, 0x3a, 0x3f, 0x45, 0x51, 0xa6, 0xa7, + 0xcc, 0xcd, 0xa0, 0x07, 0x19, 0x1a, 0x22, 0x25, 0x3e, 0x3f, 0xc5, 0xc6, + 0x04, 0x20, 0x23, 0x25, 0x26, 0x28, 0x33, 0x38, 0x3a, 0x48, 0x4a, 0x4c, + 0x50, 0x53, 0x55, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x63, 0x65, 0x66, + 0x6b, 0x73, 0x78, 0x7d, 0x7f, 0x8a, 0xa4, 0xaa, 0xaf, 0xb0, 0xc0, 0xd0, + 0xae, 0xaf, 0x79, 0xcc, 0x6e, 0x6f, 0x93, + }; + static constexpr unsigned char normal0[] = { + 0x00, 0x20, 0x5f, 0x22, 0x82, 0xdf, 0x04, 0x82, 0x44, 0x08, 0x1b, 0x04, + 0x06, 0x11, 0x81, 0xac, 0x0e, 0x80, 0xab, 0x35, 0x28, 0x0b, 0x80, 0xe0, + 0x03, 0x19, 0x08, 0x01, 0x04, 0x2f, 0x04, 0x34, 0x04, 0x07, 0x03, 0x01, + 0x07, 0x06, 0x07, 0x11, 0x0a, 0x50, 0x0f, 0x12, 0x07, 0x55, 0x07, 0x03, + 0x04, 0x1c, 0x0a, 0x09, 0x03, 0x08, 0x03, 0x07, 0x03, 0x02, 0x03, 0x03, + 0x03, 0x0c, 0x04, 0x05, 0x03, 0x0b, 0x06, 0x01, 0x0e, 0x15, 0x05, 0x3a, + 0x03, 0x11, 0x07, 0x06, 0x05, 0x10, 0x07, 0x57, 0x07, 0x02, 0x07, 0x15, + 0x0d, 0x50, 0x04, 0x43, 0x03, 0x2d, 0x03, 0x01, 0x04, 0x11, 0x06, 0x0f, + 0x0c, 0x3a, 0x04, 0x1d, 0x25, 0x5f, 0x20, 0x6d, 0x04, 0x6a, 0x25, 0x80, + 0xc8, 0x05, 0x82, 0xb0, 0x03, 0x1a, 0x06, 0x82, 0xfd, 0x03, 0x59, 0x07, + 0x15, 0x0b, 0x17, 0x09, 0x14, 0x0c, 0x14, 0x0c, 0x6a, 0x06, 0x0a, 0x06, + 0x1a, 0x06, 0x59, 0x07, 0x2b, 0x05, 0x46, 0x0a, 0x2c, 0x04, 0x0c, 0x04, + 0x01, 0x03, 0x31, 0x0b, 0x2c, 0x04, 0x1a, 0x06, 0x0b, 0x03, 0x80, 0xac, + 0x06, 0x0a, 0x06, 0x21, 0x3f, 0x4c, 0x04, 0x2d, 0x03, 0x74, 0x08, 0x3c, + 0x03, 0x0f, 0x03, 0x3c, 0x07, 0x38, 0x08, 0x2b, 0x05, 0x82, 0xff, 0x11, + 0x18, 0x08, 0x2f, 0x11, 0x2d, 0x03, 0x20, 0x10, 0x21, 0x0f, 0x80, 0x8c, + 0x04, 0x82, 0x97, 0x19, 0x0b, 0x15, 0x88, 0x94, 0x05, 0x2f, 0x05, 0x3b, + 0x07, 0x02, 0x0e, 0x18, 0x09, 0x80, 0xb3, 0x2d, 0x74, 0x0c, 0x80, 0xd6, + 0x1a, 0x0c, 0x05, 0x80, 0xff, 0x05, 0x80, 0xdf, 0x0c, 0xee, 0x0d, 0x03, + 0x84, 0x8d, 0x03, 0x37, 0x09, 0x81, 0x5c, 0x14, 0x80, 0xb8, 0x08, 0x80, + 0xcb, 0x2a, 0x38, 0x03, 0x0a, 0x06, 0x38, 0x08, 0x46, 0x08, 0x0c, 0x06, + 0x74, 0x0b, 0x1e, 0x03, 0x5a, 0x04, 0x59, 0x09, 0x80, 0x83, 0x18, 0x1c, + 0x0a, 0x16, 0x09, 0x4c, 0x04, 0x80, 0x8a, 0x06, 0xab, 0xa4, 0x0c, 0x17, + 0x04, 0x31, 0xa1, 0x04, 0x81, 0xda, 0x26, 0x07, 0x0c, 0x05, 0x05, 0x80, + 0xa5, 0x11, 0x81, 0x6d, 0x10, 0x78, 0x28, 0x2a, 0x06, 0x4c, 0x04, 0x80, + 0x8d, 0x04, 0x80, 0xbe, 0x03, 0x1b, 0x03, 0x0f, 0x0d, + }; + static constexpr unsigned char normal1[] = { + 0x5e, 0x22, 0x7b, 0x05, 0x03, 0x04, 0x2d, 0x03, 0x66, 0x03, 0x01, 0x2f, + 0x2e, 0x80, 0x82, 0x1d, 0x03, 0x31, 0x0f, 0x1c, 0x04, 0x24, 0x09, 0x1e, + 0x05, 0x2b, 0x05, 0x44, 0x04, 0x0e, 0x2a, 0x80, 0xaa, 0x06, 0x24, 0x04, + 0x24, 0x04, 0x28, 0x08, 0x34, 0x0b, 0x01, 0x80, 0x90, 0x81, 0x37, 0x09, + 0x16, 0x0a, 0x08, 0x80, 0x98, 0x39, 0x03, 0x63, 0x08, 0x09, 0x30, 0x16, + 0x05, 0x21, 0x03, 0x1b, 0x05, 0x01, 0x40, 0x38, 0x04, 0x4b, 0x05, 0x2f, + 0x04, 0x0a, 0x07, 0x09, 0x07, 0x40, 0x20, 0x27, 0x04, 0x0c, 0x09, 0x36, + 0x03, 0x3a, 0x05, 0x1a, 0x07, 0x04, 0x0c, 0x07, 0x50, 0x49, 0x37, 0x33, + 0x0d, 0x33, 0x07, 0x2e, 0x08, 0x0a, 0x81, 0x26, 0x52, 0x4e, 0x28, 0x08, + 0x2a, 0x56, 0x1c, 0x14, 0x17, 0x09, 0x4e, 0x04, 0x1e, 0x0f, 0x43, 0x0e, + 0x19, 0x07, 0x0a, 0x06, 0x48, 0x08, 0x27, 0x09, 0x75, 0x0b, 0x3f, 0x41, + 0x2a, 0x06, 0x3b, 0x05, 0x0a, 0x06, 0x51, 0x06, 0x01, 0x05, 0x10, 0x03, + 0x05, 0x80, 0x8b, 0x62, 0x1e, 0x48, 0x08, 0x0a, 0x80, 0xa6, 0x5e, 0x22, + 0x45, 0x0b, 0x0a, 0x06, 0x0d, 0x13, 0x39, 0x07, 0x0a, 0x36, 0x2c, 0x04, + 0x10, 0x80, 0xc0, 0x3c, 0x64, 0x53, 0x0c, 0x48, 0x09, 0x0a, 0x46, 0x45, + 0x1b, 0x48, 0x08, 0x53, 0x1d, 0x39, 0x81, 0x07, 0x46, 0x0a, 0x1d, 0x03, + 0x47, 0x49, 0x37, 0x03, 0x0e, 0x08, 0x0a, 0x06, 0x39, 0x07, 0x0a, 0x81, + 0x36, 0x19, 0x80, 0xb7, 0x01, 0x0f, 0x32, 0x0d, 0x83, 0x9b, 0x66, 0x75, + 0x0b, 0x80, 0xc4, 0x8a, 0xbc, 0x84, 0x2f, 0x8f, 0xd1, 0x82, 0x47, 0xa1, + 0xb9, 0x82, 0x39, 0x07, 0x2a, 0x04, 0x02, 0x60, 0x26, 0x0a, 0x46, 0x0a, + 0x28, 0x05, 0x13, 0x82, 0xb0, 0x5b, 0x65, 0x4b, 0x04, 0x39, 0x07, 0x11, + 0x40, 0x05, 0x0b, 0x02, 0x0e, 0x97, 0xf8, 0x08, 0x84, 0xd6, 0x2a, 0x09, + 0xa2, 0xf7, 0x81, 0x1f, 0x31, 0x03, 0x11, 0x04, 0x08, 0x81, 0x8c, 0x89, + 0x04, 0x6b, 0x05, 0x0d, 0x03, 0x09, 0x07, 0x10, 0x93, 0x60, 0x80, 0xf6, + 0x0a, 0x73, 0x08, 0x6e, 0x17, 0x46, 0x80, 0x9a, 0x14, 0x0c, 0x57, 0x09, + 0x19, 0x80, 0x87, 0x81, 0x47, 0x03, 0x85, 0x42, 0x0f, 0x15, 0x85, 0x50, + 0x2b, 0x80, 0xd5, 0x2d, 0x03, 0x1a, 0x04, 0x02, 0x81, 0x70, 0x3a, 0x05, + 0x01, 0x85, 0x00, 0x80, 0xd7, 0x29, 0x4c, 0x04, 0x0a, 0x04, 0x02, 0x83, + 0x11, 0x44, 0x4c, 0x3d, 0x80, 0xc2, 0x3c, 0x06, 0x01, 0x04, 0x55, 0x05, + 0x1b, 0x34, 0x02, 0x81, 0x0e, 0x2c, 0x04, 0x64, 0x0c, 0x56, 0x0a, 0x80, + 0xae, 0x38, 0x1d, 0x0d, 0x2c, 0x04, 0x09, 0x07, 0x02, 0x0e, 0x06, 0x80, + 0x9a, 0x83, 0xd8, 0x08, 0x0d, 0x03, 0x0d, 0x03, 0x74, 0x0c, 0x59, 0x07, + 0x0c, 0x14, 0x0c, 0x04, 0x38, 0x08, 0x0a, 0x06, 0x28, 0x08, 0x22, 0x4e, + 0x81, 0x54, 0x0c, 0x15, 0x03, 0x03, 0x05, 0x07, 0x09, 0x19, 0x07, 0x07, + 0x09, 0x03, 0x0d, 0x07, 0x29, 0x80, 0xcb, 0x25, 0x0a, 0x84, 0x06, + }; + auto lower = static_cast(cp); + if (cp < 0x10000) { + return is_printable(lower, singletons0, + sizeof(singletons0) / sizeof(*singletons0), + singletons0_lower, normal0, sizeof(normal0)); + } + if (cp < 0x20000) { + return is_printable(lower, singletons1, + sizeof(singletons1) / sizeof(*singletons1), + singletons1_lower, normal1, sizeof(normal1)); + } + if (0x2a6de <= cp && cp < 0x2a700) return false; + if (0x2b735 <= cp && cp < 0x2b740) return false; + if (0x2b81e <= cp && cp < 0x2b820) return false; + if (0x2cea2 <= cp && cp < 0x2ceb0) return false; + if (0x2ebe1 <= cp && cp < 0x2f800) return false; + if (0x2fa1e <= cp && cp < 0x30000) return false; + if (0x3134b <= cp && cp < 0xe0100) return false; + if (0xe01f0 <= cp && cp < 0x110000) return false; + return cp < 0x110000; +} + +} // namespace detail + +FMT_END_NAMESPACE + +#endif // FMT_FORMAT_INL_H_ diff --git a/include/spdlog/fmt/bundled/format.h b/include/spdlog/fmt/bundled/format.h new file mode 100644 index 0000000..67f0ab7 --- /dev/null +++ b/include/spdlog/fmt/bundled/format.h @@ -0,0 +1,4427 @@ +/* + Formatting library for C++ + + Copyright (c) 2012 - present, Victor Zverovich + + 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. + + --- Optional exception to the license --- + + As an exception, if, as a result of your compiling your source code, portions + of this Software are embedded into a machine-executable object form of such + source code, you may redistribute such embedded portions in such object form + without including the above copyright and permission notices. + */ + +#ifndef FMT_FORMAT_H_ +#define FMT_FORMAT_H_ + +#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES +# define _LIBCPP_REMOVE_TRANSITIVE_INCLUDES +# define FMT_REMOVE_TRANSITIVE_INCLUDES +#endif + +#include "base.h" + +#ifndef FMT_MODULE +# include // std::signbit +# include // uint32_t +# include // std::memcpy +# include // std::initializer_list +# include // std::numeric_limits +# if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI) +// Workaround for pre gcc 5 libstdc++. +# include // std::allocator_traits +# endif +# include // std::runtime_error +# include // std::string +# include // std::system_error + +// Checking FMT_CPLUSPLUS for warning suppression in MSVC. +# if FMT_HAS_INCLUDE() && FMT_CPLUSPLUS > 201703L +# include // std::bit_cast +# endif + +// libc++ supports string_view in pre-c++17. +# if FMT_HAS_INCLUDE() && \ + (FMT_CPLUSPLUS >= 201703L || defined(_LIBCPP_VERSION)) +# include +# define FMT_USE_STRING_VIEW +# endif +#endif // FMT_MODULE + +#if defined __cpp_inline_variables && __cpp_inline_variables >= 201606L +# define FMT_INLINE_VARIABLE inline +#else +# define FMT_INLINE_VARIABLE +#endif + +#ifndef FMT_NO_UNIQUE_ADDRESS +# if FMT_CPLUSPLUS >= 202002L +# if FMT_HAS_CPP_ATTRIBUTE(no_unique_address) +# define FMT_NO_UNIQUE_ADDRESS [[no_unique_address]] +// VS2019 v16.10 and later except clang-cl (https://reviews.llvm.org/D110485). +# elif (FMT_MSC_VERSION >= 1929) && !FMT_CLANG_VERSION +# define FMT_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] +# endif +# endif +#endif +#ifndef FMT_NO_UNIQUE_ADDRESS +# define FMT_NO_UNIQUE_ADDRESS +#endif + +// Visibility when compiled as a shared library/object. +#if defined(FMT_LIB_EXPORT) || defined(FMT_SHARED) +# define FMT_SO_VISIBILITY(value) FMT_VISIBILITY(value) +#else +# define FMT_SO_VISIBILITY(value) +#endif + +#ifdef __has_builtin +# define FMT_HAS_BUILTIN(x) __has_builtin(x) +#else +# define FMT_HAS_BUILTIN(x) 0 +#endif + +#if FMT_GCC_VERSION || FMT_CLANG_VERSION +# define FMT_NOINLINE __attribute__((noinline)) +#else +# define FMT_NOINLINE +#endif + +namespace std { +template <> struct iterator_traits { + using iterator_category = output_iterator_tag; + using value_type = char; +}; +} // namespace std + +#ifndef FMT_THROW +# if FMT_EXCEPTIONS +# if FMT_MSC_VERSION || defined(__NVCC__) +FMT_BEGIN_NAMESPACE +namespace detail { +template inline void do_throw(const Exception& x) { + // Silence unreachable code warnings in MSVC and NVCC because these + // are nearly impossible to fix in a generic code. + volatile bool b = true; + if (b) throw x; +} +} // namespace detail +FMT_END_NAMESPACE +# define FMT_THROW(x) detail::do_throw(x) +# else +# define FMT_THROW(x) throw x +# endif +# else +# define FMT_THROW(x) \ + ::fmt::detail::assert_fail(__FILE__, __LINE__, (x).what()) +# endif +#endif + +#ifndef FMT_MAYBE_UNUSED +# if FMT_HAS_CPP17_ATTRIBUTE(maybe_unused) +# define FMT_MAYBE_UNUSED [[maybe_unused]] +# else +# define FMT_MAYBE_UNUSED +# endif +#endif + +#ifndef FMT_USE_USER_DEFINED_LITERALS +// EDG based compilers (Intel, NVIDIA, Elbrus, etc), GCC and MSVC support UDLs. +// +// GCC before 4.9 requires a space in `operator"" _a` which is invalid in later +// compiler versions. +# if (FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 409 || \ + FMT_MSC_VERSION >= 1900) && \ + (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= /* UDL feature */ 480) +# define FMT_USE_USER_DEFINED_LITERALS 1 +# else +# define FMT_USE_USER_DEFINED_LITERALS 0 +# endif +#endif + +// Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of +// integer formatter template instantiations to just one by only using the +// largest integer type. This results in a reduction in binary size but will +// cause a decrease in integer formatting performance. +#if !defined(FMT_REDUCE_INT_INSTANTIATIONS) +# define FMT_REDUCE_INT_INSTANTIATIONS 0 +#endif + +// __builtin_clz is broken in clang with Microsoft CodeGen: +// https://github.com/fmtlib/fmt/issues/519. +#if !FMT_MSC_VERSION +# if FMT_HAS_BUILTIN(__builtin_clz) || FMT_GCC_VERSION || FMT_ICC_VERSION +# define FMT_BUILTIN_CLZ(n) __builtin_clz(n) +# endif +# if FMT_HAS_BUILTIN(__builtin_clzll) || FMT_GCC_VERSION || FMT_ICC_VERSION +# define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) +# endif +#endif + +// __builtin_ctz is broken in Intel Compiler Classic on Windows: +// https://github.com/fmtlib/fmt/issues/2510. +#ifndef __ICL +# if FMT_HAS_BUILTIN(__builtin_ctz) || FMT_GCC_VERSION || FMT_ICC_VERSION || \ + defined(__NVCOMPILER) +# define FMT_BUILTIN_CTZ(n) __builtin_ctz(n) +# endif +# if FMT_HAS_BUILTIN(__builtin_ctzll) || FMT_GCC_VERSION || \ + FMT_ICC_VERSION || defined(__NVCOMPILER) +# define FMT_BUILTIN_CTZLL(n) __builtin_ctzll(n) +# endif +#endif + +#if FMT_MSC_VERSION +# include // _BitScanReverse[64], _BitScanForward[64], _umul128 +#endif + +// Some compilers masquerade as both MSVC and GCC-likes or otherwise support +// __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the +// MSVC intrinsics if the clz and clzll builtins are not available. +#if FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL) && \ + !defined(FMT_BUILTIN_CTZLL) +FMT_BEGIN_NAMESPACE +namespace detail { +// Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning. +# if !defined(__clang__) +# pragma intrinsic(_BitScanForward) +# pragma intrinsic(_BitScanReverse) +# if defined(_WIN64) +# pragma intrinsic(_BitScanForward64) +# pragma intrinsic(_BitScanReverse64) +# endif +# endif + +inline auto clz(uint32_t x) -> int { + unsigned long r = 0; + _BitScanReverse(&r, x); + FMT_ASSERT(x != 0, ""); + // Static analysis complains about using uninitialized data + // "r", but the only way that can happen is if "x" is 0, + // which the callers guarantee to not happen. + FMT_MSC_WARNING(suppress : 6102) + return 31 ^ static_cast(r); +} +# define FMT_BUILTIN_CLZ(n) detail::clz(n) + +inline auto clzll(uint64_t x) -> int { + unsigned long r = 0; +# ifdef _WIN64 + _BitScanReverse64(&r, x); +# else + // Scan the high 32 bits. + if (_BitScanReverse(&r, static_cast(x >> 32))) + return 63 ^ static_cast(r + 32); + // Scan the low 32 bits. + _BitScanReverse(&r, static_cast(x)); +# endif + FMT_ASSERT(x != 0, ""); + FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning. + return 63 ^ static_cast(r); +} +# define FMT_BUILTIN_CLZLL(n) detail::clzll(n) + +inline auto ctz(uint32_t x) -> int { + unsigned long r = 0; + _BitScanForward(&r, x); + FMT_ASSERT(x != 0, ""); + FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning. + return static_cast(r); +} +# define FMT_BUILTIN_CTZ(n) detail::ctz(n) + +inline auto ctzll(uint64_t x) -> int { + unsigned long r = 0; + FMT_ASSERT(x != 0, ""); + FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning. +# ifdef _WIN64 + _BitScanForward64(&r, x); +# else + // Scan the low 32 bits. + if (_BitScanForward(&r, static_cast(x))) return static_cast(r); + // Scan the high 32 bits. + _BitScanForward(&r, static_cast(x >> 32)); + r += 32; +# endif + return static_cast(r); +} +# define FMT_BUILTIN_CTZLL(n) detail::ctzll(n) +} // namespace detail +FMT_END_NAMESPACE +#endif + +FMT_BEGIN_NAMESPACE + +template +struct is_contiguous> + : std::true_type {}; + +namespace detail { + +FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) { + ignore_unused(condition); +#ifdef FMT_FUZZ + if (condition) throw std::runtime_error("fuzzing limit reached"); +#endif +} + +#if defined(FMT_USE_STRING_VIEW) +template using std_string_view = std::basic_string_view; +#else +template struct std_string_view {}; +#endif + +// Implementation of std::bit_cast for pre-C++20. +template +FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To { +#ifdef __cpp_lib_bit_cast + if (is_constant_evaluated()) return std::bit_cast(from); +#endif + auto to = To(); + // The cast suppresses a bogus -Wclass-memaccess on GCC. + std::memcpy(static_cast(&to), &from, sizeof(to)); + return to; +} + +inline auto is_big_endian() -> bool { +#ifdef _WIN32 + return false; +#elif defined(__BIG_ENDIAN__) + return true; +#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) + return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; +#else + struct bytes { + char data[sizeof(int)]; + }; + return bit_cast(1).data[0] == 0; +#endif +} + +class uint128_fallback { + private: + uint64_t lo_, hi_; + + public: + constexpr uint128_fallback(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {} + constexpr uint128_fallback(uint64_t value = 0) : lo_(value), hi_(0) {} + + constexpr auto high() const noexcept -> uint64_t { return hi_; } + constexpr auto low() const noexcept -> uint64_t { return lo_; } + + template ::value)> + constexpr explicit operator T() const { + return static_cast(lo_); + } + + friend constexpr auto operator==(const uint128_fallback& lhs, + const uint128_fallback& rhs) -> bool { + return lhs.hi_ == rhs.hi_ && lhs.lo_ == rhs.lo_; + } + friend constexpr auto operator!=(const uint128_fallback& lhs, + const uint128_fallback& rhs) -> bool { + return !(lhs == rhs); + } + friend constexpr auto operator>(const uint128_fallback& lhs, + const uint128_fallback& rhs) -> bool { + return lhs.hi_ != rhs.hi_ ? lhs.hi_ > rhs.hi_ : lhs.lo_ > rhs.lo_; + } + friend constexpr auto operator|(const uint128_fallback& lhs, + const uint128_fallback& rhs) + -> uint128_fallback { + return {lhs.hi_ | rhs.hi_, lhs.lo_ | rhs.lo_}; + } + friend constexpr auto operator&(const uint128_fallback& lhs, + const uint128_fallback& rhs) + -> uint128_fallback { + return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_}; + } + friend constexpr auto operator~(const uint128_fallback& n) + -> uint128_fallback { + return {~n.hi_, ~n.lo_}; + } + friend auto operator+(const uint128_fallback& lhs, + const uint128_fallback& rhs) -> uint128_fallback { + auto result = uint128_fallback(lhs); + result += rhs; + return result; + } + friend auto operator*(const uint128_fallback& lhs, uint32_t rhs) + -> uint128_fallback { + FMT_ASSERT(lhs.hi_ == 0, ""); + uint64_t hi = (lhs.lo_ >> 32) * rhs; + uint64_t lo = (lhs.lo_ & ~uint32_t()) * rhs; + uint64_t new_lo = (hi << 32) + lo; + return {(hi >> 32) + (new_lo < lo ? 1 : 0), new_lo}; + } + friend auto operator-(const uint128_fallback& lhs, uint64_t rhs) + -> uint128_fallback { + return {lhs.hi_ - (lhs.lo_ < rhs ? 1 : 0), lhs.lo_ - rhs}; + } + FMT_CONSTEXPR auto operator>>(int shift) const -> uint128_fallback { + if (shift == 64) return {0, hi_}; + if (shift > 64) return uint128_fallback(0, hi_) >> (shift - 64); + return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)}; + } + FMT_CONSTEXPR auto operator<<(int shift) const -> uint128_fallback { + if (shift == 64) return {lo_, 0}; + if (shift > 64) return uint128_fallback(lo_, 0) << (shift - 64); + return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)}; + } + FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& { + return *this = *this >> shift; + } + FMT_CONSTEXPR void operator+=(uint128_fallback n) { + uint64_t new_lo = lo_ + n.lo_; + uint64_t new_hi = hi_ + n.hi_ + (new_lo < lo_ ? 1 : 0); + FMT_ASSERT(new_hi >= hi_, ""); + lo_ = new_lo; + hi_ = new_hi; + } + FMT_CONSTEXPR void operator&=(uint128_fallback n) { + lo_ &= n.lo_; + hi_ &= n.hi_; + } + + FMT_CONSTEXPR20 auto operator+=(uint64_t n) noexcept -> uint128_fallback& { + if (is_constant_evaluated()) { + lo_ += n; + hi_ += (lo_ < n ? 1 : 0); + return *this; + } +#if FMT_HAS_BUILTIN(__builtin_addcll) && !defined(__ibmxl__) + unsigned long long carry; + lo_ = __builtin_addcll(lo_, n, 0, &carry); + hi_ += carry; +#elif FMT_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) && !defined(__ibmxl__) + unsigned long long result; + auto carry = __builtin_ia32_addcarryx_u64(0, lo_, n, &result); + lo_ = result; + hi_ += carry; +#elif defined(_MSC_VER) && defined(_M_X64) + auto carry = _addcarry_u64(0, lo_, n, &lo_); + _addcarry_u64(carry, hi_, 0, &hi_); +#else + lo_ += n; + hi_ += (lo_ < n ? 1 : 0); +#endif + return *this; + } +}; + +using uint128_t = conditional_t; + +#ifdef UINTPTR_MAX +using uintptr_t = ::uintptr_t; +#else +using uintptr_t = uint128_t; +#endif + +// Returns the largest possible value for type T. Same as +// std::numeric_limits::max() but shorter and not affected by the max macro. +template constexpr auto max_value() -> T { + return (std::numeric_limits::max)(); +} +template constexpr auto num_bits() -> int { + return std::numeric_limits::digits; +} +// std::numeric_limits::digits may return 0 for 128-bit ints. +template <> constexpr auto num_bits() -> int { return 128; } +template <> constexpr auto num_bits() -> int { return 128; } +template <> constexpr auto num_bits() -> int { return 128; } + +// A heterogeneous bit_cast used for converting 96-bit long double to uint128_t +// and 128-bit pointers to uint128_fallback. +template sizeof(From))> +inline auto bit_cast(const From& from) -> To { + constexpr auto size = static_cast(sizeof(From) / sizeof(unsigned)); + struct data_t { + unsigned value[static_cast(size)]; + } data = bit_cast(from); + auto result = To(); + if (const_check(is_big_endian())) { + for (int i = 0; i < size; ++i) + result = (result << num_bits()) | data.value[i]; + } else { + for (int i = size - 1; i >= 0; --i) + result = (result << num_bits()) | data.value[i]; + } + return result; +} + +template +FMT_CONSTEXPR20 inline auto countl_zero_fallback(UInt n) -> int { + int lz = 0; + constexpr UInt msb_mask = static_cast(1) << (num_bits() - 1); + for (; (n & msb_mask) == 0; n <<= 1) lz++; + return lz; +} + +FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int { +#ifdef FMT_BUILTIN_CLZ + if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n); +#endif + return countl_zero_fallback(n); +} + +FMT_CONSTEXPR20 inline auto countl_zero(uint64_t n) -> int { +#ifdef FMT_BUILTIN_CLZLL + if (!is_constant_evaluated()) return FMT_BUILTIN_CLZLL(n); +#endif + return countl_zero_fallback(n); +} + +FMT_INLINE void assume(bool condition) { + (void)condition; +#if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION + __builtin_assume(condition); +#elif FMT_GCC_VERSION + if (!condition) __builtin_unreachable(); +#endif +} + +// An approximation of iterator_t for pre-C++20 systems. +template +using iterator_t = decltype(std::begin(std::declval())); +template using sentinel_t = decltype(std::end(std::declval())); + +// A workaround for std::string not having mutable data() until C++17. +template +inline auto get_data(std::basic_string& s) -> Char* { + return &s[0]; +} +template +inline auto get_data(Container& c) -> typename Container::value_type* { + return c.data(); +} + +// Attempts to reserve space for n extra characters in the output range. +// Returns a pointer to the reserved range or a reference to it. +template ::value&& + is_contiguous::value)> +#if FMT_CLANG_VERSION >= 307 && !FMT_ICC_VERSION +__attribute__((no_sanitize("undefined"))) +#endif +inline auto +reserve(OutputIt it, size_t n) -> typename OutputIt::value_type* { + auto& c = get_container(it); + size_t size = c.size(); + c.resize(size + n); + return get_data(c) + size; +} + +template +inline auto reserve(basic_appender it, size_t n) -> basic_appender { + buffer& buf = get_container(it); + buf.try_reserve(buf.size() + n); + return it; +} + +template +constexpr auto reserve(Iterator& it, size_t) -> Iterator& { + return it; +} + +template +using reserve_iterator = + remove_reference_t(), 0))>; + +template +constexpr auto to_pointer(OutputIt, size_t) -> T* { + return nullptr; +} +template auto to_pointer(basic_appender it, size_t n) -> T* { + buffer& buf = get_container(it); + auto size = buf.size(); + buf.try_reserve(size + n); + if (buf.capacity() < size + n) return nullptr; + buf.try_resize(size + n); + return buf.data() + size; +} + +template ::value&& + is_contiguous::value)> +inline auto base_iterator(OutputIt it, + typename OutputIt::container_type::value_type*) + -> OutputIt { + return it; +} + +template +constexpr auto base_iterator(Iterator, Iterator it) -> Iterator { + return it; +} + +// is spectacularly slow to compile in C++20 so use a simple fill_n +// instead (#1998). +template +FMT_CONSTEXPR auto fill_n(OutputIt out, Size count, const T& value) + -> OutputIt { + for (Size i = 0; i < count; ++i) *out++ = value; + return out; +} +template +FMT_CONSTEXPR20 auto fill_n(T* out, Size count, char value) -> T* { + if (is_constant_evaluated()) { + return fill_n(out, count, value); + } + std::memset(out, value, to_unsigned(count)); + return out + count; +} + +template +FMT_CONSTEXPR FMT_NOINLINE auto copy_noinline(InputIt begin, InputIt end, + OutputIt out) -> OutputIt { + return copy(begin, end, out); +} + +// A public domain branchless UTF-8 decoder by Christopher Wellons: +// https://github.com/skeeto/branchless-utf8 +/* Decode the next character, c, from s, reporting errors in e. + * + * Since this is a branchless decoder, four bytes will be read from the + * buffer regardless of the actual length of the next character. This + * means the buffer _must_ have at least three bytes of zero padding + * following the end of the data stream. + * + * Errors are reported in e, which will be non-zero if the parsed + * character was somehow invalid: invalid byte sequence, non-canonical + * encoding, or a surrogate half. + * + * The function returns a pointer to the next character. When an error + * occurs, this pointer will be a guess that depends on the particular + * error, but it will always advance at least one byte. + */ +FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e) + -> const char* { + constexpr const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07}; + constexpr const uint32_t mins[] = {4194304, 0, 128, 2048, 65536}; + constexpr const int shiftc[] = {0, 18, 12, 6, 0}; + constexpr const int shifte[] = {0, 6, 4, 2, 0}; + + int len = "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4" + [static_cast(*s) >> 3]; + // Compute the pointer to the next character early so that the next + // iteration can start working on the next character. Neither Clang + // nor GCC figure out this reordering on their own. + const char* next = s + len + !len; + + using uchar = unsigned char; + + // Assume a four-byte character and load four bytes. Unused bits are + // shifted out. + *c = uint32_t(uchar(s[0]) & masks[len]) << 18; + *c |= uint32_t(uchar(s[1]) & 0x3f) << 12; + *c |= uint32_t(uchar(s[2]) & 0x3f) << 6; + *c |= uint32_t(uchar(s[3]) & 0x3f) << 0; + *c >>= shiftc[len]; + + // Accumulate the various error conditions. + *e = (*c < mins[len]) << 6; // non-canonical encoding + *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half? + *e |= (*c > 0x10FFFF) << 8; // out of range? + *e |= (uchar(s[1]) & 0xc0) >> 2; + *e |= (uchar(s[2]) & 0xc0) >> 4; + *e |= uchar(s[3]) >> 6; + *e ^= 0x2a; // top two bits of each tail byte correct? + *e >>= shifte[len]; + + return next; +} + +constexpr FMT_INLINE_VARIABLE uint32_t invalid_code_point = ~uint32_t(); + +// Invokes f(cp, sv) for every code point cp in s with sv being the string view +// corresponding to the code point. cp is invalid_code_point on error. +template +FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) { + auto decode = [f](const char* buf_ptr, const char* ptr) { + auto cp = uint32_t(); + auto error = 0; + auto end = utf8_decode(buf_ptr, &cp, &error); + bool result = f(error ? invalid_code_point : cp, + string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr))); + return result ? (error ? buf_ptr + 1 : end) : nullptr; + }; + auto p = s.data(); + const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars. + if (s.size() >= block_size) { + for (auto end = p + s.size() - block_size + 1; p < end;) { + p = decode(p, p); + if (!p) return; + } + } + if (auto num_chars_left = s.data() + s.size() - p) { + char buf[2 * block_size - 1] = {}; + copy(p, p + num_chars_left, buf); + const char* buf_ptr = buf; + do { + auto end = decode(buf_ptr, p); + if (!end) return; + p += end - buf_ptr; + buf_ptr = end; + } while (buf_ptr - buf < num_chars_left); + } +} + +template +inline auto compute_width(basic_string_view s) -> size_t { + return s.size(); +} + +// Computes approximate display width of a UTF-8 string. +FMT_CONSTEXPR inline auto compute_width(string_view s) -> size_t { + size_t num_code_points = 0; + // It is not a lambda for compatibility with C++14. + struct count_code_points { + size_t* count; + FMT_CONSTEXPR auto operator()(uint32_t cp, string_view) const -> bool { + *count += detail::to_unsigned( + 1 + + (cp >= 0x1100 && + (cp <= 0x115f || // Hangul Jamo init. consonants + cp == 0x2329 || // LEFT-POINTING ANGLE BRACKET + cp == 0x232a || // RIGHT-POINTING ANGLE BRACKET + // CJK ... Yi except IDEOGRAPHIC HALF FILL SPACE: + (cp >= 0x2e80 && cp <= 0xa4cf && cp != 0x303f) || + (cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables + (cp >= 0xf900 && cp <= 0xfaff) || // CJK Compatibility Ideographs + (cp >= 0xfe10 && cp <= 0xfe19) || // Vertical Forms + (cp >= 0xfe30 && cp <= 0xfe6f) || // CJK Compatibility Forms + (cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms + (cp >= 0xffe0 && cp <= 0xffe6) || // Fullwidth Forms + (cp >= 0x20000 && cp <= 0x2fffd) || // CJK + (cp >= 0x30000 && cp <= 0x3fffd) || + // Miscellaneous Symbols and Pictographs + Emoticons: + (cp >= 0x1f300 && cp <= 0x1f64f) || + // Supplemental Symbols and Pictographs: + (cp >= 0x1f900 && cp <= 0x1f9ff)))); + return true; + } + }; + // We could avoid branches by using utf8_decode directly. + for_each_codepoint(s, count_code_points{&num_code_points}); + return num_code_points; +} + +template +inline auto code_point_index(basic_string_view s, size_t n) -> size_t { + size_t size = s.size(); + return n < size ? n : size; +} + +// Calculates the index of the nth code point in a UTF-8 string. +inline auto code_point_index(string_view s, size_t n) -> size_t { + size_t result = s.size(); + const char* begin = s.begin(); + for_each_codepoint(s, [begin, &n, &result](uint32_t, string_view sv) { + if (n != 0) { + --n; + return true; + } + result = to_unsigned(sv.begin() - begin); + return false; + }); + return result; +} + +template struct is_integral : std::is_integral {}; +template <> struct is_integral : std::true_type {}; +template <> struct is_integral : std::true_type {}; + +template +using is_signed = + std::integral_constant::is_signed || + std::is_same::value>; + +template +using is_integer = + bool_constant::value && !std::is_same::value && + !std::is_same::value && + !std::is_same::value>; + +#ifndef FMT_USE_FLOAT +# define FMT_USE_FLOAT 1 +#endif +#ifndef FMT_USE_DOUBLE +# define FMT_USE_DOUBLE 1 +#endif +#ifndef FMT_USE_LONG_DOUBLE +# define FMT_USE_LONG_DOUBLE 1 +#endif + +#if defined(FMT_USE_FLOAT128) +// Use the provided definition. +#elif FMT_CLANG_VERSION && FMT_HAS_INCLUDE() +# define FMT_USE_FLOAT128 1 +#elif FMT_GCC_VERSION && defined(_GLIBCXX_USE_FLOAT128) && \ + !defined(__STRICT_ANSI__) +# define FMT_USE_FLOAT128 1 +#else +# define FMT_USE_FLOAT128 0 +#endif +#if FMT_USE_FLOAT128 +using float128 = __float128; +#else +using float128 = void; +#endif + +template using is_float128 = std::is_same; + +template +using is_floating_point = + bool_constant::value || is_float128::value>; + +template ::value> +struct is_fast_float : bool_constant::is_iec559 && + sizeof(T) <= sizeof(double)> {}; +template struct is_fast_float : std::false_type {}; + +template +using is_double_double = bool_constant::digits == 106>; + +#ifndef FMT_USE_FULL_CACHE_DRAGONBOX +# define FMT_USE_FULL_CACHE_DRAGONBOX 0 +#endif + +template +struct is_locale : std::false_type {}; +template +struct is_locale> : std::true_type {}; +} // namespace detail + +FMT_BEGIN_EXPORT + +// The number of characters to store in the basic_memory_buffer object itself +// to avoid dynamic memory allocation. +enum { inline_buffer_size = 500 }; + +/** + * A dynamically growing memory buffer for trivially copyable/constructible + * types with the first `SIZE` elements stored in the object itself. Most + * commonly used via the `memory_buffer` alias for `char`. + * + * **Example**: + * + * auto out = fmt::memory_buffer(); + * fmt::format_to(std::back_inserter(out), "The answer is {}.", 42); + * + * This will append "The answer is 42." to `out`. The buffer content can be + * converted to `std::string` with `to_string(out)`. + */ +template > +class basic_memory_buffer : public detail::buffer { + private: + T store_[SIZE]; + + // Don't inherit from Allocator to avoid generating type_info for it. + FMT_NO_UNIQUE_ADDRESS Allocator alloc_; + + // Deallocate memory allocated by the buffer. + FMT_CONSTEXPR20 void deallocate() { + T* data = this->data(); + if (data != store_) alloc_.deallocate(data, this->capacity()); + } + + static FMT_CONSTEXPR20 void grow(detail::buffer& buf, size_t size) { + detail::abort_fuzzing_if(size > 5000); + auto& self = static_cast(buf); + const size_t max_size = + std::allocator_traits::max_size(self.alloc_); + size_t old_capacity = buf.capacity(); + size_t new_capacity = old_capacity + old_capacity / 2; + if (size > new_capacity) + new_capacity = size; + else if (new_capacity > max_size) + new_capacity = size > max_size ? size : max_size; + T* old_data = buf.data(); + T* new_data = self.alloc_.allocate(new_capacity); + // Suppress a bogus -Wstringop-overflow in gcc 13.1 (#3481). + detail::assume(buf.size() <= new_capacity); + // The following code doesn't throw, so the raw pointer above doesn't leak. + memcpy(new_data, old_data, buf.size() * sizeof(T)); + self.set(new_data, new_capacity); + // deallocate must not throw according to the standard, but even if it does, + // the buffer already uses the new storage and will deallocate it in + // destructor. + if (old_data != self.store_) self.alloc_.deallocate(old_data, old_capacity); + } + + public: + using value_type = T; + using const_reference = const T&; + + FMT_CONSTEXPR20 explicit basic_memory_buffer( + const Allocator& alloc = Allocator()) + : detail::buffer(grow), alloc_(alloc) { + this->set(store_, SIZE); + if (detail::is_constant_evaluated()) detail::fill_n(store_, SIZE, T()); + } + FMT_CONSTEXPR20 ~basic_memory_buffer() { deallocate(); } + + private: + // Move data from other to this buffer. + FMT_CONSTEXPR20 void move(basic_memory_buffer& other) { + alloc_ = std::move(other.alloc_); + T* data = other.data(); + size_t size = other.size(), capacity = other.capacity(); + if (data == other.store_) { + this->set(store_, capacity); + detail::copy(other.store_, other.store_ + size, store_); + } else { + this->set(data, capacity); + // Set pointer to the inline array so that delete is not called + // when deallocating. + other.set(other.store_, 0); + other.clear(); + } + this->resize(size); + } + + public: + /// Constructs a `basic_memory_buffer` object moving the content of the other + /// object to it. + FMT_CONSTEXPR20 basic_memory_buffer(basic_memory_buffer&& other) noexcept + : detail::buffer(grow) { + move(other); + } + + /// Moves the content of the other `basic_memory_buffer` object to this one. + auto operator=(basic_memory_buffer&& other) noexcept -> basic_memory_buffer& { + FMT_ASSERT(this != &other, ""); + deallocate(); + move(other); + return *this; + } + + // Returns a copy of the allocator associated with this buffer. + auto get_allocator() const -> Allocator { return alloc_; } + + /// Resizes the buffer to contain `count` elements. If T is a POD type new + /// elements may not be initialized. + FMT_CONSTEXPR20 void resize(size_t count) { this->try_resize(count); } + + /// Increases the buffer capacity to `new_capacity`. + void reserve(size_t new_capacity) { this->try_reserve(new_capacity); } + + using detail::buffer::append; + template + void append(const ContiguousRange& range) { + append(range.data(), range.data() + range.size()); + } +}; + +using memory_buffer = basic_memory_buffer; + +template +struct is_contiguous> : std::true_type { +}; + +FMT_END_EXPORT +namespace detail { +FMT_API auto write_console(int fd, string_view text) -> bool; +FMT_API void print(std::FILE*, string_view); +} // namespace detail + +FMT_BEGIN_EXPORT + +// Suppress a misleading warning in older versions of clang. +#if FMT_CLANG_VERSION +# pragma clang diagnostic ignored "-Wweak-vtables" +#endif + +/// An error reported from a formatting function. +class FMT_SO_VISIBILITY("default") format_error : public std::runtime_error { + public: + using std::runtime_error::runtime_error; +}; + +namespace detail_exported { +#if FMT_USE_NONTYPE_TEMPLATE_ARGS +template struct fixed_string { + constexpr fixed_string(const Char (&str)[N]) { + detail::copy(static_cast(str), + str + N, data); + } + Char data[N] = {}; +}; +#endif + +// Converts a compile-time string to basic_string_view. +template +constexpr auto compile_string_to_view(const Char (&s)[N]) + -> basic_string_view { + // Remove trailing NUL character if needed. Won't be present if this is used + // with a raw character array (i.e. not defined as a string). + return {s, N - (std::char_traits::to_int_type(s[N - 1]) == 0 ? 1 : 0)}; +} +template +constexpr auto compile_string_to_view(basic_string_view s) + -> basic_string_view { + return s; +} +} // namespace detail_exported + +// A generic formatting context with custom output iterator and character +// (code unit) support. Char is the format string code unit type which can be +// different from OutputIt::value_type. +template class generic_context { + private: + OutputIt out_; + basic_format_args args_; + detail::locale_ref loc_; + + public: + using char_type = Char; + using iterator = OutputIt; + using parse_context_type = basic_format_parse_context; + template using formatter_type = formatter; + + constexpr generic_context(OutputIt out, + basic_format_args ctx_args, + detail::locale_ref loc = {}) + : out_(out), args_(ctx_args), loc_(loc) {} + generic_context(generic_context&&) = default; + generic_context(const generic_context&) = delete; + void operator=(const generic_context&) = delete; + + constexpr auto arg(int id) const -> basic_format_arg { + return args_.get(id); + } + auto arg(basic_string_view name) -> basic_format_arg { + return args_.get(name); + } + FMT_CONSTEXPR auto arg_id(basic_string_view name) -> int { + return args_.get_id(name); + } + auto args() const -> const basic_format_args& { + return args_; + } + + FMT_CONSTEXPR auto out() -> iterator { return out_; } + + void advance_to(iterator it) { + if (!detail::is_back_insert_iterator()) out_ = it; + } + + FMT_CONSTEXPR auto locale() -> detail::locale_ref { return loc_; } +}; + +class loc_value { + private: + basic_format_arg value_; + + public: + template ::value)> + loc_value(T value) : value_(detail::make_arg(value)) {} + + template ::value)> + loc_value(T) {} + + template auto visit(Visitor&& vis) -> decltype(vis(0)) { + return value_.visit(vis); + } +}; + +// A locale facet that formats values in UTF-8. +// It is parameterized on the locale to avoid the heavy include. +template class format_facet : public Locale::facet { + private: + std::string separator_; + std::string grouping_; + std::string decimal_point_; + + protected: + virtual auto do_put(appender out, loc_value val, + const format_specs& specs) const -> bool; + + public: + static FMT_API typename Locale::id id; + + explicit format_facet(Locale& loc); + explicit format_facet(string_view sep = "", + std::initializer_list g = {3}, + std::string decimal_point = ".") + : separator_(sep.data(), sep.size()), + grouping_(g.begin(), g.end()), + decimal_point_(decimal_point) {} + + auto put(appender out, loc_value val, const format_specs& specs) const + -> bool { + return do_put(out, val, specs); + } +}; + +FMT_END_EXPORT + +namespace detail { + +// Returns true if value is negative, false otherwise. +// Same as `value < 0` but doesn't produce warnings if T is an unsigned type. +template ::value)> +constexpr auto is_negative(T value) -> bool { + return value < 0; +} +template ::value)> +constexpr auto is_negative(T) -> bool { + return false; +} + +template +FMT_CONSTEXPR auto is_supported_floating_point(T) -> bool { + if (std::is_same()) return FMT_USE_FLOAT; + if (std::is_same()) return FMT_USE_DOUBLE; + if (std::is_same()) return FMT_USE_LONG_DOUBLE; + return true; +} + +// Smallest of uint32_t, uint64_t, uint128_t that is large enough to +// represent all values of an integral type T. +template +using uint32_or_64_or_128_t = + conditional_t() <= 32 && !FMT_REDUCE_INT_INSTANTIATIONS, + uint32_t, + conditional_t() <= 64, uint64_t, uint128_t>>; +template +using uint64_or_128_t = conditional_t() <= 64, uint64_t, uint128_t>; + +#define FMT_POWERS_OF_10(factor) \ + factor * 10, (factor) * 100, (factor) * 1000, (factor) * 10000, \ + (factor) * 100000, (factor) * 1000000, (factor) * 10000000, \ + (factor) * 100000000, (factor) * 1000000000 + +// Converts value in the range [0, 100) to a string. +constexpr auto digits2(size_t value) -> const char* { + // GCC generates slightly better code when value is pointer-size. + return &"0001020304050607080910111213141516171819" + "2021222324252627282930313233343536373839" + "4041424344454647484950515253545556575859" + "6061626364656667686970717273747576777879" + "8081828384858687888990919293949596979899"[value * 2]; +} + +// Sign is a template parameter to workaround a bug in gcc 4.8. +template constexpr auto sign(Sign s) -> Char { +#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 604 + static_assert(std::is_same::value, ""); +#endif + return static_cast(((' ' << 24) | ('+' << 16) | ('-' << 8)) >> (s * 8)); +} + +template FMT_CONSTEXPR auto count_digits_fallback(T n) -> int { + int count = 1; + for (;;) { + // Integer division is slow so do it for a group of four digits instead + // of for every digit. The idea comes from the talk by Alexandrescu + // "Three Optimization Tips for C++". See speed-test for a comparison. + if (n < 10) return count; + if (n < 100) return count + 1; + if (n < 1000) return count + 2; + if (n < 10000) return count + 3; + n /= 10000u; + count += 4; + } +} +#if FMT_USE_INT128 +FMT_CONSTEXPR inline auto count_digits(uint128_opt n) -> int { + return count_digits_fallback(n); +} +#endif + +#ifdef FMT_BUILTIN_CLZLL +// It is a separate function rather than a part of count_digits to workaround +// the lack of static constexpr in constexpr functions. +inline auto do_count_digits(uint64_t n) -> int { + // This has comparable performance to the version by Kendall Willets + // (https://github.com/fmtlib/format-benchmark/blob/master/digits10) + // but uses smaller tables. + // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)). + static constexpr uint8_t bsr2log10[] = { + 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, + 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, + 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, + 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20}; + auto t = bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63]; + static constexpr const uint64_t zero_or_powers_of_10[] = { + 0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL), + 10000000000000000000ULL}; + return t - (n < zero_or_powers_of_10[t]); +} +#endif + +// Returns the number of decimal digits in n. Leading zeros are not counted +// except for n == 0 in which case count_digits returns 1. +FMT_CONSTEXPR20 inline auto count_digits(uint64_t n) -> int { +#ifdef FMT_BUILTIN_CLZLL + if (!is_constant_evaluated()) return do_count_digits(n); +#endif + return count_digits_fallback(n); +} + +// Counts the number of digits in n. BITS = log2(radix). +template +FMT_CONSTEXPR auto count_digits(UInt n) -> int { +#ifdef FMT_BUILTIN_CLZ + if (!is_constant_evaluated() && num_bits() == 32) + return (FMT_BUILTIN_CLZ(static_cast(n) | 1) ^ 31) / BITS + 1; +#endif + // Lambda avoids unreachable code warnings from NVHPC. + return [](UInt m) { + int num_digits = 0; + do { + ++num_digits; + } while ((m >>= BITS) != 0); + return num_digits; + }(n); +} + +#ifdef FMT_BUILTIN_CLZ +// It is a separate function rather than a part of count_digits to workaround +// the lack of static constexpr in constexpr functions. +FMT_INLINE auto do_count_digits(uint32_t n) -> int { +// An optimization by Kendall Willets from https://bit.ly/3uOIQrB. +// This increments the upper 32 bits (log10(T) - 1) when >= T is added. +# define FMT_INC(T) (((sizeof(#T) - 1ull) << 32) - T) + static constexpr uint64_t table[] = { + FMT_INC(0), FMT_INC(0), FMT_INC(0), // 8 + FMT_INC(10), FMT_INC(10), FMT_INC(10), // 64 + FMT_INC(100), FMT_INC(100), FMT_INC(100), // 512 + FMT_INC(1000), FMT_INC(1000), FMT_INC(1000), // 4096 + FMT_INC(10000), FMT_INC(10000), FMT_INC(10000), // 32k + FMT_INC(100000), FMT_INC(100000), FMT_INC(100000), // 256k + FMT_INC(1000000), FMT_INC(1000000), FMT_INC(1000000), // 2048k + FMT_INC(10000000), FMT_INC(10000000), FMT_INC(10000000), // 16M + FMT_INC(100000000), FMT_INC(100000000), FMT_INC(100000000), // 128M + FMT_INC(1000000000), FMT_INC(1000000000), FMT_INC(1000000000), // 1024M + FMT_INC(1000000000), FMT_INC(1000000000) // 4B + }; + auto inc = table[FMT_BUILTIN_CLZ(n | 1) ^ 31]; + return static_cast((n + inc) >> 32); +} +#endif + +// Optional version of count_digits for better performance on 32-bit platforms. +FMT_CONSTEXPR20 inline auto count_digits(uint32_t n) -> int { +#ifdef FMT_BUILTIN_CLZ + if (!is_constant_evaluated()) { + return do_count_digits(n); + } +#endif + return count_digits_fallback(n); +} + +template constexpr auto digits10() noexcept -> int { + return std::numeric_limits::digits10; +} +template <> constexpr auto digits10() noexcept -> int { return 38; } +template <> constexpr auto digits10() noexcept -> int { return 38; } + +template struct thousands_sep_result { + std::string grouping; + Char thousands_sep; +}; + +template +FMT_API auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result; +template +inline auto thousands_sep(locale_ref loc) -> thousands_sep_result { + auto result = thousands_sep_impl(loc); + return {result.grouping, Char(result.thousands_sep)}; +} +template <> +inline auto thousands_sep(locale_ref loc) -> thousands_sep_result { + return thousands_sep_impl(loc); +} + +template +FMT_API auto decimal_point_impl(locale_ref loc) -> Char; +template inline auto decimal_point(locale_ref loc) -> Char { + return Char(decimal_point_impl(loc)); +} +template <> inline auto decimal_point(locale_ref loc) -> wchar_t { + return decimal_point_impl(loc); +} + +// Compares two characters for equality. +template auto equal2(const Char* lhs, const char* rhs) -> bool { + return lhs[0] == Char(rhs[0]) && lhs[1] == Char(rhs[1]); +} +inline auto equal2(const char* lhs, const char* rhs) -> bool { + return memcmp(lhs, rhs, 2) == 0; +} + +// Copies two characters from src to dst. +template +FMT_CONSTEXPR20 FMT_INLINE void copy2(Char* dst, const char* src) { + if (!is_constant_evaluated() && sizeof(Char) == sizeof(char)) { + memcpy(dst, src, 2); + return; + } + *dst++ = static_cast(*src++); + *dst = static_cast(*src); +} + +template struct format_decimal_result { + Iterator begin; + Iterator end; +}; + +// Formats a decimal unsigned integer value writing into out pointing to a +// buffer of specified size. The caller must ensure that the buffer is large +// enough. +template +FMT_CONSTEXPR20 auto format_decimal(Char* out, UInt value, int size) + -> format_decimal_result { + FMT_ASSERT(size >= count_digits(value), "invalid digit count"); + out += size; + Char* end = out; + while (value >= 100) { + // Integer division is slow so do it for a group of two digits instead + // of for every digit. The idea comes from the talk by Alexandrescu + // "Three Optimization Tips for C++". See speed-test for a comparison. + out -= 2; + copy2(out, digits2(static_cast(value % 100))); + value /= 100; + } + if (value < 10) { + *--out = static_cast('0' + value); + return {out, end}; + } + out -= 2; + copy2(out, digits2(static_cast(value))); + return {out, end}; +} + +template >::value)> +FMT_CONSTEXPR inline auto format_decimal(Iterator out, UInt value, int size) + -> format_decimal_result { + // Buffer is large enough to hold all digits (digits10 + 1). + Char buffer[digits10() + 1] = {}; + auto end = format_decimal(buffer, value, size).end; + return {out, detail::copy_noinline(buffer, end, out)}; +} + +template +FMT_CONSTEXPR auto format_uint(Char* buffer, UInt value, int num_digits, + bool upper = false) -> Char* { + buffer += num_digits; + Char* end = buffer; + do { + const char* digits = upper ? "0123456789ABCDEF" : "0123456789abcdef"; + unsigned digit = static_cast(value & ((1 << BASE_BITS) - 1)); + *--buffer = static_cast(BASE_BITS < 4 ? static_cast('0' + digit) + : digits[digit]); + } while ((value >>= BASE_BITS) != 0); + return end; +} + +template +FMT_CONSTEXPR inline auto format_uint(It out, UInt value, int num_digits, + bool upper = false) -> It { + if (auto ptr = to_pointer(out, to_unsigned(num_digits))) { + format_uint(ptr, value, num_digits, upper); + return out; + } + // Buffer should be large enough to hold all digits (digits / BASE_BITS + 1). + char buffer[num_bits() / BASE_BITS + 1] = {}; + format_uint(buffer, value, num_digits, upper); + return detail::copy_noinline(buffer, buffer + num_digits, out); +} + +// A converter from UTF-8 to UTF-16. +class utf8_to_utf16 { + private: + basic_memory_buffer buffer_; + + public: + FMT_API explicit utf8_to_utf16(string_view s); + operator basic_string_view() const { return {&buffer_[0], size()}; } + auto size() const -> size_t { return buffer_.size() - 1; } + auto c_str() const -> const wchar_t* { return &buffer_[0]; } + auto str() const -> std::wstring { return {&buffer_[0], size()}; } +}; + +enum class to_utf8_error_policy { abort, replace }; + +// A converter from UTF-16/UTF-32 (host endian) to UTF-8. +template class to_utf8 { + private: + Buffer buffer_; + + public: + to_utf8() {} + explicit to_utf8(basic_string_view s, + to_utf8_error_policy policy = to_utf8_error_policy::abort) { + static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4, + "Expect utf16 or utf32"); + if (!convert(s, policy)) + FMT_THROW(std::runtime_error(sizeof(WChar) == 2 ? "invalid utf16" + : "invalid utf32")); + } + operator string_view() const { return string_view(&buffer_[0], size()); } + auto size() const -> size_t { return buffer_.size() - 1; } + auto c_str() const -> const char* { return &buffer_[0]; } + auto str() const -> std::string { return std::string(&buffer_[0], size()); } + + // Performs conversion returning a bool instead of throwing exception on + // conversion error. This method may still throw in case of memory allocation + // error. + auto convert(basic_string_view s, + to_utf8_error_policy policy = to_utf8_error_policy::abort) + -> bool { + if (!convert(buffer_, s, policy)) return false; + buffer_.push_back(0); + return true; + } + static auto convert(Buffer& buf, basic_string_view s, + to_utf8_error_policy policy = to_utf8_error_policy::abort) + -> bool { + for (auto p = s.begin(); p != s.end(); ++p) { + uint32_t c = static_cast(*p); + if (sizeof(WChar) == 2 && c >= 0xd800 && c <= 0xdfff) { + // Handle a surrogate pair. + ++p; + if (p == s.end() || (c & 0xfc00) != 0xd800 || (*p & 0xfc00) != 0xdc00) { + if (policy == to_utf8_error_policy::abort) return false; + buf.append(string_view("\xEF\xBF\xBD")); + --p; + } else { + c = (c << 10) + static_cast(*p) - 0x35fdc00; + } + } else if (c < 0x80) { + buf.push_back(static_cast(c)); + } else if (c < 0x800) { + buf.push_back(static_cast(0xc0 | (c >> 6))); + buf.push_back(static_cast(0x80 | (c & 0x3f))); + } else if ((c >= 0x800 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xffff)) { + buf.push_back(static_cast(0xe0 | (c >> 12))); + buf.push_back(static_cast(0x80 | ((c & 0xfff) >> 6))); + buf.push_back(static_cast(0x80 | (c & 0x3f))); + } else if (c >= 0x10000 && c <= 0x10ffff) { + buf.push_back(static_cast(0xf0 | (c >> 18))); + buf.push_back(static_cast(0x80 | ((c & 0x3ffff) >> 12))); + buf.push_back(static_cast(0x80 | ((c & 0xfff) >> 6))); + buf.push_back(static_cast(0x80 | (c & 0x3f))); + } else { + return false; + } + } + return true; + } +}; + +// Computes 128-bit result of multiplication of two 64-bit unsigned integers. +inline auto umul128(uint64_t x, uint64_t y) noexcept -> uint128_fallback { +#if FMT_USE_INT128 + auto p = static_cast(x) * static_cast(y); + return {static_cast(p >> 64), static_cast(p)}; +#elif defined(_MSC_VER) && defined(_M_X64) + auto hi = uint64_t(); + auto lo = _umul128(x, y, &hi); + return {hi, lo}; +#else + const uint64_t mask = static_cast(max_value()); + + uint64_t a = x >> 32; + uint64_t b = x & mask; + uint64_t c = y >> 32; + uint64_t d = y & mask; + + uint64_t ac = a * c; + uint64_t bc = b * c; + uint64_t ad = a * d; + uint64_t bd = b * d; + + uint64_t intermediate = (bd >> 32) + (ad & mask) + (bc & mask); + + return {ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32), + (intermediate << 32) + (bd & mask)}; +#endif +} + +namespace dragonbox { +// Computes floor(log10(pow(2, e))) for e in [-2620, 2620] using the method from +// https://fmt.dev/papers/Dragonbox.pdf#page=28, section 6.1. +inline auto floor_log10_pow2(int e) noexcept -> int { + FMT_ASSERT(e <= 2620 && e >= -2620, "too large exponent"); + static_assert((-1 >> 1) == -1, "right shift is not arithmetic"); + return (e * 315653) >> 20; +} + +inline auto floor_log2_pow10(int e) noexcept -> int { + FMT_ASSERT(e <= 1233 && e >= -1233, "too large exponent"); + return (e * 1741647) >> 19; +} + +// Computes upper 64 bits of multiplication of two 64-bit unsigned integers. +inline auto umul128_upper64(uint64_t x, uint64_t y) noexcept -> uint64_t { +#if FMT_USE_INT128 + auto p = static_cast(x) * static_cast(y); + return static_cast(p >> 64); +#elif defined(_MSC_VER) && defined(_M_X64) + return __umulh(x, y); +#else + return umul128(x, y).high(); +#endif +} + +// Computes upper 128 bits of multiplication of a 64-bit unsigned integer and a +// 128-bit unsigned integer. +inline auto umul192_upper128(uint64_t x, uint128_fallback y) noexcept + -> uint128_fallback { + uint128_fallback r = umul128(x, y.high()); + r += umul128_upper64(x, y.low()); + return r; +} + +FMT_API auto get_cached_power(int k) noexcept -> uint128_fallback; + +// Type-specific information that Dragonbox uses. +template struct float_info; + +template <> struct float_info { + using carrier_uint = uint32_t; + static const int exponent_bits = 8; + static const int kappa = 1; + static const int big_divisor = 100; + static const int small_divisor = 10; + static const int min_k = -31; + static const int max_k = 46; + static const int shorter_interval_tie_lower_threshold = -35; + static const int shorter_interval_tie_upper_threshold = -35; +}; + +template <> struct float_info { + using carrier_uint = uint64_t; + static const int exponent_bits = 11; + static const int kappa = 2; + static const int big_divisor = 1000; + static const int small_divisor = 100; + static const int min_k = -292; + static const int max_k = 341; + static const int shorter_interval_tie_lower_threshold = -77; + static const int shorter_interval_tie_upper_threshold = -77; +}; + +// An 80- or 128-bit floating point number. +template +struct float_info::digits == 64 || + std::numeric_limits::digits == 113 || + is_float128::value>> { + using carrier_uint = detail::uint128_t; + static const int exponent_bits = 15; +}; + +// A double-double floating point number. +template +struct float_info::value>> { + using carrier_uint = detail::uint128_t; +}; + +template struct decimal_fp { + using significand_type = typename float_info::carrier_uint; + significand_type significand; + int exponent; +}; + +template FMT_API auto to_decimal(T x) noexcept -> decimal_fp; +} // namespace dragonbox + +// Returns true iff Float has the implicit bit which is not stored. +template constexpr auto has_implicit_bit() -> bool { + // An 80-bit FP number has a 64-bit significand an no implicit bit. + return std::numeric_limits::digits != 64; +} + +// Returns the number of significand bits stored in Float. The implicit bit is +// not counted since it is not stored. +template constexpr auto num_significand_bits() -> int { + // std::numeric_limits may not support __float128. + return is_float128() ? 112 + : (std::numeric_limits::digits - + (has_implicit_bit() ? 1 : 0)); +} + +template +constexpr auto exponent_mask() -> + typename dragonbox::float_info::carrier_uint { + using float_uint = typename dragonbox::float_info::carrier_uint; + return ((float_uint(1) << dragonbox::float_info::exponent_bits) - 1) + << num_significand_bits(); +} +template constexpr auto exponent_bias() -> int { + // std::numeric_limits may not support __float128. + return is_float128() ? 16383 + : std::numeric_limits::max_exponent - 1; +} + +// Writes the exponent exp in the form "[+-]d{2,3}" to buffer. +template +FMT_CONSTEXPR auto write_exponent(int exp, It it) -> It { + FMT_ASSERT(-10000 < exp && exp < 10000, "exponent out of range"); + if (exp < 0) { + *it++ = static_cast('-'); + exp = -exp; + } else { + *it++ = static_cast('+'); + } + if (exp >= 100) { + const char* top = digits2(to_unsigned(exp / 100)); + if (exp >= 1000) *it++ = static_cast(top[0]); + *it++ = static_cast(top[1]); + exp %= 100; + } + const char* d = digits2(to_unsigned(exp)); + *it++ = static_cast(d[0]); + *it++ = static_cast(d[1]); + return it; +} + +// A floating-point number f * pow(2, e) where F is an unsigned type. +template struct basic_fp { + F f; + int e; + + static constexpr const int num_significand_bits = + static_cast(sizeof(F) * num_bits()); + + constexpr basic_fp() : f(0), e(0) {} + constexpr basic_fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {} + + // Constructs fp from an IEEE754 floating-point number. + template FMT_CONSTEXPR basic_fp(Float n) { assign(n); } + + // Assigns n to this and return true iff predecessor is closer than successor. + template ::value)> + FMT_CONSTEXPR auto assign(Float n) -> bool { + static_assert(std::numeric_limits::digits <= 113, "unsupported FP"); + // Assume Float is in the format [sign][exponent][significand]. + using carrier_uint = typename dragonbox::float_info::carrier_uint; + const auto num_float_significand_bits = + detail::num_significand_bits(); + const auto implicit_bit = carrier_uint(1) << num_float_significand_bits; + const auto significand_mask = implicit_bit - 1; + auto u = bit_cast(n); + f = static_cast(u & significand_mask); + auto biased_e = static_cast((u & exponent_mask()) >> + num_float_significand_bits); + // The predecessor is closer if n is a normalized power of 2 (f == 0) + // other than the smallest normalized number (biased_e > 1). + auto is_predecessor_closer = f == 0 && biased_e > 1; + if (biased_e == 0) + biased_e = 1; // Subnormals use biased exponent 1 (min exponent). + else if (has_implicit_bit()) + f += static_cast(implicit_bit); + e = biased_e - exponent_bias() - num_float_significand_bits; + if (!has_implicit_bit()) ++e; + return is_predecessor_closer; + } + + template ::value)> + FMT_CONSTEXPR auto assign(Float n) -> bool { + static_assert(std::numeric_limits::is_iec559, "unsupported FP"); + return assign(static_cast(n)); + } +}; + +using fp = basic_fp; + +// Normalizes the value converted from double and multiplied by (1 << SHIFT). +template +FMT_CONSTEXPR auto normalize(basic_fp value) -> basic_fp { + // Handle subnormals. + const auto implicit_bit = F(1) << num_significand_bits(); + const auto shifted_implicit_bit = implicit_bit << SHIFT; + while ((value.f & shifted_implicit_bit) == 0) { + value.f <<= 1; + --value.e; + } + // Subtract 1 to account for hidden bit. + const auto offset = basic_fp::num_significand_bits - + num_significand_bits() - SHIFT - 1; + value.f <<= offset; + value.e -= offset; + return value; +} + +// Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking. +FMT_CONSTEXPR inline auto multiply(uint64_t lhs, uint64_t rhs) -> uint64_t { +#if FMT_USE_INT128 + auto product = static_cast<__uint128_t>(lhs) * rhs; + auto f = static_cast(product >> 64); + return (static_cast(product) & (1ULL << 63)) != 0 ? f + 1 : f; +#else + // Multiply 32-bit parts of significands. + uint64_t mask = (1ULL << 32) - 1; + uint64_t a = lhs >> 32, b = lhs & mask; + uint64_t c = rhs >> 32, d = rhs & mask; + uint64_t ac = a * c, bc = b * c, ad = a * d, bd = b * d; + // Compute mid 64-bit of result and round. + uint64_t mid = (bd >> 32) + (ad & mask) + (bc & mask) + (1U << 31); + return ac + (ad >> 32) + (bc >> 32) + (mid >> 32); +#endif +} + +FMT_CONSTEXPR inline auto operator*(fp x, fp y) -> fp { + return {multiply(x.f, y.f), x.e + y.e + 64}; +} + +template () == num_bits()> +using convert_float_result = + conditional_t::value || doublish, double, T>; + +template +constexpr auto convert_float(T value) -> convert_float_result { + return static_cast>(value); +} + +template +FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n, const fill_t& fill) + -> OutputIt { + auto fill_size = fill.size(); + if (fill_size == 1) return detail::fill_n(it, n, fill.template get()); + if (const Char* data = fill.template data()) { + for (size_t i = 0; i < n; ++i) it = copy(data, data + fill_size, it); + } + return it; +} + +// Writes the output of f, padded according to format specifications in specs. +// size: output size in code units. +// width: output display width in (terminal) column positions. +template +FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs, + size_t size, size_t width, F&& f) -> OutputIt { + static_assert(align == align::left || align == align::right, ""); + unsigned spec_width = to_unsigned(specs.width); + size_t padding = spec_width > width ? spec_width - width : 0; + // Shifts are encoded as string literals because static constexpr is not + // supported in constexpr functions. + auto* shifts = align == align::left ? "\x1f\x1f\x00\x01" : "\x00\x1f\x00\x01"; + size_t left_padding = padding >> shifts[specs.align]; + size_t right_padding = padding - left_padding; + auto it = reserve(out, size + padding * specs.fill.size()); + if (left_padding != 0) it = fill(it, left_padding, specs.fill); + it = f(it); + if (right_padding != 0) it = fill(it, right_padding, specs.fill); + return base_iterator(out, it); +} + +template +constexpr auto write_padded(OutputIt out, const format_specs& specs, + size_t size, F&& f) -> OutputIt { + return write_padded(out, specs, size, size, f); +} + +template +FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes, + const format_specs& specs = {}) -> OutputIt { + return write_padded( + out, specs, bytes.size(), [bytes](reserve_iterator it) { + const char* data = bytes.data(); + return copy(data, data + bytes.size(), it); + }); +} + +template +auto write_ptr(OutputIt out, UIntPtr value, const format_specs* specs) + -> OutputIt { + int num_digits = count_digits<4>(value); + auto size = to_unsigned(num_digits) + size_t(2); + auto write = [=](reserve_iterator it) { + *it++ = static_cast('0'); + *it++ = static_cast('x'); + return format_uint<4, Char>(it, value, num_digits); + }; + return specs ? write_padded(out, *specs, size, write) + : base_iterator(out, write(reserve(out, size))); +} + +// Returns true iff the code point cp is printable. +FMT_API auto is_printable(uint32_t cp) -> bool; + +inline auto needs_escape(uint32_t cp) -> bool { + return cp < 0x20 || cp == 0x7f || cp == '"' || cp == '\\' || + !is_printable(cp); +} + +template struct find_escape_result { + const Char* begin; + const Char* end; + uint32_t cp; +}; + +template +auto find_escape(const Char* begin, const Char* end) + -> find_escape_result { + for (; begin != end; ++begin) { + uint32_t cp = static_cast>(*begin); + if (const_check(sizeof(Char) == 1) && cp >= 0x80) continue; + if (needs_escape(cp)) return {begin, begin + 1, cp}; + } + return {begin, nullptr, 0}; +} + +inline auto find_escape(const char* begin, const char* end) + -> find_escape_result { + if (!use_utf8()) return find_escape(begin, end); + auto result = find_escape_result{end, nullptr, 0}; + for_each_codepoint(string_view(begin, to_unsigned(end - begin)), + [&](uint32_t cp, string_view sv) { + if (needs_escape(cp)) { + result = {sv.begin(), sv.end(), cp}; + return false; + } + return true; + }); + return result; +} + +#define FMT_STRING_IMPL(s, base, explicit) \ + [] { \ + /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \ + /* Use a macro-like name to avoid shadowing warnings. */ \ + struct FMT_VISIBILITY("hidden") FMT_COMPILE_STRING : base { \ + using char_type FMT_MAYBE_UNUSED = fmt::remove_cvref_t; \ + FMT_MAYBE_UNUSED FMT_CONSTEXPR explicit \ + operator fmt::basic_string_view() const { \ + return fmt::detail_exported::compile_string_to_view(s); \ + } \ + }; \ + return FMT_COMPILE_STRING(); \ + }() + +/** + * Constructs a compile-time format string from a string literal `s`. + * + * **Example**: + * + * // A compile-time error because 'd' is an invalid specifier for strings. + * std::string s = fmt::format(FMT_STRING("{:d}"), "foo"); + */ +#define FMT_STRING(s) FMT_STRING_IMPL(s, fmt::detail::compile_string, ) + +template +auto write_codepoint(OutputIt out, char prefix, uint32_t cp) -> OutputIt { + *out++ = static_cast('\\'); + *out++ = static_cast(prefix); + Char buf[width]; + fill_n(buf, width, static_cast('0')); + format_uint<4>(buf, cp, width); + return copy(buf, buf + width, out); +} + +template +auto write_escaped_cp(OutputIt out, const find_escape_result& escape) + -> OutputIt { + auto c = static_cast(escape.cp); + switch (escape.cp) { + case '\n': + *out++ = static_cast('\\'); + c = static_cast('n'); + break; + case '\r': + *out++ = static_cast('\\'); + c = static_cast('r'); + break; + case '\t': + *out++ = static_cast('\\'); + c = static_cast('t'); + break; + case '"': + FMT_FALLTHROUGH; + case '\'': + FMT_FALLTHROUGH; + case '\\': + *out++ = static_cast('\\'); + break; + default: + if (escape.cp < 0x100) return write_codepoint<2, Char>(out, 'x', escape.cp); + if (escape.cp < 0x10000) + return write_codepoint<4, Char>(out, 'u', escape.cp); + if (escape.cp < 0x110000) + return write_codepoint<8, Char>(out, 'U', escape.cp); + for (Char escape_char : basic_string_view( + escape.begin, to_unsigned(escape.end - escape.begin))) { + out = write_codepoint<2, Char>(out, 'x', + static_cast(escape_char) & 0xFF); + } + return out; + } + *out++ = c; + return out; +} + +template +auto write_escaped_string(OutputIt out, basic_string_view str) + -> OutputIt { + *out++ = static_cast('"'); + auto begin = str.begin(), end = str.end(); + do { + auto escape = find_escape(begin, end); + out = copy(begin, escape.begin, out); + begin = escape.end; + if (!begin) break; + out = write_escaped_cp(out, escape); + } while (begin != end); + *out++ = static_cast('"'); + return out; +} + +template +auto write_escaped_char(OutputIt out, Char v) -> OutputIt { + Char v_array[1] = {v}; + *out++ = static_cast('\''); + if ((needs_escape(static_cast(v)) && v != static_cast('"')) || + v == static_cast('\'')) { + out = write_escaped_cp(out, + find_escape_result{v_array, v_array + 1, + static_cast(v)}); + } else { + *out++ = v; + } + *out++ = static_cast('\''); + return out; +} + +template +FMT_CONSTEXPR auto write_char(OutputIt out, Char value, + const format_specs& specs) -> OutputIt { + bool is_debug = specs.type == presentation_type::debug; + return write_padded(out, specs, 1, [=](reserve_iterator it) { + if (is_debug) return write_escaped_char(it, value); + *it++ = value; + return it; + }); +} +template +FMT_CONSTEXPR auto write(OutputIt out, Char value, const format_specs& specs, + locale_ref loc = {}) -> OutputIt { + // char is formatted as unsigned char for consistency across platforms. + using unsigned_type = + conditional_t::value, unsigned char, unsigned>; + return check_char_specs(specs) + ? write_char(out, value, specs) + : write(out, static_cast(value), specs, loc); +} + +// Data for write_int that doesn't depend on output iterator type. It is used to +// avoid template code bloat. +template struct write_int_data { + size_t size; + size_t padding; + + FMT_CONSTEXPR write_int_data(int num_digits, unsigned prefix, + const format_specs& specs) + : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) { + if (specs.align == align::numeric) { + auto width = to_unsigned(specs.width); + if (width > size) { + padding = width - size; + size = width; + } + } else if (specs.precision > num_digits) { + size = (prefix >> 24) + to_unsigned(specs.precision); + padding = to_unsigned(specs.precision - num_digits); + } + } +}; + +// Writes an integer in the format +// +// where are written by write_digits(it). +// prefix contains chars in three lower bytes and the size in the fourth byte. +template +FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, int num_digits, + unsigned prefix, + const format_specs& specs, + W write_digits) -> OutputIt { + // Slightly faster check for specs.width == 0 && specs.precision == -1. + if ((specs.width | (specs.precision + 1)) == 0) { + auto it = reserve(out, to_unsigned(num_digits) + (prefix >> 24)); + if (prefix != 0) { + for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) + *it++ = static_cast(p & 0xff); + } + return base_iterator(out, write_digits(it)); + } + auto data = write_int_data(num_digits, prefix, specs); + return write_padded( + out, specs, data.size, [=](reserve_iterator it) { + for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) + *it++ = static_cast(p & 0xff); + it = detail::fill_n(it, data.padding, static_cast('0')); + return write_digits(it); + }); +} + +template class digit_grouping { + private: + std::string grouping_; + std::basic_string thousands_sep_; + + struct next_state { + std::string::const_iterator group; + int pos; + }; + auto initial_state() const -> next_state { return {grouping_.begin(), 0}; } + + // Returns the next digit group separator position. + auto next(next_state& state) const -> int { + if (thousands_sep_.empty()) return max_value(); + if (state.group == grouping_.end()) return state.pos += grouping_.back(); + if (*state.group <= 0 || *state.group == max_value()) + return max_value(); + state.pos += *state.group++; + return state.pos; + } + + public: + explicit digit_grouping(locale_ref loc, bool localized = true) { + if (!localized) return; + auto sep = thousands_sep(loc); + grouping_ = sep.grouping; + if (sep.thousands_sep) thousands_sep_.assign(1, sep.thousands_sep); + } + digit_grouping(std::string grouping, std::basic_string sep) + : grouping_(std::move(grouping)), thousands_sep_(std::move(sep)) {} + + auto has_separator() const -> bool { return !thousands_sep_.empty(); } + + auto count_separators(int num_digits) const -> int { + int count = 0; + auto state = initial_state(); + while (num_digits > next(state)) ++count; + return count; + } + + // Applies grouping to digits and write the output to out. + template + auto apply(Out out, basic_string_view digits) const -> Out { + auto num_digits = static_cast(digits.size()); + auto separators = basic_memory_buffer(); + separators.push_back(0); + auto state = initial_state(); + while (int i = next(state)) { + if (i >= num_digits) break; + separators.push_back(i); + } + for (int i = 0, sep_index = static_cast(separators.size() - 1); + i < num_digits; ++i) { + if (num_digits - i == separators[sep_index]) { + out = copy(thousands_sep_.data(), + thousands_sep_.data() + thousands_sep_.size(), out); + --sep_index; + } + *out++ = static_cast(digits[to_unsigned(i)]); + } + return out; + } +}; + +FMT_CONSTEXPR inline void prefix_append(unsigned& prefix, unsigned value) { + prefix |= prefix != 0 ? value << 8 : value; + prefix += (1u + (value > 0xff ? 1 : 0)) << 24; +} + +// Writes a decimal integer with digit grouping. +template +auto write_int(OutputIt out, UInt value, unsigned prefix, + const format_specs& specs, const digit_grouping& grouping) + -> OutputIt { + static_assert(std::is_same, UInt>::value, ""); + int num_digits = 0; + auto buffer = memory_buffer(); + switch (specs.type) { + default: + FMT_ASSERT(false, ""); + FMT_FALLTHROUGH; + case presentation_type::none: + case presentation_type::dec: + num_digits = count_digits(value); + format_decimal(appender(buffer), value, num_digits); + break; + case presentation_type::hex: + if (specs.alt) + prefix_append(prefix, unsigned(specs.upper ? 'X' : 'x') << 8 | '0'); + num_digits = count_digits<4>(value); + format_uint<4, char>(appender(buffer), value, num_digits, specs.upper); + break; + case presentation_type::oct: + num_digits = count_digits<3>(value); + // Octal prefix '0' is counted as a digit, so only add it if precision + // is not greater than the number of digits. + if (specs.alt && specs.precision <= num_digits && value != 0) + prefix_append(prefix, '0'); + format_uint<3, char>(appender(buffer), value, num_digits); + break; + case presentation_type::bin: + if (specs.alt) + prefix_append(prefix, unsigned(specs.upper ? 'B' : 'b') << 8 | '0'); + num_digits = count_digits<1>(value); + format_uint<1, char>(appender(buffer), value, num_digits); + break; + case presentation_type::chr: + return write_char(out, static_cast(value), specs); + } + + unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) + + to_unsigned(grouping.count_separators(num_digits)); + return write_padded( + out, specs, size, size, [&](reserve_iterator it) { + for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) + *it++ = static_cast(p & 0xff); + return grouping.apply(it, string_view(buffer.data(), buffer.size())); + }); +} + +// Writes a localized value. +FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs, + locale_ref loc) -> bool; +template +inline auto write_loc(OutputIt, loc_value, const format_specs&, locale_ref) + -> bool { + return false; +} + +template struct write_int_arg { + UInt abs_value; + unsigned prefix; +}; + +template +FMT_CONSTEXPR auto make_write_int_arg(T value, sign_t sign) + -> write_int_arg> { + auto prefix = 0u; + auto abs_value = static_cast>(value); + if (is_negative(value)) { + prefix = 0x01000000 | '-'; + abs_value = 0 - abs_value; + } else { + constexpr const unsigned prefixes[4] = {0, 0, 0x1000000u | '+', + 0x1000000u | ' '}; + prefix = prefixes[sign]; + } + return {abs_value, prefix}; +} + +template struct loc_writer { + basic_appender out; + const format_specs& specs; + std::basic_string sep; + std::string grouping; + std::basic_string decimal_point; + + template ::value)> + auto operator()(T value) -> bool { + auto arg = make_write_int_arg(value, specs.sign); + write_int(out, static_cast>(arg.abs_value), arg.prefix, + specs, digit_grouping(grouping, sep)); + return true; + } + + template ::value)> + auto operator()(T) -> bool { + return false; + } +}; + +template +FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, + const format_specs& specs, locale_ref) + -> OutputIt { + static_assert(std::is_same>::value, ""); + auto abs_value = arg.abs_value; + auto prefix = arg.prefix; + switch (specs.type) { + default: + FMT_ASSERT(false, ""); + FMT_FALLTHROUGH; + case presentation_type::none: + case presentation_type::dec: { + int num_digits = count_digits(abs_value); + return write_int( + out, num_digits, prefix, specs, [=](reserve_iterator it) { + return format_decimal(it, abs_value, num_digits).end; + }); + } + case presentation_type::hex: { + if (specs.alt) + prefix_append(prefix, unsigned(specs.upper ? 'X' : 'x') << 8 | '0'); + int num_digits = count_digits<4>(abs_value); + return write_int( + out, num_digits, prefix, specs, [=](reserve_iterator it) { + return format_uint<4, Char>(it, abs_value, num_digits, specs.upper); + }); + } + case presentation_type::oct: { + int num_digits = count_digits<3>(abs_value); + // Octal prefix '0' is counted as a digit, so only add it if precision + // is not greater than the number of digits. + if (specs.alt && specs.precision <= num_digits && abs_value != 0) + prefix_append(prefix, '0'); + return write_int( + out, num_digits, prefix, specs, [=](reserve_iterator it) { + return format_uint<3, Char>(it, abs_value, num_digits); + }); + } + case presentation_type::bin: { + if (specs.alt) + prefix_append(prefix, unsigned(specs.upper ? 'B' : 'b') << 8 | '0'); + int num_digits = count_digits<1>(abs_value); + return write_int( + out, num_digits, prefix, specs, [=](reserve_iterator it) { + return format_uint<1, Char>(it, abs_value, num_digits); + }); + } + case presentation_type::chr: + return write_char(out, static_cast(abs_value), specs); + } +} +template +FMT_CONSTEXPR FMT_NOINLINE auto write_int_noinline(OutputIt out, + write_int_arg arg, + const format_specs& specs, + locale_ref loc) -> OutputIt { + return write_int(out, arg, specs, loc); +} +template ::value && + !std::is_same::value && + !std::is_same::value)> +FMT_CONSTEXPR FMT_INLINE auto write(basic_appender out, T value, + const format_specs& specs, locale_ref loc) + -> basic_appender { + if (specs.localized && write_loc(out, value, specs, loc)) return out; + return write_int_noinline(out, make_write_int_arg(value, specs.sign), + specs, loc); +} +// An inlined version of write used in format string compilation. +template ::value && + !std::is_same::value && + !std::is_same::value && + !std::is_same>::value)> +FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value, + const format_specs& specs, locale_ref loc) + -> OutputIt { + if (specs.localized && write_loc(out, value, specs, loc)) return out; + return write_int(out, make_write_int_arg(value, specs.sign), specs, + loc); +} + +// An output iterator that counts the number of objects written to it and +// discards them. +class counting_iterator { + private: + size_t count_; + + public: + using iterator_category = std::output_iterator_tag; + using difference_type = std::ptrdiff_t; + using pointer = void; + using reference = void; + FMT_UNCHECKED_ITERATOR(counting_iterator); + + struct value_type { + template FMT_CONSTEXPR void operator=(const T&) {} + }; + + FMT_CONSTEXPR counting_iterator() : count_(0) {} + + FMT_CONSTEXPR auto count() const -> size_t { return count_; } + + FMT_CONSTEXPR auto operator++() -> counting_iterator& { + ++count_; + return *this; + } + FMT_CONSTEXPR auto operator++(int) -> counting_iterator { + auto it = *this; + ++*this; + return it; + } + + FMT_CONSTEXPR friend auto operator+(counting_iterator it, difference_type n) + -> counting_iterator { + it.count_ += static_cast(n); + return it; + } + + FMT_CONSTEXPR auto operator*() const -> value_type { return {}; } +}; + +template +FMT_CONSTEXPR auto write(OutputIt out, basic_string_view s, + const format_specs& specs) -> OutputIt { + auto data = s.data(); + auto size = s.size(); + if (specs.precision >= 0 && to_unsigned(specs.precision) < size) + size = code_point_index(s, to_unsigned(specs.precision)); + bool is_debug = specs.type == presentation_type::debug; + size_t width = 0; + + if (is_debug) size = write_escaped_string(counting_iterator{}, s).count(); + + if (specs.width != 0) { + if (is_debug) + width = size; + else + width = compute_width(basic_string_view(data, size)); + } + return write_padded(out, specs, size, width, + [=](reserve_iterator it) { + if (is_debug) return write_escaped_string(it, s); + return copy(data, data + size, it); + }); +} +template +FMT_CONSTEXPR auto write(OutputIt out, + basic_string_view> s, + const format_specs& specs, locale_ref) -> OutputIt { + return write(out, s, specs); +} +template +FMT_CONSTEXPR auto write(OutputIt out, const Char* s, const format_specs& specs, + locale_ref) -> OutputIt { + if (specs.type == presentation_type::pointer) + return write_ptr(out, bit_cast(s), &specs); + if (!s) report_error("string pointer is null"); + return write(out, basic_string_view(s), specs, {}); +} + +template ::value && + !std::is_same::value && + !std::is_same::value)> +FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt { + auto abs_value = static_cast>(value); + bool negative = is_negative(value); + // Don't do -abs_value since it trips unsigned-integer-overflow sanitizer. + if (negative) abs_value = ~abs_value + 1; + int num_digits = count_digits(abs_value); + auto size = (negative ? 1 : 0) + static_cast(num_digits); + if (auto ptr = to_pointer(out, size)) { + if (negative) *ptr++ = static_cast('-'); + format_decimal(ptr, abs_value, num_digits); + return out; + } + if (negative) *out++ = static_cast('-'); + return format_decimal(out, abs_value, num_digits).end; +} + +// DEPRECATED! +template +FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end, + format_specs& specs) -> const Char* { + FMT_ASSERT(begin != end, ""); + auto align = align::none; + auto p = begin + code_point_length(begin); + if (end - p <= 0) p = begin; + for (;;) { + switch (to_ascii(*p)) { + case '<': + align = align::left; + break; + case '>': + align = align::right; + break; + case '^': + align = align::center; + break; + } + if (align != align::none) { + if (p != begin) { + auto c = *begin; + if (c == '}') return begin; + if (c == '{') { + report_error("invalid fill character '{'"); + return begin; + } + specs.fill = basic_string_view(begin, to_unsigned(p - begin)); + begin = p + 1; + } else { + ++begin; + } + break; + } else if (p == begin) { + break; + } + p = begin; + } + specs.align = align; + return begin; +} + +// A floating-point presentation format. +enum class float_format : unsigned char { + general, // General: exponent notation or fixed point based on magnitude. + exp, // Exponent notation with the default precision of 6, e.g. 1.2e-3. + fixed // Fixed point with the default precision of 6, e.g. 0.0012. +}; + +struct float_specs { + int precision; + float_format format : 8; + sign_t sign : 8; + bool locale : 1; + bool binary32 : 1; + bool showpoint : 1; +}; + +// DEPRECATED! +FMT_CONSTEXPR inline auto parse_float_type_spec(const format_specs& specs) + -> float_specs { + auto result = float_specs(); + result.showpoint = specs.alt; + result.locale = specs.localized; + switch (specs.type) { + default: + FMT_FALLTHROUGH; + case presentation_type::none: + result.format = float_format::general; + break; + case presentation_type::exp: + result.format = float_format::exp; + result.showpoint |= specs.precision != 0; + break; + case presentation_type::fixed: + result.format = float_format::fixed; + result.showpoint |= specs.precision != 0; + break; + case presentation_type::general: + result.format = float_format::general; + break; + } + return result; +} + +template +FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan, + format_specs specs, sign_t sign) + -> OutputIt { + auto str = + isnan ? (specs.upper ? "NAN" : "nan") : (specs.upper ? "INF" : "inf"); + constexpr size_t str_size = 3; + auto size = str_size + (sign ? 1 : 0); + // Replace '0'-padding with space for non-finite values. + const bool is_zero_fill = + specs.fill.size() == 1 && specs.fill.template get() == '0'; + if (is_zero_fill) specs.fill = ' '; + return write_padded(out, specs, size, + [=](reserve_iterator it) { + if (sign) *it++ = detail::sign(sign); + return copy(str, str + str_size, it); + }); +} + +// A decimal floating-point number significand * pow(10, exp). +struct big_decimal_fp { + const char* significand; + int significand_size; + int exponent; +}; + +constexpr auto get_significand_size(const big_decimal_fp& f) -> int { + return f.significand_size; +} +template +inline auto get_significand_size(const dragonbox::decimal_fp& f) -> int { + return count_digits(f.significand); +} + +template +constexpr auto write_significand(OutputIt out, const char* significand, + int significand_size) -> OutputIt { + return copy(significand, significand + significand_size, out); +} +template +inline auto write_significand(OutputIt out, UInt significand, + int significand_size) -> OutputIt { + return format_decimal(out, significand, significand_size).end; +} +template +FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, + int significand_size, int exponent, + const Grouping& grouping) -> OutputIt { + if (!grouping.has_separator()) { + out = write_significand(out, significand, significand_size); + return detail::fill_n(out, exponent, static_cast('0')); + } + auto buffer = memory_buffer(); + write_significand(appender(buffer), significand, significand_size); + detail::fill_n(appender(buffer), exponent, '0'); + return grouping.apply(out, string_view(buffer.data(), buffer.size())); +} + +template ::value)> +inline auto write_significand(Char* out, UInt significand, int significand_size, + int integral_size, Char decimal_point) -> Char* { + if (!decimal_point) + return format_decimal(out, significand, significand_size).end; + out += significand_size + 1; + Char* end = out; + int floating_size = significand_size - integral_size; + for (int i = floating_size / 2; i > 0; --i) { + out -= 2; + copy2(out, digits2(static_cast(significand % 100))); + significand /= 100; + } + if (floating_size % 2 != 0) { + *--out = static_cast('0' + significand % 10); + significand /= 10; + } + *--out = decimal_point; + format_decimal(out - integral_size, significand, integral_size); + return end; +} + +template >::value)> +inline auto write_significand(OutputIt out, UInt significand, + int significand_size, int integral_size, + Char decimal_point) -> OutputIt { + // Buffer is large enough to hold digits (digits10 + 1) and a decimal point. + Char buffer[digits10() + 2]; + auto end = write_significand(buffer, significand, significand_size, + integral_size, decimal_point); + return detail::copy_noinline(buffer, end, out); +} + +template +FMT_CONSTEXPR auto write_significand(OutputIt out, const char* significand, + int significand_size, int integral_size, + Char decimal_point) -> OutputIt { + out = detail::copy_noinline(significand, significand + integral_size, + out); + if (!decimal_point) return out; + *out++ = decimal_point; + return detail::copy_noinline(significand + integral_size, + significand + significand_size, out); +} + +template +FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, + int significand_size, int integral_size, + Char decimal_point, + const Grouping& grouping) -> OutputIt { + if (!grouping.has_separator()) { + return write_significand(out, significand, significand_size, integral_size, + decimal_point); + } + auto buffer = basic_memory_buffer(); + write_significand(basic_appender(buffer), significand, significand_size, + integral_size, decimal_point); + grouping.apply( + out, basic_string_view(buffer.data(), to_unsigned(integral_size))); + return detail::copy_noinline(buffer.data() + integral_size, + buffer.end(), out); +} + +template > +FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, + const format_specs& specs, + float_specs fspecs, locale_ref loc) + -> OutputIt { + auto significand = f.significand; + int significand_size = get_significand_size(f); + const Char zero = static_cast('0'); + auto sign = fspecs.sign; + size_t size = to_unsigned(significand_size) + (sign ? 1 : 0); + using iterator = reserve_iterator; + + Char decimal_point = + fspecs.locale ? detail::decimal_point(loc) : static_cast('.'); + + int output_exp = f.exponent + significand_size - 1; + auto use_exp_format = [=]() { + if (fspecs.format == float_format::exp) return true; + if (fspecs.format != float_format::general) return false; + // Use the fixed notation if the exponent is in [exp_lower, exp_upper), + // e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation. + const int exp_lower = -4, exp_upper = 16; + return output_exp < exp_lower || + output_exp >= (fspecs.precision > 0 ? fspecs.precision : exp_upper); + }; + if (use_exp_format()) { + int num_zeros = 0; + if (fspecs.showpoint) { + num_zeros = fspecs.precision - significand_size; + if (num_zeros < 0) num_zeros = 0; + size += to_unsigned(num_zeros); + } else if (significand_size == 1) { + decimal_point = Char(); + } + auto abs_output_exp = output_exp >= 0 ? output_exp : -output_exp; + int exp_digits = 2; + if (abs_output_exp >= 100) exp_digits = abs_output_exp >= 1000 ? 4 : 3; + + size += to_unsigned((decimal_point ? 1 : 0) + 2 + exp_digits); + char exp_char = specs.upper ? 'E' : 'e'; + auto write = [=](iterator it) { + if (sign) *it++ = detail::sign(sign); + // Insert a decimal point after the first digit and add an exponent. + it = write_significand(it, significand, significand_size, 1, + decimal_point); + if (num_zeros > 0) it = detail::fill_n(it, num_zeros, zero); + *it++ = static_cast(exp_char); + return write_exponent(output_exp, it); + }; + return specs.width > 0 + ? write_padded(out, specs, size, write) + : base_iterator(out, write(reserve(out, size))); + } + + int exp = f.exponent + significand_size; + if (f.exponent >= 0) { + // 1234e5 -> 123400000[.0+] + size += to_unsigned(f.exponent); + int num_zeros = fspecs.precision - exp; + abort_fuzzing_if(num_zeros > 5000); + if (fspecs.showpoint) { + ++size; + if (num_zeros <= 0 && fspecs.format != float_format::fixed) num_zeros = 0; + if (num_zeros > 0) size += to_unsigned(num_zeros); + } + auto grouping = Grouping(loc, fspecs.locale); + size += to_unsigned(grouping.count_separators(exp)); + return write_padded(out, specs, size, [&](iterator it) { + if (sign) *it++ = detail::sign(sign); + it = write_significand(it, significand, significand_size, + f.exponent, grouping); + if (!fspecs.showpoint) return it; + *it++ = decimal_point; + return num_zeros > 0 ? detail::fill_n(it, num_zeros, zero) : it; + }); + } else if (exp > 0) { + // 1234e-2 -> 12.34[0+] + int num_zeros = fspecs.showpoint ? fspecs.precision - significand_size : 0; + size += 1 + to_unsigned(num_zeros > 0 ? num_zeros : 0); + auto grouping = Grouping(loc, fspecs.locale); + size += to_unsigned(grouping.count_separators(exp)); + return write_padded(out, specs, size, [&](iterator it) { + if (sign) *it++ = detail::sign(sign); + it = write_significand(it, significand, significand_size, exp, + decimal_point, grouping); + return num_zeros > 0 ? detail::fill_n(it, num_zeros, zero) : it; + }); + } + // 1234e-6 -> 0.001234 + int num_zeros = -exp; + if (significand_size == 0 && fspecs.precision >= 0 && + fspecs.precision < num_zeros) { + num_zeros = fspecs.precision; + } + bool pointy = num_zeros != 0 || significand_size != 0 || fspecs.showpoint; + size += 1 + (pointy ? 1 : 0) + to_unsigned(num_zeros); + return write_padded(out, specs, size, [&](iterator it) { + if (sign) *it++ = detail::sign(sign); + *it++ = zero; + if (!pointy) return it; + *it++ = decimal_point; + it = detail::fill_n(it, num_zeros, zero); + return write_significand(it, significand, significand_size); + }); +} + +template class fallback_digit_grouping { + public: + constexpr fallback_digit_grouping(locale_ref, bool) {} + + constexpr auto has_separator() const -> bool { return false; } + + constexpr auto count_separators(int) const -> int { return 0; } + + template + constexpr auto apply(Out out, basic_string_view) const -> Out { + return out; + } +}; + +template +FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f, + const format_specs& specs, float_specs fspecs, + locale_ref loc) -> OutputIt { + if (is_constant_evaluated()) { + return do_write_float>(out, f, specs, fspecs, + loc); + } else { + return do_write_float(out, f, specs, fspecs, loc); + } +} + +template constexpr auto isnan(T value) -> bool { + return value != value; // std::isnan doesn't support __float128. +} + +template +struct has_isfinite : std::false_type {}; + +template +struct has_isfinite> + : std::true_type {}; + +template ::value&& + has_isfinite::value)> +FMT_CONSTEXPR20 auto isfinite(T value) -> bool { + constexpr T inf = T(std::numeric_limits::infinity()); + if (is_constant_evaluated()) + return !detail::isnan(value) && value < inf && value > -inf; + return std::isfinite(value); +} +template ::value)> +FMT_CONSTEXPR auto isfinite(T value) -> bool { + T inf = T(std::numeric_limits::infinity()); + // std::isfinite doesn't support __float128. + return !detail::isnan(value) && value < inf && value > -inf; +} + +template ::value)> +FMT_INLINE FMT_CONSTEXPR bool signbit(T value) { + if (is_constant_evaluated()) { +#ifdef __cpp_if_constexpr + if constexpr (std::numeric_limits::is_iec559) { + auto bits = detail::bit_cast(static_cast(value)); + return (bits >> (num_bits() - 1)) != 0; + } +#endif + } + return std::signbit(static_cast(value)); +} + +inline FMT_CONSTEXPR20 void adjust_precision(int& precision, int exp10) { + // Adjust fixed precision by exponent because it is relative to decimal + // point. + if (exp10 > 0 && precision > max_value() - exp10) + FMT_THROW(format_error("number is too big")); + precision += exp10; +} + +class bigint { + private: + // A bigint is stored as an array of bigits (big digits), with bigit at index + // 0 being the least significant one. + using bigit = uint32_t; + using double_bigit = uint64_t; + enum { bigits_capacity = 32 }; + basic_memory_buffer bigits_; + int exp_; + + FMT_CONSTEXPR20 auto operator[](int index) const -> bigit { + return bigits_[to_unsigned(index)]; + } + FMT_CONSTEXPR20 auto operator[](int index) -> bigit& { + return bigits_[to_unsigned(index)]; + } + + static constexpr const int bigit_bits = num_bits(); + + friend struct formatter; + + FMT_CONSTEXPR20 void subtract_bigits(int index, bigit other, bigit& borrow) { + auto result = static_cast((*this)[index]) - other - borrow; + (*this)[index] = static_cast(result); + borrow = static_cast(result >> (bigit_bits * 2 - 1)); + } + + FMT_CONSTEXPR20 void remove_leading_zeros() { + int num_bigits = static_cast(bigits_.size()) - 1; + while (num_bigits > 0 && (*this)[num_bigits] == 0) --num_bigits; + bigits_.resize(to_unsigned(num_bigits + 1)); + } + + // Computes *this -= other assuming aligned bigints and *this >= other. + FMT_CONSTEXPR20 void subtract_aligned(const bigint& other) { + FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints"); + FMT_ASSERT(compare(*this, other) >= 0, ""); + bigit borrow = 0; + int i = other.exp_ - exp_; + for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j) + subtract_bigits(i, other.bigits_[j], borrow); + while (borrow > 0) subtract_bigits(i, 0, borrow); + remove_leading_zeros(); + } + + FMT_CONSTEXPR20 void multiply(uint32_t value) { + const double_bigit wide_value = value; + bigit carry = 0; + for (size_t i = 0, n = bigits_.size(); i < n; ++i) { + double_bigit result = bigits_[i] * wide_value + carry; + bigits_[i] = static_cast(result); + carry = static_cast(result >> bigit_bits); + } + if (carry != 0) bigits_.push_back(carry); + } + + template ::value || + std::is_same::value)> + FMT_CONSTEXPR20 void multiply(UInt value) { + using half_uint = + conditional_t::value, uint64_t, uint32_t>; + const int shift = num_bits() - bigit_bits; + const UInt lower = static_cast(value); + const UInt upper = value >> num_bits(); + UInt carry = 0; + for (size_t i = 0, n = bigits_.size(); i < n; ++i) { + UInt result = lower * bigits_[i] + static_cast(carry); + carry = (upper * bigits_[i] << shift) + (result >> bigit_bits) + + (carry >> bigit_bits); + bigits_[i] = static_cast(result); + } + while (carry != 0) { + bigits_.push_back(static_cast(carry)); + carry >>= bigit_bits; + } + } + + template ::value || + std::is_same::value)> + FMT_CONSTEXPR20 void assign(UInt n) { + size_t num_bigits = 0; + do { + bigits_[num_bigits++] = static_cast(n); + n >>= bigit_bits; + } while (n != 0); + bigits_.resize(num_bigits); + exp_ = 0; + } + + public: + FMT_CONSTEXPR20 bigint() : exp_(0) {} + explicit bigint(uint64_t n) { assign(n); } + + bigint(const bigint&) = delete; + void operator=(const bigint&) = delete; + + FMT_CONSTEXPR20 void assign(const bigint& other) { + auto size = other.bigits_.size(); + bigits_.resize(size); + auto data = other.bigits_.data(); + copy(data, data + size, bigits_.data()); + exp_ = other.exp_; + } + + template FMT_CONSTEXPR20 void operator=(Int n) { + FMT_ASSERT(n > 0, ""); + assign(uint64_or_128_t(n)); + } + + FMT_CONSTEXPR20 auto num_bigits() const -> int { + return static_cast(bigits_.size()) + exp_; + } + + FMT_NOINLINE FMT_CONSTEXPR20 auto operator<<=(int shift) -> bigint& { + FMT_ASSERT(shift >= 0, ""); + exp_ += shift / bigit_bits; + shift %= bigit_bits; + if (shift == 0) return *this; + bigit carry = 0; + for (size_t i = 0, n = bigits_.size(); i < n; ++i) { + bigit c = bigits_[i] >> (bigit_bits - shift); + bigits_[i] = (bigits_[i] << shift) + carry; + carry = c; + } + if (carry != 0) bigits_.push_back(carry); + return *this; + } + + template + FMT_CONSTEXPR20 auto operator*=(Int value) -> bigint& { + FMT_ASSERT(value > 0, ""); + multiply(uint32_or_64_or_128_t(value)); + return *this; + } + + friend FMT_CONSTEXPR20 auto compare(const bigint& lhs, const bigint& rhs) + -> int { + int num_lhs_bigits = lhs.num_bigits(), num_rhs_bigits = rhs.num_bigits(); + if (num_lhs_bigits != num_rhs_bigits) + return num_lhs_bigits > num_rhs_bigits ? 1 : -1; + int i = static_cast(lhs.bigits_.size()) - 1; + int j = static_cast(rhs.bigits_.size()) - 1; + int end = i - j; + if (end < 0) end = 0; + for (; i >= end; --i, --j) { + bigit lhs_bigit = lhs[i], rhs_bigit = rhs[j]; + if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1; + } + if (i != j) return i > j ? 1 : -1; + return 0; + } + + // Returns compare(lhs1 + lhs2, rhs). + friend FMT_CONSTEXPR20 auto add_compare(const bigint& lhs1, + const bigint& lhs2, const bigint& rhs) + -> int { + auto minimum = [](int a, int b) { return a < b ? a : b; }; + auto maximum = [](int a, int b) { return a > b ? a : b; }; + int max_lhs_bigits = maximum(lhs1.num_bigits(), lhs2.num_bigits()); + int num_rhs_bigits = rhs.num_bigits(); + if (max_lhs_bigits + 1 < num_rhs_bigits) return -1; + if (max_lhs_bigits > num_rhs_bigits) return 1; + auto get_bigit = [](const bigint& n, int i) -> bigit { + return i >= n.exp_ && i < n.num_bigits() ? n[i - n.exp_] : 0; + }; + double_bigit borrow = 0; + int min_exp = minimum(minimum(lhs1.exp_, lhs2.exp_), rhs.exp_); + for (int i = num_rhs_bigits - 1; i >= min_exp; --i) { + double_bigit sum = + static_cast(get_bigit(lhs1, i)) + get_bigit(lhs2, i); + bigit rhs_bigit = get_bigit(rhs, i); + if (sum > rhs_bigit + borrow) return 1; + borrow = rhs_bigit + borrow - sum; + if (borrow > 1) return -1; + borrow <<= bigit_bits; + } + return borrow != 0 ? -1 : 0; + } + + // Assigns pow(10, exp) to this bigint. + FMT_CONSTEXPR20 void assign_pow10(int exp) { + FMT_ASSERT(exp >= 0, ""); + if (exp == 0) return *this = 1; + // Find the top bit. + int bitmask = 1; + while (exp >= bitmask) bitmask <<= 1; + bitmask >>= 1; + // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by + // repeated squaring and multiplication. + *this = 5; + bitmask >>= 1; + while (bitmask != 0) { + square(); + if ((exp & bitmask) != 0) *this *= 5; + bitmask >>= 1; + } + *this <<= exp; // Multiply by pow(2, exp) by shifting. + } + + FMT_CONSTEXPR20 void square() { + int num_bigits = static_cast(bigits_.size()); + int num_result_bigits = 2 * num_bigits; + basic_memory_buffer n(std::move(bigits_)); + bigits_.resize(to_unsigned(num_result_bigits)); + auto sum = uint128_t(); + for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) { + // Compute bigit at position bigit_index of the result by adding + // cross-product terms n[i] * n[j] such that i + j == bigit_index. + for (int i = 0, j = bigit_index; j >= 0; ++i, --j) { + // Most terms are multiplied twice which can be optimized in the future. + sum += static_cast(n[i]) * n[j]; + } + (*this)[bigit_index] = static_cast(sum); + sum >>= num_bits(); // Compute the carry. + } + // Do the same for the top half. + for (int bigit_index = num_bigits; bigit_index < num_result_bigits; + ++bigit_index) { + for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;) + sum += static_cast(n[i++]) * n[j--]; + (*this)[bigit_index] = static_cast(sum); + sum >>= num_bits(); + } + remove_leading_zeros(); + exp_ *= 2; + } + + // If this bigint has a bigger exponent than other, adds trailing zero to make + // exponents equal. This simplifies some operations such as subtraction. + FMT_CONSTEXPR20 void align(const bigint& other) { + int exp_difference = exp_ - other.exp_; + if (exp_difference <= 0) return; + int num_bigits = static_cast(bigits_.size()); + bigits_.resize(to_unsigned(num_bigits + exp_difference)); + for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j) + bigits_[j] = bigits_[i]; + memset(bigits_.data(), 0, to_unsigned(exp_difference) * sizeof(bigit)); + exp_ -= exp_difference; + } + + // Divides this bignum by divisor, assigning the remainder to this and + // returning the quotient. + FMT_CONSTEXPR20 auto divmod_assign(const bigint& divisor) -> int { + FMT_ASSERT(this != &divisor, ""); + if (compare(*this, divisor) < 0) return 0; + FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, ""); + align(divisor); + int quotient = 0; + do { + subtract_aligned(divisor); + ++quotient; + } while (compare(*this, divisor) >= 0); + return quotient; + } +}; + +// format_dragon flags. +enum dragon { + predecessor_closer = 1, + fixup = 2, // Run fixup to correct exp10 which can be off by one. + fixed = 4, +}; + +// Formats a floating-point number using a variation of the Fixed-Precision +// Positive Floating-Point Printout ((FPP)^2) algorithm by Steele & White: +// https://fmt.dev/papers/p372-steele.pdf. +FMT_CONSTEXPR20 inline void format_dragon(basic_fp value, + unsigned flags, int num_digits, + buffer& buf, int& exp10) { + bigint numerator; // 2 * R in (FPP)^2. + bigint denominator; // 2 * S in (FPP)^2. + // lower and upper are differences between value and corresponding boundaries. + bigint lower; // (M^- in (FPP)^2). + bigint upper_store; // upper's value if different from lower. + bigint* upper = nullptr; // (M^+ in (FPP)^2). + // Shift numerator and denominator by an extra bit or two (if lower boundary + // is closer) to make lower and upper integers. This eliminates multiplication + // by 2 during later computations. + bool is_predecessor_closer = (flags & dragon::predecessor_closer) != 0; + int shift = is_predecessor_closer ? 2 : 1; + if (value.e >= 0) { + numerator = value.f; + numerator <<= value.e + shift; + lower = 1; + lower <<= value.e; + if (is_predecessor_closer) { + upper_store = 1; + upper_store <<= value.e + 1; + upper = &upper_store; + } + denominator.assign_pow10(exp10); + denominator <<= shift; + } else if (exp10 < 0) { + numerator.assign_pow10(-exp10); + lower.assign(numerator); + if (is_predecessor_closer) { + upper_store.assign(numerator); + upper_store <<= 1; + upper = &upper_store; + } + numerator *= value.f; + numerator <<= shift; + denominator = 1; + denominator <<= shift - value.e; + } else { + numerator = value.f; + numerator <<= shift; + denominator.assign_pow10(exp10); + denominator <<= shift - value.e; + lower = 1; + if (is_predecessor_closer) { + upper_store = 1ULL << 1; + upper = &upper_store; + } + } + int even = static_cast((value.f & 1) == 0); + if (!upper) upper = &lower; + bool shortest = num_digits < 0; + if ((flags & dragon::fixup) != 0) { + if (add_compare(numerator, *upper, denominator) + even <= 0) { + --exp10; + numerator *= 10; + if (num_digits < 0) { + lower *= 10; + if (upper != &lower) *upper *= 10; + } + } + if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1); + } + // Invariant: value == (numerator / denominator) * pow(10, exp10). + if (shortest) { + // Generate the shortest representation. + num_digits = 0; + char* data = buf.data(); + for (;;) { + int digit = numerator.divmod_assign(denominator); + bool low = compare(numerator, lower) - even < 0; // numerator <[=] lower. + // numerator + upper >[=] pow10: + bool high = add_compare(numerator, *upper, denominator) + even > 0; + data[num_digits++] = static_cast('0' + digit); + if (low || high) { + if (!low) { + ++data[num_digits - 1]; + } else if (high) { + int result = add_compare(numerator, numerator, denominator); + // Round half to even. + if (result > 0 || (result == 0 && (digit % 2) != 0)) + ++data[num_digits - 1]; + } + buf.try_resize(to_unsigned(num_digits)); + exp10 -= num_digits - 1; + return; + } + numerator *= 10; + lower *= 10; + if (upper != &lower) *upper *= 10; + } + } + // Generate the given number of digits. + exp10 -= num_digits - 1; + if (num_digits <= 0) { + auto digit = '0'; + if (num_digits == 0) { + denominator *= 10; + digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0'; + } + buf.push_back(digit); + return; + } + buf.try_resize(to_unsigned(num_digits)); + for (int i = 0; i < num_digits - 1; ++i) { + int digit = numerator.divmod_assign(denominator); + buf[i] = static_cast('0' + digit); + numerator *= 10; + } + int digit = numerator.divmod_assign(denominator); + auto result = add_compare(numerator, numerator, denominator); + if (result > 0 || (result == 0 && (digit % 2) != 0)) { + if (digit == 9) { + const auto overflow = '0' + 10; + buf[num_digits - 1] = overflow; + // Propagate the carry. + for (int i = num_digits - 1; i > 0 && buf[i] == overflow; --i) { + buf[i] = '0'; + ++buf[i - 1]; + } + if (buf[0] == overflow) { + buf[0] = '1'; + if ((flags & dragon::fixed) != 0) + buf.push_back('0'); + else + ++exp10; + } + return; + } + ++digit; + } + buf[num_digits - 1] = static_cast('0' + digit); +} + +// Formats a floating-point number using the hexfloat format. +template ::value)> +FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs, + buffer& buf) { + // float is passed as double to reduce the number of instantiations and to + // simplify implementation. + static_assert(!std::is_same::value, ""); + + using info = dragonbox::float_info; + + // Assume Float is in the format [sign][exponent][significand]. + using carrier_uint = typename info::carrier_uint; + + constexpr auto num_float_significand_bits = + detail::num_significand_bits(); + + basic_fp f(value); + f.e += num_float_significand_bits; + if (!has_implicit_bit()) --f.e; + + constexpr auto num_fraction_bits = + num_float_significand_bits + (has_implicit_bit() ? 1 : 0); + constexpr auto num_xdigits = (num_fraction_bits + 3) / 4; + + constexpr auto leading_shift = ((num_xdigits - 1) * 4); + const auto leading_mask = carrier_uint(0xF) << leading_shift; + const auto leading_xdigit = + static_cast((f.f & leading_mask) >> leading_shift); + if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1); + + int print_xdigits = num_xdigits - 1; + if (specs.precision >= 0 && print_xdigits > specs.precision) { + const int shift = ((print_xdigits - specs.precision - 1) * 4); + const auto mask = carrier_uint(0xF) << shift; + const auto v = static_cast((f.f & mask) >> shift); + + if (v >= 8) { + const auto inc = carrier_uint(1) << (shift + 4); + f.f += inc; + f.f &= ~(inc - 1); + } + + // Check long double overflow + if (!has_implicit_bit()) { + const auto implicit_bit = carrier_uint(1) << num_float_significand_bits; + if ((f.f & implicit_bit) == implicit_bit) { + f.f >>= 4; + f.e += 4; + } + } + + print_xdigits = specs.precision; + } + + char xdigits[num_bits() / 4]; + detail::fill_n(xdigits, sizeof(xdigits), '0'); + format_uint<4>(xdigits, f.f, num_xdigits, specs.upper); + + // Remove zero tail + while (print_xdigits > 0 && xdigits[print_xdigits] == '0') --print_xdigits; + + buf.push_back('0'); + buf.push_back(specs.upper ? 'X' : 'x'); + buf.push_back(xdigits[0]); + if (specs.alt || print_xdigits > 0 || print_xdigits < specs.precision) + buf.push_back('.'); + buf.append(xdigits + 1, xdigits + 1 + print_xdigits); + for (; print_xdigits < specs.precision; ++print_xdigits) buf.push_back('0'); + + buf.push_back(specs.upper ? 'P' : 'p'); + + uint32_t abs_e; + if (f.e < 0) { + buf.push_back('-'); + abs_e = static_cast(-f.e); + } else { + buf.push_back('+'); + abs_e = static_cast(f.e); + } + format_decimal(appender(buf), abs_e, detail::count_digits(abs_e)); +} + +template ::value)> +FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs, + buffer& buf) { + format_hexfloat(static_cast(value), specs, buf); +} + +constexpr auto fractional_part_rounding_thresholds(int index) -> uint32_t { + // For checking rounding thresholds. + // The kth entry is chosen to be the smallest integer such that the + // upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k. + // It is equal to ceil(2^31 + 2^32/10^(k + 1)). + // These are stored in a string literal because we cannot have static arrays + // in constexpr functions and non-static ones are poorly optimized. + return U"\x9999999a\x828f5c29\x80418938\x80068db9\x8000a7c6\x800010c7" + U"\x800001ae\x8000002b"[index]; +} + +template +FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, + buffer& buf) -> int { + // float is passed as double to reduce the number of instantiations. + static_assert(!std::is_same::value, ""); + FMT_ASSERT(value >= 0, "value is negative"); + auto converted_value = convert_float(value); + + const bool fixed = specs.format == float_format::fixed; + if (value <= 0) { // <= instead of == to silence a warning. + if (precision <= 0 || !fixed) { + buf.push_back('0'); + return 0; + } + buf.try_resize(to_unsigned(precision)); + fill_n(buf.data(), precision, '0'); + return -precision; + } + + int exp = 0; + bool use_dragon = true; + unsigned dragon_flags = 0; + if (!is_fast_float() || is_constant_evaluated()) { + const auto inv_log2_10 = 0.3010299956639812; // 1 / log2(10) + using info = dragonbox::float_info; + const auto f = basic_fp(converted_value); + // Compute exp, an approximate power of 10, such that + // 10^(exp - 1) <= value < 10^exp or 10^exp <= value < 10^(exp + 1). + // This is based on log10(value) == log2(value) / log2(10) and approximation + // of log2(value) by e + num_fraction_bits idea from double-conversion. + auto e = (f.e + count_digits<1>(f.f) - 1) * inv_log2_10 - 1e-10; + exp = static_cast(e); + if (e > exp) ++exp; // Compute ceil. + dragon_flags = dragon::fixup; + } else if (precision < 0) { + // Use Dragonbox for the shortest format. + if (specs.binary32) { + auto dec = dragonbox::to_decimal(static_cast(value)); + write(appender(buf), dec.significand); + return dec.exponent; + } + auto dec = dragonbox::to_decimal(static_cast(value)); + write(appender(buf), dec.significand); + return dec.exponent; + } else { + // Extract significand bits and exponent bits. + using info = dragonbox::float_info; + auto br = bit_cast(static_cast(value)); + + const uint64_t significand_mask = + (static_cast(1) << num_significand_bits()) - 1; + uint64_t significand = (br & significand_mask); + int exponent = static_cast((br & exponent_mask()) >> + num_significand_bits()); + + if (exponent != 0) { // Check if normal. + exponent -= exponent_bias() + num_significand_bits(); + significand |= + (static_cast(1) << num_significand_bits()); + significand <<= 1; + } else { + // Normalize subnormal inputs. + FMT_ASSERT(significand != 0, "zeros should not appear here"); + int shift = countl_zero(significand); + FMT_ASSERT(shift >= num_bits() - num_significand_bits(), + ""); + shift -= (num_bits() - num_significand_bits() - 2); + exponent = (std::numeric_limits::min_exponent - + num_significand_bits()) - + shift; + significand <<= shift; + } + + // Compute the first several nonzero decimal significand digits. + // We call the number we get the first segment. + const int k = info::kappa - dragonbox::floor_log10_pow2(exponent); + exp = -k; + const int beta = exponent + dragonbox::floor_log2_pow10(k); + uint64_t first_segment; + bool has_more_segments; + int digits_in_the_first_segment; + { + const auto r = dragonbox::umul192_upper128( + significand << beta, dragonbox::get_cached_power(k)); + first_segment = r.high(); + has_more_segments = r.low() != 0; + + // The first segment can have 18 ~ 19 digits. + if (first_segment >= 1000000000000000000ULL) { + digits_in_the_first_segment = 19; + } else { + // When it is of 18-digits, we align it to 19-digits by adding a bogus + // zero at the end. + digits_in_the_first_segment = 18; + first_segment *= 10; + } + } + + // Compute the actual number of decimal digits to print. + if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment); + + // Use Dragon4 only when there might be not enough digits in the first + // segment. + if (digits_in_the_first_segment > precision) { + use_dragon = false; + + if (precision <= 0) { + exp += digits_in_the_first_segment; + + if (precision < 0) { + // Nothing to do, since all we have are just leading zeros. + buf.try_resize(0); + } else { + // We may need to round-up. + buf.try_resize(1); + if ((first_segment | static_cast(has_more_segments)) > + 5000000000000000000ULL) { + buf[0] = '1'; + } else { + buf[0] = '0'; + } + } + } // precision <= 0 + else { + exp += digits_in_the_first_segment - precision; + + // When precision > 0, we divide the first segment into three + // subsegments, each with 9, 9, and 0 ~ 1 digits so that each fits + // in 32-bits which usually allows faster calculation than in + // 64-bits. Since some compiler (e.g. MSVC) doesn't know how to optimize + // division-by-constant for large 64-bit divisors, we do it here + // manually. The magic number 7922816251426433760 below is equal to + // ceil(2^(64+32) / 10^10). + const uint32_t first_subsegment = static_cast( + dragonbox::umul128_upper64(first_segment, 7922816251426433760ULL) >> + 32); + const uint64_t second_third_subsegments = + first_segment - first_subsegment * 10000000000ULL; + + uint64_t prod; + uint32_t digits; + bool should_round_up; + int number_of_digits_to_print = precision > 9 ? 9 : precision; + + // Print a 9-digits subsegment, either the first or the second. + auto print_subsegment = [&](uint32_t subsegment, char* buffer) { + int number_of_digits_printed = 0; + + // If we want to print an odd number of digits from the subsegment, + if ((number_of_digits_to_print & 1) != 0) { + // Convert to 64-bit fixed-point fractional form with 1-digit + // integer part. The magic number 720575941 is a good enough + // approximation of 2^(32 + 24) / 10^8; see + // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case + // for details. + prod = ((subsegment * static_cast(720575941)) >> 24) + 1; + digits = static_cast(prod >> 32); + *buffer = static_cast('0' + digits); + number_of_digits_printed++; + } + // If we want to print an even number of digits from the + // first_subsegment, + else { + // Convert to 64-bit fixed-point fractional form with 2-digits + // integer part. The magic number 450359963 is a good enough + // approximation of 2^(32 + 20) / 10^7; see + // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case + // for details. + prod = ((subsegment * static_cast(450359963)) >> 20) + 1; + digits = static_cast(prod >> 32); + copy2(buffer, digits2(digits)); + number_of_digits_printed += 2; + } + + // Print all digit pairs. + while (number_of_digits_printed < number_of_digits_to_print) { + prod = static_cast(prod) * static_cast(100); + digits = static_cast(prod >> 32); + copy2(buffer + number_of_digits_printed, digits2(digits)); + number_of_digits_printed += 2; + } + }; + + // Print first subsegment. + print_subsegment(first_subsegment, buf.data()); + + // Perform rounding if the first subsegment is the last subsegment to + // print. + if (precision <= 9) { + // Rounding inside the subsegment. + // We round-up if: + // - either the fractional part is strictly larger than 1/2, or + // - the fractional part is exactly 1/2 and the last digit is odd. + // We rely on the following observations: + // - If fractional_part >= threshold, then the fractional part is + // strictly larger than 1/2. + // - If the MSB of fractional_part is set, then the fractional part + // must be at least 1/2. + // - When the MSB of fractional_part is set, either + // second_third_subsegments being nonzero or has_more_segments + // being true means there are further digits not printed, so the + // fractional part is strictly larger than 1/2. + if (precision < 9) { + uint32_t fractional_part = static_cast(prod); + should_round_up = + fractional_part >= fractional_part_rounding_thresholds( + 8 - number_of_digits_to_print) || + ((fractional_part >> 31) & + ((digits & 1) | (second_third_subsegments != 0) | + has_more_segments)) != 0; + } + // Rounding at the subsegment boundary. + // In this case, the fractional part is at least 1/2 if and only if + // second_third_subsegments >= 5000000000ULL, and is strictly larger + // than 1/2 if we further have either second_third_subsegments > + // 5000000000ULL or has_more_segments == true. + else { + should_round_up = second_third_subsegments > 5000000000ULL || + (second_third_subsegments == 5000000000ULL && + ((digits & 1) != 0 || has_more_segments)); + } + } + // Otherwise, print the second subsegment. + else { + // Compilers are not aware of how to leverage the maximum value of + // second_third_subsegments to find out a better magic number which + // allows us to eliminate an additional shift. 1844674407370955162 = + // ceil(2^64/10) < ceil(2^64*(10^9/(10^10 - 1))). + const uint32_t second_subsegment = + static_cast(dragonbox::umul128_upper64( + second_third_subsegments, 1844674407370955162ULL)); + const uint32_t third_subsegment = + static_cast(second_third_subsegments) - + second_subsegment * 10; + + number_of_digits_to_print = precision - 9; + print_subsegment(second_subsegment, buf.data() + 9); + + // Rounding inside the subsegment. + if (precision < 18) { + // The condition third_subsegment != 0 implies that the segment was + // of 19 digits, so in this case the third segment should be + // consisting of a genuine digit from the input. + uint32_t fractional_part = static_cast(prod); + should_round_up = + fractional_part >= fractional_part_rounding_thresholds( + 8 - number_of_digits_to_print) || + ((fractional_part >> 31) & + ((digits & 1) | (third_subsegment != 0) | + has_more_segments)) != 0; + } + // Rounding at the subsegment boundary. + else { + // In this case, the segment must be of 19 digits, thus + // the third subsegment should be consisting of a genuine digit from + // the input. + should_round_up = third_subsegment > 5 || + (third_subsegment == 5 && + ((digits & 1) != 0 || has_more_segments)); + } + } + + // Round-up if necessary. + if (should_round_up) { + ++buf[precision - 1]; + for (int i = precision - 1; i > 0 && buf[i] > '9'; --i) { + buf[i] = '0'; + ++buf[i - 1]; + } + if (buf[0] > '9') { + buf[0] = '1'; + if (fixed) + buf[precision++] = '0'; + else + ++exp; + } + } + buf.try_resize(to_unsigned(precision)); + } + } // if (digits_in_the_first_segment > precision) + else { + // Adjust the exponent for its use in Dragon4. + exp += digits_in_the_first_segment - 1; + } + } + if (use_dragon) { + auto f = basic_fp(); + bool is_predecessor_closer = specs.binary32 + ? f.assign(static_cast(value)) + : f.assign(converted_value); + if (is_predecessor_closer) dragon_flags |= dragon::predecessor_closer; + if (fixed) dragon_flags |= dragon::fixed; + // Limit precision to the maximum possible number of significant digits in + // an IEEE754 double because we don't need to generate zeros. + const int max_double_digits = 767; + if (precision > max_double_digits) precision = max_double_digits; + format_dragon(f, dragon_flags, precision, buf, exp); + } + if (!fixed && !specs.showpoint) { + // Remove trailing zeros. + auto num_digits = buf.size(); + while (num_digits > 0 && buf[num_digits - 1] == '0') { + --num_digits; + ++exp; + } + buf.try_resize(num_digits); + } + return exp; +} + +template +FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, + locale_ref loc) -> OutputIt { + sign_t sign = specs.sign; + if (detail::signbit(value)) { // value < 0 is false for NaN so use signbit. + sign = sign::minus; + value = -value; + } else if (sign == sign::minus) { + sign = sign::none; + } + + if (!detail::isfinite(value)) + return write_nonfinite(out, detail::isnan(value), specs, sign); + + if (specs.align == align::numeric && sign) { + auto it = reserve(out, 1); + *it++ = detail::sign(sign); + out = base_iterator(out, it); + sign = sign::none; + if (specs.width != 0) --specs.width; + } + + memory_buffer buffer; + if (specs.type == presentation_type::hexfloat) { + if (sign) buffer.push_back(detail::sign(sign)); + format_hexfloat(convert_float(value), specs, buffer); + return write_bytes(out, {buffer.data(), buffer.size()}, + specs); + } + + int precision = specs.precision >= 0 || specs.type == presentation_type::none + ? specs.precision + : 6; + if (specs.type == presentation_type::exp) { + if (precision == max_value()) + report_error("number is too big"); + else + ++precision; + } else if (specs.type != presentation_type::fixed && precision == 0) { + precision = 1; + } + float_specs fspecs = parse_float_type_spec(specs); + fspecs.sign = sign; + if (const_check(std::is_same())) fspecs.binary32 = true; + int exp = format_float(convert_float(value), precision, fspecs, buffer); + fspecs.precision = precision; + auto f = big_decimal_fp{buffer.data(), static_cast(buffer.size()), exp}; + return write_float(out, f, specs, fspecs, loc); +} + +template ::value)> +FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs specs, + locale_ref loc = {}) -> OutputIt { + if (const_check(!is_supported_floating_point(value))) return out; + return specs.localized && write_loc(out, value, specs, loc) + ? out + : write_float(out, value, specs, loc); +} + +template ::value)> +FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt { + if (is_constant_evaluated()) return write(out, value, format_specs()); + if (const_check(!is_supported_floating_point(value))) return out; + + auto sign = sign_t::none; + if (detail::signbit(value)) { + sign = sign::minus; + value = -value; + } + + constexpr auto specs = format_specs(); + using floaty = conditional_t::value, double, T>; + using floaty_uint = typename dragonbox::float_info::carrier_uint; + floaty_uint mask = exponent_mask(); + if ((bit_cast(value) & mask) == mask) + return write_nonfinite(out, std::isnan(value), specs, sign); + + auto fspecs = float_specs(); + fspecs.sign = sign; + auto dec = dragonbox::to_decimal(static_cast(value)); + return write_float(out, dec, specs, fspecs, {}); +} + +template ::value && + !is_fast_float::value)> +inline auto write(OutputIt out, T value) -> OutputIt { + return write(out, value, format_specs()); +} + +template +auto write(OutputIt out, monostate, format_specs = {}, locale_ref = {}) + -> OutputIt { + FMT_ASSERT(false, ""); + return out; +} + +template +FMT_CONSTEXPR auto write(OutputIt out, basic_string_view value) + -> OutputIt { + return copy_noinline(value.begin(), value.end(), out); +} + +template ::value)> +constexpr auto write(OutputIt out, const T& value) -> OutputIt { + return write(out, to_string_view(value)); +} + +// FMT_ENABLE_IF() condition separated to workaround an MSVC bug. +template < + typename Char, typename OutputIt, typename T, + bool check = + std::is_enum::value && !std::is_same::value && + mapped_type_constant>::value != + type::custom_type, + FMT_ENABLE_IF(check)> +FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt { + return write(out, static_cast>(value)); +} + +template ::value)> +FMT_CONSTEXPR auto write(OutputIt out, T value, const format_specs& specs = {}, + locale_ref = {}) -> OutputIt { + return specs.type != presentation_type::none && + specs.type != presentation_type::string + ? write(out, value ? 1 : 0, specs, {}) + : write_bytes(out, value ? "true" : "false", specs); +} + +template +FMT_CONSTEXPR auto write(OutputIt out, Char value) -> OutputIt { + auto it = reserve(out, 1); + *it++ = value; + return base_iterator(out, it); +} + +template +FMT_CONSTEXPR20 auto write(OutputIt out, const Char* value) -> OutputIt { + if (value) return write(out, basic_string_view(value)); + report_error("string pointer is null"); + return out; +} + +template ::value)> +auto write(OutputIt out, const T* value, const format_specs& specs = {}, + locale_ref = {}) -> OutputIt { + return write_ptr(out, bit_cast(value), &specs); +} + +// A write overload that handles implicit conversions. +template > +FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> enable_if_t< + std::is_class::value && !has_to_string_view::value && + !is_floating_point::value && !std::is_same::value && + !std::is_same().map( + value))>>::value, + OutputIt> { + return write(out, arg_mapper().map(value)); +} + +template > +FMT_CONSTEXPR auto write(OutputIt out, const T& value) + -> enable_if_t::value == + type::custom_type && + !std::is_fundamental::value, + OutputIt> { + auto formatter = typename Context::template formatter_type(); + auto parse_ctx = typename Context::parse_context_type({}); + formatter.parse(parse_ctx); + auto ctx = Context(out, {}, {}); + return formatter.format(value, ctx); +} + +// An argument visitor that formats the argument and writes it via the output +// iterator. It's a class and not a generic lambda for compatibility with C++11. +template struct default_arg_formatter { + using iterator = basic_appender; + using context = buffered_context; + + iterator out; + basic_format_args args; + locale_ref loc; + + template auto operator()(T value) -> iterator { + return write(out, value); + } + auto operator()(typename basic_format_arg::handle h) -> iterator { + basic_format_parse_context parse_ctx({}); + context format_ctx(out, args, loc); + h.format(parse_ctx, format_ctx); + return format_ctx.out(); + } +}; + +template struct arg_formatter { + using iterator = basic_appender; + using context = buffered_context; + + iterator out; + const format_specs& specs; + locale_ref locale; + + template + FMT_CONSTEXPR FMT_INLINE auto operator()(T value) -> iterator { + return detail::write(out, value, specs, locale); + } + auto operator()(typename basic_format_arg::handle) -> iterator { + // User-defined types are handled separately because they require access + // to the parse context. + return out; + } +}; + +struct width_checker { + template ::value)> + FMT_CONSTEXPR auto operator()(T value) -> unsigned long long { + if (is_negative(value)) report_error("negative width"); + return static_cast(value); + } + + template ::value)> + FMT_CONSTEXPR auto operator()(T) -> unsigned long long { + report_error("width is not integer"); + return 0; + } +}; + +struct precision_checker { + template ::value)> + FMT_CONSTEXPR auto operator()(T value) -> unsigned long long { + if (is_negative(value)) report_error("negative precision"); + return static_cast(value); + } + + template ::value)> + FMT_CONSTEXPR auto operator()(T) -> unsigned long long { + report_error("precision is not integer"); + return 0; + } +}; + +template +FMT_CONSTEXPR auto get_dynamic_spec(FormatArg arg) -> int { + unsigned long long value = arg.visit(Handler()); + if (value > to_unsigned(max_value())) report_error("number is too big"); + return static_cast(value); +} + +template +FMT_CONSTEXPR auto get_arg(Context& ctx, ID id) -> decltype(ctx.arg(id)) { + auto arg = ctx.arg(id); + if (!arg) report_error("argument not found"); + return arg; +} + +template +FMT_CONSTEXPR void handle_dynamic_spec(int& value, + arg_ref ref, + Context& ctx) { + switch (ref.kind) { + case arg_id_kind::none: + break; + case arg_id_kind::index: + value = detail::get_dynamic_spec(get_arg(ctx, ref.val.index)); + break; + case arg_id_kind::name: + value = detail::get_dynamic_spec(get_arg(ctx, ref.val.name)); + break; + } +} + +#if FMT_USE_USER_DEFINED_LITERALS +# if FMT_USE_NONTYPE_TEMPLATE_ARGS +template Str> +struct statically_named_arg : view { + static constexpr auto name = Str.data; + + const T& value; + statically_named_arg(const T& v) : value(v) {} +}; + +template Str> +struct is_named_arg> : std::true_type {}; + +template Str> +struct is_statically_named_arg> + : std::true_type {}; + +template Str> +struct udl_arg { + template auto operator=(T&& value) const { + return statically_named_arg(std::forward(value)); + } +}; +# else +template struct udl_arg { + const Char* str; + + template auto operator=(T&& value) const -> named_arg { + return {str, std::forward(value)}; + } +}; +# endif +#endif // FMT_USE_USER_DEFINED_LITERALS + +template +auto vformat(const Locale& loc, basic_string_view fmt, + typename detail::vformat_args::type args) + -> std::basic_string { + auto buf = basic_memory_buffer(); + detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); + return {buf.data(), buf.size()}; +} + +using format_func = void (*)(detail::buffer&, int, const char*); + +FMT_API void format_error_code(buffer& out, int error_code, + string_view message) noexcept; + +using fmt::report_error; +FMT_API void report_error(format_func func, int error_code, + const char* message) noexcept; +} // namespace detail + +FMT_BEGIN_EXPORT +FMT_API auto vsystem_error(int error_code, string_view format_str, + format_args args) -> std::system_error; + +/** + * Constructs `std::system_error` with a message formatted with + * `fmt::format(fmt, args...)`. + * `error_code` is a system error code as given by `errno`. + * + * **Example**: + * + * // This throws std::system_error with the description + * // cannot open file 'madeup': No such file or directory + * // or similar (system message may vary). + * const char* filename = "madeup"; + * std::FILE* file = std::fopen(filename, "r"); + * if (!file) + * throw fmt::system_error(errno, "cannot open file '{}'", filename); + */ +template +auto system_error(int error_code, format_string fmt, T&&... args) + -> std::system_error { + return vsystem_error(error_code, fmt, fmt::make_format_args(args...)); +} + +/** + * Formats an error message for an error returned by an operating system or a + * language runtime, for example a file opening error, and writes it to `out`. + * The format is the same as the one used by `std::system_error(ec, message)` + * where `ec` is `std::error_code(error_code, std::generic_category())`. + * It is implementation-defined but normally looks like: + * + * : + * + * where `` is the passed message and `` is the system + * message corresponding to the error code. + * `error_code` is a system error code as given by `errno`. + */ +FMT_API void format_system_error(detail::buffer& out, int error_code, + const char* message) noexcept; + +// Reports a system error without throwing an exception. +// Can be used to report errors from destructors. +FMT_API void report_system_error(int error_code, const char* message) noexcept; + +/// A fast integer formatter. +class format_int { + private: + // Buffer should be large enough to hold all digits (digits10 + 1), + // a sign and a null character. + enum { buffer_size = std::numeric_limits::digits10 + 3 }; + mutable char buffer_[buffer_size]; + char* str_; + + template + FMT_CONSTEXPR20 auto format_unsigned(UInt value) -> char* { + auto n = static_cast>(value); + return detail::format_decimal(buffer_, n, buffer_size - 1).begin; + } + + template + FMT_CONSTEXPR20 auto format_signed(Int value) -> char* { + auto abs_value = static_cast>(value); + bool negative = value < 0; + if (negative) abs_value = 0 - abs_value; + auto begin = format_unsigned(abs_value); + if (negative) *--begin = '-'; + return begin; + } + + public: + explicit FMT_CONSTEXPR20 format_int(int value) : str_(format_signed(value)) {} + explicit FMT_CONSTEXPR20 format_int(long value) + : str_(format_signed(value)) {} + explicit FMT_CONSTEXPR20 format_int(long long value) + : str_(format_signed(value)) {} + explicit FMT_CONSTEXPR20 format_int(unsigned value) + : str_(format_unsigned(value)) {} + explicit FMT_CONSTEXPR20 format_int(unsigned long value) + : str_(format_unsigned(value)) {} + explicit FMT_CONSTEXPR20 format_int(unsigned long long value) + : str_(format_unsigned(value)) {} + + /// Returns the number of characters written to the output buffer. + FMT_CONSTEXPR20 auto size() const -> size_t { + return detail::to_unsigned(buffer_ - str_ + buffer_size - 1); + } + + /// Returns a pointer to the output buffer content. No terminating null + /// character is appended. + FMT_CONSTEXPR20 auto data() const -> const char* { return str_; } + + /// Returns a pointer to the output buffer content with terminating null + /// character appended. + FMT_CONSTEXPR20 auto c_str() const -> const char* { + buffer_[buffer_size - 1] = '\0'; + return str_; + } + + /// Returns the content of the output buffer as an `std::string`. + auto str() const -> std::string { return std::string(str_, size()); } +}; + +template +struct formatter::value>> + : formatter, Char> { + template + auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) { + auto&& val = format_as(value); // Make an lvalue reference for format. + return formatter, Char>::format(val, ctx); + } +}; + +#define FMT_FORMAT_AS(Type, Base) \ + template \ + struct formatter : formatter { \ + template \ + auto format(Type value, FormatContext& ctx) const -> decltype(ctx.out()) { \ + return formatter::format(value, ctx); \ + } \ + } + +FMT_FORMAT_AS(signed char, int); +FMT_FORMAT_AS(unsigned char, unsigned); +FMT_FORMAT_AS(short, int); +FMT_FORMAT_AS(unsigned short, unsigned); +FMT_FORMAT_AS(long, detail::long_type); +FMT_FORMAT_AS(unsigned long, detail::ulong_type); +FMT_FORMAT_AS(Char*, const Char*); +FMT_FORMAT_AS(std::nullptr_t, const void*); +FMT_FORMAT_AS(detail::std_string_view, basic_string_view); +FMT_FORMAT_AS(void*, const void*); + +template +class formatter, Char> + : public formatter, Char> {}; + +template +struct formatter : formatter, Char> {}; + +/** + * Converts `p` to `const void*` for pointer formatting. + * + * **Example**: + * + * auto s = fmt::format("{}", fmt::ptr(p)); + */ +template auto ptr(T p) -> const void* { + static_assert(std::is_pointer::value, ""); + return detail::bit_cast(p); +} + +/** + * Converts `e` to the underlying type. + * + * **Example**: + * + * enum class color { red, green, blue }; + * auto s = fmt::format("{}", fmt::underlying(color::red)); + */ +template +constexpr auto underlying(Enum e) noexcept -> underlying_t { + return static_cast>(e); +} + +namespace enums { +template ::value)> +constexpr auto format_as(Enum e) noexcept -> underlying_t { + return static_cast>(e); +} +} // namespace enums + +class bytes { + private: + string_view data_; + friend struct formatter; + + public: + explicit bytes(string_view data) : data_(data) {} +}; + +template <> struct formatter { + private: + detail::dynamic_format_specs<> specs_; + + public: + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* { + return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, + detail::type::string_type); + } + + template + auto format(bytes b, FormatContext& ctx) const -> decltype(ctx.out()) { + auto specs = specs_; + detail::handle_dynamic_spec(specs.width, + specs.width_ref, ctx); + detail::handle_dynamic_spec( + specs.precision, specs.precision_ref, ctx); + return detail::write_bytes(ctx.out(), b.data_, specs); + } +}; + +// group_digits_view is not derived from view because it copies the argument. +template struct group_digits_view { + T value; +}; + +/** + * Returns a view that formats an integer value using ',' as a + * locale-independent thousands separator. + * + * **Example**: + * + * fmt::print("{}", fmt::group_digits(12345)); + * // Output: "12,345" + */ +template auto group_digits(T value) -> group_digits_view { + return {value}; +} + +template struct formatter> : formatter { + private: + detail::dynamic_format_specs<> specs_; + + public: + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* { + return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, + detail::type::int_type); + } + + template + auto format(group_digits_view t, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto specs = specs_; + detail::handle_dynamic_spec(specs.width, + specs.width_ref, ctx); + detail::handle_dynamic_spec( + specs.precision, specs.precision_ref, ctx); + auto arg = detail::make_write_int_arg(t.value, specs.sign); + return detail::write_int( + ctx.out(), static_cast>(arg.abs_value), + arg.prefix, specs, detail::digit_grouping("\3", ",")); + } +}; + +template struct nested_view { + const formatter* fmt; + const T* value; +}; + +template +struct formatter, Char> { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + template + auto format(nested_view view, FormatContext& ctx) const + -> decltype(ctx.out()) { + return view.fmt->format(*view.value, ctx); + } +}; + +template struct nested_formatter { + private: + int width_; + detail::fill_t fill_; + align_t align_ : 4; + formatter formatter_; + + public: + constexpr nested_formatter() : width_(0), align_(align_t::none) {} + + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto specs = detail::dynamic_format_specs(); + auto it = parse_format_specs(ctx.begin(), ctx.end(), specs, ctx, + detail::type::none_type); + width_ = specs.width; + fill_ = specs.fill; + align_ = specs.align; + ctx.advance_to(it); + return formatter_.parse(ctx); + } + + template + auto write_padded(FormatContext& ctx, F write) const -> decltype(ctx.out()) { + if (width_ == 0) return write(ctx.out()); + auto buf = basic_memory_buffer(); + write(basic_appender(buf)); + auto specs = format_specs(); + specs.width = width_; + specs.fill = fill_; + specs.align = align_; + return detail::write( + ctx.out(), basic_string_view(buf.data(), buf.size()), specs); + } + + auto nested(const T& value) const -> nested_view { + return nested_view{&formatter_, &value}; + } +}; + +/** + * Converts `value` to `std::string` using the default format for type `T`. + * + * **Example**: + * + * std::string answer = fmt::to_string(42); + */ +template ::value && + !detail::has_format_as::value)> +inline auto to_string(const T& value) -> std::string { + auto buffer = memory_buffer(); + detail::write(appender(buffer), value); + return {buffer.data(), buffer.size()}; +} + +template ::value)> +FMT_NODISCARD inline auto to_string(T value) -> std::string { + // The buffer should be large enough to store the number including the sign + // or "false" for bool. + constexpr int max_size = detail::digits10() + 2; + char buffer[max_size > 5 ? static_cast(max_size) : 5]; + char* begin = buffer; + return std::string(begin, detail::write(begin, value)); +} + +template +FMT_NODISCARD auto to_string(const basic_memory_buffer& buf) + -> std::basic_string { + auto size = buf.size(); + detail::assume(size < std::basic_string().max_size()); + return std::basic_string(buf.data(), size); +} + +template ::value && + detail::has_format_as::value)> +inline auto to_string(const T& value) -> std::string { + return to_string(format_as(value)); +} + +FMT_END_EXPORT + +namespace detail { + +template +void vformat_to(buffer& buf, basic_string_view fmt, + typename vformat_args::type args, locale_ref loc) { + auto out = basic_appender(buf); + if (fmt.size() == 2 && equal2(fmt.data(), "{}")) { + auto arg = args.get(0); + if (!arg) report_error("argument not found"); + arg.visit(default_arg_formatter{out, args, loc}); + return; + } + + struct format_handler { + basic_format_parse_context parse_context; + buffered_context context; + + format_handler(basic_appender p_out, basic_string_view str, + basic_format_args> p_args, + locale_ref p_loc) + : parse_context(str), context(p_out, p_args, p_loc) {} + + void on_text(const Char* begin, const Char* end) { + auto text = basic_string_view(begin, to_unsigned(end - begin)); + context.advance_to(write(context.out(), text)); + } + + FMT_CONSTEXPR auto on_arg_id() -> int { + return parse_context.next_arg_id(); + } + FMT_CONSTEXPR auto on_arg_id(int id) -> int { + parse_context.check_arg_id(id); + return id; + } + FMT_CONSTEXPR auto on_arg_id(basic_string_view id) -> int { + parse_context.check_arg_id(id); + int arg_id = context.arg_id(id); + if (arg_id < 0) report_error("argument not found"); + return arg_id; + } + + FMT_INLINE void on_replacement_field(int id, const Char*) { + auto arg = get_arg(context, id); + context.advance_to(arg.visit(default_arg_formatter{ + context.out(), context.args(), context.locale()})); + } + + auto on_format_specs(int id, const Char* begin, const Char* end) + -> const Char* { + auto arg = get_arg(context, id); + // Not using a visitor for custom types gives better codegen. + if (arg.format_custom(begin, parse_context, context)) + return parse_context.begin(); + auto specs = detail::dynamic_format_specs(); + begin = parse_format_specs(begin, end, specs, parse_context, arg.type()); + detail::handle_dynamic_spec( + specs.width, specs.width_ref, context); + detail::handle_dynamic_spec( + specs.precision, specs.precision_ref, context); + if (begin == end || *begin != '}') + report_error("missing '}' in format string"); + context.advance_to(arg.visit( + arg_formatter{context.out(), specs, context.locale()})); + return begin; + } + + FMT_NORETURN void on_error(const char* message) { report_error(message); } + }; + detail::parse_format_string(fmt, format_handler(out, fmt, args, loc)); +} + +FMT_BEGIN_EXPORT + +#ifndef FMT_HEADER_ONLY +extern template FMT_API void vformat_to(buffer&, string_view, + typename vformat_args<>::type, + locale_ref); +extern template FMT_API auto thousands_sep_impl(locale_ref) + -> thousands_sep_result; +extern template FMT_API auto thousands_sep_impl(locale_ref) + -> thousands_sep_result; +extern template FMT_API auto decimal_point_impl(locale_ref) -> char; +extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t; +#endif // FMT_HEADER_ONLY + +FMT_END_EXPORT + +template +template +FMT_CONSTEXPR FMT_INLINE auto native_formatter::format( + const T& val, FormatContext& ctx) const -> decltype(ctx.out()) { + if (specs_.width_ref.kind == arg_id_kind::none && + specs_.precision_ref.kind == arg_id_kind::none) { + return write(ctx.out(), val, specs_, ctx.locale()); + } + auto specs = specs_; + handle_dynamic_spec(specs.width, specs.width_ref, ctx); + handle_dynamic_spec(specs.precision, specs.precision_ref, + ctx); + return write(ctx.out(), val, specs, ctx.locale()); +} + +} // namespace detail + +FMT_BEGIN_EXPORT + +template +struct formatter + : detail::native_formatter {}; + +#if FMT_USE_USER_DEFINED_LITERALS +inline namespace literals { +/** + * User-defined literal equivalent of `fmt::arg`. + * + * **Example**: + * + * using namespace fmt::literals; + * fmt::print("The answer is {answer}.", "answer"_a=42); + */ +# if FMT_USE_NONTYPE_TEMPLATE_ARGS +template constexpr auto operator""_a() { + using char_t = remove_cvref_t; + return detail::udl_arg(); +} +# else +constexpr auto operator""_a(const char* s, size_t) -> detail::udl_arg { + return {s}; +} +# endif +} // namespace literals +#endif // FMT_USE_USER_DEFINED_LITERALS + +FMT_API auto vformat(string_view fmt, format_args args) -> std::string; + +/** + * Formats `args` according to specifications in `fmt` and returns the result + * as a string. + * + * **Example**: + * + * #include + * std::string message = fmt::format("The answer is {}.", 42); + */ +template +FMT_NODISCARD FMT_INLINE auto format(format_string fmt, T&&... args) + -> std::string { + return vformat(fmt, fmt::make_format_args(args...)); +} + +template ::value)> +inline auto vformat(const Locale& loc, string_view fmt, format_args args) + -> std::string { + return detail::vformat(loc, fmt, args); +} + +template ::value)> +inline auto format(const Locale& loc, format_string fmt, T&&... args) + -> std::string { + return fmt::vformat(loc, string_view(fmt), fmt::make_format_args(args...)); +} + +template ::value&& + detail::is_locale::value)> +auto vformat_to(OutputIt out, const Locale& loc, string_view fmt, + format_args args) -> OutputIt { + using detail::get_buffer; + auto&& buf = get_buffer(out); + detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); + return detail::get_iterator(buf, out); +} + +template ::value&& + detail::is_locale::value)> +FMT_INLINE auto format_to(OutputIt out, const Locale& loc, + format_string fmt, T&&... args) -> OutputIt { + return vformat_to(out, loc, fmt, fmt::make_format_args(args...)); +} + +template ::value)> +FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc, + format_string fmt, + T&&... args) -> size_t { + auto buf = detail::counting_buffer<>(); + detail::vformat_to(buf, fmt, fmt::make_format_args(args...), + detail::locale_ref(loc)); + return buf.count(); +} + +FMT_END_EXPORT + +FMT_END_NAMESPACE + +#ifdef FMT_HEADER_ONLY +# define FMT_FUNC inline +# include "format-inl.h" +#else +# define FMT_FUNC +#endif + +// Restore _LIBCPP_REMOVE_TRANSITIVE_INCLUDES. +#ifdef FMT_REMOVE_TRANSITIVE_INCLUDES +# undef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES +#endif + +#endif // FMT_FORMAT_H_ diff --git a/include/spdlog/fmt/bundled/locale.h b/include/spdlog/fmt/bundled/locale.h new file mode 100644 index 0000000..7571b52 --- /dev/null +++ b/include/spdlog/fmt/bundled/locale.h @@ -0,0 +1,2 @@ +#include "xchar.h" +#warning fmt/locale.h is deprecated, include fmt/format.h or fmt/xchar.h instead diff --git a/include/spdlog/fmt/bundled/os.h b/include/spdlog/fmt/bundled/os.h new file mode 100644 index 0000000..5c85ea0 --- /dev/null +++ b/include/spdlog/fmt/bundled/os.h @@ -0,0 +1,439 @@ +// Formatting library for C++ - optional OS-specific functionality +// +// Copyright (c) 2012 - present, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_OS_H_ +#define FMT_OS_H_ + +#include "format.h" + +#ifndef FMT_MODULE +# include +# include +# include +# include // std::system_error + +# if FMT_HAS_INCLUDE() +# include // LC_NUMERIC_MASK on macOS +# endif +#endif // FMT_MODULE + +#ifndef FMT_USE_FCNTL +// UWP doesn't provide _pipe. +# if FMT_HAS_INCLUDE("winapifamily.h") +# include +# endif +# if (FMT_HAS_INCLUDE() || defined(__APPLE__) || \ + defined(__linux__)) && \ + (!defined(WINAPI_FAMILY) || \ + (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) +# include // for O_RDONLY +# define FMT_USE_FCNTL 1 +# else +# define FMT_USE_FCNTL 0 +# endif +#endif + +#ifndef FMT_POSIX +# if defined(_WIN32) && !defined(__MINGW32__) +// Fix warnings about deprecated symbols. +# define FMT_POSIX(call) _##call +# else +# define FMT_POSIX(call) call +# endif +#endif + +// Calls to system functions are wrapped in FMT_SYSTEM for testability. +#ifdef FMT_SYSTEM +# define FMT_HAS_SYSTEM +# define FMT_POSIX_CALL(call) FMT_SYSTEM(call) +#else +# define FMT_SYSTEM(call) ::call +# ifdef _WIN32 +// Fix warnings about deprecated symbols. +# define FMT_POSIX_CALL(call) ::_##call +# else +# define FMT_POSIX_CALL(call) ::call +# endif +#endif + +// Retries the expression while it evaluates to error_result and errno +// equals to EINTR. +#ifndef _WIN32 +# define FMT_RETRY_VAL(result, expression, error_result) \ + do { \ + (result) = (expression); \ + } while ((result) == (error_result) && errno == EINTR) +#else +# define FMT_RETRY_VAL(result, expression, error_result) result = (expression) +#endif + +#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1) + +FMT_BEGIN_NAMESPACE +FMT_BEGIN_EXPORT + +/** + * A reference to a null-terminated string. It can be constructed from a C + * string or `std::string`. + * + * You can use one of the following type aliases for common character types: + * + * +---------------+-----------------------------+ + * | Type | Definition | + * +===============+=============================+ + * | cstring_view | basic_cstring_view | + * +---------------+-----------------------------+ + * | wcstring_view | basic_cstring_view | + * +---------------+-----------------------------+ + * + * This class is most useful as a parameter type for functions that wrap C APIs. + */ +template class basic_cstring_view { + private: + const Char* data_; + + public: + /// Constructs a string reference object from a C string. + basic_cstring_view(const Char* s) : data_(s) {} + + /// Constructs a string reference from an `std::string` object. + basic_cstring_view(const std::basic_string& s) : data_(s.c_str()) {} + + /// Returns the pointer to a C string. + auto c_str() const -> const Char* { return data_; } +}; + +using cstring_view = basic_cstring_view; +using wcstring_view = basic_cstring_view; + +#ifdef _WIN32 +FMT_API const std::error_category& system_category() noexcept; + +namespace detail { +FMT_API void format_windows_error(buffer& out, int error_code, + const char* message) noexcept; +} + +FMT_API std::system_error vwindows_error(int error_code, string_view format_str, + format_args args); + +/** + * Constructs a `std::system_error` object with the description of the form + * + * : + * + * where `` is the formatted message and `` is the + * system message corresponding to the error code. + * `error_code` is a Windows error code as given by `GetLastError`. + * If `error_code` is not a valid error code such as -1, the system message + * will look like "error -1". + * + * **Example**: + * + * // This throws a system_error with the description + * // cannot open file 'madeup': The system cannot find the file + * specified. + * // or similar (system message may vary). + * const char *filename = "madeup"; + * LPOFSTRUCT of = LPOFSTRUCT(); + * HFILE file = OpenFile(filename, &of, OF_READ); + * if (file == HFILE_ERROR) { + * throw fmt::windows_error(GetLastError(), + * "cannot open file '{}'", filename); + * } + */ +template +std::system_error windows_error(int error_code, string_view message, + const Args&... args) { + return vwindows_error(error_code, message, fmt::make_format_args(args...)); +} + +// Reports a Windows error without throwing an exception. +// Can be used to report errors from destructors. +FMT_API void report_windows_error(int error_code, const char* message) noexcept; +#else +inline auto system_category() noexcept -> const std::error_category& { + return std::system_category(); +} +#endif // _WIN32 + +// std::system is not available on some platforms such as iOS (#2248). +#ifdef __OSX__ +template > +void say(const S& format_str, Args&&... args) { + std::system(format("say \"{}\"", format(format_str, args...)).c_str()); +} +#endif + +// A buffered file. +class buffered_file { + private: + FILE* file_; + + friend class file; + + explicit buffered_file(FILE* f) : file_(f) {} + + public: + buffered_file(const buffered_file&) = delete; + void operator=(const buffered_file&) = delete; + + // Constructs a buffered_file object which doesn't represent any file. + buffered_file() noexcept : file_(nullptr) {} + + // Destroys the object closing the file it represents if any. + FMT_API ~buffered_file() noexcept; + + public: + buffered_file(buffered_file&& other) noexcept : file_(other.file_) { + other.file_ = nullptr; + } + + auto operator=(buffered_file&& other) -> buffered_file& { + close(); + file_ = other.file_; + other.file_ = nullptr; + return *this; + } + + // Opens a file. + FMT_API buffered_file(cstring_view filename, cstring_view mode); + + // Closes the file. + FMT_API void close(); + + // Returns the pointer to a FILE object representing this file. + auto get() const noexcept -> FILE* { return file_; } + + FMT_API auto descriptor() const -> int; + + template + inline void print(string_view fmt, const T&... args) { + const auto& vargs = fmt::make_format_args(args...); + detail::is_locking() ? fmt::vprint_buffered(file_, fmt, vargs) + : fmt::vprint(file_, fmt, vargs); + } +}; + +#if FMT_USE_FCNTL + +// A file. Closed file is represented by a file object with descriptor -1. +// Methods that are not declared with noexcept may throw +// fmt::system_error in case of failure. Note that some errors such as +// closing the file multiple times will cause a crash on Windows rather +// than an exception. You can get standard behavior by overriding the +// invalid parameter handler with _set_invalid_parameter_handler. +class FMT_API file { + private: + int fd_; // File descriptor. + + // Constructs a file object with a given descriptor. + explicit file(int fd) : fd_(fd) {} + + friend struct pipe; + + public: + // Possible values for the oflag argument to the constructor. + enum { + RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only. + WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only. + RDWR = FMT_POSIX(O_RDWR), // Open for reading and writing. + CREATE = FMT_POSIX(O_CREAT), // Create if the file doesn't exist. + APPEND = FMT_POSIX(O_APPEND), // Open in append mode. + TRUNC = FMT_POSIX(O_TRUNC) // Truncate the content of the file. + }; + + // Constructs a file object which doesn't represent any file. + file() noexcept : fd_(-1) {} + + // Opens a file and constructs a file object representing this file. + file(cstring_view path, int oflag); + + public: + file(const file&) = delete; + void operator=(const file&) = delete; + + file(file&& other) noexcept : fd_(other.fd_) { other.fd_ = -1; } + + // Move assignment is not noexcept because close may throw. + auto operator=(file&& other) -> file& { + close(); + fd_ = other.fd_; + other.fd_ = -1; + return *this; + } + + // Destroys the object closing the file it represents if any. + ~file() noexcept; + + // Returns the file descriptor. + auto descriptor() const noexcept -> int { return fd_; } + + // Closes the file. + void close(); + + // Returns the file size. The size has signed type for consistency with + // stat::st_size. + auto size() const -> long long; + + // Attempts to read count bytes from the file into the specified buffer. + auto read(void* buffer, size_t count) -> size_t; + + // Attempts to write count bytes from the specified buffer to the file. + auto write(const void* buffer, size_t count) -> size_t; + + // Duplicates a file descriptor with the dup function and returns + // the duplicate as a file object. + static auto dup(int fd) -> file; + + // Makes fd be the copy of this file descriptor, closing fd first if + // necessary. + void dup2(int fd); + + // Makes fd be the copy of this file descriptor, closing fd first if + // necessary. + void dup2(int fd, std::error_code& ec) noexcept; + + // Creates a buffered_file object associated with this file and detaches + // this file object from the file. + auto fdopen(const char* mode) -> buffered_file; + +# if defined(_WIN32) && !defined(__MINGW32__) + // Opens a file and constructs a file object representing this file by + // wcstring_view filename. Windows only. + static file open_windows_file(wcstring_view path, int oflag); +# endif +}; + +struct FMT_API pipe { + file read_end; + file write_end; + + // Creates a pipe setting up read_end and write_end file objects for reading + // and writing respectively. + pipe(); +}; + +// Returns the memory page size. +auto getpagesize() -> long; + +namespace detail { + +struct buffer_size { + buffer_size() = default; + size_t value = 0; + auto operator=(size_t val) const -> buffer_size { + auto bs = buffer_size(); + bs.value = val; + return bs; + } +}; + +struct ostream_params { + int oflag = file::WRONLY | file::CREATE | file::TRUNC; + size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768; + + ostream_params() {} + + template + ostream_params(T... params, int new_oflag) : ostream_params(params...) { + oflag = new_oflag; + } + + template + ostream_params(T... params, detail::buffer_size bs) + : ostream_params(params...) { + this->buffer_size = bs.value; + } + +// Intel has a bug that results in failure to deduce a constructor +// for empty parameter packs. +# if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000 + ostream_params(int new_oflag) : oflag(new_oflag) {} + ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {} +# endif +}; + +class file_buffer final : public buffer { + private: + file file_; + + FMT_API static void grow(buffer& buf, size_t); + + public: + FMT_API file_buffer(cstring_view path, const ostream_params& params); + FMT_API file_buffer(file_buffer&& other) noexcept; + FMT_API ~file_buffer(); + + void flush() { + if (size() == 0) return; + file_.write(data(), size() * sizeof(data()[0])); + clear(); + } + + void close() { + flush(); + file_.close(); + } +}; + +} // namespace detail + +constexpr auto buffer_size = detail::buffer_size(); + +/// A fast output stream for writing from a single thread. Writing from +/// multiple threads without external synchronization may result in a data race. +class FMT_API ostream { + private: + FMT_MSC_WARNING(suppress : 4251) + detail::file_buffer buffer_; + + ostream(cstring_view path, const detail::ostream_params& params) + : buffer_(path, params) {} + + public: + ostream(ostream&& other) : buffer_(std::move(other.buffer_)) {} + + ~ostream(); + + void flush() { buffer_.flush(); } + + template + friend auto output_file(cstring_view path, T... params) -> ostream; + + void close() { buffer_.close(); } + + /// Formats `args` according to specifications in `fmt` and writes the + /// output to the file. + template void print(format_string fmt, T&&... args) { + vformat_to(appender(buffer_), fmt, fmt::make_format_args(args...)); + } +}; + +/** + * Opens a file for writing. Supported parameters passed in `params`: + * + * - ``: Flags passed to [open]( + * https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html) + * (`file::WRONLY | file::CREATE | file::TRUNC` by default) + * - `buffer_size=`: Output buffer size + * + * **Example**: + * + * auto out = fmt::output_file("guide.txt"); + * out.print("Don't {}", "Panic"); + */ +template +inline auto output_file(cstring_view path, T... params) -> ostream { + return {path, detail::ostream_params(params...)}; +} +#endif // FMT_USE_FCNTL + +FMT_END_EXPORT +FMT_END_NAMESPACE + +#endif // FMT_OS_H_ diff --git a/include/spdlog/fmt/bundled/ostream.h b/include/spdlog/fmt/bundled/ostream.h new file mode 100644 index 0000000..98faef6 --- /dev/null +++ b/include/spdlog/fmt/bundled/ostream.h @@ -0,0 +1,211 @@ +// Formatting library for C++ - std::ostream support +// +// Copyright (c) 2012 - present, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_OSTREAM_H_ +#define FMT_OSTREAM_H_ + +#ifndef FMT_MODULE +# include // std::filebuf +#endif + +#ifdef _WIN32 +# ifdef __GLIBCXX__ +# include +# include +# endif +# include +#endif + +#include "chrono.h" // formatbuf + +FMT_BEGIN_NAMESPACE +namespace detail { + +// Generate a unique explicit instantion in every translation unit using a tag +// type in an anonymous namespace. +namespace { +struct file_access_tag {}; +} // namespace +template +class file_access { + friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; } +}; + +#if FMT_MSC_VERSION +template class file_access; +auto get_file(std::filebuf&) -> FILE*; +#endif + +inline auto write_ostream_unicode(std::ostream& os, fmt::string_view data) + -> bool { + FILE* f = nullptr; +#if FMT_MSC_VERSION && FMT_USE_RTTI + if (auto* buf = dynamic_cast(os.rdbuf())) + f = get_file(*buf); + else + return false; +#elif defined(_WIN32) && defined(__GLIBCXX__) && FMT_USE_RTTI + auto* rdbuf = os.rdbuf(); + if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf*>(rdbuf)) + f = sfbuf->file(); + else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf*>(rdbuf)) + f = fbuf->file(); + else + return false; +#else + ignore_unused(os, data, f); +#endif +#ifdef _WIN32 + if (f) { + int fd = _fileno(f); + if (_isatty(fd)) { + os.flush(); + return write_console(fd, data); + } + } +#endif + return false; +} +inline auto write_ostream_unicode(std::wostream&, + fmt::basic_string_view) -> bool { + return false; +} + +// Write the content of buf to os. +// It is a separate function rather than a part of vprint to simplify testing. +template +void write_buffer(std::basic_ostream& os, buffer& buf) { + const Char* buf_data = buf.data(); + using unsigned_streamsize = std::make_unsigned::type; + unsigned_streamsize size = buf.size(); + unsigned_streamsize max_size = to_unsigned(max_value()); + do { + unsigned_streamsize n = size <= max_size ? size : max_size; + os.write(buf_data, static_cast(n)); + buf_data += n; + size -= n; + } while (size != 0); +} + +template +void format_value(buffer& buf, const T& value) { + auto&& format_buf = formatbuf>(buf); + auto&& output = std::basic_ostream(&format_buf); +#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) + output.imbue(std::locale::classic()); // The default is always unlocalized. +#endif + output << value; + output.exceptions(std::ios_base::failbit | std::ios_base::badbit); +} + +template struct streamed_view { + const T& value; +}; + +} // namespace detail + +// Formats an object of type T that has an overloaded ostream operator<<. +template +struct basic_ostream_formatter : formatter, Char> { + void set_debug_format() = delete; + + template + auto format(const T& value, Context& ctx) const -> decltype(ctx.out()) { + auto buffer = basic_memory_buffer(); + detail::format_value(buffer, value); + return formatter, Char>::format( + {buffer.data(), buffer.size()}, ctx); + } +}; + +using ostream_formatter = basic_ostream_formatter; + +template +struct formatter, Char> + : basic_ostream_formatter { + template + auto format(detail::streamed_view view, Context& ctx) const + -> decltype(ctx.out()) { + return basic_ostream_formatter::format(view.value, ctx); + } +}; + +/** + * Returns a view that formats `value` via an ostream `operator<<`. + * + * **Example**: + * + * fmt::print("Current thread id: {}\n", + * fmt::streamed(std::this_thread::get_id())); + */ +template +constexpr auto streamed(const T& value) -> detail::streamed_view { + return {value}; +} + +namespace detail { + +inline void vprint_directly(std::ostream& os, string_view format_str, + format_args args) { + auto buffer = memory_buffer(); + detail::vformat_to(buffer, format_str, args); + detail::write_buffer(os, buffer); +} + +} // namespace detail + +FMT_EXPORT template +void vprint(std::basic_ostream& os, + basic_string_view> format_str, + typename detail::vformat_args::type args) { + auto buffer = basic_memory_buffer(); + detail::vformat_to(buffer, format_str, args); + if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return; + detail::write_buffer(os, buffer); +} + +/** + * Prints formatted data to the stream `os`. + * + * **Example**: + * + * fmt::print(cerr, "Don't {}!", "panic"); + */ +FMT_EXPORT template +void print(std::ostream& os, format_string fmt, T&&... args) { + const auto& vargs = fmt::make_format_args(args...); + if (detail::use_utf8()) + vprint(os, fmt, vargs); + else + detail::vprint_directly(os, fmt, vargs); +} + +FMT_EXPORT +template +void print(std::wostream& os, + basic_format_string...> fmt, + Args&&... args) { + vprint(os, fmt, fmt::make_format_args>(args...)); +} + +FMT_EXPORT template +void println(std::ostream& os, format_string fmt, T&&... args) { + fmt::print(os, "{}\n", fmt::format(fmt, std::forward(args)...)); +} + +FMT_EXPORT +template +void println(std::wostream& os, + basic_format_string...> fmt, + Args&&... args) { + print(os, L"{}\n", fmt::format(fmt, std::forward(args)...)); +} + +FMT_END_NAMESPACE + +#endif // FMT_OSTREAM_H_ diff --git a/include/spdlog/fmt/bundled/printf.h b/include/spdlog/fmt/bundled/printf.h new file mode 100644 index 0000000..072cc6b --- /dev/null +++ b/include/spdlog/fmt/bundled/printf.h @@ -0,0 +1,656 @@ +// Formatting library for C++ - legacy printf implementation +// +// Copyright (c) 2012 - 2016, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_PRINTF_H_ +#define FMT_PRINTF_H_ + +#ifndef FMT_MODULE +# include // std::max +# include // std::numeric_limits +#endif + +#include "format.h" + +FMT_BEGIN_NAMESPACE +FMT_BEGIN_EXPORT + +template struct printf_formatter { + printf_formatter() = delete; +}; + +template class basic_printf_context { + private: + basic_appender out_; + basic_format_args args_; + + static_assert(std::is_same::value || + std::is_same::value, + "Unsupported code unit type."); + + public: + using char_type = Char; + using parse_context_type = basic_format_parse_context; + template using formatter_type = printf_formatter; + + /// Constructs a `printf_context` object. References to the arguments are + /// stored in the context object so make sure they have appropriate lifetimes. + basic_printf_context(basic_appender out, + basic_format_args args) + : out_(out), args_(args) {} + + auto out() -> basic_appender { return out_; } + void advance_to(basic_appender) {} + + auto locale() -> detail::locale_ref { return {}; } + + auto arg(int id) const -> basic_format_arg { + return args_.get(id); + } +}; + +namespace detail { + +// Checks if a value fits in int - used to avoid warnings about comparing +// signed and unsigned integers. +template struct int_checker { + template static auto fits_in_int(T value) -> bool { + unsigned max = to_unsigned(max_value()); + return value <= max; + } + static auto fits_in_int(bool) -> bool { return true; } +}; + +template <> struct int_checker { + template static auto fits_in_int(T value) -> bool { + return value >= (std::numeric_limits::min)() && + value <= max_value(); + } + static auto fits_in_int(int) -> bool { return true; } +}; + +struct printf_precision_handler { + template ::value)> + auto operator()(T value) -> int { + if (!int_checker::is_signed>::fits_in_int(value)) + report_error("number is too big"); + return (std::max)(static_cast(value), 0); + } + + template ::value)> + auto operator()(T) -> int { + report_error("precision is not integer"); + return 0; + } +}; + +// An argument visitor that returns true iff arg is a zero integer. +struct is_zero_int { + template ::value)> + auto operator()(T value) -> bool { + return value == 0; + } + + template ::value)> + auto operator()(T) -> bool { + return false; + } +}; + +template struct make_unsigned_or_bool : std::make_unsigned {}; + +template <> struct make_unsigned_or_bool { + using type = bool; +}; + +template class arg_converter { + private: + using char_type = typename Context::char_type; + + basic_format_arg& arg_; + char_type type_; + + public: + arg_converter(basic_format_arg& arg, char_type type) + : arg_(arg), type_(type) {} + + void operator()(bool value) { + if (type_ != 's') operator()(value); + } + + template ::value)> + void operator()(U value) { + bool is_signed = type_ == 'd' || type_ == 'i'; + using target_type = conditional_t::value, U, T>; + if (const_check(sizeof(target_type) <= sizeof(int))) { + // Extra casts are used to silence warnings. + if (is_signed) { + auto n = static_cast(static_cast(value)); + arg_ = detail::make_arg(n); + } else { + using unsigned_type = typename make_unsigned_or_bool::type; + auto n = static_cast(static_cast(value)); + arg_ = detail::make_arg(n); + } + } else { + if (is_signed) { + // glibc's printf doesn't sign extend arguments of smaller types: + // std::printf("%lld", -42); // prints "4294967254" + // but we don't have to do the same because it's a UB. + auto n = static_cast(value); + arg_ = detail::make_arg(n); + } else { + auto n = static_cast::type>(value); + arg_ = detail::make_arg(n); + } + } + } + + template ::value)> + void operator()(U) {} // No conversion needed for non-integral types. +}; + +// Converts an integer argument to T for printf, if T is an integral type. +// If T is void, the argument is converted to corresponding signed or unsigned +// type depending on the type specifier: 'd' and 'i' - signed, other - +// unsigned). +template +void convert_arg(basic_format_arg& arg, Char type) { + arg.visit(arg_converter(arg, type)); +} + +// Converts an integer argument to char for printf. +template class char_converter { + private: + basic_format_arg& arg_; + + public: + explicit char_converter(basic_format_arg& arg) : arg_(arg) {} + + template ::value)> + void operator()(T value) { + auto c = static_cast(value); + arg_ = detail::make_arg(c); + } + + template ::value)> + void operator()(T) {} // No conversion needed for non-integral types. +}; + +// An argument visitor that return a pointer to a C string if argument is a +// string or null otherwise. +template struct get_cstring { + template auto operator()(T) -> const Char* { return nullptr; } + auto operator()(const Char* s) -> const Char* { return s; } +}; + +// Checks if an argument is a valid printf width specifier and sets +// left alignment if it is negative. +class printf_width_handler { + private: + format_specs& specs_; + + public: + explicit printf_width_handler(format_specs& specs) : specs_(specs) {} + + template ::value)> + auto operator()(T value) -> unsigned { + auto width = static_cast>(value); + if (detail::is_negative(value)) { + specs_.align = align::left; + width = 0 - width; + } + unsigned int_max = to_unsigned(max_value()); + if (width > int_max) report_error("number is too big"); + return static_cast(width); + } + + template ::value)> + auto operator()(T) -> unsigned { + report_error("width is not integer"); + return 0; + } +}; + +// Workaround for a bug with the XL compiler when initializing +// printf_arg_formatter's base class. +template +auto make_arg_formatter(basic_appender iter, format_specs& s) + -> arg_formatter { + return {iter, s, locale_ref()}; +} + +// The `printf` argument formatter. +template +class printf_arg_formatter : public arg_formatter { + private: + using base = arg_formatter; + using context_type = basic_printf_context; + + context_type& context_; + + void write_null_pointer(bool is_string = false) { + auto s = this->specs; + s.type = presentation_type::none; + write_bytes(this->out, is_string ? "(null)" : "(nil)", s); + } + + public: + printf_arg_formatter(basic_appender iter, format_specs& s, + context_type& ctx) + : base(make_arg_formatter(iter, s)), context_(ctx) {} + + void operator()(monostate value) { base::operator()(value); } + + template ::value)> + void operator()(T value) { + // MSVC2013 fails to compile separate overloads for bool and Char so use + // std::is_same instead. + if (!std::is_same::value) { + base::operator()(value); + return; + } + format_specs s = this->specs; + if (s.type != presentation_type::none && s.type != presentation_type::chr) { + return (*this)(static_cast(value)); + } + s.sign = sign::none; + s.alt = false; + s.fill = ' '; // Ignore '0' flag for char types. + // align::numeric needs to be overwritten here since the '0' flag is + // ignored for non-numeric types + if (s.align == align::none || s.align == align::numeric) + s.align = align::right; + write(this->out, static_cast(value), s); + } + + template ::value)> + void operator()(T value) { + base::operator()(value); + } + + void operator()(const char* value) { + if (value) + base::operator()(value); + else + write_null_pointer(this->specs.type != presentation_type::pointer); + } + + void operator()(const wchar_t* value) { + if (value) + base::operator()(value); + else + write_null_pointer(this->specs.type != presentation_type::pointer); + } + + void operator()(basic_string_view value) { base::operator()(value); } + + void operator()(const void* value) { + if (value) + base::operator()(value); + else + write_null_pointer(); + } + + void operator()(typename basic_format_arg::handle handle) { + auto parse_ctx = basic_format_parse_context({}); + handle.format(parse_ctx, context_); + } +}; + +template +void parse_flags(format_specs& specs, const Char*& it, const Char* end) { + for (; it != end; ++it) { + switch (*it) { + case '-': + specs.align = align::left; + break; + case '+': + specs.sign = sign::plus; + break; + case '0': + specs.fill = '0'; + break; + case ' ': + if (specs.sign != sign::plus) specs.sign = sign::space; + break; + case '#': + specs.alt = true; + break; + default: + return; + } + } +} + +template +auto parse_header(const Char*& it, const Char* end, format_specs& specs, + GetArg get_arg) -> int { + int arg_index = -1; + Char c = *it; + if (c >= '0' && c <= '9') { + // Parse an argument index (if followed by '$') or a width possibly + // preceded with '0' flag(s). + int value = parse_nonnegative_int(it, end, -1); + if (it != end && *it == '$') { // value is an argument index + ++it; + arg_index = value != -1 ? value : max_value(); + } else { + if (c == '0') specs.fill = '0'; + if (value != 0) { + // Nonzero value means that we parsed width and don't need to + // parse it or flags again, so return now. + if (value == -1) report_error("number is too big"); + specs.width = value; + return arg_index; + } + } + } + parse_flags(specs, it, end); + // Parse width. + if (it != end) { + if (*it >= '0' && *it <= '9') { + specs.width = parse_nonnegative_int(it, end, -1); + if (specs.width == -1) report_error("number is too big"); + } else if (*it == '*') { + ++it; + specs.width = static_cast( + get_arg(-1).visit(detail::printf_width_handler(specs))); + } + } + return arg_index; +} + +inline auto parse_printf_presentation_type(char c, type t, bool& upper) + -> presentation_type { + using pt = presentation_type; + constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; + switch (c) { + case 'd': + return in(t, integral_set) ? pt::dec : pt::none; + case 'o': + return in(t, integral_set) ? pt::oct : pt::none; + case 'X': + upper = true; + FMT_FALLTHROUGH; + case 'x': + return in(t, integral_set) ? pt::hex : pt::none; + case 'E': + upper = true; + FMT_FALLTHROUGH; + case 'e': + return in(t, float_set) ? pt::exp : pt::none; + case 'F': + upper = true; + FMT_FALLTHROUGH; + case 'f': + return in(t, float_set) ? pt::fixed : pt::none; + case 'G': + upper = true; + FMT_FALLTHROUGH; + case 'g': + return in(t, float_set) ? pt::general : pt::none; + case 'A': + upper = true; + FMT_FALLTHROUGH; + case 'a': + return in(t, float_set) ? pt::hexfloat : pt::none; + case 'c': + return in(t, integral_set) ? pt::chr : pt::none; + case 's': + return in(t, string_set | cstring_set) ? pt::string : pt::none; + case 'p': + return in(t, pointer_set | cstring_set) ? pt::pointer : pt::none; + default: + return pt::none; + } +} + +template +void vprintf(buffer& buf, basic_string_view format, + basic_format_args args) { + using iterator = basic_appender; + auto out = iterator(buf); + auto context = basic_printf_context(out, args); + auto parse_ctx = basic_format_parse_context(format); + + // Returns the argument with specified index or, if arg_index is -1, the next + // argument. + auto get_arg = [&](int arg_index) { + if (arg_index < 0) + arg_index = parse_ctx.next_arg_id(); + else + parse_ctx.check_arg_id(--arg_index); + return detail::get_arg(context, arg_index); + }; + + const Char* start = parse_ctx.begin(); + const Char* end = parse_ctx.end(); + auto it = start; + while (it != end) { + if (!find(it, end, '%', it)) { + it = end; // find leaves it == nullptr if it doesn't find '%'. + break; + } + Char c = *it++; + if (it != end && *it == c) { + write(out, basic_string_view(start, to_unsigned(it - start))); + start = ++it; + continue; + } + write(out, basic_string_view(start, to_unsigned(it - 1 - start))); + + auto specs = format_specs(); + specs.align = align::right; + + // Parse argument index, flags and width. + int arg_index = parse_header(it, end, specs, get_arg); + if (arg_index == 0) report_error("argument not found"); + + // Parse precision. + if (it != end && *it == '.') { + ++it; + c = it != end ? *it : 0; + if ('0' <= c && c <= '9') { + specs.precision = parse_nonnegative_int(it, end, 0); + } else if (c == '*') { + ++it; + specs.precision = + static_cast(get_arg(-1).visit(printf_precision_handler())); + } else { + specs.precision = 0; + } + } + + auto arg = get_arg(arg_index); + // For d, i, o, u, x, and X conversion specifiers, if a precision is + // specified, the '0' flag is ignored + if (specs.precision >= 0 && arg.is_integral()) { + // Ignore '0' for non-numeric types or if '-' present. + specs.fill = ' '; + } + if (specs.precision >= 0 && arg.type() == type::cstring_type) { + auto str = arg.visit(get_cstring()); + auto str_end = str + specs.precision; + auto nul = std::find(str, str_end, Char()); + auto sv = basic_string_view( + str, to_unsigned(nul != str_end ? nul - str : specs.precision)); + arg = make_arg>(sv); + } + if (specs.alt && arg.visit(is_zero_int())) specs.alt = false; + if (specs.fill.template get() == '0') { + if (arg.is_arithmetic() && specs.align != align::left) + specs.align = align::numeric; + else + specs.fill = ' '; // Ignore '0' flag for non-numeric types or if '-' + // flag is also present. + } + + // Parse length and convert the argument to the required type. + c = it != end ? *it++ : 0; + Char t = it != end ? *it : 0; + switch (c) { + case 'h': + if (t == 'h') { + ++it; + t = it != end ? *it : 0; + convert_arg(arg, t); + } else { + convert_arg(arg, t); + } + break; + case 'l': + if (t == 'l') { + ++it; + t = it != end ? *it : 0; + convert_arg(arg, t); + } else { + convert_arg(arg, t); + } + break; + case 'j': + convert_arg(arg, t); + break; + case 'z': + convert_arg(arg, t); + break; + case 't': + convert_arg(arg, t); + break; + case 'L': + // printf produces garbage when 'L' is omitted for long double, no + // need to do the same. + break; + default: + --it; + convert_arg(arg, c); + } + + // Parse type. + if (it == end) report_error("invalid format string"); + char type = static_cast(*it++); + if (arg.is_integral()) { + // Normalize type. + switch (type) { + case 'i': + case 'u': + type = 'd'; + break; + case 'c': + arg.visit(char_converter>(arg)); + break; + } + } + bool upper = false; + specs.type = parse_printf_presentation_type(type, arg.type(), upper); + if (specs.type == presentation_type::none) + report_error("invalid format specifier"); + specs.upper = upper; + + start = it; + + // Format argument. + arg.visit(printf_arg_formatter(out, specs, context)); + } + write(out, basic_string_view(start, to_unsigned(it - start))); +} +} // namespace detail + +using printf_context = basic_printf_context; +using wprintf_context = basic_printf_context; + +using printf_args = basic_format_args; +using wprintf_args = basic_format_args; + +/// Constructs an `format_arg_store` object that contains references to +/// arguments and can be implicitly converted to `printf_args`. +template +inline auto make_printf_args(T&... args) + -> decltype(fmt::make_format_args>(args...)) { + return fmt::make_format_args>(args...); +} + +template struct vprintf_args { + using type = basic_format_args>; +}; + +template +inline auto vsprintf(basic_string_view fmt, + typename vprintf_args::type args) + -> std::basic_string { + auto buf = basic_memory_buffer(); + detail::vprintf(buf, fmt, args); + return to_string(buf); +} + +/** + * Formats `args` according to specifications in `fmt` and returns the result + * as as string. + * + * **Example**: + * + * std::string message = fmt::sprintf("The answer is %d", 42); + */ +template > +inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string { + return vsprintf(detail::to_string_view(fmt), + fmt::make_format_args>(args...)); +} + +template +inline auto vfprintf(std::FILE* f, basic_string_view fmt, + typename vprintf_args::type args) -> int { + auto buf = basic_memory_buffer(); + detail::vprintf(buf, fmt, args); + size_t size = buf.size(); + return std::fwrite(buf.data(), sizeof(Char), size, f) < size + ? -1 + : static_cast(size); +} + +/** + * Formats `args` according to specifications in `fmt` and writes the output + * to `f`. + * + * **Example**: + * + * fmt::fprintf(stderr, "Don't %s!", "panic"); + */ +template > +inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int { + return vfprintf(f, detail::to_string_view(fmt), + make_printf_args(args...)); +} + +template +FMT_DEPRECATED inline auto vprintf(basic_string_view fmt, + typename vprintf_args::type args) + -> int { + return vfprintf(stdout, fmt, args); +} + +/** + * Formats `args` according to specifications in `fmt` and writes the output + * to `stdout`. + * + * **Example**: + * + * fmt::printf("Elapsed time: %.2f seconds", 1.23); + */ +template +inline auto printf(string_view fmt, const T&... args) -> int { + return vfprintf(stdout, fmt, make_printf_args(args...)); +} +template +FMT_DEPRECATED inline auto printf(basic_string_view fmt, + const T&... args) -> int { + return vfprintf(stdout, fmt, make_printf_args(args...)); +} + +FMT_END_EXPORT +FMT_END_NAMESPACE + +#endif // FMT_PRINTF_H_ diff --git a/include/spdlog/fmt/bundled/ranges.h b/include/spdlog/fmt/bundled/ranges.h new file mode 100644 index 0000000..0d3dfbd --- /dev/null +++ b/include/spdlog/fmt/bundled/ranges.h @@ -0,0 +1,882 @@ +// Formatting library for C++ - range and tuple support +// +// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_RANGES_H_ +#define FMT_RANGES_H_ + +#ifndef FMT_MODULE +# include +# include +# include +# include +# include +# include +#endif + +#include "format.h" + +FMT_BEGIN_NAMESPACE + +FMT_EXPORT +enum class range_format { disabled, map, set, sequence, string, debug_string }; + +namespace detail { + +template class is_map { + template static auto check(U*) -> typename U::mapped_type; + template static void check(...); + + public: + static constexpr const bool value = + !std::is_void(nullptr))>::value; +}; + +template class is_set { + template static auto check(U*) -> typename U::key_type; + template static void check(...); + + public: + static constexpr const bool value = + !std::is_void(nullptr))>::value && !is_map::value; +}; + +template struct conditional_helper {}; + +template struct is_range_ : std::false_type {}; + +#if !FMT_MSC_VERSION || FMT_MSC_VERSION > 1800 + +# define FMT_DECLTYPE_RETURN(val) \ + ->decltype(val) { return val; } \ + static_assert( \ + true, "") // This makes it so that a semicolon is required after the + // macro, which helps clang-format handle the formatting. + +// C array overload +template +auto range_begin(const T (&arr)[N]) -> const T* { + return arr; +} +template +auto range_end(const T (&arr)[N]) -> const T* { + return arr + N; +} + +template +struct has_member_fn_begin_end_t : std::false_type {}; + +template +struct has_member_fn_begin_end_t().begin()), + decltype(std::declval().end())>> + : std::true_type {}; + +// Member function overloads. +template +auto range_begin(T&& rng) FMT_DECLTYPE_RETURN(static_cast(rng).begin()); +template +auto range_end(T&& rng) FMT_DECLTYPE_RETURN(static_cast(rng).end()); + +// ADL overloads. Only participate in overload resolution if member functions +// are not found. +template +auto range_begin(T&& rng) + -> enable_if_t::value, + decltype(begin(static_cast(rng)))> { + return begin(static_cast(rng)); +} +template +auto range_end(T&& rng) -> enable_if_t::value, + decltype(end(static_cast(rng)))> { + return end(static_cast(rng)); +} + +template +struct has_const_begin_end : std::false_type {}; +template +struct has_mutable_begin_end : std::false_type {}; + +template +struct has_const_begin_end< + T, void_t&>())), + decltype(detail::range_end( + std::declval&>()))>> + : std::true_type {}; + +template +struct has_mutable_begin_end< + T, void_t())), + decltype(detail::range_end(std::declval())), + // the extra int here is because older versions of MSVC don't + // SFINAE properly unless there are distinct types + int>> : std::true_type {}; + +template +struct is_range_ + : std::integral_constant::value || + has_mutable_begin_end::value)> {}; +# undef FMT_DECLTYPE_RETURN +#endif + +// tuple_size and tuple_element check. +template class is_tuple_like_ { + template + static auto check(U* p) -> decltype(std::tuple_size::value, int()); + template static void check(...); + + public: + static constexpr const bool value = + !std::is_void(nullptr))>::value; +}; + +// Check for integer_sequence +#if defined(__cpp_lib_integer_sequence) || FMT_MSC_VERSION >= 1900 +template +using integer_sequence = std::integer_sequence; +template using index_sequence = std::index_sequence; +template using make_index_sequence = std::make_index_sequence; +#else +template struct integer_sequence { + using value_type = T; + + static FMT_CONSTEXPR auto size() -> size_t { return sizeof...(N); } +}; + +template using index_sequence = integer_sequence; + +template +struct make_integer_sequence : make_integer_sequence {}; +template +struct make_integer_sequence : integer_sequence {}; + +template +using make_index_sequence = make_integer_sequence; +#endif + +template +using tuple_index_sequence = make_index_sequence::value>; + +template ::value> +class is_tuple_formattable_ { + public: + static constexpr const bool value = false; +}; +template class is_tuple_formattable_ { + template + static auto all_true(index_sequence, + integer_sequence= 0)...>) -> std::true_type; + static auto all_true(...) -> std::false_type; + + template + static auto check(index_sequence) -> decltype(all_true( + index_sequence{}, + integer_sequence::type, + C>::value)...>{})); + + public: + static constexpr const bool value = + decltype(check(tuple_index_sequence{}))::value; +}; + +template +FMT_CONSTEXPR void for_each(index_sequence, Tuple&& t, F&& f) { + using std::get; + // Using a free function get(Tuple) now. + const int unused[] = {0, ((void)f(get(t)), 0)...}; + ignore_unused(unused); +} + +template +FMT_CONSTEXPR void for_each(Tuple&& t, F&& f) { + for_each(tuple_index_sequence>(), + std::forward(t), std::forward(f)); +} + +template +void for_each2(index_sequence, Tuple1&& t1, Tuple2&& t2, F&& f) { + using std::get; + const int unused[] = {0, ((void)f(get(t1), get(t2)), 0)...}; + ignore_unused(unused); +} + +template +void for_each2(Tuple1&& t1, Tuple2&& t2, F&& f) { + for_each2(tuple_index_sequence>(), + std::forward(t1), std::forward(t2), + std::forward(f)); +} + +namespace tuple { +// Workaround a bug in MSVC 2019 (v140). +template +using result_t = std::tuple, Char>...>; + +using std::get; +template +auto get_formatters(index_sequence) + -> result_t(std::declval()))...>; +} // namespace tuple + +#if FMT_MSC_VERSION && FMT_MSC_VERSION < 1920 +// Older MSVC doesn't get the reference type correctly for arrays. +template struct range_reference_type_impl { + using type = decltype(*detail::range_begin(std::declval())); +}; + +template struct range_reference_type_impl { + using type = T&; +}; + +template +using range_reference_type = typename range_reference_type_impl::type; +#else +template +using range_reference_type = + decltype(*detail::range_begin(std::declval())); +#endif + +// We don't use the Range's value_type for anything, but we do need the Range's +// reference type, with cv-ref stripped. +template +using uncvref_type = remove_cvref_t>; + +template +FMT_CONSTEXPR auto maybe_set_debug_format(Formatter& f, bool set) + -> decltype(f.set_debug_format(set)) { + f.set_debug_format(set); +} +template +FMT_CONSTEXPR void maybe_set_debug_format(Formatter&, ...) {} + +template +struct range_format_kind_ + : std::integral_constant, T>::value + ? range_format::disabled + : is_map::value ? range_format::map + : is_set::value ? range_format::set + : range_format::sequence> {}; + +template +using range_format_constant = std::integral_constant; + +// These are not generic lambdas for compatibility with C++11. +template struct parse_empty_specs { + template FMT_CONSTEXPR void operator()(Formatter& f) { + f.parse(ctx); + detail::maybe_set_debug_format(f, true); + } + ParseContext& ctx; +}; +template struct format_tuple_element { + using char_type = typename FormatContext::char_type; + + template + void operator()(const formatter& f, const T& v) { + if (i > 0) ctx.advance_to(detail::copy(separator, ctx.out())); + ctx.advance_to(f.format(v, ctx)); + ++i; + } + + int i; + FormatContext& ctx; + basic_string_view separator; +}; + +} // namespace detail + +template struct is_tuple_like { + static constexpr const bool value = + detail::is_tuple_like_::value && !detail::is_range_::value; +}; + +template struct is_tuple_formattable { + static constexpr const bool value = + detail::is_tuple_formattable_::value; +}; + +template +struct formatter::value && + fmt::is_tuple_formattable::value>> { + private: + decltype(detail::tuple::get_formatters( + detail::tuple_index_sequence())) formatters_; + + basic_string_view separator_ = detail::string_literal{}; + basic_string_view opening_bracket_ = + detail::string_literal{}; + basic_string_view closing_bracket_ = + detail::string_literal{}; + + public: + FMT_CONSTEXPR formatter() {} + + FMT_CONSTEXPR void set_separator(basic_string_view sep) { + separator_ = sep; + } + + FMT_CONSTEXPR void set_brackets(basic_string_view open, + basic_string_view close) { + opening_bracket_ = open; + closing_bracket_ = close; + } + + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + auto it = ctx.begin(); + if (it != ctx.end() && *it != '}') report_error("invalid format specifier"); + detail::for_each(formatters_, detail::parse_empty_specs{ctx}); + return it; + } + + template + auto format(const Tuple& value, FormatContext& ctx) const + -> decltype(ctx.out()) { + ctx.advance_to(detail::copy(opening_bracket_, ctx.out())); + detail::for_each2( + formatters_, value, + detail::format_tuple_element{0, ctx, separator_}); + return detail::copy(closing_bracket_, ctx.out()); + } +}; + +template struct is_range { + static constexpr const bool value = + detail::is_range_::value && !detail::has_to_string_view::value; +}; + +namespace detail { +template struct range_mapper { + using mapper = arg_mapper; + + template , Context>::value)> + static auto map(T&& value) -> T&& { + return static_cast(value); + } + template , Context>::value)> + static auto map(T&& value) + -> decltype(mapper().map(static_cast(value))) { + return mapper().map(static_cast(value)); + } +}; + +template +using range_formatter_type = + formatter>{} + .map(std::declval()))>, + Char>; + +template +using maybe_const_range = + conditional_t::value, const R, R>; + +// Workaround a bug in MSVC 2015 and earlier. +#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910 +template +struct is_formattable_delayed + : is_formattable>, Char> {}; +#endif +} // namespace detail + +template struct conjunction : std::true_type {}; +template struct conjunction

: P {}; +template +struct conjunction + : conditional_t, P1> {}; + +template +struct range_formatter; + +template +struct range_formatter< + T, Char, + enable_if_t>, + is_formattable>::value>> { + private: + detail::range_formatter_type underlying_; + basic_string_view separator_ = detail::string_literal{}; + basic_string_view opening_bracket_ = + detail::string_literal{}; + basic_string_view closing_bracket_ = + detail::string_literal{}; + bool is_debug = false; + + template ::value)> + auto write_debug_string(Output& out, It it, Sentinel end) const -> Output { + auto buf = basic_memory_buffer(); + for (; it != end; ++it) buf.push_back(*it); + auto specs = format_specs(); + specs.type = presentation_type::debug; + return detail::write( + out, basic_string_view(buf.data(), buf.size()), specs); + } + + template ::value)> + auto write_debug_string(Output& out, It, Sentinel) const -> Output { + return out; + } + + public: + FMT_CONSTEXPR range_formatter() {} + + FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type& { + return underlying_; + } + + FMT_CONSTEXPR void set_separator(basic_string_view sep) { + separator_ = sep; + } + + FMT_CONSTEXPR void set_brackets(basic_string_view open, + basic_string_view close) { + opening_bracket_ = open; + closing_bracket_ = close; + } + + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + auto it = ctx.begin(); + auto end = ctx.end(); + detail::maybe_set_debug_format(underlying_, true); + if (it == end) return underlying_.parse(ctx); + + switch (detail::to_ascii(*it)) { + case 'n': + set_brackets({}, {}); + ++it; + break; + case '?': + is_debug = true; + set_brackets({}, {}); + ++it; + if (it == end || *it != 's') report_error("invalid format specifier"); + FMT_FALLTHROUGH; + case 's': + if (!std::is_same::value) + report_error("invalid format specifier"); + if (!is_debug) { + set_brackets(detail::string_literal{}, + detail::string_literal{}); + set_separator({}); + detail::maybe_set_debug_format(underlying_, false); + } + ++it; + return it; + } + + if (it != end && *it != '}') { + if (*it != ':') report_error("invalid format specifier"); + detail::maybe_set_debug_format(underlying_, false); + ++it; + } + + ctx.advance_to(it); + return underlying_.parse(ctx); + } + + template + auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) { + auto mapper = detail::range_mapper>(); + auto out = ctx.out(); + auto it = detail::range_begin(range); + auto end = detail::range_end(range); + if (is_debug) return write_debug_string(out, std::move(it), end); + + out = detail::copy(opening_bracket_, out); + int i = 0; + for (; it != end; ++it) { + if (i > 0) out = detail::copy(separator_, out); + ctx.advance_to(out); + auto&& item = *it; // Need an lvalue + out = underlying_.format(mapper.map(item), ctx); + ++i; + } + out = detail::copy(closing_bracket_, out); + return out; + } +}; + +FMT_EXPORT +template +struct range_format_kind + : conditional_t< + is_range::value, detail::range_format_kind_, + std::integral_constant> {}; + +template +struct formatter< + R, Char, + enable_if_t::value != range_format::disabled && + range_format_kind::value != range_format::map && + range_format_kind::value != range_format::string && + range_format_kind::value != range_format::debug_string> +// Workaround a bug in MSVC 2015 and earlier. +#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910 + , + detail::is_formattable_delayed +#endif + >::value>> { + private: + using range_type = detail::maybe_const_range; + range_formatter, Char> range_formatter_; + + public: + using nonlocking = void; + + FMT_CONSTEXPR formatter() { + if (detail::const_check(range_format_kind::value != + range_format::set)) + return; + range_formatter_.set_brackets(detail::string_literal{}, + detail::string_literal{}); + } + + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return range_formatter_.parse(ctx); + } + + template + auto format(range_type& range, FormatContext& ctx) const + -> decltype(ctx.out()) { + return range_formatter_.format(range, ctx); + } +}; + +// A map formatter. +template +struct formatter< + R, Char, + enable_if_t::value == range_format::map>> { + private: + using map_type = detail::maybe_const_range; + using element_type = detail::uncvref_type; + + decltype(detail::tuple::get_formatters( + detail::tuple_index_sequence())) formatters_; + bool no_delimiters_ = false; + + public: + FMT_CONSTEXPR formatter() {} + + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + auto it = ctx.begin(); + auto end = ctx.end(); + if (it != end) { + if (detail::to_ascii(*it) == 'n') { + no_delimiters_ = true; + ++it; + } + if (it != end && *it != '}') { + if (*it != ':') report_error("invalid format specifier"); + ++it; + } + ctx.advance_to(it); + } + detail::for_each(formatters_, detail::parse_empty_specs{ctx}); + return it; + } + + template + auto format(map_type& map, FormatContext& ctx) const -> decltype(ctx.out()) { + auto out = ctx.out(); + basic_string_view open = detail::string_literal{}; + if (!no_delimiters_) out = detail::copy(open, out); + int i = 0; + auto mapper = detail::range_mapper>(); + basic_string_view sep = detail::string_literal{}; + for (auto&& value : map) { + if (i > 0) out = detail::copy(sep, out); + ctx.advance_to(out); + detail::for_each2(formatters_, mapper.map(value), + detail::format_tuple_element{ + 0, ctx, detail::string_literal{}}); + ++i; + } + basic_string_view close = detail::string_literal{}; + if (!no_delimiters_) out = detail::copy(close, out); + return out; + } +}; + +// A (debug_)string formatter. +template +struct formatter< + R, Char, + enable_if_t::value == range_format::string || + range_format_kind::value == + range_format::debug_string>> { + private: + using range_type = detail::maybe_const_range; + using string_type = + conditional_t, + decltype(detail::range_begin(std::declval())), + decltype(detail::range_end(std::declval()))>::value, + detail::std_string_view, std::basic_string>; + + formatter underlying_; + + public: + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return underlying_.parse(ctx); + } + + template + auto format(range_type& range, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + if (detail::const_check(range_format_kind::value == + range_format::debug_string)) + *out++ = '"'; + out = underlying_.format( + string_type{detail::range_begin(range), detail::range_end(range)}, ctx); + if (detail::const_check(range_format_kind::value == + range_format::debug_string)) + *out++ = '"'; + return out; + } +}; + +template +struct join_view : detail::view { + It begin; + Sentinel end; + basic_string_view sep; + + join_view(It b, Sentinel e, basic_string_view s) + : begin(std::move(b)), end(e), sep(s) {} +}; + +template +struct formatter, Char> { + private: + using value_type = +#ifdef __cpp_lib_ranges + std::iter_value_t; +#else + typename std::iterator_traits::value_type; +#endif + formatter, Char> value_formatter_; + + using view_ref = conditional_t::value, + const join_view&, + join_view&&>; + + public: + using nonlocking = void; + + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* { + return value_formatter_.parse(ctx); + } + + template + auto format(view_ref& value, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto it = std::forward(value).begin; + auto out = ctx.out(); + if (it == value.end) return out; + out = value_formatter_.format(*it, ctx); + ++it; + while (it != value.end) { + out = detail::copy(value.sep.begin(), value.sep.end(), out); + ctx.advance_to(out); + out = value_formatter_.format(*it, ctx); + ++it; + } + return out; + } +}; + +/// Returns a view that formats the iterator range `[begin, end)` with elements +/// separated by `sep`. +template +auto join(It begin, Sentinel end, string_view sep) -> join_view { + return {std::move(begin), end, sep}; +} + +/** + * Returns a view that formats `range` with elements separated by `sep`. + * + * **Example**: + * + * auto v = std::vector{1, 2, 3}; + * fmt::print("{}", fmt::join(v, ", ")); + * // Output: 1, 2, 3 + * + * `fmt::join` applies passed format specifiers to the range elements: + * + * fmt::print("{:02}", fmt::join(v, ", ")); + * // Output: 01, 02, 03 + */ +template +auto join(Range&& r, string_view sep) + -> join_view { + return {detail::range_begin(r), detail::range_end(r), sep}; +} + +template struct tuple_join_view : detail::view { + const std::tuple& tuple; + basic_string_view sep; + + tuple_join_view(const std::tuple& t, basic_string_view s) + : tuple(t), sep{s} {} +}; + +// Define FMT_TUPLE_JOIN_SPECIFIERS to enable experimental format specifiers +// support in tuple_join. It is disabled by default because of issues with +// the dynamic width and precision. +#ifndef FMT_TUPLE_JOIN_SPECIFIERS +# define FMT_TUPLE_JOIN_SPECIFIERS 0 +#endif + +template +struct formatter, Char> { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return do_parse(ctx, std::integral_constant()); + } + + template + auto format(const tuple_join_view& value, + FormatContext& ctx) const -> typename FormatContext::iterator { + return do_format(value, ctx, + std::integral_constant()); + } + + private: + std::tuple::type, Char>...> formatters_; + + template + FMT_CONSTEXPR auto do_parse(ParseContext& ctx, + std::integral_constant) + -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template + FMT_CONSTEXPR auto do_parse(ParseContext& ctx, + std::integral_constant) + -> decltype(ctx.begin()) { + auto end = ctx.begin(); +#if FMT_TUPLE_JOIN_SPECIFIERS + end = std::get(formatters_).parse(ctx); + if (N > 1) { + auto end1 = do_parse(ctx, std::integral_constant()); + if (end != end1) + report_error("incompatible format specs for tuple elements"); + } +#endif + return end; + } + + template + auto do_format(const tuple_join_view&, FormatContext& ctx, + std::integral_constant) const -> + typename FormatContext::iterator { + return ctx.out(); + } + + template + auto do_format(const tuple_join_view& value, FormatContext& ctx, + std::integral_constant) const -> + typename FormatContext::iterator { + auto out = std::get(formatters_) + .format(std::get(value.tuple), ctx); + if (N <= 1) return out; + out = detail::copy(value.sep, out); + ctx.advance_to(out); + return do_format(value, ctx, std::integral_constant()); + } +}; + +namespace detail { +// Check if T has an interface like a container adaptor (e.g. std::stack, +// std::queue, std::priority_queue). +template class is_container_adaptor_like { + template static auto check(U* p) -> typename U::container_type; + template static void check(...); + + public: + static constexpr const bool value = + !std::is_void(nullptr))>::value; +}; + +template struct all { + const Container& c; + auto begin() const -> typename Container::const_iterator { return c.begin(); } + auto end() const -> typename Container::const_iterator { return c.end(); } +}; +} // namespace detail + +template +struct formatter< + T, Char, + enable_if_t, + bool_constant::value == + range_format::disabled>>::value>> + : formatter, Char> { + using all = detail::all; + template + auto format(const T& t, FormatContext& ctx) const -> decltype(ctx.out()) { + struct getter : T { + static auto get(const T& t) -> all { + return {t.*(&getter::c)}; // Access c through the derived class. + } + }; + return formatter::format(getter::get(t), ctx); + } +}; + +FMT_BEGIN_EXPORT + +/** + * Returns an object that formats `std::tuple` with elements separated by `sep`. + * + * **Example**: + * + * auto t = std::tuple{1, 'a'}; + * fmt::print("{}", fmt::join(t, ", ")); + * // Output: 1, a + */ +template +FMT_CONSTEXPR auto join(const std::tuple& tuple, string_view sep) + -> tuple_join_view { + return {tuple, sep}; +} + +/** + * Returns an object that formats `std::initializer_list` with elements + * separated by `sep`. + * + * **Example**: + * + * fmt::print("{}", fmt::join({1, 2, 3}, ", ")); + * // Output: "1, 2, 3" + */ +template +auto join(std::initializer_list list, string_view sep) + -> join_view { + return join(std::begin(list), std::end(list), sep); +} + +FMT_END_EXPORT +FMT_END_NAMESPACE + +#endif // FMT_RANGES_H_ diff --git a/include/spdlog/fmt/bundled/std.h b/include/spdlog/fmt/bundled/std.h new file mode 100644 index 0000000..fb43940 --- /dev/null +++ b/include/spdlog/fmt/bundled/std.h @@ -0,0 +1,699 @@ +// Formatting library for C++ - formatters for standard library types +// +// Copyright (c) 2012 - present, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_STD_H_ +#define FMT_STD_H_ + +#include "format.h" +#include "ostream.h" + +#ifndef FMT_MODULE +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include +# include + +// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC. +# if FMT_CPLUSPLUS >= 201703L +# if FMT_HAS_INCLUDE() +# include +# endif +# if FMT_HAS_INCLUDE() +# include +# endif +# if FMT_HAS_INCLUDE() +# include +# endif +# endif +// Use > instead of >= in the version check because may be +// available after C++17 but before C++20 is marked as implemented. +# if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE() +# include +# endif +# if FMT_CPLUSPLUS > 202002L && FMT_HAS_INCLUDE() +# include +# endif +#endif // FMT_MODULE + +#if FMT_HAS_INCLUDE() +# include +#endif + +// GCC 4 does not support FMT_HAS_INCLUDE. +#if FMT_HAS_INCLUDE() || defined(__GLIBCXX__) +# include +// Android NDK with gabi++ library on some architectures does not implement +// abi::__cxa_demangle(). +# ifndef __GABIXX_CXXABI_H__ +# define FMT_HAS_ABI_CXA_DEMANGLE +# endif +#endif + +// For older Xcode versions, __cpp_lib_xxx flags are inaccurately defined. +#ifndef FMT_CPP_LIB_FILESYSTEM +# ifdef __cpp_lib_filesystem +# define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem +# else +# define FMT_CPP_LIB_FILESYSTEM 0 +# endif +#endif + +#ifndef FMT_CPP_LIB_VARIANT +# ifdef __cpp_lib_variant +# define FMT_CPP_LIB_VARIANT __cpp_lib_variant +# else +# define FMT_CPP_LIB_VARIANT 0 +# endif +#endif + +#if FMT_CPP_LIB_FILESYSTEM +FMT_BEGIN_NAMESPACE + +namespace detail { + +template +auto get_path_string(const std::filesystem::path& p, + const std::basic_string& native) { + if constexpr (std::is_same_v && std::is_same_v) + return to_utf8(native, to_utf8_error_policy::replace); + else + return p.string(); +} + +template +void write_escaped_path(basic_memory_buffer& quoted, + const std::filesystem::path& p, + const std::basic_string& native) { + if constexpr (std::is_same_v && + std::is_same_v) { + auto buf = basic_memory_buffer(); + write_escaped_string(std::back_inserter(buf), native); + bool valid = to_utf8::convert(quoted, {buf.data(), buf.size()}); + FMT_ASSERT(valid, "invalid utf16"); + } else if constexpr (std::is_same_v) { + write_escaped_string( + std::back_inserter(quoted), native); + } else { + write_escaped_string(std::back_inserter(quoted), p.string()); + } +} + +} // namespace detail + +FMT_EXPORT +template struct formatter { + private: + format_specs specs_; + detail::arg_ref width_ref_; + bool debug_ = false; + char path_type_ = 0; + + public: + FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } + + template FMT_CONSTEXPR auto parse(ParseContext& ctx) { + auto it = ctx.begin(), end = ctx.end(); + if (it == end) return it; + + it = detail::parse_align(it, end, specs_); + if (it == end) return it; + + it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx); + if (it != end && *it == '?') { + debug_ = true; + ++it; + } + if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++); + return it; + } + + template + auto format(const std::filesystem::path& p, FormatContext& ctx) const { + auto specs = specs_; + auto path_string = + !path_type_ ? p.native() + : p.generic_string(); + + detail::handle_dynamic_spec(specs.width, width_ref_, + ctx); + if (!debug_) { + auto s = detail::get_path_string(p, path_string); + return detail::write(ctx.out(), basic_string_view(s), specs); + } + auto quoted = basic_memory_buffer(); + detail::write_escaped_path(quoted, p, path_string); + return detail::write(ctx.out(), + basic_string_view(quoted.data(), quoted.size()), + specs); + } +}; + +class path : public std::filesystem::path { + public: + auto display_string() const -> std::string { + const std::filesystem::path& base = *this; + return fmt::format(FMT_STRING("{}"), base); + } + auto system_string() const -> std::string { return string(); } + + auto generic_display_string() const -> std::string { + const std::filesystem::path& base = *this; + return fmt::format(FMT_STRING("{:g}"), base); + } + auto generic_system_string() const -> std::string { return generic_string(); } +}; + +FMT_END_NAMESPACE +#endif // FMT_CPP_LIB_FILESYSTEM + +FMT_BEGIN_NAMESPACE +FMT_EXPORT +template +struct formatter, Char> : nested_formatter { + private: + // Functor because C++11 doesn't support generic lambdas. + struct writer { + const std::bitset& bs; + + template + FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt { + for (auto pos = N; pos > 0; --pos) { + out = detail::write(out, bs[pos - 1] ? Char('1') : Char('0')); + } + + return out; + } + }; + + public: + template + auto format(const std::bitset& bs, FormatContext& ctx) const + -> decltype(ctx.out()) { + return write_padded(ctx, writer{bs}); + } +}; + +FMT_EXPORT +template +struct formatter : basic_ostream_formatter {}; +FMT_END_NAMESPACE + +#ifdef __cpp_lib_optional +FMT_BEGIN_NAMESPACE +FMT_EXPORT +template +struct formatter, Char, + std::enable_if_t::value>> { + private: + formatter underlying_; + static constexpr basic_string_view optional = + detail::string_literal{}; + static constexpr basic_string_view none = + detail::string_literal{}; + + template + FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set) + -> decltype(u.set_debug_format(set)) { + u.set_debug_format(set); + } + + template + FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {} + + public: + template FMT_CONSTEXPR auto parse(ParseContext& ctx) { + maybe_set_debug_format(underlying_, true); + return underlying_.parse(ctx); + } + + template + auto format(const std::optional& opt, FormatContext& ctx) const + -> decltype(ctx.out()) { + if (!opt) return detail::write(ctx.out(), none); + + auto out = ctx.out(); + out = detail::write(out, optional); + ctx.advance_to(out); + out = underlying_.format(*opt, ctx); + return detail::write(out, ')'); + } +}; +FMT_END_NAMESPACE +#endif // __cpp_lib_optional + +#if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT + +FMT_BEGIN_NAMESPACE +namespace detail { + +template +auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt { + if constexpr (has_to_string_view::value) + return write_escaped_string(out, detail::to_string_view(v)); + if constexpr (std::is_same_v) return write_escaped_char(out, v); + return write(out, v); +} + +} // namespace detail + +FMT_END_NAMESPACE +#endif + +#ifdef __cpp_lib_expected +FMT_BEGIN_NAMESPACE + +FMT_EXPORT +template +struct formatter, Char, + std::enable_if_t::value && + is_formattable::value>> { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template + auto format(const std::expected& value, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + + if (value.has_value()) { + out = detail::write(out, "expected("); + out = detail::write_escaped_alternative(out, *value); + } else { + out = detail::write(out, "unexpected("); + out = detail::write_escaped_alternative(out, value.error()); + } + *out++ = ')'; + return out; + } +}; +FMT_END_NAMESPACE +#endif // __cpp_lib_expected + +#ifdef __cpp_lib_source_location +FMT_BEGIN_NAMESPACE +FMT_EXPORT +template <> struct formatter { + template FMT_CONSTEXPR auto parse(ParseContext& ctx) { + return ctx.begin(); + } + + template + auto format(const std::source_location& loc, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + out = detail::write(out, loc.file_name()); + out = detail::write(out, ':'); + out = detail::write(out, loc.line()); + out = detail::write(out, ':'); + out = detail::write(out, loc.column()); + out = detail::write(out, ": "); + out = detail::write(out, loc.function_name()); + return out; + } +}; +FMT_END_NAMESPACE +#endif + +#if FMT_CPP_LIB_VARIANT +FMT_BEGIN_NAMESPACE +namespace detail { + +template +using variant_index_sequence = + std::make_index_sequence::value>; + +template struct is_variant_like_ : std::false_type {}; +template +struct is_variant_like_> : std::true_type {}; + +// formattable element check. +template class is_variant_formattable_ { + template + static std::conjunction< + is_formattable, C>...> + check(std::index_sequence); + + public: + static constexpr const bool value = + decltype(check(variant_index_sequence{}))::value; +}; + +} // namespace detail + +template struct is_variant_like { + static constexpr const bool value = detail::is_variant_like_::value; +}; + +template struct is_variant_formattable { + static constexpr const bool value = + detail::is_variant_formattable_::value; +}; + +FMT_EXPORT +template struct formatter { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template + auto format(const std::monostate&, FormatContext& ctx) const + -> decltype(ctx.out()) { + return detail::write(ctx.out(), "monostate"); + } +}; + +FMT_EXPORT +template +struct formatter< + Variant, Char, + std::enable_if_t, is_variant_formattable>>> { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template + auto format(const Variant& value, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + + out = detail::write(out, "variant("); + FMT_TRY { + std::visit( + [&](const auto& v) { + out = detail::write_escaped_alternative(out, v); + }, + value); + } + FMT_CATCH(const std::bad_variant_access&) { + detail::write(out, "valueless by exception"); + } + *out++ = ')'; + return out; + } +}; +FMT_END_NAMESPACE +#endif // FMT_CPP_LIB_VARIANT + +FMT_BEGIN_NAMESPACE +FMT_EXPORT +template struct formatter { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template + FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); + out = detail::write_bytes(out, ec.category().name(), format_specs()); + out = detail::write(out, Char(':')); + out = detail::write(out, ec.value()); + return out; + } +}; + +#if FMT_USE_RTTI +namespace detail { + +template +auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt { +# ifdef FMT_HAS_ABI_CXA_DEMANGLE + int status = 0; + std::size_t size = 0; + std::unique_ptr demangled_name_ptr( + abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free); + + string_view demangled_name_view; + if (demangled_name_ptr) { + demangled_name_view = demangled_name_ptr.get(); + + // Normalization of stdlib inline namespace names. + // libc++ inline namespaces. + // std::__1::* -> std::* + // std::__1::__fs::* -> std::* + // libstdc++ inline namespaces. + // std::__cxx11::* -> std::* + // std::filesystem::__cxx11::* -> std::filesystem::* + if (demangled_name_view.starts_with("std::")) { + char* begin = demangled_name_ptr.get(); + char* to = begin + 5; // std:: + for (char *from = to, *end = begin + demangled_name_view.size(); + from < end;) { + // This is safe, because demangled_name is NUL-terminated. + if (from[0] == '_' && from[1] == '_') { + char* next = from + 1; + while (next < end && *next != ':') next++; + if (next[0] == ':' && next[1] == ':') { + from = next + 2; + continue; + } + } + *to++ = *from++; + } + demangled_name_view = {begin, detail::to_unsigned(to - begin)}; + } + } else { + demangled_name_view = string_view(ti.name()); + } + return detail::write_bytes(out, demangled_name_view); +# elif FMT_MSC_VERSION + const string_view demangled_name(ti.name()); + for (std::size_t i = 0; i < demangled_name.size(); ++i) { + auto sub = demangled_name; + sub.remove_prefix(i); + if (sub.starts_with("enum ")) { + i += 4; + continue; + } + if (sub.starts_with("class ") || sub.starts_with("union ")) { + i += 5; + continue; + } + if (sub.starts_with("struct ")) { + i += 6; + continue; + } + if (*sub.begin() != ' ') *out++ = *sub.begin(); + } + return out; +# else + return detail::write_bytes(out, string_view(ti.name())); +# endif +} + +} // namespace detail + +FMT_EXPORT +template +struct formatter { + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + return ctx.begin(); + } + + template + auto format(const std::type_info& ti, Context& ctx) const + -> decltype(ctx.out()) { + return detail::write_demangled_name(ctx.out(), ti); + } +}; +#endif + +FMT_EXPORT +template +struct formatter< + T, Char, // DEPRECATED! Mixing code unit types. + typename std::enable_if::value>::type> { + private: + bool with_typename_ = false; + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + auto it = ctx.begin(); + auto end = ctx.end(); + if (it == end || *it == '}') return it; + if (*it == 't') { + ++it; + with_typename_ = FMT_USE_RTTI != 0; + } + return it; + } + + template + auto format(const std::exception& ex, Context& ctx) const + -> decltype(ctx.out()) { + auto out = ctx.out(); +#if FMT_USE_RTTI + if (with_typename_) { + out = detail::write_demangled_name(out, typeid(ex)); + *out++ = ':'; + *out++ = ' '; + } +#endif + return detail::write_bytes(out, string_view(ex.what())); + } +}; + +namespace detail { + +template +struct has_flip : std::false_type {}; + +template +struct has_flip().flip())>> + : std::true_type {}; + +template struct is_bit_reference_like { + static constexpr const bool value = + std::is_convertible::value && + std::is_nothrow_assignable::value && has_flip::value; +}; + +#ifdef _LIBCPP_VERSION + +// Workaround for libc++ incompatibility with C++ standard. +// According to the Standard, `bitset::operator[] const` returns bool. +template +struct is_bit_reference_like> { + static constexpr const bool value = true; +}; + +#endif + +} // namespace detail + +// We can't use std::vector::reference and +// std::bitset::reference because the compiler can't deduce Allocator and N +// in partial specialization. +FMT_EXPORT +template +struct formatter::value>> + : formatter { + template + FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const + -> decltype(ctx.out()) { + return formatter::format(v, ctx); + } +}; + +template +auto ptr(const std::unique_ptr& p) -> const void* { + return p.get(); +} +template auto ptr(const std::shared_ptr& p) -> const void* { + return p.get(); +} + +FMT_EXPORT +template +struct formatter, Char, + enable_if_t::value>> + : formatter { + template + auto format(const std::atomic& v, FormatContext& ctx) const + -> decltype(ctx.out()) { + return formatter::format(v.load(), ctx); + } +}; + +#ifdef __cpp_lib_atomic_flag_test +FMT_EXPORT +template +struct formatter : formatter { + template + auto format(const std::atomic_flag& v, FormatContext& ctx) const + -> decltype(ctx.out()) { + return formatter::format(v.test(), ctx); + } +}; +#endif // __cpp_lib_atomic_flag_test + +FMT_EXPORT +template struct formatter, Char> { + private: + detail::dynamic_format_specs specs_; + + template + FMT_CONSTEXPR auto do_format(const std::complex& c, + detail::dynamic_format_specs& specs, + FormatContext& ctx, OutputIt out) const + -> OutputIt { + if (c.real() != 0) { + *out++ = Char('('); + out = detail::write(out, c.real(), specs, ctx.locale()); + specs.sign = sign::plus; + out = detail::write(out, c.imag(), specs, ctx.locale()); + if (!detail::isfinite(c.imag())) *out++ = Char(' '); + *out++ = Char('i'); + *out++ = Char(')'); + return out; + } + out = detail::write(out, c.imag(), specs, ctx.locale()); + if (!detail::isfinite(c.imag())) *out++ = Char(' '); + *out++ = Char('i'); + return out; + } + + public: + FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) + -> decltype(ctx.begin()) { + if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); + return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, + detail::type_constant::value); + } + + template + auto format(const std::complex& c, FormatContext& ctx) const + -> decltype(ctx.out()) { + auto specs = specs_; + if (specs.width_ref.kind != detail::arg_id_kind::none || + specs.precision_ref.kind != detail::arg_id_kind::none) { + detail::handle_dynamic_spec(specs.width, + specs.width_ref, ctx); + detail::handle_dynamic_spec( + specs.precision, specs.precision_ref, ctx); + } + + if (specs.width == 0) return do_format(c, specs, ctx, ctx.out()); + auto buf = basic_memory_buffer(); + + auto outer_specs = format_specs(); + outer_specs.width = specs.width; + outer_specs.fill = specs.fill; + outer_specs.align = specs.align; + + specs.width = 0; + specs.fill = {}; + specs.align = align::none; + + do_format(c, specs, ctx, basic_appender(buf)); + return detail::write(ctx.out(), + basic_string_view(buf.data(), buf.size()), + outer_specs); + } +}; + +FMT_END_NAMESPACE +#endif // FMT_STD_H_ diff --git a/include/spdlog/fmt/bundled/xchar.h b/include/spdlog/fmt/bundled/xchar.h new file mode 100644 index 0000000..b1f39ed --- /dev/null +++ b/include/spdlog/fmt/bundled/xchar.h @@ -0,0 +1,322 @@ +// Formatting library for C++ - optional wchar_t and exotic character support +// +// Copyright (c) 2012 - present, Victor Zverovich +// All rights reserved. +// +// For the license information refer to format.h. + +#ifndef FMT_XCHAR_H_ +#define FMT_XCHAR_H_ + +#include "color.h" +#include "format.h" +#include "ranges.h" + +#ifndef FMT_MODULE +# include +# if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) +# include +# endif +#endif + +FMT_BEGIN_NAMESPACE +namespace detail { + +template +using is_exotic_char = bool_constant::value>; + +template struct format_string_char {}; + +template +struct format_string_char< + S, void_t())))>> { + using type = char_t; +}; + +template +struct format_string_char::value>> { + using type = typename S::char_type; +}; + +template +using format_string_char_t = typename format_string_char::type; + +inline auto write_loc(basic_appender out, loc_value value, + const format_specs& specs, locale_ref loc) -> bool { +#ifndef FMT_STATIC_THOUSANDS_SEPARATOR + auto& numpunct = + std::use_facet>(loc.get()); + auto separator = std::wstring(); + auto grouping = numpunct.grouping(); + if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep()); + return value.visit(loc_writer{out, specs, separator, grouping, {}}); +#endif + return false; +} +} // namespace detail + +FMT_BEGIN_EXPORT + +using wstring_view = basic_string_view; +using wformat_parse_context = basic_format_parse_context; +using wformat_context = buffered_context; +using wformat_args = basic_format_args; +using wmemory_buffer = basic_memory_buffer; + +#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 +// Workaround broken conversion on older gcc. +template using wformat_string = wstring_view; +inline auto runtime(wstring_view s) -> wstring_view { return s; } +#else +template +using wformat_string = basic_format_string...>; +inline auto runtime(wstring_view s) -> runtime_format_string { + return {{s}}; +} +#endif + +template <> struct is_char : std::true_type {}; +template <> struct is_char : std::true_type {}; +template <> struct is_char : std::true_type {}; + +#ifdef __cpp_char8_t +template <> +struct is_char : bool_constant {}; +#endif + +template +constexpr auto make_wformat_args(T&... args) + -> decltype(fmt::make_format_args(args...)) { + return fmt::make_format_args(args...); +} + +inline namespace literals { +#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS +constexpr auto operator""_a(const wchar_t* s, size_t) + -> detail::udl_arg { + return {s}; +} +#endif +} // namespace literals + +template +auto join(It begin, Sentinel end, wstring_view sep) + -> join_view { + return {begin, end, sep}; +} + +template +auto join(Range&& range, wstring_view sep) + -> join_view, detail::sentinel_t, + wchar_t> { + return join(std::begin(range), std::end(range), sep); +} + +template +auto join(std::initializer_list list, wstring_view sep) + -> join_view { + return join(std::begin(list), std::end(list), sep); +} + +template +auto join(const std::tuple& tuple, basic_string_view sep) + -> tuple_join_view { + return {tuple, sep}; +} + +template ::value)> +auto vformat(basic_string_view format_str, + typename detail::vformat_args::type args) + -> std::basic_string { + auto buf = basic_memory_buffer(); + detail::vformat_to(buf, format_str, args); + return to_string(buf); +} + +template +auto format(wformat_string fmt, T&&... args) -> std::wstring { + return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...)); +} + +template +auto format_to(OutputIt out, wformat_string fmt, T&&... args) + -> OutputIt { + return vformat_to(out, fmt::wstring_view(fmt), + fmt::make_wformat_args(args...)); +} + +// Pass char_t as a default template parameter instead of using +// std::basic_string> to reduce the symbol size. +template , + FMT_ENABLE_IF(!std::is_same::value && + !std::is_same::value)> +auto format(const S& format_str, T&&... args) -> std::basic_string { + return vformat(detail::to_string_view(format_str), + fmt::make_format_args>(args...)); +} + +template , + FMT_ENABLE_IF(detail::is_locale::value&& + detail::is_exotic_char::value)> +inline auto vformat(const Locale& loc, const S& format_str, + typename detail::vformat_args::type args) + -> std::basic_string { + return detail::vformat(loc, detail::to_string_view(format_str), args); +} + +template , + FMT_ENABLE_IF(detail::is_locale::value&& + detail::is_exotic_char::value)> +inline auto format(const Locale& loc, const S& format_str, T&&... args) + -> std::basic_string { + return detail::vformat( + loc, detail::to_string_view(format_str), + fmt::make_format_args>(args...)); +} + +template , + FMT_ENABLE_IF(detail::is_output_iterator::value&& + detail::is_exotic_char::value)> +auto vformat_to(OutputIt out, const S& format_str, + typename detail::vformat_args::type args) -> OutputIt { + auto&& buf = detail::get_buffer(out); + detail::vformat_to(buf, detail::to_string_view(format_str), args); + return detail::get_iterator(buf, out); +} + +template , + FMT_ENABLE_IF(detail::is_output_iterator::value && + !std::is_same::value && + !std::is_same::value)> +inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt { + return vformat_to(out, detail::to_string_view(fmt), + fmt::make_format_args>(args...)); +} + +template , + FMT_ENABLE_IF(detail::is_output_iterator::value&& + detail::is_locale::value&& + detail::is_exotic_char::value)> +inline auto vformat_to(OutputIt out, const Locale& loc, const S& format_str, + typename detail::vformat_args::type args) + -> OutputIt { + auto&& buf = detail::get_buffer(out); + vformat_to(buf, detail::to_string_view(format_str), args, + detail::locale_ref(loc)); + return detail::get_iterator(buf, out); +} + +template , + bool enable = detail::is_output_iterator::value && + detail::is_locale::value && + detail::is_exotic_char::value> +inline auto format_to(OutputIt out, const Locale& loc, const S& format_str, + T&&... args) -> + typename std::enable_if::type { + return vformat_to(out, loc, detail::to_string_view(format_str), + fmt::make_format_args>(args...)); +} + +template ::value&& + detail::is_exotic_char::value)> +inline auto vformat_to_n(OutputIt out, size_t n, + basic_string_view format_str, + typename detail::vformat_args::type args) + -> format_to_n_result { + using traits = detail::fixed_buffer_traits; + auto buf = detail::iterator_buffer(out, n); + detail::vformat_to(buf, format_str, args); + return {buf.out(), buf.count()}; +} + +template , + FMT_ENABLE_IF(detail::is_output_iterator::value&& + detail::is_exotic_char::value)> +inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args) + -> format_to_n_result { + return vformat_to_n(out, n, fmt::basic_string_view(fmt), + fmt::make_format_args>(args...)); +} + +template , + FMT_ENABLE_IF(detail::is_exotic_char::value)> +inline auto formatted_size(const S& fmt, T&&... args) -> size_t { + auto buf = detail::counting_buffer(); + detail::vformat_to(buf, detail::to_string_view(fmt), + fmt::make_format_args>(args...)); + return buf.count(); +} + +inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) { + auto buf = wmemory_buffer(); + detail::vformat_to(buf, fmt, args); + buf.push_back(L'\0'); + if (std::fputws(buf.data(), f) == -1) + FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); +} + +inline void vprint(wstring_view fmt, wformat_args args) { + vprint(stdout, fmt, args); +} + +template +void print(std::FILE* f, wformat_string fmt, T&&... args) { + return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...)); +} + +template void print(wformat_string fmt, T&&... args) { + return vprint(wstring_view(fmt), fmt::make_wformat_args(args...)); +} + +template +void println(std::FILE* f, wformat_string fmt, T&&... args) { + return print(f, L"{}\n", fmt::format(fmt, std::forward(args)...)); +} + +template void println(wformat_string fmt, T&&... args) { + return print(L"{}\n", fmt::format(fmt, std::forward(args)...)); +} + +inline auto vformat(const text_style& ts, wstring_view fmt, wformat_args args) + -> std::wstring { + auto buf = wmemory_buffer(); + detail::vformat_to(buf, ts, fmt, args); + return fmt::to_string(buf); +} + +template +inline auto format(const text_style& ts, wformat_string fmt, T&&... args) + -> std::wstring { + return fmt::vformat(ts, fmt, fmt::make_wformat_args(args...)); +} + +template +FMT_DEPRECATED void print(std::FILE* f, const text_style& ts, + wformat_string fmt, const T&... args) { + vprint(f, ts, fmt, fmt::make_wformat_args(args...)); +} + +template +FMT_DEPRECATED void print(const text_style& ts, wformat_string fmt, + const T&... args) { + return print(stdout, ts, fmt, args...); +} + +/// Converts `value` to `std::wstring` using the default format for type `T`. +template inline auto to_wstring(const T& value) -> std::wstring { + return format(FMT_STRING(L"{}"), value); +} +FMT_END_EXPORT +FMT_END_NAMESPACE + +#endif // FMT_XCHAR_H_ diff --git a/include/spdlog/fmt/chrono.h b/include/spdlog/fmt/chrono.h new file mode 100644 index 0000000..a72a5bd --- /dev/null +++ b/include/spdlog/fmt/chrono.h @@ -0,0 +1,23 @@ +// +// Copyright(c) 2016 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once +// +// include bundled or external copy of fmtlib's chrono support +// +#include + +#if !defined(SPDLOG_USE_STD_FORMAT) + #if !defined(SPDLOG_FMT_EXTERNAL) + #ifdef SPDLOG_HEADER_ONLY + #ifndef FMT_HEADER_ONLY + #define FMT_HEADER_ONLY + #endif + #endif + #include + #else + #include + #endif +#endif diff --git a/include/spdlog/fmt/compile.h b/include/spdlog/fmt/compile.h new file mode 100644 index 0000000..3c9c25d --- /dev/null +++ b/include/spdlog/fmt/compile.h @@ -0,0 +1,23 @@ +// +// Copyright(c) 2016 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once +// +// include bundled or external copy of fmtlib's compile-time support +// +#include + +#if !defined(SPDLOG_USE_STD_FORMAT) + #if !defined(SPDLOG_FMT_EXTERNAL) + #ifdef SPDLOG_HEADER_ONLY + #ifndef FMT_HEADER_ONLY + #define FMT_HEADER_ONLY + #endif + #endif + #include + #else + #include + #endif +#endif diff --git a/include/spdlog/fmt/fmt.h b/include/spdlog/fmt/fmt.h new file mode 100644 index 0000000..2f09c15 --- /dev/null +++ b/include/spdlog/fmt/fmt.h @@ -0,0 +1,31 @@ +// +// Copyright(c) 2016-2018 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once + +// +// Include a bundled header-only copy of fmtlib or an external one. +// By default spdlog include its own copy. +// +#include + +#if defined(SPDLOG_USE_STD_FORMAT) // SPDLOG_USE_STD_FORMAT is defined - use std::format + #include +#elif !defined(SPDLOG_FMT_EXTERNAL) + #if !defined(SPDLOG_COMPILED_LIB) && !defined(FMT_HEADER_ONLY) + #define FMT_HEADER_ONLY + #endif + #ifndef FMT_USE_WINDOWS_H + #define FMT_USE_WINDOWS_H 0 + #endif + + #include + #include + +#else // SPDLOG_FMT_EXTERNAL is defined - use external fmtlib + #include + #include + #include +#endif diff --git a/include/spdlog/fmt/ostr.h b/include/spdlog/fmt/ostr.h new file mode 100644 index 0000000..2b90105 --- /dev/null +++ b/include/spdlog/fmt/ostr.h @@ -0,0 +1,23 @@ +// +// Copyright(c) 2016 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once +// +// include bundled or external copy of fmtlib's ostream support +// +#include + +#if !defined(SPDLOG_USE_STD_FORMAT) + #if !defined(SPDLOG_FMT_EXTERNAL) + #ifdef SPDLOG_HEADER_ONLY + #ifndef FMT_HEADER_ONLY + #define FMT_HEADER_ONLY + #endif + #endif + #include + #else + #include + #endif +#endif diff --git a/include/spdlog/fmt/ranges.h b/include/spdlog/fmt/ranges.h new file mode 100644 index 0000000..5bb91e9 --- /dev/null +++ b/include/spdlog/fmt/ranges.h @@ -0,0 +1,23 @@ +// +// Copyright(c) 2016 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once +// +// include bundled or external copy of fmtlib's ranges support +// +#include + +#if !defined(SPDLOG_USE_STD_FORMAT) + #if !defined(SPDLOG_FMT_EXTERNAL) + #ifdef SPDLOG_HEADER_ONLY + #ifndef FMT_HEADER_ONLY + #define FMT_HEADER_ONLY + #endif + #endif + #include + #else + #include + #endif +#endif diff --git a/include/spdlog/fmt/std.h b/include/spdlog/fmt/std.h new file mode 100644 index 0000000..dabe6f6 --- /dev/null +++ b/include/spdlog/fmt/std.h @@ -0,0 +1,24 @@ +// +// Copyright(c) 2016 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once +// +// include bundled or external copy of fmtlib's std support (for formatting e.g. +// std::filesystem::path, std::thread::id, std::monostate, std::variant, ...) +// +#include + +#if !defined(SPDLOG_USE_STD_FORMAT) + #if !defined(SPDLOG_FMT_EXTERNAL) + #ifdef SPDLOG_HEADER_ONLY + #ifndef FMT_HEADER_ONLY + #define FMT_HEADER_ONLY + #endif + #endif + #include + #else + #include + #endif +#endif diff --git a/include/spdlog/fmt/xchar.h b/include/spdlog/fmt/xchar.h new file mode 100644 index 0000000..2525f05 --- /dev/null +++ b/include/spdlog/fmt/xchar.h @@ -0,0 +1,23 @@ +// +// Copyright(c) 2016 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once +// +// include bundled or external copy of fmtlib's xchar support +// +#include + +#if !defined(SPDLOG_USE_STD_FORMAT) + #if !defined(SPDLOG_FMT_EXTERNAL) + #ifdef SPDLOG_HEADER_ONLY + #ifndef FMT_HEADER_ONLY + #define FMT_HEADER_ONLY + #endif + #endif + #include + #else + #include + #endif +#endif diff --git a/include/spdlog/formatter.h b/include/spdlog/formatter.h new file mode 100644 index 0000000..4d482f8 --- /dev/null +++ b/include/spdlog/formatter.h @@ -0,0 +1,17 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +namespace spdlog { + +class formatter { +public: + virtual ~formatter() = default; + virtual void format(const details::log_msg &msg, memory_buf_t &dest) = 0; + virtual std::unique_ptr clone() const = 0; +}; +} // namespace spdlog diff --git a/include/spdlog/fwd.h b/include/spdlog/fwd.h new file mode 100644 index 0000000..647b16b --- /dev/null +++ b/include/spdlog/fwd.h @@ -0,0 +1,18 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +namespace spdlog { +class logger; +class formatter; + +namespace sinks { +class sink; +} + +namespace level { +enum level_enum : int; +} + +} // namespace spdlog diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h new file mode 100644 index 0000000..5218fe4 --- /dev/null +++ b/include/spdlog/logger-inl.h @@ -0,0 +1,198 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include +#include + +#include + +namespace spdlog { + +// public methods +SPDLOG_INLINE logger::logger(const logger &other) + : name_(other.name_), + sinks_(other.sinks_), + level_(other.level_.load(std::memory_order_relaxed)), + flush_level_(other.flush_level_.load(std::memory_order_relaxed)), + custom_err_handler_(other.custom_err_handler_), + tracer_(other.tracer_) {} + +SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT + : name_(std::move(other.name_)), + sinks_(std::move(other.sinks_)), + level_(other.level_.load(std::memory_order_relaxed)), + flush_level_(other.flush_level_.load(std::memory_order_relaxed)), + custom_err_handler_(std::move(other.custom_err_handler_)), + tracer_(std::move(other.tracer_)) + +{} + +SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT { + this->swap(other); + return *this; +} + +SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT { + name_.swap(other.name_); + sinks_.swap(other.sinks_); + + // swap level_ + auto other_level = other.level_.load(); + auto my_level = level_.exchange(other_level); + other.level_.store(my_level); + + // swap flush level_ + other_level = other.flush_level_.load(); + my_level = flush_level_.exchange(other_level); + other.flush_level_.store(my_level); + + custom_err_handler_.swap(other.custom_err_handler_); + std::swap(tracer_, other.tracer_); +} + +SPDLOG_INLINE void swap(logger &a, logger &b) { a.swap(b); } + +SPDLOG_INLINE void logger::set_level(level::level_enum log_level) { level_.store(log_level); } + +SPDLOG_INLINE level::level_enum logger::level() const { + return static_cast(level_.load(std::memory_order_relaxed)); +} + +SPDLOG_INLINE const std::string &logger::name() const { return name_; } + +// set formatting for the sinks in this logger. +// each sink will get a separate instance of the formatter object. +SPDLOG_INLINE void logger::set_formatter(std::unique_ptr f) { + for (auto it = sinks_.begin(); it != sinks_.end(); ++it) { + if (std::next(it) == sinks_.end()) { + // last element - we can be move it. + (*it)->set_formatter(std::move(f)); + break; // to prevent clang-tidy warning + } else { + (*it)->set_formatter(f->clone()); + } + } +} + +SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type) { + auto new_formatter = details::make_unique(std::move(pattern), time_type); + set_formatter(std::move(new_formatter)); +} + +// create new backtrace sink and move to it all our child sinks +SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages) { tracer_.enable(n_messages); } + +// restore orig sinks and level and delete the backtrace sink +SPDLOG_INLINE void logger::disable_backtrace() { tracer_.disable(); } + +SPDLOG_INLINE void logger::dump_backtrace() { dump_backtrace_(); } + +// flush functions +SPDLOG_INLINE void logger::flush() { flush_(); } + +SPDLOG_INLINE void logger::flush_on(level::level_enum log_level) { flush_level_.store(log_level); } + +SPDLOG_INLINE level::level_enum logger::flush_level() const { + return static_cast(flush_level_.load(std::memory_order_relaxed)); +} + +// sinks +SPDLOG_INLINE const std::vector &logger::sinks() const { return sinks_; } + +SPDLOG_INLINE std::vector &logger::sinks() { return sinks_; } + +// error handler +SPDLOG_INLINE void logger::set_error_handler(err_handler handler) { + custom_err_handler_ = std::move(handler); +} + +// create new logger with same sinks and configuration. +SPDLOG_INLINE std::shared_ptr logger::clone(std::string logger_name) { + auto cloned = std::make_shared(*this); + cloned->name_ = std::move(logger_name); + return cloned; +} + +// protected methods +SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, + bool log_enabled, + bool traceback_enabled) { + if (log_enabled) { + sink_it_(log_msg); + } + if (traceback_enabled) { + tracer_.push_back(log_msg); + } +} + +SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { + for (auto &sink : sinks_) { + if (sink->should_log(msg.level)) { + SPDLOG_TRY { sink->log(msg); } + SPDLOG_LOGGER_CATCH(msg.source) + } + } + + if (should_flush_(msg)) { + flush_(); + } +} + +SPDLOG_INLINE void logger::flush_() { + for (auto &sink : sinks_) { + SPDLOG_TRY { sink->flush(); } + SPDLOG_LOGGER_CATCH(source_loc()) + } +} + +SPDLOG_INLINE void logger::dump_backtrace_() { + using details::log_msg; + if (tracer_.enabled() && !tracer_.empty()) { + sink_it_( + log_msg{name(), level::info, "****************** Backtrace Start ******************"}); + tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); }); + sink_it_( + log_msg{name(), level::info, "****************** Backtrace End ********************"}); + } +} + +SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg) { + auto flush_level = flush_level_.load(std::memory_order_relaxed); + return (msg.level >= flush_level) && (msg.level != level::off); +} + +SPDLOG_INLINE void logger::err_handler_(const std::string &msg) { + if (custom_err_handler_) { + custom_err_handler_(msg); + } else { + using std::chrono::system_clock; + static std::mutex mutex; + static std::chrono::system_clock::time_point last_report_time; + static size_t err_counter = 0; + std::lock_guard lk{mutex}; + auto now = system_clock::now(); + err_counter++; + if (now - last_report_time < std::chrono::seconds(1)) { + return; + } + last_report_time = now; + auto tm_time = details::os::localtime(system_clock::to_time_t(now)); + char date_buf[64]; + std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time); +#if defined(USING_R) && defined(R_R_H) // if in R environment + REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), + msg.c_str()); +#else + std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, + name().c_str(), msg.c_str()); +#endif + } +} +} // namespace spdlog diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h new file mode 100644 index 0000000..f49bdc0 --- /dev/null +++ b/include/spdlog/logger.h @@ -0,0 +1,379 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// Thread safe logger (except for set_error_handler()) +// Has name, log level, vector of std::shared sink pointers and formatter +// Upon each log write the logger: +// 1. Checks if its log level is enough to log the message and if yes: +// 2. Call the underlying sinks to do the job. +// 3. Each sink use its own private copy of a formatter to format the message +// and send to its destination. +// +// The use of private formatter per sink provides the opportunity to cache some +// formatted data, and support for different format per sink. + +#include +#include +#include + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + #ifndef _WIN32 + #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows + #endif + #include +#endif + +#include + +#ifndef SPDLOG_NO_EXCEPTIONS + #define SPDLOG_LOGGER_CATCH(location) \ + catch (const std::exception &ex) { \ + if (location.filename) { \ + err_handler_(fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), \ + location.filename, location.line)); \ + } else { \ + err_handler_(ex.what()); \ + } \ + } \ + catch (...) { \ + err_handler_("Rethrowing unknown exception in logger"); \ + throw; \ + } +#else + #define SPDLOG_LOGGER_CATCH(location) +#endif + +namespace spdlog { + +class SPDLOG_API logger { +public: + // Empty logger + explicit logger(std::string name) + : name_(std::move(name)), + sinks_() {} + + // Logger with range on sinks + template + logger(std::string name, It begin, It end) + : name_(std::move(name)), + sinks_(begin, end) {} + + // Logger with single sink + logger(std::string name, sink_ptr single_sink) + : logger(std::move(name), {std::move(single_sink)}) {} + + // Logger with sinks init list + logger(std::string name, sinks_init_list sinks) + : logger(std::move(name), sinks.begin(), sinks.end()) {} + + virtual ~logger() = default; + + logger(const logger &other); + logger(logger &&other) SPDLOG_NOEXCEPT; + logger &operator=(logger other) SPDLOG_NOEXCEPT; + void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; + + template + void log(source_loc loc, level::level_enum lvl, format_string_t fmt, Args &&...args) { + log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); + } + + template + void log(level::level_enum lvl, format_string_t fmt, Args &&...args) { + log(source_loc{}, lvl, fmt, std::forward(args)...); + } + + template + void log(level::level_enum lvl, const T &msg) { + log(source_loc{}, lvl, msg); + } + + // T cannot be statically converted to format string (including string_view/wstring_view) + template ::value, + int>::type = 0> + void log(source_loc loc, level::level_enum lvl, const T &msg) { + log(loc, lvl, "{}", msg); + } + + void log(log_clock::time_point log_time, + source_loc loc, + level::level_enum lvl, + string_view_t msg) { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { + return; + } + + details::log_msg log_msg(log_time, loc, name_, lvl, msg); + log_it_(log_msg, log_enabled, traceback_enabled); + } + + void log(source_loc loc, level::level_enum lvl, string_view_t msg) { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { + return; + } + + details::log_msg log_msg(loc, name_, lvl, msg); + log_it_(log_msg, log_enabled, traceback_enabled); + } + + void log(level::level_enum lvl, string_view_t msg) { log(source_loc{}, lvl, msg); } + + template + void trace(format_string_t fmt, Args &&...args) { + log(level::trace, fmt, std::forward(args)...); + } + + template + void debug(format_string_t fmt, Args &&...args) { + log(level::debug, fmt, std::forward(args)...); + } + + template + void info(format_string_t fmt, Args &&...args) { + log(level::info, fmt, std::forward(args)...); + } + + template + void warn(format_string_t fmt, Args &&...args) { + log(level::warn, fmt, std::forward(args)...); + } + + template + void error(format_string_t fmt, Args &&...args) { + log(level::err, fmt, std::forward(args)...); + } + + template + void critical(format_string_t fmt, Args &&...args) { + log(level::critical, fmt, std::forward(args)...); + } + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + template + void log(source_loc loc, level::level_enum lvl, wformat_string_t fmt, Args &&...args) { + log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); + } + + template + void log(level::level_enum lvl, wformat_string_t fmt, Args &&...args) { + log(source_loc{}, lvl, fmt, std::forward(args)...); + } + + void log(log_clock::time_point log_time, + source_loc loc, + level::level_enum lvl, + wstring_view_t msg) { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { + return; + } + + memory_buf_t buf; + details::os::wstr_to_utf8buf(wstring_view_t(msg.data(), msg.size()), buf); + details::log_msg log_msg(log_time, loc, name_, lvl, string_view_t(buf.data(), buf.size())); + log_it_(log_msg, log_enabled, traceback_enabled); + } + + void log(source_loc loc, level::level_enum lvl, wstring_view_t msg) { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { + return; + } + + memory_buf_t buf; + details::os::wstr_to_utf8buf(wstring_view_t(msg.data(), msg.size()), buf); + details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); + log_it_(log_msg, log_enabled, traceback_enabled); + } + + void log(level::level_enum lvl, wstring_view_t msg) { log(source_loc{}, lvl, msg); } + + template + void trace(wformat_string_t fmt, Args &&...args) { + log(level::trace, fmt, std::forward(args)...); + } + + template + void debug(wformat_string_t fmt, Args &&...args) { + log(level::debug, fmt, std::forward(args)...); + } + + template + void info(wformat_string_t fmt, Args &&...args) { + log(level::info, fmt, std::forward(args)...); + } + + template + void warn(wformat_string_t fmt, Args &&...args) { + log(level::warn, fmt, std::forward(args)...); + } + + template + void error(wformat_string_t fmt, Args &&...args) { + log(level::err, fmt, std::forward(args)...); + } + + template + void critical(wformat_string_t fmt, Args &&...args) { + log(level::critical, fmt, std::forward(args)...); + } +#endif + + template + void trace(const T &msg) { + log(level::trace, msg); + } + + template + void debug(const T &msg) { + log(level::debug, msg); + } + + template + void info(const T &msg) { + log(level::info, msg); + } + + template + void warn(const T &msg) { + log(level::warn, msg); + } + + template + void error(const T &msg) { + log(level::err, msg); + } + + template + void critical(const T &msg) { + log(level::critical, msg); + } + + // return true logging is enabled for the given level. + bool should_log(level::level_enum msg_level) const { + return msg_level >= level_.load(std::memory_order_relaxed); + } + + // return true if backtrace logging is enabled. + bool should_backtrace() const { return tracer_.enabled(); } + + void set_level(level::level_enum log_level); + + level::level_enum level() const; + + const std::string &name() const; + + // set formatting for the sinks in this logger. + // each sink will get a separate instance of the formatter object. + void set_formatter(std::unique_ptr f); + + // set formatting for the sinks in this logger. + // equivalent to + // set_formatter(make_unique(pattern, time_type)) + // Note: each sink will get a new instance of a formatter object, replacing the old one. + void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); + + // backtrace support. + // efficiently store all debug/trace messages in a circular buffer until needed for debugging. + void enable_backtrace(size_t n_messages); + void disable_backtrace(); + void dump_backtrace(); + + // flush functions + void flush(); + void flush_on(level::level_enum log_level); + level::level_enum flush_level() const; + + // sinks + const std::vector &sinks() const; + + std::vector &sinks(); + + // error handler + void set_error_handler(err_handler); + + // create new logger with same sinks and configuration. + virtual std::shared_ptr clone(std::string logger_name); + +protected: + std::string name_; + std::vector sinks_; + spdlog::level_t level_{level::info}; + spdlog::level_t flush_level_{level::off}; + err_handler custom_err_handler_{nullptr}; + details::backtracer tracer_; + + // common implementation for after templated public api has been resolved + template + void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args) { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { + return; + } + SPDLOG_TRY { + memory_buf_t buf; +#ifdef SPDLOG_USE_STD_FORMAT + fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(args...)); +#else + fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(args...)); +#endif + + details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); + log_it_(log_msg, log_enabled, traceback_enabled); + } + SPDLOG_LOGGER_CATCH(loc) + } + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + template + void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&...args) { + bool log_enabled = should_log(lvl); + bool traceback_enabled = tracer_.enabled(); + if (!log_enabled && !traceback_enabled) { + return; + } + SPDLOG_TRY { + // format to wmemory_buffer and convert to utf8 + wmemory_buf_t wbuf; + fmt_lib::vformat_to(std::back_inserter(wbuf), fmt, + fmt_lib::make_format_args(args...)); + + memory_buf_t buf; + details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); + details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); + log_it_(log_msg, log_enabled, traceback_enabled); + } + SPDLOG_LOGGER_CATCH(loc) + } +#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + + // log the given message (if the given log level is high enough), + // and save backtrace (if backtrace is enabled). + void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled); + virtual void sink_it_(const details::log_msg &msg); + virtual void flush_(); + void dump_backtrace_(); + bool should_flush_(const details::log_msg &msg); + + // handle errors during logging. + // default handler prints the error to stderr at max rate of 1 message/sec. + void err_handler_(const std::string &msg); +}; + +void swap(logger &a, logger &b); + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "logger-inl.h" +#endif diff --git a/include/spdlog/mdc.h b/include/spdlog/mdc.h new file mode 100644 index 0000000..80b6f25 --- /dev/null +++ b/include/spdlog/mdc.h @@ -0,0 +1,50 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#if defined(SPDLOG_NO_TLS) + #error "This header requires thread local storage support, but SPDLOG_NO_TLS is defined." +#endif + +#include +#include + +#include + +// MDC is a simple map of key->string values stored in thread local storage whose content will be printed by the loggers. +// Note: Not supported in async mode (thread local storage - so the async thread pool have different copy). +// +// Usage example: +// spdlog::mdc::put("mdc_key_1", "mdc_value_1"); +// spdlog::info("Hello, {}", "World!"); // => [2024-04-26 02:08:05.040] [info] [mdc_key_1:mdc_value_1] Hello, World! + +namespace spdlog { +class SPDLOG_API mdc { +public: + using mdc_map_t = std::map; + + static void put(const std::string &key, const std::string &value) { + get_context()[key] = value; + } + + static std::string get(const std::string &key) { + auto &context = get_context(); + auto it = context.find(key); + if (it != context.end()) { + return it->second; + } + return ""; + } + + static void remove(const std::string &key) { get_context().erase(key); } + + static void clear() { get_context().clear(); } + + static mdc_map_t &get_context() { + static thread_local mdc_map_t context; + return context; + } +}; + +} // namespace spdlog diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h new file mode 100644 index 0000000..b53d805 --- /dev/null +++ b/include/spdlog/pattern_formatter-inl.h @@ -0,0 +1,1338 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include +#include + +#ifndef SPDLOG_NO_TLS + #include +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace spdlog { +namespace details { + +/////////////////////////////////////////////////////////////////////// +// name & level pattern appender +/////////////////////////////////////////////////////////////////////// + +class scoped_padder { +public: + scoped_padder(size_t wrapped_size, const padding_info &padinfo, memory_buf_t &dest) + : padinfo_(padinfo), + dest_(dest) { + remaining_pad_ = static_cast(padinfo.width_) - static_cast(wrapped_size); + if (remaining_pad_ <= 0) { + return; + } + + if (padinfo_.side_ == padding_info::pad_side::left) { + pad_it(remaining_pad_); + remaining_pad_ = 0; + } else if (padinfo_.side_ == padding_info::pad_side::center) { + auto half_pad = remaining_pad_ / 2; + auto reminder = remaining_pad_ & 1; + pad_it(half_pad); + remaining_pad_ = half_pad + reminder; // for the right side + } + } + + template + static unsigned int count_digits(T n) { + return fmt_helper::count_digits(n); + } + + ~scoped_padder() { + if (remaining_pad_ >= 0) { + pad_it(remaining_pad_); + } else if (padinfo_.truncate_) { + long new_size = static_cast(dest_.size()) + remaining_pad_; + dest_.resize(static_cast(new_size)); + } + } + +private: + void pad_it(long count) { + fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast(count)), + dest_); + } + + const padding_info &padinfo_; + memory_buf_t &dest_; + long remaining_pad_; + string_view_t spaces_{" ", 64}; +}; + +struct null_scoped_padder { + null_scoped_padder(size_t /*wrapped_size*/, + const padding_info & /*padinfo*/, + memory_buf_t & /*dest*/) {} + + template + static unsigned int count_digits(T /* number */) { + return 0; + } +}; + +template +class name_formatter final : public flag_formatter { +public: + explicit name_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + ScopedPadder p(msg.logger_name.size(), padinfo_, dest); + fmt_helper::append_string_view(msg.logger_name, dest); + } +}; + +// log level appender +template +class level_formatter final : public flag_formatter { +public: + explicit level_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + const string_view_t &level_name = level::to_string_view(msg.level); + ScopedPadder p(level_name.size(), padinfo_, dest); + fmt_helper::append_string_view(level_name, dest); + } +}; + +// short log level appender +template +class short_level_formatter final : public flag_formatter { +public: + explicit short_level_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + string_view_t level_name{level::to_short_c_str(msg.level)}; + ScopedPadder p(level_name.size(), padinfo_, dest); + fmt_helper::append_string_view(level_name, dest); + } +}; + +/////////////////////////////////////////////////////////////////////// +// Date time pattern appenders +/////////////////////////////////////////////////////////////////////// + +static const char *ampm(const tm &t) { return t.tm_hour >= 12 ? "PM" : "AM"; } + +static int to12h(const tm &t) { return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour; } + +// Abbreviated weekday name +static std::array days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}}; + +template +class a_formatter final : public flag_formatter { +public: + explicit a_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + string_view_t field_value{days[static_cast(tm_time.tm_wday)]}; + ScopedPadder p(field_value.size(), padinfo_, dest); + fmt_helper::append_string_view(field_value, dest); + } +}; + +// Full weekday name +static std::array full_days{ + {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}}; + +template +class A_formatter : public flag_formatter { +public: + explicit A_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + string_view_t field_value{full_days[static_cast(tm_time.tm_wday)]}; + ScopedPadder p(field_value.size(), padinfo_, dest); + fmt_helper::append_string_view(field_value, dest); + } +}; + +// Abbreviated month +static const std::array months{ + {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}}; + +template +class b_formatter final : public flag_formatter { +public: + explicit b_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + string_view_t field_value{months[static_cast(tm_time.tm_mon)]}; + ScopedPadder p(field_value.size(), padinfo_, dest); + fmt_helper::append_string_view(field_value, dest); + } +}; + +// Full month name +static const std::array full_months{{"January", "February", "March", "April", + "May", "June", "July", "August", "September", + "October", "November", "December"}}; + +template +class B_formatter final : public flag_formatter { +public: + explicit B_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + string_view_t field_value{full_months[static_cast(tm_time.tm_mon)]}; + ScopedPadder p(field_value.size(), padinfo_, dest); + fmt_helper::append_string_view(field_value, dest); + } +}; + +// Date and time representation (Thu Aug 23 15:35:46 2014) +template +class c_formatter final : public flag_formatter { +public: + explicit c_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 24; + ScopedPadder p(field_size, padinfo_, dest); + + fmt_helper::append_string_view(days[static_cast(tm_time.tm_wday)], dest); + dest.push_back(' '); + fmt_helper::append_string_view(months[static_cast(tm_time.tm_mon)], dest); + dest.push_back(' '); + fmt_helper::append_int(tm_time.tm_mday, dest); + dest.push_back(' '); + // time + + fmt_helper::pad2(tm_time.tm_hour, dest); + dest.push_back(':'); + fmt_helper::pad2(tm_time.tm_min, dest); + dest.push_back(':'); + fmt_helper::pad2(tm_time.tm_sec, dest); + dest.push_back(' '); + fmt_helper::append_int(tm_time.tm_year + 1900, dest); + } +}; + +// year - 2 digit +template +class C_formatter final : public flag_formatter { +public: + explicit C_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad2(tm_time.tm_year % 100, dest); + } +}; + +// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01 +template +class D_formatter final : public flag_formatter { +public: + explicit D_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 10; + ScopedPadder p(field_size, padinfo_, dest); + + fmt_helper::pad2(tm_time.tm_mon + 1, dest); + dest.push_back('/'); + fmt_helper::pad2(tm_time.tm_mday, dest); + dest.push_back('/'); + fmt_helper::pad2(tm_time.tm_year % 100, dest); + } +}; + +// year - 4 digit +template +class Y_formatter final : public flag_formatter { +public: + explicit Y_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 4; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::append_int(tm_time.tm_year + 1900, dest); + } +}; + +// month 1-12 +template +class m_formatter final : public flag_formatter { +public: + explicit m_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad2(tm_time.tm_mon + 1, dest); + } +}; + +// day of month 1-31 +template +class d_formatter final : public flag_formatter { +public: + explicit d_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad2(tm_time.tm_mday, dest); + } +}; + +// hours in 24 format 0-23 +template +class H_formatter final : public flag_formatter { +public: + explicit H_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad2(tm_time.tm_hour, dest); + } +}; + +// hours in 12 format 1-12 +template +class I_formatter final : public flag_formatter { +public: + explicit I_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad2(to12h(tm_time), dest); + } +}; + +// minutes 0-59 +template +class M_formatter final : public flag_formatter { +public: + explicit M_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad2(tm_time.tm_min, dest); + } +}; + +// seconds 0-59 +template +class S_formatter final : public flag_formatter { +public: + explicit S_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad2(tm_time.tm_sec, dest); + } +}; + +// milliseconds +template +class e_formatter final : public flag_formatter { +public: + explicit e_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + auto millis = fmt_helper::time_fraction(msg.time); + const size_t field_size = 3; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad3(static_cast(millis.count()), dest); + } +}; + +// microseconds +template +class f_formatter final : public flag_formatter { +public: + explicit f_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + auto micros = fmt_helper::time_fraction(msg.time); + + const size_t field_size = 6; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad6(static_cast(micros.count()), dest); + } +}; + +// nanoseconds +template +class F_formatter final : public flag_formatter { +public: + explicit F_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + auto ns = fmt_helper::time_fraction(msg.time); + const size_t field_size = 9; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::pad9(static_cast(ns.count()), dest); + } +}; + +// seconds since epoch +template +class E_formatter final : public flag_formatter { +public: + explicit E_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + const size_t field_size = 10; + ScopedPadder p(field_size, padinfo_, dest); + auto duration = msg.time.time_since_epoch(); + auto seconds = std::chrono::duration_cast(duration).count(); + fmt_helper::append_int(seconds, dest); + } +}; + +// AM/PM +template +class p_formatter final : public flag_formatter { +public: + explicit p_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 2; + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::append_string_view(ampm(tm_time), dest); + } +}; + +// 12 hour clock 02:55:02 pm +template +class r_formatter final : public flag_formatter { +public: + explicit r_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 11; + ScopedPadder p(field_size, padinfo_, dest); + + fmt_helper::pad2(to12h(tm_time), dest); + dest.push_back(':'); + fmt_helper::pad2(tm_time.tm_min, dest); + dest.push_back(':'); + fmt_helper::pad2(tm_time.tm_sec, dest); + dest.push_back(' '); + fmt_helper::append_string_view(ampm(tm_time), dest); + } +}; + +// 24-hour HH:MM time, equivalent to %H:%M +template +class R_formatter final : public flag_formatter { +public: + explicit R_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 5; + ScopedPadder p(field_size, padinfo_, dest); + + fmt_helper::pad2(tm_time.tm_hour, dest); + dest.push_back(':'); + fmt_helper::pad2(tm_time.tm_min, dest); + } +}; + +// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S +template +class T_formatter final : public flag_formatter { +public: + explicit T_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 8; + ScopedPadder p(field_size, padinfo_, dest); + + fmt_helper::pad2(tm_time.tm_hour, dest); + dest.push_back(':'); + fmt_helper::pad2(tm_time.tm_min, dest); + dest.push_back(':'); + fmt_helper::pad2(tm_time.tm_sec, dest); + } +}; + +// ISO 8601 offset from UTC in timezone (+-HH:MM) +template +class z_formatter final : public flag_formatter { +public: + explicit z_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + z_formatter() = default; + z_formatter(const z_formatter &) = delete; + z_formatter &operator=(const z_formatter &) = delete; + + void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override { + const size_t field_size = 6; + ScopedPadder p(field_size, padinfo_, dest); + + auto total_minutes = get_cached_offset(msg, tm_time); + bool is_negative = total_minutes < 0; + if (is_negative) { + total_minutes = -total_minutes; + dest.push_back('-'); + } else { + dest.push_back('+'); + } + + fmt_helper::pad2(total_minutes / 60, dest); // hours + dest.push_back(':'); + fmt_helper::pad2(total_minutes % 60, dest); // minutes + } + +private: + log_clock::time_point last_update_{std::chrono::seconds(0)}; + int offset_minutes_{0}; + + int get_cached_offset(const log_msg &msg, const std::tm &tm_time) { + // refresh every 10 seconds + if (msg.time - last_update_ >= std::chrono::seconds(10)) { + offset_minutes_ = os::utc_minutes_offset(tm_time); + last_update_ = msg.time; + } + return offset_minutes_; + } +}; + +// Thread id +template +class t_formatter final : public flag_formatter { +public: + explicit t_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + const auto field_size = ScopedPadder::count_digits(msg.thread_id); + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::append_int(msg.thread_id, dest); + } +}; + +// Current pid +template +class pid_formatter final : public flag_formatter { +public: + explicit pid_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { + const auto pid = static_cast(details::os::pid()); + auto field_size = ScopedPadder::count_digits(pid); + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::append_int(pid, dest); + } +}; + +template +class v_formatter final : public flag_formatter { +public: + explicit v_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + ScopedPadder p(msg.payload.size(), padinfo_, dest); + fmt_helper::append_string_view(msg.payload, dest); + } +}; + +class ch_formatter final : public flag_formatter { +public: + explicit ch_formatter(char ch) + : ch_(ch) {} + + void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { + dest.push_back(ch_); + } + +private: + char ch_; +}; + +// aggregate user chars to display as is +class aggregate_formatter final : public flag_formatter { +public: + aggregate_formatter() = default; + + void add_ch(char ch) { str_ += ch; } + void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { + fmt_helper::append_string_view(str_, dest); + } + +private: + std::string str_; +}; + +// mark the color range. expect it to be in the form of "%^colored text%$" +class color_start_formatter final : public flag_formatter { +public: + explicit color_start_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + msg.color_range_start = dest.size(); + } +}; + +class color_stop_formatter final : public flag_formatter { +public: + explicit color_stop_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + msg.color_range_end = dest.size(); + } +}; + +// print source location +template +class source_location_formatter final : public flag_formatter { +public: + explicit source_location_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + if (msg.source.empty()) { + ScopedPadder p(0, padinfo_, dest); + return; + } + + size_t text_size; + if (padinfo_.enabled()) { + // calc text size for padding based on "filename:line" + text_size = std::char_traits::length(msg.source.filename) + + ScopedPadder::count_digits(msg.source.line) + 1; + } else { + text_size = 0; + } + + ScopedPadder p(text_size, padinfo_, dest); + fmt_helper::append_string_view(msg.source.filename, dest); + dest.push_back(':'); + fmt_helper::append_int(msg.source.line, dest); + } +}; + +// print source filename +template +class source_filename_formatter final : public flag_formatter { +public: + explicit source_filename_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + if (msg.source.empty()) { + ScopedPadder p(0, padinfo_, dest); + return; + } + size_t text_size = + padinfo_.enabled() ? std::char_traits::length(msg.source.filename) : 0; + ScopedPadder p(text_size, padinfo_, dest); + fmt_helper::append_string_view(msg.source.filename, dest); + } +}; + +template +class short_filename_formatter final : public flag_formatter { +public: + explicit short_filename_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + +#ifdef _MSC_VER + #pragma warning(push) + #pragma warning(disable : 4127) // consider using 'if constexpr' instead +#endif // _MSC_VER + static const char *basename(const char *filename) { + // if the size is 2 (1 character + null terminator) we can use the more efficient strrchr + // the branch will be elided by optimizations + if (sizeof(os::folder_seps) == 2) { + const char *rv = std::strrchr(filename, os::folder_seps[0]); + return rv != nullptr ? rv + 1 : filename; + } else { + const std::reverse_iterator begin(filename + std::strlen(filename)); + const std::reverse_iterator end(filename); + + const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps), + std::end(os::folder_seps) - 1); + return it != end ? it.base() : filename; + } + } +#ifdef _MSC_VER + #pragma warning(pop) +#endif // _MSC_VER + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + if (msg.source.empty()) { + ScopedPadder p(0, padinfo_, dest); + return; + } + auto filename = basename(msg.source.filename); + size_t text_size = padinfo_.enabled() ? std::char_traits::length(filename) : 0; + ScopedPadder p(text_size, padinfo_, dest); + fmt_helper::append_string_view(filename, dest); + } +}; + +template +class source_linenum_formatter final : public flag_formatter { +public: + explicit source_linenum_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + if (msg.source.empty()) { + ScopedPadder p(0, padinfo_, dest); + return; + } + + auto field_size = ScopedPadder::count_digits(msg.source.line); + ScopedPadder p(field_size, padinfo_, dest); + fmt_helper::append_int(msg.source.line, dest); + } +}; + +// print source funcname +template +class source_funcname_formatter final : public flag_formatter { +public: + explicit source_funcname_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + if (msg.source.empty()) { + ScopedPadder p(0, padinfo_, dest); + return; + } + size_t text_size = + padinfo_.enabled() ? std::char_traits::length(msg.source.funcname) : 0; + ScopedPadder p(text_size, padinfo_, dest); + fmt_helper::append_string_view(msg.source.funcname, dest); + } +}; + +// print elapsed time since last message +template +class elapsed_formatter final : public flag_formatter { +public: + using DurationUnits = Units; + + explicit elapsed_formatter(padding_info padinfo) + : flag_formatter(padinfo), + last_message_time_(log_clock::now()) {} + + void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { + auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero()); + auto delta_units = std::chrono::duration_cast(delta); + last_message_time_ = msg.time; + auto delta_count = static_cast(delta_units.count()); + auto n_digits = static_cast(ScopedPadder::count_digits(delta_count)); + ScopedPadder p(n_digits, padinfo_, dest); + fmt_helper::append_int(delta_count, dest); + } + +private: + log_clock::time_point last_message_time_; +}; + +// Class for formatting Mapped Diagnostic Context (MDC) in log messages. +// Example: [logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message +#ifndef SPDLOG_NO_TLS +template +class mdc_formatter : public flag_formatter { +public: + explicit mdc_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { + auto &mdc_map = mdc::get_context(); + if (mdc_map.empty()) { + ScopedPadder p(0, padinfo_, dest); + return; + } else { + format_mdc(mdc_map, dest); + } + } + + void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest) { + auto last_element = --mdc_map.end(); + for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) { + auto &pair = *it; + const auto &key = pair.first; + const auto &value = pair.second; + size_t content_size = key.size() + value.size() + 1; // 1 for ':' + + if (it != last_element) { + content_size++; // 1 for ' ' + } + + ScopedPadder p(content_size, padinfo_, dest); + fmt_helper::append_string_view(key, dest); + fmt_helper::append_string_view(":", dest); + fmt_helper::append_string_view(value, dest); + if (it != last_element) { + fmt_helper::append_string_view(" ", dest); + } + } + } +}; +#endif + +// Full info formatter +// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v +class full_formatter final : public flag_formatter { +public: + explicit full_formatter(padding_info padinfo) + : flag_formatter(padinfo) {} + + void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override { + using std::chrono::duration_cast; + using std::chrono::milliseconds; + using std::chrono::seconds; + + // cache the date/time part for the next second. + auto duration = msg.time.time_since_epoch(); + auto secs = duration_cast(duration); + + if (cache_timestamp_ != secs || cached_datetime_.size() == 0) { + cached_datetime_.clear(); + cached_datetime_.push_back('['); + fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_); + cached_datetime_.push_back('-'); + + fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_); + cached_datetime_.push_back('-'); + + fmt_helper::pad2(tm_time.tm_mday, cached_datetime_); + cached_datetime_.push_back(' '); + + fmt_helper::pad2(tm_time.tm_hour, cached_datetime_); + cached_datetime_.push_back(':'); + + fmt_helper::pad2(tm_time.tm_min, cached_datetime_); + cached_datetime_.push_back(':'); + + fmt_helper::pad2(tm_time.tm_sec, cached_datetime_); + cached_datetime_.push_back('.'); + + cache_timestamp_ = secs; + } + dest.append(cached_datetime_.begin(), cached_datetime_.end()); + + auto millis = fmt_helper::time_fraction(msg.time); + fmt_helper::pad3(static_cast(millis.count()), dest); + dest.push_back(']'); + dest.push_back(' '); + + // append logger name if exists + if (msg.logger_name.size() > 0) { + dest.push_back('['); + fmt_helper::append_string_view(msg.logger_name, dest); + dest.push_back(']'); + dest.push_back(' '); + } + + dest.push_back('['); + // wrap the level name with color + msg.color_range_start = dest.size(); + // fmt_helper::append_string_view(level::to_c_str(msg.level), dest); + fmt_helper::append_string_view(level::to_string_view(msg.level), dest); + msg.color_range_end = dest.size(); + dest.push_back(']'); + dest.push_back(' '); + + // add source location if present + if (!msg.source.empty()) { + dest.push_back('['); + const char *filename = + details::short_filename_formatter::basename( + msg.source.filename); + fmt_helper::append_string_view(filename, dest); + dest.push_back(':'); + fmt_helper::append_int(msg.source.line, dest); + dest.push_back(']'); + dest.push_back(' '); + } + +#ifndef SPDLOG_NO_TLS + // add mdc if present + auto &mdc_map = mdc::get_context(); + if (!mdc_map.empty()) { + dest.push_back('['); + mdc_formatter_.format_mdc(mdc_map, dest); + dest.push_back(']'); + dest.push_back(' '); + } +#endif + // fmt_helper::append_string_view(msg.msg(), dest); + fmt_helper::append_string_view(msg.payload, dest); + } + +private: + std::chrono::seconds cache_timestamp_{0}; + memory_buf_t cached_datetime_; + +#ifndef SPDLOG_NO_TLS + mdc_formatter mdc_formatter_{padding_info{}}; +#endif + +}; + +} // namespace details + +SPDLOG_INLINE pattern_formatter::pattern_formatter(std::string pattern, + pattern_time_type time_type, + std::string eol, + custom_flags custom_user_flags) + : pattern_(std::move(pattern)), + eol_(std::move(eol)), + pattern_time_type_(time_type), + need_localtime_(false), + last_log_secs_(0), + custom_handlers_(std::move(custom_user_flags)) { + std::memset(&cached_tm_, 0, sizeof(cached_tm_)); + compile_pattern_(pattern_); +} + +// use by default full formatter for if pattern is not given +SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eol) + : pattern_("%+"), + eol_(std::move(eol)), + pattern_time_type_(time_type), + need_localtime_(true), + last_log_secs_(0) { + std::memset(&cached_tm_, 0, sizeof(cached_tm_)); + formatters_.push_back(details::make_unique(details::padding_info{})); +} + +SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const { + custom_flags cloned_custom_formatters; + for (auto &it : custom_handlers_) { + cloned_custom_formatters[it.first] = it.second->clone(); + } + auto cloned = details::make_unique(pattern_, pattern_time_type_, eol_, + std::move(cloned_custom_formatters)); + cloned->need_localtime(need_localtime_); +#if defined(__GNUC__) && __GNUC__ < 5 + return std::move(cloned); +#else + return cloned; +#endif +} + +SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) { + if (need_localtime_) { + const auto secs = + std::chrono::duration_cast(msg.time.time_since_epoch()); + if (secs != last_log_secs_) { + cached_tm_ = get_time_(msg); + last_log_secs_ = secs; + } + } + + for (auto &f : formatters_) { + f->format(msg, cached_tm_, dest); + } + // write eol + details::fmt_helper::append_string_view(eol_, dest); +} + +SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern) { + pattern_ = std::move(pattern); + need_localtime_ = false; + compile_pattern_(pattern_); +} + +SPDLOG_INLINE void pattern_formatter::need_localtime(bool need) { need_localtime_ = need; } + +SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg) { + if (pattern_time_type_ == pattern_time_type::local) { + return details::os::localtime(log_clock::to_time_t(msg.time)); + } + return details::os::gmtime(log_clock::to_time_t(msg.time)); +} + +template +SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding) { + // process custom flags + auto it = custom_handlers_.find(flag); + if (it != custom_handlers_.end()) { + auto custom_handler = it->second->clone(); + custom_handler->set_padding_info(padding); + formatters_.push_back(std::move(custom_handler)); + return; + } + + // process built-in flags + switch (flag) { + case ('+'): // default formatter + formatters_.push_back(details::make_unique(padding)); + need_localtime_ = true; + break; + + case 'n': // logger name + formatters_.push_back(details::make_unique>(padding)); + break; + + case 'l': // level + formatters_.push_back(details::make_unique>(padding)); + break; + + case 'L': // short level + formatters_.push_back( + details::make_unique>(padding)); + break; + + case ('t'): // thread id + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('v'): // the message text + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('a'): // weekday + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('A'): // short weekday + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('b'): + case ('h'): // month + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('B'): // short month + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('c'): // datetime + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('C'): // year 2 digits + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('Y'): // year 4 digits + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('D'): + case ('x'): // datetime MM/DD/YY + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('m'): // month 1-12 + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('d'): // day of month 1-31 + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('H'): // hours 24 + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('I'): // hours 12 + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('M'): // minutes + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('S'): // seconds + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('e'): // milliseconds + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('f'): // microseconds + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('F'): // nanoseconds + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('E'): // seconds since epoch + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('p'): // am/pm + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('r'): // 12 hour clock 02:55:02 pm + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('R'): // 24-hour HH:MM time + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('T'): + case ('X'): // ISO 8601 time format (HH:MM:SS) + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('z'): // timezone + formatters_.push_back(details::make_unique>(padding)); + need_localtime_ = true; + break; + + case ('P'): // pid + formatters_.push_back(details::make_unique>(padding)); + break; + + case ('^'): // color range start + formatters_.push_back(details::make_unique(padding)); + break; + + case ('$'): // color range end + formatters_.push_back(details::make_unique(padding)); + break; + + case ('@'): // source location (filename:filenumber) + formatters_.push_back( + details::make_unique>(padding)); + break; + + case ('s'): // short source filename - without directory name + formatters_.push_back( + details::make_unique>(padding)); + break; + + case ('g'): // full source filename + formatters_.push_back( + details::make_unique>(padding)); + break; + + case ('#'): // source line number + formatters_.push_back( + details::make_unique>(padding)); + break; + + case ('!'): // source funcname + formatters_.push_back( + details::make_unique>(padding)); + break; + + case ('%'): // % char + formatters_.push_back(details::make_unique('%')); + break; + + case ('u'): // elapsed time since last log message in nanos + formatters_.push_back( + details::make_unique>( + padding)); + break; + + case ('i'): // elapsed time since last log message in micros + formatters_.push_back( + details::make_unique>( + padding)); + break; + + case ('o'): // elapsed time since last log message in millis + formatters_.push_back( + details::make_unique>( + padding)); + break; + + case ('O'): // elapsed time since last log message in seconds + formatters_.push_back( + details::make_unique>( + padding)); + break; + +#ifndef SPDLOG_NO_TLS // mdc formatter requires TLS support + case ('&'): + formatters_.push_back(details::make_unique>(padding)); + break; +#endif + + default: // Unknown flag appears as is + auto unknown_flag = details::make_unique(); + + if (!padding.truncate_) { + unknown_flag->add_ch('%'); + unknown_flag->add_ch(flag); + formatters_.push_back((std::move(unknown_flag))); + } + // fix issue #1617 (prev char was '!' and should have been treated as funcname flag + // instead of truncating flag) spdlog::set_pattern("[%10!] %v") => "[ main] some + // message" spdlog::set_pattern("[%3!!] %v") => "[mai] some message" + else { + padding.truncate_ = false; + formatters_.push_back( + details::make_unique>(padding)); + unknown_flag->add_ch(flag); + formatters_.push_back((std::move(unknown_flag))); + } + + break; + } +} + +// Extract given pad spec (e.g. %8X, %=8X, %-8!X, %8!X, %=8!X, %-8!X, %+8!X) +// Advance the given it pass the end of the padding spec found (if any) +// Return padding. +SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_( + std::string::const_iterator &it, std::string::const_iterator end) { + using details::padding_info; + using details::scoped_padder; + const size_t max_width = 64; + if (it == end) { + return padding_info{}; + } + + padding_info::pad_side side; + switch (*it) { + case '-': + side = padding_info::pad_side::right; + ++it; + break; + case '=': + side = padding_info::pad_side::center; + ++it; + break; + default: + side = details::padding_info::pad_side::left; + break; + } + + if (it == end || !std::isdigit(static_cast(*it))) { + return padding_info{}; // no padding if no digit found here + } + + auto width = static_cast(*it) - '0'; + for (++it; it != end && std::isdigit(static_cast(*it)); ++it) { + auto digit = static_cast(*it) - '0'; + width = width * 10 + digit; + } + + // search for the optional truncate marker '!' + bool truncate; + if (it != end && *it == '!') { + truncate = true; + ++it; + } else { + truncate = false; + } + return details::padding_info{std::min(width, max_width), side, truncate}; +} + +SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &pattern) { + auto end = pattern.end(); + std::unique_ptr user_chars; + formatters_.clear(); + for (auto it = pattern.begin(); it != end; ++it) { + if (*it == '%') { + if (user_chars) // append user chars found so far + { + formatters_.push_back(std::move(user_chars)); + } + + auto padding = handle_padspec_(++it, end); + + if (it != end) { + if (padding.enabled()) { + handle_flag_(*it, padding); + } else { + handle_flag_(*it, padding); + } + } else { + break; + } + } else // chars not following the % sign should be displayed as is + { + if (!user_chars) { + user_chars = details::make_unique(); + } + user_chars->add_ch(*it); + } + } + if (user_chars) // append raw chars found so far + { + formatters_.push_back(std::move(user_chars)); + } +} +} // namespace spdlog diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h new file mode 100644 index 0000000..ececd67 --- /dev/null +++ b/include/spdlog/pattern_formatter.h @@ -0,0 +1,118 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +namespace spdlog { +namespace details { + +// padding information. +struct padding_info { + enum class pad_side { left, right, center }; + + padding_info() = default; + padding_info(size_t width, padding_info::pad_side side, bool truncate) + : width_(width), + side_(side), + truncate_(truncate), + enabled_(true) {} + + bool enabled() const { return enabled_; } + size_t width_ = 0; + pad_side side_ = pad_side::left; + bool truncate_ = false; + bool enabled_ = false; +}; + +class SPDLOG_API flag_formatter { +public: + explicit flag_formatter(padding_info padinfo) + : padinfo_(padinfo) {} + flag_formatter() = default; + virtual ~flag_formatter() = default; + virtual void format(const details::log_msg &msg, + const std::tm &tm_time, + memory_buf_t &dest) = 0; + +protected: + padding_info padinfo_; +}; + +} // namespace details + +class SPDLOG_API custom_flag_formatter : public details::flag_formatter { +public: + virtual std::unique_ptr clone() const = 0; + + void set_padding_info(const details::padding_info &padding) { + flag_formatter::padinfo_ = padding; + } +}; + +class SPDLOG_API pattern_formatter final : public formatter { +public: + using custom_flags = std::unordered_map>; + + explicit pattern_formatter(std::string pattern, + pattern_time_type time_type = pattern_time_type::local, + std::string eol = spdlog::details::os::default_eol, + custom_flags custom_user_flags = custom_flags()); + + // use default pattern is not given + explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, + std::string eol = spdlog::details::os::default_eol); + + pattern_formatter(const pattern_formatter &other) = delete; + pattern_formatter &operator=(const pattern_formatter &other) = delete; + + std::unique_ptr clone() const override; + void format(const details::log_msg &msg, memory_buf_t &dest) override; + + template + pattern_formatter &add_flag(char flag, Args &&...args) { + custom_handlers_[flag] = details::make_unique(std::forward(args)...); + return *this; + } + void set_pattern(std::string pattern); + void need_localtime(bool need = true); + +private: + std::string pattern_; + std::string eol_; + pattern_time_type pattern_time_type_; + bool need_localtime_; + std::tm cached_tm_; + std::chrono::seconds last_log_secs_; + std::vector> formatters_; + custom_flags custom_handlers_; + + std::tm get_time_(const details::log_msg &msg); + template + void handle_flag_(char flag, details::padding_info padding); + + // Extract given pad spec (e.g. %8X) + // Advance the given it pass the end of the padding spec found (if any) + // Return padding. + static details::padding_info handle_padspec_(std::string::const_iterator &it, + std::string::const_iterator end); + + void compile_pattern_(const std::string &pattern); +}; +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "pattern_formatter-inl.h" +#endif diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h new file mode 100644 index 0000000..4435a56 --- /dev/null +++ b/include/spdlog/sinks/android_sink.h @@ -0,0 +1,137 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifdef __ANDROID__ + + #include + #include + #include + #include + #include + + #include + #include + #include + #include + #include + #include + + #if !defined(SPDLOG_ANDROID_RETRIES) + #define SPDLOG_ANDROID_RETRIES 2 + #endif + +namespace spdlog { +namespace sinks { + +/* + * Android sink + * (logging using __android_log_write or __android_log_buf_write depending on the specified + * BufferID) + */ +template +class android_sink final : public base_sink { +public: + explicit android_sink(std::string tag = "spdlog", bool use_raw_msg = false) + : tag_(std::move(tag)), + use_raw_msg_(use_raw_msg) {} + +protected: + void sink_it_(const details::log_msg &msg) override { + const android_LogPriority priority = convert_to_android_(msg.level); + memory_buf_t formatted; + if (use_raw_msg_) { + details::fmt_helper::append_string_view(msg.payload, formatted); + } else { + base_sink::formatter_->format(msg, formatted); + } + formatted.push_back('\0'); + const char *msg_output = formatted.data(); + + // See system/core/liblog/logger_write.c for explanation of return value + int ret = android_log(priority, tag_.c_str(), msg_output); + if (ret == -EPERM) { + return; // !__android_log_is_loggable + } + int retry_count = 0; + while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) { + details::os::sleep_for_millis(5); + ret = android_log(priority, tag_.c_str(), msg_output); + retry_count++; + } + + if (ret < 0) { + throw_spdlog_ex("logging to Android failed", ret); + } + } + + void flush_() override {} + +private: + // There might be liblog versions used, that do not support __android_log_buf_write. So we only + // compile and link against + // __android_log_buf_write, if user explicitly provides a non-default log buffer. Otherwise, + // when using the default log buffer, always log via __android_log_write. + template + typename std::enable_if(log_id::LOG_ID_MAIN), int>::type android_log( + int prio, const char *tag, const char *text) { + return __android_log_write(prio, tag, text); + } + + template + typename std::enable_if(log_id::LOG_ID_MAIN), int>::type android_log( + int prio, const char *tag, const char *text) { + return __android_log_buf_write(ID, prio, tag, text); + } + + static android_LogPriority convert_to_android_(spdlog::level::level_enum level) { + switch (level) { + case spdlog::level::trace: + return ANDROID_LOG_VERBOSE; + case spdlog::level::debug: + return ANDROID_LOG_DEBUG; + case spdlog::level::info: + return ANDROID_LOG_INFO; + case spdlog::level::warn: + return ANDROID_LOG_WARN; + case spdlog::level::err: + return ANDROID_LOG_ERROR; + case spdlog::level::critical: + return ANDROID_LOG_FATAL; + default: + return ANDROID_LOG_DEFAULT; + } + } + + std::string tag_; + bool use_raw_msg_; +}; + +using android_sink_mt = android_sink; +using android_sink_st = android_sink; + +template +using android_sink_buf_mt = android_sink; +template +using android_sink_buf_st = android_sink; + +} // namespace sinks + +// Create and register android syslog logger + +template +inline std::shared_ptr android_logger_mt(const std::string &logger_name, + const std::string &tag = "spdlog") { + return Factory::template create(logger_name, tag); +} + +template +inline std::shared_ptr android_logger_st(const std::string &logger_name, + const std::string &tag = "spdlog") { + return Factory::template create(logger_name, tag); +} + +} // namespace spdlog + +#endif // __ANDROID__ diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h new file mode 100644 index 0000000..8fd3078 --- /dev/null +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -0,0 +1,135 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +namespace spdlog { +namespace sinks { + +template +SPDLOG_INLINE ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) + : target_file_(target_file), + mutex_(ConsoleMutex::mutex()), + formatter_(details::make_unique()) + +{ + set_color_mode(mode); + colors_.at(level::trace) = to_string_(white); + colors_.at(level::debug) = to_string_(cyan); + colors_.at(level::info) = to_string_(green); + colors_.at(level::warn) = to_string_(yellow_bold); + colors_.at(level::err) = to_string_(red_bold); + colors_.at(level::critical) = to_string_(bold_on_red); + colors_.at(level::off) = to_string_(reset); +} + +template +SPDLOG_INLINE void ansicolor_sink::set_color(level::level_enum color_level, + string_view_t color) { + std::lock_guard lock(mutex_); + colors_.at(static_cast(color_level)) = to_string_(color); +} + +template +SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg) { + // Wrap the originally formatted message in color codes. + // If color is not supported in the terminal, log as is instead. + std::lock_guard lock(mutex_); + msg.color_range_start = 0; + msg.color_range_end = 0; + memory_buf_t formatted; + formatter_->format(msg, formatted); + if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { + // before color range + print_range_(formatted, 0, msg.color_range_start); + // in color range + print_ccode_(colors_.at(static_cast(msg.level))); + print_range_(formatted, msg.color_range_start, msg.color_range_end); + print_ccode_(reset); + // after color range + print_range_(formatted, msg.color_range_end, formatted.size()); + } else // no color + { + print_range_(formatted, 0, formatted.size()); + } + fflush(target_file_); +} + +template +SPDLOG_INLINE void ansicolor_sink::flush() { + std::lock_guard lock(mutex_); + fflush(target_file_); +} + +template +SPDLOG_INLINE void ansicolor_sink::set_pattern(const std::string &pattern) { + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); +} + +template +SPDLOG_INLINE void ansicolor_sink::set_formatter( + std::unique_ptr sink_formatter) { + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); +} + +template +SPDLOG_INLINE bool ansicolor_sink::should_color() { + return should_do_colors_; +} + +template +SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) { + switch (mode) { + case color_mode::always: + should_do_colors_ = true; + return; + case color_mode::automatic: + should_do_colors_ = + details::os::in_terminal(target_file_) && details::os::is_color_terminal(); + return; + case color_mode::never: + should_do_colors_ = false; + return; + default: + should_do_colors_ = false; + } +} + +template +SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_t &color_code) { + details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_); +} + +template +SPDLOG_INLINE void ansicolor_sink::print_range_(const memory_buf_t &formatted, + size_t start, + size_t end) { + details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_); +} + +template +SPDLOG_INLINE std::string ansicolor_sink::to_string_(const string_view_t &sv) { + return std::string(sv.data(), sv.size()); +} + +// ansicolor_stdout_sink +template +SPDLOG_INLINE ansicolor_stdout_sink::ansicolor_stdout_sink(color_mode mode) + : ansicolor_sink(stdout, mode) {} + +// ansicolor_stderr_sink +template +SPDLOG_INLINE ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) + : ansicolor_sink(stderr, mode) {} + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h new file mode 100644 index 0000000..d0dadd7 --- /dev/null +++ b/include/spdlog/sinks/ansicolor_sink.h @@ -0,0 +1,115 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace spdlog { +namespace sinks { + +/** + * This sink prefixes the output with an ANSI escape sequence color code + * depending on the severity + * of the message. + * If no color terminal detected, omit the escape codes. + */ + +template +class ansicolor_sink : public sink { +public: + using mutex_t = typename ConsoleMutex::mutex_t; + ansicolor_sink(FILE *target_file, color_mode mode); + ~ansicolor_sink() override = default; + + ansicolor_sink(const ansicolor_sink &other) = delete; + ansicolor_sink(ansicolor_sink &&other) = delete; + + ansicolor_sink &operator=(const ansicolor_sink &other) = delete; + ansicolor_sink &operator=(ansicolor_sink &&other) = delete; + + void set_color(level::level_enum color_level, string_view_t color); + void set_color_mode(color_mode mode); + bool should_color(); + + void log(const details::log_msg &msg) override; + void flush() override; + void set_pattern(const std::string &pattern) final override; + void set_formatter(std::unique_ptr sink_formatter) override; + + // Formatting codes + const string_view_t reset = "\033[m"; + const string_view_t bold = "\033[1m"; + const string_view_t dark = "\033[2m"; + const string_view_t underline = "\033[4m"; + const string_view_t blink = "\033[5m"; + const string_view_t reverse = "\033[7m"; + const string_view_t concealed = "\033[8m"; + const string_view_t clear_line = "\033[K"; + + // Foreground colors + const string_view_t black = "\033[30m"; + const string_view_t red = "\033[31m"; + const string_view_t green = "\033[32m"; + const string_view_t yellow = "\033[33m"; + const string_view_t blue = "\033[34m"; + const string_view_t magenta = "\033[35m"; + const string_view_t cyan = "\033[36m"; + const string_view_t white = "\033[37m"; + + /// Background colors + const string_view_t on_black = "\033[40m"; + const string_view_t on_red = "\033[41m"; + const string_view_t on_green = "\033[42m"; + const string_view_t on_yellow = "\033[43m"; + const string_view_t on_blue = "\033[44m"; + const string_view_t on_magenta = "\033[45m"; + const string_view_t on_cyan = "\033[46m"; + const string_view_t on_white = "\033[47m"; + + /// Bold colors + const string_view_t yellow_bold = "\033[33m\033[1m"; + const string_view_t red_bold = "\033[31m\033[1m"; + const string_view_t bold_on_red = "\033[1m\033[41m"; + +private: + FILE *target_file_; + mutex_t &mutex_; + bool should_do_colors_; + std::unique_ptr formatter_; + std::array colors_; + void print_ccode_(const string_view_t &color_code); + void print_range_(const memory_buf_t &formatted, size_t start, size_t end); + static std::string to_string_(const string_view_t &sv); +}; + +template +class ansicolor_stdout_sink : public ansicolor_sink { +public: + explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic); +}; + +template +class ansicolor_stderr_sink : public ansicolor_sink { +public: + explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic); +}; + +using ansicolor_stdout_sink_mt = ansicolor_stdout_sink; +using ansicolor_stdout_sink_st = ansicolor_stdout_sink; + +using ansicolor_stderr_sink_mt = ansicolor_stderr_sink; +using ansicolor_stderr_sink_st = ansicolor_stderr_sink; + +} // namespace sinks +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "ansicolor_sink-inl.h" +#endif diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h new file mode 100644 index 0000000..ada161b --- /dev/null +++ b/include/spdlog/sinks/base_sink-inl.h @@ -0,0 +1,59 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +#include +#include + +template +SPDLOG_INLINE spdlog::sinks::base_sink::base_sink() + : formatter_{details::make_unique()} {} + +template +SPDLOG_INLINE spdlog::sinks::base_sink::base_sink( + std::unique_ptr formatter) + : formatter_{std::move(formatter)} {} + +template +void SPDLOG_INLINE spdlog::sinks::base_sink::log(const details::log_msg &msg) { + std::lock_guard lock(mutex_); + sink_it_(msg); +} + +template +void SPDLOG_INLINE spdlog::sinks::base_sink::flush() { + std::lock_guard lock(mutex_); + flush_(); +} + +template +void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern(const std::string &pattern) { + std::lock_guard lock(mutex_); + set_pattern_(pattern); +} + +template +void SPDLOG_INLINE +spdlog::sinks::base_sink::set_formatter(std::unique_ptr sink_formatter) { + std::lock_guard lock(mutex_); + set_formatter_(std::move(sink_formatter)); +} + +template +void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) { + set_formatter_(details::make_unique(pattern)); +} + +template +void SPDLOG_INLINE +spdlog::sinks::base_sink::set_formatter_(std::unique_ptr sink_formatter) { + formatter_ = std::move(sink_formatter); +} diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h new file mode 100644 index 0000000..1b4bb06 --- /dev/null +++ b/include/spdlog/sinks/base_sink.h @@ -0,0 +1,51 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once +// +// base sink templated over a mutex (either dummy or real) +// concrete implementation should override the sink_it_() and flush_() methods. +// locking is taken care of in this class - no locking needed by the +// implementers.. +// + +#include +#include +#include + +namespace spdlog { +namespace sinks { +template +class SPDLOG_API base_sink : public sink { +public: + base_sink(); + explicit base_sink(std::unique_ptr formatter); + ~base_sink() override = default; + + base_sink(const base_sink &) = delete; + base_sink(base_sink &&) = delete; + + base_sink &operator=(const base_sink &) = delete; + base_sink &operator=(base_sink &&) = delete; + + void log(const details::log_msg &msg) final override; + void flush() final override; + void set_pattern(const std::string &pattern) final override; + void set_formatter(std::unique_ptr sink_formatter) final override; + +protected: + // sink formatter + std::unique_ptr formatter_; + Mutex mutex_; + + virtual void sink_it_(const details::log_msg &msg) = 0; + virtual void flush_() = 0; + virtual void set_pattern_(const std::string &pattern); + virtual void set_formatter_(std::unique_ptr sink_formatter); +}; +} // namespace sinks +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "base_sink-inl.h" +#endif diff --git a/include/spdlog/sinks/basic_file_sink-inl.h b/include/spdlog/sinks/basic_file_sink-inl.h new file mode 100644 index 0000000..ce0ddad --- /dev/null +++ b/include/spdlog/sinks/basic_file_sink-inl.h @@ -0,0 +1,48 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +namespace spdlog { +namespace sinks { + +template +SPDLOG_INLINE basic_file_sink::basic_file_sink(const filename_t &filename, + bool truncate, + const file_event_handlers &event_handlers) + : file_helper_{event_handlers} { + file_helper_.open(filename, truncate); +} + +template +SPDLOG_INLINE const filename_t &basic_file_sink::filename() const { + return file_helper_.filename(); +} + +template +SPDLOG_INLINE void basic_file_sink::truncate() { + std::lock_guard lock(base_sink::mutex_); + file_helper_.reopen(true); +} + +template +SPDLOG_INLINE void basic_file_sink::sink_it_(const details::log_msg &msg) { + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + file_helper_.write(formatted); +} + +template +SPDLOG_INLINE void basic_file_sink::flush_() { + file_helper_.flush(); +} + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h new file mode 100644 index 0000000..48c0767 --- /dev/null +++ b/include/spdlog/sinks/basic_file_sink.h @@ -0,0 +1,66 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include + +#include +#include + +namespace spdlog { +namespace sinks { +/* + * Trivial file sink with single file as target + */ +template +class basic_file_sink final : public base_sink { +public: + explicit basic_file_sink(const filename_t &filename, + bool truncate = false, + const file_event_handlers &event_handlers = {}); + const filename_t &filename() const; + void truncate(); + +protected: + void sink_it_(const details::log_msg &msg) override; + void flush_() override; + +private: + details::file_helper file_helper_; +}; + +using basic_file_sink_mt = basic_file_sink; +using basic_file_sink_st = basic_file_sink; + +} // namespace sinks + +// +// factory functions +// +template +inline std::shared_ptr basic_logger_mt(const std::string &logger_name, + const filename_t &filename, + bool truncate = false, + const file_event_handlers &event_handlers = {}) { + return Factory::template create(logger_name, filename, truncate, + event_handlers); +} + +template +inline std::shared_ptr basic_logger_st(const std::string &logger_name, + const filename_t &filename, + bool truncate = false, + const file_event_handlers &event_handlers = {}) { + return Factory::template create(logger_name, filename, truncate, + event_handlers); +} + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "basic_file_sink-inl.h" +#endif diff --git a/include/spdlog/sinks/callback_sink.h b/include/spdlog/sinks/callback_sink.h new file mode 100644 index 0000000..5f8b6bc --- /dev/null +++ b/include/spdlog/sinks/callback_sink.h @@ -0,0 +1,56 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include + +#include +#include + +namespace spdlog { + +// callbacks type +typedef std::function custom_log_callback; + +namespace sinks { +/* + * Trivial callback sink, gets a callback function and calls it on each log + */ +template +class callback_sink final : public base_sink { +public: + explicit callback_sink(const custom_log_callback &callback) + : callback_{callback} {} + +protected: + void sink_it_(const details::log_msg &msg) override { callback_(msg); } + void flush_() override{} + +private: + custom_log_callback callback_; +}; + +using callback_sink_mt = callback_sink; +using callback_sink_st = callback_sink; + +} // namespace sinks + +// +// factory functions +// +template +inline std::shared_ptr callback_logger_mt(const std::string &logger_name, + const custom_log_callback &callback) { + return Factory::template create(logger_name, callback); +} + +template +inline std::shared_ptr callback_logger_st(const std::string &logger_name, + const custom_log_callback &callback) { + return Factory::template create(logger_name, callback); +} + +} // namespace spdlog diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h new file mode 100644 index 0000000..615c9f7 --- /dev/null +++ b/include/spdlog/sinks/daily_file_sink.h @@ -0,0 +1,254 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace spdlog { +namespace sinks { + +/* + * Generator of daily log file names in format basename.YYYY-MM-DD.ext + */ +struct daily_filename_calculator { + // Create filename for the form basename.YYYY-MM-DD + static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { + filename_t basename, ext; + std::tie(basename, ext) = details::file_helper::split_by_extension(filename); + return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}")), + basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, + ext); + } +}; + +/* + * Generator of daily log file names with strftime format. + * Usages: + * auto sink = + * std::make_shared("myapp-%Y-%m-%d:%H:%M:%S.log", hour, + * minute);" auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", + * hour, minute)" + * + */ +struct daily_filename_format_calculator { + static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) { +#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) + std::wstringstream stream; +#else + std::stringstream stream; +#endif + stream << std::put_time(&now_tm, file_path.c_str()); + return stream.str(); + } +}; + +/* + * Rotating file sink based on date. + * If truncate != false , the created file will be truncated. + * If max_files > 0, retain only the last max_files and delete previous. + * Note that old log files from previous executions will not be deleted by this class, + * rotation and deletion is only applied while the program is running. + */ +template +class daily_file_sink final : public base_sink { +public: + // create daily file sink which rotates on given time + daily_file_sink(filename_t base_filename, + int rotation_hour, + int rotation_minute, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) + : base_filename_(std::move(base_filename)), + rotation_h_(rotation_hour), + rotation_m_(rotation_minute), + file_helper_{event_handlers}, + truncate_(truncate), + max_files_(max_files), + filenames_q_() { + if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || + rotation_minute > 59) { + throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); + } + + auto now = log_clock::now(); + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + file_helper_.open(filename, truncate_); + rotation_tp_ = next_rotation_tp_(); + + if (max_files_ > 0) { + init_filenames_q_(); + } + } + + filename_t filename() { + std::lock_guard lock(base_sink::mutex_); + return file_helper_.filename(); + } + +protected: + void sink_it_(const details::log_msg &msg) override { + auto time = msg.time; + bool should_rotate = time >= rotation_tp_; + if (should_rotate) { + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); + file_helper_.open(filename, truncate_); + rotation_tp_ = next_rotation_tp_(); + } + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + file_helper_.write(formatted); + + // Do the cleaning only at the end because it might throw on failure. + if (should_rotate && max_files_ > 0) { + delete_old_(); + } + } + + void flush_() override { file_helper_.flush(); } + +private: + void init_filenames_q_() { + using details::os::path_exists; + + filenames_q_ = details::circular_q(static_cast(max_files_)); + std::vector filenames; + auto now = log_clock::now(); + while (filenames.size() < max_files_) { + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + if (!path_exists(filename)) { + break; + } + filenames.emplace_back(filename); + now -= std::chrono::hours(24); + } + for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) { + filenames_q_.push_back(std::move(*iter)); + } + } + + tm now_tm(log_clock::time_point tp) { + time_t tnow = log_clock::to_time_t(tp); + return spdlog::details::os::localtime(tnow); + } + + log_clock::time_point next_rotation_tp_() { + auto now = log_clock::now(); + tm date = now_tm(now); + date.tm_hour = rotation_h_; + date.tm_min = rotation_m_; + date.tm_sec = 0; + auto rotation_time = log_clock::from_time_t(std::mktime(&date)); + if (rotation_time > now) { + return rotation_time; + } + return {rotation_time + std::chrono::hours(24)}; + } + + // Delete the file N rotations ago. + // Throw spdlog_ex on failure to delete the old file. + void delete_old_() { + using details::os::filename_to_str; + using details::os::remove_if_exists; + + filename_t current_file = file_helper_.filename(); + if (filenames_q_.full()) { + auto old_filename = std::move(filenames_q_.front()); + filenames_q_.pop_front(); + bool ok = remove_if_exists(old_filename) == 0; + if (!ok) { + filenames_q_.push_back(std::move(current_file)); + throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), + errno); + } + } + filenames_q_.push_back(std::move(current_file)); + } + + filename_t base_filename_; + int rotation_h_; + int rotation_m_; + log_clock::time_point rotation_tp_; + details::file_helper file_helper_; + bool truncate_; + uint16_t max_files_; + details::circular_q filenames_q_; +}; + +using daily_file_sink_mt = daily_file_sink; +using daily_file_sink_st = daily_file_sink; +using daily_file_format_sink_mt = daily_file_sink; +using daily_file_format_sink_st = + daily_file_sink; + +} // namespace sinks + +// +// factory functions +// +template +inline std::shared_ptr daily_logger_mt(const std::string &logger_name, + const filename_t &filename, + int hour = 0, + int minute = 0, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) { + return Factory::template create(logger_name, filename, hour, minute, + truncate, max_files, event_handlers); +} + +template +inline std::shared_ptr daily_logger_format_mt( + const std::string &logger_name, + const filename_t &filename, + int hour = 0, + int minute = 0, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) { + return Factory::template create( + logger_name, filename, hour, minute, truncate, max_files, event_handlers); +} + +template +inline std::shared_ptr daily_logger_st(const std::string &logger_name, + const filename_t &filename, + int hour = 0, + int minute = 0, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) { + return Factory::template create(logger_name, filename, hour, minute, + truncate, max_files, event_handlers); +} + +template +inline std::shared_ptr daily_logger_format_st( + const std::string &logger_name, + const filename_t &filename, + int hour = 0, + int minute = 0, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) { + return Factory::template create( + logger_name, filename, hour, minute, truncate, max_files, event_handlers); +} +} // namespace spdlog diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h new file mode 100644 index 0000000..69c4971 --- /dev/null +++ b/include/spdlog/sinks/dist_sink.h @@ -0,0 +1,81 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "base_sink.h" +#include +#include +#include + +#include +#include +#include +#include + +// Distribution sink (mux). Stores a vector of sinks which get called when log +// is called + +namespace spdlog { +namespace sinks { + +template +class dist_sink : public base_sink { +public: + dist_sink() = default; + explicit dist_sink(std::vector> sinks) + : sinks_(sinks) {} + + dist_sink(const dist_sink &) = delete; + dist_sink &operator=(const dist_sink &) = delete; + + void add_sink(std::shared_ptr sub_sink) { + std::lock_guard lock(base_sink::mutex_); + sinks_.push_back(sub_sink); + } + + void remove_sink(std::shared_ptr sub_sink) { + std::lock_guard lock(base_sink::mutex_); + sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sub_sink), sinks_.end()); + } + + void set_sinks(std::vector> sinks) { + std::lock_guard lock(base_sink::mutex_); + sinks_ = std::move(sinks); + } + + std::vector> &sinks() { return sinks_; } + +protected: + void sink_it_(const details::log_msg &msg) override { + for (auto &sub_sink : sinks_) { + if (sub_sink->should_log(msg.level)) { + sub_sink->log(msg); + } + } + } + + void flush_() override { + for (auto &sub_sink : sinks_) { + sub_sink->flush(); + } + } + + void set_pattern_(const std::string &pattern) override { + set_formatter_(details::make_unique(pattern)); + } + + void set_formatter_(std::unique_ptr sink_formatter) override { + base_sink::formatter_ = std::move(sink_formatter); + for (auto &sub_sink : sinks_) { + sub_sink->set_formatter(base_sink::formatter_->clone()); + } + } + std::vector> sinks_; +}; + +using dist_sink_mt = dist_sink; +using dist_sink_st = dist_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/dup_filter_sink.h b/include/spdlog/sinks/dup_filter_sink.h new file mode 100644 index 0000000..1498142 --- /dev/null +++ b/include/spdlog/sinks/dup_filter_sink.h @@ -0,0 +1,92 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "dist_sink.h" +#include +#include + +#include +#include +#include +#include + +// Duplicate message removal sink. +// Skip the message if previous one is identical and less than "max_skip_duration" have passed +// +// Example: +// +// #include +// +// int main() { +// auto dup_filter = std::make_shared(std::chrono::seconds(5), +// level::info); dup_filter->add_sink(std::make_shared()); +// spdlog::logger l("logger", dup_filter); +// l.info("Hello"); +// l.info("Hello"); +// l.info("Hello"); +// l.info("Different Hello"); +// } +// +// Will produce: +// [2019-06-25 17:50:56.511] [logger] [info] Hello +// [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages.. +// [2019-06-25 17:50:56.512] [logger] [info] Different Hello + +namespace spdlog { +namespace sinks { +template +class dup_filter_sink : public dist_sink { +public: + template + explicit dup_filter_sink(std::chrono::duration max_skip_duration, + level::level_enum notification_level = level::info) + : max_skip_duration_{max_skip_duration}, + log_level_{notification_level} {} + +protected: + std::chrono::microseconds max_skip_duration_; + log_clock::time_point last_msg_time_; + std::string last_msg_payload_; + size_t skip_counter_ = 0; + level::level_enum log_level_; + + void sink_it_(const details::log_msg &msg) override { + bool filtered = filter_(msg); + if (!filtered) { + skip_counter_ += 1; + return; + } + + // log the "skipped.." message + if (skip_counter_ > 0) { + char buf[64]; + auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", + static_cast(skip_counter_)); + if (msg_size > 0 && static_cast(msg_size) < sizeof(buf)) { + details::log_msg skipped_msg{msg.source, msg.logger_name, log_level_, + string_view_t{buf, static_cast(msg_size)}}; + dist_sink::sink_it_(skipped_msg); + } + } + + // log current message + dist_sink::sink_it_(msg); + last_msg_time_ = msg.time; + skip_counter_ = 0; + last_msg_payload_.assign(msg.payload.data(), msg.payload.data() + msg.payload.size()); + } + + // return whether the log msg should be displayed (true) or skipped (false) + bool filter_(const details::log_msg &msg) { + auto filter_duration = msg.time - last_msg_time_; + return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_); + } +}; + +using dup_filter_sink_mt = dup_filter_sink; +using dup_filter_sink_st = dup_filter_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h new file mode 100644 index 0000000..3e61872 --- /dev/null +++ b/include/spdlog/sinks/hourly_file_sink.h @@ -0,0 +1,193 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace spdlog { +namespace sinks { + +/* + * Generator of Hourly log file names in format basename.YYYY-MM-DD-HH.ext + */ +struct hourly_filename_calculator { + // Create filename for the form basename.YYYY-MM-DD-H + static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { + filename_t basename, ext; + std::tie(basename, ext) = details::file_helper::split_by_extension(filename); + return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}{}"), basename, + now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, + now_tm.tm_hour, ext); + } +}; + +/* + * Rotating file sink based on time. + * If truncate != false , the created file will be truncated. + * If max_files > 0, retain only the last max_files and delete previous. + * Note that old log files from previous executions will not be deleted by this class, + * rotation and deletion is only applied while the program is running. + */ +template +class hourly_file_sink final : public base_sink { +public: + // create hourly file sink which rotates on given time + hourly_file_sink(filename_t base_filename, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) + : base_filename_(std::move(base_filename)), + file_helper_{event_handlers}, + truncate_(truncate), + max_files_(max_files), + filenames_q_() { + auto now = log_clock::now(); + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + file_helper_.open(filename, truncate_); + remove_init_file_ = file_helper_.size() == 0; + rotation_tp_ = next_rotation_tp_(); + + if (max_files_ > 0) { + init_filenames_q_(); + } + } + + filename_t filename() { + std::lock_guard lock(base_sink::mutex_); + return file_helper_.filename(); + } + +protected: + void sink_it_(const details::log_msg &msg) override { + auto time = msg.time; + bool should_rotate = time >= rotation_tp_; + if (should_rotate) { + if (remove_init_file_) { + file_helper_.close(); + details::os::remove(file_helper_.filename()); + } + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); + file_helper_.open(filename, truncate_); + rotation_tp_ = next_rotation_tp_(); + } + remove_init_file_ = false; + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + file_helper_.write(formatted); + + // Do the cleaning only at the end because it might throw on failure. + if (should_rotate && max_files_ > 0) { + delete_old_(); + } + } + + void flush_() override { file_helper_.flush(); } + +private: + void init_filenames_q_() { + using details::os::path_exists; + + filenames_q_ = details::circular_q(static_cast(max_files_)); + std::vector filenames; + auto now = log_clock::now(); + while (filenames.size() < max_files_) { + auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); + if (!path_exists(filename)) { + break; + } + filenames.emplace_back(filename); + now -= std::chrono::hours(1); + } + for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) { + filenames_q_.push_back(std::move(*iter)); + } + } + + tm now_tm(log_clock::time_point tp) { + time_t tnow = log_clock::to_time_t(tp); + return spdlog::details::os::localtime(tnow); + } + + log_clock::time_point next_rotation_tp_() { + auto now = log_clock::now(); + tm date = now_tm(now); + date.tm_min = 0; + date.tm_sec = 0; + auto rotation_time = log_clock::from_time_t(std::mktime(&date)); + if (rotation_time > now) { + return rotation_time; + } + return {rotation_time + std::chrono::hours(1)}; + } + + // Delete the file N rotations ago. + // Throw spdlog_ex on failure to delete the old file. + void delete_old_() { + using details::os::filename_to_str; + using details::os::remove_if_exists; + + filename_t current_file = file_helper_.filename(); + if (filenames_q_.full()) { + auto old_filename = std::move(filenames_q_.front()); + filenames_q_.pop_front(); + bool ok = remove_if_exists(old_filename) == 0; + if (!ok) { + filenames_q_.push_back(std::move(current_file)); + SPDLOG_THROW(spdlog_ex( + "Failed removing hourly file " + filename_to_str(old_filename), errno)); + } + } + filenames_q_.push_back(std::move(current_file)); + } + + filename_t base_filename_; + log_clock::time_point rotation_tp_; + details::file_helper file_helper_; + bool truncate_; + uint16_t max_files_; + details::circular_q filenames_q_; + bool remove_init_file_; +}; + +using hourly_file_sink_mt = hourly_file_sink; +using hourly_file_sink_st = hourly_file_sink; + +} // namespace sinks + +// +// factory functions +// +template +inline std::shared_ptr hourly_logger_mt(const std::string &logger_name, + const filename_t &filename, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) { + return Factory::template create(logger_name, filename, truncate, + max_files, event_handlers); +} + +template +inline std::shared_ptr hourly_logger_st(const std::string &logger_name, + const filename_t &filename, + bool truncate = false, + uint16_t max_files = 0, + const file_event_handlers &event_handlers = {}) { + return Factory::template create(logger_name, filename, truncate, + max_files, event_handlers); +} +} // namespace spdlog diff --git a/include/spdlog/sinks/kafka_sink.h b/include/spdlog/sinks/kafka_sink.h new file mode 100644 index 0000000..91e9878 --- /dev/null +++ b/include/spdlog/sinks/kafka_sink.h @@ -0,0 +1,119 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// +// Custom sink for kafka +// Building and using requires librdkafka library. +// For building librdkafka library check the url below +// https://github.com/confluentinc/librdkafka +// + +#include "spdlog/async.h" +#include "spdlog/details/log_msg.h" +#include "spdlog/details/null_mutex.h" +#include "spdlog/details/synchronous_factory.h" +#include "spdlog/sinks/base_sink.h" +#include +#include + +// kafka header +#include + +namespace spdlog { +namespace sinks { + +struct kafka_sink_config { + std::string server_addr; + std::string produce_topic; + int32_t flush_timeout_ms = 1000; + + kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000) + : server_addr{std::move(addr)}, + produce_topic{std::move(topic)}, + flush_timeout_ms(flush_timeout_ms) {} +}; + +template +class kafka_sink : public base_sink { +public: + kafka_sink(kafka_sink_config config) + : config_{std::move(config)} { + try { + std::string errstr; + conf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL)); + RdKafka::Conf::ConfResult confRes = + conf_->set("bootstrap.servers", config_.server_addr, errstr); + if (confRes != RdKafka::Conf::CONF_OK) { + throw_spdlog_ex( + fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr)); + } + + tconf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC)); + if (tconf_ == nullptr) { + throw_spdlog_ex(fmt_lib::format("create topic config failed")); + } + + producer_.reset(RdKafka::Producer::create(conf_.get(), errstr)); + if (producer_ == nullptr) { + throw_spdlog_ex(fmt_lib::format("create producer failed err:{}", errstr)); + } + topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic, + tconf_.get(), errstr)); + if (topic_ == nullptr) { + throw_spdlog_ex(fmt_lib::format("create topic failed err:{}", errstr)); + } + } catch (const std::exception &e) { + throw_spdlog_ex(fmt_lib::format("error create kafka instance: {}", e.what())); + } + } + + ~kafka_sink() { producer_->flush(config_.flush_timeout_ms); } + +protected: + void sink_it_(const details::log_msg &msg) override { + producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, + (void *)msg.payload.data(), msg.payload.size(), NULL, NULL); + } + + void flush_() override { producer_->flush(config_.flush_timeout_ms); } + +private: + kafka_sink_config config_; + std::unique_ptr producer_ = nullptr; + std::unique_ptr conf_ = nullptr; + std::unique_ptr tconf_ = nullptr; + std::unique_ptr topic_ = nullptr; +}; + +using kafka_sink_mt = kafka_sink; +using kafka_sink_st = kafka_sink; + +} // namespace sinks + +template +inline std::shared_ptr kafka_logger_mt(const std::string &logger_name, + spdlog::sinks::kafka_sink_config config) { + return Factory::template create(logger_name, config); +} + +template +inline std::shared_ptr kafka_logger_st(const std::string &logger_name, + spdlog::sinks::kafka_sink_config config) { + return Factory::template create(logger_name, config); +} + +template +inline std::shared_ptr kafka_logger_async_mt( + std::string logger_name, spdlog::sinks::kafka_sink_config config) { + return Factory::template create(logger_name, config); +} + +template +inline std::shared_ptr kafka_logger_async_st( + std::string logger_name, spdlog::sinks::kafka_sink_config config) { + return Factory::template create(logger_name, config); +} + +} // namespace spdlog diff --git a/include/spdlog/sinks/mongo_sink.h b/include/spdlog/sinks/mongo_sink.h new file mode 100644 index 0000000..c5b38ab --- /dev/null +++ b/include/spdlog/sinks/mongo_sink.h @@ -0,0 +1,108 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// +// Custom sink for mongodb +// Building and using requires mongocxx library. +// For building mongocxx library check the url below +// http://mongocxx.org/mongocxx-v3/installation/ +// + +#include "spdlog/common.h" +#include "spdlog/details/log_msg.h" +#include "spdlog/sinks/base_sink.h" +#include + +#include +#include +#include + +#include +#include +#include + +namespace spdlog { +namespace sinks { +template +class mongo_sink : public base_sink { +public: + mongo_sink(const std::string &db_name, + const std::string &collection_name, + const std::string &uri = "mongodb://localhost:27017") try + : mongo_sink(std::make_shared(), db_name, collection_name, uri) { + } catch (const std::exception &e) { + throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what())); + } + + mongo_sink(std::shared_ptr instance, + const std::string &db_name, + const std::string &collection_name, + const std::string &uri = "mongodb://localhost:27017") + : instance_(std::move(instance)), + db_name_(db_name), + coll_name_(collection_name) { + try { + client_ = spdlog::details::make_unique(mongocxx::uri{uri}); + } catch (const std::exception &e) { + throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what())); + } + } + + ~mongo_sink() { flush_(); } + +protected: + void sink_it_(const details::log_msg &msg) override { + using bsoncxx::builder::stream::document; + using bsoncxx::builder::stream::finalize; + + if (client_ != nullptr) { + auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" + << level::to_string_view(msg.level).data() << "level_num" + << msg.level << "message" + << std::string(msg.payload.begin(), msg.payload.end()) + << "logger_name" + << std::string(msg.logger_name.begin(), msg.logger_name.end()) + << "thread_id" << static_cast(msg.thread_id) << finalize; + client_->database(db_name_).collection(coll_name_).insert_one(doc.view()); + } + } + + void flush_() override {} + +private: + std::shared_ptr instance_; + std::string db_name_; + std::string coll_name_; + std::unique_ptr client_ = nullptr; +}; + +#include "spdlog/details/null_mutex.h" +#include +using mongo_sink_mt = mongo_sink; +using mongo_sink_st = mongo_sink; + +} // namespace sinks + +template +inline std::shared_ptr mongo_logger_mt( + const std::string &logger_name, + const std::string &db_name, + const std::string &collection_name, + const std::string &uri = "mongodb://localhost:27017") { + return Factory::template create(logger_name, db_name, collection_name, + uri); +} + +template +inline std::shared_ptr mongo_logger_st( + const std::string &logger_name, + const std::string &db_name, + const std::string &collection_name, + const std::string &uri = "mongodb://localhost:27017") { + return Factory::template create(logger_name, db_name, collection_name, + uri); +} + +} // namespace spdlog diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h new file mode 100644 index 0000000..c28d6eb --- /dev/null +++ b/include/spdlog/sinks/msvc_sink.h @@ -0,0 +1,68 @@ +// Copyright(c) 2016 Alexander Dalshov & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#if defined(_WIN32) + + #include + #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) + #include + #endif + #include + + #include + #include + + // Avoid including windows.h (https://stackoverflow.com/a/30741042) + #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) +extern "C" __declspec(dllimport) void __stdcall OutputDebugStringW(const wchar_t *lpOutputString); + #else +extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString); + #endif +extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); + +namespace spdlog { +namespace sinks { +/* + * MSVC sink (logging using OutputDebugStringA) + */ +template +class msvc_sink : public base_sink { +public: + msvc_sink() = default; + msvc_sink(bool check_debugger_present) + : check_debugger_present_{check_debugger_present} {} + +protected: + void sink_it_(const details::log_msg &msg) override { + if (check_debugger_present_ && !IsDebuggerPresent()) { + return; + } + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + formatted.push_back('\0'); // add a null terminator for OutputDebugString + #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) + wmemory_buf_t wformatted; + details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted); + OutputDebugStringW(wformatted.data()); + #else + OutputDebugStringA(formatted.data()); + #endif + } + + void flush_() override {} + + bool check_debugger_present_ = true; +}; + +using msvc_sink_mt = msvc_sink; +using msvc_sink_st = msvc_sink; + +using windebug_sink_mt = msvc_sink_mt; +using windebug_sink_st = msvc_sink_st; + +} // namespace sinks +} // namespace spdlog + +#endif diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h new file mode 100644 index 0000000..74530b5 --- /dev/null +++ b/include/spdlog/sinks/null_sink.h @@ -0,0 +1,41 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include + +#include + +namespace spdlog { +namespace sinks { + +template +class null_sink final : public base_sink { +protected: + void sink_it_(const details::log_msg &) override {} + void flush_() override {} +}; + +using null_sink_mt = null_sink; +using null_sink_st = null_sink; + +} // namespace sinks + +template +inline std::shared_ptr null_logger_mt(const std::string &logger_name) { + auto null_logger = Factory::template create(logger_name); + null_logger->set_level(level::off); + return null_logger; +} + +template +inline std::shared_ptr null_logger_st(const std::string &logger_name) { + auto null_logger = Factory::template create(logger_name); + null_logger->set_level(level::off); + return null_logger; +} + +} // namespace spdlog diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h new file mode 100644 index 0000000..6af9dd0 --- /dev/null +++ b/include/spdlog/sinks/ostream_sink.h @@ -0,0 +1,43 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +#include +#include + +namespace spdlog { +namespace sinks { +template +class ostream_sink final : public base_sink { +public: + explicit ostream_sink(std::ostream &os, bool force_flush = false) + : ostream_(os), + force_flush_(force_flush) {} + ostream_sink(const ostream_sink &) = delete; + ostream_sink &operator=(const ostream_sink &) = delete; + +protected: + void sink_it_(const details::log_msg &msg) override { + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + ostream_.write(formatted.data(), static_cast(formatted.size())); + if (force_flush_) { + ostream_.flush(); + } + } + + void flush_() override { ostream_.flush(); } + + std::ostream &ostream_; + bool force_flush_; +}; + +using ostream_sink_mt = ostream_sink; +using ostream_sink_st = ostream_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/qt_sinks.h b/include/spdlog/sinks/qt_sinks.h new file mode 100644 index 0000000..d319e84 --- /dev/null +++ b/include/spdlog/sinks/qt_sinks.h @@ -0,0 +1,304 @@ +// Copyright(c) 2015-present, Gabi Melman, mguludag and spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +// +// Custom sink for QPlainTextEdit or QTextEdit and its children (QTextBrowser... +// etc) Building and using requires Qt library. +// +// Warning: the qt_sink won't be notified if the target widget is destroyed. +// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent +// QObject, and then use a standard signal/slot. +// + +#include "spdlog/common.h" +#include "spdlog/details/log_msg.h" +#include "spdlog/details/synchronous_factory.h" +#include "spdlog/sinks/base_sink.h" +#include + +#include +#include + +// +// qt_sink class +// +namespace spdlog { +namespace sinks { +template +class qt_sink : public base_sink { +public: + qt_sink(QObject *qt_object, std::string meta_method) + : qt_object_(qt_object), + meta_method_(std::move(meta_method)) { + if (!qt_object_) { + throw_spdlog_ex("qt_sink: qt_object is null"); + } + } + + ~qt_sink() { flush_(); } + +protected: + void sink_it_(const details::log_msg &msg) override { + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + const string_view_t str = string_view_t(formatted.data(), formatted.size()); + QMetaObject::invokeMethod( + qt_object_, meta_method_.c_str(), Qt::AutoConnection, + Q_ARG(QString, QString::fromUtf8(str.data(), static_cast(str.size())).trimmed())); + } + + void flush_() override {} + +private: + QObject *qt_object_ = nullptr; + std::string meta_method_; +}; + +// Qt color sink to QTextEdit. +// Color location is determined by the sink log pattern like in the rest of spdlog sinks. +// Colors can be modified if needed using sink->set_color(level, qtTextCharFormat). +// max_lines is the maximum number of lines that the sink will hold before removing the oldest +// lines. By default, only ascii (latin1) is supported by this sink. Set is_utf8 to true if utf8 +// support is needed. +template +class qt_color_sink : public base_sink { +public: + qt_color_sink(QTextEdit *qt_text_edit, + int max_lines, + bool dark_colors = false, + bool is_utf8 = false) + : qt_text_edit_(qt_text_edit), + max_lines_(max_lines), + is_utf8_(is_utf8) { + if (!qt_text_edit_) { + throw_spdlog_ex("qt_color_text_sink: text_edit is null"); + } + + default_color_ = qt_text_edit_->currentCharFormat(); + // set colors + QTextCharFormat format; + // trace + format.setForeground(dark_colors ? Qt::darkGray : Qt::gray); + colors_.at(level::trace) = format; + // debug + format.setForeground(dark_colors ? Qt::darkCyan : Qt::cyan); + colors_.at(level::debug) = format; + // info + format.setForeground(dark_colors ? Qt::darkGreen : Qt::green); + colors_.at(level::info) = format; + // warn + format.setForeground(dark_colors ? Qt::darkYellow : Qt::yellow); + colors_.at(level::warn) = format; + // err + format.setForeground(Qt::red); + colors_.at(level::err) = format; + // critical + format.setForeground(Qt::white); + format.setBackground(Qt::red); + colors_.at(level::critical) = format; + } + + ~qt_color_sink() { flush_(); } + + void set_default_color(QTextCharFormat format) { + // std::lock_guard lock(base_sink::mutex_); + default_color_ = format; + } + + void set_level_color(level::level_enum color_level, QTextCharFormat format) { + // std::lock_guard lock(base_sink::mutex_); + colors_.at(static_cast(color_level)) = format; + } + + QTextCharFormat &get_level_color(level::level_enum color_level) { + std::lock_guard lock(base_sink::mutex_); + return colors_.at(static_cast(color_level)); + } + + QTextCharFormat &get_default_color() { + std::lock_guard lock(base_sink::mutex_); + return default_color_; + } + +protected: + struct invoke_params { + invoke_params(int max_lines, + QTextEdit *q_text_edit, + QString payload, + QTextCharFormat default_color, + QTextCharFormat level_color, + int color_range_start, + int color_range_end) + : max_lines(max_lines), + q_text_edit(q_text_edit), + payload(std::move(payload)), + default_color(default_color), + level_color(level_color), + color_range_start(color_range_start), + color_range_end(color_range_end) {} + int max_lines; + QTextEdit *q_text_edit; + QString payload; + QTextCharFormat default_color; + QTextCharFormat level_color; + int color_range_start; + int color_range_end; + }; + + void sink_it_(const details::log_msg &msg) override { + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + + const string_view_t str = string_view_t(formatted.data(), formatted.size()); + // apply the color to the color range in the formatted message. + QString payload; + int color_range_start = static_cast(msg.color_range_start); + int color_range_end = static_cast(msg.color_range_end); + if (is_utf8_) { + payload = QString::fromUtf8(str.data(), static_cast(str.size())); + // convert color ranges from byte index to character index. + if (msg.color_range_start < msg.color_range_end) { + color_range_start = QString::fromUtf8(str.data(), msg.color_range_start).size(); + color_range_end = QString::fromUtf8(str.data(), msg.color_range_end).size(); + } + } else { + payload = QString::fromLatin1(str.data(), static_cast(str.size())); + } + + invoke_params params{max_lines_, // max lines + qt_text_edit_, // text edit to append to + std::move(payload), // text to append + default_color_, // default color + colors_.at(msg.level), // color to apply + color_range_start, // color range start + color_range_end}; // color range end + + QMetaObject::invokeMethod( + qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection); + } + + void flush_() override {} + + // Add colored text to the text edit widget. This method is invoked in the GUI thread. + // It is a static method to ensure that it is handled correctly even if the sink is destroyed + // prematurely before it is invoked. + + static void invoke_method_(invoke_params params) { + auto *document = params.q_text_edit->document(); + QTextCursor cursor(document); + + // remove first blocks if number of blocks exceeds max_lines + while (document->blockCount() > params.max_lines) { + cursor.select(QTextCursor::BlockUnderCursor); + cursor.removeSelectedText(); + cursor.deleteChar(); // delete the newline after the block + } + + cursor.movePosition(QTextCursor::End); + cursor.setCharFormat(params.default_color); + + // if color range not specified or not not valid, just append the text with default color + if (params.color_range_end <= params.color_range_start) { + cursor.insertText(params.payload); + return; + } + + // insert the text before the color range + cursor.insertText(params.payload.left(params.color_range_start)); + + // insert the colorized text + cursor.setCharFormat(params.level_color); + cursor.insertText(params.payload.mid(params.color_range_start, + params.color_range_end - params.color_range_start)); + + // insert the text after the color range with default format + cursor.setCharFormat(params.default_color); + cursor.insertText(params.payload.mid(params.color_range_end)); + } + + QTextEdit *qt_text_edit_; + int max_lines_; + bool is_utf8_; + QTextCharFormat default_color_; + std::array colors_; +}; + +#include "spdlog/details/null_mutex.h" +#include + +using qt_sink_mt = qt_sink; +using qt_sink_st = qt_sink; +using qt_color_sink_mt = qt_color_sink; +using qt_color_sink_st = qt_color_sink; +} // namespace sinks + +// +// Factory functions +// + +// log to QTextEdit +template +inline std::shared_ptr qt_logger_mt(const std::string &logger_name, + QTextEdit *qt_object, + const std::string &meta_method = "append") { + return Factory::template create(logger_name, qt_object, meta_method); +} + +template +inline std::shared_ptr qt_logger_st(const std::string &logger_name, + QTextEdit *qt_object, + const std::string &meta_method = "append") { + return Factory::template create(logger_name, qt_object, meta_method); +} + +// log to QPlainTextEdit +template +inline std::shared_ptr qt_logger_mt(const std::string &logger_name, + QPlainTextEdit *qt_object, + const std::string &meta_method = "appendPlainText") { + return Factory::template create(logger_name, qt_object, meta_method); +} + +template +inline std::shared_ptr qt_logger_st(const std::string &logger_name, + QPlainTextEdit *qt_object, + const std::string &meta_method = "appendPlainText") { + return Factory::template create(logger_name, qt_object, meta_method); +} +// log to QObject +template +inline std::shared_ptr qt_logger_mt(const std::string &logger_name, + QObject *qt_object, + const std::string &meta_method) { + return Factory::template create(logger_name, qt_object, meta_method); +} + +template +inline std::shared_ptr qt_logger_st(const std::string &logger_name, + QObject *qt_object, + const std::string &meta_method) { + return Factory::template create(logger_name, qt_object, meta_method); +} + +// log to QTextEdit with colorized output +template +inline std::shared_ptr qt_color_logger_mt(const std::string &logger_name, + QTextEdit *qt_text_edit, + int max_lines, + bool is_utf8 = false) { + return Factory::template create(logger_name, qt_text_edit, max_lines, + false, is_utf8); +} + +template +inline std::shared_ptr qt_color_logger_st(const std::string &logger_name, + QTextEdit *qt_text_edit, + int max_lines, + bool is_utf8 = false) { + return Factory::template create(logger_name, qt_text_edit, max_lines, + false, is_utf8); +} + +} // namespace spdlog diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h new file mode 100644 index 0000000..6156c6a --- /dev/null +++ b/include/spdlog/sinks/ringbuffer_sink.h @@ -0,0 +1,67 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include "spdlog/details/circular_q.h" +#include "spdlog/details/log_msg_buffer.h" +#include "spdlog/details/null_mutex.h" +#include "spdlog/sinks/base_sink.h" + +#include +#include +#include + +namespace spdlog { +namespace sinks { +/* + * Ring buffer sink + */ +template +class ringbuffer_sink final : public base_sink { +public: + explicit ringbuffer_sink(size_t n_items) + : q_{n_items} {} + + std::vector last_raw(size_t lim = 0) { + std::lock_guard lock(base_sink::mutex_); + auto items_available = q_.size(); + auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; + std::vector ret; + ret.reserve(n_items); + for (size_t i = (items_available - n_items); i < items_available; i++) { + ret.push_back(q_.at(i)); + } + return ret; + } + + std::vector last_formatted(size_t lim = 0) { + std::lock_guard lock(base_sink::mutex_); + auto items_available = q_.size(); + auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; + std::vector ret; + ret.reserve(n_items); + for (size_t i = (items_available - n_items); i < items_available; i++) { + memory_buf_t formatted; + base_sink::formatter_->format(q_.at(i), formatted); + ret.push_back(SPDLOG_BUF_TO_STRING(formatted)); + } + return ret; + } + +protected: + void sink_it_(const details::log_msg &msg) override { + q_.push_back(details::log_msg_buffer{msg}); + } + void flush_() override {} + +private: + details::circular_q q_; +}; + +using ringbuffer_sink_mt = ringbuffer_sink; +using ringbuffer_sink_st = ringbuffer_sink; + +} // namespace sinks + +} // namespace spdlog diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h new file mode 100644 index 0000000..420bafb --- /dev/null +++ b/include/spdlog/sinks/rotating_file_sink-inl.h @@ -0,0 +1,150 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace spdlog { +namespace sinks { + +template +SPDLOG_INLINE rotating_file_sink::rotating_file_sink( + filename_t base_filename, + std::size_t max_size, + std::size_t max_files, + bool rotate_on_open, + const file_event_handlers &event_handlers) + : base_filename_(std::move(base_filename)), + max_size_(max_size), + max_files_(max_files), + file_helper_{event_handlers} { + if (max_size == 0) { + throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero"); + } + + if (max_files > 200000) { + throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000"); + } + file_helper_.open(calc_filename(base_filename_, 0)); + current_size_ = file_helper_.size(); // expensive. called only once + if (rotate_on_open && current_size_ > 0) { + rotate_(); + current_size_ = 0; + } +} + +// calc filename according to index and file extension if exists. +// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt". +template +SPDLOG_INLINE filename_t rotating_file_sink::calc_filename(const filename_t &filename, + std::size_t index) { + if (index == 0u) { + return filename; + } + + filename_t basename, ext; + std::tie(basename, ext) = details::file_helper::split_by_extension(filename); + return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}.{}{}")), basename, index, ext); +} + +template +SPDLOG_INLINE filename_t rotating_file_sink::filename() { + std::lock_guard lock(base_sink::mutex_); + return file_helper_.filename(); +} + +template +SPDLOG_INLINE void rotating_file_sink::rotate_now() { + std::lock_guard lock(base_sink::mutex_); + rotate_(); +} + +template +SPDLOG_INLINE void rotating_file_sink::sink_it_(const details::log_msg &msg) { + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + auto new_size = current_size_ + formatted.size(); + + // rotate if the new estimated file size exceeds max size. + // rotate only if the real size > 0 to better deal with full disk (see issue #2261). + // we only check the real size when new_size > max_size_ because it is relatively expensive. + if (new_size > max_size_) { + file_helper_.flush(); + if (file_helper_.size() > 0) { + rotate_(); + new_size = formatted.size(); + } + } + file_helper_.write(formatted); + current_size_ = new_size; +} + +template +SPDLOG_INLINE void rotating_file_sink::flush_() { + file_helper_.flush(); +} + +// Rotate files: +// log.txt -> log.1.txt +// log.1.txt -> log.2.txt +// log.2.txt -> log.3.txt +// log.3.txt -> delete +template +SPDLOG_INLINE void rotating_file_sink::rotate_() { + using details::os::filename_to_str; + using details::os::path_exists; + + file_helper_.close(); + for (auto i = max_files_; i > 0; --i) { + filename_t src = calc_filename(base_filename_, i - 1); + if (!path_exists(src)) { + continue; + } + filename_t target = calc_filename(base_filename_, i); + + if (!rename_file_(src, target)) { + // if failed try again after a small delay. + // this is a workaround to a windows issue, where very high rotation + // rates can cause the rename to fail with permission denied (because of antivirus?). + details::os::sleep_for_millis(100); + if (!rename_file_(src, target)) { + file_helper_.reopen( + true); // truncate the log file anyway to prevent it to grow beyond its limit! + current_size_ = 0; + throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + + " to " + filename_to_str(target), + errno); + } + } + } + file_helper_.reopen(true); +} + +// delete the target if exists, and rename the src file to target +// return true on success, false otherwise. +template +SPDLOG_INLINE bool rotating_file_sink::rename_file_(const filename_t &src_filename, + const filename_t &target_filename) { + // try to delete the target file in case it already exists. + (void)details::os::remove(target_filename); + return details::os::rename(src_filename, target_filename) == 0; +} + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h new file mode 100644 index 0000000..42bd376 --- /dev/null +++ b/include/spdlog/sinks/rotating_file_sink.h @@ -0,0 +1,90 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include + +#include +#include +#include + +namespace spdlog { +namespace sinks { + +// +// Rotating file sink based on size +// +template +class rotating_file_sink final : public base_sink { +public: + rotating_file_sink(filename_t base_filename, + std::size_t max_size, + std::size_t max_files, + bool rotate_on_open = false, + const file_event_handlers &event_handlers = {}); + static filename_t calc_filename(const filename_t &filename, std::size_t index); + filename_t filename(); + void rotate_now(); + +protected: + void sink_it_(const details::log_msg &msg) override; + void flush_() override; + +private: + // Rotate files: + // log.txt -> log.1.txt + // log.1.txt -> log.2.txt + // log.2.txt -> log.3.txt + // log.3.txt -> delete + void rotate_(); + + // delete the target if exists, and rename the src file to target + // return true on success, false otherwise. + bool rename_file_(const filename_t &src_filename, const filename_t &target_filename); + + filename_t base_filename_; + std::size_t max_size_; + std::size_t max_files_; + std::size_t current_size_; + details::file_helper file_helper_; +}; + +using rotating_file_sink_mt = rotating_file_sink; +using rotating_file_sink_st = rotating_file_sink; + +} // namespace sinks + +// +// factory functions +// + +template +inline std::shared_ptr rotating_logger_mt(const std::string &logger_name, + const filename_t &filename, + size_t max_file_size, + size_t max_files, + bool rotate_on_open = false, + const file_event_handlers &event_handlers = {}) { + return Factory::template create( + logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); +} + +template +inline std::shared_ptr rotating_logger_st(const std::string &logger_name, + const filename_t &filename, + size_t max_file_size, + size_t max_files, + bool rotate_on_open = false, + const file_event_handlers &event_handlers = {}) { + return Factory::template create( + logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); +} +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "rotating_file_sink-inl.h" +#endif diff --git a/include/spdlog/sinks/sink-inl.h b/include/spdlog/sinks/sink-inl.h new file mode 100644 index 0000000..e4b2714 --- /dev/null +++ b/include/spdlog/sinks/sink-inl.h @@ -0,0 +1,22 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include + +SPDLOG_INLINE bool spdlog::sinks::sink::should_log(spdlog::level::level_enum msg_level) const { + return msg_level >= level_.load(std::memory_order_relaxed); +} + +SPDLOG_INLINE void spdlog::sinks::sink::set_level(level::level_enum log_level) { + level_.store(log_level, std::memory_order_relaxed); +} + +SPDLOG_INLINE spdlog::level::level_enum spdlog::sinks::sink::level() const { + return static_cast(level_.load(std::memory_order_relaxed)); +} diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h new file mode 100644 index 0000000..5850685 --- /dev/null +++ b/include/spdlog/sinks/sink.h @@ -0,0 +1,34 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +namespace spdlog { + +namespace sinks { +class SPDLOG_API sink { +public: + virtual ~sink() = default; + virtual void log(const details::log_msg &msg) = 0; + virtual void flush() = 0; + virtual void set_pattern(const std::string &pattern) = 0; + virtual void set_formatter(std::unique_ptr sink_formatter) = 0; + + void set_level(level::level_enum log_level); + level::level_enum level() const; + bool should_log(level::level_enum msg_level) const; + +protected: + // sink log level - default is all + level_t level_{level::trace}; +}; + +} // namespace sinks +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "sink-inl.h" +#endif diff --git a/include/spdlog/sinks/stdout_color_sinks-inl.h b/include/spdlog/sinks/stdout_color_sinks-inl.h new file mode 100644 index 0000000..166e386 --- /dev/null +++ b/include/spdlog/sinks/stdout_color_sinks-inl.h @@ -0,0 +1,38 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +namespace spdlog { + +template +SPDLOG_INLINE std::shared_ptr stdout_color_mt(const std::string &logger_name, + color_mode mode) { + return Factory::template create(logger_name, mode); +} + +template +SPDLOG_INLINE std::shared_ptr stdout_color_st(const std::string &logger_name, + color_mode mode) { + return Factory::template create(logger_name, mode); +} + +template +SPDLOG_INLINE std::shared_ptr stderr_color_mt(const std::string &logger_name, + color_mode mode) { + return Factory::template create(logger_name, mode); +} + +template +SPDLOG_INLINE std::shared_ptr stderr_color_st(const std::string &logger_name, + color_mode mode) { + return Factory::template create(logger_name, mode); +} +} // namespace spdlog diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h new file mode 100644 index 0000000..72991fe --- /dev/null +++ b/include/spdlog/sinks/stdout_color_sinks.h @@ -0,0 +1,49 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifdef _WIN32 + #include +#else + #include +#endif + +#include + +namespace spdlog { +namespace sinks { +#ifdef _WIN32 +using stdout_color_sink_mt = wincolor_stdout_sink_mt; +using stdout_color_sink_st = wincolor_stdout_sink_st; +using stderr_color_sink_mt = wincolor_stderr_sink_mt; +using stderr_color_sink_st = wincolor_stderr_sink_st; +#else +using stdout_color_sink_mt = ansicolor_stdout_sink_mt; +using stdout_color_sink_st = ansicolor_stdout_sink_st; +using stderr_color_sink_mt = ansicolor_stderr_sink_mt; +using stderr_color_sink_st = ansicolor_stderr_sink_st; +#endif +} // namespace sinks + +template +std::shared_ptr stdout_color_mt(const std::string &logger_name, + color_mode mode = color_mode::automatic); + +template +std::shared_ptr stdout_color_st(const std::string &logger_name, + color_mode mode = color_mode::automatic); + +template +std::shared_ptr stderr_color_mt(const std::string &logger_name, + color_mode mode = color_mode::automatic); + +template +std::shared_ptr stderr_color_st(const std::string &logger_name, + color_mode mode = color_mode::automatic); + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "stdout_color_sinks-inl.h" +#endif diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h new file mode 100644 index 0000000..dcb21d8 --- /dev/null +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -0,0 +1,127 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include +#include +#include + +#ifdef _WIN32 + // under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675) + // so instead we use ::FileWrite + #include + + #ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp + #include // WriteFile (..) + #endif + + #include // _get_osfhandle(..) + #include // _fileno(..) +#endif // _WIN32 + +namespace spdlog { + +namespace sinks { + +template +SPDLOG_INLINE stdout_sink_base::stdout_sink_base(FILE *file) + : mutex_(ConsoleMutex::mutex()), + file_(file), + formatter_(details::make_unique()) { +#ifdef _WIN32 + // get windows handle from the FILE* object + + handle_ = reinterpret_cast(::_get_osfhandle(::_fileno(file_))); + + // don't throw to support cases where no console is attached, + // and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE). + // throw only if non stdout/stderr target is requested (probably regular file and not console). + if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) { + throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno); + } +#endif // _WIN32 +} + +template +SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &msg) { +#ifdef _WIN32 + if (handle_ == INVALID_HANDLE_VALUE) { + return; + } + std::lock_guard lock(mutex_); + memory_buf_t formatted; + formatter_->format(msg, formatted); + auto size = static_cast(formatted.size()); + DWORD bytes_written = 0; + bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0; + if (!ok) { + throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + + std::to_string(::GetLastError())); + } +#else + std::lock_guard lock(mutex_); + memory_buf_t formatted; + formatter_->format(msg, formatted); + details::os::fwrite_bytes(formatted.data(), formatted.size(), file_); +#endif // _WIN32 + ::fflush(file_); // flush every line to terminal +} + +template +SPDLOG_INLINE void stdout_sink_base::flush() { + std::lock_guard lock(mutex_); + fflush(file_); +} + +template +SPDLOG_INLINE void stdout_sink_base::set_pattern(const std::string &pattern) { + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); +} + +template +SPDLOG_INLINE void stdout_sink_base::set_formatter( + std::unique_ptr sink_formatter) { + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); +} + +// stdout sink +template +SPDLOG_INLINE stdout_sink::stdout_sink() + : stdout_sink_base(stdout) {} + +// stderr sink +template +SPDLOG_INLINE stderr_sink::stderr_sink() + : stdout_sink_base(stderr) {} + +} // namespace sinks + +// factory methods +template +SPDLOG_INLINE std::shared_ptr stdout_logger_mt(const std::string &logger_name) { + return Factory::template create(logger_name); +} + +template +SPDLOG_INLINE std::shared_ptr stdout_logger_st(const std::string &logger_name) { + return Factory::template create(logger_name); +} + +template +SPDLOG_INLINE std::shared_ptr stderr_logger_mt(const std::string &logger_name) { + return Factory::template create(logger_name); +} + +template +SPDLOG_INLINE std::shared_ptr stderr_logger_st(const std::string &logger_name) { + return Factory::template create(logger_name); +} +} // namespace spdlog diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h new file mode 100644 index 0000000..6ef0996 --- /dev/null +++ b/include/spdlog/sinks/stdout_sinks.h @@ -0,0 +1,84 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include + +#ifdef _WIN32 + #include +#endif + +namespace spdlog { + +namespace sinks { + +template +class stdout_sink_base : public sink { +public: + using mutex_t = typename ConsoleMutex::mutex_t; + explicit stdout_sink_base(FILE *file); + ~stdout_sink_base() override = default; + + stdout_sink_base(const stdout_sink_base &other) = delete; + stdout_sink_base(stdout_sink_base &&other) = delete; + + stdout_sink_base &operator=(const stdout_sink_base &other) = delete; + stdout_sink_base &operator=(stdout_sink_base &&other) = delete; + + void log(const details::log_msg &msg) override; + void flush() override; + void set_pattern(const std::string &pattern) override; + + void set_formatter(std::unique_ptr sink_formatter) override; + +protected: + mutex_t &mutex_; + FILE *file_; + std::unique_ptr formatter_; +#ifdef _WIN32 + HANDLE handle_; +#endif // WIN32 +}; + +template +class stdout_sink : public stdout_sink_base { +public: + stdout_sink(); +}; + +template +class stderr_sink : public stdout_sink_base { +public: + stderr_sink(); +}; + +using stdout_sink_mt = stdout_sink; +using stdout_sink_st = stdout_sink; + +using stderr_sink_mt = stderr_sink; +using stderr_sink_st = stderr_sink; + +} // namespace sinks + +// factory methods +template +std::shared_ptr stdout_logger_mt(const std::string &logger_name); + +template +std::shared_ptr stdout_logger_st(const std::string &logger_name); + +template +std::shared_ptr stderr_logger_mt(const std::string &logger_name); + +template +std::shared_ptr stderr_logger_st(const std::string &logger_name); + +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "stdout_sinks-inl.h" +#endif diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h new file mode 100644 index 0000000..913d41b --- /dev/null +++ b/include/spdlog/sinks/syslog_sink.h @@ -0,0 +1,104 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include + +#include +#include +#include + +namespace spdlog { +namespace sinks { +/** + * Sink that write to syslog using the `syscall()` library call. + */ +template +class syslog_sink : public base_sink { +public: + syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting) + : enable_formatting_{enable_formatting}, + syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, + /* spdlog::level::debug */ LOG_DEBUG, + /* spdlog::level::info */ LOG_INFO, + /* spdlog::level::warn */ LOG_WARNING, + /* spdlog::level::err */ LOG_ERR, + /* spdlog::level::critical */ LOG_CRIT, + /* spdlog::level::off */ LOG_INFO}}, + ident_{std::move(ident)} { + // set ident to be program name if empty + ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility); + } + + ~syslog_sink() override { ::closelog(); } + + syslog_sink(const syslog_sink &) = delete; + syslog_sink &operator=(const syslog_sink &) = delete; + +protected: + void sink_it_(const details::log_msg &msg) override { + string_view_t payload; + memory_buf_t formatted; + if (enable_formatting_) { + base_sink::formatter_->format(msg, formatted); + payload = string_view_t(formatted.data(), formatted.size()); + } else { + payload = msg.payload; + } + + size_t length = payload.size(); + // limit to max int + if (length > static_cast(std::numeric_limits::max())) { + length = static_cast(std::numeric_limits::max()); + } + + ::syslog(syslog_prio_from_level(msg), "%.*s", static_cast(length), payload.data()); + } + + void flush_() override {} + bool enable_formatting_ = false; + + // + // Simply maps spdlog's log level to syslog priority level. + // + virtual int syslog_prio_from_level(const details::log_msg &msg) const { + return syslog_levels_.at(static_cast(msg.level)); + } + + using levels_array = std::array; + levels_array syslog_levels_; + +private: + // must store the ident because the man says openlog might use the pointer as + // is and not a string copy + const std::string ident_; +}; + +using syslog_sink_mt = syslog_sink; +using syslog_sink_st = syslog_sink; +} // namespace sinks + +// Create and register a syslog logger +template +inline std::shared_ptr syslog_logger_mt(const std::string &logger_name, + const std::string &syslog_ident = "", + int syslog_option = 0, + int syslog_facility = LOG_USER, + bool enable_formatting = false) { + return Factory::template create(logger_name, syslog_ident, syslog_option, + syslog_facility, enable_formatting); +} + +template +inline std::shared_ptr syslog_logger_st(const std::string &logger_name, + const std::string &syslog_ident = "", + int syslog_option = 0, + int syslog_facility = LOG_USER, + bool enable_formatting = false) { + return Factory::template create(logger_name, syslog_ident, syslog_option, + syslog_facility, enable_formatting); +} +} // namespace spdlog diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h new file mode 100644 index 0000000..d2cd55f --- /dev/null +++ b/include/spdlog/sinks/systemd_sink.h @@ -0,0 +1,121 @@ +// Copyright(c) 2019 ZVYAGIN.Alexander@gmail.com +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include + +#include +#ifndef SD_JOURNAL_SUPPRESS_LOCATION + #define SD_JOURNAL_SUPPRESS_LOCATION +#endif +#include + +namespace spdlog { +namespace sinks { + +/** + * Sink that write to systemd journal using the `sd_journal_send()` library call. + */ +template +class systemd_sink : public base_sink { +public: + systemd_sink(std::string ident = "", bool enable_formatting = false) + : ident_{std::move(ident)}, + enable_formatting_{enable_formatting}, + syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, + /* spdlog::level::debug */ LOG_DEBUG, + /* spdlog::level::info */ LOG_INFO, + /* spdlog::level::warn */ LOG_WARNING, + /* spdlog::level::err */ LOG_ERR, + /* spdlog::level::critical */ LOG_CRIT, + /* spdlog::level::off */ LOG_INFO}} {} + + ~systemd_sink() override {} + + systemd_sink(const systemd_sink &) = delete; + systemd_sink &operator=(const systemd_sink &) = delete; + +protected: + const std::string ident_; + bool enable_formatting_ = false; + using levels_array = std::array; + levels_array syslog_levels_; + + void sink_it_(const details::log_msg &msg) override { + int err; + string_view_t payload; + memory_buf_t formatted; + if (enable_formatting_) { + base_sink::formatter_->format(msg, formatted); + payload = string_view_t(formatted.data(), formatted.size()); + } else { + payload = msg.payload; + } + + size_t length = payload.size(); + // limit to max int + if (length > static_cast(std::numeric_limits::max())) { + length = static_cast(std::numeric_limits::max()); + } + + const string_view_t syslog_identifier = ident_.empty() ? msg.logger_name : ident_; + + // Do not send source location if not available + if (msg.source.empty()) { + // Note: function call inside '()' to avoid macro expansion + err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), + "PRIORITY=%d", syslog_level(msg.level), +#ifndef SPDLOG_NO_THREAD_ID + "TID=%zu", msg.thread_id, +#endif + "SYSLOG_IDENTIFIER=%.*s", + static_cast(syslog_identifier.size()), + syslog_identifier.data(), nullptr); + } else { + err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), + "PRIORITY=%d", syslog_level(msg.level), +#ifndef SPDLOG_NO_THREAD_ID + "TID=%zu", msg.thread_id, +#endif + "SYSLOG_IDENTIFIER=%.*s", + static_cast(syslog_identifier.size()), + syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename, + "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", + msg.source.funcname, nullptr); + } + + if (err) { + throw_spdlog_ex("Failed writing to systemd", errno); + } + } + + int syslog_level(level::level_enum l) { + return syslog_levels_.at(static_cast(l)); + } + + void flush_() override {} +}; + +using systemd_sink_mt = systemd_sink; +using systemd_sink_st = systemd_sink; +} // namespace sinks + +// Create and register a syslog logger +template +inline std::shared_ptr systemd_logger_mt(const std::string &logger_name, + const std::string &ident = "", + bool enable_formatting = false) { + return Factory::template create(logger_name, ident, enable_formatting); +} + +template +inline std::shared_ptr systemd_logger_st(const std::string &logger_name, + const std::string &ident = "", + bool enable_formatting = false) { + return Factory::template create(logger_name, ident, enable_formatting); +} +} // namespace spdlog diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h new file mode 100644 index 0000000..2534964 --- /dev/null +++ b/include/spdlog/sinks/tcp_sink.h @@ -0,0 +1,75 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#ifdef _WIN32 + #include +#else + #include +#endif + +#include +#include +#include +#include + +#pragma once + +// Simple tcp client sink +// Connects to remote address and send the formatted log. +// Will attempt to reconnect if connection drops. +// If more complicated behaviour is needed (i.e get responses), you can inherit it and override the +// sink_it_ method. + +namespace spdlog { +namespace sinks { + +struct tcp_sink_config { + std::string server_host; + int server_port; + bool lazy_connect = false; // if true connect on first log call instead of on construction + + tcp_sink_config(std::string host, int port) + : server_host{std::move(host)}, + server_port{port} {} +}; + +template +class tcp_sink : public spdlog::sinks::base_sink { +public: + // connect to tcp host/port or throw if failed + // host can be hostname or ip address + + explicit tcp_sink(tcp_sink_config sink_config) + : config_{std::move(sink_config)} { + if (!config_.lazy_connect) { + this->client_.connect(config_.server_host, config_.server_port); + } + } + + ~tcp_sink() override = default; + +protected: + void sink_it_(const spdlog::details::log_msg &msg) override { + spdlog::memory_buf_t formatted; + spdlog::sinks::base_sink::formatter_->format(msg, formatted); + if (!client_.is_connected()) { + client_.connect(config_.server_host, config_.server_port); + } + client_.send(formatted.data(), formatted.size()); + } + + void flush_() override {} + tcp_sink_config config_; + details::tcp_client client_; +}; + +using tcp_sink_mt = tcp_sink; +using tcp_sink_st = tcp_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/udp_sink.h b/include/spdlog/sinks/udp_sink.h new file mode 100644 index 0000000..4bff0fd --- /dev/null +++ b/include/spdlog/sinks/udp_sink.h @@ -0,0 +1,69 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#ifdef _WIN32 + #include +#else + #include +#endif + +#include +#include +#include +#include + +// Simple udp client sink +// Sends formatted log via udp + +namespace spdlog { +namespace sinks { + +struct udp_sink_config { + std::string server_host; + uint16_t server_port; + + udp_sink_config(std::string host, uint16_t port) + : server_host{std::move(host)}, + server_port{port} {} +}; + +template +class udp_sink : public spdlog::sinks::base_sink { +public: + // host can be hostname or ip address + explicit udp_sink(udp_sink_config sink_config) + : client_{sink_config.server_host, sink_config.server_port} {} + + ~udp_sink() override = default; + +protected: + void sink_it_(const spdlog::details::log_msg &msg) override { + spdlog::memory_buf_t formatted; + spdlog::sinks::base_sink::formatter_->format(msg, formatted); + client_.send(formatted.data(), formatted.size()); + } + + void flush_() override {} + details::udp_client client_; +}; + +using udp_sink_mt = udp_sink; +using udp_sink_st = udp_sink; + +} // namespace sinks + +// +// factory functions +// +template +inline std::shared_ptr udp_logger_mt(const std::string &logger_name, + sinks::udp_sink_config skin_config) { + return Factory::template create(logger_name, skin_config); +} + +} // namespace spdlog diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h new file mode 100644 index 0000000..2c9b582 --- /dev/null +++ b/include/spdlog/sinks/win_eventlog_sink.h @@ -0,0 +1,260 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +// Writing to Windows Event Log requires the registry entries below to be present, with the +// following modifications: +// 1. should be replaced with your log name (e.g. your application name) +// 2. should be replaced with the specific source name and the key should be +// duplicated for +// each source used in the application +// +// Since typically modifications of this kind require elevation, it's better to do it as a part of +// setup procedure. The snippet below uses mscoree.dll as the message file as it exists on most of +// the Windows systems anyway and happens to contain the needed resource. +// +// You can also specify a custom message file if needed. +// Please refer to Event Log functions descriptions in MSDN for more details on custom message +// files. + +/*--------------------------------------------------------------------------------------- + +Windows Registry Editor Version 5.00 + +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\] + +[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\\] +"TypesSupported"=dword:00000007 +"EventMessageFile"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,\ + 00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,\ + 5c,00,6d,00,73,00,63,00,6f,00,72,00,65,00,65,00,2e,00,64,00,6c,00,6c,00,00,\ + 00 + +-----------------------------------------------------------------------------------------*/ + +#pragma once + +#include +#include + +#include +#include + +#include +#include +#include + +namespace spdlog { +namespace sinks { + +namespace win_eventlog { + +namespace internal { + +struct local_alloc_t { + HLOCAL hlocal_; + + SPDLOG_CONSTEXPR local_alloc_t() SPDLOG_NOEXCEPT : hlocal_(nullptr) {} + + local_alloc_t(local_alloc_t const &) = delete; + local_alloc_t &operator=(local_alloc_t const &) = delete; + + ~local_alloc_t() SPDLOG_NOEXCEPT { + if (hlocal_) { + LocalFree(hlocal_); + } + } +}; + +/** Windows error */ +struct win32_error : public spdlog_ex { + /** Formats an error report line: "user-message: error-code (system message)" */ + static std::string format(std::string const &user_message, DWORD error_code = GetLastError()) { + std::string system_message; + + local_alloc_t format_message_result{}; + auto format_message_succeeded = + ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&format_message_result.hlocal_, 0, nullptr); + + if (format_message_succeeded && format_message_result.hlocal_) { + system_message = fmt_lib::format(" ({})", (LPSTR)format_message_result.hlocal_); + } + + return fmt_lib::format("{}: {}{}", user_message, error_code, system_message); + } + + explicit win32_error(std::string const &func_name, DWORD error = GetLastError()) + : spdlog_ex(format(func_name, error)) {} +}; + +/** Wrapper for security identifiers (SID) on Windows */ +struct sid_t { + std::vector buffer_; + +public: + sid_t() {} + + /** creates a wrapped SID copy */ + static sid_t duplicate_sid(PSID psid) { + if (!::IsValidSid(psid)) { + throw_spdlog_ex("sid_t::sid_t(): invalid SID received"); + } + + auto const sid_length{::GetLengthSid(psid)}; + + sid_t result; + result.buffer_.resize(sid_length); + if (!::CopySid(sid_length, (PSID)result.as_sid(), psid)) { + SPDLOG_THROW(win32_error("CopySid")); + } + + return result; + } + + /** Retrieves pointer to the internal buffer contents as SID* */ + SID *as_sid() const { return buffer_.empty() ? nullptr : (SID *)buffer_.data(); } + + /** Get SID for the current user */ + static sid_t get_current_user_sid() { + /* create and init RAII holder for process token */ + struct process_token_t { + HANDLE token_handle_ = INVALID_HANDLE_VALUE; + explicit process_token_t(HANDLE process) { + if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_)) { + SPDLOG_THROW(win32_error("OpenProcessToken")); + } + } + + ~process_token_t() { ::CloseHandle(token_handle_); } + + } current_process_token( + ::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! + + // Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return + // the token size + DWORD tusize = 0; + if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, + &tusize)) { + SPDLOG_THROW(win32_error("GetTokenInformation should fail")); + } + + // get user token + std::vector buffer(static_cast(tusize)); + if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, + (LPVOID)buffer.data(), tusize, &tusize)) { + SPDLOG_THROW(win32_error("GetTokenInformation")); + } + + // create a wrapper of the SID data as stored in the user token + return sid_t::duplicate_sid(((TOKEN_USER *)buffer.data())->User.Sid); + } +}; + +struct eventlog { + static WORD get_event_type(details::log_msg const &msg) { + switch (msg.level) { + case level::trace: + case level::debug: + return EVENTLOG_SUCCESS; + + case level::info: + return EVENTLOG_INFORMATION_TYPE; + + case level::warn: + return EVENTLOG_WARNING_TYPE; + + case level::err: + case level::critical: + case level::off: + return EVENTLOG_ERROR_TYPE; + + default: + return EVENTLOG_INFORMATION_TYPE; + } + } + + static WORD get_event_category(details::log_msg const &msg) { return (WORD)msg.level; } +}; + +} // namespace internal + +/* + * Windows Event Log sink + */ +template +class win_eventlog_sink : public base_sink { +private: + HANDLE hEventLog_{NULL}; + internal::sid_t current_user_sid_; + std::string source_; + DWORD event_id_; + + HANDLE event_log_handle() { + if (!hEventLog_) { + hEventLog_ = ::RegisterEventSourceA(nullptr, source_.c_str()); + if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED) { + SPDLOG_THROW(internal::win32_error("RegisterEventSource")); + } + } + + return hEventLog_; + } + +protected: + void sink_it_(const details::log_msg &msg) override { + using namespace internal; + + bool succeeded; + memory_buf_t formatted; + base_sink::formatter_->format(msg, formatted); + formatted.push_back('\0'); + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + wmemory_buf_t buf; + details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf); + + LPCWSTR lp_wstr = buf.data(); + succeeded = static_cast(::ReportEventW( + event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), + event_id_, current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr)); +#else + LPCSTR lp_str = formatted.data(); + succeeded = static_cast(::ReportEventA( + event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), + event_id_, current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr)); +#endif + + if (!succeeded) { + SPDLOG_THROW(win32_error("ReportEvent")); + } + } + + void flush_() override {} + +public: + win_eventlog_sink(std::string const &source, + DWORD event_id = 1000 /* according to mscoree.dll */) + : source_(source), + event_id_(event_id) { + try { + current_user_sid_ = internal::sid_t::get_current_user_sid(); + } catch (...) { + // get_current_user_sid() is unlikely to fail and if it does, we can still proceed + // without current_user_sid but in the event log the record will have no user name + } + } + + ~win_eventlog_sink() { + if (hEventLog_) DeregisterEventSource(hEventLog_); + } +}; + +} // namespace win_eventlog + +using win_eventlog_sink_mt = win_eventlog::win_eventlog_sink; +using win_eventlog_sink_st = win_eventlog::win_eventlog_sink; + +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h new file mode 100644 index 0000000..696db56 --- /dev/null +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -0,0 +1,172 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +#include +#include + +namespace spdlog { +namespace sinks { +template +SPDLOG_INLINE wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) + : out_handle_(out_handle), + mutex_(ConsoleMutex::mutex()), + formatter_(details::make_unique()) { + set_color_mode_impl(mode); + // set level colors + colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white + colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan + colors_[level::info] = FOREGROUND_GREEN; // green + colors_[level::warn] = + FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow + colors_[level::err] = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red + colors_[level::critical] = BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | + FOREGROUND_BLUE | + FOREGROUND_INTENSITY; // intense white on red background + colors_[level::off] = 0; +} + +template +SPDLOG_INLINE wincolor_sink::~wincolor_sink() { + this->flush(); +} + +// change the color for the given level +template +void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, + std::uint16_t color) { + std::lock_guard lock(mutex_); + colors_[static_cast(level)] = color; +} + +template +void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) { + if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) { + return; + } + + std::lock_guard lock(mutex_); + msg.color_range_start = 0; + msg.color_range_end = 0; + memory_buf_t formatted; + formatter_->format(msg, formatted); + if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { + // before color range + print_range_(formatted, 0, msg.color_range_start); + // in color range + auto orig_attribs = + static_cast(set_foreground_color_(colors_[static_cast(msg.level)])); + print_range_(formatted, msg.color_range_start, msg.color_range_end); + // reset to orig colors + ::SetConsoleTextAttribute(static_cast(out_handle_), orig_attribs); + print_range_(formatted, msg.color_range_end, formatted.size()); + } else // print without colors if color range is invalid (or color is disabled) + { + write_to_file_(formatted); + } +} + +template +void SPDLOG_INLINE wincolor_sink::flush() { + // windows console always flushed? +} + +template +void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &pattern) { + std::lock_guard lock(mutex_); + formatter_ = std::unique_ptr(new pattern_formatter(pattern)); +} + +template +void SPDLOG_INLINE +wincolor_sink::set_formatter(std::unique_ptr sink_formatter) { + std::lock_guard lock(mutex_); + formatter_ = std::move(sink_formatter); +} + +template +void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) { + std::lock_guard lock(mutex_); + set_color_mode_impl(mode); +} + +template +void SPDLOG_INLINE wincolor_sink::set_color_mode_impl(color_mode mode) { + if (mode == color_mode::automatic) { + // should do colors only if out_handle_ points to actual console. + DWORD console_mode; + bool in_console = ::GetConsoleMode(static_cast(out_handle_), &console_mode) != 0; + should_do_colors_ = in_console; + } else { + should_do_colors_ = mode == color_mode::always ? true : false; + } +} + +// set foreground color and return the orig console attributes (for resetting later) +template +std::uint16_t SPDLOG_INLINE +wincolor_sink::set_foreground_color_(std::uint16_t attribs) { + CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; + if (!::GetConsoleScreenBufferInfo(static_cast(out_handle_), &orig_buffer_info)) { + // just return white if failed getting console info + return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; + } + + // change only the foreground bits (lowest 4 bits) + auto new_attribs = static_cast(attribs) | (orig_buffer_info.wAttributes & 0xfff0); + auto ignored = + ::SetConsoleTextAttribute(static_cast(out_handle_), static_cast(new_attribs)); + (void)(ignored); + return static_cast(orig_buffer_info.wAttributes); // return orig attribs +} + +// print a range of formatted message to console +template +void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t &formatted, + size_t start, + size_t end) { + if (end > start) { +#if defined(SPDLOG_UTF8_TO_WCHAR_CONSOLE) + wmemory_buf_t wformatted; + details::os::utf8_to_wstrbuf(string_view_t(formatted.data() + start, end - start), + wformatted); + auto size = static_cast(wformatted.size()); + auto ignored = ::WriteConsoleW(static_cast(out_handle_), wformatted.data(), size, + nullptr, nullptr); +#else + auto size = static_cast(end - start); + auto ignored = ::WriteConsoleA(static_cast(out_handle_), formatted.data() + start, + size, nullptr, nullptr); +#endif + (void)(ignored); + } +} + +template +void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_t &formatted) { + auto size = static_cast(formatted.size()); + DWORD bytes_written = 0; + auto ignored = ::WriteFile(static_cast(out_handle_), formatted.data(), size, + &bytes_written, nullptr); + (void)(ignored); +} + +// wincolor_stdout_sink +template +SPDLOG_INLINE wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) + : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {} + +// wincolor_stderr_sink +template +SPDLOG_INLINE wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) + : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} +} // namespace sinks +} // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h new file mode 100644 index 0000000..8ba594c --- /dev/null +++ b/include/spdlog/sinks/wincolor_sink.h @@ -0,0 +1,82 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace spdlog { +namespace sinks { +/* + * Windows color console sink. Uses WriteConsoleA to write to the console with + * colors + */ +template +class wincolor_sink : public sink { +public: + wincolor_sink(void *out_handle, color_mode mode); + ~wincolor_sink() override; + + wincolor_sink(const wincolor_sink &other) = delete; + wincolor_sink &operator=(const wincolor_sink &other) = delete; + + // change the color for the given level + void set_color(level::level_enum level, std::uint16_t color); + void log(const details::log_msg &msg) final override; + void flush() final override; + void set_pattern(const std::string &pattern) override final; + void set_formatter(std::unique_ptr sink_formatter) override final; + void set_color_mode(color_mode mode); + +protected: + using mutex_t = typename ConsoleMutex::mutex_t; + void *out_handle_; + mutex_t &mutex_; + bool should_do_colors_; + std::unique_ptr formatter_; + std::array colors_; + + // set foreground color and return the orig console attributes (for resetting later) + std::uint16_t set_foreground_color_(std::uint16_t attribs); + + // print a range of formatted message to console + void print_range_(const memory_buf_t &formatted, size_t start, size_t end); + + // in case we are redirected to file (not in console mode) + void write_to_file_(const memory_buf_t &formatted); + + void set_color_mode_impl(color_mode mode); +}; + +template +class wincolor_stdout_sink : public wincolor_sink { +public: + explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic); +}; + +template +class wincolor_stderr_sink : public wincolor_sink { +public: + explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic); +}; + +using wincolor_stdout_sink_mt = wincolor_stdout_sink; +using wincolor_stdout_sink_st = wincolor_stdout_sink; + +using wincolor_stderr_sink_mt = wincolor_stderr_sink; +using wincolor_stderr_sink_st = wincolor_stderr_sink; +} // namespace sinks +} // namespace spdlog + +#ifdef SPDLOG_HEADER_ONLY + #include "wincolor_sink-inl.h" +#endif diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h new file mode 100644 index 0000000..97c3622 --- /dev/null +++ b/include/spdlog/spdlog-inl.h @@ -0,0 +1,92 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#ifndef SPDLOG_HEADER_ONLY + #include +#endif + +#include +#include + +namespace spdlog { + +SPDLOG_INLINE void initialize_logger(std::shared_ptr logger) { + details::registry::instance().initialize_logger(std::move(logger)); +} + +SPDLOG_INLINE std::shared_ptr get(const std::string &name) { + return details::registry::instance().get(name); +} + +SPDLOG_INLINE void set_formatter(std::unique_ptr formatter) { + details::registry::instance().set_formatter(std::move(formatter)); +} + +SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type) { + set_formatter( + std::unique_ptr(new pattern_formatter(std::move(pattern), time_type))); +} + +SPDLOG_INLINE void enable_backtrace(size_t n_messages) { + details::registry::instance().enable_backtrace(n_messages); +} + +SPDLOG_INLINE void disable_backtrace() { details::registry::instance().disable_backtrace(); } + +SPDLOG_INLINE void dump_backtrace() { default_logger_raw()->dump_backtrace(); } + +SPDLOG_INLINE level::level_enum get_level() { return default_logger_raw()->level(); } + +SPDLOG_INLINE bool should_log(level::level_enum log_level) { + return default_logger_raw()->should_log(log_level); +} + +SPDLOG_INLINE void set_level(level::level_enum log_level) { + details::registry::instance().set_level(log_level); +} + +SPDLOG_INLINE void flush_on(level::level_enum log_level) { + details::registry::instance().flush_on(log_level); +} + +SPDLOG_INLINE void set_error_handler(void (*handler)(const std::string &msg)) { + details::registry::instance().set_error_handler(handler); +} + +SPDLOG_INLINE void register_logger(std::shared_ptr logger) { + details::registry::instance().register_logger(std::move(logger)); +} + +SPDLOG_INLINE void apply_all(const std::function)> &fun) { + details::registry::instance().apply_all(fun); +} + +SPDLOG_INLINE void drop(const std::string &name) { details::registry::instance().drop(name); } + +SPDLOG_INLINE void drop_all() { details::registry::instance().drop_all(); } + +SPDLOG_INLINE void shutdown() { details::registry::instance().shutdown(); } + +SPDLOG_INLINE void set_automatic_registration(bool automatic_registration) { + details::registry::instance().set_automatic_registration(automatic_registration); +} + +SPDLOG_INLINE std::shared_ptr default_logger() { + return details::registry::instance().default_logger(); +} + +SPDLOG_INLINE spdlog::logger *default_logger_raw() { + return details::registry::instance().get_default_raw(); +} + +SPDLOG_INLINE void set_default_logger(std::shared_ptr default_logger) { + details::registry::instance().set_default_logger(std::move(default_logger)); +} + +SPDLOG_INLINE void apply_logger_env_levels(std::shared_ptr logger) { + details::registry::instance().apply_logger_env_levels(std::move(logger)); +} + +} // namespace spdlog diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h new file mode 100644 index 0000000..a8afbce --- /dev/null +++ b/include/spdlog/spdlog.h @@ -0,0 +1,352 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +// spdlog main header file. +// see example.cpp for usage example + +#ifndef SPDLOG_H +#define SPDLOG_H + +#pragma once + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace spdlog { + +using default_factory = synchronous_factory; + +// Create and register a logger with a templated sink type +// The logger's level, formatter and flush level will be set according the +// global settings. +// +// Example: +// spdlog::create("logger_name", "dailylog_filename", 11, 59); +template +inline std::shared_ptr create(std::string logger_name, SinkArgs &&...sink_args) { + return default_factory::create(std::move(logger_name), + std::forward(sink_args)...); +} + +// Initialize and register a logger, +// formatter and flush level will be set according the global settings. +// +// Useful for initializing manually created loggers with the global settings. +// +// Example: +// auto mylogger = std::make_shared("mylogger", ...); +// spdlog::initialize_logger(mylogger); +SPDLOG_API void initialize_logger(std::shared_ptr logger); + +// Return an existing logger or nullptr if a logger with such name doesn't +// exist. +// example: spdlog::get("my_logger")->info("hello {}", "world"); +SPDLOG_API std::shared_ptr get(const std::string &name); + +// Set global formatter. Each sink in each logger will get a clone of this object +SPDLOG_API void set_formatter(std::unique_ptr formatter); + +// Set global format string. +// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v"); +SPDLOG_API void set_pattern(std::string pattern, + pattern_time_type time_type = pattern_time_type::local); + +// enable global backtrace support +SPDLOG_API void enable_backtrace(size_t n_messages); + +// disable global backtrace support +SPDLOG_API void disable_backtrace(); + +// call dump backtrace on default logger +SPDLOG_API void dump_backtrace(); + +// Get global logging level +SPDLOG_API level::level_enum get_level(); + +// Set global logging level +SPDLOG_API void set_level(level::level_enum log_level); + +// Determine whether the default logger should log messages with a certain level +SPDLOG_API bool should_log(level::level_enum lvl); + +// Set global flush level +SPDLOG_API void flush_on(level::level_enum log_level); + +// Start/Restart a periodic flusher thread +// Warning: Use only if all your loggers are thread safe! +template +inline void flush_every(std::chrono::duration interval) { + details::registry::instance().flush_every(interval); +} + +// Set global error handler +SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg)); + +// Register the given logger with the given name +SPDLOG_API void register_logger(std::shared_ptr logger); + +// Apply a user defined function on all registered loggers +// Example: +// spdlog::apply_all([&](std::shared_ptr l) {l->flush();}); +SPDLOG_API void apply_all(const std::function)> &fun); + +// Drop the reference to the given logger +SPDLOG_API void drop(const std::string &name); + +// Drop all references from the registry +SPDLOG_API void drop_all(); + +// stop any running threads started by spdlog and clean registry loggers +SPDLOG_API void shutdown(); + +// Automatic registration of loggers when using spdlog::create() or spdlog::create_async +SPDLOG_API void set_automatic_registration(bool automatic_registration); + +// API for using default logger (stdout_color_mt), +// e.g: spdlog::info("Message {}", 1); +// +// The default logger object can be accessed using the spdlog::default_logger(): +// For example, to add another sink to it: +// spdlog::default_logger()->sinks().push_back(some_sink); +// +// The default logger can replaced using spdlog::set_default_logger(new_logger). +// For example, to replace it with a file logger. +// +// IMPORTANT: +// The default API is thread safe (for _mt loggers), but: +// set_default_logger() *should not* be used concurrently with the default API. +// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. + +SPDLOG_API std::shared_ptr default_logger(); + +SPDLOG_API spdlog::logger *default_logger_raw(); + +SPDLOG_API void set_default_logger(std::shared_ptr default_logger); + +// Initialize logger level based on environment configs. +// +// Useful for applying SPDLOG_LEVEL to manually created loggers. +// +// Example: +// auto mylogger = std::make_shared("mylogger", ...); +// spdlog::apply_logger_env_levels(mylogger); +SPDLOG_API void apply_logger_env_levels(std::shared_ptr logger); + +template +inline void log(source_loc source, + level::level_enum lvl, + format_string_t fmt, + Args &&...args) { + default_logger_raw()->log(source, lvl, fmt, std::forward(args)...); +} + +template +inline void log(level::level_enum lvl, format_string_t fmt, Args &&...args) { + default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); +} + +template +inline void trace(format_string_t fmt, Args &&...args) { + default_logger_raw()->trace(fmt, std::forward(args)...); +} + +template +inline void debug(format_string_t fmt, Args &&...args) { + default_logger_raw()->debug(fmt, std::forward(args)...); +} + +template +inline void info(format_string_t fmt, Args &&...args) { + default_logger_raw()->info(fmt, std::forward(args)...); +} + +template +inline void warn(format_string_t fmt, Args &&...args) { + default_logger_raw()->warn(fmt, std::forward(args)...); +} + +template +inline void error(format_string_t fmt, Args &&...args) { + default_logger_raw()->error(fmt, std::forward(args)...); +} + +template +inline void critical(format_string_t fmt, Args &&...args) { + default_logger_raw()->critical(fmt, std::forward(args)...); +} + +template +inline void log(source_loc source, level::level_enum lvl, const T &msg) { + default_logger_raw()->log(source, lvl, msg); +} + +template +inline void log(level::level_enum lvl, const T &msg) { + default_logger_raw()->log(lvl, msg); +} + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +template +inline void log(source_loc source, + level::level_enum lvl, + wformat_string_t fmt, + Args &&...args) { + default_logger_raw()->log(source, lvl, fmt, std::forward(args)...); +} + +template +inline void log(level::level_enum lvl, wformat_string_t fmt, Args &&...args) { + default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); +} + +template +inline void trace(wformat_string_t fmt, Args &&...args) { + default_logger_raw()->trace(fmt, std::forward(args)...); +} + +template +inline void debug(wformat_string_t fmt, Args &&...args) { + default_logger_raw()->debug(fmt, std::forward(args)...); +} + +template +inline void info(wformat_string_t fmt, Args &&...args) { + default_logger_raw()->info(fmt, std::forward(args)...); +} + +template +inline void warn(wformat_string_t fmt, Args &&...args) { + default_logger_raw()->warn(fmt, std::forward(args)...); +} + +template +inline void error(wformat_string_t fmt, Args &&...args) { + default_logger_raw()->error(fmt, std::forward(args)...); +} + +template +inline void critical(wformat_string_t fmt, Args &&...args) { + default_logger_raw()->critical(fmt, std::forward(args)...); +} +#endif + +template +inline void trace(const T &msg) { + default_logger_raw()->trace(msg); +} + +template +inline void debug(const T &msg) { + default_logger_raw()->debug(msg); +} + +template +inline void info(const T &msg) { + default_logger_raw()->info(msg); +} + +template +inline void warn(const T &msg) { + default_logger_raw()->warn(msg); +} + +template +inline void error(const T &msg) { + default_logger_raw()->error(msg); +} + +template +inline void critical(const T &msg) { + default_logger_raw()->critical(msg); +} + +} // namespace spdlog + +// +// enable/disable log calls at compile time according to global level. +// +// define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h): +// SPDLOG_LEVEL_TRACE, +// SPDLOG_LEVEL_DEBUG, +// SPDLOG_LEVEL_INFO, +// SPDLOG_LEVEL_WARN, +// SPDLOG_LEVEL_ERROR, +// SPDLOG_LEVEL_CRITICAL, +// SPDLOG_LEVEL_OFF +// + +#ifndef SPDLOG_NO_SOURCE_LOC + #define SPDLOG_LOGGER_CALL(logger, level, ...) \ + (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) +#else + #define SPDLOG_LOGGER_CALL(logger, level, ...) \ + (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__) +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE + #define SPDLOG_LOGGER_TRACE(logger, ...) \ + SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) + #define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__) +#else + #define SPDLOG_LOGGER_TRACE(logger, ...) (void)0 + #define SPDLOG_TRACE(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG + #define SPDLOG_LOGGER_DEBUG(logger, ...) \ + SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__) + #define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__) +#else + #define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0 + #define SPDLOG_DEBUG(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO + #define SPDLOG_LOGGER_INFO(logger, ...) \ + SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__) + #define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__) +#else + #define SPDLOG_LOGGER_INFO(logger, ...) (void)0 + #define SPDLOG_INFO(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN + #define SPDLOG_LOGGER_WARN(logger, ...) \ + SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__) + #define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__) +#else + #define SPDLOG_LOGGER_WARN(logger, ...) (void)0 + #define SPDLOG_WARN(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR + #define SPDLOG_LOGGER_ERROR(logger, ...) \ + SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__) + #define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__) +#else + #define SPDLOG_LOGGER_ERROR(logger, ...) (void)0 + #define SPDLOG_ERROR(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL + #define SPDLOG_LOGGER_CRITICAL(logger, ...) \ + SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__) + #define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__) +#else + #define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0 + #define SPDLOG_CRITICAL(...) (void)0 +#endif + +#ifdef SPDLOG_HEADER_ONLY + #include "spdlog-inl.h" +#endif + +#endif // SPDLOG_H diff --git a/include/spdlog/stopwatch.h b/include/spdlog/stopwatch.h new file mode 100644 index 0000000..54ab3d3 --- /dev/null +++ b/include/spdlog/stopwatch.h @@ -0,0 +1,66 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#include +#include + +// Stopwatch support for spdlog (using std::chrono::steady_clock). +// Displays elapsed seconds since construction as double. +// +// Usage: +// +// spdlog::stopwatch sw; +// ... +// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds" +// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds" +// +// +// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use +// "duration_cast<..>(sw.elapsed())": +// +// #include +//.. +// using std::chrono::duration_cast; +// using std::chrono::milliseconds; +// spdlog::info("Elapsed {}", duration_cast(sw.elapsed())); => "Elapsed 5ms" + +namespace spdlog { +class stopwatch { + using clock = std::chrono::steady_clock; + std::chrono::time_point start_tp_; + +public: + stopwatch() + : start_tp_{clock::now()} {} + + std::chrono::duration elapsed() const { + return std::chrono::duration(clock::now() - start_tp_); + } + + std::chrono::milliseconds elapsed_ms() const { + return std::chrono::duration_cast(clock::now() - start_tp_); + } + + void reset() { start_tp_ = clock::now(); } +}; +} // namespace spdlog + +// Support for fmt formatting (e.g. "{:012.9}" or just "{}") +namespace +#ifdef SPDLOG_USE_STD_FORMAT + std +#else + fmt +#endif +{ + +template <> +struct formatter : formatter { + template + auto format(const spdlog::stopwatch &sw, FormatContext &ctx) const -> decltype(ctx.out()) { + return formatter::format(sw.elapsed().count(), ctx); + } +}; +} // namespace std diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h new file mode 100644 index 0000000..a47a907 --- /dev/null +++ b/include/spdlog/tweakme.h @@ -0,0 +1,141 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +/////////////////////////////////////////////////////////////////////////////// +// +// Edit this file to squeeze more performance, and to customize supported +// features +// +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Under Linux, the much faster CLOCK_REALTIME_COARSE clock can be used. +// This clock is less accurate - can be off by dozens of millis - depending on +// the kernel HZ. +// Uncomment to use it instead of the regular clock. +// +// #define SPDLOG_CLOCK_COARSE +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment if source location logging is not needed. +// This will prevent spdlog from using __FILE__, __LINE__ and SPDLOG_FUNCTION +// +// #define SPDLOG_NO_SOURCE_LOC +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment if thread id logging is not needed (i.e. no %t in the log pattern). +// This will prevent spdlog from querying the thread id on each log call. +// +// WARNING: If the log pattern contains thread id (i.e, %t) while this flag is +// on, zero will be logged as thread id. +// +// #define SPDLOG_NO_THREAD_ID +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to prevent spdlog from using thread local storage. +// +// WARNING: if your program forks, UNCOMMENT this flag to prevent undefined +// thread ids in the children logs. +// +// #define SPDLOG_NO_TLS +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to avoid spdlog's usage of atomic log levels +// Use only if your code never modifies a logger's log levels concurrently by +// different threads. +// +// #define SPDLOG_NO_ATOMIC_LEVELS +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to enable usage of wchar_t for file names on Windows. +// +// #define SPDLOG_WCHAR_FILENAMES +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to override default eol ("\n" or "\r\n" under Linux/Windows) +// +// #define SPDLOG_EOL ";-)\n" +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to override default folder separators ("/" or "\\/" under +// Linux/Windows). Each character in the string is treated as a different +// separator. +// +// #define SPDLOG_FOLDER_SEPS "\\" +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to use your own copy of the fmt library instead of spdlog's copy. +// In this case spdlog will try to include so set your -I flag +// accordingly. +// +// #define SPDLOG_FMT_EXTERNAL +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to use C++20 std::format instead of fmt. +// +// #define SPDLOG_USE_STD_FORMAT +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to enable wchar_t support (convert to utf8) +// +// #define SPDLOG_WCHAR_TO_UTF8_SUPPORT +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to prevent child processes from inheriting log file descriptors +// +// #define SPDLOG_PREVENT_CHILD_FD +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to customize level names (e.g. "MY TRACE") +// +// #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", "MY ERROR", "MY +// CRITICAL", "OFF" } +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to customize short level names (e.g. "MT") +// These can be longer than one character. +// +// #define SPDLOG_SHORT_LEVEL_NAMES { "T", "D", "I", "W", "E", "C", "O" } +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to disable default logger creation. +// This might save some (very) small initialization time if no default logger is needed. +// +// #define SPDLOG_DISABLE_DEFAULT_LOGGER +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment and set to compile time level with zero cost (default is INFO). +// Macros like SPDLOG_DEBUG(..), SPDLOG_INFO(..) will expand to empty statements if not enabled +// +// #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO +/////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment (and change if desired) macro to use for function names. +// This is compiler dependent. +// __PRETTY_FUNCTION__ might be nicer in clang/gcc, and __FUNCTION__ in msvc. +// Defaults to __FUNCTION__ (should work on all compilers) if not defined. +// +// #ifdef __PRETTY_FUNCTION__ +// # define SPDLOG_FUNCTION __PRETTY_FUNCTION__ +// #else +// # define SPDLOG_FUNCTION __FUNCTION__ +// #endif +/////////////////////////////////////////////////////////////////////////////// diff --git a/include/spdlog/version.h b/include/spdlog/version.h new file mode 100644 index 0000000..7c5e129 --- /dev/null +++ b/include/spdlog/version.h @@ -0,0 +1,11 @@ +// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) + +#pragma once + +#define SPDLOG_VER_MAJOR 1 +#define SPDLOG_VER_MINOR 15 +#define SPDLOG_VER_PATCH 0 + +#define SPDLOG_TO_VERSION(major, minor, patch) (major * 10000 + minor * 100 + patch) +#define SPDLOG_VERSION SPDLOG_TO_VERSION(SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH) From 8c19c318051a3044f9e570a2dbeb8b929ca11531 Mon Sep 17 00:00:00 2001 From: Sergei Date: Mon, 13 Jan 2025 10:47:00 +0200 Subject: [PATCH 055/156] try spdlog --- src/cli/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index c8f9a7b..1d22b82 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -9,6 +9,9 @@ #include #include +#include +#include + #include #include #include @@ -52,6 +55,7 @@ int main(const int, const char** const) setup_signal_handlers(); + spdlog::info("ocvsmd cli started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); ::openlog("ocvsmd-cli", LOG_PID, LOG_USER); ::syslog(LOG_NOTICE, "ocvsmd cli started."); // NOLINT *-vararg { From 842fefd01953520897c64c3937d23ec5eb794e0c Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 14 Jan 2025 18:48:29 +0200 Subject: [PATCH 056/156] daemon side logging --- include/spdlog/LICENSE | 25 +++++++ src/cli/main.cpp | 2 +- .../ocvsmd/common/ipc/RouteConnect.0.1.dsdl | 2 +- src/common/ipc/server_router.cpp | 47 +++++++++---- src/common/logging.hpp | 44 ++++++++++++ src/daemon/main.cpp | 67 +++++++++++++++++-- 6 files changed, 166 insertions(+), 21 deletions(-) create mode 100644 include/spdlog/LICENSE create mode 100644 src/common/logging.hpp diff --git a/include/spdlog/LICENSE b/include/spdlog/LICENSE new file mode 100644 index 0000000..c46826e --- /dev/null +++ b/include/spdlog/LICENSE @@ -0,0 +1,25 @@ +The MIT License (MIT) + +Copyright (c) 2016 Gabi Melman. + +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. + +-- NOTE: Third party dependency used by this software -- +This software depends on the fmt lib (MIT License), +and users must comply to its license: https://raw.githubusercontent.com/fmtlib/fmt/master/LICENSE diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 1d22b82..b8b47c2 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include diff --git a/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl b/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl index 8ce664c..637318c 100644 --- a/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl +++ b/src/common/dsdl/ocvsmd/common/ipc/RouteConnect.0.1.dsdl @@ -1,4 +1,4 @@ uavcan.node.Version.1.0 version int32 error_code -@extent 64 +@extent 64 * 8 diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index cfb57ed..435cfb3 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -8,6 +8,7 @@ #include "dsdl_helpers.hpp" #include "gateway.hpp" #include "ipc_types.hpp" +#include "logging.hpp" #include "pipe/server_pipe.hpp" #include "ocvsmd/common/ipc/RouteChannelMsg_0_1.hpp" @@ -22,7 +23,6 @@ #include #include #include -#include #include #include @@ -41,6 +41,7 @@ class ServerRouterImpl final : public ServerRouter ServerRouterImpl(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe) : memory_{memory} , server_pipe_{std::move(server_pipe)} + , logger_{getLogger("ipc")} { CETL_DEBUG_ASSERT(server_pipe_, ""); } @@ -99,7 +100,7 @@ class ServerRouterImpl final : public ServerRouter , endpoint_{endpoint} , next_sequence_{0} { - ::syslog(LOG_DEBUG, "Gateway(cl=%zu, tag=%zu).", endpoint.client_id, endpoint.tag); // NOLINT + router.logger_->trace("Gateway(cl={}, tag={}).", endpoint.client_id, endpoint.tag); } GatewayImpl(const GatewayImpl&) = delete; @@ -109,7 +110,7 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { - ::syslog(LOG_DEBUG, "~Gateway(cl=%zu, tag=%zu).", endpoint_.client_id, endpoint_.tag); // NOLINT + router_.logger_->trace("~Gateway(cl={}, tag={}).", endpoint_.client_id, endpoint_.tag); performWithoutThrowing([this] { // @@ -253,9 +254,9 @@ class ServerRouterImpl final : public ServerRouter } } - CETL_NODISCARD static int handlePipeEvent(const pipe::ServerPipe::Event::Connected& pipe_conn) + CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Connected& pipe_conn) { - ::syslog(LOG_DEBUG, "Pipe is connected (cl=%zu).", pipe_conn.client_id); // NOLINT + logger_->debug("Pipe is connected (cl={}).", pipe_conn.client_id); // It's not enough to consider the client router connected by the pipe event. // We gonna wait for `RouteConnect` negotiation (see `handleRouteConnect`). @@ -298,7 +299,7 @@ class ServerRouterImpl final : public ServerRouter CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Disconnected& disconn) { - ::syslog(LOG_DEBUG, "Pipe is disconnected (cl=%zu).", disconn.client_id); // NOLINT + logger_->debug("Pipe is disconnected (cl={}).", disconn.client_id); const auto cl_to_gws = client_id_to_map_of_gateways_.find(disconn.client_id); if (cl_to_gws != client_id_to_map_of_gateways_.end()) @@ -324,12 +325,11 @@ class ServerRouterImpl final : public ServerRouter CETL_NODISCARD int handleRouteConnect(const pipe::ServerPipe::ClientId client_id, const RouteConnect_0_1& rt_conn) { - ::syslog(LOG_DEBUG, // NOLINT - "Route connect request (cl=%zu, ver='%d.%d', err=%d).", - client_id, - static_cast(rt_conn.version.major), - static_cast(rt_conn.version.minor), - static_cast(rt_conn.error_code)); + logger_->debug("Route connect request (cl={}, ver='{}.{}', err={}).", + client_id, + static_cast(rt_conn.version.major), + static_cast(rt_conn.version.minor), + static_cast(rt_conn.error_code)); Route_0_1 route{&memory_}; auto& route_conn = route.set_connect(); @@ -367,6 +367,11 @@ class ServerRouterImpl final : public ServerRouter { if (auto gateway = tag_to_gw->second.lock()) { + logger_->trace("Route Ch Msg (cl={}, tag={}, seq={}).", + client_id, + route_ch_msg.tag, + route_ch_msg.sequence); + return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_real_payload}); } } @@ -382,19 +387,32 @@ class ServerRouterImpl final : public ServerRouter auto gateway = GatewayImpl::create(*this, endpoint); map_of_gws[route_ch_msg.tag] = gateway; + logger_->debug("Route Ch Msg (cl={}, tag={}, seq={}, srv=0x{:X}).", + client_id, + route_ch_msg.tag, + route_ch_msg.sequence, + route_ch_msg.service_id); + si_to_ch_factory->second(gateway, msg_real_payload); + return 0; } } } - // Nothing to do here with unsolicited messages - just ignore them. + // Nothing to do here with unsolicited messages - just trace and ignore them. + // + logger_->debug("Route Ch Unsolicited Msg (cl={}, tag={}, seq={}, srv=0x{:X}).", + client_id, + route_ch_msg.tag, + route_ch_msg.sequence, + route_ch_msg.service_id); return 0; } CETL_NODISCARD int handleRouteChannelEnd(const pipe::ServerPipe::ClientId client_id, const RouteChannelEnd_0_1& route_ch_end) { - ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT + logger_->debug("Route Ch End (cl={}, tag={}, err={}).", client_id, route_ch_end.tag, route_ch_end.error_code); const auto cl_to_gws = client_id_to_map_of_gateways_.find(client_id); if (cl_to_gws != client_id_to_map_of_gateways_.end()) @@ -419,6 +437,7 @@ class ServerRouterImpl final : public ServerRouter cetl::pmr::memory_resource& memory_; pipe::ServerPipe::Ptr server_pipe_; + LoggerPtr logger_; ClientIdToMapOfGateways client_id_to_map_of_gateways_; ServiceIdToChannelFactory service_id_to_channel_factory_; diff --git a/src/common/logging.hpp b/src/common/logging.hpp new file mode 100644 index 0000000..0bf9da8 --- /dev/null +++ b/src/common/logging.hpp @@ -0,0 +1,44 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_LOGGING_HPP_INCLUDED +#define OCVSMD_COMMON_LOGGING_HPP_INCLUDED + +#include +#include + +#include +#include + +namespace ocvsmd +{ +namespace common +{ + +using LoggerPtr = std::shared_ptr; + +inline LoggerPtr getLogger(const std::string& name) +{ + if (auto logger = spdlog::get(name)) + { + return logger; + } + + auto default_logger = spdlog::default_logger(); + CETL_DEBUG_ASSERT(default_logger, "default"); + + auto logger = default_logger->clone(name); + CETL_DEBUG_ASSERT(logger, name.c_str()); + + apply_logger_env_levels(logger); + register_logger(logger); + + return logger; +} + +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_LOGGING_HPP_INCLUDED diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index fd64c98..f01f503 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -5,6 +5,13 @@ #include "engine/application.hpp" +#include +#include +#include +#include +#include +#include + #include #include #include @@ -12,7 +19,9 @@ #include #include #include +#include #include +#include #include // NOLINT #include #include @@ -296,6 +305,51 @@ int daemonize() return -1; // Unreachable actually b/c of `::exit` call. } +/// Sets up the logging system. +/// +/// Both syslog and file logging sinks are used. +/// The syslog sink is used for the default logger only (with Info default level), +/// while the file sink is used for all loggers (with Debug default level). +/// +void setupLogging(const bool is_daemonized, const int argc, const char** const argv) +{ + using spdlog::sinks::syslog_sink_st; + using spdlog::sinks::rotating_file_sink_st; + + constexpr std::size_t log_files_max = 4; + constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB + + const std::string log_prefix = "ocvsmd"; + const std::string log_file_nm = log_prefix + ".log"; + const std::string log_file_dir = is_daemonized ? "/var/log/" : "./"; + const auto log_file_path = log_file_dir + log_file_nm; + + // Drop all existing loggers, including the default one, so that we can reconfigure them. + spdlog::drop_all(); + + const auto file_sink = std::make_shared(log_file_path, log_file_max_size, log_files_max); + file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); + + const int syslog_facility = is_daemonized ? LOG_DAEMON : LOG_USER; + const auto syslog_sink = std::make_shared(log_prefix, LOG_PID, syslog_facility, true); + syslog_sink->set_pattern("[%l] '%n' | %v"); + + // The default logger goes to all sinks. + // + const std::initializer_list sinks{syslog_sink, file_sink}; + const auto default_logger = std::make_shared("", sinks); + default_logger->flush_on(spdlog::level::trace); + register_logger(default_logger); + set_default_logger(default_logger); + + // Register specific subsystem loggers - they go to the file sink only. + // + register_logger(std::make_shared("ipc", file_sink)); + + // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug,ipc=trace`). + spdlog::cfg::load_argv_levels(argc, argv); +} + } // namespace int main(const int argc, const char** const argv) @@ -324,12 +378,16 @@ int main(const int argc, const char** const argv) setup_signal_handlers(); } - ::openlog("ocvsmd", LOG_PID, should_daemonize ? LOG_DAEMON : LOG_USER); - ::syslog(LOG_NOTICE, "ocvsmd daemon started."); // NOLINT *-vararg + setupLogging(should_daemonize, argc, argv); + + spdlog::info("OCVSMD started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); { Application application; if (const auto failure_str = application.init()) { + spdlog::critical("Failed to init application: {}", failure_str.value()); + + // Report the failure to the parent process (if daemonized; otherwise goes to stderr). write_string(pipe_write_fd, "Failed to init application: "); write_string(pipe_write_fd, failure_str.value().c_str()); ::exit(EXIT_FAILURE); @@ -343,11 +401,10 @@ int main(const int argc, const char** const argv) if (g_running == 0) { - ::syslog(LOG_NOTICE, "Received termination signal."); // NOLINT *-vararg + spdlog::debug("Received termination signal."); // NOLINT *-vararg } } - ::syslog(LOG_NOTICE, "ocvsmd daemon terminated."); // NOLINT *-vararg - ::closelog(); + spdlog::info("OCVSMD daemon terminated."); // NOLINT *-vararg return EXIT_SUCCESS; } From 4e2b60fa4ffd38177892fab01f8fbcb0ac6f5f23 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 15:15:43 +0200 Subject: [PATCH 057/156] client side logging --- src/cli/main.cpp | 90 +++++++++++++---- src/common/common_helpers.hpp | 45 +++++++++ src/common/ipc/client_router.cpp | 33 ++++--- src/common/ipc/ipc_types.hpp | 18 ---- src/common/ipc/pipe/unix_socket_base.hpp | 22 +++-- src/common/ipc/pipe/unix_socket_client.cpp | 10 +- src/common/ipc/pipe/unix_socket_server.cpp | 39 +++----- src/common/ipc/server_router.cpp | 3 +- src/common/logging.hpp | 3 +- src/daemon/engine/application.cpp | 27 +++-- src/daemon/engine/application.hpp | 2 + src/daemon/main.cpp | 110 +++++++++++---------- src/sdk/daemon.cpp | 27 ++--- submodules/nunavut | 2 +- 14 files changed, 266 insertions(+), 165 deletions(-) create mode 100644 src/common/common_helpers.hpp diff --git a/src/cli/main.cpp b/src/cli/main.cpp index b8b47c2..c484963 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -10,14 +10,19 @@ #include #include +#include +#include +#include #include #include #include #include +#include #include +#include #include // NOLINT -#include +#include namespace { @@ -25,7 +30,7 @@ namespace // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) volatile sig_atomic_t g_running = 1; -void signal_handler(const int sig) +void signalHandler(const int sig) { switch (sig) { @@ -38,26 +43,73 @@ void signal_handler(const int sig) } } -void setup_signal_handlers() +void setupSignalHandlers() { struct sigaction sigbreak {}; - sigbreak.sa_handler = &signal_handler; + sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); } +/// Sets up the logging system. +/// +/// File sink is used for all loggers (with Info default level). +/// +void setupLogging(const int argc, const char** const argv) +{ + using spdlog::sinks::rotating_file_sink_st; + + try + { + constexpr std::size_t log_max_files = 4; + constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB + + const std::string log_prefix = "ocvsmd-cli"; + const std::string log_file_nm = log_prefix + ".log"; + const auto log_file_path = "./" + log_file_nm; + + // Drop all existing loggers, including the default one, so that we can reconfigure them. + spdlog::drop_all(); + + const auto file_sink = std::make_shared( // + log_file_path, + log_file_max_size, + log_max_files); + file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); + + const auto default_logger = std::make_shared("", file_sink); + default_logger->flush_on(spdlog::level::trace); + register_logger(default_logger); + set_default_logger(default_logger); + + // Register specific subsystem loggers. + // + register_logger(std::make_shared("ipc", file_sink)); + register_logger(std::make_shared("sdk", file_sink)); + + // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug,ipc=trace`). + spdlog::cfg::load_argv_levels(argc, argv); + + } catch (const std::exception& ex) + { + std::cerr << "Failed to setup logging: " << ex.what() << '\n'; + std::exit(EXIT_FAILURE); + } +} + } // namespace -int main(const int, const char** const) +int main(const int argc, const char** const argv) { using std::chrono_literals::operator""s; - setup_signal_handlers(); + setupSignalHandlers(); + setupLogging(argc, argv); spdlog::info("ocvsmd cli started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); - ::openlog("ocvsmd-cli", LOG_PID, LOG_USER); - ::syslog(LOG_NOTICE, "ocvsmd cli started."); // NOLINT *-vararg + int result = EXIT_SUCCESS; + try { auto& memory = *cetl::pmr::new_delete_resource(); ocvsmd::platform::SingleThreadedExecutor executor; @@ -68,7 +120,6 @@ int main(const int, const char** const) std::cerr << "Failed to create daemon.\n"; return EXIT_FAILURE; } - while (g_running != 0) { const auto spin_result = executor.spinOnce(); @@ -80,20 +131,23 @@ int main(const int, const char** const) timeout = std::min(timeout, spin_result.next_exec_time.value() - executor.now()); } - // TODO: Don't ignore polling failures; come up with a strategy to handle them. - // Probably we should log it, break the loop, - // and exit with a failure code (b/c it is a critical and unexpected error). - auto maybe_poll_failure = executor.pollAwaitableResourcesFor(cetl::make_optional(timeout)); - (void) maybe_poll_failure; + if (const auto maybe_poll_failure = executor.pollAwaitableResourcesFor(cetl::make_optional(timeout))) + { + spdlog::warn("Failed to poll awaitable resources."); + } } if (g_running == 0) { - ::syslog(LOG_NOTICE, "Received termination signal."); // NOLINT *-vararg + spdlog::debug("Received termination signal."); } + + } catch (const std::exception& ex) + { + spdlog::critical("Unhandled exception: {}", ex.what()); + result = EXIT_FAILURE; } - ::syslog(LOG_NOTICE, "ocvsmd cli terminated."); // NOLINT *-vararg - ::closelog(); + spdlog::info("OCVSMD cli terminated."); - return 0; + return result; } diff --git a/src/common/common_helpers.hpp b/src/common/common_helpers.hpp new file mode 100644 index 0000000..ea13d9d --- /dev/null +++ b/src/common/common_helpers.hpp @@ -0,0 +1,45 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_HELPERS_HPP_INCLUDED +#define OCVSMD_COMMON_HELPERS_HPP_INCLUDED + +#include + +#include +#include + +namespace ocvsmd +{ +namespace common +{ + +template +bool performWithoutThrowing(Action&& action) noexcept +{ +#if defined(__cpp_exceptions) + try +#endif + { + std::forward(action)(); + return true; + } +#if defined(__cpp_exceptions) + catch (const std::exception& ex) + { + spdlog::critical("Unexpected C++ exception is caught: {}", ex.what()); + + } catch (...) + { + spdlog::critical("Unexpected unknown exception is caught!"); + } + return false; +#endif +} + +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_HELPERS_HPP_INCLUDED diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index c0591f8..46474f7 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -5,9 +5,11 @@ #include "client_router.hpp" +#include "common_helpers.hpp" #include "dsdl_helpers.hpp" #include "gateway.hpp" #include "ipc_types.hpp" +#include "logging.hpp" #include "pipe/client_pipe.hpp" #include "ocvsmd/common/ipc/RouteChannelEnd_0_1.hpp" @@ -23,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -43,6 +44,7 @@ class ClientRouterImpl final : public ClientRouter ClientRouterImpl(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) : memory_{memory} , client_pipe_{std::move(client_pipe)} + , logger_{getLogger("ipc")} , next_tag_{0} , is_connected_{false} { @@ -104,7 +106,7 @@ class ClientRouterImpl final : public ClientRouter , endpoint_{endpoint} , next_sequence_{0} { - ::syslog(LOG_DEBUG, "Gateway(tag=%zu).", endpoint.tag); // NOLINT + router_.logger_->trace("Gateway(tag={}).", endpoint.tag); } GatewayImpl(const GatewayImpl&) = delete; @@ -114,7 +116,7 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { - ::syslog(LOG_DEBUG, "~Gateway(tag=%zu, seq=%zu).", endpoint_.tag, next_sequence_); // NOLINT + router_.logger_->trace("~Gateway(tag={}).", endpoint_.tag); performWithoutThrowing([this] { // @@ -264,7 +266,7 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Connected) const { - ::syslog(LOG_DEBUG, "Pipe is connected."); // NOLINT + logger_->debug("Pipe is connected."); // It's not enough to consider the server route connected by the pipe event. // We gonna initiate `RouteConnect` negotiation (see `handleRouteConnect`). @@ -315,7 +317,7 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD int handlePipeEvent(const pipe::ClientPipe::Event::Disconnected) { - ::syslog(LOG_DEBUG, "Pipe is disconnected."); // NOLINT + logger_->debug("Pipe is disconnected."); if (is_connected_) { @@ -341,11 +343,10 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD int handleRouteConnect(const RouteConnect_0_1& rt_conn) { - ::syslog(LOG_DEBUG, // NOLINT - "Route connect response (ver='%d.%d', err=%d).", - static_cast(rt_conn.version.major), - static_cast(rt_conn.version.minor), - static_cast(rt_conn.error_code)); + logger_->debug("Route connect response (ver='{}.{}', err={}).", + static_cast(rt_conn.version.major), + static_cast(rt_conn.version.minor), + static_cast(rt_conn.error_code)); if (!is_connected_) { @@ -376,17 +377,24 @@ class ClientRouterImpl final : public ClientRouter { if (const auto gateway = tag_to_gw->second.lock()) { + logger_->trace("Route Ch Msg (tag={}, seq={}).", route_ch_msg.tag, route_ch_msg.sequence); + return gateway->event(detail::Gateway::Event::Message{route_ch_msg.sequence, msg_real_payload}); } } - // Nothing to do here with unsolicited messages - just ignore them. + // Nothing to do here with unsolicited messages - just trace and ignore them. + // + logger_->debug("Route Ch Unsolicited Msg (tag={}, seq={}, srv=0x{:X}).", + route_ch_msg.tag, + route_ch_msg.sequence, + route_ch_msg.service_id); return 0; } CETL_NODISCARD int handleRouteChannelEnd(const RouteChannelEnd_0_1& route_ch_end) { - ::syslog(LOG_DEBUG, "Route Ch End (tag=%zu, err=%d).", route_ch_end.tag, route_ch_end.error_code); // NOLINT + logger_->debug("Route Ch End (tag={}, err={}).", route_ch_end.tag, route_ch_end.error_code); const Endpoint endpoint{route_ch_end.tag}; const auto error_code = static_cast(route_ch_end.error_code); @@ -400,6 +408,7 @@ class ClientRouterImpl final : public ClientRouter cetl::pmr::memory_resource& memory_; pipe::ClientPipe::Ptr client_pipe_; + LoggerPtr logger_; Endpoint::Tag next_tag_; bool is_connected_; MapOfWeakGateways map_of_gateways_; diff --git a/src/common/ipc/ipc_types.hpp b/src/common/ipc/ipc_types.hpp index 1e7041d..aeca797 100644 --- a/src/common/ipc/ipc_types.hpp +++ b/src/common/ipc/ipc_types.hpp @@ -10,7 +10,6 @@ #include #include -#include namespace ocvsmd { @@ -35,23 +34,6 @@ enum class ErrorCode : int // NOLINT using Payload = cetl::span; using Payloads = cetl::span; -template -void performWithoutThrowing(Action&& action) noexcept -{ -#if defined(__cpp_exceptions) - try -#endif - { - std::forward(action)(); - } -#if defined(__cpp_exceptions) - catch (...) - { - ::syslog(LOG_WARNING, "Unexpected exception is caught!"); // NOLINT - } -#endif -} - } // namespace ipc } // namespace common } // namespace ocvsmd diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp index 423e361..fdb2976 100644 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -7,6 +7,7 @@ #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED #include "ipc/ipc_types.hpp" +#include "logging.hpp" #include "ocvsmd/platform/posix_utils.hpp" #include @@ -18,7 +19,6 @@ #include #include #include -#include #include #include @@ -43,6 +43,11 @@ class UnixSocketBase UnixSocketBase() = default; ~UnixSocketBase() = default; + Logger& logger() const noexcept + { + return *logger_; + } + CETL_NODISCARD static int send(const int output_fd, const Payloads payloads) { // 1. Write the message header (signature and total size of the following fragments). @@ -80,7 +85,7 @@ class UnixSocketBase } template - CETL_NODISCARD static int receiveMessage(const int input_fd, Action&& action) + CETL_NODISCARD int receiveMessage(const int input_fd, Action&& action) { // 1. Receive and validate the message header. // @@ -93,8 +98,7 @@ class UnixSocketBase return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); })) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_ERR, "Failed to read message header (fd=%d): %s", input_fd, std::strerror(err)); + logger_->error("Failed to read message header (fd={}): {}", input_fd, std::strerror(err)); return err; } @@ -114,16 +118,16 @@ class UnixSocketBase // 2. Read message payload. // - auto read_and_act = [input_fd, act = std::forward(action)](const cetl::span buf_span) { + auto read_and_act = [this, input_fd, act = std::forward(action)]( // + const cetl::span buf_span) { // ssize_t read = 0; - if (const auto err = platform::posixSyscallError([input_fd, buf_span, &read] { + if (const auto err = platform::posixSyscallError([this, input_fd, buf_span, &read] { // return read = ::read(input_fd, buf_span.data(), buf_span.size()); })) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_ERR, "Failed to read message payload (fd=%d): %s", input_fd, std::strerror(err)); + logger_->error("Failed to read message payload (fd={}): {}", input_fd, std::strerror(err)); return err; } if (read != buf_span.size()) @@ -154,6 +158,8 @@ class UnixSocketBase static constexpr std::uint32_t MsgSignature = 0x5356434F; // 'OCVS' static constexpr std::size_t MsgMaxSize = 1ULL << 20ULL; // 1 MB + LoggerPtr logger_{getLogger("ipc")}; + }; // UnixSocketBase } // namespace pipe diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp index 5df5056..5151683 100644 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -62,7 +62,7 @@ CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) return client_fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); })) { - std::cerr << "Failed to create socket: " << std::strerror(err) << "\n"; + logger().error("Failed to create socket: {}", std::strerror(err)); return err; } @@ -83,7 +83,7 @@ CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { - std::cerr << "Failed to connect to server: " << std::strerror(err) << "\n"; + logger().error("Failed to connect to server: {}", std::strerror(err)); return err; } @@ -107,13 +107,11 @@ void UnixSocketClient::handle_socket() { if (err == -1) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "End of server stream - closing connection."); + logger().debug("End of server stream - closing connection."); } else { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_WARNING, "Failed to handle server response - closing connection: %s", std::strerror(err)); + logger().warn("Failed to handle server response - closing connection: {}", std::strerror(err)); } socket_callback_.reset(); diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index 794a681..b0d6b53 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -5,6 +5,7 @@ #include "unix_socket_server.hpp" +#include "logging.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" #include "ocvsmd/platform/posix_utils.hpp" @@ -18,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -39,20 +39,19 @@ constexpr int MaxConnections = 5; class ClientContextImpl final : public detail::ClientContext { public: - ClientContextImpl(const UnixSocketServer::ClientId id, const int fd) + ClientContextImpl(const UnixSocketServer::ClientId id, const int fd, Logger& logger) : id_{id} , fd_{fd} + , logger_{logger} { CETL_DEBUG_ASSERT(fd_ != -1, ""); - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_NOTICE, "New client connection on fd=%d (id=%zu).", fd, id); + logger_.trace("ClientContextImpl(fd={}, id={}).", fd_, id_); } ~ClientContextImpl() override { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_NOTICE, "Closing client connection on fd=%d (id=%zu).", fd_, id_); + logger_.trace("~ClientContextImpl(fd={}, id={}).", fd_, id_); platform::posixSyscallError([this] { // @@ -73,6 +72,7 @@ class ClientContextImpl final : public detail::ClientContext private: const UnixSocketServer::ClientId id_; const int fd_; + Logger& logger_; libcyphal::IExecutor::Callback::Any fd_callback_; }; // ClientContextImpl @@ -111,8 +111,7 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) return server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); })) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_ERR, "Failed to create server socket: %s", std::strerror(err)); + logger().error("Failed to create server socket: {}", std::strerror(err)); return err; } @@ -133,8 +132,7 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_ERR, "Failed to bind server socket: %s", std::strerror(err)); + logger().error("Failed to bind server socket: {}", std::strerror(err)); return err; } @@ -143,8 +141,7 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) return ::listen(server_fd_, MaxConnections); })) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_ERR, "Failed to listen on server socket: %s", std::strerror(err)); + logger().error("Failed to listen on server socket: {}", std::strerror(err)); return err; } @@ -168,8 +165,7 @@ void UnixSocketServer::handleAccept() return client_fd = ::accept(server_fd_, nullptr, nullptr); })) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_WARNING, "Failed to accept client connection: %s", std::strerror(err)); + logger().warn("Failed to accept client connection: {}", std::strerror(err)); return; } @@ -177,7 +173,7 @@ void UnixSocketServer::handleAccept() CETL_DEBUG_ASSERT(client_fd_to_context_.find(client_fd) == client_fd_to_context_.end(), ""); const ClientId new_client_id = ++unique_client_id_counter_; - auto client_context = std::make_unique(new_client_id, client_fd); + auto client_context = std::make_unique(new_client_id, client_fd, logger()); // client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( [this, new_client_id, client_fd](const auto&) { @@ -201,17 +197,14 @@ void UnixSocketServer::handleClientRequest(const ClientId client_id, const int c { if (err == -1) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_DEBUG, "End of client stream - closing connection (id=%zu, fd=%d).", client_id, client_fd); + logger().debug("End of client stream - closing connection (id={}, fd={}).", client_id, client_fd); } else { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_WARNING, - "Failed to handle client request - closing connection (id=%zu, fd=%d): %s", - client_id, - client_fd, - std::strerror(err)); + logger().warn("Failed to handle client request - closing connection (id={}, fd={}): {}", + client_id, + client_fd, + std::strerror(err)); } client_id_to_fd_.erase(client_id); diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 435cfb3..41bb478 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -5,6 +5,7 @@ #include "server_router.hpp" +#include "common_helpers.hpp" #include "dsdl_helpers.hpp" #include "gateway.hpp" #include "ipc_types.hpp" @@ -100,7 +101,7 @@ class ServerRouterImpl final : public ServerRouter , endpoint_{endpoint} , next_sequence_{0} { - router.logger_->trace("Gateway(cl={}, tag={}).", endpoint.client_id, endpoint.tag); + router_.logger_->trace("Gateway(cl={}, tag={}).", endpoint.client_id, endpoint.tag); } GatewayImpl(const GatewayImpl&) = delete; diff --git a/src/common/logging.hpp b/src/common/logging.hpp index 0bf9da8..3259799 100644 --- a/src/common/logging.hpp +++ b/src/common/logging.hpp @@ -17,7 +17,8 @@ namespace ocvsmd namespace common { -using LoggerPtr = std::shared_ptr; +using Logger = spdlog::logger; +using LoggerPtr = std::shared_ptr; inline LoggerPtr getLogger(const std::string& name) { diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 9ff7423..21e07a8 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -13,6 +13,8 @@ #include #include +#include + #include #include #include @@ -20,7 +22,6 @@ #include #include #include -#include #include namespace ocvsmd @@ -71,8 +72,8 @@ cetl::optional Application::init() using Ch = ExecCmdChannel; ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto& request) { // - ::syslog(LOG_DEBUG, "D << 🆕 Ch created."); // NOLINT - ::syslog(LOG_DEBUG, "D << 🔵 Ch ininital Msg='%s'.", request.some_stuff.data()); // NOLINT + logger_->info("D << 🆕 Ch created."); + logger_->info("D << 🔵 Ch initial Msg='{}'.", reinterpret_cast(request.some_stuff.data())); ipc_exec_cmd_ch_ = std::move(ch); ipc_exec_cmd_ch_->subscribe([this](const auto& event_var) { @@ -81,9 +82,9 @@ cetl::optional Application::init() cetl::make_overloaded( [this](const ExecCmdChannel::Connected&) { // - ::syslog(LOG_DEBUG, "D << 🟢 Ch connected."); // NOLINT + logger_->info("D << 🟢 Ch connected."); - ::syslog(LOG_DEBUG, "D >> 🔵 Ch 'SR' msg."); // NOLINT + logger_->info("D >> 🔵 Ch 'SR' msg."); ExecCmd cmd{&memory_}; cmd.some_stuff.push_back('S'); cmd.some_stuff.push_back('R'); @@ -93,16 +94,15 @@ cetl::optional Application::init() }, [this](const ExecCmdChannel::Input& input) { // - ::syslog(LOG_DEBUG, "D << 🔵 Ch Msg='%s'.", input.some_stuff.data()); // NOLINT + logger_->info("D << 🔵 Ch Msg='{}'.", reinterpret_cast(input.some_stuff.data())); - ::syslog(LOG_DEBUG, "D >> 🔵 Ch '%s' msg.", input.some_stuff.data()); // NOLINT + logger_->info("D >> 🔵 Ch '{}' msg.", reinterpret_cast(input.some_stuff.data())); const int result = ipc_exec_cmd_ch_->send(input); (void) result; }, [this](const ExecCmdChannel::Completed& completed) { // - // NOLINTNEXTLINE - ::syslog(LOG_DEBUG, "D << 🔴 Ch Completed (err=%d).", static_cast(completed.error_code)); + logger_->info("D << 🔴 Ch Completed (err={}).", static_cast(completed.error_code)); ipc_exec_cmd_ch_.reset(); }), event_var); @@ -132,11 +132,10 @@ void Application::runWhile(const std::function& loop_predicate) timeout = std::min(timeout, spin_result.next_exec_time.value() - executor_.now()); } - // TODO: Don't ignore polling failures; come up with a strategy to handle them. - // Probably we should log it, break the loop, - // and exit with a failure code (b/c it is a critical and unexpected error). - auto maybe_poll_failure = executor_.pollAwaitableResourcesFor(cetl::make_optional(timeout)); - (void) maybe_poll_failure; + if (const auto maybe_poll_failure = executor_.pollAwaitableResourcesFor(cetl::make_optional(timeout))) + { + spdlog::warn("Failed to poll awaitable resources."); + } } } diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/application.hpp index 2a37da9..22f0134 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/application.hpp @@ -7,6 +7,7 @@ #define OCVSMD_DAEMON_ENGINE_APPLICATION_HPP_INCLUDED #include "cyphal/udp_transport_bag.hpp" +#include "logging.hpp" #include "ocvsmd/platform/defines.hpp" #include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" @@ -47,6 +48,7 @@ class Application static UniqueId getUniqueId(); + common::LoggerPtr logger_{common::getLogger("engine")}; ocvsmd::platform::SingleThreadedExecutor executor_; cetl::pmr::memory_resource& memory_{*cetl::pmr::get_default_resource()}; cyphal::UdpTransportBag udp_transport_bag_{memory_, executor_}; diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index f01f503..42580b5 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -26,7 +27,6 @@ #include #include #include -#include #include #include @@ -38,7 +38,7 @@ const auto* const s_init_complete = "init_complete"; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) volatile sig_atomic_t g_running = 1; -extern "C" void signal_handler(const int sig) +extern "C" void signalHandler(const int sig) { switch (sig) { @@ -51,26 +51,26 @@ extern "C" void signal_handler(const int sig) } } -void setup_signal_handlers() +void setupSignalHandlers() { struct sigaction sigbreak {}; - sigbreak.sa_handler = &signal_handler; + sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); } -bool write_string(const int fd, const char* const str) +bool writeString(const int fd, const char* const str) { const auto str_len = strlen(str); return str_len == ::write(fd, str, str_len); } -void exit_with_failure(const int fd, const char* const msg) +void exitWithFailure(const int fd, const char* const msg) { const char* const err_txt = std::strerror(errno); - write_string(fd, msg); - write_string(fd, err_txt); + writeString(fd, msg); + writeString(fd, err_txt); ::exit(EXIT_FAILURE); } @@ -101,7 +101,7 @@ void step_01_close_all_file_descriptors(std::array& pipe_fds) void step_02_03_setup_signal_handlers() { - setup_signal_handlers(); + setupSignalHandlers(); } void step_04_sanitize_environment() @@ -140,7 +140,7 @@ void step_06_create_new_session(const int pipe_write_fd) { if (::setsid() < 0) { - exit_with_failure(pipe_write_fd, "Failed to setsid: "); + exitWithFailure(pipe_write_fd, "Failed to setsid: "); } } @@ -152,7 +152,7 @@ void step_07_08_fork_and_exit_again(int& pipe_write_fd) const pid_t pid = fork(); if (pid < 0) { - exit_with_failure(pipe_write_fd, "Failed to fork: "); + exitWithFailure(pipe_write_fd, "Failed to fork: "); } if (pid > 0) { @@ -167,7 +167,7 @@ void step_09_redirect_stdio_to_devnull(const int pipe_write_fd) const int fd = ::open("/dev/null", O_RDWR); // NOLINT *-vararg if (fd == -1) { - exit_with_failure(pipe_write_fd, "Failed to open(/dev/null): "); + exitWithFailure(pipe_write_fd, "Failed to open(/dev/null): "); } ::dup2(fd, STDIN_FILENO); @@ -189,7 +189,7 @@ void step_11_change_curr_dir(const int pipe_write_fd) { if (::chdir("/") != 0) { - exit_with_failure(pipe_write_fd, "Failed to chdir(/): "); + exitWithFailure(pipe_write_fd, "Failed to chdir(/): "); } } @@ -198,17 +198,17 @@ void step_12_create_pid_file(const int pipe_write_fd) const int fd = ::open("/var/run/ocvsmd.pid", O_RDWR | O_CREAT, 0644); // NOLINT *-vararg if (fd == -1) { - exit_with_failure(pipe_write_fd, "Failed to create on PID file: "); + exitWithFailure(pipe_write_fd, "Failed to create on PID file: "); } if (::lockf(fd, F_TLOCK, 0) == -1) { - exit_with_failure(pipe_write_fd, "Failed to lock PID file: "); + exitWithFailure(pipe_write_fd, "Failed to lock PID file: "); } if (::ftruncate(fd, 0) != 0) { - exit_with_failure(pipe_write_fd, "Failed to ftruncate PID file: "); + exitWithFailure(pipe_write_fd, "Failed to ftruncate PID file: "); } constexpr std::size_t max_pid_str_len = 32; @@ -216,7 +216,7 @@ void step_12_create_pid_file(const int pipe_write_fd) const auto len = ::snprintf(buf.data(), buf.size(), "%ld\n", static_cast(::getpid())); // NOLINT *-vararg if (::write(fd, buf.data(), len) != len) { - exit_with_failure(pipe_write_fd, "Failed to write to PID file: "); + exitWithFailure(pipe_write_fd, "Failed to write to PID file: "); } // Keep the PID file open until the process exits. @@ -236,7 +236,7 @@ void step_14_notify_init_complete(int& pipe_write_fd) // hence available in both the original and the daemon process. // Closing the writing end of the pipe will signal the original process that the daemon is ready. - write_string(pipe_write_fd, s_init_complete); + writeString(pipe_write_fd, s_init_complete); ::close(pipe_write_fd); pipe_write_fd = -1; } @@ -311,43 +311,53 @@ int daemonize() /// The syslog sink is used for the default logger only (with Info default level), /// while the file sink is used for all loggers (with Debug default level). /// -void setupLogging(const bool is_daemonized, const int argc, const char** const argv) +void setupLogging(const int err_fd, const bool is_daemonized, const int argc, const char** const argv) { using spdlog::sinks::syslog_sink_st; using spdlog::sinks::rotating_file_sink_st; - constexpr std::size_t log_files_max = 4; - constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB + try + { + constexpr std::size_t log_files_max = 4; + constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB - const std::string log_prefix = "ocvsmd"; - const std::string log_file_nm = log_prefix + ".log"; - const std::string log_file_dir = is_daemonized ? "/var/log/" : "./"; - const auto log_file_path = log_file_dir + log_file_nm; + const std::string log_prefix = "ocvsmd"; + const std::string log_file_nm = log_prefix + ".log"; + const std::string log_file_dir = is_daemonized ? "/var/log/" : "./"; + const auto log_file_path = log_file_dir + log_file_nm; - // Drop all existing loggers, including the default one, so that we can reconfigure them. - spdlog::drop_all(); + // Drop all existing loggers, including the default one, so that we can reconfigure them. + spdlog::drop_all(); - const auto file_sink = std::make_shared(log_file_path, log_file_max_size, log_files_max); - file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); + const auto file_sink = std::make_shared(log_file_path, log_file_max_size, log_files_max); + file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); - const int syslog_facility = is_daemonized ? LOG_DAEMON : LOG_USER; - const auto syslog_sink = std::make_shared(log_prefix, LOG_PID, syslog_facility, true); - syslog_sink->set_pattern("[%l] '%n' | %v"); + const int syslog_facility = is_daemonized ? LOG_DAEMON : LOG_USER; + const auto syslog_sink = std::make_shared(log_prefix, LOG_PID, syslog_facility, true); + syslog_sink->set_pattern("[%l] '%n' | %v"); - // The default logger goes to all sinks. - // - const std::initializer_list sinks{syslog_sink, file_sink}; - const auto default_logger = std::make_shared("", sinks); - default_logger->flush_on(spdlog::level::trace); - register_logger(default_logger); - set_default_logger(default_logger); + // The default logger goes to all sinks. + // + const std::initializer_list sinks{syslog_sink, file_sink}; + const auto default_logger = std::make_shared("", sinks); + default_logger->flush_on(spdlog::level::trace); + register_logger(default_logger); + set_default_logger(default_logger); - // Register specific subsystem loggers - they go to the file sink only. - // - register_logger(std::make_shared("ipc", file_sink)); + // Register specific subsystem loggers - they go to the file sink only. + // + register_logger(std::make_shared("ipc", file_sink)); + register_logger(std::make_shared("engine", file_sink)); + + // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug,ipc=trace`). + spdlog::cfg::load_argv_levels(argc, argv); - // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug,ipc=trace`). - spdlog::cfg::load_argv_levels(argc, argv); + } catch (const std::exception& ex) + { + writeString(err_fd, "Failed to setup logging: "); + writeString(err_fd, ex.what()); + ::exit(EXIT_FAILURE); + } } } // namespace @@ -375,10 +385,10 @@ int main(const int argc, const char** const argv) } else { - setup_signal_handlers(); + setupSignalHandlers(); } - setupLogging(should_daemonize, argc, argv); + setupLogging(pipe_write_fd, should_daemonize, argc, argv); spdlog::info("OCVSMD started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); { @@ -388,8 +398,8 @@ int main(const int argc, const char** const argv) spdlog::critical("Failed to init application: {}", failure_str.value()); // Report the failure to the parent process (if daemonized; otherwise goes to stderr). - write_string(pipe_write_fd, "Failed to init application: "); - write_string(pipe_write_fd, failure_str.value().c_str()); + writeString(pipe_write_fd, "Failed to init application: "); + writeString(pipe_write_fd, failure_str.value().c_str()); ::exit(EXIT_FAILURE); } if (should_daemonize) @@ -401,10 +411,10 @@ int main(const int argc, const char** const argv) if (g_running == 0) { - spdlog::debug("Received termination signal."); // NOLINT *-vararg + spdlog::debug("Received termination signal."); } } - spdlog::info("OCVSMD daemon terminated."); // NOLINT *-vararg + spdlog::info("OCVSMD daemon terminated."); return EXIT_SUCCESS; } diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 6e6ce55..fea518a 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -7,8 +7,8 @@ #include "ipc/channel.hpp" #include "ipc/client_router.hpp" -#include "ipc/pipe/client_pipe.hpp" #include "ipc/pipe/unix_socket_client.hpp" +#include "logging.hpp" #include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -32,6 +33,7 @@ class DaemonImpl final : public Daemon public: DaemonImpl(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) : memory_{memory} + , logger_{common::getLogger("sdk")} { using ClientPipe = common::ipc::pipe::UnixSocketClient; @@ -41,47 +43,45 @@ class DaemonImpl final : public Daemon CETL_NODISCARD int start() { - const int result = ipc_router_->start(); - if (result != 0) + if (const int err = ipc_router_->start()) { - // NOLINTNEXTLINE *-vararg - ::syslog(LOG_ERR, "Failed to start IPC router: %d", result); - return result; + logger_->error("Failed to start IPC router: {}", std::strerror(err)); + return err; } ipc_exec_cmd_ch_ = ipc_router_->makeChannel("daemon"); - ::syslog(LOG_DEBUG, "C << 🆕 Ch created."); // NOLINT + logger_->debug("C << 🆕 Ch created."); ipc_exec_cmd_ch_->subscribe([this](const auto& event_var) { // cetl::visit( // cetl::make_overloaded( [this](const ExecCmdChannel::Connected&) { // - ::syslog(LOG_DEBUG, "C << 🟢 Ch connected."); // NOLINT + logger_->info("C << 🟢 Ch connected."); ExecCmd cmd{&memory_}; cmd.some_stuff.push_back('C'); cmd.some_stuff.push_back('L'); cmd.some_stuff.push_back('\0'); - ::syslog(LOG_DEBUG, "C >> 🔵 Ch 'CL' msg."); // NOLINT + logger_->info("C >> 🔵 Ch 'CL' msg."); const int result = ipc_exec_cmd_ch_->send(cmd); (void) result; }, [this](const ExecCmdChannel::Input& input) { // - ::syslog(LOG_DEBUG, "C << 🔵 Ch Msg='%s'.", input.some_stuff.data()); // NOLINT + logger_->info("C << 🔵 Ch Msg='{}'.", reinterpret_cast(input.some_stuff.data())); if (countdown_--) { - ::syslog(LOG_DEBUG, "C >> 🔵 Ch '%s' msg.", input.some_stuff.data()); // NOLINT + logger_->info("C >> 🔵 Ch '{}' msg.", + reinterpret_cast(input.some_stuff.data())); const int result = ipc_exec_cmd_ch_->send(input); (void) result; } }, [this](const ExecCmdChannel::Completed& completed) { // - // NOLINTNEXTLINE - ::syslog(LOG_DEBUG, "C << 🔴 Ch Completed (err=%d).", static_cast(completed.error_code)); + logger_->info("C << 🔴 Ch Completed (err={}).", static_cast(completed.error_code)); ipc_exec_cmd_ch_.reset(); }), event_var); @@ -95,6 +95,7 @@ class DaemonImpl final : public Daemon using ExecCmdChannel = common::ipc::Channel; cetl::pmr::memory_resource& memory_; + common::LoggerPtr logger_; common::ipc::ClientRouter::Ptr ipc_router_; cetl::optional ipc_exec_cmd_ch_; diff --git a/submodules/nunavut b/submodules/nunavut index 295b695..948b75d 160000 --- a/submodules/nunavut +++ b/submodules/nunavut @@ -1 +1 @@ -Subproject commit 295b69571ef46c2c2840ebab44fad915b323f03d +Subproject commit 948b75de19145eb8de01b9485aa2cd23a1494029 From 2e4ac064a8c9b2a0fb42d233ec9346a0e5a82f80 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 15:49:22 +0200 Subject: [PATCH 058/156] new line in the end of log --- src/cli/main.cpp | 13 ++++++++----- src/daemon/main.cpp | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index c484963..8eca3de 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -103,23 +103,26 @@ void setupLogging(const int argc, const char** const argv) int main(const int argc, const char** const argv) { using std::chrono_literals::operator""s; + using Executor = ocvsmd::platform::SingleThreadedExecutor; setupSignalHandlers(); setupLogging(argc, argv); - spdlog::info("ocvsmd cli started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); + spdlog::info("OCVSMD client started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); int result = EXIT_SUCCESS; try { - auto& memory = *cetl::pmr::new_delete_resource(); - ocvsmd::platform::SingleThreadedExecutor executor; + auto& memory = *cetl::pmr::new_delete_resource(); + Executor executor; const auto daemon = ocvsmd::sdk::Daemon::make(memory, executor); if (!daemon) { - std::cerr << "Failed to create daemon.\n"; + spdlog::critical("Failed to create daemon.\n"); + std::cerr << "Failed to create daemon."; return EXIT_FAILURE; } + while (g_running != 0) { const auto spin_result = executor.spinOnce(); @@ -147,7 +150,7 @@ int main(const int argc, const char** const argv) spdlog::critical("Unhandled exception: {}", ex.what()); result = EXIT_FAILURE; } - spdlog::info("OCVSMD cli terminated."); + spdlog::info("OCVSMD client terminated.\n"); return result; } diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 42580b5..9bed377 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -395,7 +395,7 @@ int main(const int argc, const char** const argv) Application application; if (const auto failure_str = application.init()) { - spdlog::critical("Failed to init application: {}", failure_str.value()); + spdlog::critical("Failed to init application: {}\n", failure_str.value()); // Report the failure to the parent process (if daemonized; otherwise goes to stderr). writeString(pipe_write_fd, "Failed to init application: "); @@ -414,7 +414,7 @@ int main(const int argc, const char** const argv) spdlog::debug("Received termination signal."); } } - spdlog::info("OCVSMD daemon terminated."); + spdlog::info("OCVSMD daemon terminated.\n"); return EXIT_SUCCESS; } From b8fa2f23905a4d85de1635a224d69c82db89ab14 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 15:58:33 +0200 Subject: [PATCH 059/156] revert NLs --- src/cli/main.cpp | 4 ++-- src/common/ipc/pipe/unix_socket_base.hpp | 4 ++-- src/common/ipc/pipe/unix_socket_client.cpp | 6 +++--- src/common/ipc/pipe/unix_socket_server.cpp | 10 +++++----- src/daemon/main.cpp | 4 ++-- src/sdk/daemon.cpp | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 8eca3de..0bf7c9c 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -118,7 +118,7 @@ int main(const int argc, const char** const argv) const auto daemon = ocvsmd::sdk::Daemon::make(memory, executor); if (!daemon) { - spdlog::critical("Failed to create daemon.\n"); + spdlog::critical("Failed to create daemon."); std::cerr << "Failed to create daemon."; return EXIT_FAILURE; } @@ -150,7 +150,7 @@ int main(const int argc, const char** const argv) spdlog::critical("Unhandled exception: {}", ex.what()); result = EXIT_FAILURE; } - spdlog::info("OCVSMD client terminated.\n"); + spdlog::info("OCVSMD client terminated."); return result; } diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp index fdb2976..9398581 100644 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ b/src/common/ipc/pipe/unix_socket_base.hpp @@ -98,7 +98,7 @@ class UnixSocketBase return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); })) { - logger_->error("Failed to read message header (fd={}): {}", input_fd, std::strerror(err)); + logger_->error("Failed to read message header (fd={}): {}.", input_fd, std::strerror(err)); return err; } @@ -127,7 +127,7 @@ class UnixSocketBase return read = ::read(input_fd, buf_span.data(), buf_span.size()); })) { - logger_->error("Failed to read message payload (fd={}): {}", input_fd, std::strerror(err)); + logger_->error("Failed to read message payload (fd={}): {}.", input_fd, std::strerror(err)); return err; } if (read != buf_span.size()) diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp index 5151683..ae2caa6 100644 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -62,7 +62,7 @@ CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) return client_fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); })) { - logger().error("Failed to create socket: {}", std::strerror(err)); + logger().error("Failed to create socket: {}.", std::strerror(err)); return err; } @@ -83,7 +83,7 @@ CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { - logger().error("Failed to connect to server: {}", std::strerror(err)); + logger().error("Failed to connect to server: {}.", std::strerror(err)); return err; } @@ -111,7 +111,7 @@ void UnixSocketClient::handle_socket() } else { - logger().warn("Failed to handle server response - closing connection: {}", std::strerror(err)); + logger().warn("Failed to handle server response - closing connection: {}.", std::strerror(err)); } socket_callback_.reset(); diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index b0d6b53..946220b 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -111,7 +111,7 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) return server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); })) { - logger().error("Failed to create server socket: {}", std::strerror(err)); + logger().error("Failed to create server socket: {}.", std::strerror(err)); return err; } @@ -132,7 +132,7 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { - logger().error("Failed to bind server socket: {}", std::strerror(err)); + logger().error("Failed to bind server socket: {}.", std::strerror(err)); return err; } @@ -141,7 +141,7 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) return ::listen(server_fd_, MaxConnections); })) { - logger().error("Failed to listen on server socket: {}", std::strerror(err)); + logger().error("Failed to listen on server socket: {}.", std::strerror(err)); return err; } @@ -165,7 +165,7 @@ void UnixSocketServer::handleAccept() return client_fd = ::accept(server_fd_, nullptr, nullptr); })) { - logger().warn("Failed to accept client connection: {}", std::strerror(err)); + logger().warn("Failed to accept client connection: {}.", std::strerror(err)); return; } @@ -201,7 +201,7 @@ void UnixSocketServer::handleClientRequest(const ClientId client_id, const int c } else { - logger().warn("Failed to handle client request - closing connection (id={}, fd={}): {}", + logger().warn("Failed to handle client request - closing connection (id={}, fd={}): {}.", client_id, client_fd, std::strerror(err)); diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 9bed377..42580b5 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -395,7 +395,7 @@ int main(const int argc, const char** const argv) Application application; if (const auto failure_str = application.init()) { - spdlog::critical("Failed to init application: {}\n", failure_str.value()); + spdlog::critical("Failed to init application: {}", failure_str.value()); // Report the failure to the parent process (if daemonized; otherwise goes to stderr). writeString(pipe_write_fd, "Failed to init application: "); @@ -414,7 +414,7 @@ int main(const int argc, const char** const argv) spdlog::debug("Received termination signal."); } } - spdlog::info("OCVSMD daemon terminated.\n"); + spdlog::info("OCVSMD daemon terminated."); return EXIT_SUCCESS; } diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index fea518a..3b64714 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -45,7 +45,7 @@ class DaemonImpl final : public Daemon { if (const int err = ipc_router_->start()) { - logger_->error("Failed to start IPC router: {}", std::strerror(err)); + logger_->error("Failed to start IPC router: {}.", std::strerror(err)); return err; } From 0cbbbb538ada90ec9bd2446806c8deba54ff9bb2 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 16:14:21 +0200 Subject: [PATCH 060/156] remove `flush_on`-s --- src/cli/main.cpp | 1 - src/daemon/main.cpp | 1 - 2 files changed, 2 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 0bf7c9c..246ef1f 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -79,7 +79,6 @@ void setupLogging(const int argc, const char** const argv) file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); const auto default_logger = std::make_shared("", file_sink); - default_logger->flush_on(spdlog::level::trace); register_logger(default_logger); set_default_logger(default_logger); diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 42580b5..635b4bd 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -340,7 +340,6 @@ void setupLogging(const int err_fd, const bool is_daemonized, const int argc, co // const std::initializer_list sinks{syslog_sink, file_sink}; const auto default_logger = std::make_shared("", sinks); - default_logger->flush_on(spdlog::level::trace); register_logger(default_logger); set_default_logger(default_logger); From a15cbf81bd2697f97b82a1e3fc9a3542e85515f6 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 16:20:48 +0200 Subject: [PATCH 061/156] fix clang-tidy issues --- src/common/ipc/pipe/unix_socket_client.cpp | 1 - src/daemon/engine/application.cpp | 3 +++ src/daemon/main.cpp | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp index ae2caa6..ea34f42 100644 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 21e07a8..f0bf295 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -73,6 +73,7 @@ cetl::optional Application::init() ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto& request) { // logger_->info("D << 🆕 Ch created."); + // NOLINTNEXTLINE logger_->info("D << 🔵 Ch initial Msg='{}'.", reinterpret_cast(request.some_stuff.data())); ipc_exec_cmd_ch_ = std::move(ch); @@ -94,8 +95,10 @@ cetl::optional Application::init() }, [this](const ExecCmdChannel::Input& input) { // + // NOLINTNEXTLINE logger_->info("D << 🔵 Ch Msg='{}'.", reinterpret_cast(input.some_stuff.data())); + // NOLINTNEXTLINE logger_->info("D >> 🔵 Ch '{}' msg.", reinterpret_cast(input.some_stuff.data())); const int result = ipc_exec_cmd_ch_->send(input); (void) result; diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 635b4bd..931ea48 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include From d3281938a072e2357c5aac6fd297b555c9bec9b6 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 16:33:02 +0200 Subject: [PATCH 062/156] fix clang-tidy issues --- src/daemon/main.cpp | 37 ++++++++++++++++---------- src/sdk/daemon.cpp | 3 ++- test/common/ipc/test_server_router.cpp | 4 +-- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 931ea48..e4e8e06 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -391,24 +391,33 @@ int main(const int argc, const char** const argv) setupLogging(pipe_write_fd, should_daemonize, argc, argv); spdlog::info("OCVSMD started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); + int result = EXIT_SUCCESS; { - Application application; - if (const auto failure_str = application.init()) + try { - spdlog::critical("Failed to init application: {}", failure_str.value()); - - // Report the failure to the parent process (if daemonized; otherwise goes to stderr). - writeString(pipe_write_fd, "Failed to init application: "); - writeString(pipe_write_fd, failure_str.value().c_str()); - ::exit(EXIT_FAILURE); - } - if (should_daemonize) + Application application; + if (const auto failure_str = application.init()) + { + spdlog::critical("Failed to init application: {}", failure_str.value()); + + // Report the failure to the parent process (if daemonized; otherwise goes to stderr). + writeString(pipe_write_fd, "Failed to init application: "); + writeString(pipe_write_fd, failure_str.value().c_str()); + ::exit(EXIT_FAILURE); + } + if (should_daemonize) + { + step_14_notify_init_complete(pipe_write_fd); + } + + application.runWhile([] { return g_running == 1; }); + + } catch (const std::exception& ex) { - step_14_notify_init_complete(pipe_write_fd); + spdlog::critical("Unhandled exception: {}", ex.what()); + result = EXIT_FAILURE; } - application.runWhile([] { return g_running == 1; }); - if (g_running == 0) { spdlog::debug("Received termination signal."); @@ -416,5 +425,5 @@ int main(const int argc, const char** const argv) } spdlog::info("OCVSMD daemon terminated."); - return EXIT_SUCCESS; + return result; } diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 3b64714..8600934 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -69,12 +69,13 @@ class DaemonImpl final : public Daemon }, [this](const ExecCmdChannel::Input& input) { // + // NOLINTNEXTLINE logger_->info("C << 🔵 Ch Msg='{}'.", reinterpret_cast(input.some_stuff.data())); if (countdown_--) { logger_->info("C >> 🔵 Ch '{}' msg.", - reinterpret_cast(input.some_stuff.data())); + reinterpret_cast(input.some_stuff.data())); // NOLINT const int result = ipc_exec_cmd_ch_->send(input); (void) result; } diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index a011ce4..8b33d2d 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -224,11 +224,11 @@ TEST_F(TestServerRouter, channel_send) const Channel::Output msg{&mr_}; EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannelMsg(msg, mr_, tag, seq++))) // .WillOnce(Return(0)); - EXPECT_THAT(maybe_channel->send(msg), 0); + EXPECT_THAT(maybe_channel->send(msg), 0); // NOLINT EXPECT_CALL(server_pipe_mock, send(cl_id, PayloadOfRouteChannelMsg(msg, mr_, tag, seq++))) // .WillOnce(Return(0)); - EXPECT_THAT(maybe_channel->send(msg), 0); + EXPECT_THAT(maybe_channel->send(msg), 0); // NOLINT } // NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) From 244b27ffc185b10146c2dec4d4c3ef84a8b30c32 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 16:55:09 +0200 Subject: [PATCH 063/156] make `getLogger` noexcept --- src/common/logging.hpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/common/logging.hpp b/src/common/logging.hpp index 3259799..54ee510 100644 --- a/src/common/logging.hpp +++ b/src/common/logging.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_COMMON_LOGGING_HPP_INCLUDED #define OCVSMD_COMMON_LOGGING_HPP_INCLUDED +#include "common_helpers.hpp" + #include #include @@ -20,7 +22,7 @@ namespace common using Logger = spdlog::logger; using LoggerPtr = std::shared_ptr; -inline LoggerPtr getLogger(const std::string& name) +inline LoggerPtr getLogger(const std::string& name) noexcept { if (auto logger = spdlog::get(name)) { @@ -34,7 +36,11 @@ inline LoggerPtr getLogger(const std::string& name) CETL_DEBUG_ASSERT(logger, name.c_str()); apply_logger_env_levels(logger); - register_logger(logger); + + performWithoutThrowing([&logger] { + // + register_logger(logger); + }); return logger; } From 80d8e7653fb0210075c8079571c1e9853e1bb82c Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 17:14:02 +0200 Subject: [PATCH 064/156] make it easier to compile for diff c++ stds --- CMakeLists.txt | 9 ++++++++- src/common/CMakeLists.txt | 2 +- src/daemon/engine/CMakeLists.txt | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 82ce964..2ecb046 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ enable_testing() set(NO_STATIC_ANALYSIS OFF CACHE BOOL "disable static analysis") -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 14 CACHE STRING "C++ standard to conform to") set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -56,6 +56,13 @@ endif () # Pull in Nunavut's cmake integration find_package("Nunavut" 3.0 REQUIRED) +# libcyphal requires PMR support for Nunavut generated code. +if (${CMAKE_CXX_STANDARD} STREQUAL "14") + set(CYPHAL_LANGUAGE_STANDARD "cetl++14-17") +else () + set(CYPHAL_LANGUAGE_STANDARD "c++${CMAKE_CXX_STANDARD}-pmr") +endif () + # Forward the revision information to the compiler so that we could expose it at runtime. This is entirely optional. execute_process( COMMAND git rev-parse --short=16 HEAD diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index ed60fdb..7a5046e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -14,7 +14,7 @@ add_cyphal_library( DSDL_NAMESPACES ${dsdl_ocvsmd_dir} ALLOW_EXPERIMENTAL_LANGUAGES LANGUAGE cpp - LANGUAGE_STANDARD cetl++14-17 + LANGUAGE_STANDARD ${CYPHAL_LANGUAGE_STANDARD} OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled OUT_LIBRARY_TARGET common_transpiled ) diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 9d26746..3993139 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -19,7 +19,7 @@ add_cyphal_library( DSDL_FILES ${dsdl_types_in_engine} ALLOW_EXPERIMENTAL_LANGUAGES LANGUAGE cpp - LANGUAGE_STANDARD cetl++14-17 + LANGUAGE_STANDARD ${CYPHAL_LANGUAGE_STANDARD} OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled OUT_LIBRARY_TARGET engine_transpiled ) From ce49d6c5f5149259fe19d745b0c40dec008e2fbc Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 17:18:47 +0200 Subject: [PATCH 065/156] enable CI for all (14,17,20) cpp stds --- .github/workflows/tests.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6419ab0..63f162c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,6 +7,7 @@ jobs: runs-on: ubuntu-24.04 strategy: matrix: + cpp_version: [14, 17, 20] toolchain: ['clang', 'gcc'] include: - toolchain: gcc @@ -29,7 +30,7 @@ jobs: clang-tidy --version - run: | cmake --version - cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} + cmake --preset OCVSMD-Linux -DCMAKE_CXX_STANDARD=${{ matrix.cpp_version }} -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Debug ctest --preset OCVSMD-Debug - uses: actions/upload-artifact@v4 @@ -43,6 +44,7 @@ jobs: runs-on: ubuntu-24.04 strategy: matrix: + cpp_version: [14, 17, 20] toolchain: ['clang', 'gcc'] build_type: [Release, MinSizeRel] include: @@ -61,7 +63,7 @@ jobs: sudo apt install gcc-multilib g++-multilib ninja-build - run: | cmake --version - cmake --preset OCVSMD-Linux -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} + cmake --preset OCVSMD-Linux -DCMAKE_CXX_STANDARD=${{ matrix.cpp_version }} -DCMAKE_C_COMPILER=${{ matrix.c-compiler }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx-compiler }} cmake --build --preset OCVSMD-Linux-Release ctest --preset OCVSMD-Release - uses: actions/upload-artifact@v4 From 0bf94fe924e3773207a8c3e13552e5f5e0a64bc3 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 17:27:57 +0200 Subject: [PATCH 066/156] fix CI --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 63f162c..b053208 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -36,7 +36,7 @@ jobs: - uses: actions/upload-artifact@v4 if: always() with: - name: ${{github.job}}_${{matrix.toolchain}} + name: ${{github.job}}_cpp${{ matrix.cpp_version }}_${{matrix.toolchain}} path: ${{github.workspace}}/**/* retention-days: 2 @@ -69,7 +69,7 @@ jobs: - uses: actions/upload-artifact@v4 if: always() with: - name: ${{github.job}}_${{matrix.toolchain}}_${{matrix.build_type}} + name: ${{github.job}}_cpp${{ matrix.cpp_version }}_${{matrix.toolchain}}_${{matrix.build_type}} path: ${{github.workspace}}/**/* retention-days: 2 From 2b20ab1ab38bc4ecbd6104633e9f3738016f9ddc Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 15 Jan 2025 19:45:52 +0200 Subject: [PATCH 067/156] setup unit tests logging --- src/common/logging.hpp | 2 + test/common/CMakeLists.txt | 2 +- test/common/main.cpp | 25 +++++++ test/daemon/engine/CMakeLists.txt | 2 +- test/daemon/engine/main.cpp | 25 +++++++ test/gtest_printer.hpp | 114 ++++++++++++++++++++++++++++++ 6 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 test/common/main.cpp create mode 100644 test/daemon/engine/main.cpp create mode 100644 test/gtest_printer.hpp diff --git a/src/common/logging.hpp b/src/common/logging.hpp index 54ee510..2a31fac 100644 --- a/src/common/logging.hpp +++ b/src/common/logging.hpp @@ -8,6 +8,8 @@ #include "common_helpers.hpp" +#include + #include #include diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index 8aff3d4..1613b23 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -6,13 +6,13 @@ cmake_minimum_required(VERSION 3.27) add_executable(common_tests + main.cpp ipc/test_client_router.cpp ipc/test_server_router.cpp ) target_link_libraries(common_tests ocvsmd_common GTest::gmock - GTest::gtest_main ) gtest_discover_tests(common_tests) diff --git a/test/common/main.cpp b/test/common/main.cpp new file mode 100644 index 0000000..4237a34 --- /dev/null +++ b/test/common/main.cpp @@ -0,0 +1,25 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "gtest_printer.hpp" + +#include + +#include + +int main(int argc, char** const argv) +{ + ocvsmd::GtestPrinter::setupLogging(argc, argv, "common_tests"); + + testing::InitGoogleTest(&argc, argv); + + // Adds a listener to the end. GoogleTest takes ownership. + // + auto printer = std::make_unique(); + auto& listeners = testing::UnitTest::GetInstance()->listeners(); + listeners.Append(printer.release()); + + return RUN_ALL_TESTS(); +} diff --git a/test/daemon/engine/CMakeLists.txt b/test/daemon/engine/CMakeLists.txt index e11f45d..960f5f7 100644 --- a/test/daemon/engine/CMakeLists.txt +++ b/test/daemon/engine/CMakeLists.txt @@ -6,12 +6,12 @@ cmake_minimum_required(VERSION 3.27) add_executable(engine_tests + main.cpp test_xxx.cpp ) target_link_libraries(engine_tests ocvsmd_engine GTest::gmock - GTest::gtest_main ) gtest_discover_tests(engine_tests) diff --git a/test/daemon/engine/main.cpp b/test/daemon/engine/main.cpp new file mode 100644 index 0000000..6c17fe4 --- /dev/null +++ b/test/daemon/engine/main.cpp @@ -0,0 +1,25 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "gtest_printer.hpp" + +#include + +#include + +int main(int argc, char** const argv) +{ + ocvsmd::GtestPrinter::setupLogging(argc, argv, "engine_tests"); + + testing::InitGoogleTest(&argc, argv); + + // Adds a listener to the end. GoogleTest takes ownership. + // + auto printer = std::make_unique(); + auto& listeners = testing::UnitTest::GetInstance()->listeners(); + listeners.Append(printer.release()); + + return RUN_ALL_TESTS(); +} diff --git a/test/gtest_printer.hpp b/test/gtest_printer.hpp new file mode 100644 index 0000000..b8584df --- /dev/null +++ b/test/gtest_printer.hpp @@ -0,0 +1,114 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_GTEST_PRINTER_HPP_INCLUDED +#define OCVSMD_GTEST_PRINTER_HPP_INCLUDED + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ + +class GtestPrinter final : public testing::EmptyTestEventListener +{ +public: + /// Sets up the logging system. + /// + /// File sink is used for all loggers (with Trace default level). + /// + static void setupLogging(const int argc, char** const argv, const std::string& log_prefix) + { + try + { + // Drop all existing loggers, including the default one, so that we can reconfigure them. + spdlog::drop_all(); + + const std::string log_file_nm = log_prefix + ".log"; + const auto file_sink = std::make_shared(log_file_nm); + + const auto default_logger = std::make_shared("", file_sink); + default_logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); + register_logger(default_logger); + set_default_logger(default_logger); + + // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug`). + // + spdlog::set_level(spdlog::level::trace); + spdlog::cfg::load_argv_levels(argc, argv); + + } catch (const std::exception& ex) + { + std::cerr << "Failed to setup logging: " << ex.what() << '\n'; + std::exit(EXIT_FAILURE); + } + } + +private: + // Fired before the test suite starts. + void OnTestSuiteStart(const testing::TestSuite& test_suite) override + { + spdlog::info("====================> TEST_SUITE {}", test_suite.name()); + } + + // Called before a test starts. + void OnTestStart(const testing::TestInfo& test_info) override + { + spdlog::info("--------------------------> TEST {}.{} 🔵…", test_info.test_suite_name(), test_info.name()); + } + + // Called after a failed assertion or a SUCCESS(). + void OnTestPartResult(const testing::TestPartResult& test_part_result) override + { + if (test_part_result.failed()) + { + spdlog::error("TEST Failure in {}:{} ❌\n{}", + test_part_result.file_name(), + test_part_result.line_number(), + test_part_result.summary()); + } + else + { + spdlog::debug("TEST Success in {}:{}\n{}", + test_part_result.file_name(), + test_part_result.line_number(), + test_part_result.summary()); + } + } + + // Called after a test ends. + void OnTestEnd(const testing::TestInfo& test_info) override + { + spdlog::info("<-------------------------- TEST {}.{} 🏁.", test_info.test_suite_name(), test_info.name()); + } + + // Fired after the test suite ends. + void OnTestSuiteEnd(const testing::TestSuite& test_suite) override + { + spdlog::info("<==================== TEST_SUITE {}", test_suite.name()); + spdlog::info(""); + } + + void OnTestProgramEnd(const testing::UnitTest&) override + { + spdlog::info("🏁.\n"); + } + +}; // GtestPrinter + +} // namespace ocvsmd + +#endif // OCVSMD_GTEST_PRINTER_HPP_INCLUDED From 30ca87c841d45879f1c01226ca8da9f813496380 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 17 Jan 2025 10:52:45 +0200 Subject: [PATCH 068/156] implemented server side of `exec_cmd` --- .../common/node_command/ExecCmd.0.1.dsdl | 4 - .../svc/node/ExecCmdSvcRequest.0.1.dsdl | 4 + .../svc/node/ExecCmdSvcResponse.0.1.dsdl | 4 + .../svc/node/UavcanNodeExecCmdReq.0.1.dsdl | 78 ++++++ .../svc/node/UavcanNodeExecCmdRes.0.1.dsdl | 19 ++ src/common/ipc/channel.hpp | 38 ++- src/common/ipc/client_router.cpp | 17 +- src/common/ipc/client_router.hpp | 6 +- src/common/ipc/gateway.hpp | 17 +- src/common/ipc/server_router.cpp | 29 +- src/common/ipc/server_router.hpp | 14 +- src/common/logging.hpp | 12 + src/common/svc/node/exec_cmd_spec.hpp | 36 +++ src/daemon/engine/CMakeLists.txt | 1 + src/daemon/engine/application.cpp | 46 +-- src/daemon/engine/application.hpp | 8 - .../engine/svc/node/exec_cmd_service.cpp | 261 ++++++++++++++++++ .../engine/svc/node/exec_cmd_service.hpp | 36 +++ src/daemon/engine/svc/svc_helpers.hpp | 93 +++++++ src/sdk/daemon.cpp | 50 +--- test/common/ipc/ipc_gtest_helpers.hpp | 2 +- test/common/ipc/test_client_router.cpp | 10 +- test/common/ipc/test_server_router.cpp | 8 +- 23 files changed, 647 insertions(+), 146 deletions(-) delete mode 100644 src/common/dsdl/ocvsmd/common/node_command/ExecCmd.0.1.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcResponse.0.1.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdReq.0.1.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdRes.0.1.dsdl create mode 100644 src/common/svc/node/exec_cmd_spec.hpp create mode 100644 src/daemon/engine/svc/node/exec_cmd_service.cpp create mode 100644 src/daemon/engine/svc/node/exec_cmd_service.hpp create mode 100644 src/daemon/engine/svc/svc_helpers.hpp diff --git a/src/common/dsdl/ocvsmd/common/node_command/ExecCmd.0.1.dsdl b/src/common/dsdl/ocvsmd/common/node_command/ExecCmd.0.1.dsdl deleted file mode 100644 index 57c6163..0000000 --- a/src/common/dsdl/ocvsmd/common/node_command/ExecCmd.0.1.dsdl +++ /dev/null @@ -1,4 +0,0 @@ -int8[<8] some_stuff - -# reserve twice as much as we need. -@extent _offset_.max * 2 diff --git a/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl new file mode 100644 index 0000000..0d3feff --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl @@ -0,0 +1,4 @@ +uint16[<=128] node_ids +UavcanNodeExecCmdReq.0.1 payload + +@extent 600 * 8 diff --git a/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcResponse.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcResponse.0.1.dsdl new file mode 100644 index 0000000..d32e0e4 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcResponse.0.1.dsdl @@ -0,0 +1,4 @@ +uint16 node_id +UavcanNodeExecCmdRes.0.1 payload + +@extent 64 * 8 diff --git a/src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdReq.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdReq.0.1.dsdl new file mode 100644 index 0000000..9e5fc5d --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdReq.0.1.dsdl @@ -0,0 +1,78 @@ +# This is copy/paste of the request part of the `uavcan/node/435.ExecuteCommand.1.3.dsdl` service definition. +# In use by the `ExecCmdSvcRequest.0.1.dsdl` service message type. + +# Instructs the server node to execute or commence execution of a simple predefined command. +# All standard commands are optional; i.e., not guaranteed to be supported by all nodes. + +uint16 command +# Standard pre-defined commands are at the top of the range (defined below). +# Vendors can define arbitrary, vendor-specific commands in the bottom part of the range (starting from zero). +# Vendor-specific commands shall not use identifiers above 32767. + +uint16 COMMAND_RESTART = 65535 +# Reboot the node. +# Note that some standard commands may or may not require a restart in order to take effect; e.g., factory reset. + +uint16 COMMAND_POWER_OFF = 65534 +# Shut down the node; further access will not be possible until the power is turned back on. + +uint16 COMMAND_BEGIN_SOFTWARE_UPDATE = 65533 +# Begin the software update process using uavcan.file.Read. This command makes use of the "parameter" field below. +# The parameter contains the path to the new software image file to be downloaded by the server from the client +# using the standard service uavcan.file.Read. Observe that this operation swaps the roles of the client and +# the server. +# +# Upon reception of this command, the server (updatee) will evaluate whether it is possible to begin the +# software update process. If that is deemed impossible, the command will be rejected with one of the +# error codes defined in the response section of this definition (e.g., BAD_STATE if the node is currently +# on-duty and a sudden interruption of its activities is considered unsafe, and so on). +# If an update process is already underway, the updatee should abort the process and restart with the new file, +# unless the updatee can determine that the specified file is the same file that is already being downloaded, +# in which case it is allowed to respond SUCCESS and continue the old update process. +# If there are no other conditions precluding the requested update, the updatee will return a SUCCESS and +# initiate the file transfer process by invoking the standard service uavcan.file.Read repeatedly until the file +# is transferred fully (please refer to the documentation for that data type for more information about its usage). +# +# While the software is being updated, the updatee should set its mode (the field "mode" in uavcan.node.Heartbeat) +# to MODE_SOFTWARE_UPDATE. Please refer to the documentation for uavcan.node.Heartbeat for more information. +# +# It is recognized that most systems will have to interrupt their normal services to perform the software update +# (unless some form of software hot swapping is implemented, as is the case in some high-availability systems). +# +# Microcontrollers that are requested to update their firmware may need to stop execution of their current firmware +# and start the embedded bootloader (although other approaches are possible as well). In that case, +# while the embedded bootloader is running, the mode reported via the message uavcan.node.Heartbeat should be +# MODE_SOFTWARE_UPDATE as long as the bootloader is runing, even if no update-related activities +# are currently underway. For example, if the update process failed and the bootloader cannot load the software, +# the same mode MODE_SOFTWARE_UPDATE will be reported. +# It is also recognized that in a microcontroller setting, the application that served the update request will have +# to pass the update-related metadata (such as the node-ID of the server and the firmware image file path) to +# the embedded bootloader. The tactics of that transaction lie outside of the scope of this specification. + +uint16 COMMAND_FACTORY_RESET = 65532 +# Return the node's configuration back to the factory default settings (may require restart). +# Due to the uncertainty whether a restart is required, generic interfaces should always force a restart. + +uint16 COMMAND_EMERGENCY_STOP = 65531 +# Cease activities immediately, enter a safe state until restarted. +# Further operation may no longer be possible until a restart command is executed. + +uint16 COMMAND_STORE_PERSISTENT_STATES = 65530 +# This command instructs the node to store the current configuration parameter values and other persistent states +# to the non-volatile storage. Nodes are allowed to manage persistent states automatically, obviating the need for +# this command by committing all such data to the non-volatile memory automatically as necessary. However, some +# nodes may lack this functionality, in which case this parameter should be used. Generic interfaces should always +# invoke this command in order to ensure that the data is stored even if the node doesn't implement automatic +# persistence management. + +uint16 COMMAND_IDENTIFY = 65529 +# This command instructs the node to physically identify itself in some way--e.g., by flashing a light or +# emitting a sound. The duration and the nature of the identification process is implementation-defined. +# This command can be useful for human operators to match assigned node-ID values to physical nodes during setup. + +uint8[<=uavcan.file.Path.2.0.MAX_LENGTH] parameter +# A string parameter supplied to the command. The format and interpretation is command-specific. +# The standard commands do not use this field (ignore it), excepting the following: +# - COMMAND_BEGIN_SOFTWARE_UPDATE + +@extent 300 * 8 diff --git a/src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdRes.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdRes.0.1.dsdl new file mode 100644 index 0000000..175fbe6 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/svc/node/UavcanNodeExecCmdRes.0.1.dsdl @@ -0,0 +1,19 @@ +# This is copy/paste of the response part of the `uavcan/node/435.ExecuteCommand.1.3.dsdl` service definition. +# In use by the `ExecCmdSvcResponse.0.1.dsdl` service message type. + +uint8 STATUS_SUCCESS = 0 # Started or executed successfully +uint8 STATUS_FAILURE = 1 # Could not start or the desired outcome could not be reached +uint8 STATUS_NOT_AUTHORIZED = 2 # Denied due to lack of authorization +uint8 STATUS_BAD_COMMAND = 3 # The requested command is not known or not supported +uint8 STATUS_BAD_PARAMETER = 4 # The supplied parameter cannot be used with the selected command +uint8 STATUS_BAD_STATE = 5 # The current state of the node does not permit execution of this command +uint8 STATUS_INTERNAL_ERROR = 6 # The operation should have succeeded but an unexpected failure occurred +uint8 status +# The result of the request. + +uint8[<=46] output +# Any output that could be useful that has the capability to convey detailed information. +# Users can send commands and receive specific data, like device status or measurements back in a streamlined manner. +# The standard commands should leave this field empty unless explicitly specified otherwise. + +@extent 48 * 8 diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index d3a4bb1..f970d6c 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -14,9 +14,12 @@ #include #include +#include + #include #include #include +#include #include namespace ocvsmd @@ -41,14 +44,14 @@ class AnyChannel /// Builds a service ID from either the service name (if not empty), or message type name. /// template - CETL_NODISCARD static detail::ServiceId getServiceId(const cetl::string_view service_name) noexcept + CETL_NODISCARD static detail::ServiceDesc getServiceDesc(cetl::string_view service_name) noexcept { const cetl::string_view srv_or_msg_name = !service_name.empty() // ? service_name : Message::_traits_::FullNameAndVersion(); const libcyphal::common::CRC64WE crc64{srv_or_msg_name.cbegin(), srv_or_msg_name.cend()}; - return crc64.get(); + return {crc64.get(), srv_or_msg_name}; } protected: @@ -86,6 +89,11 @@ class Channel final : public AnyChannel }); } + void complete(const int error_code) + { + return gateway_->complete(error_code); + } + void subscribe(EventHandler event_handler) { if (event_handler) @@ -140,7 +148,7 @@ class Channel final : public AnyChannel }; // Adapter - Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, const detail::ServiceId service_id) + Channel(cetl::pmr::memory_resource& memory, detail::Gateway::Ptr gateway, const detail::ServiceDesc::Id service_id) : memory_{memory} , gateway_{std::move(gateway)} , service_id_{service_id} @@ -152,7 +160,7 @@ class Channel final : public AnyChannel std::reference_wrapper memory_; detail::Gateway::Ptr gateway_; - detail::ServiceId service_id_; + detail::ServiceDesc::Id service_id_; }; // Channel @@ -160,4 +168,26 @@ class Channel final : public AnyChannel } // namespace common } // namespace ocvsmd +// MARK: - Formatting + +// NOLINTBEGIN +template <> +struct fmt::formatter : formatter +{ + auto format(ocvsmd::common::ipc::AnyChannel::Connected, format_context& ctx) const + { + return formatter::format("Connected", ctx); + } +}; + +template <> +struct fmt::formatter : formatter +{ + auto format(ocvsmd::common::ipc::AnyChannel::Completed completed, format_context& ctx) const + { + return format_to(ctx.out(), "Completed(err={})", static_cast(completed.error_code)); + } +}; +// NOLINTEND + #endif // OCVSMD_COMMON_IPC_CHANNEL_HPP_INCLUDED diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 46474f7..14c0733 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -105,6 +105,7 @@ class ClientRouterImpl final : public ClientRouter : router_{router} , endpoint_{endpoint} , next_sequence_{0} + , completion_error_code_{0} { router_.logger_->trace("Gateway(tag={}).", endpoint.tag); } @@ -116,19 +117,19 @@ class ClientRouterImpl final : public ClientRouter ~GatewayImpl() { - router_.logger_->trace("~Gateway(tag={}).", endpoint_.tag); + router_.logger_->trace("~Gateway(tag={}, err={}).", endpoint_.tag, completion_error_code_); performWithoutThrowing([this] { // // `next_sequence_ == 0` means that this gateway was never used for sending messages, // and so remote router never knew about it (its tag) - no need to post "ChEnd" event. - router_.onGatewayDisposal(endpoint_, next_sequence_ > 0); + router_.onGatewayDisposal(endpoint_, next_sequence_ > 0, completion_error_code_); }); } // detail::Gateway - CETL_NODISCARD int send(const detail::ServiceId service_id, const Payload payload) override + CETL_NODISCARD int send(const detail::ServiceDesc::Id service_id, const Payload payload) override { if (!router_.isConnected(endpoint_)) { @@ -153,6 +154,11 @@ class ClientRouterImpl final : public ClientRouter }); } + void complete(const int error_code) override + { + completion_error_code_ = error_code; + } + CETL_NODISCARD int event(const Event::Var& event) override { // It's fine to be not subscribed to events. @@ -170,6 +176,7 @@ class ClientRouterImpl final : public ClientRouter const Endpoint endpoint_; std::uint64_t next_sequence_; EventHandler event_handler_; + int completion_error_code_; }; // GatewayImpl @@ -241,7 +248,7 @@ class ClientRouterImpl final : public ClientRouter /// The "dying" gateway might wish to notify the remote router about its disposal. /// This local router fulfills the wish if the gateway was registered and the router is connected. /// - void onGatewayDisposal(const Endpoint& endpoint, const bool send_ch_end) + void onGatewayDisposal(const Endpoint& endpoint, const bool send_ch_end, const int completion_err) { const bool was_registered = (map_of_gateways_.erase(endpoint.tag) > 0); @@ -253,7 +260,7 @@ class ClientRouterImpl final : public ClientRouter Route_0_1 route{&memory_}; auto& channel_end = route.set_channel_end(); channel_end.tag = endpoint.tag; - channel_end.error_code = 0; // No error b/c it's a normal channel completion. + channel_end.error_code = completion_err; const int error = tryPerformOnSerialized(route, [this](const auto payload) { // diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index 5f58086..1a0e9df 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -40,10 +40,10 @@ class ClientRouter CETL_NODISCARD virtual cetl::pmr::memory_resource& memory() = 0; template - CETL_NODISCARD Ch makeChannel(cetl::string_view service_name = "") + CETL_NODISCARD Ch makeChannel(const cetl::string_view service_name = "") { - const auto service_id = AnyChannel::getServiceId(service_name); - return Ch{memory(), makeGateway(), service_id}; + const auto svc_desc = AnyChannel::getServiceDesc(service_name); + return Ch{memory(), makeGateway(), svc_desc.id}; } protected: diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index 39f9495..c2e59ed 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -25,7 +25,15 @@ namespace ipc namespace detail { -using ServiceId = std::uint64_t; +struct ServiceDesc +{ + using Id = std::uint64_t; + using Name = cetl::string_view; + + const Id id; + const Name name; + +}; // ServiceDesc class Gateway { @@ -58,9 +66,10 @@ class Gateway Gateway& operator=(const Gateway&) = delete; Gateway& operator=(Gateway&&) noexcept = delete; - CETL_NODISCARD virtual int send(const ServiceId service_id, const Payload payload) = 0; - CETL_NODISCARD virtual int event(const Event::Var& event) = 0; - virtual void subscribe(EventHandler event_handler) = 0; + CETL_NODISCARD virtual int send(const ServiceDesc::Id service_id, const Payload payload) = 0; + virtual void complete(int error_code) = 0; + CETL_NODISCARD virtual int event(const Event::Var& event) = 0; + virtual void subscribe(EventHandler event_handler) = 0; protected: Gateway() = default; diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 41bb478..4c05e9c 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -67,10 +67,11 @@ class ServerRouterImpl final : public ServerRouter }); } - void registerChannelFactory(const detail::ServiceId service_id, // - TypeErasedChannelFactory channel_factory) override + void registerChannelFactory(const detail::ServiceDesc service_desc, // + TypeErasedChannelFactory channel_factory) override { - service_id_to_channel_factory_[service_id] = std::move(channel_factory); + logger_->debug("Registering '{}' service (id=0x{:X}).", service_desc.name, service_desc.id); + service_id_to_channel_factory_[service_desc.id] = std::move(channel_factory); } private: @@ -100,6 +101,7 @@ class ServerRouterImpl final : public ServerRouter : router_{router} , endpoint_{endpoint} , next_sequence_{0} + , completion_error_code_{0} { router_.logger_->trace("Gateway(cl={}, tag={}).", endpoint.client_id, endpoint.tag); } @@ -111,17 +113,20 @@ class ServerRouterImpl final : public ServerRouter ~GatewayImpl() { - router_.logger_->trace("~Gateway(cl={}, tag={}).", endpoint_.client_id, endpoint_.tag); + router_.logger_->trace("~Gateway(cl={}, tag={}, err={}).", + endpoint_.client_id, + endpoint_.tag, + completion_error_code_); performWithoutThrowing([this] { // - router_.onGatewayDisposal(endpoint_); + router_.onGatewayDisposal(endpoint_, completion_error_code_); }); } // detail::Gateway - CETL_NODISCARD int send(const detail::ServiceId service_id, const Payload payload) override + CETL_NODISCARD int send(const detail::ServiceDesc::Id service_id, const Payload payload) override { if (!router_.isConnected(endpoint_)) { @@ -146,6 +151,11 @@ class ServerRouterImpl final : public ServerRouter }); } + void complete(const int error_code) override + { + completion_error_code_ = error_code; + } + CETL_NODISCARD int event(const Event::Var& event) override { // It's fine to be not subscribed to events. @@ -163,10 +173,11 @@ class ServerRouterImpl final : public ServerRouter const Endpoint endpoint_; std::uint64_t next_sequence_; EventHandler event_handler_; + int completion_error_code_; }; // GatewayImpl - using ServiceIdToChannelFactory = std::unordered_map; + using ServiceIdToChannelFactory = std::unordered_map; using MapOfWeakGateways = std::unordered_map; using ClientIdToMapOfGateways = std::unordered_map; @@ -227,7 +238,7 @@ class ServerRouterImpl final : public ServerRouter /// The "dying" gateway wishes to notify the remote client router about its disposal. /// This local router fulfills the wish if the gateway was registered and the client router is connected. /// - void onGatewayDisposal(const Endpoint& endpoint) + void onGatewayDisposal(const Endpoint& endpoint, const int completion_err) { const auto cl_to_gws = client_id_to_map_of_gateways_.find(endpoint.client_id); if (cl_to_gws != client_id_to_map_of_gateways_.end()) @@ -243,7 +254,7 @@ class ServerRouterImpl final : public ServerRouter Route_0_1 route{&memory_}; auto& channel_end = route.set_channel_end(); channel_end.tag = endpoint.tag; - channel_end.error_code = 0; // No error b/c it's a normal channel completion. + channel_end.error_code = completion_err; const int error = tryPerformOnSerialized(route, [this, &endpoint](const auto payload) { // diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index 326252f..6d3d09a 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -48,16 +48,16 @@ class ServerRouter { CETL_DEBUG_ASSERT(handler, ""); - const auto service_id = AnyChannel::getServiceId(service_name); + const auto svc_desc = AnyChannel::getServiceDesc(service_name); registerChannelFactory( // - service_id, - [this, service_id, new_ch_handler = std::move(handler)](detail::Gateway::Ptr gateway, - const Payload payload) { + svc_desc, + [this, svc_id = svc_desc.id, new_ch_handler = std::move(handler)](detail::Gateway::Ptr gateway, + const Payload payload) { typename Ch::Input input{&memory()}; if (tryDeserializePayload(payload, input)) { - new_ch_handler(Ch{memory(), gateway, service_id}, input); + new_ch_handler(Ch{memory(), gateway, svc_id}, input); } }); } @@ -67,8 +67,8 @@ class ServerRouter ServerRouter() = default; - virtual void registerChannelFactory(const detail::ServiceId service_id, - TypeErasedChannelFactory channel_factory) = 0; + virtual void registerChannelFactory(const detail::ServiceDesc service_desc, + TypeErasedChannelFactory channel_factory) = 0; }; // ServerRouter diff --git a/src/common/logging.hpp b/src/common/logging.hpp index 2a31fac..d525660 100644 --- a/src/common/logging.hpp +++ b/src/common/logging.hpp @@ -10,6 +10,7 @@ #include +#include #include #include @@ -50,4 +51,15 @@ inline LoggerPtr getLogger(const std::string& name) noexcept } // namespace common } // namespace ocvsmd +#if (__cplusplus < CETL_CPP_STANDARD_17) +template <> +struct fmt::formatter : formatter +{ + auto format(cetl::string_view sv, format_context& ctx) const + { + return formatter::format(string_view{sv.data(), sv.size()}, ctx); + } +}; +#endif + #endif // OCVSMD_COMMON_LOGGING_HPP_INCLUDED diff --git a/src/common/svc/node/exec_cmd_spec.hpp b/src/common/svc/node/exec_cmd_spec.hpp new file mode 100644 index 0000000..881565d --- /dev/null +++ b/src/common/svc/node/exec_cmd_spec.hpp @@ -0,0 +1,36 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_SVC_NODE_EXEC_CMD_SPEC_HPP_INCLUDED +#define OCVSMD_COMMON_SVC_NODE_EXEC_CMD_SPEC_HPP_INCLUDED + +#include "ocvsmd/common/svc/node/ExecCmdSvcRequest_0_1.hpp" +#include "ocvsmd/common/svc/node/ExecCmdSvcResponse_0_1.hpp" + +namespace ocvsmd +{ +namespace common +{ +namespace svc +{ +namespace node +{ + +struct ExecCmdSpec +{ + using Request = ExecCmdSvcRequest_0_1; + using Response = ExecCmdSvcResponse_0_1; + + constexpr auto static svc_full_name = "ocvsmd.svc.node.exec_cmd"; + + ExecCmdSpec() = delete; +}; + +} // namespace node +} // namespace svc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_SVC_NODE_EXEC_CMD_SPEC_HPP_INCLUDED diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 3993139..bc12bd5 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -35,6 +35,7 @@ add_library(ocvsmd_engine application.cpp config.cpp platform/udp/udp.c + svc/node/exec_cmd_service.cpp ) target_link_libraries(ocvsmd_engine PUBLIC udpard diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index f0bf295..6cf960f 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -5,9 +5,9 @@ #include "application.hpp" -#include "ipc/channel.hpp" #include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" +#include "svc/node/exec_cmd_service.hpp" #include #include @@ -69,48 +69,8 @@ cetl::optional Application::init() auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); - using Ch = ExecCmdChannel; - ipc_router_->registerChannel("daemon", [this](ExecCmdChannel&& ch, const auto& request) { - // - logger_->info("D << 🆕 Ch created."); - // NOLINTNEXTLINE - logger_->info("D << 🔵 Ch initial Msg='{}'.", reinterpret_cast(request.some_stuff.data())); - - ipc_exec_cmd_ch_ = std::move(ch); - ipc_exec_cmd_ch_->subscribe([this](const auto& event_var) { - // - cetl::visit( // - cetl::make_overloaded( - [this](const ExecCmdChannel::Connected&) { - // - logger_->info("D << 🟢 Ch connected."); - - logger_->info("D >> 🔵 Ch 'SR' msg."); - ExecCmd cmd{&memory_}; - cmd.some_stuff.push_back('S'); - cmd.some_stuff.push_back('R'); - cmd.some_stuff.push_back('\0'); - const int result = ipc_exec_cmd_ch_->send(cmd); - (void) result; - }, - [this](const ExecCmdChannel::Input& input) { - // - // NOLINTNEXTLINE - logger_->info("D << 🔵 Ch Msg='{}'.", reinterpret_cast(input.some_stuff.data())); - - // NOLINTNEXTLINE - logger_->info("D >> 🔵 Ch '{}' msg.", reinterpret_cast(input.some_stuff.data())); - const int result = ipc_exec_cmd_ch_->send(input); - (void) result; - }, - [this](const ExecCmdChannel::Completed& completed) { - // - logger_->info("D << 🔴 Ch Completed (err={}).", static_cast(completed.error_code)); - ipc_exec_cmd_ch_.reset(); - }), - event_var); - }); - }); + const svc::ScvContext svc_context{memory_, *ipc_router_, *presentation_}; + svc::node::ExecCmdService::registerWithContext(svc_context); if (0 != ipc_router_->start()) { diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/application.hpp index 22f0134..d2010cb 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/application.hpp @@ -10,9 +10,6 @@ #include "logging.hpp" #include "ocvsmd/platform/defines.hpp" -#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" - -#include #include #include @@ -39,11 +36,6 @@ class Application void runWhile(const std::function& loop_predicate); private: - // TODO: temp stuff - using ExecCmd = common::node_command::ExecCmd_0_1; - using ExecCmdChannel = common::ipc::Channel; - cetl::optional ipc_exec_cmd_ch_; - using UniqueId = uavcan::node::GetInfo::Response_1_0::_traits_::TypeOf::unique_id; static UniqueId getUniqueId(); diff --git a/src/daemon/engine/svc/node/exec_cmd_service.cpp b/src/daemon/engine/svc/node/exec_cmd_service.cpp new file mode 100644 index 0000000..556d63b --- /dev/null +++ b/src/daemon/engine/svc/node/exec_cmd_service.cpp @@ -0,0 +1,261 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "exec_cmd_service.hpp" + +#include "ipc/channel.hpp" +#include "ipc/server_router.hpp" +#include "logging.hpp" +#include "svc/node/exec_cmd_spec.hpp" +#include "svc/svc_helpers.hpp" + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace node +{ +namespace +{ + +class ExecCmdServiceImpl final +{ +public: + using Spec = common::svc::node::ExecCmdSpec; + using Channel = common::ipc::Channel; + + explicit ExecCmdServiceImpl(const ScvContext& context) + : context_{context} + { + } + + void operator()(Channel&& ch, const Spec::Request& request) + { + const auto fsm_id = next_fsm_id_++; + logger_->debug("New '{}' service channel (fsm={}).", Spec::svc_full_name, fsm_id); + + auto fsm = std::make_shared(*this, fsm_id, std::move(ch)); + id_to_fsm_[fsm_id] = fsm; + + fsm->start(request); + } + +private: + class Fsm // Finite State Machine + { + public: + using Id = std::uint64_t; + using Ptr = std::shared_ptr; + + Fsm(ExecCmdServiceImpl& service, const Id id, Channel&& channel) + : id_{id} + , channel_{std::move(channel)} + , service_{service} + { + logger().trace("ExecCmdSvc::Fsm (id={}).", id_); + + channel_.subscribe([this](const auto& event_var) { + // + cetl::visit([this](const auto& event) { handleEvent(event); }, event_var); + }); + } + + ~Fsm() + { + logger().trace("ExecCmdSvc::~Fsm (id={}).", id_); + } + + Fsm(const Fsm&) = delete; + Fsm(Fsm&&) noexcept = delete; + Fsm& operator=(const Fsm&) = delete; + Fsm& operator=(Fsm&&) noexcept = delete; + + void start(const Spec::Request& request) + { + logger().trace("ExecCmdSvc::Fsm::start (fsm_id={}).", id_); + + // Immediately complete if there are no nodes to execute the command on. + // + if (request.node_ids.empty()) + { + complete(0); + return; + } + + // It's ok to have duplicates in the request - + // we just ignore duplicates, and work with unique ones. + const SetOfNodeIds unique_node_ids{request.node_ids.begin(), request.node_ids.end()}; + + const CyphalExecCmdSvc::Request cy_request{request.payload.command, request.payload.parameter, &memory()}; + for (const auto node_id : unique_node_ids) + { + if (const auto err = makeCyphalSvcCallFor(node_id, cy_request)) + { + complete(err); + return; + } + } + } + + private: + using SetOfNodeIds = std::unordered_set; + using CyphalExecCmdSvc = uavcan::node::ExecuteCommand_1_3; + using CyphalSvcClient = libcyphal::presentation::ServiceClient; + using CyphalPromise = libcyphal::presentation::ResponsePromise; + using CyphalPromiseFailure = libcyphal::presentation::ResponsePromiseFailure; + + struct CyNodeOp + { + CyphalSvcClient client; + CyphalPromise promise; + }; + + common::Logger& logger() const + { + return *service_.logger_; + } + + cetl::pmr::memory_resource& memory() const + { + return service_.context_.memory_; + } + + // We are not interested in handling these events. + static void handleEvent(const Channel::Connected&) {} + static void handleEvent(const Channel::Input&) {} + + void handleEvent(const Channel::Completed& completed) + { + logger().debug("ExecCmdSvc::Fsm::handleEvent({}) (id={}).", completed, id_); + complete(ECANCELED); + } + + int makeCyphalSvcCallFor(const std::uint16_t node_id, const CyphalExecCmdSvc::Request& cy_request) + { + using CyphalMakeFailure = libcyphal::presentation::Presentation::MakeFailure; + + auto cy_make_result = service_.context_.presentation.makeClient(node_id); + if (const auto* cy_failure = cetl::get_if(&cy_make_result)) + { + const auto err = failureToErrorCode(*cy_failure); + logger().error("ExecCmdSvc: failed to make svc client for node {} (err={}, fsm_id={}).", + node_id, + err, + id_); + return err; + } + auto cy_svc_client = cetl::get(std::move(cy_make_result)); + + auto cy_req_result = cy_svc_client.request({}, cy_request); + if (const auto* cy_failure = cetl::get_if(&cy_req_result)) + { + const auto err = failureToErrorCode(*cy_failure); + logger().error("ExecCmdSvc: failed to send svc request to node {} (err={}, fsm_id={})", + node_id, + err, + id_); + return err; + } + auto cy_promise = cetl::get(std::move(cy_req_result)); + + cy_promise.setCallback([this, node_id](const auto& arg) { + // + if (const auto* cy_failure = cetl::get_if(&arg.result)) + { + const auto err = failureToErrorCode(*cy_failure); + logger().warn("ExecCmdSvc: promise failure for node {} (err={}, fsm_id={}).", node_id, err, id_); + } + else if (const auto* success = cetl::get_if(&arg.result)) + { + const auto& res = success->response; + logger().debug("ExecCmdSvc: promise success from node {} (status={}, fsm_id={}).", + node_id, + res.status, + id_); + + const Spec::Response ipc_response{node_id, {res.status, res.output, &memory()}, &memory()}; + if (const auto err = channel_.send(ipc_response)) + { + logger().warn("ExecCmdSvc: failed to send ipc response for node {} (err={}, fsm_id={}).", + node_id, + err, + id_); + } + } + + // We've got the response from the node, so we can release associated resources (client & promise). + // If no nodes left, then it means we did it for all nodes, so the whole FSM is completed. + // + node_id_to_op_.erase(node_id); + if (node_id_to_op_.empty()) + { + complete(0); + } + }); + + node_id_to_op_.emplace(node_id, CyNodeOp{std::move(cy_svc_client), std::move(cy_promise)}); + return 0; + } + + void complete(const int err) + { + // Cancel anything that might be still pending. + node_id_to_op_.clear(); + + channel_.complete(err); + + service_.releaseFsmBy(id_); + } + + const Id id_; + Channel channel_; + ExecCmdServiceImpl& service_; + std::unordered_map node_id_to_op_; + + }; // Fsm + + void releaseFsmBy(const Fsm::Id fsm_id) + { + id_to_fsm_.erase(fsm_id); + } + + const ScvContext& context_; + std::uint64_t next_fsm_id_{0}; + std::unordered_map id_to_fsm_; + common::LoggerPtr logger_{common::getLogger("engine")}; + +}; // ExecCmdServiceImpl + +} // namespace + +void ExecCmdService::registerWithContext(const ScvContext& context) +{ + using Impl = ExecCmdServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name, Impl(context)); +} + +} // namespace node +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd diff --git a/src/daemon/engine/svc/node/exec_cmd_service.hpp b/src/daemon/engine/svc/node/exec_cmd_service.hpp new file mode 100644 index 0000000..89788f0 --- /dev/null +++ b/src/daemon/engine/svc/node/exec_cmd_service.hpp @@ -0,0 +1,36 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_SVC_NODE_EXEC_CMD_SERVICE_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_SVC_NODE_EXEC_CMD_SERVICE_HPP_INCLUDED + +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace node +{ + +class ExecCmdService +{ +public: + ExecCmdService() = delete; + static void registerWithContext(const ScvContext& context); + +}; // ExecCmdService + +} // namespace node +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_SVC_NODE_EXEC_CMD_SERVICE_HPP_INCLUDED diff --git a/src/daemon/engine/svc/svc_helpers.hpp b/src/daemon/engine/svc/svc_helpers.hpp new file mode 100644 index 0000000..d4aa00e --- /dev/null +++ b/src/daemon/engine/svc/svc_helpers.hpp @@ -0,0 +1,93 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_SVC_HELPERS_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_SVC_HELPERS_HPP_INCLUDED + +#include "ipc/server_router.hpp" + +#include + +#include +#include +#include +#include +#include +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ + +struct ScvContext +{ + cetl::pmr::memory_resource& memory_; + common::ipc::ServerRouter& ipc_router; + libcyphal::presentation::Presentation& presentation; + +}; // ScvContext + +inline int errorToCode(const libcyphal::MemoryError) noexcept +{ + return ENOMEM; +} +inline int errorToCode(const libcyphal::transport::CapacityError) noexcept +{ + return ENOMEM; +} + +inline int errorToCode(const libcyphal::ArgumentError) noexcept +{ + return EINVAL; +} +inline int errorToCode(const libcyphal::transport::AnonymousError) noexcept +{ + return EINVAL; +} +inline int errorToCode(const nunavut::support::Error) noexcept +{ + return EINVAL; +} + +inline int errorToCode(const libcyphal::transport::AlreadyExistsError) noexcept +{ + return EEXIST; +} + +inline int errorToCode(const libcyphal::transport::PlatformError& platform_error) noexcept +{ + return static_cast(platform_error->code()); +} + + +inline int errorToCode(const libcyphal::presentation::ResponsePromiseExpired) noexcept +{ + return ETIMEDOUT; +} + +inline int errorToCode(const libcyphal::presentation::detail::ClientBase::TooManyPendingRequestsError) noexcept +{ + return EBUSY; +} + +template +int failureToErrorCode(const Variant& failure) +{ + return cetl::visit([](const auto& error) { return errorToCode(error); }, failure); +} + +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_SVC_HELPERS_HPP_INCLUDED diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 8600934..9a4c893 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -10,11 +10,8 @@ #include "ipc/pipe/unix_socket_client.hpp" #include "logging.hpp" -#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" - #include #include -#include #include #include @@ -41,7 +38,7 @@ class DaemonImpl final : public Daemon ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); } - CETL_NODISCARD int start() + CETL_NODISCARD int start() const { if (const int err = ipc_router_->start()) { @@ -49,58 +46,13 @@ class DaemonImpl final : public Daemon return err; } - ipc_exec_cmd_ch_ = ipc_router_->makeChannel("daemon"); - logger_->debug("C << 🆕 Ch created."); - ipc_exec_cmd_ch_->subscribe([this](const auto& event_var) { - // - cetl::visit( // - cetl::make_overloaded( - [this](const ExecCmdChannel::Connected&) { - // - logger_->info("C << 🟢 Ch connected."); - - ExecCmd cmd{&memory_}; - cmd.some_stuff.push_back('C'); - cmd.some_stuff.push_back('L'); - cmd.some_stuff.push_back('\0'); - logger_->info("C >> 🔵 Ch 'CL' msg."); - const int result = ipc_exec_cmd_ch_->send(cmd); - (void) result; - }, - [this](const ExecCmdChannel::Input& input) { - // - // NOLINTNEXTLINE - logger_->info("C << 🔵 Ch Msg='{}'.", reinterpret_cast(input.some_stuff.data())); - - if (countdown_--) - { - logger_->info("C >> 🔵 Ch '{}' msg.", - reinterpret_cast(input.some_stuff.data())); // NOLINT - const int result = ipc_exec_cmd_ch_->send(input); - (void) result; - } - }, - [this](const ExecCmdChannel::Completed& completed) { - // - logger_->info("C << 🔴 Ch Completed (err={}).", static_cast(completed.error_code)); - ipc_exec_cmd_ch_.reset(); - }), - event_var); - }); - return 0; } private: - using ExecCmd = common::node_command::ExecCmd_0_1; - using ExecCmdChannel = common::ipc::Channel; - cetl::pmr::memory_resource& memory_; common::LoggerPtr logger_; common::ipc::ClientRouter::Ptr ipc_router_; - cetl::optional ipc_exec_cmd_ch_; - - int countdown_{2}; }; // DaemonImpl diff --git a/test/common/ipc/ipc_gtest_helpers.hpp b/test/common/ipc/ipc_gtest_helpers.hpp index ce81073..1a78e56 100644 --- a/test/common/ipc/ipc_gtest_helpers.hpp +++ b/test/common/ipc/ipc_gtest_helpers.hpp @@ -181,7 +181,7 @@ auto PayloadOfRouteChannelMsg(const Msg& msg, const std::uint64_t seq, const cetl::string_view srv_name = "") { - RouteChannelMsg_0_1 route_ch_msg{tag, seq, AnyChannel::getServiceId(srv_name), 0, &mr}; + RouteChannelMsg_0_1 route_ch_msg{tag, seq, AnyChannel::getServiceDesc(srv_name).id, 0, &mr}; EXPECT_THAT(tryPerformOnSerialized( // msg, [&route_ch_msg](const auto payload) { diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index 5c5cf67..b71a893 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -16,7 +16,7 @@ #include "ocvsmd/common/ipc/RouteChannelMsg_0_1.hpp" #include "ocvsmd/common/ipc/RouteConnect_0_1.hpp" #include "ocvsmd/common/ipc/Route_0_1.hpp" -#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" +#include "ocvsmd/common/svc/node/ExecCmdSvcRequest_0_1.hpp" #include @@ -98,7 +98,7 @@ class TestClientRouter : public testing::Test auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag; channel_msg.sequence = seq++; - channel_msg.service_id = AnyChannel::getServiceId(service_name); + channel_msg.service_id = AnyChannel::getServiceDesc(service_name).id; const int result = tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -155,7 +155,7 @@ TEST_F(TestClientRouter, start) TEST_F(TestClientRouter, makeChannel) { - using Msg = ocvsmd::common::node_command::ExecCmd_0_1; + using Msg = ocvsmd::common::svc::node::ExecCmdSvcRequest_0_1; using Channel = Channel; StrictMock client_pipe_mock; @@ -175,7 +175,7 @@ TEST_F(TestClientRouter, makeChannel) TEST_F(TestClientRouter, makeChannel_send) { - using Msg = ocvsmd::common::node_command::ExecCmd_0_1; + using Msg = ocvsmd::common::svc::node::ExecCmdSvcRequest_0_1; using Channel = Channel; StrictMock client_pipe_mock; @@ -212,7 +212,7 @@ TEST_F(TestClientRouter, makeChannel_send) TEST_F(TestClientRouter, makeChannel_receive_events) { - using Msg = ocvsmd::common::node_command::ExecCmd_0_1; + using Msg = ocvsmd::common::svc::node::ExecCmdSvcRequest_0_1; using Channel = Channel; StrictMock client_pipe_mock; diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 8b33d2d..4fcc837 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -13,7 +13,7 @@ #include "tracking_memory_resource.hpp" #include "ocvsmd/common/ipc/Route_0_1.hpp" -#include "ocvsmd/common/node_command/ExecCmd_0_1.hpp" +#include "ocvsmd/common/svc/node/ExecCmdSvcRequest_0_1.hpp" #include @@ -96,7 +96,7 @@ class TestServerRouter : public testing::Test auto& channel_msg = route.set_channel_msg(); channel_msg.tag = tag; channel_msg.sequence = seq++; - channel_msg.service_id = AnyChannel::getServiceId(service_name); + channel_msg.service_id = AnyChannel::getServiceDesc(service_name).id; const int result = tryPerformOnSerialized(route, [&](const auto prefix) { // @@ -151,7 +151,7 @@ TEST_F(TestServerRouter, start) TEST_F(TestServerRouter, registerChannel) { - using Msg = ocvsmd::common::node_command::ExecCmd_0_1; + using Msg = ocvsmd::common::svc::node::ExecCmdSvcRequest_0_1; using Channel = Channel; StrictMock server_pipe_mock; @@ -172,7 +172,7 @@ TEST_F(TestServerRouter, registerChannel) TEST_F(TestServerRouter, channel_send) { - using Msg = ocvsmd::common::node_command::ExecCmd_0_1; + using Msg = ocvsmd::common::svc::node::ExecCmdSvcRequest_0_1; using Channel = Channel; StrictMock server_pipe_mock; From f2e3ead4aad272956f2b255e9917a0686c8d4f5b Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 17 Jan 2025 18:43:25 +0200 Subject: [PATCH 069/156] added `NodeCommandClient` --- include/ocvsmd/sdk/daemon.hpp | 10 +- include/ocvsmd/sdk/node_command_client.hpp | 70 ++++++++++ src/cli/main.cpp | 1 + src/common/ipc/client_router.cpp | 2 +- src/common/ipc/client_router.hpp | 2 +- src/common/svc/node/exec_cmd_spec.hpp | 5 +- src/daemon/engine/application.cpp | 1 + .../engine/svc/node/exec_cmd_service.cpp | 4 +- src/daemon/engine/svc/svc_helpers.hpp | 1 - src/sdk/CMakeLists.txt | 13 ++ src/sdk/daemon.cpp | 17 ++- src/sdk/node_command_client.cpp | 88 +++++++++++++ src/sdk/sdk_factory.hpp | 31 +++++ src/sdk/svc/node/exec_cmd_client.cpp | 121 ++++++++++++++++++ src/sdk/svc/node/exec_cmd_client.hpp | 63 +++++++++ 15 files changed, 418 insertions(+), 11 deletions(-) create mode 100644 include/ocvsmd/sdk/node_command_client.hpp create mode 100644 src/sdk/node_command_client.cpp create mode 100644 src/sdk/sdk_factory.hpp create mode 100644 src/sdk/svc/node/exec_cmd_client.cpp create mode 100644 src/sdk/svc/node/exec_cmd_client.hpp diff --git a/include/ocvsmd/sdk/daemon.hpp b/include/ocvsmd/sdk/daemon.hpp index fc71750..d13b939 100644 --- a/include/ocvsmd/sdk/daemon.hpp +++ b/include/ocvsmd/sdk/daemon.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_SDK_DAEMON_HPP_INCLUDED #define OCVSMD_SDK_DAEMON_HPP_INCLUDED +#include "node_command_client.hpp" + #include #include #include @@ -22,9 +24,9 @@ namespace sdk class Daemon { public: - CETL_NODISCARD static std::unique_ptr make( // - cetl::pmr::memory_resource& memory, - libcyphal::IExecutor& executor); + using Ptr = std::shared_ptr; + + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor); Daemon(Daemon&&) = delete; Daemon(const Daemon&) = delete; @@ -33,6 +35,8 @@ class Daemon virtual ~Daemon() = default; + virtual NodeCommandClient::Ptr getNodeCommandClient() = 0; + protected: Daemon() = default; diff --git a/include/ocvsmd/sdk/node_command_client.hpp b/include/ocvsmd/sdk/node_command_client.hpp new file mode 100644 index 0000000..4a904f4 --- /dev/null +++ b/include/ocvsmd/sdk/node_command_client.hpp @@ -0,0 +1,70 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED +#define OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED + +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ + +class NodeCommandClient +{ +public: + using Ptr = std::shared_ptr; + + NodeCommandClient(NodeCommandClient&&) = delete; + NodeCommandClient(const NodeCommandClient&) = delete; + NodeCommandClient& operator=(NodeCommandClient&&) = delete; + NodeCommandClient& operator=(const NodeCommandClient&) = delete; + + virtual ~NodeCommandClient() = default; + + struct Command final + { + using NodeRequest = uavcan::node::ExecuteCommand_1_3::Request; + using NodeResponse = uavcan::node::ExecuteCommand_1_3::Response; + + /// Empty option indicates that the corresponding node did not return a response on time. + using Success = std::unordered_map>; + + /// `errno`-like error code. + using Failure = int; + + using Result = cetl::variant; + using ResultHandler = std::function; + + }; // Command + + /// All requests are sent concurrently. + /// The callback is executed when the last response has arrived, + /// or the timeout has expired. + /// + virtual int sendCommand(const cetl::span node_ids, + const Command::NodeRequest& node_request, + const std::chrono::microseconds timeout, + Command::ResultHandler result_handler) = 0; + +protected: + NodeCommandClient() = default; + +}; // NodeCommandClient + +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 246ef1f..e6c0d76 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -86,6 +86,7 @@ void setupLogging(const int argc, const char** const argv) // register_logger(std::make_shared("ipc", file_sink)); register_logger(std::make_shared("sdk", file_sink)); + register_logger(std::make_shared("svc", file_sink)); // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug,ipc=trace`). spdlog::cfg::load_argv_levels(argc, argv); diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 14c0733..39945fc 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -427,7 +427,7 @@ class ClientRouterImpl final : public ClientRouter CETL_NODISCARD ClientRouter::Ptr ClientRouter::make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) { - return std::make_unique(memory, std::move(client_pipe)); + return std::make_shared(memory, std::move(client_pipe)); } } // namespace ipc diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index 1a0e9df..fd604a2 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -25,7 +25,7 @@ namespace ipc class ClientRouter { public: - using Ptr = std::unique_ptr; + using Ptr = std::shared_ptr; CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe); diff --git a/src/common/svc/node/exec_cmd_spec.hpp b/src/common/svc/node/exec_cmd_spec.hpp index 881565d..1eac9a7 100644 --- a/src/common/svc/node/exec_cmd_spec.hpp +++ b/src/common/svc/node/exec_cmd_spec.hpp @@ -23,7 +23,10 @@ struct ExecCmdSpec using Request = ExecCmdSvcRequest_0_1; using Response = ExecCmdSvcResponse_0_1; - constexpr auto static svc_full_name = "ocvsmd.svc.node.exec_cmd"; + constexpr auto static svc_full_name() + { + return "ocvsmd.svc.node.exec_cmd"; + } ExecCmdSpec() = delete; }; diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index 6cf960f..d288885 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -7,6 +7,7 @@ #include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" +#include "svc/svc_helpers.hpp" #include "svc/node/exec_cmd_service.hpp" #include diff --git a/src/daemon/engine/svc/node/exec_cmd_service.cpp b/src/daemon/engine/svc/node/exec_cmd_service.cpp index 556d63b..0f4ed06 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.cpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.cpp @@ -52,7 +52,7 @@ class ExecCmdServiceImpl final void operator()(Channel&& ch, const Spec::Request& request) { const auto fsm_id = next_fsm_id_++; - logger_->debug("New '{}' service channel (fsm={}).", Spec::svc_full_name, fsm_id); + logger_->debug("New '{}' service channel (fsm={}).", Spec::svc_full_name(), fsm_id); auto fsm = std::make_shared(*this, fsm_id, std::move(ch)); id_to_fsm_[fsm_id] = fsm; @@ -251,7 +251,7 @@ class ExecCmdServiceImpl final void ExecCmdService::registerWithContext(const ScvContext& context) { using Impl = ExecCmdServiceImpl; - context.ipc_router.registerChannel(Impl::Spec::svc_full_name, Impl(context)); + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl(context)); } } // namespace node diff --git a/src/daemon/engine/svc/svc_helpers.hpp b/src/daemon/engine/svc/svc_helpers.hpp index d4aa00e..839e6b5 100644 --- a/src/daemon/engine/svc/svc_helpers.hpp +++ b/src/daemon/engine/svc/svc_helpers.hpp @@ -68,7 +68,6 @@ inline int errorToCode(const libcyphal::transport::PlatformError& platform_error return static_cast(platform_error->code()); } - inline int errorToCode(const libcyphal::presentation::ResponsePromiseExpired) noexcept { return ETIMEDOUT; diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index 18125e6..1ecfb6a 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -5,10 +5,23 @@ cmake_minimum_required(VERSION 3.27) +add_cyphal_library( + NAME sdk + DSDL_FILES ${submodules_dir}/nunavut/submodules/public_regulated_data_types/uavcan/node/435.ExecuteCommand.1.3.dsdl + ALLOW_EXPERIMENTAL_LANGUAGES + LANGUAGE cpp + LANGUAGE_STANDARD ${CYPHAL_LANGUAGE_STANDARD} + OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/dsdl_transpiled + OUT_LIBRARY_TARGET sdk_transpiled +) + add_library(ocvsmd_sdk daemon.cpp + node_command_client.cpp + svc/node/exec_cmd_client.cpp ) target_link_libraries(ocvsmd_sdk + PUBLIC ${sdk_transpiled} PRIVATE ocvsmd_common ) target_include_directories(ocvsmd_sdk diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 9a4c893..4969f28 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -9,6 +9,8 @@ #include "ipc/client_router.hpp" #include "ipc/pipe/unix_socket_client.hpp" #include "logging.hpp" +#include "ocvsmd/sdk/node_command_client.hpp" +#include "sdk_factory.hpp" #include #include @@ -36,6 +38,8 @@ class DaemonImpl final : public Daemon auto client_pipe = std::make_unique(executor, "/var/run/ocvsmd/local.sock"); ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); + + node_command_client_ = Factory::makeNodeCommandClient(memory, ipc_router_); } CETL_NODISCARD int start() const @@ -49,24 +53,33 @@ class DaemonImpl final : public Daemon return 0; } + // Daemon + + NodeCommandClient::Ptr getNodeCommandClient() override + { + return node_command_client_; + } + private: cetl::pmr::memory_resource& memory_; common::LoggerPtr logger_; common::ipc::ClientRouter::Ptr ipc_router_; + NodeCommandClient::Ptr node_command_client_; }; // DaemonImpl } // namespace -CETL_NODISCARD std::unique_ptr Daemon::make( // +CETL_NODISCARD Daemon::Ptr Daemon::make( // cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) { - auto daemon = std::make_unique(memory, executor); + auto daemon = std::make_shared(memory, executor); if (0 != daemon->start()) { return nullptr; } + return daemon; } diff --git a/src/sdk/node_command_client.cpp b/src/sdk/node_command_client.cpp new file mode 100644 index 0000000..ad9c842 --- /dev/null +++ b/src/sdk/node_command_client.cpp @@ -0,0 +1,88 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include + +#include "ipc/channel.hpp" +#include "ipc/client_router.hpp" +#include "logging.hpp" +#include "sdk_factory.hpp" +#include "svc/node/exec_cmd_spec.hpp" + +#include +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace +{ + +class NodeCommandClientImpl final : public NodeCommandClient +{ +public: + NodeCommandClientImpl(cetl::pmr::memory_resource& memory, common::ipc::ClientRouter::Ptr ipc_router) + : memory_{memory} + , ipc_router_{std::move(ipc_router)} + , logger_{common::getLogger("sdk")} + { + } + + // NodeCommandClient + + int sendCommand(const cetl::span node_ids, + const Command::NodeRequest& node_request, + const std::chrono::microseconds timeout, + Command::ResultHandler result_handler) override + { + // using ExecCmdRequest = ExecCmdSvcSpec::Request; + // using RequestPayload = ExecCmdSvcSpec::Request::_traits_::TypeOf::payload; + // + // if (node_ids.size() > ExecCmdSvcSpec::Request::_traits_::ArrayCapacity::node_ids) + // { + // logger_->error("Too many node IDs: {} (max {}).", + // node_ids.size(), + // ExecCmdSvcSpec::Request::_traits_::ArrayCapacity::node_ids); + // return EINVAL; + // } + // + // auto exec_cmd_ch = ipc_router_->makeChannel(ExecCmdSvcSpec::svc_full_name); + // exec_cmd_ch.subscribe([](const auto& event) {}); + // + // const RequestPayload request_payload{node_request.command, node_request.parameter, &memory_}; + // const ExecCmdRequest exec_cmd_req{{node_ids.begin(), node_ids.end()}, request_payload, &memory_}; + // + // return exec_cmd_ch.send(exec_cmd_req); + + return 0; + } + +private: + using ExecCmdSvcSpec = common::svc::node::ExecCmdSpec; + using ExecCmdChannel = common::ipc::Channel; + + cetl::pmr::memory_resource& memory_; + common::LoggerPtr logger_; + common::ipc::ClientRouter::Ptr ipc_router_; + +}; // NodeCommandClientImpl + +} // namespace + +CETL_NODISCARD NodeCommandClient::Ptr Factory::makeNodeCommandClient(cetl::pmr::memory_resource& memory, + common::ipc::ClientRouter::Ptr ipc_router) +{ + return std::make_shared(memory, std::move(ipc_router)); +} + +} // namespace sdk +} // namespace ocvsmd diff --git a/src/sdk/sdk_factory.hpp b/src/sdk/sdk_factory.hpp new file mode 100644 index 0000000..21eb107 --- /dev/null +++ b/src/sdk/sdk_factory.hpp @@ -0,0 +1,31 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_FACTORY_HPP_INCLUDED +#define OCVSMD_SDK_FACTORY_HPP_INCLUDED + +#include + +#include "ipc/client_router.hpp" + +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ + +struct Factory +{ + CETL_NODISCARD static NodeCommandClient::Ptr makeNodeCommandClient(cetl::pmr::memory_resource& memory, + common::ipc::ClientRouter::Ptr ipc_router); + +}; // Factory + +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_FACTORY_HPP_INCLUDED diff --git a/src/sdk/svc/node/exec_cmd_client.cpp b/src/sdk/svc/node/exec_cmd_client.cpp new file mode 100644 index 0000000..1d36589 --- /dev/null +++ b/src/sdk/svc/node/exec_cmd_client.cpp @@ -0,0 +1,121 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "exec_cmd_client.hpp" + +#include "ipc/channel.hpp" +#include "ipc/client_router.hpp" +#include "logging.hpp" +#include "svc/node/exec_cmd_spec.hpp" + +#include + +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace node +{ +namespace +{ + +class ExecCmdClientImpl final : public ExecCmdClient +{ +public: + ExecCmdClientImpl(cetl::pmr::memory_resource& memory, + common::ipc::ClientRouter::Ptr ipc_router, + Spec::Request&& request, + const std::chrono::microseconds timeout) + : memory_{memory} + , logger_{common::getLogger("svc")} + , request_{std::move(request)} + , channel_{ipc_router->makeChannel(Spec::svc_full_name())} + { + channel_.subscribe([this](const auto& event_var) { + // + cetl::visit([this](const auto& event) { handleEvent(event); }, event_var); + }); + + // TODO: handle timeout + (void) timeout; + } + + CETL_NODISCARD cetl::optional completed() const override + { + return completion_error_code_; + } + + CETL_NODISCARD cetl::optional takeResult() override + { + if (!completion_error_code_.has_value()) + { + return cetl::nullopt; + } + + Result result; + std::swap(result, node_id_to_response_); + return result; + } + +private: + using Channel = common::ipc::Channel; + + void handleEvent(const Channel::Connected& connected) + { + logger_->trace("ExecCmdClient::handleEvent({}).", connected); + + if (const auto err = channel_.send(request_)) + { + completion_error_code_.emplace(err); + } + } + + void handleEvent(const Channel::Input& input) + { + logger_->trace("ExecCmdClient::handleEvent(Input)."); + + NodeResponse node_response{input.payload.status, input.payload.output, &memory_}; + node_id_to_response_.emplace(input.node_id, std::move(node_response)); + } + + void handleEvent(const Channel::Completed& completed) + { + logger_->debug("ExecCmdClient::handleEvent({}).", completed); + completion_error_code_ = static_cast(completed.error_code); + } + + cetl::pmr::memory_resource& memory_; + common::LoggerPtr logger_; + Spec::Request request_; + Channel channel_; + std::unordered_map node_id_to_response_; + cetl::optional completion_error_code_; + +}; // ExecCmdClientImpl + +} // namespace + +CETL_NODISCARD ExecCmdClient::Ptr ExecCmdClient::make(cetl::pmr::memory_resource& memory, + common::ipc::ClientRouter::Ptr ipc_router, + Spec::Request&& request, + const std::chrono::microseconds timeout) +{ + return std::make_shared(memory, ipc_router, std::move(request), timeout); +} + +} // namespace node +} // namespace svc +} // namespace sdk +} // namespace ocvsmd diff --git a/src/sdk/svc/node/exec_cmd_client.hpp b/src/sdk/svc/node/exec_cmd_client.hpp new file mode 100644 index 0000000..ce1a83f --- /dev/null +++ b/src/sdk/svc/node/exec_cmd_client.hpp @@ -0,0 +1,63 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_SVC_NODE_EXEC_CMD_CLIENT_HPP_INCLUDED +#define OCVSMD_SDK_SVC_NODE_EXEC_CMD_CLIENT_HPP_INCLUDED + +#include "ipc/client_router.hpp" +#include "svc/node/exec_cmd_spec.hpp" + +#include + +#include +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace node +{ + +class ExecCmdClient +{ +public: + using Ptr = std::shared_ptr; + using Spec = common::svc::node::ExecCmdSpec; + using NodeResponse = uavcan::node::ExecuteCommand_1_3::Response; + using Result = std::unordered_map; + + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, + common::ipc::ClientRouter::Ptr ipc_router, + Spec::Request&& request, + const std::chrono::microseconds timeout); + + ExecCmdClient(ExecCmdClient&&) = delete; + ExecCmdClient(const ExecCmdClient&) = delete; + ExecCmdClient& operator=(ExecCmdClient&&) = delete; + ExecCmdClient& operator=(const ExecCmdClient&) = delete; + + virtual ~ExecCmdClient() = default; + + CETL_NODISCARD virtual cetl::optional completed() const = 0; + CETL_NODISCARD virtual cetl::optional takeResult() = 0; + +protected: + ExecCmdClient() = default; + +}; // ExecCmdClient + +} // namespace node +} // namespace svc +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_SVC_NODE_EXEC_CMD_CLIENT_HPP_INCLUDED From 65c211d61d82e204282c84c284b10524e811a510 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 17 Jan 2025 18:56:49 +0200 Subject: [PATCH 070/156] ci fixes --- src/daemon/engine/application.cpp | 2 +- src/sdk/CMakeLists.txt | 3 ++- src/sdk/node_command_client.cpp | 9 +++++---- src/sdk/svc/node/exec_cmd_client.cpp | 17 +++++++++-------- src/sdk/svc/node/exec_cmd_client.hpp | 8 ++++---- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index d288885..d72eb68 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -7,8 +7,8 @@ #include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" -#include "svc/svc_helpers.hpp" #include "svc/node/exec_cmd_service.hpp" +#include "svc/svc_helpers.hpp" #include #include diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index 1ecfb6a..e912e71 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -7,7 +7,8 @@ cmake_minimum_required(VERSION 3.27) add_cyphal_library( NAME sdk - DSDL_FILES ${submodules_dir}/nunavut/submodules/public_regulated_data_types/uavcan/node/435.ExecuteCommand.1.3.dsdl + DSDL_FILES + "uavcan/node/435.ExecuteCommand.1.3.dsdl" ALLOW_EXPERIMENTAL_LANGUAGES LANGUAGE cpp LANGUAGE_STANDARD ${CYPHAL_LANGUAGE_STANDARD} diff --git a/src/sdk/node_command_client.cpp b/src/sdk/node_command_client.cpp index ad9c842..d3d55a5 100644 --- a/src/sdk/node_command_client.cpp +++ b/src/sdk/node_command_client.cpp @@ -11,6 +11,7 @@ #include "sdk_factory.hpp" #include "svc/node/exec_cmd_spec.hpp" +#include #include #include @@ -39,10 +40,10 @@ class NodeCommandClientImpl final : public NodeCommandClient // NodeCommandClient - int sendCommand(const cetl::span node_ids, - const Command::NodeRequest& node_request, - const std::chrono::microseconds timeout, - Command::ResultHandler result_handler) override + int sendCommand(const cetl::span /* node_ids */, + const Command::NodeRequest& /* node_request */, + const std::chrono::microseconds /* timeout */, + Command::ResultHandler /* result_handler */) override { // using ExecCmdRequest = ExecCmdSvcSpec::Request; // using RequestPayload = ExecCmdSvcSpec::Request::_traits_::TypeOf::payload; diff --git a/src/sdk/svc/node/exec_cmd_client.cpp b/src/sdk/svc/node/exec_cmd_client.cpp index 1d36589..3aae170 100644 --- a/src/sdk/svc/node/exec_cmd_client.cpp +++ b/src/sdk/svc/node/exec_cmd_client.cpp @@ -12,6 +12,7 @@ #include +#include #include #include @@ -34,10 +35,10 @@ namespace class ExecCmdClientImpl final : public ExecCmdClient { public: - ExecCmdClientImpl(cetl::pmr::memory_resource& memory, - common::ipc::ClientRouter::Ptr ipc_router, - Spec::Request&& request, - const std::chrono::microseconds timeout) + ExecCmdClientImpl(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + Spec::Request&& request, + const std::chrono::microseconds timeout) : memory_{memory} , logger_{common::getLogger("svc")} , request_{std::move(request)} @@ -107,10 +108,10 @@ class ExecCmdClientImpl final : public ExecCmdClient } // namespace -CETL_NODISCARD ExecCmdClient::Ptr ExecCmdClient::make(cetl::pmr::memory_resource& memory, - common::ipc::ClientRouter::Ptr ipc_router, - Spec::Request&& request, - const std::chrono::microseconds timeout) +CETL_NODISCARD ExecCmdClient::Ptr ExecCmdClient::make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + Spec::Request&& request, + const std::chrono::microseconds timeout) { return std::make_shared(memory, ipc_router, std::move(request), timeout); } diff --git a/src/sdk/svc/node/exec_cmd_client.hpp b/src/sdk/svc/node/exec_cmd_client.hpp index ce1a83f..daa4349 100644 --- a/src/sdk/svc/node/exec_cmd_client.hpp +++ b/src/sdk/svc/node/exec_cmd_client.hpp @@ -35,10 +35,10 @@ class ExecCmdClient using NodeResponse = uavcan::node::ExecuteCommand_1_3::Response; using Result = std::unordered_map; - CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, - common::ipc::ClientRouter::Ptr ipc_router, - Spec::Request&& request, - const std::chrono::microseconds timeout); + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + Spec::Request&& request, + const std::chrono::microseconds timeout); ExecCmdClient(ExecCmdClient&&) = delete; ExecCmdClient(const ExecCmdClient&) = delete; From cc451c7f1cff7dca4f467f024e78224c8c357ee9 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 17 Jan 2025 18:58:44 +0200 Subject: [PATCH 071/156] ci fixes --- src/sdk/node_command_client.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sdk/node_command_client.cpp b/src/sdk/node_command_client.cpp index d3d55a5..9028cd8 100644 --- a/src/sdk/node_command_client.cpp +++ b/src/sdk/node_command_client.cpp @@ -41,9 +41,9 @@ class NodeCommandClientImpl final : public NodeCommandClient // NodeCommandClient int sendCommand(const cetl::span /* node_ids */, - const Command::NodeRequest& /* node_request */, - const std::chrono::microseconds /* timeout */, - Command::ResultHandler /* result_handler */) override + const Command::NodeRequest& /* node_request */, + const std::chrono::microseconds /* timeout */, + Command::ResultHandler /* result_handler */) override { // using ExecCmdRequest = ExecCmdSvcSpec::Request; // using RequestPayload = ExecCmdSvcSpec::Request::_traits_::TypeOf::payload; From 7853dca545f803ead213fb6b65504f7ccf505159 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 21 Jan 2025 19:25:44 +0200 Subject: [PATCH 072/156] implemented client side of exec cmd --- include/ocvsmd/platform/defines.hpp | 41 +++++ .../platform/posix_executor_extension.hpp | 2 + include/ocvsmd/sdk/execution.hpp | 154 ++++++++++++++++++ include/ocvsmd/sdk/node_command_client.hpp | 26 ++- src/cli/main.cpp | 35 ++-- .../svc/node/ExecCmdSvcRequest.0.1.dsdl | 1 + src/daemon/engine/application.cpp | 2 +- .../engine/svc/node/exec_cmd_service.cpp | 15 +- src/daemon/engine/svc/svc_helpers.hpp | 4 +- src/sdk/node_command_client.cpp | 84 +++++++--- src/sdk/svc/node/exec_cmd_client.cpp | 44 ++--- src/sdk/svc/node/exec_cmd_client.hpp | 18 +- 12 files changed, 341 insertions(+), 85 deletions(-) create mode 100644 include/ocvsmd/sdk/execution.hpp diff --git a/include/ocvsmd/platform/defines.hpp b/include/ocvsmd/platform/defines.hpp index 98a9746..29af730 100644 --- a/include/ocvsmd/platform/defines.hpp +++ b/include/ocvsmd/platform/defines.hpp @@ -6,12 +6,20 @@ #ifndef OCVSMD_PLATFORM_DEFINES_HPP_INCLUDED #define OCVSMD_PLATFORM_DEFINES_HPP_INCLUDED +#include +#include + +#include + #ifdef PLATFORM_OS_TYPE_BSD # include "bsd/kqueue_single_threaded_executor.hpp" #else # include "linux/epoll_single_threaded_executor.hpp" #endif +#include +#include + namespace ocvsmd { namespace platform @@ -23,6 +31,39 @@ using SingleThreadedExecutor = bsd::KqueueSingleThreadedExecutor; using SingleThreadedExecutor = Linux::EpollSingleThreadedExecutor; #endif +template +void waitPollingUntil(Executor& executor, Predicate predicate) +{ + spdlog::trace("Waiting for predicate to be fulfilled..."); + + libcyphal::Duration worst_lateness{0}; + while (!predicate()) + { + const auto spin_result = executor.spinOnce(); + worst_lateness = std::max(worst_lateness, spin_result.worst_lateness); + + // Above `spinOnce` might fulfill the predicate. + if (predicate()) + { + break; + } + + // Poll awaitable resources but awake at least once per second. + libcyphal::Duration timeout{std::chrono::seconds{1}}; + if (spin_result.next_exec_time.has_value()) + { + timeout = std::min(timeout, spin_result.next_exec_time.value() - executor.now()); + } + + if (const auto maybe_poll_failure = executor.pollAwaitableResourcesFor(cetl::make_optional(timeout))) + { + spdlog::warn("Failed to poll awaitable resources."); + } + } + + spdlog::debug("Predicate is fulfilled (worst_lateness={}us).", worst_lateness.count()); +} + } // namespace platform } // namespace ocvsmd diff --git a/include/ocvsmd/platform/posix_executor_extension.hpp b/include/ocvsmd/platform/posix_executor_extension.hpp index dc78087..488fda6 100644 --- a/include/ocvsmd/platform/posix_executor_extension.hpp +++ b/include/ocvsmd/platform/posix_executor_extension.hpp @@ -9,8 +9,10 @@ #include #include #include +#include #include #include +#include namespace ocvsmd { diff --git a/include/ocvsmd/sdk/execution.hpp b/include/ocvsmd/sdk/execution.hpp new file mode 100644 index 0000000..372964d --- /dev/null +++ b/include/ocvsmd/sdk/execution.hpp @@ -0,0 +1,154 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_EXECUTION_HPP_INCLUDED +#define OCVSMD_SDK_EXECUTION_HPP_INCLUDED + +#include "ocvsmd/platform/defines.hpp" + +#include + +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ + +/// Internal implementation details. +/// Not supposed to be used directly by the users of the SDK. +/// +namespace detail +{ + +template +class StateOf; + +template +class ReceiverOf final +{ +public: + explicit ReceiverOf(std::shared_ptr> state) + : state_{std::move(state)} + { + } + + template + void operator()(Args&&... args) + { + state_->maybe_result_.emplace(std::forward(args)...); + } + +private: + std::shared_ptr> state_; + +}; // ReceiverOf + +template +class StateOf final : public std::enable_shared_from_this> +{ +public: + bool completed() const + { + return maybe_result_.has_value(); + } + + Result get() + { + CETL_DEBUG_ASSERT(maybe_result_, ""); + return std::move(*maybe_result_); + } + + ReceiverOf makeReceiver() + { + return ReceiverOf{this->shared_from_this()}; + } + +private: + friend class ReceiverOf; + + cetl::optional maybe_result_; + +}; // StateOf + +} // namespace detail + +/// Abstract interface of a result sender. +/// +template +class SenderOf +{ +public: + using Ptr = std::unique_ptr; + using Result = Result_; + + SenderOf(SenderOf&&) = delete; + SenderOf(const SenderOf&) = delete; + SenderOf& operator=(SenderOf&&) = delete; + SenderOf& operator=(const SenderOf&) = delete; + + virtual ~SenderOf() = default; + + /// Initiates an operation execution by submitting a given receiver to this sender. + /// + /// The submit "consumes" the receiver (no longer usable after this call). + /// + template + void submit(Receiver&& receiver) + { + submitImpl([receive = std::forward(receiver)](Result&& result) mutable { + // + receive(std::move(result)); + }); + } + +protected: + SenderOf() = default; + + virtual void submitImpl(std::function&& receiver) = 0; + +}; // SenderOf + +/// Initiates an operation execution by submitting a given receiver to the sender. +/// +/// The submit "consumes" the receiver (no longer usable after this call). +/// +template +void submit(Sender& sender, Receiver&& receiver) +{ + sender.submit(std::forward(receiver)); +} + +/// Initiates an operation execution by submitting a given receiver to the sender. +/// +/// The submit "consumes" the receiver (no longer usable after this call). +/// +template +void submit(std::unique_ptr& sender_ptr, Receiver&& receiver) +{ + sender_ptr->submit(std::forward(receiver)); +} + +/// Algorithm that synchronously waits for the sender to emit result. +/// +/// This algorithm "consumes" the sender, meaning that the sender is no longer usable after this call. +/// +template +Result sync_wait(Executor& executor, Sender&& sender) +{ + auto state = std::make_shared>(); + + submit(sender, state->makeReceiver()); + + platform::waitPollingUntil(executor, [state] { return state->completed(); }); + + return state->get(); +} + +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_EXECUTION_HPP_INCLUDED diff --git a/include/ocvsmd/sdk/node_command_client.hpp b/include/ocvsmd/sdk/node_command_client.hpp index 4a904f4..06ac00f 100644 --- a/include/ocvsmd/sdk/node_command_client.hpp +++ b/include/ocvsmd/sdk/node_command_client.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED #define OCVSMD_SDK_NODE_COMMAND_CLIENT_HPP_INCLUDED +#include "execution.hpp" + #include #include @@ -13,7 +15,6 @@ #include #include -#include #include #include @@ -39,25 +40,20 @@ class NodeCommandClient using NodeRequest = uavcan::node::ExecuteCommand_1_3::Request; using NodeResponse = uavcan::node::ExecuteCommand_1_3::Response; - /// Empty option indicates that the corresponding node did not return a response on time. - using Success = std::unordered_map>; - - /// `errno`-like error code. - using Failure = int; - - using Result = cetl::variant; - using ResultHandler = std::function; + using Success = std::unordered_map; + using Failure = int; // `errno`-like error code. + using Result = cetl::variant; }; // Command - /// All requests are sent concurrently. - /// The callback is executed when the last response has arrived, + /// Request is sent concurrently to all nodes. + /// Duplicate node IDs are ignored. + /// Result will be available when the last response has arrived, /// or the timeout has expired. /// - virtual int sendCommand(const cetl::span node_ids, - const Command::NodeRequest& node_request, - const std::chrono::microseconds timeout, - Command::ResultHandler result_handler) = 0; + virtual SenderOf::Ptr sendCommand(const cetl::span node_ids, + const Command::NodeRequest& node_request, + const std::chrono::microseconds timeout) = 0; protected: NodeCommandClient() = default; diff --git a/src/cli/main.cpp b/src/cli/main.cpp index e6c0d76..5757457 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -5,9 +5,10 @@ #include #include +#include +#include #include -#include #include #include @@ -15,14 +16,17 @@ #include #include -#include +#include #include +#include #include +#include #include #include #include #include // NOLINT #include +#include namespace { @@ -123,20 +127,29 @@ int main(const int argc, const char** const argv) return EXIT_FAILURE; } - while (g_running != 0) + auto node_cmd_client = daemon->getNodeCommandClient(); { - const auto spin_result = executor.spinOnce(); + using Command = ocvsmd::sdk::NodeCommandClient::Command; - // Poll awaitable resources but awake at least once per second. - libcyphal::Duration timeout{1s}; - if (spin_result.next_exec_time.has_value()) + constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; + constexpr std::array node_ids = {42}; + const Command::NodeRequest node_request{cmd_id, {{}, &memory}, &memory}; + auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); + + auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); + if (const auto* const err = cetl::get_if(&cmd_result)) { - timeout = std::min(timeout, spin_result.next_exec_time.value() - executor.now()); + spdlog::error("Failed to send command: {}", std::strerror(*err)); } - - if (const auto maybe_poll_failure = executor.pollAwaitableResourcesFor(cetl::make_optional(timeout))) + else { - spdlog::warn("Failed to poll awaitable resources."); + const auto responds = cetl::get(std::move(cmd_result)); + for (const auto& node_and_respond : responds) + { + spdlog::info("Node {} responded with status: {}.", + node_and_respond.first, + node_and_respond.second.status); + } } } diff --git a/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl index 0d3feff..12caa68 100644 --- a/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl +++ b/src/common/dsdl/ocvsmd/common/svc/node/ExecCmdSvcRequest.0.1.dsdl @@ -1,3 +1,4 @@ +uint64 timeout_us uint16[<=128] node_ids UavcanNodeExecCmdReq.0.1 payload diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index d72eb68..a8ca25a 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -70,7 +70,7 @@ cetl::optional Application::init() auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); - const svc::ScvContext svc_context{memory_, *ipc_router_, *presentation_}; + const svc::ScvContext svc_context{memory_, executor_, *ipc_router_, *presentation_}; svc::node::ExecCmdService::registerWithContext(svc_context); if (0 != ipc_router_->start()) diff --git a/src/daemon/engine/svc/node/exec_cmd_service.cpp b/src/daemon/engine/svc/node/exec_cmd_service.cpp index 0f4ed06..29ebe85 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.cpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.cpp @@ -106,10 +106,11 @@ class ExecCmdServiceImpl final // we just ignore duplicates, and work with unique ones. const SetOfNodeIds unique_node_ids{request.node_ids.begin(), request.node_ids.end()}; + const auto deadline = service_.context_.executor.now() + std::chrono::microseconds{request.timeout_us}; const CyphalExecCmdSvc::Request cy_request{request.payload.command, request.payload.parameter, &memory()}; for (const auto node_id : unique_node_ids) { - if (const auto err = makeCyphalSvcCallFor(node_id, cy_request)) + if (const auto err = makeCyphalSvcCallFor(deadline, node_id, cy_request)) { complete(err); return; @@ -137,7 +138,7 @@ class ExecCmdServiceImpl final cetl::pmr::memory_resource& memory() const { - return service_.context_.memory_; + return service_.context_.memory; } // We are not interested in handling these events. @@ -150,7 +151,9 @@ class ExecCmdServiceImpl final complete(ECANCELED); } - int makeCyphalSvcCallFor(const std::uint16_t node_id, const CyphalExecCmdSvc::Request& cy_request) + int makeCyphalSvcCallFor(const libcyphal::TimePoint deadline, + const std::uint16_t node_id, + const CyphalExecCmdSvc::Request& cy_request) { using CyphalMakeFailure = libcyphal::presentation::Presentation::MakeFailure; @@ -166,7 +169,7 @@ class ExecCmdServiceImpl final } auto cy_svc_client = cetl::get(std::move(cy_make_result)); - auto cy_req_result = cy_svc_client.request({}, cy_request); + auto cy_req_result = cy_svc_client.request(deadline, cy_request); if (const auto* cy_failure = cetl::get_if(&cy_req_result)) { const auto err = failureToErrorCode(*cy_failure); @@ -239,7 +242,7 @@ class ExecCmdServiceImpl final id_to_fsm_.erase(fsm_id); } - const ScvContext& context_; + const ScvContext context_; std::uint64_t next_fsm_id_{0}; std::unordered_map id_to_fsm_; common::LoggerPtr logger_{common::getLogger("engine")}; @@ -251,7 +254,7 @@ class ExecCmdServiceImpl final void ExecCmdService::registerWithContext(const ScvContext& context) { using Impl = ExecCmdServiceImpl; - context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl(context)); + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context}); } } // namespace node diff --git a/src/daemon/engine/svc/svc_helpers.hpp b/src/daemon/engine/svc/svc_helpers.hpp index 839e6b5..7102e8a 100644 --- a/src/daemon/engine/svc/svc_helpers.hpp +++ b/src/daemon/engine/svc/svc_helpers.hpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -30,7 +31,8 @@ namespace svc struct ScvContext { - cetl::pmr::memory_resource& memory_; + cetl::pmr::memory_resource& memory; + libcyphal::IExecutor& executor; common::ipc::ServerRouter& ipc_router; libcyphal::presentation::Presentation& presentation; diff --git a/src/sdk/node_command_client.cpp b/src/sdk/node_command_client.cpp index 9028cd8..60683b6 100644 --- a/src/sdk/node_command_client.cpp +++ b/src/sdk/node_command_client.cpp @@ -8,16 +8,20 @@ #include "ipc/channel.hpp" #include "ipc/client_router.hpp" #include "logging.hpp" +#include "ocvsmd/sdk/execution.hpp" #include "sdk_factory.hpp" +#include "svc/node/exec_cmd_client.hpp" #include "svc/node/exec_cmd_spec.hpp" #include #include #include +#include #include #include #include +#include #include #include @@ -40,36 +44,64 @@ class NodeCommandClientImpl final : public NodeCommandClient // NodeCommandClient - int sendCommand(const cetl::span /* node_ids */, - const Command::NodeRequest& /* node_request */, - const std::chrono::microseconds /* timeout */, - Command::ResultHandler /* result_handler */) override + SenderOf::Ptr sendCommand(const cetl::span node_ids, + const Command::NodeRequest& node_request, + const std::chrono::microseconds timeout) override { - // using ExecCmdRequest = ExecCmdSvcSpec::Request; - // using RequestPayload = ExecCmdSvcSpec::Request::_traits_::TypeOf::payload; - // - // if (node_ids.size() > ExecCmdSvcSpec::Request::_traits_::ArrayCapacity::node_ids) - // { - // logger_->error("Too many node IDs: {} (max {}).", - // node_ids.size(), - // ExecCmdSvcSpec::Request::_traits_::ArrayCapacity::node_ids); - // return EINVAL; - // } - // - // auto exec_cmd_ch = ipc_router_->makeChannel(ExecCmdSvcSpec::svc_full_name); - // exec_cmd_ch.subscribe([](const auto& event) {}); - // - // const RequestPayload request_payload{node_request.command, node_request.parameter, &memory_}; - // const ExecCmdRequest exec_cmd_req{{node_ids.begin(), node_ids.end()}, request_payload, &memory_}; - // - // return exec_cmd_ch.send(exec_cmd_req); - - return 0; + common::svc::node::ExecCmdSpec::Request request{std::max(0, timeout.count()), + {node_ids.begin(), node_ids.end(), &memory_}, + {node_request.command, node_request.parameter, &memory_}, + &memory_}; + + auto svc_client = ExecCmdClient::make(memory_, ipc_router_, std::move(request), timeout); + + return std::make_unique(std::move(svc_client)); } private: - using ExecCmdSvcSpec = common::svc::node::ExecCmdSpec; - using ExecCmdChannel = common::ipc::Channel; + using ExecCmdClient = svc::node::ExecCmdClient; + + class CommandSender final : public SenderOf + { + public: + explicit CommandSender(ExecCmdClient::Ptr svc_client) + : svc_client_{std::move(svc_client)} + { + } + + void submitImpl(std::function&& receiver) override + { + svc_client_->submit([receiver = std::move(receiver)](ExecCmdClient::Result&& result) mutable { + // + receiver(cetl::visit( + [](auto&& value) { + // + return transform(std::forward(value)); + }, + std::move(result))); + }); + } + + private: + static Command::Result transform(ExecCmdClient::Success&& svc_success) + { + Command::Success cmd_success{}; + cmd_success.reserve(svc_success.size()); + for (auto&& pair : std::move(svc_success)) + { + cmd_success.emplace(pair.first, std::move(pair.second)); + } + return cmd_success; + } + + static Command::Result transform(ExecCmdClient::Failure failure) + { + return Command::Failure{failure}; + } + + ExecCmdClient::Ptr svc_client_; + + }; // CommandSender cetl::pmr::memory_resource& memory_; common::LoggerPtr logger_; diff --git a/src/sdk/svc/node/exec_cmd_client.cpp b/src/sdk/svc/node/exec_cmd_client.cpp index 3aae170..9d1436f 100644 --- a/src/sdk/svc/node/exec_cmd_client.cpp +++ b/src/sdk/svc/node/exec_cmd_client.cpp @@ -7,6 +7,7 @@ #include "ipc/channel.hpp" #include "ipc/client_router.hpp" +#include "ipc/ipc_types.hpp" #include "logging.hpp" #include "svc/node/exec_cmd_spec.hpp" @@ -17,6 +18,7 @@ #include #include +#include #include #include #include @@ -44,30 +46,18 @@ class ExecCmdClientImpl final : public ExecCmdClient , request_{std::move(request)} , channel_{ipc_router->makeChannel(Spec::svc_full_name())} { - channel_.subscribe([this](const auto& event_var) { - // - cetl::visit([this](const auto& event) { handleEvent(event); }, event_var); - }); - // TODO: handle timeout (void) timeout; } - CETL_NODISCARD cetl::optional completed() const override + void submitImpl(std::function&& receiver) override { - return completion_error_code_; - } - - CETL_NODISCARD cetl::optional takeResult() override - { - if (!completion_error_code_.has_value()) - { - return cetl::nullopt; - } + receiver_ = std::move(receiver); - Result result; - std::swap(result, node_id_to_response_); - return result; + channel_.subscribe([this](const auto& event_var) { + // + cetl::visit([this](const auto& event) { handleEvent(event); }, event_var); + }); } private: @@ -79,7 +69,9 @@ class ExecCmdClientImpl final : public ExecCmdClient if (const auto err = channel_.send(request_)) { - completion_error_code_.emplace(err); + CETL_DEBUG_ASSERT(receiver_, ""); + + receiver_(Failure{err}); } } @@ -91,18 +83,26 @@ class ExecCmdClientImpl final : public ExecCmdClient node_id_to_response_.emplace(input.node_id, std::move(node_response)); } - void handleEvent(const Channel::Completed& completed) + void handleEvent(const Channel::Completed& completed) const { + CETL_DEBUG_ASSERT(receiver_, ""); + logger_->debug("ExecCmdClient::handleEvent({}).", completed); - completion_error_code_ = static_cast(completed.error_code); + + if (completed.error_code != common::ipc::ErrorCode::Success) + { + receiver_(static_cast(completed.error_code)); + return; + } + receiver_(Success{node_id_to_response_}); } cetl::pmr::memory_resource& memory_; common::LoggerPtr logger_; Spec::Request request_; Channel channel_; + std::function receiver_; std::unordered_map node_id_to_response_; - cetl::optional completion_error_code_; }; // ExecCmdClientImpl diff --git a/src/sdk/svc/node/exec_cmd_client.hpp b/src/sdk/svc/node/exec_cmd_client.hpp index daa4349..1166afe 100644 --- a/src/sdk/svc/node/exec_cmd_client.hpp +++ b/src/sdk/svc/node/exec_cmd_client.hpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace ocvsmd { @@ -33,7 +34,10 @@ class ExecCmdClient using Ptr = std::shared_ptr; using Spec = common::svc::node::ExecCmdSpec; using NodeResponse = uavcan::node::ExecuteCommand_1_3::Response; - using Result = std::unordered_map; + + using Success = std::unordered_map; + using Failure = int; // `errno`-like error code + using Result = cetl::variant; CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, const common::ipc::ClientRouter::Ptr& ipc_router, @@ -47,12 +51,20 @@ class ExecCmdClient virtual ~ExecCmdClient() = default; - CETL_NODISCARD virtual cetl::optional completed() const = 0; - CETL_NODISCARD virtual cetl::optional takeResult() = 0; + template + void submit(Receiver&& receiver) + { + submitImpl([receive = std::forward(receiver)](Result&& result) mutable { + // + receive(std::move(result)); + }); + } protected: ExecCmdClient() = default; + virtual void submitImpl(std::function&& receiver) = 0; + }; // ExecCmdClient } // namespace node From 53702d48b79785b24ef86ff6ac233089c928d5a1 Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 21 Jan 2025 22:56:59 +0200 Subject: [PATCH 073/156] fix build --- src/cli/main.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 5757457..782a948 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -129,11 +129,12 @@ int main(const int argc, const char** const argv) auto node_cmd_client = daemon->getNodeCommandClient(); { - using Command = ocvsmd::sdk::NodeCommandClient::Command; + using Command = ocvsmd::sdk::NodeCommandClient::Command; + using CommandParam = Command::NodeRequest::_traits_::TypeOf::parameter; constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; constexpr std::array node_ids = {42}; - const Command::NodeRequest node_request{cmd_id, {{}, &memory}, &memory}; + const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); From 6e41843ffaf580528a12819e6d62cc44295331e0 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 22 Jan 2025 14:05:35 +0200 Subject: [PATCH 074/156] implemented log files flush level --- README.md | 33 +++++++- init.d/ocvsmd | 8 +- src/cli/main.cpp | 59 ++----------- src/cli/setup_logging.hpp | 133 +++++++++++++++++++++++++++++ src/daemon/main.cpp | 69 +--------------- src/daemon/setup_logging.hpp | 156 +++++++++++++++++++++++++++++++++++ 6 files changed, 330 insertions(+), 128 deletions(-) create mode 100644 src/cli/setup_logging.hpp create mode 100644 src/daemon/setup_logging.hpp diff --git a/README.md b/README.md index 81c6d8c..8a4d0ad 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,32 @@ Then one of the two presets depending on your system: ```bash sudo /etc/init.d/ocvsmd stop ``` -- View logs: - ```bash - sudo grep -r "ocvsmd\[" /var/log/syslog - ``` +### Logging + +#### View logs: + - Syslog: + ```bash + sudo grep -r "ocvsmd\[" /var/log/syslog + ``` + - Log files: + ```bash + cat /var/log/ocvsmd.log + ``` +#### Manipulate log levels: + + Append `SPDLOG_LEVEL` and/or `SPDLOG_FLUSH_LEVEL` to the daemon command in the init script + to enable more verbose logging level (`trace` or `debug`). + Default level is `info`. More severe levels are: `warn`, `error` and `critical`. + `off` level disables the logging. + +By default, the log files are not immediately flushed to disk (at `off` level). +To enable flushing, set `SPDLOG_FLUSH_LEVEL` to a required default (or per component) level. + + - Example to set default level: + ```bash + sudo /etc/init.d/ocvsmd start SPDLOG_LEVEL=trace SPDLOG_FLUSH_LEVEL=trace + ``` + - Example to set default and per component level (comma separated pairs): + ```bash + sudo /etc/init.d/ocvsmd restart SPDLOG_LEVEL=debug,ipc=off SPDLOG_FLUSH_LEVEL=warn,engine=debug + ``` diff --git a/init.d/ocvsmd b/init.d/ocvsmd index ade5d09..52d340c 100755 --- a/init.d/ocvsmd +++ b/init.d/ocvsmd @@ -15,12 +15,13 @@ PIDFILE="/var/run/${DAEMON_NAME}.pid" case "$1" in start) - echo "Starting $DAEMON_NAME..." + echo "Starting $DAEMON_NAME" "$@" "..." if [ -f $PIDFILE ]; then echo "$DAEMON_NAME is already running." exit 1 fi - start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON_PATH + shift + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON_PATH "$@" ret=$? if [ $ret -eq 0 ]; then echo "$DAEMON_NAME started." @@ -41,7 +42,8 @@ case "$1" in ;; restart) $0 stop - $0 start + shift + $0 start "$@" ;; status) if [ -f $PIDFILE ]; then diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 782a948..b803c5a 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -3,6 +3,8 @@ // SPDX-License-Identifier: MIT // +#include "setup_logging.hpp" + #include #include #include @@ -10,10 +12,6 @@ #include -#include -#include -#include -#include #include #include @@ -23,9 +21,7 @@ #include #include #include -#include #include // NOLINT -#include #include namespace @@ -56,52 +52,6 @@ void setupSignalHandlers() ::sigaction(SIGTERM, &sigbreak, nullptr); } -/// Sets up the logging system. -/// -/// File sink is used for all loggers (with Info default level). -/// -void setupLogging(const int argc, const char** const argv) -{ - using spdlog::sinks::rotating_file_sink_st; - - try - { - constexpr std::size_t log_max_files = 4; - constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB - - const std::string log_prefix = "ocvsmd-cli"; - const std::string log_file_nm = log_prefix + ".log"; - const auto log_file_path = "./" + log_file_nm; - - // Drop all existing loggers, including the default one, so that we can reconfigure them. - spdlog::drop_all(); - - const auto file_sink = std::make_shared( // - log_file_path, - log_file_max_size, - log_max_files); - file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); - - const auto default_logger = std::make_shared("", file_sink); - register_logger(default_logger); - set_default_logger(default_logger); - - // Register specific subsystem loggers. - // - register_logger(std::make_shared("ipc", file_sink)); - register_logger(std::make_shared("sdk", file_sink)); - register_logger(std::make_shared("svc", file_sink)); - - // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug,ipc=trace`). - spdlog::cfg::load_argv_levels(argc, argv); - - } catch (const std::exception& ex) - { - std::cerr << "Failed to setup logging: " << ex.what() << '\n'; - std::exit(EXIT_FAILURE); - } -} - } // namespace int main(const int argc, const char** const argv) @@ -127,17 +77,20 @@ int main(const int argc, const char** const argv) return EXIT_FAILURE; } - auto node_cmd_client = daemon->getNodeCommandClient(); + // Demo of daemon's node command client, sending a command to node 42. { using Command = ocvsmd::sdk::NodeCommandClient::Command; using CommandParam = Command::NodeRequest::_traits_::TypeOf::parameter; + auto node_cmd_client = daemon->getNodeCommandClient(); + constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; constexpr std::array node_ids = {42}; const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); + if (const auto* const err = cetl::get_if(&cmd_result)) { spdlog::error("Failed to send command: {}", std::strerror(*err)); diff --git a/src/cli/setup_logging.hpp b/src/cli/setup_logging.hpp new file mode 100644 index 0000000..ad5bd12 --- /dev/null +++ b/src/cli/setup_logging.hpp @@ -0,0 +1,133 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_CLI_SETUP_LOGGING_HPP_INCLUDED +#define OCVSMD_CLI_SETUP_LOGGING_HPP_INCLUDED + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace detail +{ + +inline void loadFlushLevels(const std::string& flush_levels) +{ + constexpr std::size_t max_levels_len = 512; + if (flush_levels.empty() || (flush_levels.size() > max_levels_len)) + { + return; + } + + auto key_vals = spdlog::cfg::helpers::extract_key_vals_(flush_levels); // NOLINT + for (auto& name_level : key_vals) + { + const auto& logger_name = name_level.first; + auto& level_name = spdlog::cfg::helpers::to_lower_(name_level.second); // NOLINT + const auto level = spdlog::level::from_str(level_name); + // Ignore unrecognized level names. + if (level == spdlog::level::off && level_name != "off") + { + continue; + } + + if (const auto logger = spdlog::get(logger_name)) + { + logger->flush_on(level); + } + } + + // Apply default flush level to all other loggers (if not specified in the `key_vals`). + // + const auto default_flush_level = spdlog::default_logger()->flush_level(); + spdlog::apply_all([&key_vals, default_flush_level](const auto& logger) { + // + // Skip default logger and loggers with explicit flush levels. + if (!logger->name().empty() && (key_vals.find(logger->name()) == key_vals.end())) + { + logger->flush_on(default_flush_level); + } + }); +} + +/// Search for SPDLOG_FLUSH_LEVEL= in the args and use it to init the flush levels. +/// +inline void loadArgvFlushLevels(const int argc, const char** const argv) +{ + const std::string spdlog_level_prefix = "SPDLOG_FLUSH_LEVEL="; + for (int i = 1; i < argc; i++) + { + const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + if (arg_str.find(spdlog_level_prefix) == 0) + { + const auto levels_str = arg_str.substr(spdlog_level_prefix.size()); + loadFlushLevels(levels_str); + } + } +} + +} // namespace detail + +/// Sets up the logging system. +/// +/// File sink is used for all loggers (with Info default level). +/// +inline void setupLogging(const int argc, const char** const argv) +{ + using spdlog::sinks::rotating_file_sink_st; + + try + { + constexpr std::size_t log_max_files = 4; + constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB + + const std::string log_prefix = "ocvsmd-cli"; + const std::string log_file_nm = log_prefix + ".log"; + const auto log_file_path = "./" + log_file_nm; + + // Drop all existing loggers, including the default one, so that we can reconfigure them. + spdlog::drop_all(); + + const auto file_sink = std::make_shared( // + log_file_path, + log_file_max_size, + log_max_files); + file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); + + const auto default_logger = std::make_shared("", file_sink); + register_logger(default_logger); + set_default_logger(default_logger); + + // Register specific subsystem loggers. + // + register_logger(std::make_shared("ipc", file_sink)); + register_logger(std::make_shared("sdk", file_sink)); + register_logger(std::make_shared("svc", file_sink)); + + // Accept `SPDLOG_LEVEL` & `SPDLOG_FLUSH_LEVEL` arguments (like `SPDLOG_LEVEL=debug,ipc=trace`). + // + spdlog::cfg::load_argv_levels(argc, argv); + detail::loadArgvFlushLevels(argc, argv); + + // Insert "--…--" just to have clearer separation in the log file between two different process runs. + spdlog::info("--------------------------"); + + } catch (const std::exception& ex) + { + std::cerr << "Failed to setup logging: " << ex.what() << '\n'; + std::exit(EXIT_FAILURE); + } +} + +#endif // OCVSMD_CLI_SETUP_LOGGING_HPP_INCLUDED diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index e4e8e06..6cbbb33 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -4,12 +4,8 @@ // #include "engine/application.hpp" +#include "setup_logging.hpp" -#include -#include -#include -#include -#include #include #include @@ -20,14 +16,11 @@ #include #include #include -#include #include -#include #include // NOLINT #include #include #include -#include #include #include @@ -61,12 +54,6 @@ void setupSignalHandlers() ::sigaction(SIGTERM, &sigbreak, nullptr); } -bool writeString(const int fd, const char* const str) -{ - const auto str_len = strlen(str); - return str_len == ::write(fd, str, str_len); -} - void exitWithFailure(const int fd, const char* const msg) { const char* const err_txt = std::strerror(errno); @@ -306,60 +293,6 @@ int daemonize() return -1; // Unreachable actually b/c of `::exit` call. } -/// Sets up the logging system. -/// -/// Both syslog and file logging sinks are used. -/// The syslog sink is used for the default logger only (with Info default level), -/// while the file sink is used for all loggers (with Debug default level). -/// -void setupLogging(const int err_fd, const bool is_daemonized, const int argc, const char** const argv) -{ - using spdlog::sinks::syslog_sink_st; - using spdlog::sinks::rotating_file_sink_st; - - try - { - constexpr std::size_t log_files_max = 4; - constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB - - const std::string log_prefix = "ocvsmd"; - const std::string log_file_nm = log_prefix + ".log"; - const std::string log_file_dir = is_daemonized ? "/var/log/" : "./"; - const auto log_file_path = log_file_dir + log_file_nm; - - // Drop all existing loggers, including the default one, so that we can reconfigure them. - spdlog::drop_all(); - - const auto file_sink = std::make_shared(log_file_path, log_file_max_size, log_files_max); - file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); - - const int syslog_facility = is_daemonized ? LOG_DAEMON : LOG_USER; - const auto syslog_sink = std::make_shared(log_prefix, LOG_PID, syslog_facility, true); - syslog_sink->set_pattern("[%l] '%n' | %v"); - - // The default logger goes to all sinks. - // - const std::initializer_list sinks{syslog_sink, file_sink}; - const auto default_logger = std::make_shared("", sinks); - register_logger(default_logger); - set_default_logger(default_logger); - - // Register specific subsystem loggers - they go to the file sink only. - // - register_logger(std::make_shared("ipc", file_sink)); - register_logger(std::make_shared("engine", file_sink)); - - // Accept `SPDLOG_LEVEL` argument (like `SPDLOG_LEVEL=debug,ipc=trace`). - spdlog::cfg::load_argv_levels(argc, argv); - - } catch (const std::exception& ex) - { - writeString(err_fd, "Failed to setup logging: "); - writeString(err_fd, ex.what()); - ::exit(EXIT_FAILURE); - } -} - } // namespace int main(const int argc, const char** const argv) diff --git a/src/daemon/setup_logging.hpp b/src/daemon/setup_logging.hpp new file mode 100644 index 0000000..d1a1ffb --- /dev/null +++ b/src/daemon/setup_logging.hpp @@ -0,0 +1,156 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_SETUP_LOGGING_HPP_INCLUDED +#define OCVSMD_DAEMON_SETUP_LOGGING_HPP_INCLUDED + +#include +#include // NOLINT +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace detail +{ + +inline void loadFlushLevels(const std::string& flush_levels) +{ + constexpr std::size_t max_levels_len = 512; + if (flush_levels.empty() || (flush_levels.size() > max_levels_len)) + { + return; + } + + auto key_vals = spdlog::cfg::helpers::extract_key_vals_(flush_levels); // NOLINT + for (auto& name_level : key_vals) + { + const auto& logger_name = name_level.first; + auto& level_name = spdlog::cfg::helpers::to_lower_(name_level.second); // NOLINT + const auto level = spdlog::level::from_str(level_name); + // Ignore unrecognized level names. + if (level == spdlog::level::off && level_name != "off") + { + continue; + } + + if (const auto logger = spdlog::get(logger_name)) + { + logger->flush_on(level); + } + } + + // Apply default flush level to all other loggers (if not specified in the `key_vals`). + // + const auto default_flush_level = spdlog::default_logger()->flush_level(); + spdlog::apply_all([&key_vals, default_flush_level](const auto& logger) { + // + // Skip default logger and loggers with explicit flush levels. + if (!logger->name().empty() && (key_vals.find(logger->name()) == key_vals.end())) + { + logger->flush_on(default_flush_level); + } + }); +} + +/// Search for SPDLOG_FLUSH_LEVEL= in the args and use it to init the flush levels. +/// +inline void loadArgvFlushLevels(const int argc, const char** const argv) +{ + const std::string spdlog_level_prefix = "SPDLOG_FLUSH_LEVEL="; + for (int i = 1; i < argc; i++) + { + const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + if (arg_str.find(spdlog_level_prefix) == 0) + { + const auto levels_str = arg_str.substr(spdlog_level_prefix.size()); + loadFlushLevels(levels_str); + } + } +} + +} // namespace detail + +inline bool writeString(const int fd, const char* const str) +{ + const auto str_len = strlen(str); + return str_len == ::write(fd, str, str_len); +} + +/// Sets up the logging system. +/// +/// Both syslog and file logging sinks are used. +/// The syslog sink is used for the default logger only (with Info default level), +/// while the file sink is used for all loggers (with Debug default level). +/// +inline void setupLogging(const int err_fd, const bool is_daemonized, const int argc, const char** const argv) +{ + using spdlog::sinks::syslog_sink_st; + using spdlog::sinks::rotating_file_sink_st; + + try + { + constexpr std::size_t log_files_max = 4; + constexpr std::size_t log_file_max_size = 16UL * 1048576UL; // 16 MB + + const std::string log_prefix = "ocvsmd"; + const std::string log_file_nm = log_prefix + ".log"; + const std::string log_file_dir = is_daemonized ? "/var/log/" : "./"; + const auto log_file_path = log_file_dir + log_file_nm; + + // Drop all existing loggers, including the default one, so that we can reconfigure them. + spdlog::drop_all(); + + const auto file_sink = std::make_shared(log_file_path, log_file_max_size, log_files_max); + file_sink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%P] [%n] [%l] %v"); + + const int syslog_facility = is_daemonized ? LOG_DAEMON : LOG_USER; + const auto syslog_sink = std::make_shared(log_prefix, LOG_PID, syslog_facility, true); + syslog_sink->set_pattern("[%l] '%n' | %v"); + + // The default logger goes to all sinks. + // + const std::initializer_list sinks{syslog_sink, file_sink}; + const auto default_logger = std::make_shared("", sinks); + register_logger(default_logger); + set_default_logger(default_logger); + + // Register specific subsystem loggers - they go to the file sink only. + // + register_logger(std::make_shared("ipc", file_sink)); + register_logger(std::make_shared("engine", file_sink)); + + // Accept `SPDLOG_LEVEL` & `SPDLOG_FLUSH_LEVEL` arguments (like `SPDLOG_LEVEL=debug,ipc=trace`). + // + spdlog::cfg::load_argv_levels(argc, argv); + detail::loadArgvFlushLevels(argc, argv); + + // Insert "--…--" just to have clearer separation in the log file between two different process runs. + // + if (spdlog::default_logger()->should_log(spdlog::level::info)) + { + // It goes directly to the file sink to avoid logging into syslog. + file_sink->log({"", spdlog::level::info, "--------------------------"}); + } + + } catch (const std::exception& ex) + { + writeString(err_fd, "Failed to setup logging: "); + writeString(err_fd, ex.what()); + ::exit(EXIT_FAILURE); + } +} + +#endif // OCVSMD_DAEMON_SETUP_LOGGING_HPP_INCLUDED From 25d38117a3e57881528548b0907c4d11daa11f5e Mon Sep 17 00:00:00 2001 From: Sergei Date: Sat, 25 Jan 2025 23:59:03 +0200 Subject: [PATCH 075/156] implemented network socket client/server --- .gitmodules | 1 + src/cli/main.cpp | 2 +- src/common/CMakeLists.txt | 4 + src/common/ipc/pipe/client_context.hpp | 83 ++++++++++ src/common/ipc/pipe/net_socket_client.cpp | 168 ++++++++++++++++++++ src/common/ipc/pipe/net_socket_client.hpp | 68 ++++++++ src/common/ipc/pipe/net_socket_server.cpp | 68 ++++++++ src/common/ipc/pipe/net_socket_server.hpp | 49 ++++++ src/common/ipc/pipe/socket_base.cpp | 167 ++++++++++++++++++++ src/common/ipc/pipe/socket_base.hpp | 73 +++++++++ src/common/ipc/pipe/socket_server_base.cpp | 174 +++++++++++++++++++++ src/common/ipc/pipe/socket_server_base.hpp | 67 ++++++++ src/common/ipc/pipe/unix_socket_base.hpp | 170 -------------------- src/common/ipc/pipe/unix_socket_client.cpp | 21 ++- src/common/ipc/pipe/unix_socket_client.hpp | 10 +- src/common/ipc/pipe/unix_socket_server.cpp | 162 ++----------------- src/common/ipc/pipe/unix_socket_server.hpp | 58 +------ src/common/ipc/server_router.hpp | 2 +- src/common/logging.hpp | 1 + src/daemon/engine/application.cpp | 11 +- src/sdk/daemon.cpp | 11 +- submodules/libcyphal | 2 +- 22 files changed, 971 insertions(+), 401 deletions(-) create mode 100644 src/common/ipc/pipe/client_context.hpp create mode 100644 src/common/ipc/pipe/net_socket_client.cpp create mode 100644 src/common/ipc/pipe/net_socket_client.hpp create mode 100644 src/common/ipc/pipe/net_socket_server.cpp create mode 100644 src/common/ipc/pipe/net_socket_server.hpp create mode 100644 src/common/ipc/pipe/socket_base.cpp create mode 100644 src/common/ipc/pipe/socket_base.hpp create mode 100644 src/common/ipc/pipe/socket_server_base.cpp create mode 100644 src/common/ipc/pipe/socket_server_base.hpp delete mode 100644 src/common/ipc/pipe/unix_socket_base.hpp diff --git a/.gitmodules b/.gitmodules index 0b13fe0..c07468c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,7 @@ [submodule "submodules/libcyphal"] path = submodules/libcyphal url = https://github.com/OpenCyphal-Garage/libcyphal.git + branch = sshirokov/412_rpc_clients [submodule "submodules/libudpard"] path = submodules/libudpard url = https://github.com/OpenCyphal/libudpard diff --git a/src/cli/main.cpp b/src/cli/main.cpp index b803c5a..77d87a2 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -85,7 +85,7 @@ int main(const int argc, const char** const argv) auto node_cmd_client = daemon->getNodeCommandClient(); constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; - constexpr std::array node_ids = {42}; + constexpr std::array node_ids = {42, 43}; const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 7a5046e..58f9396 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -21,6 +21,10 @@ add_cyphal_library( add_library(ocvsmd_common ipc/client_router.cpp + ipc/pipe/net_socket_client.cpp + ipc/pipe/net_socket_server.cpp + ipc/pipe/socket_base.cpp + ipc/pipe/socket_server_base.cpp ipc/pipe/unix_socket_client.cpp ipc/pipe/unix_socket_server.cpp ipc/server_router.cpp diff --git a/src/common/ipc/pipe/client_context.hpp b/src/common/ipc/pipe/client_context.hpp new file mode 100644 index 0000000..c6b40f7 --- /dev/null +++ b/src/common/ipc/pipe/client_context.hpp @@ -0,0 +1,83 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_PIPE_CLIENT_CONTEXT_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_CLIENT_CONTEXT_HPP_INCLUDED + +#include "logging.hpp" +#include "ocvsmd/platform/posix_utils.hpp" +#include "server_pipe.hpp" +#include "socket_base.hpp" + +#include +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class ClientContext final +{ +public: + using Ptr = std::unique_ptr; + + ClientContext(const ServerPipe::ClientId id, const int fd, Logger& logger) + : id_{id} + , logger_{logger} + { + CETL_DEBUG_ASSERT(fd != -1, ""); + + state_.fd = fd; + logger_.trace("ClientContext(fd={}, id={}).", fd, id_); + } + + ~ClientContext() + { + logger_.trace("~ClientContext(fd={}, id={}).", state_.fd, id_); + + platform::posixSyscallError([this] { + // + return ::close(state_.fd); + }); + } + + ClientContext(const ClientContext&) = delete; + ClientContext(ClientContext&&) noexcept = delete; + ClientContext& operator=(const ClientContext&) = delete; + ClientContext& operator=(ClientContext&&) noexcept = delete; + + SocketBase::State& state() noexcept + { + return state_; + } + + void setCallback(libcyphal::IExecutor::Callback::Any&& fd_callback) + { + fd_callback_ = std::move(fd_callback); + } + +private: + const ServerPipe::ClientId id_; + Logger& logger_; + SocketBase::State state_; + libcyphal::IExecutor::Callback::Any fd_callback_; + +}; // ClientContext + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_PIPE_CLIENT_CONTEXT_HPP_INCLUDED diff --git a/src/common/ipc/pipe/net_socket_client.cpp b/src/common/ipc/pipe/net_socket_client.cpp new file mode 100644 index 0000000..c340b0a --- /dev/null +++ b/src/common/ipc/pipe/net_socket_client.cpp @@ -0,0 +1,168 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "net_socket_client.hpp" + +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_utils.hpp" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +NetSocketClient::NetSocketClient(libcyphal::IExecutor& executor, std::string server_ip, const int server_port) + : server_ip_{std::move(server_ip)} + , server_port_{server_port} + , posix_executor_ext_{cetl::rtti_cast(&executor)} +{ + CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); +} + +NetSocketClient::~NetSocketClient() +{ + if (state_.fd != -1) + { + platform::posixSyscallError([this] { + // + return ::close(state_.fd); + }); + } +} + +int NetSocketClient::start(EventHandler event_handler) +{ + CETL_DEBUG_ASSERT(event_handler, ""); + CETL_DEBUG_ASSERT(state_.fd == -1, ""); + + event_handler_ = std::move(event_handler); + + if (const auto err = platform::posixSyscallError([this] { + // + return state_.fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); + })) + { + logger().error("Failed to create socket: {}.", std::strerror(err)); + return err; + } + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_port = htons(server_port_); + if (inet_pton(AF_INET, server_ip_.c_str(), &addr.sin_addr) <= 0) + { + logger().error("Invalid server IP address: {}.", server_ip_); + return EINVAL; + } + + if (const auto err = platform::posixSyscallError([this, &addr] { + // + return ::connect(state_.fd, + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + reinterpret_cast(&addr), + sizeof(addr)); + })) + { + if (err != EINPROGRESS) + { + logger().error("Failed to connect to server: {}.", std::strerror(err)); + return err; + } + } + + socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // + [this](const auto&) { + // + handle_connect(); + }, + platform::IPosixExecutorExtension::Trigger::Writable{state_.fd}); + + return 0; +} + +void NetSocketClient::handle_connect() +{ + socket_callback_.reset(); + + int so_error = 0; + if (const auto err = platform::posixSyscallError([this, &so_error] { + // + socklen_t len = sizeof(so_error); + return ::getsockopt(state_.fd, SOL_SOCKET, SO_ERROR, &so_error, &len); // NOLINT(misc-include-cleaner) + })) + { + logger().warn("Failed to query socket error: {}.", std::strerror(err)); + so_error = err; + } + if (so_error != 0) + { + logger().error("Failed to connect to server: {}.", std::strerror(so_error)); + handle_disconnect(); + return; + } + + socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // + [this](const auto&) { + // + handle_receive(); + }, + platform::IPosixExecutorExtension::Trigger::Readable{state_.fd}); + + state_.read_phase = State::ReadPhase::Header; + event_handler_(Event::Connected{}); +} + +void NetSocketClient::handle_receive() +{ + if (const auto err = receiveMessage(state_, [this](const auto payload) { + // + return event_handler_(Event::Message{payload}); + })) + { + if (err == -1) + { + logger().debug("End of server stream - closing connection."); + } + else + { + logger().warn("Failed to handle server response - closing connection: {}.", std::strerror(err)); + } + + handle_disconnect(); + } +} + +void NetSocketClient::handle_disconnect() +{ + socket_callback_.reset(); + + ::close(state_.fd); + state_.fd = -1; + state_.read_phase = State::ReadPhase::Header; + + event_handler_(Event::Disconnected{}); +} + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd diff --git a/src/common/ipc/pipe/net_socket_client.hpp b/src/common/ipc/pipe/net_socket_client.hpp new file mode 100644 index 0000000..bbf7f92 --- /dev/null +++ b/src/common/ipc/pipe/net_socket_client.hpp @@ -0,0 +1,68 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_CLIENT_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_CLIENT_HPP_INCLUDED + +#include "client_pipe.hpp" +#include "ipc/ipc_types.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "socket_base.hpp" + +#include +#include + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class NetSocketClient final : public SocketBase, public ClientPipe +{ +public: + NetSocketClient(libcyphal::IExecutor& executor, std::string server_ip, const int server_port); + + NetSocketClient(const NetSocketClient&) = delete; + NetSocketClient(NetSocketClient&&) noexcept = delete; + NetSocketClient& operator=(const NetSocketClient&) = delete; + NetSocketClient& operator=(NetSocketClient&&) noexcept = delete; + + ~NetSocketClient() override; + + // ClientPipe + + CETL_NODISCARD int start(EventHandler event_handler) override; + + CETL_NODISCARD int send(const Payloads payloads) override + { + return SocketBase::send(state_, payloads); + } + +private: + void handle_connect(); + void handle_receive(); + void handle_disconnect(); + + const std::string server_ip_; + const int server_port_; + platform::IPosixExecutorExtension* const posix_executor_ext_; + State state_; + libcyphal::IExecutor::Callback::Any socket_callback_; + EventHandler event_handler_; + +}; // NetSocketClient + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_CLIENT_HPP_INCLUDED diff --git a/src/common/ipc/pipe/net_socket_server.cpp b/src/common/ipc/pipe/net_socket_server.cpp new file mode 100644 index 0000000..7c83663 --- /dev/null +++ b/src/common/ipc/pipe/net_socket_server.cpp @@ -0,0 +1,68 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "net_socket_server.hpp" + +#include "logging.hpp" +#include "ocvsmd/platform/posix_utils.hpp" + +#include +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +NetSocketServer::NetSocketServer(libcyphal::IExecutor& executor, const int server_port) + : Base{executor} + , server_port_{server_port} +{ +} + +int NetSocketServer::makeSocketHandle(int& out_fd) +{ + CETL_DEBUG_ASSERT(out_fd == -1, ""); + + if (const auto err = platform::posixSyscallError([&out_fd] { + // + return out_fd = ::socket(AF_INET, SOCK_STREAM, 0); + })) + { + logger().error("Failed to create server socket: {}.", std::strerror(err)); + return err; + } + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = INADDR_ANY; + addr.sin_port = htons(server_port_); + + if (const auto err = platform::posixSyscallError([&out_fd, &addr] { + // + return ::bind(out_fd, + reinterpret_cast(&addr), // NOLINT(*-reinterpret-cast) + sizeof(addr)); + })) + { + logger().error("Failed to bind server socket: {}.", std::strerror(err)); + return err; + } + + return 0; +} + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd diff --git a/src/common/ipc/pipe/net_socket_server.hpp b/src/common/ipc/pipe/net_socket_server.hpp new file mode 100644 index 0000000..1c1f2fa --- /dev/null +++ b/src/common/ipc/pipe/net_socket_server.hpp @@ -0,0 +1,49 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_SERVER_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_SERVER_HPP_INCLUDED + +#include "socket_server_base.hpp" + +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class NetSocketServer final : public SocketServerBase +{ +public: + NetSocketServer(libcyphal::IExecutor& executor, const int server_port); + + NetSocketServer(const NetSocketServer&) = delete; + NetSocketServer(NetSocketServer&&) noexcept = delete; + NetSocketServer& operator=(const NetSocketServer&) = delete; + NetSocketServer& operator=(NetSocketServer&&) noexcept = delete; + + ~NetSocketServer() override = default; + +private: + using Base = SocketServerBase; + + CETL_NODISCARD int makeSocketHandle(int& out_fd) override; + + const int server_port_; + +}; // NetSocketServer + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_SERVER_HPP_INCLUDED diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp new file mode 100644 index 0000000..54f6083 --- /dev/null +++ b/src/common/ipc/pipe/socket_base.cpp @@ -0,0 +1,167 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "socket_base.hpp" + +#include "ipc/ipc_types.hpp" +#include "ocvsmd/platform/posix_utils.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ +namespace +{ + +struct MsgHeader final +{ + std::uint32_t signature; + std::uint32_t size; +}; + +constexpr std::size_t MsgSmallPayloadSize = 256; +constexpr std::uint32_t MsgSignature = 0x5356434F; // 'OCVS' +constexpr std::size_t MsgMaxSize = 1ULL << 20ULL; // 1 MB + +} // namespace + +int SocketBase::send(const State& state, const Payloads payloads) +{ + // 1. Write the message header (signature and total size of the following fragments). + // + const std::size_t total_size = std::accumulate( // NOLINT + payloads.begin(), + payloads.end(), + 0ULL, + [](const std::size_t acc, const Payload payload) { + // + return acc + payload.size(); + }); + if (const int err = platform::posixSyscallError([total_size, &state] { + // + const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; + return ::write(state.fd, &msg_header, sizeof(msg_header)); + })) + { + return err; + } + + // 2. Write the message payload fragments. + // + for (const auto payload : payloads) + { + if (const int err = platform::posixSyscallError([payload, &state] { + // + return ::write(state.fd, payload.data(), payload.size()); + })) + { + return err; + } + } + return 0; +} + +int SocketBase::receiveMessage(State& state, std::function&& action) const +{ + // 1. Receive and validate the message header. + // + if (state.read_phase == State::ReadPhase::Header) + { + MsgHeader msg_header{}; + ssize_t bytes_read = 0; + if (const auto err = platform::posixSyscallError([&state, &msg_header, &bytes_read] { + // + return bytes_read = ::read(state.fd, &msg_header, sizeof(msg_header)); + })) + { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + { + return 0; // no data available yet + } + logger_->error("Failed to read message header (fd={}): {}.", state.fd, std::strerror(err)); + return err; + } + + if (bytes_read == 0) + { + return -1; // EOF + } + + if ((bytes_read != sizeof(msg_header)) || (msg_header.signature != MsgSignature) // + || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) + { + return EINVAL; + } + + state.read_msg_size = msg_header.size; + state.read_phase = State::ReadPhase::Payload; + } + + // 2. Read message payload. + // + if (state.read_phase == State::ReadPhase::Payload) + { + auto read_and_act = [this, &state, act = std::move(action)]( // + const cetl::span buf_span) { + // + ssize_t read = 0; + if (const auto err = platform::posixSyscallError([this, &state, buf_span, &read] { + // + return read = ::read(state.fd, buf_span.data(), buf_span.size()); + })) + { + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + { + return 0; // no data available + } + logger_->error("Failed to read message payload (fd={}): {}.", state.fd, std::strerror(err)); + return err; + } + if (read != buf_span.size()) + { + return EINVAL; + } + + state.read_phase = State::ReadPhase::Header; + + const cetl::span const_buf_span{buf_span}; + return act(const_buf_span); + }; + if (state.read_msg_size <= MsgSmallPayloadSize) // on stack buffer? + { + std::array buffer; // NOLINT(*-member-init) + return read_and_act({buffer.data(), state.read_msg_size}); + } + + // NOLINTNEXTLINE(*-avoid-c-arrays) + const std::unique_ptr buffer{new std::uint8_t[state.read_msg_size]}; + return read_and_act({buffer.get(), state.read_msg_size}); + } + + return 0; +} + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd diff --git a/src/common/ipc/pipe/socket_base.hpp b/src/common/ipc/pipe/socket_base.hpp new file mode 100644 index 0000000..e731e72 --- /dev/null +++ b/src/common/ipc/pipe/socket_base.hpp @@ -0,0 +1,73 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_BASE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_SOCKET_BASE_HPP_INCLUDED + +#include "ipc/ipc_types.hpp" +#include "logging.hpp" + +#include + +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class SocketBase +{ +public: + struct State final + { + enum class ReadPhase : std::uint8_t + { + Header, + Payload + }; + + int fd{-1}; + std::size_t read_msg_size{0}; + ReadPhase read_phase{ReadPhase::Header}; + + }; // State + + SocketBase(const SocketBase&) = delete; + SocketBase(SocketBase&&) noexcept = delete; + SocketBase& operator=(const SocketBase&) = delete; + SocketBase& operator=(SocketBase&&) noexcept = delete; + +protected: + SocketBase() = default; + ~SocketBase() = default; + + Logger& logger() const noexcept + { + return *logger_; + } + + CETL_NODISCARD static int send(const State& state, const Payloads payloads); + + CETL_NODISCARD int receiveMessage(State& state, std::function&& action) const; + +private: + LoggerPtr logger_{getLogger("ipc")}; + +}; // SocketBase + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_PIPE_SOCKET_BASE_HPP_INCLUDED diff --git a/src/common/ipc/pipe/socket_server_base.cpp b/src/common/ipc/pipe/socket_server_base.cpp new file mode 100644 index 0000000..f094d86 --- /dev/null +++ b/src/common/ipc/pipe/socket_server_base.cpp @@ -0,0 +1,174 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "socket_server_base.hpp" + +#include "client_context.hpp" +#include "ipc/ipc_types.hpp" +#include "logging.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_utils.hpp" +#include "socket_base.hpp" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ +namespace +{ + +constexpr int MaxConnections = 5; + +} // namespace + +SocketServerBase::SocketServerBase(libcyphal::IExecutor& executor) + : server_fd_{-1} + , posix_executor_ext_{cetl::rtti_cast(&executor)} + , unique_client_id_counter_{0} +{ + CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); +} + +SocketServerBase::~SocketServerBase() +{ + if (server_fd_ != -1) + { + platform::posixSyscallError([this] { + // + return ::close(server_fd_); + }); + } +} + +int SocketServerBase::start(EventHandler event_handler) +{ + CETL_DEBUG_ASSERT(server_fd_ == -1, ""); + CETL_DEBUG_ASSERT(event_handler, ""); + + event_handler_ = std::move(event_handler); + + if (const auto err = makeSocketHandle(server_fd_)) + { + logger().error("Failed to make socket handle: {}.", std::strerror(err)); + return err; + } + + if (const auto err = platform::posixSyscallError([this] { + // + return ::listen(server_fd_, MaxConnections); + })) + { + logger().error("Failed to listen on server socket: {}.", std::strerror(err)); + return err; + } + + accept_callback_ = posix_executor_ext_->registerAwaitableCallback( // + [this](const auto&) { + // + handleAccept(); + }, + platform::IPosixExecutorExtension::Trigger::Readable{server_fd_}); + + return 0; +} + +int SocketServerBase::send(const ClientId client_id, const Payloads payloads) +{ + if (auto* const client_context = tryFindClientContext(client_id)) + { + return SocketBase::send(client_context->state(), payloads); + } + + logger().warn("Client context is not found (id={}).", client_id); + return EINVAL; +} + +void SocketServerBase::handleAccept() +{ + CETL_DEBUG_ASSERT(server_fd_ != -1, ""); + + int client_fd = -1; + if (const auto err = platform::posixSyscallError([this, &client_fd] { + // + return client_fd = ::accept(server_fd_, nullptr, nullptr); + })) + { + logger().warn("Failed to accept client connection: {}.", std::strerror(err)); + return; + } + CETL_DEBUG_ASSERT(client_fd != -1, ""); + + const ClientId new_client_id = ++unique_client_id_counter_; + auto client_context = std::make_unique(new_client_id, client_fd, logger()); + // + client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( + [this, new_client_id](const auto&) { + // + handleClientRequest(new_client_id); + }, + platform::IPosixExecutorExtension::Trigger::Readable{client_fd})); + + client_id_to_context_.emplace(new_client_id, std::move(client_context)); + + event_handler_(Event::Connected{new_client_id}); +} + +void SocketServerBase::handleClientRequest(const ClientId client_id) +{ + auto* const client_context = tryFindClientContext(client_id); + CETL_DEBUG_ASSERT(client_context, ""); + auto& state = client_context->state(); + + if (const auto err = receiveMessage(state, [this, client_id](const auto payload) { + // + return event_handler_(Event::Message{client_id, payload}); + })) + { + if (err == -1) + { + logger().debug("End of client stream - closing connection (id={}, fd={}).", client_id, state.fd); + } + else + { + logger().warn("Failed to handle client request - closing connection (id={}, fd={}): {}.", + client_id, + state.fd, + std::strerror(err)); + } + + client_id_to_context_.erase(client_id); + event_handler_(Event::Disconnected{client_id}); + } +} + +ClientContext* SocketServerBase::tryFindClientContext(const ClientId client_id) +{ + const auto id_and_context = client_id_to_context_.find(client_id); + if (id_and_context != client_id_to_context_.end()) + { + return id_and_context->second.get(); + } + return nullptr; +} + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd diff --git a/src/common/ipc/pipe/socket_server_base.hpp b/src/common/ipc/pipe/socket_server_base.hpp new file mode 100644 index 0000000..976666c --- /dev/null +++ b/src/common/ipc/pipe/socket_server_base.hpp @@ -0,0 +1,67 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_BASE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_BASE_HPP_INCLUDED + +#include "client_context.hpp" +#include "ipc/ipc_types.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "server_pipe.hpp" +#include "socket_base.hpp" + +#include +#include + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class SocketServerBase : public SocketBase, public ServerPipe +{ +public: + SocketServerBase(const SocketServerBase&) = delete; + SocketServerBase(SocketServerBase&&) noexcept = delete; + SocketServerBase& operator=(const SocketServerBase&) = delete; + SocketServerBase& operator=(SocketServerBase&&) noexcept = delete; + +protected: + explicit SocketServerBase(libcyphal::IExecutor& executor); + ~SocketServerBase() override; + + // ServerPipe + // + CETL_NODISCARD int start(EventHandler event_handler) override; + CETL_NODISCARD int send(const ClientId client_id, const Payloads payloads) override; + + CETL_NODISCARD virtual int makeSocketHandle(int& out_fd) = 0; + +private: + void handleAccept(); + void handleClientRequest(const ClientId client_id); + ClientContext* tryFindClientContext(const ClientId client_id); + + int server_fd_; + platform::IPosixExecutorExtension* const posix_executor_ext_; + ClientId unique_client_id_counter_; + EventHandler event_handler_; + libcyphal::IExecutor::Callback::Any accept_callback_; + std::unordered_map client_id_to_context_; + +}; // SocketServerBase + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_BASE_HPP_INCLUDED diff --git a/src/common/ipc/pipe/unix_socket_base.hpp b/src/common/ipc/pipe/unix_socket_base.hpp deleted file mode 100644 index 9398581..0000000 --- a/src/common/ipc/pipe/unix_socket_base.hpp +++ /dev/null @@ -1,170 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED - -#include "ipc/ipc_types.hpp" -#include "logging.hpp" -#include "ocvsmd/platform/posix_utils.hpp" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -class UnixSocketBase -{ -public: - UnixSocketBase(const UnixSocketBase&) = delete; - UnixSocketBase(UnixSocketBase&&) noexcept = delete; - UnixSocketBase& operator=(const UnixSocketBase&) = delete; - UnixSocketBase& operator=(UnixSocketBase&&) noexcept = delete; - -protected: - UnixSocketBase() = default; - ~UnixSocketBase() = default; - - Logger& logger() const noexcept - { - return *logger_; - } - - CETL_NODISCARD static int send(const int output_fd, const Payloads payloads) - { - // 1. Write the message header (signature and total size of the following fragments). - // - const std::size_t total_size = std::accumulate( // - payloads.begin(), - payloads.end(), - 0ULL, - [](const std::size_t acc, const Payload payload) { - // - return acc + payload.size(); - }); - if (const int err = platform::posixSyscallError([output_fd, total_size] { - // - const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; - return ::write(output_fd, &msg_header, sizeof(msg_header)); - })) - { - return err; - } - - // 2. Write the message payload fragments. - // - for (const auto payload : payloads) - { - if (const int err = platform::posixSyscallError([output_fd, payload] { - // - return ::write(output_fd, payload.data(), payload.size()); - })) - { - return err; - } - } - return 0; - } - - template - CETL_NODISCARD int receiveMessage(const int input_fd, Action&& action) - { - // 1. Receive and validate the message header. - // - std::size_t msg_size = 0; - { - MsgHeader msg_header; - ssize_t bytes_read = 0; - if (const auto err = platform::posixSyscallError([input_fd, &msg_header, &bytes_read] { - // - return bytes_read = ::read(input_fd, &msg_header, sizeof(msg_header)); - })) - { - logger_->error("Failed to read message header (fd={}): {}.", input_fd, std::strerror(err)); - return err; - } - - if (bytes_read == 0) - { - return -1; // EOF - } - - if ((bytes_read != sizeof(msg_header)) || (msg_header.signature != MsgSignature) // - || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) - { - return EINVAL; - } - - msg_size = msg_header.size; - } - - // 2. Read message payload. - // - auto read_and_act = [this, input_fd, act = std::forward(action)]( // - const cetl::span buf_span) { - // - ssize_t read = 0; - if (const auto err = platform::posixSyscallError([this, input_fd, buf_span, &read] { - // - return read = ::read(input_fd, buf_span.data(), buf_span.size()); - })) - { - logger_->error("Failed to read message payload (fd={}): {}.", input_fd, std::strerror(err)); - return err; - } - if (read != buf_span.size()) - { - return EINVAL; - } - - const cetl::span const_buf_span{buf_span}; - return act(const_buf_span); - }; - if (msg_size <= MsgSmallPayloadSize) // on stack buffer? - { - std::array buffer; - return read_and_act({buffer.data(), msg_size}); - } - const std::unique_ptr buffer{new std::uint8_t[msg_size]}; - return read_and_act({buffer.get(), msg_size}); - } - -private: - struct MsgHeader final - { - std::uint32_t signature; - std::uint32_t size; - }; - - static constexpr std::size_t MsgSmallPayloadSize = 256; - static constexpr std::uint32_t MsgSignature = 0x5356434F; // 'OCVS' - static constexpr std::size_t MsgMaxSize = 1ULL << 20ULL; // 1 MB - - LoggerPtr logger_{getLogger("ipc")}; - -}; // UnixSocketBase - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd - -#endif // OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_BASE_HPP_INCLUDED diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp index ea34f42..6455c44 100644 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -32,7 +32,6 @@ namespace pipe UnixSocketClient::UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path) : socket_path_{std::move(socket_path)} - , client_fd_{-1} , posix_executor_ext_{cetl::rtti_cast(&executor)} { CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); @@ -40,25 +39,25 @@ UnixSocketClient::UnixSocketClient(libcyphal::IExecutor& executor, std::string s UnixSocketClient::~UnixSocketClient() { - if (client_fd_ != -1) + if (state_.fd != -1) { platform::posixSyscallError([this] { // - return ::close(client_fd_); + return ::close(state_.fd); }); } } -CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) +int UnixSocketClient::start(EventHandler event_handler) { - CETL_DEBUG_ASSERT(client_fd_ == -1, ""); CETL_DEBUG_ASSERT(event_handler, ""); + CETL_DEBUG_ASSERT(state_.fd == -1, ""); event_handler_ = std::move(event_handler); if (const auto err = platform::posixSyscallError([this] { // - return client_fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); + return state_.fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); })) { logger().error("Failed to create socket: {}.", std::strerror(err)); @@ -76,7 +75,7 @@ CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) if (const auto err = platform::posixSyscallError([this, &addr, &abstract_socket_path] { // - return ::connect(client_fd_, + return ::connect(state_.fd, // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) reinterpret_cast(&addr), offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); @@ -91,7 +90,7 @@ CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) // handle_socket(); }, - platform::IPosixExecutorExtension::Trigger::Readable{client_fd_}); + platform::IPosixExecutorExtension::Trigger::Readable{state_.fd}); event_handler_(Event::Connected{}); return 0; @@ -99,7 +98,7 @@ CETL_NODISCARD int UnixSocketClient::start(EventHandler event_handler) void UnixSocketClient::handle_socket() { - if (const auto err = receiveMessage(client_fd_, [this](const auto payload) { + if (const auto err = receiveMessage(state_, [this](const auto payload) { // return event_handler_(Event::Message{payload}); })) @@ -114,8 +113,8 @@ void UnixSocketClient::handle_socket() } socket_callback_.reset(); - ::close(client_fd_); - client_fd_ = -1; + ::close(state_.fd); + state_.fd = -1; event_handler_(Event::Disconnected{}); } diff --git a/src/common/ipc/pipe/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp index 26f1e27..3ed37fd 100644 --- a/src/common/ipc/pipe/unix_socket_client.hpp +++ b/src/common/ipc/pipe/unix_socket_client.hpp @@ -9,7 +9,7 @@ #include "client_pipe.hpp" #include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" -#include "unix_socket_base.hpp" +#include "socket_base.hpp" #include #include @@ -25,7 +25,7 @@ namespace ipc namespace pipe { -class UnixSocketClient final : public UnixSocketBase, public ClientPipe +class UnixSocketClient final : public SocketBase, public ClientPipe { public: UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path); @@ -43,15 +43,15 @@ class UnixSocketClient final : public UnixSocketBase, public ClientPipe CETL_NODISCARD int send(const Payloads payloads) override { - return UnixSocketBase::send(client_fd_, payloads); + return SocketBase::send(state_, payloads); } private: void handle_socket(); - std::string socket_path_; - int client_fd_; + const std::string socket_path_; platform::IPosixExecutorExtension* const posix_executor_ext_; + State state_; libcyphal::IExecutor::Callback::Any socket_callback_; EventHandler event_handler_; diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index 946220b..7080131 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -6,21 +6,17 @@ #include "unix_socket_server.hpp" #include "logging.hpp" -#include "ocvsmd/platform/posix_executor_extension.hpp" #include "ocvsmd/platform/posix_utils.hpp" #include -#include #include #include #include #include -#include #include #include #include -#include #include namespace ocvsmd @@ -31,84 +27,20 @@ namespace ipc { namespace pipe { -namespace -{ - -constexpr int MaxConnections = 5; - -class ClientContextImpl final : public detail::ClientContext -{ -public: - ClientContextImpl(const UnixSocketServer::ClientId id, const int fd, Logger& logger) - : id_{id} - , fd_{fd} - , logger_{logger} - { - CETL_DEBUG_ASSERT(fd_ != -1, ""); - - logger_.trace("ClientContextImpl(fd={}, id={}).", fd_, id_); - } - - ~ClientContextImpl() override - { - logger_.trace("~ClientContextImpl(fd={}, id={}).", fd_, id_); - - platform::posixSyscallError([this] { - // - return ::close(fd_); - }); - } - - ClientContextImpl(const ClientContextImpl&) = delete; - ClientContextImpl(ClientContextImpl&&) noexcept = delete; - ClientContextImpl& operator=(const ClientContextImpl&) = delete; - ClientContextImpl& operator=(ClientContextImpl&&) noexcept = delete; - - void setCallback(libcyphal::IExecutor::Callback::Any&& fd_callback) - { - fd_callback_ = std::move(fd_callback); - } - -private: - const UnixSocketServer::ClientId id_; - const int fd_; - Logger& logger_; - libcyphal::IExecutor::Callback::Any fd_callback_; - -}; // ClientContextImpl - -} // namespace UnixSocketServer::UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path) - : socket_path_{std::move(socket_path)} - , server_fd_{-1} - , posix_executor_ext_{cetl::rtti_cast(&executor)} - , unique_client_id_counter_{0} -{ - CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); -} - -UnixSocketServer::~UnixSocketServer() + : Base{executor} + , socket_path_{std::move(socket_path)} { - if (server_fd_ != -1) - { - platform::posixSyscallError([this] { - // - return ::close(server_fd_); - }); - } } -CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) +int UnixSocketServer::makeSocketHandle(int& out_fd) { - CETL_DEBUG_ASSERT(server_fd_ == -1, ""); - CETL_DEBUG_ASSERT(event_handler, ""); + CETL_DEBUG_ASSERT(out_fd == -1, ""); - event_handler_ = std::move(event_handler); - - if (const auto err = platform::posixSyscallError([this] { + if (const auto err = platform::posixSyscallError([&out_fd] { // - return server_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); + return out_fd = ::socket(AF_UNIX, SOCK_STREAM, 0); })) { logger().error("Failed to create server socket: {}.", std::strerror(err)); @@ -124,11 +56,10 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) abstract_socket_path.c_str(), std::min(sizeof(addr.sun_path), abstract_socket_path.size())); - if (const auto err = platform::posixSyscallError([this, &addr, &abstract_socket_path] { + if (const auto err = platform::posixSyscallError([&out_fd, &addr, &abstract_socket_path] { // - return ::bind(server_fd_, - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(&addr), + return ::bind(out_fd, + reinterpret_cast(&addr), // NOLINT(*-reinterpret-cast) offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); })) { @@ -136,84 +67,9 @@ CETL_NODISCARD int UnixSocketServer::start(EventHandler event_handler) return err; } - if (const auto err = platform::posixSyscallError([this] { - // - return ::listen(server_fd_, MaxConnections); - })) - { - logger().error("Failed to listen on server socket: {}.", std::strerror(err)); - return err; - } - - accept_callback_ = posix_executor_ext_->registerAwaitableCallback( // - [this](const auto&) { - // - handleAccept(); - }, - platform::IPosixExecutorExtension::Trigger::Readable{server_fd_}); - return 0; } -void UnixSocketServer::handleAccept() -{ - CETL_DEBUG_ASSERT(server_fd_ != -1, ""); - - int client_fd = -1; - if (const auto err = platform::posixSyscallError([this, &client_fd] { - // - return client_fd = ::accept(server_fd_, nullptr, nullptr); - })) - { - logger().warn("Failed to accept client connection: {}.", std::strerror(err)); - return; - } - - CETL_DEBUG_ASSERT(client_fd != -1, ""); - CETL_DEBUG_ASSERT(client_fd_to_context_.find(client_fd) == client_fd_to_context_.end(), ""); - - const ClientId new_client_id = ++unique_client_id_counter_; - auto client_context = std::make_unique(new_client_id, client_fd, logger()); - // - client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( - [this, new_client_id, client_fd](const auto&) { - // - handleClientRequest(new_client_id, client_fd); - }, - platform::IPosixExecutorExtension::Trigger::Readable{client_fd})); - - client_id_to_fd_[new_client_id] = client_fd; - client_fd_to_context_.emplace(client_fd, std::move(client_context)); - - event_handler_(Event::Connected{new_client_id}); -} - -void UnixSocketServer::handleClientRequest(const ClientId client_id, const int client_fd) -{ - if (const auto err = receiveMessage(client_fd, [this, client_id](const auto payload) { - // - return event_handler_(Event::Message{client_id, payload}); - })) - { - if (err == -1) - { - logger().debug("End of client stream - closing connection (id={}, fd={}).", client_id, client_fd); - } - else - { - logger().warn("Failed to handle client request - closing connection (id={}, fd={}): {}.", - client_id, - client_fd, - std::strerror(err)); - } - - client_id_to_fd_.erase(client_id); - client_fd_to_context_.erase(client_fd); - - event_handler_(Event::Disconnected{client_id}); - } -} - } // namespace pipe } // namespace ipc } // namespace common diff --git a/src/common/ipc/pipe/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp index 57e4a4f..81965df 100644 --- a/src/common/ipc/pipe/unix_socket_server.hpp +++ b/src/common/ipc/pipe/unix_socket_server.hpp @@ -6,18 +6,12 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED -#include "ipc/ipc_types.hpp" -#include "ocvsmd/platform/posix_executor_extension.hpp" -#include "server_pipe.hpp" -#include "unix_socket_base.hpp" +#include "socket_server_base.hpp" #include #include -#include -#include #include -#include namespace ocvsmd { @@ -27,28 +21,8 @@ namespace ipc { namespace pipe { -namespace detail -{ - -class ClientContext -{ -public: - using Ptr = std::unique_ptr; - - ClientContext() = default; - - ClientContext(const ClientContext&) = delete; - ClientContext(ClientContext&&) noexcept = delete; - ClientContext& operator=(const ClientContext&) = delete; - ClientContext& operator=(ClientContext&&) noexcept = delete; - virtual ~ClientContext() = default; - -}; // ClientContext - -} // namespace detail - -class UnixSocketServer final : public UnixSocketBase, public ServerPipe +class UnixSocketServer final : public SocketServerBase { public: UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path); @@ -58,32 +32,14 @@ class UnixSocketServer final : public UnixSocketBase, public ServerPipe UnixSocketServer& operator=(UnixSocketServer&&) = delete; UnixSocketServer& operator=(const UnixSocketServer&) = delete; - ~UnixSocketServer() override; - - CETL_NODISCARD int start(EventHandler event_handler) override; - - CETL_NODISCARD int send(const ClientId client_id, const Payloads payloads) override - { - const auto id_and_fd = client_id_to_fd_.find(client_id); - if (id_and_fd == client_id_to_fd_.end()) - { - return EINVAL; - } - return UnixSocketBase::send(id_and_fd->second, payloads); - } + ~UnixSocketServer() override = default; private: - void handleAccept(); - void handleClientRequest(const ClientId client_id, const int client_fd); + using Base = SocketServerBase; + + CETL_NODISCARD int makeSocketHandle(int& out_fd) override; - const std::string socket_path_; - int server_fd_; - platform::IPosixExecutorExtension* const posix_executor_ext_; - ClientId unique_client_id_counter_; - EventHandler event_handler_; - std::unordered_map client_fd_to_context_; - std::unordered_map client_id_to_fd_; - libcyphal::IExecutor::Callback::Any accept_callback_; + const std::string socket_path_; }; // UnixSocketServer diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index 6d3d09a..7ce4d33 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -8,9 +8,9 @@ #include "channel.hpp" #include "gateway.hpp" -#include "ipc_types.hpp" #include "pipe/server_pipe.hpp" +#include #include #include diff --git a/src/common/logging.hpp b/src/common/logging.hpp index d525660..409ff30 100644 --- a/src/common/logging.hpp +++ b/src/common/logging.hpp @@ -9,6 +9,7 @@ #include "common_helpers.hpp" #include +#include #include #include diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/application.cpp index a8ca25a..a4de518 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/application.cpp @@ -5,7 +5,8 @@ #include "application.hpp" -#include "ipc/pipe/unix_socket_server.hpp" +#include "ipc/pipe/net_socket_server.hpp" +// #include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" #include "svc/node/exec_cmd_service.hpp" #include "svc/svc_helpers.hpp" @@ -65,10 +66,12 @@ cetl::optional Application::init() .setSoftwareVcsRevisionId(VCS_REVISION_ID) .setUniqueId(getUniqueId()); - using ServerPipe = common::ipc::pipe::UnixSocketServer; + // using ServerPipe = common::ipc::pipe::UnixSocketServer; + // auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); + using ServerPipe = common::ipc::pipe::NetSocketServer; + auto server_pipe = std::make_unique(executor_, 9875); // NOLINT(*-magic-numbers) - auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); - ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); + ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); const svc::ScvContext svc_context{memory_, executor_, *ipc_router_, *presentation_}; svc::node::ExecCmdService::registerWithContext(svc_context); diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 4969f28..21d9b0b 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -7,7 +7,8 @@ #include "ipc/channel.hpp" #include "ipc/client_router.hpp" -#include "ipc/pipe/unix_socket_client.hpp" +#include "ipc/pipe/net_socket_client.hpp" +// #include "ipc/pipe/unix_socket_client.hpp" #include "logging.hpp" #include "ocvsmd/sdk/node_command_client.hpp" #include "sdk_factory.hpp" @@ -34,10 +35,12 @@ class DaemonImpl final : public Daemon : memory_{memory} , logger_{common::getLogger("sdk")} { - using ClientPipe = common::ipc::pipe::UnixSocketClient; + // using ClientPipe = common::ipc::pipe::UnixSocketClient; + // auto client_pipe = std::make_unique(executor, "/var/run/ocvsmd/local.sock"); + using ClientPipe = common::ipc::pipe::NetSocketClient; + auto client_pipe = std::make_unique(executor, "127.0.0.1", 9875); // NOLINT(*-magic-numbers) - auto client_pipe = std::make_unique(executor, "/var/run/ocvsmd/local.sock"); - ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); + ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); node_command_client_ = Factory::makeNodeCommandClient(memory, ipc_router_); } diff --git a/submodules/libcyphal b/submodules/libcyphal index f66b8a6..ff44f7a 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit f66b8a676dd6a78a79a8051e6db0c9d9e954d8a2 +Subproject commit ff44f7aa70f2def5f51263dfe978427fdc7da09b From a00af48e914deab91d58a6aeba0ffcbb1e550cbb Mon Sep 17 00:00:00 2001 From: Sergei Date: Sun, 26 Jan 2025 02:18:22 +0200 Subject: [PATCH 076/156] reduced code duplication between net & unix socket client/server --- src/cli/main.cpp | 4 +- src/common/CMakeLists.txt | 1 + src/common/ipc/pipe/net_socket_client.cpp | 117 +-------------- src/common/ipc/pipe/net_socket_client.hpp | 31 ++-- src/common/ipc/pipe/net_socket_server.cpp | 13 +- src/common/ipc/pipe/socket_client_base.cpp | 160 +++++++++++++++++++++ src/common/ipc/pipe/socket_client_base.hpp | 66 +++++++++ src/common/ipc/pipe/socket_server_base.cpp | 13 ++ src/common/ipc/pipe/socket_server_base.hpp | 6 +- src/common/ipc/pipe/unix_socket_client.cpp | 76 +--------- src/common/ipc/pipe/unix_socket_client.hpp | 27 +--- src/common/ipc/pipe/unix_socket_server.cpp | 13 +- 12 files changed, 278 insertions(+), 249 deletions(-) create mode 100644 src/common/ipc/pipe/socket_client_base.cpp create mode 100644 src/common/ipc/pipe/socket_client_base.hpp diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 77d87a2..3599925 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -84,8 +84,8 @@ int main(const int argc, const char** const argv) auto node_cmd_client = daemon->getNodeCommandClient(); - constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; - constexpr std::array node_ids = {42, 43}; + constexpr auto cmd_id = Command::NodeRequest::COMMAND_RESTART; + constexpr std::array node_ids = {42, 43, 44}; const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 58f9396..336e24b 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -24,6 +24,7 @@ add_library(ocvsmd_common ipc/pipe/net_socket_client.cpp ipc/pipe/net_socket_server.cpp ipc/pipe/socket_base.cpp + ipc/pipe/socket_client_base.cpp ipc/pipe/socket_server_base.cpp ipc/pipe/unix_socket_client.cpp ipc/pipe/unix_socket_server.cpp diff --git a/src/common/ipc/pipe/net_socket_client.cpp b/src/common/ipc/pipe/net_socket_client.cpp index c340b0a..16e8cec 100644 --- a/src/common/ipc/pipe/net_socket_client.cpp +++ b/src/common/ipc/pipe/net_socket_client.cpp @@ -5,11 +5,9 @@ #include "net_socket_client.hpp" -#include "ocvsmd/platform/posix_executor_extension.hpp" #include "ocvsmd/platform/posix_utils.hpp" #include -#include #include #include @@ -18,7 +16,6 @@ #include #include #include -#include #include namespace ocvsmd @@ -31,34 +28,19 @@ namespace pipe { NetSocketClient::NetSocketClient(libcyphal::IExecutor& executor, std::string server_ip, const int server_port) - : server_ip_{std::move(server_ip)} + : Base{executor} + , server_ip_{std::move(server_ip)} , server_port_{server_port} - , posix_executor_ext_{cetl::rtti_cast(&executor)} { - CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); } -NetSocketClient::~NetSocketClient() +int NetSocketClient::makeSocketHandle(int& out_fd) { - if (state_.fd != -1) - { - platform::posixSyscallError([this] { - // - return ::close(state_.fd); - }); - } -} - -int NetSocketClient::start(EventHandler event_handler) -{ - CETL_DEBUG_ASSERT(event_handler, ""); - CETL_DEBUG_ASSERT(state_.fd == -1, ""); - - event_handler_ = std::move(event_handler); + CETL_DEBUG_ASSERT(out_fd == -1, ""); - if (const auto err = platform::posixSyscallError([this] { + if (const auto err = platform::posixSyscallError([&out_fd] { // - return state_.fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); + return out_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); })) { logger().error("Failed to create socket: {}.", std::strerror(err)); @@ -74,92 +56,7 @@ int NetSocketClient::start(EventHandler event_handler) return EINVAL; } - if (const auto err = platform::posixSyscallError([this, &addr] { - // - return ::connect(state_.fd, - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(&addr), - sizeof(addr)); - })) - { - if (err != EINPROGRESS) - { - logger().error("Failed to connect to server: {}.", std::strerror(err)); - return err; - } - } - - socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // - [this](const auto&) { - // - handle_connect(); - }, - platform::IPosixExecutorExtension::Trigger::Writable{state_.fd}); - - return 0; -} - -void NetSocketClient::handle_connect() -{ - socket_callback_.reset(); - - int so_error = 0; - if (const auto err = platform::posixSyscallError([this, &so_error] { - // - socklen_t len = sizeof(so_error); - return ::getsockopt(state_.fd, SOL_SOCKET, SO_ERROR, &so_error, &len); // NOLINT(misc-include-cleaner) - })) - { - logger().warn("Failed to query socket error: {}.", std::strerror(err)); - so_error = err; - } - if (so_error != 0) - { - logger().error("Failed to connect to server: {}.", std::strerror(so_error)); - handle_disconnect(); - return; - } - - socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // - [this](const auto&) { - // - handle_receive(); - }, - platform::IPosixExecutorExtension::Trigger::Readable{state_.fd}); - - state_.read_phase = State::ReadPhase::Header; - event_handler_(Event::Connected{}); -} - -void NetSocketClient::handle_receive() -{ - if (const auto err = receiveMessage(state_, [this](const auto payload) { - // - return event_handler_(Event::Message{payload}); - })) - { - if (err == -1) - { - logger().debug("End of server stream - closing connection."); - } - else - { - logger().warn("Failed to handle server response - closing connection: {}.", std::strerror(err)); - } - - handle_disconnect(); - } -} - -void NetSocketClient::handle_disconnect() -{ - socket_callback_.reset(); - - ::close(state_.fd); - state_.fd = -1; - state_.read_phase = State::ReadPhase::Header; - - event_handler_(Event::Disconnected{}); + return connectSocket(out_fd, &addr, sizeof(addr)); } } // namespace pipe diff --git a/src/common/ipc/pipe/net_socket_client.hpp b/src/common/ipc/pipe/net_socket_client.hpp index bbf7f92..0f7e91a 100644 --- a/src/common/ipc/pipe/net_socket_client.hpp +++ b/src/common/ipc/pipe/net_socket_client.hpp @@ -7,9 +7,7 @@ #define OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_CLIENT_HPP_INCLUDED #include "client_pipe.hpp" -#include "ipc/ipc_types.hpp" -#include "ocvsmd/platform/posix_executor_extension.hpp" -#include "socket_base.hpp" +#include "socket_client_base.hpp" #include #include @@ -25,7 +23,7 @@ namespace ipc namespace pipe { -class NetSocketClient final : public SocketBase, public ClientPipe +class NetSocketClient final : public SocketClientBase { public: NetSocketClient(libcyphal::IExecutor& executor, std::string server_ip, const int server_port); @@ -35,28 +33,15 @@ class NetSocketClient final : public SocketBase, public ClientPipe NetSocketClient& operator=(const NetSocketClient&) = delete; NetSocketClient& operator=(NetSocketClient&&) noexcept = delete; - ~NetSocketClient() override; - - // ClientPipe - - CETL_NODISCARD int start(EventHandler event_handler) override; - - CETL_NODISCARD int send(const Payloads payloads) override - { - return SocketBase::send(state_, payloads); - } + ~NetSocketClient() override = default; private: - void handle_connect(); - void handle_receive(); - void handle_disconnect(); + using Base = SocketClientBase; + + CETL_NODISCARD int makeSocketHandle(int& out_fd) override; - const std::string server_ip_; - const int server_port_; - platform::IPosixExecutorExtension* const posix_executor_ext_; - State state_; - libcyphal::IExecutor::Callback::Any socket_callback_; - EventHandler event_handler_; + const std::string server_ip_; + const int server_port_; }; // NetSocketClient diff --git a/src/common/ipc/pipe/net_socket_server.cpp b/src/common/ipc/pipe/net_socket_server.cpp index 7c83663..7509541 100644 --- a/src/common/ipc/pipe/net_socket_server.cpp +++ b/src/common/ipc/pipe/net_socket_server.cpp @@ -48,18 +48,7 @@ int NetSocketServer::makeSocketHandle(int& out_fd) addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(server_port_); - if (const auto err = platform::posixSyscallError([&out_fd, &addr] { - // - return ::bind(out_fd, - reinterpret_cast(&addr), // NOLINT(*-reinterpret-cast) - sizeof(addr)); - })) - { - logger().error("Failed to bind server socket: {}.", std::strerror(err)); - return err; - } - - return 0; + return bindSocket(out_fd, &addr, sizeof(addr)); } } // namespace pipe diff --git a/src/common/ipc/pipe/socket_client_base.cpp b/src/common/ipc/pipe/socket_client_base.cpp new file mode 100644 index 0000000..4b279bf --- /dev/null +++ b/src/common/ipc/pipe/socket_client_base.cpp @@ -0,0 +1,160 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "socket_client_base.hpp" + +#include "ipc/ipc_types.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_utils.hpp" +#include "socket_base.hpp" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +SocketClientBase::SocketClientBase(libcyphal::IExecutor& executor) + : posix_executor_ext_{cetl::rtti_cast(&executor)} +{ + CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); +} + +SocketClientBase::~SocketClientBase() +{ + if (state_.fd != -1) + { + platform::posixSyscallError([this] { + // + return ::close(state_.fd); + }); + } +} + +int SocketClientBase::start(EventHandler event_handler) +{ + CETL_DEBUG_ASSERT(event_handler, ""); + CETL_DEBUG_ASSERT(state_.fd == -1, ""); + + event_handler_ = std::move(event_handler); + + if (const auto err = makeSocketHandle(state_.fd)) + { + logger().error("Failed to make socket handle: {}.", std::strerror(err)); + return err; + } + + socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // + [this](const auto&) { + // + handle_connect(); + }, + platform::IPosixExecutorExtension::Trigger::Writable{state_.fd}); + + return 0; +} + +int SocketClientBase::send(const Payloads payloads) +{ + return SocketBase::send(state_, payloads); +} + +int SocketClientBase::connectSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const +{ + if (const auto err = platform::posixSyscallError([fd, addr_ptr, addr_size] { + // + return ::connect(fd, static_cast(addr_ptr), addr_size); + })) + { + if (err != EINPROGRESS) + { + logger().error("Failed to connect to server: {}.", std::strerror(err)); + return err; + } + } + return 0; +} + +void SocketClientBase::handle_connect() +{ + socket_callback_.reset(); + + int so_error = 0; + if (const auto err = platform::posixSyscallError([this, &so_error] { + // + socklen_t len = sizeof(so_error); + return ::getsockopt(state_.fd, SOL_SOCKET, SO_ERROR, &so_error, &len); // NOLINT(misc-include-cleaner) + })) + { + logger().warn("Failed to query socket error: {}.", std::strerror(err)); + so_error = err; + } + if (so_error != 0) + { + logger().error("Failed to connect to server: {}.", std::strerror(so_error)); + handle_disconnect(); + return; + } + + socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // + [this](const auto&) { + // + handle_receive(); + }, + platform::IPosixExecutorExtension::Trigger::Readable{state_.fd}); + + state_.read_phase = State::ReadPhase::Header; + event_handler_(Event::Connected{}); +} + +void SocketClientBase::handle_receive() +{ + if (const auto err = receiveMessage(state_, [this](const auto payload) { + // + return event_handler_(Event::Message{payload}); + })) + { + if (err == -1) + { + logger().debug("End of server stream - closing connection."); + } + else + { + logger().warn("Failed to handle server response - closing connection: {}.", std::strerror(err)); + } + + handle_disconnect(); + } +} + +void SocketClientBase::handle_disconnect() +{ + socket_callback_.reset(); + + ::close(state_.fd); + state_.fd = -1; + state_.read_phase = State::ReadPhase::Header; + + event_handler_(Event::Disconnected{}); +} + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd diff --git a/src/common/ipc/pipe/socket_client_base.hpp b/src/common/ipc/pipe/socket_client_base.hpp new file mode 100644 index 0000000..651e257 --- /dev/null +++ b/src/common/ipc/pipe/socket_client_base.hpp @@ -0,0 +1,66 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_BASE_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_BASE_HPP_INCLUDED + +#include "client_pipe.hpp" +#include "ipc/ipc_types.hpp" +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "socket_base.hpp" + +#include +#include + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace pipe +{ + +class SocketClientBase : public SocketBase, public ClientPipe +{ +public: + SocketClientBase(const SocketClientBase&) = delete; + SocketClientBase(SocketClientBase&&) noexcept = delete; + SocketClientBase& operator=(const SocketClientBase&) = delete; + SocketClientBase& operator=(SocketClientBase&&) noexcept = delete; + + ~SocketClientBase() override; + +protected: + explicit SocketClientBase(libcyphal::IExecutor& executor); + + // ClientPipe + CETL_NODISCARD int start(EventHandler event_handler) override; + CETL_NODISCARD int send(const Payloads payloads) override; + + CETL_NODISCARD virtual int makeSocketHandle(int& out_fd) = 0; + + CETL_NODISCARD int connectSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const; + +private: + void handle_connect(); + void handle_receive(); + void handle_disconnect(); + + platform::IPosixExecutorExtension* const posix_executor_ext_; + State state_; + libcyphal::IExecutor::Callback::Any socket_callback_; + EventHandler event_handler_; + +}; // SocketClientBase + +} // namespace pipe +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_BASE_HPP_INCLUDED diff --git a/src/common/ipc/pipe/socket_server_base.cpp b/src/common/ipc/pipe/socket_server_base.cpp index f094d86..2b6a784 100644 --- a/src/common/ipc/pipe/socket_server_base.cpp +++ b/src/common/ipc/pipe/socket_server_base.cpp @@ -100,6 +100,19 @@ int SocketServerBase::send(const ClientId client_id, const Payloads payloads) return EINVAL; } +int SocketServerBase::bindSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const +{ + if (const auto err = platform::posixSyscallError([fd, addr_ptr, addr_size] { + // + return ::bind(fd, static_cast(addr_ptr), addr_size); + })) + { + logger().error("Failed to bind server socket: {}.", std::strerror(err)); + return err; + } + return 0; +} + void SocketServerBase::handleAccept() { CETL_DEBUG_ASSERT(server_fd_ != -1, ""); diff --git a/src/common/ipc/pipe/socket_server_base.hpp b/src/common/ipc/pipe/socket_server_base.hpp index 976666c..e21e714 100644 --- a/src/common/ipc/pipe/socket_server_base.hpp +++ b/src/common/ipc/pipe/socket_server_base.hpp @@ -15,6 +15,7 @@ #include #include +#include #include namespace ocvsmd @@ -34,9 +35,10 @@ class SocketServerBase : public SocketBase, public ServerPipe SocketServerBase& operator=(const SocketServerBase&) = delete; SocketServerBase& operator=(SocketServerBase&&) noexcept = delete; + ~SocketServerBase() override; + protected: explicit SocketServerBase(libcyphal::IExecutor& executor); - ~SocketServerBase() override; // ServerPipe // @@ -45,6 +47,8 @@ class SocketServerBase : public SocketBase, public ServerPipe CETL_NODISCARD virtual int makeSocketHandle(int& out_fd) = 0; + CETL_NODISCARD int bindSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const; + private: void handleAccept(); void handleClientRequest(const ClientId client_id); diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp index 6455c44..5728b3d 100644 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ b/src/common/ipc/pipe/unix_socket_client.cpp @@ -5,11 +5,9 @@ #include "unix_socket_client.hpp" -#include "ocvsmd/platform/posix_executor_extension.hpp" #include "ocvsmd/platform/posix_utils.hpp" #include -#include #include #include @@ -18,7 +16,6 @@ #include #include #include -#include #include namespace ocvsmd @@ -31,33 +28,18 @@ namespace pipe { UnixSocketClient::UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path) - : socket_path_{std::move(socket_path)} - , posix_executor_ext_{cetl::rtti_cast(&executor)} + : Base{executor} + , socket_path_{std::move(socket_path)} { - CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); } -UnixSocketClient::~UnixSocketClient() +int UnixSocketClient::makeSocketHandle(int& out_fd) { - if (state_.fd != -1) - { - platform::posixSyscallError([this] { - // - return ::close(state_.fd); - }); - } -} - -int UnixSocketClient::start(EventHandler event_handler) -{ - CETL_DEBUG_ASSERT(event_handler, ""); - CETL_DEBUG_ASSERT(state_.fd == -1, ""); + CETL_DEBUG_ASSERT(out_fd == -1, ""); - event_handler_ = std::move(event_handler); - - if (const auto err = platform::posixSyscallError([this] { + if (const auto err = platform::posixSyscallError([&out_fd] { // - return state_.fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); + return out_fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); })) { logger().error("Failed to create socket: {}.", std::strerror(err)); @@ -73,51 +55,7 @@ int UnixSocketClient::start(EventHandler event_handler) abstract_socket_path.c_str(), std::min(sizeof(addr.sun_path), abstract_socket_path.size())); - if (const auto err = platform::posixSyscallError([this, &addr, &abstract_socket_path] { - // - return ::connect(state_.fd, - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - reinterpret_cast(&addr), - offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); - })) - { - logger().error("Failed to connect to server: {}.", std::strerror(err)); - return err; - } - - socket_callback_ = posix_executor_ext_->registerAwaitableCallback( // - [this](const auto&) { - // - handle_socket(); - }, - platform::IPosixExecutorExtension::Trigger::Readable{state_.fd}); - - event_handler_(Event::Connected{}); - return 0; -} - -void UnixSocketClient::handle_socket() -{ - if (const auto err = receiveMessage(state_, [this](const auto payload) { - // - return event_handler_(Event::Message{payload}); - })) - { - if (err == -1) - { - logger().debug("End of server stream - closing connection."); - } - else - { - logger().warn("Failed to handle server response - closing connection: {}.", std::strerror(err)); - } - - socket_callback_.reset(); - ::close(state_.fd); - state_.fd = -1; - - event_handler_(Event::Disconnected{}); - } + return connectSocket(out_fd, &addr, offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); } } // namespace pipe diff --git a/src/common/ipc/pipe/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp index 3ed37fd..5a62193 100644 --- a/src/common/ipc/pipe/unix_socket_client.hpp +++ b/src/common/ipc/pipe/unix_socket_client.hpp @@ -7,9 +7,7 @@ #define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED #include "client_pipe.hpp" -#include "ipc/ipc_types.hpp" -#include "ocvsmd/platform/posix_executor_extension.hpp" -#include "socket_base.hpp" +#include "socket_client_base.hpp" #include #include @@ -25,7 +23,7 @@ namespace ipc namespace pipe { -class UnixSocketClient final : public SocketBase, public ClientPipe +class UnixSocketClient final : public SocketClientBase { public: UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path); @@ -35,25 +33,14 @@ class UnixSocketClient final : public SocketBase, public ClientPipe UnixSocketClient& operator=(const UnixSocketClient&) = delete; UnixSocketClient& operator=(UnixSocketClient&&) noexcept = delete; - ~UnixSocketClient() override; - - // ClientPipe - - CETL_NODISCARD int start(EventHandler event_handler) override; - - CETL_NODISCARD int send(const Payloads payloads) override - { - return SocketBase::send(state_, payloads); - } + ~UnixSocketClient() override = default; private: - void handle_socket(); + using Base = SocketClientBase; + + CETL_NODISCARD int makeSocketHandle(int& out_fd) override; - const std::string socket_path_; - platform::IPosixExecutorExtension* const posix_executor_ext_; - State state_; - libcyphal::IExecutor::Callback::Any socket_callback_; - EventHandler event_handler_; + const std::string socket_path_; }; // UnixSocketClient diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp index 7080131..03ae9b9 100644 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ b/src/common/ipc/pipe/unix_socket_server.cpp @@ -56,18 +56,7 @@ int UnixSocketServer::makeSocketHandle(int& out_fd) abstract_socket_path.c_str(), std::min(sizeof(addr.sun_path), abstract_socket_path.size())); - if (const auto err = platform::posixSyscallError([&out_fd, &addr, &abstract_socket_path] { - // - return ::bind(out_fd, - reinterpret_cast(&addr), // NOLINT(*-reinterpret-cast) - offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); - })) - { - logger().error("Failed to bind server socket: {}.", std::strerror(err)); - return err; - } - - return 0; + return bindSocket(out_fd, &addr, offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); } } // namespace pipe From 277653fb59ee41fbcc9ae648d9c3c1634aee0e5f Mon Sep 17 00:00:00 2001 From: Sergei Date: Sun, 26 Jan 2025 13:27:18 +0200 Subject: [PATCH 077/156] added toml11 4.3.0 dependency --- include/toml.hpp | 62 + include/toml11/LICENSE | 21 + include/toml11/color.hpp | 10 + include/toml11/comments.hpp | 10 + include/toml11/compat.hpp | 810 ++++ include/toml11/context.hpp | 68 + include/toml11/conversion.hpp | 203 + include/toml11/datetime.hpp | 10 + include/toml11/error_info.hpp | 10 + include/toml11/exception.hpp | 17 + include/toml11/find.hpp | 548 +++ include/toml11/format.hpp | 10 + include/toml11/from.hpp | 17 + include/toml11/fwd/color_fwd.hpp | 88 + include/toml11/fwd/comments_fwd.hpp | 451 +++ include/toml11/fwd/datetime_fwd.hpp | 261 ++ include/toml11/fwd/error_info_fwd.hpp | 97 + include/toml11/fwd/format_fwd.hpp | 250 ++ include/toml11/fwd/literal_fwd.hpp | 33 + include/toml11/fwd/location_fwd.hpp | 149 + include/toml11/fwd/region_fwd.hpp | 110 + include/toml11/fwd/scanner_fwd.hpp | 391 ++ include/toml11/fwd/source_location_fwd.hpp | 148 + include/toml11/fwd/syntax_fwd.hpp | 357 ++ include/toml11/fwd/value_t_fwd.hpp | 117 + include/toml11/get.hpp | 632 +++ include/toml11/impl/color_impl.hpp | 76 + include/toml11/impl/comments_impl.hpp | 46 + include/toml11/impl/datetime_impl.hpp | 518 +++ include/toml11/impl/error_info_impl.hpp | 75 + include/toml11/impl/format_impl.hpp | 297 ++ include/toml11/impl/literal_impl.hpp | 174 + include/toml11/impl/location_impl.hpp | 209 + include/toml11/impl/region_impl.hpp | 246 ++ include/toml11/impl/scanner_impl.hpp | 473 +++ include/toml11/impl/source_location_impl.hpp | 211 + include/toml11/impl/syntax_impl.hpp | 732 ++++ include/toml11/impl/value_t_impl.hpp | 40 + include/toml11/into.hpp | 17 + include/toml11/literal.hpp | 10 + include/toml11/location.hpp | 10 + include/toml11/ordered_map.hpp | 265 ++ include/toml11/parser.hpp | 3829 ++++++++++++++++++ include/toml11/region.hpp | 10 + include/toml11/result.hpp | 486 +++ include/toml11/scanner.hpp | 10 + include/toml11/serializer.hpp | 1275 ++++++ include/toml11/skip.hpp | 392 ++ include/toml11/source_location.hpp | 10 + include/toml11/spec.hpp | 121 + include/toml11/storage.hpp | 49 + include/toml11/syntax.hpp | 10 + include/toml11/traits.hpp | 254 ++ include/toml11/types.hpp | 374 ++ include/toml11/utility.hpp | 170 + include/toml11/value.hpp | 2257 +++++++++++ include/toml11/value_t.hpp | 10 + include/toml11/version.hpp | 121 + include/toml11/visit.hpp | 136 + 59 files changed, 17793 insertions(+) create mode 100644 include/toml.hpp create mode 100644 include/toml11/LICENSE create mode 100644 include/toml11/color.hpp create mode 100644 include/toml11/comments.hpp create mode 100644 include/toml11/compat.hpp create mode 100644 include/toml11/context.hpp create mode 100644 include/toml11/conversion.hpp create mode 100644 include/toml11/datetime.hpp create mode 100644 include/toml11/error_info.hpp create mode 100644 include/toml11/exception.hpp create mode 100644 include/toml11/find.hpp create mode 100644 include/toml11/format.hpp create mode 100644 include/toml11/from.hpp create mode 100644 include/toml11/fwd/color_fwd.hpp create mode 100644 include/toml11/fwd/comments_fwd.hpp create mode 100644 include/toml11/fwd/datetime_fwd.hpp create mode 100644 include/toml11/fwd/error_info_fwd.hpp create mode 100644 include/toml11/fwd/format_fwd.hpp create mode 100644 include/toml11/fwd/literal_fwd.hpp create mode 100644 include/toml11/fwd/location_fwd.hpp create mode 100644 include/toml11/fwd/region_fwd.hpp create mode 100644 include/toml11/fwd/scanner_fwd.hpp create mode 100644 include/toml11/fwd/source_location_fwd.hpp create mode 100644 include/toml11/fwd/syntax_fwd.hpp create mode 100644 include/toml11/fwd/value_t_fwd.hpp create mode 100644 include/toml11/get.hpp create mode 100644 include/toml11/impl/color_impl.hpp create mode 100644 include/toml11/impl/comments_impl.hpp create mode 100644 include/toml11/impl/datetime_impl.hpp create mode 100644 include/toml11/impl/error_info_impl.hpp create mode 100644 include/toml11/impl/format_impl.hpp create mode 100644 include/toml11/impl/literal_impl.hpp create mode 100644 include/toml11/impl/location_impl.hpp create mode 100644 include/toml11/impl/region_impl.hpp create mode 100644 include/toml11/impl/scanner_impl.hpp create mode 100644 include/toml11/impl/source_location_impl.hpp create mode 100644 include/toml11/impl/syntax_impl.hpp create mode 100644 include/toml11/impl/value_t_impl.hpp create mode 100644 include/toml11/into.hpp create mode 100644 include/toml11/literal.hpp create mode 100644 include/toml11/location.hpp create mode 100644 include/toml11/ordered_map.hpp create mode 100644 include/toml11/parser.hpp create mode 100644 include/toml11/region.hpp create mode 100644 include/toml11/result.hpp create mode 100644 include/toml11/scanner.hpp create mode 100644 include/toml11/serializer.hpp create mode 100644 include/toml11/skip.hpp create mode 100644 include/toml11/source_location.hpp create mode 100644 include/toml11/spec.hpp create mode 100644 include/toml11/storage.hpp create mode 100644 include/toml11/syntax.hpp create mode 100644 include/toml11/traits.hpp create mode 100644 include/toml11/types.hpp create mode 100644 include/toml11/utility.hpp create mode 100644 include/toml11/value.hpp create mode 100644 include/toml11/value_t.hpp create mode 100644 include/toml11/version.hpp create mode 100644 include/toml11/visit.hpp diff --git a/include/toml.hpp b/include/toml.hpp new file mode 100644 index 0000000..75cc95b --- /dev/null +++ b/include/toml.hpp @@ -0,0 +1,62 @@ +#ifndef TOML11_TOML_HPP +#define TOML11_TOML_HPP + +// The MIT License (MIT) +// +// Copyright (c) 2017-now Toru Niina +// +// 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. + +// IWYU pragma: begin_exports +#include "toml11/color.hpp" +#include "toml11/comments.hpp" +#include "toml11/compat.hpp" +#include "toml11/context.hpp" +#include "toml11/conversion.hpp" +#include "toml11/datetime.hpp" +#include "toml11/error_info.hpp" +#include "toml11/exception.hpp" +#include "toml11/find.hpp" +#include "toml11/format.hpp" +#include "toml11/from.hpp" +#include "toml11/get.hpp" +#include "toml11/into.hpp" +#include "toml11/literal.hpp" +#include "toml11/location.hpp" +#include "toml11/ordered_map.hpp" +#include "toml11/parser.hpp" +#include "toml11/region.hpp" +#include "toml11/result.hpp" +#include "toml11/scanner.hpp" +#include "toml11/serializer.hpp" +#include "toml11/skip.hpp" +#include "toml11/source_location.hpp" +#include "toml11/spec.hpp" +#include "toml11/storage.hpp" +#include "toml11/syntax.hpp" +#include "toml11/traits.hpp" +#include "toml11/types.hpp" +#include "toml11/utility.hpp" +#include "toml11/value.hpp" +#include "toml11/value_t.hpp" +#include "toml11/version.hpp" +#include "toml11/visit.hpp" +// IWYU pragma: end_exports + +#endif// TOML11_TOML_HPP diff --git a/include/toml11/LICENSE b/include/toml11/LICENSE new file mode 100644 index 0000000..f55c511 --- /dev/null +++ b/include/toml11/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2017 Toru Niina + +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. diff --git a/include/toml11/color.hpp b/include/toml11/color.hpp new file mode 100644 index 0000000..d40d7f8 --- /dev/null +++ b/include/toml11/color.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_COLOR_HPP +#define TOML11_COLOR_HPP + +#include "fwd/color_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/color_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_COLOR_HPP diff --git a/include/toml11/comments.hpp b/include/toml11/comments.hpp new file mode 100644 index 0000000..4697f4e --- /dev/null +++ b/include/toml11/comments.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_COMMENTS_HPP +#define TOML11_COMMENTS_HPP + +#include "fwd/comments_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/comments_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_COMMENTS_HPP diff --git a/include/toml11/compat.hpp b/include/toml11/compat.hpp new file mode 100644 index 0000000..15a00af --- /dev/null +++ b/include/toml11/compat.hpp @@ -0,0 +1,810 @@ +#ifndef TOML11_COMPAT_HPP +#define TOML11_COMPAT_HPP + +#include "version.hpp" + +#include +#include +#include +#include +#include + +#include + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE +# if __has_include() +# include +# endif +#endif + +#include + +// ---------------------------------------------------------------------------- + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE +# if __has_cpp_attribute(deprecated) +# define TOML11_HAS_ATTR_DEPRECATED 1 +# endif +#endif + +#if defined(TOML11_HAS_ATTR_DEPRECATED) +# define TOML11_DEPRECATED(msg) [[deprecated(msg)]] +#elif defined(__GNUC__) +# define TOML11_DEPRECATED(msg) __attribute__((deprecated(msg))) +#elif defined(_MSC_VER) +# define TOML11_DEPRECATED(msg) __declspec(deprecated(msg)) +#else +# define TOML11_DEPRECATED(msg) +#endif + +// ---------------------------------------------------------------------------- + +#if defined(__cpp_if_constexpr) +# if __cpp_if_constexpr >= 201606L +# define TOML11_HAS_CONSTEXPR_IF 1 +# endif +#endif + +#if defined(TOML11_HAS_CONSTEXPR_IF) +# define TOML11_CONSTEXPR_IF if constexpr +#else +# define TOML11_CONSTEXPR_IF if +#endif + +// ---------------------------------------------------------------------------- + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE +# if defined(__cpp_lib_make_unique) +# if __cpp_lib_make_unique >= 201304L +# define TOML11_HAS_STD_MAKE_UNIQUE 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ + +#if defined(TOML11_HAS_STD_MAKE_UNIQUE) + +using std::make_unique; + +#else + +template +std::unique_ptr make_unique(Ts&& ... args) +{ + return std::unique_ptr(new T(std::forward(args)...)); +} + +#endif // TOML11_HAS_STD_MAKE_UNIQUE + +} // cxx +} // toml + +// --------------------------------------------------------------------------- + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE +# if defined(__cpp_lib_make_reverse_iterator) +# if __cpp_lib_make_reverse_iterator >= 201402L +# define TOML11_HAS_STD_MAKE_REVERSE_ITERATOR 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +# if defined(TOML11_HAS_STD_MAKE_REVERSE_ITERATOR) + +using std::make_reverse_iterator; + +#else + +template +std::reverse_iterator make_reverse_iterator(Iterator iter) +{ + return std::reverse_iterator(iter); +} + +#endif // TOML11_HAS_STD_MAKE_REVERSE_ITERATOR + +} // cxx +} // toml + +// --------------------------------------------------------------------------- + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE +# if defined(__cpp_lib_clamp) +# if __cpp_lib_clamp >= 201603L +# define TOML11_HAS_STD_CLAMP 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_CLAMP) + +using std::clamp; + +#else + +template +T clamp(const T& x, const T& low, const T& high) noexcept +{ + assert(low <= high); + return (std::min)((std::max)(x, low), high); +} + +#endif // TOML11_HAS_STD_CLAMP + +} // cxx +} // toml + +// --------------------------------------------------------------------------- + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE +# if defined(__cpp_lib_bit_cast) +# if __cpp_lib_bit_cast >= 201806L +# define TOML11_HAS_STD_BIT_CAST 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_BIT_CAST) + +using std::bit_cast; + +#else + +template +U bit_cast(const T& x) noexcept +{ + static_assert(sizeof(T) == sizeof(U), ""); + static_assert(std::is_default_constructible::value, ""); + + U z; + std::memcpy(reinterpret_cast(std::addressof(z)), + reinterpret_cast(std::addressof(x)), + sizeof(T)); + + return z; +} + +#endif // TOML11_HAS_STD_BIT_CAST + +} // cxx +} // toml + +// --------------------------------------------------------------------------- +// C++20 remove_cvref_t + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE +# if defined(__cpp_lib_remove_cvref) +# if __cpp_lib_remove_cvref >= 201711L +# define TOML11_HAS_STD_REMOVE_CVREF 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_REMOVE_CVREF) + +using std::remove_cvref; +using std::remove_cvref_t; + +#else + +template +struct remove_cvref +{ + using type = typename std::remove_cv< + typename std::remove_reference::type>::type; +}; + +template +using remove_cvref_t = typename remove_cvref::type; + +#endif // TOML11_HAS_STD_REMOVE_CVREF + +} // cxx +} // toml + +// --------------------------------------------------------------------------- +// C++17 and/or/not + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if defined(__cpp_lib_logical_traits) +# if __cpp_lib_logical_traits >= 201510L +# define TOML11_HAS_STD_CONJUNCTION 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_CONJUNCTION) + +using std::conjunction; +using std::disjunction; +using std::negation; + +#else + +template struct conjunction : std::true_type{}; +template struct conjunction : T{}; +template +struct conjunction : + std::conditional(T::value), conjunction, T>::type +{}; + +template struct disjunction : std::false_type{}; +template struct disjunction : T {}; +template +struct disjunction : + std::conditional(T::value), T, disjunction>::type +{}; + +template +struct negation : std::integral_constant(T::value)>{}; + +#endif // TOML11_HAS_STD_CONJUNCTION + +} // cxx +} // toml + +// --------------------------------------------------------------------------- +// C++14 index_sequence + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE +# if defined(__cpp_lib_integer_sequence) +# if __cpp_lib_integer_sequence >= 201304L +# define TOML11_HAS_STD_INTEGER_SEQUENCE 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_INTEGER_SEQUENCE) + +using std::index_sequence; +using std::make_index_sequence; + +#else + +template struct index_sequence{}; + +template +struct double_index_sequence; + +template +struct double_index_sequence> +{ + using type = index_sequence; +}; +template +struct double_index_sequence> +{ + using type = index_sequence; +}; + +template +struct index_sequence_maker +{ + using type = typename double_index_sequence< + N % 2 == 1, N/2, typename index_sequence_maker::type + >::type; +}; +template<> +struct index_sequence_maker<0> +{ + using type = index_sequence<>; +}; + +template +using make_index_sequence = typename index_sequence_maker::type; + +#endif // TOML11_HAS_STD_INTEGER_SEQUENCE + +} // cxx +} // toml + +// --------------------------------------------------------------------------- +// C++14 enable_if_t + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE +# if defined(__cpp_lib_transformation_trait_aliases) +# if __cpp_lib_transformation_trait_aliases >= 201304L +# define TOML11_HAS_STD_ENABLE_IF_T 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_ENABLE_IF_T) + +using std::enable_if_t; + +#else + +template +using enable_if_t = typename std::enable_if::type; + +#endif // TOML11_HAS_STD_ENABLE_IF_T + +} // cxx +} // toml + +// --------------------------------------------------------------------------- +// return_type_of_t + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if defined(__cpp_lib_is_invocable) +# if __cpp_lib_is_invocable >= 201703 +# define TOML11_HAS_STD_INVOKE_RESULT 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_INVOKE_RESULT) + +template +using return_type_of_t = std::invoke_result_t; + +#else + +// result_of is deprecated after C++17 +template +using return_type_of_t = typename std::result_of::type; + +#endif // TOML11_HAS_STD_INVOKE_RESULT + +} // cxx +} // toml + +// --------------------------------------------------------------------------- +// C++17 void_t + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if defined(__cpp_lib_void_t) +# if __cpp_lib_void_t >= 201411L +# define TOML11_HAS_STD_VOID_T 1 +# endif +# endif +#endif + +namespace toml +{ +namespace cxx +{ +#if defined(TOML11_HAS_STD_VOID_T) + +using std::void_t; + +#else + +template +using void_t = void; + +#endif // TOML11_HAS_STD_VOID_T + +} // cxx +} // toml + +// ---------------------------------------------------------------------------- +// (subset of) source_location + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 202002L +# if __has_include() +# define TOML11_HAS_STD_SOURCE_LOCATION +# endif // has_include +#endif // c++20 + +#if ! defined(TOML11_HAS_STD_SOURCE_LOCATION) +# if defined(__GNUC__) && ! defined(__clang__) +# if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE +# if __has_include() +# define TOML11_HAS_EXPERIMENTAL_SOURCE_LOCATION +# endif +# endif +# endif // GNU g++ +#endif // not TOML11_HAS_STD_SOURCE_LOCATION + +#if ! defined(TOML11_HAS_STD_SOURCE_LOCATION) && ! defined(TOML11_HAS_EXPERIMENTAL_SOURCE_LOCATION) +# if defined(__GNUC__) && ! defined(__clang__) +# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) +# define TOML11_HAS_BUILTIN_FILE_LINE 1 +# define TOML11_BUILTIN_LINE_TYPE int +# endif +# elif defined(__clang__) // clang 9.0.0 implements builtin_FILE/LINE +# if __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE) +# define TOML11_HAS_BUILTIN_FILE_LINE 1 +# define TOML11_BUILTIN_LINE_TYPE unsigned int +# endif +# elif defined(_MSVC_LANG) && defined(_MSC_VER) +# if _MSC_VER > 1926 +# define TOML11_HAS_BUILTIN_FILE_LINE 1 +# define TOML11_BUILTIN_LINE_TYPE int +# endif +# endif +#endif + +#if defined(TOML11_HAS_STD_SOURCE_LOCATION) +#include +namespace toml +{ +namespace cxx +{ +using source_location = std::source_location; + +inline std::string to_string(const source_location& loc) +{ + const char* fname = loc.file_name(); + if(fname) + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in file ") + std::string(fname); + } + else + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in unknown file"); + } +} + +} // cxx +} // toml +#elif defined(TOML11_HAS_EXPERIMENTAL_SOURCE_LOCATION) +#include +namespace toml +{ +namespace cxx +{ +using source_location = std::experimental::source_location; + +inline std::string to_string(const source_location& loc) +{ + const char* fname = loc.file_name(); + if(fname) + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in file ") + std::string(fname); + } + else + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in unknown file"); + } +} + +} // cxx +} // toml +#elif defined(TOML11_HAS_BUILTIN_FILE_LINE) +namespace toml +{ +namespace cxx +{ +struct source_location +{ + using line_type = TOML11_BUILTIN_LINE_TYPE; + static source_location current(const line_type line = __builtin_LINE(), + const char* file = __builtin_FILE()) + { + return source_location(line, file); + } + + source_location(const line_type line, const char* file) + : line_(line), file_name_(file) + {} + + line_type line() const noexcept {return line_;} + const char* file_name() const noexcept {return file_name_;} + + private: + + line_type line_; + const char* file_name_; +}; + +inline std::string to_string(const source_location& loc) +{ + const char* fname = loc.file_name(); + if(fname) + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in file ") + std::string(fname); + } + else + { + return std::string(" at line ") + std::to_string(loc.line()) + + std::string(" in unknown file"); + } +} + +} // cxx +} // toml +#else // no builtin +namespace toml +{ +namespace cxx +{ +struct source_location +{ + static source_location current() { return source_location{}; } +}; + +inline std::string to_string(const source_location&) +{ + return std::string(""); +} +} // cxx +} // toml +#endif // TOML11_HAS_STD_SOURCE_LOCATION + +// ---------------------------------------------------------------------------- +// (subset of) optional + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if __has_include() +# include +# endif // has_include(optional) +#endif // C++17 + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if defined(__cpp_lib_optional) +# if __cpp_lib_optional >= 201606L +# define TOML11_HAS_STD_OPTIONAL 1 +# endif +# endif +#endif + +#if defined(TOML11_HAS_STD_OPTIONAL) + +namespace toml +{ +namespace cxx +{ +using std::optional; + +inline std::nullopt_t make_nullopt() {return std::nullopt;} + +template +std::basic_ostream& +operator<<(std::basic_ostream& os, const std::nullopt_t&) +{ + os << "nullopt"; + return os; +} + +} // cxx +} // toml + +#else // TOML11_HAS_STD_OPTIONAL + +namespace toml +{ +namespace cxx +{ + +struct nullopt_t{}; +inline nullopt_t make_nullopt() {return nullopt_t{};} + +inline bool operator==(const nullopt_t&, const nullopt_t&) noexcept {return true;} +inline bool operator!=(const nullopt_t&, const nullopt_t&) noexcept {return false;} +inline bool operator< (const nullopt_t&, const nullopt_t&) noexcept {return false;} +inline bool operator<=(const nullopt_t&, const nullopt_t&) noexcept {return true;} +inline bool operator> (const nullopt_t&, const nullopt_t&) noexcept {return false;} +inline bool operator>=(const nullopt_t&, const nullopt_t&) noexcept {return true;} + +template +std::basic_ostream& +operator<<(std::basic_ostream& os, const nullopt_t&) +{ + os << "nullopt"; + return os; +} + +template +class optional +{ + public: + + using value_type = T; + + public: + + optional() noexcept : has_value_(false), null_('\0') {} + optional(nullopt_t) noexcept : has_value_(false), null_('\0') {} + + optional(const T& x): has_value_(true), value_(x) {} + optional(T&& x): has_value_(true), value_(std::move(x)) {} + + template::value, std::nullptr_t> = nullptr> + explicit optional(U&& x): has_value_(true), value_(std::forward(x)) {} + + optional(const optional& rhs): has_value_(rhs.has_value_) + { + if(rhs.has_value_) + { + this->assigner(rhs.value_); + } + } + optional(optional&& rhs): has_value_(rhs.has_value_) + { + if(this->has_value_) + { + this->assigner(std::move(rhs.value_)); + } + } + + optional& operator=(const optional& rhs) + { + if(this == std::addressof(rhs)) {return *this;} + + this->cleanup(); + this->has_value_ = rhs.has_value_; + if(this->has_value_) + { + this->assigner(rhs.value_); + } + return *this; + } + optional& operator=(optional&& rhs) + { + if(this == std::addressof(rhs)) {return *this;} + + this->cleanup(); + this->has_value_ = rhs.has_value_; + if(this->has_value_) + { + this->assigner(std::move(rhs.value_)); + } + return *this; + } + + template>, std::is_constructible + >::value, std::nullptr_t> = nullptr> + explicit optional(const optional& rhs): has_value_(rhs.has_value_), null_('\0') + { + if(rhs.has_value_) + { + this->assigner(rhs.value_); + } + } + template>, std::is_constructible + >::value, std::nullptr_t> = nullptr> + explicit optional(optional&& rhs): has_value_(rhs.has_value_), null_('\0') + { + if(this->has_value_) + { + this->assigner(std::move(rhs.value_)); + } + } + + template>, std::is_constructible + >::value, std::nullptr_t> = nullptr> + optional& operator=(const optional& rhs) + { + if(this == std::addressof(rhs)) {return *this;} + + this->cleanup(); + this->has_value_ = rhs.has_value_; + if(this->has_value_) + { + this->assigner(rhs.value_); + } + return *this; + } + + template>, std::is_constructible + >::value, std::nullptr_t> = nullptr> + optional& operator=(optional&& rhs) + { + if(this == std::addressof(rhs)) {return *this;} + + this->cleanup(); + this->has_value_ = rhs.has_value_; + if(this->has_value_) + { + this->assigner(std::move(rhs.value_)); + } + return *this; + } + ~optional() noexcept + { + this->cleanup(); + } + + explicit operator bool() const noexcept + { + return has_value_; + } + + bool has_value() const noexcept {return has_value_;} + + value_type const& value(source_location loc = source_location::current()) const + { + if( ! this->has_value_) + { + throw std::runtime_error("optional::value(): bad_unwrap" + to_string(loc)); + } + return this->value_; + } + value_type& value(source_location loc = source_location::current()) + { + if( ! this->has_value_) + { + throw std::runtime_error("optional::value(): bad_unwrap" + to_string(loc)); + } + return this->value_; + } + + value_type const& value_or(const value_type& opt) const + { + if(this->has_value_) {return this->value_;} else {return opt;} + } + value_type& value_or(value_type& opt) + { + if(this->has_value_) {return this->value_;} else {return opt;} + } + + private: + + void cleanup() noexcept + { + if(this->has_value_) + { + value_.~T(); + } + } + + template + void assigner(U&& x) + { + const auto tmp = ::new(std::addressof(this->value_)) value_type(std::forward(x)); + assert(tmp == std::addressof(this->value_)); + (void)tmp; + } + + private: + + bool has_value_; + union + { + char null_; + T value_; + }; +}; +} // cxx +} // toml +#endif // TOML11_HAS_STD_OPTIONAL + +#endif // TOML11_COMPAT_HPP diff --git a/include/toml11/context.hpp b/include/toml11/context.hpp new file mode 100644 index 0000000..cda038f --- /dev/null +++ b/include/toml11/context.hpp @@ -0,0 +1,68 @@ +#ifndef TOML11_CONTEXT_HPP +#define TOML11_CONTEXT_HPP + +#include "error_info.hpp" +#include "spec.hpp" + +#include + +namespace toml +{ +namespace detail +{ + +template +class context +{ + public: + + explicit context(const spec& toml_spec) + : toml_spec_(toml_spec), errors_{} + {} + + bool has_error() const noexcept {return !errors_.empty();} + + std::vector const& errors() const noexcept {return errors_;} + + semantic_version& toml_version() noexcept {return toml_spec_.version;} + semantic_version const& toml_version() const noexcept {return toml_spec_.version;} + + spec& toml_spec() noexcept {return toml_spec_;} + spec const& toml_spec() const noexcept {return toml_spec_;} + + void report_error(error_info err) + { + this->errors_.push_back(std::move(err)); + } + + error_info pop_last_error() + { + assert( ! errors_.empty()); + auto e = std::move(errors_.back()); + errors_.pop_back(); + return e; + } + + private: + + spec toml_spec_; + std::vector errors_; +}; + +} // detail +} // toml + +#if defined(TOML11_COMPILE_SOURCES) +namespace toml +{ +struct type_config; +struct ordered_type_config; +namespace detail +{ +extern template class context<::toml::type_config>; +extern template class context<::toml::ordered_type_config>; +} // detail +} // toml +#endif // TOML11_COMPILE_SOURCES + +#endif // TOML11_CONTEXT_HPP diff --git a/include/toml11/conversion.hpp b/include/toml11/conversion.hpp new file mode 100644 index 0000000..819a765 --- /dev/null +++ b/include/toml11/conversion.hpp @@ -0,0 +1,203 @@ +#ifndef TOML11_CONVERSION_HPP +#define TOML11_CONVERSION_HPP + +#include "find.hpp" +#include "from.hpp" // IWYU pragma: keep +#include "into.hpp" // IWYU pragma: keep + +#if defined(TOML11_HAS_OPTIONAL) + +#include + +namespace toml +{ +namespace detail +{ + +template +inline constexpr bool is_optional_v = false; + +template +inline constexpr bool is_optional_v> = true; + +template +void find_member_variable_from_value(T& obj, const basic_value& v, const char* var_name) +{ + if constexpr(is_optional_v) + { + if(v.contains(var_name)) + { + obj = toml::find(v, var_name); + } + else + { + obj = std::nullopt; + } + } + else + { + obj = toml::find(v, var_name); + } +} + +template +void assign_member_variable_to_value(const T& obj, basic_value& v, const char* var_name) +{ + if constexpr(is_optional_v) + { + if(obj.has_value()) + { + v[var_name] = obj.value(); + } + } + else + { + v[var_name] = obj; + } +} + +} // detail +} // toml + +#else + +namespace toml +{ +namespace detail +{ + +template +void find_member_variable_from_value(T& obj, const basic_value& v, const char* var_name) +{ + obj = toml::find(v, var_name); +} + +template +void assign_member_variable_to_value(const T& obj, basic_value& v, const char* var_name) +{ + v[var_name] = obj; +} + +} // detail +} // toml + +#endif // optional + +// use it in the following way. +// ```cpp +// namespace foo +// { +// struct Foo +// { +// std::string s; +// double d; +// int i; +// }; +// } // foo +// +// TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(foo::Foo, s, d, i) +// ``` +// +// And then you can use `toml::get(v)` and `toml::find(file, "foo");` +// + +#define TOML11_STRINGIZE_AUX(x) #x +#define TOML11_STRINGIZE(x) TOML11_STRINGIZE_AUX(x) + +#define TOML11_CONCATENATE_AUX(x, y) x##y +#define TOML11_CONCATENATE(x, y) TOML11_CONCATENATE_AUX(x, y) + +// ============================================================================ +// TOML11_DEFINE_CONVERSION_NON_INTRUSIVE + +#ifndef TOML11_WITHOUT_DEFINE_NON_INTRUSIVE + +// ---------------------------------------------------------------------------- +// TOML11_ARGS_SIZE + +#define TOML11_INDEX_RSEQ() \ + 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, \ + 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 +#define TOML11_ARGS_SIZE_IMPL(\ + ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, ARG10, \ + ARG11, ARG12, ARG13, ARG14, ARG15, ARG16, ARG17, ARG18, ARG19, ARG20, \ + ARG21, ARG22, ARG23, ARG24, ARG25, ARG26, ARG27, ARG28, ARG29, ARG30, \ + ARG31, ARG32, N, ...) N +#define TOML11_ARGS_SIZE_AUX(...) TOML11_ARGS_SIZE_IMPL(__VA_ARGS__) +#define TOML11_ARGS_SIZE(...) TOML11_ARGS_SIZE_AUX(__VA_ARGS__, TOML11_INDEX_RSEQ()) + +// ---------------------------------------------------------------------------- +// TOML11_FOR_EACH_VA_ARGS + +#define TOML11_FOR_EACH_VA_ARGS_AUX_1( FUNCTOR, ARG1 ) FUNCTOR(ARG1) +#define TOML11_FOR_EACH_VA_ARGS_AUX_2( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_1( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_3( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_2( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_4( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_3( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_5( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_4( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_6( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_5( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_7( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_6( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_8( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_7( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_9( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_8( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_10(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_9( FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_11(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_10(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_12(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_11(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_13(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_12(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_14(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_13(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_15(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_14(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_16(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_15(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_17(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_16(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_18(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_17(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_19(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_18(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_20(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_19(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_21(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_20(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_22(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_21(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_23(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_22(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_24(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_23(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_25(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_24(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_26(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_25(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_27(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_26(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_28(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_27(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_29(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_28(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_30(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_29(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_31(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_30(FUNCTOR, __VA_ARGS__) +#define TOML11_FOR_EACH_VA_ARGS_AUX_32(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_31(FUNCTOR, __VA_ARGS__) + +#define TOML11_FOR_EACH_VA_ARGS(FUNCTOR, ...)\ + TOML11_CONCATENATE(TOML11_FOR_EACH_VA_ARGS_AUX_, TOML11_ARGS_SIZE(__VA_ARGS__))(FUNCTOR, __VA_ARGS__) + + +#define TOML11_FIND_MEMBER_VARIABLE_FROM_VALUE(VAR_NAME)\ + toml::detail::find_member_variable_from_value(obj.VAR_NAME, v, TOML11_STRINGIZE(VAR_NAME)); + +#define TOML11_ASSIGN_MEMBER_VARIABLE_TO_VALUE(VAR_NAME)\ + toml::detail::assign_member_variable_to_value(obj.VAR_NAME, v, TOML11_STRINGIZE(VAR_NAME)); + +#define TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(NAME, ...)\ + namespace toml { \ + template<> \ + struct from \ + { \ + template \ + static NAME from_toml(const basic_value& v) \ + { \ + NAME obj; \ + TOML11_FOR_EACH_VA_ARGS(TOML11_FIND_MEMBER_VARIABLE_FROM_VALUE, __VA_ARGS__) \ + return obj; \ + } \ + }; \ + template<> \ + struct into \ + { \ + template \ + static basic_value into_toml(const NAME& obj) \ + { \ + ::toml::basic_value v = typename ::toml::basic_value::table_type{}; \ + TOML11_FOR_EACH_VA_ARGS(TOML11_ASSIGN_MEMBER_VARIABLE_TO_VALUE, __VA_ARGS__) \ + return v; \ + } \ + }; \ + } /* toml */ + +#endif// TOML11_WITHOUT_DEFINE_NON_INTRUSIVE + +#endif // TOML11_CONVERSION_HPP diff --git a/include/toml11/datetime.hpp b/include/toml11/datetime.hpp new file mode 100644 index 0000000..0841180 --- /dev/null +++ b/include/toml11/datetime.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_DATETIME_HPP +#define TOML11_DATETIME_HPP + +#include "fwd/datetime_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/datetime_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_DATETIME_HPP diff --git a/include/toml11/error_info.hpp b/include/toml11/error_info.hpp new file mode 100644 index 0000000..4575f7a --- /dev/null +++ b/include/toml11/error_info.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_ERROR_INFO_HPP +#define TOML11_ERROR_INFO_HPP + +#include "fwd/error_info_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/error_info_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_ERROR_INFO_HPP diff --git a/include/toml11/exception.hpp b/include/toml11/exception.hpp new file mode 100644 index 0000000..7149eb3 --- /dev/null +++ b/include/toml11/exception.hpp @@ -0,0 +1,17 @@ +#ifndef TOML11_EXCEPTION_HPP +#define TOML11_EXCEPTION_HPP + +#include + +namespace toml +{ + +struct exception : public std::exception +{ + public: + virtual ~exception() noexcept override = default; + virtual const char* what() const noexcept override {return "";} +}; + +} // toml +#endif // TOMl11_EXCEPTION_HPP diff --git a/include/toml11/find.hpp b/include/toml11/find.hpp new file mode 100644 index 0000000..1d0658d --- /dev/null +++ b/include/toml11/find.hpp @@ -0,0 +1,548 @@ +#ifndef TOML11_FIND_HPP +#define TOML11_FIND_HPP + +#include + +#include "get.hpp" +#include "value.hpp" + +#if defined(TOML11_HAS_STRING_VIEW) +#include +#endif + +namespace toml +{ + +// ---------------------------------------------------------------------------- +// find(value, key); + +template +decltype(::toml::get(std::declval const&>())) +find(const basic_value& v, const typename basic_value::key_type& ky) +{ + return ::toml::get(v.at(ky)); +} + +template +decltype(::toml::get(std::declval&>())) +find(basic_value& v, const typename basic_value::key_type& ky) +{ + return ::toml::get(v.at(ky)); +} + +template +decltype(::toml::get(std::declval&&>())) +find(basic_value&& v, const typename basic_value::key_type& ky) +{ + return ::toml::get(std::move(v.at(ky))); +} + +// ---------------------------------------------------------------------------- +// find(value, idx) + +template +decltype(::toml::get(std::declval const&>())) +find(const basic_value& v, const std::size_t idx) +{ + return ::toml::get(v.at(idx)); +} +template +decltype(::toml::get(std::declval&>())) +find(basic_value& v, const std::size_t idx) +{ + return ::toml::get(v.at(idx)); +} +template +decltype(::toml::get(std::declval&&>())) +find(basic_value&& v, const std::size_t idx) +{ + return ::toml::get(std::move(v.at(idx))); +} + +// ---------------------------------------------------------------------------- +// find(value, key/idx), w/o conversion + +template +cxx::enable_if_t::value, basic_value>& +find(basic_value& v, const typename basic_value::key_type& ky) +{ + return v.at(ky); +} +template +cxx::enable_if_t::value, basic_value> const& +find(basic_value const& v, const typename basic_value::key_type& ky) +{ + return v.at(ky); +} +template +cxx::enable_if_t::value, basic_value> +find(basic_value&& v, const typename basic_value::key_type& ky) +{ + return basic_value(std::move(v.at(ky))); +} + +template +cxx::enable_if_t::value, basic_value>& +find(basic_value& v, const std::size_t idx) +{ + return v.at(idx); +} +template +cxx::enable_if_t::value, basic_value> const& +find(basic_value const& v, const std::size_t idx) +{ + return v.at(idx); +} +template +cxx::enable_if_t::value, basic_value> +find(basic_value&& v, const std::size_t idx) +{ + return basic_value(std::move(v.at(idx))); +} + +// -------------------------------------------------------------------------- +// find> + +#if defined(TOML11_HAS_OPTIONAL) +template +cxx::enable_if_t::value, T> +find(const basic_value& v, const typename basic_value::key_type& ky) +{ + if(v.contains(ky)) + { + return ::toml::get(v.at(ky)); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value, T> +find(basic_value& v, const typename basic_value::key_type& ky) +{ + if(v.contains(ky)) + { + return ::toml::get(v.at(ky)); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value, T> +find(basic_value&& v, const typename basic_value::key_type& ky) +{ + if(v.contains(ky)) + { + return ::toml::get(std::move(v.at(ky))); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(const basic_value& v, const K& k) +{ + if(static_cast(k) < v.size()) + { + return ::toml::get(v.at(static_cast(k))); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value& v, const K& k) +{ + if(static_cast(k) < v.size()) + { + return ::toml::get(v.at(static_cast(k))); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value&& v, const K& k) +{ + if(static_cast(k) < v.size()) + { + return ::toml::get(std::move(v.at(static_cast(k)))); + } + else + { + return std::nullopt; + } +} +#endif // optional + +// -------------------------------------------------------------------------- +// toml::find(toml::value, toml::key, Ts&& ... keys) + +namespace detail +{ + +// It suppresses warnings by -Wsign-conversion when we pass integer literal +// to toml::find. integer literal `0` is deduced as an int, and will be +// converted to std::size_t. This causes sign-conversion. + +template +std::size_t key_cast(const std::size_t& v) noexcept +{ + return v; +} +template +cxx::enable_if_t>::value, std::size_t> +key_cast(const T& v) noexcept +{ + return static_cast(v); +} + +// for string-like (string, string literal, string_view) + +template +typename basic_value::key_type const& +key_cast(const typename basic_value::key_type& v) noexcept +{ + return v; +} +template +typename basic_value::key_type +key_cast(const typename basic_value::key_type::value_type* v) +{ + return typename basic_value::key_type(v); +} +#if defined(TOML11_HAS_STRING_VIEW) +template +typename basic_value::key_type +key_cast(const std::string_view v) +{ + return typename basic_value::key_type(v); +} +#endif // string_view + +} // detail + +// ---------------------------------------------------------------------------- +// find(v, keys...) + +template +cxx::enable_if_t::value, basic_value> const& +find(const basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); +} +template +cxx::enable_if_t::value, basic_value>& +find(basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); +} +template +cxx::enable_if_t::value, basic_value> +find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + return find(std::move(v.at(detail::key_cast(k1))), detail::key_cast(k2), ks...); +} + +// ---------------------------------------------------------------------------- +// find(v, keys...) + +template +decltype(::toml::get(std::declval&>())) +find(const basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); +} +template +decltype(::toml::get(std::declval&>())) +find(basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); +} +template +decltype(::toml::get(std::declval&&>())) +find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + return find(std::move(v.at(detail::key_cast(k1))), detail::key_cast(k2), ks...); +} + +#if defined(TOML11_HAS_OPTIONAL) +template +cxx::enable_if_t::value, T> +find(const basic_value& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) +{ + if(v.contains(k1)) + { + return find(v.at(k1), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value, T> +find(basic_value& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) +{ + if(v.contains(k1)) + { + return find(v.at(k1), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value, T> +find(basic_value&& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) +{ + if(v.contains(k1)) + { + return find(v.at(k1), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} + +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(const basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + if(static_cast(k1) < v.size()) + { + return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + if(static_cast(k1) < v.size()) + { + return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +template +cxx::enable_if_t::value && std::is_integral::value, T> +find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) +{ + if(static_cast(k1) < v.size()) + { + return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); + } + else + { + return std::nullopt; + } +} +#endif // optional + +// =========================================================================== +// find_or(value, key, fallback) + +// --------------------------------------------------------------------------- +// find_or(v, key, other_v) + +template +cxx::enable_if_t::value, basic_value>& +find_or(basic_value& v, const K& k, basic_value& opt) noexcept +{ + try + { + return ::toml::find(v, detail::key_cast(k)); + } + catch(...) + { + return opt; + } +} +template +cxx::enable_if_t::value, basic_value> const& +find_or(const basic_value& v, const K& k, const basic_value& opt) noexcept +{ + try + { + return ::toml::find(v, detail::key_cast(k)); + } + catch(...) + { + return opt; + } +} +template +cxx::enable_if_t::value, basic_value> +find_or(basic_value&& v, const K& k, basic_value&& opt) noexcept +{ + try + { + return ::toml::find(v, detail::key_cast(k)); + } + catch(...) + { + return opt; + } +} + +// --------------------------------------------------------------------------- +// toml types (return type can be a reference) + +template +cxx::enable_if_t>::value, + cxx::remove_cvref_t const&> +find_or(const basic_value& v, const K& k, const T& opt) +{ + try + { + return ::toml::get(v.at(detail::key_cast(k))); + } + catch(...) + { + return opt; + } +} + +template +cxx::enable_if_t>, + detail::is_exact_toml_type> + >::value, cxx::remove_cvref_t&> +find_or(basic_value& v, const K& k, T& opt) +{ + try + { + return ::toml::get(v.at(detail::key_cast(k))); + } + catch(...) + { + return opt; + } +} + +template +cxx::enable_if_t>::value, + cxx::remove_cvref_t> +find_or(basic_value&& v, const K& k, T opt) +{ + try + { + return ::toml::get(std::move(v.at(detail::key_cast(k)))); + } + catch(...) + { + return T(std::move(opt)); + } +} + +// --------------------------------------------------------------------------- +// string literal (deduced as std::string) + +// XXX to avoid confusion when T is explicitly specified in find_or(), +// we restrict the string type as std::string. +template +cxx::enable_if_t::value, std::string> +find_or(const basic_value& v, const K& k, const char* opt) +{ + try + { + return ::toml::get(v.at(detail::key_cast(k))); + } + catch(...) + { + return std::string(opt); + } +} + +// --------------------------------------------------------------------------- +// other types (requires type conversion and return type cannot be a reference) + +template +cxx::enable_if_t>>, + detail::is_not_toml_type, basic_value>, + cxx::negation, + const typename basic_value::string_type::value_type*>> + >::value, cxx::remove_cvref_t> +find_or(const basic_value& v, const K& ky, T opt) +{ + try + { + return ::toml::get>(v.at(detail::key_cast(ky))); + } + catch(...) + { + return cxx::remove_cvref_t(std::move(opt)); + } +} + +// ---------------------------------------------------------------------------- +// recursive + +namespace detail +{ + +template +auto last_one(Ts&&... args) + -> decltype(std::get(std::forward_as_tuple(std::forward(args)...))) +{ + return std::get(std::forward_as_tuple(std::forward(args)...)); +} + +} // detail + +template +auto find_or(Value&& v, const K1& k1, const K2& k2, K3&& k3, Ks&& ... keys) noexcept + -> cxx::enable_if_t< + detail::is_basic_value>::value, + decltype(find_or(v, k2, std::forward(k3), std::forward(keys)...)) + > +{ + try + { + return find_or(v.at(k1), k2, std::forward(k3), std::forward(keys)...); + } + catch(...) + { + return detail::last_one(k3, keys...); + } +} + +template +T find_or(const basic_value& v, const K1& k1, const K2& k2, const K3& k3, const Ks& ... keys) noexcept +{ + try + { + return find_or(v.at(k1), k2, k3, keys...); + } + catch(...) + { + return static_cast(detail::last_one(k3, keys...)); + } +} + +} // toml +#endif // TOML11_FIND_HPP diff --git a/include/toml11/format.hpp b/include/toml11/format.hpp new file mode 100644 index 0000000..6662220 --- /dev/null +++ b/include/toml11/format.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_FORMAT_HPP +#define TOML11_FORMAT_HPP + +#include "fwd/format_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/format_impl.hpp" // IWYU pragma: export +#endif + +#endif// TOML11_FORMAT_HPP diff --git a/include/toml11/from.hpp b/include/toml11/from.hpp new file mode 100644 index 0000000..d2e0e13 --- /dev/null +++ b/include/toml11/from.hpp @@ -0,0 +1,17 @@ +#ifndef TOML11_FROM_HPP +#define TOML11_FROM_HPP + +namespace toml +{ + +template +struct from; +// { +// static T from_toml(const toml::value& v) +// { +// // User-defined conversions ... +// } +// }; + +} // toml +#endif // TOML11_FROM_HPP diff --git a/include/toml11/fwd/color_fwd.hpp b/include/toml11/fwd/color_fwd.hpp new file mode 100644 index 0000000..ed711c0 --- /dev/null +++ b/include/toml11/fwd/color_fwd.hpp @@ -0,0 +1,88 @@ +#ifndef TOML11_COLOR_FWD_HPP +#define TOML11_COLOR_FWD_HPP + +#include + +#ifdef TOML11_COLORIZE_ERROR_MESSAGE +#define TOML11_ERROR_MESSAGE_COLORIZED true +#else +#define TOML11_ERROR_MESSAGE_COLORIZED false +#endif + +#ifdef TOML11_USE_THREAD_LOCAL_COLORIZATION +#define TOML11_THREAD_LOCAL_COLORIZATION thread_local +#else +#define TOML11_THREAD_LOCAL_COLORIZATION +#endif + +namespace toml +{ +namespace color +{ +// put ANSI escape sequence to ostream +inline namespace ansi +{ +namespace detail +{ + +// Control color mode globally +class color_mode +{ + public: + + void enable() noexcept + { + should_color_ = true; + } + void disable() noexcept + { + should_color_ = false; + } + bool should_color() const noexcept + { + return should_color_; + } + + private: + + bool should_color_ = TOML11_ERROR_MESSAGE_COLORIZED; +}; + +inline color_mode& color_status() noexcept +{ + static TOML11_THREAD_LOCAL_COLORIZATION color_mode status; + return status; +} + +} // detail + +std::ostream& reset (std::ostream& os); +std::ostream& bold (std::ostream& os); +std::ostream& grey (std::ostream& os); +std::ostream& gray (std::ostream& os); +std::ostream& red (std::ostream& os); +std::ostream& green (std::ostream& os); +std::ostream& yellow (std::ostream& os); +std::ostream& blue (std::ostream& os); +std::ostream& magenta(std::ostream& os); +std::ostream& cyan (std::ostream& os); +std::ostream& white (std::ostream& os); + +} // ansi + +inline void enable() +{ + return detail::color_status().enable(); +} +inline void disable() +{ + return detail::color_status().disable(); +} +inline bool should_color() +{ + return detail::color_status().should_color(); +} + +} // color +} // toml +#endif // TOML11_COLOR_FWD_HPP diff --git a/include/toml11/fwd/comments_fwd.hpp b/include/toml11/fwd/comments_fwd.hpp new file mode 100644 index 0000000..bbc9926 --- /dev/null +++ b/include/toml11/fwd/comments_fwd.hpp @@ -0,0 +1,451 @@ +#ifndef TOML11_COMMENTS_FWD_HPP +#define TOML11_COMMENTS_FWD_HPP + +// to use __has_builtin +#include "../version.hpp" // IWYU pragma: keep + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// This file provides mainly two classes, `preserve_comments` and `discard_comments`. +// Those two are a container that have the same interface as `std::vector` +// but bahaves in the opposite way. `preserve_comments` is just the same as +// `std::vector` and each `std::string` corresponds to a comment line. +// Conversely, `discard_comments` discards all the strings and ignores everything +// assigned in it. `discard_comments` is always empty and you will encounter an +// error whenever you access to the element. +namespace toml +{ +class discard_comments; // forward decl + +class preserve_comments +{ + public: + // `container_type` is not provided in discard_comments. + // do not use this inner-type in a generic code. + using container_type = std::vector; + + using size_type = container_type::size_type; + using difference_type = container_type::difference_type; + using value_type = container_type::value_type; + using reference = container_type::reference; + using const_reference = container_type::const_reference; + using pointer = container_type::pointer; + using const_pointer = container_type::const_pointer; + using iterator = container_type::iterator; + using const_iterator = container_type::const_iterator; + using reverse_iterator = container_type::reverse_iterator; + using const_reverse_iterator = container_type::const_reverse_iterator; + + public: + + preserve_comments() = default; + ~preserve_comments() = default; + preserve_comments(preserve_comments const&) = default; + preserve_comments(preserve_comments &&) = default; + preserve_comments& operator=(preserve_comments const&) = default; + preserve_comments& operator=(preserve_comments &&) = default; + + explicit preserve_comments(const std::vector& c): comments(c){} + explicit preserve_comments(std::vector&& c) + : comments(std::move(c)) + {} + preserve_comments& operator=(const std::vector& c) + { + comments = c; + return *this; + } + preserve_comments& operator=(std::vector&& c) + { + comments = std::move(c); + return *this; + } + + explicit preserve_comments(const discard_comments&) {} + + explicit preserve_comments(size_type n): comments(n) {} + preserve_comments(size_type n, const std::string& x): comments(n, x) {} + preserve_comments(std::initializer_list x): comments(x) {} + template + preserve_comments(InputIterator first, InputIterator last) + : comments(first, last) + {} + + template + void assign(InputIterator first, InputIterator last) {comments.assign(first, last);} + void assign(std::initializer_list ini) {comments.assign(ini);} + void assign(size_type n, const std::string& val) {comments.assign(n, val);} + + // Related to the issue #97. + // + // `std::vector::insert` and `std::vector::erase` in the STL implementation + // included in GCC 4.8.5 takes `std::vector::iterator` instead of + // `std::vector::const_iterator`. It causes compilation error in GCC 4.8.5. +#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) && !defined(__clang__) +# if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) <= 40805 +# define TOML11_WORKAROUND_GCC_4_8_X_STANDARD_LIBRARY_IMPLEMENTATION +# endif +#endif + +#ifdef TOML11_WORKAROUND_GCC_4_8_X_STANDARD_LIBRARY_IMPLEMENTATION + iterator insert(iterator p, const std::string& x) + { + return comments.insert(p, x); + } + iterator insert(iterator p, std::string&& x) + { + return comments.insert(p, std::move(x)); + } + void insert(iterator p, size_type n, const std::string& x) + { + return comments.insert(p, n, x); + } + template + void insert(iterator p, InputIterator first, InputIterator last) + { + return comments.insert(p, first, last); + } + void insert(iterator p, std::initializer_list ini) + { + return comments.insert(p, ini); + } + + template + iterator emplace(iterator p, Ts&& ... args) + { + return comments.emplace(p, std::forward(args)...); + } + + iterator erase(iterator pos) {return comments.erase(pos);} + iterator erase(iterator first, iterator last) + { + return comments.erase(first, last); + } +#else + iterator insert(const_iterator p, const std::string& x) + { + return comments.insert(p, x); + } + iterator insert(const_iterator p, std::string&& x) + { + return comments.insert(p, std::move(x)); + } + iterator insert(const_iterator p, size_type n, const std::string& x) + { + return comments.insert(p, n, x); + } + template + iterator insert(const_iterator p, InputIterator first, InputIterator last) + { + return comments.insert(p, first, last); + } + iterator insert(const_iterator p, std::initializer_list ini) + { + return comments.insert(p, ini); + } + + template + iterator emplace(const_iterator p, Ts&& ... args) + { + return comments.emplace(p, std::forward(args)...); + } + + iterator erase(const_iterator pos) {return comments.erase(pos);} + iterator erase(const_iterator first, const_iterator last) + { + return comments.erase(first, last); + } +#endif + + void swap(preserve_comments& other) {comments.swap(other.comments);} + + void push_back(const std::string& v) {comments.push_back(v);} + void push_back(std::string&& v) {comments.push_back(std::move(v));} + void pop_back() {comments.pop_back();} + + template + void emplace_back(Ts&& ... args) {comments.emplace_back(std::forward(args)...);} + + void clear() {comments.clear();} + + size_type size() const noexcept {return comments.size();} + size_type max_size() const noexcept {return comments.max_size();} + size_type capacity() const noexcept {return comments.capacity();} + bool empty() const noexcept {return comments.empty();} + + void reserve(size_type n) {comments.reserve(n);} + void resize(size_type n) {comments.resize(n);} + void resize(size_type n, const std::string& c) {comments.resize(n, c);} + void shrink_to_fit() {comments.shrink_to_fit();} + + reference operator[](const size_type n) noexcept {return comments[n];} + const_reference operator[](const size_type n) const noexcept {return comments[n];} + reference at(const size_type n) {return comments.at(n);} + const_reference at(const size_type n) const {return comments.at(n);} + reference front() noexcept {return comments.front();} + const_reference front() const noexcept {return comments.front();} + reference back() noexcept {return comments.back();} + const_reference back() const noexcept {return comments.back();} + + pointer data() noexcept {return comments.data();} + const_pointer data() const noexcept {return comments.data();} + + iterator begin() noexcept {return comments.begin();} + iterator end() noexcept {return comments.end();} + const_iterator begin() const noexcept {return comments.begin();} + const_iterator end() const noexcept {return comments.end();} + const_iterator cbegin() const noexcept {return comments.cbegin();} + const_iterator cend() const noexcept {return comments.cend();} + + reverse_iterator rbegin() noexcept {return comments.rbegin();} + reverse_iterator rend() noexcept {return comments.rend();} + const_reverse_iterator rbegin() const noexcept {return comments.rbegin();} + const_reverse_iterator rend() const noexcept {return comments.rend();} + const_reverse_iterator crbegin() const noexcept {return comments.crbegin();} + const_reverse_iterator crend() const noexcept {return comments.crend();} + + friend bool operator==(const preserve_comments&, const preserve_comments&); + friend bool operator!=(const preserve_comments&, const preserve_comments&); + friend bool operator< (const preserve_comments&, const preserve_comments&); + friend bool operator<=(const preserve_comments&, const preserve_comments&); + friend bool operator> (const preserve_comments&, const preserve_comments&); + friend bool operator>=(const preserve_comments&, const preserve_comments&); + + friend void swap(preserve_comments&, std::vector&); + friend void swap(std::vector&, preserve_comments&); + + private: + + container_type comments; +}; + +bool operator==(const preserve_comments& lhs, const preserve_comments& rhs); +bool operator!=(const preserve_comments& lhs, const preserve_comments& rhs); +bool operator< (const preserve_comments& lhs, const preserve_comments& rhs); +bool operator<=(const preserve_comments& lhs, const preserve_comments& rhs); +bool operator> (const preserve_comments& lhs, const preserve_comments& rhs); +bool operator>=(const preserve_comments& lhs, const preserve_comments& rhs); + +void swap(preserve_comments& lhs, preserve_comments& rhs); +void swap(preserve_comments& lhs, std::vector& rhs); +void swap(std::vector& lhs, preserve_comments& rhs); + +std::ostream& operator<<(std::ostream& os, const preserve_comments& com); + +namespace detail +{ + +// To provide the same interface with `preserve_comments`, `discard_comments` +// should have an iterator. But it does not contain anything, so we need to +// add an iterator that points nothing. +// +// It always points null, so DO NOT unwrap this iterator. It always crashes +// your program. +template +struct empty_iterator +{ + using value_type = T; + using reference_type = typename std::conditional::type; + using pointer_type = typename std::conditional::type; + using difference_type = std::ptrdiff_t; + using iterator_category = std::random_access_iterator_tag; + + empty_iterator() = default; + ~empty_iterator() = default; + empty_iterator(empty_iterator const&) = default; + empty_iterator(empty_iterator &&) = default; + empty_iterator& operator=(empty_iterator const&) = default; + empty_iterator& operator=(empty_iterator &&) = default; + + // DO NOT call these operators. + reference_type operator*() const noexcept {std::terminate();} + pointer_type operator->() const noexcept {return nullptr;} + reference_type operator[](difference_type) const noexcept {return this->operator*();} + + // These operators do nothing. + empty_iterator& operator++() noexcept {return *this;} + empty_iterator operator++(int) noexcept {return *this;} + empty_iterator& operator--() noexcept {return *this;} + empty_iterator operator--(int) noexcept {return *this;} + + empty_iterator& operator+=(difference_type) noexcept {return *this;} + empty_iterator& operator-=(difference_type) noexcept {return *this;} + + empty_iterator operator+(difference_type) const noexcept {return *this;} + empty_iterator operator-(difference_type) const noexcept {return *this;} +}; + +template +bool operator==(const empty_iterator&, const empty_iterator&) noexcept {return true;} +template +bool operator!=(const empty_iterator&, const empty_iterator&) noexcept {return false;} +template +bool operator< (const empty_iterator&, const empty_iterator&) noexcept {return false;} +template +bool operator<=(const empty_iterator&, const empty_iterator&) noexcept {return true;} +template +bool operator> (const empty_iterator&, const empty_iterator&) noexcept {return false;} +template +bool operator>=(const empty_iterator&, const empty_iterator&) noexcept {return true;} + +template +typename empty_iterator::difference_type +operator-(const empty_iterator&, const empty_iterator&) noexcept {return 0;} + +template +empty_iterator +operator+(typename empty_iterator::difference_type, const empty_iterator& rhs) noexcept {return rhs;} +template +empty_iterator +operator+(const empty_iterator& lhs, typename empty_iterator::difference_type) noexcept {return lhs;} + +} // detail + +// The default comment type. It discards all the comments. It requires only one +// byte to contain, so the memory footprint is smaller than preserve_comments. +// +// It just ignores `push_back`, `insert`, `erase`, and any other modifications. +// IT always returns size() == 0, the iterator taken by `begin()` is always the +// same as that of `end()`, and accessing through `operator[]` or iterators +// always causes a segmentation fault. DO NOT access to the element of this. +// +// Why this is chose as the default type is because the last version (2.x.y) +// does not contain any comments in a value. To minimize the impact on the +// efficiency, this is chosen as a default. +// +// To reduce the memory footprint, later we can try empty base optimization (EBO). +class discard_comments +{ + public: + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using value_type = std::string; + using reference = std::string&; + using const_reference = std::string const&; + using pointer = std::string*; + using const_pointer = std::string const*; + using iterator = detail::empty_iterator; + using const_iterator = detail::empty_iterator; + using reverse_iterator = detail::empty_iterator; + using const_reverse_iterator = detail::empty_iterator; + + public: + discard_comments() = default; + ~discard_comments() = default; + discard_comments(discard_comments const&) = default; + discard_comments(discard_comments &&) = default; + discard_comments& operator=(discard_comments const&) = default; + discard_comments& operator=(discard_comments &&) = default; + + explicit discard_comments(const std::vector&) noexcept {} + explicit discard_comments(std::vector&&) noexcept {} + discard_comments& operator=(const std::vector&) noexcept {return *this;} + discard_comments& operator=(std::vector&&) noexcept {return *this;} + + explicit discard_comments(const preserve_comments&) noexcept {} + + explicit discard_comments(size_type) noexcept {} + discard_comments(size_type, const std::string&) noexcept {} + discard_comments(std::initializer_list) noexcept {} + template + discard_comments(InputIterator, InputIterator) noexcept {} + + template + void assign(InputIterator, InputIterator) noexcept {} + void assign(std::initializer_list) noexcept {} + void assign(size_type, const std::string&) noexcept {} + + iterator insert(const_iterator, const std::string&) {return iterator{};} + iterator insert(const_iterator, std::string&&) {return iterator{};} + iterator insert(const_iterator, size_type, const std::string&) {return iterator{};} + template + iterator insert(const_iterator, InputIterator, InputIterator) {return iterator{};} + iterator insert(const_iterator, std::initializer_list) {return iterator{};} + + template + iterator emplace(const_iterator, Ts&& ...) {return iterator{};} + iterator erase(const_iterator) {return iterator{};} + iterator erase(const_iterator, const_iterator) {return iterator{};} + + void swap(discard_comments&) {return;} + + void push_back(const std::string&) {return;} + void push_back(std::string&& ) {return;} + void pop_back() {return;} + + template + void emplace_back(Ts&& ...) {return;} + + void clear() {return;} + + size_type size() const noexcept {return 0;} + size_type max_size() const noexcept {return 0;} + size_type capacity() const noexcept {return 0;} + bool empty() const noexcept {return true;} + + void reserve(size_type) {return;} + void resize(size_type) {return;} + void resize(size_type, const std::string&) {return;} + void shrink_to_fit() {return;} + + // DO NOT access to the element of this container. This container is always + // empty, so accessing through operator[], front/back, data causes address + // error. + + reference operator[](const size_type) noexcept {never_call("toml::discard_comment::operator[]");} + const_reference operator[](const size_type) const noexcept {never_call("toml::discard_comment::operator[]");} + reference at(const size_type) {throw std::out_of_range("toml::discard_comment is always empty.");} + const_reference at(const size_type) const {throw std::out_of_range("toml::discard_comment is always empty.");} + reference front() noexcept {never_call("toml::discard_comment::front");} + const_reference front() const noexcept {never_call("toml::discard_comment::front");} + reference back() noexcept {never_call("toml::discard_comment::back");} + const_reference back() const noexcept {never_call("toml::discard_comment::back");} + + pointer data() noexcept {return nullptr;} + const_pointer data() const noexcept {return nullptr;} + + iterator begin() noexcept {return iterator{};} + iterator end() noexcept {return iterator{};} + const_iterator begin() const noexcept {return const_iterator{};} + const_iterator end() const noexcept {return const_iterator{};} + const_iterator cbegin() const noexcept {return const_iterator{};} + const_iterator cend() const noexcept {return const_iterator{};} + + reverse_iterator rbegin() noexcept {return iterator{};} + reverse_iterator rend() noexcept {return iterator{};} + const_reverse_iterator rbegin() const noexcept {return const_iterator{};} + const_reverse_iterator rend() const noexcept {return const_iterator{};} + const_reverse_iterator crbegin() const noexcept {return const_iterator{};} + const_reverse_iterator crend() const noexcept {return const_iterator{};} + + private: + + [[noreturn]] static void never_call(const char *const this_function) + { +#if __has_builtin(__builtin_unreachable) + __builtin_unreachable(); +#endif + throw std::logic_error{this_function}; + } +}; + +inline bool operator==(const discard_comments&, const discard_comments&) noexcept {return true;} +inline bool operator!=(const discard_comments&, const discard_comments&) noexcept {return false;} +inline bool operator< (const discard_comments&, const discard_comments&) noexcept {return false;} +inline bool operator<=(const discard_comments&, const discard_comments&) noexcept {return true;} +inline bool operator> (const discard_comments&, const discard_comments&) noexcept {return false;} +inline bool operator>=(const discard_comments&, const discard_comments&) noexcept {return true;} + +inline void swap(const discard_comments&, const discard_comments&) noexcept {return;} + +inline std::ostream& operator<<(std::ostream& os, const discard_comments&) {return os;} + +} // toml11 +#endif // TOML11_COMMENTS_FWD_HPP diff --git a/include/toml11/fwd/datetime_fwd.hpp b/include/toml11/fwd/datetime_fwd.hpp new file mode 100644 index 0000000..44616a1 --- /dev/null +++ b/include/toml11/fwd/datetime_fwd.hpp @@ -0,0 +1,261 @@ +#ifndef TOML11_DATETIME_FWD_HPP +#define TOML11_DATETIME_FWD_HPP + +#include +#include +#include + +#include +#include +#include + +namespace toml +{ + +enum class month_t : std::uint8_t +{ + Jan = 0, + Feb = 1, + Mar = 2, + Apr = 3, + May = 4, + Jun = 5, + Jul = 6, + Aug = 7, + Sep = 8, + Oct = 9, + Nov = 10, + Dec = 11 +}; + +// ---------------------------------------------------------------------------- + +struct local_date +{ + std::int16_t year{0}; // A.D. (like, 2018) + std::uint8_t month{0}; // [0, 11] + std::uint8_t day{0}; // [1, 31] + + local_date(int y, month_t m, int d) + : year {static_cast(y)}, + month{static_cast(m)}, + day {static_cast(d)} + {} + + explicit local_date(const std::tm& t) + : year {static_cast(t.tm_year + 1900)}, + month{static_cast(t.tm_mon)}, + day {static_cast(t.tm_mday)} + {} + + explicit local_date(const std::chrono::system_clock::time_point& tp); + explicit local_date(const std::time_t t); + + operator std::chrono::system_clock::time_point() const; + operator std::time_t() const; + + local_date() = default; + ~local_date() = default; + local_date(local_date const&) = default; + local_date(local_date&&) = default; + local_date& operator=(local_date const&) = default; + local_date& operator=(local_date&&) = default; +}; +bool operator==(const local_date& lhs, const local_date& rhs); +bool operator!=(const local_date& lhs, const local_date& rhs); +bool operator< (const local_date& lhs, const local_date& rhs); +bool operator<=(const local_date& lhs, const local_date& rhs); +bool operator> (const local_date& lhs, const local_date& rhs); +bool operator>=(const local_date& lhs, const local_date& rhs); + +std::ostream& operator<<(std::ostream& os, const local_date& date); +std::string to_string(const local_date& date); + +// ----------------------------------------------------------------------------- + +struct local_time +{ + std::uint8_t hour{0}; // [0, 23] + std::uint8_t minute{0}; // [0, 59] + std::uint8_t second{0}; // [0, 60] + std::uint16_t millisecond{0}; // [0, 999] + std::uint16_t microsecond{0}; // [0, 999] + std::uint16_t nanosecond{0}; // [0, 999] + + local_time(int h, int m, int s, + int ms = 0, int us = 0, int ns = 0) + : hour {static_cast(h)}, + minute{static_cast(m)}, + second{static_cast(s)}, + millisecond{static_cast(ms)}, + microsecond{static_cast(us)}, + nanosecond {static_cast(ns)} + {} + + explicit local_time(const std::tm& t) + : hour {static_cast(t.tm_hour)}, + minute{static_cast(t.tm_min )}, + second{static_cast(t.tm_sec )}, + millisecond{0}, microsecond{0}, nanosecond{0} + {} + + template + explicit local_time(const std::chrono::duration& t) + { + const auto h = std::chrono::duration_cast(t); + this->hour = static_cast(h.count()); + const auto t2 = t - h; + const auto m = std::chrono::duration_cast(t2); + this->minute = static_cast(m.count()); + const auto t3 = t2 - m; + const auto s = std::chrono::duration_cast(t3); + this->second = static_cast(s.count()); + const auto t4 = t3 - s; + const auto ms = std::chrono::duration_cast(t4); + this->millisecond = static_cast(ms.count()); + const auto t5 = t4 - ms; + const auto us = std::chrono::duration_cast(t5); + this->microsecond = static_cast(us.count()); + const auto t6 = t5 - us; + const auto ns = std::chrono::duration_cast(t6); + this->nanosecond = static_cast(ns.count()); + } + + operator std::chrono::nanoseconds() const; + + local_time() = default; + ~local_time() = default; + local_time(local_time const&) = default; + local_time(local_time&&) = default; + local_time& operator=(local_time const&) = default; + local_time& operator=(local_time&&) = default; +}; + +bool operator==(const local_time& lhs, const local_time& rhs); +bool operator!=(const local_time& lhs, const local_time& rhs); +bool operator< (const local_time& lhs, const local_time& rhs); +bool operator<=(const local_time& lhs, const local_time& rhs); +bool operator> (const local_time& lhs, const local_time& rhs); +bool operator>=(const local_time& lhs, const local_time& rhs); + +std::ostream& operator<<(std::ostream& os, const local_time& time); +std::string to_string(const local_time& time); + +// ---------------------------------------------------------------------------- + +struct time_offset +{ + std::int8_t hour{0}; // [-12, 12] + std::int8_t minute{0}; // [-59, 59] + + time_offset(int h, int m) + : hour {static_cast(h)}, + minute{static_cast(m)} + {} + + operator std::chrono::minutes() const; + + time_offset() = default; + ~time_offset() = default; + time_offset(time_offset const&) = default; + time_offset(time_offset&&) = default; + time_offset& operator=(time_offset const&) = default; + time_offset& operator=(time_offset&&) = default; +}; + +bool operator==(const time_offset& lhs, const time_offset& rhs); +bool operator!=(const time_offset& lhs, const time_offset& rhs); +bool operator< (const time_offset& lhs, const time_offset& rhs); +bool operator<=(const time_offset& lhs, const time_offset& rhs); +bool operator> (const time_offset& lhs, const time_offset& rhs); +bool operator>=(const time_offset& lhs, const time_offset& rhs); + +std::ostream& operator<<(std::ostream& os, const time_offset& offset); + +std::string to_string(const time_offset& offset); + +// ----------------------------------------------------------------------------- + +struct local_datetime +{ + local_date date{}; + local_time time{}; + + local_datetime(local_date d, local_time t): date{d}, time{t} {} + + explicit local_datetime(const std::tm& t): date{t}, time{t}{} + + explicit local_datetime(const std::chrono::system_clock::time_point& tp); + explicit local_datetime(const std::time_t t); + + operator std::chrono::system_clock::time_point() const; + operator std::time_t() const; + + local_datetime() = default; + ~local_datetime() = default; + local_datetime(local_datetime const&) = default; + local_datetime(local_datetime&&) = default; + local_datetime& operator=(local_datetime const&) = default; + local_datetime& operator=(local_datetime&&) = default; +}; + +bool operator==(const local_datetime& lhs, const local_datetime& rhs); +bool operator!=(const local_datetime& lhs, const local_datetime& rhs); +bool operator< (const local_datetime& lhs, const local_datetime& rhs); +bool operator<=(const local_datetime& lhs, const local_datetime& rhs); +bool operator> (const local_datetime& lhs, const local_datetime& rhs); +bool operator>=(const local_datetime& lhs, const local_datetime& rhs); + +std::ostream& operator<<(std::ostream& os, const local_datetime& dt); + +std::string to_string(const local_datetime& dt); + +// ----------------------------------------------------------------------------- + +struct offset_datetime +{ + local_date date{}; + local_time time{}; + time_offset offset{}; + + offset_datetime(local_date d, local_time t, time_offset o) + : date{d}, time{t}, offset{o} + {} + offset_datetime(const local_datetime& dt, time_offset o) + : date{dt.date}, time{dt.time}, offset{o} + {} + // use the current local timezone offset + explicit offset_datetime(const local_datetime& ld); + explicit offset_datetime(const std::chrono::system_clock::time_point& tp); + explicit offset_datetime(const std::time_t& t); + explicit offset_datetime(const std::tm& t); + + operator std::chrono::system_clock::time_point() const; + + operator std::time_t() const; + + offset_datetime() = default; + ~offset_datetime() = default; + offset_datetime(offset_datetime const&) = default; + offset_datetime(offset_datetime&&) = default; + offset_datetime& operator=(offset_datetime const&) = default; + offset_datetime& operator=(offset_datetime&&) = default; + + private: + + static time_offset get_local_offset(const std::time_t* tp); +}; + +bool operator==(const offset_datetime& lhs, const offset_datetime& rhs); +bool operator!=(const offset_datetime& lhs, const offset_datetime& rhs); +bool operator< (const offset_datetime& lhs, const offset_datetime& rhs); +bool operator<=(const offset_datetime& lhs, const offset_datetime& rhs); +bool operator> (const offset_datetime& lhs, const offset_datetime& rhs); +bool operator>=(const offset_datetime& lhs, const offset_datetime& rhs); + +std::ostream& operator<<(std::ostream& os, const offset_datetime& dt); + +std::string to_string(const offset_datetime& dt); + +}//toml +#endif // TOML11_DATETIME_FWD_HPP diff --git a/include/toml11/fwd/error_info_fwd.hpp b/include/toml11/fwd/error_info_fwd.hpp new file mode 100644 index 0000000..5b8600c --- /dev/null +++ b/include/toml11/fwd/error_info_fwd.hpp @@ -0,0 +1,97 @@ +#ifndef TOML11_ERROR_INFO_FWD_HPP +#define TOML11_ERROR_INFO_FWD_HPP + +#include "../source_location.hpp" +#include "../utility.hpp" + +namespace toml +{ + +// error info returned from parser. +struct error_info +{ + error_info(std::string t, source_location l, std::string m, std::string s = "") + : title_(std::move(t)), locations_{std::make_pair(std::move(l), std::move(m))}, + suffix_(std::move(s)) + {} + + error_info(std::string t, std::vector> l, + std::string s = "") + : title_(std::move(t)), locations_(std::move(l)), suffix_(std::move(s)) + {} + + std::string const& title() const noexcept {return title_;} + std::string & title() noexcept {return title_;} + + std::vector> const& + locations() const noexcept {return locations_;} + + void add_locations(source_location loc, std::string msg) noexcept + { + locations_.emplace_back(std::move(loc), std::move(msg)); + } + + std::string const& suffix() const noexcept {return suffix_;} + std::string & suffix() noexcept {return suffix_;} + + private: + + std::string title_; + std::vector> locations_; + std::string suffix_; // hint or something like that +}; + +// forward decl +template +class basic_value; + +namespace detail +{ +inline error_info make_error_info_rec(error_info e) +{ + return e; +} +inline error_info make_error_info_rec(error_info e, std::string s) +{ + e.suffix() = s; + return e; +} + +template +error_info make_error_info_rec(error_info e, + const basic_value& v, std::string msg, Ts&& ... tail); + +template +error_info make_error_info_rec(error_info e, + source_location loc, std::string msg, Ts&& ... tail) +{ + e.add_locations(std::move(loc), std::move(msg)); + return make_error_info_rec(std::move(e), std::forward(tail)...); +} + +} // detail + +template +error_info make_error_info( + std::string title, source_location loc, std::string msg, Ts&& ... tail) +{ + error_info ei(std::move(title), std::move(loc), std::move(msg)); + return detail::make_error_info_rec(ei, std::forward(tail) ... ); +} + +std::string format_error(const std::string& errkind, const error_info& err); +std::string format_error(const error_info& err); + +// for custom error message +template +std::string format_error(std::string title, + source_location loc, std::string msg, Ts&& ... tail) +{ + return format_error("", make_error_info(std::move(title), + std::move(loc), std::move(msg), std::forward(tail)...)); +} + +std::ostream& operator<<(std::ostream& os, const error_info& e); + +} // toml +#endif // TOML11_ERROR_INFO_FWD_HPP diff --git a/include/toml11/fwd/format_fwd.hpp b/include/toml11/fwd/format_fwd.hpp new file mode 100644 index 0000000..d478d96 --- /dev/null +++ b/include/toml11/fwd/format_fwd.hpp @@ -0,0 +1,250 @@ +#ifndef TOML11_FORMAT_FWD_HPP +#define TOML11_FORMAT_FWD_HPP + +#include +#include +#include + +#include +#include + +namespace toml +{ + +// toml types with serialization info + +enum class indent_char : std::uint8_t +{ + space, // use space + tab, // use tab + none // no indent +}; + +std::ostream& operator<<(std::ostream& os, const indent_char& c); +std::string to_string(const indent_char c); + +// ---------------------------------------------------------------------------- +// boolean + +struct boolean_format_info +{ + // nothing, for now +}; + +inline bool operator==(const boolean_format_info&, const boolean_format_info&) noexcept +{ + return true; +} +inline bool operator!=(const boolean_format_info&, const boolean_format_info&) noexcept +{ + return false; +} + +// ---------------------------------------------------------------------------- +// integer + +enum class integer_format : std::uint8_t +{ + dec = 0, + bin = 1, + oct = 2, + hex = 3, +}; + +std::ostream& operator<<(std::ostream& os, const integer_format f); +std::string to_string(const integer_format); + +struct integer_format_info +{ + integer_format fmt = integer_format::dec; + bool uppercase = true; // hex with uppercase + std::size_t width = 0; // minimal width (may exceed) + std::size_t spacer = 0; // position of `_` (if 0, no spacer) + std::string suffix = ""; // _suffix (library extension) +}; + +bool operator==(const integer_format_info&, const integer_format_info&) noexcept; +bool operator!=(const integer_format_info&, const integer_format_info&) noexcept; + +// ---------------------------------------------------------------------------- +// floating + +enum class floating_format : std::uint8_t +{ + defaultfloat = 0, + fixed = 1, // does not include exponential part + scientific = 2, // always include exponential part + hex = 3 // hexfloat extension +}; + +std::ostream& operator<<(std::ostream& os, const floating_format f); +std::string to_string(const floating_format); + +struct floating_format_info +{ + floating_format fmt = floating_format::defaultfloat; + std::size_t prec = 0; // precision (if 0, use the default) + std::string suffix = ""; // 1.0e+2_suffix (library extension) +}; + +bool operator==(const floating_format_info&, const floating_format_info&) noexcept; +bool operator!=(const floating_format_info&, const floating_format_info&) noexcept; + +// ---------------------------------------------------------------------------- +// string + +enum class string_format : std::uint8_t +{ + basic = 0, + literal = 1, + multiline_basic = 2, + multiline_literal = 3 +}; + +std::ostream& operator<<(std::ostream& os, const string_format f); +std::string to_string(const string_format); + +struct string_format_info +{ + string_format fmt = string_format::basic; + bool start_with_newline = false; +}; + +bool operator==(const string_format_info&, const string_format_info&) noexcept; +bool operator!=(const string_format_info&, const string_format_info&) noexcept; + +// ---------------------------------------------------------------------------- +// datetime + +enum class datetime_delimiter_kind : std::uint8_t +{ + upper_T = 0, + lower_t = 1, + space = 2, +}; +std::ostream& operator<<(std::ostream& os, const datetime_delimiter_kind d); +std::string to_string(const datetime_delimiter_kind); + +struct offset_datetime_format_info +{ + datetime_delimiter_kind delimiter = datetime_delimiter_kind::upper_T; + bool has_seconds = true; + std::size_t subsecond_precision = 6; // [us] +}; + +bool operator==(const offset_datetime_format_info&, const offset_datetime_format_info&) noexcept; +bool operator!=(const offset_datetime_format_info&, const offset_datetime_format_info&) noexcept; + +struct local_datetime_format_info +{ + datetime_delimiter_kind delimiter = datetime_delimiter_kind::upper_T; + bool has_seconds = true; + std::size_t subsecond_precision = 6; // [us] +}; + +bool operator==(const local_datetime_format_info&, const local_datetime_format_info&) noexcept; +bool operator!=(const local_datetime_format_info&, const local_datetime_format_info&) noexcept; + +struct local_date_format_info +{ + // nothing, for now +}; + +bool operator==(const local_date_format_info&, const local_date_format_info&) noexcept; +bool operator!=(const local_date_format_info&, const local_date_format_info&) noexcept; + +struct local_time_format_info +{ + bool has_seconds = true; + std::size_t subsecond_precision = 6; // [us] +}; + +bool operator==(const local_time_format_info&, const local_time_format_info&) noexcept; +bool operator!=(const local_time_format_info&, const local_time_format_info&) noexcept; + +// ---------------------------------------------------------------------------- +// array + +enum class array_format : std::uint8_t +{ + default_format = 0, + oneline = 1, + multiline = 2, + array_of_tables = 3 // [[format.in.this.way]] +}; + +std::ostream& operator<<(std::ostream& os, const array_format f); +std::string to_string(const array_format); + +struct array_format_info +{ + array_format fmt = array_format::default_format; + indent_char indent_type = indent_char::space; + std::int32_t body_indent = 4; // indent in case of multiline + std::int32_t closing_indent = 0; // indent of `]` +}; + +bool operator==(const array_format_info&, const array_format_info&) noexcept; +bool operator!=(const array_format_info&, const array_format_info&) noexcept; + +// ---------------------------------------------------------------------------- +// table + +enum class table_format : std::uint8_t +{ + multiline = 0, // [foo] \n bar = "baz" + oneline = 1, // foo = {bar = "baz"} + dotted = 2, // foo.bar = "baz" + multiline_oneline = 3, // foo = { \n bar = "baz" \n } + implicit = 4 // [x] defined by [x.y.z]. skip in serializer. +}; + +std::ostream& operator<<(std::ostream& os, const table_format f); +std::string to_string(const table_format); + +struct table_format_info +{ + table_format fmt = table_format::multiline; + indent_char indent_type = indent_char::space; + std::int32_t body_indent = 0; // indent of values + std::int32_t name_indent = 0; // indent of [table] + std::int32_t closing_indent = 0; // in case of {inline-table} +}; + +bool operator==(const table_format_info&, const table_format_info&) noexcept; +bool operator!=(const table_format_info&, const table_format_info&) noexcept; + +// ---------------------------------------------------------------------------- +// wrapper + +namespace detail +{ +template +struct value_with_format +{ + using value_type = T; + using format_type = F; + + value_with_format() = default; + ~value_with_format() = default; + value_with_format(const value_with_format&) = default; + value_with_format(value_with_format&&) = default; + value_with_format& operator=(const value_with_format&) = default; + value_with_format& operator=(value_with_format&&) = default; + + value_with_format(value_type v, format_type f) + : value{std::move(v)}, format{std::move(f)} + {} + + template + value_with_format(value_with_format other) + : value{std::move(other.value)}, format{std::move(other.format)} + {} + + value_type value; + format_type format; +}; +} // detail + +} // namespace toml +#endif // TOML11_FORMAT_FWD_HPP diff --git a/include/toml11/fwd/literal_fwd.hpp b/include/toml11/fwd/literal_fwd.hpp new file mode 100644 index 0000000..e46612c --- /dev/null +++ b/include/toml11/fwd/literal_fwd.hpp @@ -0,0 +1,33 @@ +#ifndef TOML11_LITERAL_FWD_HPP +#define TOML11_LITERAL_FWD_HPP + +#include "../location.hpp" +#include "../types.hpp" +#include "../version.hpp" // IWYU pragma: keep for TOML11_HAS_CHAR8_T + +namespace toml +{ + +namespace detail +{ +// implementation +::toml::value literal_internal_impl(location loc); +} // detail + +inline namespace literals +{ +inline namespace toml_literals +{ + +::toml::value operator"" _toml(const char* str, std::size_t len); + +#if defined(TOML11_HAS_CHAR8_T) +// value of u8"" literal has been changed from char to char8_t and char8_t is +// NOT compatible to char +::toml::value operator"" _toml(const char8_t* str, std::size_t len); +#endif + +} // toml_literals +} // literals +} // toml +#endif // TOML11_LITERAL_FWD_HPP diff --git a/include/toml11/fwd/location_fwd.hpp b/include/toml11/fwd/location_fwd.hpp new file mode 100644 index 0000000..395b96c --- /dev/null +++ b/include/toml11/fwd/location_fwd.hpp @@ -0,0 +1,149 @@ +#ifndef TOML11_LOCATION_FWD_HPP +#define TOML11_LOCATION_FWD_HPP + +#include "../result.hpp" + +#include +#include +#include + +namespace toml +{ +namespace detail +{ + +class region; // fwd decl + +// +// To represent where we are reading in the parse functions. +// Since it "points" somewhere in the input stream, the length is always 1. +// +class location +{ + public: + + using char_type = unsigned char; // must be unsigned + using container_type = std::vector; + using difference_type = typename container_type::difference_type; // to suppress sign-conversion warning + using source_ptr = std::shared_ptr; + + public: + + location(source_ptr src, std::string src_name) + : source_(std::move(src)), source_name_(std::move(src_name)), + location_(0), line_number_(1), column_number_(1) + {} + + location(const location&) = default; + location(location&&) = default; + location& operator=(const location&) = default; + location& operator=(location&&) = default; + ~location() = default; + + void advance(std::size_t n = 1) noexcept; + void retrace() noexcept; + + bool is_ok() const noexcept { return static_cast(this->source_); } + + bool eof() const noexcept; + char_type current() const; + + char_type peek(); + + std::size_t get_location() const noexcept + { + return this->location_; + } + + std::size_t line_number() const noexcept + { + return this->line_number_; + } + std::size_t column_number() const noexcept + { + return this->column_number_; + } + std::string get_line() const; + + source_ptr const& source() const noexcept {return this->source_;} + std::string const& source_name() const noexcept {return this->source_name_;} + + private: + + void advance_impl(const std::size_t n); + void retrace_impl(); + std::size_t calc_column_number() const noexcept; + + private: + + friend region; + + private: + + source_ptr source_; + std::string source_name_; + std::size_t location_; // std::vector<>::difference_type is signed + std::size_t line_number_; + std::size_t column_number_; +}; + +bool operator==(const location& lhs, const location& rhs) noexcept; +bool operator!=(const location& lhs, const location& rhs); + +location prev(const location& loc); +location next(const location& loc); +location make_temporary_location(const std::string& str) noexcept; + +template +result +find_if(const location& first, const location& last, const F& func) noexcept +{ + if(first.source() != last.source()) { return err(); } + if(first.get_location() >= last.get_location()) { return err(); } + + auto loc = first; + while(loc.get_location() != last.get_location()) + { + if(func(loc.current())) + { + return ok(loc); + } + loc.advance(); + } + return err(); +} + +template +result +rfind_if(location first, const location& last, const F& func) +{ + if(first.source() != last.source()) { return err(); } + if(first.get_location() >= last.get_location()) { return err(); } + + auto loc = last; + while(loc.get_location() != first.get_location()) + { + if(func(loc.current())) + { + return ok(loc); + } + loc.retrace(); + } + if(func(first.current())) + { + return ok(first); + } + return err(); +} + +result find(const location& first, const location& last, + const location::char_type val); +result rfind(const location& first, const location& last, + const location::char_type val); + +std::size_t count(const location& first, const location& last, + const location::char_type& c); + +} // detail +} // toml +#endif // TOML11_LOCATION_FWD_HPP diff --git a/include/toml11/fwd/region_fwd.hpp b/include/toml11/fwd/region_fwd.hpp new file mode 100644 index 0000000..04ce56f --- /dev/null +++ b/include/toml11/fwd/region_fwd.hpp @@ -0,0 +1,110 @@ +#ifndef TOML11_REGION_FWD_HPP +#define TOML11_REGION_FWD_HPP + +#include "../location.hpp" + +#include +#include + +#include + +namespace toml +{ +namespace detail +{ + +// +// To represent where is a toml::value defined, or where does an error occur. +// Stored in toml::value. source_location will be constructed based on this. +// +class region +{ + public: + + using char_type = location::char_type; + using container_type = location::container_type; + using difference_type = location::difference_type; + using source_ptr = location::source_ptr; + + using iterator = typename container_type::iterator; + using const_iterator = typename container_type::const_iterator; + + public: + + // a value that is constructed manually does not have input stream info + region() + : source_(nullptr), source_name_(""), length_(0), + first_(0), first_line_(0), first_column_(0), last_(0), last_line_(0), + last_column_(0) + {} + + // a value defined in [first, last). + // Those source must be the same. Instread, `region` does not make sense. + region(const location& first, const location& last); + + // shorthand of [loc, loc+1) + explicit region(const location& loc); + + ~region() = default; + region(const region&) = default; + region(region&&) = default; + region& operator=(const region&) = default; + region& operator=(region&&) = default; + + bool is_ok() const noexcept { return static_cast(this->source_); } + + operator bool() const noexcept { return this->is_ok(); } + + std::size_t length() const noexcept {return this->length_;} + + std::size_t first_line_number() const noexcept + { + return this->first_line_; + } + std::size_t first_column_number() const noexcept + { + return this->first_column_; + } + std::size_t last_line_number() const noexcept + { + return this->last_line_; + } + std::size_t last_column_number() const noexcept + { + return this->last_column_; + } + + char_type at(std::size_t i) const; + + const_iterator begin() const noexcept; + const_iterator end() const noexcept; + const_iterator cbegin() const noexcept; + const_iterator cend() const noexcept; + + std::string as_string() const; + std::vector> as_lines() const; + + source_ptr const& source() const noexcept {return this->source_;} + std::string const& source_name() const noexcept {return this->source_name_;} + + private: + + std::pair + take_line(const_iterator begin, const_iterator end) const; + + private: + + source_ptr source_; + std::string source_name_; + std::size_t length_; + std::size_t first_; + std::size_t first_line_; + std::size_t first_column_; + std::size_t last_; + std::size_t last_line_; + std::size_t last_column_; +}; + +} // namespace detail +} // namespace toml +#endif // TOML11_REGION_FWD_HPP diff --git a/include/toml11/fwd/scanner_fwd.hpp b/include/toml11/fwd/scanner_fwd.hpp new file mode 100644 index 0000000..a886727 --- /dev/null +++ b/include/toml11/fwd/scanner_fwd.hpp @@ -0,0 +1,391 @@ +#ifndef TOML11_SCANNER_FWD_HPP +#define TOML11_SCANNER_FWD_HPP + +#include "../region.hpp" + +#include +#include +#include +#include + +#include +#include +#include + +namespace toml +{ +namespace detail +{ + +class scanner_base +{ + public: + virtual ~scanner_base() = default; + virtual region scan(location& loc) const = 0; + virtual scanner_base* clone() const = 0; + + // returns expected character or set of characters or literal. + // to show the error location, it changes loc (in `sequence`, especially). + virtual std::string expected_chars(location& loc) const = 0; + virtual std::string name() const = 0; +}; + +// make `scanner*` copyable +struct scanner_storage +{ + template>::value, + std::nullptr_t> = nullptr> + explicit scanner_storage(Scanner&& s) + : scanner_(cxx::make_unique>(std::forward(s))) + {} + ~scanner_storage() = default; + + scanner_storage(const scanner_storage& other); + scanner_storage& operator=(const scanner_storage& other); + scanner_storage(scanner_storage&&) = default; + scanner_storage& operator=(scanner_storage&&) = default; + + bool is_ok() const noexcept {return static_cast(scanner_);} + + region scan(location& loc) const; + + std::string expected_chars(location& loc) const; + + scanner_base& get() const noexcept; + + std::string name() const; + + private: + + std::unique_ptr scanner_; +}; + +// ---------------------------------------------------------------------------- + +class character final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit character(const char_type c) noexcept + : value_(c) + {} + ~character() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location&) const override; + + scanner_base* clone() const override; + + std::string name() const override; + + private: + char_type value_; +}; + +// ---------------------------------------------------------------------------- + +class character_either final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit character_either(std::initializer_list cs) noexcept + : chars_(std::move(cs)) + { + assert(! this->chars_.empty()); + } + + template + explicit character_either(const char (&cs)[N]) noexcept + : chars_(N-1, '\0') + { + static_assert(N >= 1, ""); + for(std::size_t i=0; i+1 chars_; +}; + +// ---------------------------------------------------------------------------- + +class character_in_range final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit character_in_range(const char_type from, const char_type to) noexcept + : from_(from), to_(to) + {} + ~character_in_range() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location&) const override; + + scanner_base* clone() const override; + + std::string name() const override; + + private: + char_type from_; + char_type to_; +}; + +// ---------------------------------------------------------------------------- + +class literal final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + template + explicit literal(const char (&cs)[N]) noexcept + : value_(cs), size_(N-1) // remove null character at the end + {} + ~literal() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location&) const override; + + scanner_base* clone() const override; + + std::string name() const override; + + private: + const char* value_; + std::size_t size_; +}; + +// ---------------------------------------------------------------------------- + +class sequence final: public scanner_base +{ + public: + using char_type = location::char_type; + + public: + + template + explicit sequence(Ts&& ... args) + { + push_back_all(std::forward(args)...); + } + sequence(const sequence&) = default; + sequence(sequence&&) = default; + sequence& operator=(const sequence&) = default; + sequence& operator=(sequence&&) = default; + ~sequence() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location& loc) const override; + + scanner_base* clone() const override; + + template + void push_back(Scanner&& other_scanner) + { + this->others_.emplace_back(std::forward(other_scanner)); + } + + std::string name() const override; + + private: + + void push_back_all() + { + return; + } + template + void push_back_all(T&& head, Ts&& ... args) + { + others_.emplace_back(std::forward(head)); + push_back_all(std::forward(args)...); + return; + } + + private: + std::vector others_; +}; + +// ---------------------------------------------------------------------------- + +class either final: public scanner_base +{ + public: + using char_type = location::char_type; + + public: + + template + explicit either(Ts&& ... args) + { + push_back_all(std::forward(args)...); + } + either(const either&) = default; + either(either&&) = default; + either& operator=(const either&) = default; + either& operator=(either&&) = default; + ~either() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location& loc) const override; + + scanner_base* clone() const override; + + template + void push_back(Scanner&& other_scanner) + { + this->others_.emplace_back(std::forward(other_scanner)); + } + + std::string name() const override; + + private: + + void push_back_all() + { + return; + } + template + void push_back_all(T&& head, Ts&& ... args) + { + others_.emplace_back(std::forward(head)); + push_back_all(std::forward(args)...); + return; + } + + private: + std::vector others_; +}; + +// ---------------------------------------------------------------------------- + +class repeat_exact final: public scanner_base +{ + public: + using char_type = location::char_type; + + public: + + template + repeat_exact(const std::size_t length, Scanner&& other) + : length_(length), other_(std::forward(other)) + {} + repeat_exact(const repeat_exact&) = default; + repeat_exact(repeat_exact&&) = default; + repeat_exact& operator=(const repeat_exact&) = default; + repeat_exact& operator=(repeat_exact&&) = default; + ~repeat_exact() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location& loc) const override; + + scanner_base* clone() const override; + + std::string name() const override; + + private: + std::size_t length_; + scanner_storage other_; +}; + +// ---------------------------------------------------------------------------- + +class repeat_at_least final: public scanner_base +{ + public: + using char_type = location::char_type; + + public: + + template + repeat_at_least(const std::size_t length, Scanner&& s) + : length_(length), other_(std::forward(s)) + {} + repeat_at_least(const repeat_at_least&) = default; + repeat_at_least(repeat_at_least&&) = default; + repeat_at_least& operator=(const repeat_at_least&) = default; + repeat_at_least& operator=(repeat_at_least&&) = default; + ~repeat_at_least() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location& loc) const override; + + scanner_base* clone() const override; + + std::string name() const override; + + private: + std::size_t length_; + scanner_storage other_; +}; + +// ---------------------------------------------------------------------------- + +class maybe final: public scanner_base +{ + public: + using char_type = location::char_type; + + public: + + template + explicit maybe(Scanner&& s) + : other_(std::forward(s)) + {} + maybe(const maybe&) = default; + maybe(maybe&&) = default; + maybe& operator=(const maybe&) = default; + maybe& operator=(maybe&&) = default; + ~maybe() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location&) const override; + + scanner_base* clone() const override; + + std::string name() const override; + + private: + scanner_storage other_; +}; + +} // detail +} // toml +#endif // TOML11_SCANNER_FWD_HPP diff --git a/include/toml11/fwd/source_location_fwd.hpp b/include/toml11/fwd/source_location_fwd.hpp new file mode 100644 index 0000000..7022689 --- /dev/null +++ b/include/toml11/fwd/source_location_fwd.hpp @@ -0,0 +1,148 @@ +#ifndef TOML11_SOURCE_LOCATION_FWD_HPP +#define TOML11_SOURCE_LOCATION_FWD_HPP + +#include "../region.hpp" + +#include +#include +#include + +namespace toml +{ + +// +// A struct to contain location in a toml file. +// +// To reduce memory consumption, it omits unrelated parts of long lines. like: +// +// 1. one long line, short region +// ``` +// | +// 1 | ... "foo", "bar", baz, "qux", "foobar", ... +// | ^-- unknown value +// ``` +// 2. long region +// ``` +// | +// 1 | array = [ "foo", ... "bar" ] +// | ^^^^^^^^^^^^^^^^^^^^- in this array +// ``` +// 3. many lines +// | +// 1 | array = [ "foo", +// | ^^^^^^^^ +// | ... +// | ^^^ +// | +// 10 | , "bar"] +// | ^^^^^^^^- in this array +// ``` +// +struct source_location +{ + public: + + explicit source_location(const detail::region& r); + ~source_location() = default; + source_location(source_location const&) = default; + source_location(source_location &&) = default; + source_location& operator=(source_location const&) = default; + source_location& operator=(source_location &&) = default; + + bool is_ok() const noexcept {return this->is_ok_;} + std::size_t length() const noexcept {return this->length_;} + + std::size_t first_line_number() const noexcept {return this->first_line_;} + std::size_t first_column_number() const noexcept {return this->first_column_;} + std::size_t last_line_number() const noexcept {return this->last_line_;} + std::size_t last_column_number() const noexcept {return this->last_column_;} + + std::string const& file_name() const noexcept {return this->file_name_;} + + std::size_t num_lines() const noexcept {return this->line_str_.size();} + + std::string const& first_line() const; + std::string const& last_line() const; + + std::vector const& lines() const noexcept {return line_str_;} + + // for internal use + std::size_t first_column_offset() const noexcept {return this->first_offset_;} + std::size_t last_column_offset() const noexcept {return this->last_offset_;} + + private: + + bool is_ok_; + std::size_t first_line_; + std::size_t first_column_; // column num in the actual file + std::size_t first_offset_; // column num in the shown line + std::size_t last_line_; + std::size_t last_column_; // column num in the actual file + std::size_t last_offset_; // column num in the shown line + std::size_t length_; + std::string file_name_; + std::vector line_str_; +}; + +namespace detail +{ + +std::size_t integer_width_base10(std::size_t i) noexcept; + +inline std::size_t line_width() noexcept {return 0;} + +template +std::size_t line_width(const source_location& loc, const std::string& /*msg*/, + const Ts& ... tail) noexcept +{ + return (std::max)( + integer_width_base10(loc.last_line_number()), line_width(tail...)); +} + +std::ostringstream& +format_filename(std::ostringstream& oss, const source_location& loc); + +std::ostringstream& +format_empty_line(std::ostringstream& oss, const std::size_t lnw); + +std::ostringstream& format_line(std::ostringstream& oss, + const std::size_t lnw, const std::size_t linenum, const std::string& line); + +std::ostringstream& format_underline(std::ostringstream& oss, + const std::size_t lnw, const std::size_t col, const std::size_t len, + const std::string& msg); + +std::string format_location_impl(const std::size_t lnw, + const std::string& prev_fname, + const source_location& loc, const std::string& msg); + +inline std::string format_location_rec(const std::size_t, const std::string&) +{ + return ""; +} + +template +std::string format_location_rec(const std::size_t lnw, + const std::string& prev_fname, + const source_location& loc, const std::string& msg, + const Ts& ... tail) +{ + return format_location_impl(lnw, prev_fname, loc, msg) + + format_location_rec(lnw, loc.file_name(), tail...); +} + +} // namespace detail + +// format a location info without title +template +std::string format_location( + const source_location& loc, const std::string& msg, const Ts& ... tail) +{ + const auto lnw = detail::line_width(loc, msg, tail...); + + const std::string f(""); // at the 1st iteration, no prev_filename is given + return detail::format_location_rec(lnw, f, loc, msg, tail...); +} + +} // toml +#endif // TOML11_SOURCE_LOCATION_FWD_HPP diff --git a/include/toml11/fwd/syntax_fwd.hpp b/include/toml11/fwd/syntax_fwd.hpp new file mode 100644 index 0000000..3560821 --- /dev/null +++ b/include/toml11/fwd/syntax_fwd.hpp @@ -0,0 +1,357 @@ +#ifndef TOML11_SYNTAX_FWD_HPP +#define TOML11_SYNTAX_FWD_HPP + +#include "../scanner.hpp" +#include "../spec.hpp" + +namespace toml +{ +namespace detail +{ +namespace syntax +{ + +using char_type = location::char_type; + +// =========================================================================== +// UTF-8 + +// avoid redundant representation and out-of-unicode sequence + +character_in_range utf8_1byte (const spec&); +sequence utf8_2bytes(const spec&); +sequence utf8_3bytes(const spec&); +sequence utf8_4bytes(const spec&); + +class non_ascii final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit non_ascii(const spec& s) noexcept; + ~non_ascii() override = default; + + region scan(location& loc) const override + { + return scanner_.scan(loc); + } + + std::string expected_chars(location&) const override + { + return "non-ascii utf-8 bytes"; + } + + scanner_base* clone() const override + { + return new non_ascii(*this); + } + + std::string name() const override + { + return "non_ascii"; + } + + private: + + either scanner_; +}; + +// =========================================================================== +// Whitespace + +character_either wschar(const spec&); + +repeat_at_least ws(const spec& s); + +// =========================================================================== +// Newline + +either newline(const spec&); + +// =========================================================================== +// Comments + +either allowed_comment_char(const spec& s); + +// XXX Note that it does not take newline +sequence comment(const spec& s); + +// =========================================================================== +// Boolean + +either boolean(const spec&); + +// =========================================================================== +// Integer + +class digit final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit digit(const spec&) noexcept; + ~digit() override = default; + + region scan(location& loc) const override + { + return scanner_.scan(loc); + } + + std::string expected_chars(location&) const override + { + return "digit [0-9]"; + } + + scanner_base* clone() const override + { + return new digit(*this); + } + + std::string name() const override + { + return "digit"; + } + + private: + + character_in_range scanner_; +}; + +class alpha final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit alpha(const spec&) noexcept; + ~alpha() override = default; + + region scan(location& loc) const override + { + return scanner_.scan(loc); + } + + std::string expected_chars(location&) const override + { + return "alpha [a-zA-Z]"; + } + + scanner_base* clone() const override + { + return new alpha(*this); + } + + std::string name() const override + { + return "alpha"; + } + + private: + + either scanner_; +}; + +class hexdig final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit hexdig(const spec& s) noexcept; + ~hexdig() override = default; + + region scan(location& loc) const override + { + return scanner_.scan(loc); + } + + std::string expected_chars(location&) const override + { + return "hex [0-9a-fA-F]"; + } + + scanner_base* clone() const override + { + return new hexdig(*this); + } + + std::string name() const override + { + return "hexdig"; + } + + private: + + either scanner_; +}; + +sequence num_suffix(const spec& s); + +sequence dec_int(const spec& s); +sequence hex_int(const spec& s); +sequence oct_int(const spec&); +sequence bin_int(const spec&); +either integer(const spec& s); + +// =========================================================================== +// Floating + +sequence zero_prefixable_int(const spec& s); +sequence fractional_part(const spec& s); +sequence exponent_part(const spec& s); +sequence hex_floating(const spec& s); +either floating(const spec& s); + +// =========================================================================== +// Datetime + +sequence local_date(const spec& s); +sequence local_time(const spec& s); +either time_offset(const spec& s); +sequence full_time(const spec& s); +character_either time_delim(const spec&); +sequence local_datetime(const spec& s); +sequence offset_datetime(const spec& s); + +// =========================================================================== +// String + +sequence escaped(const spec& s); + +either basic_char(const spec& s); + +sequence basic_string(const spec& s); + +// --------------------------------------------------------------------------- +// multiline string + +sequence escaped_newline(const spec& s); +sequence ml_basic_string(const spec& s); + +// --------------------------------------------------------------------------- +// literal string + +either literal_char(const spec& s); +sequence literal_string(const spec& s); + +sequence ml_literal_string(const spec& s); + +either string(const spec& s); + +// =========================================================================== +// Keys + +// to keep `expected_chars` simple +class non_ascii_key_char final : public scanner_base +{ + public: + + using char_type = location::char_type; + + private: + + using in_range = character_in_range; // make definition short + + public: + + explicit non_ascii_key_char(const spec& s) noexcept; + ~non_ascii_key_char() override = default; + + region scan(location& loc) const override; + + std::string expected_chars(location&) const override + { + return "bare key non-ASCII script"; + } + + scanner_base* clone() const override + { + return new non_ascii_key_char(*this); + } + + std::string name() const override + { + return "non-ASCII bare key"; + } + + private: + + std::uint32_t read_utf8(location& loc) const; +}; + + +repeat_at_least unquoted_key(const spec& s); + +either quoted_key(const spec& s); + +either simple_key(const spec& s); + +sequence dot_sep(const spec& s); + +sequence dotted_key(const spec& s); + + +class key final : public scanner_base +{ + public: + + using char_type = location::char_type; + + public: + + explicit key(const spec& s) noexcept; + ~key() override = default; + + region scan(location& loc) const override + { + return scanner_.scan(loc); + } + + std::string expected_chars(location&) const override + { + return "basic key([a-zA-Z0-9_-]) or quoted key(\" or ')"; + } + + scanner_base* clone() const override + { + return new key(*this); + } + + std::string name() const override + { + return "key"; + } + + private: + + either scanner_; +}; + +sequence keyval_sep(const spec& s); + +// =========================================================================== +// Table key + +sequence std_table(const spec& s); + +sequence array_table(const spec& s); + +// =========================================================================== +// extension: null + +literal null_value(const spec&); + +} // namespace syntax +} // namespace detail +} // namespace toml +#endif // TOML11_SYNTAX_FWD_HPP diff --git a/include/toml11/fwd/value_t_fwd.hpp b/include/toml11/fwd/value_t_fwd.hpp new file mode 100644 index 0000000..fddf843 --- /dev/null +++ b/include/toml11/fwd/value_t_fwd.hpp @@ -0,0 +1,117 @@ +#ifndef TOML11_VALUE_T_FWD_HPP +#define TOML11_VALUE_T_FWD_HPP + +#include "../compat.hpp" +#include "../format.hpp" + +#include +#include +#include + +#include + +namespace toml +{ + +// forward decl +template +class basic_value; + +// ---------------------------------------------------------------------------- +// enum representing toml types + +enum class value_t : std::uint8_t +{ + empty = 0, + boolean = 1, + integer = 2, + floating = 3, + string = 4, + offset_datetime = 5, + local_datetime = 6, + local_date = 7, + local_time = 8, + array = 9, + table = 10 +}; + +std::ostream& operator<<(std::ostream& os, value_t t); +std::string to_string(value_t t); + + +// ---------------------------------------------------------------------------- +// meta functions for internal use + +namespace detail +{ + +template +using value_t_constant = std::integral_constant; + +template +struct type_to_enum : value_t_constant {}; + +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; +template struct type_to_enum : value_t_constant {}; + +template +struct enum_to_type { using type = void; }; + +template struct enum_to_type { using type = typename V::boolean_type ; }; +template struct enum_to_type { using type = typename V::integer_type ; }; +template struct enum_to_type { using type = typename V::floating_type ; }; +template struct enum_to_type { using type = typename V::string_type ; }; +template struct enum_to_type { using type = typename V::offset_datetime_type; }; +template struct enum_to_type { using type = typename V::local_datetime_type ; }; +template struct enum_to_type { using type = typename V::local_date_type ; }; +template struct enum_to_type { using type = typename V::local_time_type ; }; +template struct enum_to_type { using type = typename V::array_type ; }; +template struct enum_to_type { using type = typename V::table_type ; }; + +template +using enum_to_type_t = typename enum_to_type::type; + +template +struct enum_to_fmt_type { using type = void; }; + +template<> struct enum_to_fmt_type { using type = boolean_format_info ; }; +template<> struct enum_to_fmt_type { using type = integer_format_info ; }; +template<> struct enum_to_fmt_type { using type = floating_format_info ; }; +template<> struct enum_to_fmt_type { using type = string_format_info ; }; +template<> struct enum_to_fmt_type { using type = offset_datetime_format_info; }; +template<> struct enum_to_fmt_type { using type = local_datetime_format_info ; }; +template<> struct enum_to_fmt_type { using type = local_date_format_info ; }; +template<> struct enum_to_fmt_type { using type = local_time_format_info ; }; +template<> struct enum_to_fmt_type { using type = array_format_info ; }; +template<> struct enum_to_fmt_type { using type = table_format_info ; }; + +template +using enum_to_fmt_type_t = typename enum_to_fmt_type::type; + +template +struct is_exact_toml_type0 : cxx::disjunction< + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same, + std::is_same + >{}; +template struct is_exact_toml_type: is_exact_toml_type0, V> {}; +template struct is_not_toml_type : cxx::negation> {}; + +} // namespace detail +} // namespace toml +#endif // TOML11_VALUE_T_FWD_HPP diff --git a/include/toml11/get.hpp b/include/toml11/get.hpp new file mode 100644 index 0000000..3805d09 --- /dev/null +++ b/include/toml11/get.hpp @@ -0,0 +1,632 @@ +#ifndef TOML11_GET_HPP +#define TOML11_GET_HPP + +#include + +#include "from.hpp" +#include "types.hpp" +#include "value.hpp" + +#if defined(TOML11_HAS_STRING_VIEW) +#include +#endif // string_view + +namespace toml +{ + +// ============================================================================ +// T is toml::value; identity transformation. + +template +cxx::enable_if_t>::value, T>& +get(basic_value& v) +{ + return v; +} + +template +cxx::enable_if_t>::value, T> const& +get(const basic_value& v) +{ + return v; +} + +template +cxx::enable_if_t>::value, T> +get(basic_value&& v) +{ + return basic_value(std::move(v)); +} + +// ============================================================================ +// exact toml::* type + +template +cxx::enable_if_t>::value, T> & +get(basic_value& v) +{ + constexpr auto ty = detail::type_to_enum>::value; + return detail::getter::get(v); +} + +template +cxx::enable_if_t>::value, T> const& +get(const basic_value& v) +{ + constexpr auto ty = detail::type_to_enum>::value; + return detail::getter::get(v); +} + +template +cxx::enable_if_t>::value, T> +get(basic_value&& v) +{ + constexpr auto ty = detail::type_to_enum>::value; + return detail::getter::get(std::move(v)); +} + +// ============================================================================ +// T is toml::basic_value + +template +cxx::enable_if_t, + cxx::negation>> + >::value, T> +get(basic_value v) +{ + return T(std::move(v)); +} + +// ============================================================================ +// integer convertible from toml::value::integer_type + +template +cxx::enable_if_t, + cxx::negation>, + detail::is_not_toml_type>, + cxx::negation>, + cxx::negation> + >::value, T> +get(const basic_value& v) +{ + return static_cast(v.as_integer()); +} + +// ============================================================================ +// floating point convertible from toml::value::floating_type + +template +cxx::enable_if_t, + detail::is_not_toml_type>, + cxx::negation>, + cxx::negation> + >::value, T> +get(const basic_value& v) +{ + return static_cast(v.as_floating()); +} + +// ============================================================================ +// std::string with different char/trait/allocator + +template +cxx::enable_if_t>, + detail::is_1byte_std_basic_string + >::value, T> +get(const basic_value& v) +{ + return detail::string_conv>(v.as_string()); +} + +// ============================================================================ +// std::string_view + +#if defined(TOML11_HAS_STRING_VIEW) + +template +cxx::enable_if_t::string_type>::value, T> +get(const basic_value& v) +{ + return T(v.as_string()); +} + +#endif // string_view + +// ============================================================================ +// std::chrono::duration from toml::local_time + +template +cxx::enable_if_t::value, T> +get(const basic_value& v) +{ + return std::chrono::duration_cast( + std::chrono::nanoseconds(v.as_local_time())); +} + +// ============================================================================ +// std::chrono::system_clock::time_point from toml::datetime variants + +template +cxx::enable_if_t< + std::is_same::value, T> +get(const basic_value& v) +{ + switch(v.type()) + { + case value_t::local_date: + { + return std::chrono::system_clock::time_point(v.as_local_date()); + } + case value_t::local_datetime: + { + return std::chrono::system_clock::time_point(v.as_local_datetime()); + } + case value_t::offset_datetime: + { + return std::chrono::system_clock::time_point(v.as_offset_datetime()); + } + default: + { + const auto loc = v.location(); + throw type_error(format_error("toml::get: " + "bad_cast to std::chrono::system_clock::time_point", loc, + "the actual type is " + to_string(v.type())), loc); + } + } +} + +// ============================================================================ +// forward declaration to use this recursively. ignore this and go ahead. + +// array-like (w/ push_back) +template +cxx::enable_if_t, // T is a container + detail::has_push_back_method, // .push_back() works + detail::is_not_toml_type>, // but not toml::array + cxx::negation>, // but not std::basic_string +#if defined(TOML11_HAS_STRING_VIEW) + cxx::negation>, // but not std::basic_string_view +#endif + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value&); + +// std::array +template +cxx::enable_if_t::value, T> +get(const basic_value&); + +// std::forward_list +template +cxx::enable_if_t::value, T> +get(const basic_value&); + +// std::pair +template +cxx::enable_if_t::value, T> +get(const basic_value&); + +// std::tuple +template +cxx::enable_if_t::value, T> +get(const basic_value&); + +// std::map (key is convertible from toml::value::key_type) +template +cxx::enable_if_t, // T is map + detail::is_not_toml_type>, // but not toml::table + std::is_convertible::key_type, + typename T::key_type>, // keys are convertible + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value& v); + +// std::map (key is not convertible from toml::value::key_type, but +// is a std::basic_string) +template +cxx::enable_if_t, // T is map + detail::is_not_toml_type>, // but not toml::table + cxx::negation::key_type, + typename T::key_type>>, // keys are NOT convertible + detail::is_1byte_std_basic_string, // is std::basic_string + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value& v); + +// toml::from::from_toml(v) +template +cxx::enable_if_t::value, T> +get(const basic_value&); + +// has T.from_toml(v) but no from +template +cxx::enable_if_t, // has T.from_toml() + cxx::negation>, // no toml::from + std::is_default_constructible // T{} works + >::value, T> +get(const basic_value&); + +// T(const toml::value&) and T is not toml::basic_value, +// and it does not have `from` nor `from_toml`. +template +cxx::enable_if_t&>, // has T(const basic_value&) + cxx::negation>, // but not basic_value itself + cxx::negation>, // no .from_toml() + cxx::negation> // no toml::from + >::value, T> +get(const basic_value&); + +// ============================================================================ +// array-like types; most likely STL container, like std::vector, etc. + +template +cxx::enable_if_t, // T is a container + detail::has_push_back_method, // .push_back() works + detail::is_not_toml_type>, // but not toml::array + cxx::negation>, // but not std::basic_string +#if defined(TOML11_HAS_STRING_VIEW) + cxx::negation>, // but not std::basic_string_view +#endif + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value& v) +{ + using value_type = typename T::value_type; + const auto& a = v.as_array(); + + T container; + detail::try_reserve(container, a.size()); // if T has .reserve(), call it + + for(const auto& elem : a) + { + container.push_back(get(elem)); + } + return container; +} + +// ============================================================================ +// std::array + +template +cxx::enable_if_t::value, T> +get(const basic_value& v) +{ + using value_type = typename T::value_type; + const auto& a = v.as_array(); + + T container; + if(a.size() != container.size()) + { + const auto loc = v.location(); + throw std::out_of_range(format_error("toml::get: while converting to an array: " + " array size is " + std::to_string(container.size()) + + " but there are " + std::to_string(a.size()) + " elements in toml array.", + loc, "here")); + } + for(std::size_t i=0; i(a.at(i)); + } + return container; +} + +// ============================================================================ +// std::forward_list + +template +cxx::enable_if_t::value, T> +get(const basic_value& v) +{ + using value_type = typename T::value_type; + + T container; + for(const auto& elem : v.as_array()) + { + container.push_front(get(elem)); + } + container.reverse(); + return container; +} + +// ============================================================================ +// std::pair + +template +cxx::enable_if_t::value, T> +get(const basic_value& v) +{ + using first_type = typename T::first_type; + using second_type = typename T::second_type; + + const auto& ar = v.as_array(); + if(ar.size() != 2) + { + const auto loc = v.location(); + throw std::out_of_range(format_error("toml::get: while converting std::pair: " + " but there are " + std::to_string(ar.size()) + " > 2 elements in toml array.", + loc, "here")); + } + return std::make_pair(::toml::get(ar.at(0)), + ::toml::get(ar.at(1))); +} + +// ============================================================================ +// std::tuple. + +namespace detail +{ +template +T get_tuple_impl(const Array& a, cxx::index_sequence) +{ + return std::make_tuple( + ::toml::get::type>(a.at(I))...); +} +} // detail + +template +cxx::enable_if_t::value, T> +get(const basic_value& v) +{ + const auto& ar = v.as_array(); + if(ar.size() != std::tuple_size::value) + { + const auto loc = v.location(); + throw std::out_of_range(format_error("toml::get: while converting std::tuple: " + " there are " + std::to_string(ar.size()) + " > " + + std::to_string(std::tuple_size::value) + " elements in toml array.", + loc, "here")); + } + return detail::get_tuple_impl(ar, + cxx::make_index_sequence::value>{}); +} + +// ============================================================================ +// map-like types; most likely STL map, like std::map or std::unordered_map. + +// key is convertible from toml::value::key_type +template +cxx::enable_if_t, // T is map + detail::is_not_toml_type>, // but not toml::table + std::is_convertible::key_type, + typename T::key_type>, // keys are convertible + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value& v) +{ + using key_type = typename T::key_type; + using mapped_type = typename T::mapped_type; + static_assert( + std::is_convertible::key_type, key_type>::value, + "toml::get only supports map type of which key_type is " + "convertible from toml::basic_value::key_type."); + + T m; + for(const auto& kv : v.as_table()) + { + m.emplace(key_type(kv.first), get(kv.second)); + } + return m; +} + +// key is NOT convertible from toml::value::key_type but std::basic_string +template +cxx::enable_if_t, // T is map + detail::is_not_toml_type>, // but not toml::table + cxx::negation::key_type, + typename T::key_type>>, // keys are NOT convertible + detail::is_1byte_std_basic_string, // is std::basic_string + cxx::negation>, // no T.from_toml() + cxx::negation>, // no toml::from + cxx::negation&>> + >::value, T> +get(const basic_value& v) +{ + using key_type = typename T::key_type; + using mapped_type = typename T::mapped_type; + + T m; + for(const auto& kv : v.as_table()) + { + m.emplace(detail::string_conv(kv.first), get(kv.second)); + } + return m; +} + +// ============================================================================ +// user-defined, but convertible types. + +// toml::from +template +cxx::enable_if_t::value, T> +get(const basic_value& v) +{ + return ::toml::from::from_toml(v); +} + +// has T.from_toml(v) but no from +template +cxx::enable_if_t, // has T.from_toml() + cxx::negation>, // no toml::from + std::is_default_constructible // T{} works + >::value, T> +get(const basic_value& v) +{ + T ud; + ud.from_toml(v); + return ud; +} + +// T(const toml::value&) and T is not toml::basic_value, +// and it does not have `from` nor `from_toml`. +template +cxx::enable_if_t&>, // has T(const basic_value&) + cxx::negation>, // but not basic_value itself + cxx::negation>, // no .from_toml() + cxx::negation> // no toml::from + >::value, T> +get(const basic_value& v) +{ + return T(v); +} + +// ============================================================================ +// get_or(value, fallback) + +template +cxx::enable_if_t::value, basic_value> const& +get_or(const basic_value& v, const basic_value&) +{ + return v; +} + +template +cxx::enable_if_t::value, basic_value>& +get_or(basic_value& v, basic_value&) +{ + return v; +} + +template +cxx::enable_if_t::value, basic_value> +get_or(basic_value&& v, basic_value&&) +{ + return v; +} + +// ---------------------------------------------------------------------------- +// specialization for the exact toml types (return type becomes lvalue ref) + +template +cxx::enable_if_t< + detail::is_exact_toml_type>::value, T> const& +get_or(const basic_value& v, const T& opt) noexcept +{ + try + { + return get>(v); + } + catch(...) + { + return opt; + } +} +template +cxx::enable_if_t>, + detail::is_exact_toml_type> + >::value, T>& +get_or(basic_value& v, T& opt) noexcept +{ + try + { + return get>(v); + } + catch(...) + { + return opt; + } +} +template +cxx::enable_if_t, + basic_value>::value, cxx::remove_cvref_t> +get_or(basic_value&& v, T&& opt) noexcept +{ + try + { + return get>(std::move(v)); + } + catch(...) + { + return cxx::remove_cvref_t(std::forward(opt)); + } +} + +// ---------------------------------------------------------------------------- +// specialization for string literal + +// template +// typename basic_value::string_type +// get_or(const basic_value& v, +// const typename basic_value::string_type::value_type (&opt)[N]) +// { +// try +// { +// return v.as_string(); +// } +// catch(...) +// { +// return typename basic_value::string_type(opt); +// } +// } +// +// The above only matches to the literal, like `get_or(v, "foo");` but not +// ```cpp +// const auto opt = "foo"; +// const auto str = get_or(v, opt); +// ``` +// . And the latter causes an error. +// To match to both `"foo"` and `const auto opt = "foo"`, we take a pointer to +// a character here. + +template +typename basic_value::string_type +get_or(const basic_value& v, + const typename basic_value::string_type::value_type* opt) +{ + try + { + return v.as_string(); + } + catch(...) + { + return typename basic_value::string_type(opt); + } +} + +// ---------------------------------------------------------------------------- +// others (require type conversion and return type cannot be lvalue reference) + +template +cxx::enable_if_t>, + cxx::negation>>, + cxx::negation, typename basic_value::string_type::value_type const*>> + >::value, cxx::remove_cvref_t> +get_or(const basic_value& v, T&& opt) +{ + try + { + return get>(v); + } + catch(...) + { + return cxx::remove_cvref_t(std::forward(opt)); + } +} + +} // toml +#endif // TOML11_GET_HPP diff --git a/include/toml11/impl/color_impl.hpp b/include/toml11/impl/color_impl.hpp new file mode 100644 index 0000000..4215587 --- /dev/null +++ b/include/toml11/impl/color_impl.hpp @@ -0,0 +1,76 @@ +#ifndef TOML11_COLOR_IMPL_HPP +#define TOML11_COLOR_IMPL_HPP + +#include "../fwd/color_fwd.hpp" +#include "../version.hpp" + +#include + +namespace toml +{ +namespace color +{ +// put ANSI escape sequence to ostream +inline namespace ansi +{ + +TOML11_INLINE std::ostream& reset(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[00m";} + return os; +} +TOML11_INLINE std::ostream& bold(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[01m";} + return os; +} +TOML11_INLINE std::ostream& grey(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[30m";} + return os; +} +TOML11_INLINE std::ostream& gray(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[30m";} + return os; +} +TOML11_INLINE std::ostream& red(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[31m";} + return os; +} +TOML11_INLINE std::ostream& green(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[32m";} + return os; +} +TOML11_INLINE std::ostream& yellow(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[33m";} + return os; +} +TOML11_INLINE std::ostream& blue(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[34m";} + return os; +} +TOML11_INLINE std::ostream& magenta(std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[35m";} + return os; +} +TOML11_INLINE std::ostream& cyan (std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[36m";} + return os; +} +TOML11_INLINE std::ostream& white (std::ostream& os) +{ + if(detail::color_status().should_color()) {os << "\033[37m";} + return os; +} + +} // ansi +} // color +} // toml +#endif // TOML11_COLOR_IMPL_HPP diff --git a/include/toml11/impl/comments_impl.hpp b/include/toml11/impl/comments_impl.hpp new file mode 100644 index 0000000..25023eb --- /dev/null +++ b/include/toml11/impl/comments_impl.hpp @@ -0,0 +1,46 @@ +#ifndef TOML11_COMMENTS_IMPL_HPP +#define TOML11_COMMENTS_IMPL_HPP + +#include "../fwd/comments_fwd.hpp" // IWYU pragma: keep + +namespace toml +{ + +TOML11_INLINE bool operator==(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments == rhs.comments;} +TOML11_INLINE bool operator!=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments != rhs.comments;} +TOML11_INLINE bool operator< (const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments < rhs.comments;} +TOML11_INLINE bool operator<=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments <= rhs.comments;} +TOML11_INLINE bool operator> (const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments > rhs.comments;} +TOML11_INLINE bool operator>=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments >= rhs.comments;} + +TOML11_INLINE void swap(preserve_comments& lhs, preserve_comments& rhs) +{ + lhs.swap(rhs); + return; +} +TOML11_INLINE void swap(preserve_comments& lhs, std::vector& rhs) +{ + lhs.comments.swap(rhs); + return; +} +TOML11_INLINE void swap(std::vector& lhs, preserve_comments& rhs) +{ + lhs.swap(rhs.comments); + return; +} + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const preserve_comments& com) +{ + for(const auto& c : com) + { + if(c.front() != '#') + { + os << '#'; + } + os << c << '\n'; + } + return os; +} + +} // toml11 +#endif // TOML11_COMMENTS_IMPL_HPP diff --git a/include/toml11/impl/datetime_impl.hpp b/include/toml11/impl/datetime_impl.hpp new file mode 100644 index 0000000..50c0455 --- /dev/null +++ b/include/toml11/impl/datetime_impl.hpp @@ -0,0 +1,518 @@ +#ifndef TOML11_DATETIME_IMPL_HPP +#define TOML11_DATETIME_IMPL_HPP + +#include "../fwd/datetime_fwd.hpp" +#include "../version.hpp" + +#include +#include +#include +#include +#include + +#include +#include + +namespace toml +{ + +// To avoid non-threadsafe std::localtime. In C11 (not C++11!), localtime_s is +// provided in the absolutely same purpose, but C++11 is actually not compatible +// with C11. We need to dispatch the function depending on the OS. +namespace detail +{ +// TODO: find more sophisticated way to handle this +#if defined(_MSC_VER) +TOML11_INLINE std::tm localtime_s(const std::time_t* src) +{ + std::tm dst; + const auto result = ::localtime_s(&dst, src); + if (result) { throw std::runtime_error("localtime_s failed."); } + return dst; +} +TOML11_INLINE std::tm gmtime_s(const std::time_t* src) +{ + std::tm dst; + const auto result = ::gmtime_s(&dst, src); + if (result) { throw std::runtime_error("gmtime_s failed."); } + return dst; +} +#elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1) || defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_POSIX_SOURCE) +TOML11_INLINE std::tm localtime_s(const std::time_t* src) +{ + std::tm dst; + const auto result = ::localtime_r(src, &dst); + if (!result) { throw std::runtime_error("localtime_r failed."); } + return dst; +} +TOML11_INLINE std::tm gmtime_s(const std::time_t* src) +{ + std::tm dst; + const auto result = ::gmtime_r(src, &dst); + if (!result) { throw std::runtime_error("gmtime_r failed."); } + return dst; +} +#else // fallback. not threadsafe +TOML11_INLINE std::tm localtime_s(const std::time_t* src) +{ + const auto result = std::localtime(src); + if (!result) { throw std::runtime_error("localtime failed."); } + return *result; +} +TOML11_INLINE std::tm gmtime_s(const std::time_t* src) +{ + const auto result = std::gmtime(src); + if (!result) { throw std::runtime_error("gmtime failed."); } + return *result; +} +#endif +} // detail + +// ---------------------------------------------------------------------------- + +TOML11_INLINE local_date::local_date(const std::chrono::system_clock::time_point& tp) +{ + const auto t = std::chrono::system_clock::to_time_t(tp); + const auto time = detail::localtime_s(&t); + *this = local_date(time); +} + +TOML11_INLINE local_date::local_date(const std::time_t t) + : local_date{std::chrono::system_clock::from_time_t(t)} +{} + +TOML11_INLINE local_date::operator std::chrono::system_clock::time_point() const +{ + // std::mktime returns date as local time zone. no conversion needed + std::tm t; + t.tm_sec = 0; + t.tm_min = 0; + t.tm_hour = 0; + t.tm_mday = static_cast(this->day); + t.tm_mon = static_cast(this->month); + t.tm_year = static_cast(this->year) - 1900; + t.tm_wday = 0; // the value will be ignored + t.tm_yday = 0; // the value will be ignored + t.tm_isdst = -1; + return std::chrono::system_clock::from_time_t(std::mktime(&t)); +} + +TOML11_INLINE local_date::operator std::time_t() const +{ + return std::chrono::system_clock::to_time_t( + std::chrono::system_clock::time_point(*this)); +} + +TOML11_INLINE bool operator==(const local_date& lhs, const local_date& rhs) +{ + return std::make_tuple(lhs.year, lhs.month, lhs.day) == + std::make_tuple(rhs.year, rhs.month, rhs.day); +} +TOML11_INLINE bool operator!=(const local_date& lhs, const local_date& rhs) +{ + return !(lhs == rhs); +} +TOML11_INLINE bool operator< (const local_date& lhs, const local_date& rhs) +{ + return std::make_tuple(lhs.year, lhs.month, lhs.day) < + std::make_tuple(rhs.year, rhs.month, rhs.day); +} +TOML11_INLINE bool operator<=(const local_date& lhs, const local_date& rhs) +{ + return (lhs < rhs) || (lhs == rhs); +} +TOML11_INLINE bool operator> (const local_date& lhs, const local_date& rhs) +{ + return !(lhs <= rhs); +} +TOML11_INLINE bool operator>=(const local_date& lhs, const local_date& rhs) +{ + return !(lhs < rhs); +} + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const local_date& date) +{ + os << std::setfill('0') << std::setw(4) << static_cast(date.year ) << '-'; + os << std::setfill('0') << std::setw(2) << static_cast(date.month) + 1 << '-'; + os << std::setfill('0') << std::setw(2) << static_cast(date.day ) ; + return os; +} + +TOML11_INLINE std::string to_string(const local_date& date) +{ + std::ostringstream oss; + oss.imbue(std::locale::classic()); + oss << date; + return oss.str(); +} + +// ----------------------------------------------------------------------------- + +TOML11_INLINE local_time::operator std::chrono::nanoseconds() const +{ + return std::chrono::nanoseconds (this->nanosecond) + + std::chrono::microseconds(this->microsecond) + + std::chrono::milliseconds(this->millisecond) + + std::chrono::seconds(this->second) + + std::chrono::minutes(this->minute) + + std::chrono::hours(this->hour); +} + +TOML11_INLINE bool operator==(const local_time& lhs, const local_time& rhs) +{ + return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) == + std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond); +} +TOML11_INLINE bool operator!=(const local_time& lhs, const local_time& rhs) +{ + return !(lhs == rhs); +} +TOML11_INLINE bool operator< (const local_time& lhs, const local_time& rhs) +{ + return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) < + std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond); +} +TOML11_INLINE bool operator<=(const local_time& lhs, const local_time& rhs) +{ + return (lhs < rhs) || (lhs == rhs); +} +TOML11_INLINE bool operator> (const local_time& lhs, const local_time& rhs) +{ + return !(lhs <= rhs); +} +TOML11_INLINE bool operator>=(const local_time& lhs, const local_time& rhs) +{ + return !(lhs < rhs); +} + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const local_time& time) +{ + os << std::setfill('0') << std::setw(2) << static_cast(time.hour ) << ':'; + os << std::setfill('0') << std::setw(2) << static_cast(time.minute) << ':'; + os << std::setfill('0') << std::setw(2) << static_cast(time.second); + if(time.millisecond != 0 || time.microsecond != 0 || time.nanosecond != 0) + { + os << '.'; + os << std::setfill('0') << std::setw(3) << static_cast(time.millisecond); + if(time.microsecond != 0 || time.nanosecond != 0) + { + os << std::setfill('0') << std::setw(3) << static_cast(time.microsecond); + if(time.nanosecond != 0) + { + os << std::setfill('0') << std::setw(3) << static_cast(time.nanosecond); + } + } + } + return os; +} + +TOML11_INLINE std::string to_string(const local_time& time) +{ + std::ostringstream oss; + oss.imbue(std::locale::classic()); + oss << time; + return oss.str(); +} + +// ---------------------------------------------------------------------------- + +TOML11_INLINE time_offset::operator std::chrono::minutes() const +{ + return std::chrono::minutes(this->minute) + + std::chrono::hours(this->hour); +} + +TOML11_INLINE bool operator==(const time_offset& lhs, const time_offset& rhs) +{ + return std::make_tuple(lhs.hour, lhs.minute) == + std::make_tuple(rhs.hour, rhs.minute); +} +TOML11_INLINE bool operator!=(const time_offset& lhs, const time_offset& rhs) +{ + return !(lhs == rhs); +} +TOML11_INLINE bool operator< (const time_offset& lhs, const time_offset& rhs) +{ + return std::make_tuple(lhs.hour, lhs.minute) < + std::make_tuple(rhs.hour, rhs.minute); +} +TOML11_INLINE bool operator<=(const time_offset& lhs, const time_offset& rhs) +{ + return (lhs < rhs) || (lhs == rhs); +} +TOML11_INLINE bool operator> (const time_offset& lhs, const time_offset& rhs) +{ + return !(lhs <= rhs); +} +TOML11_INLINE bool operator>=(const time_offset& lhs, const time_offset& rhs) +{ + return !(lhs < rhs); +} + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const time_offset& offset) +{ + if(offset.hour == 0 && offset.minute == 0) + { + os << 'Z'; + return os; + } + int minute = static_cast(offset.hour) * 60 + offset.minute; + if(minute < 0){os << '-'; minute = std::abs(minute);} else {os << '+';} + os << std::setfill('0') << std::setw(2) << minute / 60 << ':'; + os << std::setfill('0') << std::setw(2) << minute % 60; + return os; +} + +TOML11_INLINE std::string to_string(const time_offset& offset) +{ + std::ostringstream oss; + oss.imbue(std::locale::classic()); + oss << offset; + return oss.str(); +} + +// ----------------------------------------------------------------------------- + +TOML11_INLINE local_datetime::local_datetime(const std::chrono::system_clock::time_point& tp) +{ + const auto t = std::chrono::system_clock::to_time_t(tp); + std::tm ltime = detail::localtime_s(&t); + + this->date = local_date(ltime); + this->time = local_time(ltime); + + // std::tm lacks subsecond information, so diff between tp and tm + // can be used to get millisecond & microsecond information. + const auto t_diff = tp - + std::chrono::system_clock::from_time_t(std::mktime(<ime)); + this->time.millisecond = static_cast( + std::chrono::duration_cast(t_diff).count()); + this->time.microsecond = static_cast( + std::chrono::duration_cast(t_diff).count()); + this->time.nanosecond = static_cast( + std::chrono::duration_cast(t_diff).count()); +} + +TOML11_INLINE local_datetime::local_datetime(const std::time_t t) + : local_datetime{std::chrono::system_clock::from_time_t(t)} +{} + +TOML11_INLINE local_datetime::operator std::chrono::system_clock::time_point() const +{ + using internal_duration = + typename std::chrono::system_clock::time_point::duration; + + // Normally DST begins at A.M. 3 or 4. If we re-use conversion operator + // of local_date and local_time independently, the conversion fails if + // it is the day when DST begins or ends. Since local_date considers the + // time is 00:00 A.M. and local_time does not consider DST because it + // does not have any date information. We need to consider both date and + // time information at the same time to convert it correctly. + + std::tm t; + t.tm_sec = static_cast(this->time.second); + t.tm_min = static_cast(this->time.minute); + t.tm_hour = static_cast(this->time.hour); + t.tm_mday = static_cast(this->date.day); + t.tm_mon = static_cast(this->date.month); + t.tm_year = static_cast(this->date.year) - 1900; + t.tm_wday = 0; // the value will be ignored + t.tm_yday = 0; // the value will be ignored + t.tm_isdst = -1; + + // std::mktime returns date as local time zone. no conversion needed + auto dt = std::chrono::system_clock::from_time_t(std::mktime(&t)); + dt += std::chrono::duration_cast( + std::chrono::milliseconds(this->time.millisecond) + + std::chrono::microseconds(this->time.microsecond) + + std::chrono::nanoseconds (this->time.nanosecond)); + return dt; +} + +TOML11_INLINE local_datetime::operator std::time_t() const +{ + return std::chrono::system_clock::to_time_t( + std::chrono::system_clock::time_point(*this)); +} + +TOML11_INLINE bool operator==(const local_datetime& lhs, const local_datetime& rhs) +{ + return std::make_tuple(lhs.date, lhs.time) == + std::make_tuple(rhs.date, rhs.time); +} +TOML11_INLINE bool operator!=(const local_datetime& lhs, const local_datetime& rhs) +{ + return !(lhs == rhs); +} +TOML11_INLINE bool operator< (const local_datetime& lhs, const local_datetime& rhs) +{ + return std::make_tuple(lhs.date, lhs.time) < + std::make_tuple(rhs.date, rhs.time); +} +TOML11_INLINE bool operator<=(const local_datetime& lhs, const local_datetime& rhs) +{ + return (lhs < rhs) || (lhs == rhs); +} +TOML11_INLINE bool operator> (const local_datetime& lhs, const local_datetime& rhs) +{ + return !(lhs <= rhs); +} +TOML11_INLINE bool operator>=(const local_datetime& lhs, const local_datetime& rhs) +{ + return !(lhs < rhs); +} + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const local_datetime& dt) +{ + os << dt.date << 'T' << dt.time; + return os; +} + +TOML11_INLINE std::string to_string(const local_datetime& dt) +{ + std::ostringstream oss; + oss.imbue(std::locale::classic()); + oss << dt; + return oss.str(); +} + +// ----------------------------------------------------------------------------- + + +TOML11_INLINE offset_datetime::offset_datetime(const local_datetime& ld) + : date{ld.date}, time{ld.time}, offset{get_local_offset(nullptr)} + // use the current local timezone offset +{} +TOML11_INLINE offset_datetime::offset_datetime(const std::chrono::system_clock::time_point& tp) + : offset{0, 0} // use gmtime +{ + const auto timet = std::chrono::system_clock::to_time_t(tp); + const auto tm = detail::gmtime_s(&timet); + this->date = local_date(tm); + this->time = local_time(tm); +} +TOML11_INLINE offset_datetime::offset_datetime(const std::time_t& t) + : offset{0, 0} // use gmtime +{ + const auto tm = detail::gmtime_s(&t); + this->date = local_date(tm); + this->time = local_time(tm); +} +TOML11_INLINE offset_datetime::offset_datetime(const std::tm& t) + : offset{0, 0} // assume gmtime +{ + this->date = local_date(t); + this->time = local_time(t); +} + +TOML11_INLINE offset_datetime::operator std::chrono::system_clock::time_point() const +{ + // get date-time + using internal_duration = + typename std::chrono::system_clock::time_point::duration; + + // first, convert it to local date-time information in the same way as + // local_datetime does. later we will use time_t to adjust time offset. + std::tm t; + t.tm_sec = static_cast(this->time.second); + t.tm_min = static_cast(this->time.minute); + t.tm_hour = static_cast(this->time.hour); + t.tm_mday = static_cast(this->date.day); + t.tm_mon = static_cast(this->date.month); + t.tm_year = static_cast(this->date.year) - 1900; + t.tm_wday = 0; // the value will be ignored + t.tm_yday = 0; // the value will be ignored + t.tm_isdst = -1; + const std::time_t tp_loc = std::mktime(std::addressof(t)); + + auto tp = std::chrono::system_clock::from_time_t(tp_loc); + tp += std::chrono::duration_cast( + std::chrono::milliseconds(this->time.millisecond) + + std::chrono::microseconds(this->time.microsecond) + + std::chrono::nanoseconds (this->time.nanosecond)); + + // Since mktime uses local time zone, it should be corrected. + // `12:00:00+09:00` means `03:00:00Z`. So mktime returns `03:00:00Z` if + // we are in `+09:00` timezone. To represent `12:00:00Z` there, we need + // to add `+09:00` to `03:00:00Z`. + // Here, it uses the time_t converted from date-time info to handle + // daylight saving time. + const auto ofs = get_local_offset(std::addressof(tp_loc)); + tp += std::chrono::hours (ofs.hour); + tp += std::chrono::minutes(ofs.minute); + + // We got `12:00:00Z` by correcting local timezone applied by mktime. + // Then we will apply the offset. Let's say `12:00:00-08:00` is given. + // And now, we have `12:00:00Z`. `12:00:00-08:00` means `20:00:00Z`. + // So we need to subtract the offset. + tp -= std::chrono::minutes(this->offset); + return tp; +} + +TOML11_INLINE offset_datetime::operator std::time_t() const +{ + return std::chrono::system_clock::to_time_t( + std::chrono::system_clock::time_point(*this)); +} + +TOML11_INLINE time_offset offset_datetime::get_local_offset(const std::time_t* tp) +{ + // get local timezone with the same date-time information as mktime + const auto t = detail::localtime_s(tp); + + std::array buf; + const auto result = std::strftime(buf.data(), 6, "%z", &t); // +hhmm\0 + if(result != 5) + { + throw std::runtime_error("toml::offset_datetime: cannot obtain " + "timezone information of current env"); + } + const int ofs = std::atoi(buf.data()); + const int ofs_h = ofs / 100; + const int ofs_m = ofs - (ofs_h * 100); + return time_offset(ofs_h, ofs_m); +} + +TOML11_INLINE bool operator==(const offset_datetime& lhs, const offset_datetime& rhs) +{ + return std::make_tuple(lhs.date, lhs.time, lhs.offset) == + std::make_tuple(rhs.date, rhs.time, rhs.offset); +} +TOML11_INLINE bool operator!=(const offset_datetime& lhs, const offset_datetime& rhs) +{ + return !(lhs == rhs); +} +TOML11_INLINE bool operator< (const offset_datetime& lhs, const offset_datetime& rhs) +{ + return std::make_tuple(lhs.date, lhs.time, lhs.offset) < + std::make_tuple(rhs.date, rhs.time, rhs.offset); +} +TOML11_INLINE bool operator<=(const offset_datetime& lhs, const offset_datetime& rhs) +{ + return (lhs < rhs) || (lhs == rhs); +} +TOML11_INLINE bool operator> (const offset_datetime& lhs, const offset_datetime& rhs) +{ + return !(lhs <= rhs); +} +TOML11_INLINE bool operator>=(const offset_datetime& lhs, const offset_datetime& rhs) +{ + return !(lhs < rhs); +} + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const offset_datetime& dt) +{ + os << dt.date << 'T' << dt.time << dt.offset; + return os; +} + +TOML11_INLINE std::string to_string(const offset_datetime& dt) +{ + std::ostringstream oss; + oss.imbue(std::locale::classic()); + oss << dt; + return oss.str(); +} + +}//toml +#endif // TOML11_DATETIME_IMPL_HPP diff --git a/include/toml11/impl/error_info_impl.hpp b/include/toml11/impl/error_info_impl.hpp new file mode 100644 index 0000000..b87f477 --- /dev/null +++ b/include/toml11/impl/error_info_impl.hpp @@ -0,0 +1,75 @@ +#ifndef TOML11_ERROR_INFO_IMPL_HPP +#define TOML11_ERROR_INFO_IMPL_HPP + +#include "../fwd/error_info_fwd.hpp" +#include "../fwd/color_fwd.hpp" + +#include + +namespace toml +{ + +TOML11_INLINE std::string format_error(const std::string& errkind, const error_info& err) +{ + std::string errmsg; + if( ! errkind.empty()) + { + errmsg = errkind; + errmsg += ' '; + } + errmsg += err.title(); + errmsg += '\n'; + + const auto lnw = [&err]() { + std::size_t width = 0; + for(const auto& l : err.locations()) + { + width = (std::max)(detail::integer_width_base10(l.first.last_line_number()), width); + } + return width; + }(); + + bool first = true; + std::string prev_fname; + for(const auto& lm : err.locations()) + { + if( ! first) + { + std::ostringstream oss; + oss << detail::make_string(lnw + 1, ' ') + << color::bold << color::blue << " |" << color::reset + << color::bold << " ...\n" << color::reset; + oss << detail::make_string(lnw + 1, ' ') + << color::bold << color::blue << " |\n" << color::reset; + errmsg += oss.str(); + } + + const auto& l = lm.first; + const auto& m = lm.second; + + errmsg += detail::format_location_impl(lnw, prev_fname, l, m); + + prev_fname = l.file_name(); + first = false; + } + + errmsg += err.suffix(); + + return errmsg; +} + +TOML11_INLINE std::string format_error(const error_info& err) +{ + std::ostringstream oss; + oss << color::red << color::bold << "[error]" << color::reset; + return format_error(oss.str(), err); +} + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const error_info& e) +{ + os << format_error(e); + return os; +} + +} // toml +#endif // TOML11_ERROR_INFO_IMPL_HPP diff --git a/include/toml11/impl/format_impl.hpp b/include/toml11/impl/format_impl.hpp new file mode 100644 index 0000000..c4985fa --- /dev/null +++ b/include/toml11/impl/format_impl.hpp @@ -0,0 +1,297 @@ +#ifndef TOML11_FORMAT_IMPL_HPP +#define TOML11_FORMAT_IMPL_HPP + +#include "../fwd/format_fwd.hpp" +#include "../version.hpp" + +#include +#include + +namespace toml +{ + +// toml types with serialization info + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const indent_char& c) +{ + switch(c) + { + case indent_char::space: {os << "space" ; break;} + case indent_char::tab: {os << "tab" ; break;} + case indent_char::none: {os << "none" ; break;} + default: + { + os << "unknown indent char: " << static_cast(c); + } + } + return os; +} + +TOML11_INLINE std::string to_string(const indent_char c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + +// ---------------------------------------------------------------------------- +// boolean + +// ---------------------------------------------------------------------------- +// integer + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const integer_format f) +{ + switch(f) + { + case integer_format::dec: {os << "dec"; break;} + case integer_format::bin: {os << "bin"; break;} + case integer_format::oct: {os << "oct"; break;} + case integer_format::hex: {os << "hex"; break;} + default: + { + os << "unknown integer_format: " << static_cast(f); + break; + } + } + return os; +} +TOML11_INLINE std::string to_string(const integer_format c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + + +TOML11_INLINE bool operator==(const integer_format_info& lhs, const integer_format_info& rhs) noexcept +{ + return lhs.fmt == rhs.fmt && + lhs.uppercase == rhs.uppercase && + lhs.width == rhs.width && + lhs.spacer == rhs.spacer && + lhs.suffix == rhs.suffix ; +} +TOML11_INLINE bool operator!=(const integer_format_info& lhs, const integer_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +// ---------------------------------------------------------------------------- +// floating + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const floating_format f) +{ + switch(f) + { + case floating_format::defaultfloat: {os << "defaultfloat"; break;} + case floating_format::fixed : {os << "fixed" ; break;} + case floating_format::scientific : {os << "scientific" ; break;} + case floating_format::hex : {os << "hex" ; break;} + default: + { + os << "unknown floating_format: " << static_cast(f); + break; + } + } + return os; +} +TOML11_INLINE std::string to_string(const floating_format c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + +TOML11_INLINE bool operator==(const floating_format_info& lhs, const floating_format_info& rhs) noexcept +{ + return lhs.fmt == rhs.fmt && + lhs.prec == rhs.prec && + lhs.suffix == rhs.suffix ; +} +TOML11_INLINE bool operator!=(const floating_format_info& lhs, const floating_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +// ---------------------------------------------------------------------------- +// string + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const string_format f) +{ + switch(f) + { + case string_format::basic : {os << "basic" ; break;} + case string_format::literal : {os << "literal" ; break;} + case string_format::multiline_basic : {os << "multiline_basic" ; break;} + case string_format::multiline_literal: {os << "multiline_literal"; break;} + default: + { + os << "unknown string_format: " << static_cast(f); + break; + } + } + return os; +} +TOML11_INLINE std::string to_string(const string_format c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + +TOML11_INLINE bool operator==(const string_format_info& lhs, const string_format_info& rhs) noexcept +{ + return lhs.fmt == rhs.fmt && + lhs.start_with_newline == rhs.start_with_newline ; +} +TOML11_INLINE bool operator!=(const string_format_info& lhs, const string_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} +// ---------------------------------------------------------------------------- +// datetime + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const datetime_delimiter_kind d) +{ + switch(d) + { + case datetime_delimiter_kind::upper_T: { os << "upper_T, "; break; } + case datetime_delimiter_kind::lower_t: { os << "lower_t, "; break; } + case datetime_delimiter_kind::space: { os << "space, "; break; } + default: + { + os << "unknown datetime delimiter: " << static_cast(d); + break; + } + } + return os; +} +TOML11_INLINE std::string to_string(const datetime_delimiter_kind c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + +TOML11_INLINE bool operator==(const offset_datetime_format_info& lhs, const offset_datetime_format_info& rhs) noexcept +{ + return lhs.delimiter == rhs.delimiter && + lhs.has_seconds == rhs.has_seconds && + lhs.subsecond_precision == rhs.subsecond_precision ; +} +TOML11_INLINE bool operator!=(const offset_datetime_format_info& lhs, const offset_datetime_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +TOML11_INLINE bool operator==(const local_datetime_format_info& lhs, const local_datetime_format_info& rhs) noexcept +{ + return lhs.delimiter == rhs.delimiter && + lhs.has_seconds == rhs.has_seconds && + lhs.subsecond_precision == rhs.subsecond_precision ; +} +TOML11_INLINE bool operator!=(const local_datetime_format_info& lhs, const local_datetime_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +TOML11_INLINE bool operator==(const local_date_format_info&, const local_date_format_info&) noexcept +{ + return true; +} +TOML11_INLINE bool operator!=(const local_date_format_info& lhs, const local_date_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +TOML11_INLINE bool operator==(const local_time_format_info& lhs, const local_time_format_info& rhs) noexcept +{ + return lhs.has_seconds == rhs.has_seconds && + lhs.subsecond_precision == rhs.subsecond_precision ; +} +TOML11_INLINE bool operator!=(const local_time_format_info& lhs, const local_time_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +// ---------------------------------------------------------------------------- +// array + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const array_format f) +{ + switch(f) + { + case array_format::default_format : {os << "default_format" ; break;} + case array_format::oneline : {os << "oneline" ; break;} + case array_format::multiline : {os << "multiline" ; break;} + case array_format::array_of_tables: {os << "array_of_tables"; break;} + default: + { + os << "unknown array_format: " << static_cast(f); + break; + } + } + return os; +} +TOML11_INLINE std::string to_string(const array_format c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + +TOML11_INLINE bool operator==(const array_format_info& lhs, const array_format_info& rhs) noexcept +{ + return lhs.fmt == rhs.fmt && + lhs.indent_type == rhs.indent_type && + lhs.body_indent == rhs.body_indent && + lhs.closing_indent == rhs.closing_indent ; +} +TOML11_INLINE bool operator!=(const array_format_info& lhs, const array_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +// ---------------------------------------------------------------------------- +// table + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const table_format f) +{ + switch(f) + { + case table_format::multiline : {os << "multiline" ; break;} + case table_format::oneline : {os << "oneline" ; break;} + case table_format::dotted : {os << "dotted" ; break;} + case table_format::multiline_oneline: {os << "multiline_oneline"; break;} + case table_format::implicit : {os << "implicit" ; break;} + default: + { + os << "unknown table_format: " << static_cast(f); + break; + } + } + return os; +} +TOML11_INLINE std::string to_string(const table_format c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + +TOML11_INLINE bool operator==(const table_format_info& lhs, const table_format_info& rhs) noexcept +{ + return lhs.fmt == rhs.fmt && + lhs.indent_type == rhs.indent_type && + lhs.body_indent == rhs.body_indent && + lhs.name_indent == rhs.name_indent && + lhs.closing_indent == rhs.closing_indent ; +} +TOML11_INLINE bool operator!=(const table_format_info& lhs, const table_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + +} // namespace toml +#endif // TOML11_FORMAT_IMPL_HPP diff --git a/include/toml11/impl/literal_impl.hpp b/include/toml11/impl/literal_impl.hpp new file mode 100644 index 0000000..e8298c2 --- /dev/null +++ b/include/toml11/impl/literal_impl.hpp @@ -0,0 +1,174 @@ +#ifndef TOML11_LITERAL_IMPL_HPP +#define TOML11_LITERAL_IMPL_HPP + +#include "../fwd/literal_fwd.hpp" +#include "../parser.hpp" +#include "../syntax.hpp" + +namespace toml +{ + +namespace detail +{ +// implementation +TOML11_INLINE ::toml::value literal_internal_impl(location loc) +{ + const auto s = ::toml::spec::default_version(); + context ctx(s); + + const auto front = loc; + + // ------------------------------------------------------------------------ + // check if it is a raw value. + + // skip empty lines and comment lines + auto sp = skip_multiline_spacer(loc, ctx); + if(loc.eof()) + { + ::toml::value val; + if(sp.has_value()) + { + for(std::size_t i=0; i(str), + reinterpret_cast(str + len), + c.begin()); + if( ! c.empty() && c.back()) + { + c.push_back('\n'); // to make it easy to parse comment, we add newline + } + + return literal_internal_impl(::toml::detail::location( + std::make_shared(std::move(c)), + "TOML literal encoded in a C++ code")); +} + +#if defined(__cpp_char8_t) +# if __cpp_char8_t >= 201811L +# define TOML11_HAS_CHAR8_T 1 +# endif +#endif + +#if defined(TOML11_HAS_CHAR8_T) +// value of u8"" literal has been changed from char to char8_t and char8_t is +// NOT compatible to char +TOML11_INLINE ::toml::value +operator"" _toml(const char8_t* str, std::size_t len) +{ + if(len == 0) + { + return ::toml::value{}; + } + + ::toml::detail::location::container_type c(len); + std::copy(reinterpret_cast(str), + reinterpret_cast(str + len), + c.begin()); + if( ! c.empty() && c.back()) + { + c.push_back('\n'); // to make it easy to parse comment, we add newline + } + + return literal_internal_impl(::toml::detail::location( + std::make_shared(std::move(c)), + "TOML literal encoded in a C++ code")); +} +#endif + +} // toml_literals +} // literals +} // toml +#endif // TOML11_LITERAL_IMPL_HPP diff --git a/include/toml11/impl/location_impl.hpp b/include/toml11/impl/location_impl.hpp new file mode 100644 index 0000000..b996ab6 --- /dev/null +++ b/include/toml11/impl/location_impl.hpp @@ -0,0 +1,209 @@ +#ifndef TOML11_LOCATION_IMPL_HPP +#define TOML11_LOCATION_IMPL_HPP + +#include "../fwd/location_fwd.hpp" +#include "../utility.hpp" +#include "../version.hpp" + +namespace toml +{ +namespace detail +{ + +TOML11_INLINE void location::advance(std::size_t n) noexcept +{ + assert(this->is_ok()); + if(this->location_ + n < this->source_->size()) + { + this->advance_impl(n); + } + else + { + this->advance_impl(this->source_->size() - this->location_); + + assert(this->location_ == this->source_->size()); + } +} +TOML11_INLINE void location::retrace(/*restricted to n=1*/) noexcept +{ + assert(this->is_ok()); + if(this->location_ == 0) + { + this->location_ = 0; + this->line_number_ = 1; + this->column_number_ = 1; + } + else + { + this->retrace_impl(); + } +} + +TOML11_INLINE bool location::eof() const noexcept +{ + assert(this->is_ok()); + return this->location_ >= this->source_->size(); +} +TOML11_INLINE location::char_type location::current() const +{ + assert(this->is_ok()); + if(this->eof()) {return '\0';} + + assert(this->location_ < this->source_->size()); + return this->source_->at(this->location_); +} + +TOML11_INLINE location::char_type location::peek() +{ + assert(this->is_ok()); + if(this->location_ >= this->source_->size()) + { + return '\0'; + } + else + { + return this->source_->at(this->location_ + 1); + } +} + +TOML11_INLINE std::string location::get_line() const +{ + assert(this->is_ok()); + const auto iter = std::next(this->source_->cbegin(), static_cast(this->location_)); + const auto riter = cxx::make_reverse_iterator(iter); + + const auto prev = std::find(riter, this->source_->crend(), char_type('\n')); + const auto next = std::find(iter, this->source_->cend(), char_type('\n')); + + return make_string(std::next(prev.base()), next); +} + +TOML11_INLINE std::size_t location::calc_column_number() const noexcept +{ + assert(this->is_ok()); + const auto iter = std::next(this->source_->cbegin(), static_cast(this->location_)); + const auto riter = cxx::make_reverse_iterator(iter); + const auto prev = std::find(riter, this->source_->crend(), char_type('\n')); + + assert(prev.base() <= iter); + return static_cast(std::distance(prev.base(), iter) + 1); // 1-origin +} + +TOML11_INLINE void location::advance_impl(const std::size_t n) +{ + assert(this->is_ok()); + assert(this->location_ + n <= this->source_->size()); + + auto iter = this->source_->cbegin(); + std::advance(iter, static_cast(this->location_)); + + for(std::size_t i=0; iline_number_ += 1; + this->column_number_ = 1; + } + else + { + this->column_number_ += 1; + } + iter++; + } + this->location_ += n; + return; +} +TOML11_INLINE void location::retrace_impl(/*n == 1*/) +{ + assert(this->is_ok()); + assert(this->location_ != 0); + + this->location_ -= 1; + + auto iter = this->source_->cbegin(); + std::advance(iter, static_cast(this->location_)); + if(*iter == '\n') + { + this->line_number_ -= 1; + this->column_number_ = this->calc_column_number(); + } + return; +} + +TOML11_INLINE bool operator==(const location& lhs, const location& rhs) noexcept +{ + if( ! lhs.is_ok() || ! rhs.is_ok()) + { + return (!lhs.is_ok()) && (!rhs.is_ok()); + } + return lhs.source() == rhs.source() && + lhs.source_name() == rhs.source_name() && + lhs.get_location() == rhs.get_location(); +} +TOML11_INLINE bool operator!=(const location& lhs, const location& rhs) +{ + return !(lhs == rhs); +} + +TOML11_INLINE location prev(const location& loc) +{ + location p(loc); + p.retrace(); + return p; +} +TOML11_INLINE location next(const location& loc) +{ + location p(loc); + p.advance(1); + return p; +} + +TOML11_INLINE location make_temporary_location(const std::string& str) noexcept +{ + location::container_type cont(str.size()); + std::transform(str.begin(), str.end(), cont.begin(), + [](const std::string::value_type& c) { + return cxx::bit_cast(c); + }); + return location(std::make_shared( + std::move(cont)), "internal temporary"); +} + +TOML11_INLINE result +find(const location& first, const location& last, const location::char_type val) +{ + return find_if(first, last, [val](const location::char_type c) { + return c == val; + }); +} +TOML11_INLINE result +rfind(const location& first, const location& last, const location::char_type val) +{ + return rfind_if(first, last, [val](const location::char_type c) { + return c == val; + }); +} + +TOML11_INLINE std::size_t +count(const location& first, const location& last, const location::char_type& c) +{ + if(first.source() != last.source()) { return 0; } + if(first.get_location() >= last.get_location()) { return 0; } + + auto loc = first; + std::size_t num = 0; + while(loc.get_location() != last.get_location()) + { + if(loc.current() == c) + { + num += 1; + } + loc.advance(); + } + return num; +} + +} // detail +} // toml +#endif // TOML11_LOCATION_HPP diff --git a/include/toml11/impl/region_impl.hpp b/include/toml11/impl/region_impl.hpp new file mode 100644 index 0000000..28242c8 --- /dev/null +++ b/include/toml11/impl/region_impl.hpp @@ -0,0 +1,246 @@ +#ifndef TOML11_REGION_IMPL_HPP +#define TOML11_REGION_IMPL_HPP + +#include "../fwd/region_fwd.hpp" +#include "../utility.hpp" + +#include +#include +#include +#include +#include +#include + +namespace toml +{ +namespace detail +{ + +// a value defined in [first, last). +// Those source must be the same. Instread, `region` does not make sense. +TOML11_INLINE region::region(const location& first, const location& last) + : source_(first.source()), source_name_(first.source_name()), + length_(last.get_location() - first.get_location()), + first_(first.get_location()), + first_line_(first.line_number()), + first_column_(first.column_number()), + last_(last.get_location()), + last_line_(last.line_number()), + last_column_(last.column_number()) +{ + assert(first.source() == last.source()); + assert(first.source_name() == last.source_name()); +} + + // shorthand of [loc, loc+1) +TOML11_INLINE region::region(const location& loc) + : source_(loc.source()), source_name_(loc.source_name()), length_(0), + first_line_(0), first_column_(0), last_line_(0), last_column_(0) +{ + // if the file ends with LF, the resulting region points no char. + if(loc.eof()) + { + if(loc.get_location() == 0) + { + this->length_ = 0; + this->first_ = 0; + this->first_line_ = 0; + this->first_column_ = 0; + this->last_ = 0; + this->last_line_ = 0; + this->last_column_ = 0; + } + else + { + const auto first = prev(loc); + this->first_ = first.get_location(); + this->first_line_ = first.line_number(); + this->first_column_ = first.column_number(); + this->last_ = loc.get_location(); + this->last_line_ = loc.line_number(); + this->last_column_ = loc.column_number(); + this->length_ = 1; + } + } + else + { + this->first_ = loc.get_location(); + this->first_line_ = loc.line_number(); + this->first_column_ = loc.column_number(); + this->last_ = loc.get_location() + 1; + this->last_line_ = loc.line_number(); + this->last_column_ = loc.column_number() + 1; + this->length_ = 1; + } +} + +TOML11_INLINE region::char_type region::at(std::size_t i) const +{ + if(this->last_ <= this->first_ + i) + { + throw std::out_of_range("range::at: index " + std::to_string(i) + + " exceeds length " + std::to_string(this->length_)); + } + const auto iter = std::next(this->source_->cbegin(), + static_cast(this->first_ + i)); + return *iter; +} + +TOML11_INLINE region::const_iterator region::begin() const noexcept +{ + return std::next(this->source_->cbegin(), + static_cast(this->first_)); +} +TOML11_INLINE region::const_iterator region::end() const noexcept +{ + return std::next(this->source_->cbegin(), + static_cast(this->last_)); +} +TOML11_INLINE region::const_iterator region::cbegin() const noexcept +{ + return std::next(this->source_->cbegin(), + static_cast(this->first_)); +} +TOML11_INLINE region::const_iterator region::cend() const noexcept +{ + return std::next(this->source_->cbegin(), + static_cast(this->last_)); +} + +TOML11_INLINE std::string region::as_string() const +{ + if(this->is_ok()) + { + const auto begin = std::next(this->source_->cbegin(), static_cast(this->first_)); + const auto end = std::next(this->source_->cbegin(), static_cast(this->last_ )); + return ::toml::detail::make_string(begin, end); + } + else + { + return std::string(""); + } +} + +TOML11_INLINE std::pair +region::take_line(const_iterator begin, const_iterator end) const +{ + // To omit long line, we cap region by before/after 30 chars + const auto dist_before = std::distance(source_->cbegin(), begin); + const auto dist_after = std::distance(end, source_->cend()); + + const const_iterator capped_begin = (dist_before <= 30) ? source_->cbegin() : std::prev(begin, 30); + const const_iterator capped_end = (dist_after <= 30) ? source_->cend() : std::next(end, 30); + + const auto lf = char_type('\n'); + const auto lf_before = std::find(cxx::make_reverse_iterator(begin), + cxx::make_reverse_iterator(capped_begin), lf); + const auto lf_after = std::find(end, capped_end, lf); + + auto offset = static_cast(std::distance(lf_before.base(), begin)); + + std::string retval = make_string(lf_before.base(), lf_after); + + if(lf_before.base() != source_->cbegin() && *lf_before != lf) + { + retval = "... " + retval; + offset += 4; + } + + if(lf_after != source_->cend() && *lf_after != lf) + { + retval = retval + " ..."; + } + + return std::make_pair(retval, offset); +} + +TOML11_INLINE std::vector> region::as_lines() const +{ + assert(this->is_ok()); + if(this->length_ == 0) + { + return std::vector>{ + std::make_pair("", std::size_t(0)) + }; + } + + // Consider the following toml file + // ``` + // array = [ + // 1, 2, 3, + // ] # comment + // ``` + // and the region represnets + // ``` + // [ + // 1, 2, 3, + // ] + // ``` + // but we want to show the following. + // ``` + // array = [ + // 1, 2, 3, + // ] # comment + // ``` + // So we need to find LFs before `begin` and after `end`. + // + // But, if region ends with LF, it should not include the next line. + // ``` + // a = 42 + // ^^^- with the last LF + // ``` + // So we start from `end-1` when looking for LF. + + const auto begin_idx = static_cast(this->first_); + const auto end_idx = static_cast(this->last_) - 1; + + // length_ != 0, so begin < end. then begin <= end-1 + assert(begin_idx <= end_idx); + + const auto begin = std::next(this->source_->cbegin(), begin_idx); + const auto end = std::next(this->source_->cbegin(), end_idx); + + assert(this->first_line_number() <= this->last_line_number()); + + if(this->first_line_number() == this->last_line_number()) + { + return std::vector>{ + this->take_line(begin, end) + }; + } + + // we have multiple lines. `begin` and `end` points different lines. + // that means that there is at least one `LF` between `begin` and `end`. + + const auto after_begin = std::distance(begin, this->source_->cend()); + const auto before_end = std::distance(this->source_->cbegin(), end); + + const_iterator capped_file_end = this->source_->cend(); + const_iterator capped_file_begin = this->source_->cbegin(); + if(60 < after_begin) {capped_file_end = std::next(begin, 50);} + if(60 < before_end) {capped_file_begin = std::prev(end, 50);} + + const auto lf = char_type('\n'); + const auto first_line_end = std::find(begin, capped_file_end, lf); + const auto last_line_begin = std::find(capped_file_begin, end, lf); + + const auto first_line = this->take_line(begin, first_line_end); + const auto last_line = this->take_line(last_line_begin, end); + + if(this->first_line_number() + 1 == this->last_line_number()) + { + return std::vector>{ + first_line, last_line + }; + } + else + { + return std::vector>{ + first_line, std::make_pair("...", 0), last_line + }; + } +} + +} // namespace detail +} // namespace toml +#endif // TOML11_REGION_IMPL_HPP diff --git a/include/toml11/impl/scanner_impl.hpp b/include/toml11/impl/scanner_impl.hpp new file mode 100644 index 0000000..d97d317 --- /dev/null +++ b/include/toml11/impl/scanner_impl.hpp @@ -0,0 +1,473 @@ +#ifndef TOML11_SCANNER_IMPL_HPP +#define TOML11_SCANNER_IMPL_HPP + +#include "../fwd/scanner_fwd.hpp" +#include "../utility.hpp" + +namespace toml +{ +namespace detail +{ + +TOML11_INLINE scanner_storage::scanner_storage(const scanner_storage& other) + : scanner_(nullptr) +{ + if(other.is_ok()) + { + scanner_.reset(other.get().clone()); + } +} +TOML11_INLINE scanner_storage& scanner_storage::operator=(const scanner_storage& other) +{ + if(this == std::addressof(other)) {return *this;} + if(other.is_ok()) + { + scanner_.reset(other.get().clone()); + } + return *this; +} + +TOML11_INLINE region scanner_storage::scan(location& loc) const +{ + assert(this->is_ok()); + return this->scanner_->scan(loc); +} + +TOML11_INLINE std::string scanner_storage::expected_chars(location& loc) const +{ + assert(this->is_ok()); + return this->scanner_->expected_chars(loc); +} + +TOML11_INLINE scanner_base& scanner_storage::get() const noexcept +{ + assert(this->is_ok()); + return *scanner_; +} + +TOML11_INLINE std::string scanner_storage::name() const +{ + assert(this->is_ok()); + return this->scanner_->name(); +} + +// ---------------------------------------------------------------------------- + +TOML11_INLINE region character::scan(location& loc) const +{ + if(loc.eof()) {return region{};} + + if(loc.current() == this->value_) + { + const auto first = loc; + loc.advance(1); + return region(first, loc); + } + return region{}; +} + +TOML11_INLINE std::string character::expected_chars(location&) const +{ + return show_char(value_); +} + +TOML11_INLINE scanner_base* character::clone() const +{ + return new character(*this); +} + +TOML11_INLINE std::string character::name() const +{ + return "character{" + show_char(value_) + "}"; +} + +// ---------------------------------------------------------------------------- + +TOML11_INLINE region character_either::scan(location& loc) const +{ + if(loc.eof()) {return region{};} + + for(const auto c : this->chars_) + { + if(loc.current() == c) + { + const auto first = loc; + loc.advance(1); + return region(first, loc); + } + } + return region{}; +} + +TOML11_INLINE std::string character_either::expected_chars(location&) const +{ + assert( ! chars_.empty()); + + std::string expected; + if(chars_.size() == 1) + { + expected += show_char(chars_.at(0)); + } + else if(chars_.size() == 2) + { + expected += show_char(chars_.at(0)) + " or " + show_char(chars_.at(1)); + } + else + { + for(std::size_t i=0; ichars_) + { + n += show_char(c); + n += ", "; + } + if( ! this->chars_.empty()) + { + n.pop_back(); + n.pop_back(); + } + n += "}"; + return n; +} + +// ---------------------------------------------------------------------------- +// character_in_range + +TOML11_INLINE region character_in_range::scan(location& loc) const +{ + if(loc.eof()) {return region{};} + + const auto curr = loc.current(); + if(this->from_ <= curr && curr <= this->to_) + { + const auto first = loc; + loc.advance(1); + return region(first, loc); + } + return region{}; +} + +TOML11_INLINE std::string character_in_range::expected_chars(location&) const +{ + std::string expected("from `"); + expected += show_char(from_); + expected += "` to `"; + expected += show_char(to_); + expected += "`"; + return expected; +} + +TOML11_INLINE scanner_base* character_in_range::clone() const +{ + return new character_in_range(*this); +} + +TOML11_INLINE std::string character_in_range::name() const +{ + return "character_in_range{" + show_char(from_) + "," + show_char(to_) + "}"; +} + +// ---------------------------------------------------------------------------- +// literal + +TOML11_INLINE region literal::scan(location& loc) const +{ + const auto first = loc; + for(std::size_t i=0; iothers_.empty()) + { + n.pop_back(); + n.pop_back(); + } + n += "}"; + return n; +} + +// ---------------------------------------------------------------------------- +// either + +TOML11_INLINE region either::scan(location& loc) const +{ + for(const auto& other : others_) + { + const auto reg = other.scan(loc); + if(reg.is_ok()) + { + return reg; + } + } + return region{}; +} + +TOML11_INLINE std::string either::expected_chars(location& loc) const +{ + assert( ! others_.empty()); + + std::string expected = others_.at(0).expected_chars(loc); + if(others_.size() == 2) + { + expected += " or "; + expected += others_.at(1).expected_chars(loc); + } + else + { + for(std::size_t i=1; iothers_.empty()) + { + n.pop_back(); + n.pop_back(); + } + n += "}"; + return n; +} + +// ---------------------------------------------------------------------------- +// repeat_exact + +TOML11_INLINE region repeat_exact::scan(location& loc) const +{ + const auto first = loc; + for(std::size_t i=0; i +#include +#include +#include + +#include + +namespace toml +{ + +TOML11_INLINE source_location::source_location(const detail::region& r) + : is_ok_(false), + first_line_(1), + first_column_(1), + first_offset_(1), + last_line_(1), + last_column_(1), + last_offset_(1), + length_(0), + file_name_("unknown file") +{ + if(r.is_ok()) + { + this->is_ok_ = true; + this->file_name_ = r.source_name(); + this->first_line_ = r.first_line_number(); + this->first_column_ = r.first_column_number(); + this->last_line_ = r.last_line_number(); + this->last_column_ = r.last_column_number(); + this->length_ = r.length(); + + const auto lines = r.as_lines(); + assert( ! lines.empty()); + + for(const auto& l : lines) + { + this->line_str_.push_back(l.first); + } + + this->first_offset_ = lines.at( 0).second + 1; // to 1-origin + this->last_offset_ = lines.at(lines.size()-1).second + 1; + } +} + +TOML11_INLINE std::string const& source_location::first_line() const +{ + if(this->line_str_.size() == 0) + { + throw std::out_of_range("toml::source_location::first_line: `lines` is empty"); + } + return this->line_str_.front(); +} +TOML11_INLINE std::string const& source_location::last_line() const +{ + if(this->line_str_.size() == 0) + { + throw std::out_of_range("toml::source_location::first_line: `lines` is empty"); + } + return this->line_str_.back(); +} + +namespace detail +{ + +TOML11_INLINE std::size_t integer_width_base10(std::size_t i) noexcept +{ + std::size_t width = 0; + while(i != 0) + { + i /= 10; + width += 1; + } + return width; +} + +TOML11_INLINE std::ostringstream& +format_filename(std::ostringstream& oss, const source_location& loc) +{ + // --> example.toml + oss << color::bold << color::blue << " --> " << color::reset + << color::bold << loc.file_name() << '\n' << color::reset; + return oss; +} + +TOML11_INLINE std::ostringstream& format_empty_line(std::ostringstream& oss, + const std::size_t lnw) +{ + // | + oss << detail::make_string(lnw + 1, ' ') + << color::bold << color::blue << " |\n" << color::reset; + return oss; +} + +TOML11_INLINE std::ostringstream& format_line(std::ostringstream& oss, + const std::size_t lnw, const std::size_t linenum, const std::string& line) +{ + // 10 | key = "value" + oss << ' ' << color::bold << color::blue + << std::setw(static_cast(lnw)) + << std::right << linenum << " | " << color::reset; + for(const char c : line) + { + if(std::isgraph(c) || c == ' ') + { + oss << c; + } + else + { + oss << show_char(c); + } + } + oss << '\n'; + return oss; +} +TOML11_INLINE std::ostringstream& format_underline(std::ostringstream& oss, + const std::size_t lnw, const std::size_t col, const std::size_t len, + const std::string& msg) +{ + // | ^^^^^^^-- this part + oss << make_string(lnw + 1, ' ') + << color::bold << color::blue << " | " << color::reset; + + // in case col is 0, so we don't create a string with size_t max length + const std::size_t sanitized_col = col == 0 ? 0 : col - 1 /*1-origin*/; + oss << make_string(sanitized_col, ' ') + << color::bold << color::red + << make_string(len, '^') << "-- " + << color::reset << msg << '\n'; + + return oss; +} + +TOML11_INLINE std::string format_location_impl(const std::size_t lnw, + const std::string& prev_fname, + const source_location& loc, const std::string& msg) +{ + std::ostringstream oss; + + if(loc.file_name() != prev_fname) + { + format_filename(oss, loc); + if( ! loc.lines().empty()) + { + format_empty_line(oss, lnw); + } + } + + if(loc.lines().size() == 1) + { + // when column points LF, it exceeds the size of the first line. + std::size_t underline_limit = 1; + if(loc.first_line().size() < loc.first_column_offset()) + { + underline_limit = 1; + } + else + { + underline_limit = loc.first_line().size() - loc.first_column_offset() + 1; + } + const auto underline_len = (std::min)(underline_limit, loc.length()); + + format_line(oss, lnw, loc.first_line_number(), loc.first_line()); + format_underline(oss, lnw, loc.first_column_offset(), underline_len, msg); + } + else if(loc.lines().size() == 2) + { + const auto first_underline_len = + loc.first_line().size() - loc.first_column_offset() + 1; + format_line(oss, lnw, loc.first_line_number(), loc.first_line()); + format_underline(oss, lnw, loc.first_column_offset(), + first_underline_len, ""); + + format_line(oss, lnw, loc.last_line_number(), loc.last_line()); + format_underline(oss, lnw, 1, loc.last_column_offset(), msg); + } + else if(loc.lines().size() > 2) + { + const auto first_underline_len = + loc.first_line().size() - loc.first_column_offset() + 1; + format_line(oss, lnw, loc.first_line_number(), loc.first_line()); + format_underline(oss, lnw, loc.first_column_offset(), + first_underline_len, "and"); + + if(loc.lines().size() == 3) + { + format_line(oss, lnw, loc.first_line_number()+1, loc.lines().at(1)); + format_underline(oss, lnw, 1, loc.lines().at(1).size(), "and"); + } + else + { + format_line(oss, lnw, loc.first_line_number()+1, " ..."); + format_empty_line(oss, lnw); + } + format_line(oss, lnw, loc.last_line_number(), loc.last_line()); + format_underline(oss, lnw, 1, loc.last_column_offset(), msg); + } + // if loc is empty, do nothing. + return oss.str(); +} + +} // namespace detail +} // toml +#endif // TOML11_SOURCE_LOCATION_IMPL_HPP diff --git a/include/toml11/impl/syntax_impl.hpp b/include/toml11/impl/syntax_impl.hpp new file mode 100644 index 0000000..7dd3b81 --- /dev/null +++ b/include/toml11/impl/syntax_impl.hpp @@ -0,0 +1,732 @@ +#ifndef TOML11_SYNTAX_IMPL_HPP +#define TOML11_SYNTAX_IMPL_HPP + +#include "../fwd/syntax_fwd.hpp" +#include "../scanner.hpp" +#include "../spec.hpp" + +namespace toml +{ +namespace detail +{ +namespace syntax +{ + +using char_type = location::char_type; + +// =========================================================================== +// UTF-8 + +// avoid redundant representation and out-of-unicode sequence + +TOML11_INLINE character_in_range utf8_1byte(const spec&) +{ + return character_in_range(0x00, 0x7F); +} + +TOML11_INLINE sequence utf8_2bytes(const spec&) +{ + return sequence(character_in_range(0xC2, 0xDF), + character_in_range(0x80, 0xBF)); +} + +TOML11_INLINE sequence utf8_3bytes(const spec&) +{ + return sequence(/*1~2 bytes = */either( + sequence(character (0xE0), character_in_range(0xA0, 0xBF)), + sequence(character_in_range(0xE1, 0xEC), character_in_range(0x80, 0xBF)), + sequence(character (0xED), character_in_range(0x80, 0x9F)), + sequence(character_in_range(0xEE, 0xEF), character_in_range(0x80, 0xBF)) + ), /*3rd byte = */ character_in_range(0x80, 0xBF)); +} + +TOML11_INLINE sequence utf8_4bytes(const spec&) +{ + return sequence(/*1~2 bytes = */either( + sequence(character (0xF0), character_in_range(0x90, 0xBF)), + sequence(character_in_range(0xF1, 0xF3), character_in_range(0x80, 0xBF)), + sequence(character (0xF4), character_in_range(0x80, 0x8F)) + ), character_in_range(0x80, 0xBF), character_in_range(0x80, 0xBF)); +} + +TOML11_INLINE non_ascii::non_ascii(const spec& s) noexcept + : scanner_(utf8_2bytes(s), utf8_3bytes(s), utf8_4bytes(s)) +{} + + +// =========================================================================== +// Whitespace + +TOML11_INLINE character_either wschar(const spec&) +{ + return character_either{char_type(' '), char_type('\t')}; +} + +TOML11_INLINE repeat_at_least ws(const spec& s) +{ + return repeat_at_least(0, wschar(s)); +} + +// =========================================================================== +// Newline + +TOML11_INLINE either newline(const spec&) +{ + return either(character(char_type('\n')), literal("\r\n")); +} + +// =========================================================================== +// Comments + +TOML11_INLINE either allowed_comment_char(const spec& s) +{ + if(s.v1_1_0_allow_control_characters_in_comments) + { + return either( + character_in_range(0x01, 0x09), + character_in_range(0x0E, 0x7F), + non_ascii(s) + ); + } + else + { + return either( + character(0x09), + character_in_range(0x20, 0x7E), + non_ascii(s) + ); + } +} + +// XXX Note that it does not take newline +TOML11_INLINE sequence comment(const spec& s) +{ + return sequence(character(char_type('#')), + repeat_at_least(0, allowed_comment_char(s))); +} + +// =========================================================================== +// Boolean + +TOML11_INLINE either boolean(const spec&) +{ + return either(literal("true"), literal("false")); +} + +// =========================================================================== +// Integer + +TOML11_INLINE digit::digit(const spec&) noexcept + : scanner_(char_type('0'), char_type('9')) +{} + +TOML11_INLINE alpha::alpha(const spec&) noexcept + : scanner_( + character_in_range(char_type('a'), char_type('z')), + character_in_range(char_type('A'), char_type('Z')) + ) +{} + +TOML11_INLINE hexdig::hexdig(const spec& s) noexcept + : scanner_( + digit(s), + character_in_range(char_type('a'), char_type('f')), + character_in_range(char_type('A'), char_type('F')) + ) +{} + +// non-digit-graph = ([a-zA-Z]|unicode mb char) +// graph = ([a-zA-Z0-9]|unicode mb char) +// suffix = _ non-digit-graph (graph | _graph) +TOML11_INLINE sequence num_suffix(const spec& s) +{ + const auto non_digit_graph = [&s]() { + return either( + alpha(s), + non_ascii(s) + ); + }; + const auto graph = [&s]() { + return either( + alpha(s), + digit(s), + non_ascii(s) + ); + }; + + return sequence( + character(char_type('_')), + non_digit_graph(), + repeat_at_least(0, + either( + sequence(character(char_type('_')), graph()), + graph() + ) + ) + ); +} + +TOML11_INLINE sequence dec_int(const spec& s) +{ + const auto digit19 = []() { + return character_in_range(char_type('1'), char_type('9')); + }; + return sequence( + maybe(character_either{char_type('-'), char_type('+')}), + either( + sequence( + digit19(), + repeat_at_least(1, + either( + digit(s), + sequence(character(char_type('_')), digit(s)) + ) + ) + ), + digit(s) + ) + ); +} + +TOML11_INLINE sequence hex_int(const spec& s) +{ + return sequence( + literal("0x"), + hexdig(s), + repeat_at_least(0, + either( + hexdig(s), + sequence(character(char_type('_')), hexdig(s)) + ) + ) + ); +} + +TOML11_INLINE sequence oct_int(const spec&) +{ + const auto digit07 = []() { + return character_in_range(char_type('0'), char_type('7')); + }; + return sequence( + literal("0o"), + digit07(), + repeat_at_least(0, + either( + digit07(), + sequence(character(char_type('_')), digit07()) + ) + ) + ); +} + +TOML11_INLINE sequence bin_int(const spec&) +{ + const auto digit01 = []() { + return character_either{char_type('0'), char_type('1')}; + }; + return sequence( + literal("0b"), + digit01(), + repeat_at_least(0, + either( + digit01(), + sequence(character(char_type('_')), digit01()) + ) + ) + ); +} + +TOML11_INLINE either integer(const spec& s) +{ + return either( + hex_int(s), + oct_int(s), + bin_int(s), + dec_int(s) + ); +} + + +// =========================================================================== +// Floating + +TOML11_INLINE sequence zero_prefixable_int(const spec& s) +{ + return sequence( + digit(s), + repeat_at_least(0, + either( + digit(s), + sequence(character('_'), digit(s)) + ) + ) + ); +} + +TOML11_INLINE sequence fractional_part(const spec& s) +{ + return sequence( + character('.'), + zero_prefixable_int(s) + ); +} + +TOML11_INLINE sequence exponent_part(const spec& s) +{ + return sequence( + character_either{char_type('e'), char_type('E')}, + maybe(character_either{char_type('+'), char_type('-')}), + zero_prefixable_int(s) + ); +} + +TOML11_INLINE sequence hex_floating(const spec& s) +{ + // C99 hexfloat (%a) + // [+-]? 0x ( [0-9a-fA-F]*\.[0-9a-fA-F]+ | [0-9a-fA-F]+\.? ) [pP] [+-]? [0-9]+ + + // - 0x(int).(frac)p[+-](int) + // - 0x(int).p[+-](int) + // - 0x.(frac)p[+-](int) + // - 0x(int)p[+-](int) + + return sequence( + maybe(character_either{char_type('+'), char_type('-')}), + character('0'), + character_either{char_type('x'), char_type('X')}, + either( + sequence( + repeat_at_least(0, hexdig(s)), + character('.'), + repeat_at_least(1, hexdig(s)) + ), + sequence( + repeat_at_least(1, hexdig(s)), + maybe(character('.')) + ) + ), + character_either{char_type('p'), char_type('P')}, + maybe(character_either{char_type('+'), char_type('-')}), + repeat_at_least(1, character_in_range('0', '9')) + ); +} + +TOML11_INLINE either floating(const spec& s) +{ + return either( + sequence( + dec_int(s), + either( + exponent_part(s), + sequence(fractional_part(s), maybe(exponent_part(s))) + ) + ), + sequence( + maybe(character_either{char_type('-'), char_type('+')}), + either(literal("inf"), literal("nan")) + ) + ); +} + +// =========================================================================== +// Datetime + +TOML11_INLINE sequence local_date(const spec& s) +{ + return sequence( + repeat_exact(4, digit(s)), + character('-'), + repeat_exact(2, digit(s)), + character('-'), + repeat_exact(2, digit(s)) + ); +} +TOML11_INLINE sequence local_time(const spec& s) +{ + auto time = sequence( + repeat_exact(2, digit(s)), + character(':'), + repeat_exact(2, digit(s)) + ); + + if(s.v1_1_0_make_seconds_optional) + { + time.push_back(maybe(sequence( + character(':'), + repeat_exact(2, digit(s)), + maybe(sequence(character('.'), repeat_at_least(1, digit(s)))) + ))); + } + else + { + time.push_back(character(':')); + time.push_back(repeat_exact(2, digit(s))); + time.push_back( + maybe(sequence(character('.'), repeat_at_least(1, digit(s)))) + ); + } + + return time; +} +TOML11_INLINE either time_offset(const spec& s) +{ + return either( + character_either{'Z', 'z'}, + sequence(character_either{'+', '-'}, + repeat_exact(2, digit(s)), + character(':'), + repeat_exact(2, digit(s)) + ) + ); +} +TOML11_INLINE sequence full_time(const spec& s) +{ + return sequence(local_time(s), time_offset(s)); +} +TOML11_INLINE character_either time_delim(const spec&) +{ + return character_either{'T', 't', ' '}; +} +TOML11_INLINE sequence local_datetime(const spec& s) +{ + return sequence(local_date(s), time_delim(s), local_time(s)); +} +TOML11_INLINE sequence offset_datetime(const spec& s) +{ + return sequence(local_date(s), time_delim(s), full_time(s)); +} + +// =========================================================================== +// String + +TOML11_INLINE sequence escaped(const spec& s) +{ + character_either escape_char{ + '\"','\\', 'b', 'f', 'n', 'r', 't' + }; + if(s.v1_1_0_add_escape_sequence_e) + { + escape_char.push_back(char_type('e')); + } + + either escape_seq( + std::move(escape_char), + sequence(character('u'), repeat_exact(4, hexdig(s))), + sequence(character('U'), repeat_exact(8, hexdig(s))) + ); + + if(s.v1_1_0_add_escape_sequence_x) + { + escape_seq.push_back( + sequence(character('x'), repeat_exact(2, hexdig(s))) + ); + } + + return sequence( + character('\\'), + std::move(escape_seq) + ); +} + +TOML11_INLINE either basic_char(const spec& s) +{ + const auto basic_unescaped = [&s]() { + return either( + wschar(s), + character(0x21), // 22 is " + character_in_range(0x23, 0x5B), // 5C is backslash + character_in_range(0x5D, 0x7E), // 7F is DEL + non_ascii(s) + ); + }; + return either(basic_unescaped(), escaped(s)); +} + +TOML11_INLINE sequence basic_string(const spec& s) +{ + return sequence( + character('"'), + repeat_at_least(0, basic_char(s)), + character('"') + ); +} + +// --------------------------------------------------------------------------- +// multiline string + +TOML11_INLINE sequence escaped_newline(const spec& s) +{ + return sequence( + character('\\'), ws(s), newline(s), + repeat_at_least(0, either(wschar(s), newline(s))) + ); +} + +TOML11_INLINE sequence ml_basic_string(const spec& s) +{ + const auto mlb_content = [&s]() { + return either(basic_char(s), newline(s), escaped_newline(s)); + }; + const auto mlb_quotes = []() { + return either(literal("\"\""), character('\"')); + }; + + return sequence( + literal("\"\"\""), + maybe(newline(s)), + repeat_at_least(0, mlb_content()), + repeat_at_least(0, + sequence( + mlb_quotes(), + repeat_at_least(1, mlb_content()) + ) + ), + // XXX """ and mlb_quotes are intentionally reordered to avoid + // unexpected match of mlb_quotes + literal("\"\"\""), + maybe(mlb_quotes()) + ); +} + +// --------------------------------------------------------------------------- +// literal string + +TOML11_INLINE either literal_char(const spec& s) +{ + return either( + character (0x09), + character_in_range(0x20, 0x26), + character_in_range(0x28, 0x7E), + non_ascii(s) + ); +} + +TOML11_INLINE sequence literal_string(const spec& s) +{ + return sequence( + character('\''), + repeat_at_least(0, literal_char(s)), + character('\'') + ); +} + +TOML11_INLINE sequence ml_literal_string(const spec& s) +{ + const auto mll_quotes = []() { + return either(literal("''"), character('\'')); + }; + const auto mll_content = [&s]() { + return either(literal_char(s), newline(s)); + }; + + return sequence( + literal("'''"), + maybe(newline(s)), + repeat_at_least(0, mll_content()), + repeat_at_least(0, sequence( + mll_quotes(), + repeat_at_least(1, mll_content()) + ) + ), + literal("'''"), + maybe(mll_quotes()) + // XXX ''' and mll_quotes are intentionally reordered to avoid + // unexpected match of mll_quotes + ); +} + +TOML11_INLINE either string(const spec& s) +{ + return either( + ml_basic_string(s), + ml_literal_string(s), + basic_string(s), + literal_string(s) + ); +} + +// =========================================================================== +// Keys + +// to keep `expected_chars` simple +TOML11_INLINE non_ascii_key_char::non_ascii_key_char(const spec& s) noexcept +{ + assert(s.v1_1_0_allow_non_english_in_bare_keys); + (void)s; // for NDEBUG +} + +TOML11_INLINE std::uint32_t non_ascii_key_char::read_utf8(location& loc) const +{ + // U+0000 ... U+0079 ; 0xxx_xxxx + // U+0080 ... U+07FF ; 110y_yyyx 10xx_xxxx; + // U+0800 ... U+FFFF ; 1110_yyyy 10yx_xxxx 10xx_xxxx + // U+010000 ... U+10FFFF; 1111_0yyy 10yy_xxxx 10xx_xxxx 10xx_xxxx + + const unsigned char b1 = loc.current(); loc.advance(1); + if(b1 < 0x80) + { + return static_cast(b1); + } + else if((b1 >> 5) == 6) // 0b110 == 6 + { + const auto b2 = loc.current(); loc.advance(1); + + const std::uint32_t c1 = b1 & ((1 << 5) - 1); + const std::uint32_t c2 = b2 & ((1 << 6) - 1); + const std::uint32_t codep = (c1 << 6) + c2; + + if(codep < 0x80) + { + return 0xFFFFFFFF; + } + return codep; + } + else if((b1 >> 4) == 14) // 0b1110 == 14 + { + const auto b2 = loc.current(); loc.advance(1); if(loc.eof()) {return 0xFFFFFFFF;} + const auto b3 = loc.current(); loc.advance(1); + + const std::uint32_t c1 = b1 & ((1 << 4) - 1); + const std::uint32_t c2 = b2 & ((1 << 6) - 1); + const std::uint32_t c3 = b3 & ((1 << 6) - 1); + + const std::uint32_t codep = (c1 << 12) + (c2 << 6) + c3; + if(codep < 0x800) + { + return 0xFFFFFFFF; + } + return codep; + } + else if((b1 >> 3) == 30) // 0b11110 == 30 + { + const auto b2 = loc.current(); loc.advance(1); if(loc.eof()) {return 0xFFFFFFFF;} + const auto b3 = loc.current(); loc.advance(1); if(loc.eof()) {return 0xFFFFFFFF;} + const auto b4 = loc.current(); loc.advance(1); + + const std::uint32_t c1 = b1 & ((1 << 3) - 1); + const std::uint32_t c2 = b2 & ((1 << 6) - 1); + const std::uint32_t c3 = b3 & ((1 << 6) - 1); + const std::uint32_t c4 = b4 & ((1 << 6) - 1); + const std::uint32_t codep = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4; + + if(codep < 0x10000) + { + return 0xFFFFFFFF; + } + return codep; + } + else // not a Unicode codepoint in UTF-8 + { + return 0xFFFFFFFF; + } +} + +TOML11_INLINE region non_ascii_key_char::scan(location& loc) const +{ + if(loc.eof()) {return region{};} + + const auto first = loc; + + const auto cp = read_utf8(loc); + + if(cp == 0xFFFFFFFF) + { + return region{}; + } + + // ALPHA / DIGIT / %x2D / %x5F ; a-z A-Z 0-9 - _ + // / %xB2 / %xB3 / %xB9 / %xBC-BE ; superscript digits, fractions + // / %xC0-D6 / %xD8-F6 / %xF8-37D ; non-symbol chars in Latin block + // / %x37F-1FFF ; exclude GREEK QUESTION MARK, which is basically a semi-colon + // / %x200C-200D / %x203F-2040 ; from General Punctuation Block, include the two tie symbols and ZWNJ, ZWJ + // / %x2070-218F / %x2460-24FF ; include super-/subscripts, letterlike/numberlike forms, enclosed alphanumerics + // / %x2C00-2FEF / %x3001-D7FF ; skip arrows, math, box drawing etc, skip 2FF0-3000 ideographic up/down markers and spaces + // / %xF900-FDCF / %xFDF0-FFFD ; skip D800-DFFF surrogate block, E000-F8FF Private Use area, FDD0-FDEF intended for process-internal use (unicode) + // / %x10000-EFFFF ; all chars outside BMP range, excluding Private Use planes (F0000-10FFFF) + + if(cp == 0xB2 || cp == 0xB3 || cp == 0xB9 || (0xBC <= cp && cp <= 0xBE) || + (0xC0 <= cp && cp <= 0xD6 ) || (0xD8 <= cp && cp <= 0xF6) || (0xF8 <= cp && cp <= 0x37D) || + (0x37F <= cp && cp <= 0x1FFF) || + (0x200C <= cp && cp <= 0x200D) || (0x203F <= cp && cp <= 0x2040) || + (0x2070 <= cp && cp <= 0x218F) || (0x2460 <= cp && cp <= 0x24FF) || + (0x2C00 <= cp && cp <= 0x2FEF) || (0x3001 <= cp && cp <= 0xD7FF) || + (0xF900 <= cp && cp <= 0xFDCF) || (0xFDF0 <= cp && cp <= 0xFFFD) || + (0x10000 <= cp && cp <= 0xEFFFF) ) + { + return region(first, loc); + } + loc = first; + return region{}; +} + +TOML11_INLINE repeat_at_least unquoted_key(const spec& s) +{ + auto keychar = either( + alpha(s), digit(s), character{0x2D}, character{0x5F} + ); + + if(s.v1_1_0_allow_non_english_in_bare_keys) + { + keychar.push_back(non_ascii_key_char(s)); + } + + return repeat_at_least(1, std::move(keychar)); +} + +TOML11_INLINE either quoted_key(const spec& s) +{ + return either(basic_string(s), literal_string(s)); +} + +TOML11_INLINE either simple_key(const spec& s) +{ + return either(unquoted_key(s), quoted_key(s)); +} + +TOML11_INLINE sequence dot_sep(const spec& s) +{ + return sequence(ws(s), character('.'), ws(s)); +} + +TOML11_INLINE sequence dotted_key(const spec& s) +{ + return sequence( + simple_key(s), + repeat_at_least(1, sequence(dot_sep(s), simple_key(s))) + ); +} + +TOML11_INLINE key::key(const spec& s) noexcept + : scanner_(dotted_key(s), simple_key(s)) +{} + +TOML11_INLINE sequence keyval_sep(const spec& s) +{ + return sequence(ws(s), character('='), ws(s)); +} + +// =========================================================================== +// Table key + +TOML11_INLINE sequence std_table(const spec& s) +{ + return sequence(character('['), ws(s), key(s), ws(s), character(']')); +} + +TOML11_INLINE sequence array_table(const spec& s) +{ + return sequence(literal("[["), ws(s), key(s), ws(s), literal("]]")); +} + +// =========================================================================== +// extension: null + +TOML11_INLINE literal null_value(const spec&) +{ + return literal("null"); +} + +} // namespace syntax +} // namespace detail +} // namespace toml +#endif // TOML11_SYNTAX_IMPL_HPP diff --git a/include/toml11/impl/value_t_impl.hpp b/include/toml11/impl/value_t_impl.hpp new file mode 100644 index 0000000..784dc8b --- /dev/null +++ b/include/toml11/impl/value_t_impl.hpp @@ -0,0 +1,40 @@ +#ifndef TOML11_VALUE_T_IMPL_HPP +#define TOML11_VALUE_T_IMPL_HPP + +#include "../fwd/value_t_fwd.hpp" + +#include +#include +#include + +namespace toml +{ + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, value_t t) +{ + switch(t) + { + case value_t::boolean : os << "boolean"; return os; + case value_t::integer : os << "integer"; return os; + case value_t::floating : os << "floating"; return os; + case value_t::string : os << "string"; return os; + case value_t::offset_datetime : os << "offset_datetime"; return os; + case value_t::local_datetime : os << "local_datetime"; return os; + case value_t::local_date : os << "local_date"; return os; + case value_t::local_time : os << "local_time"; return os; + case value_t::array : os << "array"; return os; + case value_t::table : os << "table"; return os; + case value_t::empty : os << "empty"; return os; + default : os << "unknown"; return os; + } +} + +TOML11_INLINE std::string to_string(value_t t) +{ + std::ostringstream oss; + oss << t; + return oss.str(); +} + +} // namespace toml +#endif // TOML11_VALUE_T_IMPL_HPP diff --git a/include/toml11/into.hpp b/include/toml11/into.hpp new file mode 100644 index 0000000..86a0020 --- /dev/null +++ b/include/toml11/into.hpp @@ -0,0 +1,17 @@ +#ifndef TOML11_INTO_HPP +#define TOML11_INTO_HPP + +namespace toml +{ + +template +struct into; +// { +// static toml::value into_toml(const T& user_defined_type) +// { +// // User-defined conversions ... +// } +// }; + +} // toml +#endif // TOML11_INTO_HPP diff --git a/include/toml11/literal.hpp b/include/toml11/literal.hpp new file mode 100644 index 0000000..e30e7a8 --- /dev/null +++ b/include/toml11/literal.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_LITERAL_HPP +#define TOML11_LITERAL_HPP + +#include "fwd/literal_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/literal_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_LITERAL_HPP diff --git a/include/toml11/location.hpp b/include/toml11/location.hpp new file mode 100644 index 0000000..fd23274 --- /dev/null +++ b/include/toml11/location.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_LOCATION_HPP +#define TOML11_LOCATION_HPP + +#include "fwd/location_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/location_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_LOCATION_HPP diff --git a/include/toml11/ordered_map.hpp b/include/toml11/ordered_map.hpp new file mode 100644 index 0000000..b9cd304 --- /dev/null +++ b/include/toml11/ordered_map.hpp @@ -0,0 +1,265 @@ +#ifndef TOML11_ORDERED_MAP_HPP +#define TOML11_ORDERED_MAP_HPP + +#include +#include +#include +#include + +namespace toml +{ + +namespace detail +{ +template +struct ordered_map_ebo_container +{ + Cmp cmp_; // empty base optimization for empty Cmp type +}; +} // detail + +template, + typename Allocator = std::allocator>> +class ordered_map : detail::ordered_map_ebo_container +{ + public: + using key_type = Key; + using mapped_type = Val; + using value_type = std::pair; + + using key_compare = Cmp; + using allocator_type = Allocator; + + using container_type = std::vector; + using reference = typename container_type::reference; + using pointer = typename container_type::pointer; + using const_reference = typename container_type::const_reference; + using const_pointer = typename container_type::const_pointer; + using iterator = typename container_type::iterator; + using const_iterator = typename container_type::const_iterator; + using size_type = typename container_type::size_type; + using difference_type = typename container_type::difference_type; + + private: + + using ebo_base = detail::ordered_map_ebo_container; + + public: + + ordered_map() = default; + ~ordered_map() = default; + ordered_map(const ordered_map&) = default; + ordered_map(ordered_map&&) = default; + ordered_map& operator=(const ordered_map&) = default; + ordered_map& operator=(ordered_map&&) = default; + + ordered_map(const ordered_map& other, const Allocator& alloc) + : container_(other.container_, alloc) + {} + ordered_map(ordered_map&& other, const Allocator& alloc) + : container_(std::move(other.container_), alloc) + {} + + explicit ordered_map(const Cmp& cmp, const Allocator& alloc = Allocator()) + : ebo_base{cmp}, container_(alloc) + {} + explicit ordered_map(const Allocator& alloc) + : container_(alloc) + {} + + template + ordered_map(InputIterator first, InputIterator last, const Cmp& cmp = Cmp(), const Allocator& alloc = Allocator()) + : ebo_base{cmp}, container_(first, last, alloc) + {} + template + ordered_map(InputIterator first, InputIterator last, const Allocator& alloc) + : container_(first, last, alloc) + {} + + ordered_map(std::initializer_list v, const Cmp& cmp = Cmp(), const Allocator& alloc = Allocator()) + : ebo_base{cmp}, container_(std::move(v), alloc) + {} + ordered_map(std::initializer_list v, const Allocator& alloc) + : container_(std::move(v), alloc) + {} + ordered_map& operator=(std::initializer_list v) + { + this->container_ = std::move(v); + return *this; + } + + iterator begin() noexcept {return container_.begin();} + iterator end() noexcept {return container_.end();} + const_iterator begin() const noexcept {return container_.begin();} + const_iterator end() const noexcept {return container_.end();} + const_iterator cbegin() const noexcept {return container_.cbegin();} + const_iterator cend() const noexcept {return container_.cend();} + + bool empty() const noexcept {return container_.empty();} + std::size_t size() const noexcept {return container_.size();} + std::size_t max_size() const noexcept {return container_.max_size();} + + void clear() {container_.clear();} + + void push_back(const value_type& v) + { + if(this->contains(v.first)) + { + throw std::out_of_range("ordered_map: value already exists"); + } + container_.push_back(v); + } + void push_back(value_type&& v) + { + if(this->contains(v.first)) + { + throw std::out_of_range("ordered_map: value already exists"); + } + container_.push_back(std::move(v)); + } + void emplace_back(key_type k, mapped_type v) + { + if(this->contains(k)) + { + throw std::out_of_range("ordered_map: value already exists"); + } + container_.emplace_back(std::move(k), std::move(v)); + } + void pop_back() {container_.pop_back();} + + void insert(value_type kv) + { + if(this->contains(kv.first)) + { + throw std::out_of_range("ordered_map: value already exists"); + } + container_.push_back(std::move(kv)); + } + void emplace(key_type k, mapped_type v) + { + if(this->contains(k)) + { + throw std::out_of_range("ordered_map: value already exists"); + } + container_.emplace_back(std::move(k), std::move(v)); + } + + std::size_t count(const key_type& key) const + { + if(this->find(key) != this->end()) + { + return 1; + } + else + { + return 0; + } + } + bool contains(const key_type& key) const + { + return this->find(key) != this->end(); + } + iterator find(const key_type& key) noexcept + { + return std::find_if(this->begin(), this->end(), + [&key, this](const value_type& v) {return this->cmp_(v.first, key);}); + } + const_iterator find(const key_type& key) const noexcept + { + return std::find_if(this->begin(), this->end(), + [&key, this](const value_type& v) {return this->cmp_(v.first, key);}); + } + + mapped_type& at(const key_type& k) + { + const auto iter = this->find(k); + if(iter == this->end()) + { + throw std::out_of_range("ordered_map: no such element"); + } + return iter->second; + } + mapped_type const& at(const key_type& k) const + { + const auto iter = this->find(k); + if(iter == this->end()) + { + throw std::out_of_range("ordered_map: no such element"); + } + return iter->second; + } + + mapped_type& operator[](const key_type& k) + { + const auto iter = this->find(k); + if(iter == this->end()) + { + this->container_.emplace_back(k, mapped_type{}); + return this->container_.back().second; + } + return iter->second; + } + + mapped_type const& operator[](const key_type& k) const + { + const auto iter = this->find(k); + if(iter == this->end()) + { + throw std::out_of_range("ordered_map: no such element"); + } + return iter->second; + } + + key_compare key_comp() const {return this->cmp_;} + + void swap(ordered_map& other) + { + container_.swap(other.container_); + } + + private: + + container_type container_; +}; + +template +bool operator==(const ordered_map& lhs, const ordered_map& rhs) +{ + return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); +} +template +bool operator!=(const ordered_map& lhs, const ordered_map& rhs) +{ + return !(lhs == rhs); +} +template +bool operator<(const ordered_map& lhs, const ordered_map& rhs) +{ + return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); +} +template +bool operator>(const ordered_map& lhs, const ordered_map& rhs) +{ + return rhs < lhs; +} +template +bool operator<=(const ordered_map& lhs, const ordered_map& rhs) +{ + return !(lhs > rhs); +} +template +bool operator>=(const ordered_map& lhs, const ordered_map& rhs) +{ + return !(lhs < rhs); +} + +template +void swap(ordered_map& lhs, ordered_map& rhs) +{ + lhs.swap(rhs); + return; +} + + +} // toml +#endif // TOML11_ORDERED_MAP_HPP diff --git a/include/toml11/parser.hpp b/include/toml11/parser.hpp new file mode 100644 index 0000000..3915650 --- /dev/null +++ b/include/toml11/parser.hpp @@ -0,0 +1,3829 @@ +#ifndef TOML11_PARSER_HPP +#define TOML11_PARSER_HPP + +#include "context.hpp" +#include "datetime.hpp" +#include "error_info.hpp" +#include "region.hpp" +#include "result.hpp" +#include "scanner.hpp" +#include "skip.hpp" +#include "syntax.hpp" +#include "value.hpp" + +#include +#include + +#include +#include + +#if defined(TOML11_HAS_FILESYSTEM) && TOML11_HAS_FILESYSTEM +#include +#endif + +namespace toml +{ + +struct syntax_error final : public ::toml::exception +{ + public: + syntax_error(std::string what_arg, std::vector err) + : what_(std::move(what_arg)), err_(std::move(err)) + {} + ~syntax_error() noexcept override = default; + + const char* what() const noexcept override {return what_.c_str();} + + std::vector const& errors() const noexcept + { + return err_; + } + + private: + std::string what_; + std::vector err_; +}; + +struct file_io_error final : public ::toml::exception +{ + public: + + file_io_error(const std::string& msg, const std::string& fname) + : errno_(cxx::make_nullopt()), + what_(msg + " \"" + fname + "\"") + {} + file_io_error(int errnum, const std::string& msg, const std::string& fname) + : errno_(errnum), + what_(msg + " \"" + fname + "\": errno=" + std::to_string(errnum)) + {} + ~file_io_error() noexcept override = default; + + const char* what() const noexcept override {return what_.c_str();} + + bool has_errno() const noexcept {return errno_.has_value();} + int get_errno() const noexcept {return errno_.value_or(0);} + + private: + + cxx::optional errno_; + std::string what_; +}; + +namespace detail +{ + +/* ============================================================================ + * __ ___ _ __ _ __ ___ _ _ + * / _/ _ \ ' \| ' \/ _ \ ' \ + * \__\___/_|_|_|_|_|_\___/_||_| + */ + +template +error_info make_syntax_error(std::string title, + const S& scanner, location loc, std::string suffix = "") +{ + auto msg = std::string("expected ") + scanner.expected_chars(loc); + auto src = source_location(region(loc)); + return make_error_info( + std::move(title), std::move(src), std::move(msg), std::move(suffix)); +} + + +/* ============================================================================ + * _ + * __ ___ _ __ _ __ ___ _ _| |_ + * / _/ _ \ ' \| ' \/ -_) ' \ _| + * \__\___/_|_|_|_|_|_\___|_||_\__| + */ + +template +result, error_info> +parse_comment_line(location& loc, context& ctx) +{ + const auto& spec = ctx.toml_spec(); + const auto first = loc; + + skip_whitespace(loc, ctx); + + const auto com_reg = syntax::comment(spec).scan(loc); + if(com_reg.is_ok()) + { + // once comment started, newline must follow (or reach EOF). + if( ! loc.eof() && ! syntax::newline(spec).scan(loc).is_ok()) + { + while( ! loc.eof()) // skip until newline to continue parsing + { + loc.advance(); + if(loc.current() == '\n') { /*skip LF*/ loc.advance(); break; } + } + return err(make_error_info("toml::parse_comment_line: " + "newline (LF / CRLF) or EOF is expected", + source_location(region(loc)), "but got this", + "Hint: most of the control characters are not allowed in comments")); + } + return ok(cxx::optional(com_reg.as_string())); + } + else + { + loc = first; // rollback whitespace to parse indent + return ok(cxx::optional(cxx::make_nullopt())); + } +} + +/* ============================================================================ + * ___ _ + * | _ ) ___ ___| |___ __ _ _ _ + * | _ \/ _ \/ _ \ / -_) _` | ' \ + * |___/\___/\___/_\___\__,_|_||_| + */ + +template +result, error_info> +parse_boolean(location& loc, const context& ctx) +{ + const auto& spec = ctx.toml_spec(); + + // ---------------------------------------------------------------------- + // check syntax + auto reg = syntax::boolean(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_boolean: " + "invalid boolean: boolean must be `true` or `false`, in lowercase. " + "string must be surrounded by `\"`", syntax::boolean(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + const auto str = reg.as_string(); + const auto val = [&str]() { + if(str == "true") + { + return true; + } + else + { + assert(str == "false"); + return false; + } + }(); + + // ---------------------------------------------------------------------- + // no format info for boolean + boolean_format_info fmt; + + return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); +} + +/* ============================================================================ + * ___ _ + * |_ _|_ _| |_ ___ __ _ ___ _ _ + * | || ' \ _/ -_) _` / -_) '_| + * |___|_||_\__\___\__, \___|_| + * |___/ + */ + +template +result, error_info> +parse_bin_integer(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + auto reg = syntax::bin_int(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_bin_integer: " + "invalid integer: bin_int must be like: 0b0101, 0b1111_0000", + syntax::bin_int(spec), loc)); + } + + auto str = reg.as_string(); + + integer_format_info fmt; + fmt.fmt = integer_format::bin; + fmt.width = str.size() - 2 - static_cast(std::count(str.begin(), str.end(), '_')); + + const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); + if(first_underscore != str.rend()) + { + fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); + } + + // skip prefix `0b` and zeros and underscores at the MSB + str.erase(str.begin(), std::find(std::next(str.begin(), 2), str.end(), '1')); + + // remove all `_` before calling TC::parse_int + str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); + + // 0b0000_0000 becomes empty. + if(str.empty()) { str = "0"; } + + const auto val = TC::parse_int(str, source_location(region(loc)), 2); + if(val.is_ok()) + { + return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); + } + else + { + loc = first; + return err(val.as_err()); + } +} + +// ---------------------------------------------------------------------------- + +template +result, error_info> +parse_oct_integer(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + auto reg = syntax::oct_int(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_oct_integer: " + "invalid integer: oct_int must be like: 0o775, 0o04_44", + syntax::oct_int(spec), loc)); + } + + auto str = reg.as_string(); + + integer_format_info fmt; + fmt.fmt = integer_format::oct; + fmt.width = str.size() - 2 - static_cast(std::count(str.begin(), str.end(), '_')); + + const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); + if(first_underscore != str.rend()) + { + fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); + } + + // skip prefix `0o` and zeros and underscores at the MSB + str.erase(str.begin(), std::find_if( + std::next(str.begin(), 2), str.end(), [](const char c) { + return c != '0' && c != '_'; + })); + + // remove all `_` before calling TC::parse_int + str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); + + // 0o0000_0000 becomes empty. + if(str.empty()) { str = "0"; } + + const auto val = TC::parse_int(str, source_location(region(loc)), 8); + if(val.is_ok()) + { + return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); + } + else + { + loc = first; + return err(val.as_err()); + } +} + +template +result, error_info> +parse_hex_integer(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + auto reg = syntax::hex_int(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_hex_integer: " + "invalid integer: hex_int must be like: 0xC0FFEE, 0xdead_beef", + syntax::hex_int(spec), loc)); + } + + auto str = reg.as_string(); + + integer_format_info fmt; + fmt.fmt = integer_format::hex; + fmt.width = str.size() - 2 - static_cast(std::count(str.begin(), str.end(), '_')); + + const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); + if(first_underscore != str.rend()) + { + fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); + } + + // skip prefix `0x` and zeros and underscores at the MSB + str.erase(str.begin(), std::find_if( + std::next(str.begin(), 2), str.end(), [](const char c) { + return c != '0' && c != '_'; + })); + + // remove all `_` before calling TC::parse_int + str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); + + // 0x0000_0000 becomes empty. + if(str.empty()) { str = "0"; } + + // prefix zero and _ is removed. check if it uses upper/lower case. + // if both upper and lower case letters are found, set upper=true. + const auto lower_not_found = std::find_if(str.begin(), str.end(), + [](const char c) { return std::islower(static_cast(c)) != 0; }) == str.end(); + const auto upper_found = std::find_if(str.begin(), str.end(), + [](const char c) { return std::isupper(static_cast(c)) != 0; }) != str.end(); + fmt.uppercase = lower_not_found || upper_found; + + const auto val = TC::parse_int(str, source_location(region(loc)), 16); + if(val.is_ok()) + { + return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); + } + else + { + loc = first; + return err(val.as_err()); + } +} + +template +result, error_info> +parse_dec_integer(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + // ---------------------------------------------------------------------- + // check syntax + auto reg = syntax::dec_int(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_dec_integer: " + "invalid integer: dec_int must be like: 42, 123_456_789", + syntax::dec_int(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + auto str = reg.as_string(); + + integer_format_info fmt; + fmt.fmt = integer_format::dec; + fmt.width = str.size() - static_cast(std::count(str.begin(), str.end(), '_')); + + const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); + if(first_underscore != str.rend()) + { + fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); + } + + // remove all `_` before calling TC::parse_int + str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); + + auto src = source_location(region(loc)); + const auto val = TC::parse_int(str, src, 10); + if(val.is_err()) + { + loc = first; + return err(val.as_err()); + } + + // ---------------------------------------------------------------------- + // parse suffix (extension) + + if(spec.ext_num_suffix && loc.current() == '_') + { + const auto sfx_reg = syntax::num_suffix(spec).scan(loc); + if( ! sfx_reg.is_ok()) + { + loc = first; + return err(make_error_info("toml::parse_dec_integer: " + "invalid suffix: should be `_ non-digit-graph (graph | _graph)`", + source_location(region(loc)), "here")); + } + auto sfx = sfx_reg.as_string(); + assert( ! sfx.empty() && sfx.front() == '_'); + sfx.erase(sfx.begin()); // remove the first `_` + + fmt.suffix = sfx; + } + + return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); +} + +template +result, error_info> +parse_integer(location& loc, const context& ctx) +{ + const auto first = loc; + + if( ! loc.eof() && (loc.current() == '+' || loc.current() == '-')) + { + // skip +/- to diagnose +0xDEADBEEF or -0b0011 (invalid). + // without this, +0xDEAD_BEEF will be parsed as a decimal int and + // unexpected "xDEAD_BEEF" will appear after integer "+0". + loc.advance(); + } + + if( ! loc.eof() && loc.current() == '0') + { + loc.advance(); + if(loc.eof()) + { + // `[+-]?0`. parse as an decimal integer. + loc = first; + return parse_dec_integer(loc, ctx); + } + + const auto prefix = loc.current(); + auto prefix_src = source_location(region(loc)); + + loc = first; + + if(prefix == 'b') {return parse_bin_integer(loc, ctx);} + if(prefix == 'o') {return parse_oct_integer(loc, ctx);} + if(prefix == 'x') {return parse_hex_integer(loc, ctx);} + + if(std::isdigit(prefix)) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_integer: " + "leading zero in an decimal integer is not allowed", + std::move(src), "leading zero")); + } + } + + loc = first; + return parse_dec_integer(loc, ctx); +} + +/* ============================================================================ + * ___ _ _ _ + * | __| |___ __ _| |_(_)_ _ __ _ + * | _|| / _ \/ _` | _| | ' \/ _` | + * |_| |_\___/\__,_|\__|_|_||_\__, | + * |___/ + */ + +template +result, error_info> +parse_floating(location& loc, const context& ctx) +{ + using floating_type = typename basic_value::floating_type; + + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + // ---------------------------------------------------------------------- + // check syntax + bool is_hex = false; + std::string str; + region reg; + if(spec.ext_hex_float && sequence(character('0'), character('x')).scan(loc).is_ok()) + { + loc = first; + is_hex = true; + + reg = syntax::hex_floating(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_floating: " + "invalid hex floating: float must be like: 0xABCp-3f", + syntax::floating(spec), loc)); + } + str = reg.as_string(); + } + else + { + reg = syntax::floating(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_floating: " + "invalid floating: float must be like: -3.14159_26535, 6.022e+23, " + "inf, or nan (lowercase).", syntax::floating(spec), loc)); + } + str = reg.as_string(); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + + floating_format_info fmt; + + if(is_hex) + { + fmt.fmt = floating_format::hex; + } + else + { + // since we already checked that the string conforms the TOML standard. + if(std::find(str.begin(), str.end(), 'e') != str.end() || + std::find(str.begin(), str.end(), 'E') != str.end()) + { + fmt.fmt = floating_format::scientific; // use exponent part + } + else + { + fmt.fmt = floating_format::fixed; // do not use exponent part + } + } + + str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); + + floating_type val{0}; + + if(str == "inf" || str == "+inf") + { + TOML11_CONSTEXPR_IF(std::numeric_limits::has_infinity) + { + val = std::numeric_limits::infinity(); + } + else + { + return err(make_error_info("toml::parse_floating: inf value found" + " but the current environment does not support inf. Please" + " make sure that the floating-point implementation conforms" + " IEEE 754/ISO 60559 international standard.", + source_location(region(loc)), + "floating_type: inf is not supported")); + } + } + else if(str == "-inf") + { + TOML11_CONSTEXPR_IF(std::numeric_limits::has_infinity) + { + val = -std::numeric_limits::infinity(); + } + else + { + return err(make_error_info("toml::parse_floating: inf value found" + " but the current environment does not support inf. Please" + " make sure that the floating-point implementation conforms" + " IEEE 754/ISO 60559 international standard.", + source_location(region(loc)), + "floating_type: inf is not supported")); + } + } + else if(str == "nan" || str == "+nan") + { + TOML11_CONSTEXPR_IF(std::numeric_limits::has_quiet_NaN) + { + val = std::numeric_limits::quiet_NaN(); + } + else TOML11_CONSTEXPR_IF(std::numeric_limits::has_signaling_NaN) + { + val = std::numeric_limits::signaling_NaN(); + } + else + { + return err(make_error_info("toml::parse_floating: NaN value found" + " but the current environment does not support NaN. Please" + " make sure that the floating-point implementation conforms" + " IEEE 754/ISO 60559 international standard.", + source_location(region(loc)), + "floating_type: NaN is not supported")); + } + } + else if(str == "-nan") + { + using std::copysign; + TOML11_CONSTEXPR_IF(std::numeric_limits::has_quiet_NaN) + { + val = copysign(std::numeric_limits::quiet_NaN(), floating_type(-1)); + } + else TOML11_CONSTEXPR_IF(std::numeric_limits::has_signaling_NaN) + { + val = copysign(std::numeric_limits::signaling_NaN(), floating_type(-1)); + } + else + { + return err(make_error_info("toml::parse_floating: NaN value found" + " but the current environment does not support NaN. Please" + " make sure that the floating-point implementation conforms" + " IEEE 754/ISO 60559 international standard.", + source_location(region(loc)), + "floating_type: NaN is not supported")); + } + } + else + { + // set precision + const auto has_sign = ! str.empty() && (str.front() == '+' || str.front() == '-'); + const auto decpoint = std::find(str.begin(), str.end(), '.'); + const auto exponent = std::find_if(str.begin(), str.end(), + [](const char c) { return c == 'e' || c == 'E'; }); + if(decpoint != str.end() && exponent != str.end()) + { + assert(decpoint < exponent); + } + + if(fmt.fmt == floating_format::scientific) + { + // total width + fmt.prec = static_cast(std::distance(str.begin(), exponent)); + if(has_sign) + { + fmt.prec -= 1; + } + if(decpoint != str.end()) + { + fmt.prec -= 1; + } + } + else if(fmt.fmt == floating_format::hex) + { + fmt.prec = std::numeric_limits::max_digits10; + } + else + { + // width after decimal point + fmt.prec = static_cast(std::distance(std::next(decpoint), exponent)); + } + + auto src = source_location(region(loc)); + const auto res = TC::parse_float(str, src, is_hex); + if(res.is_ok()) + { + val = res.as_ok(); + } + else + { + return err(res.as_err()); + } + } + + // ---------------------------------------------------------------------- + // parse suffix (extension) + + if(spec.ext_num_suffix && loc.current() == '_') + { + const auto sfx_reg = syntax::num_suffix(spec).scan(loc); + if( ! sfx_reg.is_ok()) + { + auto src = source_location(region(loc)); + loc = first; + return err(make_error_info("toml::parse_floating: " + "invalid suffix: should be `_ non-digit-graph (graph | _graph)`", + std::move(src), "here")); + } + auto sfx = sfx_reg.as_string(); + assert( ! sfx.empty() && sfx.front() == '_'); + sfx.erase(sfx.begin()); // remove the first `_` + + fmt.suffix = sfx; + } + + return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); +} + +/* ============================================================================ + * ___ _ _ _ + * | \ __ _| |_ ___| |_(_)_ __ ___ + * | |) / _` | _/ -_) _| | ' \/ -_) + * |___/\__,_|\__\___|\__|_|_|_|_\___| + */ + +// all the offset_datetime, local_datetime, local_date parses date part. +template +result, error_info> +parse_local_date_only(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + local_date_format_info fmt; + + // ---------------------------------------------------------------------- + // check syntax + auto reg = syntax::local_date(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_local_date: " + "invalid date: date must be like: 1234-05-06, yyyy-mm-dd.", + syntax::local_date(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + const auto str = reg.as_string(); + + // 0123456789 + // yyyy-mm-dd + const auto year_r = from_string(str.substr(0, 4)); + const auto month_r = from_string(str.substr(5, 2)); + const auto day_r = from_string(str.substr(8, 2)); + + if(year_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_date: " + "failed to read year `" + str.substr(0, 4) + "`", + std::move(src), "here")); + } + if(month_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_date: " + "failed to read month `" + str.substr(5, 2) + "`", + std::move(src), "here")); + } + if(day_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_date: " + "failed to read day `" + str.substr(8, 2) + "`", + std::move(src), "here")); + } + + const auto year = year_r.unwrap(); + const auto month = month_r.unwrap(); + const auto day = day_r.unwrap(); + + { + // We briefly check whether the input date is valid or not. + // Actually, because of the historical reasons, there are several + // edge cases, such as 1582/10/5-1582/10/14 (only in several countries). + // But here, we do not care about it. + // It makes the code complicated and there is only low probability + // that such a specific date is needed in practice. If someone need to + // validate date accurately, that means that the one need a specialized + // library for their purpose in another layer. + + const bool is_leap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); + const auto max_day = [month, is_leap]() { + if(month == 2) + { + return is_leap ? 29 : 28; + } + if(month == 4 || month == 6 || month == 9 || month == 11) + { + return 30; + } + return 31; + }(); + + if((month < 1 || 12 < month) || (day < 1 || max_day < day)) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_date: invalid date.", + std::move(src), "month must be 01-12, day must be any of " + "01-28,29,30,31 depending on the month/year.")); + } + } + + return ok(std::make_tuple( + local_date(year, static_cast(month - 1), day), + std::move(fmt), std::move(reg) + )); +} + +template +result, error_info> +parse_local_date(location& loc, const context& ctx) +{ + auto val_fmt_reg = parse_local_date_only(loc, ctx); + if(val_fmt_reg.is_err()) + { + return err(val_fmt_reg.unwrap_err()); + } + + auto val = std::move(std::get<0>(val_fmt_reg.unwrap())); + auto fmt = std::move(std::get<1>(val_fmt_reg.unwrap())); + auto reg = std::move(std::get<2>(val_fmt_reg.unwrap())); + + return ok(basic_value(std::move(val), std::move(fmt), {}, std::move(reg))); +} + +// all the offset_datetime, local_datetime, local_time parses date part. +template +result, error_info> +parse_local_time_only(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + local_time_format_info fmt; + + // ---------------------------------------------------------------------- + // check syntax + auto reg = syntax::local_time(spec).scan(loc); + if( ! reg.is_ok()) + { + if(spec.v1_1_0_make_seconds_optional) + { + return err(make_syntax_error("toml::parse_local_time: " + "invalid time: time must be HH:MM(:SS.sss) (seconds are optional)", + syntax::local_time(spec), loc)); + } + else + { + return err(make_syntax_error("toml::parse_local_time: " + "invalid time: time must be HH:MM:SS(.sss) (subseconds are optional)", + syntax::local_time(spec), loc)); + } + } + + // ---------------------------------------------------------------------- + // it matches. gen value + const auto str = reg.as_string(); + + // at least we have HH:MM. + // 01234 + // HH:MM + const auto hour_r = from_string(str.substr(0, 2)); + const auto minute_r = from_string(str.substr(3, 2)); + + if(hour_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: " + "failed to read hour `" + str.substr(0, 2) + "`", + std::move(src), "here")); + } + if(minute_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: " + "failed to read minute `" + str.substr(3, 2) + "`", + std::move(src), "here")); + } + + const auto hour = hour_r.unwrap(); + const auto minute = minute_r.unwrap(); + + if((hour < 0 || 24 <= hour) || (minute < 0 || 60 <= minute)) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: invalid time.", + std::move(src), "hour must be 00-23, minute must be 00-59.")); + } + + // ----------------------------------------------------------------------- + // we have hour and minute. + // Since toml v1.1.0, second and subsecond part becomes optional. + // Check the version and return if second does not exist. + + if(str.size() == 5 && spec.v1_1_0_make_seconds_optional) + { + fmt.has_seconds = false; + fmt.subsecond_precision = 0; + return ok(std::make_tuple(local_time(hour, minute, 0), std::move(fmt), std::move(reg))); + } + assert(str.at(5) == ':'); + + // we have at least `:SS` part. `.subseconds` are optional. + + // 0 1 + // 012345678901234 + // HH:MM:SS.subsec + const auto sec_r = from_string(str.substr(6, 2)); + if(sec_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: " + "failed to read second `" + str.substr(6, 2) + "`", + std::move(src), "here")); + } + const auto sec = sec_r.unwrap(); + + if(sec < 0 || 60 < sec) // :60 is allowed + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: invalid time.", + std::move(src), "second must be 00-60.")); + } + + if(str.size() == 8) + { + fmt.has_seconds = true; + fmt.subsecond_precision = 0; + return ok(std::make_tuple(local_time(hour, minute, sec), std::move(fmt), std::move(reg))); + } + + assert(str.at(8) == '.'); + + auto secfrac = str.substr(9, str.size() - 9); + + fmt.has_seconds = true; + fmt.subsecond_precision = secfrac.size(); + + while(secfrac.size() < 9) + { + secfrac += '0'; + } + assert(9 <= secfrac.size()); + const auto ms_r = from_string(secfrac.substr(0, 3)); + const auto us_r = from_string(secfrac.substr(3, 3)); + const auto ns_r = from_string(secfrac.substr(6, 3)); + + if(ms_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: " + "failed to read milliseconds `" + secfrac.substr(0, 3) + "`", + std::move(src), "here")); + } + if(us_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: " + "failed to read microseconds`" + str.substr(3, 3) + "`", + std::move(src), "here")); + } + if(ns_r.is_err()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_local_time: " + "failed to read nanoseconds`" + str.substr(6, 3) + "`", + std::move(src), "here")); + } + const auto ms = ms_r.unwrap(); + const auto us = us_r.unwrap(); + const auto ns = ns_r.unwrap(); + + return ok(std::make_tuple(local_time(hour, minute, sec, ms, us, ns), std::move(fmt), std::move(reg))); +} + +template +result, error_info> +parse_local_time(location& loc, const context& ctx) +{ + const auto first = loc; + + auto val_fmt_reg = parse_local_time_only(loc, ctx); + if(val_fmt_reg.is_err()) + { + return err(val_fmt_reg.unwrap_err()); + } + + auto val = std::move(std::get<0>(val_fmt_reg.unwrap())); + auto fmt = std::move(std::get<1>(val_fmt_reg.unwrap())); + auto reg = std::move(std::get<2>(val_fmt_reg.unwrap())); + + return ok(basic_value(std::move(val), std::move(fmt), {}, std::move(reg))); +} + +template +result, error_info> +parse_local_datetime(location& loc, const context& ctx) +{ + using char_type = location::char_type; + + const auto first = loc; + + local_datetime_format_info fmt; + + // ---------------------------------------------------------------------- + + auto date_fmt_reg = parse_local_date_only(loc, ctx); + if(date_fmt_reg.is_err()) + { + return err(date_fmt_reg.unwrap_err()); + } + + if(loc.current() == char_type('T')) + { + loc.advance(); + fmt.delimiter = datetime_delimiter_kind::upper_T; + } + else if(loc.current() == char_type('t')) + { + loc.advance(); + fmt.delimiter = datetime_delimiter_kind::lower_t; + } + else if(loc.current() == char_type(' ')) + { + loc.advance(); + fmt.delimiter = datetime_delimiter_kind::space; + } + else + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_local_datetime: " + "expect date-time delimiter `T`, `t` or ` `(space).", + std::move(src), "here")); + } + + auto time_fmt_reg = parse_local_time_only(loc, ctx); + if(time_fmt_reg.is_err()) + { + return err(time_fmt_reg.unwrap_err()); + } + + fmt.has_seconds = std::get<1>(time_fmt_reg.unwrap()).has_seconds; + fmt.subsecond_precision = std::get<1>(time_fmt_reg.unwrap()).subsecond_precision; + + // ---------------------------------------------------------------------- + + region reg(first, loc); + local_datetime val(std::get<0>(date_fmt_reg.unwrap()), + std::get<0>(time_fmt_reg.unwrap())); + + return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); +} + +template +result, error_info> +parse_offset_datetime(location& loc, const context& ctx) +{ + using char_type = location::char_type; + + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + offset_datetime_format_info fmt; + + // ---------------------------------------------------------------------- + // date part + + auto date_fmt_reg = parse_local_date_only(loc, ctx); + if(date_fmt_reg.is_err()) + { + return err(date_fmt_reg.unwrap_err()); + } + + // ---------------------------------------------------------------------- + // delimiter + + if(loc.current() == char_type('T')) + { + loc.advance(); + fmt.delimiter = datetime_delimiter_kind::upper_T; + } + else if(loc.current() == char_type('t')) + { + loc.advance(); + fmt.delimiter = datetime_delimiter_kind::lower_t; + } + else if(loc.current() == char_type(' ')) + { + loc.advance(); + fmt.delimiter = datetime_delimiter_kind::space; + } + else + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_offset_datetime: " + "expect date-time delimiter `T` or ` `(space).", std::move(src), "here" + )); + } + + // ---------------------------------------------------------------------- + // time part + + auto time_fmt_reg = parse_local_time_only(loc, ctx); + if(time_fmt_reg.is_err()) + { + return err(time_fmt_reg.unwrap_err()); + } + + fmt.has_seconds = std::get<1>(time_fmt_reg.unwrap()).has_seconds; + fmt.subsecond_precision = std::get<1>(time_fmt_reg.unwrap()).subsecond_precision; + + // ---------------------------------------------------------------------- + // offset part + + const auto ofs_reg = syntax::time_offset(spec).scan(loc); + if( ! ofs_reg.is_ok()) + { + return err(make_syntax_error("toml::parse_offset_datetime: " + "invalid offset: offset must be like: Z, +01:00, or -10:00.", + syntax::time_offset(spec), loc)); + } + + const auto ofs_str = ofs_reg.as_string(); + + time_offset offset(0, 0); + + assert(ofs_str.size() != 0); + + if(ofs_str.at(0) == char_type('+') || ofs_str.at(0) == char_type('-')) + { + const auto hour_r = from_string(ofs_str.substr(1, 2)); + const auto minute_r = from_string(ofs_str.substr(4, 2)); + if(hour_r.is_err()) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_offset_datetime: " + "Failed to read offset hour part", std::move(src), "here")); + } + if(minute_r.is_err()) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_offset_datetime: " + "Failed to read offset minute part", std::move(src), "here")); + } + const auto hour = hour_r.unwrap(); + const auto minute = minute_r.unwrap(); + + if(ofs_str.at(0) == '+') + { + offset = time_offset(hour, minute); + } + else + { + offset = time_offset(-hour, -minute); + } + } + else + { + assert(ofs_str.at(0) == char_type('Z') || ofs_str.at(0) == char_type('z')); + } + + if (offset.hour < -24 || 24 < offset.hour || + offset.minute < -60 || 60 < offset.minute) + { + return err(make_error_info("toml::parse_offset_datetime: " + "too large offset: |hour| <= 24, |minute| <= 60", + source_location(region(first, loc)), "here")); + } + + + // ---------------------------------------------------------------------- + + region reg(first, loc); + offset_datetime val(local_datetime(std::get<0>(date_fmt_reg.unwrap()), + std::get<0>(time_fmt_reg.unwrap())), + offset); + + return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); +} + +/* ============================================================================ + * ___ _ _ + * / __| |_ _ _(_)_ _ __ _ + * \__ \ _| '_| | ' \/ _` | + * |___/\__|_| |_|_||_\__, | + * |___/ + */ + +template +result::string_type, error_info> +parse_utf8_codepoint(const region& reg) +{ + using string_type = typename basic_value::string_type; + using char_type = typename string_type::value_type; + + // assert(reg.as_lines().size() == 1); // XXX heavy check + + const auto str = reg.as_string(); + assert( ! str.empty()); + assert(str.front() == 'u' || str.front() == 'U' || str.front() == 'x'); + + std::uint_least32_t codepoint; + std::istringstream iss(str.substr(1)); + iss >> std::hex >> codepoint; + + const auto to_char = [](const std::uint_least32_t i) noexcept -> char_type { + const auto uc = static_cast(i & 0xFF); + return cxx::bit_cast(uc); + }; + + string_type character; + if(codepoint < 0x80) // U+0000 ... U+0079 ; just an ASCII. + { + character += static_cast(codepoint); + } + else if(codepoint < 0x800) //U+0080 ... U+07FF + { + // 110yyyyx 10xxxxxx; 0x3f == 0b0011'1111 + character += to_char(0xC0|(codepoint >> 6 )); + character += to_char(0x80|(codepoint & 0x3F)); + } + else if(codepoint < 0x10000) // U+0800...U+FFFF + { + if(0xD800 <= codepoint && codepoint <= 0xDFFF) + { + auto src = source_location(reg); + return err(make_error_info("toml::parse_utf8_codepoint: " + "[0xD800, 0xDFFF] is not a valid UTF-8", + std::move(src), "here")); + } + assert(codepoint < 0xD800 || 0xDFFF < codepoint); + // 1110yyyy 10yxxxxx 10xxxxxx + character += to_char(0xE0| (codepoint >> 12)); + character += to_char(0x80|((codepoint >> 6 ) & 0x3F)); + character += to_char(0x80|((codepoint ) & 0x3F)); + } + else if(codepoint < 0x110000) // U+010000 ... U+10FFFF + { + // 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx + character += to_char(0xF0| (codepoint >> 18)); + character += to_char(0x80|((codepoint >> 12) & 0x3F)); + character += to_char(0x80|((codepoint >> 6 ) & 0x3F)); + character += to_char(0x80|((codepoint ) & 0x3F)); + } + else // out of UTF-8 region + { + auto src = source_location(reg); + return err(make_error_info("toml::parse_utf8_codepoint: " + "input codepoint is too large.", + std::move(src), "must be in range [0x00, 0x10FFFF]")); + } + return ok(character); +} + +template +result::string_type, error_info> +parse_escape_sequence(location& loc, const context& ctx) +{ + using string_type = typename basic_value::string_type; + using char_type = typename string_type::value_type; + + const auto& spec = ctx.toml_spec(); + + assert( ! loc.eof()); + assert(loc.current() == '\\'); + loc.advance(); // consume the first backslash + + string_type retval; + + if (loc.current() == '\\') { retval += char_type('\\'); loc.advance(); } + else if(loc.current() == '"') { retval += char_type('\"'); loc.advance(); } + else if(loc.current() == 'b') { retval += char_type('\b'); loc.advance(); } + else if(loc.current() == 'f') { retval += char_type('\f'); loc.advance(); } + else if(loc.current() == 'n') { retval += char_type('\n'); loc.advance(); } + else if(loc.current() == 'r') { retval += char_type('\r'); loc.advance(); } + else if(loc.current() == 't') { retval += char_type('\t'); loc.advance(); } + else if(spec.v1_1_0_add_escape_sequence_e && loc.current() == 'e') + { + retval += char_type('\x1b'); + loc.advance(); + } + else if(spec.v1_1_0_add_escape_sequence_x && loc.current() == 'x') + { + auto scanner = sequence(character('x'), repeat_exact(2, syntax::hexdig(spec))); + const auto reg = scanner.scan(loc); + if( ! reg.is_ok()) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_escape_sequence: " + "invalid token found in UTF-8 codepoint \\xhh", + std::move(src), "here")); + } + const auto utf8 = parse_utf8_codepoint(reg); + if(utf8.is_err()) + { + return err(utf8.as_err()); + } + retval += utf8.unwrap(); + } + else if(loc.current() == 'u') + { + auto scanner = sequence(character('u'), repeat_exact(4, syntax::hexdig(spec))); + const auto reg = scanner.scan(loc); + if( ! reg.is_ok()) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_escape_sequence: " + "invalid token found in UTF-8 codepoint \\uhhhh", + std::move(src), "here")); + } + const auto utf8 = parse_utf8_codepoint(reg); + if(utf8.is_err()) + { + return err(utf8.as_err()); + } + retval += utf8.unwrap(); + } + else if(loc.current() == 'U') + { + auto scanner = sequence(character('U'), repeat_exact(8, syntax::hexdig(spec))); + const auto reg = scanner.scan(loc); + if( ! reg.is_ok()) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_escape_sequence: " + "invalid token found in UTF-8 codepoint \\Uhhhhhhhh", + std::move(src), "here")); + } + const auto utf8 = parse_utf8_codepoint(reg); + if(utf8.is_err()) + { + return err(utf8.as_err()); + } + retval += utf8.unwrap(); + } + else + { + auto src = source_location(region(loc)); + std::string escape_seqs = "allowed escape seqs: \\\\, \\\", \\b, \\f, \\n, \\r, \\t"; + if(spec.v1_1_0_add_escape_sequence_e) + { + escape_seqs += ", \\e"; + } + if(spec.v1_1_0_add_escape_sequence_x) + { + escape_seqs += ", \\xhh"; + } + escape_seqs += ", \\uhhhh, or \\Uhhhhhhhh"; + + return err(make_error_info("toml::parse_escape_sequence: " + "unknown escape sequence.", std::move(src), escape_seqs)); + } + return ok(retval); +} + +template +result, error_info> +parse_ml_basic_string(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + string_format_info fmt; + fmt.fmt = string_format::multiline_basic; + + auto reg = syntax::ml_basic_string(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_ml_basic_string: " + "invalid string format", + syntax::ml_basic_string(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + + auto str = reg.as_string(); + + // we already checked that it starts with """ and ends with """. + assert(str.substr(0, 3) == "\"\"\""); + str.erase(0, 3); + + assert(str.size() >= 3); + assert(str.substr(str.size()-3, 3) == "\"\"\""); + str.erase(str.size()-3, 3); + + // the first newline just after """ is trimmed + if(str.size() >= 1 && str.at(0) == '\n') + { + str.erase(0, 1); + fmt.start_with_newline = true; + } + else if(str.size() >= 2 && str.at(0) == '\r' && str.at(1) == '\n') + { + str.erase(0, 2); + fmt.start_with_newline = true; + } + + using string_type = typename basic_value::string_type; + string_type val; + { + auto iter = str.cbegin(); + while(iter != str.cend()) + { + if(*iter == '\\') // remove whitespaces around escaped-newline + { + // we assume that the string is not too long to copy + auto loc2 = make_temporary_location(make_string(iter, str.cend())); + if(syntax::escaped_newline(spec).scan(loc2).is_ok()) + { + std::advance(iter, loc2.get_location()); // skip escaped newline and indent + // now iter points non-WS char + assert(iter == str.end() || (*iter != ' ' && *iter != '\t')); + } + else // normal escape seq. + { + auto esc = parse_escape_sequence(loc2, ctx); + + // syntax does not check its value. the unicode codepoint may be + // invalid, e.g. out-of-bound, [0xD800, 0xDFFF] + if(esc.is_err()) + { + return err(esc.unwrap_err()); + } + + val += esc.unwrap(); + std::advance(iter, loc2.get_location()); + } + } + else // we already checked the syntax. we don't need to check it again. + { + val += static_cast(*iter); + ++iter; + } + } + } + + return ok(basic_value( + std::move(val), std::move(fmt), {}, std::move(reg) + )); +} + +template +result::string_type, region>, error_info> +parse_basic_string_only(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + auto reg = syntax::basic_string(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_basic_string: " + "invalid string format", + syntax::basic_string(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + + auto str = reg.as_string(); + + assert(str.back() == '\"'); + str.pop_back(); + assert(str.at(0) == '\"'); + str.erase(0, 1); + + using string_type = typename basic_value::string_type; + using char_type = typename string_type::value_type; + string_type val; + + { + auto iter = str.begin(); + while(iter != str.end()) + { + if(*iter == '\\') + { + auto loc2 = make_temporary_location(make_string(iter, str.end())); + + auto esc = parse_escape_sequence(loc2, ctx); + + // syntax does not check its value. the unicode codepoint may be + // invalid, e.g. out-of-bound, [0xD800, 0xDFFF] + if(esc.is_err()) + { + return err(esc.unwrap_err()); + } + + val += esc.unwrap(); + std::advance(iter, loc2.get_location()); + } + else + { + val += char_type(*iter); // we already checked the syntax. + ++iter; + } + } + } + return ok(std::make_pair(val, reg)); +} + +template +result, error_info> +parse_basic_string(location& loc, const context& ctx) +{ + const auto first = loc; + + string_format_info fmt; + fmt.fmt = string_format::basic; + + auto val_res = parse_basic_string_only(loc, ctx); + if(val_res.is_err()) + { + return err(std::move(val_res.unwrap_err())); + } + auto val = std::move(val_res.unwrap().first ); + auto reg = std::move(val_res.unwrap().second); + + return ok(basic_value(std::move(val), std::move(fmt), {}, std::move(reg))); +} + +template +result, error_info> +parse_ml_literal_string(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + string_format_info fmt; + fmt.fmt = string_format::multiline_literal; + + auto reg = syntax::ml_literal_string(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_ml_literal_string: " + "invalid string format", + syntax::ml_literal_string(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + + auto str = reg.as_string(); + + assert(str.substr(0, 3) == "'''"); + assert(str.substr(str.size()-3, 3) == "'''"); + str.erase(0, 3); + str.erase(str.size()-3, 3); + + // the first newline just after """ is trimmed + if(str.size() >= 1 && str.at(0) == '\n') + { + str.erase(0, 1); + fmt.start_with_newline = true; + } + else if(str.size() >= 2 && str.at(0) == '\r' && str.at(1) == '\n') + { + str.erase(0, 2); + fmt.start_with_newline = true; + } + + using string_type = typename basic_value::string_type; + string_type val(str.begin(), str.end()); + + return ok(basic_value( + std::move(val), std::move(fmt), {}, std::move(reg) + )); +} + +template +result::string_type, region>, error_info> +parse_literal_string_only(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + auto reg = syntax::literal_string(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_literal_string: " + "invalid string format", + syntax::literal_string(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + + auto str = reg.as_string(); + + assert(str.back() == '\''); + str.pop_back(); + assert(str.at(0) == '\''); + str.erase(0, 1); + + using string_type = typename basic_value::string_type; + string_type val(str.begin(), str.end()); + + return ok(std::make_pair(std::move(val), std::move(reg))); +} + +template +result, error_info> +parse_literal_string(location& loc, const context& ctx) +{ + const auto first = loc; + + string_format_info fmt; + fmt.fmt = string_format::literal; + + auto val_res = parse_literal_string_only(loc, ctx); + if(val_res.is_err()) + { + return err(std::move(val_res.unwrap_err())); + } + auto val = std::move(val_res.unwrap().first ); + auto reg = std::move(val_res.unwrap().second); + + return ok(basic_value( + std::move(val), std::move(fmt), {}, std::move(reg) + )); +} + +template +result, error_info> +parse_string(location& loc, const context& ctx) +{ + const auto first = loc; + + if( ! loc.eof() && loc.current() == '"') + { + if(literal("\"\"\"").scan(loc).is_ok()) + { + loc = first; + return parse_ml_basic_string(loc, ctx); + } + else + { + loc = first; + return parse_basic_string(loc, ctx); + } + } + else if( ! loc.eof() && loc.current() == '\'') + { + if(literal("'''").scan(loc).is_ok()) + { + loc = first; + return parse_ml_literal_string(loc, ctx); + } + else + { + loc = first; + return parse_literal_string(loc, ctx); + } + } + else + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_string: " + "not a string", std::move(src), "here")); + } +} + +template +result, error_info> +parse_null(location& loc, const context& ctx) +{ + const auto& spec = ctx.toml_spec(); + if( ! spec.ext_null_value) + { + return err(make_error_info("toml::parse_null: " + "invalid spec: spec.ext_null_value must be true.", + source_location(region(loc)), "here")); + } + + // ---------------------------------------------------------------------- + // check syntax + auto reg = syntax::null_value(spec).scan(loc); + if( ! reg.is_ok()) + { + return err(make_syntax_error("toml::parse_null: " + "invalid null: null must be lowercase. ", + syntax::null_value(spec), loc)); + } + + // ---------------------------------------------------------------------- + // it matches. gen value + + // ---------------------------------------------------------------------- + // no format info for boolean + + return ok(basic_value(detail::none_t{}, std::move(reg))); +} + +/* ============================================================================ + * _ __ + * | |/ /___ _ _ + * | ' +result::key_type, error_info> +parse_simple_key(location& loc, const context& ctx) +{ + using key_type = typename basic_value::key_type; + const auto& spec = ctx.toml_spec(); + + if(loc.current() == '\"') + { + auto str_res = parse_basic_string_only(loc, ctx); + if(str_res.is_ok()) + { + return ok(std::move(str_res.unwrap().first)); + } + else + { + return err(std::move(str_res.unwrap_err())); + } + } + else if(loc.current() == '\'') + { + auto str_res = parse_literal_string_only(loc, ctx); + if(str_res.is_ok()) + { + return ok(std::move(str_res.unwrap().first)); + } + else + { + return err(std::move(str_res.unwrap_err())); + } + } + + // bare key. + + if(const auto bare = syntax::unquoted_key(spec).scan(loc)) + { + return ok(string_conv(bare.as_string())); + } + else + { + std::string postfix; + if(spec.v1_1_0_allow_non_english_in_bare_keys) + { + postfix = "Hint: Not all Unicode characters are allowed as bare key.\n"; + } + else + { + postfix = "Hint: non-ASCII scripts are allowed in toml v1.1.0, but not in v1.0.0.\n"; + } + return err(make_syntax_error("toml::parse_simple_key: " + "invalid key: key must be \"quoted\", 'quoted-literal', or bare key.", + syntax::unquoted_key(spec), loc, postfix)); + } +} + +// dotted key become vector of keys +template +result::key_type>, region>, error_info> +parse_key(location& loc, const context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + using key_type = typename basic_value::key_type; + std::vector keys; + while( ! loc.eof()) + { + auto key = parse_simple_key(loc, ctx); + if( ! key.is_ok()) + { + return err(key.unwrap_err()); + } + keys.push_back(std::move(key.unwrap())); + + auto reg = syntax::dot_sep(spec).scan(loc); + if( ! reg.is_ok()) + { + break; + } + } + if(keys.empty()) + { + auto src = source_location(region(first)); + return err(make_error_info("toml::parse_key: expected a new key, " + "but got nothing", std::move(src), "reached EOF")); + } + + return ok(std::make_pair(std::move(keys), region(first, loc))); +} + +// ============================================================================ + +// forward-decl to implement parse_array and parse_table +template +result, error_info> +parse_value(location&, context& ctx); + +template +result::key_type>, region>, + basic_value + >, error_info> +parse_key_value_pair(location& loc, context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + auto key_res = parse_key(loc, ctx); + if(key_res.is_err()) + { + loc = first; + return err(key_res.unwrap_err()); + } + + if( ! syntax::keyval_sep(spec).scan(loc).is_ok()) + { + auto e = make_syntax_error("toml::parse_key_value_pair: " + "invalid key value separator `=`", syntax::keyval_sep(spec), loc); + loc = first; + return err(std::move(e)); + } + + auto v_res = parse_value(loc, ctx); + if(v_res.is_err()) + { + // loc = first; + return err(v_res.unwrap_err()); + } + return ok(std::make_pair(std::move(key_res.unwrap()), std::move(v_res.unwrap()))); +} + +/* ============================================================================ + * __ _ _ _ _ _ __ _ _ _ + * / _` | '_| '_/ _` | || | + * \__,_|_| |_| \__,_|\_, | + * |__/ + */ + +// array(and multiline inline table with `{` and `}`) has the following format. +// `[` +// (ws|newline|comment-line)? (value) (ws|newline|comment-line)? `,` +// (ws|newline|comment-line)? (value) (ws|newline|comment-line)? `,` +// ... +// (ws|newline|comment-line)? (value) (ws|newline|comment-line)? (`,`)? +// (ws|newline|comment-line)? `]` +// it skips (ws|newline|comment-line) and returns the token. +template +struct multiline_spacer +{ + using comment_type = typename TC::comment_type; + bool newline_found; + indent_char indent_type; + std::int32_t indent; + comment_type comments; +}; +template +std::ostream& operator<<(std::ostream& os, const multiline_spacer& sp) +{ + os << "{newline=" << sp.newline_found << ", "; + os << "indent_type=" << sp.indent_type << ", "; + os << "indent=" << sp.indent << ", "; + os << "comments=" << sp.comments.size() << "}"; + return os; +} + +template +cxx::optional> +skip_multiline_spacer(location& loc, context& ctx, const bool newline_found = false) +{ + const auto& spec = ctx.toml_spec(); + + multiline_spacer spacer; + spacer.newline_found = newline_found; + spacer.indent_type = indent_char::none; + spacer.indent = 0; + spacer.comments.clear(); + + bool spacer_found = false; + while( ! loc.eof()) + { + if(auto comm = sequence(syntax::comment(spec), syntax::newline(spec)).scan(loc)) + { + spacer.newline_found = true; + auto comment = comm.as_string(); + if( ! comment.empty() && comment.back() == '\n') + { + comment.pop_back(); + if (!comment.empty() && comment.back() == '\r') + { + comment.pop_back(); + } + } + + spacer.comments.push_back(std::move(comment)); + spacer.indent_type = indent_char::none; + spacer.indent = 0; + spacer_found = true; + } + else if(auto nl = syntax::newline(spec).scan(loc)) + { + spacer.newline_found = true; + spacer.comments.clear(); + spacer.indent_type = indent_char::none; + spacer.indent = 0; + spacer_found = true; + } + else if(auto sp = repeat_at_least(1, character(cxx::bit_cast(' '))).scan(loc)) + { + spacer.indent_type = indent_char::space; + spacer.indent = static_cast(sp.length()); + spacer_found = true; + } + else if(auto tabs = repeat_at_least(1, character(cxx::bit_cast('\t'))).scan(loc)) + { + spacer.indent_type = indent_char::tab; + spacer.indent = static_cast(tabs.length()); + spacer_found = true; + } + else + { + break; // done + } + } + if( ! spacer_found) + { + return cxx::make_nullopt(); + } + return spacer; +} + +// not an [[array.of.tables]]. It parses ["this", "type"] +template +result, error_info> +parse_array(location& loc, context& ctx) +{ + const auto num_errors = ctx.errors().size(); + + const auto first = loc; + + if(loc.eof() || loc.current() != '[') + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_array: " + "The next token is not an array", std::move(src), "here")); + } + loc.advance(); + + typename basic_value::array_type val; + + array_format_info fmt; + fmt.fmt = array_format::oneline; + fmt.indent_type = indent_char::none; + + auto spacer = skip_multiline_spacer(loc, ctx); + if(spacer.has_value() && spacer.value().newline_found) + { + fmt.fmt = array_format::multiline; + } + + bool comma_found = true; + while( ! loc.eof()) + { + if(loc.current() == location::char_type(']')) + { + if(spacer.has_value() && spacer.value().newline_found && + spacer.value().indent_type != indent_char::none) + { + fmt.indent_type = spacer.value().indent_type; + fmt.closing_indent = spacer.value().indent; + } + break; + } + + if( ! comma_found) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_array: " + "expected value-separator `,` or closing `]`", + std::move(src), "here")); + } + + if(spacer.has_value() && spacer.value().newline_found && + spacer.value().indent_type != indent_char::none) + { + fmt.indent_type = spacer.value().indent_type; + fmt.body_indent = spacer.value().indent; + } + + if(auto elem_res = parse_value(loc, ctx)) + { + auto elem = std::move(elem_res.unwrap()); + + if(spacer.has_value()) // copy previous comments to value + { + elem.comments() = std::move(spacer.value().comments); + } + + // parse spaces between a value and a comma + // array = [ + // 42 , # the answer + // ^^^^ + // 3.14 # pi + // , 2.71 ^^^^ + // ^^ + spacer = skip_multiline_spacer(loc, ctx); + if(spacer.has_value()) + { + for(std::size_t i=0; i( + std::move(val), std::move(fmt), {}, region(first, loc) + )); +} + +/* ============================================================================ + * _ _ _ _ _ _ + * (_)_ _ | (_)_ _ ___ | |_ __ _| |__| |___ + * | | ' \| | | ' \/ -_) | _/ _` | '_ \ / -_) + * |_|_||_|_|_|_||_\___| \__\__,_|_.__/_\___| + */ + +// ---------------------------------------------------------------------------- +// insert_value is the most complicated part of the toml spec. +// +// To parse a toml file correctly, we sometimes need to check an exising value +// is appendable or not. +// +// For example while parsing an inline array of tables, +// +// ```toml +// aot = [ +// {a = "foo"}, +// {a = "bar", b = "baz"}, +// ] +// ``` +// +// this `aot` is appendable until parser reaches to `]`. After that, it becomes +// non-appendable. +// +// On the other hand, a normal array of tables, such as +// +// ```toml +// [[aot]] +// a = "foo" +// +// [[aot]] +// a = "bar" +// b = "baz" +// ``` +// This `[[aot]]` is appendable until the parser reaches to the EOF. +// +// +// It becomes a bit more difficult in case of dotted keys. +// In TOML, it is allowed to append a key-value pair to a table that is +// *implicitly* defined by a subtable definitino. +// +// ```toml +// [x.y.z] +// w = 123 +// +// [x] +// a = "foo" # OK. x is defined implicitly by `[x.y.z]`. +// ``` +// +// But if the table is defined by a dotted keys, it is not appendable. +// +// ```toml +// [x] +// y.z.w = 123 +// +// [x.y] +// # ERROR. x.y is already defined by a dotted table in the previous table. +// ``` +// +// Also, reopening a table using dotted keys is invalid. +// +// ```toml +// [x.y.z] +// w = 123 +// +// [x] +// y.z.v = 42 # ERROR. [x.y.z] is already defined. +// ``` +// +// +// ```toml +// [a] +// b.c = "foo" +// b.d = "bar" +// ``` +// +// +// ```toml +// a.b = "foo" +// [a] +// c = "bar" # ERROR +// ``` +// +// In summary, +// - a table must be defined only once. +// - assignment to an exising table is possible only when: +// - defining a subtable [x.y] to an existing table [x]. +// - defining supertable [x] explicitly after [x.y]. +// - adding dotted keys in the same table. + +enum class inserting_value_kind : std::uint8_t +{ + std_table, // insert [standard.table] + array_table, // insert [[array.of.tables]] + dotted_keys // insert a.b.c = "this" +}; + +template +result*, error_info> +insert_value(const inserting_value_kind kind, + typename basic_value::table_type* current_table_ptr, + const std::vector::key_type>& keys, region key_reg, + basic_value val) +{ + using value_type = basic_value; + using array_type = typename basic_value::array_type; + using table_type = typename basic_value::table_type; + + auto key_loc = source_location(key_reg); + + assert( ! keys.empty()); + + // dotted key can insert to dotted key tables defined at the same level. + // dotted key can NOT reopen a table even if it is implcitly-defined one. + // + // [x.y.z] # define x and x.y implicitly. + // a = 42 + // + // [x] # reopening implcitly defined table + // r.s.t = 3.14 # VALID r and r.s are new tables. + // r.s.u = 2.71 # VALID r and r.s are dotted-key tables. valid. + // + // y.z.b = "foo" # INVALID x.y.z are multiline table, not a dotted key. + // y.c = "bar" # INVALID x.y is implicit multiline table, not a dotted key. + + // a table cannot reopen dotted-key tables. + // + // [t1] + // t2.t3.v = 0 + // [t1.t2] # INVALID t1.t2 is defined as a dotted-key table. + + for(std::size_t i=0; i{}, key_reg)); + + assert(current_table.at(key).is_table()); + current_table_ptr = std::addressof(current_table.at(key).as_table()); + } + else if (found->second.is_table()) + { + const auto fmt = found->second.as_table_fmt().fmt; + if(fmt == table_format::oneline || fmt == table_format::multiline_oneline) + { + // foo = {bar = "baz"} or foo = { \n bar = "baz" \n } + return err(make_error_info("toml::insert_value: " + "failed to insert a value: inline table is immutable", + key_loc, "inserting this", + found->second.location(), "to this table")); + } + // dotted key cannot reopen a table. + if(kind ==inserting_value_kind::dotted_keys && fmt != table_format::dotted) + { + return err(make_error_info("toml::insert_value: " + "reopening a table using dotted keys", + key_loc, "dotted key cannot reopen a table", + found->second.location(), "this table is already closed")); + } + assert(found->second.is_table()); + current_table_ptr = std::addressof(found->second.as_table()); + } + else if(found->second.is_array_of_tables()) + { + // aot = [{this = "type", of = "aot"}] # cannot be reopened + if(found->second.as_array_fmt().fmt != array_format::array_of_tables) + { + return err(make_error_info("toml::insert_value:" + "inline array of tables are immutable", + key_loc, "inserting this", + found->second.location(), "inline array of tables")); + } + // appending to [[aot]] + + if(kind == inserting_value_kind::dotted_keys) + { + // [[array.of.tables]] + // [array.of] # reopening supertable is okay + // tables.x = "foo" # appending `x` to the first table + return err(make_error_info("toml::insert_value:" + "dotted key cannot reopen an array-of-tables", + key_loc, "inserting this", + found->second.location(), "to this array-of-tables.")); + } + + // insert_value_by_dotkeys::std_table + // [[array.of.tables]] + // [array.of.tables.subtable] # appending to the last aot + // + // insert_value_by_dotkeys::array_table + // [[array.of.tables]] + // [[array.of.tables.subtable]] # appending to the last aot + auto& current_array_table = found->second.as_array().back(); + + assert(current_array_table.is_table()); + current_table_ptr = std::addressof(current_array_table.as_table()); + } + else + { + return err(make_error_info("toml::insert_value: " + "failed to insert a value, value already exists", + key_loc, "while inserting this", + found->second.location(), "non-table value already exists")); + } + } + else // this is the last key. insert a new value. + { + switch(kind) + { + case inserting_value_kind::dotted_keys: + { + if(current_table.find(key) != current_table.end()) + { + return err(make_error_info("toml::insert_value: " + "failed to insert a value, value already exists", + key_loc, "inserting this", + current_table.at(key).location(), "but value already exists")); + } + current_table.emplace(key, std::move(val)); + return ok(std::addressof(current_table.at(key))); + } + case inserting_value_kind::std_table: + { + // defining a new table or reopening supertable + auto found = current_table.find(key); + if(found == current_table.end()) // define a new aot + { + current_table.emplace(key, std::move(val)); + return ok(std::addressof(current_table.at(key))); + } + else // the table is already defined, reopen it + { + // assigning a [std.table]. it must be an implicit table. + auto& target = found->second; + if( ! target.is_table() || // could be an array-of-tables + target.as_table_fmt().fmt != table_format::implicit) + { + return err(make_error_info("toml::insert_value: " + "failed to insert a table, table already defined", + key_loc, "inserting this", + target.location(), "this table is explicitly defined")); + } + + // merge table + for(const auto& kv : val.as_table()) + { + if(target.contains(kv.first)) + { + // [x.y.z] + // w = "foo" + // [x] + // y = "bar" + return err(make_error_info("toml::insert_value: " + "failed to insert a table, table keys conflict to each other", + key_loc, "inserting this table", + kv.second.location(), "having this value", + target.at(kv.first).location(), "already defined here")); + } + else + { + target[kv.first] = kv.second; + } + } + // change implicit -> explicit + target.as_table_fmt().fmt = table_format::multiline; + // change definition region + change_region_of_value(target, val); + + return ok(std::addressof(current_table.at(key))); + } + } + case inserting_value_kind::array_table: + { + auto found = current_table.find(key); + if(found == current_table.end()) // define a new aot + { + array_format_info fmt; + fmt.fmt = array_format::array_of_tables; + fmt.indent_type = indent_char::none; + + current_table.emplace(key, value_type( + array_type{ std::move(val) }, std::move(fmt), + std::vector{}, std::move(key_reg) + )); + + assert( ! current_table.at(key).as_array().empty()); + return ok(std::addressof(current_table.at(key).as_array().back())); + } + else // the array is already defined, append to it + { + if( ! found->second.is_array_of_tables()) + { + return err(make_error_info("toml::insert_value: " + "failed to insert an array of tables, value already exists", + key_loc, "while inserting this", + found->second.location(), "non-table value already exists")); + } + if(found->second.as_array_fmt().fmt != array_format::array_of_tables) + { + return err(make_error_info("toml::insert_value: " + "failed to insert a table, inline array of tables is immutable", + key_loc, "while inserting this", + found->second.location(), "this is inline array-of-tables")); + } + found->second.as_array().push_back(std::move(val)); + assert( ! current_table.at(key).as_array().empty()); + return ok(std::addressof(current_table.at(key).as_array().back())); + } + } + default: {assert(false);} + } + } + } + return err(make_error_info("toml::insert_key: no keys found", + std::move(key_loc), "here")); +} + +// ---------------------------------------------------------------------------- + +template +result, error_info> +parse_inline_table(location& loc, context& ctx) +{ + using table_type = typename basic_value::table_type; + + const auto num_errors = ctx.errors().size(); + + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + if(loc.eof() || loc.current() != '{') + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_inline_table: " + "The next token is not an inline table", std::move(src), "here")); + } + loc.advance(); + + table_type table; + table_format_info fmt; + fmt.fmt = table_format::oneline; + fmt.indent_type = indent_char::none; + + cxx::optional> spacer(cxx::make_nullopt()); + + if(spec.v1_1_0_allow_newlines_in_inline_tables) + { + spacer = skip_multiline_spacer(loc, ctx); + if(spacer.has_value() && spacer.value().newline_found) + { + fmt.fmt = table_format::multiline_oneline; + } + } + else + { + skip_whitespace(loc, ctx); + } + + bool still_empty = true; + bool comma_found = false; + while( ! loc.eof()) + { + // closing! + if(loc.current() == '}') + { + if(comma_found && ! spec.v1_1_0_allow_trailing_comma_in_inline_tables) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_inline_table: trailing " + "comma is not allowed in TOML-v1.0.0)", std::move(src), "here")); + } + + if(spec.v1_1_0_allow_newlines_in_inline_tables) + { + if(spacer.has_value() && spacer.value().newline_found && + spacer.value().indent_type != indent_char::none) + { + fmt.indent_type = spacer.value().indent_type; + fmt.closing_indent = spacer.value().indent; + } + } + break; + } + + // if we already found a value and didn't found `,` nor `}`, error. + if( ! comma_found && ! still_empty) + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_inline_table: " + "expected value-separator `,` or closing `}`", + std::move(src), "here")); + } + + // parse indent. + if(spacer.has_value() && spacer.value().newline_found && + spacer.value().indent_type != indent_char::none) + { + fmt.indent_type = spacer.value().indent_type; + fmt.body_indent = spacer.value().indent; + } + + still_empty = false; // parsing a value... + if(auto kv_res = parse_key_value_pair(loc, ctx)) + { + auto keys = std::move(kv_res.unwrap().first.first); + auto key_reg = std::move(kv_res.unwrap().first.second); + auto val = std::move(kv_res.unwrap().second); + + auto ins_res = insert_value(inserting_value_kind::dotted_keys, + std::addressof(table), keys, std::move(key_reg), std::move(val)); + if(ins_res.is_err()) + { + ctx.report_error(std::move(ins_res.unwrap_err())); + // we need to skip until the next value (or end of the table) + // because we don't have valid kv pair. + while( ! loc.eof()) + { + const auto c = loc.current(); + if(c == ',' || c == '\n' || c == '}') + { + comma_found = (c == ','); + break; + } + loc.advance(); + } + continue; + } + + // if comment line follows immediately(without newline) after `,`, then + // the comment is for the elem. we need to check if comment follows `,`. + // + // (key) = (val) (ws|newline|comment-line)? `,` (ws)? (comment)? + + if(spec.v1_1_0_allow_newlines_in_inline_tables) + { + if(spacer.has_value()) // copy previous comments to value + { + for(std::size_t i=0; icomments().push_back(spacer.value().comments.at(i)); + } + } + spacer = skip_multiline_spacer(loc, ctx); + if(spacer.has_value()) + { + for(std::size_t i=0; icomments().push_back(spacer.value().comments.at(i)); + } + if(spacer.value().newline_found) + { + fmt.fmt = table_format::multiline_oneline; + if(spacer.value().indent_type != indent_char::none) + { + fmt.indent_type = spacer.value().indent_type; + fmt.body_indent = spacer.value().indent; + } + } + } + } + else + { + skip_whitespace(loc, ctx); + } + + comma_found = character(',').scan(loc).is_ok(); + + if(spec.v1_1_0_allow_newlines_in_inline_tables) + { + auto com_res = parse_comment_line(loc, ctx); + if(com_res.is_err()) + { + ctx.report_error(com_res.unwrap_err()); + } + const bool comment_found = com_res.is_ok() && com_res.unwrap().has_value(); + if(comment_found) + { + fmt.fmt = table_format::multiline_oneline; + ins_res.unwrap()->comments().push_back(com_res.unwrap().value()); + } + if(comma_found) + { + spacer = skip_multiline_spacer(loc, ctx, comment_found); + if(spacer.has_value() && spacer.value().newline_found) + { + fmt.fmt = table_format::multiline_oneline; + } + } + } + else + { + skip_whitespace(loc, ctx); + } + } + else + { + ctx.report_error(std::move(kv_res.unwrap_err())); + while( ! loc.eof()) + { + if(loc.current() == '}') + { + break; + } + if( ! spec.v1_1_0_allow_newlines_in_inline_tables && loc.current() == '\n') + { + break; + } + loc.advance(); + } + break; + } + } + + if(loc.current() != '}') + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_inline_table: " + "missing closing bracket `}`", + std::move(src), "expected `}`, reached line end")); + } + else + { + loc.advance(); // skip } + } + + // any error reported from this function + if(num_errors < ctx.errors().size()) + { + assert(ctx.has_error()); // already reported + return err(ctx.pop_last_error()); + } + + basic_value retval( + std::move(table), std::move(fmt), {}, region(first, loc)); + + return ok(std::move(retval)); +} + +/* ============================================================================ + * _ + * __ ____ _| |_ _ ___ + * \ V / _` | | || / -_) + * \_/\__,_|_|\_,_\___| + */ + +template +result +guess_number_type(const location& first, const context& ctx) +{ + const auto& spec = ctx.toml_spec(); + location loc = first; + + if(syntax::offset_datetime(spec).scan(loc).is_ok()) + { + return ok(value_t::offset_datetime); + } + loc = first; + + if(syntax::local_datetime(spec).scan(loc).is_ok()) + { + const auto curr = loc.current(); + // if offset_datetime contains bad offset, it syntax::offset_datetime + // fails to scan it. + if(curr == '+' || curr == '-') + { + return err(make_syntax_error("bad offset: must be [+-]HH:MM or Z", + syntax::time_offset(spec), loc, std::string( + "Hint: valid : +09:00, -05:30\n" + "Hint: invalid: +9:00, -5:30\n"))); + } + return ok(value_t::local_datetime); + } + loc = first; + + if(syntax::local_date(spec).scan(loc).is_ok()) + { + // bad time may appear after this. + + if( ! loc.eof()) + { + const auto c = loc.current(); + if(c == 'T' || c == 't') + { + loc.advance(); + + return err(make_syntax_error("bad time: must be HH:MM:SS.subsec", + syntax::local_time(spec), loc, std::string( + "Hint: valid : 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999\n" + "Hint: invalid: 1979-05-27T7:32:00, 1979-05-27 17:32\n"))); + } + if(c == ' ') + { + // A space is allowed as a delimiter between local time. + // But there is a case where bad time follows a space. + // - invalid: 2019-06-16 7:00:00 + // - valid : 2019-06-16 07:00:00 + loc.advance(); + if( ! loc.eof() && ('0' <= loc.current() && loc.current() <= '9')) + { + return err(make_syntax_error("bad time: must be HH:MM:SS.subsec", + syntax::local_time(spec), loc, std::string( + "Hint: valid : 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999\n" + "Hint: invalid: 1979-05-27T7:32:00, 1979-05-27 17:32\n"))); + } + } + if('0' <= c && c <= '9') + { + return err(make_syntax_error("bad datetime: missing T or space", + character_either{'T', 't', ' '}, loc, std::string( + "Hint: valid : 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999\n" + "Hint: invalid: 1979-05-27T7:32:00, 1979-05-27 17:32\n"))); + } + } + return ok(value_t::local_date); + } + loc = first; + + if(syntax::local_time(spec).scan(loc).is_ok()) + { + return ok(value_t::local_time); + } + loc = first; + + if(syntax::floating(spec).scan(loc).is_ok()) + { + if( ! loc.eof() && loc.current() == '_') + { + if(spec.ext_num_suffix && syntax::num_suffix(spec).scan(loc).is_ok()) + { + return ok(value_t::floating); + } + auto src = source_location(region(loc)); + return err(make_error_info( + "bad float: `_` must be surrounded by digits", + std::move(src), "invalid underscore", + "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" + "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n")); + } + return ok(value_t::floating); + } + loc = first; + + if(spec.ext_hex_float) + { + if(syntax::hex_floating(spec).scan(loc).is_ok()) + { + if( ! loc.eof() && loc.current() == '_') + { + if(spec.ext_num_suffix && syntax::num_suffix(spec).scan(loc).is_ok()) + { + return ok(value_t::floating); + } + auto src = source_location(region(loc)); + return err(make_error_info( + "bad float: `_` must be surrounded by digits", + std::move(src), "invalid underscore", + "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" + "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n")); + } + return ok(value_t::floating); + } + loc = first; + } + + if(auto int_reg = syntax::integer(spec).scan(loc)) + { + if( ! loc.eof()) + { + const auto c = loc.current(); + if(c == '_') + { + if(spec.ext_num_suffix && syntax::num_suffix(spec).scan(loc).is_ok()) + { + return ok(value_t::integer); + } + + if(int_reg.length() <= 2 && (int_reg.as_string() == "0" || + int_reg.as_string() == "-0" || int_reg.as_string() == "+0")) + { + auto src = source_location(region(loc)); + return err(make_error_info( + "bad integer: leading zero is not allowed in decimal int", + std::move(src), "leading zero", + "Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" + "Hint: invalid: _42, 1__000, 0123\n")); + } + else + { + auto src = source_location(region(loc)); + return err(make_error_info( + "bad integer: `_` must be surrounded by digits", + std::move(src), "invalid underscore", + "Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" + "Hint: invalid: _42, 1__000, 0123\n")); + } + } + if('0' <= c && c <= '9') + { + if(loc.current() == '0') + { + loc.retrace(); + return err(make_error_info( + "bad integer: leading zero", + source_location(region(loc)), "leading zero is not allowed", + std::string("Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" + "Hint: invalid: _42, 1__000, 0123\n") + )); + } + else // invalid digits, especially in oct/bin ints. + { + return err(make_error_info( + "bad integer: invalid digit after an integer", + source_location(region(loc)), "this digit is not allowed", + std::string("Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" + "Hint: invalid: _42, 1__000, 0123\n") + )); + } + } + if(c == ':' || c == '-') + { + auto src = source_location(region(loc)); + return err(make_error_info("bad datetime: invalid format", + std::move(src), "here", + std::string("Hint: valid : 1979-05-27T07:32:00-07:00, 1979-05-27 07:32:00.999999Z\n" + "Hint: invalid: 1979-05-27T7:32:00-7:00, 1979-05-27 7:32-00:30") + )); + } + if(c == '.' || c == 'e' || c == 'E') + { + auto src = source_location(region(loc)); + return err(make_error_info("bad float: invalid format", + std::move(src), "here", std::string( + "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" + "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n"))); + } + } + return ok(value_t::integer); + } + if( ! loc.eof() && loc.current() == '.') + { + auto src = source_location(region(loc)); + return err(make_error_info("bad float: integer part is required before decimal point", + std::move(src), "missing integer part", std::string( + "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" + "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n") + )); + } + if( ! loc.eof() && loc.current() == '_') + { + auto src = source_location(region(loc)); + return err(make_error_info("bad number: `_` must be surrounded by digits", + std::move(src), "digits required before `_`", std::string( + "Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" + "Hint: invalid: _42, 1__000, 0123\n") + )); + } + + auto src = source_location(region(loc)); + return err(make_error_info("bad format: unknown value appeared", + std::move(src), "here")); +} + +template +result +guess_value_type(const location& loc, const context& ctx) +{ + const auto& sp = ctx.toml_spec(); + location inner(loc); + + switch(loc.current()) + { + case '"' : {return ok(value_t::string); } + case '\'': {return ok(value_t::string); } + case '[' : {return ok(value_t::array); } + case '{' : {return ok(value_t::table); } + case 't' : + { + return ok(value_t::boolean); + } + case 'f' : + { + return ok(value_t::boolean); + } + case 'T' : // invalid boolean. + { + return err(make_syntax_error("toml::parse_value: " + "`true` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::boolean(sp), inner)); + } + case 'F' : + { + return err(make_syntax_error("toml::parse_value: " + "`false` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::boolean(sp), inner)); + } + case 'i' : // inf or string without quotes(syntax error). + { + if(literal("inf").scan(inner).is_ok()) + { + return ok(value_t::floating); + } + else + { + return err(make_syntax_error("toml::parse_value: " + "`inf` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::floating(sp), inner)); + } + } + case 'I' : // Inf or string without quotes(syntax error). + { + return err(make_syntax_error("toml::parse_value: " + "`inf` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::floating(sp), inner)); + } + case 'n' : // nan or null-extension + { + if(sp.ext_null_value) + { + if(literal("nan").scan(inner).is_ok()) + { + return ok(value_t::floating); + } + else if(literal("null").scan(inner).is_ok()) + { + return ok(value_t::empty); + } + else + { + return err(make_syntax_error("toml::parse_value: " + "Both `nan` and `null` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::floating(sp), inner)); + } + } + else // must be nan. + { + if(literal("nan").scan(inner).is_ok()) + { + return ok(value_t::floating); + } + else + { + return err(make_syntax_error("toml::parse_value: " + "`nan` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::floating(sp), inner)); + } + } + } + case 'N' : // nan or null-extension + { + if(sp.ext_null_value) + { + return err(make_syntax_error("toml::parse_value: " + "Both `nan` and `null` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::floating(sp), inner)); + } + else + { + return err(make_syntax_error("toml::parse_value: " + "`nan` must be in lowercase. " + "A string must be surrounded by quotes.", + syntax::floating(sp), inner)); + } + } + default : + { + return guess_number_type(loc, ctx); + } + } +} + +template +result, error_info> +parse_value(location& loc, context& ctx) +{ + const auto ty_res = guess_value_type(loc, ctx); + if(ty_res.is_err()) + { + return err(ty_res.unwrap_err()); + } + + switch(ty_res.unwrap()) + { + case value_t::empty: + { + if(ctx.toml_spec().ext_null_value) + { + return parse_null(loc, ctx); + } + else + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_value: unknown value appeared", + std::move(src), "here")); + } + } + case value_t::boolean : {return parse_boolean (loc, ctx);} + case value_t::integer : {return parse_integer (loc, ctx);} + case value_t::floating : {return parse_floating (loc, ctx);} + case value_t::string : {return parse_string (loc, ctx);} + case value_t::offset_datetime: {return parse_offset_datetime(loc, ctx);} + case value_t::local_datetime : {return parse_local_datetime (loc, ctx);} + case value_t::local_date : {return parse_local_date (loc, ctx);} + case value_t::local_time : {return parse_local_time (loc, ctx);} + case value_t::array : {return parse_array (loc, ctx);} + case value_t::table : {return parse_inline_table (loc, ctx);} + default: + { + auto src = source_location(region(loc)); + return err(make_error_info("toml::parse_value: unknown value appeared", + std::move(src), "here")); + } + } +} + +/* ============================================================================ + * _____ _ _ + * |_ _|_ _| |__| |___ + * | |/ _` | '_ \ / -_) + * |_|\__,_|_.__/_\___| + */ + +template +result::key_type>, region>, error_info> +parse_table_key(location& loc, context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + auto reg = syntax::std_table(spec).scan(loc); + if(!reg.is_ok()) + { + return err(make_syntax_error("toml::parse_table_key: invalid table key", + syntax::std_table(spec), loc)); + } + + loc = first; + loc.advance(); // skip [ + skip_whitespace(loc, ctx); + + auto keys_res = parse_key(loc, ctx); + if(keys_res.is_err()) + { + return err(std::move(keys_res.unwrap_err())); + } + + skip_whitespace(loc, ctx); + loc.advance(); // ] + + return ok(std::make_pair(std::move(keys_res.unwrap().first), std::move(reg))); +} + +template +result::key_type>, region>, error_info> +parse_array_table_key(location& loc, context& ctx) +{ + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + auto reg = syntax::array_table(spec).scan(loc); + if(!reg.is_ok()) + { + return err(make_syntax_error("toml::parse_array_table_key: invalid array-of-tables key", + syntax::array_table(spec), loc)); + } + + loc = first; + loc.advance(); // [ + loc.advance(); // [ + skip_whitespace(loc, ctx); + + auto keys_res = parse_key(loc, ctx); + if(keys_res.is_err()) + { + return err(std::move(keys_res.unwrap_err())); + } + + skip_whitespace(loc, ctx); + loc.advance(); // ] + loc.advance(); // ] + + return ok(std::make_pair(std::move(keys_res.unwrap().first), std::move(reg))); +} + +// called after reading [table.keys] and comments around it. +// Since table may already contain a subtable ([x.y.z] can be defined before [x]), +// the table that is being parsed is passed as an argument. +template +result +parse_table(location& loc, context& ctx, basic_value& table) +{ + assert(table.is_table()); + + const auto num_errors = ctx.errors().size(); + const auto& spec = ctx.toml_spec(); + + // clear indent info + table.as_table_fmt().indent_type = indent_char::none; + + bool newline_found = true; + while( ! loc.eof()) + { + const auto start = loc; + + auto sp = skip_multiline_spacer(loc, ctx, newline_found); + + // if reached to EOF, the table ends here. return. + if(loc.eof()) + { + break; + } + // if next table is comming, return. + if(sequence(syntax::ws(spec), character('[')).scan(loc).is_ok()) + { + loc = start; + break; + } + // otherwise, it should be a key-value pair. + newline_found = newline_found || (sp.has_value() && sp.value().newline_found); + if( ! newline_found) + { + return err(make_error_info("toml::parse_table: " + "newline (LF / CRLF) or EOF is expected", + source_location(region(loc)), "here")); + } + if(sp.has_value() && sp.value().indent_type != indent_char::none) + { + table.as_table_fmt().indent_type = sp.value().indent_type; + table.as_table_fmt().body_indent = sp.value().indent; + } + + newline_found = false; // reset + if(auto kv_res = parse_key_value_pair(loc, ctx)) + { + auto keys = std::move(kv_res.unwrap().first.first); + auto key_reg = std::move(kv_res.unwrap().first.second); + auto val = std::move(kv_res.unwrap().second); + + if(sp.has_value()) + { + for(const auto& com : sp.value().comments) + { + val.comments().push_back(com); + } + } + + if(auto com_res = parse_comment_line(loc, ctx)) + { + if(auto com_opt = com_res.unwrap()) + { + val.comments().push_back(com_opt.value()); + newline_found = true; // comment includes newline at the end + } + } + else + { + ctx.report_error(std::move(com_res.unwrap_err())); + } + + auto ins_res = insert_value(inserting_value_kind::dotted_keys, + std::addressof(table.as_table()), + keys, std::move(key_reg), std::move(val)); + if(ins_res.is_err()) + { + ctx.report_error(std::move(ins_res.unwrap_err())); + } + } + else + { + ctx.report_error(std::move(kv_res.unwrap_err())); + skip_key_value_pair(loc, ctx); + } + } + + if(num_errors < ctx.errors().size()) + { + assert(ctx.has_error()); // already reported + return err(ctx.pop_last_error()); + } + return ok(); +} + +template +result, std::vector> +parse_file(location& loc, context& ctx) +{ + using value_type = basic_value; + using table_type = typename value_type::table_type; + + const auto first = loc; + const auto& spec = ctx.toml_spec(); + + if(loc.eof()) + { + return ok(value_type(table_type(), table_format_info{}, {}, region(loc))); + } + + value_type root(table_type(), table_format_info{}, {}, region(loc)); + root.as_table_fmt().fmt = table_format::multiline; + root.as_table_fmt().indent_type = indent_char::none; + + // parse top comment. + // + // ```toml + // # this is a comment for the top-level table. + // + // key = "the first value" + // ``` + // + // ```toml + // # this is a comment for "the first value". + // key = "the first value" + // ``` + while( ! loc.eof()) + { + if(auto com_res = parse_comment_line(loc, ctx)) + { + if(auto com_opt = com_res.unwrap()) + { + root.comments().push_back(std::move(com_opt.value())); + } + else // no comment found. + { + // if it is not an empty line, clear the root comment. + if( ! sequence(syntax::ws(spec), syntax::newline(spec)).scan(loc).is_ok()) + { + loc = first; + root.comments().clear(); + } + break; + } + } + else + { + ctx.report_error(std::move(com_res.unwrap_err())); + skip_comment_block(loc, ctx); + } + } + + // parse root table + { + const auto res = parse_table(loc, ctx, root); + if(res.is_err()) + { + ctx.report_error(std::move(res.unwrap_err())); + skip_until_next_table(loc, ctx); + } + } + + // parse tables + + while( ! loc.eof()) + { + auto sp = skip_multiline_spacer(loc, ctx, /*newline_found=*/true); + + if(auto key_res = parse_array_table_key(loc, ctx)) + { + auto key = std::move(std::get<0>(key_res.unwrap())); + auto reg = std::move(std::get<1>(key_res.unwrap())); + + std::vector com; + if(sp.has_value()) + { + for(std::size_t i=0; i(table_type()); + auto res = parse_table(loc, ctx, tmp); + if(res.is_err()) + { + ctx.report_error(res.unwrap_err()); + skip_until_next_table(loc, ctx); + } + continue; + } + + auto tab_ptr = inserted.unwrap(); + assert(tab_ptr); + + const auto tab_res = parse_table(loc, ctx, *tab_ptr); + if(tab_res.is_err()) + { + ctx.report_error(tab_res.unwrap_err()); + skip_until_next_table(loc, ctx); + } + + // parse_table first clears `indent_type`. + // to keep header indent info, we must store it later. + if(sp.has_value() && sp.value().indent_type != indent_char::none) + { + tab_ptr->as_table_fmt().indent_type = sp.value().indent_type; + tab_ptr->as_table_fmt().name_indent = sp.value().indent; + } + continue; + } + if(auto key_res = parse_table_key(loc, ctx)) + { + auto key = std::move(std::get<0>(key_res.unwrap())); + auto reg = std::move(std::get<1>(key_res.unwrap())); + + std::vector com; + if(sp.has_value()) + { + for(std::size_t i=0; i(table_type()); + auto res = parse_table(loc, ctx, tmp); + if(res.is_err()) + { + ctx.report_error(res.unwrap_err()); + skip_until_next_table(loc, ctx); + } + continue; + } + + auto tab_ptr = inserted.unwrap(); + assert(tab_ptr); + + const auto tab_res = parse_table(loc, ctx, *tab_ptr); + if(tab_res.is_err()) + { + ctx.report_error(tab_res.unwrap_err()); + skip_until_next_table(loc, ctx); + } + if(sp.has_value() && sp.value().indent_type != indent_char::none) + { + tab_ptr->as_table_fmt().indent_type = sp.value().indent_type; + tab_ptr->as_table_fmt().name_indent = sp.value().indent; + } + continue; + } + + // does not match array_table nor std_table. report an error. + const auto keytop = loc; + const auto maybe_array_of_tables = literal("[[").scan(loc).is_ok(); + loc = keytop; + + if(maybe_array_of_tables) + { + ctx.report_error(make_syntax_error("toml::parse_file: invalid array-table key", + syntax::array_table(spec), loc)); + } + else + { + ctx.report_error(make_syntax_error("toml::parse_file: invalid table key", + syntax::std_table(spec), loc)); + } + skip_until_next_table(loc, ctx); + } + + if( ! ctx.errors().empty()) + { + return err(std::move(ctx.errors())); + } + return ok(std::move(root)); +} + +template +result, std::vector> +parse_impl(std::vector cs, std::string fname, const spec& s) +{ + using value_type = basic_value; + using table_type = typename value_type::table_type; + + // an empty file is a valid toml file. + if(cs.empty()) + { + auto src = std::make_shared>(std::move(cs)); + location loc(std::move(src), std::move(fname)); + return ok(value_type(table_type(), table_format_info{}, std::vector{}, region(loc))); + } + + // to simplify parser, add newline at the end if there is no LF. + // But, if it has raw CR, the file is invalid (in TOML, CR is not a valid + // newline char). if it ends with CR, do not add LF and report it. + if(cs.back() != '\n' && cs.back() != '\r') + { + cs.push_back('\n'); + } + + auto src = std::make_shared>(std::move(cs)); + + location loc(std::move(src), std::move(fname)); + + // skip BOM if found + if(loc.source()->size() >= 3) + { + auto first = loc; + + const auto c0 = loc.current(); loc.advance(); + const auto c1 = loc.current(); loc.advance(); + const auto c2 = loc.current(); loc.advance(); + + const auto bom_found = (c0 == 0xEF) && (c1 == 0xBB) && (c2 == 0xBF); + if( ! bom_found) + { + loc = first; + } + } + + context ctx(s); + + return parse_file(loc, ctx); +} + +} // detail + +// ----------------------------------------------------------------------------- +// parse(byte array) + +template +result, std::vector> +try_parse(std::vector content, std::string filename, + spec s = spec::default_version()) +{ + return detail::parse_impl(std::move(content), std::move(filename), std::move(s)); +} +template +basic_value +parse(std::vector content, std::string filename, + spec s = spec::default_version()) +{ + auto res = try_parse(std::move(content), std::move(filename), std::move(s)); + if(res.is_ok()) + { + return res.unwrap(); + } + else + { + std::string msg; + for(const auto& err : res.unwrap_err()) + { + msg += format_error(err); + } + throw syntax_error(std::move(msg), std::move(res.unwrap_err())); + } +} + +// ----------------------------------------------------------------------------- +// parse(istream) + +template +result, std::vector> +try_parse(std::istream& is, std::string fname = "unknown file", spec s = spec::default_version()) +{ + const auto beg = is.tellg(); + is.seekg(0, std::ios::end); + const auto end = is.tellg(); + const auto fsize = end - beg; + is.seekg(beg); + + // read whole file as a sequence of char + assert(fsize >= 0); + std::vector letters(static_cast(fsize), '\0'); + is.read(reinterpret_cast(letters.data()), static_cast(fsize)); + + return detail::parse_impl(std::move(letters), std::move(fname), std::move(s)); +} + +template +basic_value parse(std::istream& is, std::string fname = "unknown file", spec s = spec::default_version()) +{ + auto res = try_parse(is, std::move(fname), std::move(s)); + if(res.is_ok()) + { + return res.unwrap(); + } + else + { + std::string msg; + for(const auto& err : res.unwrap_err()) + { + msg += format_error(err); + } + throw syntax_error(std::move(msg), std::move(res.unwrap_err())); + } +} + +// ----------------------------------------------------------------------------- +// parse(filename) + +template +result, std::vector> +try_parse(std::string fname, spec s = spec::default_version()) +{ + std::ifstream ifs(fname, std::ios_base::binary); + if(!ifs.good()) + { + std::vector e; + e.push_back(error_info("toml::parse: Error opening file \"" + fname + "\"", {})); + return err(std::move(e)); + } + ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); + + return try_parse(ifs, std::move(fname), std::move(s)); +} + +template +basic_value parse(std::string fname, spec s = spec::default_version()) +{ + std::ifstream ifs(fname, std::ios_base::binary); + if(!ifs.good()) + { + throw file_io_error("toml::parse: error opening file", fname); + } + ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); + + return parse(ifs, std::move(fname), std::move(s)); +} + +template +result, std::vector> +try_parse(const char (&fname)[N], spec s = spec::default_version()) +{ + return try_parse(std::string(fname), std::move(s)); +} + +template +basic_value parse(const char (&fname)[N], spec s = spec::default_version()) +{ + return parse(std::string(fname), std::move(s)); +} + +// ---------------------------------------------------------------------------- +// parse_str + +template +result, std::vector> +try_parse_str(std::string content, spec s = spec::default_version(), + cxx::source_location loc = cxx::source_location::current()) +{ + std::istringstream iss(std::move(content)); + std::string name("internal string" + cxx::to_string(loc)); + return try_parse(iss, std::move(name), std::move(s)); +} + +template +basic_value parse_str(std::string content, spec s = spec::default_version(), + cxx::source_location loc = cxx::source_location::current()) +{ + auto res = try_parse_str(std::move(content), std::move(s), std::move(loc)); + if(res.is_ok()) + { + return res.unwrap(); + } + else + { + std::string msg; + for(const auto& err : res.unwrap_err()) + { + msg += format_error(err); + } + throw syntax_error(std::move(msg), std::move(res.unwrap_err())); + } +} + +// ---------------------------------------------------------------------------- +// filesystem + +#if defined(TOML11_HAS_FILESYSTEM) + +template +cxx::enable_if_t::value, + result, std::vector>> +try_parse(const FSPATH& fpath, spec s = spec::default_version()) +{ + std::ifstream ifs(fpath, std::ios_base::binary); + if(!ifs.good()) + { + std::vector e; + e.push_back(error_info("toml::parse: Error opening file \"" + fpath.string() + "\"", {})); + return err(std::move(e)); + } + ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); + + return try_parse(ifs, fpath.string(), std::move(s)); +} + +template +cxx::enable_if_t::value, + basic_value> +parse(const FSPATH& fpath, spec s = spec::default_version()) +{ + std::ifstream ifs(fpath, std::ios_base::binary); + if(!ifs.good()) + { + throw file_io_error("toml::parse: error opening file", fpath.string()); + } + ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); + + return parse(ifs, fpath.string(), std::move(s)); +} +#endif + +// ----------------------------------------------------------------------------- +// FILE* + +template +result, std::vector> +try_parse(FILE* fp, std::string filename, spec s = spec::default_version()) +{ + const long beg = std::ftell(fp); + if (beg == -1L) + { + return err(std::vector{error_info( + std::string("Failed to access: \"") + filename + + "\", errno = " + std::to_string(errno), {} + )}); + } + + const int res_seekend = std::fseek(fp, 0, SEEK_END); + if (res_seekend != 0) + { + return err(std::vector{error_info( + std::string("Failed to seek: \"") + filename + + "\", errno = " + std::to_string(errno), {} + )}); + } + + const long end = std::ftell(fp); + if (end == -1L) + { + return err(std::vector{error_info( + std::string("Failed to access: \"") + filename + + "\", errno = " + std::to_string(errno), {} + )}); + } + + const auto fsize = end - beg; + + const auto res_seekbeg = std::fseek(fp, beg, SEEK_SET); + if (res_seekbeg != 0) + { + return err(std::vector{error_info( + std::string("Failed to seek: \"") + filename + + "\", errno = " + std::to_string(errno), {} + )}); + + } + + // read whole file as a sequence of char + assert(fsize >= 0); + std::vector letters(static_cast(fsize)); + const auto actual = std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); + if(actual != static_cast(fsize)) + { + return err(std::vector{error_info( + std::string("File size changed: \"") + filename + + std::string("\" make sure that FILE* is in binary mode " + "to avoid LF <-> CRLF conversion"), {} + )}); + } + + return detail::parse_impl(std::move(letters), std::move(filename), std::move(s)); +} + +template +basic_value +parse(FILE* fp, std::string filename, spec s = spec::default_version()) +{ + const long beg = std::ftell(fp); + if (beg == -1L) + { + throw file_io_error(errno, "Failed to access", filename); + } + + const int res_seekend = std::fseek(fp, 0, SEEK_END); + if (res_seekend != 0) + { + throw file_io_error(errno, "Failed to seek", filename); + } + + const long end = std::ftell(fp); + if (end == -1L) + { + throw file_io_error(errno, "Failed to access", filename); + } + + const auto fsize = end - beg; + + const auto res_seekbeg = std::fseek(fp, beg, SEEK_SET); + if (res_seekbeg != 0) + { + throw file_io_error(errno, "Failed to seek", filename); + } + + // read whole file as a sequence of char + assert(fsize >= 0); + std::vector letters(static_cast(fsize)); + const auto actual = std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); + if(actual != static_cast(fsize)) + { + throw file_io_error(errno, "File size changed; make sure that " + "FILE* is in binary mode to avoid LF <-> CRLF conversion", filename); + } + + auto res = detail::parse_impl(std::move(letters), std::move(filename), std::move(s)); + if(res.is_ok()) + { + return res.unwrap(); + } + else + { + std::string msg; + for(const auto& err : res.unwrap_err()) + { + msg += format_error(err); + } + throw syntax_error(std::move(msg), std::move(res.unwrap_err())); + } +} + +} // namespace toml + +#if defined(TOML11_COMPILE_SOURCES) +namespace toml +{ +struct type_config; +struct ordered_type_config; + +extern template result, std::vector> try_parse(std::vector, std::string, spec); +extern template result, std::vector> try_parse(std::istream&, std::string, spec); +extern template result, std::vector> try_parse(std::string, spec); +extern template result, std::vector> try_parse(FILE*, std::string, spec); +extern template result, std::vector> try_parse_str(std::string, spec, cxx::source_location); + +extern template basic_value parse(std::vector, std::string, spec); +extern template basic_value parse(std::istream&, std::string, spec); +extern template basic_value parse(std::string, spec); +extern template basic_value parse(FILE*, std::string, spec); +extern template basic_value parse_str(std::string, spec, cxx::source_location); + +extern template result, std::vector> try_parse(std::vector, std::string, spec); +extern template result, std::vector> try_parse(std::istream&, std::string, spec); +extern template result, std::vector> try_parse(std::string, spec); +extern template result, std::vector> try_parse(FILE*, std::string, spec); +extern template result, std::vector> try_parse_str(std::string, spec, cxx::source_location); + +extern template basic_value parse(std::vector, std::string, spec); +extern template basic_value parse(std::istream&, std::string, spec); +extern template basic_value parse(std::string, spec); +extern template basic_value parse(FILE*, std::string, spec); +extern template basic_value parse_str(std::string, spec, cxx::source_location); + +#if defined(TOML11_HAS_FILESYSTEM) +extern template cxx::enable_if_t::value, result, std::vector>> try_parse(const std::filesystem::path&, spec); +extern template cxx::enable_if_t::value, result, std::vector>> try_parse(const std::filesystem::path&, spec); +extern template cxx::enable_if_t::value, basic_value > parse (const std::filesystem::path&, spec); +extern template cxx::enable_if_t::value, basic_value > parse (const std::filesystem::path&, spec); +#endif // filesystem + +} // toml +#endif // TOML11_COMPILE_SOURCES + +#endif // TOML11_PARSER_HPP diff --git a/include/toml11/region.hpp b/include/toml11/region.hpp new file mode 100644 index 0000000..b48300c --- /dev/null +++ b/include/toml11/region.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_REGION_HPP +#define TOML11_REGION_HPP + +#include "fwd/region_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/region_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_REGION_HPP diff --git a/include/toml11/result.hpp b/include/toml11/result.hpp new file mode 100644 index 0000000..e847c4b --- /dev/null +++ b/include/toml11/result.hpp @@ -0,0 +1,486 @@ +#ifndef TOML11_RESULT_HPP +#define TOML11_RESULT_HPP + +#include "compat.hpp" +#include "exception.hpp" + +#include +#include +#include +#include + +#include + +namespace toml +{ + +struct bad_result_access final : public ::toml::exception +{ + public: + explicit bad_result_access(std::string what_arg) + : what_(std::move(what_arg)) + {} + ~bad_result_access() noexcept override = default; + const char* what() const noexcept override {return what_.c_str();} + + private: + std::string what_; +}; + +// ----------------------------------------------------------------------------- + +template +struct success +{ + static_assert( ! std::is_same::value, ""); + + using value_type = T; + + explicit success(value_type v) + noexcept(std::is_nothrow_move_constructible::value) + : value(std::move(v)) + {} + + template, T>::value, + std::nullptr_t> = nullptr> + explicit success(U&& v): value(std::forward(v)) {} + + template + explicit success(success v): value(std::move(v.value)) {} + + ~success() = default; + success(const success&) = default; + success(success&&) = default; + success& operator=(const success&) = default; + success& operator=(success&&) = default; + + value_type& get() noexcept {return value;} + value_type const& get() const noexcept {return value;} + + private: + + value_type value; +}; + +template +struct success> +{ + static_assert( ! std::is_same::value, ""); + + using value_type = T; + + explicit success(std::reference_wrapper v) noexcept + : value(std::move(v)) + {} + + ~success() = default; + success(const success&) = default; + success(success&&) = default; + success& operator=(const success&) = default; + success& operator=(success&&) = default; + + value_type& get() noexcept {return value.get();} + value_type const& get() const noexcept {return value.get();} + + private: + + std::reference_wrapper value; +}; + +template +success::type> ok(T&& v) +{ + return success::type>(std::forward(v)); +} +template +success ok(const char (&literal)[N]) +{ + return success(std::string(literal)); +} + +// ----------------------------------------------------------------------------- + +template +struct failure +{ + using value_type = T; + + explicit failure(value_type v) + noexcept(std::is_nothrow_move_constructible::value) + : value(std::move(v)) + {} + + template, T>::value, + std::nullptr_t> = nullptr> + explicit failure(U&& v): value(std::forward(v)) {} + + template + explicit failure(failure v): value(std::move(v.value)) {} + + ~failure() = default; + failure(const failure&) = default; + failure(failure&&) = default; + failure& operator=(const failure&) = default; + failure& operator=(failure&&) = default; + + value_type& get() noexcept {return value;} + value_type const& get() const noexcept {return value;} + + private: + + value_type value; +}; + +template +struct failure> +{ + using value_type = T; + + explicit failure(std::reference_wrapper v) noexcept + : value(std::move(v)) + {} + + ~failure() = default; + failure(const failure&) = default; + failure(failure&&) = default; + failure& operator=(const failure&) = default; + failure& operator=(failure&&) = default; + + value_type& get() noexcept {return value.get();} + value_type const& get() const noexcept {return value.get();} + + private: + + std::reference_wrapper value; +}; + +template +failure::type> err(T&& v) +{ + return failure::type>(std::forward(v)); +} + +template +failure err(const char (&literal)[N]) +{ + return failure(std::string(literal)); +} + +/* ============================================================================ + * _ _ + * _ _ ___ ____ _| | |_ + * | '_/ -_|_-< || | | _| + * |_| \___/__/\_,_|_|\__| + */ + +template +struct result +{ + using success_type = success; + using failure_type = failure; + using value_type = typename success_type::value_type; + using error_type = typename failure_type::value_type; + + result(success_type s): is_ok_(true), succ_(std::move(s)) {} + result(failure_type f): is_ok_(false), fail_(std::move(f)) {} + + template, value_type>>, + std::is_convertible, value_type> + >::value, std::nullptr_t> = nullptr> + result(success s): is_ok_(true), succ_(std::move(s.value)) {} + + template, error_type>>, + std::is_convertible, error_type> + >::value, std::nullptr_t> = nullptr> + result(failure f): is_ok_(false), fail_(std::move(f.value)) {} + + result& operator=(success_type s) + { + this->cleanup(); + this->is_ok_ = true; + auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(s)); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + return *this; + } + result& operator=(failure_type f) + { + this->cleanup(); + this->is_ok_ = false; + auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(f)); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + return *this; + } + + template + result& operator=(success s) + { + this->cleanup(); + this->is_ok_ = true; + auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(s.value)); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + return *this; + } + template + result& operator=(failure f) + { + this->cleanup(); + this->is_ok_ = false; + auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(f.value)); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + return *this; + } + + ~result() noexcept {this->cleanup();} + + result(const result& other): is_ok_(other.is_ok()) + { + if(other.is_ok()) + { + auto tmp = ::new(std::addressof(this->succ_)) success_type(other.succ_); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + } + else + { + auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.fail_); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + } + } + result(result&& other): is_ok_(other.is_ok()) + { + if(other.is_ok()) + { + auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.succ_)); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + } + else + { + auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.fail_)); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + } + } + + result& operator=(const result& other) + { + this->cleanup(); + if(other.is_ok()) + { + auto tmp = ::new(std::addressof(this->succ_)) success_type(other.succ_); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + } + else + { + auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.fail_); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + } + is_ok_ = other.is_ok(); + return *this; + } + result& operator=(result&& other) + { + this->cleanup(); + if(other.is_ok()) + { + auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.succ_)); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + } + else + { + auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.fail_)); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + } + is_ok_ = other.is_ok(); + return *this; + } + + template, value_type>>, + cxx::negation, error_type>>, + std::is_convertible, value_type>, + std::is_convertible, error_type> + >::value, std::nullptr_t> = nullptr> + result(result other): is_ok_(other.is_ok()) + { + if(other.is_ok()) + { + auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.as_ok())); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + } + else + { + auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.as_err())); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + } + } + + template, value_type>>, + cxx::negation, error_type>>, + std::is_convertible, value_type>, + std::is_convertible, error_type> + >::value, std::nullptr_t> = nullptr> + result& operator=(result other) + { + this->cleanup(); + if(other.is_ok()) + { + auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.as_ok())); + assert(tmp == std::addressof(this->succ_)); + (void)tmp; + } + else + { + auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.as_err())); + assert(tmp == std::addressof(this->fail_)); + (void)tmp; + } + is_ok_ = other.is_ok(); + return *this; + } + + bool is_ok() const noexcept {return is_ok_;} + bool is_err() const noexcept {return !is_ok_;} + + explicit operator bool() const noexcept {return is_ok_;} + + value_type& unwrap(cxx::source_location loc = cxx::source_location::current()) + { + if(this->is_err()) + { + throw bad_result_access("toml::result: bad unwrap" + cxx::to_string(loc)); + } + return this->succ_.get(); + } + value_type const& unwrap(cxx::source_location loc = cxx::source_location::current()) const + { + if(this->is_err()) + { + throw bad_result_access("toml::result: bad unwrap" + cxx::to_string(loc)); + } + return this->succ_.get(); + } + + value_type& unwrap_or(value_type& opt) noexcept + { + if(this->is_err()) {return opt;} + return this->succ_.get(); + } + value_type const& unwrap_or(value_type const& opt) const noexcept + { + if(this->is_err()) {return opt;} + return this->succ_.get(); + } + + error_type& unwrap_err(cxx::source_location loc = cxx::source_location::current()) + { + if(this->is_ok()) + { + throw bad_result_access("toml::result: bad unwrap_err" + cxx::to_string(loc)); + } + return this->fail_.get(); + } + error_type const& unwrap_err(cxx::source_location loc = cxx::source_location::current()) const + { + if(this->is_ok()) + { + throw bad_result_access("toml::result: bad unwrap_err" + cxx::to_string(loc)); + } + return this->fail_.get(); + } + + value_type& as_ok() noexcept + { + assert(this->is_ok()); + return this->succ_.get(); + } + value_type const& as_ok() const noexcept + { + assert(this->is_ok()); + return this->succ_.get(); + } + + error_type& as_err() noexcept + { + assert(this->is_err()); + return this->fail_.get(); + } + error_type const& as_err() const noexcept + { + assert(this->is_err()); + return this->fail_.get(); + } + + private: + + void cleanup() noexcept + { +#if defined(__GNUC__) && ! defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wduplicated-branches" +#endif + + if(this->is_ok_) {this->succ_.~success_type();} + else {this->fail_.~failure_type();} + +#if defined(__GNUC__) && ! defined(__clang__) +#pragma GCC diagnostic pop +#endif + return; + } + + private: + + bool is_ok_; + union + { + success_type succ_; + failure_type fail_; + }; +}; + +// ---------------------------------------------------------------------------- + +namespace detail +{ +struct none_t {}; +inline bool operator==(const none_t&, const none_t&) noexcept {return true;} +inline bool operator!=(const none_t&, const none_t&) noexcept {return false;} +inline bool operator< (const none_t&, const none_t&) noexcept {return false;} +inline bool operator<=(const none_t&, const none_t&) noexcept {return true;} +inline bool operator> (const none_t&, const none_t&) noexcept {return false;} +inline bool operator>=(const none_t&, const none_t&) noexcept {return true;} +inline std::ostream& operator<<(std::ostream& os, const none_t&) +{ + os << "none"; + return os; +} +} // detail + +inline success ok() noexcept +{ + return success(detail::none_t{}); +} +inline failure err() noexcept +{ + return failure(detail::none_t{}); +} + +} // toml +#endif // TOML11_RESULT_HPP diff --git a/include/toml11/scanner.hpp b/include/toml11/scanner.hpp new file mode 100644 index 0000000..85812d9 --- /dev/null +++ b/include/toml11/scanner.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_SCANNER_HPP +#define TOML11_SCANNER_HPP + +#include "fwd/scanner_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/scanner_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_SCANNER_HPP diff --git a/include/toml11/serializer.hpp b/include/toml11/serializer.hpp new file mode 100644 index 0000000..a3f148e --- /dev/null +++ b/include/toml11/serializer.hpp @@ -0,0 +1,1275 @@ +#ifndef TOML11_SERIALIZER_HPP +#define TOML11_SERIALIZER_HPP + +#include "comments.hpp" +#include "error_info.hpp" +#include "exception.hpp" +#include "source_location.hpp" +#include "spec.hpp" +#include "syntax.hpp" +#include "types.hpp" +#include "utility.hpp" +#include "value.hpp" + +#include +#include +#include + +#include +#include + +namespace toml +{ + +struct serialization_error final : public ::toml::exception +{ + public: + explicit serialization_error(std::string what_arg, source_location loc) + : what_(std::move(what_arg)), loc_(std::move(loc)) + {} + ~serialization_error() noexcept override = default; + + const char* what() const noexcept override {return what_.c_str();} + source_location const& location() const noexcept {return loc_;} + + private: + std::string what_; + source_location loc_; +}; + +namespace detail +{ +template +class serializer +{ + public: + + using value_type = basic_value; + + using key_type = typename value_type::key_type ; + using comment_type = typename value_type::comment_type ; + using boolean_type = typename value_type::boolean_type ; + using integer_type = typename value_type::integer_type ; + using floating_type = typename value_type::floating_type ; + using string_type = typename value_type::string_type ; + using local_time_type = typename value_type::local_time_type ; + using local_date_type = typename value_type::local_date_type ; + using local_datetime_type = typename value_type::local_datetime_type ; + using offset_datetime_type = typename value_type::offset_datetime_type; + using array_type = typename value_type::array_type ; + using table_type = typename value_type::table_type ; + + using char_type = typename string_type::value_type; + + public: + + explicit serializer(const spec& sp) + : spec_(sp), force_inline_(false), current_indent_(0) + {} + + string_type operator()(const std::vector& ks, const value_type& v) + { + for(const auto& k : ks) + { + this->keys_.push_back(k); + } + return (*this)(v); + } + + string_type operator()(const key_type& k, const value_type& v) + { + this->keys_.push_back(k); + return (*this)(v); + } + + string_type operator()(const value_type& v) + { + switch(v.type()) + { + case value_t::boolean : {return (*this)(v.as_boolean (), v.as_boolean_fmt (), v.location());} + case value_t::integer : {return (*this)(v.as_integer (), v.as_integer_fmt (), v.location());} + case value_t::floating : {return (*this)(v.as_floating (), v.as_floating_fmt (), v.location());} + case value_t::string : {return (*this)(v.as_string (), v.as_string_fmt (), v.location());} + case value_t::offset_datetime: {return (*this)(v.as_offset_datetime(), v.as_offset_datetime_fmt(), v.location());} + case value_t::local_datetime : {return (*this)(v.as_local_datetime (), v.as_local_datetime_fmt (), v.location());} + case value_t::local_date : {return (*this)(v.as_local_date (), v.as_local_date_fmt (), v.location());} + case value_t::local_time : {return (*this)(v.as_local_time (), v.as_local_time_fmt (), v.location());} + case value_t::array : + { + return (*this)(v.as_array(), v.as_array_fmt(), v.comments(), v.location()); + } + case value_t::table : + { + string_type retval; + if(this->keys_.empty()) // it might be the root table. emit comments here. + { + retval += format_comments(v.comments(), v.as_table_fmt().indent_type); + } + if( ! retval.empty()) // we have comment. + { + retval += char_type('\n'); + } + + retval += (*this)(v.as_table(), v.as_table_fmt(), v.comments(), v.location()); + return retval; + } + case value_t::empty: + { + if(this->spec_.ext_null_value) + { + return string_conv("null"); + } + break; + } + default: + { + break; + } + } + throw serialization_error(format_error( + "[error] toml::serializer: toml::basic_value " + "does not have any valid type.", v.location(), "here"), v.location()); + } + + private: + + string_type operator()(const boolean_type& b, const boolean_format_info&, const source_location&) // {{{ + { + if(b) + { + return string_conv("true"); + } + else + { + return string_conv("false"); + } + } // }}} + + string_type operator()(const integer_type i, const integer_format_info& fmt, const source_location& loc) // {{{ + { + std::ostringstream oss; + this->set_locale(oss); + + const auto insert_spacer = [&fmt](std::string s) -> std::string { + if(fmt.spacer == 0) {return s;} + + std::string sign; + if( ! s.empty() && (s.at(0) == '+' || s.at(0) == '-')) + { + sign += s.at(0); + s.erase(s.begin()); + } + + std::string spaced; + std::size_t counter = 0; + for(auto iter = s.rbegin(); iter != s.rend(); ++iter) + { + if(counter != 0 && counter % fmt.spacer == 0) + { + spaced += '_'; + } + spaced += *iter; + counter += 1; + } + if(!spaced.empty() && spaced.back() == '_') {spaced.pop_back();} + + s.clear(); + std::copy(spaced.rbegin(), spaced.rend(), std::back_inserter(s)); + return sign + s; + }; + + std::string retval; + if(fmt.fmt == integer_format::dec) + { + oss << std::setw(static_cast(fmt.width)) << std::dec << i; + retval = insert_spacer(oss.str()); + + if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) + { + retval += '_'; + retval += fmt.suffix; + } + } + else + { + if(i < 0) + { + throw serialization_error(format_error("binary, octal, hexadecimal " + "integer does not allow negative value", loc, "here"), loc); + } + switch(fmt.fmt) + { + case integer_format::hex: + { + oss << std::noshowbase + << std::setw(static_cast(fmt.width)) + << std::setfill('0') + << std::hex; + if(fmt.uppercase) + { + oss << std::uppercase; + } + else + { + oss << std::nouppercase; + } + oss << i; + retval = std::string("0x") + insert_spacer(oss.str()); + break; + } + case integer_format::oct: + { + oss << std::setw(static_cast(fmt.width)) << std::setfill('0') << std::oct << i; + retval = std::string("0o") + insert_spacer(oss.str()); + break; + } + case integer_format::bin: + { + integer_type x{i}; + std::string tmp; + std::size_t bits(0); + while(x != 0) + { + if(fmt.spacer != 0) + { + if(bits != 0 && (bits % fmt.spacer) == 0) {tmp += '_';} + } + if(x % 2 == 1) { tmp += '1'; } else { tmp += '0'; } + x >>= 1; + bits += 1; + } + for(; bits < fmt.width; ++bits) + { + if(fmt.spacer != 0) + { + if(bits != 0 && (bits % fmt.spacer) == 0) {tmp += '_';} + } + tmp += '0'; + } + for(auto iter = tmp.rbegin(); iter != tmp.rend(); ++iter) + { + oss << *iter; + } + retval = std::string("0b") + oss.str(); + break; + } + default: + { + throw serialization_error(format_error( + "none of dec, hex, oct, bin: " + to_string(fmt.fmt), + loc, "here"), loc); + } + } + } + return string_conv(retval); + } // }}} + + string_type operator()(const floating_type f, const floating_format_info& fmt, const source_location&) // {{{ + { + using std::isnan; + using std::isinf; + using std::signbit; + + std::ostringstream oss; + this->set_locale(oss); + + if(isnan(f)) + { + if(signbit(f)) + { + oss << '-'; + } + oss << "nan"; + if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) + { + oss << '_'; + oss << fmt.suffix; + } + return string_conv(oss.str()); + } + + if(isinf(f)) + { + if(signbit(f)) + { + oss << '-'; + } + oss << "inf"; + if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) + { + oss << '_'; + oss << fmt.suffix; + } + return string_conv(oss.str()); + } + + switch(fmt.fmt) + { + case floating_format::defaultfloat: + { + if(fmt.prec != 0) + { + oss << std::setprecision(static_cast(fmt.prec)); + } + oss << f; + // since defaultfloat may omit point, we need to add it + std::string s = oss.str(); + if (s.find('.') == std::string::npos && + s.find('e') == std::string::npos && + s.find('E') == std::string::npos ) + { + s += ".0"; + } + if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) + { + s += '_'; + s += fmt.suffix; + } + return string_conv(s); + } + case floating_format::fixed: + { + if(fmt.prec != 0) + { + oss << std::setprecision(static_cast(fmt.prec)); + } + oss << std::fixed << f; + if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) + { + oss << '_' << fmt.suffix; + } + return string_conv(oss.str()); + } + case floating_format::scientific: + { + if(fmt.prec != 0) + { + oss << std::setprecision(static_cast(fmt.prec)); + } + oss << std::scientific << f; + if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) + { + oss << '_' << fmt.suffix; + } + return string_conv(oss.str()); + } + case floating_format::hex: + { + if(this->spec_.ext_hex_float) + { + oss << std::hexfloat << f; + // suffix is only for decimal numbers. + return string_conv(oss.str()); + } + else // no hex allowed. output with max precision. + { + oss << std::setprecision(std::numeric_limits::max_digits10) + << std::scientific << f; + // suffix is only for decimal numbers. + return string_conv(oss.str()); + } + } + default: + { + if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) + { + oss << '_' << fmt.suffix; + } + return string_conv(oss.str()); + } + } + } // }}} + + string_type operator()(string_type s, const string_format_info& fmt, const source_location& loc) // {{{ + { + string_type retval; + switch(fmt.fmt) + { + case string_format::basic: + { + retval += char_type('"'); + retval += this->escape_basic_string(s); + retval += char_type('"'); + return retval; + } + case string_format::literal: + { + if(std::find(s.begin(), s.end(), char_type('\n')) != s.end()) + { + throw serialization_error(format_error("toml::serializer: " + "(non-multiline) literal string cannot have a newline", + loc, "here"), loc); + } + retval += char_type('\''); + retval += s; + retval += char_type('\''); + return retval; + } + case string_format::multiline_basic: + { + retval += string_conv("\"\"\""); + if(fmt.start_with_newline) + { + retval += char_type('\n'); + } + + retval += this->escape_ml_basic_string(s); + + retval += string_conv("\"\"\""); + return retval; + } + case string_format::multiline_literal: + { + retval += string_conv("'''"); + if(fmt.start_with_newline) + { + retval += char_type('\n'); + } + retval += s; + retval += string_conv("'''"); + return retval; + } + default: + { + throw serialization_error(format_error( + "[error] toml::serializer::operator()(string): " + "invalid string_format value", loc, "here"), loc); + } + } + } // }}} + + string_type operator()(const local_date_type& d, const local_date_format_info&, const source_location&) // {{{ + { + std::ostringstream oss; + oss << d; + return string_conv(oss.str()); + } // }}} + + string_type operator()(const local_time_type& t, const local_time_format_info& fmt, const source_location&) // {{{ + { + return this->format_local_time(t, fmt.has_seconds, fmt.subsecond_precision); + } // }}} + + string_type operator()(const local_datetime_type& dt, const local_datetime_format_info& fmt, const source_location&) // {{{ + { + std::ostringstream oss; + oss << dt.date; + switch(fmt.delimiter) + { + case datetime_delimiter_kind::upper_T: { oss << 'T'; break; } + case datetime_delimiter_kind::lower_t: { oss << 't'; break; } + case datetime_delimiter_kind::space: { oss << ' '; break; } + default: { oss << 'T'; break; } + } + return string_conv(oss.str()) + + this->format_local_time(dt.time, fmt.has_seconds, fmt.subsecond_precision); + } // }}} + + string_type operator()(const offset_datetime_type& odt, const offset_datetime_format_info& fmt, const source_location&) // {{{ + { + std::ostringstream oss; + oss << odt.date; + switch(fmt.delimiter) + { + case datetime_delimiter_kind::upper_T: { oss << 'T'; break; } + case datetime_delimiter_kind::lower_t: { oss << 't'; break; } + case datetime_delimiter_kind::space: { oss << ' '; break; } + default: { oss << 'T'; break; } + } + oss << string_conv(this->format_local_time(odt.time, fmt.has_seconds, fmt.subsecond_precision)); + oss << odt.offset; + return string_conv(oss.str()); + } // }}} + + string_type operator()(const array_type& a, const array_format_info& fmt, const comment_type& com, const source_location& loc) // {{{ + { + array_format f = fmt.fmt; + if(fmt.fmt == array_format::default_format) + { + // [[in.this.form]], you cannot add a comment to the array itself + // (but you can add a comment to each table). + // To keep comments, we need to avoid multiline array-of-tables + // if array itself has a comment. + if( ! this->keys_.empty() && + ! a.empty() && + com.empty() && + std::all_of(a.begin(), a.end(), [](const value_type& e) {return e.is_table();})) + { + f = array_format::array_of_tables; + } + else + { + f = array_format::oneline; + + // check if it becomes long + std::size_t approx_len = 0; + for(const auto& e : a) + { + // have a comment. cannot be inlined + if( ! e.comments().empty()) + { + f = array_format::multiline; + break; + } + // possibly long types ... + if(e.is_array() || e.is_table() || e.is_offset_datetime() || e.is_local_datetime()) + { + f = array_format::multiline; + break; + } + else if(e.is_boolean()) + { + approx_len += (*this)(e.as_boolean(), e.as_boolean_fmt(), e.location()).size(); + } + else if(e.is_integer()) + { + approx_len += (*this)(e.as_integer(), e.as_integer_fmt(), e.location()).size(); + } + else if(e.is_floating()) + { + approx_len += (*this)(e.as_floating(), e.as_floating_fmt(), e.location()).size(); + } + else if(e.is_string()) + { + if(e.as_string_fmt().fmt == string_format::multiline_basic || + e.as_string_fmt().fmt == string_format::multiline_literal) + { + f = array_format::multiline; + break; + } + approx_len += 2 + (*this)(e.as_string(), e.as_string_fmt(), e.location()).size(); + } + else if(e.is_local_date()) + { + approx_len += 10; // 1234-56-78 + } + else if(e.is_local_time()) + { + approx_len += 15; // 12:34:56.789012 + } + + if(approx_len > 60) // key, ` = `, `[...]` < 80 + { + f = array_format::multiline; + break; + } + approx_len += 2; // `, ` + } + } + } + if(this->force_inline_ && f == array_format::array_of_tables) + { + f = array_format::multiline; + } + if(a.empty() && f == array_format::array_of_tables) + { + f = array_format::oneline; + } + + // -------------------------------------------------------------------- + + if(f == array_format::array_of_tables) + { + if(this->keys_.empty()) + { + throw serialization_error("array of table must have its key. " + "use format(key, v)", loc); + } + string_type retval; + for(const auto& e : a) + { + assert(e.is_table()); + + this->current_indent_ += e.as_table_fmt().name_indent; + retval += this->format_comments(e.comments(), e.as_table_fmt().indent_type); + retval += this->format_indent(e.as_table_fmt().indent_type); + this->current_indent_ -= e.as_table_fmt().name_indent; + + retval += string_conv("[["); + retval += this->format_keys(this->keys_).value(); + retval += string_conv("]]\n"); + + retval += this->format_ml_table(e.as_table(), e.as_table_fmt()); + } + return retval; + } + else if(f == array_format::oneline) + { + // ignore comments. we cannot emit comments + string_type retval; + retval += char_type('['); + for(const auto& e : a) + { + this->force_inline_ = true; + retval += (*this)(e); + retval += string_conv(", "); + } + if( ! a.empty()) + { + retval.pop_back(); // ` ` + retval.pop_back(); // `,` + } + retval += char_type(']'); + this->force_inline_ = false; + return retval; + } + else + { + assert(f == array_format::multiline); + + string_type retval; + retval += string_conv("[\n"); + + for(const auto& e : a) + { + this->current_indent_ += fmt.body_indent; + retval += this->format_comments(e.comments(), fmt.indent_type); + retval += this->format_indent(fmt.indent_type); + this->current_indent_ -= fmt.body_indent; + + this->force_inline_ = true; + retval += (*this)(e); + retval += string_conv(",\n"); + } + this->force_inline_ = false; + + this->current_indent_ += fmt.closing_indent; + retval += this->format_indent(fmt.indent_type); + this->current_indent_ -= fmt.closing_indent; + + retval += char_type(']'); + return retval; + } + } // }}} + + string_type operator()(const table_type& t, const table_format_info& fmt, const comment_type& com, const source_location& loc) // {{{ + { + if(this->force_inline_) + { + if(fmt.fmt == table_format::multiline_oneline) + { + return this->format_ml_inline_table(t, fmt); + } + else + { + return this->format_inline_table(t, fmt); + } + } + else + { + if(fmt.fmt == table_format::multiline) + { + string_type retval; + // comment is emitted inside format_ml_table + if(auto k = this->format_keys(this->keys_)) + { + this->current_indent_ += fmt.name_indent; + retval += this->format_comments(com, fmt.indent_type); + retval += this->format_indent(fmt.indent_type); + this->current_indent_ -= fmt.name_indent; + retval += char_type('['); + retval += k.value(); + retval += string_conv("]\n"); + } + // otherwise, its the root. + + retval += this->format_ml_table(t, fmt); + return retval; + } + else if(fmt.fmt == table_format::oneline) + { + return this->format_inline_table(t, fmt); + } + else if(fmt.fmt == table_format::multiline_oneline) + { + return this->format_ml_inline_table(t, fmt); + } + else if(fmt.fmt == table_format::dotted) + { + std::vector keys; + if(this->keys_.empty()) + { + throw serialization_error(format_error("toml::serializer: " + "dotted table must have its key. use format(key, v)", + loc, "here"), loc); + } + keys.push_back(this->keys_.back()); + + const auto retval = this->format_dotted_table(t, fmt, loc, keys); + keys.pop_back(); + return retval; + } + else + { + assert(fmt.fmt == table_format::implicit); + + string_type retval; + for(const auto& kv : t) + { + const auto& k = kv.first; + const auto& v = kv.second; + + if( ! v.is_table() && ! v.is_array_of_tables()) + { + throw serialization_error(format_error("toml::serializer: " + "an implicit table cannot have non-table value.", + v.location(), "here"), v.location()); + } + if(v.is_table()) + { + if(v.as_table_fmt().fmt != table_format::multiline && + v.as_table_fmt().fmt != table_format::implicit) + { + throw serialization_error(format_error("toml::serializer: " + "an implicit table cannot have non-multiline table", + v.location(), "here"), v.location()); + } + } + else + { + assert(v.is_array()); + for(const auto& e : v.as_array()) + { + if(e.as_table_fmt().fmt != table_format::multiline && + v.as_table_fmt().fmt != table_format::implicit) + { + throw serialization_error(format_error("toml::serializer: " + "an implicit table cannot have non-multiline table", + e.location(), "here"), e.location()); + } + } + } + + keys_.push_back(k); + retval += (*this)(v); + keys_.pop_back(); + } + return retval; + } + } + } // }}} + + private: + + string_type escape_basic_string(const string_type& s) const // {{{ + { + string_type retval; + for(const char_type c : s) + { + switch(c) + { + case char_type('\\'): {retval += string_conv("\\\\"); break;} + case char_type('\"'): {retval += string_conv("\\\""); break;} + case char_type('\b'): {retval += string_conv("\\b" ); break;} + case char_type('\t'): {retval += string_conv("\\t" ); break;} + case char_type('\f'): {retval += string_conv("\\f" ); break;} + case char_type('\n'): {retval += string_conv("\\n" ); break;} + case char_type('\r'): {retval += string_conv("\\r" ); break;} + default : + { + if(c == char_type(0x1B) && spec_.v1_1_0_add_escape_sequence_e) + { + retval += string_conv("\\e"); + } + else if((char_type(0x00) <= c && c <= char_type(0x08)) || + (char_type(0x0A) <= c && c <= char_type(0x1F)) || + c == char_type(0x7F)) + { + if(spec_.v1_1_0_add_escape_sequence_x) + { + retval += string_conv("\\x"); + } + else + { + retval += string_conv("\\u00"); + } + const auto c1 = c / 16; + const auto c2 = c % 16; + retval += static_cast('0' + c1); + if(c2 < 10) + { + retval += static_cast('0' + c2); + } + else // 10 <= c2 + { + retval += static_cast('A' + (c2 - 10)); + } + } + else + { + retval += c; + } + } + } + } + return retval; + } // }}} + + string_type escape_ml_basic_string(const string_type& s) // {{{ + { + string_type retval; + for(const char_type c : s) + { + switch(c) + { + case char_type('\\'): {retval += string_conv("\\\\"); break;} + case char_type('\b'): {retval += string_conv("\\b" ); break;} + case char_type('\t'): {retval += string_conv("\\t" ); break;} + case char_type('\f'): {retval += string_conv("\\f" ); break;} + case char_type('\n'): {retval += string_conv("\n" ); break;} + case char_type('\r'): {retval += string_conv("\\r" ); break;} + default : + { + if(c == char_type(0x1B) && spec_.v1_1_0_add_escape_sequence_e) + { + retval += string_conv("\\e"); + } + else if((char_type(0x00) <= c && c <= char_type(0x08)) || + (char_type(0x0A) <= c && c <= char_type(0x1F)) || + c == char_type(0x7F)) + { + if(spec_.v1_1_0_add_escape_sequence_x) + { + retval += string_conv("\\x"); + } + else + { + retval += string_conv("\\u00"); + } + const auto c1 = c / 16; + const auto c2 = c % 16; + retval += static_cast('0' + c1); + if(c2 < 10) + { + retval += static_cast('0' + c2); + } + else // 10 <= c2 + { + retval += static_cast('A' + (c2 - 10)); + } + } + else + { + retval += c; + } + } + } + } + // Only 1 or 2 consecutive `"`s are allowed in multiline basic string. + // 3 consecutive `"`s are considered as a closing delimiter. + // We need to check if there are 3 or more consecutive `"`s and insert + // backslash to break them down into several short `"`s like the `str6` + // in the following example. + // ```toml + // str4 = """Here are two quotation marks: "". Simple enough.""" + // # str5 = """Here are three quotation marks: """.""" # INVALID + // str5 = """Here are three quotation marks: ""\".""" + // str6 = """Here are fifteen quotation marks: ""\"""\"""\"""\"""\".""" + // ``` + auto found_3_quotes = retval.find(string_conv("\"\"\"")); + while(found_3_quotes != string_type::npos) + { + retval.replace(found_3_quotes, 3, string_conv("\"\"\\\"")); + found_3_quotes = retval.find(string_conv("\"\"\"")); + } + return retval; + } // }}} + + string_type format_local_time(const local_time_type& t, const bool has_seconds, const std::size_t subsec_prec) // {{{ + { + std::ostringstream oss; + oss << std::setfill('0') << std::setw(2) << static_cast(t.hour); + oss << ':'; + oss << std::setfill('0') << std::setw(2) << static_cast(t.minute); + if(has_seconds) + { + oss << ':'; + oss << std::setfill('0') << std::setw(2) << static_cast(t.second); + if(subsec_prec != 0) + { + std::ostringstream subsec; + subsec << std::setfill('0') << std::setw(3) << static_cast(t.millisecond); + subsec << std::setfill('0') << std::setw(3) << static_cast(t.microsecond); + subsec << std::setfill('0') << std::setw(3) << static_cast(t.nanosecond); + std::string subsec_str = subsec.str(); + oss << '.' << subsec_str.substr(0, subsec_prec); + } + } + return string_conv(oss.str()); + } // }}} + + string_type format_ml_table(const table_type& t, const table_format_info& fmt) // {{{ + { + const auto format_later = [](const value_type& v) -> bool { + + const bool is_ml_table = v.is_table() && + v.as_table_fmt().fmt != table_format::oneline && + v.as_table_fmt().fmt != table_format::multiline_oneline && + v.as_table_fmt().fmt != table_format::dotted ; + + const bool is_ml_array_table = v.is_array_of_tables() && + v.as_array_fmt().fmt != array_format::oneline && + v.as_array_fmt().fmt != array_format::multiline; + + return is_ml_table || is_ml_array_table; + }; + + string_type retval; + this->current_indent_ += fmt.body_indent; + for(const auto& kv : t) + { + const auto& key = kv.first; + const auto& val = kv.second; + if(format_later(val)) + { + continue; + } + this->keys_.push_back(key); + + retval += format_comments(val.comments(), fmt.indent_type); + retval += format_indent(fmt.indent_type); + if(val.is_table() && val.as_table_fmt().fmt == table_format::dotted) + { + retval += (*this)(val); + } + else + { + retval += format_key(key); + retval += string_conv(" = "); + retval += (*this)(val); + retval += char_type('\n'); + } + this->keys_.pop_back(); + } + this->current_indent_ -= fmt.body_indent; + + if( ! retval.empty()) + { + retval += char_type('\n'); // for readability, add empty line between tables + } + for(const auto& kv : t) + { + if( ! format_later(kv.second)) + { + continue; + } + // must be a [multiline.table] or [[multiline.array.of.tables]]. + // comments will be generated inside it. + this->keys_.push_back(kv.first); + retval += (*this)(kv.second); + this->keys_.pop_back(); + } + return retval; + } // }}} + + string_type format_inline_table(const table_type& t, const table_format_info&) // {{{ + { + // comments are ignored because we cannot write without newline + string_type retval; + retval += char_type('{'); + for(const auto& kv : t) + { + this->force_inline_ = true; + retval += this->format_key(kv.first); + retval += string_conv(" = "); + retval += (*this)(kv.second); + retval += string_conv(", "); + } + if( ! t.empty()) + { + retval.pop_back(); // ' ' + retval.pop_back(); // ',' + } + retval += char_type('}'); + this->force_inline_ = false; + return retval; + } // }}} + + string_type format_ml_inline_table(const table_type& t, const table_format_info& fmt) // {{{ + { + string_type retval; + retval += string_conv("{\n"); + this->current_indent_ += fmt.body_indent; + for(const auto& kv : t) + { + this->force_inline_ = true; + retval += format_comments(kv.second.comments(), fmt.indent_type); + retval += format_indent(fmt.indent_type); + retval += kv.first; + retval += string_conv(" = "); + + this->force_inline_ = true; + retval += (*this)(kv.second); + + retval += string_conv(",\n"); + } + if( ! t.empty()) + { + retval.pop_back(); // '\n' + retval.pop_back(); // ',' + } + this->current_indent_ -= fmt.body_indent; + this->force_inline_ = false; + + this->current_indent_ += fmt.closing_indent; + retval += format_indent(fmt.indent_type); + this->current_indent_ -= fmt.closing_indent; + + retval += char_type('}'); + return retval; + } // }}} + + string_type format_dotted_table(const table_type& t, const table_format_info& fmt, // {{{ + const source_location&, std::vector& keys) + { + // lets say we have: `{"a": {"b": {"c": {"d": "foo", "e": "bar"} } }` + // and `a` and `b` are `dotted`. + // + // - in case if `c` is `oneline`: + // ```toml + // a.b.c = {d = "foo", e = "bar"} + // ``` + // + // - in case if and `c` is `dotted`: + // ```toml + // a.b.c.d = "foo" + // a.b.c.e = "bar" + // ``` + + string_type retval; + + for(const auto& kv : t) + { + const auto& key = kv.first; + const auto& val = kv.second; + + keys.push_back(key); + + // format recursive dotted table? + if (val.is_table() && + val.as_table_fmt().fmt != table_format::oneline && + val.as_table_fmt().fmt != table_format::multiline_oneline) + { + retval += this->format_dotted_table(val.as_table(), val.as_table_fmt(), val.location(), keys); + } + else // non-table or inline tables. format normally + { + retval += format_comments(val.comments(), fmt.indent_type); + retval += format_indent(fmt.indent_type); + retval += format_keys(keys).value(); + retval += string_conv(" = "); + this->force_inline_ = true; // sub-table must be inlined + retval += (*this)(val); + retval += char_type('\n'); + this->force_inline_ = false; + } + keys.pop_back(); + } + return retval; + } // }}} + + string_type format_key(const key_type& key) // {{{ + { + if(key.empty()) + { + return string_conv("\"\""); + } + + // check the key can be a bare (unquoted) key + auto loc = detail::make_temporary_location(string_conv(key)); + auto reg = detail::syntax::unquoted_key(this->spec_).scan(loc); + if(reg.is_ok() && loc.eof()) + { + return key; + } + + //if it includes special characters, then format it in a "quoted" key. + string_type formatted = string_conv("\""); + for(const char_type c : key) + { + switch(c) + { + case char_type('\\'): {formatted += string_conv("\\\\"); break;} + case char_type('\"'): {formatted += string_conv("\\\""); break;} + case char_type('\b'): {formatted += string_conv("\\b" ); break;} + case char_type('\t'): {formatted += string_conv("\\t" ); break;} + case char_type('\f'): {formatted += string_conv("\\f" ); break;} + case char_type('\n'): {formatted += string_conv("\\n" ); break;} + case char_type('\r'): {formatted += string_conv("\\r" ); break;} + default : + { + // ASCII ctrl char + if( (char_type(0x00) <= c && c <= char_type(0x08)) || + (char_type(0x0A) <= c && c <= char_type(0x1F)) || + c == char_type(0x7F)) + { + if(spec_.v1_1_0_add_escape_sequence_x) + { + formatted += string_conv("\\x"); + } + else + { + formatted += string_conv("\\u00"); + } + const auto c1 = c / 16; + const auto c2 = c % 16; + formatted += static_cast('0' + c1); + if(c2 < 10) + { + formatted += static_cast('0' + c2); + } + else // 10 <= c2 + { + formatted += static_cast('A' + (c2 - 10)); + } + } + else + { + formatted += c; + } + break; + } + } + } + formatted += string_conv("\""); + return formatted; + } // }}} + cxx::optional format_keys(const std::vector& keys) // {{{ + { + if(keys.empty()) + { + return cxx::make_nullopt(); + } + + string_type formatted; + for(const auto& ky : keys) + { + formatted += format_key(ky); + formatted += char_type('.'); + } + formatted.pop_back(); // remove the last dot '.' + return formatted; + } // }}} + + string_type format_comments(const discard_comments&, const indent_char) const // {{{ + { + return string_conv(""); + } // }}} + string_type format_comments(const preserve_comments& comments, const indent_char indent_type) const // {{{ + { + string_type retval; + for(const auto& c : comments) + { + if(c.empty()) {continue;} + retval += format_indent(indent_type); + if(c.front() != '#') {retval += char_type('#');} + retval += string_conv(c); + if(c.back() != '\n') {retval += char_type('\n');} + } + return retval; + } // }}} + + string_type format_indent(const indent_char indent_type) const // {{{ + { + const auto indent = static_cast((std::max)(0, this->current_indent_)); + if(indent_type == indent_char::space) + { + return string_conv(make_string(indent, ' ')); + } + else if(indent_type == indent_char::tab) + { + return string_conv(make_string(indent, '\t')); + } + else + { + return string_type{}; + } + } // }}} + + std::locale set_locale(std::ostream& os) const + { + return os.imbue(std::locale::classic()); + } + + private: + + spec spec_; + bool force_inline_; // table inside an array without fmt specification + std::int32_t current_indent_; + std::vector keys_; +}; +} // detail + +template +typename basic_value::string_type +format(const basic_value& v, const spec s = spec::default_version()) +{ + detail::serializer ser(s); + return ser(v); +} +template +typename basic_value::string_type +format(const typename basic_value::key_type& k, + const basic_value& v, + const spec s = spec::default_version()) +{ + detail::serializer ser(s); + return ser(k, v); +} +template +typename basic_value::string_type +format(const std::vector::key_type>& ks, + const basic_value& v, + const spec s = spec::default_version()) +{ + detail::serializer ser(s); + return ser(ks, v); +} + +template +std::ostream& operator<<(std::ostream& os, const basic_value& v) +{ + os << format(v); + return os; +} + +} // toml + +#if defined(TOML11_COMPILE_SOURCES) +namespace toml +{ +struct type_config; +struct ordered_type_config; + +extern template typename basic_value::string_type +format(const basic_value&, const spec); + +extern template typename basic_value::string_type +format(const typename basic_value::key_type& k, + const basic_value& v, const spec); + +extern template typename basic_value::string_type +format(const std::vector::key_type>& ks, + const basic_value& v, const spec s); + +extern template typename basic_value::string_type +format(const basic_value&, const spec); + +extern template typename basic_value::string_type +format(const typename basic_value::key_type& k, + const basic_value& v, const spec); + +extern template typename basic_value::string_type +format(const std::vector::key_type>& ks, + const basic_value& v, const spec s); + +namespace detail +{ +extern template class serializer<::toml::type_config>; +extern template class serializer<::toml::ordered_type_config>; +} // detail +} // toml +#endif // TOML11_COMPILE_SOURCES + + +#endif // TOML11_SERIALIZER_HPP diff --git a/include/toml11/skip.hpp b/include/toml11/skip.hpp new file mode 100644 index 0000000..5a3b1b7 --- /dev/null +++ b/include/toml11/skip.hpp @@ -0,0 +1,392 @@ +#ifndef TOML11_SKIP_HPP +#define TOML11_SKIP_HPP + +#include "context.hpp" +#include "region.hpp" +#include "scanner.hpp" +#include "syntax.hpp" +#include "types.hpp" + +#include + +namespace toml +{ +namespace detail +{ + +template +bool skip_whitespace(location& loc, const context& ctx) +{ + return syntax::ws(ctx.toml_spec()).scan(loc).is_ok(); +} + +template +bool skip_empty_lines(location& loc, const context& ctx) +{ + return repeat_at_least(1, sequence( + syntax::ws(ctx.toml_spec()), + syntax::newline(ctx.toml_spec()) + )).scan(loc).is_ok(); +} + +// For error recovery. +// +// In case if a comment line contains an invalid character, we need to skip it +// to advance parsing. +template +void skip_comment_block(location& loc, const context& ctx) +{ + while( ! loc.eof()) + { + skip_whitespace(loc, ctx); + if(loc.current() == '#') + { + while( ! loc.eof()) + { + // both CRLF and LF ends with LF. + if(loc.current() == '\n') + { + loc.advance(); + break; + } + } + } + else if(syntax::newline(ctx.toml_spec()).scan(loc).is_ok()) + { + ; // an empty line. skip this also + } + else + { + // the next token is neither a comment nor empty line. + return ; + } + } + return ; +} + +template +void skip_empty_or_comment_lines(location& loc, const context& ctx) +{ + const auto& spec = ctx.toml_spec(); + repeat_at_least(0, sequence( + syntax::ws(spec), + maybe(syntax::comment(spec)), + syntax::newline(spec)) + ).scan(loc); + return ; +} + +// For error recovery. +// +// Sometimes we need to skip a value and find a delimiter, like `,`, `]`, or `}`. +// To find delimiter, we need to skip delimiters in a string. +// Since we are skipping invalid value while error recovery, we don't need +// to check the syntax. Here we just skip string-like region until closing quote +// is found. +template +void skip_string_like(location& loc, const context&) +{ + // if """ is found, skip until the closing """ is found. + if(literal("\"\"\"").scan(loc).is_ok()) + { + while( ! loc.eof()) + { + if(literal("\"\"\"").scan(loc).is_ok()) + { + return; + } + loc.advance(); + } + } + else if(literal("'''").scan(loc).is_ok()) + { + while( ! loc.eof()) + { + if(literal("'''").scan(loc).is_ok()) + { + return; + } + loc.advance(); + } + } + // if " is found, skip until the closing " or newline is found. + else if(loc.current() == '"') + { + while( ! loc.eof()) + { + loc.advance(); + if(loc.current() == '"' || loc.current() == '\n') + { + loc.advance(); + return; + } + } + } + else if(loc.current() == '\'') + { + while( ! loc.eof()) + { + loc.advance(); + if(loc.current() == '\'' || loc.current() == '\n') + { + loc.advance(); + return ; + } + } + } + return; +} + +template +void skip_value(location& loc, const context& ctx); +template +void skip_array_like(location& loc, const context& ctx); +template +void skip_inline_table_like(location& loc, const context& ctx); +template +void skip_key_value_pair(location& loc, const context& ctx); + +template +result +guess_value_type(const location& loc, const context& ctx); + +template +void skip_array_like(location& loc, const context& ctx) +{ + const auto& spec = ctx.toml_spec(); + assert(loc.current() == '['); + loc.advance(); + + while( ! loc.eof()) + { + if(loc.current() == '\"' || loc.current() == '\'') + { + skip_string_like(loc, ctx); + } + else if(loc.current() == '#') + { + skip_comment_block(loc, ctx); + } + else if(loc.current() == '{') + { + skip_inline_table_like(loc, ctx); + } + else if(loc.current() == '[') + { + const auto checkpoint = loc; + if(syntax::std_table(spec).scan(loc).is_ok() || + syntax::array_table(spec).scan(loc).is_ok()) + { + loc = checkpoint; + break; + } + // if it is not a table-definition, then it is an array. + skip_array_like(loc, ctx); + } + else if(loc.current() == '=') + { + // key-value pair cannot be inside the array. + // guessing the error is "missing closing bracket `]`". + // find the previous key just before `=`. + while(loc.get_location() != 0) + { + loc.retrace(); + if(loc.current() == '\n') + { + loc.advance(); + break; + } + } + break; + } + else if(loc.current() == ']') + { + break; // found closing bracket + } + else + { + loc.advance(); + } + } + return ; +} + +template +void skip_inline_table_like(location& loc, const context& ctx) +{ + assert(loc.current() == '{'); + loc.advance(); + + const auto& spec = ctx.toml_spec(); + + while( ! loc.eof()) + { + if(loc.current() == '\n' && ! spec.v1_1_0_allow_newlines_in_inline_tables) + { + break; // missing closing `}`. + } + else if(loc.current() == '\"' || loc.current() == '\'') + { + skip_string_like(loc, ctx); + } + else if(loc.current() == '#') + { + skip_comment_block(loc, ctx); + if( ! spec.v1_1_0_allow_newlines_in_inline_tables) + { + // comment must end with newline. + break; // missing closing `}`. + } + } + else if(loc.current() == '[') + { + const auto checkpoint = loc; + if(syntax::std_table(spec).scan(loc).is_ok() || + syntax::array_table(spec).scan(loc).is_ok()) + { + loc = checkpoint; + break; // missing closing `}`. + } + // if it is not a table-definition, then it is an array. + skip_array_like(loc, ctx); + } + else if(loc.current() == '{') + { + skip_inline_table_like(loc, ctx); + } + else if(loc.current() == '}') + { + // closing brace found. guessing the error is inside the table. + break; + } + else + { + // skip otherwise. + loc.advance(); + } + } + return ; +} + +template +void skip_value(location& loc, const context& ctx) +{ + value_t ty = guess_value_type(loc, ctx).unwrap_or(value_t::empty); + if(ty == value_t::string) + { + skip_string_like(loc, ctx); + } + else if(ty == value_t::array) + { + skip_array_like(loc, ctx); + } + else if(ty == value_t::table) + { + // In case of multiline tables, it may skip key-value pair but not the + // whole table. + skip_inline_table_like(loc, ctx); + } + else // others are an "in-line" values. skip until the next line + { + while( ! loc.eof()) + { + if(loc.current() == '\n') + { + break; + } + else if(loc.current() == ',' || loc.current() == ']' || loc.current() == '}') + { + break; + } + loc.advance(); + } + } + return; +} + +template +void skip_key_value_pair(location& loc, const context& ctx) +{ + while( ! loc.eof()) + { + if(loc.current() == '=') + { + skip_whitespace(loc, ctx); + skip_value(loc, ctx); + return; + } + else if(loc.current() == '\n') + { + // newline is found before finding `=`. assuming "missing `=`". + return; + } + loc.advance(); + } + return ; +} + +template +void skip_until_next_table(location& loc, const context& ctx) +{ + const auto& spec = ctx.toml_spec(); + while( ! loc.eof()) + { + if(loc.current() == '\n') + { + loc.advance(); + const auto line_begin = loc; + + skip_whitespace(loc, ctx); + if(syntax::std_table(spec).scan(loc).is_ok()) + { + loc = line_begin; + return ; + } + if(syntax::array_table(spec).scan(loc).is_ok()) + { + loc = line_begin; + return ; + } + } + loc.advance(); + } +} + +} // namespace detail +} // namespace toml + +#if defined(TOML11_COMPILE_SOURCES) +namespace toml +{ +struct type_config; +struct ordered_type_config; + +namespace detail +{ +extern template bool skip_whitespace (location& loc, const context&); +extern template bool skip_empty_lines (location& loc, const context&); +extern template void skip_comment_block (location& loc, const context&); +extern template void skip_empty_or_comment_lines(location& loc, const context&); +extern template void skip_string_like (location& loc, const context&); +extern template void skip_array_like (location& loc, const context&); +extern template void skip_inline_table_like (location& loc, const context&); +extern template void skip_value (location& loc, const context&); +extern template void skip_key_value_pair (location& loc, const context&); +extern template void skip_until_next_table (location& loc, const context&); + +extern template bool skip_whitespace (location& loc, const context&); +extern template bool skip_empty_lines (location& loc, const context&); +extern template void skip_comment_block (location& loc, const context&); +extern template void skip_empty_or_comment_lines(location& loc, const context&); +extern template void skip_string_like (location& loc, const context&); +extern template void skip_array_like (location& loc, const context&); +extern template void skip_inline_table_like (location& loc, const context&); +extern template void skip_value (location& loc, const context&); +extern template void skip_key_value_pair (location& loc, const context&); +extern template void skip_until_next_table (location& loc, const context&); + +} // detail +} // toml +#endif // TOML11_COMPILE_SOURCES + +#endif // TOML11_SKIP_HPP diff --git a/include/toml11/source_location.hpp b/include/toml11/source_location.hpp new file mode 100644 index 0000000..02e6285 --- /dev/null +++ b/include/toml11/source_location.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_SOURCE_LOCATION_HPP +#define TOML11_SOURCE_LOCATION_HPP + +#include "fwd/source_location_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/source_location_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_SOURCE_LOCATION_HPP diff --git a/include/toml11/spec.hpp b/include/toml11/spec.hpp new file mode 100644 index 0000000..ca0a996 --- /dev/null +++ b/include/toml11/spec.hpp @@ -0,0 +1,121 @@ +#ifndef TOML11_SPEC_HPP +#define TOML11_SPEC_HPP + +#include +#include + +#include + +namespace toml +{ + +struct semantic_version +{ + constexpr semantic_version(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept + : major{mjr}, minor{mnr}, patch{p} + {} + + std::uint32_t major; + std::uint32_t minor; + std::uint32_t patch; +}; + +constexpr inline semantic_version +make_semver(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept +{ + return semantic_version(mjr, mnr, p); +} + +constexpr inline bool +operator==(const semantic_version& lhs, const semantic_version& rhs) noexcept +{ + return lhs.major == rhs.major && + lhs.minor == rhs.minor && + lhs.patch == rhs.patch; +} +constexpr inline bool +operator!=(const semantic_version& lhs, const semantic_version& rhs) noexcept +{ + return !(lhs == rhs); +} +constexpr inline bool +operator<(const semantic_version& lhs, const semantic_version& rhs) noexcept +{ + return lhs.major < rhs.major || + (lhs.major == rhs.major && lhs.minor < rhs.minor) || + (lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch < rhs.patch); +} +constexpr inline bool +operator>(const semantic_version& lhs, const semantic_version& rhs) noexcept +{ + return rhs < lhs; +} +constexpr inline bool +operator<=(const semantic_version& lhs, const semantic_version& rhs) noexcept +{ + return !(lhs > rhs); +} +constexpr inline bool +operator>=(const semantic_version& lhs, const semantic_version& rhs) noexcept +{ + return !(lhs < rhs); +} + +inline std::ostream& operator<<(std::ostream& os, const semantic_version& v) +{ + os << v.major << '.' << v.minor << '.' << v.patch; + return os; +} + +inline std::string to_string(const semantic_version& v) +{ + std::ostringstream oss; + oss << v; + return oss.str(); +} + +struct spec +{ + constexpr static spec default_version() noexcept + { + return spec::v(1, 0, 0); + } + + constexpr static spec v(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept + { + return spec(make_semver(mjr, mnr, p)); + } + + constexpr explicit spec(const semantic_version& semver) noexcept + : version{semver}, + v1_1_0_allow_control_characters_in_comments {semantic_version{1, 1, 0} <= semver}, + v1_1_0_allow_newlines_in_inline_tables {semantic_version{1, 1, 0} <= semver}, + v1_1_0_allow_trailing_comma_in_inline_tables{semantic_version{1, 1, 0} <= semver}, + v1_1_0_allow_non_english_in_bare_keys {semantic_version{1, 1, 0} <= semver}, + v1_1_0_add_escape_sequence_e {semantic_version{1, 1, 0} <= semver}, + v1_1_0_add_escape_sequence_x {semantic_version{1, 1, 0} <= semver}, + v1_1_0_make_seconds_optional {semantic_version{1, 1, 0} <= semver}, + ext_hex_float {false}, + ext_num_suffix{false}, + ext_null_value{false} + {} + + semantic_version version; // toml version + + // diff from v1.0.0 -> v1.1.0 + bool v1_1_0_allow_control_characters_in_comments; + bool v1_1_0_allow_newlines_in_inline_tables; + bool v1_1_0_allow_trailing_comma_in_inline_tables; + bool v1_1_0_allow_non_english_in_bare_keys; + bool v1_1_0_add_escape_sequence_e; + bool v1_1_0_add_escape_sequence_x; + bool v1_1_0_make_seconds_optional; + + // library extensions + bool ext_hex_float; // allow hex float (in C++ style) + bool ext_num_suffix; // allow number suffix (in C++ style) + bool ext_null_value; // allow `null` as a value +}; + +} // namespace toml +#endif // TOML11_SPEC_HPP diff --git a/include/toml11/storage.hpp b/include/toml11/storage.hpp new file mode 100644 index 0000000..b63e24f --- /dev/null +++ b/include/toml11/storage.hpp @@ -0,0 +1,49 @@ +#ifndef TOML11_STORAGE_HPP +#define TOML11_STORAGE_HPP + +#include "compat.hpp" + +namespace toml +{ +namespace detail +{ + +// It owns a pointer to T. It does deep-copy when copied. +// This struct is introduced to implement a recursive type. +// +// `toml::value` contains `std::vector` to represent a toml array. +// But, in the definition of `toml::value`, `toml::value` is still incomplete. +// `std::vector` of an incomplete type is not allowed in C++11 (it is allowed +// after C++17). To avoid this, we need to use a pointer to `toml::value`, like +// `std::vector>`. Although `std::unique_ptr` is +// noncopyable, we want to make `toml::value` copyable. `storage` is introduced +// to resolve those problems. +template +struct storage +{ + using value_type = T; + + explicit storage(value_type v): ptr_(cxx::make_unique(std::move(v))) {} + ~storage() = default; + + storage(const storage& rhs): ptr_(cxx::make_unique(*rhs.ptr_)) {} + storage& operator=(const storage& rhs) + { + this->ptr_ = cxx::make_unique(*rhs.ptr_); + return *this; + } + + storage(storage&&) = default; + storage& operator=(storage&&) = default; + + bool is_ok() const noexcept {return static_cast(ptr_);} + + value_type& get() const noexcept {return *ptr_;} + + private: + std::unique_ptr ptr_; +}; + +} // detail +} // toml +#endif // TOML11_STORAGE_HPP diff --git a/include/toml11/syntax.hpp b/include/toml11/syntax.hpp new file mode 100644 index 0000000..912535d --- /dev/null +++ b/include/toml11/syntax.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_SYNTAX_HPP +#define TOML11_SYNTAX_HPP + +#include "fwd/syntax_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/syntax_impl.hpp" // IWYU pragma: export +#endif + +#endif// TOML11_SYNTAX_HPP diff --git a/include/toml11/traits.hpp b/include/toml11/traits.hpp new file mode 100644 index 0000000..7a34fbb --- /dev/null +++ b/include/toml11/traits.hpp @@ -0,0 +1,254 @@ +#ifndef TOML11_TRAITS_HPP +#define TOML11_TRAITS_HPP + +#include "from.hpp" +#include "into.hpp" +#include "compat.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#if defined(TOML11_HAS_STRING_VIEW) +#include +#endif + +#if defined(TOML11_HAS_OPTIONAL) +#include +#endif + +namespace toml +{ +template +class basic_value; + +namespace detail +{ +// --------------------------------------------------------------------------- +// check whether type T is a kind of container/map class + +struct has_iterator_impl +{ + template static std::true_type check(typename T::iterator*); + template static std::false_type check(...); +}; +struct has_value_type_impl +{ + template static std::true_type check(typename T::value_type*); + template static std::false_type check(...); +}; +struct has_key_type_impl +{ + template static std::true_type check(typename T::key_type*); + template static std::false_type check(...); +}; +struct has_mapped_type_impl +{ + template static std::true_type check(typename T::mapped_type*); + template static std::false_type check(...); +}; +struct has_reserve_method_impl +{ + template static std::false_type check(...); + template static std::true_type check( + decltype(std::declval().reserve(std::declval()))*); +}; +struct has_push_back_method_impl +{ + template static std::false_type check(...); + template static std::true_type check( + decltype(std::declval().push_back(std::declval()))*); +}; +struct is_comparable_impl +{ + template static std::false_type check(...); + template static std::true_type check( + decltype(std::declval() < std::declval())*); +}; + +struct has_from_toml_method_impl +{ + template + static std::true_type check( + decltype(std::declval().from_toml(std::declval<::toml::basic_value>()))*); + + template + static std::false_type check(...); +}; +struct has_into_toml_method_impl +{ + template + static std::true_type check(decltype(std::declval().into_toml())*); + template + static std::false_type check(...); +}; + +struct has_template_into_toml_method_impl +{ + template + static std::true_type check(decltype(std::declval().template into_toml())*); + template + static std::false_type check(...); +}; + +struct has_specialized_from_impl +{ + template + static std::false_type check(...); + template)> + static std::true_type check(::toml::from*); +}; +struct has_specialized_into_impl +{ + template + static std::false_type check(...); + template)> + static std::true_type check(::toml::into*); +}; + + +/// Intel C++ compiler can not use decltype in parent class declaration, here +/// is a hack to work around it. https://stackoverflow.com/a/23953090/4692076 +#ifdef __INTEL_COMPILER +#define decltype(...) std::enable_if::type +#endif + +template +struct has_iterator: decltype(has_iterator_impl::check(nullptr)){}; +template +struct has_value_type: decltype(has_value_type_impl::check(nullptr)){}; +template +struct has_key_type: decltype(has_key_type_impl::check(nullptr)){}; +template +struct has_mapped_type: decltype(has_mapped_type_impl::check(nullptr)){}; +template +struct has_reserve_method: decltype(has_reserve_method_impl::check(nullptr)){}; +template +struct has_push_back_method: decltype(has_push_back_method_impl::check(nullptr)){}; +template +struct is_comparable: decltype(is_comparable_impl::check(nullptr)){}; + +template +struct has_from_toml_method: decltype(has_from_toml_method_impl::check(nullptr)){}; + +template +struct has_into_toml_method: decltype(has_into_toml_method_impl::check(nullptr)){}; + +template +struct has_template_into_toml_method: decltype(has_template_into_toml_method_impl::check(nullptr)){}; + +template +struct has_specialized_from: decltype(has_specialized_from_impl::check(nullptr)){}; +template +struct has_specialized_into: decltype(has_specialized_into_impl::check(nullptr)){}; + +#ifdef __INTEL_COMPILER +#undef decltype +#endif + +// --------------------------------------------------------------------------- +// type checkers + +template struct is_std_pair_impl : std::false_type{}; +template +struct is_std_pair_impl> : std::true_type{}; +template +using is_std_pair = is_std_pair_impl>; + +template struct is_std_tuple_impl : std::false_type{}; +template +struct is_std_tuple_impl> : std::true_type{}; +template +using is_std_tuple = is_std_tuple_impl>; + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if __has_include() +template struct is_std_optional_impl : std::false_type{}; +template +struct is_std_optional_impl> : std::true_type{}; +template +using is_std_optional = is_std_optional_impl>; +# endif // +#endif // > C++17 + +template struct is_std_array_impl : std::false_type{}; +template +struct is_std_array_impl> : std::true_type{}; +template +using is_std_array = is_std_array_impl>; + +template struct is_std_forward_list_impl : std::false_type{}; +template +struct is_std_forward_list_impl> : std::true_type{}; +template +using is_std_forward_list = is_std_forward_list_impl>; + +template struct is_std_basic_string_impl : std::false_type{}; +template +struct is_std_basic_string_impl> : std::true_type{}; +template +using is_std_basic_string = is_std_basic_string_impl>; + +template struct is_1byte_std_basic_string_impl : std::false_type{}; +template +struct is_1byte_std_basic_string_impl> + : std::integral_constant {}; +template +using is_1byte_std_basic_string = is_std_basic_string_impl>; + +#if defined(TOML11_HAS_STRING_VIEW) +template struct is_std_basic_string_view_impl : std::false_type{}; +template +struct is_std_basic_string_view_impl> : std::true_type{}; +template +using is_std_basic_string_view = is_std_basic_string_view_impl>; + +template +struct is_string_view_of : std::false_type {}; +template +struct is_string_view_of, std::basic_string> : std::true_type {}; +#endif + +template struct is_chrono_duration_impl: std::false_type{}; +template +struct is_chrono_duration_impl>: std::true_type{}; +template +using is_chrono_duration = is_chrono_duration_impl>; + +template +struct is_map_impl : cxx::conjunction< // map satisfies all the following conditions + has_iterator, // has T::iterator + has_value_type, // has T::value_type + has_key_type, // has T::key_type + has_mapped_type // has T::mapped_type + >{}; +template +using is_map = is_map_impl>; + +template +struct is_container_impl : cxx::conjunction< + cxx::negation>, // not a map + cxx::negation>, // not a std::string +#ifdef TOML11_HAS_STRING_VIEW + cxx::negation>, // not a std::string_view +#endif + has_iterator, // has T::iterator + has_value_type // has T::value_type + >{}; +template +using is_container = is_container_impl>; + +template +struct is_basic_value_impl: std::false_type{}; +template +struct is_basic_value_impl<::toml::basic_value>: std::true_type{}; +template +using is_basic_value = is_basic_value_impl>; + +}// detail +}//toml +#endif // TOML11_TRAITS_HPP diff --git a/include/toml11/types.hpp b/include/toml11/types.hpp new file mode 100644 index 0000000..6d59b99 --- /dev/null +++ b/include/toml11/types.hpp @@ -0,0 +1,374 @@ +#ifndef TOML11_TYPES_HPP +#define TOML11_TYPES_HPP + +#include "comments.hpp" +#include "compat.hpp" +#include "error_info.hpp" +#include "format.hpp" +#include "ordered_map.hpp" +#include "value.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace toml +{ + +// forward decl +template +class basic_value; + +// when you use a special integer type as toml::value::integer_type, parse must +// be able to read it. So, type_config has static member functions that read the +// integer_type as {dec, hex, oct, bin}-integer. But, in most cases, operator<< +// is enough. To make config easy, we provide the default read functions. +// +// Before this functions is called, syntax is checked and prefix(`0x` etc) and +// spacer(`_`) are removed. + +template +result +read_dec_int(const std::string& str, const source_location src) +{ + constexpr auto max_digits = std::numeric_limits::digits; + assert( ! str.empty()); + + T val{0}; + std::istringstream iss(str); + iss >> val; + if(iss.fail()) + { + return err(make_error_info("toml::parse_dec_integer: " + "too large integer: current max digits = 2^" + std::to_string(max_digits), + std::move(src), "must be < 2^" + std::to_string(max_digits))); + } + return ok(val); +} + +template +result +read_hex_int(const std::string& str, const source_location src) +{ + constexpr auto max_digits = std::numeric_limits::digits; + assert( ! str.empty()); + + T val{0}; + std::istringstream iss(str); + iss >> std::hex >> val; + if(iss.fail()) + { + return err(make_error_info("toml::parse_hex_integer: " + "too large integer: current max value = 2^" + std::to_string(max_digits), + std::move(src), "must be < 2^" + std::to_string(max_digits))); + } + return ok(val); +} + +template +result +read_oct_int(const std::string& str, const source_location src) +{ + constexpr auto max_digits = std::numeric_limits::digits; + assert( ! str.empty()); + + T val{0}; + std::istringstream iss(str); + iss >> std::oct >> val; + if(iss.fail()) + { + return err(make_error_info("toml::parse_oct_integer: " + "too large integer: current max value = 2^" + std::to_string(max_digits), + std::move(src), "must be < 2^" + std::to_string(max_digits))); + } + return ok(val); +} + +template +result +read_bin_int(const std::string& str, const source_location src) +{ + constexpr auto is_bounded = std::numeric_limits::is_bounded; + constexpr auto max_digits = std::numeric_limits::digits; + const auto max_value = (std::numeric_limits::max)(); + + T val{0}; + T base{1}; + for(auto i = str.rbegin(); i != str.rend(); ++i) + { + const auto c = *i; + if(c == '1') + { + val += base; + // prevent `base` from overflow + if(is_bounded && max_value / 2 < base && std::next(i) != str.rend()) + { + base = 0; + } + else + { + base *= 2; + } + } + else + { + assert(c == '0'); + + if(is_bounded && max_value / 2 < base && std::next(i) != str.rend()) + { + base = 0; + } + else + { + base *= 2; + } + } + } + if(base == 0) + { + return err(make_error_info("toml::parse_bin_integer: " + "too large integer: current max value = 2^" + std::to_string(max_digits), + std::move(src), "must be < 2^" + std::to_string(max_digits))); + } + return ok(val); +} + +template +result +read_int(const std::string& str, const source_location src, const std::uint8_t base) +{ + assert(base == 10 || base == 16 || base == 8 || base == 2); + switch(base) + { + case 2: { return read_bin_int(str, src); } + case 8: { return read_oct_int(str, src); } + case 16: { return read_hex_int(str, src); } + default: + { + assert(base == 10); + return read_dec_int(str, src); + } + } +} + +inline result +read_hex_float(const std::string& str, const source_location src, float val) +{ +#if defined(_MSC_VER) && ! defined(__clang__) + const auto res = ::sscanf_s(str.c_str(), "%a", std::addressof(val)); +#else + const auto res = std::sscanf(str.c_str(), "%a", std::addressof(val)); +#endif + if(res != 1) + { + return err(make_error_info("toml::parse_floating: " + "failed to read hexadecimal floating point value ", + std::move(src), "here")); + } + return ok(val); +} +inline result +read_hex_float(const std::string& str, const source_location src, double val) +{ +#if defined(_MSC_VER) && ! defined(__clang__) + const auto res = ::sscanf_s(str.c_str(), "%la", std::addressof(val)); +#else + const auto res = std::sscanf(str.c_str(), "%la", std::addressof(val)); +#endif + if(res != 1) + { + return err(make_error_info("toml::parse_floating: " + "failed to read hexadecimal floating point value ", + std::move(src), "here")); + } + return ok(val); +} +template +cxx::enable_if_t, double>>, + cxx::negation, float>> + >::value, result> +read_hex_float(const std::string&, const source_location src, T) +{ + return err(make_error_info("toml::parse_floating: failed to read " + "floating point value because of unknown type in type_config", + std::move(src), "here")); +} + +template +result +read_dec_float(const std::string& str, const source_location src) +{ + T val; + std::istringstream iss(str); + iss >> val; + if(iss.fail()) + { + return err(make_error_info("toml::parse_floating: " + "failed to read floating point value from stream", + std::move(src), "here")); + } + return ok(val); +} + +template +result +read_float(const std::string& str, const source_location src, const bool is_hex) +{ + if(is_hex) + { + return read_hex_float(str, src, T{}); + } + else + { + return read_dec_float(str, src); + } +} + +struct type_config +{ + using comment_type = preserve_comments; + + using boolean_type = bool; + using integer_type = std::int64_t; + using floating_type = double; + using string_type = std::string; + + template + using array_type = std::vector; + template + using table_type = std::unordered_map; + + static result + parse_int(const std::string& str, const source_location src, const std::uint8_t base) + { + return read_int(str, src, base); + } + static result + parse_float(const std::string& str, const source_location src, const bool is_hex) + { + return read_float(str, src, is_hex); + } +}; + +using value = basic_value; +using table = typename value::table_type; +using array = typename value::array_type; + +struct ordered_type_config +{ + using comment_type = preserve_comments; + + using boolean_type = bool; + using integer_type = std::int64_t; + using floating_type = double; + using string_type = std::string; + + template + using array_type = std::vector; + template + using table_type = ordered_map; + + static result + parse_int(const std::string& str, const source_location src, const std::uint8_t base) + { + return read_int(str, src, base); + } + static result + parse_float(const std::string& str, const source_location src, const bool is_hex) + { + return read_float(str, src, is_hex); + } +}; + +using ordered_value = basic_value; +using ordered_table = typename ordered_value::table_type; +using ordered_array = typename ordered_value::array_type; + +// ---------------------------------------------------------------------------- +// meta functions for internal use + +namespace detail +{ + +// ---------------------------------------------------------------------------- +// check if type T has all the needed member types + +template +struct has_comment_type: std::false_type{}; +template +struct has_comment_type>: std::true_type{}; + +template +struct has_integer_type: std::false_type{}; +template +struct has_integer_type>: std::true_type{}; + +template +struct has_floating_type: std::false_type{}; +template +struct has_floating_type>: std::true_type{}; + +template +struct has_string_type: std::false_type{}; +template +struct has_string_type>: std::true_type{}; + +template +struct has_array_type: std::false_type{}; +template +struct has_array_type>>: std::true_type{}; + +template +struct has_table_type: std::false_type{}; +template +struct has_table_type>>: std::true_type{}; + +template +struct has_parse_int: std::false_type{}; +template +struct has_parse_int().parse_int( + std::declval(), + std::declval<::toml::source_location const&>(), + std::declval() + ))>>: std::true_type{}; + +template +struct has_parse_float: std::false_type{}; +template +struct has_parse_float().parse_float( + std::declval(), + std::declval<::toml::source_location const&>(), + std::declval() + ))>>: std::true_type{}; + +template +using is_type_config = cxx::conjunction< + has_comment_type, + has_integer_type, + has_floating_type, + has_string_type, + has_array_type, + has_table_type, + has_parse_int, + has_parse_float + >; + +} // namespace detail +} // namespace toml + +#if defined(TOML11_COMPILE_SOURCES) +namespace toml +{ +extern template class basic_value; +extern template class basic_value; +} // toml +#endif // TOML11_COMPILE_SOURCES + +#endif // TOML11_TYPES_HPP diff --git a/include/toml11/utility.hpp b/include/toml11/utility.hpp new file mode 100644 index 0000000..9e30ccb --- /dev/null +++ b/include/toml11/utility.hpp @@ -0,0 +1,170 @@ +#ifndef TOML11_UTILITY_HPP +#define TOML11_UTILITY_HPP + +#include "result.hpp" +#include "traits.hpp" + +#include +#include + +#include +#include +#include + +namespace toml +{ +namespace detail +{ + +// to output character in an error message. +inline std::string show_char(const int c) +{ + using char_type = unsigned char; + if(std::isgraph(c)) + { + return std::string(1, static_cast(c)); + } + else + { + std::array buf; + buf.fill('\0'); + const auto r = std::snprintf(buf.data(), buf.size(), "0x%02x", c & 0xFF); + assert(r == static_cast(buf.size()) - 1); + (void) r; // Unused variable warning + auto in_hex = std::string(buf.data()); + switch(c) + { + case char_type('\0'): {in_hex += "(NUL)"; break;} + case char_type(' ') : {in_hex += "(SPACE)"; break;} + case char_type('\n'): {in_hex += "(LINE FEED)"; break;} + case char_type('\r'): {in_hex += "(CARRIAGE RETURN)"; break;} + case char_type('\t'): {in_hex += "(TAB)"; break;} + case char_type('\v'): {in_hex += "(VERTICAL TAB)"; break;} + case char_type('\f'): {in_hex += "(FORM FEED)"; break;} + case char_type('\x1B'): {in_hex += "(ESCAPE)"; break;} + default: break; + } + return in_hex; + } +} + +// --------------------------------------------------------------------------- + +template +void try_reserve_impl(Container& container, std::size_t N, std::true_type) +{ + container.reserve(N); + return; +} +template +void try_reserve_impl(Container&, std::size_t, std::false_type) noexcept +{ + return; +} + +template +void try_reserve(Container& container, std::size_t N) +{ + try_reserve_impl(container, N, has_reserve_method{}); + return; +} + +// --------------------------------------------------------------------------- + +template +result from_string(const std::string& str) +{ + T v; + std::istringstream iss(str); + iss >> v; + if(iss.fail()) + { + return err(); + } + return ok(v); +} + +// --------------------------------------------------------------------------- + +// helper function to avoid std::string(0, 'c') or std::string(iter, iter) +template +std::string make_string(Iterator first, Iterator last) +{ + if(first == last) {return "";} + return std::string(first, last); +} +inline std::string make_string(std::size_t len, char c) +{ + if(len == 0) {return "";} + return std::string(len, c); +} + +// --------------------------------------------------------------------------- + +template +struct string_conv_impl +{ + static_assert(sizeof(Char) == sizeof(char), ""); + static_assert(sizeof(Char2) == sizeof(char), ""); + + static std::basic_string invoke(std::basic_string s) + { + std::basic_string retval; + std::transform(s.begin(), s.end(), std::back_inserter(retval), + [](const Char2 c) {return static_cast(c);}); + return retval; + } + template + static std::basic_string invoke(const Char2 (&s)[N]) + { + std::basic_string retval; + // "string literal" has null-char at the end. to skip it, we use prev. + std::transform(std::begin(s), std::prev(std::end(s)), std::back_inserter(retval), + [](const Char2 c) {return static_cast(c);}); + return retval; + } +}; + +template +struct string_conv_impl +{ + static_assert(sizeof(Char) == sizeof(char), ""); + + static std::basic_string invoke(std::basic_string s) + { + return s; + } + template + static std::basic_string invoke(const Char (&s)[N]) + { + return std::basic_string(s); + } +}; + +template +cxx::enable_if_t::value, S> +string_conv(std::basic_string s) +{ + using C = typename S::value_type; + using T = typename S::traits_type; + using A = typename S::allocator_type; + return string_conv_impl::invoke(std::move(s)); +} +template +cxx::enable_if_t::value, S> +string_conv(const char (&s)[N]) +{ + using C = typename S::value_type; + using T = typename S::traits_type; + using A = typename S::allocator_type; + using C2 = char; + using T2 = std::char_traits; + using A2 = std::allocator; + + return string_conv_impl::template invoke(s); +} + +} // namespace detail +} // namespace toml +#endif // TOML11_UTILITY_HPP diff --git a/include/toml11/value.hpp b/include/toml11/value.hpp new file mode 100644 index 0000000..d945fa0 --- /dev/null +++ b/include/toml11/value.hpp @@ -0,0 +1,2257 @@ +#ifndef TOML11_VALUE_HPP +#define TOML11_VALUE_HPP + +#include "color.hpp" +#include "datetime.hpp" +#include "exception.hpp" +#include "error_info.hpp" +#include "format.hpp" +#include "region.hpp" +#include "source_location.hpp" +#include "storage.hpp" +#include "traits.hpp" +#include "value_t.hpp" +#include "version.hpp" // IWYU pragma: keep < TOML11_HAS_STRING_VIEW + +#ifdef TOML11_HAS_STRING_VIEW +#include +#endif + +#include + +namespace toml +{ +template +class basic_value; + +struct type_error final : public ::toml::exception +{ + public: + type_error(std::string what_arg, source_location loc) + : what_(std::move(what_arg)), loc_(std::move(loc)) + {} + ~type_error() noexcept override = default; + + const char* what() const noexcept override {return what_.c_str();} + + source_location const& location() const noexcept {return loc_;} + + private: + std::string what_; + source_location loc_; +}; + +// only for internal use +namespace detail +{ +template +error_info make_type_error(const basic_value&, const std::string&, const value_t); + +template +error_info make_not_found_error(const basic_value&, const std::string&, const typename basic_value::key_type&); + +template +void change_region_of_value(basic_value&, const basic_value&); + +template +struct getter; +} // detail + +template +class basic_value +{ + public: + + using config_type = TypeConfig; + using key_type = typename config_type::string_type; + using value_type = basic_value; + using boolean_type = typename config_type::boolean_type; + using integer_type = typename config_type::integer_type; + using floating_type = typename config_type::floating_type; + using string_type = typename config_type::string_type; + using local_time_type = ::toml::local_time; + using local_date_type = ::toml::local_date; + using local_datetime_type = ::toml::local_datetime; + using offset_datetime_type = ::toml::offset_datetime; + using array_type = typename config_type::template array_type; + using table_type = typename config_type::template table_type; + using comment_type = typename config_type::comment_type; + using char_type = typename string_type::value_type; + + private: + + using region_type = detail::region; + + public: + + basic_value() noexcept + : type_(value_t::empty), empty_('\0'), region_{}, comments_{} + {} + ~basic_value() noexcept {this->cleanup();} + + // copy/move constructor/assigner ===================================== {{{ + + basic_value(const basic_value& v) + : type_(v.type_), region_(v.region_), comments_(v.comments_) + { + switch(this->type_) + { + case value_t::boolean : assigner(boolean_ , v.boolean_ ); break; + case value_t::integer : assigner(integer_ , v.integer_ ); break; + case value_t::floating : assigner(floating_ , v.floating_ ); break; + case value_t::string : assigner(string_ , v.string_ ); break; + case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break; + case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break; + case value_t::local_date : assigner(local_date_ , v.local_date_ ); break; + case value_t::local_time : assigner(local_time_ , v.local_time_ ); break; + case value_t::array : assigner(array_ , v.array_ ); break; + case value_t::table : assigner(table_ , v.table_ ); break; + default : assigner(empty_ , '\0' ); break; + } + } + basic_value(basic_value&& v) + : type_(v.type()), region_(std::move(v.region_)), + comments_(std::move(v.comments_)) + { + switch(this->type_) + { + case value_t::boolean : assigner(boolean_ , std::move(v.boolean_ )); break; + case value_t::integer : assigner(integer_ , std::move(v.integer_ )); break; + case value_t::floating : assigner(floating_ , std::move(v.floating_ )); break; + case value_t::string : assigner(string_ , std::move(v.string_ )); break; + case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break; + case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break; + case value_t::local_date : assigner(local_date_ , std::move(v.local_date_ )); break; + case value_t::local_time : assigner(local_time_ , std::move(v.local_time_ )); break; + case value_t::array : assigner(array_ , std::move(v.array_ )); break; + case value_t::table : assigner(table_ , std::move(v.table_ )); break; + default : assigner(empty_ , '\0' ); break; + } + } + + basic_value& operator=(const basic_value& v) + { + if(this == std::addressof(v)) {return *this;} + + this->cleanup(); + this->type_ = v.type_; + this->region_ = v.region_; + this->comments_ = v.comments_; + switch(this->type_) + { + case value_t::boolean : assigner(boolean_ , v.boolean_ ); break; + case value_t::integer : assigner(integer_ , v.integer_ ); break; + case value_t::floating : assigner(floating_ , v.floating_ ); break; + case value_t::string : assigner(string_ , v.string_ ); break; + case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break; + case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break; + case value_t::local_date : assigner(local_date_ , v.local_date_ ); break; + case value_t::local_time : assigner(local_time_ , v.local_time_ ); break; + case value_t::array : assigner(array_ , v.array_ ); break; + case value_t::table : assigner(table_ , v.table_ ); break; + default : assigner(empty_ , '\0' ); break; + } + return *this; + } + basic_value& operator=(basic_value&& v) + { + if(this == std::addressof(v)) {return *this;} + + this->cleanup(); + this->type_ = v.type_; + this->region_ = std::move(v.region_); + this->comments_ = std::move(v.comments_); + switch(this->type_) + { + case value_t::boolean : assigner(boolean_ , std::move(v.boolean_ )); break; + case value_t::integer : assigner(integer_ , std::move(v.integer_ )); break; + case value_t::floating : assigner(floating_ , std::move(v.floating_ )); break; + case value_t::string : assigner(string_ , std::move(v.string_ )); break; + case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break; + case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break; + case value_t::local_date : assigner(local_date_ , std::move(v.local_date_ )); break; + case value_t::local_time : assigner(local_time_ , std::move(v.local_time_ )); break; + case value_t::array : assigner(array_ , std::move(v.array_ )); break; + case value_t::table : assigner(table_ , std::move(v.table_ )); break; + default : assigner(empty_ , '\0' ); break; + } + return *this; + } + // }}} + + // constructor to overwrite commnets ================================== {{{ + + basic_value(basic_value v, std::vector com) + : type_(v.type()), region_(std::move(v.region_)), + comments_(std::move(com)) + { + switch(this->type_) + { + case value_t::boolean : assigner(boolean_ , std::move(v.boolean_ )); break; + case value_t::integer : assigner(integer_ , std::move(v.integer_ )); break; + case value_t::floating : assigner(floating_ , std::move(v.floating_ )); break; + case value_t::string : assigner(string_ , std::move(v.string_ )); break; + case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break; + case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break; + case value_t::local_date : assigner(local_date_ , std::move(v.local_date_ )); break; + case value_t::local_time : assigner(local_time_ , std::move(v.local_time_ )); break; + case value_t::array : assigner(array_ , std::move(v.array_ )); break; + case value_t::table : assigner(table_ , std::move(v.table_ )); break; + default : assigner(empty_ , '\0' ); break; + } + } + // }}} + + // conversion between different basic_values ========================== {{{ + + template + basic_value(basic_value other) + : type_(other.type_), + region_(std::move(other.region_)), + comments_(std::move(other.comments_)) + { + switch(other.type_) + { + // use auto-convert in constructor + case value_t::boolean : assigner(boolean_ , std::move(other.boolean_ )); break; + case value_t::integer : assigner(integer_ , std::move(other.integer_ )); break; + case value_t::floating : assigner(floating_ , std::move(other.floating_ )); break; + case value_t::string : assigner(string_ , std::move(other.string_ )); break; + case value_t::offset_datetime: assigner(offset_datetime_, std::move(other.offset_datetime_)); break; + case value_t::local_datetime : assigner(local_datetime_ , std::move(other.local_datetime_ )); break; + case value_t::local_date : assigner(local_date_ , std::move(other.local_date_ )); break; + case value_t::local_time : assigner(local_time_ , std::move(other.local_time_ )); break; + + // may have different container type + case value_t::array : + { + array_type tmp( + std::make_move_iterator(other.array_.value.get().begin()), + std::make_move_iterator(other.array_.value.get().end())); + assigner(array_, array_storage( + detail::storage(std::move(tmp)), + other.array_.format + )); + break; + } + case value_t::table : + { + table_type tmp( + std::make_move_iterator(other.table_.value.get().begin()), + std::make_move_iterator(other.table_.value.get().end())); + assigner(table_, table_storage( + detail::storage(std::move(tmp)), + other.table_.format + )); + break; + } + default: break; + } + } + + template + basic_value(basic_value other, std::vector com) + : type_(other.type_), + region_(std::move(other.region_)), + comments_(std::move(com)) + { + switch(other.type_) + { + // use auto-convert in constructor + case value_t::boolean : assigner(boolean_ , std::move(other.boolean_ )); break; + case value_t::integer : assigner(integer_ , std::move(other.integer_ )); break; + case value_t::floating : assigner(floating_ , std::move(other.floating_ )); break; + case value_t::string : assigner(string_ , std::move(other.string_ )); break; + case value_t::offset_datetime: assigner(offset_datetime_, std::move(other.offset_datetime_)); break; + case value_t::local_datetime : assigner(local_datetime_ , std::move(other.local_datetime_ )); break; + case value_t::local_date : assigner(local_date_ , std::move(other.local_date_ )); break; + case value_t::local_time : assigner(local_time_ , std::move(other.local_time_ )); break; + + // may have different container type + case value_t::array : + { + array_type tmp( + std::make_move_iterator(other.array_.value.get().begin()), + std::make_move_iterator(other.array_.value.get().end())); + assigner(array_, array_storage( + detail::storage(std::move(tmp)), + other.array_.format + )); + break; + } + case value_t::table : + { + table_type tmp( + std::make_move_iterator(other.table_.value.get().begin()), + std::make_move_iterator(other.table_.value.get().end())); + assigner(table_, table_storage( + detail::storage(std::move(tmp)), + other.table_.format + )); + break; + } + default: break; + } + } + template + basic_value& operator=(basic_value other) + { + this->cleanup(); + this->region_ = other.region_; + this->comments_ = comment_type(other.comments_); + this->type_ = other.type_; + switch(other.type_) + { + // use auto-convert in constructor + case value_t::boolean : assigner(boolean_ , std::move(other.boolean_ )); break; + case value_t::integer : assigner(integer_ , std::move(other.integer_ )); break; + case value_t::floating : assigner(floating_ , std::move(other.floating_ )); break; + case value_t::string : assigner(string_ , std::move(other.string_ )); break; + case value_t::offset_datetime: assigner(offset_datetime_, std::move(other.offset_datetime_)); break; + case value_t::local_datetime : assigner(local_datetime_ , std::move(other.local_datetime_ )); break; + case value_t::local_date : assigner(local_date_ , std::move(other.local_date_ )); break; + case value_t::local_time : assigner(local_time_ , std::move(other.local_time_ )); break; + + // may have different container type + case value_t::array : + { + array_type tmp( + std::make_move_iterator(other.array_.value.get().begin()), + std::make_move_iterator(other.array_.value.get().end())); + assigner(array_, array_storage( + detail::storage(std::move(tmp)), + other.array_.format + )); + break; + } + case value_t::table : + { + table_type tmp( + std::make_move_iterator(other.table_.value.get().begin()), + std::make_move_iterator(other.table_.value.get().end())); + assigner(table_, table_storage( + detail::storage(std::move(tmp)), + other.table_.format + )); + break; + } + default: break; + } + return *this; + } + // }}} + + // constructor (boolean) ============================================== {{{ + + basic_value(boolean_type x) + : basic_value(x, boolean_format_info{}, std::vector{}, region_type{}) + {} + basic_value(boolean_type x, boolean_format_info fmt) + : basic_value(x, fmt, std::vector{}, region_type{}) + {} + basic_value(boolean_type x, std::vector com) + : basic_value(x, boolean_format_info{}, std::move(com), region_type{}) + {} + basic_value(boolean_type x, boolean_format_info fmt, std::vector com) + : basic_value(x, fmt, std::move(com), region_type{}) + {} + basic_value(boolean_type x, boolean_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::boolean), boolean_(boolean_storage(x, fmt)), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(boolean_type x) + { + boolean_format_info fmt; + if(this->is_boolean()) + { + fmt = this->as_boolean_fmt(); + } + this->cleanup(); + this->type_ = value_t::boolean; + this->region_ = region_type{}; + assigner(this->boolean_, boolean_storage(x, fmt)); + return *this; + } + + // }}} + + // constructor (integer) ============================================== {{{ + + basic_value(integer_type x) + : basic_value(std::move(x), integer_format_info{}, std::vector{}, region_type{}) + {} + basic_value(integer_type x, integer_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + basic_value(integer_type x, std::vector com) + : basic_value(std::move(x), integer_format_info{}, std::move(com), region_type{}) + {} + basic_value(integer_type x, integer_format_info fmt, std::vector com) + : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) + {} + basic_value(integer_type x, integer_format_info fmt, std::vector com, region_type reg) + : type_(value_t::integer), integer_(integer_storage(std::move(x), std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(integer_type x) + { + integer_format_info fmt; + if(this->is_integer()) + { + fmt = this->as_integer_fmt(); + } + this->cleanup(); + this->type_ = value_t::integer; + this->region_ = region_type{}; + assigner(this->integer_, integer_storage(std::move(x), std::move(fmt))); + return *this; + } + + private: + + template + using enable_if_integer_like_t = cxx::enable_if_t, boolean_type>>, + cxx::negation, integer_type>>, + std::is_integral> + >::value, std::nullptr_t>; + + public: + + template = nullptr> + basic_value(T x) + : basic_value(std::move(x), integer_format_info{}, std::vector{}, region_type{}) + {} + template = nullptr> + basic_value(T x, integer_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + template = nullptr> + basic_value(T x, std::vector com) + : basic_value(std::move(x), integer_format_info{}, std::move(com), region_type{}) + {} + template = nullptr> + basic_value(T x, integer_format_info fmt, std::vector com) + : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) + {} + template = nullptr> + basic_value(T x, integer_format_info fmt, std::vector com, region_type reg) + : type_(value_t::integer), integer_(integer_storage(std::move(x), std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + template = nullptr> + basic_value& operator=(T x) + { + integer_format_info fmt; + if(this->is_integer()) + { + fmt = this->as_integer_fmt(); + } + this->cleanup(); + this->type_ = value_t::integer; + this->region_ = region_type{}; + assigner(this->integer_, integer_storage(x, std::move(fmt))); + return *this; + } + + // }}} + + // constructor (floating) ============================================= {{{ + + basic_value(floating_type x) + : basic_value(std::move(x), floating_format_info{}, std::vector{}, region_type{}) + {} + basic_value(floating_type x, floating_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + basic_value(floating_type x, std::vector com) + : basic_value(std::move(x), floating_format_info{}, std::move(com), region_type{}) + {} + basic_value(floating_type x, floating_format_info fmt, std::vector com) + : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) + {} + basic_value(floating_type x, floating_format_info fmt, std::vector com, region_type reg) + : type_(value_t::floating), floating_(floating_storage(std::move(x), std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(floating_type x) + { + floating_format_info fmt; + if(this->is_floating()) + { + fmt = this->as_floating_fmt(); + } + this->cleanup(); + this->type_ = value_t::floating; + this->region_ = region_type{}; + assigner(this->floating_, floating_storage(std::move(x), std::move(fmt))); + return *this; + } + + private: + + template + using enable_if_floating_like_t = cxx::enable_if_t, floating_type>>, + std::is_floating_point> + >::value, std::nullptr_t>; + + public: + + template = nullptr> + basic_value(T x) + : basic_value(x, floating_format_info{}, std::vector{}, region_type{}) + {} + + template = nullptr> + basic_value(T x, floating_format_info fmt) + : basic_value(x, std::move(fmt), std::vector{}, region_type{}) + {} + + template = nullptr> + basic_value(T x, std::vector com) + : basic_value(x, floating_format_info{}, std::move(com), region_type{}) + {} + + template = nullptr> + basic_value(T x, floating_format_info fmt, std::vector com) + : basic_value(x, std::move(fmt), std::move(com), region_type{}) + {} + + template = nullptr> + basic_value(T x, floating_format_info fmt, std::vector com, region_type reg) + : type_(value_t::floating), floating_(floating_storage(x, std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + + template = nullptr> + basic_value& operator=(T x) + { + floating_format_info fmt; + if(this->is_floating()) + { + fmt = this->as_floating_fmt(); + } + this->cleanup(); + this->type_ = value_t::floating; + this->region_ = region_type{}; + assigner(this->floating_, floating_storage(x, std::move(fmt))); + return *this; + } + + // }}} + + // constructor (string) =============================================== {{{ + + basic_value(string_type x) + : basic_value(std::move(x), string_format_info{}, std::vector{}, region_type{}) + {} + basic_value(string_type x, string_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + basic_value(string_type x, std::vector com) + : basic_value(std::move(x), string_format_info{}, std::move(com), region_type{}) + {} + basic_value(string_type x, string_format_info fmt, std::vector com) + : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) + {} + basic_value(string_type x, string_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::string), string_(string_storage(std::move(x), std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(string_type x) + { + string_format_info fmt; + if(this->is_string()) + { + fmt = this->as_string_fmt(); + } + this->cleanup(); + this->type_ = value_t::string; + this->region_ = region_type{}; + assigner(this->string_, string_storage(x, std::move(fmt))); + return *this; + } + + // "string literal" + + basic_value(const typename string_type::value_type* x) + : basic_value(x, string_format_info{}, std::vector{}, region_type{}) + {} + basic_value(const typename string_type::value_type* x, string_format_info fmt) + : basic_value(x, std::move(fmt), std::vector{}, region_type{}) + {} + basic_value(const typename string_type::value_type* x, std::vector com) + : basic_value(x, string_format_info{}, std::move(com), region_type{}) + {} + basic_value(const typename string_type::value_type* x, string_format_info fmt, std::vector com) + : basic_value(x, std::move(fmt), std::move(com), region_type{}) + {} + basic_value(const typename string_type::value_type* x, string_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::string), string_(string_storage(string_type(x), std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(const typename string_type::value_type* x) + { + string_format_info fmt; + if(this->is_string()) + { + fmt = this->as_string_fmt(); + } + this->cleanup(); + this->type_ = value_t::string; + this->region_ = region_type{}; + assigner(this->string_, string_storage(string_type(x), std::move(fmt))); + return *this; + } + +#if defined(TOML11_HAS_STRING_VIEW) + using string_view_type = std::basic_string_view< + typename string_type::value_type, typename string_type::traits_type>; + + basic_value(string_view_type x) + : basic_value(x, string_format_info{}, std::vector{}, region_type{}) + {} + basic_value(string_view_type x, string_format_info fmt) + : basic_value(x, std::move(fmt), std::vector{}, region_type{}) + {} + basic_value(string_view_type x, std::vector com) + : basic_value(x, string_format_info{}, std::move(com), region_type{}) + {} + basic_value(string_view_type x, string_format_info fmt, std::vector com) + : basic_value(x, std::move(fmt), std::move(com), region_type{}) + {} + basic_value(string_view_type x, string_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::string), string_(string_storage(string_type(x), std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(string_view_type x) + { + string_format_info fmt; + if(this->is_string()) + { + fmt = this->as_string_fmt(); + } + this->cleanup(); + this->type_ = value_t::string; + this->region_ = region_type{}; + assigner(this->string_, string_storage(string_type(x), std::move(fmt))); + return *this; + } + +#endif // TOML11_HAS_STRING_VIEW + + template, string_type>>, + detail::is_1byte_std_basic_string + >::value, std::nullptr_t> = nullptr> + basic_value(const T& x) + : basic_value(x, string_format_info{}, std::vector{}, region_type{}) + {} + template, string_type>>, + detail::is_1byte_std_basic_string + >::value, std::nullptr_t> = nullptr> + basic_value(const T& x, string_format_info fmt) + : basic_value(x, std::move(fmt), std::vector{}, region_type{}) + {} + template, string_type>>, + detail::is_1byte_std_basic_string + >::value, std::nullptr_t> = nullptr> + basic_value(const T& x, std::vector com) + : basic_value(x, string_format_info{}, std::move(com), region_type{}) + {} + template, string_type>>, + detail::is_1byte_std_basic_string + >::value, std::nullptr_t> = nullptr> + basic_value(const T& x, string_format_info fmt, std::vector com) + : basic_value(x, std::move(fmt), std::move(com), region_type{}) + {} + template, string_type>>, + detail::is_1byte_std_basic_string + >::value, std::nullptr_t> = nullptr> + basic_value(const T& x, string_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::string), + string_(string_storage(detail::string_conv(x), std::move(fmt))), + region_(std::move(reg)), comments_(std::move(com)) + {} + template, string_type>>, + detail::is_1byte_std_basic_string + >::value, std::nullptr_t> = nullptr> + basic_value& operator=(const T& x) + { + string_format_info fmt; + if(this->is_string()) + { + fmt = this->as_string_fmt(); + } + this->cleanup(); + this->type_ = value_t::string; + this->region_ = region_type{}; + assigner(this->string_, string_storage(detail::string_conv(x), std::move(fmt))); + return *this; + } + + // }}} + + // constructor (local_date) =========================================== {{{ + + basic_value(local_date_type x) + : basic_value(x, local_date_format_info{}, std::vector{}, region_type{}) + {} + basic_value(local_date_type x, local_date_format_info fmt) + : basic_value(x, fmt, std::vector{}, region_type{}) + {} + basic_value(local_date_type x, std::vector com) + : basic_value(x, local_date_format_info{}, std::move(com), region_type{}) + {} + basic_value(local_date_type x, local_date_format_info fmt, std::vector com) + : basic_value(x, fmt, std::move(com), region_type{}) + {} + basic_value(local_date_type x, local_date_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::local_date), local_date_(local_date_storage(x, fmt)), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(local_date_type x) + { + local_date_format_info fmt; + if(this->is_local_date()) + { + fmt = this->as_local_date_fmt(); + } + this->cleanup(); + this->type_ = value_t::local_date; + this->region_ = region_type{}; + assigner(this->local_date_, local_date_storage(x, fmt)); + return *this; + } + + // }}} + + // constructor (local_time) =========================================== {{{ + + basic_value(local_time_type x) + : basic_value(x, local_time_format_info{}, std::vector{}, region_type{}) + {} + basic_value(local_time_type x, local_time_format_info fmt) + : basic_value(x, fmt, std::vector{}, region_type{}) + {} + basic_value(local_time_type x, std::vector com) + : basic_value(x, local_time_format_info{}, std::move(com), region_type{}) + {} + basic_value(local_time_type x, local_time_format_info fmt, std::vector com) + : basic_value(x, fmt, std::move(com), region_type{}) + {} + basic_value(local_time_type x, local_time_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::local_time), local_time_(local_time_storage(x, fmt)), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(local_time_type x) + { + local_time_format_info fmt; + if(this->is_local_time()) + { + fmt = this->as_local_time_fmt(); + } + this->cleanup(); + this->type_ = value_t::local_time; + this->region_ = region_type{}; + assigner(this->local_time_, local_time_storage(x, fmt)); + return *this; + } + + template + basic_value(const std::chrono::duration& x) + : basic_value(local_time_type(x), local_time_format_info{}, std::vector{}, region_type{}) + {} + template + basic_value(const std::chrono::duration& x, local_time_format_info fmt) + : basic_value(local_time_type(x), std::move(fmt), std::vector{}, region_type{}) + {} + template + basic_value(const std::chrono::duration& x, std::vector com) + : basic_value(local_time_type(x), local_time_format_info{}, std::move(com), region_type{}) + {} + template + basic_value(const std::chrono::duration& x, local_time_format_info fmt, std::vector com) + : basic_value(local_time_type(x), std::move(fmt), std::move(com), region_type{}) + {} + template + basic_value(const std::chrono::duration& x, + local_time_format_info fmt, + std::vector com, region_type reg) + : basic_value(local_time_type(x), std::move(fmt), std::move(com), std::move(reg)) + {} + template + basic_value& operator=(const std::chrono::duration& x) + { + local_time_format_info fmt; + if(this->is_local_time()) + { + fmt = this->as_local_time_fmt(); + } + this->cleanup(); + this->type_ = value_t::local_time; + this->region_ = region_type{}; + assigner(this->local_time_, local_time_storage(local_time_type(x), std::move(fmt))); + return *this; + } + + // }}} + + // constructor (local_datetime) =========================================== {{{ + + basic_value(local_datetime_type x) + : basic_value(x, local_datetime_format_info{}, std::vector{}, region_type{}) + {} + basic_value(local_datetime_type x, local_datetime_format_info fmt) + : basic_value(x, fmt, std::vector{}, region_type{}) + {} + basic_value(local_datetime_type x, std::vector com) + : basic_value(x, local_datetime_format_info{}, std::move(com), region_type{}) + {} + basic_value(local_datetime_type x, local_datetime_format_info fmt, std::vector com) + : basic_value(x, fmt, std::move(com), region_type{}) + {} + basic_value(local_datetime_type x, local_datetime_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::local_datetime), local_datetime_(local_datetime_storage(x, fmt)), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(local_datetime_type x) + { + local_datetime_format_info fmt; + if(this->is_local_datetime()) + { + fmt = this->as_local_datetime_fmt(); + } + this->cleanup(); + this->type_ = value_t::local_datetime; + this->region_ = region_type{}; + assigner(this->local_datetime_, local_datetime_storage(x, fmt)); + return *this; + } + + // }}} + + // constructor (offset_datetime) =========================================== {{{ + + basic_value(offset_datetime_type x) + : basic_value(x, offset_datetime_format_info{}, std::vector{}, region_type{}) + {} + basic_value(offset_datetime_type x, offset_datetime_format_info fmt) + : basic_value(x, fmt, std::vector{}, region_type{}) + {} + basic_value(offset_datetime_type x, std::vector com) + : basic_value(x, offset_datetime_format_info{}, std::move(com), region_type{}) + {} + basic_value(offset_datetime_type x, offset_datetime_format_info fmt, std::vector com) + : basic_value(x, fmt, std::move(com), region_type{}) + {} + basic_value(offset_datetime_type x, offset_datetime_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::offset_datetime), offset_datetime_(offset_datetime_storage(x, fmt)), + region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(offset_datetime_type x) + { + offset_datetime_format_info fmt; + if(this->is_offset_datetime()) + { + fmt = this->as_offset_datetime_fmt(); + } + this->cleanup(); + this->type_ = value_t::offset_datetime; + this->region_ = region_type{}; + assigner(this->offset_datetime_, offset_datetime_storage(x, fmt)); + return *this; + } + + // system_clock::time_point + + basic_value(std::chrono::system_clock::time_point x) + : basic_value(offset_datetime_type(x), offset_datetime_format_info{}, std::vector{}, region_type{}) + {} + basic_value(std::chrono::system_clock::time_point x, offset_datetime_format_info fmt) + : basic_value(offset_datetime_type(x), fmt, std::vector{}, region_type{}) + {} + basic_value(std::chrono::system_clock::time_point x, std::vector com) + : basic_value(offset_datetime_type(x), offset_datetime_format_info{}, std::move(com), region_type{}) + {} + basic_value(std::chrono::system_clock::time_point x, offset_datetime_format_info fmt, std::vector com) + : basic_value(offset_datetime_type(x), fmt, std::move(com), region_type{}) + {} + basic_value(std::chrono::system_clock::time_point x, offset_datetime_format_info fmt, + std::vector com, region_type reg) + : basic_value(offset_datetime_type(x), std::move(fmt), std::move(com), std::move(reg)) + {} + basic_value& operator=(std::chrono::system_clock::time_point x) + { + offset_datetime_format_info fmt; + if(this->is_offset_datetime()) + { + fmt = this->as_offset_datetime_fmt(); + } + this->cleanup(); + this->type_ = value_t::offset_datetime; + this->region_ = region_type{}; + assigner(this->offset_datetime_, offset_datetime_storage(offset_datetime_type(x), fmt)); + return *this; + } + + // }}} + + // constructor (array) ================================================ {{{ + + basic_value(array_type x) + : basic_value(std::move(x), array_format_info{}, std::vector{}, region_type{}) + {} + basic_value(array_type x, array_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + basic_value(array_type x, std::vector com) + : basic_value(std::move(x), array_format_info{}, std::move(com), region_type{}) + {} + basic_value(array_type x, array_format_info fmt, std::vector com) + : basic_value(std::move(x), fmt, std::move(com), region_type{}) + {} + basic_value(array_type x, array_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::array), array_(array_storage( + detail::storage(std::move(x)), std::move(fmt) + )), region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(array_type x) + { + array_format_info fmt; + if(this->is_array()) + { + fmt = this->as_array_fmt(); + } + this->cleanup(); + this->type_ = value_t::array; + this->region_ = region_type{}; + assigner(this->array_, array_storage( + detail::storage(std::move(x)), std::move(fmt))); + return *this; + } + + private: + + template + using enable_if_array_like_t = cxx::enable_if_t, + cxx::negation>, + cxx::negation>, +#if defined(TOML11_HAS_STRING_VIEW) + cxx::negation>, +#endif + cxx::negation>, + cxx::negation> + >::value, std::nullptr_t>; + + public: + + template = nullptr> + basic_value(T x) + : basic_value(std::move(x), array_format_info{}, std::vector{}, region_type{}) + {} + template = nullptr> + basic_value(T x, array_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + template = nullptr> + basic_value(T x, std::vector com) + : basic_value(std::move(x), array_format_info{}, std::move(com), region_type{}) + {} + template = nullptr> + basic_value(T x, array_format_info fmt, std::vector com) + : basic_value(std::move(x), fmt, std::move(com), region_type{}) + {} + template = nullptr> + basic_value(T x, array_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::array), array_(array_storage( + detail::storage(array_type( + std::make_move_iterator(x.begin()), + std::make_move_iterator(x.end())) + ), std::move(fmt) + )), region_(std::move(reg)), comments_(std::move(com)) + {} + template = nullptr> + basic_value& operator=(T x) + { + array_format_info fmt; + if(this->is_array()) + { + fmt = this->as_array_fmt(); + } + this->cleanup(); + this->type_ = value_t::array; + this->region_ = region_type{}; + + array_type a(std::make_move_iterator(x.begin()), + std::make_move_iterator(x.end())); + assigner(this->array_, array_storage( + detail::storage(std::move(a)), std::move(fmt))); + return *this; + } + + // }}} + + // constructor (table) ================================================ {{{ + + basic_value(table_type x) + : basic_value(std::move(x), table_format_info{}, std::vector{}, region_type{}) + {} + basic_value(table_type x, table_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + basic_value(table_type x, std::vector com) + : basic_value(std::move(x), table_format_info{}, std::move(com), region_type{}) + {} + basic_value(table_type x, table_format_info fmt, std::vector com) + : basic_value(std::move(x), fmt, std::move(com), region_type{}) + {} + basic_value(table_type x, table_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::table), table_(table_storage( + detail::storage(std::move(x)), std::move(fmt) + )), region_(std::move(reg)), comments_(std::move(com)) + {} + basic_value& operator=(table_type x) + { + table_format_info fmt; + if(this->is_table()) + { + fmt = this->as_table_fmt(); + } + this->cleanup(); + this->type_ = value_t::table; + this->region_ = region_type{}; + assigner(this->table_, table_storage( + detail::storage(std::move(x)), std::move(fmt))); + return *this; + } + + // table-like + + private: + + template + using enable_if_table_like_t = cxx::enable_if_t>, + detail::is_map, + cxx::negation>, + cxx::negation> + >::value, std::nullptr_t>; + + public: + + template = nullptr> + basic_value(T x) + : basic_value(std::move(x), table_format_info{}, std::vector{}, region_type{}) + {} + template = nullptr> + basic_value(T x, table_format_info fmt) + : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) + {} + template = nullptr> + basic_value(T x, std::vector com) + : basic_value(std::move(x), table_format_info{}, std::move(com), region_type{}) + {} + template = nullptr> + basic_value(T x, table_format_info fmt, std::vector com) + : basic_value(std::move(x), fmt, std::move(com), region_type{}) + {} + template = nullptr> + basic_value(T x, table_format_info fmt, + std::vector com, region_type reg) + : type_(value_t::table), table_(table_storage( + detail::storage(table_type( + std::make_move_iterator(x.begin()), + std::make_move_iterator(x.end()) + )), std::move(fmt) + )), region_(std::move(reg)), comments_(std::move(com)) + {} + template = nullptr> + basic_value& operator=(T x) + { + table_format_info fmt; + if(this->is_table()) + { + fmt = this->as_table_fmt(); + } + this->cleanup(); + this->type_ = value_t::table; + this->region_ = region_type{}; + + table_type t(std::make_move_iterator(x.begin()), + std::make_move_iterator(x.end())); + assigner(this->table_, table_storage( + detail::storage(std::move(t)), std::move(fmt))); + return *this; + } + + // }}} + + // constructor (user_defined) ========================================= {{{ + + template::value, std::nullptr_t> = nullptr> + basic_value(const T& ud) + : basic_value( + into>::template into_toml(ud)) + {} + template::value, std::nullptr_t> = nullptr> + basic_value(const T& ud, std::vector com) + : basic_value( + into>::template into_toml(ud), + std::move(com)) + {} + template::value, std::nullptr_t> = nullptr> + basic_value& operator=(const T& ud) + { + *this = into>::template into_toml(ud); + return *this; + } + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud): basic_value(ud.into_toml()) {} + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud, std::vector com) + : basic_value(ud.into_toml(), std::move(com)) + {} + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value& operator=(const T& ud) + { + *this = ud.into_toml(); + return *this; + } + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud): basic_value(ud.template into_toml()) {} + + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value(const T& ud, std::vector com) + : basic_value(ud.template into_toml(), std::move(com)) + {} + template, + cxx::negation> + >::value, std::nullptr_t> = nullptr> + basic_value& operator=(const T& ud) + { + *this = ud.template into_toml(); + return *this; + } + // }}} + + // empty value with region info ======================================= {{{ + + // mainly for `null` extension + basic_value(detail::none_t, region_type reg) noexcept + : type_(value_t::empty), empty_('\0'), region_(std::move(reg)), comments_{} + {} + + // }}} + + // type checking ====================================================== {{{ + + template, value_type>::value, + std::nullptr_t> = nullptr> + bool is() const noexcept + { + return detail::type_to_enum::value == this->type_; + } + bool is(value_t t) const noexcept {return t == this->type_;} + + bool is_empty() const noexcept {return this->is(value_t::empty );} + bool is_boolean() const noexcept {return this->is(value_t::boolean );} + bool is_integer() const noexcept {return this->is(value_t::integer );} + bool is_floating() const noexcept {return this->is(value_t::floating );} + bool is_string() const noexcept {return this->is(value_t::string );} + bool is_offset_datetime() const noexcept {return this->is(value_t::offset_datetime);} + bool is_local_datetime() const noexcept {return this->is(value_t::local_datetime );} + bool is_local_date() const noexcept {return this->is(value_t::local_date );} + bool is_local_time() const noexcept {return this->is(value_t::local_time );} + bool is_array() const noexcept {return this->is(value_t::array );} + bool is_table() const noexcept {return this->is(value_t::table );} + + bool is_array_of_tables() const noexcept + { + if( ! this->is_array()) {return false;} + const auto& a = this->as_array(std::nothrow); // already checked. + + // when you define [[array.of.tables]], at least one empty table will be + // assigned. In case of array of inline tables, `array_of_tables = []`, + // there is no reason to consider this as an array of *tables*. + // So empty array is not an array-of-tables. + if(a.empty()) {return false;} + + // since toml v1.0.0 allows array of heterogeneous types, we need to + // check all the elements. if any of the elements is not a table, it + // is a heterogeneous array and cannot be expressed by `[[aot]]` form. + for(const auto& e : a) + { + if( ! e.is_table()) {return false;} + } + return true; + } + + value_t type() const noexcept {return type_;} + + // }}} + + // as_xxx (noexcept) version ========================================== {{{ + + template + detail::enum_to_type_t> const& + as(const std::nothrow_t&) const noexcept + { + return detail::getter::get_nothrow(*this); + } + template + detail::enum_to_type_t>& + as(const std::nothrow_t&) noexcept + { + return detail::getter::get_nothrow(*this); + } + + boolean_type const& as_boolean (const std::nothrow_t&) const noexcept {return this->boolean_.value;} + integer_type const& as_integer (const std::nothrow_t&) const noexcept {return this->integer_.value;} + floating_type const& as_floating (const std::nothrow_t&) const noexcept {return this->floating_.value;} + string_type const& as_string (const std::nothrow_t&) const noexcept {return this->string_.value;} + offset_datetime_type const& as_offset_datetime(const std::nothrow_t&) const noexcept {return this->offset_datetime_.value;} + local_datetime_type const& as_local_datetime (const std::nothrow_t&) const noexcept {return this->local_datetime_.value;} + local_date_type const& as_local_date (const std::nothrow_t&) const noexcept {return this->local_date_.value;} + local_time_type const& as_local_time (const std::nothrow_t&) const noexcept {return this->local_time_.value;} + array_type const& as_array (const std::nothrow_t&) const noexcept {return this->array_.value.get();} + table_type const& as_table (const std::nothrow_t&) const noexcept {return this->table_.value.get();} + + boolean_type & as_boolean (const std::nothrow_t&) noexcept {return this->boolean_.value;} + integer_type & as_integer (const std::nothrow_t&) noexcept {return this->integer_.value;} + floating_type & as_floating (const std::nothrow_t&) noexcept {return this->floating_.value;} + string_type & as_string (const std::nothrow_t&) noexcept {return this->string_.value;} + offset_datetime_type& as_offset_datetime(const std::nothrow_t&) noexcept {return this->offset_datetime_.value;} + local_datetime_type & as_local_datetime (const std::nothrow_t&) noexcept {return this->local_datetime_.value;} + local_date_type & as_local_date (const std::nothrow_t&) noexcept {return this->local_date_.value;} + local_time_type & as_local_time (const std::nothrow_t&) noexcept {return this->local_time_.value;} + array_type & as_array (const std::nothrow_t&) noexcept {return this->array_.value.get();} + table_type & as_table (const std::nothrow_t&) noexcept {return this->table_.value.get();} + + // }}} + + // as_xxx (throw) ===================================================== {{{ + + template + detail::enum_to_type_t> const& as() const + { + return detail::getter::get(*this); + } + template + detail::enum_to_type_t>& as() + { + return detail::getter::get(*this); + } + + boolean_type const& as_boolean() const + { + if(this->type_ != value_t::boolean) + { + this->throw_bad_cast("toml::value::as_boolean()", value_t::boolean); + } + return this->boolean_.value; + } + integer_type const& as_integer() const + { + if(this->type_ != value_t::integer) + { + this->throw_bad_cast("toml::value::as_integer()", value_t::integer); + } + return this->integer_.value; + } + floating_type const& as_floating() const + { + if(this->type_ != value_t::floating) + { + this->throw_bad_cast("toml::value::as_floating()", value_t::floating); + } + return this->floating_.value; + } + string_type const& as_string() const + { + if(this->type_ != value_t::string) + { + this->throw_bad_cast("toml::value::as_string()", value_t::string); + } + return this->string_.value; + } + offset_datetime_type const& as_offset_datetime() const + { + if(this->type_ != value_t::offset_datetime) + { + this->throw_bad_cast("toml::value::as_offset_datetime()", value_t::offset_datetime); + } + return this->offset_datetime_.value; + } + local_datetime_type const& as_local_datetime() const + { + if(this->type_ != value_t::local_datetime) + { + this->throw_bad_cast("toml::value::as_local_datetime()", value_t::local_datetime); + } + return this->local_datetime_.value; + } + local_date_type const& as_local_date() const + { + if(this->type_ != value_t::local_date) + { + this->throw_bad_cast("toml::value::as_local_date()", value_t::local_date); + } + return this->local_date_.value; + } + local_time_type const& as_local_time() const + { + if(this->type_ != value_t::local_time) + { + this->throw_bad_cast("toml::value::as_local_time()", value_t::local_time); + } + return this->local_time_.value; + } + array_type const& as_array() const + { + if(this->type_ != value_t::array) + { + this->throw_bad_cast("toml::value::as_array()", value_t::array); + } + return this->array_.value.get(); + } + table_type const& as_table() const + { + if(this->type_ != value_t::table) + { + this->throw_bad_cast("toml::value::as_table()", value_t::table); + } + return this->table_.value.get(); + } + + // ------------------------------------------------------------------------ + // nonconst reference + + boolean_type& as_boolean() + { + if(this->type_ != value_t::boolean) + { + this->throw_bad_cast("toml::value::as_boolean()", value_t::boolean); + } + return this->boolean_.value; + } + integer_type& as_integer() + { + if(this->type_ != value_t::integer) + { + this->throw_bad_cast("toml::value::as_integer()", value_t::integer); + } + return this->integer_.value; + } + floating_type& as_floating() + { + if(this->type_ != value_t::floating) + { + this->throw_bad_cast("toml::value::as_floating()", value_t::floating); + } + return this->floating_.value; + } + string_type& as_string() + { + if(this->type_ != value_t::string) + { + this->throw_bad_cast("toml::value::as_string()", value_t::string); + } + return this->string_.value; + } + offset_datetime_type& as_offset_datetime() + { + if(this->type_ != value_t::offset_datetime) + { + this->throw_bad_cast("toml::value::as_offset_datetime()", value_t::offset_datetime); + } + return this->offset_datetime_.value; + } + local_datetime_type& as_local_datetime() + { + if(this->type_ != value_t::local_datetime) + { + this->throw_bad_cast("toml::value::as_local_datetime()", value_t::local_datetime); + } + return this->local_datetime_.value; + } + local_date_type& as_local_date() + { + if(this->type_ != value_t::local_date) + { + this->throw_bad_cast("toml::value::as_local_date()", value_t::local_date); + } + return this->local_date_.value; + } + local_time_type& as_local_time() + { + if(this->type_ != value_t::local_time) + { + this->throw_bad_cast("toml::value::as_local_time()", value_t::local_time); + } + return this->local_time_.value; + } + array_type& as_array() + { + if(this->type_ != value_t::array) + { + this->throw_bad_cast("toml::value::as_array()", value_t::array); + } + return this->array_.value.get(); + } + table_type& as_table() + { + if(this->type_ != value_t::table) + { + this->throw_bad_cast("toml::value::as_table()", value_t::table); + } + return this->table_.value.get(); + } + + // }}} + + // format accessors (noexcept) ======================================== {{{ + + template + detail::enum_to_fmt_type_t const& + as_fmt(const std::nothrow_t&) const noexcept + { + return detail::getter::get_fmt_nothrow(*this); + } + template + detail::enum_to_fmt_type_t& + as_fmt(const std::nothrow_t&) noexcept + { + return detail::getter::get_fmt_nothrow(*this); + } + + boolean_format_info & as_boolean_fmt (const std::nothrow_t&) noexcept {return this->boolean_.format;} + integer_format_info & as_integer_fmt (const std::nothrow_t&) noexcept {return this->integer_.format;} + floating_format_info & as_floating_fmt (const std::nothrow_t&) noexcept {return this->floating_.format;} + string_format_info & as_string_fmt (const std::nothrow_t&) noexcept {return this->string_.format;} + offset_datetime_format_info& as_offset_datetime_fmt(const std::nothrow_t&) noexcept {return this->offset_datetime_.format;} + local_datetime_format_info & as_local_datetime_fmt (const std::nothrow_t&) noexcept {return this->local_datetime_.format;} + local_date_format_info & as_local_date_fmt (const std::nothrow_t&) noexcept {return this->local_date_.format;} + local_time_format_info & as_local_time_fmt (const std::nothrow_t&) noexcept {return this->local_time_.format;} + array_format_info & as_array_fmt (const std::nothrow_t&) noexcept {return this->array_.format;} + table_format_info & as_table_fmt (const std::nothrow_t&) noexcept {return this->table_.format;} + + boolean_format_info const& as_boolean_fmt (const std::nothrow_t&) const noexcept {return this->boolean_.format;} + integer_format_info const& as_integer_fmt (const std::nothrow_t&) const noexcept {return this->integer_.format;} + floating_format_info const& as_floating_fmt (const std::nothrow_t&) const noexcept {return this->floating_.format;} + string_format_info const& as_string_fmt (const std::nothrow_t&) const noexcept {return this->string_.format;} + offset_datetime_format_info const& as_offset_datetime_fmt(const std::nothrow_t&) const noexcept {return this->offset_datetime_.format;} + local_datetime_format_info const& as_local_datetime_fmt (const std::nothrow_t&) const noexcept {return this->local_datetime_.format;} + local_date_format_info const& as_local_date_fmt (const std::nothrow_t&) const noexcept {return this->local_date_.format;} + local_time_format_info const& as_local_time_fmt (const std::nothrow_t&) const noexcept {return this->local_time_.format;} + array_format_info const& as_array_fmt (const std::nothrow_t&) const noexcept {return this->array_.format;} + table_format_info const& as_table_fmt (const std::nothrow_t&) const noexcept {return this->table_.format;} + + // }}} + + // format accessors (throw) =========================================== {{{ + + template + detail::enum_to_fmt_type_t const& as_fmt() const + { + return detail::getter::get_fmt(*this); + } + template + detail::enum_to_fmt_type_t& as_fmt() + { + return detail::getter::get_fmt(*this); + } + + boolean_format_info const& as_boolean_fmt() const + { + if(this->type_ != value_t::boolean) + { + this->throw_bad_cast("toml::value::as_boolean_fmt()", value_t::boolean); + } + return this->boolean_.format; + } + integer_format_info const& as_integer_fmt() const + { + if(this->type_ != value_t::integer) + { + this->throw_bad_cast("toml::value::as_integer_fmt()", value_t::integer); + } + return this->integer_.format; + } + floating_format_info const& as_floating_fmt() const + { + if(this->type_ != value_t::floating) + { + this->throw_bad_cast("toml::value::as_floating_fmt()", value_t::floating); + } + return this->floating_.format; + } + string_format_info const& as_string_fmt() const + { + if(this->type_ != value_t::string) + { + this->throw_bad_cast("toml::value::as_string_fmt()", value_t::string); + } + return this->string_.format; + } + offset_datetime_format_info const& as_offset_datetime_fmt() const + { + if(this->type_ != value_t::offset_datetime) + { + this->throw_bad_cast("toml::value::as_offset_datetime_fmt()", value_t::offset_datetime); + } + return this->offset_datetime_.format; + } + local_datetime_format_info const& as_local_datetime_fmt() const + { + if(this->type_ != value_t::local_datetime) + { + this->throw_bad_cast("toml::value::as_local_datetime_fmt()", value_t::local_datetime); + } + return this->local_datetime_.format; + } + local_date_format_info const& as_local_date_fmt() const + { + if(this->type_ != value_t::local_date) + { + this->throw_bad_cast("toml::value::as_local_date_fmt()", value_t::local_date); + } + return this->local_date_.format; + } + local_time_format_info const& as_local_time_fmt() const + { + if(this->type_ != value_t::local_time) + { + this->throw_bad_cast("toml::value::as_local_time_fmt()", value_t::local_time); + } + return this->local_time_.format; + } + array_format_info const& as_array_fmt() const + { + if(this->type_ != value_t::array) + { + this->throw_bad_cast("toml::value::as_array_fmt()", value_t::array); + } + return this->array_.format; + } + table_format_info const& as_table_fmt() const + { + if(this->type_ != value_t::table) + { + this->throw_bad_cast("toml::value::as_table_fmt()", value_t::table); + } + return this->table_.format; + } + + // ------------------------------------------------------------------------ + // nonconst reference + + boolean_format_info& as_boolean_fmt() + { + if(this->type_ != value_t::boolean) + { + this->throw_bad_cast("toml::value::as_boolean_fmt()", value_t::boolean); + } + return this->boolean_.format; + } + integer_format_info& as_integer_fmt() + { + if(this->type_ != value_t::integer) + { + this->throw_bad_cast("toml::value::as_integer_fmt()", value_t::integer); + } + return this->integer_.format; + } + floating_format_info& as_floating_fmt() + { + if(this->type_ != value_t::floating) + { + this->throw_bad_cast("toml::value::as_floating_fmt()", value_t::floating); + } + return this->floating_.format; + } + string_format_info& as_string_fmt() + { + if(this->type_ != value_t::string) + { + this->throw_bad_cast("toml::value::as_string_fmt()", value_t::string); + } + return this->string_.format; + } + offset_datetime_format_info& as_offset_datetime_fmt() + { + if(this->type_ != value_t::offset_datetime) + { + this->throw_bad_cast("toml::value::as_offset_datetime_fmt()", value_t::offset_datetime); + } + return this->offset_datetime_.format; + } + local_datetime_format_info& as_local_datetime_fmt() + { + if(this->type_ != value_t::local_datetime) + { + this->throw_bad_cast("toml::value::as_local_datetime_fmt()", value_t::local_datetime); + } + return this->local_datetime_.format; + } + local_date_format_info& as_local_date_fmt() + { + if(this->type_ != value_t::local_date) + { + this->throw_bad_cast("toml::value::as_local_date_fmt()", value_t::local_date); + } + return this->local_date_.format; + } + local_time_format_info& as_local_time_fmt() + { + if(this->type_ != value_t::local_time) + { + this->throw_bad_cast("toml::value::as_local_time_fmt()", value_t::local_time); + } + return this->local_time_.format; + } + array_format_info& as_array_fmt() + { + if(this->type_ != value_t::array) + { + this->throw_bad_cast("toml::value::as_array_fmt()", value_t::array); + } + return this->array_.format; + } + table_format_info& as_table_fmt() + { + if(this->type_ != value_t::table) + { + this->throw_bad_cast("toml::value::as_table_fmt()", value_t::table); + } + return this->table_.format; + } + // }}} + + // table accessors ==================================================== {{{ + + value_type& at(const key_type& k) + { + if(!this->is_table()) + { + this->throw_bad_cast("toml::value::at(key_type)", value_t::table); + } + auto& table = this->as_table(std::nothrow); + const auto found = table.find(k); + if(found == table.end()) + { + this->throw_key_not_found_error("toml::value::at", k); + } + assert(found->first == k); + return found->second; + } + value_type const& at(const key_type& k) const + { + if(!this->is_table()) + { + this->throw_bad_cast("toml::value::at(key_type)", value_t::table); + } + const auto& table = this->as_table(std::nothrow); + const auto found = table.find(k); + if(found == table.end()) + { + this->throw_key_not_found_error("toml::value::at", k); + } + assert(found->first == k); + return found->second; + } + value_type& operator[](const key_type& k) + { + if(this->is_empty()) + { + (*this) = table_type{}; + } + else if( ! this->is_table()) // initialized, but not a table + { + this->throw_bad_cast("toml::value::operator[](key_type)", value_t::table); + } + return (this->as_table(std::nothrow))[k]; + } + std::size_t count(const key_type& k) const + { + if(!this->is_table()) + { + this->throw_bad_cast("toml::value::count(key_type)", value_t::table); + } + return this->as_table(std::nothrow).count(k); + } + bool contains(const key_type& k) const + { + if(!this->is_table()) + { + this->throw_bad_cast("toml::value::contains(key_type)", value_t::table); + } + const auto& table = this->as_table(std::nothrow); + return table.find(k) != table.end(); + } + // }}} + + // array accessors ==================================================== {{{ + + value_type& at(const std::size_t idx) + { + if(!this->is_array()) + { + this->throw_bad_cast("toml::value::at(idx)", value_t::array); + } + auto& ar = this->as_array(std::nothrow); + + if(ar.size() <= idx) + { + std::ostringstream oss; + oss << "actual length (" << ar.size() + << ") is shorter than the specified index (" << idx << ")."; + throw std::out_of_range(format_error( + "toml::value::at(idx): no element corresponding to the index", + this->location(), oss.str() + )); + } + return ar.at(idx); + } + value_type const& at(const std::size_t idx) const + { + if(!this->is_array()) + { + this->throw_bad_cast("toml::value::at(idx)", value_t::array); + } + const auto& ar = this->as_array(std::nothrow); + + if(ar.size() <= idx) + { + std::ostringstream oss; + oss << "actual length (" << ar.size() + << ") is shorter than the specified index (" << idx << ")."; + + throw std::out_of_range(format_error( + "toml::value::at(idx): no element corresponding to the index", + this->location(), oss.str() + )); + } + return ar.at(idx); + } + + value_type& operator[](const std::size_t idx) noexcept + { + // no check... + return this->as_array(std::nothrow)[idx]; + } + value_type const& operator[](const std::size_t idx) const noexcept + { + // no check... + return this->as_array(std::nothrow)[idx]; + } + + void push_back(const value_type& x) + { + if(!this->is_array()) + { + this->throw_bad_cast("toml::value::push_back(idx)", value_t::array); + } + this->as_array(std::nothrow).push_back(x); + return; + } + void push_back(value_type&& x) + { + if(!this->is_array()) + { + this->throw_bad_cast("toml::value::push_back(idx)", value_t::array); + } + this->as_array(std::nothrow).push_back(std::move(x)); + return; + } + + template + value_type& emplace_back(Ts&& ... args) + { + if(!this->is_array()) + { + this->throw_bad_cast("toml::value::emplace_back(idx)", value_t::array); + } + auto& ar = this->as_array(std::nothrow); + ar.emplace_back(std::forward(args) ...); + return ar.back(); + } + + std::size_t size() const + { + switch(this->type_) + { + case value_t::array: + { + return this->as_array(std::nothrow).size(); + } + case value_t::table: + { + return this->as_table(std::nothrow).size(); + } + case value_t::string: + { + return this->as_string(std::nothrow).size(); + } + default: + { + throw type_error(format_error( + "toml::value::size(): bad_cast to container types", + this->location(), + "the actual type is " + to_string(this->type_) + ), this->location()); + } + } + } + + // }}} + + source_location location() const + { + return source_location(this->region_); + } + + comment_type const& comments() const noexcept {return this->comments_;} + comment_type& comments() noexcept {return this->comments_;} + + private: + + // private helper functions =========================================== {{{ + + void cleanup() noexcept + { + switch(this->type_) + { + case value_t::boolean : { boolean_ .~boolean_storage (); break; } + case value_t::integer : { integer_ .~integer_storage (); break; } + case value_t::floating : { floating_ .~floating_storage (); break; } + case value_t::string : { string_ .~string_storage (); break; } + case value_t::offset_datetime : { offset_datetime_.~offset_datetime_storage (); break; } + case value_t::local_datetime : { local_datetime_ .~local_datetime_storage (); break; } + case value_t::local_date : { local_date_ .~local_date_storage (); break; } + case value_t::local_time : { local_time_ .~local_time_storage (); break; } + case value_t::array : { array_ .~array_storage (); break; } + case value_t::table : { table_ .~table_storage (); break; } + default : { break; } + } + this->type_ = value_t::empty; + return; + } + + template + static void assigner(T& dst, U&& v) + { + const auto tmp = ::new(std::addressof(dst)) T(std::forward(v)); + assert(tmp == std::addressof(dst)); + (void)tmp; + } + + [[noreturn]] + void throw_bad_cast(const std::string& funcname, const value_t ty) const + { + throw type_error(format_error(detail::make_type_error(*this, funcname, ty)), + this->location()); + } + + [[noreturn]] + void throw_key_not_found_error(const std::string& funcname, const key_type& key) const + { + throw std::out_of_range(format_error( + detail::make_not_found_error(*this, funcname, key))); + } + + template + friend void detail::change_region_of_value(basic_value&, const basic_value&); + + template + friend class basic_value; + + // }}} + + private: + + using boolean_storage = detail::value_with_format; + using integer_storage = detail::value_with_format; + using floating_storage = detail::value_with_format; + using string_storage = detail::value_with_format; + using offset_datetime_storage = detail::value_with_format; + using local_datetime_storage = detail::value_with_format; + using local_date_storage = detail::value_with_format; + using local_time_storage = detail::value_with_format; + using array_storage = detail::value_with_format, array_format_info >; + using table_storage = detail::value_with_format, table_format_info >; + + private: + + value_t type_; + union + { + char empty_; // the smallest type + boolean_storage boolean_; + integer_storage integer_; + floating_storage floating_; + string_storage string_; + offset_datetime_storage offset_datetime_; + local_datetime_storage local_datetime_; + local_date_storage local_date_; + local_time_storage local_time_; + array_storage array_; + table_storage table_; + }; + region_type region_; + comment_type comments_; +}; + +template +bool operator==(const basic_value& lhs, const basic_value& rhs) +{ + if(lhs.type() != rhs.type()) {return false;} + if(lhs.comments() != rhs.comments()) {return false;} + + switch(lhs.type()) + { + case value_t::boolean : + { + return lhs.as_boolean() == rhs.as_boolean(); + } + case value_t::integer : + { + return lhs.as_integer() == rhs.as_integer(); + } + case value_t::floating : + { + return lhs.as_floating() == rhs.as_floating(); + } + case value_t::string : + { + return lhs.as_string() == rhs.as_string(); + } + case value_t::offset_datetime: + { + return lhs.as_offset_datetime() == rhs.as_offset_datetime(); + } + case value_t::local_datetime: + { + return lhs.as_local_datetime() == rhs.as_local_datetime(); + } + case value_t::local_date: + { + return lhs.as_local_date() == rhs.as_local_date(); + } + case value_t::local_time: + { + return lhs.as_local_time() == rhs.as_local_time(); + } + case value_t::array : + { + return lhs.as_array() == rhs.as_array(); + } + case value_t::table : + { + return lhs.as_table() == rhs.as_table(); + } + case value_t::empty : {return true; } + default: {return false;} + } +} + +template +bool operator!=(const basic_value& lhs, const basic_value& rhs) +{ + return !(lhs == rhs); +} + +template +cxx::enable_if_t::array_type>, + detail::is_comparable::table_type> + >::value, bool> +operator<(const basic_value& lhs, const basic_value& rhs) +{ + if(lhs.type() != rhs.type()) + { + return (lhs.type() < rhs.type()); + } + switch(lhs.type()) + { + case value_t::boolean : + { + return lhs.as_boolean() < rhs.as_boolean() || + (lhs.as_boolean() == rhs.as_boolean() && + lhs.comments() < rhs.comments()); + } + case value_t::integer : + { + return lhs.as_integer() < rhs.as_integer() || + (lhs.as_integer() == rhs.as_integer() && + lhs.comments() < rhs.comments()); + } + case value_t::floating : + { + return lhs.as_floating() < rhs.as_floating() || + (lhs.as_floating() == rhs.as_floating() && + lhs.comments() < rhs.comments()); + } + case value_t::string : + { + return lhs.as_string() < rhs.as_string() || + (lhs.as_string() == rhs.as_string() && + lhs.comments() < rhs.comments()); + } + case value_t::offset_datetime: + { + return lhs.as_offset_datetime() < rhs.as_offset_datetime() || + (lhs.as_offset_datetime() == rhs.as_offset_datetime() && + lhs.comments() < rhs.comments()); + } + case value_t::local_datetime: + { + return lhs.as_local_datetime() < rhs.as_local_datetime() || + (lhs.as_local_datetime() == rhs.as_local_datetime() && + lhs.comments() < rhs.comments()); + } + case value_t::local_date: + { + return lhs.as_local_date() < rhs.as_local_date() || + (lhs.as_local_date() == rhs.as_local_date() && + lhs.comments() < rhs.comments()); + } + case value_t::local_time: + { + return lhs.as_local_time() < rhs.as_local_time() || + (lhs.as_local_time() == rhs.as_local_time() && + lhs.comments() < rhs.comments()); + } + case value_t::array : + { + return lhs.as_array() < rhs.as_array() || + (lhs.as_array() == rhs.as_array() && + lhs.comments() < rhs.comments()); + } + case value_t::table : + { + return lhs.as_table() < rhs.as_table() || + (lhs.as_table() == rhs.as_table() && + lhs.comments() < rhs.comments()); + } + case value_t::empty : + { + return lhs.comments() < rhs.comments(); + } + default: + { + return lhs.comments() < rhs.comments(); + } + } +} + +template +cxx::enable_if_t::array_type>, + detail::is_comparable::table_type> + >::value, bool> +operator<=(const basic_value& lhs, const basic_value& rhs) +{ + return (lhs < rhs) || (lhs == rhs); +} +template +cxx::enable_if_t::array_type>, + detail::is_comparable::table_type> + >::value, bool> +operator>(const basic_value& lhs, const basic_value& rhs) +{ + return !(lhs <= rhs); +} +template +cxx::enable_if_t::array_type>, + detail::is_comparable::table_type> + >::value, bool> +operator>=(const basic_value& lhs, const basic_value& rhs) +{ + return !(lhs < rhs); +} + +// error_info helper +namespace detail +{ +template +error_info make_error_info_rec(error_info e, + const basic_value& v, std::string msg, Ts&& ... tail) +{ + return make_error_info_rec(std::move(e), v.location(), std::move(msg), std::forward(tail)...); +} +} // detail + +template +error_info make_error_info( + std::string title, const basic_value& v, std::string msg, Ts&& ... tail) +{ + return make_error_info(std::move(title), + v.location(), std::move(msg), std::forward(tail)...); +} +template +std::string format_error(std::string title, + const basic_value& v, std::string msg, Ts&& ... tail) +{ + return format_error(std::move(title), + v.location(), std::move(msg), std::forward(tail)...); +} + +namespace detail +{ + +template +error_info make_type_error(const basic_value& v, const std::string& fname, const value_t ty) +{ + return make_error_info(fname + ": bad_cast to " + to_string(ty), + v.location(), "the actual type is " + to_string(v.type())); +} +template +error_info make_not_found_error(const basic_value& v, const std::string& fname, const typename basic_value::key_type& key) +{ + const auto loc = v.location(); + const std::string title = fname + ": key \"" + string_conv(key) + "\" not found"; + + std::vector> locs; + if( ! loc.is_ok()) + { + return error_info(title, locs); + } + + if(loc.first_line_number() == 1 && loc.first_column_number() == 1 && loc.length() == 1) + { + // The top-level table has its region at the 0th character of the file. + // That means that, in the case when a key is not found in the top-level + // table, the error message points to the first character. If the file has + // the first table at the first line, the error message would be like this. + // ```console + // [error] key "a" not found + // --> example.toml + // | + // 1 | [table] + // | ^------ in this table + // ``` + // It actually points to the top-level table at the first character, not + // `[table]`. But it is too confusing. To avoid the confusion, the error + // message should explicitly say "key not found in the top-level table". + locs.emplace_back(v.location(), "at the top-level table"); + } + else + { + locs.emplace_back(v.location(), "in this table"); + } + return error_info(title, locs); +} + +#define TOML11_DETAIL_GENERATE_COMPTIME_GETTER(ty) \ + template \ + struct getter \ + { \ + using value_type = basic_value; \ + using result_type = enum_to_type_t; \ + using format_type = enum_to_fmt_type_t; \ + \ + static result_type& get(value_type& v) \ + { \ + return v.as_ ## ty(); \ + } \ + static result_type const& get(const value_type& v) \ + { \ + return v.as_ ## ty(); \ + } \ + \ + static result_type& get_nothrow(value_type& v) noexcept \ + { \ + return v.as_ ## ty(std::nothrow); \ + } \ + static result_type const& get_nothrow(const value_type& v) noexcept \ + { \ + return v.as_ ## ty(std::nothrow); \ + } \ + \ + static format_type& get_fmt(value_type& v) \ + { \ + return v.as_ ## ty ## _fmt(); \ + } \ + static format_type const& get_fmt(const value_type& v) \ + { \ + return v.as_ ## ty ## _fmt(); \ + } \ + \ + static format_type& get_fmt_nothrow(value_type& v) noexcept \ + { \ + return v.as_ ## ty ## _fmt(std::nothrow); \ + } \ + static format_type const& get_fmt_nothrow(const value_type& v) noexcept \ + { \ + return v.as_ ## ty ## _fmt(std::nothrow); \ + } \ + }; + +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(boolean ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(integer ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(floating ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(string ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(offset_datetime) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(local_datetime ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(local_date ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(local_time ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(array ) +TOML11_DETAIL_GENERATE_COMPTIME_GETTER(table ) + +#undef TOML11_DETAIL_GENERATE_COMPTIME_GETTER + +template +void change_region_of_value(basic_value& dst, const basic_value& src) +{ + dst.region_ = std::move(src.region_); + return; +} + +} // namespace detail +} // namespace toml +#endif // TOML11_VALUE_HPP diff --git a/include/toml11/value_t.hpp b/include/toml11/value_t.hpp new file mode 100644 index 0000000..ed0fbe9 --- /dev/null +++ b/include/toml11/value_t.hpp @@ -0,0 +1,10 @@ +#ifndef TOML11_VALUE_T_HPP +#define TOML11_VALUE_T_HPP + +#include "fwd/value_t_fwd.hpp" // IWYU pragma: export + +#if ! defined(TOML11_COMPILE_SOURCES) +#include "impl/value_t_impl.hpp" // IWYU pragma: export +#endif + +#endif // TOML11_VALUE_T_HPP diff --git a/include/toml11/version.hpp b/include/toml11/version.hpp new file mode 100644 index 0000000..24fcd7d --- /dev/null +++ b/include/toml11/version.hpp @@ -0,0 +1,121 @@ +#ifndef TOML11_VERSION_HPP +#define TOML11_VERSION_HPP + +#define TOML11_VERSION_MAJOR 4 +#define TOML11_VERSION_MINOR 3 +#define TOML11_VERSION_PATCH 0 + +#ifndef __cplusplus +# error "__cplusplus is not defined" +#endif + +// Since MSVC does not define `__cplusplus` correctly unless you pass +// `/Zc:__cplusplus` when compiling, the workaround macros are added. +// +// The value of `__cplusplus` macro is defined in the C++ standard spec, but +// MSVC ignores the value, maybe because of backward compatibility. Instead, +// MSVC defines _MSVC_LANG that has the same value as __cplusplus defined in +// the C++ standard. So we check if _MSVC_LANG is defined before using `__cplusplus`. +// +// FYI: https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170 +// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170 +// + +#if defined(_MSVC_LANG) && defined(_MSC_VER) && 190024210 <= _MSC_FULL_VER +# define TOML11_CPLUSPLUS_STANDARD_VERSION _MSVC_LANG +#else +# define TOML11_CPLUSPLUS_STANDARD_VERSION __cplusplus +#endif + +#if TOML11_CPLUSPLUS_STANDARD_VERSION < 201103L +# error "toml11 requires C++11 or later." +#endif + +#if ! defined(__has_include) +# define __has_include(x) 0 +#endif + +#if ! defined(__has_cpp_attribute) +# define __has_cpp_attribute(x) 0 +#endif + +#if ! defined(__has_builtin) +# define __has_builtin(x) 0 +#endif + +// hard to remember + +#ifndef TOML11_CXX14_VALUE +#define TOML11_CXX14_VALUE 201402L +#endif//TOML11_CXX14_VALUE + +#ifndef TOML11_CXX17_VALUE +#define TOML11_CXX17_VALUE 201703L +#endif//TOML11_CXX17_VALUE + +#ifndef TOML11_CXX20_VALUE +#define TOML11_CXX20_VALUE 202002L +#endif//TOML11_CXX20_VALUE + +#if defined(__cpp_char8_t) +# if __cpp_char8_t >= 201811L +# define TOML11_HAS_CHAR8_T 1 +# endif +#endif + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if __has_include() +# define TOML11_HAS_STRING_VIEW 1 +# endif +#endif + +#ifndef TOML11_DISABLE_STD_FILESYSTEM +# if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if __has_include() +# define TOML11_HAS_FILESYSTEM 1 +# endif +# endif +#endif + +#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE +# if __has_include() +# define TOML11_HAS_OPTIONAL 1 +# endif +#endif + +#if defined(TOML11_COMPILE_SOURCES) +# define TOML11_INLINE +#else +# define TOML11_INLINE inline +#endif + +namespace toml +{ + +inline const char* license_notice() noexcept +{ + return R"(The MIT License (MIT) + +Copyright (c) 2017-now Toru Niina + +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.)"; +} + +} // toml +#endif // TOML11_VERSION_HPP diff --git a/include/toml11/visit.hpp b/include/toml11/visit.hpp new file mode 100644 index 0000000..715b403 --- /dev/null +++ b/include/toml11/visit.hpp @@ -0,0 +1,136 @@ +#ifndef TOML11_VISIT_HPP +#define TOML11_VISIT_HPP + +#include "exception.hpp" +#include "traits.hpp" +#include "value.hpp" + +namespace toml +{ + +namespace detail +{ + +template +using visit_result_t = decltype(std::declval()(std::declval().as_boolean() ...)); + +template +struct front_binder +{ + template + auto operator()(Args&& ... args) -> decltype(std::declval()(std::declval(), std::forward(args)...)) + { + return func(std::move(front), std::forward(args)...); + } + F func; + T front; +}; + +template +front_binder, cxx::remove_cvref_t> +bind_front(F&& f, T&& t) +{ + return front_binder, cxx::remove_cvref_t>{ + std::forward(f), std::forward(t) + }; +} + +template +visit_result_t&, Args...> +visit_impl(Visitor&& visitor, const basic_value& v, Args&& ... args); + +template +visit_result_t&, Args...> +visit_impl(Visitor&& visitor, basic_value& v, Args&& ... args); + +template +visit_result_t, Args...> +visit_impl(Visitor&& visitor, basic_value&& v, Args&& ... args); + + +template +visit_result_t visit_impl(Visitor&& visitor) +{ + return visitor(); +} + +template +visit_result_t&, Args...> +visit_impl(Visitor&& visitor, basic_value& v, Args&& ... args) +{ + switch(v.type()) + { + case value_t::boolean : {return visit_impl(bind_front(visitor, std::ref(v.as_boolean ())), std::forward(args)...);} + case value_t::integer : {return visit_impl(bind_front(visitor, std::ref(v.as_integer ())), std::forward(args)...);} + case value_t::floating : {return visit_impl(bind_front(visitor, std::ref(v.as_floating ())), std::forward(args)...);} + case value_t::string : {return visit_impl(bind_front(visitor, std::ref(v.as_string ())), std::forward(args)...);} + case value_t::offset_datetime: {return visit_impl(bind_front(visitor, std::ref(v.as_offset_datetime())), std::forward(args)...);} + case value_t::local_datetime : {return visit_impl(bind_front(visitor, std::ref(v.as_local_datetime ())), std::forward(args)...);} + case value_t::local_date : {return visit_impl(bind_front(visitor, std::ref(v.as_local_date ())), std::forward(args)...);} + case value_t::local_time : {return visit_impl(bind_front(visitor, std::ref(v.as_local_time ())), std::forward(args)...);} + case value_t::array : {return visit_impl(bind_front(visitor, std::ref(v.as_array ())), std::forward(args)...);} + case value_t::table : {return visit_impl(bind_front(visitor, std::ref(v.as_table ())), std::forward(args)...);} + case value_t::empty : break; + default: break; + } + throw type_error(format_error("[error] toml::visit: toml::basic_value " + "does not have any valid type.", v.location(), "here"), v.location()); +} + +template +visit_result_t&, Args...> +visit_impl(Visitor&& visitor, const basic_value& v, Args&& ... args) +{ + switch(v.type()) + { + case value_t::boolean : {return visit_impl(bind_front(visitor, std::cref(v.as_boolean ())), std::forward(args)...);} + case value_t::integer : {return visit_impl(bind_front(visitor, std::cref(v.as_integer ())), std::forward(args)...);} + case value_t::floating : {return visit_impl(bind_front(visitor, std::cref(v.as_floating ())), std::forward(args)...);} + case value_t::string : {return visit_impl(bind_front(visitor, std::cref(v.as_string ())), std::forward(args)...);} + case value_t::offset_datetime: {return visit_impl(bind_front(visitor, std::cref(v.as_offset_datetime())), std::forward(args)...);} + case value_t::local_datetime : {return visit_impl(bind_front(visitor, std::cref(v.as_local_datetime ())), std::forward(args)...);} + case value_t::local_date : {return visit_impl(bind_front(visitor, std::cref(v.as_local_date ())), std::forward(args)...);} + case value_t::local_time : {return visit_impl(bind_front(visitor, std::cref(v.as_local_time ())), std::forward(args)...);} + case value_t::array : {return visit_impl(bind_front(visitor, std::cref(v.as_array ())), std::forward(args)...);} + case value_t::table : {return visit_impl(bind_front(visitor, std::cref(v.as_table ())), std::forward(args)...);} + case value_t::empty : break; + default: break; + } + throw type_error(format_error("[error] toml::visit: toml::basic_value " + "does not have any valid type.", v.location(), "here"), v.location()); +} + +template +visit_result_t, Args...> +visit_impl(Visitor&& visitor, basic_value&& v, Args&& ... args) +{ + switch(v.type()) + { + case value_t::boolean : {return visit_impl(bind_front(visitor, std::move(v.as_boolean ())), std::forward(args)...);} + case value_t::integer : {return visit_impl(bind_front(visitor, std::move(v.as_integer ())), std::forward(args)...);} + case value_t::floating : {return visit_impl(bind_front(visitor, std::move(v.as_floating ())), std::forward(args)...);} + case value_t::string : {return visit_impl(bind_front(visitor, std::move(v.as_string ())), std::forward(args)...);} + case value_t::offset_datetime: {return visit_impl(bind_front(visitor, std::move(v.as_offset_datetime())), std::forward(args)...);} + case value_t::local_datetime : {return visit_impl(bind_front(visitor, std::move(v.as_local_datetime ())), std::forward(args)...);} + case value_t::local_date : {return visit_impl(bind_front(visitor, std::move(v.as_local_date ())), std::forward(args)...);} + case value_t::local_time : {return visit_impl(bind_front(visitor, std::move(v.as_local_time ())), std::forward(args)...);} + case value_t::array : {return visit_impl(bind_front(visitor, std::move(v.as_array ())), std::forward(args)...);} + case value_t::table : {return visit_impl(bind_front(visitor, std::move(v.as_table ())), std::forward(args)...);} + case value_t::empty : break; + default: break; + } + throw type_error(format_error("[error] toml::visit: toml::basic_value " + "does not have any valid type.", v.location(), "here"), v.location()); +} + +} // detail + +template +detail::visit_result_t +visit(Visitor&& visitor, Args&& ... args) +{ + return detail::visit_impl(std::forward(visitor), std::forward(args)...); +} + +} // toml +#endif // TOML11_VISIT_HPP From d8e1c2f29c3a33b2808388fde69c6280abb68b09 Mon Sep 17 00:00:00 2001 From: Sergei Date: Mon, 27 Jan 2025 00:10:43 +0200 Subject: [PATCH 078/156] implemented config --- README.md | 4 +- init.d/ocvsmd.toml | 16 +++ src/common/ipc/client_router.cpp | 3 +- src/common/ipc/server_router.cpp | 3 +- src/daemon/engine/CMakeLists.txt | 2 +- src/daemon/engine/config.cpp | 132 ++++++++++++++++++ src/daemon/engine/config.hpp | 36 ++++- .../engine/cyphal/udp_transport_bag.hpp | 18 ++- .../engine/{application.cpp => engine.cpp} | 28 ++-- .../engine/{application.hpp => engine.hpp} | 20 +-- src/daemon/main.cpp | 36 +++-- 11 files changed, 258 insertions(+), 40 deletions(-) create mode 100644 init.d/ocvsmd.toml rename src/daemon/engine/{application.cpp => engine.cpp} (86%) rename src/daemon/engine/{application.hpp => engine.hpp} (78%) diff --git a/README.md b/README.md index 8a4d0ad..22e541d 100644 --- a/README.md +++ b/README.md @@ -33,10 +33,12 @@ Then one of the two presets depending on your system: sudo cp build/bin/Release/ocvsmd /usr/local/bin/ocvsmd sudo cp build/bin/Release/ocvsmd-cli /usr/local/bin/ocvsmd-cli ``` -- Installing the Init Script: +- Installing the Init Script and Config file: ```bash sudo cp init.d/ocvsmd /etc/init.d/ocvsmd sudo chmod +x /etc/init.d/ocvsmd + sudo mkdir -p /etc/ocvsmd + sudo cp init.d/ocvsmd.toml /etc/ocvsmd/ocvsmd.toml ``` - Enabling at Startup if needed (on SysV-based systems): diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml new file mode 100644 index 0000000..facb883 --- /dev/null +++ b/init.d/ocvsmd.toml @@ -0,0 +1,16 @@ +[cyphal.node] +# The ID assigned to OCVSMD Cyphal node. Must be unique in the Cyphal network. +id = 0 +# The Unique-ID (16 bytes) of the Cyphal node. Automatically generated on the first run. +unique_id = [] + +[cyphal.udp] +iface = '127.0.0.1' + +# Metadata of the configuration file. +[__meta__] +# The version of the configuration file. +version = 1 +# The last modified date of the configuration file. +last_modified = 1970-01-01T00:00:00Z + diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index 39945fc..dd155c2 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -424,8 +424,7 @@ class ClientRouterImpl final : public ClientRouter } // namespace -CETL_NODISCARD ClientRouter::Ptr ClientRouter::make(cetl::pmr::memory_resource& memory, - pipe::ClientPipe::Ptr client_pipe) +ClientRouter::Ptr ClientRouter::make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe) { return std::make_shared(memory, std::move(client_pipe)); } diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 4c05e9c..401280b 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -457,8 +457,7 @@ class ServerRouterImpl final : public ServerRouter } // namespace -CETL_NODISCARD ServerRouter::Ptr ServerRouter::make(cetl::pmr::memory_resource& memory, - pipe::ServerPipe::Ptr server_pipe) +ServerRouter::Ptr ServerRouter::make(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe) { return std::make_unique(memory, std::move(server_pipe)); } diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index bc12bd5..0f99b17 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -32,8 +32,8 @@ target_include_directories(udpard ) add_library(ocvsmd_engine - application.cpp config.cpp + engine.cpp platform/udp/udp.c svc/node/exec_cmd_service.cpp ) diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index 9eaebc6..b3f0609 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -2,3 +2,135 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT // + +#include "config.hpp" + +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace +{ + +struct Default +{ + struct Cyphal + { + struct Udp + { + constexpr static auto Iface = "127.0.0.1"; + }; + }; + +}; // Default + +class ConfigImpl final : public Config +{ +public: + using TomlConf = toml::ordered_type_config; + using TomlValue = toml::basic_value; + + ConfigImpl(std::string file_path, TomlValue&& root) + : file_path_{std::move(file_path)} + , root_{std::move(root)} + , is_dirty_{false} + { + } + + // Config + + void save() override + { + if (is_dirty_) + { + try + { + const auto cfg_str = format(root_); + std::ofstream file{file_path_, std::ios_base::out | std::ios_base::binary}; + file << cfg_str; + + is_dirty_ = false; + + } catch (const std::exception& ex) + { + spdlog::error("Failed to save config. Error: {}", file_path_, ex.what()); + } + } + } + + auto getCyphalNodeId() const -> cetl::optional override + { + return findImpl("cyphal", "node", "id"); + } + + auto getCyphalNodeUniqueId() const -> cetl::optional override + { + return findImpl("cyphal", "node", "unique_id"); + } + + void setCyphalNodeUniqueId(const CyphalNodeUniqueId& unique_id) override + { + auto& toml_unique_id = root_["cyphal"]["node"]["unique_id"]; + toml_unique_id = unique_id; + for (auto& item : toml_unique_id.as_array()) + { + item.as_integer_fmt().fmt = toml::integer_format::hex; + } + is_dirty_ = true; + } + + auto getCyphalUdpIface() const -> std::string override + { + return find_or(root_, "cyphal", "udp", "iface", Default::Cyphal::Udp::Iface); + } + +private: + template + cetl::optional findImpl(Keys&&... keys) const + { + try + { + return toml::find(root_, std::forward(keys)...); + + } catch (...) + { + return cetl::nullopt; + } + } + + std::string file_path_; + TomlValue root_; + bool is_dirty_; + +}; // ConfigImpl + +} // namespace + +Config::Ptr Config::make(std::string file_path) +{ + auto maybe_root = toml::try_parse(file_path); + if (maybe_root.is_err()) + { + const auto err_str = format_error(maybe_root.unwrap_err().at(0)); + spdlog::error("Failed to load config. Error:\n{}", err_str); + return nullptr; + } + return std::make_shared(std::move(file_path), std::move(maybe_root.unwrap())); +} + +} // namespace engine +} // namespace daemon +} // namespace ocvsmd \ No newline at end of file diff --git a/src/daemon/engine/config.hpp b/src/daemon/engine/config.hpp index fd77391..7fa29f9 100644 --- a/src/daemon/engine/config.hpp +++ b/src/daemon/engine/config.hpp @@ -6,6 +6,14 @@ #ifndef OCVSMD_DAEMON_ENGINE_CONFIG_HPP_INCLUDED #define OCVSMD_DAEMON_ENGINE_CONFIG_HPP_INCLUDED +#include +#include + +#include +#include +#include +#include + namespace ocvsmd { namespace daemon @@ -14,7 +22,33 @@ namespace engine { class Config -{}; // Config +{ +public: + using Ptr = std::shared_ptr; + + using CyphalNodeId = std::uint16_t; + using CyphalNodeUniqueId = std::array; // NOLINT(*-magic-numbers) + + CETL_NODISCARD static Ptr make(std::string file_path); + + Config(const Config&) = delete; + Config(Config&&) noexcept = delete; + Config& operator=(const Config&) = delete; + Config& operator=(Config&&) noexcept = delete; + + virtual ~Config() = default; + + virtual void save() = 0; + + CETL_NODISCARD virtual auto getCyphalNodeId() const -> cetl::optional = 0; + CETL_NODISCARD virtual auto getCyphalNodeUniqueId() const -> cetl::optional = 0; + virtual void setCyphalNodeUniqueId(const CyphalNodeUniqueId& unique_id) = 0; + CETL_NODISCARD virtual auto getCyphalUdpIface() const -> std::string = 0; + +protected: + Config() = default; + +}; // Config } // namespace engine } // namespace daemon diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index 7ea41bc..5739801 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -6,8 +6,10 @@ #ifndef OCVSMD_DAEMON_ENGINE_CYPHAL_UDP_TRANSPORT_BAG_HPP_INCLUDED #define OCVSMD_DAEMON_ENGINE_CYPHAL_UDP_TRANSPORT_BAG_HPP_INCLUDED +#include "config.hpp" #include "platform/udp/udp_media.hpp" +#include #include #include #include @@ -18,6 +20,7 @@ #include #include +#include namespace ocvsmd { @@ -39,13 +42,11 @@ struct UdpTransportBag final { } - libcyphal::transport::udp::IUdpTransport* create() + libcyphal::transport::udp::IUdpTransport* create(const Config::Ptr& config) { - // TODO: Make it configurable. - const libcyphal::transport::NodeId node_id{7}; - const auto* const udp_iface = "127.0.0.1"; + CETL_DEBUG_ASSERT(config, ""); - media_collection_.parse(udp_iface); + media_collection_.parse(config->getCyphalUdpIface()); auto maybe_udp_transport = makeTransport({memory_}, executor_, media_collection_.span(), TxQueueCapacity); if (const auto* failure = cetl::get_if(&maybe_udp_transport)) { @@ -55,8 +56,11 @@ struct UdpTransportBag final transport_ = cetl::get>( // std::move(maybe_udp_transport)); - // TODO: Uncomment! - transport_->setLocalNodeId(node_id); + if (const auto node_id = config->getCyphalNodeId()) + { + transport_->setLocalNodeId(node_id.value()); + } + // transport_->setTransientErrorHandler(platform::CommonHelpers::Udp::transientErrorReporter); return transport_.get(); diff --git a/src/daemon/engine/application.cpp b/src/daemon/engine/engine.cpp similarity index 86% rename from src/daemon/engine/application.cpp rename to src/daemon/engine/engine.cpp index a4de518..a627e0b 100644 --- a/src/daemon/engine/application.cpp +++ b/src/daemon/engine/engine.cpp @@ -3,10 +3,11 @@ // SPDX-License-Identifier: MIT // -#include "application.hpp" +#include "engine.hpp" +#include "config.hpp" #include "ipc/pipe/net_socket_server.hpp" -// #include "ipc/pipe/unix_socket_server.hpp" +#include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" #include "svc/node/exec_cmd_service.hpp" #include "svc/svc_helpers.hpp" @@ -33,11 +34,16 @@ namespace daemon namespace engine { -cetl::optional Application::init() +Engine::Engine(Config::Ptr config) + : config_{std::move(config)} +{ +} + +cetl::optional Engine::init() { // 1. Create the transport layer object. // - auto* const transport_iface = udp_transport_bag_.create(); + auto* const transport_iface = udp_transport_bag_.create(config_); if (transport_iface == nullptr) { return "Failed to create cyphal UDP transport."; @@ -84,7 +90,7 @@ cetl::optional Application::init() return cetl::nullopt; } -void Application::runWhile(const std::function& loop_predicate) +void Engine::runWhile(const std::function& loop_predicate) { using std::chrono_literals::operator""s; @@ -106,11 +112,14 @@ void Application::runWhile(const std::function& loop_predicate) } } -Application::UniqueId Application::getUniqueId() +Engine::UniqueId Engine::getUniqueId() const { - UniqueId out_unique_id = {}; + if (const auto unique_id = config_->getCyphalNodeUniqueId()) + { + return unique_id.value(); + } - // TODO: add storage for the unique ID + UniqueId out_unique_id = {}; std::random_device rd; // Seed for the random number engine std::mt19937 gen{rd()}; // Mersenne Twister engine @@ -123,6 +132,9 @@ Application::UniqueId Application::getUniqueId() b = dis(gen); } + config_->setCyphalNodeUniqueId(out_unique_id); + config_->save(); + return out_unique_id; } diff --git a/src/daemon/engine/application.hpp b/src/daemon/engine/engine.hpp similarity index 78% rename from src/daemon/engine/application.hpp rename to src/daemon/engine/engine.hpp index d2010cb..8552e7d 100644 --- a/src/daemon/engine/application.hpp +++ b/src/daemon/engine/engine.hpp @@ -3,9 +3,10 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_DAEMON_ENGINE_APPLICATION_HPP_INCLUDED -#define OCVSMD_DAEMON_ENGINE_APPLICATION_HPP_INCLUDED +#ifndef OCVSMD_DAEMON_ENGINE_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_HPP_INCLUDED +#include "config.hpp" #include "cyphal/udp_transport_bag.hpp" #include "logging.hpp" #include "ocvsmd/platform/defines.hpp" @@ -17,8 +18,6 @@ #include #include -#include - #include #include @@ -29,17 +28,20 @@ namespace daemon namespace engine { -class Application +class Engine { public: + explicit Engine(Config::Ptr config); + CETL_NODISCARD cetl::optional init(); void runWhile(const std::function& loop_predicate); private: - using UniqueId = uavcan::node::GetInfo::Response_1_0::_traits_::TypeOf::unique_id; + using UniqueId = Config::CyphalNodeUniqueId; - static UniqueId getUniqueId(); + UniqueId getUniqueId() const; + Config::Ptr config_; common::LoggerPtr logger_{common::getLogger("engine")}; ocvsmd::platform::SingleThreadedExecutor executor_; cetl::pmr::memory_resource& memory_{*cetl::pmr::get_default_resource()}; @@ -48,10 +50,10 @@ class Application cetl::optional node_; common::ipc::ServerRouter::Ptr ipc_router_; -}; // Application +}; // Engine } // namespace engine } // namespace daemon } // namespace ocvsmd -#endif // OCVSMD_DAEMON_ENGINE_APPLICATION_HPP_INCLUDED +#endif // OCVSMD_DAEMON_ENGINE_HPP_INCLUDED diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index 6cbbb33..a2abcbe 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -3,7 +3,8 @@ // SPDX-License-Identifier: MIT // -#include "engine/application.hpp" +#include "engine/config.hpp" +#include "engine/engine.hpp" #include "setup_logging.hpp" #include @@ -280,7 +281,7 @@ int daemonize() step_13_drop_privileges(); // `step_14_notify_init_complete(pipe_write_fd);` will be called by the main - // when the application has been successfully initialized. + // when the engine has been successfully initialized. return pipe_write_fd; } @@ -293,12 +294,25 @@ int daemonize() return -1; // Unreachable actually b/c of `::exit` call. } +ocvsmd::daemon::engine::Config::Ptr loadConfig(const int err_fd, const bool is_daemonized) +{ + const std::string cfg_file_nm = "ocvsmd.toml"; + const std::string cfg_file_dir = is_daemonized ? "/etc/ocvsmd/" : "./"; + const auto cfg_file_path = cfg_file_dir + cfg_file_nm; + + if (auto config = ocvsmd::daemon::engine::Config::make(cfg_file_path)) + { + return config; + } + + writeString(err_fd, "Failed to load configuration file."); + ::exit(EXIT_FAILURE); +} + } // namespace int main(const int argc, const char** const argv) { - using ocvsmd::daemon::engine::Application; - bool should_daemonize = true; for (int i = 1; i < argc; ++i) { @@ -328,13 +342,15 @@ int main(const int argc, const char** const argv) { try { - Application application; - if (const auto failure_str = application.init()) + const auto config = loadConfig(pipe_write_fd, should_daemonize); + + ocvsmd::daemon::engine::Engine engine{config}; + if (const auto failure_str = engine.init()) { - spdlog::critical("Failed to init application: {}", failure_str.value()); + spdlog::critical("Failed to init engine: {}", failure_str.value()); // Report the failure to the parent process (if daemonized; otherwise goes to stderr). - writeString(pipe_write_fd, "Failed to init application: "); + writeString(pipe_write_fd, "Failed to init engine: "); writeString(pipe_write_fd, failure_str.value().c_str()); ::exit(EXIT_FAILURE); } @@ -343,7 +359,9 @@ int main(const int argc, const char** const argv) step_14_notify_init_complete(pipe_write_fd); } - application.runWhile([] { return g_running == 1; }); + engine.runWhile([] { return g_running == 1; }); + + config->save(); } catch (const std::exception& ex) { From 69e7e547d83ab2dc5468ca46758ef739f0afe3c3 Mon Sep 17 00:00:00 2001 From: Sergei Date: Mon, 27 Jan 2025 10:36:13 +0200 Subject: [PATCH 079/156] extended config file --- init.d/ocvsmd.toml | 28 +++++++++++++++++++++++----- src/daemon/engine/config.cpp | 4 ++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index facb883..6ab3951 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -1,11 +1,29 @@ -[cyphal.node] -# The ID assigned to OCVSMD Cyphal node. Must be unique in the Cyphal network. -id = 0 +# Cyphal application layer settings. +[cyphal.application] +# The ID assigned to OCVSMD Cyphal node. +# Must be unique in the Cyphal network. +# Supported values: 0-65534 (for UDP) +node_id = 0 # The Unique-ID (16 bytes) of the Cyphal node. Automatically generated on the first run. unique_id = [] -[cyphal.udp] -iface = '127.0.0.1' +# Cyphal transport layer settings. +[cyphal.transport] +# List of redundant interfaces for the Cyphal network. +# Currently, only one interface is supported. +# Supported formats: +# - 'udp:' +interfaces = ['udp:127.0.0.1'] + +# IPC server settings. +[ipc] +# Connection strings for the IPC server. +# Currently, only one connection is supported. +# Supported formats: +# - 'tcp://:' +# - 'unix:' +# - 'unix-abstract:' (linux only) +connections = ['unix-abstract:org.opencyphal.ocvsmd.ipc'] # Metadata of the configuration file. [__meta__] diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index b3f0609..fc107fc 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -10,6 +10,8 @@ #include #include +#include +#include #include #include #include @@ -58,6 +60,8 @@ class ConfigImpl final : public Config { try { + root_["__meta__"]["last_modified"] = std::chrono::system_clock::now(); + const auto cfg_str = format(root_); std::ofstream file{file_path_, std::ios_base::out | std::ios_base::binary}; file << cfg_str; From 6b215ad61f43574a24b61d967e27b824107c41bc Mon Sep 17 00:00:00 2001 From: Sergei Date: Mon, 27 Jan 2025 23:29:28 +0200 Subject: [PATCH 080/156] `[cyphal.application]` config section --- src/daemon/engine/config.cpp | 14 ++++++++------ src/daemon/engine/config.hpp | 13 ++++++++----- src/daemon/engine/cyphal/udp_transport_bag.hpp | 3 +-- src/daemon/engine/engine.cpp | 4 ++-- src/daemon/engine/engine.hpp | 2 +- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index fc107fc..907fb0a 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -75,24 +75,26 @@ class ConfigImpl final : public Config } } - auto getCyphalNodeId() const -> cetl::optional override + auto getCyphalAppNodeId() const -> cetl::optional override { - return findImpl("cyphal", "node", "id"); + return findImpl("cyphal", "application", "node_id"); } - auto getCyphalNodeUniqueId() const -> cetl::optional override + auto getCyphalAppUniqueId() const -> cetl::optional override { - return findImpl("cyphal", "node", "unique_id"); + return findImpl("cyphal", "application", "unique_id"); } - void setCyphalNodeUniqueId(const CyphalNodeUniqueId& unique_id) override + void setCyphalAppUniqueId(const CyphalApp::UniqueId& unique_id) override { - auto& toml_unique_id = root_["cyphal"]["node"]["unique_id"]; + auto& toml_unique_id = root_["cyphal"]["application"]["unique_id"]; toml_unique_id = unique_id; for (auto& item : toml_unique_id.as_array()) { item.as_integer_fmt().fmt = toml::integer_format::hex; } + toml_unique_id.as_array_fmt().fmt = toml::array_format::oneline; + is_dirty_ = true; } diff --git a/src/daemon/engine/config.hpp b/src/daemon/engine/config.hpp index 7fa29f9..812ac05 100644 --- a/src/daemon/engine/config.hpp +++ b/src/daemon/engine/config.hpp @@ -26,8 +26,11 @@ class Config public: using Ptr = std::shared_ptr; - using CyphalNodeId = std::uint16_t; - using CyphalNodeUniqueId = std::array; // NOLINT(*-magic-numbers) + struct CyphalApp + { + using NodeId = std::uint16_t; + using UniqueId = std::array; // NOLINT(*-magic-numbers) + }; CETL_NODISCARD static Ptr make(std::string file_path); @@ -40,9 +43,9 @@ class Config virtual void save() = 0; - CETL_NODISCARD virtual auto getCyphalNodeId() const -> cetl::optional = 0; - CETL_NODISCARD virtual auto getCyphalNodeUniqueId() const -> cetl::optional = 0; - virtual void setCyphalNodeUniqueId(const CyphalNodeUniqueId& unique_id) = 0; + CETL_NODISCARD virtual auto getCyphalAppNodeId() const -> cetl::optional = 0; + CETL_NODISCARD virtual auto getCyphalAppUniqueId() const -> cetl::optional = 0; + virtual void setCyphalAppUniqueId(const CyphalApp::UniqueId& unique_id) = 0; CETL_NODISCARD virtual auto getCyphalUdpIface() const -> std::string = 0; protected: diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index 5739801..ba153df 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -56,7 +55,7 @@ struct UdpTransportBag final transport_ = cetl::get>( // std::move(maybe_udp_transport)); - if (const auto node_id = config->getCyphalNodeId()) + if (const auto node_id = config->getCyphalAppNodeId()) { transport_->setLocalNodeId(node_id.value()); } diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index a627e0b..e50bdc2 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -114,7 +114,7 @@ void Engine::runWhile(const std::function& loop_predicate) Engine::UniqueId Engine::getUniqueId() const { - if (const auto unique_id = config_->getCyphalNodeUniqueId()) + if (const auto unique_id = config_->getCyphalAppUniqueId()) { return unique_id.value(); } @@ -132,7 +132,7 @@ Engine::UniqueId Engine::getUniqueId() const b = dis(gen); } - config_->setCyphalNodeUniqueId(out_unique_id); + config_->setCyphalAppUniqueId(out_unique_id); config_->save(); return out_unique_id; diff --git a/src/daemon/engine/engine.hpp b/src/daemon/engine/engine.hpp index 8552e7d..6ced556 100644 --- a/src/daemon/engine/engine.hpp +++ b/src/daemon/engine/engine.hpp @@ -37,7 +37,7 @@ class Engine void runWhile(const std::function& loop_predicate); private: - using UniqueId = Config::CyphalNodeUniqueId; + using UniqueId = Config::CyphalApp::UniqueId; UniqueId getUniqueId() const; From 4d0889b60140844924229b7dc95e4ada3069034f Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 28 Jan 2025 01:20:55 +0200 Subject: [PATCH 081/156] `[logging]` config section --- init.d/ocvsmd.toml | 10 +++++++++ src/daemon/engine/config.cpp | 25 ++++++++++++++------- src/daemon/engine/config.hpp | 7 +++++- src/daemon/main.cpp | 42 +++++++++++++++++++++++++++--------- src/daemon/setup_logging.hpp | 25 ++++++++++++++++++--- 5 files changed, 87 insertions(+), 22 deletions(-) diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index 6ab3951..72f23a6 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -25,6 +25,16 @@ interfaces = ['udp:127.0.0.1'] # - 'unix-abstract:' (linux only) connections = ['unix-abstract:org.opencyphal.ocvsmd.ipc'] +# Logging related settings. +# See also README documentation for more details. +[logging] +# The path to the log file. +file = '/etc/var/ocvsmd.log' +# Supported log levels: 'trace', 'debug', 'info', 'warning', 'error', 'critical', 'off'. +level = 'info' +# By default, the log file is not immediately flushed to disk (at `off` level). +flush_level = 'off' + # Metadata of the configuration file. [__meta__] # The version of the configuration file. diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index 907fb0a..589348b 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -103,6 +103,21 @@ class ConfigImpl final : public Config return find_or(root_, "cyphal", "udp", "iface", Default::Cyphal::Udp::Iface); } + auto getLoggingFile() const -> cetl::optional override + { + return findImpl("logging", "file"); + } + + auto getLoggingLevel() const -> cetl::optional override + { + return findImpl("logging", "level"); + } + + auto getLoggingFlushLevel() const -> cetl::optional override + { + return findImpl("logging", "flush_level"); + } + private: template cetl::optional findImpl(Keys&&... keys) const @@ -127,14 +142,8 @@ class ConfigImpl final : public Config Config::Ptr Config::make(std::string file_path) { - auto maybe_root = toml::try_parse(file_path); - if (maybe_root.is_err()) - { - const auto err_str = format_error(maybe_root.unwrap_err().at(0)); - spdlog::error("Failed to load config. Error:\n{}", err_str); - return nullptr; - } - return std::make_shared(std::move(file_path), std::move(maybe_root.unwrap())); + auto root = toml::parse(file_path); + return std::make_shared(std::move(file_path), std::move(root)); } } // namespace engine diff --git a/src/daemon/engine/config.hpp b/src/daemon/engine/config.hpp index 812ac05..5d3c4da 100644 --- a/src/daemon/engine/config.hpp +++ b/src/daemon/engine/config.hpp @@ -46,7 +46,12 @@ class Config CETL_NODISCARD virtual auto getCyphalAppNodeId() const -> cetl::optional = 0; CETL_NODISCARD virtual auto getCyphalAppUniqueId() const -> cetl::optional = 0; virtual void setCyphalAppUniqueId(const CyphalApp::UniqueId& unique_id) = 0; - CETL_NODISCARD virtual auto getCyphalUdpIface() const -> std::string = 0; + + CETL_NODISCARD virtual auto getCyphalUdpIface() const -> std::string = 0; + + CETL_NODISCARD virtual auto getLoggingFile() const -> cetl::optional = 0; + CETL_NODISCARD virtual auto getLoggingLevel() const -> cetl::optional = 0; + CETL_NODISCARD virtual auto getLoggingFlushLevel() const -> cetl::optional = 0; protected: Config() = default; diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index a2abcbe..b6e8adf 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -19,6 +19,7 @@ #include #include #include // NOLINT +#include #include #include #include @@ -235,7 +236,7 @@ void step_15_exit_org_process(int& pipe_read_fd) // Call exit() in the original process. The process that invoked the daemon must be able to rely on that this exit() // happens after initialization is complete and all external communication channels are established and accessible. - constexpr std::size_t buf_size = 256; + constexpr std::size_t buf_size = 1024; std::array msg_from_child{}; const auto res = ::read(pipe_read_fd, msg_from_child.data(), msg_from_child.size() - 1); if (res == -1) @@ -244,6 +245,7 @@ void step_15_exit_org_process(int& pipe_read_fd) std::cerr << "Failed to read pipe: " << err_txt << "\n"; ::exit(EXIT_FAILURE); } + msg_from_child[res] = '\0'; // NOLINT if (::strcmp(msg_from_child.data(), s_init_complete) != 0) { @@ -294,18 +296,39 @@ int daemonize() return -1; // Unreachable actually b/c of `::exit` call. } -ocvsmd::daemon::engine::Config::Ptr loadConfig(const int err_fd, const bool is_daemonized) +ocvsmd::daemon::engine::Config::Ptr loadConfig(const int err_fd, + const bool is_daemonized, + const int argc, + const char** const argv) { const std::string cfg_file_nm = "ocvsmd.toml"; const std::string cfg_file_dir = is_daemonized ? "/etc/ocvsmd/" : "./"; - const auto cfg_file_path = cfg_file_dir + cfg_file_nm; - - if (auto config = ocvsmd::daemon::engine::Config::make(cfg_file_path)) + auto cfg_file_path = cfg_file_dir + cfg_file_nm; + // + const std::string config_file_prefix = "CONFIG_FILE="; + for (int i = 1; i < argc; i++) { - return config; + const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + if (arg_str.find(config_file_prefix) == 0) + { + cfg_file_path = arg_str.substr(config_file_prefix.size()); + } } - writeString(err_fd, "Failed to load configuration file."); + try + { + return ocvsmd::daemon::engine::Config::make(cfg_file_path); + + } catch (const std::exception& ex) + { + std::stringstream ss; + ss << "Failed to load configuration file (path='" << cfg_file_path << "').\n" << ex.what(); + writeString(err_fd, ss.str().c_str()); + + } catch (...) + { + writeString(err_fd, "Failed to load configuration file."); + } ::exit(EXIT_FAILURE); } @@ -335,15 +358,14 @@ int main(const int argc, const char** const argv) setupSignalHandlers(); } - setupLogging(pipe_write_fd, should_daemonize, argc, argv); + const auto config = loadConfig(pipe_write_fd, should_daemonize, argc, argv); + setupLogging(pipe_write_fd, should_daemonize, argc, argv, config); spdlog::info("OCVSMD started (ver='{}.{}').", VERSION_MAJOR, VERSION_MINOR); int result = EXIT_SUCCESS; { try { - const auto config = loadConfig(pipe_write_fd, should_daemonize); - ocvsmd::daemon::engine::Engine engine{config}; if (const auto failure_str = engine.init()) { diff --git a/src/daemon/setup_logging.hpp b/src/daemon/setup_logging.hpp index d1a1ffb..a4de006 100644 --- a/src/daemon/setup_logging.hpp +++ b/src/daemon/setup_logging.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_DAEMON_SETUP_LOGGING_HPP_INCLUDED #define OCVSMD_DAEMON_SETUP_LOGGING_HPP_INCLUDED +#include "config.hpp" + #include #include // NOLINT #include @@ -95,7 +97,11 @@ inline bool writeString(const int fd, const char* const str) /// The syslog sink is used for the default logger only (with Info default level), /// while the file sink is used for all loggers (with Debug default level). /// -inline void setupLogging(const int err_fd, const bool is_daemonized, const int argc, const char** const argv) +inline void setupLogging(const int err_fd, + const bool is_daemonized, + const int argc, + const char** const argv, + const ocvsmd::daemon::engine::Config::Ptr& config) { using spdlog::sinks::syslog_sink_st; using spdlog::sinks::rotating_file_sink_st; @@ -108,7 +114,11 @@ inline void setupLogging(const int err_fd, const bool is_daemonized, const int a const std::string log_prefix = "ocvsmd"; const std::string log_file_nm = log_prefix + ".log"; const std::string log_file_dir = is_daemonized ? "/var/log/" : "./"; - const auto log_file_path = log_file_dir + log_file_nm; + auto log_file_path = log_file_dir + log_file_nm; + if (const auto logging_file = config->getLoggingFile()) + { + log_file_path = logging_file.value(); + } // Drop all existing loggers, including the default one, so that we can reconfigure them. spdlog::drop_all(); @@ -132,8 +142,17 @@ inline void setupLogging(const int err_fd, const bool is_daemonized, const int a register_logger(std::make_shared("ipc", file_sink)); register_logger(std::make_shared("engine", file_sink)); - // Accept `SPDLOG_LEVEL` & `SPDLOG_FLUSH_LEVEL` arguments (like `SPDLOG_LEVEL=debug,ipc=trace`). + // Setup log levels from the configuration file. + // Also accept `SPDLOG_LEVEL` & `SPDLOG_FLUSH_LEVEL` arguments if any (like `SPDLOG_LEVEL=debug,ipc=trace`). // + if (const auto logging_level = config->getLoggingLevel()) + { + spdlog::cfg::helpers::load_levels(logging_level.value()); + } + if (const auto logging_flush_level = config->getLoggingFlushLevel()) + { + detail::loadFlushLevels(logging_flush_level.value()); + } spdlog::cfg::load_argv_levels(argc, argv); detail::loadArgvFlushLevels(argc, argv); From d6185d444e5cd0e5a5d98faff7aa4bc5d024b23b Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 28 Jan 2025 02:01:45 +0200 Subject: [PATCH 082/156] `[cyphal.transport]` config section --- init.d/ocvsmd.toml | 4 +++- src/daemon/engine/config.cpp | 5 +++-- src/daemon/engine/config.hpp | 3 ++- src/daemon/engine/cyphal/udp_transport_bag.hpp | 15 +++++++++++++-- src/daemon/engine/platform/udp/udp_media.hpp | 2 +- src/daemon/setup_logging.hpp | 4 ++++ 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index 72f23a6..9bb3747 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -13,7 +13,9 @@ unique_id = [] # Currently, only one interface is supported. # Supported formats: # - 'udp:' -interfaces = ['udp:127.0.0.1'] +interfaces = [ + 'udp:127.0.0.1', +] # IPC server settings. [ipc] diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index 589348b..c1474dd 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace ocvsmd { @@ -98,9 +99,9 @@ class ConfigImpl final : public Config is_dirty_ = true; } - auto getCyphalUdpIface() const -> std::string override + auto getCyphalTransportInterfaces() const -> std::vector override { - return find_or(root_, "cyphal", "udp", "iface", Default::Cyphal::Udp::Iface); + return find_or(root_, "cyphal", "transport", "interfaces", std::vector{}); } auto getLoggingFile() const -> cetl::optional override diff --git a/src/daemon/engine/config.hpp b/src/daemon/engine/config.hpp index 5d3c4da..d6c8a8d 100644 --- a/src/daemon/engine/config.hpp +++ b/src/daemon/engine/config.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace ocvsmd { @@ -47,7 +48,7 @@ class Config CETL_NODISCARD virtual auto getCyphalAppUniqueId() const -> cetl::optional = 0; virtual void setCyphalAppUniqueId(const CyphalApp::UniqueId& unique_id) = 0; - CETL_NODISCARD virtual auto getCyphalUdpIface() const -> std::string = 0; + CETL_NODISCARD virtual auto getCyphalTransportInterfaces() const -> std::vector = 0; CETL_NODISCARD virtual auto getLoggingFile() const -> cetl::optional = 0; CETL_NODISCARD virtual auto getLoggingLevel() const -> cetl::optional = 0; diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index ba153df..7da1d66 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -18,8 +18,8 @@ #include #include +#include #include -#include namespace ocvsmd { @@ -45,7 +45,18 @@ struct UdpTransportBag final { CETL_DEBUG_ASSERT(config, ""); - media_collection_.parse(config->getCyphalUdpIface()); + std::string udp_ifaces; + for (const auto& iface : config->getCyphalTransportInterfaces()) + { + constexpr static std::string udp_prefix = "udp:"; + if (0 == iface.find(udp_prefix)) + { + udp_ifaces += iface.substr(udp_prefix.size()); + udp_ifaces += ","; + } + } + + media_collection_.parse(udp_ifaces); auto maybe_udp_transport = makeTransport({memory_}, executor_, media_collection_.span(), TxQueueCapacity); if (const auto* failure = cetl::get_if(&maybe_udp_transport)) { diff --git a/src/daemon/engine/platform/udp/udp_media.hpp b/src/daemon/engine/platform/udp/udp_media.hpp index 9d09e43..5a011e5 100644 --- a/src/daemon/engine/platform/udp/udp_media.hpp +++ b/src/daemon/engine/platform/udp/udp_media.hpp @@ -111,7 +111,7 @@ struct UdpMediaCollection std::size_t curr = 0; while ((curr != cetl::string_view::npos) && (index < MaxUdpMedia)) { - const auto next = iface_addresses.find(' ', curr); + const auto next = iface_addresses.find(',', curr); const auto iface_address = iface_addresses.substr(curr, next - curr); if (!iface_address.empty()) { diff --git a/src/daemon/setup_logging.hpp b/src/daemon/setup_logging.hpp index a4de006..4c4e2d2 100644 --- a/src/daemon/setup_logging.hpp +++ b/src/daemon/setup_logging.hpp @@ -8,6 +8,8 @@ #include "config.hpp" +#include + #include #include // NOLINT #include @@ -103,6 +105,8 @@ inline void setupLogging(const int err_fd, const char** const argv, const ocvsmd::daemon::engine::Config::Ptr& config) { + CETL_DEBUG_ASSERT(config, ""); + using spdlog::sinks::syslog_sink_st; using spdlog::sinks::rotating_file_sink_st; From 26e7763cae8e91db329b66aa4951df62da947966 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 28 Jan 2025 14:49:02 +0200 Subject: [PATCH 083/156] fix build --- README.md | 14 +++++++------- init.d/ocvsmd.toml | 4 +++- src/cli/main.cpp | 12 ++++++------ src/daemon/engine/cyphal/udp_transport_bag.hpp | 2 +- src/daemon/engine/engine.cpp | 2 +- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 22e541d..6719178 100644 --- a/README.md +++ b/README.md @@ -11,12 +11,12 @@ Then one of the two presets depending on your system: - `Demo-BSD` – BSD based like MacOS. ###### Debug ```bash - cmake --preset OCVSMD-Linux + cmake --preset OCVSMD-Linux && \ cmake --build --preset OCVSMD-Linux-Debug ``` ###### Release ```bash - cmake --preset OCVSMD-Linux + cmake --preset OCVSMD-Linux && \ cmake --build --preset OCVSMD-Linux-Release ``` @@ -25,19 +25,19 @@ Then one of the two presets depending on your system: - Installing the Daemon Binary: ###### Debug ```bash - sudo cp build/bin/Debug/ocvsmd /usr/local/bin/ocvsmd + sudo cp build/bin/Debug/ocvsmd /usr/local/bin/ocvsmd && \ sudo cp build/bin/Debug/ocvsmd-cli /usr/local/bin/ocvsmd-cli ``` ###### Release ```bash - sudo cp build/bin/Release/ocvsmd /usr/local/bin/ocvsmd + sudo cp build/bin/Release/ocvsmd /usr/local/bin/ocvsmd && \ sudo cp build/bin/Release/ocvsmd-cli /usr/local/bin/ocvsmd-cli ``` - Installing the Init Script and Config file: ```bash - sudo cp init.d/ocvsmd /etc/init.d/ocvsmd - sudo chmod +x /etc/init.d/ocvsmd - sudo mkdir -p /etc/ocvsmd + sudo cp init.d/ocvsmd /etc/init.d/ocvsmd && \ + sudo chmod +x /etc/init.d/ocvsmd && \ + sudo mkdir -p /etc/ocvsmd && \ sudo cp init.d/ocvsmd.toml /etc/ocvsmd/ocvsmd.toml ``` diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index 9bb3747..ed24b61 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -25,7 +25,9 @@ interfaces = [ # - 'tcp://:' # - 'unix:' # - 'unix-abstract:' (linux only) -connections = ['unix-abstract:org.opencyphal.ocvsmd.ipc'] +connections = [ + 'unix-abstract:org.opencyphal.ocvsmd.ipc', +] # Logging related settings. # See also README documentation for more details. diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 3599925..8f0c6e4 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -23,6 +23,7 @@ #include #include // NOLINT #include +#include namespace { @@ -45,8 +46,7 @@ void signalHandler(const int sig) void setupSignalHandlers() { - struct sigaction sigbreak - {}; + struct sigaction sigbreak{}; sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); @@ -84,11 +84,11 @@ int main(const int argc, const char** const argv) auto node_cmd_client = daemon->getNodeCommandClient(); - constexpr auto cmd_id = Command::NodeRequest::COMMAND_RESTART; - constexpr std::array node_ids = {42, 43, 44}; - const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; - auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); + constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; + const std::vector node_ids = {42, 143, 44}; + const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; + auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); if (const auto* const err = cetl::get_if(&cmd_result)) diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index 7da1d66..3b235f1 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -48,7 +48,7 @@ struct UdpTransportBag final std::string udp_ifaces; for (const auto& iface : config->getCyphalTransportInterfaces()) { - constexpr static std::string udp_prefix = "udp:"; + const static std::string udp_prefix = "udp:"; if (0 == iface.find(udp_prefix)) { udp_ifaces += iface.substr(udp_prefix.size()); diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index e50bdc2..35510c5 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -7,7 +7,7 @@ #include "config.hpp" #include "ipc/pipe/net_socket_server.hpp" -#include "ipc/pipe/unix_socket_server.hpp" +// #include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" #include "svc/node/exec_cmd_service.hpp" #include "svc/svc_helpers.hpp" From 77181392be2f6c77801756db5ca09d7506c8f757 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 28 Jan 2025 14:52:03 +0200 Subject: [PATCH 084/156] fix style --- src/cli/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 8f0c6e4..958f6e1 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -46,7 +46,8 @@ void signalHandler(const int sig) void setupSignalHandlers() { - struct sigaction sigbreak{}; + struct sigaction sigbreak + {}; sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); From 383978cbf2012ad598d28939b682a7d4dd52eaee Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 28 Jan 2025 18:20:38 +0200 Subject: [PATCH 085/156] fix build --- src/cli/main.cpp | 2 +- src/daemon/engine/config.cpp | 2 +- submodules/libcyphal | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 958f6e1..38b9046 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -89,7 +89,7 @@ int main(const int argc, const char** const argv) const std::vector node_ids = {42, 143, 44}; const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; - auto sender = node_cmd_client->sendCommand(node_ids, node_request, 1s); + auto sender = node_cmd_client->sendCommand({node_ids.data(), node_ids.size()}, node_request, 1s); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); if (const auto* const err = cetl::get_if(&cmd_result)) diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index c1474dd..f2f6892 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -125,7 +125,7 @@ class ConfigImpl final : public Config { try { - return toml::find(root_, std::forward(keys)...); + return cetl::make_optional(toml::find(root_, std::forward(keys)...)); } catch (...) { diff --git a/submodules/libcyphal b/submodules/libcyphal index ff44f7a..80fe743 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit ff44f7aa70f2def5f51263dfe978427fdc7da09b +Subproject commit 80fe743df06ba091ed6f43191c5a4c416d1b29c3 From a7eded1cefb29738491e6efa124ee39d8328e85a Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 28 Jan 2025 18:42:45 +0200 Subject: [PATCH 086/156] fix build --- src/cli/main.cpp | 1 - src/common/common_helpers.hpp | 19 ++++++++++--------- src/daemon/main.cpp | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 38b9046..ea73d4c 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -14,7 +14,6 @@ #include -#include #include #include #include diff --git a/src/common/common_helpers.hpp b/src/common/common_helpers.hpp index ea13d9d..1488a35 100644 --- a/src/common/common_helpers.hpp +++ b/src/common/common_helpers.hpp @@ -16,26 +16,27 @@ namespace ocvsmd namespace common { -template +/// @brief Wraps the given action into a try/catch block, and performs it without throwing the given exception type. +/// +/// @return `true` if the action was performed successfully, `false` if an exception was thrown. +/// Always `true` if exceptions are disabled. +/// +template bool performWithoutThrowing(Action&& action) noexcept { #if defined(__cpp_exceptions) try -#endif { +#endif std::forward(action)(); return true; - } + #if defined(__cpp_exceptions) - catch (const std::exception& ex) + } catch (const Exception& ex) { spdlog::critical("Unexpected C++ exception is caught: {}", ex.what()); - - } catch (...) - { - spdlog::critical("Unexpected unknown exception is caught!"); + return false; } - return false; #endif } diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index b6e8adf..ba63b5c 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -308,8 +308,8 @@ ocvsmd::daemon::engine::Config::Ptr loadConfig(const int err_fd, const std::string config_file_prefix = "CONFIG_FILE="; for (int i = 1; i < argc; i++) { - const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) - if (arg_str.find(config_file_prefix) == 0) + const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + if (arg_str.find(config_file_prefix) == 0) // NOLINT(modernize-use-starts-ends-with) { cfg_file_path = arg_str.substr(config_file_prefix.size()); } From e8d4558b49121f72d2ae5b3233dd3a25679db6d7 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 28 Jan 2025 20:30:05 +0200 Subject: [PATCH 087/156] latest libcyphal --- .gitmodules | 1 - submodules/libcyphal | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index c07468c..0b13fe0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,6 @@ [submodule "submodules/libcyphal"] path = submodules/libcyphal url = https://github.com/OpenCyphal-Garage/libcyphal.git - branch = sshirokov/412_rpc_clients [submodule "submodules/libudpard"] path = submodules/libudpard url = https://github.com/OpenCyphal/libudpard diff --git a/submodules/libcyphal b/submodules/libcyphal index 80fe743..1d3b038 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit 80fe743df06ba091ed6f43191c5a4c416d1b29c3 +Subproject commit 1d3b0384e009784d981f1072d990df02c9679264 From 0a00cc5ed5d8f25a4b4432547c4cc9671d1ac9f1 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 30 Jan 2025 12:47:32 +0200 Subject: [PATCH 088/156] use new TransferIdMap feature of libcyphal --- .gitmodules | 1 + src/cli/main.cpp | 2 +- src/daemon/engine/engine.cpp | 1 + src/daemon/engine/engine.hpp | 25 +++++++++++++++++++++++++ submodules/libcyphal | 2 +- 5 files changed, 29 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 0b13fe0..299c292 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,7 @@ [submodule "submodules/libcyphal"] path = submodules/libcyphal url = https://github.com/OpenCyphal-Garage/libcyphal.git + branch = sshirokov/416_tid [submodule "submodules/libudpard"] path = submodules/libudpard url = https://github.com/OpenCyphal/libudpard diff --git a/src/cli/main.cpp b/src/cli/main.cpp index ea73d4c..95ce7f7 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -85,7 +85,7 @@ int main(const int argc, const char** const argv) auto node_cmd_client = daemon->getNodeCommandClient(); constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; - const std::vector node_ids = {42, 143, 44}; + const std::vector node_ids = {42, 43, 44}; const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; auto sender = node_cmd_client->sendCommand({node_ids.data(), node_ids.size()}, node_request, 1s); diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 35510c5..4971196 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -52,6 +52,7 @@ cetl::optional Engine::init() // 2. Create the presentation layer object. // presentation_.emplace(memory_, executor_, *transport_iface); + presentation_->setTransferIdMap(&transfer_id_map_); // 3. Create the node object with name. // diff --git a/src/daemon/engine/engine.hpp b/src/daemon/engine/engine.hpp index 6ced556..d8b4681 100644 --- a/src/daemon/engine/engine.hpp +++ b/src/daemon/engine/engine.hpp @@ -17,9 +17,12 @@ #include #include #include +#include +#include #include #include +#include namespace ocvsmd { @@ -39,6 +42,27 @@ class Engine private: using UniqueId = Config::CyphalApp::UniqueId; + class TransferIdMap final : public libcyphal::transport::ITransferIdMap + { + using TransferId = libcyphal::transport::TransferId; + + // ITransferIdMap + + TransferId getIdFor(const SessionSpec& session_spec) const noexcept override + { + const auto it = session_spec_to_transfer_id_.find(session_spec); + return it != session_spec_to_transfer_id_.end() ? it->second : 0; + } + + void setIdFor(const SessionSpec& session_spec, const TransferId transfer_id) noexcept override + { + session_spec_to_transfer_id_[session_spec] = transfer_id; + } + + std::unordered_map session_spec_to_transfer_id_; + + }; // TransferIdMap + UniqueId getUniqueId() const; Config::Ptr config_; @@ -46,6 +70,7 @@ class Engine ocvsmd::platform::SingleThreadedExecutor executor_; cetl::pmr::memory_resource& memory_{*cetl::pmr::get_default_resource()}; cyphal::UdpTransportBag udp_transport_bag_{memory_, executor_}; + TransferIdMap transfer_id_map_; cetl::optional presentation_; cetl::optional node_; common::ipc::ServerRouter::Ptr ipc_router_; diff --git a/submodules/libcyphal b/submodules/libcyphal index 1d3b038..1e226b9 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit 1d3b0384e009784d981f1072d990df02c9679264 +Subproject commit 1e226b9b336ae082d228d21d8dc42a16b7cf2825 From e340382f6cf0fccbf1c3d26a2ef3c42a6701017d Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 30 Jan 2025 14:39:43 +0200 Subject: [PATCH 089/156] added support for `restart` and `beginSoftwareUpdate` methods --- include/ocvsmd/sdk/node_command_client.hpp | 16 +++++++++ src/cli/main.cpp | 14 +++----- src/sdk/node_command_client.cpp | 40 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/include/ocvsmd/sdk/node_command_client.hpp b/include/ocvsmd/sdk/node_command_client.hpp index 06ac00f..733e42a 100644 --- a/include/ocvsmd/sdk/node_command_client.hpp +++ b/include/ocvsmd/sdk/node_command_client.hpp @@ -55,9 +55,25 @@ class NodeCommandClient const Command::NodeRequest& node_request, const std::chrono::microseconds timeout) = 0; +`` /// A convenience method for invoking `sendCommand` with COMMAND_RESTART. + /// + SenderOf::Ptr restart( // + const cetl::span node_ids, + const std::chrono::microseconds timeout = std::chrono::seconds{1}); + + /// A convenience method for invoking `sendCommand` with COMMAND_BEGIN_SOFTWARE_UPDATE. + /// The file_path is relative to one of the roots configured in the file server. + /// + SenderOf::Ptr beginSoftwareUpdate( // + const cetl::span node_ids, + const cetl::string_view file_path, + const std::chrono::microseconds timeout = std::chrono::seconds{1}); + protected: NodeCommandClient() = default; + virtual cetl::pmr::memory_resource& getMemoryResource() const noexcept = 0; + }; // NodeCommandClient } // namespace sdk diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 95ce7f7..2b875ce 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -45,8 +45,7 @@ void signalHandler(const int sig) void setupSignalHandlers() { - struct sigaction sigbreak - {}; + struct sigaction sigbreak{}; sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); @@ -77,18 +76,15 @@ int main(const int argc, const char** const argv) return EXIT_FAILURE; } - // Demo of daemon's node command client, sending a command to node 42. + // Demo of daemon's node command client, sending a command to node 42, 43 & 44. { - using Command = ocvsmd::sdk::NodeCommandClient::Command; - using CommandParam = Command::NodeRequest::_traits_::TypeOf::parameter; + using Command = ocvsmd::sdk::NodeCommandClient::Command; auto node_cmd_client = daemon->getNodeCommandClient(); - constexpr auto cmd_id = Command::NodeRequest::COMMAND_IDENTIFY; const std::vector node_ids = {42, 43, 44}; - const Command::NodeRequest node_request{cmd_id, CommandParam{&memory}, &memory}; - - auto sender = node_cmd_client->sendCommand({node_ids.data(), node_ids.size()}, node_request, 1s); + // auto sender = node_cmd_client->restart({node_ids.data(), node_ids.size()}); + auto sender = node_cmd_client->beginSoftwareUpdate({node_ids.data(), node_ids.size()}, "firmware.bin"); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); if (const auto* const err = cetl::get_if(&cmd_result)) diff --git a/src/sdk/node_command_client.cpp b/src/sdk/node_command_client.cpp index 60683b6..5d885e8 100644 --- a/src/sdk/node_command_client.cpp +++ b/src/sdk/node_command_client.cpp @@ -44,6 +44,11 @@ class NodeCommandClientImpl final : public NodeCommandClient // NodeCommandClient + cetl::pmr::memory_resource& getMemoryResource() const noexcept override + { + return memory_; + } + SenderOf::Ptr sendCommand(const cetl::span node_ids, const Command::NodeRequest& node_request, const std::chrono::microseconds timeout) override @@ -111,6 +116,41 @@ class NodeCommandClientImpl final : public NodeCommandClient } // namespace +SenderOf::Ptr NodeCommandClient::restart( // + const cetl::span node_ids, + const std::chrono::microseconds timeout) +{ + auto& memory = getMemoryResource(); + + constexpr auto command = Command::NodeRequest::COMMAND_RESTART; + const Command::NodeRequest::_traits_::TypeOf::parameter no_param{&memory}; + const Command::NodeRequest node_request{command, no_param, {&memory}}; + + return sendCommand(node_ids, node_request, timeout); +} + +/// A convenience method for invoking `sendCommand` with COMMAND_BEGIN_SOFTWARE_UPDATE. +/// The file_path is relative to one of the roots configured in the file server. +/// +SenderOf::Ptr NodeCommandClient::beginSoftwareUpdate( // + const cetl::span node_ids, + const cetl::string_view file_path, + const std::chrono::microseconds timeout) +{ + using Parameter = Command::NodeRequest::_traits_::TypeOf::parameter; + + auto& memory = getMemoryResource(); + constexpr auto command = Command::NodeRequest::COMMAND_BEGIN_SOFTWARE_UPDATE; + const Parameter param{file_path.begin(), + file_path.end(), + Command::NodeRequest::_traits_::ArrayCapacity::parameter, + &memory}; + + const Command::NodeRequest node_request{command, param, {&memory}}; + + return sendCommand(node_ids, node_request, timeout); +} + CETL_NODISCARD NodeCommandClient::Ptr Factory::makeNodeCommandClient(cetl::pmr::memory_resource& memory, common::ipc::ClientRouter::Ptr ipc_router) { From 711427309300ed87ac44f69a83a1d6908686d6f4 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 30 Jan 2025 14:40:13 +0200 Subject: [PATCH 090/156] build fix --- include/ocvsmd/sdk/node_command_client.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ocvsmd/sdk/node_command_client.hpp b/include/ocvsmd/sdk/node_command_client.hpp index 733e42a..8d071bb 100644 --- a/include/ocvsmd/sdk/node_command_client.hpp +++ b/include/ocvsmd/sdk/node_command_client.hpp @@ -55,7 +55,7 @@ class NodeCommandClient const Command::NodeRequest& node_request, const std::chrono::microseconds timeout) = 0; -`` /// A convenience method for invoking `sendCommand` with COMMAND_RESTART. + /// A convenience method for invoking `sendCommand` with COMMAND_RESTART. /// SenderOf::Ptr restart( // const cetl::span node_ids, From 53042fd4625851ea579417921a03f5f5f4993b1b Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 30 Jan 2025 14:41:40 +0200 Subject: [PATCH 091/156] style fix --- src/cli/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 2b875ce..1c48606 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -45,7 +45,8 @@ void signalHandler(const int sig) void setupSignalHandlers() { - struct sigaction sigbreak{}; + struct sigaction sigbreak + {}; sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); From edb3a115de0c5b550b04dcd273431fa198b00526 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 30 Jan 2025 14:57:36 +0200 Subject: [PATCH 092/156] fix build --- src/sdk/node_command_client.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/sdk/node_command_client.cpp b/src/sdk/node_command_client.cpp index 5d885e8..85c63fe 100644 --- a/src/sdk/node_command_client.cpp +++ b/src/sdk/node_command_client.cpp @@ -141,10 +141,7 @@ SenderOf::Ptr NodeCommandClient::beginSoftwa auto& memory = getMemoryResource(); constexpr auto command = Command::NodeRequest::COMMAND_BEGIN_SOFTWARE_UPDATE; - const Parameter param{file_path.begin(), - file_path.end(), - Command::NodeRequest::_traits_::ArrayCapacity::parameter, - &memory}; + const Parameter param{file_path.begin(), file_path.end(), &memory}; const Command::NodeRequest node_request{command, param, {&memory}}; From 79b41abbf5ca2016e501a95e322ec3f038502004 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 31 Jan 2025 17:58:25 +0200 Subject: [PATCH 093/156] first draft of file provider --- .gitmodules | 1 - src/common/ipc/server_router.hpp | 1 + src/daemon/engine/CMakeLists.txt | 6 + src/daemon/engine/cyphal/file_provider.cpp | 200 ++++++++++++++++++ src/daemon/engine/cyphal/file_provider.hpp | 58 +++++ src/daemon/engine/engine.cpp | 19 +- src/daemon/engine/engine.hpp | 2 + src/daemon/engine/engine_helpers.hpp | 78 +++++++ .../engine/svc/node/exec_cmd_service.cpp | 1 + src/daemon/engine/svc/svc_helpers.hpp | 56 ----- submodules/libcyphal | 2 +- test/daemon/engine/CMakeLists.txt | 1 - test/daemon/engine/test_xxx.cpp | 29 --- 13 files changed, 362 insertions(+), 92 deletions(-) create mode 100644 src/daemon/engine/cyphal/file_provider.cpp create mode 100644 src/daemon/engine/cyphal/file_provider.hpp create mode 100644 src/daemon/engine/engine_helpers.hpp delete mode 100644 test/daemon/engine/test_xxx.cpp diff --git a/.gitmodules b/.gitmodules index 299c292..0b13fe0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,6 @@ [submodule "submodules/libcyphal"] path = submodules/libcyphal url = https://github.com/OpenCyphal-Garage/libcyphal.git - branch = sshirokov/416_tid [submodule "submodules/libudpard"] path = submodules/libudpard url = https://github.com/OpenCyphal/libudpard diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index 7ce4d33..667aa5e 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -8,6 +8,7 @@ #include "channel.hpp" #include "gateway.hpp" +#include "ipc_types.hpp" #include "pipe/server_pipe.hpp" #include diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 0f99b17..b8eab8d 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -8,6 +8,11 @@ cmake_minimum_required(VERSION 3.27) # Define type generation and header library all in one go. # set(dsdl_types_in_engine # List all the DSDL types used in the engine + uavcan/file/405.GetInfo.0.2.dsdl + uavcan/file/406.List.0.2.dsdl + uavcan/file/407.Modify.1.1.dsdl + uavcan/file/408.Read.1.1.dsdl + uavcan/file/409.Write.1.1.dsdl uavcan/node/430.GetInfo.1.0.dsdl uavcan/node/435.ExecuteCommand.1.3.dsdl uavcan/node/7509.Heartbeat.1.0.dsdl @@ -33,6 +38,7 @@ target_include_directories(udpard add_library(ocvsmd_engine config.cpp + cyphal/file_provider.cpp engine.cpp platform/udp/udp.c svc/node/exec_cmd_service.cpp diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp new file mode 100644 index 0000000..e2c58fd --- /dev/null +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -0,0 +1,200 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "file_provider.hpp" + +#include "engine_helpers.hpp" +#include "logging.hpp" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace cyphal +{ +namespace +{ + +class FileProviderImpl final : public FileProvider +{ + template + struct SvcSpec : T + { + using Server = libcyphal::presentation::ServiceServer; + using CallbackArg = typename Server::OnRequestCallback::Arg; + }; + struct Svc + { + using List = SvcSpec; + using Read = SvcSpec; + using Write = SvcSpec; + using Modify = SvcSpec; + using GetInfo = SvcSpec; + + }; // Svc + +public: + static Ptr make(cetl::pmr::memory_resource& memory, libcyphal::presentation::Presentation& presentation) + { + auto list_srv = makeServer("List", presentation); + auto read_srv = makeServer("Read", presentation); + auto write_srv = makeServer("Write", presentation); + auto modify_srv = makeServer("Modify", presentation); + auto get_info_srv = makeServer("GetInfo", presentation); + if (!list_srv || !read_srv || !write_srv || !modify_srv || !get_info_srv) + { + return nullptr; + } + return std::make_unique(memory, + std::move(*list_srv), + std::move(*read_srv), + std::move(*write_srv), + std::move(*modify_srv), + std::move(*get_info_srv)); + } + + FileProviderImpl(cetl::pmr::memory_resource& memory, + Svc::List::Server&& list_srv, + Svc::Read::Server&& read_srv, + Svc::Write::Server&& write_srv, + Svc::Modify::Server&& modify_srv, + Svc::GetInfo::Server&& get_info_srv) + : memory_{memory} + , list_srv_{std::move(list_srv)} + , read_srv_{std::move(read_srv)} + , write_srv_{std::move(write_srv)} + , modify_srv_{std::move(modify_srv)} + , get_info_srv_{std::move(get_info_srv)} + { + logger_->trace("FileProviderImpl()."); + + setupOnRequestCallback(get_info_srv_, [this](const auto& arg) { + // + return serveGetInfoRequest(arg); + }); + setupOnRequestCallback(read_srv_, [this](const auto& arg) { + // + return serveReadRequest(arg); + }); + } + + FileProviderImpl(const FileProviderImpl&) = delete; + FileProviderImpl(FileProviderImpl&&) noexcept = delete; + FileProviderImpl& operator=(const FileProviderImpl&) = delete; + FileProviderImpl& operator=(FileProviderImpl&&) noexcept = delete; + + ~FileProviderImpl() override + { + logger_->trace("~FileProviderImpl."); + } + + // FileProvider + +private: + using Presentation = libcyphal::presentation::Presentation; + + template + static auto makeServer(const cetl::string_view role, Presentation& presentation) + -> cetl::optional + { + auto maybe_server = presentation.makeServer(); + if (const auto* const failure = cetl::get_if(&maybe_server)) + { + const auto err = failureToErrorCode(*failure); + spdlog::error("Failed to make '{}' server (err={}).", role, err); + return cetl::nullopt; + } + return cetl::get(std::move(maybe_server)); + } + + template + void setupOnRequestCallback(typename Service::Server& server, Handler&& handler) + { + server.setOnRequestCallback([handle = std::forward(handler)](const auto& arg, auto& continuation) { + // + constexpr auto timeout = std::chrono::seconds{1}; + continuation(arg.approx_now + timeout, handle(arg)); + }); + } + + static cetl::string_view stringViewFrom(const uavcan::file::Path_2_0& file_path) + { + const auto* const path_data = reinterpret_cast(file_path.path.data()); // NOLINT + return cetl::string_view{path_data, file_path.path.size()}; + } + + static constexpr std::size_t TestFileSize = 1000000000; + + Svc::GetInfo::Response serveGetInfoRequest(const Svc::GetInfo::CallbackArg& arg) + { + const auto request_path = stringViewFrom(arg.request.path); + logger_->trace("'GetInfo' request (from={}, path='{}').", arg.metadata.remote_node_id, request_path); + + Svc::GetInfo::Response response{&memory_}; + response._error.value = uavcan::file::Error_1_0::OK; + response.size = TestFileSize; + response.is_file_not_directory = true; + response.is_readable = true; + return response; + } + + Svc::Read::Response serveReadRequest(const Svc::Read::CallbackArg& arg) + { + // const auto request_path = stringViewFrom(arg.request.path); + // logger_->trace("'Read' request (from={}, offset={}, path='{}').", + // arg.metadata.remote_node_id, + // arg.request.offset, + // request_path); + + Svc::Read::Response response{&memory_}; + response._error.value = uavcan::file::Error_1_0::OK; + response.data.value.resize(std::min(TestFileSize - arg.request.offset, 256U)); // NOLINT + return response; + } + + static constexpr libcyphal::Duration Timeout = std::chrono::seconds{1}; + + cetl::pmr::memory_resource& memory_; + Svc::List::Server list_srv_; + Svc::Read::Server read_srv_; + Svc::Write::Server write_srv_; + Svc::Modify::Server modify_srv_; + Svc::GetInfo::Server get_info_srv_; + common::LoggerPtr logger_{common::getLogger("engine")}; + +}; // FileProviderImpl + +} // namespace + +FileProvider::Ptr FileProvider::make(cetl::pmr::memory_resource& memory, + libcyphal::presentation::Presentation& presentation) +{ + return FileProviderImpl::make(memory, presentation); +} + +} // namespace cyphal +} // namespace engine +} // namespace daemon +} // namespace ocvsmd diff --git a/src/daemon/engine/cyphal/file_provider.hpp b/src/daemon/engine/cyphal/file_provider.hpp new file mode 100644 index 0000000..1787307 --- /dev/null +++ b/src/daemon/engine/cyphal/file_provider.hpp @@ -0,0 +1,58 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_CYPHAL_FILE_PROVIDER_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_CYPHAL_FILE_PROVIDER_HPP_INCLUDED + +#include +#include +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace cyphal +{ + +/// @brief Defines 'File' provider component for the application node. +/// +/// Internally uses several `uavcan.file` cyphal servers: +// - 'GetInfo' +// - 'List' +// - 'Modify' +// - 'Read' +// - 'Write' +/// +class FileProvider +{ +public: + using Ptr = std::unique_ptr; + + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, + libcyphal::presentation::Presentation& presentation); + + FileProvider(const FileProvider&) = delete; + FileProvider(FileProvider&&) noexcept = delete; + FileProvider& operator=(const FileProvider&) = delete; + FileProvider& operator=(FileProvider&&) noexcept = delete; + + virtual ~FileProvider() = default; + +protected: + FileProvider() = default; + +}; // FileProvider + +} // namespace cyphal +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_CYPHAL_FILE_PROVIDER_HPP_INCLUDED diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 4971196..10e917b 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -6,6 +6,7 @@ #include "engine.hpp" #include "config.hpp" +#include "cyphal/file_provider.hpp" #include "ipc/pipe/net_socket_server.hpp" // #include "ipc/pipe/unix_socket_server.hpp" #include "ipc/server_router.hpp" @@ -56,7 +57,7 @@ cetl::optional Engine::init() // 3. Create the node object with name. // - auto maybe_node = libcyphal::application::Node::make(presentation_.value()); + auto maybe_node = libcyphal::application::Node::make(*presentation_); if (const auto* failure = cetl::get_if(&maybe_node)) { (void) failure; @@ -73,16 +74,26 @@ cetl::optional Engine::init() .setSoftwareVcsRevisionId(VCS_REVISION_ID) .setUniqueId(getUniqueId()); + // 5. Bring up various providers. + // + file_provider_ = cyphal::FileProvider::make(memory_, *presentation_); + if (file_provider_ == nullptr) + { + return "Failed to create cyphal file provider."; + } + + // 6. Bring up the IPC router and its services. + // // using ServerPipe = common::ipc::pipe::UnixSocketServer; // auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); using ServerPipe = common::ipc::pipe::NetSocketServer; auto server_pipe = std::make_unique(executor_, 9875); // NOLINT(*-magic-numbers) - + // ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); - + // const svc::ScvContext svc_context{memory_, executor_, *ipc_router_, *presentation_}; svc::node::ExecCmdService::registerWithContext(svc_context); - + // if (0 != ipc_router_->start()) { return "Failed to start IPC router."; diff --git a/src/daemon/engine/engine.hpp b/src/daemon/engine/engine.hpp index d8b4681..f5b4171 100644 --- a/src/daemon/engine/engine.hpp +++ b/src/daemon/engine/engine.hpp @@ -7,6 +7,7 @@ #define OCVSMD_DAEMON_ENGINE_HPP_INCLUDED #include "config.hpp" +#include "cyphal/file_provider.hpp" #include "cyphal/udp_transport_bag.hpp" #include "logging.hpp" #include "ocvsmd/platform/defines.hpp" @@ -73,6 +74,7 @@ class Engine TransferIdMap transfer_id_map_; cetl::optional presentation_; cetl::optional node_; + cyphal::FileProvider::Ptr file_provider_; common::ipc::ServerRouter::Ptr ipc_router_; }; // Engine diff --git a/src/daemon/engine/engine_helpers.hpp b/src/daemon/engine/engine_helpers.hpp new file mode 100644 index 0000000..eaf252d --- /dev/null +++ b/src/daemon/engine/engine_helpers.hpp @@ -0,0 +1,78 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_HELPERS_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_HELPERS_HPP_INCLUDED + +#include + +#include +#include +#include +#include +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ + +inline int errorToCode(const libcyphal::MemoryError) noexcept +{ + return ENOMEM; +} +inline int errorToCode(const libcyphal::transport::CapacityError) noexcept +{ + return ENOMEM; +} + +inline int errorToCode(const libcyphal::ArgumentError) noexcept +{ + return EINVAL; +} +inline int errorToCode(const libcyphal::transport::AnonymousError) noexcept +{ + return EINVAL; +} +inline int errorToCode(const nunavut::support::Error) noexcept +{ + return EINVAL; +} + +inline int errorToCode(const libcyphal::transport::AlreadyExistsError) noexcept +{ + return EEXIST; +} + +inline int errorToCode(const libcyphal::transport::PlatformError& platform_error) noexcept +{ + return static_cast(platform_error->code()); +} + +inline int errorToCode(const libcyphal::presentation::ResponsePromiseExpired) noexcept +{ + return ETIMEDOUT; +} + +inline int errorToCode(const libcyphal::presentation::detail::ClientBase::TooManyPendingRequestsError) noexcept +{ + return EBUSY; +} + +template +int failureToErrorCode(const Variant& failure) +{ + return cetl::visit([](const auto& error) { return errorToCode(error); }, failure); +} + +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_HELPERS_HPP_INCLUDED diff --git a/src/daemon/engine/svc/node/exec_cmd_service.cpp b/src/daemon/engine/svc/node/exec_cmd_service.cpp index 29ebe85..6d6a32c 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.cpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.cpp @@ -5,6 +5,7 @@ #include "exec_cmd_service.hpp" +#include "engine_helpers.hpp" #include "ipc/channel.hpp" #include "ipc/server_router.hpp" #include "logging.hpp" diff --git a/src/daemon/engine/svc/svc_helpers.hpp b/src/daemon/engine/svc/svc_helpers.hpp index 7102e8a..3ba09d6 100644 --- a/src/daemon/engine/svc/svc_helpers.hpp +++ b/src/daemon/engine/svc/svc_helpers.hpp @@ -8,17 +8,9 @@ #include "ipc/server_router.hpp" -#include - #include -#include #include -#include #include -#include -#include - -#include namespace ocvsmd { @@ -38,54 +30,6 @@ struct ScvContext }; // ScvContext -inline int errorToCode(const libcyphal::MemoryError) noexcept -{ - return ENOMEM; -} -inline int errorToCode(const libcyphal::transport::CapacityError) noexcept -{ - return ENOMEM; -} - -inline int errorToCode(const libcyphal::ArgumentError) noexcept -{ - return EINVAL; -} -inline int errorToCode(const libcyphal::transport::AnonymousError) noexcept -{ - return EINVAL; -} -inline int errorToCode(const nunavut::support::Error) noexcept -{ - return EINVAL; -} - -inline int errorToCode(const libcyphal::transport::AlreadyExistsError) noexcept -{ - return EEXIST; -} - -inline int errorToCode(const libcyphal::transport::PlatformError& platform_error) noexcept -{ - return static_cast(platform_error->code()); -} - -inline int errorToCode(const libcyphal::presentation::ResponsePromiseExpired) noexcept -{ - return ETIMEDOUT; -} - -inline int errorToCode(const libcyphal::presentation::detail::ClientBase::TooManyPendingRequestsError) noexcept -{ - return EBUSY; -} - -template -int failureToErrorCode(const Variant& failure) -{ - return cetl::visit([](const auto& error) { return errorToCode(error); }, failure); -} - } // namespace svc } // namespace engine } // namespace daemon diff --git a/submodules/libcyphal b/submodules/libcyphal index 1e226b9..3d0320d 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit 1e226b9b336ae082d228d21d8dc42a16b7cf2825 +Subproject commit 3d0320dee3c64f59d8773bdc39e5bc3318bb40d8 diff --git a/test/daemon/engine/CMakeLists.txt b/test/daemon/engine/CMakeLists.txt index 960f5f7..0639903 100644 --- a/test/daemon/engine/CMakeLists.txt +++ b/test/daemon/engine/CMakeLists.txt @@ -7,7 +7,6 @@ cmake_minimum_required(VERSION 3.27) add_executable(engine_tests main.cpp - test_xxx.cpp ) target_link_libraries(engine_tests ocvsmd_engine diff --git a/test/daemon/engine/test_xxx.cpp b/test/daemon/engine/test_xxx.cpp deleted file mode 100644 index ed7c944..0000000 --- a/test/daemon/engine/test_xxx.cpp +++ /dev/null @@ -1,29 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#include -#include - -namespace -{ - -class TestXxx : public testing::Test -{ -protected: - void SetUp() override {} - void TearDown() override {} -}; - -// MARK: - Tests: - -TEST_F(TestXxx, make) -{ - // Expect two strings not to be equal. - EXPECT_STRNE("hello", "world"); - // Expect equality. - EXPECT_THAT(7 * 6, 42); -} - -} // namespace From e03f6977f632dbd41cf9b4d4375394452aeb7e2f Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 31 Jan 2025 20:19:33 +0200 Subject: [PATCH 094/156] implemented file reading --- src/daemon/engine/cyphal/file_provider.cpp | 79 ++++++++++++++++------ 1 file changed, 60 insertions(+), 19 deletions(-) diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index e2c58fd..c40e976 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -23,7 +23,14 @@ #include +#include +#include +#include +#include #include +#include +#include +#include #include namespace ocvsmd @@ -139,43 +146,77 @@ class FileProviderImpl final : public FileProvider }); } - static cetl::string_view stringViewFrom(const uavcan::file::Path_2_0& file_path) + static std::string stringFrom(const uavcan::file::Path_2_0& file_path) { const auto* const path_data = reinterpret_cast(file_path.path.data()); // NOLINT - return cetl::string_view{path_data, file_path.path.size()}; + return std::string{path_data, file_path.path.size()}; } - static constexpr std::size_t TestFileSize = 1000000000; + static cetl::optional canonicalizePath(const std::string& path) + { + char* const resolved_path = ::realpath(path.c_str(), nullptr); + if (resolved_path == nullptr) + { + return cetl::nullopt; + } + + std::string result{resolved_path}; + ::free(resolved_path); // NOLINT - Svc::GetInfo::Response serveGetInfoRequest(const Svc::GetInfo::CallbackArg& arg) + return result; + } + + Svc::GetInfo::Response serveGetInfoRequest(const Svc::GetInfo::CallbackArg& arg) const { - const auto request_path = stringViewFrom(arg.request.path); + const auto request_path = stringFrom(arg.request.path); logger_->trace("'GetInfo' request (from={}, path='{}').", arg.metadata.remote_node_id, request_path); Svc::GetInfo::Response response{&memory_}; - response._error.value = uavcan::file::Error_1_0::OK; - response.size = TestFileSize; - response.is_file_not_directory = true; - response.is_readable = true; + response._error.value = uavcan::file::Error_1_0::NOT_FOUND; + + if (const auto real_path = canonicalizePath(request_path)) + { + struct stat file_stat{}; + if (::stat(real_path->c_str(), &file_stat) == 0) + { + response.size = file_stat.st_size; + response.is_file_not_directory = true; + response.is_readable = true; + response._error.value = uavcan::file::Error_1_0::OK; + } + } + return response; } - Svc::Read::Response serveReadRequest(const Svc::Read::CallbackArg& arg) + Svc::Read::Response serveReadRequest(const Svc::Read::CallbackArg& arg) const { - // const auto request_path = stringViewFrom(arg.request.path); - // logger_->trace("'Read' request (from={}, offset={}, path='{}').", - // arg.metadata.remote_node_id, - // arg.request.offset, - // request_path); + const auto request_path = stringFrom(arg.request.path); + const auto real_path = canonicalizePath(request_path); Svc::Read::Response response{&memory_}; - response._error.value = uavcan::file::Error_1_0::OK; - response.data.value.resize(std::min(TestFileSize - arg.request.offset, 256U)); // NOLINT + auto& buffer = response.data.value; + + constexpr std::size_t BufferSize = 256U; + buffer.resize(BufferSize); + + try + { + std::ifstream file{request_path.data(), std::ios::binary}; + file.seekg(static_cast(arg.request.offset)); + file.read(reinterpret_cast(buffer.data()), BufferSize); // NOLINT + buffer.resize(file.gcount()); + response._error.value = uavcan::file::Error_1_0::OK; + + } catch (std::exception& ex) + { + buffer.clear(); + logger_->error("Failed to read file '{}': {}.", request_path, ex.what()); + response._error.value = uavcan::file::Error_1_0::UNKNOWN_ERROR; + } return response; } - static constexpr libcyphal::Duration Timeout = std::chrono::seconds{1}; - cetl::pmr::memory_resource& memory_; Svc::List::Server list_srv_; Svc::Read::Server read_srv_; From c5d1fde1fe019f1a3c72f41b262444e274085bb4 Mon Sep 17 00:00:00 2001 From: Sergei Date: Sat, 1 Feb 2025 00:58:32 +0200 Subject: [PATCH 095/156] fix build --- src/daemon/engine/cyphal/file_provider.cpp | 3 +-- src/daemon/engine/engine.hpp | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index c40e976..540189e 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -24,11 +23,11 @@ #include #include +#include #include #include #include #include -#include #include #include #include diff --git a/src/daemon/engine/engine.hpp b/src/daemon/engine/engine.hpp index f5b4171..770f424 100644 --- a/src/daemon/engine/engine.hpp +++ b/src/daemon/engine/engine.hpp @@ -46,6 +46,7 @@ class Engine class TransferIdMap final : public libcyphal::transport::ITransferIdMap { using TransferId = libcyphal::transport::TransferId; + using Map = std::unordered_map; // ITransferIdMap @@ -60,7 +61,7 @@ class Engine session_spec_to_transfer_id_[session_spec] = transfer_id; } - std::unordered_map session_spec_to_transfer_id_; + Map session_spec_to_transfer_id_; }; // TransferIdMap From 00cee90785a2b061a2f3b08c1f9d2818f545988f Mon Sep 17 00:00:00 2001 From: Sergei Date: Sat, 1 Feb 2025 01:02:04 +0200 Subject: [PATCH 096/156] fix style --- src/daemon/engine/cyphal/file_provider.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 540189e..5d1d91b 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -122,8 +122,8 @@ class FileProviderImpl final : public FileProvider using Presentation = libcyphal::presentation::Presentation; template - static auto makeServer(const cetl::string_view role, Presentation& presentation) - -> cetl::optional + static auto makeServer(const cetl::string_view role, + Presentation& presentation) -> cetl::optional { auto maybe_server = presentation.makeServer(); if (const auto* const failure = cetl::get_if(&maybe_server)) @@ -175,7 +175,8 @@ class FileProviderImpl final : public FileProvider if (const auto real_path = canonicalizePath(request_path)) { - struct stat file_stat{}; + struct stat file_stat + {}; if (::stat(real_path->c_str(), &file_stat) == 0) { response.size = file_stat.st_size; @@ -191,7 +192,7 @@ class FileProviderImpl final : public FileProvider Svc::Read::Response serveReadRequest(const Svc::Read::CallbackArg& arg) const { const auto request_path = stringFrom(arg.request.path); - const auto real_path = canonicalizePath(request_path); + const auto real_path = canonicalizePath(request_path); Svc::Read::Response response{&memory_}; auto& buffer = response.data.value; From 6a7f50548de26b650123f321e37a8d145008864f Mon Sep 17 00:00:00 2001 From: Sergei Date: Sat, 1 Feb 2025 21:45:29 +0200 Subject: [PATCH 097/156] fix build --- src/daemon/engine/cyphal/file_provider.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 5d1d91b..481f97e 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -23,11 +23,11 @@ #include #include -#include #include #include #include #include +#include // ::realpath #include #include #include From 93960263a2cfecd39e74a30c7880308b0ab973ce Mon Sep 17 00:00:00 2001 From: Sergei Date: Sat, 1 Feb 2025 22:25:15 +0200 Subject: [PATCH 098/156] fix build --- src/daemon/engine/cyphal/file_provider.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 481f97e..b25f769 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -27,7 +27,7 @@ #include #include #include -#include // ::realpath +#include // NOLINT ::realpath #include #include #include From 48d76d53cd80daee142009e77211f69996c1f1ef Mon Sep 17 00:00:00 2001 From: Sergei Date: Mon, 3 Feb 2025 00:40:57 +0200 Subject: [PATCH 099/156] added new `io::socket_address` & `io::OwnFd` --- .../bsd/kqueue_single_threaded_executor.hpp | 2 +- src/cli/setup_logging.hpp | 1 + src/common/CMakeLists.txt | 2 + src/common/io/io.cpp | 36 +++ src/common/io/io.hpp | 66 +++++ src/common/io/socket_address.cpp | 248 ++++++++++++++++++ src/common/io/socket_address.hpp | 55 ++++ src/common/ipc/pipe/socket_server_base.hpp | 1 + src/daemon/setup_logging.hpp | 1 + 9 files changed, 411 insertions(+), 1 deletion(-) create mode 100644 src/common/io/io.cpp create mode 100644 src/common/io/io.hpp create mode 100644 src/common/io/socket_address.cpp create mode 100644 src/common/io/socket_address.hpp diff --git a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp index 5524437..82f6605 100644 --- a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp @@ -43,7 +43,7 @@ namespace bsd /// @brief Defines BSD Linux platform specific single-threaded executor based on `kqueue` mechanism. /// class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThreadedExecutor, - public common::platform::IPosixExecutorExtension + public IPosixExecutorExtension { public: KqueueSingleThreadedExecutor() diff --git a/src/cli/setup_logging.hpp b/src/cli/setup_logging.hpp index ad5bd12..f731eca 100644 --- a/src/cli/setup_logging.hpp +++ b/src/cli/setup_logging.hpp @@ -111,6 +111,7 @@ inline void setupLogging(const int argc, const char** const argv) // Register specific subsystem loggers. // + register_logger(std::make_shared("io", file_sink)); register_logger(std::make_shared("ipc", file_sink)); register_logger(std::make_shared("sdk", file_sink)); register_logger(std::make_shared("svc", file_sink)); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 336e24b..f76b24d 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -20,6 +20,8 @@ add_cyphal_library( ) add_library(ocvsmd_common + io/io.cpp + io/socket_address.cpp ipc/client_router.cpp ipc/pipe/net_socket_client.cpp ipc/pipe/net_socket_server.cpp diff --git a/src/common/io/io.cpp b/src/common/io/io.cpp new file mode 100644 index 0000000..79cbc42 --- /dev/null +++ b/src/common/io/io.cpp @@ -0,0 +1,36 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "io.hpp" + +#include "logging.hpp" + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace io +{ + +OwnFd::~OwnFd() +{ + if (fd_ >= 0) + { + // Do not use `posixSyscallError` here b/c `close` should not be repeated on `EINTR`. + if (::close(fd_) < 0) + { + const int err = errno; + getLogger("io")->error("Failed to close file descriptor {}: {}.", fd_, std::strerror(err)); + } + } +} + +} // namespace io +} // namespace common +} // namespace ocvsmd diff --git a/src/common/io/io.hpp b/src/common/io/io.hpp new file mode 100644 index 0000000..74eeaef --- /dev/null +++ b/src/common/io/io.hpp @@ -0,0 +1,66 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IO_HPP_INCLUDED +#define OCVSMD_COMMON_IO_HPP_INCLUDED + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace io +{ + +/// RAII wrapper for a file descriptor. +/// +class OwnFd final +{ +public: + OwnFd() + : fd_{-1} + { + } + + explicit OwnFd(const int fd) + : fd_{fd} + { + } + + OwnFd(OwnFd&& other) noexcept + : fd_{std::exchange(other.fd_, -1)} + { + } + + OwnFd& operator=(OwnFd&& other) noexcept + { + const OwnFd old{std::move(*this)}; + fd_ = std::exchange(other.fd_, -1); + return *this; + } + + OwnFd& operator=(std::nullptr_t) + { + const OwnFd old{std::move(*this)}; + return *this; + } + + // Disallow copy. + OwnFd(const OwnFd&) = delete; + OwnFd& operator=(const OwnFd&) = delete; + + ~OwnFd(); + +private: + int fd_; + +}; // OwnFd + +} // namespace io +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IO_HPP_INCLUDED diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp new file mode 100644 index 0000000..7383c83 --- /dev/null +++ b/src/common/io/socket_address.cpp @@ -0,0 +1,248 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "socket_address.hpp" + +#include "logging.hpp" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace io +{ + +SocketAddress::SocketAddress() noexcept + : is_wildcard_{false} + , addr_len_{0} + , addr_storage_{} +{ +} + +SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, const std::uint16_t port_hint) +{ + // Unix domain? + // + if (auto result = tryParseAsUnixDomain(str)) + { + return *result; + } + if (auto result = tryParseAsAbstractUnixDomain(str)) + { + return *result; + } + + // Extract the family, host, and port. + // + std::string host; + std::uint16_t port = port_hint; + const int family = extractFamilyHostAndPort(str, host, port); + if (family == AF_UNSPEC) + { + return EINVAL; + } + if (auto result = tryParseAsWildcard(host, port)) + { + return *result; + } + + // Convert the host string to inet address. + // + SocketAddress result{}; + void* addr_target = nullptr; + if (family == AF_INET6) + { + auto& result_inet6 = reinterpret_cast(result.addr_storage_); // NOLINT + result.addr_len_ = sizeof(result_inet6); + result_inet6.sin6_family = AF_INET6; + result_inet6.sin6_port = ::htons(port); + addr_target = &result_inet6.sin6_addr; + } + else + { + auto& result_inet4 = reinterpret_cast(result.addr_storage_); // NOLINT + result.addr_len_ = sizeof(result_inet4); + result_inet4.sin_family = AF_INET; + result_inet4.sin_port = ::htons(port); + addr_target = &result_inet4.sin_addr; + } + const int convert_result = ::inet_pton(family, host.c_str(), addr_target); + switch (convert_result) + { + case 1: { + return result; + } + case 0: { + getLogger("io")->error("Unsupported address (addr='{}').", host); + return EINVAL; + } + default: { + const int err = errno; + getLogger("io")->error("Failed to parse address (addr='{}'): {}", host, std::strerror(err)); + return err; + } + } +} + +cetl::optional SocketAddress::tryParseAsUnixDomain(const std::string& str) +{ + if (0 != str.find_first_of("unix:")) + { + return cetl::nullopt; + } + const auto path = str.substr(std::strlen("unix:")); + + SocketAddress result{}; + auto& result_un = reinterpret_cast(result.addr_storage_); // NOLINT + result_un.sun_family = AF_UNIX; + + if (path.size() >= sizeof(result_un.sun_path)) + { + getLogger("io")->error("Unix domain path is too long (path='{}').", str); + return EINVAL; + } + // NOLINTNEXTLINE(*-array-to-pointer-decay, *-no-array-decay) + std::strcpy(result_un.sun_path, path.c_str()); + + result.addr_len_ = offsetof(sockaddr_un, sun_path) + path.size() + 1; + return result; +} + +cetl::optional SocketAddress::tryParseAsAbstractUnixDomain(const std::string& str) +{ + if (0 != str.find_first_of("unix-abstract:")) + { + return cetl::nullopt; + } + const auto path = str.substr(std::strlen("unix-abstract:")); + + SocketAddress result{}; + auto& result_un = reinterpret_cast(result.addr_storage_); // NOLINT + result_un.sun_family = AF_UNIX; + + if (path.size() >= sizeof(result_un.sun_path)) + { + getLogger("io")->error("Unix domain path is too long (path='{}').", str); + return EINVAL; + } + // NOLINTNEXTLINE(*-array-to-pointer-decay, *-no-array-decay, *-pointer-arithmetic) + std::memcpy(result_un.sun_path + 1, path.c_str(), path.size() + 1); + + result.addr_len_ = offsetof(sockaddr_un, sun_path) + path.size() + 1; + return result; +} + +int SocketAddress::extractFamilyHostAndPort(const std::string& str, std::string& host, std::uint16_t& port) +{ + int family = AF_INET; + std::string port_part; + + if (0 == str.find_first_of('[')) + { + // IPv6 starts with a bracket when with a port. + family = AF_INET6; + + const auto end_bracket_pos = str.find_last_of(']'); + if (end_bracket_pos == std::string::npos) + { + getLogger("io")->error("Invalid IPv6 address; unclosed '[' (addr='{}').", str); + return AF_UNSPEC; + } + host = str.substr(1, end_bracket_pos); + + if (str.size() > end_bracket_pos + 1) + { + if (0 != str.find_first_of(':', end_bracket_pos + 1)) + { + getLogger("io")->error("Invalid IPv6 address; expected port suffix after ']': (addr='{}').", str); + return AF_UNSPEC; + } + port_part = str.substr(end_bracket_pos + 2); + } + } + else + { + const auto colon_pos = str.find_first_of(':'); + if (colon_pos != std::string::npos) + { + if (str.find_first_of(':', colon_pos + 1) != std::string::npos) + { + // There are at least two colons, so it must be an IPv6 address (without port). + family = AF_INET6; + host = str; + } + else + { + // There is only one colon (and no brackets), so it must be an IPv4 address with a port. + host = str.substr(0, colon_pos); + port_part = str.substr(colon_pos + 1); + } + } + else + { + // There is no colon in the string, so it must be an IPv4 address (without port). + host = str; + } + } + + // Parse the port if any; otherwise keep untouched (hint). + // + if (!port_part.empty()) + { + char* end_ptr = nullptr; + const std::uint64_t maybe_port = std::strtoull(port_part.c_str(), &end_ptr, 0); + if (*end_ptr != '\0') + { + getLogger("io")->error("Invalid port number (port='{}').", port_part); + return AF_UNSPEC; + } + if (maybe_port > std::numeric_limits::max()) + { + getLogger("io")->error("Port number is too large (port={}).", maybe_port); + return AF_UNSPEC; + } + port = static_cast(maybe_port); + } + + return family; +} + +cetl::optional SocketAddress::tryParseAsWildcard(const std::string& host, + const std::uint16_t port) +{ + if (host != "*") + { + return cetl::nullopt; + } + + SocketAddress result{}; + result.is_wildcard_ = true; + auto& result_inet6 = reinterpret_cast(result.addr_storage_); // NOLINT + result_inet6.sin6_port = ::htons(port); + result.addr_len_ = sizeof(result_inet6); + + // IPv4 will be also enabled by IPV6_V6ONLY=0 (at `bind` method). + result_inet6.sin6_family = AF_INET6; + + return result; +} + +} // namespace io +} // namespace common +} // namespace ocvsmd diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp new file mode 100644 index 0000000..8feb452 --- /dev/null +++ b/src/common/io/socket_address.hpp @@ -0,0 +1,55 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_NET_SOCKET_ADDRESS_HPP_INCLUDED +#define OCVSMD_COMMON_NET_SOCKET_ADDRESS_HPP_INCLUDED + +#include "io.hpp" + +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace common +{ +namespace io +{ + +class SocketAddress +{ +public: + OwnFd socket() const; + + struct ParseResult + { + using Failure = int; // aka errno + using Success = SocketAddress; + using Var = cetl::variant; + }; + static ParseResult::Var parse(const std::string& str, const std::uint16_t port_hint); + +private: + SocketAddress() noexcept; + + static cetl::optional tryParseAsUnixDomain(const std::string& str); + static cetl::optional tryParseAsAbstractUnixDomain(const std::string& str); + static int extractFamilyHostAndPort(const std::string& str, std::string& host, std::uint16_t& port); + static cetl::optional tryParseAsWildcard(const std::string& host, const std::uint16_t port); + + bool is_wildcard_; + socklen_t addr_len_; + sockaddr_storage addr_storage_; + +}; // SocketAddress + +} // namespace io +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_NET_SOCKET_ADDRESS_HPP_INCLUDED diff --git a/src/common/ipc/pipe/socket_server_base.hpp b/src/common/ipc/pipe/socket_server_base.hpp index e21e714..6f72b12 100644 --- a/src/common/ipc/pipe/socket_server_base.hpp +++ b/src/common/ipc/pipe/socket_server_base.hpp @@ -7,6 +7,7 @@ #define OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_BASE_HPP_INCLUDED #include "client_context.hpp" +#include "io/io.hpp" #include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" #include "server_pipe.hpp" diff --git a/src/daemon/setup_logging.hpp b/src/daemon/setup_logging.hpp index 4c4e2d2..2707181 100644 --- a/src/daemon/setup_logging.hpp +++ b/src/daemon/setup_logging.hpp @@ -143,6 +143,7 @@ inline void setupLogging(const int err_fd, // Register specific subsystem loggers - they go to the file sink only. // + register_logger(std::make_shared("io", file_sink)); register_logger(std::make_shared("ipc", file_sink)); register_logger(std::make_shared("engine", file_sink)); From 803e136c52636c0773e40befe692b10e46a14ba0 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Feb 2025 00:48:26 +0200 Subject: [PATCH 100/156] fix macos build --- src/common/io/socket_address.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 7383c83..7e0050f 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -71,7 +71,7 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con auto& result_inet6 = reinterpret_cast(result.addr_storage_); // NOLINT result.addr_len_ = sizeof(result_inet6); result_inet6.sin6_family = AF_INET6; - result_inet6.sin6_port = ::htons(port); + result_inet6.sin6_port = htons(port); addr_target = &result_inet6.sin6_addr; } else @@ -79,7 +79,7 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con auto& result_inet4 = reinterpret_cast(result.addr_storage_); // NOLINT result.addr_len_ = sizeof(result_inet4); result_inet4.sin_family = AF_INET; - result_inet4.sin_port = ::htons(port); + result_inet4.sin_port = htons(port); addr_target = &result_inet4.sin_addr; } const int convert_result = ::inet_pton(family, host.c_str(), addr_target); @@ -234,7 +234,7 @@ cetl::optional SocketAddress::tryParseAsWil SocketAddress result{}; result.is_wildcard_ = true; auto& result_inet6 = reinterpret_cast(result.addr_storage_); // NOLINT - result_inet6.sin6_port = ::htons(port); + result_inet6.sin6_port = htons(port); result.addr_len_ = sizeof(result_inet6); // IPv4 will be also enabled by IPV6_V6ONLY=0 (at `bind` method). From 3dc4034febb8b49aab41aef2944c87a294926c0c Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Feb 2025 14:59:08 +0200 Subject: [PATCH 101/156] unit tests for `SocketAddress` --- CMakePresets.json | 21 +++ init.d/ocvsmd.toml | 3 +- src/common/io/socket_address.cpp | 32 +++- src/common/io/socket_address.hpp | 3 + test/common/CMakeLists.txt | 1 + test/common/io/test_socket_address.cpp | 247 +++++++++++++++++++++++++ 6 files changed, 297 insertions(+), 10 deletions(-) create mode 100644 test/common/io/test_socket_address.cpp diff --git a/CMakePresets.json b/CMakePresets.json index f7d7bf3..7289cc4 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -49,6 +49,20 @@ "config-linux" ] }, + { + "name": "OCVSMD-Linux-Coverage", + "displayName": "Linux OCVSMD (Coverage)", + "description": "Configures OCVSMD for Linux with coverage.", + "inherits": [ + "config-common", + "config-linux" + ], + "cacheVariables": { + "CMAKE_C_FLAGS": "--coverage", + "CMAKE_CXX_FLAGS": "--coverage", + "NO_STATIC_ANALYSIS": "ON" + } + }, { "name": "OCVSMD-BSD", "displayName": "BSD OCVSMD", @@ -71,6 +85,13 @@ "configurePreset": "OCVSMD-Linux", "configuration": "Debug" }, + { + "name": "OCVSMD-Linux-Debug-Coverage", + "displayName": "Linux OCVSMD (Debug, Coverage)", + "description": "Builds OCVSMD for Linux with coverage", + "configurePreset": "OCVSMD-Linux-Coverage", + "configuration": "Debug" + }, { "name": "OCVSMD-Linux-Release", "displayName": "Linux OCVSMD (Release)", diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index ed24b61..a783d99 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -22,7 +22,8 @@ interfaces = [ # Connection strings for the IPC server. # Currently, only one connection is supported. # Supported formats: -# - 'tcp://:' +# - 'tcp://:' +# - 'tcp://[]:' # - 'unix:' # - 'unix-abstract:' (linux only) connections = [ diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 7e0050f..fe9f2b6 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace ocvsmd { @@ -35,6 +36,12 @@ SocketAddress::SocketAddress() noexcept { } +std::pair SocketAddress::getRaw() const +{ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return {reinterpret_cast(&addr_storage_), addr_len_}; +} + SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, const std::uint16_t port_hint) { // Unix domain? @@ -102,7 +109,7 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con cetl::optional SocketAddress::tryParseAsUnixDomain(const std::string& str) { - if (0 != str.find_first_of("unix:")) + if (0 != str.find("unix:")) { return cetl::nullopt; } @@ -112,13 +119,15 @@ cetl::optional SocketAddress::tryParseAsUnixDom auto& result_un = reinterpret_cast(result.addr_storage_); // NOLINT result_un.sun_family = AF_UNIX; - if (path.size() >= sizeof(result_un.sun_path)) + // Reserve one byte for the null terminator. + if ((path.size() + 1) > sizeof(result_un.sun_path)) { getLogger("io")->error("Unix domain path is too long (path='{}').", str); return EINVAL; } + // NOLINTNEXTLINE(*-array-to-pointer-decay, *-no-array-decay) - std::strcpy(result_un.sun_path, path.c_str()); + std::strncpy(result_un.sun_path, path.c_str(), sizeof(result_un.sun_path)); result.addr_len_ = offsetof(sockaddr_un, sun_path) + path.size() + 1; return result; @@ -126,7 +135,7 @@ cetl::optional SocketAddress::tryParseAsUnixDom cetl::optional SocketAddress::tryParseAsAbstractUnixDomain(const std::string& str) { - if (0 != str.find_first_of("unix-abstract:")) + if (0 != str.find("unix-abstract:")) { return cetl::nullopt; } @@ -136,15 +145,19 @@ cetl::optional SocketAddress::tryParseAsAbstrac auto& result_un = reinterpret_cast(result.addr_storage_); // NOLINT result_un.sun_family = AF_UNIX; - if (path.size() >= sizeof(result_un.sun_path)) + // Reserve +1 byte for the null terminator. Not required for abstract domain but it is harmless. + if ((path.size() + 1) > (sizeof(result_un.sun_path) - 1)) // `-1` b/c path starts at `[1]` (see `memcpy` below). { getLogger("io")->error("Unix domain path is too long (path='{}').", str); return EINVAL; } + + result_un.sun_path[0] = '\0'; + // `memcpy` (instead of `strcpy`) b/c `path` is allowed to contain null characters. // NOLINTNEXTLINE(*-array-to-pointer-decay, *-no-array-decay, *-pointer-arithmetic) - std::memcpy(result_un.sun_path + 1, path.c_str(), path.size() + 1); + std::memcpy(&result_un.sun_path[1], path.c_str(), path.size() + 1); - result.addr_len_ = offsetof(sockaddr_un, sun_path) + path.size() + 1; + result.addr_len_ = offsetof(sockaddr_un, sun_path) + path.size() + 1; // include prefix null byte return result; } @@ -164,11 +177,12 @@ int SocketAddress::extractFamilyHostAndPort(const std::string& str, std::string& getLogger("io")->error("Invalid IPv6 address; unclosed '[' (addr='{}').", str); return AF_UNSPEC; } - host = str.substr(1, end_bracket_pos); + host = str.substr(1, end_bracket_pos - 1); if (str.size() > end_bracket_pos + 1) { - if (0 != str.find_first_of(':', end_bracket_pos + 1)) + const auto expected_colon_pos = end_bracket_pos + 1; + if (str[expected_colon_pos] != ':') { getLogger("io")->error("Invalid IPv6 address; expected port suffix after ']': (addr='{}').", str); return AF_UNSPEC; diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index 8feb452..dc5a587 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace ocvsmd { @@ -34,6 +35,8 @@ class SocketAddress }; static ParseResult::Var parse(const std::string& str, const std::uint16_t port_hint); + std::pair getRaw() const; + private: SocketAddress() noexcept; diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index 1613b23..359e9b1 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -7,6 +7,7 @@ cmake_minimum_required(VERSION 3.27) add_executable(common_tests main.cpp + io/test_socket_address.cpp ipc/test_client_router.cpp ipc/test_server_router.cpp ) diff --git a/test/common/io/test_socket_address.cpp b/test/common/io/test_socket_address.cpp new file mode 100644 index 0000000..3ace48d --- /dev/null +++ b/test/common/io/test_socket_address.cpp @@ -0,0 +1,247 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "io/socket_address.hpp" + +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace +{ + +using namespace ocvsmd::common::io; // NOLINT This our main concern here in the unit tests. + +using testing::_; +using testing::VariantWith; + +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +class TestSocketAddress : public testing::Test +{}; + +// MARK: - Tests: + +TEST_F(TestSocketAddress, parse_unix_domain) +{ + using Result = SocketAddress::ParseResult; + + { + const std::string test_path = "/tmp/ocvsmd.sock"; + auto maybe_socket_addr = SocketAddress::parse("unix:" + test_path, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + + const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_un->sun_family, AF_UNIX); + EXPECT_THAT(addr_un->sun_path, test_path); + } + + // try max possible path length + { + const std::string max_path(108 - 1, 'x'); + auto maybe_socket_addr = SocketAddress::parse("unix:" + max_path, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + + const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_un->sun_family, AF_UNIX); + EXPECT_THAT(addr_un->sun_path, max_path); + } + + // try beyond max possible path length + { + const std::string too_long_path(108, 'x'); + auto maybe_socket_addr = SocketAddress::parse("unix:" + too_long_path, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); + } +} + +TEST_F(TestSocketAddress, parse_abstract_unix_domain) +{ + using Result = SocketAddress::ParseResult; + + { + const std::string test_path = "com.example.ocvsmd"; + auto maybe_socket_addr = SocketAddress::parse("unix-abstract:" + test_path, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_un->sun_family, AF_UNIX); + EXPECT_THAT(addr_un->sun_path[0], '\0'); + EXPECT_THAT(addr_un->sun_path + 1, test_path); // NOLINT + } + + // try with \0 in the path + { + const std::string test_path{"com\0example\0ocvsmd", 18}; // NOLINT + auto maybe_socket_addr = SocketAddress::parse("unix-abstract:" + test_path, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_un->sun_family, AF_UNIX); + EXPECT_THAT(addr_un->sun_path[0], '\0'); + EXPECT_TRUE(0 == std::memcmp(addr_un->sun_path + 1, test_path.data(), test_path.size() + 1)); // NOLINT + } + + // try max possible path length + { + const std::string max_path(108 - 2, 'x'); + auto maybe_socket_addr = SocketAddress::parse("unix-abstract:" + max_path, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + + const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_un->sun_family, AF_UNIX); + EXPECT_THAT(addr_un->sun_path[0], '\0'); + EXPECT_THAT(addr_un->sun_path + 1, max_path); // NOLINT + } + + // try beyond max possible path length + { + const std::string too_long_path(108 - 1, 'x'); + auto maybe_socket_addr = SocketAddress::parse("unix-abstract:" + too_long_path, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); + } +} + +TEST_F(TestSocketAddress, parse_ipv4) +{ + using Result = SocketAddress::ParseResult; + + { + const std::string test_addr = "127.0.0.1"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_in = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_in->sin_family, AF_INET); + EXPECT_THAT(ntohs(addr_in->sin_port), 0x1234); + EXPECT_THAT(ntohl(addr_in->sin_addr.s_addr), 0x7F000001); + } + + // try with port + { + const std::string test_addr = "192.168.1.123:8080"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 80); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_in = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_in->sin_family, AF_INET); + EXPECT_THAT(ntohs(addr_in->sin_port), 8080); + EXPECT_THAT(ntohl(addr_in->sin_addr.s_addr), 0xC0A8017B); + } + + // try invalid ip + { + const std::string test_addr = "127.0.0.256"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 80); + ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); + } + + // try unsupported + { + const std::string test_addr = "localhost"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 80); + ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); + } +} + +TEST_F(TestSocketAddress, parse_ipv6) +{ + using Result = SocketAddress::ParseResult; + + { + const std::string test_addr = "::1"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_in6->sin6_family, AF_INET6); + EXPECT_THAT(ntohs(addr_in6->sin6_port), 0x1234); + EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // + testing::ElementsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)); + } + + // try with port + { + const std::string test_addr = "[2001:db8::1]:8080"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 0); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_in6->sin6_family, AF_INET6); + EXPECT_THAT(ntohs(addr_in6->sin6_port), 8080); + EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // + testing::ElementsAre(0x20, 0x01, 0x0D, 0xB8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)); + } + + // try invalid + { + // missing closing bracket + ASSERT_THAT(SocketAddress::parse("[::1", 0), VariantWith(EINVAL)); + + // missing colon after bracket + ASSERT_THAT(SocketAddress::parse("[::1]8080", 0), VariantWith(EINVAL)); + + // invalid port number + ASSERT_THAT(SocketAddress::parse("[::1]:80_80", 0), VariantWith(EINVAL)); + + // too big port number + ASSERT_THAT(SocketAddress::parse("[::1]:65536", 0), VariantWith(EINVAL)); + } +} + +TEST_F(TestSocketAddress, parse_wildcard) +{ + using Result = SocketAddress::ParseResult; + + { + const std::string test_addr = "*"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_in6->sin6_family, AF_INET6); + EXPECT_THAT(ntohs(addr_in6->sin6_port), 0x1234); + EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // + testing::ElementsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); + } + + // try with port + { + const std::string test_addr = "*:8080"; + auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); + ASSERT_THAT(maybe_socket_addr, VariantWith(_)); + auto socket_address = cetl::get(maybe_socket_addr); + auto raw_address_and_len = socket_address.getRaw(); + const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_THAT(addr_in6->sin6_family, AF_INET6); + EXPECT_THAT(ntohs(addr_in6->sin6_port), 8080); + EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // + testing::ElementsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); + } +} + +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +} // namespace From 479f78af89df40e223b1aa630f15e78dc4bc9f08 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Feb 2025 15:16:33 +0200 Subject: [PATCH 102/156] fix build --- init.d/ocvsmd.toml | 1 + src/common/io/socket_address.cpp | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index a783d99..72a3873 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -22,6 +22,7 @@ interfaces = [ # Connection strings for the IPC server. # Currently, only one connection is supported. # Supported formats: +# - 'tcp://*:' # - 'tcp://:' # - 'tcp://[]:' # - 'unix:' diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index fe9f2b6..9e763da 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -109,7 +110,7 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con cetl::optional SocketAddress::tryParseAsUnixDomain(const std::string& str) { - if (0 != str.find("unix:")) + if (0 != str.find("unix:")) // NOLINT(modernize-use-starts-ends-with) { return cetl::nullopt; } @@ -135,7 +136,7 @@ cetl::optional SocketAddress::tryParseAsUnixDom cetl::optional SocketAddress::tryParseAsAbstractUnixDomain(const std::string& str) { - if (0 != str.find("unix-abstract:")) + if (0 != str.find("unix-abstract:")) // NOLINT(modernize-use-starts-ends-with) { return cetl::nullopt; } From 58e92db081a78acdcfb516c97ce00a1e3837447b Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Feb 2025 16:25:18 +0200 Subject: [PATCH 103/156] added `socket` and `bind` methods --- src/common/io/io.hpp | 5 ++ src/common/io/socket_address.cpp | 91 +++++++++++++++++++++++++++++--- src/common/io/socket_address.hpp | 40 +++++++++++++- 3 files changed, 127 insertions(+), 9 deletions(-) diff --git a/src/common/io/io.hpp b/src/common/io/io.hpp index 74eeaef..a3e8b36 100644 --- a/src/common/io/io.hpp +++ b/src/common/io/io.hpp @@ -52,6 +52,11 @@ class OwnFd final OwnFd(const OwnFd&) = delete; OwnFd& operator=(const OwnFd&) = delete; + explicit operator int() const + { + return fd_; + } + ~OwnFd(); private: diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 9e763da..59d704d 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -5,7 +5,9 @@ #include "socket_address.hpp" +#include "io.hpp" #include "logging.hpp" +#include "ocvsmd/platform/posix_utils.hpp" #include @@ -17,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -37,10 +40,82 @@ SocketAddress::SocketAddress() noexcept { } -std::pair SocketAddress::getRaw() const +std::pair SocketAddress::getRaw() const noexcept { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) - return {reinterpret_cast(&addr_storage_), addr_len_}; + return {&asGenericAddr(), addr_len_}; +} + +SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const +{ + const bool is_stream = (SOCK_STREAM == type); + uint socket_type = type; +#if __linux__ + socket_type |= static_cast(SOCK_NONBLOCK); + socket_type |= static_cast(SOCK_CLOEXEC); +#endif + + OwnFd out_fd; + + const auto& addr_generic = asGenericAddr(); + if (auto err = platform::posixSyscallError([this, socket_type, &addr_generic, &out_fd] { + // + const int fd = ::socket(addr_generic.sa_family, static_cast(socket_type), 0); + if (fd != -1) + { + out_fd = OwnFd{fd}; + } + return fd; + })) + { + getLogger("io")->error("Failed to create socket: {}.", std::strerror(err)); + return err; + } + + // Disable Nagle's algorithm for TCP sockets, so that our small IPC packets are sent immediately. + // + if (is_stream && ((addr_generic.sa_family == AF_INET) || (addr_generic.sa_family == AF_INET6))) + { + if (auto err = platform::posixSyscallError([&out_fd] { + // + constexpr int enable = 1; + return ::setsockopt(static_cast(out_fd), IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); + })) + { + getLogger("io")->error("Failed to set TCP_NODELAY=1: {}.", std::strerror(err)); + return err; + } + } + + return out_fd; +} + +int SocketAddress::bind(const OwnFd& socket_fd) const +{ + const int raw_fd = static_cast(socket_fd); + + // Disable IPv6-only mode for dual-stack sockets (aka wildcard). + if (is_wildcard_) + { + if (auto err = platform::posixSyscallError([raw_fd] { + // + constexpr int disable = 0; + return ::setsockopt(raw_fd, IPPROTO_TCP, IPV6_V6ONLY, &disable, sizeof(disable)); + })) + { + getLogger("io")->error("Failed to set IPV6_V6ONLY=0: {}.", std::strerror(err)); + return err; + } + } + + const auto err = platform::posixSyscallError([this, raw_fd] { + // + return ::bind(raw_fd, &asGenericAddr(), addr_len_); + }); + if (err != 0) + { + getLogger("io")->error("Failed to set IPV6_V6ONLY=0: {}.", std::strerror(err)); + } + return err; } SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, const std::uint16_t port_hint) @@ -76,7 +151,7 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con void* addr_target = nullptr; if (family == AF_INET6) { - auto& result_inet6 = reinterpret_cast(result.addr_storage_); // NOLINT + auto& result_inet6 = result.asInet6Addr(); result.addr_len_ = sizeof(result_inet6); result_inet6.sin6_family = AF_INET6; result_inet6.sin6_port = htons(port); @@ -84,7 +159,7 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con } else { - auto& result_inet4 = reinterpret_cast(result.addr_storage_); // NOLINT + auto& result_inet4 = result.asInetAddr(); result.addr_len_ = sizeof(result_inet4); result_inet4.sin_family = AF_INET; result_inet4.sin_port = htons(port); @@ -117,7 +192,7 @@ cetl::optional SocketAddress::tryParseAsUnixDom const auto path = str.substr(std::strlen("unix:")); SocketAddress result{}; - auto& result_un = reinterpret_cast(result.addr_storage_); // NOLINT + auto& result_un = result.asUnixAddr(); result_un.sun_family = AF_UNIX; // Reserve one byte for the null terminator. @@ -143,7 +218,7 @@ cetl::optional SocketAddress::tryParseAsAbstrac const auto path = str.substr(std::strlen("unix-abstract:")); SocketAddress result{}; - auto& result_un = reinterpret_cast(result.addr_storage_); // NOLINT + auto& result_un = result.asUnixAddr(); result_un.sun_family = AF_UNIX; // Reserve +1 byte for the null terminator. Not required for abstract domain but it is harmless. @@ -248,7 +323,7 @@ cetl::optional SocketAddress::tryParseAsWil SocketAddress result{}; result.is_wildcard_ = true; - auto& result_inet6 = reinterpret_cast(result.addr_storage_); // NOLINT + auto& result_inet6 = result.asInet6Addr(); result_inet6.sin6_port = htons(port); result.addr_len_ = sizeof(result_inet6); diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index dc5a587..5420b3d 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -11,8 +11,10 @@ #include #include +#include #include #include +#include #include namespace ocvsmd @@ -35,7 +37,17 @@ class SocketAddress }; static ParseResult::Var parse(const std::string& str, const std::uint16_t port_hint); - std::pair getRaw() const; + std::pair getRaw() const noexcept; + + struct SocketResult + { + using Failure = int; // aka errno + using Success = OwnFd; + using Var = cetl::variant; + }; + SocketResult::Var socket(const int type) const; + + int bind(const OwnFd& socket_fd) const; private: SocketAddress() noexcept; @@ -45,6 +57,32 @@ class SocketAddress static int extractFamilyHostAndPort(const std::string& str, std::string& host, std::uint16_t& port); static cetl::optional tryParseAsWildcard(const std::string& host, const std::uint16_t port); + sockaddr& asGenericAddr() + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } + const sockaddr& asGenericAddr() const + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } + sockaddr_un& asUnixAddr() + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } + sockaddr_in& asInetAddr() + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } + sockaddr_in6& asInet6Addr() + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } + bool is_wildcard_; socklen_t addr_len_; sockaddr_storage addr_storage_; From b49195d8e84d45304b379fc683154bf1b779ffa1 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Feb 2025 16:42:31 +0200 Subject: [PATCH 104/156] switch off "misc-include-cleaner" rule --- .clang-tidy | 1 + 1 file changed, 1 insertion(+) diff --git a/.clang-tidy b/.clang-tidy index 99d9c05..899da63 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -27,6 +27,7 @@ Checks: >- -readability-identifier-length, -*-use-trailing-return-type, -*-named-parameter, + -misc-include-cleaner, CheckOptions: - key: readability-function-cognitive-complexity.Threshold value: '90' From 067157ef533fe3048c47c8aeab40a3e44747ce0e Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Feb 2025 21:18:22 +0200 Subject: [PATCH 105/156] socket clients now use `SocketAddress` and `OwnFd` --- include/ocvsmd/sdk/daemon.hpp | 5 +- src/cli/main.cpp | 8 +- src/common/CMakeLists.txt | 8 +- src/common/io/io.cpp | 9 +- src/common/io/io.hpp | 2 + src/common/io/socket_address.cpp | 30 ++++++- src/common/io/socket_address.hpp | 10 ++- src/common/ipc/pipe/client_context.hpp | 16 ++-- src/common/ipc/pipe/net_socket_client.cpp | 65 -------------- src/common/ipc/pipe/net_socket_client.hpp | 53 ----------- src/common/ipc/pipe/net_socket_server.cpp | 57 ------------ src/common/ipc/pipe/net_socket_server.hpp | 49 ---------- src/common/ipc/pipe/server_pipe.hpp | 2 + src/common/ipc/pipe/socket_base.cpp | 16 ++-- src/common/ipc/pipe/socket_base.hpp | 3 +- ...cket_client_base.cpp => socket_client.cpp} | 70 +++++++++------ ...cket_client_base.hpp => socket_client.hpp} | 40 ++++----- ...cket_server_base.cpp => socket_server.cpp} | 90 ++++++++++--------- ...cket_server_base.hpp => socket_server.hpp} | 41 ++++----- src/common/ipc/pipe/unix_socket_client.cpp | 64 ------------- src/common/ipc/pipe/unix_socket_client.hpp | 52 ----------- src/common/ipc/pipe/unix_socket_server.cpp | 65 -------------- src/common/ipc/pipe/unix_socket_server.hpp | 51 ----------- src/daemon/engine/config.cpp | 5 ++ src/daemon/engine/config.hpp | 2 + src/daemon/engine/engine.cpp | 52 ++++++++--- src/sdk/daemon.cpp | 46 ++++++---- test/common/io/test_socket_address.cpp | 7 +- 28 files changed, 292 insertions(+), 626 deletions(-) delete mode 100644 src/common/ipc/pipe/net_socket_client.cpp delete mode 100644 src/common/ipc/pipe/net_socket_client.hpp delete mode 100644 src/common/ipc/pipe/net_socket_server.cpp delete mode 100644 src/common/ipc/pipe/net_socket_server.hpp rename src/common/ipc/pipe/{socket_client_base.cpp => socket_client.cpp} (65%) rename src/common/ipc/pipe/{socket_client_base.hpp => socket_client.hpp} (53%) rename src/common/ipc/pipe/{socket_server_base.cpp => socket_server.cpp} (64%) rename src/common/ipc/pipe/{socket_server_base.hpp => socket_server.hpp} (58%) delete mode 100644 src/common/ipc/pipe/unix_socket_client.cpp delete mode 100644 src/common/ipc/pipe/unix_socket_client.hpp delete mode 100644 src/common/ipc/pipe/unix_socket_server.cpp delete mode 100644 src/common/ipc/pipe/unix_socket_server.hpp diff --git a/include/ocvsmd/sdk/daemon.hpp b/include/ocvsmd/sdk/daemon.hpp index d13b939..81a5fb3 100644 --- a/include/ocvsmd/sdk/daemon.hpp +++ b/include/ocvsmd/sdk/daemon.hpp @@ -13,6 +13,7 @@ #include #include +#include namespace ocvsmd { @@ -26,7 +27,9 @@ class Daemon public: using Ptr = std::shared_ptr; - CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor); + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, + libcyphal::IExecutor& executor, + const std::string& connection); Daemon(Daemon&&) = delete; Daemon(const Daemon&) = delete; diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 1c48606..09d6f6a 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -69,7 +69,13 @@ int main(const int argc, const char** const argv) auto& memory = *cetl::pmr::new_delete_resource(); Executor executor; - const auto daemon = ocvsmd::sdk::Daemon::make(memory, executor); + std::string ipc_connection = "unix-abstract:org.opencyphal.ocvsmd.ipc"; + if (const auto* const env_connection_str = std::getenv("OCVSMD_CONNECTION")) + { + ipc_connection = env_connection_str; + } + + const auto daemon = ocvsmd::sdk::Daemon::make(memory, executor, ipc_connection); if (!daemon) { spdlog::critical("Failed to create daemon."); diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index f76b24d..fd28879 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -23,13 +23,9 @@ add_library(ocvsmd_common io/io.cpp io/socket_address.cpp ipc/client_router.cpp - ipc/pipe/net_socket_client.cpp - ipc/pipe/net_socket_server.cpp ipc/pipe/socket_base.cpp - ipc/pipe/socket_client_base.cpp - ipc/pipe/socket_server_base.cpp - ipc/pipe/unix_socket_client.cpp - ipc/pipe/unix_socket_server.cpp + ipc/pipe/socket_client.cpp + ipc/pipe/socket_server.cpp ipc/server_router.cpp ) target_link_libraries(ocvsmd_common diff --git a/src/common/io/io.cpp b/src/common/io/io.cpp index 79cbc42..d6c8109 100644 --- a/src/common/io/io.cpp +++ b/src/common/io/io.cpp @@ -18,7 +18,7 @@ namespace common namespace io { -OwnFd::~OwnFd() +void OwnFd::reset() noexcept { if (fd_ >= 0) { @@ -28,9 +28,16 @@ OwnFd::~OwnFd() const int err = errno; getLogger("io")->error("Failed to close file descriptor {}: {}.", fd_, std::strerror(err)); } + + fd_ = -1; } } +OwnFd::~OwnFd() +{ + reset(); +} + } // namespace io } // namespace common } // namespace ocvsmd diff --git a/src/common/io/io.hpp b/src/common/io/io.hpp index a3e8b36..fed7cef 100644 --- a/src/common/io/io.hpp +++ b/src/common/io/io.hpp @@ -57,6 +57,8 @@ class OwnFd final return fd_; } + void reset() noexcept; + ~OwnFd(); private: diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 59d704d..86fcac7 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -77,7 +77,8 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const { if (auto err = platform::posixSyscallError([&out_fd] { // - constexpr int enable = 1; + // TODO: Enable! + constexpr int enable = 0; return ::setsockopt(static_cast(out_fd), IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); })) { @@ -92,14 +93,15 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const int SocketAddress::bind(const OwnFd& socket_fd) const { const int raw_fd = static_cast(socket_fd); + CETL_DEBUG_ASSERT(raw_fd != -1, ""); // Disable IPv6-only mode for dual-stack sockets (aka wildcard). if (is_wildcard_) { if (auto err = platform::posixSyscallError([raw_fd] { // - constexpr int disable = 0; - return ::setsockopt(raw_fd, IPPROTO_TCP, IPV6_V6ONLY, &disable, sizeof(disable)); + int disable = 0; + return ::setsockopt(raw_fd, IPPROTO_IPV6, IPV6_V6ONLY, &disable, sizeof(disable)); })) { getLogger("io")->error("Failed to set IPV6_V6ONLY=0: {}.", std::strerror(err)); @@ -118,6 +120,28 @@ int SocketAddress::bind(const OwnFd& socket_fd) const return err; } +int SocketAddress::connect(const OwnFd& socket_fd) const +{ + const int raw_fd = static_cast(socket_fd); + CETL_DEBUG_ASSERT(raw_fd != -1, ""); + + const auto err = platform::posixSyscallError([this, raw_fd] { + // + return ::connect(raw_fd, &asGenericAddr(), addr_len_); + }); + switch (err) + { + case 0: + case EINPROGRESS: { + return 0; + } + default: { + getLogger("io")->error("Failed to connect to server: {}.", std::strerror(err)); + return err; + } + } +} + SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, const std::uint16_t port_hint) { // Unix domain? diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index 5420b3d..e06408b 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -24,11 +24,9 @@ namespace common namespace io { -class SocketAddress +class SocketAddress final { public: - OwnFd socket() const; - struct ParseResult { using Failure = int; // aka errno @@ -39,6 +37,11 @@ class SocketAddress std::pair getRaw() const noexcept; + bool isUnix() const noexcept + { + return asGenericAddr().sa_family == AF_UNIX; + } + struct SocketResult { using Failure = int; // aka errno @@ -48,6 +51,7 @@ class SocketAddress SocketResult::Var socket(const int type) const; int bind(const OwnFd& socket_fd) const; + int connect(const OwnFd& socket_fd) const; private: SocketAddress() noexcept; diff --git a/src/common/ipc/pipe/client_context.hpp b/src/common/ipc/pipe/client_context.hpp index c6b40f7..25dbc00 100644 --- a/src/common/ipc/pipe/client_context.hpp +++ b/src/common/ipc/pipe/client_context.hpp @@ -6,6 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_CLIENT_CONTEXT_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_CLIENT_CONTEXT_HPP_INCLUDED +#include "io/io.hpp" #include "logging.hpp" #include "ocvsmd/platform/posix_utils.hpp" #include "server_pipe.hpp" @@ -32,24 +33,19 @@ class ClientContext final public: using Ptr = std::unique_ptr; - ClientContext(const ServerPipe::ClientId id, const int fd, Logger& logger) + ClientContext(const ServerPipe::ClientId id, io::OwnFd&& fd, Logger& logger) : id_{id} , logger_{logger} { - CETL_DEBUG_ASSERT(fd != -1, ""); + CETL_DEBUG_ASSERT(static_cast(fd) != -1, ""); - state_.fd = fd; - logger_.trace("ClientContext(fd={}, id={}).", fd, id_); + logger_.trace("ClientContext(fd={}, id={}).", static_cast(fd), id_); + state_.fd = std::move(fd); } ~ClientContext() { - logger_.trace("~ClientContext(fd={}, id={}).", state_.fd, id_); - - platform::posixSyscallError([this] { - // - return ::close(state_.fd); - }); + logger_.trace("~ClientContext(fd={}, id={}).", static_cast(state_.fd), id_); } ClientContext(const ClientContext&) = delete; diff --git a/src/common/ipc/pipe/net_socket_client.cpp b/src/common/ipc/pipe/net_socket_client.cpp deleted file mode 100644 index 16e8cec..0000000 --- a/src/common/ipc/pipe/net_socket_client.cpp +++ /dev/null @@ -1,65 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#include "net_socket_client.hpp" - -#include "ocvsmd/platform/posix_utils.hpp" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -NetSocketClient::NetSocketClient(libcyphal::IExecutor& executor, std::string server_ip, const int server_port) - : Base{executor} - , server_ip_{std::move(server_ip)} - , server_port_{server_port} -{ -} - -int NetSocketClient::makeSocketHandle(int& out_fd) -{ - CETL_DEBUG_ASSERT(out_fd == -1, ""); - - if (const auto err = platform::posixSyscallError([&out_fd] { - // - return out_fd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); - })) - { - logger().error("Failed to create socket: {}.", std::strerror(err)); - return err; - } - - sockaddr_in addr{}; - addr.sin_family = AF_INET; - addr.sin_port = htons(server_port_); - if (inet_pton(AF_INET, server_ip_.c_str(), &addr.sin_addr) <= 0) - { - logger().error("Invalid server IP address: {}.", server_ip_); - return EINVAL; - } - - return connectSocket(out_fd, &addr, sizeof(addr)); -} - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd diff --git a/src/common/ipc/pipe/net_socket_client.hpp b/src/common/ipc/pipe/net_socket_client.hpp deleted file mode 100644 index 0f7e91a..0000000 --- a/src/common/ipc/pipe/net_socket_client.hpp +++ /dev/null @@ -1,53 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#ifndef OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_CLIENT_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_CLIENT_HPP_INCLUDED - -#include "client_pipe.hpp" -#include "socket_client_base.hpp" - -#include -#include - -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -class NetSocketClient final : public SocketClientBase -{ -public: - NetSocketClient(libcyphal::IExecutor& executor, std::string server_ip, const int server_port); - - NetSocketClient(const NetSocketClient&) = delete; - NetSocketClient(NetSocketClient&&) noexcept = delete; - NetSocketClient& operator=(const NetSocketClient&) = delete; - NetSocketClient& operator=(NetSocketClient&&) noexcept = delete; - - ~NetSocketClient() override = default; - -private: - using Base = SocketClientBase; - - CETL_NODISCARD int makeSocketHandle(int& out_fd) override; - - const std::string server_ip_; - const int server_port_; - -}; // NetSocketClient - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd - -#endif // OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_CLIENT_HPP_INCLUDED diff --git a/src/common/ipc/pipe/net_socket_server.cpp b/src/common/ipc/pipe/net_socket_server.cpp deleted file mode 100644 index 7509541..0000000 --- a/src/common/ipc/pipe/net_socket_server.cpp +++ /dev/null @@ -1,57 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#include "net_socket_server.hpp" - -#include "logging.hpp" -#include "ocvsmd/platform/posix_utils.hpp" - -#include -#include - -#include -#include -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -NetSocketServer::NetSocketServer(libcyphal::IExecutor& executor, const int server_port) - : Base{executor} - , server_port_{server_port} -{ -} - -int NetSocketServer::makeSocketHandle(int& out_fd) -{ - CETL_DEBUG_ASSERT(out_fd == -1, ""); - - if (const auto err = platform::posixSyscallError([&out_fd] { - // - return out_fd = ::socket(AF_INET, SOCK_STREAM, 0); - })) - { - logger().error("Failed to create server socket: {}.", std::strerror(err)); - return err; - } - - sockaddr_in addr{}; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = htons(server_port_); - - return bindSocket(out_fd, &addr, sizeof(addr)); -} - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd diff --git a/src/common/ipc/pipe/net_socket_server.hpp b/src/common/ipc/pipe/net_socket_server.hpp deleted file mode 100644 index 1c1f2fa..0000000 --- a/src/common/ipc/pipe/net_socket_server.hpp +++ /dev/null @@ -1,49 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#ifndef OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_SERVER_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_SERVER_HPP_INCLUDED - -#include "socket_server_base.hpp" - -#include -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -class NetSocketServer final : public SocketServerBase -{ -public: - NetSocketServer(libcyphal::IExecutor& executor, const int server_port); - - NetSocketServer(const NetSocketServer&) = delete; - NetSocketServer(NetSocketServer&&) noexcept = delete; - NetSocketServer& operator=(const NetSocketServer&) = delete; - NetSocketServer& operator=(NetSocketServer&&) noexcept = delete; - - ~NetSocketServer() override = default; - -private: - using Base = SocketServerBase; - - CETL_NODISCARD int makeSocketHandle(int& out_fd) override; - - const int server_port_; - -}; // NetSocketServer - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd - -#endif // OCVSMD_COMMON_IPC_PIPE_NET_SOCKET_SERVER_HPP_INCLUDED diff --git a/src/common/ipc/pipe/server_pipe.hpp b/src/common/ipc/pipe/server_pipe.hpp index 1346f26..fd6d548 100644 --- a/src/common/ipc/pipe/server_pipe.hpp +++ b/src/common/ipc/pipe/server_pipe.hpp @@ -6,10 +6,12 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_SERVER_PIPE_HPP_INCLUDED +#include "io/socket_address.hpp" #include "ipc/ipc_types.hpp" #include #include +#include #include #include diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp index 54f6083..f550789 100644 --- a/src/common/ipc/pipe/socket_base.cpp +++ b/src/common/ipc/pipe/socket_base.cpp @@ -60,7 +60,7 @@ int SocketBase::send(const State& state, const Payloads payloads) if (const int err = platform::posixSyscallError([total_size, &state] { // const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; - return ::write(state.fd, &msg_header, sizeof(msg_header)); + return ::write(static_cast(state.fd), &msg_header, sizeof(msg_header)); })) { return err; @@ -72,7 +72,7 @@ int SocketBase::send(const State& state, const Payloads payloads) { if (const int err = platform::posixSyscallError([payload, &state] { // - return ::write(state.fd, payload.data(), payload.size()); + return ::write(static_cast(state.fd), payload.data(), payload.size()); })) { return err; @@ -91,14 +91,16 @@ int SocketBase::receiveMessage(State& state, std::function&& actio ssize_t bytes_read = 0; if (const auto err = platform::posixSyscallError([&state, &msg_header, &bytes_read] { // - return bytes_read = ::read(state.fd, &msg_header, sizeof(msg_header)); + return bytes_read = ::read(static_cast(state.fd), &msg_header, sizeof(msg_header)); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { return 0; // no data available yet } - logger_->error("Failed to read message header (fd={}): {}.", state.fd, std::strerror(err)); + logger_->error("Failed to read message header (fd={}): {}.", + static_cast(state.fd), + std::strerror(err)); return err; } @@ -127,14 +129,16 @@ int SocketBase::receiveMessage(State& state, std::function&& actio ssize_t read = 0; if (const auto err = platform::posixSyscallError([this, &state, buf_span, &read] { // - return read = ::read(state.fd, buf_span.data(), buf_span.size()); + return read = ::read(static_cast(state.fd), buf_span.data(), buf_span.size()); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { return 0; // no data available } - logger_->error("Failed to read message payload (fd={}): {}.", state.fd, std::strerror(err)); + logger_->error("Failed to read message payload (fd={}): {}.", + static_cast(state.fd), + std::strerror(err)); return err; } if (read != buf_span.size()) diff --git a/src/common/ipc/pipe/socket_base.hpp b/src/common/ipc/pipe/socket_base.hpp index e731e72..c8143e4 100644 --- a/src/common/ipc/pipe/socket_base.hpp +++ b/src/common/ipc/pipe/socket_base.hpp @@ -6,6 +6,7 @@ #ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_BASE_HPP_INCLUDED #define OCVSMD_COMMON_IPC_PIPE_SOCKET_BASE_HPP_INCLUDED +#include "io/io.hpp" #include "ipc/ipc_types.hpp" #include "logging.hpp" @@ -36,7 +37,7 @@ class SocketBase Payload }; - int fd{-1}; + io::OwnFd fd{}; std::size_t read_msg_size{0}; ReadPhase read_phase{ReadPhase::Header}; diff --git a/src/common/ipc/pipe/socket_client_base.cpp b/src/common/ipc/pipe/socket_client.cpp similarity index 65% rename from src/common/ipc/pipe/socket_client_base.cpp rename to src/common/ipc/pipe/socket_client.cpp index 4b279bf..332ba56 100644 --- a/src/common/ipc/pipe/socket_client_base.cpp +++ b/src/common/ipc/pipe/socket_client.cpp @@ -3,7 +3,7 @@ // SPDX-License-Identifier: MIT // -#include "socket_client_base.hpp" +#include "socket_client.hpp" #include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" @@ -30,31 +30,21 @@ namespace ipc namespace pipe { -SocketClientBase::SocketClientBase(libcyphal::IExecutor& executor) - : posix_executor_ext_{cetl::rtti_cast(&executor)} +SocketClient::SocketClient(libcyphal::IExecutor& executor, const io::SocketAddress& address) + : socket_address_{address} + , posix_executor_ext_{cetl::rtti_cast(&executor)} { CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); } -SocketClientBase::~SocketClientBase() -{ - if (state_.fd != -1) - { - platform::posixSyscallError([this] { - // - return ::close(state_.fd); - }); - } -} - -int SocketClientBase::start(EventHandler event_handler) +int SocketClient::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(event_handler, ""); - CETL_DEBUG_ASSERT(state_.fd == -1, ""); + CETL_DEBUG_ASSERT(static_cast(state_.fd) == -1, ""); event_handler_ = std::move(event_handler); - if (const auto err = makeSocketHandle(state_.fd)) + if (const auto err = makeSocketHandle()) { logger().error("Failed to make socket handle: {}.", std::strerror(err)); return err; @@ -65,17 +55,44 @@ int SocketClientBase::start(EventHandler event_handler) // handle_connect(); }, - platform::IPosixExecutorExtension::Trigger::Writable{state_.fd}); + platform::IPosixExecutorExtension::Trigger::Writable{static_cast(state_.fd)}); + + return 0; +} + +int SocketClient::makeSocketHandle() +{ + using SocketResult = io::SocketAddress::SocketResult; + + auto maybe_socket = socket_address_.socket(SOCK_STREAM); + if (auto* const err = cetl::get_if(&maybe_socket)) + { + logger().error("Failed to create client socket: {}.", std::strerror(*err)); + return *err; + } + auto socket_fd = cetl::get(std::move(maybe_socket)); + CETL_DEBUG_ASSERT(static_cast(socket_fd) != -1, ""); + + const int err = socket_address_.connect(socket_fd); + if (err != 0) + { + if (err != EINPROGRESS) + { + logger().error("Failed to connect to server: {}.", std::strerror(err)); + return err; + } + } + state_.fd = std::move(socket_fd); return 0; } -int SocketClientBase::send(const Payloads payloads) +int SocketClient::send(const Payloads payloads) { return SocketBase::send(state_, payloads); } -int SocketClientBase::connectSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const +int SocketClient::connectSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const { if (const auto err = platform::posixSyscallError([fd, addr_ptr, addr_size] { // @@ -91,7 +108,7 @@ int SocketClientBase::connectSocket(const int fd, const void* const addr_ptr, co return 0; } -void SocketClientBase::handle_connect() +void SocketClient::handle_connect() { socket_callback_.reset(); @@ -99,7 +116,7 @@ void SocketClientBase::handle_connect() if (const auto err = platform::posixSyscallError([this, &so_error] { // socklen_t len = sizeof(so_error); - return ::getsockopt(state_.fd, SOL_SOCKET, SO_ERROR, &so_error, &len); // NOLINT(misc-include-cleaner) + return ::getsockopt(static_cast(state_.fd), SOL_SOCKET, SO_ERROR, &so_error, &len); })) { logger().warn("Failed to query socket error: {}.", std::strerror(err)); @@ -117,13 +134,13 @@ void SocketClientBase::handle_connect() // handle_receive(); }, - platform::IPosixExecutorExtension::Trigger::Readable{state_.fd}); + platform::IPosixExecutorExtension::Trigger::Readable{static_cast(state_.fd)}); state_.read_phase = State::ReadPhase::Header; event_handler_(Event::Connected{}); } -void SocketClientBase::handle_receive() +void SocketClient::handle_receive() { if (const auto err = receiveMessage(state_, [this](const auto payload) { // @@ -143,12 +160,11 @@ void SocketClientBase::handle_receive() } } -void SocketClientBase::handle_disconnect() +void SocketClient::handle_disconnect() { socket_callback_.reset(); - ::close(state_.fd); - state_.fd = -1; + state_.fd.reset(); state_.read_phase = State::ReadPhase::Header; event_handler_(Event::Disconnected{}); diff --git a/src/common/ipc/pipe/socket_client_base.hpp b/src/common/ipc/pipe/socket_client.hpp similarity index 53% rename from src/common/ipc/pipe/socket_client_base.hpp rename to src/common/ipc/pipe/socket_client.hpp index 651e257..dd1d037 100644 --- a/src/common/ipc/pipe/socket_client_base.hpp +++ b/src/common/ipc/pipe/socket_client.hpp @@ -3,10 +3,11 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_BASE_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_BASE_HPP_INCLUDED +#ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_HPP_INCLUDED #include "client_pipe.hpp" +#include "io/socket_address.hpp" #include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" #include "socket_base.hpp" @@ -25,42 +26,41 @@ namespace ipc namespace pipe { -class SocketClientBase : public SocketBase, public ClientPipe +class SocketClient final : public SocketBase, public ClientPipe { public: - SocketClientBase(const SocketClientBase&) = delete; - SocketClientBase(SocketClientBase&&) noexcept = delete; - SocketClientBase& operator=(const SocketClientBase&) = delete; - SocketClientBase& operator=(SocketClientBase&&) noexcept = delete; + SocketClient(libcyphal::IExecutor& executor, const io::SocketAddress& address); - ~SocketClientBase() override; + SocketClient(const SocketClient&) = delete; + SocketClient(SocketClient&&) noexcept = delete; + SocketClient& operator=(const SocketClient&) = delete; + SocketClient& operator=(SocketClient&&) noexcept = delete; -protected: - explicit SocketClientBase(libcyphal::IExecutor& executor); - - // ClientPipe - CETL_NODISCARD int start(EventHandler event_handler) override; - CETL_NODISCARD int send(const Payloads payloads) override; - - CETL_NODISCARD virtual int makeSocketHandle(int& out_fd) = 0; - - CETL_NODISCARD int connectSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const; + ~SocketClient() override = default; private: + int makeSocketHandle(); + int connectSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const; void handle_connect(); void handle_receive(); void handle_disconnect(); + // ClientPipe + // + CETL_NODISCARD int start(EventHandler event_handler) override; + CETL_NODISCARD int send(const Payloads payloads) override; + + io::SocketAddress socket_address_; platform::IPosixExecutorExtension* const posix_executor_ext_; State state_; libcyphal::IExecutor::Callback::Any socket_callback_; EventHandler event_handler_; -}; // SocketClientBase +}; // SocketClient } // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_BASE_HPP_INCLUDED +#endif // OCVSMD_COMMON_IPC_PIPE_SOCKET_CLIENT_HPP_INCLUDED diff --git a/src/common/ipc/pipe/socket_server_base.cpp b/src/common/ipc/pipe/socket_server.cpp similarity index 64% rename from src/common/ipc/pipe/socket_server_base.cpp rename to src/common/ipc/pipe/socket_server.cpp index 2b6a784..5452b8a 100644 --- a/src/common/ipc/pipe/socket_server_base.cpp +++ b/src/common/ipc/pipe/socket_server.cpp @@ -3,7 +3,7 @@ // SPDX-License-Identifier: MIT // -#include "socket_server_base.hpp" +#include "socket_server.hpp" #include "client_context.hpp" #include "ipc/ipc_types.hpp" @@ -38,33 +38,22 @@ constexpr int MaxConnections = 5; } // namespace -SocketServerBase::SocketServerBase(libcyphal::IExecutor& executor) - : server_fd_{-1} +SocketServer::SocketServer(libcyphal::IExecutor& executor, const io::SocketAddress& address) + : socket_address_{address} , posix_executor_ext_{cetl::rtti_cast(&executor)} , unique_client_id_counter_{0} { CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); } -SocketServerBase::~SocketServerBase() +int SocketServer::start(EventHandler event_handler) { - if (server_fd_ != -1) - { - platform::posixSyscallError([this] { - // - return ::close(server_fd_); - }); - } -} - -int SocketServerBase::start(EventHandler event_handler) -{ - CETL_DEBUG_ASSERT(server_fd_ == -1, ""); CETL_DEBUG_ASSERT(event_handler, ""); + CETL_DEBUG_ASSERT(static_cast(server_fd_) == -1, ""); event_handler_ = std::move(event_handler); - if (const auto err = makeSocketHandle(server_fd_)) + if (const auto err = makeSocketHandle()) { logger().error("Failed to make socket handle: {}.", std::strerror(err)); return err; @@ -72,7 +61,7 @@ int SocketServerBase::start(EventHandler event_handler) if (const auto err = platform::posixSyscallError([this] { // - return ::listen(server_fd_, MaxConnections); + return ::listen(static_cast(server_fd_), MaxConnections); })) { logger().error("Failed to listen on server socket: {}.", std::strerror(err)); @@ -84,66 +73,79 @@ int SocketServerBase::start(EventHandler event_handler) // handleAccept(); }, - platform::IPosixExecutorExtension::Trigger::Readable{server_fd_}); + platform::IPosixExecutorExtension::Trigger::Readable{static_cast(server_fd_)}); return 0; } -int SocketServerBase::send(const ClientId client_id, const Payloads payloads) +int SocketServer::makeSocketHandle() { - if (auto* const client_context = tryFindClientContext(client_id)) + using SocketResult = io::SocketAddress::SocketResult; + + auto maybe_socket = socket_address_.socket(SOCK_STREAM); + if (auto* const err = cetl::get_if(&maybe_socket)) { - return SocketBase::send(client_context->state(), payloads); + logger().error("Failed to create server socket: {}.", std::strerror(*err)); + return *err; } + auto socket_fd = cetl::get(std::move(maybe_socket)); + CETL_DEBUG_ASSERT(static_cast(socket_fd) != -1, ""); - logger().warn("Client context is not found (id={}).", client_id); - return EINVAL; -} - -int SocketServerBase::bindSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const -{ - if (const auto err = platform::posixSyscallError([fd, addr_ptr, addr_size] { - // - return ::bind(fd, static_cast(addr_ptr), addr_size); - })) + const int err = socket_address_.bind(socket_fd); + if (err != 0) { logger().error("Failed to bind server socket: {}.", std::strerror(err)); return err; } + + server_fd_ = std::move(socket_fd); return 0; } -void SocketServerBase::handleAccept() +int SocketServer::send(const ClientId client_id, const Payloads payloads) +{ + if (auto* const client_context = tryFindClientContext(client_id)) + { + return SocketBase::send(client_context->state(), payloads); + } + + logger().warn("Client context is not found (id={}).", client_id); + return EINVAL; +} + +void SocketServer::handleAccept() { - CETL_DEBUG_ASSERT(server_fd_ != -1, ""); + CETL_DEBUG_ASSERT(static_cast(server_fd_) != -1, ""); - int client_fd = -1; + io::OwnFd client_fd; if (const auto err = platform::posixSyscallError([this, &client_fd] { // - return client_fd = ::accept(server_fd_, nullptr, nullptr); + client_fd = io::OwnFd{::accept(static_cast(server_fd_), nullptr, nullptr)}; + return static_cast(client_fd); })) { logger().warn("Failed to accept client connection: {}.", std::strerror(err)); return; } - CETL_DEBUG_ASSERT(client_fd != -1, ""); + const int raw_fd = static_cast(client_fd); + CETL_DEBUG_ASSERT(raw_fd != -1, ""); const ClientId new_client_id = ++unique_client_id_counter_; - auto client_context = std::make_unique(new_client_id, client_fd, logger()); + auto client_context = std::make_unique(new_client_id, std::move(client_fd), logger()); // client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( [this, new_client_id](const auto&) { // handleClientRequest(new_client_id); }, - platform::IPosixExecutorExtension::Trigger::Readable{client_fd})); + platform::IPosixExecutorExtension::Trigger::Readable{raw_fd})); client_id_to_context_.emplace(new_client_id, std::move(client_context)); event_handler_(Event::Connected{new_client_id}); } -void SocketServerBase::handleClientRequest(const ClientId client_id) +void SocketServer::handleClientRequest(const ClientId client_id) { auto* const client_context = tryFindClientContext(client_id); CETL_DEBUG_ASSERT(client_context, ""); @@ -156,13 +158,15 @@ void SocketServerBase::handleClientRequest(const ClientId client_id) { if (err == -1) { - logger().debug("End of client stream - closing connection (id={}, fd={}).", client_id, state.fd); + logger().debug("End of client stream - closing connection (id={}, fd={}).", + client_id, + static_cast(state.fd)); } else { logger().warn("Failed to handle client request - closing connection (id={}, fd={}): {}.", client_id, - state.fd, + static_cast(state.fd), std::strerror(err)); } @@ -171,7 +175,7 @@ void SocketServerBase::handleClientRequest(const ClientId client_id) } } -ClientContext* SocketServerBase::tryFindClientContext(const ClientId client_id) +ClientContext* SocketServer::tryFindClientContext(const ClientId client_id) { const auto id_and_context = client_id_to_context_.find(client_id); if (id_and_context != client_id_to_context_.end()) diff --git a/src/common/ipc/pipe/socket_server_base.hpp b/src/common/ipc/pipe/socket_server.hpp similarity index 58% rename from src/common/ipc/pipe/socket_server_base.hpp rename to src/common/ipc/pipe/socket_server.hpp index 6f72b12..d495020 100644 --- a/src/common/ipc/pipe/socket_server_base.hpp +++ b/src/common/ipc/pipe/socket_server.hpp @@ -3,8 +3,8 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_BASE_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_BASE_HPP_INCLUDED +#ifndef OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_HPP_INCLUDED #include "client_context.hpp" #include "io/io.hpp" @@ -28,45 +28,42 @@ namespace ipc namespace pipe { -class SocketServerBase : public SocketBase, public ServerPipe +class SocketServer final : public SocketBase, public ServerPipe { public: - SocketServerBase(const SocketServerBase&) = delete; - SocketServerBase(SocketServerBase&&) noexcept = delete; - SocketServerBase& operator=(const SocketServerBase&) = delete; - SocketServerBase& operator=(SocketServerBase&&) noexcept = delete; + SocketServer(libcyphal::IExecutor& executor, const io::SocketAddress& address); - ~SocketServerBase() override; + SocketServer(const SocketServer&) = delete; + SocketServer(SocketServer&&) noexcept = delete; + SocketServer& operator=(const SocketServer&) = delete; + SocketServer& operator=(SocketServer&&) noexcept = delete; -protected: - explicit SocketServerBase(libcyphal::IExecutor& executor); - - // ServerPipe - // - CETL_NODISCARD int start(EventHandler event_handler) override; - CETL_NODISCARD int send(const ClientId client_id, const Payloads payloads) override; - - CETL_NODISCARD virtual int makeSocketHandle(int& out_fd) = 0; - - CETL_NODISCARD int bindSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const; + ~SocketServer() override = default; private: + int makeSocketHandle(); void handleAccept(); void handleClientRequest(const ClientId client_id); ClientContext* tryFindClientContext(const ClientId client_id); - int server_fd_; + // ServerPipe + // + CETL_NODISCARD int start(EventHandler event_handler) override; + CETL_NODISCARD int send(const ClientId client_id, const Payloads payloads) override; + + io::OwnFd server_fd_; + io::SocketAddress socket_address_; platform::IPosixExecutorExtension* const posix_executor_ext_; ClientId unique_client_id_counter_; EventHandler event_handler_; libcyphal::IExecutor::Callback::Any accept_callback_; std::unordered_map client_id_to_context_; -}; // SocketServerBase +}; // SocketServer } // namespace pipe } // namespace ipc } // namespace common } // namespace ocvsmd -#endif // OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_BASE_HPP_INCLUDED +#endif // OCVSMD_COMMON_IPC_PIPE_SOCKET_SERVER_HPP_INCLUDED diff --git a/src/common/ipc/pipe/unix_socket_client.cpp b/src/common/ipc/pipe/unix_socket_client.cpp deleted file mode 100644 index 5728b3d..0000000 --- a/src/common/ipc/pipe/unix_socket_client.cpp +++ /dev/null @@ -1,64 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#include "unix_socket_client.hpp" - -#include "ocvsmd/platform/posix_utils.hpp" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -UnixSocketClient::UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path) - : Base{executor} - , socket_path_{std::move(socket_path)} -{ -} - -int UnixSocketClient::makeSocketHandle(int& out_fd) -{ - CETL_DEBUG_ASSERT(out_fd == -1, ""); - - if (const auto err = platform::posixSyscallError([&out_fd] { - // - return out_fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0); - })) - { - logger().error("Failed to create socket: {}.", std::strerror(err)); - return err; - } - - sockaddr_un addr{}; - addr.sun_family = AF_UNIX; - const std::string abstract_socket_path = '\0' + socket_path_; - CETL_DEBUG_ASSERT(abstract_socket_path.size() <= sizeof(addr.sun_path), ""); - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) - std::memcpy(addr.sun_path, - abstract_socket_path.c_str(), - std::min(sizeof(addr.sun_path), abstract_socket_path.size())); - - return connectSocket(out_fd, &addr, offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); -} - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd diff --git a/src/common/ipc/pipe/unix_socket_client.hpp b/src/common/ipc/pipe/unix_socket_client.hpp deleted file mode 100644 index 5a62193..0000000 --- a/src/common/ipc/pipe/unix_socket_client.hpp +++ /dev/null @@ -1,52 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED - -#include "client_pipe.hpp" -#include "socket_client_base.hpp" - -#include -#include - -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -class UnixSocketClient final : public SocketClientBase -{ -public: - UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path); - - UnixSocketClient(const UnixSocketClient&) = delete; - UnixSocketClient(UnixSocketClient&&) noexcept = delete; - UnixSocketClient& operator=(const UnixSocketClient&) = delete; - UnixSocketClient& operator=(UnixSocketClient&&) noexcept = delete; - - ~UnixSocketClient() override = default; - -private: - using Base = SocketClientBase; - - CETL_NODISCARD int makeSocketHandle(int& out_fd) override; - - const std::string socket_path_; - -}; // UnixSocketClient - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd - -#endif // OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_CLIENT_HPP_INCLUDED diff --git a/src/common/ipc/pipe/unix_socket_server.cpp b/src/common/ipc/pipe/unix_socket_server.cpp deleted file mode 100644 index 03ae9b9..0000000 --- a/src/common/ipc/pipe/unix_socket_server.cpp +++ /dev/null @@ -1,65 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#include "unix_socket_server.hpp" - -#include "logging.hpp" -#include "ocvsmd/platform/posix_utils.hpp" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -UnixSocketServer::UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path) - : Base{executor} - , socket_path_{std::move(socket_path)} -{ -} - -int UnixSocketServer::makeSocketHandle(int& out_fd) -{ - CETL_DEBUG_ASSERT(out_fd == -1, ""); - - if (const auto err = platform::posixSyscallError([&out_fd] { - // - return out_fd = ::socket(AF_UNIX, SOCK_STREAM, 0); - })) - { - logger().error("Failed to create server socket: {}.", std::strerror(err)); - return err; - } - - sockaddr_un addr{}; - addr.sun_family = AF_UNIX; - const std::string abstract_socket_path = '\0' + socket_path_; - CETL_DEBUG_ASSERT(abstract_socket_path.size() <= sizeof(addr.sun_path), ""); - // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) - std::memcpy(addr.sun_path, - abstract_socket_path.c_str(), - std::min(sizeof(addr.sun_path), abstract_socket_path.size())); - - return bindSocket(out_fd, &addr, offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); -} - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd diff --git a/src/common/ipc/pipe/unix_socket_server.hpp b/src/common/ipc/pipe/unix_socket_server.hpp deleted file mode 100644 index 81965df..0000000 --- a/src/common/ipc/pipe/unix_socket_server.hpp +++ /dev/null @@ -1,51 +0,0 @@ -// -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: MIT -// - -#ifndef OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED -#define OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED - -#include "socket_server_base.hpp" - -#include -#include - -#include - -namespace ocvsmd -{ -namespace common -{ -namespace ipc -{ -namespace pipe -{ - -class UnixSocketServer final : public SocketServerBase -{ -public: - UnixSocketServer(libcyphal::IExecutor& executor, std::string socket_path); - - UnixSocketServer(UnixSocketServer&&) = delete; - UnixSocketServer(const UnixSocketServer&) = delete; - UnixSocketServer& operator=(UnixSocketServer&&) = delete; - UnixSocketServer& operator=(const UnixSocketServer&) = delete; - - ~UnixSocketServer() override = default; - -private: - using Base = SocketServerBase; - - CETL_NODISCARD int makeSocketHandle(int& out_fd) override; - - const std::string socket_path_; - -}; // UnixSocketServer - -} // namespace pipe -} // namespace ipc -} // namespace common -} // namespace ocvsmd - -#endif // OCVSMD_COMMON_IPC_PIPE_UNIX_SOCKET_SERVER_HPP_INCLUDED diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index f2f6892..ffdb72b 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -104,6 +104,11 @@ class ConfigImpl final : public Config return find_or(root_, "cyphal", "transport", "interfaces", std::vector{}); } + auto getIpcConnections() const -> std::vector override + { + return find_or(root_, "ipc", "connections", std::vector{}); + } + auto getLoggingFile() const -> cetl::optional override { return findImpl("logging", "file"); diff --git a/src/daemon/engine/config.hpp b/src/daemon/engine/config.hpp index d6c8a8d..7dc2022 100644 --- a/src/daemon/engine/config.hpp +++ b/src/daemon/engine/config.hpp @@ -50,6 +50,8 @@ class Config CETL_NODISCARD virtual auto getCyphalTransportInterfaces() const -> std::vector = 0; + CETL_NODISCARD virtual auto getIpcConnections() const -> std::vector = 0; + CETL_NODISCARD virtual auto getLoggingFile() const -> cetl::optional = 0; CETL_NODISCARD virtual auto getLoggingLevel() const -> cetl::optional = 0; CETL_NODISCARD virtual auto getLoggingFlushLevel() const -> cetl::optional = 0; diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 10e917b..7a9471d 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -7,8 +7,9 @@ #include "config.hpp" #include "cyphal/file_provider.hpp" -#include "ipc/pipe/net_socket_server.hpp" -// #include "ipc/pipe/unix_socket_server.hpp" +#include "io/socket_address.hpp" +#include "ipc/pipe/server_pipe.hpp" +#include "ipc/pipe/socket_server.hpp" #include "ipc/server_router.hpp" #include "svc/node/exec_cmd_service.hpp" #include "svc/svc_helpers.hpp" @@ -42,12 +43,16 @@ Engine::Engine(Config::Ptr config) cetl::optional Engine::init() { + logger_->trace("Initializing engine..."); + // 1. Create the transport layer object. // auto* const transport_iface = udp_transport_bag_.create(config_); if (transport_iface == nullptr) { - return "Failed to create cyphal UDP transport."; + std::string msg = "Failed to create cyphal UDP transport."; + logger_->error(msg); + return msg; } // 2. Create the presentation layer object. @@ -61,7 +66,9 @@ cetl::optional Engine::init() if (const auto* failure = cetl::get_if(&maybe_node)) { (void) failure; - return "Failed to create cyphal node."; + std::string msg = "Failed to create cyphal node."; + logger_->error(msg); + return msg; } node_.emplace(cetl::get(std::move(maybe_node))); @@ -79,15 +86,37 @@ cetl::optional Engine::init() file_provider_ = cyphal::FileProvider::make(memory_, *presentation_); if (file_provider_ == nullptr) { - return "Failed to create cyphal file provider."; + std::string msg = "Failed to create cyphal file provider."; + logger_->error(msg); + return msg; } // 6. Bring up the IPC router and its services. // - // using ServerPipe = common::ipc::pipe::UnixSocketServer; - // auto server_pipe = std::make_unique(executor_, "/var/run/ocvsmd/local.sock"); - using ServerPipe = common::ipc::pipe::NetSocketServer; - auto server_pipe = std::make_unique(executor_, 9875); // NOLINT(*-magic-numbers) + common::ipc::pipe::ServerPipe::Ptr server_pipe; + { + using ParseResult = common::io::SocketAddress::ParseResult; + + auto ipc_connections = config_->getIpcConnections(); + if (ipc_connections.empty()) + { + std::string msg = "No IPC connections configured."; + logger_->error(msg); + return msg; + } + + logger_->debug("Starting with IPC connection '{}'...", ipc_connections.front()); + auto maybe_socket_address = common::io::SocketAddress::parse(ipc_connections.front(), 0); + if (const auto* const failure = cetl::get_if(&maybe_socket_address)) + { + (void) failure; + std::string msg = "Failed to parse IPC connection."; + logger_->error(msg); + return msg; + } + const auto socket_address = cetl::get(maybe_socket_address); + server_pipe = std::make_unique(executor_, socket_address); + } // ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); // @@ -96,9 +125,12 @@ cetl::optional Engine::init() // if (0 != ipc_router_->start()) { - return "Failed to start IPC router."; + std::string msg = "Failed to start IPC router."; + logger_->error(msg); + return msg; } + logger_->debug("Engine is initialized."); return cetl::nullopt; } diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 21d9b0b..09416d7 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -7,8 +7,8 @@ #include "ipc/channel.hpp" #include "ipc/client_router.hpp" -#include "ipc/pipe/net_socket_client.hpp" -// #include "ipc/pipe/unix_socket_client.hpp" +#include "ipc/pipe/client_pipe.hpp" +#include "ipc/pipe/socket_client.hpp" #include "logging.hpp" #include "ocvsmd/sdk/node_command_client.hpp" #include "sdk_factory.hpp" @@ -19,6 +19,7 @@ #include #include +#include #include namespace ocvsmd @@ -33,26 +34,40 @@ class DaemonImpl final : public Daemon public: DaemonImpl(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) : memory_{memory} + , executor_{executor} , logger_{common::getLogger("sdk")} { - // using ClientPipe = common::ipc::pipe::UnixSocketClient; - // auto client_pipe = std::make_unique(executor, "/var/run/ocvsmd/local.sock"); - using ClientPipe = common::ipc::pipe::NetSocketClient; - auto client_pipe = std::make_unique(executor, "127.0.0.1", 9875); // NOLINT(*-magic-numbers) - - ipc_router_ = common::ipc::ClientRouter::make(memory, std::move(client_pipe)); - - node_command_client_ = Factory::makeNodeCommandClient(memory, ipc_router_); } - CETL_NODISCARD int start() const + CETL_NODISCARD int start(const std::string& connection) { + logger_->info("Starting with IPC connection '{}'...", connection); + + common::ipc::pipe::ClientPipe::Ptr client_pipe; + { + using ParseResult = common::io::SocketAddress::ParseResult; + + auto maybe_socket_address = common::io::SocketAddress::parse(connection, 0); + if (const auto* const err = cetl::get_if(&maybe_socket_address)) + { + logger_->error("Failed to parse IPC connection string ('{}'): {}.", connection, std::strerror(*err)); + return *err; + } + const auto socket_address = cetl::get(maybe_socket_address); + client_pipe = std::make_unique(executor_, socket_address); + } + + ipc_router_ = common::ipc::ClientRouter::make(memory_, std::move(client_pipe)); + + node_command_client_ = Factory::makeNodeCommandClient(memory_, ipc_router_); + if (const int err = ipc_router_->start()) { logger_->error("Failed to start IPC router: {}.", std::strerror(err)); return err; } + logger_->debug("Started IPC connection."); return 0; } @@ -65,6 +80,7 @@ class DaemonImpl final : public Daemon private: cetl::pmr::memory_resource& memory_; + libcyphal::IExecutor& executor_; common::LoggerPtr logger_; common::ipc::ClientRouter::Ptr ipc_router_; NodeCommandClient::Ptr node_command_client_; @@ -73,12 +89,12 @@ class DaemonImpl final : public Daemon } // namespace -CETL_NODISCARD Daemon::Ptr Daemon::make( // - cetl::pmr::memory_resource& memory, - libcyphal::IExecutor& executor) +CETL_NODISCARD Daemon::Ptr Daemon::make(cetl::pmr::memory_resource& memory, + libcyphal::IExecutor& executor, + const std::string& connection) { auto daemon = std::make_shared(memory, executor); - if (0 != daemon->start()) + if (0 != daemon->start(connection)) { return nullptr; } diff --git a/test/common/io/test_socket_address.cpp b/test/common/io/test_socket_address.cpp index 3ace48d..3c1ba20 100644 --- a/test/common/io/test_socket_address.cpp +++ b/test/common/io/test_socket_address.cpp @@ -43,6 +43,7 @@ TEST_F(TestSocketAddress, parse_unix_domain) auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_TRUE(socket_address.isUnix()); EXPECT_THAT(addr_un->sun_family, AF_UNIX); EXPECT_THAT(addr_un->sun_path, test_path); } @@ -79,6 +80,7 @@ TEST_F(TestSocketAddress, parse_abstract_unix_domain) auto socket_address = cetl::get(maybe_socket_addr); auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_TRUE(socket_address.isUnix()); EXPECT_THAT(addr_un->sun_family, AF_UNIX); EXPECT_THAT(addr_un->sun_path[0], '\0'); EXPECT_THAT(addr_un->sun_path + 1, test_path); // NOLINT @@ -94,7 +96,7 @@ TEST_F(TestSocketAddress, parse_abstract_unix_domain) const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT EXPECT_THAT(addr_un->sun_family, AF_UNIX); EXPECT_THAT(addr_un->sun_path[0], '\0'); - EXPECT_TRUE(0 == std::memcmp(addr_un->sun_path + 1, test_path.data(), test_path.size() + 1)); // NOLINT + EXPECT_TRUE(0 == memcmp(addr_un->sun_path + 1, test_path.data(), test_path.size() + 1)); // NOLINT } // try max possible path length @@ -130,6 +132,7 @@ TEST_F(TestSocketAddress, parse_ipv4) auto socket_address = cetl::get(maybe_socket_addr); auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_in = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_FALSE(socket_address.isUnix()); EXPECT_THAT(addr_in->sin_family, AF_INET); EXPECT_THAT(ntohs(addr_in->sin_port), 0x1234); EXPECT_THAT(ntohl(addr_in->sin_addr.s_addr), 0x7F000001); @@ -174,6 +177,7 @@ TEST_F(TestSocketAddress, parse_ipv6) auto socket_address = cetl::get(maybe_socket_addr); auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_FALSE(socket_address.isUnix()); EXPECT_THAT(addr_in6->sin6_family, AF_INET6); EXPECT_THAT(ntohs(addr_in6->sin6_port), 0x1234); EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // @@ -221,6 +225,7 @@ TEST_F(TestSocketAddress, parse_wildcard) auto socket_address = cetl::get(maybe_socket_addr); auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT + EXPECT_FALSE(socket_address.isUnix()); EXPECT_THAT(addr_in6->sin6_family, AF_INET6); EXPECT_THAT(ntohs(addr_in6->sin6_port), 0x1234); EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // From f6655a0d4b5a243af301e691761cc0f33b06e797 Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 4 Feb 2025 00:15:44 +0200 Subject: [PATCH 106/156] added `io::OwnFd::get` --- src/common/io/io.hpp | 2 +- src/common/io/socket_address.cpp | 6 +++--- src/common/ipc/pipe/client_context.hpp | 6 +++--- src/common/ipc/pipe/socket_base.cpp | 16 ++++++---------- src/common/ipc/pipe/socket_client.cpp | 10 +++++----- src/common/ipc/pipe/socket_server.cpp | 22 ++++++++++------------ 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/common/io/io.hpp b/src/common/io/io.hpp index fed7cef..5212ae2 100644 --- a/src/common/io/io.hpp +++ b/src/common/io/io.hpp @@ -52,7 +52,7 @@ class OwnFd final OwnFd(const OwnFd&) = delete; OwnFd& operator=(const OwnFd&) = delete; - explicit operator int() const + int get() const { return fd_; } diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 86fcac7..4b46b9f 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -79,7 +79,7 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const // // TODO: Enable! constexpr int enable = 0; - return ::setsockopt(static_cast(out_fd), IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); + return ::setsockopt(out_fd.get(), IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); })) { getLogger("io")->error("Failed to set TCP_NODELAY=1: {}.", std::strerror(err)); @@ -92,7 +92,7 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const int SocketAddress::bind(const OwnFd& socket_fd) const { - const int raw_fd = static_cast(socket_fd); + const int raw_fd = socket_fd.get(); CETL_DEBUG_ASSERT(raw_fd != -1, ""); // Disable IPv6-only mode for dual-stack sockets (aka wildcard). @@ -122,7 +122,7 @@ int SocketAddress::bind(const OwnFd& socket_fd) const int SocketAddress::connect(const OwnFd& socket_fd) const { - const int raw_fd = static_cast(socket_fd); + const int raw_fd = socket_fd.get(); CETL_DEBUG_ASSERT(raw_fd != -1, ""); const auto err = platform::posixSyscallError([this, raw_fd] { diff --git a/src/common/ipc/pipe/client_context.hpp b/src/common/ipc/pipe/client_context.hpp index 25dbc00..6766c00 100644 --- a/src/common/ipc/pipe/client_context.hpp +++ b/src/common/ipc/pipe/client_context.hpp @@ -37,15 +37,15 @@ class ClientContext final : id_{id} , logger_{logger} { - CETL_DEBUG_ASSERT(static_cast(fd) != -1, ""); + CETL_DEBUG_ASSERT(fd.get() != -1, ""); - logger_.trace("ClientContext(fd={}, id={}).", static_cast(fd), id_); + logger_.trace("ClientContext(fd={}, id={}).", fd.get(), id_); state_.fd = std::move(fd); } ~ClientContext() { - logger_.trace("~ClientContext(fd={}, id={}).", static_cast(state_.fd), id_); + logger_.trace("~ClientContext(fd={}, id={}).", state_.fd.get(), id_); } ClientContext(const ClientContext&) = delete; diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp index f550789..d18b239 100644 --- a/src/common/ipc/pipe/socket_base.cpp +++ b/src/common/ipc/pipe/socket_base.cpp @@ -60,7 +60,7 @@ int SocketBase::send(const State& state, const Payloads payloads) if (const int err = platform::posixSyscallError([total_size, &state] { // const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; - return ::write(static_cast(state.fd), &msg_header, sizeof(msg_header)); + return ::write(state.fd.get(), &msg_header, sizeof(msg_header)); })) { return err; @@ -72,7 +72,7 @@ int SocketBase::send(const State& state, const Payloads payloads) { if (const int err = platform::posixSyscallError([payload, &state] { // - return ::write(static_cast(state.fd), payload.data(), payload.size()); + return ::write(state.fd.get(), payload.data(), payload.size()); })) { return err; @@ -91,16 +91,14 @@ int SocketBase::receiveMessage(State& state, std::function&& actio ssize_t bytes_read = 0; if (const auto err = platform::posixSyscallError([&state, &msg_header, &bytes_read] { // - return bytes_read = ::read(static_cast(state.fd), &msg_header, sizeof(msg_header)); + return bytes_read = ::read(state.fd.get(), &msg_header, sizeof(msg_header)); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { return 0; // no data available yet } - logger_->error("Failed to read message header (fd={}): {}.", - static_cast(state.fd), - std::strerror(err)); + logger_->error("Failed to read message header (fd={}): {}.", state.fd.get(), std::strerror(err)); return err; } @@ -129,16 +127,14 @@ int SocketBase::receiveMessage(State& state, std::function&& actio ssize_t read = 0; if (const auto err = platform::posixSyscallError([this, &state, buf_span, &read] { // - return read = ::read(static_cast(state.fd), buf_span.data(), buf_span.size()); + return read = ::read(state.fd.get(), buf_span.data(), buf_span.size()); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { return 0; // no data available } - logger_->error("Failed to read message payload (fd={}): {}.", - static_cast(state.fd), - std::strerror(err)); + logger_->error("Failed to read message payload (fd={}): {}.", state.fd.get(), std::strerror(err)); return err; } if (read != buf_span.size()) diff --git a/src/common/ipc/pipe/socket_client.cpp b/src/common/ipc/pipe/socket_client.cpp index 332ba56..5eaa17e 100644 --- a/src/common/ipc/pipe/socket_client.cpp +++ b/src/common/ipc/pipe/socket_client.cpp @@ -40,7 +40,7 @@ SocketClient::SocketClient(libcyphal::IExecutor& executor, const io::SocketAddre int SocketClient::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(event_handler, ""); - CETL_DEBUG_ASSERT(static_cast(state_.fd) == -1, ""); + CETL_DEBUG_ASSERT(state_.fd.get() == -1, ""); event_handler_ = std::move(event_handler); @@ -55,7 +55,7 @@ int SocketClient::start(EventHandler event_handler) // handle_connect(); }, - platform::IPosixExecutorExtension::Trigger::Writable{static_cast(state_.fd)}); + platform::IPosixExecutorExtension::Trigger::Writable{state_.fd.get()}); return 0; } @@ -71,7 +71,7 @@ int SocketClient::makeSocketHandle() return *err; } auto socket_fd = cetl::get(std::move(maybe_socket)); - CETL_DEBUG_ASSERT(static_cast(socket_fd) != -1, ""); + CETL_DEBUG_ASSERT(socket_fd.get() != -1, ""); const int err = socket_address_.connect(socket_fd); if (err != 0) @@ -116,7 +116,7 @@ void SocketClient::handle_connect() if (const auto err = platform::posixSyscallError([this, &so_error] { // socklen_t len = sizeof(so_error); - return ::getsockopt(static_cast(state_.fd), SOL_SOCKET, SO_ERROR, &so_error, &len); + return ::getsockopt(state_.fd.get(), SOL_SOCKET, SO_ERROR, &so_error, &len); })) { logger().warn("Failed to query socket error: {}.", std::strerror(err)); @@ -134,7 +134,7 @@ void SocketClient::handle_connect() // handle_receive(); }, - platform::IPosixExecutorExtension::Trigger::Readable{static_cast(state_.fd)}); + platform::IPosixExecutorExtension::Trigger::Readable{state_.fd.get()}); state_.read_phase = State::ReadPhase::Header; event_handler_(Event::Connected{}); diff --git a/src/common/ipc/pipe/socket_server.cpp b/src/common/ipc/pipe/socket_server.cpp index 5452b8a..6d9de38 100644 --- a/src/common/ipc/pipe/socket_server.cpp +++ b/src/common/ipc/pipe/socket_server.cpp @@ -49,7 +49,7 @@ SocketServer::SocketServer(libcyphal::IExecutor& executor, const io::SocketAddre int SocketServer::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(event_handler, ""); - CETL_DEBUG_ASSERT(static_cast(server_fd_) == -1, ""); + CETL_DEBUG_ASSERT(server_fd_.get() == -1, ""); event_handler_ = std::move(event_handler); @@ -61,7 +61,7 @@ int SocketServer::start(EventHandler event_handler) if (const auto err = platform::posixSyscallError([this] { // - return ::listen(static_cast(server_fd_), MaxConnections); + return ::listen(server_fd_.get(), MaxConnections); })) { logger().error("Failed to listen on server socket: {}.", std::strerror(err)); @@ -73,7 +73,7 @@ int SocketServer::start(EventHandler event_handler) // handleAccept(); }, - platform::IPosixExecutorExtension::Trigger::Readable{static_cast(server_fd_)}); + platform::IPosixExecutorExtension::Trigger::Readable{server_fd_.get()}); return 0; } @@ -89,7 +89,7 @@ int SocketServer::makeSocketHandle() return *err; } auto socket_fd = cetl::get(std::move(maybe_socket)); - CETL_DEBUG_ASSERT(static_cast(socket_fd) != -1, ""); + CETL_DEBUG_ASSERT(socket_fd.get() != -1, ""); const int err = socket_address_.bind(socket_fd); if (err != 0) @@ -115,19 +115,19 @@ int SocketServer::send(const ClientId client_id, const Payloads payloads) void SocketServer::handleAccept() { - CETL_DEBUG_ASSERT(static_cast(server_fd_) != -1, ""); + CETL_DEBUG_ASSERT(server_fd_.get() != -1, ""); io::OwnFd client_fd; if (const auto err = platform::posixSyscallError([this, &client_fd] { // - client_fd = io::OwnFd{::accept(static_cast(server_fd_), nullptr, nullptr)}; - return static_cast(client_fd); + client_fd = io::OwnFd{::accept(server_fd_.get(), nullptr, nullptr)}; + return client_fd.get(); })) { logger().warn("Failed to accept client connection: {}.", std::strerror(err)); return; } - const int raw_fd = static_cast(client_fd); + const int raw_fd = client_fd.get(); CETL_DEBUG_ASSERT(raw_fd != -1, ""); const ClientId new_client_id = ++unique_client_id_counter_; @@ -158,15 +158,13 @@ void SocketServer::handleClientRequest(const ClientId client_id) { if (err == -1) { - logger().debug("End of client stream - closing connection (id={}, fd={}).", - client_id, - static_cast(state.fd)); + logger().debug("End of client stream - closing connection (id={}, fd={}).", client_id, state.fd.get()); } else { logger().warn("Failed to handle client request - closing connection (id={}, fd={}): {}.", client_id, - static_cast(state.fd), + state.fd.get(), std::strerror(err)); } From 885304012a0ed8aede4bdebe6988f2136f7dc65d Mon Sep 17 00:00:00 2001 From: Sergei Date: Tue, 4 Feb 2025 11:15:12 +0200 Subject: [PATCH 107/156] moved `accept` into `SocketAddress` --- src/common/io/socket_address.cpp | 96 +++++++++++++++++++++++---- src/common/io/socket_address.hpp | 10 +-- src/common/ipc/pipe/socket_server.cpp | 46 ++++++------- 3 files changed, 113 insertions(+), 39 deletions(-) diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 4b46b9f..0f7e9f3 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -57,7 +57,7 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const OwnFd out_fd; const auto& addr_generic = asGenericAddr(); - if (auto err = platform::posixSyscallError([this, socket_type, &addr_generic, &out_fd] { + if (const auto err = platform::posixSyscallError([this, socket_type, &addr_generic, &out_fd] { // const int fd = ::socket(addr_generic.sa_family, static_cast(socket_type), 0); if (fd != -1) @@ -75,16 +75,7 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const // if (is_stream && ((addr_generic.sa_family == AF_INET) || (addr_generic.sa_family == AF_INET6))) { - if (auto err = platform::posixSyscallError([&out_fd] { - // - // TODO: Enable! - constexpr int enable = 0; - return ::setsockopt(out_fd.get(), IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); - })) - { - getLogger("io")->error("Failed to set TCP_NODELAY=1: {}.", std::strerror(err)); - return err; - } + configureNoDelay(out_fd); } return out_fd; @@ -98,7 +89,7 @@ int SocketAddress::bind(const OwnFd& socket_fd) const // Disable IPv6-only mode for dual-stack sockets (aka wildcard). if (is_wildcard_) { - if (auto err = platform::posixSyscallError([raw_fd] { + if (const auto err = platform::posixSyscallError([raw_fd] { // int disable = 0; return ::setsockopt(raw_fd, IPPROTO_IPV6, IPV6_V6ONLY, &disable, sizeof(disable)); @@ -142,6 +133,87 @@ int SocketAddress::connect(const OwnFd& socket_fd) const } } +cetl::optional SocketAddress::accept(const OwnFd& server_fd) +{ + CETL_DEBUG_ASSERT(server_fd.get() != -1, ""); + + while (true) + { + addr_len_ = sizeof(addr_storage_); +#if __linux__ + OwnFd client_fd{::accept4(server_fd.get(), &asGenericAddr(), &addr_len_, SOCK_NONBLOCK | SOCK_CLOEXEC)}; +#else + OwnFd client_fd{::accept(server_fd.get(), &asGenericAddr(), &addr_len_)}; +#endif + if (client_fd.get() >= 0) + { + configureNoDelay(client_fd); + return client_fd; + } + + const int err = errno; + switch (err) + { + case EAGAIN: +#if EAGAIN != EWOULDBLOCK + case EWOULDBLOCK: +#endif + { + // Not ready yet - just exit. + return cetl::nullopt; + } + + // The list of errors below is a guess of temporary network errors (vs permanent ones). + // + case EINTR: + case ENETDOWN: + case ETIMEDOUT: + case EHOSTDOWN: + case ENETUNREACH: + case ECONNABORTED: + case EHOSTUNREACH: +#ifdef EPROTO + case EPROTO: // not defined on OpenBSD +#endif + { + // Just log and retry. + getLogger("io")->debug("Failed to accept connection; retrying (fd={}, err={}).", server_fd.get(), err); + break; + } + + default: { + // Just log and exit. + getLogger("io")->warn("Failed to accept connection (fd={}, err={}): {}.", + server_fd.get(), + err, + std::strerror(err)); + return cetl::nullopt; + } + } // switch err + + } // while(true) + + return cetl::nullopt; +} + +void SocketAddress::configureNoDelay(const OwnFd& fd) +{ + // TODO: Temporary disabled, but enable when `receiveMessage` could accept partial payloads! + constexpr int enable = 0; + + if (const auto err = platform::posixSyscallError([&fd, &enable] { + // + return ::setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); + })) + { + getLogger("io")->warn("Failed to set TCP_NODELAY={} (fd={}, err={}): {}.", + enable, + fd.get(), + err, + std::strerror(err)); + } +} + SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, const std::uint16_t port_hint) { // Unix domain? diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index e06408b..9441089 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -35,6 +35,8 @@ class SocketAddress final }; static ParseResult::Var parse(const std::string& str, const std::uint16_t port_hint); + SocketAddress() noexcept; + std::pair getRaw() const noexcept; bool isUnix() const noexcept @@ -50,12 +52,12 @@ class SocketAddress final }; SocketResult::Var socket(const int type) const; - int bind(const OwnFd& socket_fd) const; - int connect(const OwnFd& socket_fd) const; + int bind(const OwnFd& socket_fd) const; + int connect(const OwnFd& socket_fd) const; + cetl::optional accept(const OwnFd& socket_fd); private: - SocketAddress() noexcept; - + static void configureNoDelay(const OwnFd& fd); static cetl::optional tryParseAsUnixDomain(const std::string& str); static cetl::optional tryParseAsAbstractUnixDomain(const std::string& str); static int extractFamilyHostAndPort(const std::string& str, std::string& host, std::uint16_t& port); diff --git a/src/common/ipc/pipe/socket_server.cpp b/src/common/ipc/pipe/socket_server.cpp index 6d9de38..84613ca 100644 --- a/src/common/ipc/pipe/socket_server.cpp +++ b/src/common/ipc/pipe/socket_server.cpp @@ -6,6 +6,8 @@ #include "socket_server.hpp" #include "client_context.hpp" +#include "io/io.hpp" +#include "io/socket_address.hpp" #include "ipc/ipc_types.hpp" #include "logging.hpp" #include "ocvsmd/platform/posix_executor_extension.hpp" @@ -117,32 +119,30 @@ void SocketServer::handleAccept() { CETL_DEBUG_ASSERT(server_fd_.get() != -1, ""); - io::OwnFd client_fd; - if (const auto err = platform::posixSyscallError([this, &client_fd] { - // - client_fd = io::OwnFd{::accept(server_fd_.get(), nullptr, nullptr)}; - return client_fd.get(); - })) + io::SocketAddress client_address; + if (auto client_fd = client_address.accept(server_fd_)) { - logger().warn("Failed to accept client connection: {}.", std::strerror(err)); - return; - } - const int raw_fd = client_fd.get(); - CETL_DEBUG_ASSERT(raw_fd != -1, ""); - - const ClientId new_client_id = ++unique_client_id_counter_; - auto client_context = std::make_unique(new_client_id, std::move(client_fd), logger()); - // - client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( - [this, new_client_id](const auto&) { - // - handleClientRequest(new_client_id); - }, - platform::IPosixExecutorExtension::Trigger::Readable{raw_fd})); + const ClientId new_client_id = ++unique_client_id_counter_; - client_id_to_context_.emplace(new_client_id, std::move(client_context)); + // Log to default logger (syslog) the client connection. + getLogger("")->debug("New client connection (id={}).", new_client_id); - event_handler_(Event::Connected{new_client_id}); + const int raw_fd = client_fd->get(); + CETL_DEBUG_ASSERT(raw_fd != -1, ""); + + auto client_context = std::make_unique(new_client_id, std::move(*client_fd), logger()); + // + client_context->setCallback(posix_executor_ext_->registerAwaitableCallback( + [this, new_client_id](const auto&) { + // + handleClientRequest(new_client_id); + }, + platform::IPosixExecutorExtension::Trigger::Readable{raw_fd})); + + client_id_to_context_.emplace(new_client_id, std::move(client_context)); + + event_handler_(Event::Connected{new_client_id}); + } } void SocketServer::handleClientRequest(const ClientId client_id) From ffadf9d16ffdfc1f71fdb8f1fe3d88903a132c44 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 4 Feb 2025 13:08:12 +0200 Subject: [PATCH 108/156] SO_REUSEADDR & TCP_NODELAY --- init.d/ocvsmd.toml | 2 +- src/common/io/socket_address.cpp | 12 +++++++++--- src/common/io/socket_address.hpp | 10 ++++++++-- src/common/ipc/pipe/socket_client.cpp | 2 +- src/common/ipc/pipe/socket_server.cpp | 16 ++++++++++++++-- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index 72a3873..46f4aa6 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -35,7 +35,7 @@ connections = [ # See also README documentation for more details. [logging] # The path to the log file. -file = '/etc/var/ocvsmd.log' +file = '/var/log/ocvsmd.log' # Supported log levels: 'trace', 'debug', 'info', 'warning', 'error', 'critical', 'off'. level = 'info' # By default, the log file is not immediately flushed to disk (at `off` level). diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 0f7e9f3..8259b61 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -73,7 +73,7 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const // Disable Nagle's algorithm for TCP sockets, so that our small IPC packets are sent immediately. // - if (is_stream && ((addr_generic.sa_family == AF_INET) || (addr_generic.sa_family == AF_INET6))) + if (is_stream && isAnyInet()) { configureNoDelay(out_fd); } @@ -106,7 +106,7 @@ int SocketAddress::bind(const OwnFd& socket_fd) const }); if (err != 0) { - getLogger("io")->error("Failed to set IPV6_V6ONLY=0: {}.", std::strerror(err)); + getLogger("io")->error("Failed to bind socket: {}.", std::strerror(err)); } return err; } @@ -147,7 +147,13 @@ cetl::optional SocketAddress::accept(const OwnFd& server_fd) #endif if (client_fd.get() >= 0) { - configureNoDelay(client_fd); + // Disable Nagle's algorithm for TCP sockets, so that our small IPC packets are sent immediately. + // + if (isAnyInet()) + { + configureNoDelay(client_fd); + } + return client_fd; } diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index 9441089..1932759 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -44,6 +44,12 @@ class SocketAddress final return asGenericAddr().sa_family == AF_UNIX; } + bool isAnyInet() const noexcept + { + const auto family = asGenericAddr().sa_family; + return (family == AF_INET) || (family == AF_INET6); + } + struct SocketResult { using Failure = int; // aka errno @@ -54,10 +60,10 @@ class SocketAddress final int bind(const OwnFd& socket_fd) const; int connect(const OwnFd& socket_fd) const; - cetl::optional accept(const OwnFd& socket_fd); + cetl::optional accept(const OwnFd& server_fd); private: - static void configureNoDelay(const OwnFd& fd); + static void configureNoDelay(const OwnFd& fd); static cetl::optional tryParseAsUnixDomain(const std::string& str); static cetl::optional tryParseAsAbstractUnixDomain(const std::string& str); static int extractFamilyHostAndPort(const std::string& str, std::string& host, std::uint16_t& port); diff --git a/src/common/ipc/pipe/socket_client.cpp b/src/common/ipc/pipe/socket_client.cpp index 5eaa17e..785bb32 100644 --- a/src/common/ipc/pipe/socket_client.cpp +++ b/src/common/ipc/pipe/socket_client.cpp @@ -46,7 +46,7 @@ int SocketClient::start(EventHandler event_handler) if (const auto err = makeSocketHandle()) { - logger().error("Failed to make socket handle: {}.", std::strerror(err)); + logger().error("Failed to make client socket handle: {}.", std::strerror(err)); return err; } diff --git a/src/common/ipc/pipe/socket_server.cpp b/src/common/ipc/pipe/socket_server.cpp index 84613ca..6fc81c1 100644 --- a/src/common/ipc/pipe/socket_server.cpp +++ b/src/common/ipc/pipe/socket_server.cpp @@ -36,7 +36,7 @@ namespace pipe namespace { -constexpr int MaxConnections = 5; +constexpr int MaxConnections = 32; } // namespace @@ -57,7 +57,7 @@ int SocketServer::start(EventHandler event_handler) if (const auto err = makeSocketHandle()) { - logger().error("Failed to make socket handle: {}.", std::strerror(err)); + logger().error("Failed to make server socket handle: {}.", std::strerror(err)); return err; } @@ -93,6 +93,18 @@ int SocketServer::makeSocketHandle() auto socket_fd = cetl::get(std::move(maybe_socket)); CETL_DEBUG_ASSERT(socket_fd.get() != -1, ""); + // Set SO_REUSEADDR to allow binding to the same address. + // Otherwise, you have to wait for 5 minutes after the server is stopped to bind to the same address. + if (const auto err = platform::posixSyscallError([this, &socket_fd] { + // + constexpr int enable = 1; + return ::setsockopt(socket_fd.get(), SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); + })) + { + logger().error("Failed to set server socket SO_REUSEADDR=1: {}.", std::strerror(err)); + return err; + } + const int err = socket_address_.bind(socket_fd); if (err != 0) { From 9aaac25c5a1666dfe6df317331a7178935db212c Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 4 Feb 2025 13:27:52 +0200 Subject: [PATCH 109/156] fix mac tests --- src/common/io/socket_address.hpp | 2 +- test/common/io/test_socket_address.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index 1932759..1708558 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -63,7 +63,7 @@ class SocketAddress final cetl::optional accept(const OwnFd& server_fd); private: - static void configureNoDelay(const OwnFd& fd); + static void configureNoDelay(const OwnFd& fd); static cetl::optional tryParseAsUnixDomain(const std::string& str); static cetl::optional tryParseAsAbstractUnixDomain(const std::string& str); static int extractFamilyHostAndPort(const std::string& str, std::string& host, std::uint16_t& port); diff --git a/test/common/io/test_socket_address.cpp b/test/common/io/test_socket_address.cpp index 3c1ba20..040dc5c 100644 --- a/test/common/io/test_socket_address.cpp +++ b/test/common/io/test_socket_address.cpp @@ -49,8 +49,9 @@ TEST_F(TestSocketAddress, parse_unix_domain) } // try max possible path length + constexpr auto MaxPath = sizeof(sockaddr_un::sun_path); { - const std::string max_path(108 - 1, 'x'); + const std::string max_path(MaxPath - 1, 'x'); auto maybe_socket_addr = SocketAddress::parse("unix:" + max_path, 0); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); @@ -63,7 +64,7 @@ TEST_F(TestSocketAddress, parse_unix_domain) // try beyond max possible path length { - const std::string too_long_path(108, 'x'); + const std::string too_long_path(MaxPath, 'x'); auto maybe_socket_addr = SocketAddress::parse("unix:" + too_long_path, 0); ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); } @@ -100,8 +101,9 @@ TEST_F(TestSocketAddress, parse_abstract_unix_domain) } // try max possible path length + constexpr auto MaxPath = sizeof(sockaddr_un::sun_path); { - const std::string max_path(108 - 2, 'x'); + const std::string max_path(MaxPath - 2, 'x'); auto maybe_socket_addr = SocketAddress::parse("unix-abstract:" + max_path, 0); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); @@ -115,7 +117,7 @@ TEST_F(TestSocketAddress, parse_abstract_unix_domain) // try beyond max possible path length { - const std::string too_long_path(108 - 1, 'x'); + const std::string too_long_path(MaxPath - 1, 'x'); auto maybe_socket_addr = SocketAddress::parse("unix-abstract:" + too_long_path, 0); ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); } From f64c06d3fa472a7c94f0ff81bed1c3ccadc8eddc Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 4 Feb 2025 14:12:11 +0200 Subject: [PATCH 110/156] test for `isAnyInet` --- test/common/io/test_socket_address.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/common/io/test_socket_address.cpp b/test/common/io/test_socket_address.cpp index 040dc5c..2f61143 100644 --- a/test/common/io/test_socket_address.cpp +++ b/test/common/io/test_socket_address.cpp @@ -44,6 +44,7 @@ TEST_F(TestSocketAddress, parse_unix_domain) const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT EXPECT_TRUE(socket_address.isUnix()); + EXPECT_FALSE(socket_address.isAnyInet()); EXPECT_THAT(addr_un->sun_family, AF_UNIX); EXPECT_THAT(addr_un->sun_path, test_path); } @@ -82,6 +83,7 @@ TEST_F(TestSocketAddress, parse_abstract_unix_domain) auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_un = reinterpret_cast(raw_address_and_len.first); // NOLINT EXPECT_TRUE(socket_address.isUnix()); + EXPECT_FALSE(socket_address.isAnyInet()); EXPECT_THAT(addr_un->sun_family, AF_UNIX); EXPECT_THAT(addr_un->sun_path[0], '\0'); EXPECT_THAT(addr_un->sun_path + 1, test_path); // NOLINT @@ -135,6 +137,7 @@ TEST_F(TestSocketAddress, parse_ipv4) auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_in = reinterpret_cast(raw_address_and_len.first); // NOLINT EXPECT_FALSE(socket_address.isUnix()); + EXPECT_TRUE(socket_address.isAnyInet()); EXPECT_THAT(addr_in->sin_family, AF_INET); EXPECT_THAT(ntohs(addr_in->sin_port), 0x1234); EXPECT_THAT(ntohl(addr_in->sin_addr.s_addr), 0x7F000001); @@ -180,6 +183,7 @@ TEST_F(TestSocketAddress, parse_ipv6) auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT EXPECT_FALSE(socket_address.isUnix()); + EXPECT_TRUE(socket_address.isAnyInet()); EXPECT_THAT(addr_in6->sin6_family, AF_INET6); EXPECT_THAT(ntohs(addr_in6->sin6_port), 0x1234); EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // @@ -228,6 +232,7 @@ TEST_F(TestSocketAddress, parse_wildcard) auto raw_address_and_len = socket_address.getRaw(); const auto* const addr_in6 = reinterpret_cast(raw_address_and_len.first); // NOLINT EXPECT_FALSE(socket_address.isUnix()); + EXPECT_TRUE(socket_address.isAnyInet()); EXPECT_THAT(addr_in6->sin6_family, AF_INET6); EXPECT_THAT(ntohs(addr_in6->sin6_port), 0x1234); EXPECT_THAT(addr_in6->sin6_addr.s6_addr, // From 51d2bd46f81404c527ed6f829b52a404f2c10c5e Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 4 Feb 2025 21:52:41 +0200 Subject: [PATCH 111/156] implemented list of file server roots --- docs/file_server.hpp | 34 +++-- init.d/ocvsmd.toml | 8 ++ src/cli/main.cpp | 5 +- src/daemon/engine/config.cpp | 24 ++-- src/daemon/engine/config.hpp | 3 + src/daemon/engine/cyphal/file_provider.cpp | 159 +++++++++++++++++---- src/daemon/engine/cyphal/file_provider.hpp | 5 +- src/daemon/engine/engine.cpp | 2 +- 8 files changed, 191 insertions(+), 49 deletions(-) diff --git a/docs/file_server.hpp b/docs/file_server.hpp index 5d4cd29..f689977 100644 --- a/docs/file_server.hpp +++ b/docs/file_server.hpp @@ -4,23 +4,41 @@ namespace ocvsmd /// The daemon always has the standard file server running. /// This interface can be used to configure it. /// It is not possible to stop the server; the closest alternative is to remove all root directories. +/// File server serves Cyphal network 'File' requests by matching the requested path against the list of root directories. +/// The very first match (found file) is served - that is why order of the root entries is important. class FileServer { public: + // `errno`-like error code (or anything else we might add in the future). + using Failure = int; // or `cetl::variant` + /// When the file server handles a request, it will attempt to locate the path relative to each of its root /// directories. See Yakut for a hands-on example. - /// The daemon will canonicalize the path and resolve symlinks. + /// The daemon will internally canonicalize the path and resolve symlinks, + /// and use real the file system path when matching and serving Cyphal network 'File' requests. /// The same path may be added multiple times to avoid interference across different clients. - /// The path may be that of a file rather than a directory. - virtual std::expected add_root(const std::string_view path); + /// Currently the path should be a directory (later we might support a direct file as well). + /// The `back` flag determines whether the path is added to the front or the back of the list. + /// The changed list of paths is persisted by the daemon (in its configuration; on its exit), + /// so the list will be automatically restored on the next daemon start. + /// Returns `cetl::nullopt` on success. + /// + virtual cetl::optinal push_root(const cetl::string_view path, const bool back); /// Does nothing if such root does not exist (no error reported). - /// If such root is listed more than once, only one copy is removed. - /// The daemon will canonicalize the path and resolve symlinks. - virtual std::expected remove_root(const std::string_view path); + /// If such root is listed more than once, only one copy is removed (see `back` param). + /// The `back` flag determines whether the path is searched from the front or the back of the list. + /// The flag has no effect if there are no duplicates. + /// The changed list of paths is persisted by the daemon (in its configuration; on its exit), + /// so the list will be automatically restored on the next daemon start. + /// Returns `cetl::nullopt` on success (or if path not found). + /// + virtual cetl::optinal pop_root(const cetl::string_view path, const bool back); - /// The returned paths are canonicalized. The entries are not unique. - virtual std::expected, Error> list_roots() const; + /// The returned paths are the same as they were added by `push_root`. + /// The entries are not unique. The order is preserved. + /// + virtual cetl::variant, Failure> list_roots() const; }; } diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index 46f4aa6..7a15302 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -17,6 +17,14 @@ interfaces = [ 'udp:127.0.0.1', ] +# File Server settings. +[file_server] +# List of file server roots. +# The daemon will canonicalize paths and resolve symlinks. +roots = [ + '.', +] + # IPC server settings. [ipc] # Connection strings for the IPC server. diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 09d6f6a..5b9d92d 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -89,9 +89,10 @@ int main(const int argc, const char** const argv) auto node_cmd_client = daemon->getNodeCommandClient(); - const std::vector node_ids = {42, 43, 44}; + const std::vector node_ids = {42, 143, 144}; // auto sender = node_cmd_client->restart({node_ids.data(), node_ids.size()}); - auto sender = node_cmd_client->beginSoftwareUpdate({node_ids.data(), node_ids.size()}, "firmware.bin"); + auto sender = + node_cmd_client->beginSoftwareUpdate({node_ids.data(), node_ids.size()}, "firmware.bin"); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); if (const auto* const err = cetl::get_if(&cmd_result)) diff --git a/src/daemon/engine/config.cpp b/src/daemon/engine/config.cpp index ffdb72b..9841b5a 100644 --- a/src/daemon/engine/config.cpp +++ b/src/daemon/engine/config.cpp @@ -28,18 +28,6 @@ namespace engine namespace { -struct Default -{ - struct Cyphal - { - struct Udp - { - constexpr static auto Iface = "127.0.0.1"; - }; - }; - -}; // Default - class ConfigImpl final : public Config { public: @@ -104,6 +92,18 @@ class ConfigImpl final : public Config return find_or(root_, "cyphal", "transport", "interfaces", std::vector{}); } + auto getFileServerRoots() const -> std::vector override + { + return find_or(root_, "file_server", "roots", std::vector{}); + } + + void setFileServerRoots(const std::vector& roots) override + { + auto& toml_fs_roots = root_["file_server"]["roots"]; + toml_fs_roots = roots; + is_dirty_ = true; + } + auto getIpcConnections() const -> std::vector override { return find_or(root_, "ipc", "connections", std::vector{}); diff --git a/src/daemon/engine/config.hpp b/src/daemon/engine/config.hpp index 7dc2022..684b296 100644 --- a/src/daemon/engine/config.hpp +++ b/src/daemon/engine/config.hpp @@ -50,6 +50,9 @@ class Config CETL_NODISCARD virtual auto getCyphalTransportInterfaces() const -> std::vector = 0; + CETL_NODISCARD virtual auto getFileServerRoots() const -> std::vector = 0; + virtual void setFileServerRoots(const std::vector& roots) = 0; + CETL_NODISCARD virtual auto getIpcConnections() const -> std::vector = 0; CETL_NODISCARD virtual auto getLoggingFile() const -> cetl::optional = 0; diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index b25f769..46a8bdb 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MIT // +#include "config.hpp" #include "file_provider.hpp" #include "engine_helpers.hpp" @@ -31,6 +32,7 @@ #include #include #include +#include namespace ocvsmd { @@ -62,7 +64,9 @@ class FileProviderImpl final : public FileProvider }; // Svc public: - static Ptr make(cetl::pmr::memory_resource& memory, libcyphal::presentation::Presentation& presentation) + static Ptr make(cetl::pmr::memory_resource& memory, + libcyphal::presentation::Presentation& presentation, + Config::Ptr config) { auto list_srv = makeServer("List", presentation); auto read_srv = makeServer("Read", presentation); @@ -74,6 +78,7 @@ class FileProviderImpl final : public FileProvider return nullptr; } return std::make_unique(memory, + std::move(config), std::move(*list_srv), std::move(*read_srv), std::move(*write_srv), @@ -82,12 +87,14 @@ class FileProviderImpl final : public FileProvider } FileProviderImpl(cetl::pmr::memory_resource& memory, + Config::Ptr config, Svc::List::Server&& list_srv, Svc::Read::Server&& read_srv, Svc::Write::Server&& write_srv, Svc::Modify::Server&& modify_srv, Svc::GetInfo::Server&& get_info_srv) : memory_{memory} + , config_{std::move(config)} , list_srv_{std::move(list_srv)} , read_srv_{std::move(read_srv)} , write_srv_{std::move(write_srv)} @@ -96,6 +103,20 @@ class FileProviderImpl final : public FileProvider { logger_->trace("FileProviderImpl()."); + roots_ = config_->getFileServerRoots(); + logger_->debug("There are {} file server roots.", roots_.size()); + for (std::size_t i = 0; i < roots_.size(); ++i) + { + if (const auto real_root = canonicalizePath(roots_[i])) + { + logger_->trace("{:4} '{}' → '{}'", i, roots_[i], *real_root); + } + else + { + logger_->warn("{:4} '{}' → ❌ not found!", i, roots_[i]); + } + } + setupOnRequestCallback(get_info_srv_, [this](const auto& arg) { // return serveGetInfoRequest(arg); @@ -151,6 +172,31 @@ class FileProviderImpl final : public FileProvider return std::string{path_data, file_path.path.size()}; } + static auto convertErrorCode(const int code) -> uavcan::file::Error_1_0::_traits_::TypeOf::value + { + switch (code) + { + case EIO: + return uavcan::file::Error_1_0::IO_ERROR; + case ENOENT: + return uavcan::file::Error_1_0::NOT_FOUND; + case EISDIR: + return uavcan::file::Error_1_0::IS_DIRECTORY; + case ENOSPC: + return uavcan::file::Error_1_0::OUT_OF_SPACE; + case EACCES: + return uavcan::file::Error_1_0::ACCESS_DENIED; + case EINVAL: + return uavcan::file::Error_1_0::INVALID_VALUE; + case ENOTSUP: + return uavcan::file::Error_1_0::NOT_SUPPORTED; + case E2BIG: + return uavcan::file::Error_1_0::FILE_TOO_LARGE; + default: + return uavcan::file::Error_1_0::UNKNOWN_ERROR; + } + } + static cetl::optional canonicalizePath(const std::string& path) { char* const resolved_path = ::realpath(path.c_str(), nullptr); @@ -165,74 +211,137 @@ class FileProviderImpl final : public FileProvider return result; } + static cetl::optional buildAndValidateRootWithPath(const std::string& root, const std::string& file) + { + if (const auto root_path = canonicalizePath(root)) + { + if (auto sure_file_path = canonicalizePath(root + "/" + file)) + { + auto& file_path = *sure_file_path; + + // This is a security check to ensure that the resolved path is UNDER the real root path. + // The last `/` check is important to ensure that the `root_len` path is a directory. + // + const auto root_len = root_path->size(); + if ((0 == file_path.compare(0, root_len, *root_path)) && (file_path[root_len] == '/')) + { + return sure_file_path; + } + } + } + return cetl::nullopt; + } + + cetl::optional> findFirstValidFile(const std::string& request_path) const + { + for (const auto& root : roots_) + { + if (const auto real_path = buildAndValidateRootWithPath(root, request_path)) + { + // As "best effort" strategy, we skip anything we can't even `stat`. + // + struct stat file_stat + {}; + if (::stat(real_path->c_str(), &file_stat) == 0) + { + return std::make_pair(*real_path, file_stat); + } + } + } + return cetl::nullopt; + } + Svc::GetInfo::Response serveGetInfoRequest(const Svc::GetInfo::CallbackArg& arg) const { const auto request_path = stringFrom(arg.request.path); logger_->trace("'GetInfo' request (from={}, path='{}').", arg.metadata.remote_node_id, request_path); Svc::GetInfo::Response response{&memory_}; - response._error.value = uavcan::file::Error_1_0::NOT_FOUND; - - if (const auto real_path = canonicalizePath(request_path)) + if (const auto path_and_stat = findFirstValidFile(request_path)) { - struct stat file_stat - {}; - if (::stat(real_path->c_str(), &file_stat) == 0) - { - response.size = file_stat.st_size; - response.is_file_not_directory = true; - response.is_readable = true; - response._error.value = uavcan::file::Error_1_0::OK; - } + const auto& file_path = path_and_stat->first; + logger_->debug("Found file info (request='{}', real_path='{}').", request_path, file_path); + const auto& stat = path_and_stat->second; + + response._error.value = uavcan::file::Error_1_0::OK; + response.size = stat.st_size; + response.unix_timestamp_of_last_modification = stat.st_mtime; + response.is_file_not_directory = !S_ISDIR(stat.st_mode); + response.is_link = false; // `::realpath()` resolved all possible links + response.is_readable = (0 == ::access(file_path.c_str(), R_OK)); + response.is_writeable = (0 == ::access(file_path.c_str(), W_OK)); + return response; } + response._error.value = uavcan::file::Error_1_0::NOT_FOUND; return response; } Svc::Read::Response serveReadRequest(const Svc::Read::CallbackArg& arg) const { - const auto request_path = stringFrom(arg.request.path); - const auto real_path = canonicalizePath(request_path); + using DataType = Svc::Read::Response::_traits_::TypeOf::data; + constexpr auto MaxDataSize = DataType::_traits_::ArrayCapacity::value; Svc::Read::Response response{&memory_}; - auto& buffer = response.data.value; - constexpr std::size_t BufferSize = 256U; - buffer.resize(BufferSize); + // Find the first valid file candidate in the list of roots. + // + const auto path_and_stat = findFirstValidFile(stringFrom(arg.request.path)); + if (!path_and_stat) + { + response._error.value = uavcan::file::Error_1_0::NOT_FOUND; + return response; + } + + // Don't allow reading beyond the end of the file. + // + const auto& file_path = path_and_stat->first; + if (arg.request.offset >= path_and_stat->second.st_size) + { + response._error.value = uavcan::file::Error_1_0::OK; + return response; + } + auto& buffer = response.data.value; + const auto bytes_to_read = std::min(path_and_stat->second.st_size - arg.request.offset, MaxDataSize); + buffer.resize(bytes_to_read); try { - std::ifstream file{request_path.data(), std::ios::binary}; + std::ifstream file{file_path.c_str(), std::ios::binary}; + file.exceptions(std::ifstream::failbit | std::ifstream::badbit); file.seekg(static_cast(arg.request.offset)); - file.read(reinterpret_cast(buffer.data()), BufferSize); // NOLINT + file.read(reinterpret_cast(buffer.data()), bytes_to_read); // NOLINT buffer.resize(file.gcount()); response._error.value = uavcan::file::Error_1_0::OK; - } catch (std::exception& ex) + } catch (const std::ios_base::failure& ex) { buffer.clear(); - logger_->error("Failed to read file '{}': {}.", request_path, ex.what()); - response._error.value = uavcan::file::Error_1_0::UNKNOWN_ERROR; + logger_->error("Failed to read file '{}' (err={}): {}.", file_path, ex.code().value(), ex.what()); + response._error.value = convertErrorCode(ex.code().value()); } return response; } cetl::pmr::memory_resource& memory_; + Config::Ptr config_; Svc::List::Server list_srv_; Svc::Read::Server read_srv_; Svc::Write::Server write_srv_; Svc::Modify::Server modify_srv_; Svc::GetInfo::Server get_info_srv_; common::LoggerPtr logger_{common::getLogger("engine")}; + std::vector roots_; }; // FileProviderImpl } // namespace FileProvider::Ptr FileProvider::make(cetl::pmr::memory_resource& memory, - libcyphal::presentation::Presentation& presentation) + libcyphal::presentation::Presentation& presentation, + Config::Ptr config) { - return FileProviderImpl::make(memory, presentation); + return FileProviderImpl::make(memory, presentation, std::move(config)); } } // namespace cyphal diff --git a/src/daemon/engine/cyphal/file_provider.hpp b/src/daemon/engine/cyphal/file_provider.hpp index 1787307..286c50f 100644 --- a/src/daemon/engine/cyphal/file_provider.hpp +++ b/src/daemon/engine/cyphal/file_provider.hpp @@ -6,6 +6,8 @@ #ifndef OCVSMD_DAEMON_ENGINE_CYPHAL_FILE_PROVIDER_HPP_INCLUDED #define OCVSMD_DAEMON_ENGINE_CYPHAL_FILE_PROVIDER_HPP_INCLUDED +#include "config.hpp" + #include #include #include @@ -36,7 +38,8 @@ class FileProvider using Ptr = std::unique_ptr; CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, - libcyphal::presentation::Presentation& presentation); + libcyphal::presentation::Presentation& presentation, + Config::Ptr config); FileProvider(const FileProvider&) = delete; FileProvider(FileProvider&&) noexcept = delete; diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 7a9471d..9a540dd 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -83,7 +83,7 @@ cetl::optional Engine::init() // 5. Bring up various providers. // - file_provider_ = cyphal::FileProvider::make(memory_, *presentation_); + file_provider_ = cyphal::FileProvider::make(memory_, *presentation_, config_); if (file_provider_ == nullptr) { std::string msg = "Failed to create cyphal file provider."; From b1dff9bdc7c6205b7194bb3b0c1d6cf69cad0250 Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 5 Feb 2025 00:48:11 +0200 Subject: [PATCH 112/156] fix style --- src/cli/main.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 5b9d92d..4c4e159 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -45,8 +45,7 @@ void signalHandler(const int sig) void setupSignalHandlers() { - struct sigaction sigbreak - {}; + struct sigaction sigbreak{}; sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); @@ -91,8 +90,7 @@ int main(const int argc, const char** const argv) const std::vector node_ids = {42, 143, 144}; // auto sender = node_cmd_client->restart({node_ids.data(), node_ids.size()}); - auto sender = - node_cmd_client->beginSoftwareUpdate({node_ids.data(), node_ids.size()}, "firmware.bin"); + auto sender = node_cmd_client->beginSoftwareUpdate({node_ids.data(), node_ids.size()}, "firmware.bin"); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); if (const auto* const err = cetl::get_if(&cmd_result)) From 088509b09baeaede04587fd133a0971b7aa9012b Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 5 Feb 2025 07:11:29 +0200 Subject: [PATCH 113/156] fix style --- src/cli/main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 4c4e159..ac6ad8d 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -45,7 +45,8 @@ void signalHandler(const int sig) void setupSignalHandlers() { - struct sigaction sigbreak{}; + struct sigaction sigbreak + {}; sigbreak.sa_handler = &signalHandler; ::sigaction(SIGINT, &sigbreak, nullptr); ::sigaction(SIGTERM, &sigbreak, nullptr); From 60e0dd660ccb47f092f5969741a5e1d552ec6781 Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 5 Feb 2025 08:54:56 +0200 Subject: [PATCH 114/156] switch `read`->`recv` & `write`->`send` --- CMakePresets.json | 1 + src/cli/main.cpp | 2 +- src/common/io/socket_address.cpp | 29 ++++++++++++++++------------- src/common/io/socket_address.hpp | 2 +- src/common/ipc/pipe/socket_base.cpp | 9 +++++---- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 7289cc4..e77475b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -57,6 +57,7 @@ "config-common", "config-linux" ], + "binaryDir": "${sourceDir}/cmake-build-coverage", "cacheVariables": { "CMAKE_C_FLAGS": "--coverage", "CMAKE_CXX_FLAGS": "--coverage", diff --git a/src/cli/main.cpp b/src/cli/main.cpp index ac6ad8d..e62b3c6 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -89,7 +89,7 @@ int main(const int argc, const char** const argv) auto node_cmd_client = daemon->getNodeCommandClient(); - const std::vector node_ids = {42, 143, 144}; + const std::vector node_ids = {42}; // auto sender = node_cmd_client->restart({node_ids.data(), node_ids.size()}); auto sender = node_cmd_client->beginSoftwareUpdate({node_ids.data(), node_ids.size()}, "firmware.bin"); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index 8259b61..b52687c 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -45,21 +45,14 @@ std::pair SocketAddress::getRaw() const noexcept return {&asGenericAddr(), addr_len_}; } -SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const +SocketAddress::SocketResult::Var SocketAddress::socket(const int socket_type) const { - const bool is_stream = (SOCK_STREAM == type); - uint socket_type = type; -#if __linux__ - socket_type |= static_cast(SOCK_NONBLOCK); - socket_type |= static_cast(SOCK_CLOEXEC); -#endif - OwnFd out_fd; const auto& addr_generic = asGenericAddr(); if (const auto err = platform::posixSyscallError([this, socket_type, &addr_generic, &out_fd] { // - const int fd = ::socket(addr_generic.sa_family, static_cast(socket_type), 0); + const int fd = ::socket(addr_generic.sa_family, socket_type, 0); if (fd != -1) { out_fd = OwnFd{fd}; @@ -71,9 +64,19 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int type) const return err; } + if (const auto err = platform::posixSyscallError([this, &out_fd] { + // + // NOLINTNEXTLINE(*-vararg) + return ::fcntl(out_fd.get(), F_SETFL, O_NONBLOCK); + })) + { + getLogger("io")->error("Failed to set socket O_NONBLOCK: {}.", std::strerror(err)); + return err; + } + // Disable Nagle's algorithm for TCP sockets, so that our small IPC packets are sent immediately. // - if (is_stream && isAnyInet()) + if ((SOCK_STREAM == socket_type) && isAnyInet()) { configureNoDelay(out_fd); } @@ -202,11 +205,11 @@ cetl::optional SocketAddress::accept(const OwnFd& server_fd) return cetl::nullopt; } +/// Disables Nagle's algorithm for TCP sockets, so that our small IPC packets are sent immediately. +/// void SocketAddress::configureNoDelay(const OwnFd& fd) { - // TODO: Temporary disabled, but enable when `receiveMessage` could accept partial payloads! - constexpr int enable = 0; - + constexpr int enable = 1; if (const auto err = platform::posixSyscallError([&fd, &enable] { // return ::setsockopt(fd.get(), IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)); diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index 1708558..0fb3353 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -56,7 +56,7 @@ class SocketAddress final using Success = OwnFd; using Var = cetl::variant; }; - SocketResult::Var socket(const int type) const; + SocketResult::Var socket(const int socket_type) const; int bind(const OwnFd& socket_fd) const; int connect(const OwnFd& socket_fd) const; diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp index d18b239..4a8f7f4 100644 --- a/src/common/ipc/pipe/socket_base.cpp +++ b/src/common/ipc/pipe/socket_base.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -60,7 +61,7 @@ int SocketBase::send(const State& state, const Payloads payloads) if (const int err = platform::posixSyscallError([total_size, &state] { // const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; - return ::write(state.fd.get(), &msg_header, sizeof(msg_header)); + return ::send(state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT | MSG_MORE); })) { return err; @@ -72,7 +73,7 @@ int SocketBase::send(const State& state, const Payloads payloads) { if (const int err = platform::posixSyscallError([payload, &state] { // - return ::write(state.fd.get(), payload.data(), payload.size()); + return ::send(state.fd.get(), payload.data(), payload.size(), MSG_DONTWAIT); })) { return err; @@ -91,7 +92,7 @@ int SocketBase::receiveMessage(State& state, std::function&& actio ssize_t bytes_read = 0; if (const auto err = platform::posixSyscallError([&state, &msg_header, &bytes_read] { // - return bytes_read = ::read(state.fd.get(), &msg_header, sizeof(msg_header)); + return bytes_read = ::recv(state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) @@ -127,7 +128,7 @@ int SocketBase::receiveMessage(State& state, std::function&& actio ssize_t read = 0; if (const auto err = platform::posixSyscallError([this, &state, buf_span, &read] { // - return read = ::read(state.fd.get(), buf_span.data(), buf_span.size()); + return read = ::recv(state.fd.get(), buf_span.data(), buf_span.size(), MSG_DONTWAIT); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) From dc45d336e2e1b34fa273dc25295500d542dad771 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 5 Feb 2025 09:52:05 +0200 Subject: [PATCH 115/156] fix macos build --- CMakePresets.json | 5 +++++ src/common/ipc/pipe/socket_base.cpp | 2 +- src/daemon/engine/cyphal/file_provider.cpp | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index e77475b..434904a 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -75,6 +75,11 @@ "cacheVariables": { "CMAKE_C_COMPILER": "clang", "CMAKE_CXX_COMPILER": "clang++" + }, + "vendor": { + "jetbrains.com/clion": { + "toolchain": "Clang19" + } } } ], diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp index 4a8f7f4..07ab556 100644 --- a/src/common/ipc/pipe/socket_base.cpp +++ b/src/common/ipc/pipe/socket_base.cpp @@ -61,7 +61,7 @@ int SocketBase::send(const State& state, const Payloads payloads) if (const int err = platform::posixSyscallError([total_size, &state] { // const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; - return ::send(state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT | MSG_MORE); + return ::send(state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT); })) { return err; diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 46a8bdb..6957a6a 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -23,8 +23,8 @@ #include +#include #include -#include #include #include #include @@ -303,7 +303,7 @@ class FileProviderImpl final : public FileProvider } auto& buffer = response.data.value; - const auto bytes_to_read = std::min(path_and_stat->second.st_size - arg.request.offset, MaxDataSize); + const auto bytes_to_read = std::min(path_and_stat->second.st_size - arg.request.offset, MaxDataSize); buffer.resize(bytes_to_read); try { From 217a31590313b8179681056427560c6a9d8212ae Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 5 Feb 2025 10:01:13 +0200 Subject: [PATCH 116/156] fix style --- src/daemon/engine/cyphal/file_provider.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 6957a6a..b560475 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -296,14 +296,15 @@ class FileProviderImpl final : public FileProvider // Don't allow reading beyond the end of the file. // const auto& file_path = path_and_stat->first; - if (arg.request.offset >= path_and_stat->second.st_size) + const auto& file_stat = path_and_stat->second; + if (arg.request.offset >= file_stat.st_size) { response._error.value = uavcan::file::Error_1_0::OK; return response; } auto& buffer = response.data.value; - const auto bytes_to_read = std::min(path_and_stat->second.st_size - arg.request.offset, MaxDataSize); + const auto bytes_to_read = std::min(file_stat.st_size - arg.request.offset, MaxDataSize); buffer.resize(bytes_to_read); try { From 0f48b6876885b36473c01714d64a9263cbac6621 Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 5 Feb 2025 10:16:17 +0200 Subject: [PATCH 117/156] worst_lateness --- include/ocvsmd/platform/defines.hpp | 3 ++- src/daemon/engine/engine.cpp | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/ocvsmd/platform/defines.hpp b/include/ocvsmd/platform/defines.hpp index 29af730..d0218f9 100644 --- a/include/ocvsmd/platform/defines.hpp +++ b/include/ocvsmd/platform/defines.hpp @@ -61,7 +61,8 @@ void waitPollingUntil(Executor& executor, Predicate predicate) } } - spdlog::debug("Predicate is fulfilled (worst_lateness={}us).", worst_lateness.count()); + spdlog::debug("Predicate is fulfilled (worst_lateness={}us).", + std::chrono::duration_cast(worst_lateness).count()); } } // namespace platform diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 9a540dd..cba500d 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -138,9 +139,11 @@ void Engine::runWhile(const std::function& loop_predicate) { using std::chrono_literals::operator""s; + libcyphal::Duration worst_lateness{0}; while (loop_predicate()) { const auto spin_result = executor_.spinOnce(); + worst_lateness = std::max(worst_lateness, spin_result.worst_lateness); // Poll awaitable resources but awake at least once per second. libcyphal::Duration timeout{1s}; @@ -154,6 +157,8 @@ void Engine::runWhile(const std::function& loop_predicate) spdlog::warn("Failed to poll awaitable resources."); } } + spdlog::debug("Run loop predicate is fulfilled (worst_lateness={}us).", + std::chrono::duration_cast(worst_lateness).count()); } Engine::UniqueId Engine::getUniqueId() const From 57f5bf389eebb0c27cc2b06246d867174d217a29 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 5 Feb 2025 18:29:16 +0200 Subject: [PATCH 118/156] proper logging at file provider --- .clang-tidy | 1 + init.d/ocvsmd.toml | 8 +- src/cli/setup_logging.hpp | 5 +- src/common/io/socket_address.cpp | 136 +++++++++++++++--- src/common/io/socket_address.hpp | 28 +++- src/common/ipc/pipe/socket_server.cpp | 2 +- src/daemon/engine/cyphal/file_provider.cpp | 120 +++++++++++++--- .../engine/cyphal/udp_transport_bag.hpp | 5 +- src/daemon/main.cpp | 12 +- src/daemon/setup_logging.hpp | 5 +- 10 files changed, 260 insertions(+), 62 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 899da63..87889bf 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -23,6 +23,7 @@ Checks: >- -modernize-use-constraints, -modernize-use-default-member-init, -modernize-use-nodiscard, + -modernize-use-starts-ends-with, -readability-avoid-const-params-in-decls, -readability-identifier-length, -*-use-trailing-return-type, diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index 7a15302..e6a1051 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -10,11 +10,11 @@ unique_id = [] # Cyphal transport layer settings. [cyphal.transport] # List of redundant interfaces for the Cyphal network. -# Currently, only one interface is supported. +# Currently, only one (the first) interface is supported. # Supported formats: -# - 'udp:' +# - 'udp://' interfaces = [ - 'udp:127.0.0.1', + 'udp://127.0.0.1', ] # File Server settings. @@ -28,7 +28,7 @@ roots = [ # IPC server settings. [ipc] # Connection strings for the IPC server. -# Currently, only one connection is supported. +# Currently, only one (the first) connection is supported. # Supported formats: # - 'tcp://*:' # - 'tcp://:' diff --git a/src/cli/setup_logging.hpp b/src/cli/setup_logging.hpp index f731eca..239c372 100644 --- a/src/cli/setup_logging.hpp +++ b/src/cli/setup_logging.hpp @@ -65,11 +65,12 @@ inline void loadFlushLevels(const std::string& flush_levels) /// inline void loadArgvFlushLevels(const int argc, const char** const argv) { - const std::string spdlog_level_prefix = "SPDLOG_FLUSH_LEVEL="; + static const std::string spdlog_level_prefix = "SPDLOG_FLUSH_LEVEL="; + for (int i = 1; i < argc; i++) { const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) - if (arg_str.find(spdlog_level_prefix) == 0) + if (0 == arg_str.compare(0, spdlog_level_prefix.size(), spdlog_level_prefix)) { const auto levels_str = arg_str.substr(spdlog_level_prefix.size()); loadFlushLevels(levels_str); diff --git a/src/common/io/socket_address.cpp b/src/common/io/socket_address.cpp index b52687c..fad7451 100644 --- a/src/common/io/socket_address.cpp +++ b/src/common/io/socket_address.cpp @@ -11,7 +11,10 @@ #include +#include + #include +#include #include #include #include @@ -45,6 +48,75 @@ std::pair SocketAddress::getRaw() const noexcept return {&asGenericAddr(), addr_len_}; } +std::uint16_t SocketAddress::getPort() const +{ + switch (asGenericAddr().sa_family) + { + case AF_INET: + return ntohs(asInetAddr().sin_port); + case AF_INET6: + return ntohs(asInet6Addr().sin6_port); + default: + return 0; + } +} + +std::pair SocketAddress::getUnixPrefixAndPath() const +{ + CETL_DEBUG_ASSERT(isUnix(), ""); + CETL_DEBUG_ASSERT(addr_len_ >= offsetof(sockaddr_un, sun_path), ""); + + if (const auto path_len = addr_len_ - offsetof(sockaddr_un, sun_path)) + { + const auto& addr_un = asUnixAddr(); + if (addr_un.sun_path[0] == '\0') + { + // NOLINTNEXTLINE(*-array-to-pointer-decay, *-no-array-decay, *-pointer-arithmetic) + return {"unix-abstract:", std::string{&addr_un.sun_path[1], path_len - 1}}; + } + + // NOLINTNEXTLINE(*-array-to-pointer-decay, *-no-array-decay) + return {"unix:", std::string{addr_un.sun_path, path_len}}; + } + return {"unix:", ""}; +} + +std::string SocketAddress::toString() const +{ + if (is_wildcard_) + { + return fmt::format("*:{}", getPort()); + } + + switch (asGenericAddr().sa_family) + { + case AF_INET: { + std::array buf{}; + if (const auto* addr = ::inet_ntop(AF_INET, &asInetAddr().sin_addr, buf.data(), buf.size())) + { + return fmt::format("{}:{}", addr, getPort()); + } + break; + } + case AF_INET6: { + std::array buf{}; + if (const auto* addr = ::inet_ntop(AF_INET6, &asInet6Addr().sin6_addr, buf.data(), buf.size())) + { + return fmt::format("[{}]:{}", addr, getPort()); + } + break; + } + case AF_UNIX: { + const auto prefix_and_path = getUnixPrefixAndPath(); + return prefix_and_path.first + prefix_and_path.second; + } + default: + break; + } + + return fmt::format("(family={})", asGenericAddr().sa_family); +} + SocketAddress::SocketResult::Var SocketAddress::socket(const int socket_type) const { OwnFd out_fd; @@ -64,13 +136,13 @@ SocketAddress::SocketResult::Var SocketAddress::socket(const int socket_type) co return err; } - if (const auto err = platform::posixSyscallError([this, &out_fd] { + if (const auto err = platform::posixSyscallError([&out_fd] { // // NOLINTNEXTLINE(*-vararg) return ::fcntl(out_fd.get(), F_SETFL, O_NONBLOCK); })) { - getLogger("io")->error("Failed to set socket O_NONBLOCK: {}.", std::strerror(err)); + getLogger("io")->error("Failed to fcntl(O_NONBLOCK) socket: {}.", std::strerror(err)); return err; } @@ -143,13 +215,19 @@ cetl::optional SocketAddress::accept(const OwnFd& server_fd) while (true) { addr_len_ = sizeof(addr_storage_); -#if __linux__ - OwnFd client_fd{::accept4(server_fd.get(), &asGenericAddr(), &addr_len_, SOCK_NONBLOCK | SOCK_CLOEXEC)}; -#else OwnFd client_fd{::accept(server_fd.get(), &asGenericAddr(), &addr_len_)}; -#endif if (client_fd.get() >= 0) { + if (const auto err = platform::posixSyscallError([&client_fd] { + // + // NOLINTNEXTLINE(*-vararg) + return ::fcntl(client_fd.get(), F_SETFL, O_NONBLOCK); + })) + { + getLogger("io")->warn("Failed to fcntl(O_NONBLOCK) accept socket: {}.", std::strerror(err)); + return cetl::nullopt; + } + // Disable Nagle's algorithm for TCP sockets, so that our small IPC packets are sent immediately. // if (isAnyInet()) @@ -223,24 +301,42 @@ void SocketAddress::configureNoDelay(const OwnFd& fd) } } -SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, const std::uint16_t port_hint) +SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& conn_str, const std::uint16_t port_hint) { // Unix domain? // - if (auto result = tryParseAsUnixDomain(str)) + if (auto result = tryParseAsUnixDomain(conn_str)) + { + return *result; + } + if (auto result = tryParseAsAbstractUnixDomain(conn_str)) { return *result; } - if (auto result = tryParseAsAbstractUnixDomain(str)) + if (auto result = tryParseAsTcpAddress(conn_str, port_hint)) { return *result; } + getLogger("io")->error("Unsupported connection string format (conn_str='{}').", conn_str); + return EINVAL; +} + +cetl::optional SocketAddress::tryParseAsTcpAddress(const std::string& conn_str, + const std::uint16_t port_hint) +{ + static const std::string tcp_prefix = "tcp://"; + if (0 != conn_str.compare(0, tcp_prefix.size(), tcp_prefix)) + { + return cetl::nullopt; + } + const auto addr_str = conn_str.substr(tcp_prefix.size()); + // Extract the family, host, and port. // std::string host; std::uint16_t port = port_hint; - const int family = extractFamilyHostAndPort(str, host, port); + const int family = extractFamilyHostAndPort(addr_str, host, port); if (family == AF_UNSPEC) { return EINVAL; @@ -277,7 +373,7 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con return result; } case 0: { - getLogger("io")->error("Unsupported address (addr='{}').", host); + getLogger("io")->error("Unsupported ip address format (addr='{}').", host); return EINVAL; } default: { @@ -288,13 +384,14 @@ SocketAddress::ParseResult::Var SocketAddress::parse(const std::string& str, con } } -cetl::optional SocketAddress::tryParseAsUnixDomain(const std::string& str) +cetl::optional SocketAddress::tryParseAsUnixDomain(const std::string& conn_str) { - if (0 != str.find("unix:")) // NOLINT(modernize-use-starts-ends-with) + static const std::string unix_prefix = "unix:"; + if (0 != conn_str.compare(0, unix_prefix.size(), unix_prefix)) { return cetl::nullopt; } - const auto path = str.substr(std::strlen("unix:")); + const auto path = conn_str.substr(unix_prefix.size()); SocketAddress result{}; auto& result_un = result.asUnixAddr(); @@ -303,7 +400,7 @@ cetl::optional SocketAddress::tryParseAsUnixDom // Reserve one byte for the null terminator. if ((path.size() + 1) > sizeof(result_un.sun_path)) { - getLogger("io")->error("Unix domain path is too long (path='{}').", str); + getLogger("io")->error("Unix domain path is too long (path='{}').", conn_str); return EINVAL; } @@ -314,13 +411,14 @@ cetl::optional SocketAddress::tryParseAsUnixDom return result; } -cetl::optional SocketAddress::tryParseAsAbstractUnixDomain(const std::string& str) +cetl::optional SocketAddress::tryParseAsAbstractUnixDomain(const std::string& conn_str) { - if (0 != str.find("unix-abstract:")) // NOLINT(modernize-use-starts-ends-with) + static const std::string unix_prefix = "unix-abstract:"; + if (0 != conn_str.compare(0, unix_prefix.size(), unix_prefix)) { return cetl::nullopt; } - const auto path = str.substr(std::strlen("unix-abstract:")); + const auto path = conn_str.substr(unix_prefix.size()); SocketAddress result{}; auto& result_un = result.asUnixAddr(); @@ -329,7 +427,7 @@ cetl::optional SocketAddress::tryParseAsAbstrac // Reserve +1 byte for the null terminator. Not required for abstract domain but it is harmless. if ((path.size() + 1) > (sizeof(result_un.sun_path) - 1)) // `-1` b/c path starts at `[1]` (see `memcpy` below). { - getLogger("io")->error("Unix domain path is too long (path='{}').", str); + getLogger("io")->error("Unix domain path is too long (path='{}').", conn_str); return EINVAL; } diff --git a/src/common/io/socket_address.hpp b/src/common/io/socket_address.hpp index 0fb3353..c38c4ba 100644 --- a/src/common/io/socket_address.hpp +++ b/src/common/io/socket_address.hpp @@ -33,11 +33,12 @@ class SocketAddress final using Success = SocketAddress; using Var = cetl::variant; }; - static ParseResult::Var parse(const std::string& str, const std::uint16_t port_hint); + static ParseResult::Var parse(const std::string& conn_str, const std::uint16_t port_hint); SocketAddress() noexcept; std::pair getRaw() const noexcept; + std::uint16_t getPort() const; bool isUnix() const noexcept { @@ -50,6 +51,8 @@ class SocketAddress final return (family == AF_INET) || (family == AF_INET6); } + std::string toString() const; + struct SocketResult { using Failure = int; // aka errno @@ -64,11 +67,15 @@ class SocketAddress final private: static void configureNoDelay(const OwnFd& fd); - static cetl::optional tryParseAsUnixDomain(const std::string& str); - static cetl::optional tryParseAsAbstractUnixDomain(const std::string& str); + static cetl::optional tryParseAsUnixDomain(const std::string& conn_str); + static cetl::optional tryParseAsAbstractUnixDomain(const std::string& conn_str); + static cetl::optional tryParseAsTcpAddress(const std::string& conn_str, + const std::uint16_t port_hint); static int extractFamilyHostAndPort(const std::string& str, std::string& host, std::uint16_t& port); static cetl::optional tryParseAsWildcard(const std::string& host, const std::uint16_t port); + std::pair getUnixPrefixAndPath() const; + sockaddr& asGenericAddr() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) @@ -84,16 +91,31 @@ class SocketAddress final // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return reinterpret_cast(addr_storage_); } + const sockaddr_un& asUnixAddr() const + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } sockaddr_in& asInetAddr() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return reinterpret_cast(addr_storage_); } + const sockaddr_in& asInetAddr() const + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } sockaddr_in6& asInet6Addr() { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return reinterpret_cast(addr_storage_); } + const sockaddr_in6& asInet6Addr() const + { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + return reinterpret_cast(addr_storage_); + } bool is_wildcard_; socklen_t addr_len_; diff --git a/src/common/ipc/pipe/socket_server.cpp b/src/common/ipc/pipe/socket_server.cpp index 6fc81c1..d56b4c5 100644 --- a/src/common/ipc/pipe/socket_server.cpp +++ b/src/common/ipc/pipe/socket_server.cpp @@ -137,7 +137,7 @@ void SocketServer::handleAccept() const ClientId new_client_id = ++unique_client_id_counter_; // Log to default logger (syslog) the client connection. - getLogger("")->debug("New client connection (id={}).", new_client_id); + getLogger("")->debug("New client connection (id={}, addr='{}').", new_client_id, client_address.toString()); const int raw_fd = client_fd->get(); CETL_DEBUG_ASSERT(raw_fd != -1, ""); diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index b560475..8c51079 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -177,6 +177,7 @@ class FileProviderImpl final : public FileProvider switch (code) { case EIO: + case EPERM: return uavcan::file::Error_1_0::IO_ERROR; case ENOENT: return uavcan::file::Error_1_0::NOT_FOUND; @@ -253,27 +254,39 @@ class FileProviderImpl final : public FileProvider Svc::GetInfo::Response serveGetInfoRequest(const Svc::GetInfo::CallbackArg& arg) const { - const auto request_path = stringFrom(arg.request.path); - logger_->trace("'GetInfo' request (from={}, path='{}').", arg.metadata.remote_node_id, request_path); - Svc::GetInfo::Response response{&memory_}; - if (const auto path_and_stat = findFirstValidFile(request_path)) + + // Find the first valid file candidate in the list of roots. + // + const auto request_path = stringFrom(arg.request.path); + const auto path_and_stat = findFirstValidFile(request_path); + if (!path_and_stat) { - const auto& file_path = path_and_stat->first; - logger_->debug("Found file info (request='{}', real_path='{}').", request_path, file_path); - const auto& stat = path_and_stat->second; - - response._error.value = uavcan::file::Error_1_0::OK; - response.size = stat.st_size; - response.unix_timestamp_of_last_modification = stat.st_mtime; - response.is_file_not_directory = !S_ISDIR(stat.st_mode); - response.is_link = false; // `::realpath()` resolved all possible links - response.is_readable = (0 == ::access(file_path.c_str(), R_OK)); - response.is_writeable = (0 == ::access(file_path.c_str(), W_OK)); + logger_->warn( // + "'GetInfo' file not found (node={}, path='{}').", + arg.metadata.remote_node_id, + request_path); + + response._error.value = uavcan::file::Error_1_0::NOT_FOUND; return response; } + const auto& file_path = path_and_stat->first; + const auto& file_stat = path_and_stat->second; - response._error.value = uavcan::file::Error_1_0::NOT_FOUND; + logger_->debug( // + "'GetInfo' found file info (node={}, path='{}', size={}, real='{}').", + arg.metadata.remote_node_id, + request_path, + file_stat.st_size, + file_path); + + response._error.value = uavcan::file::Error_1_0::OK; + response.size = file_stat.st_size; + response.unix_timestamp_of_last_modification = file_stat.st_mtime; + response.is_file_not_directory = !S_ISDIR(file_stat.st_mode); + response.is_link = false; // `::realpath()` resolved all possible links + response.is_readable = (0 == ::access(file_path.c_str(), R_OK)); + response.is_writeable = (0 == ::access(file_path.c_str(), W_OK)); return response; } @@ -283,26 +296,49 @@ class FileProviderImpl final : public FileProvider constexpr auto MaxDataSize = DataType::_traits_::ArrayCapacity::value; Svc::Read::Response response{&memory_}; + response._error.value = uavcan::file::Error_1_0::OK; // Find the first valid file candidate in the list of roots. // - const auto path_and_stat = findFirstValidFile(stringFrom(arg.request.path)); + const auto request_path = stringFrom(arg.request.path); + const auto path_and_stat = findFirstValidFile(request_path); if (!path_and_stat) { + logger_->warn( // + "'Read' file not found (node={}, path='{}', off=0x{:X}).", + arg.metadata.remote_node_id, + request_path, + arg.request.offset); + response._error.value = uavcan::file::Error_1_0::NOT_FOUND; return response; } + const auto& file_path = path_and_stat->first; + const auto& file_stat = path_and_stat->second; // Don't allow reading beyond the end of the file. // - const auto& file_path = path_and_stat->first; - const auto& file_stat = path_and_stat->second; if (arg.request.offset >= file_stat.st_size) { - response._error.value = uavcan::file::Error_1_0::OK; + logger_->debug( // + "'Read' eof (node={}, path='{}', off=0x{:X}, eof=0x{:X}, real='{}').", + arg.metadata.remote_node_id, + request_path, + arg.request.offset, + file_stat.st_size, + file_path); + return response; } + // Read the next chunk of data at the given offset. + // + // Currently, we don't have any "state" in this file provider, + // so we match, validate and re-open the file every time from scratch. + // OS normally heavily caches file system entries and data anyway. + // If in the future we need to optimize this, we can add a configurable "soft" caching here + // (f.e. LRU + flush on change of roots and maybe on some expiration period; refresh on 'GetInfo'). + // auto& buffer = response.data.value; const auto bytes_to_read = std::min(file_stat.st_size - arg.request.offset, MaxDataSize); buffer.resize(bytes_to_read); @@ -310,15 +346,53 @@ class FileProviderImpl final : public FileProvider { std::ifstream file{file_path.c_str(), std::ios::binary}; file.exceptions(std::ifstream::failbit | std::ifstream::badbit); + file.seekg(static_cast(arg.request.offset)); file.read(reinterpret_cast(buffer.data()), bytes_to_read); // NOLINT + + // The read count should be the same as `bytes_to_read` but let's be sure. + // The `resize` method does nothing if the new size is the same as the old one. buffer.resize(file.gcount()); - response._error.value = uavcan::file::Error_1_0::OK; + CETL_DEBUG_ASSERT(buffer.size() <= bytes_to_read, ""); + + // Limit the log output to the first and/or last request in the sequence. Otherwise, + // the log will be flooded with almost the same messages - not useful even under trace level. + // It's still possible to do the flooding (if one keeps reading the first/last chunk over and over), + // but it's an edge case (anyway, we have a log file limit and rotation policy in place). + // + if ((arg.request.offset + buffer.size()) >= file_stat.st_size) // last? + { + logger_->debug( // + "'Read' last (node={}, path='{}', off=0x{:X}, eof=0x{:X}, real='{}').", + arg.metadata.remote_node_id, + request_path, + arg.request.offset, + file_stat.st_size, + file_path); + } + else if (arg.request.offset == 0) // first? + { + logger_->debug( // + "'Read' first (node={}, path='{}', eof=0x{:X}, real='{}')…", + arg.metadata.remote_node_id, + request_path, + file_stat.st_size, + file_path); + } } catch (const std::ios_base::failure& ex) { - buffer.clear(); - logger_->error("Failed to read file '{}' (err={}): {}.", file_path, ex.code().value(), ex.what()); + logger_->warn( // + "'Read' failed (node={}, path='{}', off=0x{:X}, eof=0x{:X}, real='{}', err={}): {}.", + arg.metadata.remote_node_id, + request_path, + arg.request.offset, + file_stat.st_size, + file_path, + ex.code().value(), + ex.what()); + + buffer.clear(); // No need to send any garbage data. response._error.value = convertErrorCode(ex.code().value()); } return response; diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index 3b235f1..630eaec 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -43,13 +43,14 @@ struct UdpTransportBag final libcyphal::transport::udp::IUdpTransport* create(const Config::Ptr& config) { + static const std::string udp_prefix = "udp://"; + CETL_DEBUG_ASSERT(config, ""); std::string udp_ifaces; for (const auto& iface : config->getCyphalTransportInterfaces()) { - const static std::string udp_prefix = "udp:"; - if (0 == iface.find(udp_prefix)) + if (0 == iface.compare(0, udp_prefix.size(), udp_prefix)) { udp_ifaces += iface.substr(udp_prefix.size()); udp_ifaces += ","; diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp index ba63b5c..cc8463c 100644 --- a/src/daemon/main.cpp +++ b/src/daemon/main.cpp @@ -301,15 +301,15 @@ ocvsmd::daemon::engine::Config::Ptr loadConfig(const int err_fd, const int argc, const char** const argv) { - const std::string cfg_file_nm = "ocvsmd.toml"; + static const std::string cfg_file_name = "ocvsmd.toml"; + static const std::string config_file_prefix = "CONFIG_FILE="; + const std::string cfg_file_dir = is_daemonized ? "/etc/ocvsmd/" : "./"; - auto cfg_file_path = cfg_file_dir + cfg_file_nm; - // - const std::string config_file_prefix = "CONFIG_FILE="; + auto cfg_file_path = cfg_file_dir + cfg_file_name; for (int i = 1; i < argc; i++) { - const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) - if (arg_str.find(config_file_prefix) == 0) // NOLINT(modernize-use-starts-ends-with) + const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + if (0 == arg_str.compare(0, config_file_prefix.size(), config_file_prefix)) { cfg_file_path = arg_str.substr(config_file_prefix.size()); } diff --git a/src/daemon/setup_logging.hpp b/src/daemon/setup_logging.hpp index 2707181..e5cec31 100644 --- a/src/daemon/setup_logging.hpp +++ b/src/daemon/setup_logging.hpp @@ -73,11 +73,12 @@ inline void loadFlushLevels(const std::string& flush_levels) /// inline void loadArgvFlushLevels(const int argc, const char** const argv) { - const std::string spdlog_level_prefix = "SPDLOG_FLUSH_LEVEL="; + static const std::string spdlog_level_prefix = "SPDLOG_FLUSH_LEVEL="; + for (int i = 1; i < argc; i++) { const std::string arg_str = argv[i]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) - if (arg_str.find(spdlog_level_prefix) == 0) + if (0 == arg_str.compare(0, spdlog_level_prefix.size(), spdlog_level_prefix)) { const auto levels_str = arg_str.substr(spdlog_level_prefix.size()); loadFlushLevels(levels_str); From 7d40596164ec5c49f42dbfe3ad8533904f0f5854 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 5 Feb 2025 18:51:17 +0200 Subject: [PATCH 119/156] fix unit tests --- test/common/io/test_socket_address.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/common/io/test_socket_address.cpp b/test/common/io/test_socket_address.cpp index 2f61143..a54b424 100644 --- a/test/common/io/test_socket_address.cpp +++ b/test/common/io/test_socket_address.cpp @@ -130,7 +130,7 @@ TEST_F(TestSocketAddress, parse_ipv4) using Result = SocketAddress::ParseResult; { - const std::string test_addr = "127.0.0.1"; + const std::string test_addr = "tcp://127.0.0.1"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); @@ -145,7 +145,7 @@ TEST_F(TestSocketAddress, parse_ipv4) // try with port { - const std::string test_addr = "192.168.1.123:8080"; + const std::string test_addr = "tcp://192.168.1.123:8080"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 80); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); @@ -158,14 +158,14 @@ TEST_F(TestSocketAddress, parse_ipv4) // try invalid ip { - const std::string test_addr = "127.0.0.256"; + const std::string test_addr = "tcp://127.0.0.256"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 80); ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); } // try unsupported { - const std::string test_addr = "localhost"; + const std::string test_addr = "tcp://localhost"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 80); ASSERT_THAT(maybe_socket_addr, VariantWith(EINVAL)); } @@ -176,7 +176,7 @@ TEST_F(TestSocketAddress, parse_ipv6) using Result = SocketAddress::ParseResult; { - const std::string test_addr = "::1"; + const std::string test_addr = "tcp://::1"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); @@ -192,7 +192,7 @@ TEST_F(TestSocketAddress, parse_ipv6) // try with port { - const std::string test_addr = "[2001:db8::1]:8080"; + const std::string test_addr = "tcp://[2001:db8::1]:8080"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 0); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); @@ -207,16 +207,16 @@ TEST_F(TestSocketAddress, parse_ipv6) // try invalid { // missing closing bracket - ASSERT_THAT(SocketAddress::parse("[::1", 0), VariantWith(EINVAL)); + ASSERT_THAT(SocketAddress::parse("tcp://[::1", 0), VariantWith(EINVAL)); // missing colon after bracket - ASSERT_THAT(SocketAddress::parse("[::1]8080", 0), VariantWith(EINVAL)); + ASSERT_THAT(SocketAddress::parse("tcp://[::1]8080", 0), VariantWith(EINVAL)); // invalid port number - ASSERT_THAT(SocketAddress::parse("[::1]:80_80", 0), VariantWith(EINVAL)); + ASSERT_THAT(SocketAddress::parse("tcp://[::1]:80_80", 0), VariantWith(EINVAL)); // too big port number - ASSERT_THAT(SocketAddress::parse("[::1]:65536", 0), VariantWith(EINVAL)); + ASSERT_THAT(SocketAddress::parse("tcp://[::1]:65536", 0), VariantWith(EINVAL)); } } @@ -225,7 +225,7 @@ TEST_F(TestSocketAddress, parse_wildcard) using Result = SocketAddress::ParseResult; { - const std::string test_addr = "*"; + const std::string test_addr = "tcp://*"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); @@ -241,7 +241,7 @@ TEST_F(TestSocketAddress, parse_wildcard) // try with port { - const std::string test_addr = "*:8080"; + const std::string test_addr = "tcp://*:8080"; auto maybe_socket_addr = SocketAddress::parse(test_addr, 0x1234); ASSERT_THAT(maybe_socket_addr, VariantWith(_)); auto socket_address = cetl::get(maybe_socket_addr); From ffb4b1a0a48038fe4007847630fff8834c92563a Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 7 Feb 2025 13:55:50 +0200 Subject: [PATCH 120/156] enabled socketcan transport support --- .clang-tidy | 1 + .gitmodules | 4 + init.d/ocvsmd.toml | 6 +- src/daemon/engine/CMakeLists.txt | 9 + .../engine/cyphal/any_transport_bag.hpp | 50 +++ .../engine/cyphal/can_transport_bag.hpp | 127 +++++++ src/daemon/engine/cyphal/file_provider.cpp | 2 +- .../engine/cyphal/transport_helpers.hpp | 190 ++++++++++ .../engine/cyphal/udp_transport_bag.hpp | 77 ++-- src/daemon/engine/engine.cpp | 31 +- src/daemon/engine/engine.hpp | 6 +- src/daemon/engine/platform/can/can_media.hpp | 337 ++++++++++++++++++ src/daemon/engine/platform/can/socketcan.c | 299 ++++++++++++++++ src/daemon/engine/platform/can/socketcan.h | 88 +++++ src/daemon/engine/platform/udp/udp_media.hpp | 5 +- submodules/libcyphal | 2 +- 16 files changed, 1195 insertions(+), 39 deletions(-) create mode 100644 src/daemon/engine/cyphal/any_transport_bag.hpp create mode 100644 src/daemon/engine/cyphal/can_transport_bag.hpp create mode 100644 src/daemon/engine/cyphal/transport_helpers.hpp create mode 100644 src/daemon/engine/platform/can/can_media.hpp create mode 100644 src/daemon/engine/platform/can/socketcan.c create mode 100644 src/daemon/engine/platform/can/socketcan.h diff --git a/.clang-tidy b/.clang-tidy index 87889bf..3cc20b7 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -26,6 +26,7 @@ Checks: >- -modernize-use-starts-ends-with, -readability-avoid-const-params-in-decls, -readability-identifier-length, + -*-use-designated-initializers, -*-use-trailing-return-type, -*-named-parameter, -misc-include-cleaner, diff --git a/.gitmodules b/.gitmodules index 0b13fe0..2dd7245 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,6 +5,10 @@ path = submodules/libudpard url = https://github.com/OpenCyphal/libudpard branch = v2 +[submodule "submodules/libcanard"] + path = submodules/libcanard + url = https://github.com/UAVCAN/libcanard + branch = v4 [submodule "submodules/nunavut"] path = submodules/nunavut url = https://github.com/OpenCyphal/nunavut.git diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index e6a1051..da0f34c 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -9,10 +9,12 @@ unique_id = [] # Cyphal transport layer settings. [cyphal.transport] -# List of redundant interfaces for the Cyphal network. -# Currently, only one (the first) interface is supported. +# List of interfaces for the Cyphal network. +# Up to three redundant homogeneous interfaces are supported. +# UDP has priorioty over CAN if both types are present. # Supported formats: # - 'udp://' +# - 'socketcan:' interfaces = [ 'udp://127.0.0.1', ] diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index b8eab8d..56ef3e1 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -36,15 +36,24 @@ target_include_directories(udpard INTERFACE SYSTEM ${submodules_dir}/libudpard/libudpard ) +add_library(canard + ${submodules_dir}/libcanard/libcanard/canard.c +) +target_include_directories(canard + INTERFACE SYSTEM ${submodules_dir}/libcanard/libcanard +) + add_library(ocvsmd_engine config.cpp cyphal/file_provider.cpp engine.cpp + platform/can/socketcan.c platform/udp/udp.c svc/node/exec_cmd_service.cpp ) target_link_libraries(ocvsmd_engine PUBLIC udpard + PUBLIC canard PUBLIC ${engine_transpiled} PUBLIC ocvsmd_common ) diff --git a/src/daemon/engine/cyphal/any_transport_bag.hpp b/src/daemon/engine/cyphal/any_transport_bag.hpp new file mode 100644 index 0000000..a23a7b0 --- /dev/null +++ b/src/daemon/engine/cyphal/any_transport_bag.hpp @@ -0,0 +1,50 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_CYPHAL_ANY_TRANSPORT_BAG_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_CYPHAL_ANY_TRANSPORT_BAG_HPP_INCLUDED + +#include +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace cyphal +{ + +/// Represents storage of some (UDP, CAN) libcyphal transport and its media. +/// +class AnyTransportBag +{ +public: + using Ptr = std::unique_ptr; + using Transport = libcyphal::transport::ITransport; + + AnyTransportBag(const AnyTransportBag&) = delete; + AnyTransportBag(AnyTransportBag&&) noexcept = delete; + AnyTransportBag& operator=(const AnyTransportBag&) = delete; + AnyTransportBag& operator=(AnyTransportBag&&) noexcept = delete; + + virtual ~AnyTransportBag() = default; + + virtual Transport& getTransport() const = 0; + +protected: + AnyTransportBag() = default; + +}; // AnyTransportBag + +} // namespace cyphal +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_CYPHAL_ANY_TRANSPORT_BAG_HPP_INCLUDED diff --git a/src/daemon/engine/cyphal/can_transport_bag.hpp b/src/daemon/engine/cyphal/can_transport_bag.hpp new file mode 100644 index 0000000..e8c50c7 --- /dev/null +++ b/src/daemon/engine/cyphal/can_transport_bag.hpp @@ -0,0 +1,127 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_CYPHAL_CAN_TRANSPORT_BAG_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_CYPHAL_CAN_TRANSPORT_BAG_HPP_INCLUDED + +#include "any_transport_bag.hpp" +#include "config.hpp" +#include "platform/can/can_media.hpp" +#include "transport_helpers.hpp" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace cyphal +{ + +/// Holds (internally) instance of the CAN transport and its media (if any). +/// +class CanTransportBag final : public AnyTransportBag +{ + /// Defines private specification for making interface unique ptr. + /// + struct Spec + { + explicit Spec() = default; + }; + +public: + Transport& getTransport() const override + { + CETL_DEBUG_ASSERT(transport_, ""); + return *transport_; + } + + static Ptr make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor, const Config::Ptr& config) + { + CETL_DEBUG_ASSERT(config, ""); + + static constexpr std::string can_prefix{"socketcan:"}; + + std::string can_ifaces; + for (const auto& iface : config->getCyphalTransportInterfaces()) + { + if (0 == iface.compare(0, can_prefix.size(), can_prefix)) + { + can_ifaces += iface.substr(can_prefix.size()); + can_ifaces += ","; + } + } + common::getLogger("io")->trace("Attempting to create CAN transport (ifaces='{}')…", can_ifaces); + + auto transport_bag = std::make_unique(Spec{}, memory, executor); + + auto& media_collection = transport_bag->media_collection_; + media_collection.parse(can_ifaces); + if (media_collection.count() == 0) + { + return nullptr; + } + + auto maybe_transport = makeTransport({memory}, executor, media_collection.span(), TxQueueCapacity); + if (const auto* failure = cetl::get_if(&maybe_transport)) + { + (void) failure; + common::getLogger("io")->warn("Failed to create CAN transport."); + return nullptr; + } + transport_bag->transport_ = cetl::get(std::move(maybe_transport)); + + // To support redundancy (multiple homogeneous interfaces), it's important to have a non-default + // handler which "swallows" expected transient failures (by returning `nullopt` result). + // Otherwise, the default Cyphal behavior will fail/interrupt current and future transfers + // if some of its media encounter transient failures - thus breaking the whole redundancy goal, + // namely, maintain communication if at least one of the interfaces is still up and running. + // + transport_bag->transport_->setTransientErrorHandler([](auto&) { return cetl::nullopt; }); + // transport_bag->transport_->setTransientErrorHandler(TransportHelpers::CanTransientErrorReporter{}); + + common::getLogger("io")->debug("Created CAN transport (ifaces={}).", media_collection.count()); + return transport_bag; + } + + CanTransportBag(Spec, cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) + : memory_{memory} + , executor_{executor} + , media_collection_{memory, executor, memory} + { + } + +private: + using TransportPtr = libcyphal::UniquePtr; + + // Our current max `SerializationBufferSizeBytes` is 313 bytes (for `uavcan.node.GetInfo.Response.1.0`) + // Assuming CAN classic presentation MTU of 7 bytes (plus a bit of overhead like CRC and stuff), + // let's calculate the required TX queue capacity, and make it twice to accommodate 2 such messages. + static constexpr std::size_t TxQueueCapacity = 2 * (313U + 8U) / 7U; + + cetl::pmr::memory_resource& memory_; + libcyphal::IExecutor& executor_; + platform::can::CanMediaCollection media_collection_; + TransportPtr transport_; + +}; // CanTransportBag + +} // namespace cyphal +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_CYPHAL_CAN_TRANSPORT_BAG_HPP_INCLUDED diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 8c51079..c20ad27 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -161,7 +161,7 @@ class FileProviderImpl final : public FileProvider { server.setOnRequestCallback([handle = std::forward(handler)](const auto& arg, auto& continuation) { // - constexpr auto timeout = std::chrono::seconds{1}; + constexpr auto timeout = std::chrono::milliseconds{100}; continuation(arg.approx_now + timeout, handle(arg)); }); } diff --git a/src/daemon/engine/cyphal/transport_helpers.hpp b/src/daemon/engine/cyphal/transport_helpers.hpp new file mode 100644 index 0000000..ba5f53c --- /dev/null +++ b/src/daemon/engine/cyphal/transport_helpers.hpp @@ -0,0 +1,190 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_CYPHAL_TRANSPORT_HELPERS_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_CYPHAL_TRANSPORT_HELPERS_HPP_INCLUDED + +#include "logging.hpp" + +#include +#include +#include +#include +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace cyphal +{ + +struct TransportHelpers +{ + struct Printers + { + static std::string describeError(const libcyphal::ArgumentError&) + { + return "ArgumentError"; + } + static std::string describeError(const libcyphal::MemoryError&) + { + return "MemoryError"; + } + static std::string describeError(const libcyphal::transport::AnonymousError&) + { + return "AnonymousError"; + } + static std::string describeError(const libcyphal::transport::CapacityError&) + { + return "CapacityError"; + } + static std::string describeError(const libcyphal::transport::AlreadyExistsError&) + { + return "AlreadyExistsError"; + } + static std::string describeError(const libcyphal::transport::PlatformError& error) + { + return fmt::format("PlatformError(code={})", error->code()); + } + + static std::string describeAnyFailure(const libcyphal::transport::AnyFailure& failure) + { + return cetl::visit([](const auto& error) { return describeError(error); }, failure); + } + + }; // Printers + + struct CanTransientErrorReporter + { + using Report = libcyphal::transport::can::ICanTransport::TransientErrorReport; + + cetl::optional operator()(const Report::Variant& report_var) const + { + cetl::visit([this](const auto& report) { return log(report); }, report_var); + return cetl::nullopt; + } + + private: + void log(const Report::CanardTxPush& report) const + { + logger_->error("Failed to push TX frame to canard (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::CanardRxAccept& report) const + { + logger_->error("Failed to accept RX frame at canard (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::MediaPop& report) const + { + logger_->error("Failed to pop frame from media (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::ConfigureMedia& report) const + { + logger_->error("Failed to configure CAN: {}.", Printers::describeAnyFailure(report.failure)); + } + void log(const Report::MediaConfig& report) const + { + logger_->error("Failed to configure media (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::MediaPush& report) const + { + logger_->error("Failed to push frame to media (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + + common::LoggerPtr logger_{common::getLogger("io")}; + + }; // CanTransientErrorReporter + + struct UdpTransientErrorReporter + { + using Report = libcyphal::transport::udp::IUdpTransport::TransientErrorReport; + + cetl::optional operator()(const Report::Variant& report_var) const + { + cetl::visit([this](const auto& report) { return log(report); }, report_var); + return cetl::nullopt; + } + + private: + void log(const Report::UdpardTxPublish& report) const + { + logger_->error("Failed to TX message frame to udpard (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::UdpardTxRequest& report) const + { + logger_->error("Failed to TX request frame to udpard (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::UdpardTxRespond& report) const + { + logger_->error("Failed to TX response frame to udpard (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::UdpardRxMsgReceive& report) const + { + logger_->error("Failed to accept RX message frame at udpard: {}.", + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::UdpardRxSvcReceive& report) const + { + logger_->error("Failed to accept RX service frame at udpard (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::MediaMakeRxSocket& report) const + { + logger_->error("Failed to make RX socket (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::MediaMakeTxSocket& report) const + { + logger_->error("Failed to make TX socket (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::MediaTxSocketSend& report) const + { + logger_->error("Failed to TX frame to socket (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + void log(const Report::MediaRxSocketReceive& report) const + { + logger_->error("Failed to RX frame from socket (mediaIdx={}): {}.", + report.media_index, + Printers::describeAnyFailure(report.failure)); + } + + common::LoggerPtr logger_{common::getLogger("io")}; + + }; // UdpTransientErrorReporter + +}; // TransportHelpers + +} // namespace cyphal +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_CYPHAL_TRANSPORT_HELPERS_HPP_INCLUDED diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index 630eaec..37f7a43 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -6,8 +6,11 @@ #ifndef OCVSMD_DAEMON_ENGINE_CYPHAL_UDP_TRANSPORT_BAG_HPP_INCLUDED #define OCVSMD_DAEMON_ENGINE_CYPHAL_UDP_TRANSPORT_BAG_HPP_INCLUDED +#include "any_transport_bag.hpp" #include "config.hpp" +#include "logging.hpp" #include "platform/udp/udp_media.hpp" +#include "transport_helpers.hpp" #include #include @@ -32,21 +35,28 @@ namespace cyphal /// Holds (internally) instance of the UDP transport and its media (if any). /// -struct UdpTransportBag final +class UdpTransportBag final : public AnyTransportBag { - UdpTransportBag(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) - : memory_{memory} - , executor_{executor} - , media_collection_{memory, executor, memory} + /// Defines private specification for making interface unique ptr. + /// + struct Spec { - } + explicit Spec() = default; + }; - libcyphal::transport::udp::IUdpTransport* create(const Config::Ptr& config) +public: + Transport& getTransport() const override { - static const std::string udp_prefix = "udp://"; + CETL_DEBUG_ASSERT(transport_, ""); + return *transport_; + } + static Ptr make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor, const Config::Ptr& config) + { CETL_DEBUG_ASSERT(config, ""); + static constexpr std::string udp_prefix{"udp://"}; + std::string udp_ifaces; for (const auto& iface : config->getCyphalTransportInterfaces()) { @@ -56,34 +66,55 @@ struct UdpTransportBag final udp_ifaces += ","; } } + common::getLogger("io")->trace("Attempting to create UDP transport (ifaces='{}')…", udp_ifaces); - media_collection_.parse(udp_ifaces); - auto maybe_udp_transport = makeTransport({memory_}, executor_, media_collection_.span(), TxQueueCapacity); - if (const auto* failure = cetl::get_if(&maybe_udp_transport)) + auto transport_bag = std::make_unique(Spec{}, memory, executor); + + auto& media_collection = transport_bag->media_collection_; + media_collection.parse(udp_ifaces); + if (media_collection.count() == 0) { - (void) failure; return nullptr; } - transport_ = cetl::get>( // - std::move(maybe_udp_transport)); - if (const auto node_id = config->getCyphalAppNodeId()) + auto maybe_transport = makeTransport({memory}, executor, media_collection.span(), TxQueueCapacity); + if (const auto* failure = cetl::get_if(&maybe_transport)) { - transport_->setLocalNodeId(node_id.value()); + (void) failure; + common::getLogger("io")->warn("Failed to create UDP transport."); + return nullptr; } + transport_bag->transport_ = cetl::get(std::move(maybe_transport)); + + // To support redundancy (multiple homogeneous interfaces), it's important to have a non-default + // handler which "swallows" expected transient failures (by returning `nullopt` result). + // Otherwise, the default Cyphal behavior will fail/interrupt current and future transfers + // if some of its media encounter transient failures - thus breaking the whole redundancy goal, + // namely, maintain communication if at least one of the interfaces is still up and running. + // + transport_bag->transport_->setTransientErrorHandler([](auto&) { return cetl::nullopt; }); + // transport_bag->transport_->setTransientErrorHandler(TransportHelpers::UdpTransientErrorReporter{}); + + common::getLogger("io")->debug("Created UDP transport (ifaces={}", media_collection.count()); + return transport_bag; + } - // transport_->setTransientErrorHandler(platform::CommonHelpers::Udp::transientErrorReporter); - - return transport_.get(); + UdpTransportBag(Spec, cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor) + : memory_{memory} + , executor_{executor} + , media_collection_{memory, executor, memory} + { } private: + using TransportPtr = libcyphal::UniquePtr; + static constexpr std::size_t TxQueueCapacity = 16; - cetl::pmr::memory_resource& memory_; - libcyphal::IExecutor& executor_; - platform::udp::UdpMediaCollection media_collection_; - libcyphal::UniquePtr transport_{nullptr}; + cetl::pmr::memory_resource& memory_; + libcyphal::IExecutor& executor_; + platform::udp::UdpMediaCollection media_collection_; + TransportPtr transport_; }; // UdpTransportBag diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index cba500d..29f4159 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -6,7 +6,9 @@ #include "engine.hpp" #include "config.hpp" +#include "cyphal/can_transport_bag.hpp" #include "cyphal/file_provider.hpp" +#include "cyphal/udp_transport_bag.hpp" #include "io/socket_address.hpp" #include "ipc/pipe/server_pipe.hpp" #include "ipc/pipe/socket_server.hpp" @@ -46,19 +48,34 @@ cetl::optional Engine::init() { logger_->trace("Initializing engine..."); - // 1. Create the transport layer object. + // 1. Create the transport layer object (try first UDP, then CAN). + // Set the local node ID if configured. // - auto* const transport_iface = udp_transport_bag_.create(config_); - if (transport_iface == nullptr) + if (auto maybe_udp_transport_bag = cyphal::UdpTransportBag::make(memory_, executor_, config_)) { - std::string msg = "Failed to create cyphal UDP transport."; - logger_->error(msg); - return msg; + any_transport_bag_ = std::move(maybe_udp_transport_bag); + } + else + { + if (auto maybe_can_transport_bag = cyphal::CanTransportBag::make(memory_, executor_, config_)) + { + any_transport_bag_ = std::move(maybe_can_transport_bag); + } + else + { + std::string msg = "Failed to create Cyphal transport."; + logger_->error(msg); + return msg; + } + } + if (const auto node_id = config_->getCyphalAppNodeId()) + { + any_transport_bag_->getTransport().setLocalNodeId(node_id.value()); } // 2. Create the presentation layer object. // - presentation_.emplace(memory_, executor_, *transport_iface); + presentation_.emplace(memory_, executor_, any_transport_bag_->getTransport()); presentation_->setTransferIdMap(&transfer_id_map_); // 3. Create the node object with name. diff --git a/src/daemon/engine/engine.hpp b/src/daemon/engine/engine.hpp index 770f424..13b56d5 100644 --- a/src/daemon/engine/engine.hpp +++ b/src/daemon/engine/engine.hpp @@ -7,8 +7,8 @@ #define OCVSMD_DAEMON_ENGINE_HPP_INCLUDED #include "config.hpp" +#include "cyphal/any_transport_bag.hpp" #include "cyphal/file_provider.hpp" -#include "cyphal/udp_transport_bag.hpp" #include "logging.hpp" #include "ocvsmd/platform/defines.hpp" @@ -69,9 +69,9 @@ class Engine Config::Ptr config_; common::LoggerPtr logger_{common::getLogger("engine")}; - ocvsmd::platform::SingleThreadedExecutor executor_; + platform::SingleThreadedExecutor executor_; cetl::pmr::memory_resource& memory_{*cetl::pmr::get_default_resource()}; - cyphal::UdpTransportBag udp_transport_bag_{memory_, executor_}; + cyphal::AnyTransportBag::Ptr any_transport_bag_; TransferIdMap transfer_id_map_; cetl::optional presentation_; cetl::optional node_; diff --git a/src/daemon/engine/platform/can/can_media.hpp b/src/daemon/engine/platform/can/can_media.hpp new file mode 100644 index 0000000..b871216 --- /dev/null +++ b/src/daemon/engine/platform/can/can_media.hpp @@ -0,0 +1,337 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_PLATFORM_CAN_MEDIA_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_PLATFORM_CAN_MEDIA_HPP_INCLUDED + +#include "ocvsmd/platform/posix_executor_extension.hpp" +#include "ocvsmd/platform/posix_platform_error.hpp" +#include "socketcan.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace platform +{ +namespace can +{ + +class CanMedia final : public libcyphal::transport::can::IMedia +{ +public: + CETL_NODISCARD static cetl::variant make( + cetl::pmr::memory_resource& general_mr, + libcyphal::IExecutor& executor, + const cetl::string_view iface_address_sv, + cetl::pmr::memory_resource& tx_mr) + { + const std::string iface_address{iface_address_sv.data(), iface_address_sv.size()}; + + const SocketCANFD socket_can_rx_fd = ::socketcanOpen(iface_address.c_str(), false); + if (socket_can_rx_fd < 0) + { + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-socket_can_rx_fd}}; + } + + // We gonna register separate callbacks for rx & tx (aka pop & push), + // so at executor (especially in case of the "epoll" one) we need separate file descriptors. + // + const SocketCANFD socket_can_tx_fd = ::socketcanOpen(iface_address.c_str(), false); + if (socket_can_tx_fd < 0) + { + const int error_code = -socket_can_tx_fd; + (void) ::close(socket_can_rx_fd); + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{error_code}}; + } + + return CanMedia{general_mr, executor, socket_can_rx_fd, socket_can_tx_fd, std::move(iface_address), tx_mr}; + } + + ~CanMedia() + { + if (socket_can_rx_fd_ >= 0) + { + (void) ::close(socket_can_rx_fd_); + } + if (socket_can_tx_fd_ >= 0) + { + (void) ::close(socket_can_tx_fd_); + } + } + + CanMedia(const CanMedia&) = delete; + CanMedia& operator=(const CanMedia&) = delete; + CanMedia* operator=(CanMedia&&) noexcept = delete; + + CanMedia(CanMedia&& other) noexcept + : general_mr_{other.general_mr_} + , executor_{other.executor_} + , socket_can_rx_fd_{std::exchange(other.socket_can_rx_fd_, -1)} + , socket_can_tx_fd_{std::exchange(other.socket_can_tx_fd_, -1)} + , iface_address_{std::move(other.iface_address_)} + , tx_mr_{other.tx_mr_} + { + } + + void tryReopen() + { + if (socket_can_rx_fd_ >= 0) + { + (void) ::close(socket_can_rx_fd_); + socket_can_rx_fd_ = -1; + } + if (socket_can_tx_fd_ >= 0) + { + (void) ::close(socket_can_tx_fd_); + socket_can_tx_fd_ = -1; + } + + const SocketCANFD socket_can_rx_fd = ::socketcanOpen(iface_address_.c_str(), false); + if (socket_can_rx_fd >= 0) + { + socket_can_rx_fd_ = socket_can_rx_fd; + } + + const SocketCANFD socket_can_tx_fd = ::socketcanOpen(iface_address_.c_str(), false); + if (socket_can_tx_fd >= 0) + { + socket_can_tx_fd_ = socket_can_tx_fd; + } + } + +private: + using Filter = libcyphal::transport::can::Filter; + using Filters = libcyphal::transport::can::Filters; + + CanMedia(cetl::pmr::memory_resource& general_mr, + libcyphal::IExecutor& executor, + const SocketCANFD socket_can_rx_fd, + const SocketCANFD socket_can_tx_fd, + std::string iface_address, + cetl::pmr::memory_resource& tx_mr) + : general_mr_{general_mr} + , executor_{executor} + , socket_can_rx_fd_{socket_can_rx_fd} + , socket_can_tx_fd_{socket_can_tx_fd} + , iface_address_{std::move(iface_address)} + , tx_mr_{tx_mr} + { + } + + CETL_NODISCARD libcyphal::IExecutor::Callback::Any registerAwaitableCallback( + libcyphal::IExecutor::Callback::Function&& function, + const ocvsmd::platform::IPosixExecutorExtension::Trigger::Variant& trigger) const + { + auto* const posix_executor_ext = cetl::rtti_cast(&executor_); + if (nullptr == posix_executor_ext) + { + return {}; + } + + return posix_executor_ext->registerAwaitableCallback(std::move(function), trigger); + } + + // MARK: - IMedia + + std::size_t getMtu() const noexcept override + { + return CANARD_MTU_CAN_CLASSIC; + } + + cetl::optional setFilters(const Filters filters) noexcept override + { + std::vector can_filters; + can_filters.reserve(filters.size()); + std::transform(filters.begin(), filters.end(), std::back_inserter(can_filters), [](const Filter filter) { + // + return CanardFilter{filter.id, filter.mask}; + }); + + const std::int16_t result = ::socketcanFilter(socket_can_rx_fd_, can_filters.size(), can_filters.data()); + if (result < 0) + { + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-result}}; + } + return cetl::nullopt; + } + + PushResult::Type push(const libcyphal::TimePoint /* deadline */, + const libcyphal::transport::can::CanId can_id, + libcyphal::transport::MediaPayload& payload) noexcept override + { + const CanardFrame canard_frame{can_id, + {payload.getSpan().size(), static_cast(payload.getSpan().data())}}; + const std::int16_t result = ::socketcanPush(socket_can_tx_fd_, &canard_frame, 0); + if (result < 0) + { + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-result}}; + } + + const bool is_accepted = result > 0; + if (is_accepted) + { + // Payload is not needed anymore, so return memory asap. + payload.reset(); + } + + return PushResult::Success{is_accepted}; + } + + CETL_NODISCARD PopResult::Type pop(const cetl::span payload_buffer) noexcept override + { + CanardFrame canard_frame{}; + bool is_loopback{false}; + + const std::int16_t result = ::socketcanPop(socket_can_rx_fd_, + &canard_frame, + nullptr, + payload_buffer.size(), + payload_buffer.data(), + 0, + &is_loopback); + if (result < 0) + { + return libcyphal::transport::PlatformError{ocvsmd::platform::PosixPlatformError{-result}}; + } + if (result == 0) + { + return cetl::nullopt; + } + + return PopResult::Metadata{executor_.now(), canard_frame.extended_can_id, canard_frame.payload.size}; + } + + CETL_NODISCARD libcyphal::IExecutor::Callback::Any registerPushCallback( + libcyphal::IExecutor::Callback::Function&& function) override + { + using WritableTrigger = ocvsmd::platform::IPosixExecutorExtension::Trigger::Writable; + return registerAwaitableCallback(std::move(function), WritableTrigger{socket_can_tx_fd_}); + } + + CETL_NODISCARD libcyphal::IExecutor::Callback::Any registerPopCallback( + libcyphal::IExecutor::Callback::Function&& function) override + { + using ReadableTrigger = ocvsmd::platform::IPosixExecutorExtension::Trigger::Readable; + return registerAwaitableCallback(std::move(function), ReadableTrigger{socket_can_rx_fd_}); + } + + cetl::pmr::memory_resource& getTxMemoryResource() override + { + return tx_mr_; + } + + // MARK: Data members: + + cetl::pmr::memory_resource& general_mr_; + libcyphal::IExecutor& executor_; + SocketCANFD socket_can_rx_fd_; + SocketCANFD socket_can_tx_fd_; + std::string iface_address_; + cetl::pmr::memory_resource& tx_mr_; + +}; // CanMedia + +// MARK: - + +struct CanMediaCollection +{ + CanMediaCollection(cetl::pmr::memory_resource& general_mr, + libcyphal::IExecutor& executor, + cetl::pmr::memory_resource& tx_mr) + : general_mr_{general_mr} + , executor_{executor} + , media_array_{{cetl::nullopt, cetl::nullopt, cetl::nullopt}} + , tx_mr_{tx_mr} + { + } + + void parse(const cetl::string_view iface_addresses) + { + // Reset the collection. + for (std::size_t i = 0; i < MaxCanMedia; i++) + { + media_array_[i].reset(); // NOLINT + media_ifaces_[i] = nullptr; // NOLINT + } + + // Split addresses by commas. + // + std::size_t index = 0; + std::size_t curr = 0; + while ((curr != cetl::string_view::npos) && (index < MaxCanMedia)) + { + const auto next = iface_addresses.find(',', curr); + const auto iface_address = iface_addresses.substr(curr, next - curr); + if (!iface_address.empty()) + { + auto maybe_media = CanMedia::make(general_mr_, executor_, iface_address, tx_mr_); + if (auto* const media_ptr = cetl::get_if(&maybe_media)) + { + media_array_[index].emplace(std::move(*media_ptr)); // NOLINT + media_ifaces_[index] = &(media_array_[index].value()); // NOLINT + index++; + } + } + + curr = std::max(next + 1, next); // `+1` to skip the comma + } + } + + cetl::span span() + { + return {media_ifaces_.data(), media_ifaces_.size()}; + } + + std::size_t count() const + { + return std::count_if(media_ifaces_.cbegin(), media_ifaces_.cend(), [](const auto* iface) { + // + return iface != nullptr; + }); + } + +private: + static constexpr std::size_t MaxCanMedia = 3; + + cetl::pmr::memory_resource& general_mr_; + libcyphal::IExecutor& executor_; + std::array, MaxCanMedia> media_array_; + std::array media_ifaces_{}; + cetl::pmr::memory_resource& tx_mr_; + +}; // CanMediaCollection + +} // namespace can +} // namespace platform +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_PLATFORM_CAN_MEDIA_HPP_INCLUDED diff --git a/src/daemon/engine/platform/can/socketcan.c b/src/daemon/engine/platform/can/socketcan.c new file mode 100644 index 0000000..9c8eb62 --- /dev/null +++ b/src/daemon/engine/platform/can/socketcan.c @@ -0,0 +1,299 @@ +/// This software is distributed under the terms of the MIT License. +/// Copyright (C) OpenCyphal Development Team +/// Copyright Amazon.com Inc. or its affiliates. +/// SPDX-License-Identifier: MIT +/// Authors: Pavel Kirienko , Tom De Rybel + +// This is needed to enable the necessary declarations in sys/ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + +#include "socketcan.h" + +#ifdef __linux__ +# include +# include +# include +# include +# include +#else +# error "Unsupported OS -- feel free to add support for your OS here. " \ + "Zephyr and NuttX are known to support the SocketCAN API." +#endif + +#include +#include +#include +#include +#include +#include + +#define KILO 1000L +#define MEGA (KILO * KILO) + +static int16_t getNegatedErrno(void) +{ + const int out = -abs(errno); + if (out < 0) + { + if (out >= INT16_MIN) + { + return (int16_t) out; + } + } + else + { + assert(false); // Requested an error when errno is zero? + } + return INT16_MIN; +} + +static int16_t doPoll(const SocketCANFD fd, const int16_t mask, const CanardMicrosecond timeout_usec) +{ + struct pollfd fds; + memset(&fds, 0, sizeof(fds)); + fds.fd = fd; + fds.events = mask; + + struct timespec ts; + ts.tv_sec = (long) (timeout_usec / (CanardMicrosecond) MEGA); + ts.tv_nsec = (long) (timeout_usec % (CanardMicrosecond) MEGA) * KILO; + + const int poll_result = ppoll(&fds, 1, &ts, NULL); + if (poll_result < 0) + { + return getNegatedErrno(); + } + if (poll_result == 0) + { + return 0; + } + if (((uint32_t) fds.revents & (uint32_t) mask) == 0) + { + return -EIO; + } + + return 1; +} + +SocketCANFD socketcanOpen(const char* const iface_name, const bool can_fd) +{ + const size_t iface_name_size = strlen(iface_name) + 1; + if (iface_name_size > IFNAMSIZ) + { + return -ENAMETOOLONG; + } + + const int fd = socket(PF_CAN, SOCK_RAW | SOCK_NONBLOCK, CAN_RAW); // NOLINT + bool ok = fd >= 0; + + if (ok) + { + struct ifreq ifr; + (void) memset(&ifr, 0, sizeof(ifr)); + (void) memcpy(ifr.ifr_name, iface_name, iface_name_size); + ok = 0 == ioctl(fd, SIOCGIFINDEX, &ifr); + if (ok) + { + struct sockaddr_can addr; + (void) memset(&addr, 0, sizeof(addr)); + addr.can_family = AF_CAN; + addr.can_ifindex = ifr.ifr_ifindex; + ok = 0 == bind(fd, (struct sockaddr*) &addr, sizeof(addr)); + } + } + + // Enable CAN FD if required. + if (ok && can_fd) + { + const int en = 1; + ok = 0 == setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &en, sizeof(en)); + } + + // Enable timestamping. + if (ok) + { + const int en = 1; + ok = 0 == setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &en, sizeof(en)); + } + + // Enable outgoing-frame loop-back. + if (ok) + { + const int en = 1; + ok = 0 == setsockopt(fd, SOL_CAN_RAW, CAN_RAW_RECV_OWN_MSGS, &en, sizeof(en)); + } + + if (ok) + { + return fd; + } + + (void) close(fd); + return getNegatedErrno(); +} + +int16_t socketcanPush(const SocketCANFD fd, const struct CanardFrame* const frame, const CanardMicrosecond timeout_usec) +{ + if ((frame == NULL) || (frame->payload.data == NULL) || (frame->payload.size > UINT8_MAX)) + { + return -EINVAL; + } + + const int16_t poll_result = doPoll(fd, POLLOUT, timeout_usec); + if (poll_result > 0) + { + // We use the CAN FD struct regardless of whether the CAN FD socket option is set. + // Per the user manual, this is acceptable because they are binary compatible. + struct canfd_frame cfd; + (void) memset(&cfd, 0, sizeof(cfd)); + cfd.can_id = frame->extended_can_id | CAN_EFF_FLAG; + cfd.len = (uint8_t) frame->payload.size; + // We set the bit rate switch on the assumption that it will be ignored by non-CAN-FD-capable hardware. + cfd.flags = CANFD_BRS; + (void) memcpy(cfd.data, frame->payload.data, frame->payload.size); + + // If the payload is small, use the smaller MTU for compatibility with non-FD sockets. + // This way, if the user attempts to transmit a CAN FD frame without having the CAN FD socket option enabled, + // an error will be triggered here. This is convenient -- we can handle both FD and Classic CAN uniformly. + const size_t mtu = (frame->payload.size > CAN_MAX_DLEN) ? CANFD_MTU : CAN_MTU; + if (write(fd, &cfd, mtu) < 0) + { + return getNegatedErrno(); + } + } + return poll_result; +} + +int16_t socketcanPop(const SocketCANFD fd, + struct CanardFrame* const out_frame, + CanardMicrosecond* const out_timestamp_usec, + const size_t payload_buffer_size, + void* const payload_buffer, + const CanardMicrosecond timeout_usec, + bool* const loopback) +{ + if ((out_frame == NULL) || (payload_buffer == NULL)) + { + return -EINVAL; + } + + const int16_t poll_result = doPoll(fd, POLLIN, timeout_usec); + if (poll_result > 0) + { + // Initialize the message header scatter/gather array. It is to hold a single CAN FD frame struct. + // We use the CAN FD struct regardless of whether the CAN FD socket option is set. + // Per the user manual, this is acceptable because they are binary compatible. + struct canfd_frame sockcan_frame = {0}; // CAN FD frame storage. + struct iovec iov = { + // Scatter/gather array items struct. + .iov_base = &sockcan_frame, // Starting address. + .iov_len = sizeof(sockcan_frame) // Number of bytes to transfer. + + }; + + // Determine the size of the ancillary data and zero-initialize the buffer for it. + // We require space for both the receive message header (implied in CMSG_SPACE) and the time stamp. + // The ancillary data buffer is wrapped in a union to ensure it is suitably aligned. + // See the cmsg(3) man page (release 5.08 dated 2020-06-09, or later) for details. + union + { + uint8_t buf[CMSG_SPACE(sizeof(struct timeval))]; + struct cmsghdr align; + } control; + (void) memset(control.buf, 0, sizeof(control.buf)); + + // Initialize the message header used by recvmsg. + struct msghdr msg = {0}; // Message header struct. + msg.msg_iov = &iov; // Scatter/gather array. + msg.msg_iovlen = 1; // Number of elements in the scatter/gather array. + msg.msg_control = control.buf; // Ancillary data. + msg.msg_controllen = sizeof(control.buf); // Ancillary data buffer length. + + // Non-blocking receive messages from the socket and validate. + const ssize_t read_size = recvmsg(fd, &msg, MSG_DONTWAIT); + if (read_size < 0) + { + return getNegatedErrno(); // Error occurred -- return the negated error code. + } + if ((read_size != CAN_MTU) && (read_size != CANFD_MTU)) + { + return -EIO; + } + if (sockcan_frame.len > payload_buffer_size) + { + return -EFBIG; + } + + const bool valid = ((sockcan_frame.can_id & CAN_EFF_FLAG) != 0) && // Extended frame + ((sockcan_frame.can_id & CAN_ERR_FLAG) == 0) && // Not RTR frame + ((sockcan_frame.can_id & CAN_RTR_FLAG) == 0); // Not error frame + if (!valid) + { + return 0; // Not an extended data frame -- drop silently and return early. + } + + // Handle the loopback frame logic. + const bool loopback_frame = ((uint32_t) msg.msg_flags & (uint32_t) MSG_CONFIRM) != 0; + if (loopback == NULL && loopback_frame) + { + return 0; // The loopback pointer is NULL and this is a loopback frame -- drop silently and return early. + } + if (loopback != NULL) + { + *loopback = loopback_frame; + } + + // Obtain the CAN frame time stamp from the kernel. + // This time stamp is from the CLOCK_REALTIME kernel source. + if (NULL != out_timestamp_usec) + { + const struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); + struct timeval tv = {0}; + assert(cmsg != NULL); + if ((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SO_TIMESTAMP)) + { + (void) memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv)); // Copy to avoid alignment problems + assert(tv.tv_sec >= 0 && tv.tv_usec >= 0); + } + else + { + assert(0); + return -EIO; + } + + (void) memset(out_frame, 0, sizeof(struct CanardFrame)); + *out_timestamp_usec = (CanardMicrosecond) (((uint64_t) tv.tv_sec * MEGA) + (uint64_t) tv.tv_usec); + } + out_frame->extended_can_id = sockcan_frame.can_id & CAN_EFF_MASK; + out_frame->payload.size = sockcan_frame.len; + out_frame->payload.data = payload_buffer; + (void) memcpy(payload_buffer, &sockcan_frame.data[0], sockcan_frame.len); + } + return poll_result; +} + +int16_t socketcanFilter(const SocketCANFD fd, const size_t num_configs, const struct CanardFilter* const configs) +{ + if (configs == NULL) + { + return -EINVAL; + } + if (num_configs > CAN_RAW_FILTER_MAX) + { + return -EFBIG; + } + + struct can_filter cfs[CAN_RAW_FILTER_MAX]; + for (size_t i = 0; i < num_configs; i++) + { + cfs[i].can_id = (configs[i].extended_can_id & CAN_EFF_MASK) | CAN_EFF_FLAG; + cfs[i].can_mask = (configs[i].extended_mask & CAN_EFF_MASK) | CAN_EFF_FLAG | CAN_RTR_FLAG; + } + + const int ret = + setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, cfs, (socklen_t) (sizeof(struct can_filter) * num_configs)); + + return (ret < 0) ? getNegatedErrno() : 0; +} diff --git a/src/daemon/engine/platform/can/socketcan.h b/src/daemon/engine/platform/can/socketcan.h new file mode 100644 index 0000000..233656e --- /dev/null +++ b/src/daemon/engine/platform/can/socketcan.h @@ -0,0 +1,88 @@ +/// ____ ______ __ __ +/// / __ `____ ___ ____ / ____/_ ______ / /_ ____ / / +/// / / / / __ `/ _ `/ __ `/ / / / / / __ `/ __ `/ __ `/ / +/// / /_/ / /_/ / __/ / / / /___/ /_/ / /_/ / / / / /_/ / / +/// `____/ .___/`___/_/ /_/`____/`__, / .___/_/ /_/`__,_/_/ +/// /_/ /____/_/ +/// +/// This is a basic adapter library that bridges Libcanard with SocketCAN. +/// Read the API documentation for usage information. +/// +/// To integrate the library into your application, just copy-paste the c/h files into your project tree. +/// +/// -------------------------------------------------------------------------------------------------------------------- +/// Changelog +/// +/// v3.0 - Update for compatibility with Libcanard v3. +/// +/// v2.0 - Added loop-back functionality. +/// API change in socketcanPop(): loopback flag added. +/// - Changed to kernel-based time-stamping for received frames for improved accuracy. +/// API change in socketcanPop(): time stamp clock source is now CLOCK_REALTIME, vs CLOCK_TAI before. +/// +/// v1.0 - Initial release +/// -------------------------------------------------------------------------------------------------------------------- +/// +/// This software is distributed under the terms of the MIT License. +/// Copyright (c) 2020 OpenCyphal +/// Author: Pavel Kirienko + +#ifndef SOCKETCAN_H_INCLUDED +#define SOCKETCAN_H_INCLUDED + +#include "canard.h" +#include +#include +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /// File descriptor alias. + typedef int SocketCANFD; + + /// Initialize a new non-blocking (sic!) SocketCAN socket and return its handle on success. + /// On failure, a negated errno is returned. + /// To discard the socket just call close() on it; no additional de-initialization activities are required. + /// The argument can_fd enables support for CAN FD frames. + SocketCANFD socketcanOpen(const char* const iface_name, const bool can_fd); + + /// Enqueue a new extended CAN data frame for transmission. + /// Block until the frame is enqueued or until the timeout is expired. + /// Zero timeout makes the operation non-blocking. + /// Returns 1 on success, 0 on timeout, negated errno on error. + int16_t socketcanPush(const SocketCANFD fd, + const struct CanardFrame* const frame, + const CanardMicrosecond timeout_usec); + + /// Fetch a new extended CAN data frame from the RX queue. + /// If the received frame is not an extended-ID data frame, it will be dropped and the function will return early. + /// The payload pointer of the returned frame will point to the payload_buffer. It can be a stack-allocated array. + /// The payload_buffer_size shall be large enough (64 bytes is enough for CAN FD), otherwise an error is returned. + /// The received frame timestamp will be set to CLOCK_REALTIME by the kernel, sampled near the moment of its + /// arrival. The loopback flag pointer is used to both indicate and control behavior when a looped-back message is + /// received. If the flag pointer is NULL, loopback frames are silently dropped; if not NULL, they are accepted and + /// indicated using this flag. The function will block until a frame is received or until the timeout is expired. It + /// may return early. Zero timeout makes the operation non-blocking. Returns 1 on success, 0 on timeout, negated + /// errno on error. + int16_t socketcanPop(const SocketCANFD fd, + struct CanardFrame* const out_frame, + CanardMicrosecond* const out_timestamp_usec, + const size_t payload_buffer_size, + void* const payload_buffer, + const CanardMicrosecond timeout_usec, + bool* const loopback); + + /// Apply the specified acceptance filter configuration. + /// Note that it is only possible to accept extended-format data frames. + /// The default configuration is to accept everything. + /// Returns 0 on success, negated errno on error. + int16_t socketcanFilter(const SocketCANFD fd, const size_t num_configs, const struct CanardFilter* const configs); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/daemon/engine/platform/udp/udp_media.hpp b/src/daemon/engine/platform/udp/udp_media.hpp index 5a011e5..06d0b58 100644 --- a/src/daemon/engine/platform/udp/udp_media.hpp +++ b/src/daemon/engine/platform/udp/udp_media.hpp @@ -9,6 +9,7 @@ #include "udp_sockets.hpp" #include +#include #include #include #include @@ -105,7 +106,7 @@ struct UdpMediaCollection void parse(const cetl::string_view iface_addresses) { - // Split addresses by spaces. + // Split addresses by commas. // std::size_t index = 0; std::size_t curr = 0; @@ -119,7 +120,7 @@ struct UdpMediaCollection index++; } - curr = std::max(next + 1, next); // `+1` to skip the space + curr = std::max(next + 1, next); // `+1` to skip the comma } media_ifaces_ = {}; diff --git a/submodules/libcyphal b/submodules/libcyphal index 3d0320d..a3be9d3 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit 3d0320dee3c64f59d8773bdc39e5bc3318bb40d8 +Subproject commit a3be9d3eef99c64d31adf23955ca2046c9a79f3b From 23f7ae73fb8d993cec285cba58e8f4d1f0e814b5 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 7 Feb 2025 14:18:29 +0200 Subject: [PATCH 121/156] fix build --- submodules/libcanard | 1 + 1 file changed, 1 insertion(+) create mode 160000 submodules/libcanard diff --git a/submodules/libcanard b/submodules/libcanard new file mode 160000 index 0000000..76ec3b4 --- /dev/null +++ b/submodules/libcanard @@ -0,0 +1 @@ +Subproject commit 76ec3b4493d440f22e6aaa77e7390de6fbc24a68 From 9b58d2e2bdf79e2d4dfa79fcb28efad0d1e50c70 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 7 Feb 2025 14:35:14 +0200 Subject: [PATCH 122/156] fix build --- src/daemon/engine/cyphal/can_transport_bag.hpp | 2 +- src/daemon/engine/cyphal/udp_transport_bag.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/daemon/engine/cyphal/can_transport_bag.hpp b/src/daemon/engine/cyphal/can_transport_bag.hpp index e8c50c7..fc22b37 100644 --- a/src/daemon/engine/cyphal/can_transport_bag.hpp +++ b/src/daemon/engine/cyphal/can_transport_bag.hpp @@ -53,7 +53,7 @@ class CanTransportBag final : public AnyTransportBag { CETL_DEBUG_ASSERT(config, ""); - static constexpr std::string can_prefix{"socketcan:"}; + static const std::string can_prefix{"socketcan:"}; std::string can_ifaces; for (const auto& iface : config->getCyphalTransportInterfaces()) diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index 37f7a43..540e884 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -55,7 +55,7 @@ class UdpTransportBag final : public AnyTransportBag { CETL_DEBUG_ASSERT(config, ""); - static constexpr std::string udp_prefix{"udp://"}; + static const std::string udp_prefix{"udp://"}; std::string udp_ifaces; for (const auto& iface : config->getCyphalTransportInterfaces()) From 79b14f97b1dd9e95cbfbe4b460cf4121598c4369 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 10 Feb 2025 13:47:19 +0200 Subject: [PATCH 123/156] fix typo --- docs/file_server.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/file_server.hpp b/docs/file_server.hpp index f689977..b0ba4bb 100644 --- a/docs/file_server.hpp +++ b/docs/file_server.hpp @@ -23,7 +23,7 @@ class FileServer /// so the list will be automatically restored on the next daemon start. /// Returns `cetl::nullopt` on success. /// - virtual cetl::optinal push_root(const cetl::string_view path, const bool back); + virtual cetl::optional push_root(const cetl::string_view path, const bool back); /// Does nothing if such root does not exist (no error reported). /// If such root is listed more than once, only one copy is removed (see `back` param). @@ -33,7 +33,7 @@ class FileServer /// so the list will be automatically restored on the next daemon start. /// Returns `cetl::nullopt` on success (or if path not found). /// - virtual cetl::optinal pop_root(const cetl::string_view path, const bool back); + virtual cetl::optional pop_root(const cetl::string_view path, const bool back); /// The returned paths are the same as they were added by `push_root`. /// The entries are not unique. The order is preserved. From 7d9be3d04af572430986a913d1e2680b6b35ebd8 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 10 Feb 2025 14:03:28 +0200 Subject: [PATCH 124/156] make SPDLOG v1.x dependency as submodule --- .gitmodules | 4 + include/spdlog/LICENSE | 25 - include/spdlog/async.h | 100 - include/spdlog/async_logger-inl.h | 84 - include/spdlog/async_logger.h | 74 - include/spdlog/cfg/argv.h | 40 - include/spdlog/cfg/env.h | 36 - include/spdlog/cfg/helpers-inl.h | 107 - include/spdlog/cfg/helpers.h | 29 - include/spdlog/common-inl.h | 68 - include/spdlog/common.h | 406 -- include/spdlog/details/backtracer-inl.h | 63 - include/spdlog/details/backtracer.h | 45 - include/spdlog/details/circular_q.h | 115 - include/spdlog/details/console_globals.h | 28 - include/spdlog/details/file_helper-inl.h | 153 - include/spdlog/details/file_helper.h | 61 - include/spdlog/details/fmt_helper.h | 141 - include/spdlog/details/log_msg-inl.h | 44 - include/spdlog/details/log_msg.h | 40 - include/spdlog/details/log_msg_buffer-inl.h | 54 - include/spdlog/details/log_msg_buffer.h | 32 - include/spdlog/details/mpmc_blocking_q.h | 177 - include/spdlog/details/null_mutex.h | 35 - include/spdlog/details/os-inl.h | 606 --- include/spdlog/details/os.h | 127 - include/spdlog/details/periodic_worker-inl.h | 26 - include/spdlog/details/periodic_worker.h | 58 - include/spdlog/details/registry-inl.h | 261 - include/spdlog/details/registry.h | 129 - include/spdlog/details/synchronous_factory.h | 22 - include/spdlog/details/tcp_client-windows.h | 135 - include/spdlog/details/tcp_client.h | 127 - include/spdlog/details/thread_pool-inl.h | 127 - include/spdlog/details/thread_pool.h | 117 - include/spdlog/details/udp_client-windows.h | 98 - include/spdlog/details/udp_client.h | 81 - include/spdlog/details/windows_include.h | 11 - include/spdlog/fmt/bin_to_hex.h | 224 - include/spdlog/fmt/bundled/args.h | 228 - include/spdlog/fmt/bundled/base.h | 3077 ------------ include/spdlog/fmt/bundled/chrono.h | 2432 --------- include/spdlog/fmt/bundled/color.h | 612 --- include/spdlog/fmt/bundled/compile.h | 529 -- include/spdlog/fmt/bundled/core.h | 5 - include/spdlog/fmt/bundled/fmt.license.rst | 27 - include/spdlog/fmt/bundled/format-inl.h | 1928 ------- include/spdlog/fmt/bundled/format.h | 4427 ----------------- include/spdlog/fmt/bundled/locale.h | 2 - include/spdlog/fmt/bundled/os.h | 439 -- include/spdlog/fmt/bundled/ostream.h | 211 - include/spdlog/fmt/bundled/printf.h | 656 --- include/spdlog/fmt/bundled/ranges.h | 882 ---- include/spdlog/fmt/bundled/std.h | 699 --- include/spdlog/fmt/bundled/xchar.h | 322 -- include/spdlog/fmt/chrono.h | 23 - include/spdlog/fmt/compile.h | 23 - include/spdlog/fmt/fmt.h | 31 - include/spdlog/fmt/ostr.h | 23 - include/spdlog/fmt/ranges.h | 23 - include/spdlog/fmt/std.h | 24 - include/spdlog/fmt/xchar.h | 23 - include/spdlog/formatter.h | 17 - include/spdlog/fwd.h | 18 - include/spdlog/logger-inl.h | 198 - include/spdlog/logger.h | 379 -- include/spdlog/mdc.h | 50 - include/spdlog/pattern_formatter-inl.h | 1338 ----- include/spdlog/pattern_formatter.h | 118 - include/spdlog/sinks/android_sink.h | 137 - include/spdlog/sinks/ansicolor_sink-inl.h | 135 - include/spdlog/sinks/ansicolor_sink.h | 115 - include/spdlog/sinks/base_sink-inl.h | 59 - include/spdlog/sinks/base_sink.h | 51 - include/spdlog/sinks/basic_file_sink-inl.h | 48 - include/spdlog/sinks/basic_file_sink.h | 66 - include/spdlog/sinks/callback_sink.h | 56 - include/spdlog/sinks/daily_file_sink.h | 254 - include/spdlog/sinks/dist_sink.h | 81 - include/spdlog/sinks/dup_filter_sink.h | 92 - include/spdlog/sinks/hourly_file_sink.h | 193 - include/spdlog/sinks/kafka_sink.h | 119 - include/spdlog/sinks/mongo_sink.h | 108 - include/spdlog/sinks/msvc_sink.h | 68 - include/spdlog/sinks/null_sink.h | 41 - include/spdlog/sinks/ostream_sink.h | 43 - include/spdlog/sinks/qt_sinks.h | 304 -- include/spdlog/sinks/ringbuffer_sink.h | 67 - include/spdlog/sinks/rotating_file_sink-inl.h | 150 - include/spdlog/sinks/rotating_file_sink.h | 90 - include/spdlog/sinks/sink-inl.h | 22 - include/spdlog/sinks/sink.h | 34 - include/spdlog/sinks/stdout_color_sinks-inl.h | 38 - include/spdlog/sinks/stdout_color_sinks.h | 49 - include/spdlog/sinks/stdout_sinks-inl.h | 127 - include/spdlog/sinks/stdout_sinks.h | 84 - include/spdlog/sinks/syslog_sink.h | 104 - include/spdlog/sinks/systemd_sink.h | 121 - include/spdlog/sinks/tcp_sink.h | 75 - include/spdlog/sinks/udp_sink.h | 69 - include/spdlog/sinks/win_eventlog_sink.h | 260 - include/spdlog/sinks/wincolor_sink-inl.h | 172 - include/spdlog/sinks/wincolor_sink.h | 82 - include/spdlog/spdlog-inl.h | 92 - include/spdlog/spdlog.h | 352 -- include/spdlog/stopwatch.h | 66 - include/spdlog/tweakme.h | 141 - include/spdlog/version.h | 11 - src/cli/CMakeLists.txt | 3 + src/common/CMakeLists.txt | 1 + submodules/spdlog | 1 + 111 files changed, 9 insertions(+), 27046 deletions(-) delete mode 100644 include/spdlog/LICENSE delete mode 100644 include/spdlog/async.h delete mode 100644 include/spdlog/async_logger-inl.h delete mode 100644 include/spdlog/async_logger.h delete mode 100644 include/spdlog/cfg/argv.h delete mode 100644 include/spdlog/cfg/env.h delete mode 100644 include/spdlog/cfg/helpers-inl.h delete mode 100644 include/spdlog/cfg/helpers.h delete mode 100644 include/spdlog/common-inl.h delete mode 100644 include/spdlog/common.h delete mode 100644 include/spdlog/details/backtracer-inl.h delete mode 100644 include/spdlog/details/backtracer.h delete mode 100644 include/spdlog/details/circular_q.h delete mode 100644 include/spdlog/details/console_globals.h delete mode 100644 include/spdlog/details/file_helper-inl.h delete mode 100644 include/spdlog/details/file_helper.h delete mode 100644 include/spdlog/details/fmt_helper.h delete mode 100644 include/spdlog/details/log_msg-inl.h delete mode 100644 include/spdlog/details/log_msg.h delete mode 100644 include/spdlog/details/log_msg_buffer-inl.h delete mode 100644 include/spdlog/details/log_msg_buffer.h delete mode 100644 include/spdlog/details/mpmc_blocking_q.h delete mode 100644 include/spdlog/details/null_mutex.h delete mode 100644 include/spdlog/details/os-inl.h delete mode 100644 include/spdlog/details/os.h delete mode 100644 include/spdlog/details/periodic_worker-inl.h delete mode 100644 include/spdlog/details/periodic_worker.h delete mode 100644 include/spdlog/details/registry-inl.h delete mode 100644 include/spdlog/details/registry.h delete mode 100644 include/spdlog/details/synchronous_factory.h delete mode 100644 include/spdlog/details/tcp_client-windows.h delete mode 100644 include/spdlog/details/tcp_client.h delete mode 100644 include/spdlog/details/thread_pool-inl.h delete mode 100644 include/spdlog/details/thread_pool.h delete mode 100644 include/spdlog/details/udp_client-windows.h delete mode 100644 include/spdlog/details/udp_client.h delete mode 100644 include/spdlog/details/windows_include.h delete mode 100644 include/spdlog/fmt/bin_to_hex.h delete mode 100644 include/spdlog/fmt/bundled/args.h delete mode 100644 include/spdlog/fmt/bundled/base.h delete mode 100644 include/spdlog/fmt/bundled/chrono.h delete mode 100644 include/spdlog/fmt/bundled/color.h delete mode 100644 include/spdlog/fmt/bundled/compile.h delete mode 100644 include/spdlog/fmt/bundled/core.h delete mode 100644 include/spdlog/fmt/bundled/fmt.license.rst delete mode 100644 include/spdlog/fmt/bundled/format-inl.h delete mode 100644 include/spdlog/fmt/bundled/format.h delete mode 100644 include/spdlog/fmt/bundled/locale.h delete mode 100644 include/spdlog/fmt/bundled/os.h delete mode 100644 include/spdlog/fmt/bundled/ostream.h delete mode 100644 include/spdlog/fmt/bundled/printf.h delete mode 100644 include/spdlog/fmt/bundled/ranges.h delete mode 100644 include/spdlog/fmt/bundled/std.h delete mode 100644 include/spdlog/fmt/bundled/xchar.h delete mode 100644 include/spdlog/fmt/chrono.h delete mode 100644 include/spdlog/fmt/compile.h delete mode 100644 include/spdlog/fmt/fmt.h delete mode 100644 include/spdlog/fmt/ostr.h delete mode 100644 include/spdlog/fmt/ranges.h delete mode 100644 include/spdlog/fmt/std.h delete mode 100644 include/spdlog/fmt/xchar.h delete mode 100644 include/spdlog/formatter.h delete mode 100644 include/spdlog/fwd.h delete mode 100644 include/spdlog/logger-inl.h delete mode 100644 include/spdlog/logger.h delete mode 100644 include/spdlog/mdc.h delete mode 100644 include/spdlog/pattern_formatter-inl.h delete mode 100644 include/spdlog/pattern_formatter.h delete mode 100644 include/spdlog/sinks/android_sink.h delete mode 100644 include/spdlog/sinks/ansicolor_sink-inl.h delete mode 100644 include/spdlog/sinks/ansicolor_sink.h delete mode 100644 include/spdlog/sinks/base_sink-inl.h delete mode 100644 include/spdlog/sinks/base_sink.h delete mode 100644 include/spdlog/sinks/basic_file_sink-inl.h delete mode 100644 include/spdlog/sinks/basic_file_sink.h delete mode 100644 include/spdlog/sinks/callback_sink.h delete mode 100644 include/spdlog/sinks/daily_file_sink.h delete mode 100644 include/spdlog/sinks/dist_sink.h delete mode 100644 include/spdlog/sinks/dup_filter_sink.h delete mode 100644 include/spdlog/sinks/hourly_file_sink.h delete mode 100644 include/spdlog/sinks/kafka_sink.h delete mode 100644 include/spdlog/sinks/mongo_sink.h delete mode 100644 include/spdlog/sinks/msvc_sink.h delete mode 100644 include/spdlog/sinks/null_sink.h delete mode 100644 include/spdlog/sinks/ostream_sink.h delete mode 100644 include/spdlog/sinks/qt_sinks.h delete mode 100644 include/spdlog/sinks/ringbuffer_sink.h delete mode 100644 include/spdlog/sinks/rotating_file_sink-inl.h delete mode 100644 include/spdlog/sinks/rotating_file_sink.h delete mode 100644 include/spdlog/sinks/sink-inl.h delete mode 100644 include/spdlog/sinks/sink.h delete mode 100644 include/spdlog/sinks/stdout_color_sinks-inl.h delete mode 100644 include/spdlog/sinks/stdout_color_sinks.h delete mode 100644 include/spdlog/sinks/stdout_sinks-inl.h delete mode 100644 include/spdlog/sinks/stdout_sinks.h delete mode 100644 include/spdlog/sinks/syslog_sink.h delete mode 100644 include/spdlog/sinks/systemd_sink.h delete mode 100644 include/spdlog/sinks/tcp_sink.h delete mode 100644 include/spdlog/sinks/udp_sink.h delete mode 100644 include/spdlog/sinks/win_eventlog_sink.h delete mode 100644 include/spdlog/sinks/wincolor_sink-inl.h delete mode 100644 include/spdlog/sinks/wincolor_sink.h delete mode 100644 include/spdlog/spdlog-inl.h delete mode 100644 include/spdlog/spdlog.h delete mode 100644 include/spdlog/stopwatch.h delete mode 100644 include/spdlog/tweakme.h delete mode 100644 include/spdlog/version.h create mode 160000 submodules/spdlog diff --git a/.gitmodules b/.gitmodules index 2dd7245..90ba1a8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,7 @@ [submodule "submodules/cetl"] path = submodules/cetl url = https://github.com/OpenCyphal/CETL.git +[submodule "submodules/spdlog"] + path = submodules/spdlog + url = https://github.com/gabime/spdlog.git + branch = v1.x diff --git a/include/spdlog/LICENSE b/include/spdlog/LICENSE deleted file mode 100644 index c46826e..0000000 --- a/include/spdlog/LICENSE +++ /dev/null @@ -1,25 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2016 Gabi Melman. - -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. - --- NOTE: Third party dependency used by this software -- -This software depends on the fmt lib (MIT License), -and users must comply to its license: https://raw.githubusercontent.com/fmtlib/fmt/master/LICENSE diff --git a/include/spdlog/async.h b/include/spdlog/async.h deleted file mode 100644 index e96abd1..0000000 --- a/include/spdlog/async.h +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// -// Async logging using global thread pool -// All loggers created here share same global thread pool. -// Each log message is pushed to a queue along with a shared pointer to the -// logger. -// If a logger deleted while having pending messages in the queue, it's actual -// destruction will defer -// until all its messages are processed by the thread pool. -// This is because each message in the queue holds a shared_ptr to the -// originating logger. - -#include -#include -#include - -#include -#include -#include - -namespace spdlog { - -namespace details { -static const size_t default_async_q_size = 8192; -} - -// async logger factory - creates async loggers backed with thread pool. -// if a global thread pool doesn't already exist, create it with default queue -// size of 8192 items and single thread. -template -struct async_factory_impl { - template - static std::shared_ptr create(std::string logger_name, SinkArgs &&...args) { - auto ®istry_inst = details::registry::instance(); - - // create global thread pool if not already exists.. - - auto &mutex = registry_inst.tp_mutex(); - std::lock_guard tp_lock(mutex); - auto tp = registry_inst.get_tp(); - if (tp == nullptr) { - tp = std::make_shared(details::default_async_q_size, 1U); - registry_inst.set_tp(tp); - } - - auto sink = std::make_shared(std::forward(args)...); - auto new_logger = std::make_shared(std::move(logger_name), std::move(sink), - std::move(tp), OverflowPolicy); - registry_inst.initialize_logger(new_logger); - return new_logger; - } -}; - -using async_factory = async_factory_impl; -using async_factory_nonblock = async_factory_impl; - -template -inline std::shared_ptr create_async(std::string logger_name, - SinkArgs &&...sink_args) { - return async_factory::create(std::move(logger_name), - std::forward(sink_args)...); -} - -template -inline std::shared_ptr create_async_nb(std::string logger_name, - SinkArgs &&...sink_args) { - return async_factory_nonblock::create(std::move(logger_name), - std::forward(sink_args)...); -} - -// set global thread pool. -inline void init_thread_pool(size_t q_size, - size_t thread_count, - std::function on_thread_start, - std::function on_thread_stop) { - auto tp = std::make_shared(q_size, thread_count, on_thread_start, - on_thread_stop); - details::registry::instance().set_tp(std::move(tp)); -} - -inline void init_thread_pool(size_t q_size, - size_t thread_count, - std::function on_thread_start) { - init_thread_pool(q_size, thread_count, on_thread_start, [] {}); -} - -inline void init_thread_pool(size_t q_size, size_t thread_count) { - init_thread_pool( - q_size, thread_count, [] {}, [] {}); -} - -// get the global thread pool. -inline std::shared_ptr thread_pool() { - return details::registry::instance().get_tp(); -} -} // namespace spdlog diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h deleted file mode 100644 index 1e79479..0000000 --- a/include/spdlog/async_logger-inl.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -#include -#include - -SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name, - sinks_init_list sinks_list, - std::weak_ptr tp, - async_overflow_policy overflow_policy) - : async_logger(std::move(logger_name), - sinks_list.begin(), - sinks_list.end(), - std::move(tp), - overflow_policy) {} - -SPDLOG_INLINE spdlog::async_logger::async_logger(std::string logger_name, - sink_ptr single_sink, - std::weak_ptr tp, - async_overflow_policy overflow_policy) - : async_logger( - std::move(logger_name), {std::move(single_sink)}, std::move(tp), overflow_policy) {} - -// send the log message to the thread pool -SPDLOG_INLINE void spdlog::async_logger::sink_it_(const details::log_msg &msg){ - SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){ - pool_ptr->post_log(shared_from_this(), msg, overflow_policy_); -} -else { - throw_spdlog_ex("async log: thread pool doesn't exist anymore"); -} -} -SPDLOG_LOGGER_CATCH(msg.source) -} - -// send flush request to the thread pool -SPDLOG_INLINE void spdlog::async_logger::flush_(){ - SPDLOG_TRY{if (auto pool_ptr = thread_pool_.lock()){ - pool_ptr->post_flush(shared_from_this(), overflow_policy_); -} -else { - throw_spdlog_ex("async flush: thread pool doesn't exist anymore"); -} -} -SPDLOG_LOGGER_CATCH(source_loc()) -} - -// -// backend functions - called from the thread pool to do the actual job -// -SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg &msg) { - for (auto &sink : sinks_) { - if (sink->should_log(msg.level)) { - SPDLOG_TRY { sink->log(msg); } - SPDLOG_LOGGER_CATCH(msg.source) - } - } - - if (should_flush_(msg)) { - backend_flush_(); - } -} - -SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { - for (auto &sink : sinks_) { - SPDLOG_TRY { sink->flush(); } - SPDLOG_LOGGER_CATCH(source_loc()) - } -} - -SPDLOG_INLINE std::shared_ptr spdlog::async_logger::clone(std::string new_name) { - auto cloned = std::make_shared(*this); - cloned->name_ = std::move(new_name); - return cloned; -} diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h deleted file mode 100644 index 846c4c6..0000000 --- a/include/spdlog/async_logger.h +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// Fast asynchronous logger. -// Uses pre allocated queue. -// Creates a single back thread to pop messages from the queue and log them. -// -// Upon each log write the logger: -// 1. Checks if its log level is enough to log the message -// 2. Push a new copy of the message to a queue (or block the caller until -// space is available in the queue) -// Upon destruction, logs all remaining messages in the queue before -// destructing.. - -#include - -namespace spdlog { - -// Async overflow policy - block by default. -enum class async_overflow_policy { - block, // Block until message can be enqueued - overrun_oldest, // Discard oldest message in the queue if full when trying to - // add new item. - discard_new // Discard new message if the queue is full when trying to add new item. -}; - -namespace details { -class thread_pool; -} - -class SPDLOG_API async_logger final : public std::enable_shared_from_this, - public logger { - friend class details::thread_pool; - -public: - template - async_logger(std::string logger_name, - It begin, - It end, - std::weak_ptr tp, - async_overflow_policy overflow_policy = async_overflow_policy::block) - : logger(std::move(logger_name), begin, end), - thread_pool_(std::move(tp)), - overflow_policy_(overflow_policy) {} - - async_logger(std::string logger_name, - sinks_init_list sinks_list, - std::weak_ptr tp, - async_overflow_policy overflow_policy = async_overflow_policy::block); - - async_logger(std::string logger_name, - sink_ptr single_sink, - std::weak_ptr tp, - async_overflow_policy overflow_policy = async_overflow_policy::block); - - std::shared_ptr clone(std::string new_name) override; - -protected: - void sink_it_(const details::log_msg &msg) override; - void flush_() override; - void backend_sink_it_(const details::log_msg &incoming_log_msg); - void backend_flush_(); - -private: - std::weak_ptr thread_pool_; - async_overflow_policy overflow_policy_; -}; -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "async_logger-inl.h" -#endif diff --git a/include/spdlog/cfg/argv.h b/include/spdlog/cfg/argv.h deleted file mode 100644 index 7de2f83..0000000 --- a/include/spdlog/cfg/argv.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once -#include -#include - -// -// Init log levels using each argv entry that starts with "SPDLOG_LEVEL=" -// -// set all loggers to debug level: -// example.exe "SPDLOG_LEVEL=debug" - -// set logger1 to trace level -// example.exe "SPDLOG_LEVEL=logger1=trace" - -// turn off all logging except for logger1 and logger2: -// example.exe "SPDLOG_LEVEL=off,logger1=debug,logger2=info" - -namespace spdlog { -namespace cfg { - -// search for SPDLOG_LEVEL= in the args and use it to init the levels -inline void load_argv_levels(int argc, const char **argv) { - const std::string spdlog_level_prefix = "SPDLOG_LEVEL="; - for (int i = 1; i < argc; i++) { - std::string arg = argv[i]; - if (arg.find(spdlog_level_prefix) == 0) { - auto levels_string = arg.substr(spdlog_level_prefix.size()); - helpers::load_levels(levels_string); - } - } -} - -inline void load_argv_levels(int argc, char **argv) { - load_argv_levels(argc, const_cast(argv)); -} - -} // namespace cfg -} // namespace spdlog diff --git a/include/spdlog/cfg/env.h b/include/spdlog/cfg/env.h deleted file mode 100644 index 6e55414..0000000 --- a/include/spdlog/cfg/env.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once -#include -#include -#include - -// -// Init levels and patterns from env variables SPDLOG_LEVEL -// Inspired from Rust's "env_logger" crate (https://crates.io/crates/env_logger). -// Note - fallback to "info" level on unrecognized levels -// -// Examples: -// -// set global level to debug: -// export SPDLOG_LEVEL=debug -// -// turn off all logging except for logger1: -// export SPDLOG_LEVEL="*=off,logger1=debug" -// - -// turn off all logging except for logger1 and logger2: -// export SPDLOG_LEVEL="off,logger1=debug,logger2=info" - -namespace spdlog { -namespace cfg { -inline void load_env_levels() { - auto env_val = details::os::getenv("SPDLOG_LEVEL"); - if (!env_val.empty()) { - helpers::load_levels(env_val); - } -} - -} // namespace cfg -} // namespace spdlog diff --git a/include/spdlog/cfg/helpers-inl.h b/include/spdlog/cfg/helpers-inl.h deleted file mode 100644 index 93650a2..0000000 --- a/include/spdlog/cfg/helpers-inl.h +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include -#include - -#include -#include -#include -#include - -namespace spdlog { -namespace cfg { -namespace helpers { - -// inplace convert to lowercase -inline std::string &to_lower_(std::string &str) { - std::transform(str.begin(), str.end(), str.begin(), [](char ch) { - return static_cast((ch >= 'A' && ch <= 'Z') ? ch + ('a' - 'A') : ch); - }); - return str; -} - -// inplace trim spaces -inline std::string &trim_(std::string &str) { - const char *spaces = " \n\r\t"; - str.erase(str.find_last_not_of(spaces) + 1); - str.erase(0, str.find_first_not_of(spaces)); - return str; -} - -// return (name,value) trimmed pair from given "name=value" string. -// return empty string on missing parts -// "key=val" => ("key", "val") -// " key = val " => ("key", "val") -// "key=" => ("key", "") -// "val" => ("", "val") - -inline std::pair extract_kv_(char sep, const std::string &str) { - auto n = str.find(sep); - std::string k, v; - if (n == std::string::npos) { - v = str; - } else { - k = str.substr(0, n); - v = str.substr(n + 1); - } - return std::make_pair(trim_(k), trim_(v)); -} - -// return vector of key/value pairs from sequence of "K1=V1,K2=V2,.." -// "a=AAA,b=BBB,c=CCC,.." => {("a","AAA"),("b","BBB"),("c", "CCC"),...} -inline std::unordered_map extract_key_vals_(const std::string &str) { - std::string token; - std::istringstream token_stream(str); - std::unordered_map rv{}; - while (std::getline(token_stream, token, ',')) { - if (token.empty()) { - continue; - } - auto kv = extract_kv_('=', token); - rv[kv.first] = kv.second; - } - return rv; -} - -SPDLOG_INLINE void load_levels(const std::string &input) { - if (input.empty() || input.size() > 512) { - return; - } - - auto key_vals = extract_key_vals_(input); - std::unordered_map levels; - level::level_enum global_level = level::info; - bool global_level_found = false; - - for (auto &name_level : key_vals) { - auto &logger_name = name_level.first; - auto level_name = to_lower_(name_level.second); - auto level = level::from_str(level_name); - // ignore unrecognized level names - if (level == level::off && level_name != "off") { - continue; - } - if (logger_name.empty()) // no logger name indicate global level - { - global_level_found = true; - global_level = level; - } else { - levels[logger_name] = level; - } - } - - details::registry::instance().set_levels(std::move(levels), - global_level_found ? &global_level : nullptr); -} - -} // namespace helpers -} // namespace cfg -} // namespace spdlog diff --git a/include/spdlog/cfg/helpers.h b/include/spdlog/cfg/helpers.h deleted file mode 100644 index c023818..0000000 --- a/include/spdlog/cfg/helpers.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -namespace spdlog { -namespace cfg { -namespace helpers { -// -// Init levels from given string -// -// Examples: -// -// set global level to debug: "debug" -// turn off all logging except for logger1: "off,logger1=debug" -// turn off all logging except for logger1 and logger2: "off,logger1=debug,logger2=info" -// -SPDLOG_API void load_levels(const std::string &txt); -} // namespace helpers - -} // namespace cfg -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "helpers-inl.h" -#endif // SPDLOG_HEADER_ONLY diff --git a/include/spdlog/common-inl.h b/include/spdlog/common-inl.h deleted file mode 100644 index a8a0453..0000000 --- a/include/spdlog/common-inl.h +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -namespace spdlog { -namespace level { - -#if __cplusplus >= 201703L -constexpr -#endif - static string_view_t level_string_views[] SPDLOG_LEVEL_NAMES; - -static const char *short_level_names[] SPDLOG_SHORT_LEVEL_NAMES; - -SPDLOG_INLINE const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT { - return level_string_views[l]; -} - -SPDLOG_INLINE const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT { - return short_level_names[l]; -} - -SPDLOG_INLINE spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT { - auto it = std::find(std::begin(level_string_views), std::end(level_string_views), name); - if (it != std::end(level_string_views)) - return static_cast(std::distance(std::begin(level_string_views), it)); - - // check also for "warn" and "err" before giving up.. - if (name == "warn") { - return level::warn; - } - if (name == "err") { - return level::err; - } - return level::off; -} -} // namespace level - -SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg) - : msg_(std::move(msg)) {} - -SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string &msg, int last_errno) { -#ifdef SPDLOG_USE_STD_FORMAT - msg_ = std::system_error(std::error_code(last_errno, std::generic_category()), msg).what(); -#else - memory_buf_t outbuf; - fmt::format_system_error(outbuf, last_errno, msg.c_str()); - msg_ = fmt::to_string(outbuf); -#endif -} - -SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT { return msg_.c_str(); } - -SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno) { - SPDLOG_THROW(spdlog_ex(msg, last_errno)); -} - -SPDLOG_INLINE void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); } - -} // namespace spdlog diff --git a/include/spdlog/common.h b/include/spdlog/common.h deleted file mode 100644 index 71ffd24..0000000 --- a/include/spdlog/common.h +++ /dev/null @@ -1,406 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef SPDLOG_USE_STD_FORMAT - #include - #if __cpp_lib_format >= 202207L - #include - #else - #include - #endif -#endif - -#ifdef SPDLOG_COMPILED_LIB - #undef SPDLOG_HEADER_ONLY - #if defined(SPDLOG_SHARED_LIB) - #if defined(_WIN32) - #ifdef spdlog_EXPORTS - #define SPDLOG_API __declspec(dllexport) - #else // !spdlog_EXPORTS - #define SPDLOG_API __declspec(dllimport) - #endif - #else // !defined(_WIN32) - #define SPDLOG_API __attribute__((visibility("default"))) - #endif - #else // !defined(SPDLOG_SHARED_LIB) - #define SPDLOG_API - #endif - #define SPDLOG_INLINE -#else // !defined(SPDLOG_COMPILED_LIB) - #define SPDLOG_API - #define SPDLOG_HEADER_ONLY - #define SPDLOG_INLINE inline -#endif // #ifdef SPDLOG_COMPILED_LIB - -#include - -#if !defined(SPDLOG_USE_STD_FORMAT) && \ - FMT_VERSION >= 80000 // backward compatibility with fmt versions older than 8 - #define SPDLOG_FMT_RUNTIME(format_string) fmt::runtime(format_string) - #define SPDLOG_FMT_STRING(format_string) FMT_STRING(format_string) - #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) - #include - #endif -#else - #define SPDLOG_FMT_RUNTIME(format_string) format_string - #define SPDLOG_FMT_STRING(format_string) format_string -#endif - -// visual studio up to 2013 does not support noexcept nor constexpr -#if defined(_MSC_VER) && (_MSC_VER < 1900) - #define SPDLOG_NOEXCEPT _NOEXCEPT - #define SPDLOG_CONSTEXPR -#else - #define SPDLOG_NOEXCEPT noexcept - #define SPDLOG_CONSTEXPR constexpr -#endif - -// If building with std::format, can just use constexpr, otherwise if building with fmt -// SPDLOG_CONSTEXPR_FUNC needs to be set the same as FMT_CONSTEXPR to avoid situations where -// a constexpr function in spdlog could end up calling a non-constexpr function in fmt -// depending on the compiler -// If fmt determines it can't use constexpr, we should inline the function instead -#ifdef SPDLOG_USE_STD_FORMAT - #define SPDLOG_CONSTEXPR_FUNC constexpr -#else // Being built with fmt - #if FMT_USE_CONSTEXPR - #define SPDLOG_CONSTEXPR_FUNC FMT_CONSTEXPR - #else - #define SPDLOG_CONSTEXPR_FUNC inline - #endif -#endif - -#if defined(__GNUC__) || defined(__clang__) - #define SPDLOG_DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) - #define SPDLOG_DEPRECATED __declspec(deprecated) -#else - #define SPDLOG_DEPRECATED -#endif - -// disable thread local on msvc 2013 -#ifndef SPDLOG_NO_TLS - #if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__cplusplus_winrt) - #define SPDLOG_NO_TLS 1 - #endif -#endif - -#ifndef SPDLOG_FUNCTION - #define SPDLOG_FUNCTION static_cast(__FUNCTION__) -#endif - -#ifdef SPDLOG_NO_EXCEPTIONS - #define SPDLOG_TRY - #define SPDLOG_THROW(ex) \ - do { \ - printf("spdlog fatal error: %s\n", ex.what()); \ - std::abort(); \ - } while (0) - #define SPDLOG_CATCH_STD -#else - #define SPDLOG_TRY try - #define SPDLOG_THROW(ex) throw(ex) - #define SPDLOG_CATCH_STD \ - catch (const std::exception &) { \ - } -#endif - -namespace spdlog { - -class formatter; - -namespace sinks { -class sink; -} - -#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) -using filename_t = std::wstring; - // allow macro expansion to occur in SPDLOG_FILENAME_T - #define SPDLOG_FILENAME_T_INNER(s) L##s - #define SPDLOG_FILENAME_T(s) SPDLOG_FILENAME_T_INNER(s) -#else -using filename_t = std::string; - #define SPDLOG_FILENAME_T(s) s -#endif - -using log_clock = std::chrono::system_clock; -using sink_ptr = std::shared_ptr; -using sinks_init_list = std::initializer_list; -using err_handler = std::function; -#ifdef SPDLOG_USE_STD_FORMAT -namespace fmt_lib = std; - -using string_view_t = std::string_view; -using memory_buf_t = std::string; - -template - #if __cpp_lib_format >= 202207L -using format_string_t = std::format_string; - #else -using format_string_t = std::string_view; - #endif - -template -struct is_convertible_to_basic_format_string - : std::integral_constant>::value> {}; - - #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) -using wstring_view_t = std::wstring_view; -using wmemory_buf_t = std::wstring; - -template - #if __cpp_lib_format >= 202207L -using wformat_string_t = std::wformat_string; - #else -using wformat_string_t = std::wstring_view; - #endif - #endif - #define SPDLOG_BUF_TO_STRING(x) x -#else // use fmt lib instead of std::format -namespace fmt_lib = fmt; - -using string_view_t = fmt::basic_string_view; -using memory_buf_t = fmt::basic_memory_buffer; - -template -using format_string_t = fmt::format_string; - -template -using remove_cvref_t = typename std::remove_cv::type>::type; - -template - #if FMT_VERSION >= 90101 -using fmt_runtime_string = fmt::runtime_format_string; - #else -using fmt_runtime_string = fmt::basic_runtime; - #endif - -// clang doesn't like SFINAE disabled constructor in std::is_convertible<> so have to repeat the -// condition from basic_format_string here, in addition, fmt::basic_runtime is only -// convertible to basic_format_string but not basic_string_view -template -struct is_convertible_to_basic_format_string - : std::integral_constant>::value || - std::is_same, fmt_runtime_string>::value> { -}; - - #if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) -using wstring_view_t = fmt::basic_string_view; -using wmemory_buf_t = fmt::basic_memory_buffer; - -template -using wformat_string_t = fmt::wformat_string; - #endif - #define SPDLOG_BUF_TO_STRING(x) fmt::to_string(x) -#endif - -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - #ifndef _WIN32 - #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows - #endif // _WIN32 -#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT - -template -struct is_convertible_to_any_format_string - : std::integral_constant::value || - is_convertible_to_basic_format_string::value> {}; - -#if defined(SPDLOG_NO_ATOMIC_LEVELS) -using level_t = details::null_atomic_int; -#else -using level_t = std::atomic; -#endif - -#define SPDLOG_LEVEL_TRACE 0 -#define SPDLOG_LEVEL_DEBUG 1 -#define SPDLOG_LEVEL_INFO 2 -#define SPDLOG_LEVEL_WARN 3 -#define SPDLOG_LEVEL_ERROR 4 -#define SPDLOG_LEVEL_CRITICAL 5 -#define SPDLOG_LEVEL_OFF 6 - -#if !defined(SPDLOG_ACTIVE_LEVEL) - #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO -#endif - -// Log level enum -namespace level { -enum level_enum : int { - trace = SPDLOG_LEVEL_TRACE, - debug = SPDLOG_LEVEL_DEBUG, - info = SPDLOG_LEVEL_INFO, - warn = SPDLOG_LEVEL_WARN, - err = SPDLOG_LEVEL_ERROR, - critical = SPDLOG_LEVEL_CRITICAL, - off = SPDLOG_LEVEL_OFF, - n_levels -}; - -#define SPDLOG_LEVEL_NAME_TRACE spdlog::string_view_t("trace", 5) -#define SPDLOG_LEVEL_NAME_DEBUG spdlog::string_view_t("debug", 5) -#define SPDLOG_LEVEL_NAME_INFO spdlog::string_view_t("info", 4) -#define SPDLOG_LEVEL_NAME_WARNING spdlog::string_view_t("warning", 7) -#define SPDLOG_LEVEL_NAME_ERROR spdlog::string_view_t("error", 5) -#define SPDLOG_LEVEL_NAME_CRITICAL spdlog::string_view_t("critical", 8) -#define SPDLOG_LEVEL_NAME_OFF spdlog::string_view_t("off", 3) - -#if !defined(SPDLOG_LEVEL_NAMES) - #define SPDLOG_LEVEL_NAMES \ - { \ - SPDLOG_LEVEL_NAME_TRACE, SPDLOG_LEVEL_NAME_DEBUG, SPDLOG_LEVEL_NAME_INFO, \ - SPDLOG_LEVEL_NAME_WARNING, SPDLOG_LEVEL_NAME_ERROR, SPDLOG_LEVEL_NAME_CRITICAL, \ - SPDLOG_LEVEL_NAME_OFF \ - } -#endif - -#if !defined(SPDLOG_SHORT_LEVEL_NAMES) - - #define SPDLOG_SHORT_LEVEL_NAMES \ - { "T", "D", "I", "W", "E", "C", "O" } -#endif - -SPDLOG_API const string_view_t &to_string_view(spdlog::level::level_enum l) SPDLOG_NOEXCEPT; -SPDLOG_API const char *to_short_c_str(spdlog::level::level_enum l) SPDLOG_NOEXCEPT; -SPDLOG_API spdlog::level::level_enum from_str(const std::string &name) SPDLOG_NOEXCEPT; - -} // namespace level - -// -// Color mode used by sinks with color support. -// -enum class color_mode { always, automatic, never }; - -// -// Pattern time - specific time getting to use for pattern_formatter. -// local time by default -// -enum class pattern_time_type { - local, // log localtime - utc // log utc -}; - -// -// Log exception -// -class SPDLOG_API spdlog_ex : public std::exception { -public: - explicit spdlog_ex(std::string msg); - spdlog_ex(const std::string &msg, int last_errno); - const char *what() const SPDLOG_NOEXCEPT override; - -private: - std::string msg_; -}; - -[[noreturn]] SPDLOG_API void throw_spdlog_ex(const std::string &msg, int last_errno); -[[noreturn]] SPDLOG_API void throw_spdlog_ex(std::string msg); - -struct source_loc { - SPDLOG_CONSTEXPR source_loc() = default; - SPDLOG_CONSTEXPR source_loc(const char *filename_in, int line_in, const char *funcname_in) - : filename{filename_in}, - line{line_in}, - funcname{funcname_in} {} - - SPDLOG_CONSTEXPR bool empty() const SPDLOG_NOEXCEPT { return line <= 0; } - const char *filename{nullptr}; - int line{0}; - const char *funcname{nullptr}; -}; - -struct file_event_handlers { - file_event_handlers() - : before_open(nullptr), - after_open(nullptr), - before_close(nullptr), - after_close(nullptr) {} - - std::function before_open; - std::function after_open; - std::function before_close; - std::function after_close; -}; - -namespace details { - -// to_string_view - -SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(const memory_buf_t &buf) - SPDLOG_NOEXCEPT { - return spdlog::string_view_t{buf.data(), buf.size()}; -} - -SPDLOG_CONSTEXPR_FUNC spdlog::string_view_t to_string_view(spdlog::string_view_t str) - SPDLOG_NOEXCEPT { - return str; -} - -#if defined(SPDLOG_WCHAR_FILENAMES) || defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) -SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(const wmemory_buf_t &buf) - SPDLOG_NOEXCEPT { - return spdlog::wstring_view_t{buf.data(), buf.size()}; -} - -SPDLOG_CONSTEXPR_FUNC spdlog::wstring_view_t to_string_view(spdlog::wstring_view_t str) - SPDLOG_NOEXCEPT { - return str; -} -#endif - -#if defined(SPDLOG_USE_STD_FORMAT) && __cpp_lib_format >= 202207L -template -SPDLOG_CONSTEXPR_FUNC std::basic_string_view to_string_view( - std::basic_format_string fmt) SPDLOG_NOEXCEPT { - return fmt.get(); -} -#endif - -// make_unique support for pre c++14 -#if __cplusplus >= 201402L // C++14 and beyond -using std::enable_if_t; -using std::make_unique; -#else -template -using enable_if_t = typename std::enable_if::type; - -template -std::unique_ptr make_unique(Args &&...args) { - static_assert(!std::is_array::value, "arrays not supported"); - return std::unique_ptr(new T(std::forward(args)...)); -} -#endif - -// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) -template ::value, int> = 0> -constexpr T conditional_static_cast(U value) { - return static_cast(value); -} - -template ::value, int> = 0> -constexpr T conditional_static_cast(U value) { - return value; -} - -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "common-inl.h" -#endif diff --git a/include/spdlog/details/backtracer-inl.h b/include/spdlog/details/backtracer-inl.h deleted file mode 100644 index 43d1002..0000000 --- a/include/spdlog/details/backtracer-inl.h +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif -namespace spdlog { -namespace details { -SPDLOG_INLINE backtracer::backtracer(const backtracer &other) { - std::lock_guard lock(other.mutex_); - enabled_ = other.enabled(); - messages_ = other.messages_; -} - -SPDLOG_INLINE backtracer::backtracer(backtracer &&other) SPDLOG_NOEXCEPT { - std::lock_guard lock(other.mutex_); - enabled_ = other.enabled(); - messages_ = std::move(other.messages_); -} - -SPDLOG_INLINE backtracer &backtracer::operator=(backtracer other) { - std::lock_guard lock(mutex_); - enabled_ = other.enabled(); - messages_ = std::move(other.messages_); - return *this; -} - -SPDLOG_INLINE void backtracer::enable(size_t size) { - std::lock_guard lock{mutex_}; - enabled_.store(true, std::memory_order_relaxed); - messages_ = circular_q{size}; -} - -SPDLOG_INLINE void backtracer::disable() { - std::lock_guard lock{mutex_}; - enabled_.store(false, std::memory_order_relaxed); -} - -SPDLOG_INLINE bool backtracer::enabled() const { return enabled_.load(std::memory_order_relaxed); } - -SPDLOG_INLINE void backtracer::push_back(const log_msg &msg) { - std::lock_guard lock{mutex_}; - messages_.push_back(log_msg_buffer{msg}); -} - -SPDLOG_INLINE bool backtracer::empty() const { - std::lock_guard lock{mutex_}; - return messages_.empty(); -} - -// pop all items in the q and apply the given fun on each of them. -SPDLOG_INLINE void backtracer::foreach_pop(std::function fun) { - std::lock_guard lock{mutex_}; - while (!messages_.empty()) { - auto &front_msg = messages_.front(); - fun(front_msg); - messages_.pop_front(); - } -} -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/backtracer.h b/include/spdlog/details/backtracer.h deleted file mode 100644 index 541339c..0000000 --- a/include/spdlog/details/backtracer.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -#include -#include -#include - -// Store log messages in circular buffer. -// Useful for storing debug data in case of error/warning happens. - -namespace spdlog { -namespace details { -class SPDLOG_API backtracer { - mutable std::mutex mutex_; - std::atomic enabled_{false}; - circular_q messages_; - -public: - backtracer() = default; - backtracer(const backtracer &other); - - backtracer(backtracer &&other) SPDLOG_NOEXCEPT; - backtracer &operator=(backtracer other); - - void enable(size_t size); - void disable(); - bool enabled() const; - void push_back(const log_msg &msg); - bool empty() const; - - // pop all items in the q and apply the given fun on each of them. - void foreach_pop(std::function fun); -}; - -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "backtracer-inl.h" -#endif diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h deleted file mode 100644 index 29e9d25..0000000 --- a/include/spdlog/details/circular_q.h +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -// circular q view of std::vector. -#pragma once - -#include -#include - -#include "spdlog/common.h" - -namespace spdlog { -namespace details { -template -class circular_q { - size_t max_items_ = 0; - typename std::vector::size_type head_ = 0; - typename std::vector::size_type tail_ = 0; - size_t overrun_counter_ = 0; - std::vector v_; - -public: - using value_type = T; - - // empty ctor - create a disabled queue with no elements allocated at all - circular_q() = default; - - explicit circular_q(size_t max_items) - : max_items_(max_items + 1) // one item is reserved as marker for full q - , - v_(max_items_) {} - - circular_q(const circular_q &) = default; - circular_q &operator=(const circular_q &) = default; - - // move cannot be default, - // since we need to reset head_, tail_, etc to zero in the moved object - circular_q(circular_q &&other) SPDLOG_NOEXCEPT { copy_moveable(std::move(other)); } - - circular_q &operator=(circular_q &&other) SPDLOG_NOEXCEPT { - copy_moveable(std::move(other)); - return *this; - } - - // push back, overrun (oldest) item if no room left - void push_back(T &&item) { - if (max_items_ > 0) { - v_[tail_] = std::move(item); - tail_ = (tail_ + 1) % max_items_; - - if (tail_ == head_) // overrun last item if full - { - head_ = (head_ + 1) % max_items_; - ++overrun_counter_; - } - } - } - - // Return reference to the front item. - // If there are no elements in the container, the behavior is undefined. - const T &front() const { return v_[head_]; } - - T &front() { return v_[head_]; } - - // Return number of elements actually stored - size_t size() const { - if (tail_ >= head_) { - return tail_ - head_; - } else { - return max_items_ - (head_ - tail_); - } - } - - // Return const reference to item by index. - // If index is out of range 0…size()-1, the behavior is undefined. - const T &at(size_t i) const { - assert(i < size()); - return v_[(head_ + i) % max_items_]; - } - - // Pop item from front. - // If there are no elements in the container, the behavior is undefined. - void pop_front() { head_ = (head_ + 1) % max_items_; } - - bool empty() const { return tail_ == head_; } - - bool full() const { - // head is ahead of the tail by 1 - if (max_items_ > 0) { - return ((tail_ + 1) % max_items_) == head_; - } - return false; - } - - size_t overrun_counter() const { return overrun_counter_; } - - void reset_overrun_counter() { overrun_counter_ = 0; } - -private: - // copy from other&& and reset it to disabled state - void copy_moveable(circular_q &&other) SPDLOG_NOEXCEPT { - max_items_ = other.max_items_; - head_ = other.head_; - tail_ = other.tail_; - overrun_counter_ = other.overrun_counter_; - v_ = std::move(other.v_); - - // put &&other in disabled, but valid state - other.max_items_ = 0; - other.head_ = other.tail_ = 0; - other.overrun_counter_ = 0; - } -}; -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/console_globals.h b/include/spdlog/details/console_globals.h deleted file mode 100644 index 9c55210..0000000 --- a/include/spdlog/details/console_globals.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -namespace spdlog { -namespace details { - -struct console_mutex { - using mutex_t = std::mutex; - static mutex_t &mutex() { - static mutex_t s_mutex; - return s_mutex; - } -}; - -struct console_nullmutex { - using mutex_t = null_mutex; - static mutex_t &mutex() { - static mutex_t s_mutex; - return s_mutex; - } -}; -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/file_helper-inl.h b/include/spdlog/details/file_helper-inl.h deleted file mode 100644 index 8742b96..0000000 --- a/include/spdlog/details/file_helper-inl.h +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace spdlog { -namespace details { - -SPDLOG_INLINE file_helper::file_helper(const file_event_handlers &event_handlers) - : event_handlers_(event_handlers) {} - -SPDLOG_INLINE file_helper::~file_helper() { close(); } - -SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate) { - close(); - filename_ = fname; - - auto *mode = SPDLOG_FILENAME_T("ab"); - auto *trunc_mode = SPDLOG_FILENAME_T("wb"); - - if (event_handlers_.before_open) { - event_handlers_.before_open(filename_); - } - for (int tries = 0; tries < open_tries_; ++tries) { - // create containing folder if not exists already. - os::create_dir(os::dir_name(fname)); - if (truncate) { - // Truncate by opening-and-closing a tmp file in "wb" mode, always - // opening the actual log-we-write-to in "ab" mode, since that - // interacts more politely with eternal processes that might - // rotate/truncate the file underneath us. - std::FILE *tmp; - if (os::fopen_s(&tmp, fname, trunc_mode)) { - continue; - } - std::fclose(tmp); - } - if (!os::fopen_s(&fd_, fname, mode)) { - if (event_handlers_.after_open) { - event_handlers_.after_open(filename_, fd_); - } - return; - } - - details::os::sleep_for_millis(open_interval_); - } - - throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", - errno); -} - -SPDLOG_INLINE void file_helper::reopen(bool truncate) { - if (filename_.empty()) { - throw_spdlog_ex("Failed re opening file - was not opened before"); - } - this->open(filename_, truncate); -} - -SPDLOG_INLINE void file_helper::flush() { - if (std::fflush(fd_) != 0) { - throw_spdlog_ex("Failed flush to file " + os::filename_to_str(filename_), errno); - } -} - -SPDLOG_INLINE void file_helper::sync() { - if (!os::fsync(fd_)) { - throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno); - } -} - -SPDLOG_INLINE void file_helper::close() { - if (fd_ != nullptr) { - if (event_handlers_.before_close) { - event_handlers_.before_close(filename_, fd_); - } - - std::fclose(fd_); - fd_ = nullptr; - - if (event_handlers_.after_close) { - event_handlers_.after_close(filename_); - } - } -} - -SPDLOG_INLINE void file_helper::write(const memory_buf_t &buf) { - if (fd_ == nullptr) return; - size_t msg_size = buf.size(); - auto data = buf.data(); - - if (!details::os::fwrite_bytes(data, msg_size, fd_)) { - throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno); - } -} - -SPDLOG_INLINE size_t file_helper::size() const { - if (fd_ == nullptr) { - throw_spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_)); - } - return os::filesize(fd_); -} - -SPDLOG_INLINE const filename_t &file_helper::filename() const { return filename_; } - -// -// return file path and its extension: -// -// "mylog.txt" => ("mylog", ".txt") -// "mylog" => ("mylog", "") -// "mylog." => ("mylog.", "") -// "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") -// -// the starting dot in filenames is ignored (hidden files): -// -// ".mylog" => (".mylog". "") -// "my_folder/.mylog" => ("my_folder/.mylog", "") -// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") -SPDLOG_INLINE std::tuple file_helper::split_by_extension( - const filename_t &fname) { - auto ext_index = fname.rfind('.'); - - // no valid extension found - return whole path and empty string as - // extension - if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1) { - return std::make_tuple(fname, filename_t()); - } - - // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile" - auto folder_index = fname.find_last_of(details::os::folder_seps_filename); - if (folder_index != filename_t::npos && folder_index >= ext_index - 1) { - return std::make_tuple(fname, filename_t()); - } - - // finally - return a valid base and extension tuple - return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index)); -} - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h deleted file mode 100644 index f0e5d18..0000000 --- a/include/spdlog/details/file_helper.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -namespace spdlog { -namespace details { - -// Helper class for file sinks. -// When failing to open a file, retry several times(5) with a delay interval(10 ms). -// Throw spdlog_ex exception on errors. - -class SPDLOG_API file_helper { -public: - file_helper() = default; - explicit file_helper(const file_event_handlers &event_handlers); - - file_helper(const file_helper &) = delete; - file_helper &operator=(const file_helper &) = delete; - ~file_helper(); - - void open(const filename_t &fname, bool truncate = false); - void reopen(bool truncate); - void flush(); - void sync(); - void close(); - void write(const memory_buf_t &buf); - size_t size() const; - const filename_t &filename() const; - - // - // return file path and its extension: - // - // "mylog.txt" => ("mylog", ".txt") - // "mylog" => ("mylog", "") - // "mylog." => ("mylog.", "") - // "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt") - // - // the starting dot in filenames is ignored (hidden files): - // - // ".mylog" => (".mylog". "") - // "my_folder/.mylog" => ("my_folder/.mylog", "") - // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") - static std::tuple split_by_extension(const filename_t &fname); - -private: - const int open_tries_ = 5; - const unsigned int open_interval_ = 10; - std::FILE *fd_{nullptr}; - filename_t filename_; - file_event_handlers event_handlers_; -}; -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "file_helper-inl.h" -#endif diff --git a/include/spdlog/details/fmt_helper.h b/include/spdlog/details/fmt_helper.h deleted file mode 100644 index 6130600..0000000 --- a/include/spdlog/details/fmt_helper.h +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -#pragma once - -#include -#include -#include -#include -#include - -#ifdef SPDLOG_USE_STD_FORMAT - #include - #include -#endif - -// Some fmt helpers to efficiently format and pad ints and strings -namespace spdlog { -namespace details { -namespace fmt_helper { - -inline void append_string_view(spdlog::string_view_t view, memory_buf_t &dest) { - auto *buf_ptr = view.data(); - dest.append(buf_ptr, buf_ptr + view.size()); -} - -#ifdef SPDLOG_USE_STD_FORMAT -template -inline void append_int(T n, memory_buf_t &dest) { - // Buffer should be large enough to hold all digits (digits10 + 1) and a sign - SPDLOG_CONSTEXPR const auto BUF_SIZE = std::numeric_limits::digits10 + 2; - char buf[BUF_SIZE]; - - auto [ptr, ec] = std::to_chars(buf, buf + BUF_SIZE, n, 10); - if (ec == std::errc()) { - dest.append(buf, ptr); - } else { - throw_spdlog_ex("Failed to format int", static_cast(ec)); - } -} -#else -template -inline void append_int(T n, memory_buf_t &dest) { - fmt::format_int i(n); - dest.append(i.data(), i.data() + i.size()); -} -#endif - -template -SPDLOG_CONSTEXPR_FUNC unsigned int count_digits_fallback(T n) { - // taken from fmt: https://github.com/fmtlib/fmt/blob/8.0.1/include/fmt/format.h#L899-L912 - unsigned int count = 1; - for (;;) { - // Integer division is slow so do it for a group of four digits instead - // of for every digit. The idea comes from the talk by Alexandrescu - // "Three Optimization Tips for C++". See speed-test for a comparison. - if (n < 10) return count; - if (n < 100) return count + 1; - if (n < 1000) return count + 2; - if (n < 10000) return count + 3; - n /= 10000u; - count += 4; - } -} - -template -inline unsigned int count_digits(T n) { - using count_type = - typename std::conditional<(sizeof(T) > sizeof(uint32_t)), uint64_t, uint32_t>::type; -#ifdef SPDLOG_USE_STD_FORMAT - return count_digits_fallback(static_cast(n)); -#else - return static_cast(fmt:: - // fmt 7.0.0 renamed the internal namespace to detail. - // See: https://github.com/fmtlib/fmt/issues/1538 - #if FMT_VERSION < 70000 - internal - #else - detail - #endif - ::count_digits(static_cast(n))); -#endif -} - -inline void pad2(int n, memory_buf_t &dest) { - if (n >= 0 && n < 100) // 0-99 - { - dest.push_back(static_cast('0' + n / 10)); - dest.push_back(static_cast('0' + n % 10)); - } else // unlikely, but just in case, let fmt deal with it - { - fmt_lib::format_to(std::back_inserter(dest), SPDLOG_FMT_STRING("{:02}"), n); - } -} - -template -inline void pad_uint(T n, unsigned int width, memory_buf_t &dest) { - static_assert(std::is_unsigned::value, "pad_uint must get unsigned T"); - for (auto digits = count_digits(n); digits < width; digits++) { - dest.push_back('0'); - } - append_int(n, dest); -} - -template -inline void pad3(T n, memory_buf_t &dest) { - static_assert(std::is_unsigned::value, "pad3 must get unsigned T"); - if (n < 1000) { - dest.push_back(static_cast(n / 100 + '0')); - n = n % 100; - dest.push_back(static_cast((n / 10) + '0')); - dest.push_back(static_cast((n % 10) + '0')); - } else { - append_int(n, dest); - } -} - -template -inline void pad6(T n, memory_buf_t &dest) { - pad_uint(n, 6, dest); -} - -template -inline void pad9(T n, memory_buf_t &dest) { - pad_uint(n, 9, dest); -} - -// return fraction of a second of the given time_point. -// e.g. -// fraction(tp) -> will return the millis part of the second -template -inline ToDuration time_fraction(log_clock::time_point tp) { - using std::chrono::duration_cast; - using std::chrono::seconds; - auto duration = tp.time_since_epoch(); - auto secs = duration_cast(duration); - return duration_cast(duration) - duration_cast(secs); -} - -} // namespace fmt_helper -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/log_msg-inl.h b/include/spdlog/details/log_msg-inl.h deleted file mode 100644 index aa3a957..0000000 --- a/include/spdlog/details/log_msg-inl.h +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include - -namespace spdlog { -namespace details { - -SPDLOG_INLINE log_msg::log_msg(spdlog::log_clock::time_point log_time, - spdlog::source_loc loc, - string_view_t a_logger_name, - spdlog::level::level_enum lvl, - spdlog::string_view_t msg) - : logger_name(a_logger_name), - level(lvl), - time(log_time) -#ifndef SPDLOG_NO_THREAD_ID - , - thread_id(os::thread_id()) -#endif - , - source(loc), - payload(msg) { -} - -SPDLOG_INLINE log_msg::log_msg(spdlog::source_loc loc, - string_view_t a_logger_name, - spdlog::level::level_enum lvl, - spdlog::string_view_t msg) - : log_msg(os::now(), loc, a_logger_name, lvl, msg) {} - -SPDLOG_INLINE log_msg::log_msg(string_view_t a_logger_name, - spdlog::level::level_enum lvl, - spdlog::string_view_t msg) - : log_msg(os::now(), source_loc{}, a_logger_name, lvl, msg) {} - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/log_msg.h b/include/spdlog/details/log_msg.h deleted file mode 100644 index 87df1e8..0000000 --- a/include/spdlog/details/log_msg.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -namespace spdlog { -namespace details { -struct SPDLOG_API log_msg { - log_msg() = default; - log_msg(log_clock::time_point log_time, - source_loc loc, - string_view_t logger_name, - level::level_enum lvl, - string_view_t msg); - log_msg(source_loc loc, string_view_t logger_name, level::level_enum lvl, string_view_t msg); - log_msg(string_view_t logger_name, level::level_enum lvl, string_view_t msg); - log_msg(const log_msg &other) = default; - log_msg &operator=(const log_msg &other) = default; - - string_view_t logger_name; - level::level_enum level{level::off}; - log_clock::time_point time; - size_t thread_id{0}; - - // wrapping the formatted text with color (updated by pattern_formatter). - mutable size_t color_range_start{0}; - mutable size_t color_range_end{0}; - - source_loc source; - string_view_t payload; -}; -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "log_msg-inl.h" -#endif diff --git a/include/spdlog/details/log_msg_buffer-inl.h b/include/spdlog/details/log_msg_buffer-inl.h deleted file mode 100644 index 2eb2428..0000000 --- a/include/spdlog/details/log_msg_buffer-inl.h +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -namespace spdlog { -namespace details { - -SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg &orig_msg) - : log_msg{orig_msg} { - buffer.append(logger_name.begin(), logger_name.end()); - buffer.append(payload.begin(), payload.end()); - update_string_views(); -} - -SPDLOG_INLINE log_msg_buffer::log_msg_buffer(const log_msg_buffer &other) - : log_msg{other} { - buffer.append(logger_name.begin(), logger_name.end()); - buffer.append(payload.begin(), payload.end()); - update_string_views(); -} - -SPDLOG_INLINE log_msg_buffer::log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT - : log_msg{other}, - buffer{std::move(other.buffer)} { - update_string_views(); -} - -SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(const log_msg_buffer &other) { - log_msg::operator=(other); - buffer.clear(); - buffer.append(other.buffer.data(), other.buffer.data() + other.buffer.size()); - update_string_views(); - return *this; -} - -SPDLOG_INLINE log_msg_buffer &log_msg_buffer::operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT { - log_msg::operator=(other); - buffer = std::move(other.buffer); - update_string_views(); - return *this; -} - -SPDLOG_INLINE void log_msg_buffer::update_string_views() { - logger_name = string_view_t{buffer.data(), logger_name.size()}; - payload = string_view_t{buffer.data() + logger_name.size(), payload.size()}; -} - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/log_msg_buffer.h b/include/spdlog/details/log_msg_buffer.h deleted file mode 100644 index 1143b3b..0000000 --- a/include/spdlog/details/log_msg_buffer.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include - -namespace spdlog { -namespace details { - -// Extend log_msg with internal buffer to store its payload. -// This is needed since log_msg holds string_views that points to stack data. - -class SPDLOG_API log_msg_buffer : public log_msg { - memory_buf_t buffer; - void update_string_views(); - -public: - log_msg_buffer() = default; - explicit log_msg_buffer(const log_msg &orig_msg); - log_msg_buffer(const log_msg_buffer &other); - log_msg_buffer(log_msg_buffer &&other) SPDLOG_NOEXCEPT; - log_msg_buffer &operator=(const log_msg_buffer &other); - log_msg_buffer &operator=(log_msg_buffer &&other) SPDLOG_NOEXCEPT; -}; - -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "log_msg_buffer-inl.h" -#endif diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h deleted file mode 100644 index 5848cca..0000000 --- a/include/spdlog/details/mpmc_blocking_q.h +++ /dev/null @@ -1,177 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// multi producer-multi consumer blocking queue. -// enqueue(..) - will block until room found to put the new message. -// enqueue_nowait(..) - will return immediately with false if no room left in -// the queue. -// dequeue_for(..) - will block until the queue is not empty or timeout have -// passed. - -#include - -#include -#include -#include - -namespace spdlog { -namespace details { - -template -class mpmc_blocking_queue { -public: - using item_type = T; - explicit mpmc_blocking_queue(size_t max_items) - : q_(max_items) {} - -#ifndef __MINGW32__ - // try to enqueue and block if no room left - void enqueue(T &&item) { - { - std::unique_lock lock(queue_mutex_); - pop_cv_.wait(lock, [this] { return !this->q_.full(); }); - q_.push_back(std::move(item)); - } - push_cv_.notify_one(); - } - - // enqueue immediately. overrun oldest message in the queue if no room left. - void enqueue_nowait(T &&item) { - { - std::unique_lock lock(queue_mutex_); - q_.push_back(std::move(item)); - } - push_cv_.notify_one(); - } - - void enqueue_if_have_room(T &&item) { - bool pushed = false; - { - std::unique_lock lock(queue_mutex_); - if (!q_.full()) { - q_.push_back(std::move(item)); - pushed = true; - } - } - - if (pushed) { - push_cv_.notify_one(); - } else { - ++discard_counter_; - } - } - - // dequeue with a timeout. - // Return true, if succeeded dequeue item, false otherwise - bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) { - { - std::unique_lock lock(queue_mutex_); - if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) { - return false; - } - popped_item = std::move(q_.front()); - q_.pop_front(); - } - pop_cv_.notify_one(); - return true; - } - - // blocking dequeue without a timeout. - void dequeue(T &popped_item) { - { - std::unique_lock lock(queue_mutex_); - push_cv_.wait(lock, [this] { return !this->q_.empty(); }); - popped_item = std::move(q_.front()); - q_.pop_front(); - } - pop_cv_.notify_one(); - } - -#else - // apparently mingw deadlocks if the mutex is released before cv.notify_one(), - // so release the mutex at the very end each function. - - // try to enqueue and block if no room left - void enqueue(T &&item) { - std::unique_lock lock(queue_mutex_); - pop_cv_.wait(lock, [this] { return !this->q_.full(); }); - q_.push_back(std::move(item)); - push_cv_.notify_one(); - } - - // enqueue immediately. overrun oldest message in the queue if no room left. - void enqueue_nowait(T &&item) { - std::unique_lock lock(queue_mutex_); - q_.push_back(std::move(item)); - push_cv_.notify_one(); - } - - void enqueue_if_have_room(T &&item) { - bool pushed = false; - std::unique_lock lock(queue_mutex_); - if (!q_.full()) { - q_.push_back(std::move(item)); - pushed = true; - } - - if (pushed) { - push_cv_.notify_one(); - } else { - ++discard_counter_; - } - } - - // dequeue with a timeout. - // Return true, if succeeded dequeue item, false otherwise - bool dequeue_for(T &popped_item, std::chrono::milliseconds wait_duration) { - std::unique_lock lock(queue_mutex_); - if (!push_cv_.wait_for(lock, wait_duration, [this] { return !this->q_.empty(); })) { - return false; - } - popped_item = std::move(q_.front()); - q_.pop_front(); - pop_cv_.notify_one(); - return true; - } - - // blocking dequeue without a timeout. - void dequeue(T &popped_item) { - std::unique_lock lock(queue_mutex_); - push_cv_.wait(lock, [this] { return !this->q_.empty(); }); - popped_item = std::move(q_.front()); - q_.pop_front(); - pop_cv_.notify_one(); - } - -#endif - - size_t overrun_counter() { - std::lock_guard lock(queue_mutex_); - return q_.overrun_counter(); - } - - size_t discard_counter() { return discard_counter_.load(std::memory_order_relaxed); } - - size_t size() { - std::lock_guard lock(queue_mutex_); - return q_.size(); - } - - void reset_overrun_counter() { - std::lock_guard lock(queue_mutex_); - q_.reset_overrun_counter(); - } - - void reset_discard_counter() { discard_counter_.store(0, std::memory_order_relaxed); } - -private: - std::mutex queue_mutex_; - std::condition_variable push_cv_; - std::condition_variable pop_cv_; - spdlog::details::circular_q q_; - std::atomic discard_counter_{0}; -}; -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/null_mutex.h b/include/spdlog/details/null_mutex.h deleted file mode 100644 index e3b3220..0000000 --- a/include/spdlog/details/null_mutex.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -// null, no cost dummy "mutex" and dummy "atomic" int - -namespace spdlog { -namespace details { -struct null_mutex { - void lock() const {} - void unlock() const {} -}; - -struct null_atomic_int { - int value; - null_atomic_int() = default; - - explicit null_atomic_int(int new_value) - : value(new_value) {} - - int load(std::memory_order = std::memory_order_relaxed) const { return value; } - - void store(int new_value, std::memory_order = std::memory_order_relaxed) { value = new_value; } - - int exchange(int new_value, std::memory_order = std::memory_order_relaxed) { - std::swap(new_value, value); - return new_value; // return value before the call - } -}; - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/os-inl.h b/include/spdlog/details/os-inl.h deleted file mode 100644 index 8ee4230..0000000 --- a/include/spdlog/details/os-inl.h +++ /dev/null @@ -1,606 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef _WIN32 - #include - #include // for FlushFileBuffers - #include // for _get_osfhandle, _isatty, _fileno - #include // for _get_pid - - #ifdef __MINGW32__ - #include - #endif - - #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES) - #include - #include - #endif - - #include // for _mkdir/_wmkdir - -#else // unix - - #include - #include - - #ifdef __linux__ - #include //Use gettid() syscall under linux to get thread id - - #elif defined(_AIX) - #include // for pthread_getthrds_np - - #elif defined(__DragonFly__) || defined(__FreeBSD__) - #include // for pthread_getthreadid_np - - #elif defined(__NetBSD__) - #include // for _lwp_self - - #elif defined(__sun) - #include // for thr_self - #endif - -#endif // unix - -#if defined __APPLE__ - #include -#endif - -#ifndef __has_feature // Clang - feature checking macros. - #define __has_feature(x) 0 // Compatibility with non-clang compilers. -#endif - -namespace spdlog { -namespace details { -namespace os { - -SPDLOG_INLINE spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT { -#if defined __linux__ && defined SPDLOG_CLOCK_COARSE - timespec ts; - ::clock_gettime(CLOCK_REALTIME_COARSE, &ts); - return std::chrono::time_point( - std::chrono::duration_cast( - std::chrono::seconds(ts.tv_sec) + std::chrono::nanoseconds(ts.tv_nsec))); - -#else - return log_clock::now(); -#endif -} -SPDLOG_INLINE std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT { -#ifdef _WIN32 - std::tm tm; - ::localtime_s(&tm, &time_tt); -#else - std::tm tm; - ::localtime_r(&time_tt, &tm); -#endif - return tm; -} - -SPDLOG_INLINE std::tm localtime() SPDLOG_NOEXCEPT { - std::time_t now_t = ::time(nullptr); - return localtime(now_t); -} - -SPDLOG_INLINE std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT { -#ifdef _WIN32 - std::tm tm; - ::gmtime_s(&tm, &time_tt); -#else - std::tm tm; - ::gmtime_r(&time_tt, &tm); -#endif - return tm; -} - -SPDLOG_INLINE std::tm gmtime() SPDLOG_NOEXCEPT { - std::time_t now_t = ::time(nullptr); - return gmtime(now_t); -} - -// fopen_s on non windows for writing -SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode) { -#ifdef _WIN32 - #ifdef SPDLOG_WCHAR_FILENAMES - *fp = ::_wfsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); - #else - *fp = ::_fsopen((filename.c_str()), mode.c_str(), _SH_DENYNO); - #endif - #if defined(SPDLOG_PREVENT_CHILD_FD) - if (*fp != nullptr) { - auto file_handle = reinterpret_cast(_get_osfhandle(::_fileno(*fp))); - if (!::SetHandleInformation(file_handle, HANDLE_FLAG_INHERIT, 0)) { - ::fclose(*fp); - *fp = nullptr; - } - } - #endif -#else // unix - #if defined(SPDLOG_PREVENT_CHILD_FD) - const int mode_flag = mode == SPDLOG_FILENAME_T("ab") ? O_APPEND : O_TRUNC; - const int fd = - ::open((filename.c_str()), O_CREAT | O_WRONLY | O_CLOEXEC | mode_flag, mode_t(0644)); - if (fd == -1) { - return true; - } - *fp = ::fdopen(fd, mode.c_str()); - if (*fp == nullptr) { - ::close(fd); - } - #else - *fp = ::fopen((filename.c_str()), mode.c_str()); - #endif -#endif - - return *fp == nullptr; -} - -SPDLOG_INLINE int remove(const filename_t &filename) SPDLOG_NOEXCEPT { -#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - return ::_wremove(filename.c_str()); -#else - return std::remove(filename.c_str()); -#endif -} - -SPDLOG_INLINE int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT { - return path_exists(filename) ? remove(filename) : 0; -} - -SPDLOG_INLINE int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT { -#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - return ::_wrename(filename1.c_str(), filename2.c_str()); -#else - return std::rename(filename1.c_str(), filename2.c_str()); -#endif -} - -// Return true if path exists (file or directory) -SPDLOG_INLINE bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT { -#ifdef _WIN32 - struct _stat buffer; - #ifdef SPDLOG_WCHAR_FILENAMES - return (::_wstat(filename.c_str(), &buffer) == 0); - #else - return (::_stat(filename.c_str(), &buffer) == 0); - #endif -#else // common linux/unix all have the stat system call - struct stat buffer; - return (::stat(filename.c_str(), &buffer) == 0); -#endif -} - -#ifdef _MSC_VER - // avoid warning about unreachable statement at the end of filesize() - #pragma warning(push) - #pragma warning(disable : 4702) -#endif - -// Return file size according to open FILE* object -SPDLOG_INLINE size_t filesize(FILE *f) { - if (f == nullptr) { - throw_spdlog_ex("Failed getting file size. fd is null"); - } -#if defined(_WIN32) && !defined(__CYGWIN__) - int fd = ::_fileno(f); - #if defined(_WIN64) // 64 bits - __int64 ret = ::_filelengthi64(fd); - if (ret >= 0) { - return static_cast(ret); - } - - #else // windows 32 bits - long ret = ::_filelength(fd); - if (ret >= 0) { - return static_cast(ret); - } - #endif - -#else // unix - // OpenBSD and AIX doesn't compile with :: before the fileno(..) - #if defined(__OpenBSD__) || defined(_AIX) - int fd = fileno(f); - #else - int fd = ::fileno(f); - #endif - // 64 bits(but not in osx, linux/musl or cygwin, where fstat64 is deprecated) - #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__sun) || defined(_AIX)) && \ - (defined(__LP64__) || defined(_LP64)) - struct stat64 st; - if (::fstat64(fd, &st) == 0) { - return static_cast(st.st_size); - } - #else // other unix or linux 32 bits or cygwin - struct stat st; - if (::fstat(fd, &st) == 0) { - return static_cast(st.st_size); - } - #endif -#endif - throw_spdlog_ex("Failed getting file size from fd", errno); - return 0; // will not be reached. -} - -#ifdef _MSC_VER - #pragma warning(pop) -#endif - -// Return utc offset in minutes or throw spdlog_ex on failure -SPDLOG_INLINE int utc_minutes_offset(const std::tm &tm) { -#ifdef _WIN32 - #if _WIN32_WINNT < _WIN32_WINNT_WS08 - TIME_ZONE_INFORMATION tzinfo; - auto rv = ::GetTimeZoneInformation(&tzinfo); - #else - DYNAMIC_TIME_ZONE_INFORMATION tzinfo; - auto rv = ::GetDynamicTimeZoneInformation(&tzinfo); - #endif - if (rv == TIME_ZONE_ID_INVALID) throw_spdlog_ex("Failed getting timezone info. ", errno); - - int offset = -tzinfo.Bias; - if (tm.tm_isdst) { - offset -= tzinfo.DaylightBias; - } else { - offset -= tzinfo.StandardBias; - } - return offset; -#else - - #if defined(sun) || defined(__sun) || defined(_AIX) || \ - (defined(__NEWLIB__) && !defined(__TM_GMTOFF)) || \ - (!defined(_BSD_SOURCE) && !defined(_GNU_SOURCE)) - // 'tm_gmtoff' field is BSD extension and it's missing on SunOS/Solaris - struct helper { - static long int calculate_gmt_offset(const std::tm &localtm = details::os::localtime(), - const std::tm &gmtm = details::os::gmtime()) { - int local_year = localtm.tm_year + (1900 - 1); - int gmt_year = gmtm.tm_year + (1900 - 1); - - long int days = ( - // difference in day of year - localtm.tm_yday - - gmtm.tm_yday - - // + intervening leap days - + ((local_year >> 2) - (gmt_year >> 2)) - (local_year / 100 - gmt_year / 100) + - ((local_year / 100 >> 2) - (gmt_year / 100 >> 2)) - - // + difference in years * 365 */ - + static_cast(local_year - gmt_year) * 365); - - long int hours = (24 * days) + (localtm.tm_hour - gmtm.tm_hour); - long int mins = (60 * hours) + (localtm.tm_min - gmtm.tm_min); - long int secs = (60 * mins) + (localtm.tm_sec - gmtm.tm_sec); - - return secs; - } - }; - - auto offset_seconds = helper::calculate_gmt_offset(tm); - #else - auto offset_seconds = tm.tm_gmtoff; - #endif - - return static_cast(offset_seconds / 60); -#endif -} - -// Return current thread id as size_t -// It exists because the std::this_thread::get_id() is much slower(especially -// under VS 2013) -SPDLOG_INLINE size_t _thread_id() SPDLOG_NOEXCEPT { -#ifdef _WIN32 - return static_cast(::GetCurrentThreadId()); -#elif defined(__linux__) - #if defined(__ANDROID__) && defined(__ANDROID_API__) && (__ANDROID_API__ < 21) - #define SYS_gettid __NR_gettid - #endif - return static_cast(::syscall(SYS_gettid)); -#elif defined(_AIX) - struct __pthrdsinfo buf; - int reg_size = 0; - pthread_t pt = pthread_self(); - int retval = pthread_getthrds_np(&pt, PTHRDSINFO_QUERY_TID, &buf, sizeof(buf), NULL, ®_size); - int tid = (!retval) ? buf.__pi_tid : 0; - return static_cast(tid); -#elif defined(__DragonFly__) || defined(__FreeBSD__) - return static_cast(::pthread_getthreadid_np()); -#elif defined(__NetBSD__) - return static_cast(::_lwp_self()); -#elif defined(__OpenBSD__) - return static_cast(::getthrid()); -#elif defined(__sun) - return static_cast(::thr_self()); -#elif __APPLE__ - uint64_t tid; - // There is no pthread_threadid_np prior to Mac OS X 10.6, and it is not supported on any PPC, - // including 10.6.8 Rosetta. __POWERPC__ is Apple-specific define encompassing ppc and ppc64. - #ifdef MAC_OS_X_VERSION_MAX_ALLOWED - { - #if (MAC_OS_X_VERSION_MAX_ALLOWED < 1060) || defined(__POWERPC__) - tid = pthread_mach_thread_np(pthread_self()); - #elif MAC_OS_X_VERSION_MIN_REQUIRED < 1060 - if (&pthread_threadid_np) { - pthread_threadid_np(nullptr, &tid); - } else { - tid = pthread_mach_thread_np(pthread_self()); - } - #else - pthread_threadid_np(nullptr, &tid); - #endif - } - #else - pthread_threadid_np(nullptr, &tid); - #endif - return static_cast(tid); -#else // Default to standard C++11 (other Unix) - return static_cast(std::hash()(std::this_thread::get_id())); -#endif -} - -// Return current thread id as size_t (from thread local storage) -SPDLOG_INLINE size_t thread_id() SPDLOG_NOEXCEPT { -#if defined(SPDLOG_NO_TLS) - return _thread_id(); -#else // cache thread id in tls - static thread_local const size_t tid = _thread_id(); - return tid; -#endif -} - -// This is avoid msvc issue in sleep_for that happens if the clock changes. -// See https://github.com/gabime/spdlog/issues/609 -SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT { -#if defined(_WIN32) - ::Sleep(milliseconds); -#else - std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); -#endif -} - -// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined) -#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) -SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) { - memory_buf_t buf; - wstr_to_utf8buf(filename, buf); - return SPDLOG_BUF_TO_STRING(buf); -} -#else -SPDLOG_INLINE std::string filename_to_str(const filename_t &filename) { return filename; } -#endif - -SPDLOG_INLINE int pid() SPDLOG_NOEXCEPT { -#ifdef _WIN32 - return conditional_static_cast(::GetCurrentProcessId()); -#else - return conditional_static_cast(::getpid()); -#endif -} - -// Determine if the terminal supports colors -// Based on: https://github.com/agauniyal/rang/ -SPDLOG_INLINE bool is_color_terminal() SPDLOG_NOEXCEPT { -#ifdef _WIN32 - return true; -#else - - static const bool result = []() { - const char *env_colorterm_p = std::getenv("COLORTERM"); - if (env_colorterm_p != nullptr) { - return true; - } - - static constexpr std::array terms = { - {"ansi", "color", "console", "cygwin", "gnome", "konsole", "kterm", "linux", "msys", - "putty", "rxvt", "screen", "vt100", "xterm", "alacritty", "vt102"}}; - - const char *env_term_p = std::getenv("TERM"); - if (env_term_p == nullptr) { - return false; - } - - return std::any_of(terms.begin(), terms.end(), [&](const char *term) { - return std::strstr(env_term_p, term) != nullptr; - }); - }(); - - return result; -#endif -} - -// Determine if the terminal attached -// Source: https://github.com/agauniyal/rang/ -SPDLOG_INLINE bool in_terminal(FILE *file) SPDLOG_NOEXCEPT { -#ifdef _WIN32 - return ::_isatty(_fileno(file)) != 0; -#else - return ::isatty(fileno(file)) != 0; -#endif -} - -#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) -SPDLOG_INLINE void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target) { - if (wstr.size() > static_cast((std::numeric_limits::max)()) / 4 - 1) { - throw_spdlog_ex("UTF-16 string is too big to be converted to UTF-8"); - } - - int wstr_size = static_cast(wstr.size()); - if (wstr_size == 0) { - target.resize(0); - return; - } - - int result_size = static_cast(target.capacity()); - if ((wstr_size + 1) * 4 > result_size) { - result_size = - ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, NULL, 0, NULL, NULL); - } - - if (result_size > 0) { - target.resize(result_size); - result_size = ::WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr_size, target.data(), - result_size, NULL, NULL); - - if (result_size > 0) { - target.resize(result_size); - return; - } - } - - throw_spdlog_ex( - fmt_lib::format("WideCharToMultiByte failed. Last error: {}", ::GetLastError())); -} - -SPDLOG_INLINE void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target) { - if (str.size() > static_cast((std::numeric_limits::max)()) - 1) { - throw_spdlog_ex("UTF-8 string is too big to be converted to UTF-16"); - } - - int str_size = static_cast(str.size()); - if (str_size == 0) { - target.resize(0); - return; - } - - // find the size to allocate for the result buffer - int result_size = - ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, NULL, 0); - - if (result_size > 0) { - target.resize(result_size); - result_size = ::MultiByteToWideChar(CP_UTF8, 0, str.data(), str_size, target.data(), - result_size); - if (result_size > 0) { - assert(result_size == target.size()); - return; - } - } - - throw_spdlog_ex( - fmt_lib::format("MultiByteToWideChar failed. Last error: {}", ::GetLastError())); -} -#endif // (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && - // defined(_WIN32) - -// return true on success -static SPDLOG_INLINE bool mkdir_(const filename_t &path) { -#ifdef _WIN32 - #ifdef SPDLOG_WCHAR_FILENAMES - return ::_wmkdir(path.c_str()) == 0; - #else - return ::_mkdir(path.c_str()) == 0; - #endif -#else - return ::mkdir(path.c_str(), mode_t(0755)) == 0; -#endif -} - -// create the given directory - and all directories leading to it -// return true on success or if the directory already exists -SPDLOG_INLINE bool create_dir(const filename_t &path) { - if (path_exists(path)) { - return true; - } - - if (path.empty()) { - return false; - } - - size_t search_offset = 0; - do { - auto token_pos = path.find_first_of(folder_seps_filename, search_offset); - // treat the entire path as a folder if no folder separator not found - if (token_pos == filename_t::npos) { - token_pos = path.size(); - } - - auto subdir = path.substr(0, token_pos); -#ifdef _WIN32 - // if subdir is just a drive letter, add a slash e.g. "c:"=>"c:\", - // otherwise path_exists(subdir) returns false (issue #3079) - const bool is_drive = subdir.length() == 2 && subdir[1] == ':'; - if (is_drive) { - subdir += '\\'; - token_pos++; - } -#endif - - if (!subdir.empty() && !path_exists(subdir) && !mkdir_(subdir)) { - return false; // return error if failed creating dir - } - search_offset = token_pos + 1; - } while (search_offset < path.size()); - - return true; -} - -// Return directory name from given path or empty string -// "abc/file" => "abc" -// "abc/" => "abc" -// "abc" => "" -// "abc///" => "abc//" -SPDLOG_INLINE filename_t dir_name(const filename_t &path) { - auto pos = path.find_last_of(folder_seps_filename); - return pos != filename_t::npos ? path.substr(0, pos) : filename_t{}; -} - -std::string SPDLOG_INLINE getenv(const char *field) { -#if defined(_MSC_VER) - #if defined(__cplusplus_winrt) - return std::string{}; // not supported under uwp - #else - size_t len = 0; - char buf[128]; - bool ok = ::getenv_s(&len, buf, sizeof(buf), field) == 0; - return ok ? buf : std::string{}; - #endif -#else // revert to getenv - char *buf = ::getenv(field); - return buf ? buf : std::string{}; -#endif -} - -// Do fsync by FILE handlerpointer -// Return true on success -SPDLOG_INLINE bool fsync(FILE *fp) { -#ifdef _WIN32 - return FlushFileBuffers(reinterpret_cast(_get_osfhandle(_fileno(fp)))) != 0; -#else - return ::fsync(fileno(fp)) == 0; -#endif -} - -// Do non-locking fwrite if possible by the os or use the regular locking fwrite -// Return true on success. -SPDLOG_INLINE bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp) { - #if defined(_WIN32) && defined(SPDLOG_FWRITE_UNLOCKED) - return _fwrite_nolock(ptr, 1, n_bytes, fp) == n_bytes; - #elif defined(SPDLOG_FWRITE_UNLOCKED) - return ::fwrite_unlocked(ptr, 1, n_bytes, fp) == n_bytes; - #else - return std::fwrite(ptr, 1, n_bytes, fp) == n_bytes; - #endif -} - -} // namespace os -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h deleted file mode 100644 index 5fd12ba..0000000 --- a/include/spdlog/details/os.h +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include // std::time_t -#include - -namespace spdlog { -namespace details { -namespace os { - -SPDLOG_API spdlog::log_clock::time_point now() SPDLOG_NOEXCEPT; - -SPDLOG_API std::tm localtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT; - -SPDLOG_API std::tm localtime() SPDLOG_NOEXCEPT; - -SPDLOG_API std::tm gmtime(const std::time_t &time_tt) SPDLOG_NOEXCEPT; - -SPDLOG_API std::tm gmtime() SPDLOG_NOEXCEPT; - -// eol definition -#if !defined(SPDLOG_EOL) - #ifdef _WIN32 - #define SPDLOG_EOL "\r\n" - #else - #define SPDLOG_EOL "\n" - #endif -#endif - -SPDLOG_CONSTEXPR static const char *default_eol = SPDLOG_EOL; - -// folder separator -#if !defined(SPDLOG_FOLDER_SEPS) - #ifdef _WIN32 - #define SPDLOG_FOLDER_SEPS "\\/" - #else - #define SPDLOG_FOLDER_SEPS "/" - #endif -#endif - -SPDLOG_CONSTEXPR static const char folder_seps[] = SPDLOG_FOLDER_SEPS; -SPDLOG_CONSTEXPR static const filename_t::value_type folder_seps_filename[] = - SPDLOG_FILENAME_T(SPDLOG_FOLDER_SEPS); - -// fopen_s on non windows for writing -SPDLOG_API bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode); - -// Remove filename. return 0 on success -SPDLOG_API int remove(const filename_t &filename) SPDLOG_NOEXCEPT; - -// Remove file if exists. return 0 on success -// Note: Non atomic (might return failure to delete if concurrently deleted by other process/thread) -SPDLOG_API int remove_if_exists(const filename_t &filename) SPDLOG_NOEXCEPT; - -SPDLOG_API int rename(const filename_t &filename1, const filename_t &filename2) SPDLOG_NOEXCEPT; - -// Return if file exists. -SPDLOG_API bool path_exists(const filename_t &filename) SPDLOG_NOEXCEPT; - -// Return file size according to open FILE* object -SPDLOG_API size_t filesize(FILE *f); - -// Return utc offset in minutes or throw spdlog_ex on failure -SPDLOG_API int utc_minutes_offset(const std::tm &tm = details::os::localtime()); - -// Return current thread id as size_t -// It exists because the std::this_thread::get_id() is much slower(especially -// under VS 2013) -SPDLOG_API size_t _thread_id() SPDLOG_NOEXCEPT; - -// Return current thread id as size_t (from thread local storage) -SPDLOG_API size_t thread_id() SPDLOG_NOEXCEPT; - -// This is avoid msvc issue in sleep_for that happens if the clock changes. -// See https://github.com/gabime/spdlog/issues/609 -SPDLOG_API void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT; - -SPDLOG_API std::string filename_to_str(const filename_t &filename); - -SPDLOG_API int pid() SPDLOG_NOEXCEPT; - -// Determine if the terminal supports colors -// Source: https://github.com/agauniyal/rang/ -SPDLOG_API bool is_color_terminal() SPDLOG_NOEXCEPT; - -// Determine if the terminal attached -// Source: https://github.com/agauniyal/rang/ -SPDLOG_API bool in_terminal(FILE *file) SPDLOG_NOEXCEPT; - -#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) -SPDLOG_API void wstr_to_utf8buf(wstring_view_t wstr, memory_buf_t &target); - -SPDLOG_API void utf8_to_wstrbuf(string_view_t str, wmemory_buf_t &target); -#endif - -// Return directory name from given path or empty string -// "abc/file" => "abc" -// "abc/" => "abc" -// "abc" => "" -// "abc///" => "abc//" -SPDLOG_API filename_t dir_name(const filename_t &path); - -// Create a dir from the given path. -// Return true if succeeded or if this dir already exists. -SPDLOG_API bool create_dir(const filename_t &path); - -// non thread safe, cross platform getenv/getenv_s -// return empty string if field not found -SPDLOG_API std::string getenv(const char *field); - -// Do fsync by FILE objectpointer. -// Return true on success. -SPDLOG_API bool fsync(FILE *fp); - -// Do non-locking fwrite if possible by the os or use the regular locking fwrite -// Return true on success. -SPDLOG_API bool fwrite_bytes(const void *ptr, const size_t n_bytes, FILE *fp); - -} // namespace os -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "os-inl.h" -#endif diff --git a/include/spdlog/details/periodic_worker-inl.h b/include/spdlog/details/periodic_worker-inl.h deleted file mode 100644 index 18f11fb..0000000 --- a/include/spdlog/details/periodic_worker-inl.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -namespace spdlog { -namespace details { - -// stop the worker thread and join it -SPDLOG_INLINE periodic_worker::~periodic_worker() { - if (worker_thread_.joinable()) { - { - std::lock_guard lock(mutex_); - active_ = false; - } - cv_.notify_one(); - worker_thread_.join(); - } -} - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/periodic_worker.h b/include/spdlog/details/periodic_worker.h deleted file mode 100644 index d647b66..0000000 --- a/include/spdlog/details/periodic_worker.h +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// periodic worker thread - periodically executes the given callback function. -// -// RAII over the owned thread: -// creates the thread on construction. -// stops and joins the thread on destruction (if the thread is executing a callback, wait for it -// to finish first). - -#include -#include -#include -#include -#include -namespace spdlog { -namespace details { - -class SPDLOG_API periodic_worker { -public: - template - periodic_worker(const std::function &callback_fun, - std::chrono::duration interval) { - active_ = (interval > std::chrono::duration::zero()); - if (!active_) { - return; - } - - worker_thread_ = std::thread([this, callback_fun, interval]() { - for (;;) { - std::unique_lock lock(this->mutex_); - if (this->cv_.wait_for(lock, interval, [this] { return !this->active_; })) { - return; // active_ == false, so exit this thread - } - callback_fun(); - } - }); - } - std::thread &get_thread() { return worker_thread_; } - periodic_worker(const periodic_worker &) = delete; - periodic_worker &operator=(const periodic_worker &) = delete; - // stop the worker thread and join it - ~periodic_worker(); - -private: - bool active_; - std::thread worker_thread_; - std::mutex mutex_; - std::condition_variable cv_; -}; -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "periodic_worker-inl.h" -#endif diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h deleted file mode 100644 index f447848..0000000 --- a/include/spdlog/details/registry-inl.h +++ /dev/null @@ -1,261 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include -#include -#include - -#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER - // support for the default stdout color logger - #ifdef _WIN32 - #include - #else - #include - #endif -#endif // SPDLOG_DISABLE_DEFAULT_LOGGER - -#include -#include -#include -#include -#include - -namespace spdlog { -namespace details { - -SPDLOG_INLINE registry::registry() - : formatter_(new pattern_formatter()) { -#ifndef SPDLOG_DISABLE_DEFAULT_LOGGER - // create default logger (ansicolor_stdout_sink_mt or wincolor_stdout_sink_mt in windows). - #ifdef _WIN32 - auto color_sink = std::make_shared(); - #else - auto color_sink = std::make_shared(); - #endif - - const char *default_logger_name = ""; - default_logger_ = std::make_shared(default_logger_name, std::move(color_sink)); - loggers_[default_logger_name] = default_logger_; - -#endif // SPDLOG_DISABLE_DEFAULT_LOGGER -} - -SPDLOG_INLINE registry::~registry() = default; - -SPDLOG_INLINE void registry::register_logger(std::shared_ptr new_logger) { - std::lock_guard lock(logger_map_mutex_); - register_logger_(std::move(new_logger)); -} - -SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr new_logger) { - std::lock_guard lock(logger_map_mutex_); - new_logger->set_formatter(formatter_->clone()); - - if (err_handler_) { - new_logger->set_error_handler(err_handler_); - } - - // set new level according to previously configured level or default level - auto it = log_levels_.find(new_logger->name()); - auto new_level = it != log_levels_.end() ? it->second : global_log_level_; - new_logger->set_level(new_level); - - new_logger->flush_on(flush_level_); - - if (backtrace_n_messages_ > 0) { - new_logger->enable_backtrace(backtrace_n_messages_); - } - - if (automatic_registration_) { - register_logger_(std::move(new_logger)); - } -} - -SPDLOG_INLINE std::shared_ptr registry::get(const std::string &logger_name) { - std::lock_guard lock(logger_map_mutex_); - auto found = loggers_.find(logger_name); - return found == loggers_.end() ? nullptr : found->second; -} - -SPDLOG_INLINE std::shared_ptr registry::default_logger() { - std::lock_guard lock(logger_map_mutex_); - return default_logger_; -} - -// Return raw ptr to the default logger. -// To be used directly by the spdlog default api (e.g. spdlog::info) -// This make the default API faster, but cannot be used concurrently with set_default_logger(). -// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. -SPDLOG_INLINE logger *registry::get_default_raw() { return default_logger_.get(); } - -// set default logger. -// default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map. -SPDLOG_INLINE void registry::set_default_logger(std::shared_ptr new_default_logger) { - std::lock_guard lock(logger_map_mutex_); - if (new_default_logger != nullptr) { - loggers_[new_default_logger->name()] = new_default_logger; - } - default_logger_ = std::move(new_default_logger); -} - -SPDLOG_INLINE void registry::set_tp(std::shared_ptr tp) { - std::lock_guard lock(tp_mutex_); - tp_ = std::move(tp); -} - -SPDLOG_INLINE std::shared_ptr registry::get_tp() { - std::lock_guard lock(tp_mutex_); - return tp_; -} - -// Set global formatter. Each sink in each logger will get a clone of this object -SPDLOG_INLINE void registry::set_formatter(std::unique_ptr formatter) { - std::lock_guard lock(logger_map_mutex_); - formatter_ = std::move(formatter); - for (auto &l : loggers_) { - l.second->set_formatter(formatter_->clone()); - } -} - -SPDLOG_INLINE void registry::enable_backtrace(size_t n_messages) { - std::lock_guard lock(logger_map_mutex_); - backtrace_n_messages_ = n_messages; - - for (auto &l : loggers_) { - l.second->enable_backtrace(n_messages); - } -} - -SPDLOG_INLINE void registry::disable_backtrace() { - std::lock_guard lock(logger_map_mutex_); - backtrace_n_messages_ = 0; - for (auto &l : loggers_) { - l.second->disable_backtrace(); - } -} - -SPDLOG_INLINE void registry::set_level(level::level_enum log_level) { - std::lock_guard lock(logger_map_mutex_); - for (auto &l : loggers_) { - l.second->set_level(log_level); - } - global_log_level_ = log_level; -} - -SPDLOG_INLINE void registry::flush_on(level::level_enum log_level) { - std::lock_guard lock(logger_map_mutex_); - for (auto &l : loggers_) { - l.second->flush_on(log_level); - } - flush_level_ = log_level; -} - -SPDLOG_INLINE void registry::set_error_handler(err_handler handler) { - std::lock_guard lock(logger_map_mutex_); - for (auto &l : loggers_) { - l.second->set_error_handler(handler); - } - err_handler_ = std::move(handler); -} - -SPDLOG_INLINE void registry::apply_all( - const std::function)> &fun) { - std::lock_guard lock(logger_map_mutex_); - for (auto &l : loggers_) { - fun(l.second); - } -} - -SPDLOG_INLINE void registry::flush_all() { - std::lock_guard lock(logger_map_mutex_); - for (auto &l : loggers_) { - l.second->flush(); - } -} - -SPDLOG_INLINE void registry::drop(const std::string &logger_name) { - std::lock_guard lock(logger_map_mutex_); - auto is_default_logger = default_logger_ && default_logger_->name() == logger_name; - loggers_.erase(logger_name); - if (is_default_logger) { - default_logger_.reset(); - } -} - -SPDLOG_INLINE void registry::drop_all() { - std::lock_guard lock(logger_map_mutex_); - loggers_.clear(); - default_logger_.reset(); -} - -// clean all resources and threads started by the registry -SPDLOG_INLINE void registry::shutdown() { - { - std::lock_guard lock(flusher_mutex_); - periodic_flusher_.reset(); - } - - drop_all(); - - { - std::lock_guard lock(tp_mutex_); - tp_.reset(); - } -} - -SPDLOG_INLINE std::recursive_mutex ®istry::tp_mutex() { return tp_mutex_; } - -SPDLOG_INLINE void registry::set_automatic_registration(bool automatic_registration) { - std::lock_guard lock(logger_map_mutex_); - automatic_registration_ = automatic_registration; -} - -SPDLOG_INLINE void registry::set_levels(log_levels levels, level::level_enum *global_level) { - std::lock_guard lock(logger_map_mutex_); - log_levels_ = std::move(levels); - auto global_level_requested = global_level != nullptr; - global_log_level_ = global_level_requested ? *global_level : global_log_level_; - - for (auto &logger : loggers_) { - auto logger_entry = log_levels_.find(logger.first); - if (logger_entry != log_levels_.end()) { - logger.second->set_level(logger_entry->second); - } else if (global_level_requested) { - logger.second->set_level(*global_level); - } - } -} - -SPDLOG_INLINE registry ®istry::instance() { - static registry s_instance; - return s_instance; -} - -SPDLOG_INLINE void registry::apply_logger_env_levels(std::shared_ptr new_logger) { - std::lock_guard lock(logger_map_mutex_); - auto it = log_levels_.find(new_logger->name()); - auto new_level = it != log_levels_.end() ? it->second : global_log_level_; - new_logger->set_level(new_level); -} - -SPDLOG_INLINE void registry::throw_if_exists_(const std::string &logger_name) { - if (loggers_.find(logger_name) != loggers_.end()) { - throw_spdlog_ex("logger with name '" + logger_name + "' already exists"); - } -} - -SPDLOG_INLINE void registry::register_logger_(std::shared_ptr new_logger) { - auto logger_name = new_logger->name(); - throw_if_exists_(logger_name); - loggers_[logger_name] = std::move(new_logger); -} - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h deleted file mode 100644 index 8afcbd6..0000000 --- a/include/spdlog/details/registry.h +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// Loggers registry of unique name->logger pointer -// An attempt to create a logger with an already existing name will result with spdlog_ex exception. -// If user requests a non existing logger, nullptr will be returned -// This class is thread safe - -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace spdlog { -class logger; - -namespace details { -class thread_pool; - -class SPDLOG_API registry { -public: - using log_levels = std::unordered_map; - registry(const registry &) = delete; - registry &operator=(const registry &) = delete; - - void register_logger(std::shared_ptr new_logger); - void initialize_logger(std::shared_ptr new_logger); - std::shared_ptr get(const std::string &logger_name); - std::shared_ptr default_logger(); - - // Return raw ptr to the default logger. - // To be used directly by the spdlog default api (e.g. spdlog::info) - // This make the default API faster, but cannot be used concurrently with set_default_logger(). - // e.g do not call set_default_logger() from one thread while calling spdlog::info() from - // another. - logger *get_default_raw(); - - // set default logger and add it to the registry if not registered already. - // default logger is stored in default_logger_ (for faster retrieval) and in the loggers_ map. - // Note: Make sure to unregister it when no longer needed or before calling again with a new - // logger. - void set_default_logger(std::shared_ptr new_default_logger); - - void set_tp(std::shared_ptr tp); - - std::shared_ptr get_tp(); - - // Set global formatter. Each sink in each logger will get a clone of this object - void set_formatter(std::unique_ptr formatter); - - void enable_backtrace(size_t n_messages); - - void disable_backtrace(); - - void set_level(level::level_enum log_level); - - void flush_on(level::level_enum log_level); - - template - void flush_every(std::chrono::duration interval) { - std::lock_guard lock(flusher_mutex_); - auto clbk = [this]() { this->flush_all(); }; - periodic_flusher_ = details::make_unique(clbk, interval); - } - - std::unique_ptr &get_flusher() { - std::lock_guard lock(flusher_mutex_); - return periodic_flusher_; - } - - void set_error_handler(err_handler handler); - - void apply_all(const std::function)> &fun); - - void flush_all(); - - void drop(const std::string &logger_name); - - void drop_all(); - - // clean all resources and threads started by the registry - void shutdown(); - - std::recursive_mutex &tp_mutex(); - - void set_automatic_registration(bool automatic_registration); - - // set levels for all existing/future loggers. global_level can be null if should not set. - void set_levels(log_levels levels, level::level_enum *global_level); - - static registry &instance(); - - void apply_logger_env_levels(std::shared_ptr new_logger); - -private: - registry(); - ~registry(); - - void throw_if_exists_(const std::string &logger_name); - void register_logger_(std::shared_ptr new_logger); - bool set_level_from_cfg_(logger *logger); - std::mutex logger_map_mutex_, flusher_mutex_; - std::recursive_mutex tp_mutex_; - std::unordered_map> loggers_; - log_levels log_levels_; - std::unique_ptr formatter_; - spdlog::level::level_enum global_log_level_ = level::info; - level::level_enum flush_level_ = level::off; - err_handler err_handler_; - std::shared_ptr tp_; - std::unique_ptr periodic_flusher_; - std::shared_ptr default_logger_; - bool automatic_registration_ = true; - size_t backtrace_n_messages_ = 0; -}; - -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "registry-inl.h" -#endif diff --git a/include/spdlog/details/synchronous_factory.h b/include/spdlog/details/synchronous_factory.h deleted file mode 100644 index 4bd5a51..0000000 --- a/include/spdlog/details/synchronous_factory.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include "registry.h" - -namespace spdlog { - -// Default logger factory- creates synchronous loggers -class logger; - -struct synchronous_factory { - template - static std::shared_ptr create(std::string logger_name, SinkArgs &&...args) { - auto sink = std::make_shared(std::forward(args)...); - auto new_logger = std::make_shared(std::move(logger_name), std::move(sink)); - details::registry::instance().initialize_logger(new_logger); - return new_logger; - } -}; -} // namespace spdlog diff --git a/include/spdlog/details/tcp_client-windows.h b/include/spdlog/details/tcp_client-windows.h deleted file mode 100644 index bf8f7b8..0000000 --- a/include/spdlog/details/tcp_client-windows.h +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#define WIN32_LEAN_AND_MEAN -// tcp client helper -#include -#include - -#include -#include -#include -#include -#include -#include - -#pragma comment(lib, "Ws2_32.lib") -#pragma comment(lib, "Mswsock.lib") -#pragma comment(lib, "AdvApi32.lib") - -namespace spdlog { -namespace details { -class tcp_client { - SOCKET socket_ = INVALID_SOCKET; - - static void init_winsock_() { - WSADATA wsaData; - auto rv = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (rv != 0) { - throw_winsock_error_("WSAStartup failed", ::WSAGetLastError()); - } - } - - static void throw_winsock_error_(const std::string &msg, int last_error) { - char buf[512]; - ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, - (sizeof(buf) / sizeof(char)), NULL); - - throw_spdlog_ex(fmt_lib::format("tcp_sink - {}: {}", msg, buf)); - } - -public: - tcp_client() { init_winsock_(); } - - ~tcp_client() { - close(); - ::WSACleanup(); - } - - bool is_connected() const { return socket_ != INVALID_SOCKET; } - - void close() { - ::closesocket(socket_); - socket_ = INVALID_SOCKET; - } - - SOCKET fd() const { return socket_; } - - // try to connect or throw on failure - void connect(const std::string &host, int port) { - if (is_connected()) { - close(); - } - struct addrinfo hints {}; - ZeroMemory(&hints, sizeof(hints)); - - hints.ai_family = AF_UNSPEC; // To work with IPv4, IPv6, and so on - hints.ai_socktype = SOCK_STREAM; // TCP - hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value - hints.ai_protocol = 0; - - auto port_str = std::to_string(port); - struct addrinfo *addrinfo_result; - auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); - int last_error = 0; - if (rv != 0) { - last_error = ::WSAGetLastError(); - WSACleanup(); - throw_winsock_error_("getaddrinfo failed", last_error); - } - - // Try each address until we successfully connect(2). - - for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) { - socket_ = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (socket_ == INVALID_SOCKET) { - last_error = ::WSAGetLastError(); - WSACleanup(); - continue; - } - if (::connect(socket_, rp->ai_addr, (int)rp->ai_addrlen) == 0) { - break; - } else { - last_error = ::WSAGetLastError(); - close(); - } - } - ::freeaddrinfo(addrinfo_result); - if (socket_ == INVALID_SOCKET) { - WSACleanup(); - throw_winsock_error_("connect failed", last_error); - } - - // set TCP_NODELAY - int enable_flag = 1; - ::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&enable_flag), - sizeof(enable_flag)); - } - - // Send exactly n_bytes of the given data. - // On error close the connection and throw. - void send(const char *data, size_t n_bytes) { - size_t bytes_sent = 0; - while (bytes_sent < n_bytes) { - const int send_flags = 0; - auto write_result = - ::send(socket_, data + bytes_sent, (int)(n_bytes - bytes_sent), send_flags); - if (write_result == SOCKET_ERROR) { - int last_error = ::WSAGetLastError(); - close(); - throw_winsock_error_("send failed", last_error); - } - - if (write_result == 0) // (probably should not happen but in any case..) - { - break; - } - bytes_sent += static_cast(write_result); - } - } -}; -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/tcp_client.h b/include/spdlog/details/tcp_client.h deleted file mode 100644 index 9d3c40d..0000000 --- a/include/spdlog/details/tcp_client.h +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifdef _WIN32 - #error include tcp_client-windows.h instead -#endif - -// tcp client helper -#include -#include - -#include -#include -#include -#include -#include -#include - -#include - -namespace spdlog { -namespace details { -class tcp_client { - int socket_ = -1; - -public: - bool is_connected() const { return socket_ != -1; } - - void close() { - if (is_connected()) { - ::close(socket_); - socket_ = -1; - } - } - - int fd() const { return socket_; } - - ~tcp_client() { close(); } - - // try to connect or throw on failure - void connect(const std::string &host, int port) { - close(); - struct addrinfo hints {}; - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; // To work with IPv4, IPv6, and so on - hints.ai_socktype = SOCK_STREAM; // TCP - hints.ai_flags = AI_NUMERICSERV; // port passed as as numeric value - hints.ai_protocol = 0; - - auto port_str = std::to_string(port); - struct addrinfo *addrinfo_result; - auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result); - if (rv != 0) { - throw_spdlog_ex(fmt_lib::format("::getaddrinfo failed: {}", gai_strerror(rv))); - } - - // Try each address until we successfully connect(2). - int last_errno = 0; - for (auto *rp = addrinfo_result; rp != nullptr; rp = rp->ai_next) { -#if defined(SOCK_CLOEXEC) - const int flags = SOCK_CLOEXEC; -#else - const int flags = 0; -#endif - socket_ = ::socket(rp->ai_family, rp->ai_socktype | flags, rp->ai_protocol); - if (socket_ == -1) { - last_errno = errno; - continue; - } - rv = ::connect(socket_, rp->ai_addr, rp->ai_addrlen); - if (rv == 0) { - break; - } - last_errno = errno; - ::close(socket_); - socket_ = -1; - } - ::freeaddrinfo(addrinfo_result); - if (socket_ == -1) { - throw_spdlog_ex("::connect failed", last_errno); - } - - // set TCP_NODELAY - int enable_flag = 1; - ::setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&enable_flag), - sizeof(enable_flag)); - - // prevent sigpipe on systems where MSG_NOSIGNAL is not available -#if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) - ::setsockopt(socket_, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast(&enable_flag), - sizeof(enable_flag)); -#endif - -#if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL) - #error "tcp_sink would raise SIGPIPE since neither SO_NOSIGPIPE nor MSG_NOSIGNAL are available" -#endif - } - - // Send exactly n_bytes of the given data. - // On error close the connection and throw. - void send(const char *data, size_t n_bytes) { - size_t bytes_sent = 0; - while (bytes_sent < n_bytes) { -#if defined(MSG_NOSIGNAL) - const int send_flags = MSG_NOSIGNAL; -#else - const int send_flags = 0; -#endif - auto write_result = - ::send(socket_, data + bytes_sent, n_bytes - bytes_sent, send_flags); - if (write_result < 0) { - close(); - throw_spdlog_ex("write(2) failed", errno); - } - - if (write_result == 0) // (probably should not happen but in any case..) - { - break; - } - bytes_sent += static_cast(write_result); - } - } -}; -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/thread_pool-inl.h b/include/spdlog/details/thread_pool-inl.h deleted file mode 100644 index 17e01c0..0000000 --- a/include/spdlog/details/thread_pool-inl.h +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -namespace spdlog { -namespace details { - -SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, - size_t threads_n, - std::function on_thread_start, - std::function on_thread_stop) - : q_(q_max_items) { - if (threads_n == 0 || threads_n > 1000) { - throw_spdlog_ex( - "spdlog::thread_pool(): invalid threads_n param (valid " - "range is 1-1000)"); - } - for (size_t i = 0; i < threads_n; i++) { - threads_.emplace_back([this, on_thread_start, on_thread_stop] { - on_thread_start(); - this->thread_pool::worker_loop_(); - on_thread_stop(); - }); - } -} - -SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, - size_t threads_n, - std::function on_thread_start) - : thread_pool(q_max_items, threads_n, on_thread_start, [] {}) {} - -SPDLOG_INLINE thread_pool::thread_pool(size_t q_max_items, size_t threads_n) - : thread_pool( - q_max_items, threads_n, [] {}, [] {}) {} - -// message all threads to terminate gracefully join them -SPDLOG_INLINE thread_pool::~thread_pool() { - SPDLOG_TRY { - for (size_t i = 0; i < threads_.size(); i++) { - post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block); - } - - for (auto &t : threads_) { - t.join(); - } - } - SPDLOG_CATCH_STD -} - -void SPDLOG_INLINE thread_pool::post_log(async_logger_ptr &&worker_ptr, - const details::log_msg &msg, - async_overflow_policy overflow_policy) { - async_msg async_m(std::move(worker_ptr), async_msg_type::log, msg); - post_async_msg_(std::move(async_m), overflow_policy); -} - -void SPDLOG_INLINE thread_pool::post_flush(async_logger_ptr &&worker_ptr, - async_overflow_policy overflow_policy) { - post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); -} - -size_t SPDLOG_INLINE thread_pool::overrun_counter() { return q_.overrun_counter(); } - -void SPDLOG_INLINE thread_pool::reset_overrun_counter() { q_.reset_overrun_counter(); } - -size_t SPDLOG_INLINE thread_pool::discard_counter() { return q_.discard_counter(); } - -void SPDLOG_INLINE thread_pool::reset_discard_counter() { q_.reset_discard_counter(); } - -size_t SPDLOG_INLINE thread_pool::queue_size() { return q_.size(); } - -void SPDLOG_INLINE thread_pool::post_async_msg_(async_msg &&new_msg, - async_overflow_policy overflow_policy) { - if (overflow_policy == async_overflow_policy::block) { - q_.enqueue(std::move(new_msg)); - } else if (overflow_policy == async_overflow_policy::overrun_oldest) { - q_.enqueue_nowait(std::move(new_msg)); - } else { - assert(overflow_policy == async_overflow_policy::discard_new); - q_.enqueue_if_have_room(std::move(new_msg)); - } -} - -void SPDLOG_INLINE thread_pool::worker_loop_() { - while (process_next_msg_()) { - } -} - -// process next message in the queue -// return true if this thread should still be active (while no terminate msg -// was received) -bool SPDLOG_INLINE thread_pool::process_next_msg_() { - async_msg incoming_async_msg; - q_.dequeue(incoming_async_msg); - - switch (incoming_async_msg.msg_type) { - case async_msg_type::log: { - incoming_async_msg.worker_ptr->backend_sink_it_(incoming_async_msg); - return true; - } - case async_msg_type::flush: { - incoming_async_msg.worker_ptr->backend_flush_(); - return true; - } - - case async_msg_type::terminate: { - return false; - } - - default: { - assert(false); - } - } - - return true; -} - -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h deleted file mode 100644 index f22b078..0000000 --- a/include/spdlog/details/thread_pool.h +++ /dev/null @@ -1,117 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace spdlog { -class async_logger; - -namespace details { - -using async_logger_ptr = std::shared_ptr; - -enum class async_msg_type { log, flush, terminate }; - -// Async msg to move to/from the queue -// Movable only. should never be copied -struct async_msg : log_msg_buffer { - async_msg_type msg_type{async_msg_type::log}; - async_logger_ptr worker_ptr; - - async_msg() = default; - ~async_msg() = default; - - // should only be moved in or out of the queue.. - async_msg(const async_msg &) = delete; - -// support for vs2013 move -#if defined(_MSC_VER) && _MSC_VER <= 1800 - async_msg(async_msg &&other) - : log_msg_buffer(std::move(other)), - msg_type(other.msg_type), - worker_ptr(std::move(other.worker_ptr)) {} - - async_msg &operator=(async_msg &&other) { - *static_cast(this) = std::move(other); - msg_type = other.msg_type; - worker_ptr = std::move(other.worker_ptr); - return *this; - } -#else // (_MSC_VER) && _MSC_VER <= 1800 - async_msg(async_msg &&) = default; - async_msg &operator=(async_msg &&) = default; -#endif - - // construct from log_msg with given type - async_msg(async_logger_ptr &&worker, async_msg_type the_type, const details::log_msg &m) - : log_msg_buffer{m}, - msg_type{the_type}, - worker_ptr{std::move(worker)} {} - - async_msg(async_logger_ptr &&worker, async_msg_type the_type) - : log_msg_buffer{}, - msg_type{the_type}, - worker_ptr{std::move(worker)} {} - - explicit async_msg(async_msg_type the_type) - : async_msg{nullptr, the_type} {} -}; - -class SPDLOG_API thread_pool { -public: - using item_type = async_msg; - using q_type = details::mpmc_blocking_queue; - - thread_pool(size_t q_max_items, - size_t threads_n, - std::function on_thread_start, - std::function on_thread_stop); - thread_pool(size_t q_max_items, size_t threads_n, std::function on_thread_start); - thread_pool(size_t q_max_items, size_t threads_n); - - // message all threads to terminate gracefully and join them - ~thread_pool(); - - thread_pool(const thread_pool &) = delete; - thread_pool &operator=(thread_pool &&) = delete; - - void post_log(async_logger_ptr &&worker_ptr, - const details::log_msg &msg, - async_overflow_policy overflow_policy); - void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy); - size_t overrun_counter(); - void reset_overrun_counter(); - size_t discard_counter(); - void reset_discard_counter(); - size_t queue_size(); - -private: - q_type q_; - - std::vector threads_; - - void post_async_msg_(async_msg &&new_msg, async_overflow_policy overflow_policy); - void worker_loop_(); - - // process next message in the queue - // return true if this thread should still be active (while no terminate msg - // was received) - bool process_next_msg_(); -}; - -} // namespace details -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "thread_pool-inl.h" -#endif diff --git a/include/spdlog/details/udp_client-windows.h b/include/spdlog/details/udp_client-windows.h deleted file mode 100644 index 8b7c223..0000000 --- a/include/spdlog/details/udp_client-windows.h +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// Helper RAII over winsock udp client socket. -// Will throw on construction if socket creation failed. - -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(_MSC_VER) - #pragma comment(lib, "Ws2_32.lib") - #pragma comment(lib, "Mswsock.lib") - #pragma comment(lib, "AdvApi32.lib") -#endif - -namespace spdlog { -namespace details { -class udp_client { - static constexpr int TX_BUFFER_SIZE = 1024 * 10; - SOCKET socket_ = INVALID_SOCKET; - sockaddr_in addr_ = {}; - - static void init_winsock_() { - WSADATA wsaData; - auto rv = ::WSAStartup(MAKEWORD(2, 2), &wsaData); - if (rv != 0) { - throw_winsock_error_("WSAStartup failed", ::WSAGetLastError()); - } - } - - static void throw_winsock_error_(const std::string &msg, int last_error) { - char buf[512]; - ::FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, - (sizeof(buf) / sizeof(char)), NULL); - - throw_spdlog_ex(fmt_lib::format("udp_sink - {}: {}", msg, buf)); - } - - void cleanup_() { - if (socket_ != INVALID_SOCKET) { - ::closesocket(socket_); - } - socket_ = INVALID_SOCKET; - ::WSACleanup(); - } - -public: - udp_client(const std::string &host, uint16_t port) { - init_winsock_(); - - addr_.sin_family = PF_INET; - addr_.sin_port = htons(port); - addr_.sin_addr.s_addr = INADDR_ANY; - if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1) { - int last_error = ::WSAGetLastError(); - ::WSACleanup(); - throw_winsock_error_("error: Invalid address!", last_error); - } - - socket_ = ::socket(PF_INET, SOCK_DGRAM, 0); - if (socket_ == INVALID_SOCKET) { - int last_error = ::WSAGetLastError(); - ::WSACleanup(); - throw_winsock_error_("error: Create Socket failed", last_error); - } - - int option_value = TX_BUFFER_SIZE; - if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, - reinterpret_cast(&option_value), sizeof(option_value)) < 0) { - int last_error = ::WSAGetLastError(); - cleanup_(); - throw_winsock_error_("error: setsockopt(SO_SNDBUF) Failed!", last_error); - } - } - - ~udp_client() { cleanup_(); } - - SOCKET fd() const { return socket_; } - - void send(const char *data, size_t n_bytes) { - socklen_t tolen = sizeof(struct sockaddr); - if (::sendto(socket_, data, static_cast(n_bytes), 0, (struct sockaddr *)&addr_, - tolen) == -1) { - throw_spdlog_ex("sendto(2) failed", errno); - } - } -}; -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/udp_client.h b/include/spdlog/details/udp_client.h deleted file mode 100644 index 95826f5..0000000 --- a/include/spdlog/details/udp_client.h +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// Helper RAII over unix udp client socket. -// Will throw on construction if the socket creation failed. - -#ifdef _WIN32 - #error "include udp_client-windows.h instead" -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace spdlog { -namespace details { - -class udp_client { - static constexpr int TX_BUFFER_SIZE = 1024 * 10; - int socket_ = -1; - struct sockaddr_in sockAddr_; - - void cleanup_() { - if (socket_ != -1) { - ::close(socket_); - socket_ = -1; - } - } - -public: - udp_client(const std::string &host, uint16_t port) { - socket_ = ::socket(PF_INET, SOCK_DGRAM, 0); - if (socket_ < 0) { - throw_spdlog_ex("error: Create Socket Failed!"); - } - - int option_value = TX_BUFFER_SIZE; - if (::setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, - reinterpret_cast(&option_value), sizeof(option_value)) < 0) { - cleanup_(); - throw_spdlog_ex("error: setsockopt(SO_SNDBUF) Failed!"); - } - - sockAddr_.sin_family = AF_INET; - sockAddr_.sin_port = htons(port); - - if (::inet_aton(host.c_str(), &sockAddr_.sin_addr) == 0) { - cleanup_(); - throw_spdlog_ex("error: Invalid address!"); - } - - ::memset(sockAddr_.sin_zero, 0x00, sizeof(sockAddr_.sin_zero)); - } - - ~udp_client() { cleanup_(); } - - int fd() const { return socket_; } - - // Send exactly n_bytes of the given data. - // On error close the connection and throw. - void send(const char *data, size_t n_bytes) { - ssize_t toslen = 0; - socklen_t tolen = sizeof(struct sockaddr); - if ((toslen = ::sendto(socket_, data, n_bytes, 0, (struct sockaddr *)&sockAddr_, tolen)) == - -1) { - throw_spdlog_ex("sendto(2) failed", errno); - } - } -}; -} // namespace details -} // namespace spdlog diff --git a/include/spdlog/details/windows_include.h b/include/spdlog/details/windows_include.h deleted file mode 100644 index bbab59b..0000000 --- a/include/spdlog/details/windows_include.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#ifndef NOMINMAX - #define NOMINMAX // prevent windows redefining min/max -#endif - -#ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN -#endif - -#include diff --git a/include/spdlog/fmt/bin_to_hex.h b/include/spdlog/fmt/bin_to_hex.h deleted file mode 100644 index c2998d5..0000000 --- a/include/spdlog/fmt/bin_to_hex.h +++ /dev/null @@ -1,224 +0,0 @@ -// -// Copyright(c) 2015 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once - -#include -#include - -#if defined(__has_include) - #if __has_include() - #include - #endif -#endif - -#if __cpp_lib_span >= 202002L - #include -#endif - -// -// Support for logging binary data as hex -// format flags, any combination of the following: -// {:X} - print in uppercase. -// {:s} - don't separate each byte with space. -// {:p} - don't print the position on each line start. -// {:n} - don't split the output to lines. -// {:a} - show ASCII if :n is not set - -// -// Examples: -// -// std::vector v(200, 0x0b); -// logger->info("Some buffer {}", spdlog::to_hex(v)); -// char buf[128]; -// logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf))); -// logger->info("Some buffer {:X}", spdlog::to_hex(std::begin(buf), std::end(buf), 16)); - -namespace spdlog { -namespace details { - -template -class dump_info { -public: - dump_info(It range_begin, It range_end, size_t size_per_line) - : begin_(range_begin), - end_(range_end), - size_per_line_(size_per_line) {} - - // do not use begin() and end() to avoid collision with fmt/ranges - It get_begin() const { return begin_; } - It get_end() const { return end_; } - size_t size_per_line() const { return size_per_line_; } - -private: - It begin_, end_; - size_t size_per_line_; -}; -} // namespace details - -// create a dump_info that wraps the given container -template -inline details::dump_info to_hex(const Container &container, - size_t size_per_line = 32) { - static_assert(sizeof(typename Container::value_type) == 1, - "sizeof(Container::value_type) != 1"); - using Iter = typename Container::const_iterator; - return details::dump_info(std::begin(container), std::end(container), size_per_line); -} - -#if __cpp_lib_span >= 202002L - -template -inline details::dump_info::iterator> to_hex( - const std::span &container, size_t size_per_line = 32) { - using Container = std::span; - static_assert(sizeof(typename Container::value_type) == 1, - "sizeof(Container::value_type) != 1"); - using Iter = typename Container::iterator; - return details::dump_info(std::begin(container), std::end(container), size_per_line); -} - -#endif - -// create dump_info from ranges -template -inline details::dump_info to_hex(const It range_begin, - const It range_end, - size_t size_per_line = 32) { - return details::dump_info(range_begin, range_end, size_per_line); -} - -} // namespace spdlog - -namespace -#ifdef SPDLOG_USE_STD_FORMAT - std -#else - fmt -#endif -{ - -template -struct formatter, char> { - const char delimiter = ' '; - bool put_newlines = true; - bool put_delimiters = true; - bool use_uppercase = false; - bool put_positions = true; // position on start of each line - bool show_ascii = false; - - // parse the format string flags - template - SPDLOG_CONSTEXPR_FUNC auto parse(ParseContext &ctx) -> decltype(ctx.begin()) { - auto it = ctx.begin(); - while (it != ctx.end() && *it != '}') { - switch (*it) { - case 'X': - use_uppercase = true; - break; - case 's': - put_delimiters = false; - break; - case 'p': - put_positions = false; - break; - case 'n': - put_newlines = false; - show_ascii = false; - break; - case 'a': - if (put_newlines) { - show_ascii = true; - } - break; - } - - ++it; - } - return it; - } - - // format the given bytes range as hex - template - auto format(const spdlog::details::dump_info &the_range, FormatContext &ctx) const - -> decltype(ctx.out()) { - SPDLOG_CONSTEXPR const char *hex_upper = "0123456789ABCDEF"; - SPDLOG_CONSTEXPR const char *hex_lower = "0123456789abcdef"; - const char *hex_chars = use_uppercase ? hex_upper : hex_lower; - -#if !defined(SPDLOG_USE_STD_FORMAT) && FMT_VERSION < 60000 - auto inserter = ctx.begin(); -#else - auto inserter = ctx.out(); -#endif - - int size_per_line = static_cast(the_range.size_per_line()); - auto start_of_line = the_range.get_begin(); - for (auto i = the_range.get_begin(); i != the_range.get_end(); i++) { - auto ch = static_cast(*i); - - if (put_newlines && - (i == the_range.get_begin() || i - start_of_line >= size_per_line)) { - if (show_ascii && i != the_range.get_begin()) { - *inserter++ = delimiter; - *inserter++ = delimiter; - for (auto j = start_of_line; j < i; j++) { - auto pc = static_cast(*j); - *inserter++ = std::isprint(pc) ? static_cast(*j) : '.'; - } - } - - put_newline(inserter, static_cast(i - the_range.get_begin())); - - // put first byte without delimiter in front of it - *inserter++ = hex_chars[(ch >> 4) & 0x0f]; - *inserter++ = hex_chars[ch & 0x0f]; - start_of_line = i; - continue; - } - - if (put_delimiters && i != the_range.get_begin()) { - *inserter++ = delimiter; - } - - *inserter++ = hex_chars[(ch >> 4) & 0x0f]; - *inserter++ = hex_chars[ch & 0x0f]; - } - if (show_ascii) // add ascii to last line - { - if (the_range.get_end() - the_range.get_begin() > size_per_line) { - auto blank_num = size_per_line - (the_range.get_end() - start_of_line); - while (blank_num-- > 0) { - *inserter++ = delimiter; - *inserter++ = delimiter; - if (put_delimiters) { - *inserter++ = delimiter; - } - } - } - *inserter++ = delimiter; - *inserter++ = delimiter; - for (auto j = start_of_line; j != the_range.get_end(); j++) { - auto pc = static_cast(*j); - *inserter++ = std::isprint(pc) ? static_cast(*j) : '.'; - } - } - return inserter; - } - - // put newline(and position header) - template - void put_newline(It inserter, std::size_t pos) const { -#ifdef _WIN32 - *inserter++ = '\r'; -#endif - *inserter++ = '\n'; - - if (put_positions) { - spdlog::fmt_lib::format_to(inserter, SPDLOG_FMT_STRING("{:04X}: "), pos); - } - } -}; -} // namespace std diff --git a/include/spdlog/fmt/bundled/args.h b/include/spdlog/fmt/bundled/args.h deleted file mode 100644 index 31a60e8..0000000 --- a/include/spdlog/fmt/bundled/args.h +++ /dev/null @@ -1,228 +0,0 @@ -// Formatting library for C++ - dynamic argument lists -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_ARGS_H_ -#define FMT_ARGS_H_ - -#ifndef FMT_MODULE -# include // std::reference_wrapper -# include // std::unique_ptr -# include -#endif - -#include "format.h" // std_string_view - -FMT_BEGIN_NAMESPACE - -namespace detail { - -template struct is_reference_wrapper : std::false_type {}; -template -struct is_reference_wrapper> : std::true_type {}; - -template auto unwrap(const T& v) -> const T& { return v; } -template -auto unwrap(const std::reference_wrapper& v) -> const T& { - return static_cast(v); -} - -// node is defined outside dynamic_arg_list to workaround a C2504 bug in MSVC -// 2022 (v17.10.0). -// -// Workaround for clang's -Wweak-vtables. Unlike for regular classes, for -// templates it doesn't complain about inability to deduce single translation -// unit for placing vtable. So node is made a fake template. -template struct node { - virtual ~node() = default; - std::unique_ptr> next; -}; - -class dynamic_arg_list { - template struct typed_node : node<> { - T value; - - template - FMT_CONSTEXPR typed_node(const Arg& arg) : value(arg) {} - - template - FMT_CONSTEXPR typed_node(const basic_string_view& arg) - : value(arg.data(), arg.size()) {} - }; - - std::unique_ptr> head_; - - public: - template auto push(const Arg& arg) -> const T& { - auto new_node = std::unique_ptr>(new typed_node(arg)); - auto& value = new_node->value; - new_node->next = std::move(head_); - head_ = std::move(new_node); - return value; - } -}; -} // namespace detail - -/** - * A dynamic list of formatting arguments with storage. - * - * It can be implicitly converted into `fmt::basic_format_args` for passing - * into type-erased formatting functions such as `fmt::vformat`. - */ -template -class dynamic_format_arg_store -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 - // Workaround a GCC template argument substitution bug. - : public basic_format_args -#endif -{ - private: - using char_type = typename Context::char_type; - - template struct need_copy { - static constexpr detail::type mapped_type = - detail::mapped_type_constant::value; - - enum { - value = !(detail::is_reference_wrapper::value || - std::is_same>::value || - std::is_same>::value || - (mapped_type != detail::type::cstring_type && - mapped_type != detail::type::string_type && - mapped_type != detail::type::custom_type)) - }; - }; - - template - using stored_type = conditional_t< - std::is_convertible>::value && - !detail::is_reference_wrapper::value, - std::basic_string, T>; - - // Storage of basic_format_arg must be contiguous. - std::vector> data_; - std::vector> named_info_; - - // Storage of arguments not fitting into basic_format_arg must grow - // without relocation because items in data_ refer to it. - detail::dynamic_arg_list dynamic_args_; - - friend class basic_format_args; - - auto get_types() const -> unsigned long long { - return detail::is_unpacked_bit | data_.size() | - (named_info_.empty() - ? 0ULL - : static_cast(detail::has_named_args_bit)); - } - - auto data() const -> const basic_format_arg* { - return named_info_.empty() ? data_.data() : data_.data() + 1; - } - - template void emplace_arg(const T& arg) { - data_.emplace_back(detail::make_arg(arg)); - } - - template - void emplace_arg(const detail::named_arg& arg) { - if (named_info_.empty()) { - constexpr const detail::named_arg_info* zero_ptr{nullptr}; - data_.insert(data_.begin(), {zero_ptr, 0}); - } - data_.emplace_back(detail::make_arg(detail::unwrap(arg.value))); - auto pop_one = [](std::vector>* data) { - data->pop_back(); - }; - std::unique_ptr>, decltype(pop_one)> - guard{&data_, pop_one}; - named_info_.push_back({arg.name, static_cast(data_.size() - 2u)}); - data_[0].value_.named_args = {named_info_.data(), named_info_.size()}; - guard.release(); - } - - public: - constexpr dynamic_format_arg_store() = default; - - /** - * Adds an argument into the dynamic store for later passing to a formatting - * function. - * - * Note that custom types and string types (but not string views) are copied - * into the store dynamically allocating memory if necessary. - * - * **Example**: - * - * fmt::dynamic_format_arg_store store; - * store.push_back(42); - * store.push_back("abc"); - * store.push_back(1.5f); - * std::string result = fmt::vformat("{} and {} and {}", store); - */ - template void push_back(const T& arg) { - if (detail::const_check(need_copy::value)) - emplace_arg(dynamic_args_.push>(arg)); - else - emplace_arg(detail::unwrap(arg)); - } - - /** - * Adds a reference to the argument into the dynamic store for later passing - * to a formatting function. - * - * **Example**: - * - * fmt::dynamic_format_arg_store store; - * char band[] = "Rolling Stones"; - * store.push_back(std::cref(band)); - * band[9] = 'c'; // Changing str affects the output. - * std::string result = fmt::vformat("{}", store); - * // result == "Rolling Scones" - */ - template void push_back(std::reference_wrapper arg) { - static_assert( - need_copy::value, - "objects of built-in types and string views are always copied"); - emplace_arg(arg.get()); - } - - /** - * Adds named argument into the dynamic store for later passing to a - * formatting function. `std::reference_wrapper` is supported to avoid - * copying of the argument. The name is always copied into the store. - */ - template - void push_back(const detail::named_arg& arg) { - const char_type* arg_name = - dynamic_args_.push>(arg.name).c_str(); - if (detail::const_check(need_copy::value)) { - emplace_arg( - fmt::arg(arg_name, dynamic_args_.push>(arg.value))); - } else { - emplace_arg(fmt::arg(arg_name, arg.value)); - } - } - - /// Erase all elements from the store. - void clear() { - data_.clear(); - named_info_.clear(); - dynamic_args_ = detail::dynamic_arg_list(); - } - - /// Reserves space to store at least `new_cap` arguments including - /// `new_cap_named` named arguments. - void reserve(size_t new_cap, size_t new_cap_named) { - FMT_ASSERT(new_cap >= new_cap_named, - "Set of arguments includes set of named arguments"); - data_.reserve(new_cap); - named_info_.reserve(new_cap_named); - } -}; - -FMT_END_NAMESPACE - -#endif // FMT_ARGS_H_ diff --git a/include/spdlog/fmt/bundled/base.h b/include/spdlog/fmt/bundled/base.h deleted file mode 100644 index 6276494..0000000 --- a/include/spdlog/fmt/bundled/base.h +++ /dev/null @@ -1,3077 +0,0 @@ -// Formatting library for C++ - the base API for char/UTF-8 -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_BASE_H_ -#define FMT_BASE_H_ - -#if defined(FMT_IMPORT_STD) && !defined(FMT_MODULE) -# define FMT_MODULE -#endif - -#ifndef FMT_MODULE -# include // CHAR_BIT -# include // FILE -# include // strlen - -// is also included transitively from . -# include // std::byte -# include // std::enable_if -#endif - -// The fmt library version in the form major * 10000 + minor * 100 + patch. -#define FMT_VERSION 110002 - -// Detect compiler versions. -#if defined(__clang__) && !defined(__ibmxl__) -# define FMT_CLANG_VERSION (__clang_major__ * 100 + __clang_minor__) -#else -# define FMT_CLANG_VERSION 0 -#endif -#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER) -# define FMT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) -#else -# define FMT_GCC_VERSION 0 -#endif -#if defined(__ICL) -# define FMT_ICC_VERSION __ICL -#elif defined(__INTEL_COMPILER) -# define FMT_ICC_VERSION __INTEL_COMPILER -#else -# define FMT_ICC_VERSION 0 -#endif -#if defined(_MSC_VER) -# define FMT_MSC_VERSION _MSC_VER -#else -# define FMT_MSC_VERSION 0 -#endif - -// Detect standard library versions. -#ifdef _GLIBCXX_RELEASE -# define FMT_GLIBCXX_RELEASE _GLIBCXX_RELEASE -#else -# define FMT_GLIBCXX_RELEASE 0 -#endif -#ifdef _LIBCPP_VERSION -# define FMT_LIBCPP_VERSION _LIBCPP_VERSION -#else -# define FMT_LIBCPP_VERSION 0 -#endif - -#ifdef _MSVC_LANG -# define FMT_CPLUSPLUS _MSVC_LANG -#else -# define FMT_CPLUSPLUS __cplusplus -#endif - -// Detect __has_*. -#ifdef __has_feature -# define FMT_HAS_FEATURE(x) __has_feature(x) -#else -# define FMT_HAS_FEATURE(x) 0 -#endif -#ifdef __has_include -# define FMT_HAS_INCLUDE(x) __has_include(x) -#else -# define FMT_HAS_INCLUDE(x) 0 -#endif -#ifdef __has_cpp_attribute -# define FMT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) -#else -# define FMT_HAS_CPP_ATTRIBUTE(x) 0 -#endif - -#define FMT_HAS_CPP14_ATTRIBUTE(attribute) \ - (FMT_CPLUSPLUS >= 201402L && FMT_HAS_CPP_ATTRIBUTE(attribute)) - -#define FMT_HAS_CPP17_ATTRIBUTE(attribute) \ - (FMT_CPLUSPLUS >= 201703L && FMT_HAS_CPP_ATTRIBUTE(attribute)) - -// Detect C++14 relaxed constexpr. -#ifdef FMT_USE_CONSTEXPR -// Use the provided definition. -#elif FMT_GCC_VERSION >= 600 && FMT_CPLUSPLUS >= 201402L -// GCC only allows throw in constexpr since version 6: -// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67371. -# define FMT_USE_CONSTEXPR 1 -#elif FMT_ICC_VERSION -# define FMT_USE_CONSTEXPR 0 // https://github.com/fmtlib/fmt/issues/1628 -#elif FMT_HAS_FEATURE(cxx_relaxed_constexpr) || FMT_MSC_VERSION >= 1912 -# define FMT_USE_CONSTEXPR 1 -#else -# define FMT_USE_CONSTEXPR 0 -#endif -#if FMT_USE_CONSTEXPR -# define FMT_CONSTEXPR constexpr -#else -# define FMT_CONSTEXPR -#endif - -// Detect consteval, C++20 constexpr extensions and std::is_constant_evaluated. -#if !defined(__cpp_lib_is_constant_evaluated) -# define FMT_USE_CONSTEVAL 0 -#elif FMT_CPLUSPLUS < 201709L -# define FMT_USE_CONSTEVAL 0 -#elif FMT_GLIBCXX_RELEASE && FMT_GLIBCXX_RELEASE < 10 -# define FMT_USE_CONSTEVAL 0 -#elif FMT_LIBCPP_VERSION && FMT_LIBCPP_VERSION < 10000 -# define FMT_USE_CONSTEVAL 0 -#elif defined(__apple_build_version__) && __apple_build_version__ < 14000029L -# define FMT_USE_CONSTEVAL 0 // consteval is broken in Apple clang < 14. -#elif FMT_MSC_VERSION && FMT_MSC_VERSION < 1929 -# define FMT_USE_CONSTEVAL 0 // consteval is broken in MSVC VS2019 < 16.10. -#elif defined(__cpp_consteval) -# define FMT_USE_CONSTEVAL 1 -#elif FMT_GCC_VERSION >= 1002 || FMT_CLANG_VERSION >= 1101 -# define FMT_USE_CONSTEVAL 1 -#else -# define FMT_USE_CONSTEVAL 0 -#endif -#if FMT_USE_CONSTEVAL -# define FMT_CONSTEVAL consteval -# define FMT_CONSTEXPR20 constexpr -#else -# define FMT_CONSTEVAL -# define FMT_CONSTEXPR20 -#endif - -#if defined(FMT_USE_NONTYPE_TEMPLATE_ARGS) -// Use the provided definition. -#elif defined(__NVCOMPILER) -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 -#elif FMT_GCC_VERSION >= 903 && FMT_CPLUSPLUS >= 201709L -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 -#elif defined(__cpp_nontype_template_args) && \ - __cpp_nontype_template_args >= 201911L -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 -#elif FMT_CLANG_VERSION >= 1200 && FMT_CPLUSPLUS >= 202002L -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 1 -#else -# define FMT_USE_NONTYPE_TEMPLATE_ARGS 0 -#endif - -#ifdef FMT_USE_CONCEPTS -// Use the provided definition. -#elif defined(__cpp_concepts) -# define FMT_USE_CONCEPTS 1 -#else -# define FMT_USE_CONCEPTS 0 -#endif - -// Check if exceptions are disabled. -#ifdef FMT_EXCEPTIONS -// Use the provided definition. -#elif defined(__GNUC__) && !defined(__EXCEPTIONS) -# define FMT_EXCEPTIONS 0 -#elif FMT_MSC_VERSION && !_HAS_EXCEPTIONS -# define FMT_EXCEPTIONS 0 -#else -# define FMT_EXCEPTIONS 1 -#endif -#if FMT_EXCEPTIONS -# define FMT_TRY try -# define FMT_CATCH(x) catch (x) -#else -# define FMT_TRY if (true) -# define FMT_CATCH(x) if (false) -#endif - -#if FMT_HAS_CPP17_ATTRIBUTE(fallthrough) -# define FMT_FALLTHROUGH [[fallthrough]] -#elif defined(__clang__) -# define FMT_FALLTHROUGH [[clang::fallthrough]] -#elif FMT_GCC_VERSION >= 700 && \ - (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 520) -# define FMT_FALLTHROUGH [[gnu::fallthrough]] -#else -# define FMT_FALLTHROUGH -#endif - -// Disable [[noreturn]] on MSVC/NVCC because of bogus unreachable code warnings. -#if FMT_HAS_CPP_ATTRIBUTE(noreturn) && !FMT_MSC_VERSION && !defined(__NVCC__) -# define FMT_NORETURN [[noreturn]] -#else -# define FMT_NORETURN -#endif - -#ifndef FMT_NODISCARD -# if FMT_HAS_CPP17_ATTRIBUTE(nodiscard) -# define FMT_NODISCARD [[nodiscard]] -# else -# define FMT_NODISCARD -# endif -#endif - -#ifdef FMT_DEPRECATED -// Use the provided definition. -#elif FMT_HAS_CPP14_ATTRIBUTE(deprecated) -# define FMT_DEPRECATED [[deprecated]] -#else -# define FMT_DEPRECATED /* deprecated */ -#endif - -#ifdef FMT_INLINE -// Use the provided definition. -#elif FMT_GCC_VERSION || FMT_CLANG_VERSION -# define FMT_ALWAYS_INLINE inline __attribute__((always_inline)) -#else -# define FMT_ALWAYS_INLINE inline -#endif -// A version of FMT_INLINE to prevent code bloat in debug mode. -#ifdef NDEBUG -# define FMT_INLINE FMT_ALWAYS_INLINE -#else -# define FMT_INLINE inline -#endif - -#if FMT_GCC_VERSION || FMT_CLANG_VERSION -# define FMT_VISIBILITY(value) __attribute__((visibility(value))) -#else -# define FMT_VISIBILITY(value) -#endif - -#ifndef FMT_GCC_PRAGMA -// Workaround a _Pragma bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59884 -// and an nvhpc warning: https://github.com/fmtlib/fmt/pull/2582. -# if FMT_GCC_VERSION >= 504 && !defined(__NVCOMPILER) -# define FMT_GCC_PRAGMA(arg) _Pragma(arg) -# else -# define FMT_GCC_PRAGMA(arg) -# endif -#endif - -// GCC < 5 requires this-> in decltype. -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 500 -# define FMT_DECLTYPE_THIS this-> -#else -# define FMT_DECLTYPE_THIS -#endif - -#if FMT_MSC_VERSION -# define FMT_MSC_WARNING(...) __pragma(warning(__VA_ARGS__)) -# define FMT_UNCHECKED_ITERATOR(It) \ - using _Unchecked_type = It // Mark iterator as checked. -#else -# define FMT_MSC_WARNING(...) -# define FMT_UNCHECKED_ITERATOR(It) using unchecked_type = It -#endif - -#ifndef FMT_BEGIN_NAMESPACE -# define FMT_BEGIN_NAMESPACE \ - namespace fmt { \ - inline namespace v11 { -# define FMT_END_NAMESPACE \ - } \ - } -#endif - -#ifndef FMT_EXPORT -# define FMT_EXPORT -# define FMT_BEGIN_EXPORT -# define FMT_END_EXPORT -#endif - -#if !defined(FMT_HEADER_ONLY) && defined(_WIN32) -# if defined(FMT_LIB_EXPORT) -# define FMT_API __declspec(dllexport) -# elif defined(FMT_SHARED) -# define FMT_API __declspec(dllimport) -# endif -#elif defined(FMT_LIB_EXPORT) || defined(FMT_SHARED) -# define FMT_API FMT_VISIBILITY("default") -#endif -#ifndef FMT_API -# define FMT_API -#endif - -#ifndef FMT_UNICODE -# define FMT_UNICODE 1 -#endif - -// Check if rtti is available. -#ifndef FMT_USE_RTTI -// __RTTI is for EDG compilers. _CPPRTTI is for MSVC. -# if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \ - defined(__INTEL_RTTI__) || defined(__RTTI) -# define FMT_USE_RTTI 1 -# else -# define FMT_USE_RTTI 0 -# endif -#endif - -#define FMT_FWD(...) static_cast(__VA_ARGS__) - -// Enable minimal optimizations for more compact code in debug mode. -FMT_GCC_PRAGMA("GCC push_options") -#if !defined(__OPTIMIZE__) && !defined(__CUDACC__) -FMT_GCC_PRAGMA("GCC optimize(\"Og\")") -#endif - -FMT_BEGIN_NAMESPACE - -// Implementations of enable_if_t and other metafunctions for older systems. -template -using enable_if_t = typename std::enable_if::type; -template -using conditional_t = typename std::conditional::type; -template using bool_constant = std::integral_constant; -template -using remove_reference_t = typename std::remove_reference::type; -template -using remove_const_t = typename std::remove_const::type; -template -using remove_cvref_t = typename std::remove_cv>::type; -template struct type_identity { - using type = T; -}; -template using type_identity_t = typename type_identity::type; -template -using make_unsigned_t = typename std::make_unsigned::type; -template -using underlying_t = typename std::underlying_type::type; - -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 500 -// A workaround for gcc 4.8 to make void_t work in a SFINAE context. -template struct void_t_impl { - using type = void; -}; -template using void_t = typename void_t_impl::type; -#else -template using void_t = void; -#endif - -struct monostate { - constexpr monostate() {} -}; - -// An enable_if helper to be used in template parameters which results in much -// shorter symbols: https://godbolt.org/z/sWw4vP. Extra parentheses are needed -// to workaround a bug in MSVC 2019 (see #1140 and #1186). -#ifdef FMT_DOC -# define FMT_ENABLE_IF(...) -#else -# define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0 -#endif - -// This is defined in base.h instead of format.h to avoid injecting in std. -// It is a template to avoid undesirable implicit conversions to std::byte. -#ifdef __cpp_lib_byte -template ::value)> -inline auto format_as(T b) -> unsigned char { - return static_cast(b); -} -#endif - -namespace detail { -// Suppresses "unused variable" warnings with the method described in -// https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/. -// (void)var does not work on many Intel compilers. -template FMT_CONSTEXPR void ignore_unused(const T&...) {} - -constexpr auto is_constant_evaluated(bool default_value = false) noexcept - -> bool { -// Workaround for incompatibility between libstdc++ consteval-based -// std::is_constant_evaluated() implementation and clang-14: -// https://github.com/fmtlib/fmt/issues/3247. -#if FMT_CPLUSPLUS >= 202002L && FMT_GLIBCXX_RELEASE >= 12 && \ - (FMT_CLANG_VERSION >= 1400 && FMT_CLANG_VERSION < 1500) - ignore_unused(default_value); - return __builtin_is_constant_evaluated(); -#elif defined(__cpp_lib_is_constant_evaluated) - ignore_unused(default_value); - return std::is_constant_evaluated(); -#else - return default_value; -#endif -} - -// Suppresses "conditional expression is constant" warnings. -template constexpr auto const_check(T value) -> T { return value; } - -FMT_NORETURN FMT_API void assert_fail(const char* file, int line, - const char* message); - -#if defined(FMT_ASSERT) -// Use the provided definition. -#elif defined(NDEBUG) -// FMT_ASSERT is not empty to avoid -Wempty-body. -# define FMT_ASSERT(condition, message) \ - fmt::detail::ignore_unused((condition), (message)) -#else -# define FMT_ASSERT(condition, message) \ - ((condition) /* void() fails with -Winvalid-constexpr on clang 4.0.1 */ \ - ? (void)0 \ - : fmt::detail::assert_fail(__FILE__, __LINE__, (message))) -#endif - -#ifdef FMT_USE_INT128 -// Do nothing. -#elif defined(__SIZEOF_INT128__) && !defined(__NVCC__) && \ - !(FMT_CLANG_VERSION && FMT_MSC_VERSION) -# define FMT_USE_INT128 1 -using int128_opt = __int128_t; // An optional native 128-bit integer. -using uint128_opt = __uint128_t; -template inline auto convert_for_visit(T value) -> T { - return value; -} -#else -# define FMT_USE_INT128 0 -#endif -#if !FMT_USE_INT128 -enum class int128_opt {}; -enum class uint128_opt {}; -// Reduce template instantiations. -template auto convert_for_visit(T) -> monostate { return {}; } -#endif - -// Casts a nonnegative integer to unsigned. -template -FMT_CONSTEXPR auto to_unsigned(Int value) -> make_unsigned_t { - FMT_ASSERT(std::is_unsigned::value || value >= 0, "negative value"); - return static_cast>(value); -} - -// A heuristic to detect std::string and std::[experimental::]string_view. -// It is mainly used to avoid dependency on <[experimental/]string_view>. -template -struct is_std_string_like : std::false_type {}; -template -struct is_std_string_like().find_first_of( - typename T::value_type(), 0))>> - : std::is_convertible().data()), - const typename T::value_type*> {}; - -// Returns true iff the literal encoding is UTF-8. -constexpr auto is_utf8_enabled() -> bool { - // Avoid an MSVC sign extension bug: https://github.com/fmtlib/fmt/pull/2297. - using uchar = unsigned char; - return sizeof("\u00A7") == 3 && uchar("\u00A7"[0]) == 0xC2 && - uchar("\u00A7"[1]) == 0xA7; -} -constexpr auto use_utf8() -> bool { - return !FMT_MSC_VERSION || is_utf8_enabled(); -} - -static_assert(!FMT_UNICODE || use_utf8(), - "Unicode support requires compiling with /utf-8"); - -template FMT_CONSTEXPR auto length(const Char* s) -> size_t { - size_t len = 0; - while (*s++) ++len; - return len; -} - -template -FMT_CONSTEXPR auto compare(const Char* s1, const Char* s2, std::size_t n) - -> int { - if (!is_constant_evaluated() && sizeof(Char) == 1) return memcmp(s1, s2, n); - for (; n != 0; ++s1, ++s2, --n) { - if (*s1 < *s2) return -1; - if (*s1 > *s2) return 1; - } - return 0; -} - -namespace adl { -using namespace std; - -template -auto invoke_back_inserter() - -> decltype(back_inserter(std::declval())); -} // namespace adl - -template -struct is_back_insert_iterator : std::false_type {}; - -template -struct is_back_insert_iterator< - It, bool_constant()), - It>::value>> : std::true_type {}; - -// Extracts a reference to the container from *insert_iterator. -template -inline auto get_container(OutputIt it) -> typename OutputIt::container_type& { - struct accessor : OutputIt { - accessor(OutputIt base) : OutputIt(base) {} - using OutputIt::container; - }; - return *accessor(it).container; -} -} // namespace detail - -// Checks whether T is a container with contiguous storage. -template struct is_contiguous : std::false_type {}; - -/** - * An implementation of `std::basic_string_view` for pre-C++17. It provides a - * subset of the API. `fmt::basic_string_view` is used for format strings even - * if `std::basic_string_view` is available to prevent issues when a library is - * compiled with a different `-std` option than the client code (which is not - * recommended). - */ -FMT_EXPORT -template class basic_string_view { - private: - const Char* data_; - size_t size_; - - public: - using value_type = Char; - using iterator = const Char*; - - constexpr basic_string_view() noexcept : data_(nullptr), size_(0) {} - - /// Constructs a string reference object from a C string and a size. - constexpr basic_string_view(const Char* s, size_t count) noexcept - : data_(s), size_(count) {} - - constexpr basic_string_view(std::nullptr_t) = delete; - - /// Constructs a string reference object from a C string. - FMT_CONSTEXPR20 - basic_string_view(const Char* s) - : data_(s), - size_(detail::const_check(std::is_same::value && - !detail::is_constant_evaluated(false)) - ? strlen(reinterpret_cast(s)) - : detail::length(s)) {} - - /// Constructs a string reference from a `std::basic_string` or a - /// `std::basic_string_view` object. - template ::value&& std::is_same< - typename S::value_type, Char>::value)> - FMT_CONSTEXPR basic_string_view(const S& s) noexcept - : data_(s.data()), size_(s.size()) {} - - /// Returns a pointer to the string data. - constexpr auto data() const noexcept -> const Char* { return data_; } - - /// Returns the string size. - constexpr auto size() const noexcept -> size_t { return size_; } - - constexpr auto begin() const noexcept -> iterator { return data_; } - constexpr auto end() const noexcept -> iterator { return data_ + size_; } - - constexpr auto operator[](size_t pos) const noexcept -> const Char& { - return data_[pos]; - } - - FMT_CONSTEXPR void remove_prefix(size_t n) noexcept { - data_ += n; - size_ -= n; - } - - FMT_CONSTEXPR auto starts_with(basic_string_view sv) const noexcept - -> bool { - return size_ >= sv.size_ && detail::compare(data_, sv.data_, sv.size_) == 0; - } - FMT_CONSTEXPR auto starts_with(Char c) const noexcept -> bool { - return size_ >= 1 && *data_ == c; - } - FMT_CONSTEXPR auto starts_with(const Char* s) const -> bool { - return starts_with(basic_string_view(s)); - } - - // Lexicographically compare this string reference to other. - FMT_CONSTEXPR auto compare(basic_string_view other) const -> int { - size_t str_size = size_ < other.size_ ? size_ : other.size_; - int result = detail::compare(data_, other.data_, str_size); - if (result == 0) - result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1); - return result; - } - - FMT_CONSTEXPR friend auto operator==(basic_string_view lhs, - basic_string_view rhs) -> bool { - return lhs.compare(rhs) == 0; - } - friend auto operator!=(basic_string_view lhs, basic_string_view rhs) -> bool { - return lhs.compare(rhs) != 0; - } - friend auto operator<(basic_string_view lhs, basic_string_view rhs) -> bool { - return lhs.compare(rhs) < 0; - } - friend auto operator<=(basic_string_view lhs, basic_string_view rhs) -> bool { - return lhs.compare(rhs) <= 0; - } - friend auto operator>(basic_string_view lhs, basic_string_view rhs) -> bool { - return lhs.compare(rhs) > 0; - } - friend auto operator>=(basic_string_view lhs, basic_string_view rhs) -> bool { - return lhs.compare(rhs) >= 0; - } -}; - -FMT_EXPORT -using string_view = basic_string_view; - -/// Specifies if `T` is a character type. Can be specialized by users. -FMT_EXPORT -template struct is_char : std::false_type {}; -template <> struct is_char : std::true_type {}; - -namespace detail { - -// Constructs fmt::basic_string_view from types implicitly convertible -// to it, deducing Char. Explicitly convertible types such as the ones returned -// from FMT_STRING are intentionally excluded. -template ::value)> -constexpr auto to_string_view(const Char* s) -> basic_string_view { - return s; -} -template ::value)> -constexpr auto to_string_view(const T& s) - -> basic_string_view { - return s; -} -template -constexpr auto to_string_view(basic_string_view s) - -> basic_string_view { - return s; -} - -template -struct has_to_string_view : std::false_type {}; -// detail:: is intentional since to_string_view is not an extension point. -template -struct has_to_string_view< - T, void_t()))>> - : std::true_type {}; - -template struct string_literal { - static constexpr Char value[sizeof...(C)] = {C...}; - constexpr operator basic_string_view() const { - return {value, sizeof...(C)}; - } -}; -#if FMT_CPLUSPLUS < 201703L -template -constexpr Char string_literal::value[sizeof...(C)]; -#endif - -enum class type { - none_type, - // Integer types should go first, - int_type, - uint_type, - long_long_type, - ulong_long_type, - int128_type, - uint128_type, - bool_type, - char_type, - last_integer_type = char_type, - // followed by floating-point types. - float_type, - double_type, - long_double_type, - last_numeric_type = long_double_type, - cstring_type, - string_type, - pointer_type, - custom_type -}; - -// Maps core type T to the corresponding type enum constant. -template -struct type_constant : std::integral_constant {}; - -#define FMT_TYPE_CONSTANT(Type, constant) \ - template \ - struct type_constant \ - : std::integral_constant {} - -FMT_TYPE_CONSTANT(int, int_type); -FMT_TYPE_CONSTANT(unsigned, uint_type); -FMT_TYPE_CONSTANT(long long, long_long_type); -FMT_TYPE_CONSTANT(unsigned long long, ulong_long_type); -FMT_TYPE_CONSTANT(int128_opt, int128_type); -FMT_TYPE_CONSTANT(uint128_opt, uint128_type); -FMT_TYPE_CONSTANT(bool, bool_type); -FMT_TYPE_CONSTANT(Char, char_type); -FMT_TYPE_CONSTANT(float, float_type); -FMT_TYPE_CONSTANT(double, double_type); -FMT_TYPE_CONSTANT(long double, long_double_type); -FMT_TYPE_CONSTANT(const Char*, cstring_type); -FMT_TYPE_CONSTANT(basic_string_view, string_type); -FMT_TYPE_CONSTANT(const void*, pointer_type); - -constexpr auto is_integral_type(type t) -> bool { - return t > type::none_type && t <= type::last_integer_type; -} -constexpr auto is_arithmetic_type(type t) -> bool { - return t > type::none_type && t <= type::last_numeric_type; -} - -constexpr auto set(type rhs) -> int { return 1 << static_cast(rhs); } -constexpr auto in(type t, int set) -> bool { - return ((set >> static_cast(t)) & 1) != 0; -} - -// Bitsets of types. -enum { - sint_set = - set(type::int_type) | set(type::long_long_type) | set(type::int128_type), - uint_set = set(type::uint_type) | set(type::ulong_long_type) | - set(type::uint128_type), - bool_set = set(type::bool_type), - char_set = set(type::char_type), - float_set = set(type::float_type) | set(type::double_type) | - set(type::long_double_type), - string_set = set(type::string_type), - cstring_set = set(type::cstring_type), - pointer_set = set(type::pointer_type) -}; -} // namespace detail - -/// Reports a format error at compile time or, via a `format_error` exception, -/// at runtime. -// This function is intentionally not constexpr to give a compile-time error. -FMT_NORETURN FMT_API void report_error(const char* message); - -FMT_DEPRECATED FMT_NORETURN inline void throw_format_error( - const char* message) { - report_error(message); -} - -/// String's character (code unit) type. -template ()))> -using char_t = typename V::value_type; - -/** - * Parsing context consisting of a format string range being parsed and an - * argument counter for automatic indexing. - * You can use the `format_parse_context` type alias for `char` instead. - */ -FMT_EXPORT -template class basic_format_parse_context { - private: - basic_string_view format_str_; - int next_arg_id_; - - FMT_CONSTEXPR void do_check_arg_id(int id); - - public: - using char_type = Char; - using iterator = const Char*; - - explicit constexpr basic_format_parse_context( - basic_string_view format_str, int next_arg_id = 0) - : format_str_(format_str), next_arg_id_(next_arg_id) {} - - /// Returns an iterator to the beginning of the format string range being - /// parsed. - constexpr auto begin() const noexcept -> iterator { - return format_str_.begin(); - } - - /// Returns an iterator past the end of the format string range being parsed. - constexpr auto end() const noexcept -> iterator { return format_str_.end(); } - - /// Advances the begin iterator to `it`. - FMT_CONSTEXPR void advance_to(iterator it) { - format_str_.remove_prefix(detail::to_unsigned(it - begin())); - } - - /// Reports an error if using the manual argument indexing; otherwise returns - /// the next argument index and switches to the automatic indexing. - FMT_CONSTEXPR auto next_arg_id() -> int { - if (next_arg_id_ < 0) { - report_error("cannot switch from manual to automatic argument indexing"); - return 0; - } - int id = next_arg_id_++; - do_check_arg_id(id); - return id; - } - - /// Reports an error if using the automatic argument indexing; otherwise - /// switches to the manual indexing. - FMT_CONSTEXPR void check_arg_id(int id) { - if (next_arg_id_ > 0) { - report_error("cannot switch from automatic to manual argument indexing"); - return; - } - next_arg_id_ = -1; - do_check_arg_id(id); - } - FMT_CONSTEXPR void check_arg_id(basic_string_view) { - next_arg_id_ = -1; - } - FMT_CONSTEXPR void check_dynamic_spec(int arg_id); -}; - -FMT_EXPORT -using format_parse_context = basic_format_parse_context; - -namespace detail { -// A parse context with extra data used only in compile-time checks. -template -class compile_parse_context : public basic_format_parse_context { - private: - int num_args_; - const type* types_; - using base = basic_format_parse_context; - - public: - explicit FMT_CONSTEXPR compile_parse_context( - basic_string_view format_str, int num_args, const type* types, - int next_arg_id = 0) - : base(format_str, next_arg_id), num_args_(num_args), types_(types) {} - - constexpr auto num_args() const -> int { return num_args_; } - constexpr auto arg_type(int id) const -> type { return types_[id]; } - - FMT_CONSTEXPR auto next_arg_id() -> int { - int id = base::next_arg_id(); - if (id >= num_args_) report_error("argument not found"); - return id; - } - - FMT_CONSTEXPR void check_arg_id(int id) { - base::check_arg_id(id); - if (id >= num_args_) report_error("argument not found"); - } - using base::check_arg_id; - - FMT_CONSTEXPR void check_dynamic_spec(int arg_id) { - detail::ignore_unused(arg_id); - if (arg_id < num_args_ && types_ && !is_integral_type(types_[arg_id])) - report_error("width/precision is not integer"); - } -}; - -/// A contiguous memory buffer with an optional growing ability. It is an -/// internal class and shouldn't be used directly, only via `memory_buffer`. -template class buffer { - private: - T* ptr_; - size_t size_; - size_t capacity_; - - using grow_fun = void (*)(buffer& buf, size_t capacity); - grow_fun grow_; - - protected: - // Don't initialize ptr_ since it is not accessed to save a few cycles. - FMT_MSC_WARNING(suppress : 26495) - FMT_CONSTEXPR20 buffer(grow_fun grow, size_t sz) noexcept - : size_(sz), capacity_(sz), grow_(grow) {} - - constexpr buffer(grow_fun grow, T* p = nullptr, size_t sz = 0, - size_t cap = 0) noexcept - : ptr_(p), size_(sz), capacity_(cap), grow_(grow) {} - - FMT_CONSTEXPR20 ~buffer() = default; - buffer(buffer&&) = default; - - /// Sets the buffer data and capacity. - FMT_CONSTEXPR void set(T* buf_data, size_t buf_capacity) noexcept { - ptr_ = buf_data; - capacity_ = buf_capacity; - } - - public: - using value_type = T; - using const_reference = const T&; - - buffer(const buffer&) = delete; - void operator=(const buffer&) = delete; - - auto begin() noexcept -> T* { return ptr_; } - auto end() noexcept -> T* { return ptr_ + size_; } - - auto begin() const noexcept -> const T* { return ptr_; } - auto end() const noexcept -> const T* { return ptr_ + size_; } - - /// Returns the size of this buffer. - constexpr auto size() const noexcept -> size_t { return size_; } - - /// Returns the capacity of this buffer. - constexpr auto capacity() const noexcept -> size_t { return capacity_; } - - /// Returns a pointer to the buffer data (not null-terminated). - FMT_CONSTEXPR auto data() noexcept -> T* { return ptr_; } - FMT_CONSTEXPR auto data() const noexcept -> const T* { return ptr_; } - - /// Clears this buffer. - void clear() { size_ = 0; } - - // Tries resizing the buffer to contain `count` elements. If T is a POD type - // the new elements may not be initialized. - FMT_CONSTEXPR void try_resize(size_t count) { - try_reserve(count); - size_ = count <= capacity_ ? count : capacity_; - } - - // Tries increasing the buffer capacity to `new_capacity`. It can increase the - // capacity by a smaller amount than requested but guarantees there is space - // for at least one additional element either by increasing the capacity or by - // flushing the buffer if it is full. - FMT_CONSTEXPR void try_reserve(size_t new_capacity) { - if (new_capacity > capacity_) grow_(*this, new_capacity); - } - - FMT_CONSTEXPR void push_back(const T& value) { - try_reserve(size_ + 1); - ptr_[size_++] = value; - } - - /// Appends data to the end of the buffer. - template void append(const U* begin, const U* end) { - while (begin != end) { - auto count = to_unsigned(end - begin); - try_reserve(size_ + count); - auto free_cap = capacity_ - size_; - if (free_cap < count) count = free_cap; - // A loop is faster than memcpy on small sizes. - T* out = ptr_ + size_; - for (size_t i = 0; i < count; ++i) out[i] = begin[i]; - size_ += count; - begin += count; - } - } - - template FMT_CONSTEXPR auto operator[](Idx index) -> T& { - return ptr_[index]; - } - template - FMT_CONSTEXPR auto operator[](Idx index) const -> const T& { - return ptr_[index]; - } -}; - -struct buffer_traits { - explicit buffer_traits(size_t) {} - auto count() const -> size_t { return 0; } - auto limit(size_t size) -> size_t { return size; } -}; - -class fixed_buffer_traits { - private: - size_t count_ = 0; - size_t limit_; - - public: - explicit fixed_buffer_traits(size_t limit) : limit_(limit) {} - auto count() const -> size_t { return count_; } - auto limit(size_t size) -> size_t { - size_t n = limit_ > count_ ? limit_ - count_ : 0; - count_ += size; - return size < n ? size : n; - } -}; - -// A buffer that writes to an output iterator when flushed. -template -class iterator_buffer : public Traits, public buffer { - private: - OutputIt out_; - enum { buffer_size = 256 }; - T data_[buffer_size]; - - static FMT_CONSTEXPR void grow(buffer& buf, size_t) { - if (buf.size() == buffer_size) static_cast(buf).flush(); - } - - void flush() { - auto size = this->size(); - this->clear(); - const T* begin = data_; - const T* end = begin + this->limit(size); - while (begin != end) *out_++ = *begin++; - } - - public: - explicit iterator_buffer(OutputIt out, size_t n = buffer_size) - : Traits(n), buffer(grow, data_, 0, buffer_size), out_(out) {} - iterator_buffer(iterator_buffer&& other) noexcept - : Traits(other), - buffer(grow, data_, 0, buffer_size), - out_(other.out_) {} - ~iterator_buffer() { - // Don't crash if flush fails during unwinding. - FMT_TRY { flush(); } - FMT_CATCH(...) {} - } - - auto out() -> OutputIt { - flush(); - return out_; - } - auto count() const -> size_t { return Traits::count() + this->size(); } -}; - -template -class iterator_buffer : public fixed_buffer_traits, - public buffer { - private: - T* out_; - enum { buffer_size = 256 }; - T data_[buffer_size]; - - static FMT_CONSTEXPR void grow(buffer& buf, size_t) { - if (buf.size() == buf.capacity()) - static_cast(buf).flush(); - } - - void flush() { - size_t n = this->limit(this->size()); - if (this->data() == out_) { - out_ += n; - this->set(data_, buffer_size); - } - this->clear(); - } - - public: - explicit iterator_buffer(T* out, size_t n = buffer_size) - : fixed_buffer_traits(n), buffer(grow, out, 0, n), out_(out) {} - iterator_buffer(iterator_buffer&& other) noexcept - : fixed_buffer_traits(other), - buffer(static_cast(other)), - out_(other.out_) { - if (this->data() != out_) { - this->set(data_, buffer_size); - this->clear(); - } - } - ~iterator_buffer() { flush(); } - - auto out() -> T* { - flush(); - return out_; - } - auto count() const -> size_t { - return fixed_buffer_traits::count() + this->size(); - } -}; - -template class iterator_buffer : public buffer { - public: - explicit iterator_buffer(T* out, size_t = 0) - : buffer([](buffer&, size_t) {}, out, 0, ~size_t()) {} - - auto out() -> T* { return &*this->end(); } -}; - -// A buffer that writes to a container with the contiguous storage. -template -class iterator_buffer< - OutputIt, - enable_if_t::value && - is_contiguous::value, - typename OutputIt::container_type::value_type>> - : public buffer { - private: - using container_type = typename OutputIt::container_type; - using value_type = typename container_type::value_type; - container_type& container_; - - static FMT_CONSTEXPR void grow(buffer& buf, size_t capacity) { - auto& self = static_cast(buf); - self.container_.resize(capacity); - self.set(&self.container_[0], capacity); - } - - public: - explicit iterator_buffer(container_type& c) - : buffer(grow, c.size()), container_(c) {} - explicit iterator_buffer(OutputIt out, size_t = 0) - : iterator_buffer(get_container(out)) {} - - auto out() -> OutputIt { return back_inserter(container_); } -}; - -// A buffer that counts the number of code units written discarding the output. -template class counting_buffer : public buffer { - private: - enum { buffer_size = 256 }; - T data_[buffer_size]; - size_t count_ = 0; - - static FMT_CONSTEXPR void grow(buffer& buf, size_t) { - if (buf.size() != buffer_size) return; - static_cast(buf).count_ += buf.size(); - buf.clear(); - } - - public: - counting_buffer() : buffer(grow, data_, 0, buffer_size) {} - - auto count() -> size_t { return count_ + this->size(); } -}; -} // namespace detail - -template -FMT_CONSTEXPR void basic_format_parse_context::do_check_arg_id(int id) { - // Argument id is only checked at compile-time during parsing because - // formatting has its own validation. - if (detail::is_constant_evaluated() && - (!FMT_GCC_VERSION || FMT_GCC_VERSION >= 1200)) { - using context = detail::compile_parse_context; - if (id >= static_cast(this)->num_args()) - report_error("argument not found"); - } -} - -template -FMT_CONSTEXPR void basic_format_parse_context::check_dynamic_spec( - int arg_id) { - if (detail::is_constant_evaluated() && - (!FMT_GCC_VERSION || FMT_GCC_VERSION >= 1200)) { - using context = detail::compile_parse_context; - static_cast(this)->check_dynamic_spec(arg_id); - } -} - -FMT_EXPORT template class basic_format_arg; -FMT_EXPORT template class basic_format_args; -FMT_EXPORT template class dynamic_format_arg_store; - -// A formatter for objects of type T. -FMT_EXPORT -template -struct formatter { - // A deleted default constructor indicates a disabled formatter. - formatter() = delete; -}; - -// Specifies if T has an enabled formatter specialization. A type can be -// formattable even if it doesn't have a formatter e.g. via a conversion. -template -using has_formatter = - std::is_constructible>; - -// An output iterator that appends to a buffer. It is used instead of -// back_insert_iterator to reduce symbol sizes and avoid dependency. -template class basic_appender { - private: - detail::buffer* buffer_; - - friend auto get_container(basic_appender app) -> detail::buffer& { - return *app.buffer_; - } - - public: - using iterator_category = int; - using value_type = T; - using difference_type = ptrdiff_t; - using pointer = T*; - using reference = T&; - using container_type = detail::buffer; - FMT_UNCHECKED_ITERATOR(basic_appender); - - FMT_CONSTEXPR basic_appender(detail::buffer& buf) : buffer_(&buf) {} - - auto operator=(T c) -> basic_appender& { - buffer_->push_back(c); - return *this; - } - auto operator*() -> basic_appender& { return *this; } - auto operator++() -> basic_appender& { return *this; } - auto operator++(int) -> basic_appender { return *this; } -}; - -using appender = basic_appender; - -namespace detail { -template -struct is_back_insert_iterator> : std::true_type {}; - -template -struct locking : std::true_type {}; -template -struct locking>::nonlocking>> - : std::false_type {}; - -template FMT_CONSTEXPR inline auto is_locking() -> bool { - return locking::value; -} -template -FMT_CONSTEXPR inline auto is_locking() -> bool { - return locking::value || is_locking(); -} - -// An optimized version of std::copy with the output value type (T). -template ::value)> -auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt { - get_container(out).append(begin, end); - return out; -} - -template ::value)> -FMT_CONSTEXPR auto copy(InputIt begin, InputIt end, OutputIt out) -> OutputIt { - while (begin != end) *out++ = static_cast(*begin++); - return out; -} - -template -FMT_CONSTEXPR auto copy(basic_string_view s, OutputIt out) -> OutputIt { - return copy(s.begin(), s.end(), out); -} - -template -constexpr auto has_const_formatter_impl(T*) - -> decltype(typename Context::template formatter_type().format( - std::declval(), std::declval()), - true) { - return true; -} -template -constexpr auto has_const_formatter_impl(...) -> bool { - return false; -} -template -constexpr auto has_const_formatter() -> bool { - return has_const_formatter_impl(static_cast(nullptr)); -} - -template -struct is_buffer_appender : std::false_type {}; -template -struct is_buffer_appender< - It, bool_constant< - is_back_insert_iterator::value && - std::is_base_of, - typename It::container_type>::value>> - : std::true_type {}; - -// Maps an output iterator to a buffer. -template ::value)> -auto get_buffer(OutputIt out) -> iterator_buffer { - return iterator_buffer(out); -} -template ::value)> -auto get_buffer(OutputIt out) -> buffer& { - return get_container(out); -} - -template -auto get_iterator(Buf& buf, OutputIt) -> decltype(buf.out()) { - return buf.out(); -} -template -auto get_iterator(buffer&, OutputIt out) -> OutputIt { - return out; -} - -struct view {}; - -template struct named_arg : view { - const Char* name; - const T& value; - named_arg(const Char* n, const T& v) : name(n), value(v) {} -}; - -template struct named_arg_info { - const Char* name; - int id; -}; - -template struct is_named_arg : std::false_type {}; -template struct is_statically_named_arg : std::false_type {}; - -template -struct is_named_arg> : std::true_type {}; - -template constexpr auto count() -> size_t { return B ? 1 : 0; } -template constexpr auto count() -> size_t { - return (B1 ? 1 : 0) + count(); -} - -template constexpr auto count_named_args() -> size_t { - return count::value...>(); -} - -template -constexpr auto count_statically_named_args() -> size_t { - return count::value...>(); -} - -struct unformattable {}; -struct unformattable_char : unformattable {}; -struct unformattable_pointer : unformattable {}; - -template struct string_value { - const Char* data; - size_t size; -}; - -template struct named_arg_value { - const named_arg_info* data; - size_t size; -}; - -template struct custom_value { - using parse_context = typename Context::parse_context_type; - void* value; - void (*format)(void* arg, parse_context& parse_ctx, Context& ctx); -}; - -// A formatting argument value. -template class value { - public: - using char_type = typename Context::char_type; - - union { - monostate no_value; - int int_value; - unsigned uint_value; - long long long_long_value; - unsigned long long ulong_long_value; - int128_opt int128_value; - uint128_opt uint128_value; - bool bool_value; - char_type char_value; - float float_value; - double double_value; - long double long_double_value; - const void* pointer; - string_value string; - custom_value custom; - named_arg_value named_args; - }; - - constexpr FMT_ALWAYS_INLINE value() : no_value() {} - constexpr FMT_ALWAYS_INLINE value(int val) : int_value(val) {} - constexpr FMT_ALWAYS_INLINE value(unsigned val) : uint_value(val) {} - constexpr FMT_ALWAYS_INLINE value(long long val) : long_long_value(val) {} - constexpr FMT_ALWAYS_INLINE value(unsigned long long val) - : ulong_long_value(val) {} - FMT_ALWAYS_INLINE value(int128_opt val) : int128_value(val) {} - FMT_ALWAYS_INLINE value(uint128_opt val) : uint128_value(val) {} - constexpr FMT_ALWAYS_INLINE value(float val) : float_value(val) {} - constexpr FMT_ALWAYS_INLINE value(double val) : double_value(val) {} - FMT_ALWAYS_INLINE value(long double val) : long_double_value(val) {} - constexpr FMT_ALWAYS_INLINE value(bool val) : bool_value(val) {} - constexpr FMT_ALWAYS_INLINE value(char_type val) : char_value(val) {} - FMT_CONSTEXPR FMT_ALWAYS_INLINE value(const char_type* val) { - string.data = val; - if (is_constant_evaluated()) string.size = {}; - } - FMT_CONSTEXPR FMT_ALWAYS_INLINE value(basic_string_view val) { - string.data = val.data(); - string.size = val.size(); - } - FMT_ALWAYS_INLINE value(const void* val) : pointer(val) {} - FMT_ALWAYS_INLINE value(const named_arg_info* args, size_t size) - : named_args{args, size} {} - - template FMT_CONSTEXPR20 FMT_ALWAYS_INLINE value(T& val) { - using value_type = remove_const_t; - // T may overload operator& e.g. std::vector::reference in libc++. -#if defined(__cpp_if_constexpr) - if constexpr (std::is_same::value) - custom.value = const_cast(&val); -#endif - if (!is_constant_evaluated()) - custom.value = const_cast(&reinterpret_cast(val)); - // Get the formatter type through the context to allow different contexts - // have different extension points, e.g. `formatter` for `format` and - // `printf_formatter` for `printf`. - custom.format = format_custom_arg< - value_type, typename Context::template formatter_type>; - } - value(unformattable); - value(unformattable_char); - value(unformattable_pointer); - - private: - // Formats an argument of a custom type, such as a user-defined class. - template - static void format_custom_arg(void* arg, - typename Context::parse_context_type& parse_ctx, - Context& ctx) { - auto f = Formatter(); - parse_ctx.advance_to(f.parse(parse_ctx)); - using qualified_type = - conditional_t(), const T, T>; - // format must be const for compatibility with std::format and compilation. - const auto& cf = f; - ctx.advance_to(cf.format(*static_cast(arg), ctx)); - } -}; - -// To minimize the number of types we need to deal with, long is translated -// either to int or to long long depending on its size. -enum { long_short = sizeof(long) == sizeof(int) }; -using long_type = conditional_t; -using ulong_type = conditional_t; - -template struct format_as_result { - template ::value || std::is_class::value)> - static auto map(U*) -> remove_cvref_t()))>; - static auto map(...) -> void; - - using type = decltype(map(static_cast(nullptr))); -}; -template using format_as_t = typename format_as_result::type; - -template -struct has_format_as - : bool_constant, void>::value> {}; - -#define FMT_MAP_API FMT_CONSTEXPR FMT_ALWAYS_INLINE - -// Maps formatting arguments to core types. -// arg_mapper reports errors by returning unformattable instead of using -// static_assert because it's used in the is_formattable trait. -template struct arg_mapper { - using char_type = typename Context::char_type; - - FMT_MAP_API auto map(signed char val) -> int { return val; } - FMT_MAP_API auto map(unsigned char val) -> unsigned { return val; } - FMT_MAP_API auto map(short val) -> int { return val; } - FMT_MAP_API auto map(unsigned short val) -> unsigned { return val; } - FMT_MAP_API auto map(int val) -> int { return val; } - FMT_MAP_API auto map(unsigned val) -> unsigned { return val; } - FMT_MAP_API auto map(long val) -> long_type { return val; } - FMT_MAP_API auto map(unsigned long val) -> ulong_type { return val; } - FMT_MAP_API auto map(long long val) -> long long { return val; } - FMT_MAP_API auto map(unsigned long long val) -> unsigned long long { - return val; - } - FMT_MAP_API auto map(int128_opt val) -> int128_opt { return val; } - FMT_MAP_API auto map(uint128_opt val) -> uint128_opt { return val; } - FMT_MAP_API auto map(bool val) -> bool { return val; } - - template ::value || - std::is_same::value)> - FMT_MAP_API auto map(T val) -> char_type { - return val; - } - template ::value || -#ifdef __cpp_char8_t - std::is_same::value || -#endif - std::is_same::value || - std::is_same::value) && - !std::is_same::value, - int> = 0> - FMT_MAP_API auto map(T) -> unformattable_char { - return {}; - } - - FMT_MAP_API auto map(float val) -> float { return val; } - FMT_MAP_API auto map(double val) -> double { return val; } - FMT_MAP_API auto map(long double val) -> long double { return val; } - - FMT_MAP_API auto map(char_type* val) -> const char_type* { return val; } - FMT_MAP_API auto map(const char_type* val) -> const char_type* { return val; } - template , - FMT_ENABLE_IF(std::is_same::value && - !std::is_pointer::value)> - FMT_MAP_API auto map(const T& val) -> basic_string_view { - return to_string_view(val); - } - template , - FMT_ENABLE_IF(!std::is_same::value && - !std::is_pointer::value)> - FMT_MAP_API auto map(const T&) -> unformattable_char { - return {}; - } - - FMT_MAP_API auto map(void* val) -> const void* { return val; } - FMT_MAP_API auto map(const void* val) -> const void* { return val; } - FMT_MAP_API auto map(volatile void* val) -> const void* { - return const_cast(val); - } - FMT_MAP_API auto map(const volatile void* val) -> const void* { - return const_cast(val); - } - FMT_MAP_API auto map(std::nullptr_t val) -> const void* { return val; } - - // Use SFINAE instead of a const T* parameter to avoid a conflict with the - // array overload. - template < - typename T, - FMT_ENABLE_IF( - std::is_pointer::value || std::is_member_pointer::value || - std::is_function::type>::value || - (std::is_array::value && - !std::is_convertible::value))> - FMT_CONSTEXPR auto map(const T&) -> unformattable_pointer { - return {}; - } - - template ::value)> - FMT_MAP_API auto map(const T (&values)[N]) -> const T (&)[N] { - return values; - } - - // Only map owning types because mapping views can be unsafe. - template , - FMT_ENABLE_IF(std::is_arithmetic::value)> - FMT_MAP_API auto map(const T& val) -> decltype(FMT_DECLTYPE_THIS map(U())) { - return map(format_as(val)); - } - - template > - struct formattable : bool_constant() || - (has_formatter::value && - !std::is_const::value)> {}; - - template ::value)> - FMT_MAP_API auto do_map(T& val) -> T& { - return val; - } - template ::value)> - FMT_MAP_API auto do_map(T&) -> unformattable { - return {}; - } - - // is_fundamental is used to allow formatters for extended FP types. - template , - FMT_ENABLE_IF( - (std::is_class::value || std::is_enum::value || - std::is_union::value || std::is_fundamental::value) && - !has_to_string_view::value && !is_char::value && - !is_named_arg::value && !std::is_integral::value && - !std::is_arithmetic>::value)> - FMT_MAP_API auto map(T& val) -> decltype(FMT_DECLTYPE_THIS do_map(val)) { - return do_map(val); - } - - template ::value)> - FMT_MAP_API auto map(const T& named_arg) - -> decltype(FMT_DECLTYPE_THIS map(named_arg.value)) { - return map(named_arg.value); - } - - auto map(...) -> unformattable { return {}; } -}; - -// A type constant after applying arg_mapper. -template -using mapped_type_constant = - type_constant().map(std::declval())), - typename Context::char_type>; - -enum { packed_arg_bits = 4 }; -// Maximum number of arguments with packed types. -enum { max_packed_args = 62 / packed_arg_bits }; -enum : unsigned long long { is_unpacked_bit = 1ULL << 63 }; -enum : unsigned long long { has_named_args_bit = 1ULL << 62 }; - -template -struct is_output_iterator : std::false_type {}; - -template <> struct is_output_iterator : std::true_type {}; - -template -struct is_output_iterator< - It, T, void_t()++ = std::declval())>> - : std::true_type {}; - -// A type-erased reference to an std::locale to avoid a heavy include. -class locale_ref { - private: - const void* locale_; // A type-erased pointer to std::locale. - - public: - constexpr locale_ref() : locale_(nullptr) {} - template explicit locale_ref(const Locale& loc); - - explicit operator bool() const noexcept { return locale_ != nullptr; } - - template auto get() const -> Locale; -}; - -template constexpr auto encode_types() -> unsigned long long { - return 0; -} - -template -constexpr auto encode_types() -> unsigned long long { - return static_cast(mapped_type_constant::value) | - (encode_types() << packed_arg_bits); -} - -template -constexpr unsigned long long make_descriptor() { - return NUM_ARGS <= max_packed_args ? encode_types() - : is_unpacked_bit | NUM_ARGS; -} - -// This type is intentionally undefined, only used for errors. -template -#if FMT_CLANG_VERSION && FMT_CLANG_VERSION <= 1500 -// https://github.com/fmtlib/fmt/issues/3796 -struct type_is_unformattable_for { -}; -#else -struct type_is_unformattable_for; -#endif - -template -FMT_CONSTEXPR auto make_arg(T& val) -> value { - using arg_type = remove_cvref_t().map(val))>; - - // Use enum instead of constexpr because the latter may generate code. - enum { - formattable_char = !std::is_same::value - }; - static_assert(formattable_char, "Mixing character types is disallowed."); - - // Formatting of arbitrary pointers is disallowed. If you want to format a - // pointer cast it to `void*` or `const void*`. In particular, this forbids - // formatting of `[const] volatile char*` printed as bool by iostreams. - enum { - formattable_pointer = !std::is_same::value - }; - static_assert(formattable_pointer, - "Formatting of non-void pointers is disallowed."); - - enum { formattable = !std::is_same::value }; -#if defined(__cpp_if_constexpr) - if constexpr (!formattable) - type_is_unformattable_for _; -#endif - static_assert( - formattable, - "Cannot format an argument. To make type T formattable provide a " - "formatter specialization: https://fmt.dev/latest/api.html#udt"); - return {arg_mapper().map(val)}; -} - -template -FMT_CONSTEXPR auto make_arg(T& val) -> basic_format_arg { - auto arg = basic_format_arg(); - arg.type_ = mapped_type_constant::value; - arg.value_ = make_arg(val); - return arg; -} - -template -FMT_CONSTEXPR inline auto make_arg(T& val) -> basic_format_arg { - return make_arg(val); -} - -template -using arg_t = conditional_t, - basic_format_arg>; - -template ::value)> -void init_named_arg(named_arg_info*, int& arg_index, int&, const T&) { - ++arg_index; -} -template ::value)> -void init_named_arg(named_arg_info* named_args, int& arg_index, - int& named_arg_index, const T& arg) { - named_args[named_arg_index++] = {arg.name, arg_index++}; -} - -// An array of references to arguments. It can be implicitly converted to -// `fmt::basic_format_args` for passing into type-erased formatting functions -// such as `fmt::vformat`. -template -struct format_arg_store { - // args_[0].named_args points to named_args to avoid bloating format_args. - // +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning. - static constexpr size_t ARGS_ARR_SIZE = 1 + (NUM_ARGS != 0 ? NUM_ARGS : +1); - - arg_t args[ARGS_ARR_SIZE]; - named_arg_info named_args[NUM_NAMED_ARGS]; - - template - FMT_MAP_API format_arg_store(T&... values) - : args{{named_args, NUM_NAMED_ARGS}, - make_arg(values)...} { - using dummy = int[]; - int arg_index = 0, named_arg_index = 0; - (void)dummy{ - 0, - (init_named_arg(named_args, arg_index, named_arg_index, values), 0)...}; - } - - format_arg_store(format_arg_store&& rhs) { - args[0] = {named_args, NUM_NAMED_ARGS}; - for (size_t i = 1; i < ARGS_ARR_SIZE; ++i) args[i] = rhs.args[i]; - for (size_t i = 0; i < NUM_NAMED_ARGS; ++i) - named_args[i] = rhs.named_args[i]; - } - - format_arg_store(const format_arg_store& rhs) = delete; - format_arg_store& operator=(const format_arg_store& rhs) = delete; - format_arg_store& operator=(format_arg_store&& rhs) = delete; -}; - -// A specialization of format_arg_store without named arguments. -// It is a plain struct to reduce binary size in debug mode. -template -struct format_arg_store { - // +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning. - arg_t args[NUM_ARGS != 0 ? NUM_ARGS : +1]; -}; - -} // namespace detail -FMT_BEGIN_EXPORT - -// A formatting argument. Context is a template parameter for the compiled API -// where output can be unbuffered. -template class basic_format_arg { - private: - detail::value value_; - detail::type type_; - - template - friend FMT_CONSTEXPR auto detail::make_arg(T& value) - -> basic_format_arg; - - friend class basic_format_args; - friend class dynamic_format_arg_store; - - using char_type = typename Context::char_type; - - template - friend struct detail::format_arg_store; - - basic_format_arg(const detail::named_arg_info* args, size_t size) - : value_(args, size) {} - - public: - class handle { - public: - explicit handle(detail::custom_value custom) : custom_(custom) {} - - void format(typename Context::parse_context_type& parse_ctx, - Context& ctx) const { - custom_.format(custom_.value, parse_ctx, ctx); - } - - private: - detail::custom_value custom_; - }; - - constexpr basic_format_arg() : type_(detail::type::none_type) {} - - constexpr explicit operator bool() const noexcept { - return type_ != detail::type::none_type; - } - - auto type() const -> detail::type { return type_; } - - auto is_integral() const -> bool { return detail::is_integral_type(type_); } - auto is_arithmetic() const -> bool { - return detail::is_arithmetic_type(type_); - } - - /** - * Visits an argument dispatching to the appropriate visit method based on - * the argument type. For example, if the argument type is `double` then - * `vis(value)` will be called with the value of type `double`. - */ - template - FMT_CONSTEXPR FMT_INLINE auto visit(Visitor&& vis) const -> decltype(vis(0)) { - switch (type_) { - case detail::type::none_type: - break; - case detail::type::int_type: - return vis(value_.int_value); - case detail::type::uint_type: - return vis(value_.uint_value); - case detail::type::long_long_type: - return vis(value_.long_long_value); - case detail::type::ulong_long_type: - return vis(value_.ulong_long_value); - case detail::type::int128_type: - return vis(detail::convert_for_visit(value_.int128_value)); - case detail::type::uint128_type: - return vis(detail::convert_for_visit(value_.uint128_value)); - case detail::type::bool_type: - return vis(value_.bool_value); - case detail::type::char_type: - return vis(value_.char_value); - case detail::type::float_type: - return vis(value_.float_value); - case detail::type::double_type: - return vis(value_.double_value); - case detail::type::long_double_type: - return vis(value_.long_double_value); - case detail::type::cstring_type: - return vis(value_.string.data); - case detail::type::string_type: - using sv = basic_string_view; - return vis(sv(value_.string.data, value_.string.size)); - case detail::type::pointer_type: - return vis(value_.pointer); - case detail::type::custom_type: - return vis(typename basic_format_arg::handle(value_.custom)); - } - return vis(monostate()); - } - - auto format_custom(const char_type* parse_begin, - typename Context::parse_context_type& parse_ctx, - Context& ctx) -> bool { - if (type_ != detail::type::custom_type) return false; - parse_ctx.advance_to(parse_begin); - value_.custom.format(value_.custom.value, parse_ctx, ctx); - return true; - } -}; - -template -FMT_DEPRECATED FMT_CONSTEXPR auto visit_format_arg( - Visitor&& vis, const basic_format_arg& arg) -> decltype(vis(0)) { - return arg.visit(static_cast(vis)); -} - -/** - * A view of a collection of formatting arguments. To avoid lifetime issues it - * should only be used as a parameter type in type-erased functions such as - * `vformat`: - * - * void vlog(fmt::string_view fmt, fmt::format_args args); // OK - * fmt::format_args args = fmt::make_format_args(); // Dangling reference - */ -template class basic_format_args { - public: - using size_type = int; - using format_arg = basic_format_arg; - - private: - // A descriptor that contains information about formatting arguments. - // If the number of arguments is less or equal to max_packed_args then - // argument types are passed in the descriptor. This reduces binary code size - // per formatting function call. - unsigned long long desc_; - union { - // If is_packed() returns true then argument values are stored in values_; - // otherwise they are stored in args_. This is done to improve cache - // locality and reduce compiled code size since storing larger objects - // may require more code (at least on x86-64) even if the same amount of - // data is actually copied to stack. It saves ~10% on the bloat test. - const detail::value* values_; - const format_arg* args_; - }; - - constexpr auto is_packed() const -> bool { - return (desc_ & detail::is_unpacked_bit) == 0; - } - constexpr auto has_named_args() const -> bool { - return (desc_ & detail::has_named_args_bit) != 0; - } - - FMT_CONSTEXPR auto type(int index) const -> detail::type { - int shift = index * detail::packed_arg_bits; - unsigned int mask = (1 << detail::packed_arg_bits) - 1; - return static_cast((desc_ >> shift) & mask); - } - - public: - constexpr basic_format_args() : desc_(0), args_(nullptr) {} - - /// Constructs a `basic_format_args` object from `format_arg_store`. - template - constexpr FMT_ALWAYS_INLINE basic_format_args( - const detail::format_arg_store& - store) - : desc_(DESC), values_(store.args + (NUM_NAMED_ARGS != 0 ? 1 : 0)) {} - - template detail::max_packed_args)> - constexpr basic_format_args( - const detail::format_arg_store& - store) - : desc_(DESC), args_(store.args + (NUM_NAMED_ARGS != 0 ? 1 : 0)) {} - - /// Constructs a `basic_format_args` object from `dynamic_format_arg_store`. - constexpr basic_format_args(const dynamic_format_arg_store& store) - : desc_(store.get_types()), args_(store.data()) {} - - /// Constructs a `basic_format_args` object from a dynamic list of arguments. - constexpr basic_format_args(const format_arg* args, int count) - : desc_(detail::is_unpacked_bit | detail::to_unsigned(count)), - args_(args) {} - - /// Returns the argument with the specified id. - FMT_CONSTEXPR auto get(int id) const -> format_arg { - format_arg arg; - if (!is_packed()) { - if (id < max_size()) arg = args_[id]; - return arg; - } - if (static_cast(id) >= detail::max_packed_args) return arg; - arg.type_ = type(id); - if (arg.type_ == detail::type::none_type) return arg; - arg.value_ = values_[id]; - return arg; - } - - template - auto get(basic_string_view name) const -> format_arg { - int id = get_id(name); - return id >= 0 ? get(id) : format_arg(); - } - - template - FMT_CONSTEXPR auto get_id(basic_string_view name) const -> int { - if (!has_named_args()) return -1; - const auto& named_args = - (is_packed() ? values_[-1] : args_[-1].value_).named_args; - for (size_t i = 0; i < named_args.size; ++i) { - if (named_args.data[i].name == name) return named_args.data[i].id; - } - return -1; - } - - auto max_size() const -> int { - unsigned long long max_packed = detail::max_packed_args; - return static_cast(is_packed() ? max_packed - : desc_ & ~detail::is_unpacked_bit); - } -}; - -// A formatting context. -class context { - private: - appender out_; - basic_format_args args_; - detail::locale_ref loc_; - - public: - /// The character type for the output. - using char_type = char; - - using iterator = appender; - using format_arg = basic_format_arg; - using parse_context_type = basic_format_parse_context; - template using formatter_type = formatter; - - /// Constructs a `basic_format_context` object. References to the arguments - /// are stored in the object so make sure they have appropriate lifetimes. - FMT_CONSTEXPR context(iterator out, basic_format_args ctx_args, - detail::locale_ref loc = {}) - : out_(out), args_(ctx_args), loc_(loc) {} - context(context&&) = default; - context(const context&) = delete; - void operator=(const context&) = delete; - - FMT_CONSTEXPR auto arg(int id) const -> format_arg { return args_.get(id); } - auto arg(string_view name) -> format_arg { return args_.get(name); } - FMT_CONSTEXPR auto arg_id(string_view name) -> int { - return args_.get_id(name); - } - auto args() const -> const basic_format_args& { return args_; } - - // Returns an iterator to the beginning of the output range. - FMT_CONSTEXPR auto out() -> iterator { return out_; } - - // Advances the begin iterator to `it`. - void advance_to(iterator) {} - - FMT_CONSTEXPR auto locale() -> detail::locale_ref { return loc_; } -}; - -template class generic_context; - -// Longer aliases for C++20 compatibility. -template -using basic_format_context = - conditional_t::value, context, - generic_context>; -using format_context = context; - -template -using buffered_context = basic_format_context, Char>; - -template -using is_formattable = bool_constant>() - .map(std::declval()))>::value>; - -#if FMT_USE_CONCEPTS -template -concept formattable = is_formattable, Char>::value; -#endif - -/** - * Constructs an object that stores references to arguments and can be - * implicitly converted to `format_args`. `Context` can be omitted in which case - * it defaults to `format_context`. See `arg` for lifetime considerations. - */ -// Take arguments by lvalue references to avoid some lifetime issues, e.g. -// auto args = make_format_args(std::string()); -template (), - unsigned long long DESC = detail::make_descriptor(), - FMT_ENABLE_IF(NUM_NAMED_ARGS == 0)> -constexpr FMT_ALWAYS_INLINE auto make_format_args(T&... args) - -> detail::format_arg_store { - return {{detail::make_arg( - args)...}}; -} - -#ifndef FMT_DOC -template (), - unsigned long long DESC = - detail::make_descriptor() | - static_cast(detail::has_named_args_bit), - FMT_ENABLE_IF(NUM_NAMED_ARGS != 0)> -constexpr auto make_format_args(T&... args) - -> detail::format_arg_store { - return {args...}; -} -#endif - -/** - * Returns a named argument to be used in a formatting function. - * It should only be used in a call to a formatting function or - * `dynamic_format_arg_store::push_back`. - * - * **Example**: - * - * fmt::print("The answer is {answer}.", fmt::arg("answer", 42)); - */ -template -inline auto arg(const Char* name, const T& arg) -> detail::named_arg { - static_assert(!detail::is_named_arg(), "nested named arguments"); - return {name, arg}; -} -FMT_END_EXPORT - -/// An alias for `basic_format_args`. -// A separate type would result in shorter symbols but break ABI compatibility -// between clang and gcc on ARM (#1919). -FMT_EXPORT using format_args = basic_format_args; - -// We cannot use enum classes as bit fields because of a gcc bug, so we put them -// in namespaces instead (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61414). -// Additionally, if an underlying type is specified, older gcc incorrectly warns -// that the type is too small. Both bugs are fixed in gcc 9.3. -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 903 -# define FMT_ENUM_UNDERLYING_TYPE(type) -#else -# define FMT_ENUM_UNDERLYING_TYPE(type) : type -#endif -namespace align { -enum type FMT_ENUM_UNDERLYING_TYPE(unsigned char){none, left, right, center, - numeric}; -} -using align_t = align::type; -namespace sign { -enum type FMT_ENUM_UNDERLYING_TYPE(unsigned char){none, minus, plus, space}; -} -using sign_t = sign::type; - -namespace detail { - -template -using unsigned_char = typename conditional_t::value, - std::make_unsigned, - type_identity>::type; - -// Character (code unit) type is erased to prevent template bloat. -struct fill_t { - private: - enum { max_size = 4 }; - char data_[max_size] = {' '}; - unsigned char size_ = 1; - - public: - template - FMT_CONSTEXPR void operator=(basic_string_view s) { - auto size = s.size(); - size_ = static_cast(size); - if (size == 1) { - unsigned uchar = static_cast>(s[0]); - data_[0] = static_cast(uchar); - data_[1] = static_cast(uchar >> 8); - return; - } - FMT_ASSERT(size <= max_size, "invalid fill"); - for (size_t i = 0; i < size; ++i) data_[i] = static_cast(s[i]); - } - - FMT_CONSTEXPR void operator=(char c) { - data_[0] = c; - size_ = 1; - } - - constexpr auto size() const -> size_t { return size_; } - - template constexpr auto get() const -> Char { - using uchar = unsigned char; - return static_cast(static_cast(data_[0]) | - (static_cast(data_[1]) << 8)); - } - - template ::value)> - constexpr auto data() const -> const Char* { - return data_; - } - template ::value)> - constexpr auto data() const -> const Char* { - return nullptr; - } -}; -} // namespace detail - -enum class presentation_type : unsigned char { - // Common specifiers: - none = 0, - debug = 1, // '?' - string = 2, // 's' (string, bool) - - // Integral, bool and character specifiers: - dec = 3, // 'd' - hex, // 'x' or 'X' - oct, // 'o' - bin, // 'b' or 'B' - chr, // 'c' - - // String and pointer specifiers: - pointer = 3, // 'p' - - // Floating-point specifiers: - exp = 1, // 'e' or 'E' (1 since there is no FP debug presentation) - fixed, // 'f' or 'F' - general, // 'g' or 'G' - hexfloat // 'a' or 'A' -}; - -// Format specifiers for built-in and string types. -struct format_specs { - int width; - int precision; - presentation_type type; - align_t align : 4; - sign_t sign : 3; - bool upper : 1; // An uppercase version e.g. 'X' for 'x'. - bool alt : 1; // Alternate form ('#'). - bool localized : 1; - detail::fill_t fill; - - constexpr format_specs() - : width(0), - precision(-1), - type(presentation_type::none), - align(align::none), - sign(sign::none), - upper(false), - alt(false), - localized(false) {} -}; - -namespace detail { - -enum class arg_id_kind { none, index, name }; - -// An argument reference. -template struct arg_ref { - FMT_CONSTEXPR arg_ref() : kind(arg_id_kind::none), val() {} - - FMT_CONSTEXPR explicit arg_ref(int index) - : kind(arg_id_kind::index), val(index) {} - FMT_CONSTEXPR explicit arg_ref(basic_string_view name) - : kind(arg_id_kind::name), val(name) {} - - FMT_CONSTEXPR auto operator=(int idx) -> arg_ref& { - kind = arg_id_kind::index; - val.index = idx; - return *this; - } - - arg_id_kind kind; - union value { - FMT_CONSTEXPR value(int idx = 0) : index(idx) {} - FMT_CONSTEXPR value(basic_string_view n) : name(n) {} - - int index; - basic_string_view name; - } val; -}; - -// Format specifiers with width and precision resolved at formatting rather -// than parsing time to allow reusing the same parsed specifiers with -// different sets of arguments (precompilation of format strings). -template struct dynamic_format_specs : format_specs { - arg_ref width_ref; - arg_ref precision_ref; -}; - -// Converts a character to ASCII. Returns '\0' on conversion failure. -template ::value)> -constexpr auto to_ascii(Char c) -> char { - return c <= 0xff ? static_cast(c) : '\0'; -} - -// Returns the number of code units in a code point or 1 on error. -template -FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { - if (const_check(sizeof(Char) != 1)) return 1; - auto c = static_cast(*begin); - return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 0x3) + 1; -} - -// Return the result via the out param to workaround gcc bug 77539. -template -FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr& out) -> bool { - for (out = first; out != last; ++out) { - if (*out == value) return true; - } - return false; -} - -template <> -inline auto find(const char* first, const char* last, char value, - const char*& out) -> bool { - out = - static_cast(memchr(first, value, to_unsigned(last - first))); - return out != nullptr; -} - -// Parses the range [begin, end) as an unsigned integer. This function assumes -// that the range is non-empty and the first character is a digit. -template -FMT_CONSTEXPR auto parse_nonnegative_int(const Char*& begin, const Char* end, - int error_value) noexcept -> int { - FMT_ASSERT(begin != end && '0' <= *begin && *begin <= '9', ""); - unsigned value = 0, prev = 0; - auto p = begin; - do { - prev = value; - value = value * 10 + unsigned(*p - '0'); - ++p; - } while (p != end && '0' <= *p && *p <= '9'); - auto num_digits = p - begin; - begin = p; - int digits10 = static_cast(sizeof(int) * CHAR_BIT * 3 / 10); - if (num_digits <= digits10) return static_cast(value); - // Check for overflow. - unsigned max = INT_MAX; - return num_digits == digits10 + 1 && - prev * 10ull + unsigned(p[-1] - '0') <= max - ? static_cast(value) - : error_value; -} - -FMT_CONSTEXPR inline auto parse_align(char c) -> align_t { - switch (c) { - case '<': - return align::left; - case '>': - return align::right; - case '^': - return align::center; - } - return align::none; -} - -template constexpr auto is_name_start(Char c) -> bool { - return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_'; -} - -template -FMT_CONSTEXPR auto do_parse_arg_id(const Char* begin, const Char* end, - Handler&& handler) -> const Char* { - Char c = *begin; - if (c >= '0' && c <= '9') { - int index = 0; - if (c != '0') - index = parse_nonnegative_int(begin, end, INT_MAX); - else - ++begin; - if (begin == end || (*begin != '}' && *begin != ':')) - report_error("invalid format string"); - else - handler.on_index(index); - return begin; - } - if (!is_name_start(c)) { - report_error("invalid format string"); - return begin; - } - auto it = begin; - do { - ++it; - } while (it != end && (is_name_start(*it) || ('0' <= *it && *it <= '9'))); - handler.on_name({begin, to_unsigned(it - begin)}); - return it; -} - -template -FMT_CONSTEXPR auto parse_arg_id(const Char* begin, const Char* end, - Handler&& handler) -> const Char* { - FMT_ASSERT(begin != end, ""); - Char c = *begin; - if (c != '}' && c != ':') return do_parse_arg_id(begin, end, handler); - handler.on_auto(); - return begin; -} - -template struct dynamic_spec_id_handler { - basic_format_parse_context& ctx; - arg_ref& ref; - - FMT_CONSTEXPR void on_auto() { - int id = ctx.next_arg_id(); - ref = arg_ref(id); - ctx.check_dynamic_spec(id); - } - FMT_CONSTEXPR void on_index(int id) { - ref = arg_ref(id); - ctx.check_arg_id(id); - ctx.check_dynamic_spec(id); - } - FMT_CONSTEXPR void on_name(basic_string_view id) { - ref = arg_ref(id); - ctx.check_arg_id(id); - } -}; - -// Parses [integer | "{" [arg_id] "}"]. -template -FMT_CONSTEXPR auto parse_dynamic_spec(const Char* begin, const Char* end, - int& value, arg_ref& ref, - basic_format_parse_context& ctx) - -> const Char* { - FMT_ASSERT(begin != end, ""); - if ('0' <= *begin && *begin <= '9') { - int val = parse_nonnegative_int(begin, end, -1); - if (val != -1) - value = val; - else - report_error("number is too big"); - } else if (*begin == '{') { - ++begin; - auto handler = dynamic_spec_id_handler{ctx, ref}; - if (begin != end) begin = parse_arg_id(begin, end, handler); - if (begin != end && *begin == '}') return ++begin; - report_error("invalid format string"); - } - return begin; -} - -template -FMT_CONSTEXPR auto parse_precision(const Char* begin, const Char* end, - int& value, arg_ref& ref, - basic_format_parse_context& ctx) - -> const Char* { - ++begin; - if (begin == end || *begin == '}') { - report_error("invalid precision"); - return begin; - } - return parse_dynamic_spec(begin, end, value, ref, ctx); -} - -enum class state { start, align, sign, hash, zero, width, precision, locale }; - -// Parses standard format specifiers. -template -FMT_CONSTEXPR auto parse_format_specs(const Char* begin, const Char* end, - dynamic_format_specs& specs, - basic_format_parse_context& ctx, - type arg_type) -> const Char* { - auto c = '\0'; - if (end - begin > 1) { - auto next = to_ascii(begin[1]); - c = parse_align(next) == align::none ? to_ascii(*begin) : '\0'; - } else { - if (begin == end) return begin; - c = to_ascii(*begin); - } - - struct { - state current_state = state::start; - FMT_CONSTEXPR void operator()(state s, bool valid = true) { - if (current_state >= s || !valid) - report_error("invalid format specifier"); - current_state = s; - } - } enter_state; - - using pres = presentation_type; - constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; - struct { - const Char*& begin; - dynamic_format_specs& specs; - type arg_type; - - FMT_CONSTEXPR auto operator()(pres pres_type, int set) -> const Char* { - if (!in(arg_type, set)) { - if (arg_type == type::none_type) return begin; - report_error("invalid format specifier"); - } - specs.type = pres_type; - return begin + 1; - } - } parse_presentation_type{begin, specs, arg_type}; - - for (;;) { - switch (c) { - case '<': - case '>': - case '^': - enter_state(state::align); - specs.align = parse_align(c); - ++begin; - break; - case '+': - case '-': - case ' ': - if (arg_type == type::none_type) return begin; - enter_state(state::sign, in(arg_type, sint_set | float_set)); - switch (c) { - case '+': - specs.sign = sign::plus; - break; - case '-': - specs.sign = sign::minus; - break; - case ' ': - specs.sign = sign::space; - break; - } - ++begin; - break; - case '#': - if (arg_type == type::none_type) return begin; - enter_state(state::hash, is_arithmetic_type(arg_type)); - specs.alt = true; - ++begin; - break; - case '0': - enter_state(state::zero); - if (!is_arithmetic_type(arg_type)) { - if (arg_type == type::none_type) return begin; - report_error("format specifier requires numeric argument"); - } - if (specs.align == align::none) { - // Ignore 0 if align is specified for compatibility with std::format. - specs.align = align::numeric; - specs.fill = '0'; - } - ++begin; - break; - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '{': - enter_state(state::width); - begin = parse_dynamic_spec(begin, end, specs.width, specs.width_ref, ctx); - break; - case '.': - if (arg_type == type::none_type) return begin; - enter_state(state::precision, - in(arg_type, float_set | string_set | cstring_set)); - begin = parse_precision(begin, end, specs.precision, specs.precision_ref, - ctx); - break; - case 'L': - if (arg_type == type::none_type) return begin; - enter_state(state::locale, is_arithmetic_type(arg_type)); - specs.localized = true; - ++begin; - break; - case 'd': - return parse_presentation_type(pres::dec, integral_set); - case 'X': - specs.upper = true; - FMT_FALLTHROUGH; - case 'x': - return parse_presentation_type(pres::hex, integral_set); - case 'o': - return parse_presentation_type(pres::oct, integral_set); - case 'B': - specs.upper = true; - FMT_FALLTHROUGH; - case 'b': - return parse_presentation_type(pres::bin, integral_set); - case 'E': - specs.upper = true; - FMT_FALLTHROUGH; - case 'e': - return parse_presentation_type(pres::exp, float_set); - case 'F': - specs.upper = true; - FMT_FALLTHROUGH; - case 'f': - return parse_presentation_type(pres::fixed, float_set); - case 'G': - specs.upper = true; - FMT_FALLTHROUGH; - case 'g': - return parse_presentation_type(pres::general, float_set); - case 'A': - specs.upper = true; - FMT_FALLTHROUGH; - case 'a': - return parse_presentation_type(pres::hexfloat, float_set); - case 'c': - if (arg_type == type::bool_type) report_error("invalid format specifier"); - return parse_presentation_type(pres::chr, integral_set); - case 's': - return parse_presentation_type(pres::string, - bool_set | string_set | cstring_set); - case 'p': - return parse_presentation_type(pres::pointer, pointer_set | cstring_set); - case '?': - return parse_presentation_type(pres::debug, - char_set | string_set | cstring_set); - case '}': - return begin; - default: { - if (*begin == '}') return begin; - // Parse fill and alignment. - auto fill_end = begin + code_point_length(begin); - if (end - fill_end <= 0) { - report_error("invalid format specifier"); - return begin; - } - if (*begin == '{') { - report_error("invalid fill character '{'"); - return begin; - } - auto align = parse_align(to_ascii(*fill_end)); - enter_state(state::align, align != align::none); - specs.fill = - basic_string_view(begin, to_unsigned(fill_end - begin)); - specs.align = align; - begin = fill_end + 1; - } - } - if (begin == end) return begin; - c = to_ascii(*begin); - } -} - -template -FMT_CONSTEXPR auto parse_replacement_field(const Char* begin, const Char* end, - Handler&& handler) -> const Char* { - struct id_adapter { - Handler& handler; - int arg_id; - - FMT_CONSTEXPR void on_auto() { arg_id = handler.on_arg_id(); } - FMT_CONSTEXPR void on_index(int id) { arg_id = handler.on_arg_id(id); } - FMT_CONSTEXPR void on_name(basic_string_view id) { - arg_id = handler.on_arg_id(id); - } - }; - - ++begin; - if (begin == end) return handler.on_error("invalid format string"), end; - if (*begin == '}') { - handler.on_replacement_field(handler.on_arg_id(), begin); - } else if (*begin == '{') { - handler.on_text(begin, begin + 1); - } else { - auto adapter = id_adapter{handler, 0}; - begin = parse_arg_id(begin, end, adapter); - Char c = begin != end ? *begin : Char(); - if (c == '}') { - handler.on_replacement_field(adapter.arg_id, begin); - } else if (c == ':') { - begin = handler.on_format_specs(adapter.arg_id, begin + 1, end); - if (begin == end || *begin != '}') - return handler.on_error("unknown format specifier"), end; - } else { - return handler.on_error("missing '}' in format string"), end; - } - } - return begin + 1; -} - -template -FMT_CONSTEXPR void parse_format_string(basic_string_view format_str, - Handler&& handler) { - auto begin = format_str.data(); - auto end = begin + format_str.size(); - if (end - begin < 32) { - // Use a simple loop instead of memchr for small strings. - const Char* p = begin; - while (p != end) { - auto c = *p++; - if (c == '{') { - handler.on_text(begin, p - 1); - begin = p = parse_replacement_field(p - 1, end, handler); - } else if (c == '}') { - if (p == end || *p != '}') - return handler.on_error("unmatched '}' in format string"); - handler.on_text(begin, p); - begin = ++p; - } - } - handler.on_text(begin, end); - return; - } - struct writer { - FMT_CONSTEXPR void operator()(const Char* from, const Char* to) { - if (from == to) return; - for (;;) { - const Char* p = nullptr; - if (!find(from, to, Char('}'), p)) - return handler_.on_text(from, to); - ++p; - if (p == to || *p != '}') - return handler_.on_error("unmatched '}' in format string"); - handler_.on_text(from, p); - from = p + 1; - } - } - Handler& handler_; - } write = {handler}; - while (begin != end) { - // Doing two passes with memchr (one for '{' and another for '}') is up to - // 2.5x faster than the naive one-pass implementation on big format strings. - const Char* p = begin; - if (*begin != '{' && !find(begin + 1, end, Char('{'), p)) - return write(begin, end); - write(begin, p); - begin = parse_replacement_field(p, end, handler); - } -} - -template ::value> struct strip_named_arg { - using type = T; -}; -template struct strip_named_arg { - using type = remove_cvref_t; -}; - -template -FMT_VISIBILITY("hidden") // Suppress an ld warning on macOS (#3769). -FMT_CONSTEXPR auto parse_format_specs(ParseContext& ctx) - -> decltype(ctx.begin()) { - using char_type = typename ParseContext::char_type; - using context = buffered_context; - using mapped_type = conditional_t< - mapped_type_constant::value != type::custom_type, - decltype(arg_mapper().map(std::declval())), - typename strip_named_arg::type>; -#if defined(__cpp_if_constexpr) - if constexpr (std::is_default_constructible< - formatter>::value) { - return formatter().parse(ctx); - } else { - type_is_unformattable_for _; - return ctx.begin(); - } -#else - return formatter().parse(ctx); -#endif -} - -// Checks char specs and returns true iff the presentation type is char-like. -FMT_CONSTEXPR inline auto check_char_specs(const format_specs& specs) -> bool { - if (specs.type != presentation_type::none && - specs.type != presentation_type::chr && - specs.type != presentation_type::debug) { - return false; - } - if (specs.align == align::numeric || specs.sign != sign::none || specs.alt) - report_error("invalid format specifier for char"); - return true; -} - -#if FMT_USE_NONTYPE_TEMPLATE_ARGS -template -constexpr auto get_arg_index_by_name(basic_string_view name) -> int { - if constexpr (is_statically_named_arg()) { - if (name == T::name) return N; - } - if constexpr (sizeof...(Args) > 0) - return get_arg_index_by_name(name); - (void)name; // Workaround an MSVC bug about "unused" parameter. - return -1; -} -#endif - -template -FMT_CONSTEXPR auto get_arg_index_by_name(basic_string_view name) -> int { -#if FMT_USE_NONTYPE_TEMPLATE_ARGS - if constexpr (sizeof...(Args) > 0) - return get_arg_index_by_name<0, Args...>(name); -#endif - (void)name; - return -1; -} - -template class format_string_checker { - private: - using parse_context_type = compile_parse_context; - static constexpr int num_args = sizeof...(Args); - - // Format specifier parsing function. - // In the future basic_format_parse_context will replace compile_parse_context - // here and will use is_constant_evaluated and downcasting to access the data - // needed for compile-time checks: https://godbolt.org/z/GvWzcTjh1. - using parse_func = const Char* (*)(parse_context_type&); - - type types_[num_args > 0 ? static_cast(num_args) : 1]; - parse_context_type context_; - parse_func parse_funcs_[num_args > 0 ? static_cast(num_args) : 1]; - - public: - explicit FMT_CONSTEXPR format_string_checker(basic_string_view fmt) - : types_{mapped_type_constant>::value...}, - context_(fmt, num_args, types_), - parse_funcs_{&parse_format_specs...} {} - - FMT_CONSTEXPR void on_text(const Char*, const Char*) {} - - FMT_CONSTEXPR auto on_arg_id() -> int { return context_.next_arg_id(); } - FMT_CONSTEXPR auto on_arg_id(int id) -> int { - return context_.check_arg_id(id), id; - } - FMT_CONSTEXPR auto on_arg_id(basic_string_view id) -> int { -#if FMT_USE_NONTYPE_TEMPLATE_ARGS - auto index = get_arg_index_by_name(id); - if (index < 0) on_error("named argument is not found"); - return index; -#else - (void)id; - on_error("compile-time checks for named arguments require C++20 support"); - return 0; -#endif - } - - FMT_CONSTEXPR void on_replacement_field(int id, const Char* begin) { - on_format_specs(id, begin, begin); // Call parse() on empty specs. - } - - FMT_CONSTEXPR auto on_format_specs(int id, const Char* begin, const Char*) - -> const Char* { - context_.advance_to(begin); - // id >= 0 check is a workaround for gcc 10 bug (#2065). - return id >= 0 && id < num_args ? parse_funcs_[id](context_) : begin; - } - - FMT_NORETURN FMT_CONSTEXPR void on_error(const char* message) { - report_error(message); - } -}; - -// A base class for compile-time strings. -struct compile_string {}; - -template -using is_compile_string = std::is_base_of; - -// Reports a compile-time error if S is not a valid format string. -template ::value)> -FMT_ALWAYS_INLINE void check_format_string(const S&) { -#ifdef FMT_ENFORCE_COMPILE_STRING - static_assert(is_compile_string::value, - "FMT_ENFORCE_COMPILE_STRING requires all format strings to use " - "FMT_STRING."); -#endif -} -template ::value)> -void check_format_string(S format_str) { - using char_t = typename S::char_type; - FMT_CONSTEXPR auto s = basic_string_view(format_str); - using checker = format_string_checker...>; - FMT_CONSTEXPR bool error = (parse_format_string(s, checker(s)), true); - ignore_unused(error); -} - -// Report truncation to prevent silent data loss. -inline void report_truncation(bool truncated) { - if (truncated) report_error("output is truncated"); -} - -// Use vformat_args and avoid type_identity to keep symbols short and workaround -// a GCC <= 4.8 bug. -template struct vformat_args { - using type = basic_format_args>; -}; -template <> struct vformat_args { - using type = format_args; -}; - -template -void vformat_to(buffer& buf, basic_string_view fmt, - typename vformat_args::type args, locale_ref loc = {}); - -FMT_API void vprint_mojibake(FILE*, string_view, format_args, bool = false); -#ifndef _WIN32 -inline void vprint_mojibake(FILE*, string_view, format_args, bool) {} -#endif - -template struct native_formatter { - private: - dynamic_format_specs specs_; - - public: - using nonlocking = void; - - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* { - if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); - auto end = parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, TYPE); - if (const_check(TYPE == type::char_type)) check_char_specs(specs_); - return end; - } - - template - FMT_CONSTEXPR void set_debug_format(bool set = true) { - specs_.type = set ? presentation_type::debug : presentation_type::none; - } - - template - FMT_CONSTEXPR auto format(const T& val, FormatContext& ctx) const - -> decltype(ctx.out()); -}; -} // namespace detail - -FMT_BEGIN_EXPORT - -// A formatter specialization for natively supported types. -template -struct formatter::value != - detail::type::custom_type>> - : detail::native_formatter::value> { -}; - -template struct runtime_format_string { - basic_string_view str; -}; - -/// A compile-time format string. -template class basic_format_string { - private: - basic_string_view str_; - - public: - template < - typename S, - FMT_ENABLE_IF( - std::is_convertible>::value || - (detail::is_compile_string::value && - std::is_constructible, const S&>::value))> - FMT_CONSTEVAL FMT_ALWAYS_INLINE basic_format_string(const S& s) : str_(s) { - static_assert( - detail::count< - (std::is_base_of>::value && - std::is_reference::value)...>() == 0, - "passing views as lvalues is disallowed"); -#if FMT_USE_CONSTEVAL - if constexpr (detail::count_named_args() == - detail::count_statically_named_args()) { - using checker = - detail::format_string_checker...>; - detail::parse_format_string(str_, checker(s)); - } -#else - detail::check_format_string(s); -#endif - } - basic_format_string(runtime_format_string fmt) : str_(fmt.str) {} - - FMT_ALWAYS_INLINE operator basic_string_view() const { return str_; } - auto get() const -> basic_string_view { return str_; } -}; - -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 -// Workaround broken conversion on older gcc. -template using format_string = string_view; -inline auto runtime(string_view s) -> string_view { return s; } -#else -template -using format_string = basic_format_string...>; -/** - * Creates a runtime format string. - * - * **Example**: - * - * // Check format string at runtime instead of compile-time. - * fmt::print(fmt::runtime("{:d}"), "I am not a number"); - */ -inline auto runtime(string_view s) -> runtime_format_string<> { return {{s}}; } -#endif - -/// Formats a string and writes the output to `out`. -template , - char>::value)> -auto vformat_to(OutputIt&& out, string_view fmt, format_args args) - -> remove_cvref_t { - auto&& buf = detail::get_buffer(out); - detail::vformat_to(buf, fmt, args, {}); - return detail::get_iterator(buf, out); -} - -/** - * Formats `args` according to specifications in `fmt`, writes the result to - * the output iterator `out` and returns the iterator past the end of the output - * range. `format_to` does not append a terminating null character. - * - * **Example**: - * - * auto out = std::vector(); - * fmt::format_to(std::back_inserter(out), "{}", 42); - */ -template , - char>::value)> -FMT_INLINE auto format_to(OutputIt&& out, format_string fmt, T&&... args) - -> remove_cvref_t { - return vformat_to(FMT_FWD(out), fmt, fmt::make_format_args(args...)); -} - -template struct format_to_n_result { - /// Iterator past the end of the output range. - OutputIt out; - /// Total (not truncated) output size. - size_t size; -}; - -template ::value)> -auto vformat_to_n(OutputIt out, size_t n, string_view fmt, format_args args) - -> format_to_n_result { - using traits = detail::fixed_buffer_traits; - auto buf = detail::iterator_buffer(out, n); - detail::vformat_to(buf, fmt, args, {}); - return {buf.out(), buf.count()}; -} - -/** - * Formats `args` according to specifications in `fmt`, writes up to `n` - * characters of the result to the output iterator `out` and returns the total - * (not truncated) output size and the iterator past the end of the output - * range. `format_to_n` does not append a terminating null character. - */ -template ::value)> -FMT_INLINE auto format_to_n(OutputIt out, size_t n, format_string fmt, - T&&... args) -> format_to_n_result { - return vformat_to_n(out, n, fmt, fmt::make_format_args(args...)); -} - -template -struct format_to_result { - /// Iterator pointing to just after the last successful write in the range. - OutputIt out; - /// Specifies if the output was truncated. - bool truncated; - - FMT_CONSTEXPR operator OutputIt&() & { - detail::report_truncation(truncated); - return out; - } - FMT_CONSTEXPR operator const OutputIt&() const& { - detail::report_truncation(truncated); - return out; - } - FMT_CONSTEXPR operator OutputIt&&() && { - detail::report_truncation(truncated); - return static_cast(out); - } -}; - -template -auto vformat_to(char (&out)[N], string_view fmt, format_args args) - -> format_to_result { - auto result = vformat_to_n(out, N, fmt, args); - return {result.out, result.size > N}; -} - -template -FMT_INLINE auto format_to(char (&out)[N], format_string fmt, T&&... args) - -> format_to_result { - auto result = fmt::format_to_n(out, N, fmt, static_cast(args)...); - return {result.out, result.size > N}; -} - -/// Returns the number of chars in the output of `format(fmt, args...)`. -template -FMT_NODISCARD FMT_INLINE auto formatted_size(format_string fmt, - T&&... args) -> size_t { - auto buf = detail::counting_buffer<>(); - detail::vformat_to(buf, fmt, fmt::make_format_args(args...), {}); - return buf.count(); -} - -FMT_API void vprint(string_view fmt, format_args args); -FMT_API void vprint(FILE* f, string_view fmt, format_args args); -FMT_API void vprint_buffered(FILE* f, string_view fmt, format_args args); -FMT_API void vprintln(FILE* f, string_view fmt, format_args args); - -/** - * Formats `args` according to specifications in `fmt` and writes the output - * to `stdout`. - * - * **Example**: - * - * fmt::print("The answer is {}.", 42); - */ -template -FMT_INLINE void print(format_string fmt, T&&... args) { - const auto& vargs = fmt::make_format_args(args...); - if (!detail::use_utf8()) return detail::vprint_mojibake(stdout, fmt, vargs); - return detail::is_locking() ? vprint_buffered(stdout, fmt, vargs) - : vprint(fmt, vargs); -} - -/** - * Formats `args` according to specifications in `fmt` and writes the - * output to the file `f`. - * - * **Example**: - * - * fmt::print(stderr, "Don't {}!", "panic"); - */ -template -FMT_INLINE void print(FILE* f, format_string fmt, T&&... args) { - const auto& vargs = fmt::make_format_args(args...); - if (!detail::use_utf8()) return detail::vprint_mojibake(f, fmt, vargs); - return detail::is_locking() ? vprint_buffered(f, fmt, vargs) - : vprint(f, fmt, vargs); -} - -/// Formats `args` according to specifications in `fmt` and writes the output -/// to the file `f` followed by a newline. -template -FMT_INLINE void println(FILE* f, format_string fmt, T&&... args) { - const auto& vargs = fmt::make_format_args(args...); - return detail::use_utf8() ? vprintln(f, fmt, vargs) - : detail::vprint_mojibake(f, fmt, vargs, true); -} - -/// Formats `args` according to specifications in `fmt` and writes the output -/// to `stdout` followed by a newline. -template -FMT_INLINE void println(format_string fmt, T&&... args) { - return fmt::println(stdout, fmt, static_cast(args)...); -} - -FMT_END_EXPORT -FMT_GCC_PRAGMA("GCC pop_options") -FMT_END_NAMESPACE - -#ifdef FMT_HEADER_ONLY -# include "format.h" -#endif -#endif // FMT_BASE_H_ diff --git a/include/spdlog/fmt/bundled/chrono.h b/include/spdlog/fmt/bundled/chrono.h deleted file mode 100644 index c93123f..0000000 --- a/include/spdlog/fmt/bundled/chrono.h +++ /dev/null @@ -1,2432 +0,0 @@ -// Formatting library for C++ - chrono support -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_CHRONO_H_ -#define FMT_CHRONO_H_ - -#ifndef FMT_MODULE -# include -# include -# include // std::isfinite -# include // std::memcpy -# include -# include -# include -# include -# include -#endif - -#include "format.h" - -FMT_BEGIN_NAMESPACE - -// Check if std::chrono::local_t is available. -#ifndef FMT_USE_LOCAL_TIME -# ifdef __cpp_lib_chrono -# define FMT_USE_LOCAL_TIME (__cpp_lib_chrono >= 201907L) -# else -# define FMT_USE_LOCAL_TIME 0 -# endif -#endif - -// Check if std::chrono::utc_timestamp is available. -#ifndef FMT_USE_UTC_TIME -# ifdef __cpp_lib_chrono -# define FMT_USE_UTC_TIME (__cpp_lib_chrono >= 201907L) -# else -# define FMT_USE_UTC_TIME 0 -# endif -#endif - -// Enable tzset. -#ifndef FMT_USE_TZSET -// UWP doesn't provide _tzset. -# if FMT_HAS_INCLUDE("winapifamily.h") -# include -# endif -# if defined(_WIN32) && (!defined(WINAPI_FAMILY) || \ - (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) -# define FMT_USE_TZSET 1 -# else -# define FMT_USE_TZSET 0 -# endif -#endif - -// Enable safe chrono durations, unless explicitly disabled. -#ifndef FMT_SAFE_DURATION_CAST -# define FMT_SAFE_DURATION_CAST 1 -#endif -#if FMT_SAFE_DURATION_CAST - -// For conversion between std::chrono::durations without undefined -// behaviour or erroneous results. -// This is a stripped down version of duration_cast, for inclusion in fmt. -// See https://github.com/pauldreik/safe_duration_cast -// -// Copyright Paul Dreik 2019 -namespace safe_duration_cast { - -template ::value && - std::numeric_limits::is_signed == - std::numeric_limits::is_signed)> -FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) - -> To { - ec = 0; - using F = std::numeric_limits; - using T = std::numeric_limits; - static_assert(F::is_integer, "From must be integral"); - static_assert(T::is_integer, "To must be integral"); - - // A and B are both signed, or both unsigned. - if (detail::const_check(F::digits <= T::digits)) { - // From fits in To without any problem. - } else { - // From does not always fit in To, resort to a dynamic check. - if (from < (T::min)() || from > (T::max)()) { - // outside range. - ec = 1; - return {}; - } - } - return static_cast(from); -} - -/// Converts From to To, without loss. If the dynamic value of from -/// can't be converted to To without loss, ec is set. -template ::value && - std::numeric_limits::is_signed != - std::numeric_limits::is_signed)> -FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) - -> To { - ec = 0; - using F = std::numeric_limits; - using T = std::numeric_limits; - static_assert(F::is_integer, "From must be integral"); - static_assert(T::is_integer, "To must be integral"); - - if (detail::const_check(F::is_signed && !T::is_signed)) { - // From may be negative, not allowed! - if (fmt::detail::is_negative(from)) { - ec = 1; - return {}; - } - // From is positive. Can it always fit in To? - if (detail::const_check(F::digits > T::digits) && - from > static_cast(detail::max_value())) { - ec = 1; - return {}; - } - } - - if (detail::const_check(!F::is_signed && T::is_signed && - F::digits >= T::digits) && - from > static_cast(detail::max_value())) { - ec = 1; - return {}; - } - return static_cast(from); // Lossless conversion. -} - -template ::value)> -FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) - -> To { - ec = 0; - return from; -} // function - -// clang-format off -/** - * converts From to To if possible, otherwise ec is set. - * - * input | output - * ---------------------------------|--------------- - * NaN | NaN - * Inf | Inf - * normal, fits in output | converted (possibly lossy) - * normal, does not fit in output | ec is set - * subnormal | best effort - * -Inf | -Inf - */ -// clang-format on -template ::value)> -FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To { - ec = 0; - using T = std::numeric_limits; - static_assert(std::is_floating_point::value, "From must be floating"); - static_assert(std::is_floating_point::value, "To must be floating"); - - // catch the only happy case - if (std::isfinite(from)) { - if (from >= T::lowest() && from <= (T::max)()) { - return static_cast(from); - } - // not within range. - ec = 1; - return {}; - } - - // nan and inf will be preserved - return static_cast(from); -} // function - -template ::value)> -FMT_CONSTEXPR auto safe_float_conversion(const From from, int& ec) -> To { - ec = 0; - static_assert(std::is_floating_point::value, "From must be floating"); - return from; -} - -/// Safe duration cast between integral durations -template ::value), - FMT_ENABLE_IF(std::is_integral::value)> -auto safe_duration_cast(std::chrono::duration from, - int& ec) -> To { - using From = std::chrono::duration; - ec = 0; - // the basic idea is that we need to convert from count() in the from type - // to count() in the To type, by multiplying it with this: - struct Factor - : std::ratio_divide {}; - - static_assert(Factor::num > 0, "num must be positive"); - static_assert(Factor::den > 0, "den must be positive"); - - // the conversion is like this: multiply from.count() with Factor::num - // /Factor::den and convert it to To::rep, all this without - // overflow/underflow. let's start by finding a suitable type that can hold - // both To, From and Factor::num - using IntermediateRep = - typename std::common_type::type; - - // safe conversion to IntermediateRep - IntermediateRep count = - lossless_integral_conversion(from.count(), ec); - if (ec) return {}; - // multiply with Factor::num without overflow or underflow - if (detail::const_check(Factor::num != 1)) { - const auto max1 = detail::max_value() / Factor::num; - if (count > max1) { - ec = 1; - return {}; - } - const auto min1 = - (std::numeric_limits::min)() / Factor::num; - if (detail::const_check(!std::is_unsigned::value) && - count < min1) { - ec = 1; - return {}; - } - count *= Factor::num; - } - - if (detail::const_check(Factor::den != 1)) count /= Factor::den; - auto tocount = lossless_integral_conversion(count, ec); - return ec ? To() : To(tocount); -} - -/// Safe duration_cast between floating point durations -template ::value), - FMT_ENABLE_IF(std::is_floating_point::value)> -auto safe_duration_cast(std::chrono::duration from, - int& ec) -> To { - using From = std::chrono::duration; - ec = 0; - if (std::isnan(from.count())) { - // nan in, gives nan out. easy. - return To{std::numeric_limits::quiet_NaN()}; - } - // maybe we should also check if from is denormal, and decide what to do about - // it. - - // +-inf should be preserved. - if (std::isinf(from.count())) { - return To{from.count()}; - } - - // the basic idea is that we need to convert from count() in the from type - // to count() in the To type, by multiplying it with this: - struct Factor - : std::ratio_divide {}; - - static_assert(Factor::num > 0, "num must be positive"); - static_assert(Factor::den > 0, "den must be positive"); - - // the conversion is like this: multiply from.count() with Factor::num - // /Factor::den and convert it to To::rep, all this without - // overflow/underflow. let's start by finding a suitable type that can hold - // both To, From and Factor::num - using IntermediateRep = - typename std::common_type::type; - - // force conversion of From::rep -> IntermediateRep to be safe, - // even if it will never happen be narrowing in this context. - IntermediateRep count = - safe_float_conversion(from.count(), ec); - if (ec) { - return {}; - } - - // multiply with Factor::num without overflow or underflow - if (detail::const_check(Factor::num != 1)) { - constexpr auto max1 = detail::max_value() / - static_cast(Factor::num); - if (count > max1) { - ec = 1; - return {}; - } - constexpr auto min1 = std::numeric_limits::lowest() / - static_cast(Factor::num); - if (count < min1) { - ec = 1; - return {}; - } - count *= static_cast(Factor::num); - } - - // this can't go wrong, right? den>0 is checked earlier. - if (detail::const_check(Factor::den != 1)) { - using common_t = typename std::common_type::type; - count /= static_cast(Factor::den); - } - - // convert to the to type, safely - using ToRep = typename To::rep; - - const ToRep tocount = safe_float_conversion(count, ec); - if (ec) { - return {}; - } - return To{tocount}; -} -} // namespace safe_duration_cast -#endif - -// Prevents expansion of a preceding token as a function-style macro. -// Usage: f FMT_NOMACRO() -#define FMT_NOMACRO - -namespace detail { -template struct null {}; -inline auto localtime_r FMT_NOMACRO(...) -> null<> { return null<>(); } -inline auto localtime_s(...) -> null<> { return null<>(); } -inline auto gmtime_r(...) -> null<> { return null<>(); } -inline auto gmtime_s(...) -> null<> { return null<>(); } - -// It is defined here and not in ostream.h because the latter has expensive -// includes. -template class formatbuf : public Streambuf { - private: - using char_type = typename Streambuf::char_type; - using streamsize = decltype(std::declval().sputn(nullptr, 0)); - using int_type = typename Streambuf::int_type; - using traits_type = typename Streambuf::traits_type; - - buffer& buffer_; - - public: - explicit formatbuf(buffer& buf) : buffer_(buf) {} - - protected: - // The put area is always empty. This makes the implementation simpler and has - // the advantage that the streambuf and the buffer are always in sync and - // sputc never writes into uninitialized memory. A disadvantage is that each - // call to sputc always results in a (virtual) call to overflow. There is no - // disadvantage here for sputn since this always results in a call to xsputn. - - auto overflow(int_type ch) -> int_type override { - if (!traits_type::eq_int_type(ch, traits_type::eof())) - buffer_.push_back(static_cast(ch)); - return ch; - } - - auto xsputn(const char_type* s, streamsize count) -> streamsize override { - buffer_.append(s, s + count); - return count; - } -}; - -inline auto get_classic_locale() -> const std::locale& { - static const auto& locale = std::locale::classic(); - return locale; -} - -template struct codecvt_result { - static constexpr const size_t max_size = 32; - CodeUnit buf[max_size]; - CodeUnit* end; -}; - -template -void write_codecvt(codecvt_result& out, string_view in_buf, - const std::locale& loc) { -#if FMT_CLANG_VERSION -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wdeprecated" - auto& f = std::use_facet>(loc); -# pragma clang diagnostic pop -#else - auto& f = std::use_facet>(loc); -#endif - auto mb = std::mbstate_t(); - const char* from_next = nullptr; - auto result = f.in(mb, in_buf.begin(), in_buf.end(), from_next, - std::begin(out.buf), std::end(out.buf), out.end); - if (result != std::codecvt_base::ok) - FMT_THROW(format_error("failed to format time")); -} - -template -auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc) - -> OutputIt { - if (detail::use_utf8() && loc != get_classic_locale()) { - // char16_t and char32_t codecvts are broken in MSVC (linkage errors) and - // gcc-4. -#if FMT_MSC_VERSION != 0 || \ - (defined(__GLIBCXX__) && \ - (!defined(_GLIBCXX_USE_DUAL_ABI) || _GLIBCXX_USE_DUAL_ABI == 0)) - // The _GLIBCXX_USE_DUAL_ABI macro is always defined in libstdc++ from gcc-5 - // and newer. - using code_unit = wchar_t; -#else - using code_unit = char32_t; -#endif - - using unit_t = codecvt_result; - unit_t unit; - write_codecvt(unit, in, loc); - // In UTF-8 is used one to four one-byte code units. - auto u = - to_utf8>(); - if (!u.convert({unit.buf, to_unsigned(unit.end - unit.buf)})) - FMT_THROW(format_error("failed to format time")); - return copy(u.c_str(), u.c_str() + u.size(), out); - } - return copy(in.data(), in.data() + in.size(), out); -} - -template ::value)> -auto write_tm_str(OutputIt out, string_view sv, const std::locale& loc) - -> OutputIt { - codecvt_result unit; - write_codecvt(unit, sv, loc); - return copy(unit.buf, unit.end, out); -} - -template ::value)> -auto write_tm_str(OutputIt out, string_view sv, const std::locale& loc) - -> OutputIt { - return write_encoded_tm_str(out, sv, loc); -} - -template -inline void do_write(buffer& buf, const std::tm& time, - const std::locale& loc, char format, char modifier) { - auto&& format_buf = formatbuf>(buf); - auto&& os = std::basic_ostream(&format_buf); - os.imbue(loc); - const auto& facet = std::use_facet>(loc); - auto end = facet.put(os, os, Char(' '), &time, format, modifier); - if (end.failed()) FMT_THROW(format_error("failed to format time")); -} - -template ::value)> -auto write(OutputIt out, const std::tm& time, const std::locale& loc, - char format, char modifier = 0) -> OutputIt { - auto&& buf = get_buffer(out); - do_write(buf, time, loc, format, modifier); - return get_iterator(buf, out); -} - -template ::value)> -auto write(OutputIt out, const std::tm& time, const std::locale& loc, - char format, char modifier = 0) -> OutputIt { - auto&& buf = basic_memory_buffer(); - do_write(buf, time, loc, format, modifier); - return write_encoded_tm_str(out, string_view(buf.data(), buf.size()), loc); -} - -template -struct is_same_arithmetic_type - : public std::integral_constant::value && - std::is_integral::value) || - (std::is_floating_point::value && - std::is_floating_point::value)> { -}; - -template < - typename To, typename FromRep, typename FromPeriod, - FMT_ENABLE_IF(is_same_arithmetic_type::value)> -auto fmt_duration_cast(std::chrono::duration from) -> To { -#if FMT_SAFE_DURATION_CAST - // Throwing version of safe_duration_cast is only available for - // integer to integer or float to float casts. - int ec; - To to = safe_duration_cast::safe_duration_cast(from, ec); - if (ec) FMT_THROW(format_error("cannot format duration")); - return to; -#else - // Standard duration cast, may overflow. - return std::chrono::duration_cast(from); -#endif -} - -template < - typename To, typename FromRep, typename FromPeriod, - FMT_ENABLE_IF(!is_same_arithmetic_type::value)> -auto fmt_duration_cast(std::chrono::duration from) -> To { - // Mixed integer <-> float cast is not supported by safe_duration_cast. - return std::chrono::duration_cast(from); -} - -template -auto to_time_t( - std::chrono::time_point time_point) - -> std::time_t { - // Cannot use std::chrono::system_clock::to_time_t since this would first - // require a cast to std::chrono::system_clock::time_point, which could - // overflow. - return fmt_duration_cast>( - time_point.time_since_epoch()) - .count(); -} -} // namespace detail - -FMT_BEGIN_EXPORT - -/** - * Converts given time since epoch as `std::time_t` value into calendar time, - * expressed in local time. Unlike `std::localtime`, this function is - * thread-safe on most platforms. - */ -inline auto localtime(std::time_t time) -> std::tm { - struct dispatcher { - std::time_t time_; - std::tm tm_; - - dispatcher(std::time_t t) : time_(t) {} - - auto run() -> bool { - using namespace fmt::detail; - return handle(localtime_r(&time_, &tm_)); - } - - auto handle(std::tm* tm) -> bool { return tm != nullptr; } - - auto handle(detail::null<>) -> bool { - using namespace fmt::detail; - return fallback(localtime_s(&tm_, &time_)); - } - - auto fallback(int res) -> bool { return res == 0; } - -#if !FMT_MSC_VERSION - auto fallback(detail::null<>) -> bool { - using namespace fmt::detail; - std::tm* tm = std::localtime(&time_); - if (tm) tm_ = *tm; - return tm != nullptr; - } -#endif - }; - dispatcher lt(time); - // Too big time values may be unsupported. - if (!lt.run()) FMT_THROW(format_error("time_t value out of range")); - return lt.tm_; -} - -#if FMT_USE_LOCAL_TIME -template -inline auto localtime(std::chrono::local_time time) -> std::tm { - return localtime( - detail::to_time_t(std::chrono::current_zone()->to_sys(time))); -} -#endif - -/** - * Converts given time since epoch as `std::time_t` value into calendar time, - * expressed in Coordinated Universal Time (UTC). Unlike `std::gmtime`, this - * function is thread-safe on most platforms. - */ -inline auto gmtime(std::time_t time) -> std::tm { - struct dispatcher { - std::time_t time_; - std::tm tm_; - - dispatcher(std::time_t t) : time_(t) {} - - auto run() -> bool { - using namespace fmt::detail; - return handle(gmtime_r(&time_, &tm_)); - } - - auto handle(std::tm* tm) -> bool { return tm != nullptr; } - - auto handle(detail::null<>) -> bool { - using namespace fmt::detail; - return fallback(gmtime_s(&tm_, &time_)); - } - - auto fallback(int res) -> bool { return res == 0; } - -#if !FMT_MSC_VERSION - auto fallback(detail::null<>) -> bool { - std::tm* tm = std::gmtime(&time_); - if (tm) tm_ = *tm; - return tm != nullptr; - } -#endif - }; - auto gt = dispatcher(time); - // Too big time values may be unsupported. - if (!gt.run()) FMT_THROW(format_error("time_t value out of range")); - return gt.tm_; -} - -template -inline auto gmtime( - std::chrono::time_point time_point) - -> std::tm { - return gmtime(detail::to_time_t(time_point)); -} - -namespace detail { - -// Writes two-digit numbers a, b and c separated by sep to buf. -// The method by Pavel Novikov based on -// https://johnnylee-sde.github.io/Fast-unsigned-integer-to-time-string/. -inline void write_digit2_separated(char* buf, unsigned a, unsigned b, - unsigned c, char sep) { - unsigned long long digits = - a | (b << 24) | (static_cast(c) << 48); - // Convert each value to BCD. - // We have x = a * 10 + b and we want to convert it to BCD y = a * 16 + b. - // The difference is - // y - x = a * 6 - // a can be found from x: - // a = floor(x / 10) - // then - // y = x + a * 6 = x + floor(x / 10) * 6 - // floor(x / 10) is (x * 205) >> 11 (needs 16 bits). - digits += (((digits * 205) >> 11) & 0x000f00000f00000f) * 6; - // Put low nibbles to high bytes and high nibbles to low bytes. - digits = ((digits & 0x00f00000f00000f0) >> 4) | - ((digits & 0x000f00000f00000f) << 8); - auto usep = static_cast(sep); - // Add ASCII '0' to each digit byte and insert separators. - digits |= 0x3030003030003030 | (usep << 16) | (usep << 40); - - constexpr const size_t len = 8; - if (const_check(is_big_endian())) { - char tmp[len]; - std::memcpy(tmp, &digits, len); - std::reverse_copy(tmp, tmp + len, buf); - } else { - std::memcpy(buf, &digits, len); - } -} - -template -FMT_CONSTEXPR inline auto get_units() -> const char* { - if (std::is_same::value) return "as"; - if (std::is_same::value) return "fs"; - if (std::is_same::value) return "ps"; - if (std::is_same::value) return "ns"; - if (std::is_same::value) return "µs"; - if (std::is_same::value) return "ms"; - if (std::is_same::value) return "cs"; - if (std::is_same::value) return "ds"; - if (std::is_same>::value) return "s"; - if (std::is_same::value) return "das"; - if (std::is_same::value) return "hs"; - if (std::is_same::value) return "ks"; - if (std::is_same::value) return "Ms"; - if (std::is_same::value) return "Gs"; - if (std::is_same::value) return "Ts"; - if (std::is_same::value) return "Ps"; - if (std::is_same::value) return "Es"; - if (std::is_same>::value) return "min"; - if (std::is_same>::value) return "h"; - if (std::is_same>::value) return "d"; - return nullptr; -} - -enum class numeric_system { - standard, - // Alternative numeric system, e.g. 十二 instead of 12 in ja_JP locale. - alternative -}; - -// Glibc extensions for formatting numeric values. -enum class pad_type { - // Pad a numeric result string with zeros (the default). - zero, - // Do not pad a numeric result string. - none, - // Pad a numeric result string with spaces. - space, -}; - -template -auto write_padding(OutputIt out, pad_type pad, int width) -> OutputIt { - if (pad == pad_type::none) return out; - return detail::fill_n(out, width, pad == pad_type::space ? ' ' : '0'); -} - -template -auto write_padding(OutputIt out, pad_type pad) -> OutputIt { - if (pad != pad_type::none) *out++ = pad == pad_type::space ? ' ' : '0'; - return out; -} - -// Parses a put_time-like format string and invokes handler actions. -template -FMT_CONSTEXPR auto parse_chrono_format(const Char* begin, const Char* end, - Handler&& handler) -> const Char* { - if (begin == end || *begin == '}') return begin; - if (*begin != '%') FMT_THROW(format_error("invalid format")); - auto ptr = begin; - while (ptr != end) { - pad_type pad = pad_type::zero; - auto c = *ptr; - if (c == '}') break; - if (c != '%') { - ++ptr; - continue; - } - if (begin != ptr) handler.on_text(begin, ptr); - ++ptr; // consume '%' - if (ptr == end) FMT_THROW(format_error("invalid format")); - c = *ptr; - switch (c) { - case '_': - pad = pad_type::space; - ++ptr; - break; - case '-': - pad = pad_type::none; - ++ptr; - break; - } - if (ptr == end) FMT_THROW(format_error("invalid format")); - c = *ptr++; - switch (c) { - case '%': - handler.on_text(ptr - 1, ptr); - break; - case 'n': { - const Char newline[] = {'\n'}; - handler.on_text(newline, newline + 1); - break; - } - case 't': { - const Char tab[] = {'\t'}; - handler.on_text(tab, tab + 1); - break; - } - // Year: - case 'Y': - handler.on_year(numeric_system::standard); - break; - case 'y': - handler.on_short_year(numeric_system::standard); - break; - case 'C': - handler.on_century(numeric_system::standard); - break; - case 'G': - handler.on_iso_week_based_year(); - break; - case 'g': - handler.on_iso_week_based_short_year(); - break; - // Day of the week: - case 'a': - handler.on_abbr_weekday(); - break; - case 'A': - handler.on_full_weekday(); - break; - case 'w': - handler.on_dec0_weekday(numeric_system::standard); - break; - case 'u': - handler.on_dec1_weekday(numeric_system::standard); - break; - // Month: - case 'b': - case 'h': - handler.on_abbr_month(); - break; - case 'B': - handler.on_full_month(); - break; - case 'm': - handler.on_dec_month(numeric_system::standard); - break; - // Day of the year/month: - case 'U': - handler.on_dec0_week_of_year(numeric_system::standard, pad); - break; - case 'W': - handler.on_dec1_week_of_year(numeric_system::standard, pad); - break; - case 'V': - handler.on_iso_week_of_year(numeric_system::standard, pad); - break; - case 'j': - handler.on_day_of_year(); - break; - case 'd': - handler.on_day_of_month(numeric_system::standard, pad); - break; - case 'e': - handler.on_day_of_month(numeric_system::standard, pad_type::space); - break; - // Hour, minute, second: - case 'H': - handler.on_24_hour(numeric_system::standard, pad); - break; - case 'I': - handler.on_12_hour(numeric_system::standard, pad); - break; - case 'M': - handler.on_minute(numeric_system::standard, pad); - break; - case 'S': - handler.on_second(numeric_system::standard, pad); - break; - // Other: - case 'c': - handler.on_datetime(numeric_system::standard); - break; - case 'x': - handler.on_loc_date(numeric_system::standard); - break; - case 'X': - handler.on_loc_time(numeric_system::standard); - break; - case 'D': - handler.on_us_date(); - break; - case 'F': - handler.on_iso_date(); - break; - case 'r': - handler.on_12_hour_time(); - break; - case 'R': - handler.on_24_hour_time(); - break; - case 'T': - handler.on_iso_time(); - break; - case 'p': - handler.on_am_pm(); - break; - case 'Q': - handler.on_duration_value(); - break; - case 'q': - handler.on_duration_unit(); - break; - case 'z': - handler.on_utc_offset(numeric_system::standard); - break; - case 'Z': - handler.on_tz_name(); - break; - // Alternative representation: - case 'E': { - if (ptr == end) FMT_THROW(format_error("invalid format")); - c = *ptr++; - switch (c) { - case 'Y': - handler.on_year(numeric_system::alternative); - break; - case 'y': - handler.on_offset_year(); - break; - case 'C': - handler.on_century(numeric_system::alternative); - break; - case 'c': - handler.on_datetime(numeric_system::alternative); - break; - case 'x': - handler.on_loc_date(numeric_system::alternative); - break; - case 'X': - handler.on_loc_time(numeric_system::alternative); - break; - case 'z': - handler.on_utc_offset(numeric_system::alternative); - break; - default: - FMT_THROW(format_error("invalid format")); - } - break; - } - case 'O': - if (ptr == end) FMT_THROW(format_error("invalid format")); - c = *ptr++; - switch (c) { - case 'y': - handler.on_short_year(numeric_system::alternative); - break; - case 'm': - handler.on_dec_month(numeric_system::alternative); - break; - case 'U': - handler.on_dec0_week_of_year(numeric_system::alternative, pad); - break; - case 'W': - handler.on_dec1_week_of_year(numeric_system::alternative, pad); - break; - case 'V': - handler.on_iso_week_of_year(numeric_system::alternative, pad); - break; - case 'd': - handler.on_day_of_month(numeric_system::alternative, pad); - break; - case 'e': - handler.on_day_of_month(numeric_system::alternative, pad_type::space); - break; - case 'w': - handler.on_dec0_weekday(numeric_system::alternative); - break; - case 'u': - handler.on_dec1_weekday(numeric_system::alternative); - break; - case 'H': - handler.on_24_hour(numeric_system::alternative, pad); - break; - case 'I': - handler.on_12_hour(numeric_system::alternative, pad); - break; - case 'M': - handler.on_minute(numeric_system::alternative, pad); - break; - case 'S': - handler.on_second(numeric_system::alternative, pad); - break; - case 'z': - handler.on_utc_offset(numeric_system::alternative); - break; - default: - FMT_THROW(format_error("invalid format")); - } - break; - default: - FMT_THROW(format_error("invalid format")); - } - begin = ptr; - } - if (begin != ptr) handler.on_text(begin, ptr); - return ptr; -} - -template struct null_chrono_spec_handler { - FMT_CONSTEXPR void unsupported() { - static_cast(this)->unsupported(); - } - FMT_CONSTEXPR void on_year(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_short_year(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_offset_year() { unsupported(); } - FMT_CONSTEXPR void on_century(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_iso_week_based_year() { unsupported(); } - FMT_CONSTEXPR void on_iso_week_based_short_year() { unsupported(); } - FMT_CONSTEXPR void on_abbr_weekday() { unsupported(); } - FMT_CONSTEXPR void on_full_weekday() { unsupported(); } - FMT_CONSTEXPR void on_dec0_weekday(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_dec1_weekday(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_abbr_month() { unsupported(); } - FMT_CONSTEXPR void on_full_month() { unsupported(); } - FMT_CONSTEXPR void on_dec_month(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_dec0_week_of_year(numeric_system, pad_type) { - unsupported(); - } - FMT_CONSTEXPR void on_dec1_week_of_year(numeric_system, pad_type) { - unsupported(); - } - FMT_CONSTEXPR void on_iso_week_of_year(numeric_system, pad_type) { - unsupported(); - } - FMT_CONSTEXPR void on_day_of_year() { unsupported(); } - FMT_CONSTEXPR void on_day_of_month(numeric_system, pad_type) { - unsupported(); - } - FMT_CONSTEXPR void on_24_hour(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_12_hour(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_minute(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_second(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_datetime(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_loc_date(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_loc_time(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_us_date() { unsupported(); } - FMT_CONSTEXPR void on_iso_date() { unsupported(); } - FMT_CONSTEXPR void on_12_hour_time() { unsupported(); } - FMT_CONSTEXPR void on_24_hour_time() { unsupported(); } - FMT_CONSTEXPR void on_iso_time() { unsupported(); } - FMT_CONSTEXPR void on_am_pm() { unsupported(); } - FMT_CONSTEXPR void on_duration_value() { unsupported(); } - FMT_CONSTEXPR void on_duration_unit() { unsupported(); } - FMT_CONSTEXPR void on_utc_offset(numeric_system) { unsupported(); } - FMT_CONSTEXPR void on_tz_name() { unsupported(); } -}; - -struct tm_format_checker : null_chrono_spec_handler { - FMT_NORETURN void unsupported() { FMT_THROW(format_error("no format")); } - - template - FMT_CONSTEXPR void on_text(const Char*, const Char*) {} - FMT_CONSTEXPR void on_year(numeric_system) {} - FMT_CONSTEXPR void on_short_year(numeric_system) {} - FMT_CONSTEXPR void on_offset_year() {} - FMT_CONSTEXPR void on_century(numeric_system) {} - FMT_CONSTEXPR void on_iso_week_based_year() {} - FMT_CONSTEXPR void on_iso_week_based_short_year() {} - FMT_CONSTEXPR void on_abbr_weekday() {} - FMT_CONSTEXPR void on_full_weekday() {} - FMT_CONSTEXPR void on_dec0_weekday(numeric_system) {} - FMT_CONSTEXPR void on_dec1_weekday(numeric_system) {} - FMT_CONSTEXPR void on_abbr_month() {} - FMT_CONSTEXPR void on_full_month() {} - FMT_CONSTEXPR void on_dec_month(numeric_system) {} - FMT_CONSTEXPR void on_dec0_week_of_year(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_dec1_week_of_year(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_iso_week_of_year(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_day_of_year() {} - FMT_CONSTEXPR void on_day_of_month(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_second(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_datetime(numeric_system) {} - FMT_CONSTEXPR void on_loc_date(numeric_system) {} - FMT_CONSTEXPR void on_loc_time(numeric_system) {} - FMT_CONSTEXPR void on_us_date() {} - FMT_CONSTEXPR void on_iso_date() {} - FMT_CONSTEXPR void on_12_hour_time() {} - FMT_CONSTEXPR void on_24_hour_time() {} - FMT_CONSTEXPR void on_iso_time() {} - FMT_CONSTEXPR void on_am_pm() {} - FMT_CONSTEXPR void on_utc_offset(numeric_system) {} - FMT_CONSTEXPR void on_tz_name() {} -}; - -inline auto tm_wday_full_name(int wday) -> const char* { - static constexpr const char* full_name_list[] = { - "Sunday", "Monday", "Tuesday", "Wednesday", - "Thursday", "Friday", "Saturday"}; - return wday >= 0 && wday <= 6 ? full_name_list[wday] : "?"; -} -inline auto tm_wday_short_name(int wday) -> const char* { - static constexpr const char* short_name_list[] = {"Sun", "Mon", "Tue", "Wed", - "Thu", "Fri", "Sat"}; - return wday >= 0 && wday <= 6 ? short_name_list[wday] : "???"; -} - -inline auto tm_mon_full_name(int mon) -> const char* { - static constexpr const char* full_name_list[] = { - "January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", "December"}; - return mon >= 0 && mon <= 11 ? full_name_list[mon] : "?"; -} -inline auto tm_mon_short_name(int mon) -> const char* { - static constexpr const char* short_name_list[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", - }; - return mon >= 0 && mon <= 11 ? short_name_list[mon] : "???"; -} - -template -struct has_member_data_tm_gmtoff : std::false_type {}; -template -struct has_member_data_tm_gmtoff> - : std::true_type {}; - -template -struct has_member_data_tm_zone : std::false_type {}; -template -struct has_member_data_tm_zone> - : std::true_type {}; - -#if FMT_USE_TZSET -inline void tzset_once() { - static bool init = []() -> bool { - _tzset(); - return true; - }(); - ignore_unused(init); -} -#endif - -// Converts value to Int and checks that it's in the range [0, upper). -template ::value)> -inline auto to_nonnegative_int(T value, Int upper) -> Int { - if (!std::is_unsigned::value && - (value < 0 || to_unsigned(value) > to_unsigned(upper))) { - FMT_THROW(fmt::format_error("chrono value is out of range")); - } - return static_cast(value); -} -template ::value)> -inline auto to_nonnegative_int(T value, Int upper) -> Int { - auto int_value = static_cast(value); - if (int_value < 0 || value > static_cast(upper)) - FMT_THROW(format_error("invalid value")); - return int_value; -} - -constexpr auto pow10(std::uint32_t n) -> long long { - return n == 0 ? 1 : 10 * pow10(n - 1); -} - -// Counts the number of fractional digits in the range [0, 18] according to the -// C++20 spec. If more than 18 fractional digits are required then returns 6 for -// microseconds precision. -template () / 10)> -struct count_fractional_digits { - static constexpr int value = - Num % Den == 0 ? N : count_fractional_digits::value; -}; - -// Base case that doesn't instantiate any more templates -// in order to avoid overflow. -template -struct count_fractional_digits { - static constexpr int value = (Num % Den == 0) ? N : 6; -}; - -// Format subseconds which are given as an integer type with an appropriate -// number of digits. -template -void write_fractional_seconds(OutputIt& out, Duration d, int precision = -1) { - constexpr auto num_fractional_digits = - count_fractional_digits::value; - - using subsecond_precision = std::chrono::duration< - typename std::common_type::type, - std::ratio<1, detail::pow10(num_fractional_digits)>>; - - const auto fractional = d - fmt_duration_cast(d); - const auto subseconds = - std::chrono::treat_as_floating_point< - typename subsecond_precision::rep>::value - ? fractional.count() - : fmt_duration_cast(fractional).count(); - auto n = static_cast>(subseconds); - const int num_digits = detail::count_digits(n); - - int leading_zeroes = (std::max)(0, num_fractional_digits - num_digits); - if (precision < 0) { - FMT_ASSERT(!std::is_floating_point::value, ""); - if (std::ratio_less::value) { - *out++ = '.'; - out = detail::fill_n(out, leading_zeroes, '0'); - out = format_decimal(out, n, num_digits).end; - } - } else if (precision > 0) { - *out++ = '.'; - leading_zeroes = (std::min)(leading_zeroes, precision); - int remaining = precision - leading_zeroes; - out = detail::fill_n(out, leading_zeroes, '0'); - if (remaining < num_digits) { - int num_truncated_digits = num_digits - remaining; - n /= to_unsigned(detail::pow10(to_unsigned(num_truncated_digits))); - if (n) { - out = format_decimal(out, n, remaining).end; - } - return; - } - if (n) { - out = format_decimal(out, n, num_digits).end; - remaining -= num_digits; - } - out = detail::fill_n(out, remaining, '0'); - } -} - -// Format subseconds which are given as a floating point type with an -// appropriate number of digits. We cannot pass the Duration here, as we -// explicitly need to pass the Rep value in the chrono_formatter. -template -void write_floating_seconds(memory_buffer& buf, Duration duration, - int num_fractional_digits = -1) { - using rep = typename Duration::rep; - FMT_ASSERT(std::is_floating_point::value, ""); - - auto val = duration.count(); - - if (num_fractional_digits < 0) { - // For `std::round` with fallback to `round`: - // On some toolchains `std::round` is not available (e.g. GCC 6). - using namespace std; - num_fractional_digits = - count_fractional_digits::value; - if (num_fractional_digits < 6 && static_cast(round(val)) != val) - num_fractional_digits = 6; - } - - fmt::format_to(std::back_inserter(buf), FMT_STRING("{:.{}f}"), - std::fmod(val * static_cast(Duration::period::num) / - static_cast(Duration::period::den), - static_cast(60)), - num_fractional_digits); -} - -template -class tm_writer { - private: - static constexpr int days_per_week = 7; - - const std::locale& loc_; - const bool is_classic_; - OutputIt out_; - const Duration* subsecs_; - const std::tm& tm_; - - auto tm_sec() const noexcept -> int { - FMT_ASSERT(tm_.tm_sec >= 0 && tm_.tm_sec <= 61, ""); - return tm_.tm_sec; - } - auto tm_min() const noexcept -> int { - FMT_ASSERT(tm_.tm_min >= 0 && tm_.tm_min <= 59, ""); - return tm_.tm_min; - } - auto tm_hour() const noexcept -> int { - FMT_ASSERT(tm_.tm_hour >= 0 && tm_.tm_hour <= 23, ""); - return tm_.tm_hour; - } - auto tm_mday() const noexcept -> int { - FMT_ASSERT(tm_.tm_mday >= 1 && tm_.tm_mday <= 31, ""); - return tm_.tm_mday; - } - auto tm_mon() const noexcept -> int { - FMT_ASSERT(tm_.tm_mon >= 0 && tm_.tm_mon <= 11, ""); - return tm_.tm_mon; - } - auto tm_year() const noexcept -> long long { return 1900ll + tm_.tm_year; } - auto tm_wday() const noexcept -> int { - FMT_ASSERT(tm_.tm_wday >= 0 && tm_.tm_wday <= 6, ""); - return tm_.tm_wday; - } - auto tm_yday() const noexcept -> int { - FMT_ASSERT(tm_.tm_yday >= 0 && tm_.tm_yday <= 365, ""); - return tm_.tm_yday; - } - - auto tm_hour12() const noexcept -> int { - const auto h = tm_hour(); - const auto z = h < 12 ? h : h - 12; - return z == 0 ? 12 : z; - } - - // POSIX and the C Standard are unclear or inconsistent about what %C and %y - // do if the year is negative or exceeds 9999. Use the convention that %C - // concatenated with %y yields the same output as %Y, and that %Y contains at - // least 4 characters, with more only if necessary. - auto split_year_lower(long long year) const noexcept -> int { - auto l = year % 100; - if (l < 0) l = -l; // l in [0, 99] - return static_cast(l); - } - - // Algorithm: https://en.wikipedia.org/wiki/ISO_week_date. - auto iso_year_weeks(long long curr_year) const noexcept -> int { - const auto prev_year = curr_year - 1; - const auto curr_p = - (curr_year + curr_year / 4 - curr_year / 100 + curr_year / 400) % - days_per_week; - const auto prev_p = - (prev_year + prev_year / 4 - prev_year / 100 + prev_year / 400) % - days_per_week; - return 52 + ((curr_p == 4 || prev_p == 3) ? 1 : 0); - } - auto iso_week_num(int tm_yday, int tm_wday) const noexcept -> int { - return (tm_yday + 11 - (tm_wday == 0 ? days_per_week : tm_wday)) / - days_per_week; - } - auto tm_iso_week_year() const noexcept -> long long { - const auto year = tm_year(); - const auto w = iso_week_num(tm_yday(), tm_wday()); - if (w < 1) return year - 1; - if (w > iso_year_weeks(year)) return year + 1; - return year; - } - auto tm_iso_week_of_year() const noexcept -> int { - const auto year = tm_year(); - const auto w = iso_week_num(tm_yday(), tm_wday()); - if (w < 1) return iso_year_weeks(year - 1); - if (w > iso_year_weeks(year)) return 1; - return w; - } - - void write1(int value) { - *out_++ = static_cast('0' + to_unsigned(value) % 10); - } - void write2(int value) { - const char* d = digits2(to_unsigned(value) % 100); - *out_++ = *d++; - *out_++ = *d; - } - void write2(int value, pad_type pad) { - unsigned int v = to_unsigned(value) % 100; - if (v >= 10) { - const char* d = digits2(v); - *out_++ = *d++; - *out_++ = *d; - } else { - out_ = detail::write_padding(out_, pad); - *out_++ = static_cast('0' + v); - } - } - - void write_year_extended(long long year) { - // At least 4 characters. - int width = 4; - if (year < 0) { - *out_++ = '-'; - year = 0 - year; - --width; - } - uint32_or_64_or_128_t n = to_unsigned(year); - const int num_digits = count_digits(n); - if (width > num_digits) - out_ = detail::fill_n(out_, width - num_digits, '0'); - out_ = format_decimal(out_, n, num_digits).end; - } - void write_year(long long year) { - if (year >= 0 && year < 10000) { - write2(static_cast(year / 100)); - write2(static_cast(year % 100)); - } else { - write_year_extended(year); - } - } - - void write_utc_offset(long offset, numeric_system ns) { - if (offset < 0) { - *out_++ = '-'; - offset = -offset; - } else { - *out_++ = '+'; - } - offset /= 60; - write2(static_cast(offset / 60)); - if (ns != numeric_system::standard) *out_++ = ':'; - write2(static_cast(offset % 60)); - } - template ::value)> - void format_utc_offset_impl(const T& tm, numeric_system ns) { - write_utc_offset(tm.tm_gmtoff, ns); - } - template ::value)> - void format_utc_offset_impl(const T& tm, numeric_system ns) { -#if defined(_WIN32) && defined(_UCRT) -# if FMT_USE_TZSET - tzset_once(); -# endif - long offset = 0; - _get_timezone(&offset); - if (tm.tm_isdst) { - long dstbias = 0; - _get_dstbias(&dstbias); - offset += dstbias; - } - write_utc_offset(-offset, ns); -#else - if (ns == numeric_system::standard) return format_localized('z'); - - // Extract timezone offset from timezone conversion functions. - std::tm gtm = tm; - std::time_t gt = std::mktime(>m); - std::tm ltm = gmtime(gt); - std::time_t lt = std::mktime(<m); - long offset = gt - lt; - write_utc_offset(offset, ns); -#endif - } - - template ::value)> - void format_tz_name_impl(const T& tm) { - if (is_classic_) - out_ = write_tm_str(out_, tm.tm_zone, loc_); - else - format_localized('Z'); - } - template ::value)> - void format_tz_name_impl(const T&) { - format_localized('Z'); - } - - void format_localized(char format, char modifier = 0) { - out_ = write(out_, tm_, loc_, format, modifier); - } - - public: - tm_writer(const std::locale& loc, OutputIt out, const std::tm& tm, - const Duration* subsecs = nullptr) - : loc_(loc), - is_classic_(loc_ == get_classic_locale()), - out_(out), - subsecs_(subsecs), - tm_(tm) {} - - auto out() const -> OutputIt { return out_; } - - FMT_CONSTEXPR void on_text(const Char* begin, const Char* end) { - out_ = copy(begin, end, out_); - } - - void on_abbr_weekday() { - if (is_classic_) - out_ = write(out_, tm_wday_short_name(tm_wday())); - else - format_localized('a'); - } - void on_full_weekday() { - if (is_classic_) - out_ = write(out_, tm_wday_full_name(tm_wday())); - else - format_localized('A'); - } - void on_dec0_weekday(numeric_system ns) { - if (is_classic_ || ns == numeric_system::standard) return write1(tm_wday()); - format_localized('w', 'O'); - } - void on_dec1_weekday(numeric_system ns) { - if (is_classic_ || ns == numeric_system::standard) { - auto wday = tm_wday(); - write1(wday == 0 ? days_per_week : wday); - } else { - format_localized('u', 'O'); - } - } - - void on_abbr_month() { - if (is_classic_) - out_ = write(out_, tm_mon_short_name(tm_mon())); - else - format_localized('b'); - } - void on_full_month() { - if (is_classic_) - out_ = write(out_, tm_mon_full_name(tm_mon())); - else - format_localized('B'); - } - - void on_datetime(numeric_system ns) { - if (is_classic_) { - on_abbr_weekday(); - *out_++ = ' '; - on_abbr_month(); - *out_++ = ' '; - on_day_of_month(numeric_system::standard, pad_type::space); - *out_++ = ' '; - on_iso_time(); - *out_++ = ' '; - on_year(numeric_system::standard); - } else { - format_localized('c', ns == numeric_system::standard ? '\0' : 'E'); - } - } - void on_loc_date(numeric_system ns) { - if (is_classic_) - on_us_date(); - else - format_localized('x', ns == numeric_system::standard ? '\0' : 'E'); - } - void on_loc_time(numeric_system ns) { - if (is_classic_) - on_iso_time(); - else - format_localized('X', ns == numeric_system::standard ? '\0' : 'E'); - } - void on_us_date() { - char buf[8]; - write_digit2_separated(buf, to_unsigned(tm_mon() + 1), - to_unsigned(tm_mday()), - to_unsigned(split_year_lower(tm_year())), '/'); - out_ = copy(std::begin(buf), std::end(buf), out_); - } - void on_iso_date() { - auto year = tm_year(); - char buf[10]; - size_t offset = 0; - if (year >= 0 && year < 10000) { - copy2(buf, digits2(static_cast(year / 100))); - } else { - offset = 4; - write_year_extended(year); - year = 0; - } - write_digit2_separated(buf + 2, static_cast(year % 100), - to_unsigned(tm_mon() + 1), to_unsigned(tm_mday()), - '-'); - out_ = copy(std::begin(buf) + offset, std::end(buf), out_); - } - - void on_utc_offset(numeric_system ns) { format_utc_offset_impl(tm_, ns); } - void on_tz_name() { format_tz_name_impl(tm_); } - - void on_year(numeric_system ns) { - if (is_classic_ || ns == numeric_system::standard) - return write_year(tm_year()); - format_localized('Y', 'E'); - } - void on_short_year(numeric_system ns) { - if (is_classic_ || ns == numeric_system::standard) - return write2(split_year_lower(tm_year())); - format_localized('y', 'O'); - } - void on_offset_year() { - if (is_classic_) return write2(split_year_lower(tm_year())); - format_localized('y', 'E'); - } - - void on_century(numeric_system ns) { - if (is_classic_ || ns == numeric_system::standard) { - auto year = tm_year(); - auto upper = year / 100; - if (year >= -99 && year < 0) { - // Zero upper on negative year. - *out_++ = '-'; - *out_++ = '0'; - } else if (upper >= 0 && upper < 100) { - write2(static_cast(upper)); - } else { - out_ = write(out_, upper); - } - } else { - format_localized('C', 'E'); - } - } - - void on_dec_month(numeric_system ns) { - if (is_classic_ || ns == numeric_system::standard) - return write2(tm_mon() + 1); - format_localized('m', 'O'); - } - - void on_dec0_week_of_year(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) - return write2((tm_yday() + days_per_week - tm_wday()) / days_per_week, - pad); - format_localized('U', 'O'); - } - void on_dec1_week_of_year(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) { - auto wday = tm_wday(); - write2((tm_yday() + days_per_week - - (wday == 0 ? (days_per_week - 1) : (wday - 1))) / - days_per_week, - pad); - } else { - format_localized('W', 'O'); - } - } - void on_iso_week_of_year(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) - return write2(tm_iso_week_of_year(), pad); - format_localized('V', 'O'); - } - - void on_iso_week_based_year() { write_year(tm_iso_week_year()); } - void on_iso_week_based_short_year() { - write2(split_year_lower(tm_iso_week_year())); - } - - void on_day_of_year() { - auto yday = tm_yday() + 1; - write1(yday / 100); - write2(yday % 100); - } - void on_day_of_month(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) - return write2(tm_mday(), pad); - format_localized('d', 'O'); - } - - void on_24_hour(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) - return write2(tm_hour(), pad); - format_localized('H', 'O'); - } - void on_12_hour(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) - return write2(tm_hour12(), pad); - format_localized('I', 'O'); - } - void on_minute(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) - return write2(tm_min(), pad); - format_localized('M', 'O'); - } - - void on_second(numeric_system ns, pad_type pad) { - if (is_classic_ || ns == numeric_system::standard) { - write2(tm_sec(), pad); - if (subsecs_) { - if (std::is_floating_point::value) { - auto buf = memory_buffer(); - write_floating_seconds(buf, *subsecs_); - if (buf.size() > 1) { - // Remove the leading "0", write something like ".123". - out_ = std::copy(buf.begin() + 1, buf.end(), out_); - } - } else { - write_fractional_seconds(out_, *subsecs_); - } - } - } else { - // Currently no formatting of subseconds when a locale is set. - format_localized('S', 'O'); - } - } - - void on_12_hour_time() { - if (is_classic_) { - char buf[8]; - write_digit2_separated(buf, to_unsigned(tm_hour12()), - to_unsigned(tm_min()), to_unsigned(tm_sec()), ':'); - out_ = copy(std::begin(buf), std::end(buf), out_); - *out_++ = ' '; - on_am_pm(); - } else { - format_localized('r'); - } - } - void on_24_hour_time() { - write2(tm_hour()); - *out_++ = ':'; - write2(tm_min()); - } - void on_iso_time() { - on_24_hour_time(); - *out_++ = ':'; - on_second(numeric_system::standard, pad_type::zero); - } - - void on_am_pm() { - if (is_classic_) { - *out_++ = tm_hour() < 12 ? 'A' : 'P'; - *out_++ = 'M'; - } else { - format_localized('p'); - } - } - - // These apply to chrono durations but not tm. - void on_duration_value() {} - void on_duration_unit() {} -}; - -struct chrono_format_checker : null_chrono_spec_handler { - bool has_precision_integral = false; - - FMT_NORETURN void unsupported() { FMT_THROW(format_error("no date")); } - - template - FMT_CONSTEXPR void on_text(const Char*, const Char*) {} - FMT_CONSTEXPR void on_day_of_year() {} - FMT_CONSTEXPR void on_24_hour(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_12_hour(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_minute(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_second(numeric_system, pad_type) {} - FMT_CONSTEXPR void on_12_hour_time() {} - FMT_CONSTEXPR void on_24_hour_time() {} - FMT_CONSTEXPR void on_iso_time() {} - FMT_CONSTEXPR void on_am_pm() {} - FMT_CONSTEXPR void on_duration_value() const { - if (has_precision_integral) { - FMT_THROW(format_error("precision not allowed for this argument type")); - } - } - FMT_CONSTEXPR void on_duration_unit() {} -}; - -template ::value&& has_isfinite::value)> -inline auto isfinite(T) -> bool { - return true; -} - -template ::value)> -inline auto mod(T x, int y) -> T { - return x % static_cast(y); -} -template ::value)> -inline auto mod(T x, int y) -> T { - return std::fmod(x, static_cast(y)); -} - -// If T is an integral type, maps T to its unsigned counterpart, otherwise -// leaves it unchanged (unlike std::make_unsigned). -template ::value> -struct make_unsigned_or_unchanged { - using type = T; -}; - -template struct make_unsigned_or_unchanged { - using type = typename std::make_unsigned::type; -}; - -template ::value)> -inline auto get_milliseconds(std::chrono::duration d) - -> std::chrono::duration { - // this may overflow and/or the result may not fit in the - // target type. -#if FMT_SAFE_DURATION_CAST - using CommonSecondsType = - typename std::common_type::type; - const auto d_as_common = fmt_duration_cast(d); - const auto d_as_whole_seconds = - fmt_duration_cast(d_as_common); - // this conversion should be nonproblematic - const auto diff = d_as_common - d_as_whole_seconds; - const auto ms = - fmt_duration_cast>(diff); - return ms; -#else - auto s = fmt_duration_cast(d); - return fmt_duration_cast(d - s); -#endif -} - -template ::value)> -auto format_duration_value(OutputIt out, Rep val, int) -> OutputIt { - return write(out, val); -} - -template ::value)> -auto format_duration_value(OutputIt out, Rep val, int precision) -> OutputIt { - auto specs = format_specs(); - specs.precision = precision; - specs.type = - precision >= 0 ? presentation_type::fixed : presentation_type::general; - return write(out, val, specs); -} - -template -auto copy_unit(string_view unit, OutputIt out, Char) -> OutputIt { - return std::copy(unit.begin(), unit.end(), out); -} - -template -auto copy_unit(string_view unit, OutputIt out, wchar_t) -> OutputIt { - // This works when wchar_t is UTF-32 because units only contain characters - // that have the same representation in UTF-16 and UTF-32. - utf8_to_utf16 u(unit); - return std::copy(u.c_str(), u.c_str() + u.size(), out); -} - -template -auto format_duration_unit(OutputIt out) -> OutputIt { - if (const char* unit = get_units()) - return copy_unit(string_view(unit), out, Char()); - *out++ = '['; - out = write(out, Period::num); - if (const_check(Period::den != 1)) { - *out++ = '/'; - out = write(out, Period::den); - } - *out++ = ']'; - *out++ = 's'; - return out; -} - -class get_locale { - private: - union { - std::locale locale_; - }; - bool has_locale_ = false; - - public: - get_locale(bool localized, locale_ref loc) : has_locale_(localized) { -#ifndef FMT_STATIC_THOUSANDS_SEPARATOR - if (localized) - ::new (&locale_) std::locale(loc.template get()); -#endif - } - ~get_locale() { - if (has_locale_) locale_.~locale(); - } - operator const std::locale&() const { - return has_locale_ ? locale_ : get_classic_locale(); - } -}; - -template -struct chrono_formatter { - FormatContext& context; - OutputIt out; - int precision; - bool localized = false; - // rep is unsigned to avoid overflow. - using rep = - conditional_t::value && sizeof(Rep) < sizeof(int), - unsigned, typename make_unsigned_or_unchanged::type>; - rep val; - using seconds = std::chrono::duration; - seconds s; - using milliseconds = std::chrono::duration; - bool negative; - - using char_type = typename FormatContext::char_type; - using tm_writer_type = tm_writer; - - chrono_formatter(FormatContext& ctx, OutputIt o, - std::chrono::duration d) - : context(ctx), - out(o), - val(static_cast(d.count())), - negative(false) { - if (d.count() < 0) { - val = 0 - val; - negative = true; - } - - // this may overflow and/or the result may not fit in the - // target type. - // might need checked conversion (rep!=Rep) - s = fmt_duration_cast(std::chrono::duration(val)); - } - - // returns true if nan or inf, writes to out. - auto handle_nan_inf() -> bool { - if (isfinite(val)) { - return false; - } - if (isnan(val)) { - write_nan(); - return true; - } - // must be +-inf - if (val > 0) { - write_pinf(); - } else { - write_ninf(); - } - return true; - } - - auto days() const -> Rep { return static_cast(s.count() / 86400); } - auto hour() const -> Rep { - return static_cast(mod((s.count() / 3600), 24)); - } - - auto hour12() const -> Rep { - Rep hour = static_cast(mod((s.count() / 3600), 12)); - return hour <= 0 ? 12 : hour; - } - - auto minute() const -> Rep { - return static_cast(mod((s.count() / 60), 60)); - } - auto second() const -> Rep { return static_cast(mod(s.count(), 60)); } - - auto time() const -> std::tm { - auto time = std::tm(); - time.tm_hour = to_nonnegative_int(hour(), 24); - time.tm_min = to_nonnegative_int(minute(), 60); - time.tm_sec = to_nonnegative_int(second(), 60); - return time; - } - - void write_sign() { - if (negative) { - *out++ = '-'; - negative = false; - } - } - - void write(Rep value, int width, pad_type pad = pad_type::zero) { - write_sign(); - if (isnan(value)) return write_nan(); - uint32_or_64_or_128_t n = - to_unsigned(to_nonnegative_int(value, max_value())); - int num_digits = detail::count_digits(n); - if (width > num_digits) { - out = detail::write_padding(out, pad, width - num_digits); - } - out = format_decimal(out, n, num_digits).end; - } - - void write_nan() { std::copy_n("nan", 3, out); } - void write_pinf() { std::copy_n("inf", 3, out); } - void write_ninf() { std::copy_n("-inf", 4, out); } - - template - void format_tm(const tm& time, Callback cb, Args... args) { - if (isnan(val)) return write_nan(); - get_locale loc(localized, context.locale()); - auto w = tm_writer_type(loc, out, time); - (w.*cb)(args...); - out = w.out(); - } - - void on_text(const char_type* begin, const char_type* end) { - std::copy(begin, end, out); - } - - // These are not implemented because durations don't have date information. - void on_abbr_weekday() {} - void on_full_weekday() {} - void on_dec0_weekday(numeric_system) {} - void on_dec1_weekday(numeric_system) {} - void on_abbr_month() {} - void on_full_month() {} - void on_datetime(numeric_system) {} - void on_loc_date(numeric_system) {} - void on_loc_time(numeric_system) {} - void on_us_date() {} - void on_iso_date() {} - void on_utc_offset(numeric_system) {} - void on_tz_name() {} - void on_year(numeric_system) {} - void on_short_year(numeric_system) {} - void on_offset_year() {} - void on_century(numeric_system) {} - void on_iso_week_based_year() {} - void on_iso_week_based_short_year() {} - void on_dec_month(numeric_system) {} - void on_dec0_week_of_year(numeric_system, pad_type) {} - void on_dec1_week_of_year(numeric_system, pad_type) {} - void on_iso_week_of_year(numeric_system, pad_type) {} - void on_day_of_month(numeric_system, pad_type) {} - - void on_day_of_year() { - if (handle_nan_inf()) return; - write(days(), 0); - } - - void on_24_hour(numeric_system ns, pad_type pad) { - if (handle_nan_inf()) return; - - if (ns == numeric_system::standard) return write(hour(), 2, pad); - auto time = tm(); - time.tm_hour = to_nonnegative_int(hour(), 24); - format_tm(time, &tm_writer_type::on_24_hour, ns, pad); - } - - void on_12_hour(numeric_system ns, pad_type pad) { - if (handle_nan_inf()) return; - - if (ns == numeric_system::standard) return write(hour12(), 2, pad); - auto time = tm(); - time.tm_hour = to_nonnegative_int(hour12(), 12); - format_tm(time, &tm_writer_type::on_12_hour, ns, pad); - } - - void on_minute(numeric_system ns, pad_type pad) { - if (handle_nan_inf()) return; - - if (ns == numeric_system::standard) return write(minute(), 2, pad); - auto time = tm(); - time.tm_min = to_nonnegative_int(minute(), 60); - format_tm(time, &tm_writer_type::on_minute, ns, pad); - } - - void on_second(numeric_system ns, pad_type pad) { - if (handle_nan_inf()) return; - - if (ns == numeric_system::standard) { - if (std::is_floating_point::value) { - auto buf = memory_buffer(); - write_floating_seconds(buf, std::chrono::duration(val), - precision); - if (negative) *out++ = '-'; - if (buf.size() < 2 || buf[1] == '.') { - out = detail::write_padding(out, pad); - } - out = std::copy(buf.begin(), buf.end(), out); - } else { - write(second(), 2, pad); - write_fractional_seconds( - out, std::chrono::duration(val), precision); - } - return; - } - auto time = tm(); - time.tm_sec = to_nonnegative_int(second(), 60); - format_tm(time, &tm_writer_type::on_second, ns, pad); - } - - void on_12_hour_time() { - if (handle_nan_inf()) return; - format_tm(time(), &tm_writer_type::on_12_hour_time); - } - - void on_24_hour_time() { - if (handle_nan_inf()) { - *out++ = ':'; - handle_nan_inf(); - return; - } - - write(hour(), 2); - *out++ = ':'; - write(minute(), 2); - } - - void on_iso_time() { - on_24_hour_time(); - *out++ = ':'; - if (handle_nan_inf()) return; - on_second(numeric_system::standard, pad_type::zero); - } - - void on_am_pm() { - if (handle_nan_inf()) return; - format_tm(time(), &tm_writer_type::on_am_pm); - } - - void on_duration_value() { - if (handle_nan_inf()) return; - write_sign(); - out = format_duration_value(out, val, precision); - } - - void on_duration_unit() { - out = format_duration_unit(out); - } -}; - -} // namespace detail - -#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907 -using weekday = std::chrono::weekday; -using day = std::chrono::day; -using month = std::chrono::month; -using year = std::chrono::year; -using year_month_day = std::chrono::year_month_day; -#else -// A fallback version of weekday. -class weekday { - private: - unsigned char value_; - - public: - weekday() = default; - constexpr explicit weekday(unsigned wd) noexcept - : value_(static_cast(wd != 7 ? wd : 0)) {} - constexpr auto c_encoding() const noexcept -> unsigned { return value_; } -}; - -class day { - private: - unsigned char value_; - - public: - day() = default; - constexpr explicit day(unsigned d) noexcept - : value_(static_cast(d)) {} - constexpr explicit operator unsigned() const noexcept { return value_; } -}; - -class month { - private: - unsigned char value_; - - public: - month() = default; - constexpr explicit month(unsigned m) noexcept - : value_(static_cast(m)) {} - constexpr explicit operator unsigned() const noexcept { return value_; } -}; - -class year { - private: - int value_; - - public: - year() = default; - constexpr explicit year(int y) noexcept : value_(y) {} - constexpr explicit operator int() const noexcept { return value_; } -}; - -class year_month_day { - private: - fmt::year year_; - fmt::month month_; - fmt::day day_; - - public: - year_month_day() = default; - constexpr year_month_day(const year& y, const month& m, const day& d) noexcept - : year_(y), month_(m), day_(d) {} - constexpr auto year() const noexcept -> fmt::year { return year_; } - constexpr auto month() const noexcept -> fmt::month { return month_; } - constexpr auto day() const noexcept -> fmt::day { return day_; } -}; -#endif - -template -struct formatter : private formatter { - private: - bool localized_ = false; - bool use_tm_formatter_ = false; - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(), end = ctx.end(); - if (it != end && *it == 'L') { - ++it; - localized_ = true; - return it; - } - use_tm_formatter_ = it != end && *it != '}'; - return use_tm_formatter_ ? formatter::parse(ctx) : it; - } - - template - auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) { - auto time = std::tm(); - time.tm_wday = static_cast(wd.c_encoding()); - if (use_tm_formatter_) return formatter::format(time, ctx); - detail::get_locale loc(localized_, ctx.locale()); - auto w = detail::tm_writer(loc, ctx.out(), time); - w.on_abbr_weekday(); - return w.out(); - } -}; - -template -struct formatter : private formatter { - private: - bool use_tm_formatter_ = false; - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(), end = ctx.end(); - use_tm_formatter_ = it != end && *it != '}'; - return use_tm_formatter_ ? formatter::parse(ctx) : it; - } - - template - auto format(day d, FormatContext& ctx) const -> decltype(ctx.out()) { - auto time = std::tm(); - time.tm_mday = static_cast(static_cast(d)); - if (use_tm_formatter_) return formatter::format(time, ctx); - detail::get_locale loc(false, ctx.locale()); - auto w = detail::tm_writer(loc, ctx.out(), time); - w.on_day_of_month(detail::numeric_system::standard, detail::pad_type::zero); - return w.out(); - } -}; - -template -struct formatter : private formatter { - private: - bool localized_ = false; - bool use_tm_formatter_ = false; - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(), end = ctx.end(); - if (it != end && *it == 'L') { - ++it; - localized_ = true; - return it; - } - use_tm_formatter_ = it != end && *it != '}'; - return use_tm_formatter_ ? formatter::parse(ctx) : it; - } - - template - auto format(month m, FormatContext& ctx) const -> decltype(ctx.out()) { - auto time = std::tm(); - time.tm_mon = static_cast(static_cast(m)) - 1; - if (use_tm_formatter_) return formatter::format(time, ctx); - detail::get_locale loc(localized_, ctx.locale()); - auto w = detail::tm_writer(loc, ctx.out(), time); - w.on_abbr_month(); - return w.out(); - } -}; - -template -struct formatter : private formatter { - private: - bool use_tm_formatter_ = false; - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(), end = ctx.end(); - use_tm_formatter_ = it != end && *it != '}'; - return use_tm_formatter_ ? formatter::parse(ctx) : it; - } - - template - auto format(year y, FormatContext& ctx) const -> decltype(ctx.out()) { - auto time = std::tm(); - time.tm_year = static_cast(y) - 1900; - if (use_tm_formatter_) return formatter::format(time, ctx); - detail::get_locale loc(false, ctx.locale()); - auto w = detail::tm_writer(loc, ctx.out(), time); - w.on_year(detail::numeric_system::standard); - return w.out(); - } -}; - -template -struct formatter : private formatter { - private: - bool use_tm_formatter_ = false; - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(), end = ctx.end(); - use_tm_formatter_ = it != end && *it != '}'; - return use_tm_formatter_ ? formatter::parse(ctx) : it; - } - - template - auto format(year_month_day val, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto time = std::tm(); - time.tm_year = static_cast(val.year()) - 1900; - time.tm_mon = static_cast(static_cast(val.month())) - 1; - time.tm_mday = static_cast(static_cast(val.day())); - if (use_tm_formatter_) return formatter::format(time, ctx); - detail::get_locale loc(true, ctx.locale()); - auto w = detail::tm_writer(loc, ctx.out(), time); - w.on_iso_date(); - return w.out(); - } -}; - -template -struct formatter, Char> { - private: - format_specs specs_; - detail::arg_ref width_ref_; - detail::arg_ref precision_ref_; - bool localized_ = false; - basic_string_view format_str_; - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(), end = ctx.end(); - if (it == end || *it == '}') return it; - - it = detail::parse_align(it, end, specs_); - if (it == end) return it; - - it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx); - if (it == end) return it; - - auto checker = detail::chrono_format_checker(); - if (*it == '.') { - checker.has_precision_integral = !std::is_floating_point::value; - it = detail::parse_precision(it, end, specs_.precision, precision_ref_, - ctx); - } - if (it != end && *it == 'L') { - localized_ = true; - ++it; - } - end = detail::parse_chrono_format(it, end, checker); - format_str_ = {it, detail::to_unsigned(end - it)}; - return end; - } - - template - auto format(std::chrono::duration d, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto specs = specs_; - auto precision = specs.precision; - specs.precision = -1; - auto begin = format_str_.begin(), end = format_str_.end(); - // As a possible future optimization, we could avoid extra copying if width - // is not specified. - auto buf = basic_memory_buffer(); - auto out = std::back_inserter(buf); - detail::handle_dynamic_spec(specs.width, width_ref_, - ctx); - detail::handle_dynamic_spec(precision, - precision_ref_, ctx); - if (begin == end || *begin == '}') { - out = detail::format_duration_value(out, d.count(), precision); - detail::format_duration_unit(out); - } else { - using chrono_formatter = - detail::chrono_formatter; - auto f = chrono_formatter(ctx, out, d); - f.precision = precision; - f.localized = localized_; - detail::parse_chrono_format(begin, end, f); - } - return detail::write( - ctx.out(), basic_string_view(buf.data(), buf.size()), specs); - } -}; - -template -struct formatter, - Char> : formatter { - FMT_CONSTEXPR formatter() { - this->format_str_ = detail::string_literal{}; - } - - template - auto format(std::chrono::time_point val, - FormatContext& ctx) const -> decltype(ctx.out()) { - std::tm tm = gmtime(val); - using period = typename Duration::period; - if (detail::const_check( - period::num == 1 && period::den == 1 && - !std::is_floating_point::value)) { - return formatter::format(tm, ctx); - } - Duration epoch = val.time_since_epoch(); - Duration subsecs = detail::fmt_duration_cast( - epoch - detail::fmt_duration_cast(epoch)); - if (subsecs.count() < 0) { - auto second = - detail::fmt_duration_cast(std::chrono::seconds(1)); - if (tm.tm_sec != 0) - --tm.tm_sec; - else - tm = gmtime(val - second); - subsecs += detail::fmt_duration_cast(std::chrono::seconds(1)); - } - return formatter::do_format(tm, ctx, &subsecs); - } -}; - -#if FMT_USE_LOCAL_TIME -template -struct formatter, Char> - : formatter { - FMT_CONSTEXPR formatter() { - this->format_str_ = detail::string_literal{}; - } - - template - auto format(std::chrono::local_time val, FormatContext& ctx) const - -> decltype(ctx.out()) { - using period = typename Duration::period; - if (period::num != 1 || period::den != 1 || - std::is_floating_point::value) { - const auto epoch = val.time_since_epoch(); - const auto subsecs = detail::fmt_duration_cast( - epoch - detail::fmt_duration_cast(epoch)); - - return formatter::do_format(localtime(val), ctx, &subsecs); - } - - return formatter::format(localtime(val), ctx); - } -}; -#endif - -#if FMT_USE_UTC_TIME -template -struct formatter, - Char> - : formatter, - Char> { - template - auto format(std::chrono::time_point val, - FormatContext& ctx) const -> decltype(ctx.out()) { - return formatter< - std::chrono::time_point, - Char>::format(std::chrono::utc_clock::to_sys(val), ctx); - } -}; -#endif - -template struct formatter { - private: - format_specs specs_; - detail::arg_ref width_ref_; - - protected: - basic_string_view format_str_; - - template - auto do_format(const std::tm& tm, FormatContext& ctx, - const Duration* subsecs) const -> decltype(ctx.out()) { - auto specs = specs_; - auto buf = basic_memory_buffer(); - auto out = std::back_inserter(buf); - detail::handle_dynamic_spec(specs.width, width_ref_, - ctx); - - auto loc_ref = ctx.locale(); - detail::get_locale loc(static_cast(loc_ref), loc_ref); - auto w = - detail::tm_writer(loc, out, tm, subsecs); - detail::parse_chrono_format(format_str_.begin(), format_str_.end(), w); - return detail::write( - ctx.out(), basic_string_view(buf.data(), buf.size()), specs); - } - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(), end = ctx.end(); - if (it == end || *it == '}') return it; - - it = detail::parse_align(it, end, specs_); - if (it == end) return it; - - it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx); - if (it == end) return it; - - end = detail::parse_chrono_format(it, end, detail::tm_format_checker()); - // Replace the default format_str only if the new spec is not empty. - if (end != it) format_str_ = {it, detail::to_unsigned(end - it)}; - return end; - } - - template - auto format(const std::tm& tm, FormatContext& ctx) const - -> decltype(ctx.out()) { - return do_format(tm, ctx, nullptr); - } -}; - -FMT_END_EXPORT -FMT_END_NAMESPACE - -#endif // FMT_CHRONO_H_ diff --git a/include/spdlog/fmt/bundled/color.h b/include/spdlog/fmt/bundled/color.h deleted file mode 100644 index f0e9dd9..0000000 --- a/include/spdlog/fmt/bundled/color.h +++ /dev/null @@ -1,612 +0,0 @@ -// Formatting library for C++ - color support -// -// Copyright (c) 2018 - present, Victor Zverovich and fmt contributors -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_COLOR_H_ -#define FMT_COLOR_H_ - -#include "format.h" - -FMT_BEGIN_NAMESPACE -FMT_BEGIN_EXPORT - -enum class color : uint32_t { - alice_blue = 0xF0F8FF, // rgb(240,248,255) - antique_white = 0xFAEBD7, // rgb(250,235,215) - aqua = 0x00FFFF, // rgb(0,255,255) - aquamarine = 0x7FFFD4, // rgb(127,255,212) - azure = 0xF0FFFF, // rgb(240,255,255) - beige = 0xF5F5DC, // rgb(245,245,220) - bisque = 0xFFE4C4, // rgb(255,228,196) - black = 0x000000, // rgb(0,0,0) - blanched_almond = 0xFFEBCD, // rgb(255,235,205) - blue = 0x0000FF, // rgb(0,0,255) - blue_violet = 0x8A2BE2, // rgb(138,43,226) - brown = 0xA52A2A, // rgb(165,42,42) - burly_wood = 0xDEB887, // rgb(222,184,135) - cadet_blue = 0x5F9EA0, // rgb(95,158,160) - chartreuse = 0x7FFF00, // rgb(127,255,0) - chocolate = 0xD2691E, // rgb(210,105,30) - coral = 0xFF7F50, // rgb(255,127,80) - cornflower_blue = 0x6495ED, // rgb(100,149,237) - cornsilk = 0xFFF8DC, // rgb(255,248,220) - crimson = 0xDC143C, // rgb(220,20,60) - cyan = 0x00FFFF, // rgb(0,255,255) - dark_blue = 0x00008B, // rgb(0,0,139) - dark_cyan = 0x008B8B, // rgb(0,139,139) - dark_golden_rod = 0xB8860B, // rgb(184,134,11) - dark_gray = 0xA9A9A9, // rgb(169,169,169) - dark_green = 0x006400, // rgb(0,100,0) - dark_khaki = 0xBDB76B, // rgb(189,183,107) - dark_magenta = 0x8B008B, // rgb(139,0,139) - dark_olive_green = 0x556B2F, // rgb(85,107,47) - dark_orange = 0xFF8C00, // rgb(255,140,0) - dark_orchid = 0x9932CC, // rgb(153,50,204) - dark_red = 0x8B0000, // rgb(139,0,0) - dark_salmon = 0xE9967A, // rgb(233,150,122) - dark_sea_green = 0x8FBC8F, // rgb(143,188,143) - dark_slate_blue = 0x483D8B, // rgb(72,61,139) - dark_slate_gray = 0x2F4F4F, // rgb(47,79,79) - dark_turquoise = 0x00CED1, // rgb(0,206,209) - dark_violet = 0x9400D3, // rgb(148,0,211) - deep_pink = 0xFF1493, // rgb(255,20,147) - deep_sky_blue = 0x00BFFF, // rgb(0,191,255) - dim_gray = 0x696969, // rgb(105,105,105) - dodger_blue = 0x1E90FF, // rgb(30,144,255) - fire_brick = 0xB22222, // rgb(178,34,34) - floral_white = 0xFFFAF0, // rgb(255,250,240) - forest_green = 0x228B22, // rgb(34,139,34) - fuchsia = 0xFF00FF, // rgb(255,0,255) - gainsboro = 0xDCDCDC, // rgb(220,220,220) - ghost_white = 0xF8F8FF, // rgb(248,248,255) - gold = 0xFFD700, // rgb(255,215,0) - golden_rod = 0xDAA520, // rgb(218,165,32) - gray = 0x808080, // rgb(128,128,128) - green = 0x008000, // rgb(0,128,0) - green_yellow = 0xADFF2F, // rgb(173,255,47) - honey_dew = 0xF0FFF0, // rgb(240,255,240) - hot_pink = 0xFF69B4, // rgb(255,105,180) - indian_red = 0xCD5C5C, // rgb(205,92,92) - indigo = 0x4B0082, // rgb(75,0,130) - ivory = 0xFFFFF0, // rgb(255,255,240) - khaki = 0xF0E68C, // rgb(240,230,140) - lavender = 0xE6E6FA, // rgb(230,230,250) - lavender_blush = 0xFFF0F5, // rgb(255,240,245) - lawn_green = 0x7CFC00, // rgb(124,252,0) - lemon_chiffon = 0xFFFACD, // rgb(255,250,205) - light_blue = 0xADD8E6, // rgb(173,216,230) - light_coral = 0xF08080, // rgb(240,128,128) - light_cyan = 0xE0FFFF, // rgb(224,255,255) - light_golden_rod_yellow = 0xFAFAD2, // rgb(250,250,210) - light_gray = 0xD3D3D3, // rgb(211,211,211) - light_green = 0x90EE90, // rgb(144,238,144) - light_pink = 0xFFB6C1, // rgb(255,182,193) - light_salmon = 0xFFA07A, // rgb(255,160,122) - light_sea_green = 0x20B2AA, // rgb(32,178,170) - light_sky_blue = 0x87CEFA, // rgb(135,206,250) - light_slate_gray = 0x778899, // rgb(119,136,153) - light_steel_blue = 0xB0C4DE, // rgb(176,196,222) - light_yellow = 0xFFFFE0, // rgb(255,255,224) - lime = 0x00FF00, // rgb(0,255,0) - lime_green = 0x32CD32, // rgb(50,205,50) - linen = 0xFAF0E6, // rgb(250,240,230) - magenta = 0xFF00FF, // rgb(255,0,255) - maroon = 0x800000, // rgb(128,0,0) - medium_aquamarine = 0x66CDAA, // rgb(102,205,170) - medium_blue = 0x0000CD, // rgb(0,0,205) - medium_orchid = 0xBA55D3, // rgb(186,85,211) - medium_purple = 0x9370DB, // rgb(147,112,219) - medium_sea_green = 0x3CB371, // rgb(60,179,113) - medium_slate_blue = 0x7B68EE, // rgb(123,104,238) - medium_spring_green = 0x00FA9A, // rgb(0,250,154) - medium_turquoise = 0x48D1CC, // rgb(72,209,204) - medium_violet_red = 0xC71585, // rgb(199,21,133) - midnight_blue = 0x191970, // rgb(25,25,112) - mint_cream = 0xF5FFFA, // rgb(245,255,250) - misty_rose = 0xFFE4E1, // rgb(255,228,225) - moccasin = 0xFFE4B5, // rgb(255,228,181) - navajo_white = 0xFFDEAD, // rgb(255,222,173) - navy = 0x000080, // rgb(0,0,128) - old_lace = 0xFDF5E6, // rgb(253,245,230) - olive = 0x808000, // rgb(128,128,0) - olive_drab = 0x6B8E23, // rgb(107,142,35) - orange = 0xFFA500, // rgb(255,165,0) - orange_red = 0xFF4500, // rgb(255,69,0) - orchid = 0xDA70D6, // rgb(218,112,214) - pale_golden_rod = 0xEEE8AA, // rgb(238,232,170) - pale_green = 0x98FB98, // rgb(152,251,152) - pale_turquoise = 0xAFEEEE, // rgb(175,238,238) - pale_violet_red = 0xDB7093, // rgb(219,112,147) - papaya_whip = 0xFFEFD5, // rgb(255,239,213) - peach_puff = 0xFFDAB9, // rgb(255,218,185) - peru = 0xCD853F, // rgb(205,133,63) - pink = 0xFFC0CB, // rgb(255,192,203) - plum = 0xDDA0DD, // rgb(221,160,221) - powder_blue = 0xB0E0E6, // rgb(176,224,230) - purple = 0x800080, // rgb(128,0,128) - rebecca_purple = 0x663399, // rgb(102,51,153) - red = 0xFF0000, // rgb(255,0,0) - rosy_brown = 0xBC8F8F, // rgb(188,143,143) - royal_blue = 0x4169E1, // rgb(65,105,225) - saddle_brown = 0x8B4513, // rgb(139,69,19) - salmon = 0xFA8072, // rgb(250,128,114) - sandy_brown = 0xF4A460, // rgb(244,164,96) - sea_green = 0x2E8B57, // rgb(46,139,87) - sea_shell = 0xFFF5EE, // rgb(255,245,238) - sienna = 0xA0522D, // rgb(160,82,45) - silver = 0xC0C0C0, // rgb(192,192,192) - sky_blue = 0x87CEEB, // rgb(135,206,235) - slate_blue = 0x6A5ACD, // rgb(106,90,205) - slate_gray = 0x708090, // rgb(112,128,144) - snow = 0xFFFAFA, // rgb(255,250,250) - spring_green = 0x00FF7F, // rgb(0,255,127) - steel_blue = 0x4682B4, // rgb(70,130,180) - tan = 0xD2B48C, // rgb(210,180,140) - teal = 0x008080, // rgb(0,128,128) - thistle = 0xD8BFD8, // rgb(216,191,216) - tomato = 0xFF6347, // rgb(255,99,71) - turquoise = 0x40E0D0, // rgb(64,224,208) - violet = 0xEE82EE, // rgb(238,130,238) - wheat = 0xF5DEB3, // rgb(245,222,179) - white = 0xFFFFFF, // rgb(255,255,255) - white_smoke = 0xF5F5F5, // rgb(245,245,245) - yellow = 0xFFFF00, // rgb(255,255,0) - yellow_green = 0x9ACD32 // rgb(154,205,50) -}; // enum class color - -enum class terminal_color : uint8_t { - black = 30, - red, - green, - yellow, - blue, - magenta, - cyan, - white, - bright_black = 90, - bright_red, - bright_green, - bright_yellow, - bright_blue, - bright_magenta, - bright_cyan, - bright_white -}; - -enum class emphasis : uint8_t { - bold = 1, - faint = 1 << 1, - italic = 1 << 2, - underline = 1 << 3, - blink = 1 << 4, - reverse = 1 << 5, - conceal = 1 << 6, - strikethrough = 1 << 7, -}; - -// rgb is a struct for red, green and blue colors. -// Using the name "rgb" makes some editors show the color in a tooltip. -struct rgb { - FMT_CONSTEXPR rgb() : r(0), g(0), b(0) {} - FMT_CONSTEXPR rgb(uint8_t r_, uint8_t g_, uint8_t b_) : r(r_), g(g_), b(b_) {} - FMT_CONSTEXPR rgb(uint32_t hex) - : r((hex >> 16) & 0xFF), g((hex >> 8) & 0xFF), b(hex & 0xFF) {} - FMT_CONSTEXPR rgb(color hex) - : r((uint32_t(hex) >> 16) & 0xFF), - g((uint32_t(hex) >> 8) & 0xFF), - b(uint32_t(hex) & 0xFF) {} - uint8_t r; - uint8_t g; - uint8_t b; -}; - -namespace detail { - -// color is a struct of either a rgb color or a terminal color. -struct color_type { - FMT_CONSTEXPR color_type() noexcept : is_rgb(), value{} {} - FMT_CONSTEXPR color_type(color rgb_color) noexcept : is_rgb(true), value{} { - value.rgb_color = static_cast(rgb_color); - } - FMT_CONSTEXPR color_type(rgb rgb_color) noexcept : is_rgb(true), value{} { - value.rgb_color = (static_cast(rgb_color.r) << 16) | - (static_cast(rgb_color.g) << 8) | rgb_color.b; - } - FMT_CONSTEXPR color_type(terminal_color term_color) noexcept - : is_rgb(), value{} { - value.term_color = static_cast(term_color); - } - bool is_rgb; - union color_union { - uint8_t term_color; - uint32_t rgb_color; - } value; -}; -} // namespace detail - -/// A text style consisting of foreground and background colors and emphasis. -class text_style { - public: - FMT_CONSTEXPR text_style(emphasis em = emphasis()) noexcept - : set_foreground_color(), set_background_color(), ems(em) {} - - FMT_CONSTEXPR auto operator|=(const text_style& rhs) -> text_style& { - if (!set_foreground_color) { - set_foreground_color = rhs.set_foreground_color; - foreground_color = rhs.foreground_color; - } else if (rhs.set_foreground_color) { - if (!foreground_color.is_rgb || !rhs.foreground_color.is_rgb) - report_error("can't OR a terminal color"); - foreground_color.value.rgb_color |= rhs.foreground_color.value.rgb_color; - } - - if (!set_background_color) { - set_background_color = rhs.set_background_color; - background_color = rhs.background_color; - } else if (rhs.set_background_color) { - if (!background_color.is_rgb || !rhs.background_color.is_rgb) - report_error("can't OR a terminal color"); - background_color.value.rgb_color |= rhs.background_color.value.rgb_color; - } - - ems = static_cast(static_cast(ems) | - static_cast(rhs.ems)); - return *this; - } - - friend FMT_CONSTEXPR auto operator|(text_style lhs, const text_style& rhs) - -> text_style { - return lhs |= rhs; - } - - FMT_CONSTEXPR auto has_foreground() const noexcept -> bool { - return set_foreground_color; - } - FMT_CONSTEXPR auto has_background() const noexcept -> bool { - return set_background_color; - } - FMT_CONSTEXPR auto has_emphasis() const noexcept -> bool { - return static_cast(ems) != 0; - } - FMT_CONSTEXPR auto get_foreground() const noexcept -> detail::color_type { - FMT_ASSERT(has_foreground(), "no foreground specified for this style"); - return foreground_color; - } - FMT_CONSTEXPR auto get_background() const noexcept -> detail::color_type { - FMT_ASSERT(has_background(), "no background specified for this style"); - return background_color; - } - FMT_CONSTEXPR auto get_emphasis() const noexcept -> emphasis { - FMT_ASSERT(has_emphasis(), "no emphasis specified for this style"); - return ems; - } - - private: - FMT_CONSTEXPR text_style(bool is_foreground, - detail::color_type text_color) noexcept - : set_foreground_color(), set_background_color(), ems() { - if (is_foreground) { - foreground_color = text_color; - set_foreground_color = true; - } else { - background_color = text_color; - set_background_color = true; - } - } - - friend FMT_CONSTEXPR auto fg(detail::color_type foreground) noexcept - -> text_style; - - friend FMT_CONSTEXPR auto bg(detail::color_type background) noexcept - -> text_style; - - detail::color_type foreground_color; - detail::color_type background_color; - bool set_foreground_color; - bool set_background_color; - emphasis ems; -}; - -/// Creates a text style from the foreground (text) color. -FMT_CONSTEXPR inline auto fg(detail::color_type foreground) noexcept - -> text_style { - return text_style(true, foreground); -} - -/// Creates a text style from the background color. -FMT_CONSTEXPR inline auto bg(detail::color_type background) noexcept - -> text_style { - return text_style(false, background); -} - -FMT_CONSTEXPR inline auto operator|(emphasis lhs, emphasis rhs) noexcept - -> text_style { - return text_style(lhs) | rhs; -} - -namespace detail { - -template struct ansi_color_escape { - FMT_CONSTEXPR ansi_color_escape(detail::color_type text_color, - const char* esc) noexcept { - // If we have a terminal color, we need to output another escape code - // sequence. - if (!text_color.is_rgb) { - bool is_background = esc == string_view("\x1b[48;2;"); - uint32_t value = text_color.value.term_color; - // Background ASCII codes are the same as the foreground ones but with - // 10 more. - if (is_background) value += 10u; - - size_t index = 0; - buffer[index++] = static_cast('\x1b'); - buffer[index++] = static_cast('['); - - if (value >= 100u) { - buffer[index++] = static_cast('1'); - value %= 100u; - } - buffer[index++] = static_cast('0' + value / 10u); - buffer[index++] = static_cast('0' + value % 10u); - - buffer[index++] = static_cast('m'); - buffer[index++] = static_cast('\0'); - return; - } - - for (int i = 0; i < 7; i++) { - buffer[i] = static_cast(esc[i]); - } - rgb color(text_color.value.rgb_color); - to_esc(color.r, buffer + 7, ';'); - to_esc(color.g, buffer + 11, ';'); - to_esc(color.b, buffer + 15, 'm'); - buffer[19] = static_cast(0); - } - FMT_CONSTEXPR ansi_color_escape(emphasis em) noexcept { - uint8_t em_codes[num_emphases] = {}; - if (has_emphasis(em, emphasis::bold)) em_codes[0] = 1; - if (has_emphasis(em, emphasis::faint)) em_codes[1] = 2; - if (has_emphasis(em, emphasis::italic)) em_codes[2] = 3; - if (has_emphasis(em, emphasis::underline)) em_codes[3] = 4; - if (has_emphasis(em, emphasis::blink)) em_codes[4] = 5; - if (has_emphasis(em, emphasis::reverse)) em_codes[5] = 7; - if (has_emphasis(em, emphasis::conceal)) em_codes[6] = 8; - if (has_emphasis(em, emphasis::strikethrough)) em_codes[7] = 9; - - size_t index = 0; - for (size_t i = 0; i < num_emphases; ++i) { - if (!em_codes[i]) continue; - buffer[index++] = static_cast('\x1b'); - buffer[index++] = static_cast('['); - buffer[index++] = static_cast('0' + em_codes[i]); - buffer[index++] = static_cast('m'); - } - buffer[index++] = static_cast(0); - } - FMT_CONSTEXPR operator const Char*() const noexcept { return buffer; } - - FMT_CONSTEXPR auto begin() const noexcept -> const Char* { return buffer; } - FMT_CONSTEXPR20 auto end() const noexcept -> const Char* { - return buffer + basic_string_view(buffer).size(); - } - - private: - static constexpr size_t num_emphases = 8; - Char buffer[7u + 3u * num_emphases + 1u]; - - static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out, - char delimiter) noexcept { - out[0] = static_cast('0' + c / 100); - out[1] = static_cast('0' + c / 10 % 10); - out[2] = static_cast('0' + c % 10); - out[3] = static_cast(delimiter); - } - static FMT_CONSTEXPR auto has_emphasis(emphasis em, emphasis mask) noexcept - -> bool { - return static_cast(em) & static_cast(mask); - } -}; - -template -FMT_CONSTEXPR auto make_foreground_color(detail::color_type foreground) noexcept - -> ansi_color_escape { - return ansi_color_escape(foreground, "\x1b[38;2;"); -} - -template -FMT_CONSTEXPR auto make_background_color(detail::color_type background) noexcept - -> ansi_color_escape { - return ansi_color_escape(background, "\x1b[48;2;"); -} - -template -FMT_CONSTEXPR auto make_emphasis(emphasis em) noexcept - -> ansi_color_escape { - return ansi_color_escape(em); -} - -template inline void reset_color(buffer& buffer) { - auto reset_color = string_view("\x1b[0m"); - buffer.append(reset_color.begin(), reset_color.end()); -} - -template struct styled_arg : detail::view { - const T& value; - text_style style; - styled_arg(const T& v, text_style s) : value(v), style(s) {} -}; - -template -void vformat_to( - buffer& buf, const text_style& ts, basic_string_view format_str, - basic_format_args>> args) { - bool has_style = false; - if (ts.has_emphasis()) { - has_style = true; - auto emphasis = detail::make_emphasis(ts.get_emphasis()); - buf.append(emphasis.begin(), emphasis.end()); - } - if (ts.has_foreground()) { - has_style = true; - auto foreground = detail::make_foreground_color(ts.get_foreground()); - buf.append(foreground.begin(), foreground.end()); - } - if (ts.has_background()) { - has_style = true; - auto background = detail::make_background_color(ts.get_background()); - buf.append(background.begin(), background.end()); - } - detail::vformat_to(buf, format_str, args, {}); - if (has_style) detail::reset_color(buf); -} - -} // namespace detail - -inline void vprint(FILE* f, const text_style& ts, string_view fmt, - format_args args) { - auto buf = memory_buffer(); - detail::vformat_to(buf, ts, fmt, args); - print(f, FMT_STRING("{}"), string_view(buf.begin(), buf.size())); -} - -/** - * Formats a string and prints it to the specified file stream using ANSI - * escape sequences to specify text formatting. - * - * **Example**: - * - * fmt::print(fmt::emphasis::bold | fg(fmt::color::red), - * "Elapsed time: {0:.2f} seconds", 1.23); - */ -template -void print(FILE* f, const text_style& ts, format_string fmt, - T&&... args) { - vprint(f, ts, fmt, fmt::make_format_args(args...)); -} - -/** - * Formats a string and prints it to stdout using ANSI escape sequences to - * specify text formatting. - * - * **Example**: - * - * fmt::print(fmt::emphasis::bold | fg(fmt::color::red), - * "Elapsed time: {0:.2f} seconds", 1.23); - */ -template -void print(const text_style& ts, format_string fmt, T&&... args) { - return print(stdout, ts, fmt, std::forward(args)...); -} - -inline auto vformat(const text_style& ts, string_view fmt, format_args args) - -> std::string { - auto buf = memory_buffer(); - detail::vformat_to(buf, ts, fmt, args); - return fmt::to_string(buf); -} - -/** - * Formats arguments and returns the result as a string using ANSI escape - * sequences to specify text formatting. - * - * **Example**: - * - * ``` - * #include - * std::string message = fmt::format(fmt::emphasis::bold | fg(fmt::color::red), - * "The answer is {}", 42); - * ``` - */ -template -inline auto format(const text_style& ts, format_string fmt, T&&... args) - -> std::string { - return fmt::vformat(ts, fmt, fmt::make_format_args(args...)); -} - -/// Formats a string with the given text_style and writes the output to `out`. -template ::value)> -auto vformat_to(OutputIt out, const text_style& ts, string_view fmt, - format_args args) -> OutputIt { - auto&& buf = detail::get_buffer(out); - detail::vformat_to(buf, ts, fmt, args); - return detail::get_iterator(buf, out); -} - -/** - * Formats arguments with the given text style, writes the result to the output - * iterator `out` and returns the iterator past the end of the output range. - * - * **Example**: - * - * std::vector out; - * fmt::format_to(std::back_inserter(out), - * fmt::emphasis::bold | fg(fmt::color::red), "{}", 42); - */ -template ::value)> -inline auto format_to(OutputIt out, const text_style& ts, - format_string fmt, T&&... args) -> OutputIt { - return vformat_to(out, ts, fmt, fmt::make_format_args(args...)); -} - -template -struct formatter, Char> : formatter { - template - auto format(const detail::styled_arg& arg, FormatContext& ctx) const - -> decltype(ctx.out()) { - const auto& ts = arg.style; - const auto& value = arg.value; - auto out = ctx.out(); - - bool has_style = false; - if (ts.has_emphasis()) { - has_style = true; - auto emphasis = detail::make_emphasis(ts.get_emphasis()); - out = std::copy(emphasis.begin(), emphasis.end(), out); - } - if (ts.has_foreground()) { - has_style = true; - auto foreground = - detail::make_foreground_color(ts.get_foreground()); - out = std::copy(foreground.begin(), foreground.end(), out); - } - if (ts.has_background()) { - has_style = true; - auto background = - detail::make_background_color(ts.get_background()); - out = std::copy(background.begin(), background.end(), out); - } - out = formatter::format(value, ctx); - if (has_style) { - auto reset_color = string_view("\x1b[0m"); - out = std::copy(reset_color.begin(), reset_color.end(), out); - } - return out; - } -}; - -/** - * Returns an argument that will be formatted using ANSI escape sequences, - * to be used in a formatting function. - * - * **Example**: - * - * fmt::print("Elapsed time: {0:.2f} seconds", - * fmt::styled(1.23, fmt::fg(fmt::color::green) | - * fmt::bg(fmt::color::blue))); - */ -template -FMT_CONSTEXPR auto styled(const T& value, text_style ts) - -> detail::styled_arg> { - return detail::styled_arg>{value, ts}; -} - -FMT_END_EXPORT -FMT_END_NAMESPACE - -#endif // FMT_COLOR_H_ diff --git a/include/spdlog/fmt/bundled/compile.h b/include/spdlog/fmt/bundled/compile.h deleted file mode 100644 index b2afc2c..0000000 --- a/include/spdlog/fmt/bundled/compile.h +++ /dev/null @@ -1,529 +0,0 @@ -// Formatting library for C++ - experimental format string compilation -// -// Copyright (c) 2012 - present, Victor Zverovich and fmt contributors -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_COMPILE_H_ -#define FMT_COMPILE_H_ - -#ifndef FMT_MODULE -# include // std::back_inserter -#endif - -#include "format.h" - -FMT_BEGIN_NAMESPACE - -// A compile-time string which is compiled into fast formatting code. -FMT_EXPORT class compiled_string {}; - -namespace detail { - -template -FMT_CONSTEXPR inline auto copy(InputIt begin, InputIt end, counting_iterator it) - -> counting_iterator { - return it + (end - begin); -} - -template -struct is_compiled_string : std::is_base_of {}; - -/** - * Converts a string literal `s` into a format string that will be parsed at - * compile time and converted into efficient formatting code. Requires C++17 - * `constexpr if` compiler support. - * - * **Example**: - * - * // Converts 42 into std::string using the most efficient method and no - * // runtime format string processing. - * std::string s = fmt::format(FMT_COMPILE("{}"), 42); - */ -#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) -# define FMT_COMPILE(s) FMT_STRING_IMPL(s, fmt::compiled_string, explicit) -#else -# define FMT_COMPILE(s) FMT_STRING(s) -#endif - -#if FMT_USE_NONTYPE_TEMPLATE_ARGS -template Str> -struct udl_compiled_string : compiled_string { - using char_type = Char; - explicit constexpr operator basic_string_view() const { - return {Str.data, N - 1}; - } -}; -#endif - -template -auto first(const T& value, const Tail&...) -> const T& { - return value; -} - -#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) -template struct type_list {}; - -// Returns a reference to the argument at index N from [first, rest...]. -template -constexpr const auto& get([[maybe_unused]] const T& first, - [[maybe_unused]] const Args&... rest) { - static_assert(N < 1 + sizeof...(Args), "index is out of bounds"); - if constexpr (N == 0) - return first; - else - return detail::get(rest...); -} - -template -constexpr int get_arg_index_by_name(basic_string_view name, - type_list) { - return get_arg_index_by_name(name); -} - -template struct get_type_impl; - -template struct get_type_impl> { - using type = - remove_cvref_t(std::declval()...))>; -}; - -template -using get_type = typename get_type_impl::type; - -template struct is_compiled_format : std::false_type {}; - -template struct text { - basic_string_view data; - using char_type = Char; - - template - constexpr OutputIt format(OutputIt out, const Args&...) const { - return write(out, data); - } -}; - -template -struct is_compiled_format> : std::true_type {}; - -template -constexpr text make_text(basic_string_view s, size_t pos, - size_t size) { - return {{&s[pos], size}}; -} - -template struct code_unit { - Char value; - using char_type = Char; - - template - constexpr OutputIt format(OutputIt out, const Args&...) const { - *out++ = value; - return out; - } -}; - -// This ensures that the argument type is convertible to `const T&`. -template -constexpr const T& get_arg_checked(const Args&... args) { - const auto& arg = detail::get(args...); - if constexpr (detail::is_named_arg>()) { - return arg.value; - } else { - return arg; - } -} - -template -struct is_compiled_format> : std::true_type {}; - -// A replacement field that refers to argument N. -template struct field { - using char_type = Char; - - template - constexpr OutputIt format(OutputIt out, const Args&... args) const { - const T& arg = get_arg_checked(args...); - if constexpr (std::is_convertible>::value) { - auto s = basic_string_view(arg); - return copy(s.begin(), s.end(), out); - } - return write(out, arg); - } -}; - -template -struct is_compiled_format> : std::true_type {}; - -// A replacement field that refers to argument with name. -template struct runtime_named_field { - using char_type = Char; - basic_string_view name; - - template - constexpr static bool try_format_argument( - OutputIt& out, - // [[maybe_unused]] due to unused-but-set-parameter warning in GCC 7,8,9 - [[maybe_unused]] basic_string_view arg_name, const T& arg) { - if constexpr (is_named_arg::type>::value) { - if (arg_name == arg.name) { - out = write(out, arg.value); - return true; - } - } - return false; - } - - template - constexpr OutputIt format(OutputIt out, const Args&... args) const { - bool found = (try_format_argument(out, name, args) || ...); - if (!found) { - FMT_THROW(format_error("argument with specified name is not found")); - } - return out; - } -}; - -template -struct is_compiled_format> : std::true_type {}; - -// A replacement field that refers to argument N and has format specifiers. -template struct spec_field { - using char_type = Char; - formatter fmt; - - template - constexpr FMT_INLINE OutputIt format(OutputIt out, - const Args&... args) const { - const auto& vargs = - fmt::make_format_args>(args...); - basic_format_context ctx(out, vargs); - return fmt.format(get_arg_checked(args...), ctx); - } -}; - -template -struct is_compiled_format> : std::true_type {}; - -template struct concat { - L lhs; - R rhs; - using char_type = typename L::char_type; - - template - constexpr OutputIt format(OutputIt out, const Args&... args) const { - out = lhs.format(out, args...); - return rhs.format(out, args...); - } -}; - -template -struct is_compiled_format> : std::true_type {}; - -template -constexpr concat make_concat(L lhs, R rhs) { - return {lhs, rhs}; -} - -struct unknown_format {}; - -template -constexpr size_t parse_text(basic_string_view str, size_t pos) { - for (size_t size = str.size(); pos != size; ++pos) { - if (str[pos] == '{' || str[pos] == '}') break; - } - return pos; -} - -template -constexpr auto compile_format_string(S fmt); - -template -constexpr auto parse_tail(T head, S fmt) { - if constexpr (POS != basic_string_view(fmt).size()) { - constexpr auto tail = compile_format_string(fmt); - if constexpr (std::is_same, - unknown_format>()) - return tail; - else - return make_concat(head, tail); - } else { - return head; - } -} - -template struct parse_specs_result { - formatter fmt; - size_t end; - int next_arg_id; -}; - -enum { manual_indexing_id = -1 }; - -template -constexpr parse_specs_result parse_specs(basic_string_view str, - size_t pos, int next_arg_id) { - str.remove_prefix(pos); - auto ctx = - compile_parse_context(str, max_value(), nullptr, next_arg_id); - auto f = formatter(); - auto end = f.parse(ctx); - return {f, pos + fmt::detail::to_unsigned(end - str.data()), - next_arg_id == 0 ? manual_indexing_id : ctx.next_arg_id()}; -} - -template struct arg_id_handler { - arg_ref arg_id; - - constexpr int on_auto() { - FMT_ASSERT(false, "handler cannot be used with automatic indexing"); - return 0; - } - constexpr int on_index(int id) { - arg_id = arg_ref(id); - return 0; - } - constexpr int on_name(basic_string_view id) { - arg_id = arg_ref(id); - return 0; - } -}; - -template struct parse_arg_id_result { - arg_ref arg_id; - const Char* arg_id_end; -}; - -template -constexpr auto parse_arg_id(const Char* begin, const Char* end) { - auto handler = arg_id_handler{arg_ref{}}; - auto arg_id_end = parse_arg_id(begin, end, handler); - return parse_arg_id_result{handler.arg_id, arg_id_end}; -} - -template struct field_type { - using type = remove_cvref_t; -}; - -template -struct field_type::value>> { - using type = remove_cvref_t; -}; - -template -constexpr auto parse_replacement_field_then_tail(S fmt) { - using char_type = typename S::char_type; - constexpr auto str = basic_string_view(fmt); - constexpr char_type c = END_POS != str.size() ? str[END_POS] : char_type(); - if constexpr (c == '}') { - return parse_tail( - field::type, ARG_INDEX>(), fmt); - } else if constexpr (c != ':') { - FMT_THROW(format_error("expected ':'")); - } else { - constexpr auto result = parse_specs::type>( - str, END_POS + 1, NEXT_ID == manual_indexing_id ? 0 : NEXT_ID); - if constexpr (result.end >= str.size() || str[result.end] != '}') { - FMT_THROW(format_error("expected '}'")); - return 0; - } else { - return parse_tail( - spec_field::type, ARG_INDEX>{ - result.fmt}, - fmt); - } - } -} - -// Compiles a non-empty format string and returns the compiled representation -// or unknown_format() on unrecognized input. -template -constexpr auto compile_format_string(S fmt) { - using char_type = typename S::char_type; - constexpr auto str = basic_string_view(fmt); - if constexpr (str[POS] == '{') { - if constexpr (POS + 1 == str.size()) - FMT_THROW(format_error("unmatched '{' in format string")); - if constexpr (str[POS + 1] == '{') { - return parse_tail(make_text(str, POS, 1), fmt); - } else if constexpr (str[POS + 1] == '}' || str[POS + 1] == ':') { - static_assert(ID != manual_indexing_id, - "cannot switch from manual to automatic argument indexing"); - constexpr auto next_id = - ID != manual_indexing_id ? ID + 1 : manual_indexing_id; - return parse_replacement_field_then_tail, Args, - POS + 1, ID, next_id>(fmt); - } else { - constexpr auto arg_id_result = - parse_arg_id(str.data() + POS + 1, str.data() + str.size()); - constexpr auto arg_id_end_pos = arg_id_result.arg_id_end - str.data(); - constexpr char_type c = - arg_id_end_pos != str.size() ? str[arg_id_end_pos] : char_type(); - static_assert(c == '}' || c == ':', "missing '}' in format string"); - if constexpr (arg_id_result.arg_id.kind == arg_id_kind::index) { - static_assert( - ID == manual_indexing_id || ID == 0, - "cannot switch from automatic to manual argument indexing"); - constexpr auto arg_index = arg_id_result.arg_id.val.index; - return parse_replacement_field_then_tail, - Args, arg_id_end_pos, - arg_index, manual_indexing_id>( - fmt); - } else if constexpr (arg_id_result.arg_id.kind == arg_id_kind::name) { - constexpr auto arg_index = - get_arg_index_by_name(arg_id_result.arg_id.val.name, Args{}); - if constexpr (arg_index >= 0) { - constexpr auto next_id = - ID != manual_indexing_id ? ID + 1 : manual_indexing_id; - return parse_replacement_field_then_tail< - decltype(get_type::value), Args, arg_id_end_pos, - arg_index, next_id>(fmt); - } else if constexpr (c == '}') { - return parse_tail( - runtime_named_field{arg_id_result.arg_id.val.name}, - fmt); - } else if constexpr (c == ':') { - return unknown_format(); // no type info for specs parsing - } - } - } - } else if constexpr (str[POS] == '}') { - if constexpr (POS + 1 == str.size()) - FMT_THROW(format_error("unmatched '}' in format string")); - return parse_tail(make_text(str, POS, 1), fmt); - } else { - constexpr auto end = parse_text(str, POS + 1); - if constexpr (end - POS > 1) { - return parse_tail(make_text(str, POS, end - POS), fmt); - } else { - return parse_tail(code_unit{str[POS]}, fmt); - } - } -} - -template ::value)> -constexpr auto compile(S fmt) { - constexpr auto str = basic_string_view(fmt); - if constexpr (str.size() == 0) { - return detail::make_text(str, 0, 0); - } else { - constexpr auto result = - detail::compile_format_string, 0, 0>(fmt); - return result; - } -} -#endif // defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) -} // namespace detail - -FMT_BEGIN_EXPORT - -#if defined(__cpp_if_constexpr) && defined(__cpp_return_type_deduction) - -template ::value)> -FMT_INLINE std::basic_string format(const CompiledFormat& cf, - const Args&... args) { - auto s = std::basic_string(); - cf.format(std::back_inserter(s), args...); - return s; -} - -template ::value)> -constexpr FMT_INLINE OutputIt format_to(OutputIt out, const CompiledFormat& cf, - const Args&... args) { - return cf.format(out, args...); -} - -template ::value)> -FMT_INLINE std::basic_string format(const S&, - Args&&... args) { - if constexpr (std::is_same::value) { - constexpr auto str = basic_string_view(S()); - if constexpr (str.size() == 2 && str[0] == '{' && str[1] == '}') { - const auto& first = detail::first(args...); - if constexpr (detail::is_named_arg< - remove_cvref_t>::value) { - return fmt::to_string(first.value); - } else { - return fmt::to_string(first); - } - } - } - constexpr auto compiled = detail::compile(S()); - if constexpr (std::is_same, - detail::unknown_format>()) { - return fmt::format( - static_cast>(S()), - std::forward(args)...); - } else { - return fmt::format(compiled, std::forward(args)...); - } -} - -template ::value)> -FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, Args&&... args) { - constexpr auto compiled = detail::compile(S()); - if constexpr (std::is_same, - detail::unknown_format>()) { - return fmt::format_to( - out, static_cast>(S()), - std::forward(args)...); - } else { - return fmt::format_to(out, compiled, std::forward(args)...); - } -} -#endif - -template ::value)> -auto format_to_n(OutputIt out, size_t n, const S& fmt, Args&&... args) - -> format_to_n_result { - using traits = detail::fixed_buffer_traits; - auto buf = detail::iterator_buffer(out, n); - fmt::format_to(std::back_inserter(buf), fmt, std::forward(args)...); - return {buf.out(), buf.count()}; -} - -template ::value)> -FMT_CONSTEXPR20 auto formatted_size(const S& fmt, const Args&... args) - -> size_t { - return fmt::format_to(detail::counting_iterator(), fmt, args...).count(); -} - -template ::value)> -void print(std::FILE* f, const S& fmt, const Args&... args) { - memory_buffer buffer; - fmt::format_to(std::back_inserter(buffer), fmt, args...); - detail::print(f, {buffer.data(), buffer.size()}); -} - -template ::value)> -void print(const S& fmt, const Args&... args) { - print(stdout, fmt, args...); -} - -#if FMT_USE_NONTYPE_TEMPLATE_ARGS -inline namespace literals { -template constexpr auto operator""_cf() { - using char_t = remove_cvref_t; - return detail::udl_compiled_string(); -} -} // namespace literals -#endif - -FMT_END_EXPORT -FMT_END_NAMESPACE - -#endif // FMT_COMPILE_H_ diff --git a/include/spdlog/fmt/bundled/core.h b/include/spdlog/fmt/bundled/core.h deleted file mode 100644 index 8ca735f..0000000 --- a/include/spdlog/fmt/bundled/core.h +++ /dev/null @@ -1,5 +0,0 @@ -// This file is only provided for compatibility and may be removed in future -// versions. Use fmt/base.h if you don't need fmt::format and fmt/format.h -// otherwise. - -#include "format.h" diff --git a/include/spdlog/fmt/bundled/fmt.license.rst b/include/spdlog/fmt/bundled/fmt.license.rst deleted file mode 100644 index f0ec3db..0000000 --- a/include/spdlog/fmt/bundled/fmt.license.rst +++ /dev/null @@ -1,27 +0,0 @@ -Copyright (c) 2012 - present, Victor Zverovich - -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. - ---- Optional exception to the license --- - -As an exception, if, as a result of your compiling your source code, portions -of this Software are embedded into a machine-executable object form of such -source code, you may redistribute such embedded portions in such object form -without including the above copyright and permission notices. diff --git a/include/spdlog/fmt/bundled/format-inl.h b/include/spdlog/fmt/bundled/format-inl.h deleted file mode 100644 index a887483..0000000 --- a/include/spdlog/fmt/bundled/format-inl.h +++ /dev/null @@ -1,1928 +0,0 @@ -// Formatting library for C++ - implementation -// -// Copyright (c) 2012 - 2016, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_FORMAT_INL_H_ -#define FMT_FORMAT_INL_H_ - -#ifndef FMT_MODULE -# include -# include // errno -# include -# include -# include - -# if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) -# include -# endif -#endif - -#if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE) -# include // _isatty -#endif - -#include "format.h" - -FMT_BEGIN_NAMESPACE -namespace detail { - -FMT_FUNC void assert_fail(const char* file, int line, const char* message) { - // Use unchecked std::fprintf to avoid triggering another assertion when - // writing to stderr fails - std::fprintf(stderr, "%s:%d: assertion failed: %s", file, line, message); - // Chosen instead of std::abort to satisfy Clang in CUDA mode during device - // code pass. - std::terminate(); -} - -FMT_FUNC void format_error_code(detail::buffer& out, int error_code, - string_view message) noexcept { - // Report error code making sure that the output fits into - // inline_buffer_size to avoid dynamic memory allocation and potential - // bad_alloc. - out.try_resize(0); - static const char SEP[] = ": "; - static const char ERROR_STR[] = "error "; - // Subtract 2 to account for terminating null characters in SEP and ERROR_STR. - size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2; - auto abs_value = static_cast>(error_code); - if (detail::is_negative(error_code)) { - abs_value = 0 - abs_value; - ++error_code_size; - } - error_code_size += detail::to_unsigned(detail::count_digits(abs_value)); - auto it = appender(out); - if (message.size() <= inline_buffer_size - error_code_size) - fmt::format_to(it, FMT_STRING("{}{}"), message, SEP); - fmt::format_to(it, FMT_STRING("{}{}"), ERROR_STR, error_code); - FMT_ASSERT(out.size() <= inline_buffer_size, ""); -} - -FMT_FUNC void report_error(format_func func, int error_code, - const char* message) noexcept { - memory_buffer full_message; - func(full_message, error_code, message); - // Don't use fwrite_fully because the latter may throw. - if (std::fwrite(full_message.data(), full_message.size(), 1, stderr) > 0) - std::fputc('\n', stderr); -} - -// A wrapper around fwrite that throws on error. -inline void fwrite_fully(const void* ptr, size_t count, FILE* stream) { - size_t written = std::fwrite(ptr, 1, count, stream); - if (written < count) - FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); -} - -#ifndef FMT_STATIC_THOUSANDS_SEPARATOR -template -locale_ref::locale_ref(const Locale& loc) : locale_(&loc) { - static_assert(std::is_same::value, ""); -} - -template auto locale_ref::get() const -> Locale { - static_assert(std::is_same::value, ""); - return locale_ ? *static_cast(locale_) : std::locale(); -} - -template -FMT_FUNC auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result { - auto& facet = std::use_facet>(loc.get()); - auto grouping = facet.grouping(); - auto thousands_sep = grouping.empty() ? Char() : facet.thousands_sep(); - return {std::move(grouping), thousands_sep}; -} -template -FMT_FUNC auto decimal_point_impl(locale_ref loc) -> Char { - return std::use_facet>(loc.get()) - .decimal_point(); -} -#else -template -FMT_FUNC auto thousands_sep_impl(locale_ref) -> thousands_sep_result { - return {"\03", FMT_STATIC_THOUSANDS_SEPARATOR}; -} -template FMT_FUNC Char decimal_point_impl(locale_ref) { - return '.'; -} -#endif - -FMT_FUNC auto write_loc(appender out, loc_value value, - const format_specs& specs, locale_ref loc) -> bool { -#ifdef FMT_STATIC_THOUSANDS_SEPARATOR - value.visit(loc_writer<>{ - out, specs, std::string(1, FMT_STATIC_THOUSANDS_SEPARATOR), "\3", "."}); - return true; -#else - auto locale = loc.get(); - // We cannot use the num_put facet because it may produce output in - // a wrong encoding. - using facet = format_facet; - if (std::has_facet(locale)) - return std::use_facet(locale).put(out, value, specs); - return facet(locale).put(out, value, specs); -#endif -} -} // namespace detail - -FMT_FUNC void report_error(const char* message) { - FMT_THROW(format_error(message)); -} - -template typename Locale::id format_facet::id; - -#ifndef FMT_STATIC_THOUSANDS_SEPARATOR -template format_facet::format_facet(Locale& loc) { - auto& numpunct = std::use_facet>(loc); - grouping_ = numpunct.grouping(); - if (!grouping_.empty()) separator_ = std::string(1, numpunct.thousands_sep()); -} - -template <> -FMT_API FMT_FUNC auto format_facet::do_put( - appender out, loc_value val, const format_specs& specs) const -> bool { - return val.visit( - detail::loc_writer<>{out, specs, separator_, grouping_, decimal_point_}); -} -#endif - -FMT_FUNC auto vsystem_error(int error_code, string_view fmt, format_args args) - -> std::system_error { - auto ec = std::error_code(error_code, std::generic_category()); - return std::system_error(ec, vformat(fmt, args)); -} - -namespace detail { - -template -inline auto operator==(basic_fp x, basic_fp y) -> bool { - return x.f == y.f && x.e == y.e; -} - -// Compilers should be able to optimize this into the ror instruction. -FMT_CONSTEXPR inline auto rotr(uint32_t n, uint32_t r) noexcept -> uint32_t { - r &= 31; - return (n >> r) | (n << (32 - r)); -} -FMT_CONSTEXPR inline auto rotr(uint64_t n, uint32_t r) noexcept -> uint64_t { - r &= 63; - return (n >> r) | (n << (64 - r)); -} - -// Implementation of Dragonbox algorithm: https://github.com/jk-jeon/dragonbox. -namespace dragonbox { -// Computes upper 64 bits of multiplication of a 32-bit unsigned integer and a -// 64-bit unsigned integer. -inline auto umul96_upper64(uint32_t x, uint64_t y) noexcept -> uint64_t { - return umul128_upper64(static_cast(x) << 32, y); -} - -// Computes lower 128 bits of multiplication of a 64-bit unsigned integer and a -// 128-bit unsigned integer. -inline auto umul192_lower128(uint64_t x, uint128_fallback y) noexcept - -> uint128_fallback { - uint64_t high = x * y.high(); - uint128_fallback high_low = umul128(x, y.low()); - return {high + high_low.high(), high_low.low()}; -} - -// Computes lower 64 bits of multiplication of a 32-bit unsigned integer and a -// 64-bit unsigned integer. -inline auto umul96_lower64(uint32_t x, uint64_t y) noexcept -> uint64_t { - return x * y; -} - -// Various fast log computations. -inline auto floor_log10_pow2_minus_log10_4_over_3(int e) noexcept -> int { - FMT_ASSERT(e <= 2936 && e >= -2985, "too large exponent"); - return (e * 631305 - 261663) >> 21; -} - -FMT_INLINE_VARIABLE constexpr struct { - uint32_t divisor; - int shift_amount; -} div_small_pow10_infos[] = {{10, 16}, {100, 16}}; - -// Replaces n by floor(n / pow(10, N)) returning true if and only if n is -// divisible by pow(10, N). -// Precondition: n <= pow(10, N + 1). -template -auto check_divisibility_and_divide_by_pow10(uint32_t& n) noexcept -> bool { - // The numbers below are chosen such that: - // 1. floor(n/d) = floor(nm / 2^k) where d=10 or d=100, - // 2. nm mod 2^k < m if and only if n is divisible by d, - // where m is magic_number, k is shift_amount - // and d is divisor. - // - // Item 1 is a common technique of replacing division by a constant with - // multiplication, see e.g. "Division by Invariant Integers Using - // Multiplication" by Granlund and Montgomery (1994). magic_number (m) is set - // to ceil(2^k/d) for large enough k. - // The idea for item 2 originates from Schubfach. - constexpr auto info = div_small_pow10_infos[N - 1]; - FMT_ASSERT(n <= info.divisor * 10, "n is too large"); - constexpr uint32_t magic_number = - (1u << info.shift_amount) / info.divisor + 1; - n *= magic_number; - const uint32_t comparison_mask = (1u << info.shift_amount) - 1; - bool result = (n & comparison_mask) < magic_number; - n >>= info.shift_amount; - return result; -} - -// Computes floor(n / pow(10, N)) for small n and N. -// Precondition: n <= pow(10, N + 1). -template auto small_division_by_pow10(uint32_t n) noexcept -> uint32_t { - constexpr auto info = div_small_pow10_infos[N - 1]; - FMT_ASSERT(n <= info.divisor * 10, "n is too large"); - constexpr uint32_t magic_number = - (1u << info.shift_amount) / info.divisor + 1; - return (n * magic_number) >> info.shift_amount; -} - -// Computes floor(n / 10^(kappa + 1)) (float) -inline auto divide_by_10_to_kappa_plus_1(uint32_t n) noexcept -> uint32_t { - // 1374389535 = ceil(2^37/100) - return static_cast((static_cast(n) * 1374389535) >> 37); -} -// Computes floor(n / 10^(kappa + 1)) (double) -inline auto divide_by_10_to_kappa_plus_1(uint64_t n) noexcept -> uint64_t { - // 2361183241434822607 = ceil(2^(64+7)/1000) - return umul128_upper64(n, 2361183241434822607ull) >> 7; -} - -// Various subroutines using pow10 cache -template struct cache_accessor; - -template <> struct cache_accessor { - using carrier_uint = float_info::carrier_uint; - using cache_entry_type = uint64_t; - - static auto get_cached_power(int k) noexcept -> uint64_t { - FMT_ASSERT(k >= float_info::min_k && k <= float_info::max_k, - "k is out of range"); - static constexpr const uint64_t pow10_significands[] = { - 0x81ceb32c4b43fcf5, 0xa2425ff75e14fc32, 0xcad2f7f5359a3b3f, - 0xfd87b5f28300ca0e, 0x9e74d1b791e07e49, 0xc612062576589ddb, - 0xf79687aed3eec552, 0x9abe14cd44753b53, 0xc16d9a0095928a28, - 0xf1c90080baf72cb2, 0x971da05074da7bef, 0xbce5086492111aeb, - 0xec1e4a7db69561a6, 0x9392ee8e921d5d08, 0xb877aa3236a4b44a, - 0xe69594bec44de15c, 0x901d7cf73ab0acda, 0xb424dc35095cd810, - 0xe12e13424bb40e14, 0x8cbccc096f5088cc, 0xafebff0bcb24aaff, - 0xdbe6fecebdedd5bf, 0x89705f4136b4a598, 0xabcc77118461cefd, - 0xd6bf94d5e57a42bd, 0x8637bd05af6c69b6, 0xa7c5ac471b478424, - 0xd1b71758e219652c, 0x83126e978d4fdf3c, 0xa3d70a3d70a3d70b, - 0xcccccccccccccccd, 0x8000000000000000, 0xa000000000000000, - 0xc800000000000000, 0xfa00000000000000, 0x9c40000000000000, - 0xc350000000000000, 0xf424000000000000, 0x9896800000000000, - 0xbebc200000000000, 0xee6b280000000000, 0x9502f90000000000, - 0xba43b74000000000, 0xe8d4a51000000000, 0x9184e72a00000000, - 0xb5e620f480000000, 0xe35fa931a0000000, 0x8e1bc9bf04000000, - 0xb1a2bc2ec5000000, 0xde0b6b3a76400000, 0x8ac7230489e80000, - 0xad78ebc5ac620000, 0xd8d726b7177a8000, 0x878678326eac9000, - 0xa968163f0a57b400, 0xd3c21bcecceda100, 0x84595161401484a0, - 0xa56fa5b99019a5c8, 0xcecb8f27f4200f3a, 0x813f3978f8940985, - 0xa18f07d736b90be6, 0xc9f2c9cd04674edf, 0xfc6f7c4045812297, - 0x9dc5ada82b70b59e, 0xc5371912364ce306, 0xf684df56c3e01bc7, - 0x9a130b963a6c115d, 0xc097ce7bc90715b4, 0xf0bdc21abb48db21, - 0x96769950b50d88f5, 0xbc143fa4e250eb32, 0xeb194f8e1ae525fe, - 0x92efd1b8d0cf37bf, 0xb7abc627050305ae, 0xe596b7b0c643c71a, - 0x8f7e32ce7bea5c70, 0xb35dbf821ae4f38c, 0xe0352f62a19e306f}; - return pow10_significands[k - float_info::min_k]; - } - - struct compute_mul_result { - carrier_uint result; - bool is_integer; - }; - struct compute_mul_parity_result { - bool parity; - bool is_integer; - }; - - static auto compute_mul(carrier_uint u, - const cache_entry_type& cache) noexcept - -> compute_mul_result { - auto r = umul96_upper64(u, cache); - return {static_cast(r >> 32), - static_cast(r) == 0}; - } - - static auto compute_delta(const cache_entry_type& cache, int beta) noexcept - -> uint32_t { - return static_cast(cache >> (64 - 1 - beta)); - } - - static auto compute_mul_parity(carrier_uint two_f, - const cache_entry_type& cache, - int beta) noexcept - -> compute_mul_parity_result { - FMT_ASSERT(beta >= 1, ""); - FMT_ASSERT(beta < 64, ""); - - auto r = umul96_lower64(two_f, cache); - return {((r >> (64 - beta)) & 1) != 0, - static_cast(r >> (32 - beta)) == 0}; - } - - static auto compute_left_endpoint_for_shorter_interval_case( - const cache_entry_type& cache, int beta) noexcept -> carrier_uint { - return static_cast( - (cache - (cache >> (num_significand_bits() + 2))) >> - (64 - num_significand_bits() - 1 - beta)); - } - - static auto compute_right_endpoint_for_shorter_interval_case( - const cache_entry_type& cache, int beta) noexcept -> carrier_uint { - return static_cast( - (cache + (cache >> (num_significand_bits() + 1))) >> - (64 - num_significand_bits() - 1 - beta)); - } - - static auto compute_round_up_for_shorter_interval_case( - const cache_entry_type& cache, int beta) noexcept -> carrier_uint { - return (static_cast( - cache >> (64 - num_significand_bits() - 2 - beta)) + - 1) / - 2; - } -}; - -template <> struct cache_accessor { - using carrier_uint = float_info::carrier_uint; - using cache_entry_type = uint128_fallback; - - static auto get_cached_power(int k) noexcept -> uint128_fallback { - FMT_ASSERT(k >= float_info::min_k && k <= float_info::max_k, - "k is out of range"); - - static constexpr const uint128_fallback pow10_significands[] = { -#if FMT_USE_FULL_CACHE_DRAGONBOX - {0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b}, - {0x9faacf3df73609b1, 0x77b191618c54e9ad}, - {0xc795830d75038c1d, 0xd59df5b9ef6a2418}, - {0xf97ae3d0d2446f25, 0x4b0573286b44ad1e}, - {0x9becce62836ac577, 0x4ee367f9430aec33}, - {0xc2e801fb244576d5, 0x229c41f793cda740}, - {0xf3a20279ed56d48a, 0x6b43527578c11110}, - {0x9845418c345644d6, 0x830a13896b78aaaa}, - {0xbe5691ef416bd60c, 0x23cc986bc656d554}, - {0xedec366b11c6cb8f, 0x2cbfbe86b7ec8aa9}, - {0x94b3a202eb1c3f39, 0x7bf7d71432f3d6aa}, - {0xb9e08a83a5e34f07, 0xdaf5ccd93fb0cc54}, - {0xe858ad248f5c22c9, 0xd1b3400f8f9cff69}, - {0x91376c36d99995be, 0x23100809b9c21fa2}, - {0xb58547448ffffb2d, 0xabd40a0c2832a78b}, - {0xe2e69915b3fff9f9, 0x16c90c8f323f516d}, - {0x8dd01fad907ffc3b, 0xae3da7d97f6792e4}, - {0xb1442798f49ffb4a, 0x99cd11cfdf41779d}, - {0xdd95317f31c7fa1d, 0x40405643d711d584}, - {0x8a7d3eef7f1cfc52, 0x482835ea666b2573}, - {0xad1c8eab5ee43b66, 0xda3243650005eed0}, - {0xd863b256369d4a40, 0x90bed43e40076a83}, - {0x873e4f75e2224e68, 0x5a7744a6e804a292}, - {0xa90de3535aaae202, 0x711515d0a205cb37}, - {0xd3515c2831559a83, 0x0d5a5b44ca873e04}, - {0x8412d9991ed58091, 0xe858790afe9486c3}, - {0xa5178fff668ae0b6, 0x626e974dbe39a873}, - {0xce5d73ff402d98e3, 0xfb0a3d212dc81290}, - {0x80fa687f881c7f8e, 0x7ce66634bc9d0b9a}, - {0xa139029f6a239f72, 0x1c1fffc1ebc44e81}, - {0xc987434744ac874e, 0xa327ffb266b56221}, - {0xfbe9141915d7a922, 0x4bf1ff9f0062baa9}, - {0x9d71ac8fada6c9b5, 0x6f773fc3603db4aa}, - {0xc4ce17b399107c22, 0xcb550fb4384d21d4}, - {0xf6019da07f549b2b, 0x7e2a53a146606a49}, - {0x99c102844f94e0fb, 0x2eda7444cbfc426e}, - {0xc0314325637a1939, 0xfa911155fefb5309}, - {0xf03d93eebc589f88, 0x793555ab7eba27cb}, - {0x96267c7535b763b5, 0x4bc1558b2f3458df}, - {0xbbb01b9283253ca2, 0x9eb1aaedfb016f17}, - {0xea9c227723ee8bcb, 0x465e15a979c1cadd}, - {0x92a1958a7675175f, 0x0bfacd89ec191eca}, - {0xb749faed14125d36, 0xcef980ec671f667c}, - {0xe51c79a85916f484, 0x82b7e12780e7401b}, - {0x8f31cc0937ae58d2, 0xd1b2ecb8b0908811}, - {0xb2fe3f0b8599ef07, 0x861fa7e6dcb4aa16}, - {0xdfbdcece67006ac9, 0x67a791e093e1d49b}, - {0x8bd6a141006042bd, 0xe0c8bb2c5c6d24e1}, - {0xaecc49914078536d, 0x58fae9f773886e19}, - {0xda7f5bf590966848, 0xaf39a475506a899f}, - {0x888f99797a5e012d, 0x6d8406c952429604}, - {0xaab37fd7d8f58178, 0xc8e5087ba6d33b84}, - {0xd5605fcdcf32e1d6, 0xfb1e4a9a90880a65}, - {0x855c3be0a17fcd26, 0x5cf2eea09a550680}, - {0xa6b34ad8c9dfc06f, 0xf42faa48c0ea481f}, - {0xd0601d8efc57b08b, 0xf13b94daf124da27}, - {0x823c12795db6ce57, 0x76c53d08d6b70859}, - {0xa2cb1717b52481ed, 0x54768c4b0c64ca6f}, - {0xcb7ddcdda26da268, 0xa9942f5dcf7dfd0a}, - {0xfe5d54150b090b02, 0xd3f93b35435d7c4d}, - {0x9efa548d26e5a6e1, 0xc47bc5014a1a6db0}, - {0xc6b8e9b0709f109a, 0x359ab6419ca1091c}, - {0xf867241c8cc6d4c0, 0xc30163d203c94b63}, - {0x9b407691d7fc44f8, 0x79e0de63425dcf1e}, - {0xc21094364dfb5636, 0x985915fc12f542e5}, - {0xf294b943e17a2bc4, 0x3e6f5b7b17b2939e}, - {0x979cf3ca6cec5b5a, 0xa705992ceecf9c43}, - {0xbd8430bd08277231, 0x50c6ff782a838354}, - {0xece53cec4a314ebd, 0xa4f8bf5635246429}, - {0x940f4613ae5ed136, 0x871b7795e136be9a}, - {0xb913179899f68584, 0x28e2557b59846e40}, - {0xe757dd7ec07426e5, 0x331aeada2fe589d0}, - {0x9096ea6f3848984f, 0x3ff0d2c85def7622}, - {0xb4bca50b065abe63, 0x0fed077a756b53aa}, - {0xe1ebce4dc7f16dfb, 0xd3e8495912c62895}, - {0x8d3360f09cf6e4bd, 0x64712dd7abbbd95d}, - {0xb080392cc4349dec, 0xbd8d794d96aacfb4}, - {0xdca04777f541c567, 0xecf0d7a0fc5583a1}, - {0x89e42caaf9491b60, 0xf41686c49db57245}, - {0xac5d37d5b79b6239, 0x311c2875c522ced6}, - {0xd77485cb25823ac7, 0x7d633293366b828c}, - {0x86a8d39ef77164bc, 0xae5dff9c02033198}, - {0xa8530886b54dbdeb, 0xd9f57f830283fdfd}, - {0xd267caa862a12d66, 0xd072df63c324fd7c}, - {0x8380dea93da4bc60, 0x4247cb9e59f71e6e}, - {0xa46116538d0deb78, 0x52d9be85f074e609}, - {0xcd795be870516656, 0x67902e276c921f8c}, - {0x806bd9714632dff6, 0x00ba1cd8a3db53b7}, - {0xa086cfcd97bf97f3, 0x80e8a40eccd228a5}, - {0xc8a883c0fdaf7df0, 0x6122cd128006b2ce}, - {0xfad2a4b13d1b5d6c, 0x796b805720085f82}, - {0x9cc3a6eec6311a63, 0xcbe3303674053bb1}, - {0xc3f490aa77bd60fc, 0xbedbfc4411068a9d}, - {0xf4f1b4d515acb93b, 0xee92fb5515482d45}, - {0x991711052d8bf3c5, 0x751bdd152d4d1c4b}, - {0xbf5cd54678eef0b6, 0xd262d45a78a0635e}, - {0xef340a98172aace4, 0x86fb897116c87c35}, - {0x9580869f0e7aac0e, 0xd45d35e6ae3d4da1}, - {0xbae0a846d2195712, 0x8974836059cca10a}, - {0xe998d258869facd7, 0x2bd1a438703fc94c}, - {0x91ff83775423cc06, 0x7b6306a34627ddd0}, - {0xb67f6455292cbf08, 0x1a3bc84c17b1d543}, - {0xe41f3d6a7377eeca, 0x20caba5f1d9e4a94}, - {0x8e938662882af53e, 0x547eb47b7282ee9d}, - {0xb23867fb2a35b28d, 0xe99e619a4f23aa44}, - {0xdec681f9f4c31f31, 0x6405fa00e2ec94d5}, - {0x8b3c113c38f9f37e, 0xde83bc408dd3dd05}, - {0xae0b158b4738705e, 0x9624ab50b148d446}, - {0xd98ddaee19068c76, 0x3badd624dd9b0958}, - {0x87f8a8d4cfa417c9, 0xe54ca5d70a80e5d7}, - {0xa9f6d30a038d1dbc, 0x5e9fcf4ccd211f4d}, - {0xd47487cc8470652b, 0x7647c32000696720}, - {0x84c8d4dfd2c63f3b, 0x29ecd9f40041e074}, - {0xa5fb0a17c777cf09, 0xf468107100525891}, - {0xcf79cc9db955c2cc, 0x7182148d4066eeb5}, - {0x81ac1fe293d599bf, 0xc6f14cd848405531}, - {0xa21727db38cb002f, 0xb8ada00e5a506a7d}, - {0xca9cf1d206fdc03b, 0xa6d90811f0e4851d}, - {0xfd442e4688bd304a, 0x908f4a166d1da664}, - {0x9e4a9cec15763e2e, 0x9a598e4e043287ff}, - {0xc5dd44271ad3cdba, 0x40eff1e1853f29fe}, - {0xf7549530e188c128, 0xd12bee59e68ef47d}, - {0x9a94dd3e8cf578b9, 0x82bb74f8301958cf}, - {0xc13a148e3032d6e7, 0xe36a52363c1faf02}, - {0xf18899b1bc3f8ca1, 0xdc44e6c3cb279ac2}, - {0x96f5600f15a7b7e5, 0x29ab103a5ef8c0ba}, - {0xbcb2b812db11a5de, 0x7415d448f6b6f0e8}, - {0xebdf661791d60f56, 0x111b495b3464ad22}, - {0x936b9fcebb25c995, 0xcab10dd900beec35}, - {0xb84687c269ef3bfb, 0x3d5d514f40eea743}, - {0xe65829b3046b0afa, 0x0cb4a5a3112a5113}, - {0x8ff71a0fe2c2e6dc, 0x47f0e785eaba72ac}, - {0xb3f4e093db73a093, 0x59ed216765690f57}, - {0xe0f218b8d25088b8, 0x306869c13ec3532d}, - {0x8c974f7383725573, 0x1e414218c73a13fc}, - {0xafbd2350644eeacf, 0xe5d1929ef90898fb}, - {0xdbac6c247d62a583, 0xdf45f746b74abf3a}, - {0x894bc396ce5da772, 0x6b8bba8c328eb784}, - {0xab9eb47c81f5114f, 0x066ea92f3f326565}, - {0xd686619ba27255a2, 0xc80a537b0efefebe}, - {0x8613fd0145877585, 0xbd06742ce95f5f37}, - {0xa798fc4196e952e7, 0x2c48113823b73705}, - {0xd17f3b51fca3a7a0, 0xf75a15862ca504c6}, - {0x82ef85133de648c4, 0x9a984d73dbe722fc}, - {0xa3ab66580d5fdaf5, 0xc13e60d0d2e0ebbb}, - {0xcc963fee10b7d1b3, 0x318df905079926a9}, - {0xffbbcfe994e5c61f, 0xfdf17746497f7053}, - {0x9fd561f1fd0f9bd3, 0xfeb6ea8bedefa634}, - {0xc7caba6e7c5382c8, 0xfe64a52ee96b8fc1}, - {0xf9bd690a1b68637b, 0x3dfdce7aa3c673b1}, - {0x9c1661a651213e2d, 0x06bea10ca65c084f}, - {0xc31bfa0fe5698db8, 0x486e494fcff30a63}, - {0xf3e2f893dec3f126, 0x5a89dba3c3efccfb}, - {0x986ddb5c6b3a76b7, 0xf89629465a75e01d}, - {0xbe89523386091465, 0xf6bbb397f1135824}, - {0xee2ba6c0678b597f, 0x746aa07ded582e2d}, - {0x94db483840b717ef, 0xa8c2a44eb4571cdd}, - {0xba121a4650e4ddeb, 0x92f34d62616ce414}, - {0xe896a0d7e51e1566, 0x77b020baf9c81d18}, - {0x915e2486ef32cd60, 0x0ace1474dc1d122f}, - {0xb5b5ada8aaff80b8, 0x0d819992132456bb}, - {0xe3231912d5bf60e6, 0x10e1fff697ed6c6a}, - {0x8df5efabc5979c8f, 0xca8d3ffa1ef463c2}, - {0xb1736b96b6fd83b3, 0xbd308ff8a6b17cb3}, - {0xddd0467c64bce4a0, 0xac7cb3f6d05ddbdf}, - {0x8aa22c0dbef60ee4, 0x6bcdf07a423aa96c}, - {0xad4ab7112eb3929d, 0x86c16c98d2c953c7}, - {0xd89d64d57a607744, 0xe871c7bf077ba8b8}, - {0x87625f056c7c4a8b, 0x11471cd764ad4973}, - {0xa93af6c6c79b5d2d, 0xd598e40d3dd89bd0}, - {0xd389b47879823479, 0x4aff1d108d4ec2c4}, - {0x843610cb4bf160cb, 0xcedf722a585139bb}, - {0xa54394fe1eedb8fe, 0xc2974eb4ee658829}, - {0xce947a3da6a9273e, 0x733d226229feea33}, - {0x811ccc668829b887, 0x0806357d5a3f5260}, - {0xa163ff802a3426a8, 0xca07c2dcb0cf26f8}, - {0xc9bcff6034c13052, 0xfc89b393dd02f0b6}, - {0xfc2c3f3841f17c67, 0xbbac2078d443ace3}, - {0x9d9ba7832936edc0, 0xd54b944b84aa4c0e}, - {0xc5029163f384a931, 0x0a9e795e65d4df12}, - {0xf64335bcf065d37d, 0x4d4617b5ff4a16d6}, - {0x99ea0196163fa42e, 0x504bced1bf8e4e46}, - {0xc06481fb9bcf8d39, 0xe45ec2862f71e1d7}, - {0xf07da27a82c37088, 0x5d767327bb4e5a4d}, - {0x964e858c91ba2655, 0x3a6a07f8d510f870}, - {0xbbe226efb628afea, 0x890489f70a55368c}, - {0xeadab0aba3b2dbe5, 0x2b45ac74ccea842f}, - {0x92c8ae6b464fc96f, 0x3b0b8bc90012929e}, - {0xb77ada0617e3bbcb, 0x09ce6ebb40173745}, - {0xe55990879ddcaabd, 0xcc420a6a101d0516}, - {0x8f57fa54c2a9eab6, 0x9fa946824a12232e}, - {0xb32df8e9f3546564, 0x47939822dc96abfa}, - {0xdff9772470297ebd, 0x59787e2b93bc56f8}, - {0x8bfbea76c619ef36, 0x57eb4edb3c55b65b}, - {0xaefae51477a06b03, 0xede622920b6b23f2}, - {0xdab99e59958885c4, 0xe95fab368e45ecee}, - {0x88b402f7fd75539b, 0x11dbcb0218ebb415}, - {0xaae103b5fcd2a881, 0xd652bdc29f26a11a}, - {0xd59944a37c0752a2, 0x4be76d3346f04960}, - {0x857fcae62d8493a5, 0x6f70a4400c562ddc}, - {0xa6dfbd9fb8e5b88e, 0xcb4ccd500f6bb953}, - {0xd097ad07a71f26b2, 0x7e2000a41346a7a8}, - {0x825ecc24c873782f, 0x8ed400668c0c28c9}, - {0xa2f67f2dfa90563b, 0x728900802f0f32fb}, - {0xcbb41ef979346bca, 0x4f2b40a03ad2ffba}, - {0xfea126b7d78186bc, 0xe2f610c84987bfa9}, - {0x9f24b832e6b0f436, 0x0dd9ca7d2df4d7ca}, - {0xc6ede63fa05d3143, 0x91503d1c79720dbc}, - {0xf8a95fcf88747d94, 0x75a44c6397ce912b}, - {0x9b69dbe1b548ce7c, 0xc986afbe3ee11abb}, - {0xc24452da229b021b, 0xfbe85badce996169}, - {0xf2d56790ab41c2a2, 0xfae27299423fb9c4}, - {0x97c560ba6b0919a5, 0xdccd879fc967d41b}, - {0xbdb6b8e905cb600f, 0x5400e987bbc1c921}, - {0xed246723473e3813, 0x290123e9aab23b69}, - {0x9436c0760c86e30b, 0xf9a0b6720aaf6522}, - {0xb94470938fa89bce, 0xf808e40e8d5b3e6a}, - {0xe7958cb87392c2c2, 0xb60b1d1230b20e05}, - {0x90bd77f3483bb9b9, 0xb1c6f22b5e6f48c3}, - {0xb4ecd5f01a4aa828, 0x1e38aeb6360b1af4}, - {0xe2280b6c20dd5232, 0x25c6da63c38de1b1}, - {0x8d590723948a535f, 0x579c487e5a38ad0f}, - {0xb0af48ec79ace837, 0x2d835a9df0c6d852}, - {0xdcdb1b2798182244, 0xf8e431456cf88e66}, - {0x8a08f0f8bf0f156b, 0x1b8e9ecb641b5900}, - {0xac8b2d36eed2dac5, 0xe272467e3d222f40}, - {0xd7adf884aa879177, 0x5b0ed81dcc6abb10}, - {0x86ccbb52ea94baea, 0x98e947129fc2b4ea}, - {0xa87fea27a539e9a5, 0x3f2398d747b36225}, - {0xd29fe4b18e88640e, 0x8eec7f0d19a03aae}, - {0x83a3eeeef9153e89, 0x1953cf68300424ad}, - {0xa48ceaaab75a8e2b, 0x5fa8c3423c052dd8}, - {0xcdb02555653131b6, 0x3792f412cb06794e}, - {0x808e17555f3ebf11, 0xe2bbd88bbee40bd1}, - {0xa0b19d2ab70e6ed6, 0x5b6aceaeae9d0ec5}, - {0xc8de047564d20a8b, 0xf245825a5a445276}, - {0xfb158592be068d2e, 0xeed6e2f0f0d56713}, - {0x9ced737bb6c4183d, 0x55464dd69685606c}, - {0xc428d05aa4751e4c, 0xaa97e14c3c26b887}, - {0xf53304714d9265df, 0xd53dd99f4b3066a9}, - {0x993fe2c6d07b7fab, 0xe546a8038efe402a}, - {0xbf8fdb78849a5f96, 0xde98520472bdd034}, - {0xef73d256a5c0f77c, 0x963e66858f6d4441}, - {0x95a8637627989aad, 0xdde7001379a44aa9}, - {0xbb127c53b17ec159, 0x5560c018580d5d53}, - {0xe9d71b689dde71af, 0xaab8f01e6e10b4a7}, - {0x9226712162ab070d, 0xcab3961304ca70e9}, - {0xb6b00d69bb55c8d1, 0x3d607b97c5fd0d23}, - {0xe45c10c42a2b3b05, 0x8cb89a7db77c506b}, - {0x8eb98a7a9a5b04e3, 0x77f3608e92adb243}, - {0xb267ed1940f1c61c, 0x55f038b237591ed4}, - {0xdf01e85f912e37a3, 0x6b6c46dec52f6689}, - {0x8b61313bbabce2c6, 0x2323ac4b3b3da016}, - {0xae397d8aa96c1b77, 0xabec975e0a0d081b}, - {0xd9c7dced53c72255, 0x96e7bd358c904a22}, - {0x881cea14545c7575, 0x7e50d64177da2e55}, - {0xaa242499697392d2, 0xdde50bd1d5d0b9ea}, - {0xd4ad2dbfc3d07787, 0x955e4ec64b44e865}, - {0x84ec3c97da624ab4, 0xbd5af13bef0b113f}, - {0xa6274bbdd0fadd61, 0xecb1ad8aeacdd58f}, - {0xcfb11ead453994ba, 0x67de18eda5814af3}, - {0x81ceb32c4b43fcf4, 0x80eacf948770ced8}, - {0xa2425ff75e14fc31, 0xa1258379a94d028e}, - {0xcad2f7f5359a3b3e, 0x096ee45813a04331}, - {0xfd87b5f28300ca0d, 0x8bca9d6e188853fd}, - {0x9e74d1b791e07e48, 0x775ea264cf55347e}, - {0xc612062576589dda, 0x95364afe032a819e}, - {0xf79687aed3eec551, 0x3a83ddbd83f52205}, - {0x9abe14cd44753b52, 0xc4926a9672793543}, - {0xc16d9a0095928a27, 0x75b7053c0f178294}, - {0xf1c90080baf72cb1, 0x5324c68b12dd6339}, - {0x971da05074da7bee, 0xd3f6fc16ebca5e04}, - {0xbce5086492111aea, 0x88f4bb1ca6bcf585}, - {0xec1e4a7db69561a5, 0x2b31e9e3d06c32e6}, - {0x9392ee8e921d5d07, 0x3aff322e62439fd0}, - {0xb877aa3236a4b449, 0x09befeb9fad487c3}, - {0xe69594bec44de15b, 0x4c2ebe687989a9b4}, - {0x901d7cf73ab0acd9, 0x0f9d37014bf60a11}, - {0xb424dc35095cd80f, 0x538484c19ef38c95}, - {0xe12e13424bb40e13, 0x2865a5f206b06fba}, - {0x8cbccc096f5088cb, 0xf93f87b7442e45d4}, - {0xafebff0bcb24aafe, 0xf78f69a51539d749}, - {0xdbe6fecebdedd5be, 0xb573440e5a884d1c}, - {0x89705f4136b4a597, 0x31680a88f8953031}, - {0xabcc77118461cefc, 0xfdc20d2b36ba7c3e}, - {0xd6bf94d5e57a42bc, 0x3d32907604691b4d}, - {0x8637bd05af6c69b5, 0xa63f9a49c2c1b110}, - {0xa7c5ac471b478423, 0x0fcf80dc33721d54}, - {0xd1b71758e219652b, 0xd3c36113404ea4a9}, - {0x83126e978d4fdf3b, 0x645a1cac083126ea}, - {0xa3d70a3d70a3d70a, 0x3d70a3d70a3d70a4}, - {0xcccccccccccccccc, 0xcccccccccccccccd}, - {0x8000000000000000, 0x0000000000000000}, - {0xa000000000000000, 0x0000000000000000}, - {0xc800000000000000, 0x0000000000000000}, - {0xfa00000000000000, 0x0000000000000000}, - {0x9c40000000000000, 0x0000000000000000}, - {0xc350000000000000, 0x0000000000000000}, - {0xf424000000000000, 0x0000000000000000}, - {0x9896800000000000, 0x0000000000000000}, - {0xbebc200000000000, 0x0000000000000000}, - {0xee6b280000000000, 0x0000000000000000}, - {0x9502f90000000000, 0x0000000000000000}, - {0xba43b74000000000, 0x0000000000000000}, - {0xe8d4a51000000000, 0x0000000000000000}, - {0x9184e72a00000000, 0x0000000000000000}, - {0xb5e620f480000000, 0x0000000000000000}, - {0xe35fa931a0000000, 0x0000000000000000}, - {0x8e1bc9bf04000000, 0x0000000000000000}, - {0xb1a2bc2ec5000000, 0x0000000000000000}, - {0xde0b6b3a76400000, 0x0000000000000000}, - {0x8ac7230489e80000, 0x0000000000000000}, - {0xad78ebc5ac620000, 0x0000000000000000}, - {0xd8d726b7177a8000, 0x0000000000000000}, - {0x878678326eac9000, 0x0000000000000000}, - {0xa968163f0a57b400, 0x0000000000000000}, - {0xd3c21bcecceda100, 0x0000000000000000}, - {0x84595161401484a0, 0x0000000000000000}, - {0xa56fa5b99019a5c8, 0x0000000000000000}, - {0xcecb8f27f4200f3a, 0x0000000000000000}, - {0x813f3978f8940984, 0x4000000000000000}, - {0xa18f07d736b90be5, 0x5000000000000000}, - {0xc9f2c9cd04674ede, 0xa400000000000000}, - {0xfc6f7c4045812296, 0x4d00000000000000}, - {0x9dc5ada82b70b59d, 0xf020000000000000}, - {0xc5371912364ce305, 0x6c28000000000000}, - {0xf684df56c3e01bc6, 0xc732000000000000}, - {0x9a130b963a6c115c, 0x3c7f400000000000}, - {0xc097ce7bc90715b3, 0x4b9f100000000000}, - {0xf0bdc21abb48db20, 0x1e86d40000000000}, - {0x96769950b50d88f4, 0x1314448000000000}, - {0xbc143fa4e250eb31, 0x17d955a000000000}, - {0xeb194f8e1ae525fd, 0x5dcfab0800000000}, - {0x92efd1b8d0cf37be, 0x5aa1cae500000000}, - {0xb7abc627050305ad, 0xf14a3d9e40000000}, - {0xe596b7b0c643c719, 0x6d9ccd05d0000000}, - {0x8f7e32ce7bea5c6f, 0xe4820023a2000000}, - {0xb35dbf821ae4f38b, 0xdda2802c8a800000}, - {0xe0352f62a19e306e, 0xd50b2037ad200000}, - {0x8c213d9da502de45, 0x4526f422cc340000}, - {0xaf298d050e4395d6, 0x9670b12b7f410000}, - {0xdaf3f04651d47b4c, 0x3c0cdd765f114000}, - {0x88d8762bf324cd0f, 0xa5880a69fb6ac800}, - {0xab0e93b6efee0053, 0x8eea0d047a457a00}, - {0xd5d238a4abe98068, 0x72a4904598d6d880}, - {0x85a36366eb71f041, 0x47a6da2b7f864750}, - {0xa70c3c40a64e6c51, 0x999090b65f67d924}, - {0xd0cf4b50cfe20765, 0xfff4b4e3f741cf6d}, - {0x82818f1281ed449f, 0xbff8f10e7a8921a5}, - {0xa321f2d7226895c7, 0xaff72d52192b6a0e}, - {0xcbea6f8ceb02bb39, 0x9bf4f8a69f764491}, - {0xfee50b7025c36a08, 0x02f236d04753d5b5}, - {0x9f4f2726179a2245, 0x01d762422c946591}, - {0xc722f0ef9d80aad6, 0x424d3ad2b7b97ef6}, - {0xf8ebad2b84e0d58b, 0xd2e0898765a7deb3}, - {0x9b934c3b330c8577, 0x63cc55f49f88eb30}, - {0xc2781f49ffcfa6d5, 0x3cbf6b71c76b25fc}, - {0xf316271c7fc3908a, 0x8bef464e3945ef7b}, - {0x97edd871cfda3a56, 0x97758bf0e3cbb5ad}, - {0xbde94e8e43d0c8ec, 0x3d52eeed1cbea318}, - {0xed63a231d4c4fb27, 0x4ca7aaa863ee4bde}, - {0x945e455f24fb1cf8, 0x8fe8caa93e74ef6b}, - {0xb975d6b6ee39e436, 0xb3e2fd538e122b45}, - {0xe7d34c64a9c85d44, 0x60dbbca87196b617}, - {0x90e40fbeea1d3a4a, 0xbc8955e946fe31ce}, - {0xb51d13aea4a488dd, 0x6babab6398bdbe42}, - {0xe264589a4dcdab14, 0xc696963c7eed2dd2}, - {0x8d7eb76070a08aec, 0xfc1e1de5cf543ca3}, - {0xb0de65388cc8ada8, 0x3b25a55f43294bcc}, - {0xdd15fe86affad912, 0x49ef0eb713f39ebf}, - {0x8a2dbf142dfcc7ab, 0x6e3569326c784338}, - {0xacb92ed9397bf996, 0x49c2c37f07965405}, - {0xd7e77a8f87daf7fb, 0xdc33745ec97be907}, - {0x86f0ac99b4e8dafd, 0x69a028bb3ded71a4}, - {0xa8acd7c0222311bc, 0xc40832ea0d68ce0d}, - {0xd2d80db02aabd62b, 0xf50a3fa490c30191}, - {0x83c7088e1aab65db, 0x792667c6da79e0fb}, - {0xa4b8cab1a1563f52, 0x577001b891185939}, - {0xcde6fd5e09abcf26, 0xed4c0226b55e6f87}, - {0x80b05e5ac60b6178, 0x544f8158315b05b5}, - {0xa0dc75f1778e39d6, 0x696361ae3db1c722}, - {0xc913936dd571c84c, 0x03bc3a19cd1e38ea}, - {0xfb5878494ace3a5f, 0x04ab48a04065c724}, - {0x9d174b2dcec0e47b, 0x62eb0d64283f9c77}, - {0xc45d1df942711d9a, 0x3ba5d0bd324f8395}, - {0xf5746577930d6500, 0xca8f44ec7ee3647a}, - {0x9968bf6abbe85f20, 0x7e998b13cf4e1ecc}, - {0xbfc2ef456ae276e8, 0x9e3fedd8c321a67f}, - {0xefb3ab16c59b14a2, 0xc5cfe94ef3ea101f}, - {0x95d04aee3b80ece5, 0xbba1f1d158724a13}, - {0xbb445da9ca61281f, 0x2a8a6e45ae8edc98}, - {0xea1575143cf97226, 0xf52d09d71a3293be}, - {0x924d692ca61be758, 0x593c2626705f9c57}, - {0xb6e0c377cfa2e12e, 0x6f8b2fb00c77836d}, - {0xe498f455c38b997a, 0x0b6dfb9c0f956448}, - {0x8edf98b59a373fec, 0x4724bd4189bd5ead}, - {0xb2977ee300c50fe7, 0x58edec91ec2cb658}, - {0xdf3d5e9bc0f653e1, 0x2f2967b66737e3ee}, - {0x8b865b215899f46c, 0xbd79e0d20082ee75}, - {0xae67f1e9aec07187, 0xecd8590680a3aa12}, - {0xda01ee641a708de9, 0xe80e6f4820cc9496}, - {0x884134fe908658b2, 0x3109058d147fdcde}, - {0xaa51823e34a7eede, 0xbd4b46f0599fd416}, - {0xd4e5e2cdc1d1ea96, 0x6c9e18ac7007c91b}, - {0x850fadc09923329e, 0x03e2cf6bc604ddb1}, - {0xa6539930bf6bff45, 0x84db8346b786151d}, - {0xcfe87f7cef46ff16, 0xe612641865679a64}, - {0x81f14fae158c5f6e, 0x4fcb7e8f3f60c07f}, - {0xa26da3999aef7749, 0xe3be5e330f38f09e}, - {0xcb090c8001ab551c, 0x5cadf5bfd3072cc6}, - {0xfdcb4fa002162a63, 0x73d9732fc7c8f7f7}, - {0x9e9f11c4014dda7e, 0x2867e7fddcdd9afb}, - {0xc646d63501a1511d, 0xb281e1fd541501b9}, - {0xf7d88bc24209a565, 0x1f225a7ca91a4227}, - {0x9ae757596946075f, 0x3375788de9b06959}, - {0xc1a12d2fc3978937, 0x0052d6b1641c83af}, - {0xf209787bb47d6b84, 0xc0678c5dbd23a49b}, - {0x9745eb4d50ce6332, 0xf840b7ba963646e1}, - {0xbd176620a501fbff, 0xb650e5a93bc3d899}, - {0xec5d3fa8ce427aff, 0xa3e51f138ab4cebf}, - {0x93ba47c980e98cdf, 0xc66f336c36b10138}, - {0xb8a8d9bbe123f017, 0xb80b0047445d4185}, - {0xe6d3102ad96cec1d, 0xa60dc059157491e6}, - {0x9043ea1ac7e41392, 0x87c89837ad68db30}, - {0xb454e4a179dd1877, 0x29babe4598c311fc}, - {0xe16a1dc9d8545e94, 0xf4296dd6fef3d67b}, - {0x8ce2529e2734bb1d, 0x1899e4a65f58660d}, - {0xb01ae745b101e9e4, 0x5ec05dcff72e7f90}, - {0xdc21a1171d42645d, 0x76707543f4fa1f74}, - {0x899504ae72497eba, 0x6a06494a791c53a9}, - {0xabfa45da0edbde69, 0x0487db9d17636893}, - {0xd6f8d7509292d603, 0x45a9d2845d3c42b7}, - {0x865b86925b9bc5c2, 0x0b8a2392ba45a9b3}, - {0xa7f26836f282b732, 0x8e6cac7768d7141f}, - {0xd1ef0244af2364ff, 0x3207d795430cd927}, - {0x8335616aed761f1f, 0x7f44e6bd49e807b9}, - {0xa402b9c5a8d3a6e7, 0x5f16206c9c6209a7}, - {0xcd036837130890a1, 0x36dba887c37a8c10}, - {0x802221226be55a64, 0xc2494954da2c978a}, - {0xa02aa96b06deb0fd, 0xf2db9baa10b7bd6d}, - {0xc83553c5c8965d3d, 0x6f92829494e5acc8}, - {0xfa42a8b73abbf48c, 0xcb772339ba1f17fa}, - {0x9c69a97284b578d7, 0xff2a760414536efc}, - {0xc38413cf25e2d70d, 0xfef5138519684abb}, - {0xf46518c2ef5b8cd1, 0x7eb258665fc25d6a}, - {0x98bf2f79d5993802, 0xef2f773ffbd97a62}, - {0xbeeefb584aff8603, 0xaafb550ffacfd8fb}, - {0xeeaaba2e5dbf6784, 0x95ba2a53f983cf39}, - {0x952ab45cfa97a0b2, 0xdd945a747bf26184}, - {0xba756174393d88df, 0x94f971119aeef9e5}, - {0xe912b9d1478ceb17, 0x7a37cd5601aab85e}, - {0x91abb422ccb812ee, 0xac62e055c10ab33b}, - {0xb616a12b7fe617aa, 0x577b986b314d600a}, - {0xe39c49765fdf9d94, 0xed5a7e85fda0b80c}, - {0x8e41ade9fbebc27d, 0x14588f13be847308}, - {0xb1d219647ae6b31c, 0x596eb2d8ae258fc9}, - {0xde469fbd99a05fe3, 0x6fca5f8ed9aef3bc}, - {0x8aec23d680043bee, 0x25de7bb9480d5855}, - {0xada72ccc20054ae9, 0xaf561aa79a10ae6b}, - {0xd910f7ff28069da4, 0x1b2ba1518094da05}, - {0x87aa9aff79042286, 0x90fb44d2f05d0843}, - {0xa99541bf57452b28, 0x353a1607ac744a54}, - {0xd3fa922f2d1675f2, 0x42889b8997915ce9}, - {0x847c9b5d7c2e09b7, 0x69956135febada12}, - {0xa59bc234db398c25, 0x43fab9837e699096}, - {0xcf02b2c21207ef2e, 0x94f967e45e03f4bc}, - {0x8161afb94b44f57d, 0x1d1be0eebac278f6}, - {0xa1ba1ba79e1632dc, 0x6462d92a69731733}, - {0xca28a291859bbf93, 0x7d7b8f7503cfdcff}, - {0xfcb2cb35e702af78, 0x5cda735244c3d43f}, - {0x9defbf01b061adab, 0x3a0888136afa64a8}, - {0xc56baec21c7a1916, 0x088aaa1845b8fdd1}, - {0xf6c69a72a3989f5b, 0x8aad549e57273d46}, - {0x9a3c2087a63f6399, 0x36ac54e2f678864c}, - {0xc0cb28a98fcf3c7f, 0x84576a1bb416a7de}, - {0xf0fdf2d3f3c30b9f, 0x656d44a2a11c51d6}, - {0x969eb7c47859e743, 0x9f644ae5a4b1b326}, - {0xbc4665b596706114, 0x873d5d9f0dde1fef}, - {0xeb57ff22fc0c7959, 0xa90cb506d155a7eb}, - {0x9316ff75dd87cbd8, 0x09a7f12442d588f3}, - {0xb7dcbf5354e9bece, 0x0c11ed6d538aeb30}, - {0xe5d3ef282a242e81, 0x8f1668c8a86da5fb}, - {0x8fa475791a569d10, 0xf96e017d694487bd}, - {0xb38d92d760ec4455, 0x37c981dcc395a9ad}, - {0xe070f78d3927556a, 0x85bbe253f47b1418}, - {0x8c469ab843b89562, 0x93956d7478ccec8f}, - {0xaf58416654a6babb, 0x387ac8d1970027b3}, - {0xdb2e51bfe9d0696a, 0x06997b05fcc0319f}, - {0x88fcf317f22241e2, 0x441fece3bdf81f04}, - {0xab3c2fddeeaad25a, 0xd527e81cad7626c4}, - {0xd60b3bd56a5586f1, 0x8a71e223d8d3b075}, - {0x85c7056562757456, 0xf6872d5667844e4a}, - {0xa738c6bebb12d16c, 0xb428f8ac016561dc}, - {0xd106f86e69d785c7, 0xe13336d701beba53}, - {0x82a45b450226b39c, 0xecc0024661173474}, - {0xa34d721642b06084, 0x27f002d7f95d0191}, - {0xcc20ce9bd35c78a5, 0x31ec038df7b441f5}, - {0xff290242c83396ce, 0x7e67047175a15272}, - {0x9f79a169bd203e41, 0x0f0062c6e984d387}, - {0xc75809c42c684dd1, 0x52c07b78a3e60869}, - {0xf92e0c3537826145, 0xa7709a56ccdf8a83}, - {0x9bbcc7a142b17ccb, 0x88a66076400bb692}, - {0xc2abf989935ddbfe, 0x6acff893d00ea436}, - {0xf356f7ebf83552fe, 0x0583f6b8c4124d44}, - {0x98165af37b2153de, 0xc3727a337a8b704b}, - {0xbe1bf1b059e9a8d6, 0x744f18c0592e4c5d}, - {0xeda2ee1c7064130c, 0x1162def06f79df74}, - {0x9485d4d1c63e8be7, 0x8addcb5645ac2ba9}, - {0xb9a74a0637ce2ee1, 0x6d953e2bd7173693}, - {0xe8111c87c5c1ba99, 0xc8fa8db6ccdd0438}, - {0x910ab1d4db9914a0, 0x1d9c9892400a22a3}, - {0xb54d5e4a127f59c8, 0x2503beb6d00cab4c}, - {0xe2a0b5dc971f303a, 0x2e44ae64840fd61e}, - {0x8da471a9de737e24, 0x5ceaecfed289e5d3}, - {0xb10d8e1456105dad, 0x7425a83e872c5f48}, - {0xdd50f1996b947518, 0xd12f124e28f7771a}, - {0x8a5296ffe33cc92f, 0x82bd6b70d99aaa70}, - {0xace73cbfdc0bfb7b, 0x636cc64d1001550c}, - {0xd8210befd30efa5a, 0x3c47f7e05401aa4f}, - {0x8714a775e3e95c78, 0x65acfaec34810a72}, - {0xa8d9d1535ce3b396, 0x7f1839a741a14d0e}, - {0xd31045a8341ca07c, 0x1ede48111209a051}, - {0x83ea2b892091e44d, 0x934aed0aab460433}, - {0xa4e4b66b68b65d60, 0xf81da84d56178540}, - {0xce1de40642e3f4b9, 0x36251260ab9d668f}, - {0x80d2ae83e9ce78f3, 0xc1d72b7c6b42601a}, - {0xa1075a24e4421730, 0xb24cf65b8612f820}, - {0xc94930ae1d529cfc, 0xdee033f26797b628}, - {0xfb9b7cd9a4a7443c, 0x169840ef017da3b2}, - {0x9d412e0806e88aa5, 0x8e1f289560ee864f}, - {0xc491798a08a2ad4e, 0xf1a6f2bab92a27e3}, - {0xf5b5d7ec8acb58a2, 0xae10af696774b1dc}, - {0x9991a6f3d6bf1765, 0xacca6da1e0a8ef2a}, - {0xbff610b0cc6edd3f, 0x17fd090a58d32af4}, - {0xeff394dcff8a948e, 0xddfc4b4cef07f5b1}, - {0x95f83d0a1fb69cd9, 0x4abdaf101564f98f}, - {0xbb764c4ca7a4440f, 0x9d6d1ad41abe37f2}, - {0xea53df5fd18d5513, 0x84c86189216dc5ee}, - {0x92746b9be2f8552c, 0x32fd3cf5b4e49bb5}, - {0xb7118682dbb66a77, 0x3fbc8c33221dc2a2}, - {0xe4d5e82392a40515, 0x0fabaf3feaa5334b}, - {0x8f05b1163ba6832d, 0x29cb4d87f2a7400f}, - {0xb2c71d5bca9023f8, 0x743e20e9ef511013}, - {0xdf78e4b2bd342cf6, 0x914da9246b255417}, - {0x8bab8eefb6409c1a, 0x1ad089b6c2f7548f}, - {0xae9672aba3d0c320, 0xa184ac2473b529b2}, - {0xda3c0f568cc4f3e8, 0xc9e5d72d90a2741f}, - {0x8865899617fb1871, 0x7e2fa67c7a658893}, - {0xaa7eebfb9df9de8d, 0xddbb901b98feeab8}, - {0xd51ea6fa85785631, 0x552a74227f3ea566}, - {0x8533285c936b35de, 0xd53a88958f872760}, - {0xa67ff273b8460356, 0x8a892abaf368f138}, - {0xd01fef10a657842c, 0x2d2b7569b0432d86}, - {0x8213f56a67f6b29b, 0x9c3b29620e29fc74}, - {0xa298f2c501f45f42, 0x8349f3ba91b47b90}, - {0xcb3f2f7642717713, 0x241c70a936219a74}, - {0xfe0efb53d30dd4d7, 0xed238cd383aa0111}, - {0x9ec95d1463e8a506, 0xf4363804324a40ab}, - {0xc67bb4597ce2ce48, 0xb143c6053edcd0d6}, - {0xf81aa16fdc1b81da, 0xdd94b7868e94050b}, - {0x9b10a4e5e9913128, 0xca7cf2b4191c8327}, - {0xc1d4ce1f63f57d72, 0xfd1c2f611f63a3f1}, - {0xf24a01a73cf2dccf, 0xbc633b39673c8ced}, - {0x976e41088617ca01, 0xd5be0503e085d814}, - {0xbd49d14aa79dbc82, 0x4b2d8644d8a74e19}, - {0xec9c459d51852ba2, 0xddf8e7d60ed1219f}, - {0x93e1ab8252f33b45, 0xcabb90e5c942b504}, - {0xb8da1662e7b00a17, 0x3d6a751f3b936244}, - {0xe7109bfba19c0c9d, 0x0cc512670a783ad5}, - {0x906a617d450187e2, 0x27fb2b80668b24c6}, - {0xb484f9dc9641e9da, 0xb1f9f660802dedf7}, - {0xe1a63853bbd26451, 0x5e7873f8a0396974}, - {0x8d07e33455637eb2, 0xdb0b487b6423e1e9}, - {0xb049dc016abc5e5f, 0x91ce1a9a3d2cda63}, - {0xdc5c5301c56b75f7, 0x7641a140cc7810fc}, - {0x89b9b3e11b6329ba, 0xa9e904c87fcb0a9e}, - {0xac2820d9623bf429, 0x546345fa9fbdcd45}, - {0xd732290fbacaf133, 0xa97c177947ad4096}, - {0x867f59a9d4bed6c0, 0x49ed8eabcccc485e}, - {0xa81f301449ee8c70, 0x5c68f256bfff5a75}, - {0xd226fc195c6a2f8c, 0x73832eec6fff3112}, - {0x83585d8fd9c25db7, 0xc831fd53c5ff7eac}, - {0xa42e74f3d032f525, 0xba3e7ca8b77f5e56}, - {0xcd3a1230c43fb26f, 0x28ce1bd2e55f35ec}, - {0x80444b5e7aa7cf85, 0x7980d163cf5b81b4}, - {0xa0555e361951c366, 0xd7e105bcc3326220}, - {0xc86ab5c39fa63440, 0x8dd9472bf3fefaa8}, - {0xfa856334878fc150, 0xb14f98f6f0feb952}, - {0x9c935e00d4b9d8d2, 0x6ed1bf9a569f33d4}, - {0xc3b8358109e84f07, 0x0a862f80ec4700c9}, - {0xf4a642e14c6262c8, 0xcd27bb612758c0fb}, - {0x98e7e9cccfbd7dbd, 0x8038d51cb897789d}, - {0xbf21e44003acdd2c, 0xe0470a63e6bd56c4}, - {0xeeea5d5004981478, 0x1858ccfce06cac75}, - {0x95527a5202df0ccb, 0x0f37801e0c43ebc9}, - {0xbaa718e68396cffd, 0xd30560258f54e6bb}, - {0xe950df20247c83fd, 0x47c6b82ef32a206a}, - {0x91d28b7416cdd27e, 0x4cdc331d57fa5442}, - {0xb6472e511c81471d, 0xe0133fe4adf8e953}, - {0xe3d8f9e563a198e5, 0x58180fddd97723a7}, - {0x8e679c2f5e44ff8f, 0x570f09eaa7ea7649}, - {0xb201833b35d63f73, 0x2cd2cc6551e513db}, - {0xde81e40a034bcf4f, 0xf8077f7ea65e58d2}, - {0x8b112e86420f6191, 0xfb04afaf27faf783}, - {0xadd57a27d29339f6, 0x79c5db9af1f9b564}, - {0xd94ad8b1c7380874, 0x18375281ae7822bd}, - {0x87cec76f1c830548, 0x8f2293910d0b15b6}, - {0xa9c2794ae3a3c69a, 0xb2eb3875504ddb23}, - {0xd433179d9c8cb841, 0x5fa60692a46151ec}, - {0x849feec281d7f328, 0xdbc7c41ba6bcd334}, - {0xa5c7ea73224deff3, 0x12b9b522906c0801}, - {0xcf39e50feae16bef, 0xd768226b34870a01}, - {0x81842f29f2cce375, 0xe6a1158300d46641}, - {0xa1e53af46f801c53, 0x60495ae3c1097fd1}, - {0xca5e89b18b602368, 0x385bb19cb14bdfc5}, - {0xfcf62c1dee382c42, 0x46729e03dd9ed7b6}, - {0x9e19db92b4e31ba9, 0x6c07a2c26a8346d2}, - {0xc5a05277621be293, 0xc7098b7305241886}, - {0xf70867153aa2db38, 0xb8cbee4fc66d1ea8}, - {0x9a65406d44a5c903, 0x737f74f1dc043329}, - {0xc0fe908895cf3b44, 0x505f522e53053ff3}, - {0xf13e34aabb430a15, 0x647726b9e7c68ff0}, - {0x96c6e0eab509e64d, 0x5eca783430dc19f6}, - {0xbc789925624c5fe0, 0xb67d16413d132073}, - {0xeb96bf6ebadf77d8, 0xe41c5bd18c57e890}, - {0x933e37a534cbaae7, 0x8e91b962f7b6f15a}, - {0xb80dc58e81fe95a1, 0x723627bbb5a4adb1}, - {0xe61136f2227e3b09, 0xcec3b1aaa30dd91d}, - {0x8fcac257558ee4e6, 0x213a4f0aa5e8a7b2}, - {0xb3bd72ed2af29e1f, 0xa988e2cd4f62d19e}, - {0xe0accfa875af45a7, 0x93eb1b80a33b8606}, - {0x8c6c01c9498d8b88, 0xbc72f130660533c4}, - {0xaf87023b9bf0ee6a, 0xeb8fad7c7f8680b5}, - {0xdb68c2ca82ed2a05, 0xa67398db9f6820e2}, -#else - {0xff77b1fcbebcdc4f, 0x25e8e89c13bb0f7b}, - {0xce5d73ff402d98e3, 0xfb0a3d212dc81290}, - {0xa6b34ad8c9dfc06f, 0xf42faa48c0ea481f}, - {0x86a8d39ef77164bc, 0xae5dff9c02033198}, - {0xd98ddaee19068c76, 0x3badd624dd9b0958}, - {0xafbd2350644eeacf, 0xe5d1929ef90898fb}, - {0x8df5efabc5979c8f, 0xca8d3ffa1ef463c2}, - {0xe55990879ddcaabd, 0xcc420a6a101d0516}, - {0xb94470938fa89bce, 0xf808e40e8d5b3e6a}, - {0x95a8637627989aad, 0xdde7001379a44aa9}, - {0xf1c90080baf72cb1, 0x5324c68b12dd6339}, - {0xc350000000000000, 0x0000000000000000}, - {0x9dc5ada82b70b59d, 0xf020000000000000}, - {0xfee50b7025c36a08, 0x02f236d04753d5b5}, - {0xcde6fd5e09abcf26, 0xed4c0226b55e6f87}, - {0xa6539930bf6bff45, 0x84db8346b786151d}, - {0x865b86925b9bc5c2, 0x0b8a2392ba45a9b3}, - {0xd910f7ff28069da4, 0x1b2ba1518094da05}, - {0xaf58416654a6babb, 0x387ac8d1970027b3}, - {0x8da471a9de737e24, 0x5ceaecfed289e5d3}, - {0xe4d5e82392a40515, 0x0fabaf3feaa5334b}, - {0xb8da1662e7b00a17, 0x3d6a751f3b936244}, - {0x95527a5202df0ccb, 0x0f37801e0c43ebc9}, - {0xf13e34aabb430a15, 0x647726b9e7c68ff0} -#endif - }; - -#if FMT_USE_FULL_CACHE_DRAGONBOX - return pow10_significands[k - float_info::min_k]; -#else - static constexpr const uint64_t powers_of_5_64[] = { - 0x0000000000000001, 0x0000000000000005, 0x0000000000000019, - 0x000000000000007d, 0x0000000000000271, 0x0000000000000c35, - 0x0000000000003d09, 0x000000000001312d, 0x000000000005f5e1, - 0x00000000001dcd65, 0x00000000009502f9, 0x0000000002e90edd, - 0x000000000e8d4a51, 0x0000000048c27395, 0x000000016bcc41e9, - 0x000000071afd498d, 0x0000002386f26fc1, 0x000000b1a2bc2ec5, - 0x000003782dace9d9, 0x00001158e460913d, 0x000056bc75e2d631, - 0x0001b1ae4d6e2ef5, 0x000878678326eac9, 0x002a5a058fc295ed, - 0x00d3c21bcecceda1, 0x0422ca8b0a00a425, 0x14adf4b7320334b9}; - - static const int compression_ratio = 27; - - // Compute base index. - int cache_index = (k - float_info::min_k) / compression_ratio; - int kb = cache_index * compression_ratio + float_info::min_k; - int offset = k - kb; - - // Get base cache. - uint128_fallback base_cache = pow10_significands[cache_index]; - if (offset == 0) return base_cache; - - // Compute the required amount of bit-shift. - int alpha = floor_log2_pow10(kb + offset) - floor_log2_pow10(kb) - offset; - FMT_ASSERT(alpha > 0 && alpha < 64, "shifting error detected"); - - // Try to recover the real cache. - uint64_t pow5 = powers_of_5_64[offset]; - uint128_fallback recovered_cache = umul128(base_cache.high(), pow5); - uint128_fallback middle_low = umul128(base_cache.low(), pow5); - - recovered_cache += middle_low.high(); - - uint64_t high_to_middle = recovered_cache.high() << (64 - alpha); - uint64_t middle_to_low = recovered_cache.low() << (64 - alpha); - - recovered_cache = - uint128_fallback{(recovered_cache.low() >> alpha) | high_to_middle, - ((middle_low.low() >> alpha) | middle_to_low)}; - FMT_ASSERT(recovered_cache.low() + 1 != 0, ""); - return {recovered_cache.high(), recovered_cache.low() + 1}; -#endif - } - - struct compute_mul_result { - carrier_uint result; - bool is_integer; - }; - struct compute_mul_parity_result { - bool parity; - bool is_integer; - }; - - static auto compute_mul(carrier_uint u, - const cache_entry_type& cache) noexcept - -> compute_mul_result { - auto r = umul192_upper128(u, cache); - return {r.high(), r.low() == 0}; - } - - static auto compute_delta(cache_entry_type const& cache, int beta) noexcept - -> uint32_t { - return static_cast(cache.high() >> (64 - 1 - beta)); - } - - static auto compute_mul_parity(carrier_uint two_f, - const cache_entry_type& cache, - int beta) noexcept - -> compute_mul_parity_result { - FMT_ASSERT(beta >= 1, ""); - FMT_ASSERT(beta < 64, ""); - - auto r = umul192_lower128(two_f, cache); - return {((r.high() >> (64 - beta)) & 1) != 0, - ((r.high() << beta) | (r.low() >> (64 - beta))) == 0}; - } - - static auto compute_left_endpoint_for_shorter_interval_case( - const cache_entry_type& cache, int beta) noexcept -> carrier_uint { - return (cache.high() - - (cache.high() >> (num_significand_bits() + 2))) >> - (64 - num_significand_bits() - 1 - beta); - } - - static auto compute_right_endpoint_for_shorter_interval_case( - const cache_entry_type& cache, int beta) noexcept -> carrier_uint { - return (cache.high() + - (cache.high() >> (num_significand_bits() + 1))) >> - (64 - num_significand_bits() - 1 - beta); - } - - static auto compute_round_up_for_shorter_interval_case( - const cache_entry_type& cache, int beta) noexcept -> carrier_uint { - return ((cache.high() >> (64 - num_significand_bits() - 2 - beta)) + - 1) / - 2; - } -}; - -FMT_FUNC auto get_cached_power(int k) noexcept -> uint128_fallback { - return cache_accessor::get_cached_power(k); -} - -// Various integer checks -template -auto is_left_endpoint_integer_shorter_interval(int exponent) noexcept -> bool { - const int case_shorter_interval_left_endpoint_lower_threshold = 2; - const int case_shorter_interval_left_endpoint_upper_threshold = 3; - return exponent >= case_shorter_interval_left_endpoint_lower_threshold && - exponent <= case_shorter_interval_left_endpoint_upper_threshold; -} - -// Remove trailing zeros from n and return the number of zeros removed (float) -FMT_INLINE int remove_trailing_zeros(uint32_t& n, int s = 0) noexcept { - FMT_ASSERT(n != 0, ""); - // Modular inverse of 5 (mod 2^32): (mod_inv_5 * 5) mod 2^32 = 1. - constexpr uint32_t mod_inv_5 = 0xcccccccd; - constexpr uint32_t mod_inv_25 = 0xc28f5c29; // = mod_inv_5 * mod_inv_5 - - while (true) { - auto q = rotr(n * mod_inv_25, 2); - if (q > max_value() / 100) break; - n = q; - s += 2; - } - auto q = rotr(n * mod_inv_5, 1); - if (q <= max_value() / 10) { - n = q; - s |= 1; - } - return s; -} - -// Removes trailing zeros and returns the number of zeros removed (double) -FMT_INLINE int remove_trailing_zeros(uint64_t& n) noexcept { - FMT_ASSERT(n != 0, ""); - - // This magic number is ceil(2^90 / 10^8). - constexpr uint64_t magic_number = 12379400392853802749ull; - auto nm = umul128(n, magic_number); - - // Is n is divisible by 10^8? - if ((nm.high() & ((1ull << (90 - 64)) - 1)) == 0 && nm.low() < magic_number) { - // If yes, work with the quotient... - auto n32 = static_cast(nm.high() >> (90 - 64)); - // ... and use the 32 bit variant of the function - int s = remove_trailing_zeros(n32, 8); - n = n32; - return s; - } - - // If n is not divisible by 10^8, work with n itself. - constexpr uint64_t mod_inv_5 = 0xcccccccccccccccd; - constexpr uint64_t mod_inv_25 = 0x8f5c28f5c28f5c29; // mod_inv_5 * mod_inv_5 - - int s = 0; - while (true) { - auto q = rotr(n * mod_inv_25, 2); - if (q > max_value() / 100) break; - n = q; - s += 2; - } - auto q = rotr(n * mod_inv_5, 1); - if (q <= max_value() / 10) { - n = q; - s |= 1; - } - - return s; -} - -// The main algorithm for shorter interval case -template -FMT_INLINE decimal_fp shorter_interval_case(int exponent) noexcept { - decimal_fp ret_value; - // Compute k and beta - const int minus_k = floor_log10_pow2_minus_log10_4_over_3(exponent); - const int beta = exponent + floor_log2_pow10(-minus_k); - - // Compute xi and zi - using cache_entry_type = typename cache_accessor::cache_entry_type; - const cache_entry_type cache = cache_accessor::get_cached_power(-minus_k); - - auto xi = cache_accessor::compute_left_endpoint_for_shorter_interval_case( - cache, beta); - auto zi = cache_accessor::compute_right_endpoint_for_shorter_interval_case( - cache, beta); - - // If the left endpoint is not an integer, increase it - if (!is_left_endpoint_integer_shorter_interval(exponent)) ++xi; - - // Try bigger divisor - ret_value.significand = zi / 10; - - // If succeed, remove trailing zeros if necessary and return - if (ret_value.significand * 10 >= xi) { - ret_value.exponent = minus_k + 1; - ret_value.exponent += remove_trailing_zeros(ret_value.significand); - return ret_value; - } - - // Otherwise, compute the round-up of y - ret_value.significand = - cache_accessor::compute_round_up_for_shorter_interval_case(cache, - beta); - ret_value.exponent = minus_k; - - // When tie occurs, choose one of them according to the rule - if (exponent >= float_info::shorter_interval_tie_lower_threshold && - exponent <= float_info::shorter_interval_tie_upper_threshold) { - ret_value.significand = ret_value.significand % 2 == 0 - ? ret_value.significand - : ret_value.significand - 1; - } else if (ret_value.significand < xi) { - ++ret_value.significand; - } - return ret_value; -} - -template auto to_decimal(T x) noexcept -> decimal_fp { - // Step 1: integer promotion & Schubfach multiplier calculation. - - using carrier_uint = typename float_info::carrier_uint; - using cache_entry_type = typename cache_accessor::cache_entry_type; - auto br = bit_cast(x); - - // Extract significand bits and exponent bits. - const carrier_uint significand_mask = - (static_cast(1) << num_significand_bits()) - 1; - carrier_uint significand = (br & significand_mask); - int exponent = - static_cast((br & exponent_mask()) >> num_significand_bits()); - - if (exponent != 0) { // Check if normal. - exponent -= exponent_bias() + num_significand_bits(); - - // Shorter interval case; proceed like Schubfach. - // In fact, when exponent == 1 and significand == 0, the interval is - // regular. However, it can be shown that the end-results are anyway same. - if (significand == 0) return shorter_interval_case(exponent); - - significand |= (static_cast(1) << num_significand_bits()); - } else { - // Subnormal case; the interval is always regular. - if (significand == 0) return {0, 0}; - exponent = - std::numeric_limits::min_exponent - num_significand_bits() - 1; - } - - const bool include_left_endpoint = (significand % 2 == 0); - const bool include_right_endpoint = include_left_endpoint; - - // Compute k and beta. - const int minus_k = floor_log10_pow2(exponent) - float_info::kappa; - const cache_entry_type cache = cache_accessor::get_cached_power(-minus_k); - const int beta = exponent + floor_log2_pow10(-minus_k); - - // Compute zi and deltai. - // 10^kappa <= deltai < 10^(kappa + 1) - const uint32_t deltai = cache_accessor::compute_delta(cache, beta); - const carrier_uint two_fc = significand << 1; - - // For the case of binary32, the result of integer check is not correct for - // 29711844 * 2^-82 - // = 6.1442653300000000008655037797566933477355632930994033813476... * 10^-18 - // and 29711844 * 2^-81 - // = 1.2288530660000000001731007559513386695471126586198806762695... * 10^-17, - // and they are the unique counterexamples. However, since 29711844 is even, - // this does not cause any problem for the endpoints calculations; it can only - // cause a problem when we need to perform integer check for the center. - // Fortunately, with these inputs, that branch is never executed, so we are - // fine. - const typename cache_accessor::compute_mul_result z_mul = - cache_accessor::compute_mul((two_fc | 1) << beta, cache); - - // Step 2: Try larger divisor; remove trailing zeros if necessary. - - // Using an upper bound on zi, we might be able to optimize the division - // better than the compiler; we are computing zi / big_divisor here. - decimal_fp ret_value; - ret_value.significand = divide_by_10_to_kappa_plus_1(z_mul.result); - uint32_t r = static_cast(z_mul.result - float_info::big_divisor * - ret_value.significand); - - if (r < deltai) { - // Exclude the right endpoint if necessary. - if (r == 0 && (z_mul.is_integer & !include_right_endpoint)) { - --ret_value.significand; - r = float_info::big_divisor; - goto small_divisor_case_label; - } - } else if (r > deltai) { - goto small_divisor_case_label; - } else { - // r == deltai; compare fractional parts. - const typename cache_accessor::compute_mul_parity_result x_mul = - cache_accessor::compute_mul_parity(two_fc - 1, cache, beta); - - if (!(x_mul.parity | (x_mul.is_integer & include_left_endpoint))) - goto small_divisor_case_label; - } - ret_value.exponent = minus_k + float_info::kappa + 1; - - // We may need to remove trailing zeros. - ret_value.exponent += remove_trailing_zeros(ret_value.significand); - return ret_value; - - // Step 3: Find the significand with the smaller divisor. - -small_divisor_case_label: - ret_value.significand *= 10; - ret_value.exponent = minus_k + float_info::kappa; - - uint32_t dist = r - (deltai / 2) + (float_info::small_divisor / 2); - const bool approx_y_parity = - ((dist ^ (float_info::small_divisor / 2)) & 1) != 0; - - // Is dist divisible by 10^kappa? - const bool divisible_by_small_divisor = - check_divisibility_and_divide_by_pow10::kappa>(dist); - - // Add dist / 10^kappa to the significand. - ret_value.significand += dist; - - if (!divisible_by_small_divisor) return ret_value; - - // Check z^(f) >= epsilon^(f). - // We have either yi == zi - epsiloni or yi == (zi - epsiloni) - 1, - // where yi == zi - epsiloni if and only if z^(f) >= epsilon^(f). - // Since there are only 2 possibilities, we only need to care about the - // parity. Also, zi and r should have the same parity since the divisor - // is an even number. - const auto y_mul = cache_accessor::compute_mul_parity(two_fc, cache, beta); - - // If z^(f) >= epsilon^(f), we might have a tie when z^(f) == epsilon^(f), - // or equivalently, when y is an integer. - if (y_mul.parity != approx_y_parity) - --ret_value.significand; - else if (y_mul.is_integer & (ret_value.significand % 2 != 0)) - --ret_value.significand; - return ret_value; -} -} // namespace dragonbox -} // namespace detail - -template <> struct formatter { - FMT_CONSTEXPR auto parse(format_parse_context& ctx) - -> format_parse_context::iterator { - return ctx.begin(); - } - - auto format(const detail::bigint& n, format_context& ctx) const - -> format_context::iterator { - auto out = ctx.out(); - bool first = true; - for (auto i = n.bigits_.size(); i > 0; --i) { - auto value = n.bigits_[i - 1u]; - if (first) { - out = fmt::format_to(out, FMT_STRING("{:x}"), value); - first = false; - continue; - } - out = fmt::format_to(out, FMT_STRING("{:08x}"), value); - } - if (n.exp_ > 0) - out = fmt::format_to(out, FMT_STRING("p{}"), - n.exp_ * detail::bigint::bigit_bits); - return out; - } -}; - -FMT_FUNC detail::utf8_to_utf16::utf8_to_utf16(string_view s) { - for_each_codepoint(s, [this](uint32_t cp, string_view) { - if (cp == invalid_code_point) FMT_THROW(std::runtime_error("invalid utf8")); - if (cp <= 0xFFFF) { - buffer_.push_back(static_cast(cp)); - } else { - cp -= 0x10000; - buffer_.push_back(static_cast(0xD800 + (cp >> 10))); - buffer_.push_back(static_cast(0xDC00 + (cp & 0x3FF))); - } - return true; - }); - buffer_.push_back(0); -} - -FMT_FUNC void format_system_error(detail::buffer& out, int error_code, - const char* message) noexcept { - FMT_TRY { - auto ec = std::error_code(error_code, std::generic_category()); - detail::write(appender(out), std::system_error(ec, message).what()); - return; - } - FMT_CATCH(...) {} - format_error_code(out, error_code, message); -} - -FMT_FUNC void report_system_error(int error_code, - const char* message) noexcept { - report_error(format_system_error, error_code, message); -} - -FMT_FUNC auto vformat(string_view fmt, format_args args) -> std::string { - // Don't optimize the "{}" case to keep the binary size small and because it - // can be better optimized in fmt::format anyway. - auto buffer = memory_buffer(); - detail::vformat_to(buffer, fmt, args); - return to_string(buffer); -} - -namespace detail { - -template struct span { - T* data; - size_t size; -}; - -template auto flockfile(F* f) -> decltype(_lock_file(f)) { - _lock_file(f); -} -template auto funlockfile(F* f) -> decltype(_unlock_file(f)) { - _unlock_file(f); -} - -#ifndef getc_unlocked -template auto getc_unlocked(F* f) -> decltype(_fgetc_nolock(f)) { - return _fgetc_nolock(f); -} -#endif - -template -struct has_flockfile : std::false_type {}; - -template -struct has_flockfile()))>> - : std::true_type {}; - -// A FILE wrapper. F is FILE defined as a template parameter to make system API -// detection work. -template class file_base { - public: - F* file_; - - public: - file_base(F* file) : file_(file) {} - operator F*() const { return file_; } - - // Reads a code unit from the stream. - auto get() -> int { - int result = getc_unlocked(file_); - if (result == EOF && ferror(file_) != 0) - FMT_THROW(system_error(errno, FMT_STRING("getc failed"))); - return result; - } - - // Puts the code unit back into the stream buffer. - void unget(char c) { - if (ungetc(c, file_) == EOF) - FMT_THROW(system_error(errno, FMT_STRING("ungetc failed"))); - } - - void flush() { fflush(this->file_); } -}; - -// A FILE wrapper for glibc. -template class glibc_file : public file_base { - private: - enum { - line_buffered = 0x200, // _IO_LINE_BUF - unbuffered = 2 // _IO_UNBUFFERED - }; - - public: - using file_base::file_base; - - auto is_buffered() const -> bool { - return (this->file_->_flags & unbuffered) == 0; - } - - void init_buffer() { - if (this->file_->_IO_write_ptr) return; - // Force buffer initialization by placing and removing a char in a buffer. - putc_unlocked(0, this->file_); - --this->file_->_IO_write_ptr; - } - - // Returns the file's read buffer. - auto get_read_buffer() const -> span { - auto ptr = this->file_->_IO_read_ptr; - return {ptr, to_unsigned(this->file_->_IO_read_end - ptr)}; - } - - // Returns the file's write buffer. - auto get_write_buffer() const -> span { - auto ptr = this->file_->_IO_write_ptr; - return {ptr, to_unsigned(this->file_->_IO_buf_end - ptr)}; - } - - void advance_write_buffer(size_t size) { this->file_->_IO_write_ptr += size; } - - bool needs_flush() const { - if ((this->file_->_flags & line_buffered) == 0) return false; - char* end = this->file_->_IO_write_end; - return memchr(end, '\n', to_unsigned(this->file_->_IO_write_ptr - end)); - } - - void flush() { fflush_unlocked(this->file_); } -}; - -// A FILE wrapper for Apple's libc. -template class apple_file : public file_base { - private: - enum { - line_buffered = 1, // __SNBF - unbuffered = 2 // __SLBF - }; - - public: - using file_base::file_base; - - auto is_buffered() const -> bool { - return (this->file_->_flags & unbuffered) == 0; - } - - void init_buffer() { - if (this->file_->_p) return; - // Force buffer initialization by placing and removing a char in a buffer. - putc_unlocked(0, this->file_); - --this->file_->_p; - ++this->file_->_w; - } - - auto get_read_buffer() const -> span { - return {reinterpret_cast(this->file_->_p), - to_unsigned(this->file_->_r)}; - } - - auto get_write_buffer() const -> span { - return {reinterpret_cast(this->file_->_p), - to_unsigned(this->file_->_bf._base + this->file_->_bf._size - - this->file_->_p)}; - } - - void advance_write_buffer(size_t size) { - this->file_->_p += size; - this->file_->_w -= size; - } - - bool needs_flush() const { - if ((this->file_->_flags & line_buffered) == 0) return false; - return memchr(this->file_->_p + this->file_->_w, '\n', - to_unsigned(-this->file_->_w)); - } -}; - -// A fallback FILE wrapper. -template class fallback_file : public file_base { - private: - char next_; // The next unconsumed character in the buffer. - bool has_next_ = false; - - public: - using file_base::file_base; - - auto is_buffered() const -> bool { return false; } - auto needs_flush() const -> bool { return false; } - void init_buffer() {} - - auto get_read_buffer() const -> span { - return {&next_, has_next_ ? 1u : 0u}; - } - - auto get_write_buffer() const -> span { return {nullptr, 0}; } - - void advance_write_buffer(size_t) {} - - auto get() -> int { - has_next_ = false; - return file_base::get(); - } - - void unget(char c) { - file_base::unget(c); - next_ = c; - has_next_ = true; - } -}; - -#ifndef FMT_USE_FALLBACK_FILE -# define FMT_USE_FALLBACK_FILE 1 -#endif - -template -auto get_file(F* f, int) -> apple_file { - return f; -} -template -inline auto get_file(F* f, int) -> glibc_file { - return f; -} - -inline auto get_file(FILE* f, ...) -> fallback_file { return f; } - -using file_ref = decltype(get_file(static_cast(nullptr), 0)); - -template -class file_print_buffer : public buffer { - public: - explicit file_print_buffer(F*) : buffer(nullptr, size_t()) {} -}; - -template -class file_print_buffer::value>> - : public buffer { - private: - file_ref file_; - - static void grow(buffer& base, size_t) { - auto& self = static_cast(base); - self.file_.advance_write_buffer(self.size()); - if (self.file_.get_write_buffer().size == 0) self.file_.flush(); - auto buf = self.file_.get_write_buffer(); - FMT_ASSERT(buf.size > 0, ""); - self.set(buf.data, buf.size); - self.clear(); - } - - public: - explicit file_print_buffer(F* f) : buffer(grow, size_t()), file_(f) { - flockfile(f); - file_.init_buffer(); - auto buf = file_.get_write_buffer(); - set(buf.data, buf.size); - } - ~file_print_buffer() { - file_.advance_write_buffer(size()); - bool flush = file_.needs_flush(); - F* f = file_; // Make funlockfile depend on the template parameter F - funlockfile(f); // for the system API detection to work. - if (flush) fflush(file_); - } -}; - -#if !defined(_WIN32) || defined(FMT_USE_WRITE_CONSOLE) -FMT_FUNC auto write_console(int, string_view) -> bool { return false; } -#else -using dword = conditional_t; -extern "C" __declspec(dllimport) int __stdcall WriteConsoleW( // - void*, const void*, dword, dword*, void*); - -FMT_FUNC bool write_console(int fd, string_view text) { - auto u16 = utf8_to_utf16(text); - return WriteConsoleW(reinterpret_cast(_get_osfhandle(fd)), u16.c_str(), - static_cast(u16.size()), nullptr, nullptr) != 0; -} -#endif - -#ifdef _WIN32 -// Print assuming legacy (non-Unicode) encoding. -FMT_FUNC void vprint_mojibake(std::FILE* f, string_view fmt, format_args args, - bool newline) { - auto buffer = memory_buffer(); - detail::vformat_to(buffer, fmt, args); - if (newline) buffer.push_back('\n'); - fwrite_fully(buffer.data(), buffer.size(), f); -} -#endif - -FMT_FUNC void print(std::FILE* f, string_view text) { -#if defined(_WIN32) && !defined(FMT_USE_WRITE_CONSOLE) - int fd = _fileno(f); - if (_isatty(fd)) { - std::fflush(f); - if (write_console(fd, text)) return; - } -#endif - fwrite_fully(text.data(), text.size(), f); -} -} // namespace detail - -FMT_FUNC void vprint_buffered(std::FILE* f, string_view fmt, format_args args) { - auto buffer = memory_buffer(); - detail::vformat_to(buffer, fmt, args); - detail::print(f, {buffer.data(), buffer.size()}); -} - -FMT_FUNC void vprint(std::FILE* f, string_view fmt, format_args args) { - if (!detail::file_ref(f).is_buffered() || !detail::has_flockfile<>()) - return vprint_buffered(f, fmt, args); - auto&& buffer = detail::file_print_buffer<>(f); - return detail::vformat_to(buffer, fmt, args); -} - -FMT_FUNC void vprintln(std::FILE* f, string_view fmt, format_args args) { - auto buffer = memory_buffer(); - detail::vformat_to(buffer, fmt, args); - buffer.push_back('\n'); - detail::print(f, {buffer.data(), buffer.size()}); -} - -FMT_FUNC void vprint(string_view fmt, format_args args) { - vprint(stdout, fmt, args); -} - -namespace detail { - -struct singleton { - unsigned char upper; - unsigned char lower_count; -}; - -inline auto is_printable(uint16_t x, const singleton* singletons, - size_t singletons_size, - const unsigned char* singleton_lowers, - const unsigned char* normal, size_t normal_size) - -> bool { - auto upper = x >> 8; - auto lower_start = 0; - for (size_t i = 0; i < singletons_size; ++i) { - auto s = singletons[i]; - auto lower_end = lower_start + s.lower_count; - if (upper < s.upper) break; - if (upper == s.upper) { - for (auto j = lower_start; j < lower_end; ++j) { - if (singleton_lowers[j] == (x & 0xff)) return false; - } - } - lower_start = lower_end; - } - - auto xsigned = static_cast(x); - auto current = true; - for (size_t i = 0; i < normal_size; ++i) { - auto v = static_cast(normal[i]); - auto len = (v & 0x80) != 0 ? (v & 0x7f) << 8 | normal[++i] : v; - xsigned -= len; - if (xsigned < 0) break; - current = !current; - } - return current; -} - -// This code is generated by support/printable.py. -FMT_FUNC auto is_printable(uint32_t cp) -> bool { - static constexpr singleton singletons0[] = { - {0x00, 1}, {0x03, 5}, {0x05, 6}, {0x06, 3}, {0x07, 6}, {0x08, 8}, - {0x09, 17}, {0x0a, 28}, {0x0b, 25}, {0x0c, 20}, {0x0d, 16}, {0x0e, 13}, - {0x0f, 4}, {0x10, 3}, {0x12, 18}, {0x13, 9}, {0x16, 1}, {0x17, 5}, - {0x18, 2}, {0x19, 3}, {0x1a, 7}, {0x1c, 2}, {0x1d, 1}, {0x1f, 22}, - {0x20, 3}, {0x2b, 3}, {0x2c, 2}, {0x2d, 11}, {0x2e, 1}, {0x30, 3}, - {0x31, 2}, {0x32, 1}, {0xa7, 2}, {0xa9, 2}, {0xaa, 4}, {0xab, 8}, - {0xfa, 2}, {0xfb, 5}, {0xfd, 4}, {0xfe, 3}, {0xff, 9}, - }; - static constexpr unsigned char singletons0_lower[] = { - 0xad, 0x78, 0x79, 0x8b, 0x8d, 0xa2, 0x30, 0x57, 0x58, 0x8b, 0x8c, 0x90, - 0x1c, 0x1d, 0xdd, 0x0e, 0x0f, 0x4b, 0x4c, 0xfb, 0xfc, 0x2e, 0x2f, 0x3f, - 0x5c, 0x5d, 0x5f, 0xb5, 0xe2, 0x84, 0x8d, 0x8e, 0x91, 0x92, 0xa9, 0xb1, - 0xba, 0xbb, 0xc5, 0xc6, 0xc9, 0xca, 0xde, 0xe4, 0xe5, 0xff, 0x00, 0x04, - 0x11, 0x12, 0x29, 0x31, 0x34, 0x37, 0x3a, 0x3b, 0x3d, 0x49, 0x4a, 0x5d, - 0x84, 0x8e, 0x92, 0xa9, 0xb1, 0xb4, 0xba, 0xbb, 0xc6, 0xca, 0xce, 0xcf, - 0xe4, 0xe5, 0x00, 0x04, 0x0d, 0x0e, 0x11, 0x12, 0x29, 0x31, 0x34, 0x3a, - 0x3b, 0x45, 0x46, 0x49, 0x4a, 0x5e, 0x64, 0x65, 0x84, 0x91, 0x9b, 0x9d, - 0xc9, 0xce, 0xcf, 0x0d, 0x11, 0x29, 0x45, 0x49, 0x57, 0x64, 0x65, 0x8d, - 0x91, 0xa9, 0xb4, 0xba, 0xbb, 0xc5, 0xc9, 0xdf, 0xe4, 0xe5, 0xf0, 0x0d, - 0x11, 0x45, 0x49, 0x64, 0x65, 0x80, 0x84, 0xb2, 0xbc, 0xbe, 0xbf, 0xd5, - 0xd7, 0xf0, 0xf1, 0x83, 0x85, 0x8b, 0xa4, 0xa6, 0xbe, 0xbf, 0xc5, 0xc7, - 0xce, 0xcf, 0xda, 0xdb, 0x48, 0x98, 0xbd, 0xcd, 0xc6, 0xce, 0xcf, 0x49, - 0x4e, 0x4f, 0x57, 0x59, 0x5e, 0x5f, 0x89, 0x8e, 0x8f, 0xb1, 0xb6, 0xb7, - 0xbf, 0xc1, 0xc6, 0xc7, 0xd7, 0x11, 0x16, 0x17, 0x5b, 0x5c, 0xf6, 0xf7, - 0xfe, 0xff, 0x80, 0x0d, 0x6d, 0x71, 0xde, 0xdf, 0x0e, 0x0f, 0x1f, 0x6e, - 0x6f, 0x1c, 0x1d, 0x5f, 0x7d, 0x7e, 0xae, 0xaf, 0xbb, 0xbc, 0xfa, 0x16, - 0x17, 0x1e, 0x1f, 0x46, 0x47, 0x4e, 0x4f, 0x58, 0x5a, 0x5c, 0x5e, 0x7e, - 0x7f, 0xb5, 0xc5, 0xd4, 0xd5, 0xdc, 0xf0, 0xf1, 0xf5, 0x72, 0x73, 0x8f, - 0x74, 0x75, 0x96, 0x2f, 0x5f, 0x26, 0x2e, 0x2f, 0xa7, 0xaf, 0xb7, 0xbf, - 0xc7, 0xcf, 0xd7, 0xdf, 0x9a, 0x40, 0x97, 0x98, 0x30, 0x8f, 0x1f, 0xc0, - 0xc1, 0xce, 0xff, 0x4e, 0x4f, 0x5a, 0x5b, 0x07, 0x08, 0x0f, 0x10, 0x27, - 0x2f, 0xee, 0xef, 0x6e, 0x6f, 0x37, 0x3d, 0x3f, 0x42, 0x45, 0x90, 0x91, - 0xfe, 0xff, 0x53, 0x67, 0x75, 0xc8, 0xc9, 0xd0, 0xd1, 0xd8, 0xd9, 0xe7, - 0xfe, 0xff, - }; - static constexpr singleton singletons1[] = { - {0x00, 6}, {0x01, 1}, {0x03, 1}, {0x04, 2}, {0x08, 8}, {0x09, 2}, - {0x0a, 5}, {0x0b, 2}, {0x0e, 4}, {0x10, 1}, {0x11, 2}, {0x12, 5}, - {0x13, 17}, {0x14, 1}, {0x15, 2}, {0x17, 2}, {0x19, 13}, {0x1c, 5}, - {0x1d, 8}, {0x24, 1}, {0x6a, 3}, {0x6b, 2}, {0xbc, 2}, {0xd1, 2}, - {0xd4, 12}, {0xd5, 9}, {0xd6, 2}, {0xd7, 2}, {0xda, 1}, {0xe0, 5}, - {0xe1, 2}, {0xe8, 2}, {0xee, 32}, {0xf0, 4}, {0xf8, 2}, {0xf9, 2}, - {0xfa, 2}, {0xfb, 1}, - }; - static constexpr unsigned char singletons1_lower[] = { - 0x0c, 0x27, 0x3b, 0x3e, 0x4e, 0x4f, 0x8f, 0x9e, 0x9e, 0x9f, 0x06, 0x07, - 0x09, 0x36, 0x3d, 0x3e, 0x56, 0xf3, 0xd0, 0xd1, 0x04, 0x14, 0x18, 0x36, - 0x37, 0x56, 0x57, 0x7f, 0xaa, 0xae, 0xaf, 0xbd, 0x35, 0xe0, 0x12, 0x87, - 0x89, 0x8e, 0x9e, 0x04, 0x0d, 0x0e, 0x11, 0x12, 0x29, 0x31, 0x34, 0x3a, - 0x45, 0x46, 0x49, 0x4a, 0x4e, 0x4f, 0x64, 0x65, 0x5c, 0xb6, 0xb7, 0x1b, - 0x1c, 0x07, 0x08, 0x0a, 0x0b, 0x14, 0x17, 0x36, 0x39, 0x3a, 0xa8, 0xa9, - 0xd8, 0xd9, 0x09, 0x37, 0x90, 0x91, 0xa8, 0x07, 0x0a, 0x3b, 0x3e, 0x66, - 0x69, 0x8f, 0x92, 0x6f, 0x5f, 0xee, 0xef, 0x5a, 0x62, 0x9a, 0x9b, 0x27, - 0x28, 0x55, 0x9d, 0xa0, 0xa1, 0xa3, 0xa4, 0xa7, 0xa8, 0xad, 0xba, 0xbc, - 0xc4, 0x06, 0x0b, 0x0c, 0x15, 0x1d, 0x3a, 0x3f, 0x45, 0x51, 0xa6, 0xa7, - 0xcc, 0xcd, 0xa0, 0x07, 0x19, 0x1a, 0x22, 0x25, 0x3e, 0x3f, 0xc5, 0xc6, - 0x04, 0x20, 0x23, 0x25, 0x26, 0x28, 0x33, 0x38, 0x3a, 0x48, 0x4a, 0x4c, - 0x50, 0x53, 0x55, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x63, 0x65, 0x66, - 0x6b, 0x73, 0x78, 0x7d, 0x7f, 0x8a, 0xa4, 0xaa, 0xaf, 0xb0, 0xc0, 0xd0, - 0xae, 0xaf, 0x79, 0xcc, 0x6e, 0x6f, 0x93, - }; - static constexpr unsigned char normal0[] = { - 0x00, 0x20, 0x5f, 0x22, 0x82, 0xdf, 0x04, 0x82, 0x44, 0x08, 0x1b, 0x04, - 0x06, 0x11, 0x81, 0xac, 0x0e, 0x80, 0xab, 0x35, 0x28, 0x0b, 0x80, 0xe0, - 0x03, 0x19, 0x08, 0x01, 0x04, 0x2f, 0x04, 0x34, 0x04, 0x07, 0x03, 0x01, - 0x07, 0x06, 0x07, 0x11, 0x0a, 0x50, 0x0f, 0x12, 0x07, 0x55, 0x07, 0x03, - 0x04, 0x1c, 0x0a, 0x09, 0x03, 0x08, 0x03, 0x07, 0x03, 0x02, 0x03, 0x03, - 0x03, 0x0c, 0x04, 0x05, 0x03, 0x0b, 0x06, 0x01, 0x0e, 0x15, 0x05, 0x3a, - 0x03, 0x11, 0x07, 0x06, 0x05, 0x10, 0x07, 0x57, 0x07, 0x02, 0x07, 0x15, - 0x0d, 0x50, 0x04, 0x43, 0x03, 0x2d, 0x03, 0x01, 0x04, 0x11, 0x06, 0x0f, - 0x0c, 0x3a, 0x04, 0x1d, 0x25, 0x5f, 0x20, 0x6d, 0x04, 0x6a, 0x25, 0x80, - 0xc8, 0x05, 0x82, 0xb0, 0x03, 0x1a, 0x06, 0x82, 0xfd, 0x03, 0x59, 0x07, - 0x15, 0x0b, 0x17, 0x09, 0x14, 0x0c, 0x14, 0x0c, 0x6a, 0x06, 0x0a, 0x06, - 0x1a, 0x06, 0x59, 0x07, 0x2b, 0x05, 0x46, 0x0a, 0x2c, 0x04, 0x0c, 0x04, - 0x01, 0x03, 0x31, 0x0b, 0x2c, 0x04, 0x1a, 0x06, 0x0b, 0x03, 0x80, 0xac, - 0x06, 0x0a, 0x06, 0x21, 0x3f, 0x4c, 0x04, 0x2d, 0x03, 0x74, 0x08, 0x3c, - 0x03, 0x0f, 0x03, 0x3c, 0x07, 0x38, 0x08, 0x2b, 0x05, 0x82, 0xff, 0x11, - 0x18, 0x08, 0x2f, 0x11, 0x2d, 0x03, 0x20, 0x10, 0x21, 0x0f, 0x80, 0x8c, - 0x04, 0x82, 0x97, 0x19, 0x0b, 0x15, 0x88, 0x94, 0x05, 0x2f, 0x05, 0x3b, - 0x07, 0x02, 0x0e, 0x18, 0x09, 0x80, 0xb3, 0x2d, 0x74, 0x0c, 0x80, 0xd6, - 0x1a, 0x0c, 0x05, 0x80, 0xff, 0x05, 0x80, 0xdf, 0x0c, 0xee, 0x0d, 0x03, - 0x84, 0x8d, 0x03, 0x37, 0x09, 0x81, 0x5c, 0x14, 0x80, 0xb8, 0x08, 0x80, - 0xcb, 0x2a, 0x38, 0x03, 0x0a, 0x06, 0x38, 0x08, 0x46, 0x08, 0x0c, 0x06, - 0x74, 0x0b, 0x1e, 0x03, 0x5a, 0x04, 0x59, 0x09, 0x80, 0x83, 0x18, 0x1c, - 0x0a, 0x16, 0x09, 0x4c, 0x04, 0x80, 0x8a, 0x06, 0xab, 0xa4, 0x0c, 0x17, - 0x04, 0x31, 0xa1, 0x04, 0x81, 0xda, 0x26, 0x07, 0x0c, 0x05, 0x05, 0x80, - 0xa5, 0x11, 0x81, 0x6d, 0x10, 0x78, 0x28, 0x2a, 0x06, 0x4c, 0x04, 0x80, - 0x8d, 0x04, 0x80, 0xbe, 0x03, 0x1b, 0x03, 0x0f, 0x0d, - }; - static constexpr unsigned char normal1[] = { - 0x5e, 0x22, 0x7b, 0x05, 0x03, 0x04, 0x2d, 0x03, 0x66, 0x03, 0x01, 0x2f, - 0x2e, 0x80, 0x82, 0x1d, 0x03, 0x31, 0x0f, 0x1c, 0x04, 0x24, 0x09, 0x1e, - 0x05, 0x2b, 0x05, 0x44, 0x04, 0x0e, 0x2a, 0x80, 0xaa, 0x06, 0x24, 0x04, - 0x24, 0x04, 0x28, 0x08, 0x34, 0x0b, 0x01, 0x80, 0x90, 0x81, 0x37, 0x09, - 0x16, 0x0a, 0x08, 0x80, 0x98, 0x39, 0x03, 0x63, 0x08, 0x09, 0x30, 0x16, - 0x05, 0x21, 0x03, 0x1b, 0x05, 0x01, 0x40, 0x38, 0x04, 0x4b, 0x05, 0x2f, - 0x04, 0x0a, 0x07, 0x09, 0x07, 0x40, 0x20, 0x27, 0x04, 0x0c, 0x09, 0x36, - 0x03, 0x3a, 0x05, 0x1a, 0x07, 0x04, 0x0c, 0x07, 0x50, 0x49, 0x37, 0x33, - 0x0d, 0x33, 0x07, 0x2e, 0x08, 0x0a, 0x81, 0x26, 0x52, 0x4e, 0x28, 0x08, - 0x2a, 0x56, 0x1c, 0x14, 0x17, 0x09, 0x4e, 0x04, 0x1e, 0x0f, 0x43, 0x0e, - 0x19, 0x07, 0x0a, 0x06, 0x48, 0x08, 0x27, 0x09, 0x75, 0x0b, 0x3f, 0x41, - 0x2a, 0x06, 0x3b, 0x05, 0x0a, 0x06, 0x51, 0x06, 0x01, 0x05, 0x10, 0x03, - 0x05, 0x80, 0x8b, 0x62, 0x1e, 0x48, 0x08, 0x0a, 0x80, 0xa6, 0x5e, 0x22, - 0x45, 0x0b, 0x0a, 0x06, 0x0d, 0x13, 0x39, 0x07, 0x0a, 0x36, 0x2c, 0x04, - 0x10, 0x80, 0xc0, 0x3c, 0x64, 0x53, 0x0c, 0x48, 0x09, 0x0a, 0x46, 0x45, - 0x1b, 0x48, 0x08, 0x53, 0x1d, 0x39, 0x81, 0x07, 0x46, 0x0a, 0x1d, 0x03, - 0x47, 0x49, 0x37, 0x03, 0x0e, 0x08, 0x0a, 0x06, 0x39, 0x07, 0x0a, 0x81, - 0x36, 0x19, 0x80, 0xb7, 0x01, 0x0f, 0x32, 0x0d, 0x83, 0x9b, 0x66, 0x75, - 0x0b, 0x80, 0xc4, 0x8a, 0xbc, 0x84, 0x2f, 0x8f, 0xd1, 0x82, 0x47, 0xa1, - 0xb9, 0x82, 0x39, 0x07, 0x2a, 0x04, 0x02, 0x60, 0x26, 0x0a, 0x46, 0x0a, - 0x28, 0x05, 0x13, 0x82, 0xb0, 0x5b, 0x65, 0x4b, 0x04, 0x39, 0x07, 0x11, - 0x40, 0x05, 0x0b, 0x02, 0x0e, 0x97, 0xf8, 0x08, 0x84, 0xd6, 0x2a, 0x09, - 0xa2, 0xf7, 0x81, 0x1f, 0x31, 0x03, 0x11, 0x04, 0x08, 0x81, 0x8c, 0x89, - 0x04, 0x6b, 0x05, 0x0d, 0x03, 0x09, 0x07, 0x10, 0x93, 0x60, 0x80, 0xf6, - 0x0a, 0x73, 0x08, 0x6e, 0x17, 0x46, 0x80, 0x9a, 0x14, 0x0c, 0x57, 0x09, - 0x19, 0x80, 0x87, 0x81, 0x47, 0x03, 0x85, 0x42, 0x0f, 0x15, 0x85, 0x50, - 0x2b, 0x80, 0xd5, 0x2d, 0x03, 0x1a, 0x04, 0x02, 0x81, 0x70, 0x3a, 0x05, - 0x01, 0x85, 0x00, 0x80, 0xd7, 0x29, 0x4c, 0x04, 0x0a, 0x04, 0x02, 0x83, - 0x11, 0x44, 0x4c, 0x3d, 0x80, 0xc2, 0x3c, 0x06, 0x01, 0x04, 0x55, 0x05, - 0x1b, 0x34, 0x02, 0x81, 0x0e, 0x2c, 0x04, 0x64, 0x0c, 0x56, 0x0a, 0x80, - 0xae, 0x38, 0x1d, 0x0d, 0x2c, 0x04, 0x09, 0x07, 0x02, 0x0e, 0x06, 0x80, - 0x9a, 0x83, 0xd8, 0x08, 0x0d, 0x03, 0x0d, 0x03, 0x74, 0x0c, 0x59, 0x07, - 0x0c, 0x14, 0x0c, 0x04, 0x38, 0x08, 0x0a, 0x06, 0x28, 0x08, 0x22, 0x4e, - 0x81, 0x54, 0x0c, 0x15, 0x03, 0x03, 0x05, 0x07, 0x09, 0x19, 0x07, 0x07, - 0x09, 0x03, 0x0d, 0x07, 0x29, 0x80, 0xcb, 0x25, 0x0a, 0x84, 0x06, - }; - auto lower = static_cast(cp); - if (cp < 0x10000) { - return is_printable(lower, singletons0, - sizeof(singletons0) / sizeof(*singletons0), - singletons0_lower, normal0, sizeof(normal0)); - } - if (cp < 0x20000) { - return is_printable(lower, singletons1, - sizeof(singletons1) / sizeof(*singletons1), - singletons1_lower, normal1, sizeof(normal1)); - } - if (0x2a6de <= cp && cp < 0x2a700) return false; - if (0x2b735 <= cp && cp < 0x2b740) return false; - if (0x2b81e <= cp && cp < 0x2b820) return false; - if (0x2cea2 <= cp && cp < 0x2ceb0) return false; - if (0x2ebe1 <= cp && cp < 0x2f800) return false; - if (0x2fa1e <= cp && cp < 0x30000) return false; - if (0x3134b <= cp && cp < 0xe0100) return false; - if (0xe01f0 <= cp && cp < 0x110000) return false; - return cp < 0x110000; -} - -} // namespace detail - -FMT_END_NAMESPACE - -#endif // FMT_FORMAT_INL_H_ diff --git a/include/spdlog/fmt/bundled/format.h b/include/spdlog/fmt/bundled/format.h deleted file mode 100644 index 67f0ab7..0000000 --- a/include/spdlog/fmt/bundled/format.h +++ /dev/null @@ -1,4427 +0,0 @@ -/* - Formatting library for C++ - - Copyright (c) 2012 - present, Victor Zverovich - - 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. - - --- Optional exception to the license --- - - As an exception, if, as a result of your compiling your source code, portions - of this Software are embedded into a machine-executable object form of such - source code, you may redistribute such embedded portions in such object form - without including the above copyright and permission notices. - */ - -#ifndef FMT_FORMAT_H_ -#define FMT_FORMAT_H_ - -#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES -# define _LIBCPP_REMOVE_TRANSITIVE_INCLUDES -# define FMT_REMOVE_TRANSITIVE_INCLUDES -#endif - -#include "base.h" - -#ifndef FMT_MODULE -# include // std::signbit -# include // uint32_t -# include // std::memcpy -# include // std::initializer_list -# include // std::numeric_limits -# if defined(__GLIBCXX__) && !defined(_GLIBCXX_USE_DUAL_ABI) -// Workaround for pre gcc 5 libstdc++. -# include // std::allocator_traits -# endif -# include // std::runtime_error -# include // std::string -# include // std::system_error - -// Checking FMT_CPLUSPLUS for warning suppression in MSVC. -# if FMT_HAS_INCLUDE() && FMT_CPLUSPLUS > 201703L -# include // std::bit_cast -# endif - -// libc++ supports string_view in pre-c++17. -# if FMT_HAS_INCLUDE() && \ - (FMT_CPLUSPLUS >= 201703L || defined(_LIBCPP_VERSION)) -# include -# define FMT_USE_STRING_VIEW -# endif -#endif // FMT_MODULE - -#if defined __cpp_inline_variables && __cpp_inline_variables >= 201606L -# define FMT_INLINE_VARIABLE inline -#else -# define FMT_INLINE_VARIABLE -#endif - -#ifndef FMT_NO_UNIQUE_ADDRESS -# if FMT_CPLUSPLUS >= 202002L -# if FMT_HAS_CPP_ATTRIBUTE(no_unique_address) -# define FMT_NO_UNIQUE_ADDRESS [[no_unique_address]] -// VS2019 v16.10 and later except clang-cl (https://reviews.llvm.org/D110485). -# elif (FMT_MSC_VERSION >= 1929) && !FMT_CLANG_VERSION -# define FMT_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] -# endif -# endif -#endif -#ifndef FMT_NO_UNIQUE_ADDRESS -# define FMT_NO_UNIQUE_ADDRESS -#endif - -// Visibility when compiled as a shared library/object. -#if defined(FMT_LIB_EXPORT) || defined(FMT_SHARED) -# define FMT_SO_VISIBILITY(value) FMT_VISIBILITY(value) -#else -# define FMT_SO_VISIBILITY(value) -#endif - -#ifdef __has_builtin -# define FMT_HAS_BUILTIN(x) __has_builtin(x) -#else -# define FMT_HAS_BUILTIN(x) 0 -#endif - -#if FMT_GCC_VERSION || FMT_CLANG_VERSION -# define FMT_NOINLINE __attribute__((noinline)) -#else -# define FMT_NOINLINE -#endif - -namespace std { -template <> struct iterator_traits { - using iterator_category = output_iterator_tag; - using value_type = char; -}; -} // namespace std - -#ifndef FMT_THROW -# if FMT_EXCEPTIONS -# if FMT_MSC_VERSION || defined(__NVCC__) -FMT_BEGIN_NAMESPACE -namespace detail { -template inline void do_throw(const Exception& x) { - // Silence unreachable code warnings in MSVC and NVCC because these - // are nearly impossible to fix in a generic code. - volatile bool b = true; - if (b) throw x; -} -} // namespace detail -FMT_END_NAMESPACE -# define FMT_THROW(x) detail::do_throw(x) -# else -# define FMT_THROW(x) throw x -# endif -# else -# define FMT_THROW(x) \ - ::fmt::detail::assert_fail(__FILE__, __LINE__, (x).what()) -# endif -#endif - -#ifndef FMT_MAYBE_UNUSED -# if FMT_HAS_CPP17_ATTRIBUTE(maybe_unused) -# define FMT_MAYBE_UNUSED [[maybe_unused]] -# else -# define FMT_MAYBE_UNUSED -# endif -#endif - -#ifndef FMT_USE_USER_DEFINED_LITERALS -// EDG based compilers (Intel, NVIDIA, Elbrus, etc), GCC and MSVC support UDLs. -// -// GCC before 4.9 requires a space in `operator"" _a` which is invalid in later -// compiler versions. -# if (FMT_HAS_FEATURE(cxx_user_literals) || FMT_GCC_VERSION >= 409 || \ - FMT_MSC_VERSION >= 1900) && \ - (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= /* UDL feature */ 480) -# define FMT_USE_USER_DEFINED_LITERALS 1 -# else -# define FMT_USE_USER_DEFINED_LITERALS 0 -# endif -#endif - -// Defining FMT_REDUCE_INT_INSTANTIATIONS to 1, will reduce the number of -// integer formatter template instantiations to just one by only using the -// largest integer type. This results in a reduction in binary size but will -// cause a decrease in integer formatting performance. -#if !defined(FMT_REDUCE_INT_INSTANTIATIONS) -# define FMT_REDUCE_INT_INSTANTIATIONS 0 -#endif - -// __builtin_clz is broken in clang with Microsoft CodeGen: -// https://github.com/fmtlib/fmt/issues/519. -#if !FMT_MSC_VERSION -# if FMT_HAS_BUILTIN(__builtin_clz) || FMT_GCC_VERSION || FMT_ICC_VERSION -# define FMT_BUILTIN_CLZ(n) __builtin_clz(n) -# endif -# if FMT_HAS_BUILTIN(__builtin_clzll) || FMT_GCC_VERSION || FMT_ICC_VERSION -# define FMT_BUILTIN_CLZLL(n) __builtin_clzll(n) -# endif -#endif - -// __builtin_ctz is broken in Intel Compiler Classic on Windows: -// https://github.com/fmtlib/fmt/issues/2510. -#ifndef __ICL -# if FMT_HAS_BUILTIN(__builtin_ctz) || FMT_GCC_VERSION || FMT_ICC_VERSION || \ - defined(__NVCOMPILER) -# define FMT_BUILTIN_CTZ(n) __builtin_ctz(n) -# endif -# if FMT_HAS_BUILTIN(__builtin_ctzll) || FMT_GCC_VERSION || \ - FMT_ICC_VERSION || defined(__NVCOMPILER) -# define FMT_BUILTIN_CTZLL(n) __builtin_ctzll(n) -# endif -#endif - -#if FMT_MSC_VERSION -# include // _BitScanReverse[64], _BitScanForward[64], _umul128 -#endif - -// Some compilers masquerade as both MSVC and GCC-likes or otherwise support -// __builtin_clz and __builtin_clzll, so only define FMT_BUILTIN_CLZ using the -// MSVC intrinsics if the clz and clzll builtins are not available. -#if FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL) && \ - !defined(FMT_BUILTIN_CTZLL) -FMT_BEGIN_NAMESPACE -namespace detail { -// Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning. -# if !defined(__clang__) -# pragma intrinsic(_BitScanForward) -# pragma intrinsic(_BitScanReverse) -# if defined(_WIN64) -# pragma intrinsic(_BitScanForward64) -# pragma intrinsic(_BitScanReverse64) -# endif -# endif - -inline auto clz(uint32_t x) -> int { - unsigned long r = 0; - _BitScanReverse(&r, x); - FMT_ASSERT(x != 0, ""); - // Static analysis complains about using uninitialized data - // "r", but the only way that can happen is if "x" is 0, - // which the callers guarantee to not happen. - FMT_MSC_WARNING(suppress : 6102) - return 31 ^ static_cast(r); -} -# define FMT_BUILTIN_CLZ(n) detail::clz(n) - -inline auto clzll(uint64_t x) -> int { - unsigned long r = 0; -# ifdef _WIN64 - _BitScanReverse64(&r, x); -# else - // Scan the high 32 bits. - if (_BitScanReverse(&r, static_cast(x >> 32))) - return 63 ^ static_cast(r + 32); - // Scan the low 32 bits. - _BitScanReverse(&r, static_cast(x)); -# endif - FMT_ASSERT(x != 0, ""); - FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning. - return 63 ^ static_cast(r); -} -# define FMT_BUILTIN_CLZLL(n) detail::clzll(n) - -inline auto ctz(uint32_t x) -> int { - unsigned long r = 0; - _BitScanForward(&r, x); - FMT_ASSERT(x != 0, ""); - FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning. - return static_cast(r); -} -# define FMT_BUILTIN_CTZ(n) detail::ctz(n) - -inline auto ctzll(uint64_t x) -> int { - unsigned long r = 0; - FMT_ASSERT(x != 0, ""); - FMT_MSC_WARNING(suppress : 6102) // Suppress a bogus static analysis warning. -# ifdef _WIN64 - _BitScanForward64(&r, x); -# else - // Scan the low 32 bits. - if (_BitScanForward(&r, static_cast(x))) return static_cast(r); - // Scan the high 32 bits. - _BitScanForward(&r, static_cast(x >> 32)); - r += 32; -# endif - return static_cast(r); -} -# define FMT_BUILTIN_CTZLL(n) detail::ctzll(n) -} // namespace detail -FMT_END_NAMESPACE -#endif - -FMT_BEGIN_NAMESPACE - -template -struct is_contiguous> - : std::true_type {}; - -namespace detail { - -FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) { - ignore_unused(condition); -#ifdef FMT_FUZZ - if (condition) throw std::runtime_error("fuzzing limit reached"); -#endif -} - -#if defined(FMT_USE_STRING_VIEW) -template using std_string_view = std::basic_string_view; -#else -template struct std_string_view {}; -#endif - -// Implementation of std::bit_cast for pre-C++20. -template -FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To { -#ifdef __cpp_lib_bit_cast - if (is_constant_evaluated()) return std::bit_cast(from); -#endif - auto to = To(); - // The cast suppresses a bogus -Wclass-memaccess on GCC. - std::memcpy(static_cast(&to), &from, sizeof(to)); - return to; -} - -inline auto is_big_endian() -> bool { -#ifdef _WIN32 - return false; -#elif defined(__BIG_ENDIAN__) - return true; -#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) - return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; -#else - struct bytes { - char data[sizeof(int)]; - }; - return bit_cast(1).data[0] == 0; -#endif -} - -class uint128_fallback { - private: - uint64_t lo_, hi_; - - public: - constexpr uint128_fallback(uint64_t hi, uint64_t lo) : lo_(lo), hi_(hi) {} - constexpr uint128_fallback(uint64_t value = 0) : lo_(value), hi_(0) {} - - constexpr auto high() const noexcept -> uint64_t { return hi_; } - constexpr auto low() const noexcept -> uint64_t { return lo_; } - - template ::value)> - constexpr explicit operator T() const { - return static_cast(lo_); - } - - friend constexpr auto operator==(const uint128_fallback& lhs, - const uint128_fallback& rhs) -> bool { - return lhs.hi_ == rhs.hi_ && lhs.lo_ == rhs.lo_; - } - friend constexpr auto operator!=(const uint128_fallback& lhs, - const uint128_fallback& rhs) -> bool { - return !(lhs == rhs); - } - friend constexpr auto operator>(const uint128_fallback& lhs, - const uint128_fallback& rhs) -> bool { - return lhs.hi_ != rhs.hi_ ? lhs.hi_ > rhs.hi_ : lhs.lo_ > rhs.lo_; - } - friend constexpr auto operator|(const uint128_fallback& lhs, - const uint128_fallback& rhs) - -> uint128_fallback { - return {lhs.hi_ | rhs.hi_, lhs.lo_ | rhs.lo_}; - } - friend constexpr auto operator&(const uint128_fallback& lhs, - const uint128_fallback& rhs) - -> uint128_fallback { - return {lhs.hi_ & rhs.hi_, lhs.lo_ & rhs.lo_}; - } - friend constexpr auto operator~(const uint128_fallback& n) - -> uint128_fallback { - return {~n.hi_, ~n.lo_}; - } - friend auto operator+(const uint128_fallback& lhs, - const uint128_fallback& rhs) -> uint128_fallback { - auto result = uint128_fallback(lhs); - result += rhs; - return result; - } - friend auto operator*(const uint128_fallback& lhs, uint32_t rhs) - -> uint128_fallback { - FMT_ASSERT(lhs.hi_ == 0, ""); - uint64_t hi = (lhs.lo_ >> 32) * rhs; - uint64_t lo = (lhs.lo_ & ~uint32_t()) * rhs; - uint64_t new_lo = (hi << 32) + lo; - return {(hi >> 32) + (new_lo < lo ? 1 : 0), new_lo}; - } - friend auto operator-(const uint128_fallback& lhs, uint64_t rhs) - -> uint128_fallback { - return {lhs.hi_ - (lhs.lo_ < rhs ? 1 : 0), lhs.lo_ - rhs}; - } - FMT_CONSTEXPR auto operator>>(int shift) const -> uint128_fallback { - if (shift == 64) return {0, hi_}; - if (shift > 64) return uint128_fallback(0, hi_) >> (shift - 64); - return {hi_ >> shift, (hi_ << (64 - shift)) | (lo_ >> shift)}; - } - FMT_CONSTEXPR auto operator<<(int shift) const -> uint128_fallback { - if (shift == 64) return {lo_, 0}; - if (shift > 64) return uint128_fallback(lo_, 0) << (shift - 64); - return {hi_ << shift | (lo_ >> (64 - shift)), (lo_ << shift)}; - } - FMT_CONSTEXPR auto operator>>=(int shift) -> uint128_fallback& { - return *this = *this >> shift; - } - FMT_CONSTEXPR void operator+=(uint128_fallback n) { - uint64_t new_lo = lo_ + n.lo_; - uint64_t new_hi = hi_ + n.hi_ + (new_lo < lo_ ? 1 : 0); - FMT_ASSERT(new_hi >= hi_, ""); - lo_ = new_lo; - hi_ = new_hi; - } - FMT_CONSTEXPR void operator&=(uint128_fallback n) { - lo_ &= n.lo_; - hi_ &= n.hi_; - } - - FMT_CONSTEXPR20 auto operator+=(uint64_t n) noexcept -> uint128_fallback& { - if (is_constant_evaluated()) { - lo_ += n; - hi_ += (lo_ < n ? 1 : 0); - return *this; - } -#if FMT_HAS_BUILTIN(__builtin_addcll) && !defined(__ibmxl__) - unsigned long long carry; - lo_ = __builtin_addcll(lo_, n, 0, &carry); - hi_ += carry; -#elif FMT_HAS_BUILTIN(__builtin_ia32_addcarryx_u64) && !defined(__ibmxl__) - unsigned long long result; - auto carry = __builtin_ia32_addcarryx_u64(0, lo_, n, &result); - lo_ = result; - hi_ += carry; -#elif defined(_MSC_VER) && defined(_M_X64) - auto carry = _addcarry_u64(0, lo_, n, &lo_); - _addcarry_u64(carry, hi_, 0, &hi_); -#else - lo_ += n; - hi_ += (lo_ < n ? 1 : 0); -#endif - return *this; - } -}; - -using uint128_t = conditional_t; - -#ifdef UINTPTR_MAX -using uintptr_t = ::uintptr_t; -#else -using uintptr_t = uint128_t; -#endif - -// Returns the largest possible value for type T. Same as -// std::numeric_limits::max() but shorter and not affected by the max macro. -template constexpr auto max_value() -> T { - return (std::numeric_limits::max)(); -} -template constexpr auto num_bits() -> int { - return std::numeric_limits::digits; -} -// std::numeric_limits::digits may return 0 for 128-bit ints. -template <> constexpr auto num_bits() -> int { return 128; } -template <> constexpr auto num_bits() -> int { return 128; } -template <> constexpr auto num_bits() -> int { return 128; } - -// A heterogeneous bit_cast used for converting 96-bit long double to uint128_t -// and 128-bit pointers to uint128_fallback. -template sizeof(From))> -inline auto bit_cast(const From& from) -> To { - constexpr auto size = static_cast(sizeof(From) / sizeof(unsigned)); - struct data_t { - unsigned value[static_cast(size)]; - } data = bit_cast(from); - auto result = To(); - if (const_check(is_big_endian())) { - for (int i = 0; i < size; ++i) - result = (result << num_bits()) | data.value[i]; - } else { - for (int i = size - 1; i >= 0; --i) - result = (result << num_bits()) | data.value[i]; - } - return result; -} - -template -FMT_CONSTEXPR20 inline auto countl_zero_fallback(UInt n) -> int { - int lz = 0; - constexpr UInt msb_mask = static_cast(1) << (num_bits() - 1); - for (; (n & msb_mask) == 0; n <<= 1) lz++; - return lz; -} - -FMT_CONSTEXPR20 inline auto countl_zero(uint32_t n) -> int { -#ifdef FMT_BUILTIN_CLZ - if (!is_constant_evaluated()) return FMT_BUILTIN_CLZ(n); -#endif - return countl_zero_fallback(n); -} - -FMT_CONSTEXPR20 inline auto countl_zero(uint64_t n) -> int { -#ifdef FMT_BUILTIN_CLZLL - if (!is_constant_evaluated()) return FMT_BUILTIN_CLZLL(n); -#endif - return countl_zero_fallback(n); -} - -FMT_INLINE void assume(bool condition) { - (void)condition; -#if FMT_HAS_BUILTIN(__builtin_assume) && !FMT_ICC_VERSION - __builtin_assume(condition); -#elif FMT_GCC_VERSION - if (!condition) __builtin_unreachable(); -#endif -} - -// An approximation of iterator_t for pre-C++20 systems. -template -using iterator_t = decltype(std::begin(std::declval())); -template using sentinel_t = decltype(std::end(std::declval())); - -// A workaround for std::string not having mutable data() until C++17. -template -inline auto get_data(std::basic_string& s) -> Char* { - return &s[0]; -} -template -inline auto get_data(Container& c) -> typename Container::value_type* { - return c.data(); -} - -// Attempts to reserve space for n extra characters in the output range. -// Returns a pointer to the reserved range or a reference to it. -template ::value&& - is_contiguous::value)> -#if FMT_CLANG_VERSION >= 307 && !FMT_ICC_VERSION -__attribute__((no_sanitize("undefined"))) -#endif -inline auto -reserve(OutputIt it, size_t n) -> typename OutputIt::value_type* { - auto& c = get_container(it); - size_t size = c.size(); - c.resize(size + n); - return get_data(c) + size; -} - -template -inline auto reserve(basic_appender it, size_t n) -> basic_appender { - buffer& buf = get_container(it); - buf.try_reserve(buf.size() + n); - return it; -} - -template -constexpr auto reserve(Iterator& it, size_t) -> Iterator& { - return it; -} - -template -using reserve_iterator = - remove_reference_t(), 0))>; - -template -constexpr auto to_pointer(OutputIt, size_t) -> T* { - return nullptr; -} -template auto to_pointer(basic_appender it, size_t n) -> T* { - buffer& buf = get_container(it); - auto size = buf.size(); - buf.try_reserve(size + n); - if (buf.capacity() < size + n) return nullptr; - buf.try_resize(size + n); - return buf.data() + size; -} - -template ::value&& - is_contiguous::value)> -inline auto base_iterator(OutputIt it, - typename OutputIt::container_type::value_type*) - -> OutputIt { - return it; -} - -template -constexpr auto base_iterator(Iterator, Iterator it) -> Iterator { - return it; -} - -// is spectacularly slow to compile in C++20 so use a simple fill_n -// instead (#1998). -template -FMT_CONSTEXPR auto fill_n(OutputIt out, Size count, const T& value) - -> OutputIt { - for (Size i = 0; i < count; ++i) *out++ = value; - return out; -} -template -FMT_CONSTEXPR20 auto fill_n(T* out, Size count, char value) -> T* { - if (is_constant_evaluated()) { - return fill_n(out, count, value); - } - std::memset(out, value, to_unsigned(count)); - return out + count; -} - -template -FMT_CONSTEXPR FMT_NOINLINE auto copy_noinline(InputIt begin, InputIt end, - OutputIt out) -> OutputIt { - return copy(begin, end, out); -} - -// A public domain branchless UTF-8 decoder by Christopher Wellons: -// https://github.com/skeeto/branchless-utf8 -/* Decode the next character, c, from s, reporting errors in e. - * - * Since this is a branchless decoder, four bytes will be read from the - * buffer regardless of the actual length of the next character. This - * means the buffer _must_ have at least three bytes of zero padding - * following the end of the data stream. - * - * Errors are reported in e, which will be non-zero if the parsed - * character was somehow invalid: invalid byte sequence, non-canonical - * encoding, or a surrogate half. - * - * The function returns a pointer to the next character. When an error - * occurs, this pointer will be a guess that depends on the particular - * error, but it will always advance at least one byte. - */ -FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e) - -> const char* { - constexpr const int masks[] = {0x00, 0x7f, 0x1f, 0x0f, 0x07}; - constexpr const uint32_t mins[] = {4194304, 0, 128, 2048, 65536}; - constexpr const int shiftc[] = {0, 18, 12, 6, 0}; - constexpr const int shifte[] = {0, 6, 4, 2, 0}; - - int len = "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4" - [static_cast(*s) >> 3]; - // Compute the pointer to the next character early so that the next - // iteration can start working on the next character. Neither Clang - // nor GCC figure out this reordering on their own. - const char* next = s + len + !len; - - using uchar = unsigned char; - - // Assume a four-byte character and load four bytes. Unused bits are - // shifted out. - *c = uint32_t(uchar(s[0]) & masks[len]) << 18; - *c |= uint32_t(uchar(s[1]) & 0x3f) << 12; - *c |= uint32_t(uchar(s[2]) & 0x3f) << 6; - *c |= uint32_t(uchar(s[3]) & 0x3f) << 0; - *c >>= shiftc[len]; - - // Accumulate the various error conditions. - *e = (*c < mins[len]) << 6; // non-canonical encoding - *e |= ((*c >> 11) == 0x1b) << 7; // surrogate half? - *e |= (*c > 0x10FFFF) << 8; // out of range? - *e |= (uchar(s[1]) & 0xc0) >> 2; - *e |= (uchar(s[2]) & 0xc0) >> 4; - *e |= uchar(s[3]) >> 6; - *e ^= 0x2a; // top two bits of each tail byte correct? - *e >>= shifte[len]; - - return next; -} - -constexpr FMT_INLINE_VARIABLE uint32_t invalid_code_point = ~uint32_t(); - -// Invokes f(cp, sv) for every code point cp in s with sv being the string view -// corresponding to the code point. cp is invalid_code_point on error. -template -FMT_CONSTEXPR void for_each_codepoint(string_view s, F f) { - auto decode = [f](const char* buf_ptr, const char* ptr) { - auto cp = uint32_t(); - auto error = 0; - auto end = utf8_decode(buf_ptr, &cp, &error); - bool result = f(error ? invalid_code_point : cp, - string_view(ptr, error ? 1 : to_unsigned(end - buf_ptr))); - return result ? (error ? buf_ptr + 1 : end) : nullptr; - }; - auto p = s.data(); - const size_t block_size = 4; // utf8_decode always reads blocks of 4 chars. - if (s.size() >= block_size) { - for (auto end = p + s.size() - block_size + 1; p < end;) { - p = decode(p, p); - if (!p) return; - } - } - if (auto num_chars_left = s.data() + s.size() - p) { - char buf[2 * block_size - 1] = {}; - copy(p, p + num_chars_left, buf); - const char* buf_ptr = buf; - do { - auto end = decode(buf_ptr, p); - if (!end) return; - p += end - buf_ptr; - buf_ptr = end; - } while (buf_ptr - buf < num_chars_left); - } -} - -template -inline auto compute_width(basic_string_view s) -> size_t { - return s.size(); -} - -// Computes approximate display width of a UTF-8 string. -FMT_CONSTEXPR inline auto compute_width(string_view s) -> size_t { - size_t num_code_points = 0; - // It is not a lambda for compatibility with C++14. - struct count_code_points { - size_t* count; - FMT_CONSTEXPR auto operator()(uint32_t cp, string_view) const -> bool { - *count += detail::to_unsigned( - 1 + - (cp >= 0x1100 && - (cp <= 0x115f || // Hangul Jamo init. consonants - cp == 0x2329 || // LEFT-POINTING ANGLE BRACKET - cp == 0x232a || // RIGHT-POINTING ANGLE BRACKET - // CJK ... Yi except IDEOGRAPHIC HALF FILL SPACE: - (cp >= 0x2e80 && cp <= 0xa4cf && cp != 0x303f) || - (cp >= 0xac00 && cp <= 0xd7a3) || // Hangul Syllables - (cp >= 0xf900 && cp <= 0xfaff) || // CJK Compatibility Ideographs - (cp >= 0xfe10 && cp <= 0xfe19) || // Vertical Forms - (cp >= 0xfe30 && cp <= 0xfe6f) || // CJK Compatibility Forms - (cp >= 0xff00 && cp <= 0xff60) || // Fullwidth Forms - (cp >= 0xffe0 && cp <= 0xffe6) || // Fullwidth Forms - (cp >= 0x20000 && cp <= 0x2fffd) || // CJK - (cp >= 0x30000 && cp <= 0x3fffd) || - // Miscellaneous Symbols and Pictographs + Emoticons: - (cp >= 0x1f300 && cp <= 0x1f64f) || - // Supplemental Symbols and Pictographs: - (cp >= 0x1f900 && cp <= 0x1f9ff)))); - return true; - } - }; - // We could avoid branches by using utf8_decode directly. - for_each_codepoint(s, count_code_points{&num_code_points}); - return num_code_points; -} - -template -inline auto code_point_index(basic_string_view s, size_t n) -> size_t { - size_t size = s.size(); - return n < size ? n : size; -} - -// Calculates the index of the nth code point in a UTF-8 string. -inline auto code_point_index(string_view s, size_t n) -> size_t { - size_t result = s.size(); - const char* begin = s.begin(); - for_each_codepoint(s, [begin, &n, &result](uint32_t, string_view sv) { - if (n != 0) { - --n; - return true; - } - result = to_unsigned(sv.begin() - begin); - return false; - }); - return result; -} - -template struct is_integral : std::is_integral {}; -template <> struct is_integral : std::true_type {}; -template <> struct is_integral : std::true_type {}; - -template -using is_signed = - std::integral_constant::is_signed || - std::is_same::value>; - -template -using is_integer = - bool_constant::value && !std::is_same::value && - !std::is_same::value && - !std::is_same::value>; - -#ifndef FMT_USE_FLOAT -# define FMT_USE_FLOAT 1 -#endif -#ifndef FMT_USE_DOUBLE -# define FMT_USE_DOUBLE 1 -#endif -#ifndef FMT_USE_LONG_DOUBLE -# define FMT_USE_LONG_DOUBLE 1 -#endif - -#if defined(FMT_USE_FLOAT128) -// Use the provided definition. -#elif FMT_CLANG_VERSION && FMT_HAS_INCLUDE() -# define FMT_USE_FLOAT128 1 -#elif FMT_GCC_VERSION && defined(_GLIBCXX_USE_FLOAT128) && \ - !defined(__STRICT_ANSI__) -# define FMT_USE_FLOAT128 1 -#else -# define FMT_USE_FLOAT128 0 -#endif -#if FMT_USE_FLOAT128 -using float128 = __float128; -#else -using float128 = void; -#endif - -template using is_float128 = std::is_same; - -template -using is_floating_point = - bool_constant::value || is_float128::value>; - -template ::value> -struct is_fast_float : bool_constant::is_iec559 && - sizeof(T) <= sizeof(double)> {}; -template struct is_fast_float : std::false_type {}; - -template -using is_double_double = bool_constant::digits == 106>; - -#ifndef FMT_USE_FULL_CACHE_DRAGONBOX -# define FMT_USE_FULL_CACHE_DRAGONBOX 0 -#endif - -template -struct is_locale : std::false_type {}; -template -struct is_locale> : std::true_type {}; -} // namespace detail - -FMT_BEGIN_EXPORT - -// The number of characters to store in the basic_memory_buffer object itself -// to avoid dynamic memory allocation. -enum { inline_buffer_size = 500 }; - -/** - * A dynamically growing memory buffer for trivially copyable/constructible - * types with the first `SIZE` elements stored in the object itself. Most - * commonly used via the `memory_buffer` alias for `char`. - * - * **Example**: - * - * auto out = fmt::memory_buffer(); - * fmt::format_to(std::back_inserter(out), "The answer is {}.", 42); - * - * This will append "The answer is 42." to `out`. The buffer content can be - * converted to `std::string` with `to_string(out)`. - */ -template > -class basic_memory_buffer : public detail::buffer { - private: - T store_[SIZE]; - - // Don't inherit from Allocator to avoid generating type_info for it. - FMT_NO_UNIQUE_ADDRESS Allocator alloc_; - - // Deallocate memory allocated by the buffer. - FMT_CONSTEXPR20 void deallocate() { - T* data = this->data(); - if (data != store_) alloc_.deallocate(data, this->capacity()); - } - - static FMT_CONSTEXPR20 void grow(detail::buffer& buf, size_t size) { - detail::abort_fuzzing_if(size > 5000); - auto& self = static_cast(buf); - const size_t max_size = - std::allocator_traits::max_size(self.alloc_); - size_t old_capacity = buf.capacity(); - size_t new_capacity = old_capacity + old_capacity / 2; - if (size > new_capacity) - new_capacity = size; - else if (new_capacity > max_size) - new_capacity = size > max_size ? size : max_size; - T* old_data = buf.data(); - T* new_data = self.alloc_.allocate(new_capacity); - // Suppress a bogus -Wstringop-overflow in gcc 13.1 (#3481). - detail::assume(buf.size() <= new_capacity); - // The following code doesn't throw, so the raw pointer above doesn't leak. - memcpy(new_data, old_data, buf.size() * sizeof(T)); - self.set(new_data, new_capacity); - // deallocate must not throw according to the standard, but even if it does, - // the buffer already uses the new storage and will deallocate it in - // destructor. - if (old_data != self.store_) self.alloc_.deallocate(old_data, old_capacity); - } - - public: - using value_type = T; - using const_reference = const T&; - - FMT_CONSTEXPR20 explicit basic_memory_buffer( - const Allocator& alloc = Allocator()) - : detail::buffer(grow), alloc_(alloc) { - this->set(store_, SIZE); - if (detail::is_constant_evaluated()) detail::fill_n(store_, SIZE, T()); - } - FMT_CONSTEXPR20 ~basic_memory_buffer() { deallocate(); } - - private: - // Move data from other to this buffer. - FMT_CONSTEXPR20 void move(basic_memory_buffer& other) { - alloc_ = std::move(other.alloc_); - T* data = other.data(); - size_t size = other.size(), capacity = other.capacity(); - if (data == other.store_) { - this->set(store_, capacity); - detail::copy(other.store_, other.store_ + size, store_); - } else { - this->set(data, capacity); - // Set pointer to the inline array so that delete is not called - // when deallocating. - other.set(other.store_, 0); - other.clear(); - } - this->resize(size); - } - - public: - /// Constructs a `basic_memory_buffer` object moving the content of the other - /// object to it. - FMT_CONSTEXPR20 basic_memory_buffer(basic_memory_buffer&& other) noexcept - : detail::buffer(grow) { - move(other); - } - - /// Moves the content of the other `basic_memory_buffer` object to this one. - auto operator=(basic_memory_buffer&& other) noexcept -> basic_memory_buffer& { - FMT_ASSERT(this != &other, ""); - deallocate(); - move(other); - return *this; - } - - // Returns a copy of the allocator associated with this buffer. - auto get_allocator() const -> Allocator { return alloc_; } - - /// Resizes the buffer to contain `count` elements. If T is a POD type new - /// elements may not be initialized. - FMT_CONSTEXPR20 void resize(size_t count) { this->try_resize(count); } - - /// Increases the buffer capacity to `new_capacity`. - void reserve(size_t new_capacity) { this->try_reserve(new_capacity); } - - using detail::buffer::append; - template - void append(const ContiguousRange& range) { - append(range.data(), range.data() + range.size()); - } -}; - -using memory_buffer = basic_memory_buffer; - -template -struct is_contiguous> : std::true_type { -}; - -FMT_END_EXPORT -namespace detail { -FMT_API auto write_console(int fd, string_view text) -> bool; -FMT_API void print(std::FILE*, string_view); -} // namespace detail - -FMT_BEGIN_EXPORT - -// Suppress a misleading warning in older versions of clang. -#if FMT_CLANG_VERSION -# pragma clang diagnostic ignored "-Wweak-vtables" -#endif - -/// An error reported from a formatting function. -class FMT_SO_VISIBILITY("default") format_error : public std::runtime_error { - public: - using std::runtime_error::runtime_error; -}; - -namespace detail_exported { -#if FMT_USE_NONTYPE_TEMPLATE_ARGS -template struct fixed_string { - constexpr fixed_string(const Char (&str)[N]) { - detail::copy(static_cast(str), - str + N, data); - } - Char data[N] = {}; -}; -#endif - -// Converts a compile-time string to basic_string_view. -template -constexpr auto compile_string_to_view(const Char (&s)[N]) - -> basic_string_view { - // Remove trailing NUL character if needed. Won't be present if this is used - // with a raw character array (i.e. not defined as a string). - return {s, N - (std::char_traits::to_int_type(s[N - 1]) == 0 ? 1 : 0)}; -} -template -constexpr auto compile_string_to_view(basic_string_view s) - -> basic_string_view { - return s; -} -} // namespace detail_exported - -// A generic formatting context with custom output iterator and character -// (code unit) support. Char is the format string code unit type which can be -// different from OutputIt::value_type. -template class generic_context { - private: - OutputIt out_; - basic_format_args args_; - detail::locale_ref loc_; - - public: - using char_type = Char; - using iterator = OutputIt; - using parse_context_type = basic_format_parse_context; - template using formatter_type = formatter; - - constexpr generic_context(OutputIt out, - basic_format_args ctx_args, - detail::locale_ref loc = {}) - : out_(out), args_(ctx_args), loc_(loc) {} - generic_context(generic_context&&) = default; - generic_context(const generic_context&) = delete; - void operator=(const generic_context&) = delete; - - constexpr auto arg(int id) const -> basic_format_arg { - return args_.get(id); - } - auto arg(basic_string_view name) -> basic_format_arg { - return args_.get(name); - } - FMT_CONSTEXPR auto arg_id(basic_string_view name) -> int { - return args_.get_id(name); - } - auto args() const -> const basic_format_args& { - return args_; - } - - FMT_CONSTEXPR auto out() -> iterator { return out_; } - - void advance_to(iterator it) { - if (!detail::is_back_insert_iterator()) out_ = it; - } - - FMT_CONSTEXPR auto locale() -> detail::locale_ref { return loc_; } -}; - -class loc_value { - private: - basic_format_arg value_; - - public: - template ::value)> - loc_value(T value) : value_(detail::make_arg(value)) {} - - template ::value)> - loc_value(T) {} - - template auto visit(Visitor&& vis) -> decltype(vis(0)) { - return value_.visit(vis); - } -}; - -// A locale facet that formats values in UTF-8. -// It is parameterized on the locale to avoid the heavy include. -template class format_facet : public Locale::facet { - private: - std::string separator_; - std::string grouping_; - std::string decimal_point_; - - protected: - virtual auto do_put(appender out, loc_value val, - const format_specs& specs) const -> bool; - - public: - static FMT_API typename Locale::id id; - - explicit format_facet(Locale& loc); - explicit format_facet(string_view sep = "", - std::initializer_list g = {3}, - std::string decimal_point = ".") - : separator_(sep.data(), sep.size()), - grouping_(g.begin(), g.end()), - decimal_point_(decimal_point) {} - - auto put(appender out, loc_value val, const format_specs& specs) const - -> bool { - return do_put(out, val, specs); - } -}; - -FMT_END_EXPORT - -namespace detail { - -// Returns true if value is negative, false otherwise. -// Same as `value < 0` but doesn't produce warnings if T is an unsigned type. -template ::value)> -constexpr auto is_negative(T value) -> bool { - return value < 0; -} -template ::value)> -constexpr auto is_negative(T) -> bool { - return false; -} - -template -FMT_CONSTEXPR auto is_supported_floating_point(T) -> bool { - if (std::is_same()) return FMT_USE_FLOAT; - if (std::is_same()) return FMT_USE_DOUBLE; - if (std::is_same()) return FMT_USE_LONG_DOUBLE; - return true; -} - -// Smallest of uint32_t, uint64_t, uint128_t that is large enough to -// represent all values of an integral type T. -template -using uint32_or_64_or_128_t = - conditional_t() <= 32 && !FMT_REDUCE_INT_INSTANTIATIONS, - uint32_t, - conditional_t() <= 64, uint64_t, uint128_t>>; -template -using uint64_or_128_t = conditional_t() <= 64, uint64_t, uint128_t>; - -#define FMT_POWERS_OF_10(factor) \ - factor * 10, (factor) * 100, (factor) * 1000, (factor) * 10000, \ - (factor) * 100000, (factor) * 1000000, (factor) * 10000000, \ - (factor) * 100000000, (factor) * 1000000000 - -// Converts value in the range [0, 100) to a string. -constexpr auto digits2(size_t value) -> const char* { - // GCC generates slightly better code when value is pointer-size. - return &"0001020304050607080910111213141516171819" - "2021222324252627282930313233343536373839" - "4041424344454647484950515253545556575859" - "6061626364656667686970717273747576777879" - "8081828384858687888990919293949596979899"[value * 2]; -} - -// Sign is a template parameter to workaround a bug in gcc 4.8. -template constexpr auto sign(Sign s) -> Char { -#if !FMT_GCC_VERSION || FMT_GCC_VERSION >= 604 - static_assert(std::is_same::value, ""); -#endif - return static_cast(((' ' << 24) | ('+' << 16) | ('-' << 8)) >> (s * 8)); -} - -template FMT_CONSTEXPR auto count_digits_fallback(T n) -> int { - int count = 1; - for (;;) { - // Integer division is slow so do it for a group of four digits instead - // of for every digit. The idea comes from the talk by Alexandrescu - // "Three Optimization Tips for C++". See speed-test for a comparison. - if (n < 10) return count; - if (n < 100) return count + 1; - if (n < 1000) return count + 2; - if (n < 10000) return count + 3; - n /= 10000u; - count += 4; - } -} -#if FMT_USE_INT128 -FMT_CONSTEXPR inline auto count_digits(uint128_opt n) -> int { - return count_digits_fallback(n); -} -#endif - -#ifdef FMT_BUILTIN_CLZLL -// It is a separate function rather than a part of count_digits to workaround -// the lack of static constexpr in constexpr functions. -inline auto do_count_digits(uint64_t n) -> int { - // This has comparable performance to the version by Kendall Willets - // (https://github.com/fmtlib/format-benchmark/blob/master/digits10) - // but uses smaller tables. - // Maps bsr(n) to ceil(log10(pow(2, bsr(n) + 1) - 1)). - static constexpr uint8_t bsr2log10[] = { - 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, - 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, - 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, - 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20}; - auto t = bsr2log10[FMT_BUILTIN_CLZLL(n | 1) ^ 63]; - static constexpr const uint64_t zero_or_powers_of_10[] = { - 0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL), - 10000000000000000000ULL}; - return t - (n < zero_or_powers_of_10[t]); -} -#endif - -// Returns the number of decimal digits in n. Leading zeros are not counted -// except for n == 0 in which case count_digits returns 1. -FMT_CONSTEXPR20 inline auto count_digits(uint64_t n) -> int { -#ifdef FMT_BUILTIN_CLZLL - if (!is_constant_evaluated()) return do_count_digits(n); -#endif - return count_digits_fallback(n); -} - -// Counts the number of digits in n. BITS = log2(radix). -template -FMT_CONSTEXPR auto count_digits(UInt n) -> int { -#ifdef FMT_BUILTIN_CLZ - if (!is_constant_evaluated() && num_bits() == 32) - return (FMT_BUILTIN_CLZ(static_cast(n) | 1) ^ 31) / BITS + 1; -#endif - // Lambda avoids unreachable code warnings from NVHPC. - return [](UInt m) { - int num_digits = 0; - do { - ++num_digits; - } while ((m >>= BITS) != 0); - return num_digits; - }(n); -} - -#ifdef FMT_BUILTIN_CLZ -// It is a separate function rather than a part of count_digits to workaround -// the lack of static constexpr in constexpr functions. -FMT_INLINE auto do_count_digits(uint32_t n) -> int { -// An optimization by Kendall Willets from https://bit.ly/3uOIQrB. -// This increments the upper 32 bits (log10(T) - 1) when >= T is added. -# define FMT_INC(T) (((sizeof(#T) - 1ull) << 32) - T) - static constexpr uint64_t table[] = { - FMT_INC(0), FMT_INC(0), FMT_INC(0), // 8 - FMT_INC(10), FMT_INC(10), FMT_INC(10), // 64 - FMT_INC(100), FMT_INC(100), FMT_INC(100), // 512 - FMT_INC(1000), FMT_INC(1000), FMT_INC(1000), // 4096 - FMT_INC(10000), FMT_INC(10000), FMT_INC(10000), // 32k - FMT_INC(100000), FMT_INC(100000), FMT_INC(100000), // 256k - FMT_INC(1000000), FMT_INC(1000000), FMT_INC(1000000), // 2048k - FMT_INC(10000000), FMT_INC(10000000), FMT_INC(10000000), // 16M - FMT_INC(100000000), FMT_INC(100000000), FMT_INC(100000000), // 128M - FMT_INC(1000000000), FMT_INC(1000000000), FMT_INC(1000000000), // 1024M - FMT_INC(1000000000), FMT_INC(1000000000) // 4B - }; - auto inc = table[FMT_BUILTIN_CLZ(n | 1) ^ 31]; - return static_cast((n + inc) >> 32); -} -#endif - -// Optional version of count_digits for better performance on 32-bit platforms. -FMT_CONSTEXPR20 inline auto count_digits(uint32_t n) -> int { -#ifdef FMT_BUILTIN_CLZ - if (!is_constant_evaluated()) { - return do_count_digits(n); - } -#endif - return count_digits_fallback(n); -} - -template constexpr auto digits10() noexcept -> int { - return std::numeric_limits::digits10; -} -template <> constexpr auto digits10() noexcept -> int { return 38; } -template <> constexpr auto digits10() noexcept -> int { return 38; } - -template struct thousands_sep_result { - std::string grouping; - Char thousands_sep; -}; - -template -FMT_API auto thousands_sep_impl(locale_ref loc) -> thousands_sep_result; -template -inline auto thousands_sep(locale_ref loc) -> thousands_sep_result { - auto result = thousands_sep_impl(loc); - return {result.grouping, Char(result.thousands_sep)}; -} -template <> -inline auto thousands_sep(locale_ref loc) -> thousands_sep_result { - return thousands_sep_impl(loc); -} - -template -FMT_API auto decimal_point_impl(locale_ref loc) -> Char; -template inline auto decimal_point(locale_ref loc) -> Char { - return Char(decimal_point_impl(loc)); -} -template <> inline auto decimal_point(locale_ref loc) -> wchar_t { - return decimal_point_impl(loc); -} - -// Compares two characters for equality. -template auto equal2(const Char* lhs, const char* rhs) -> bool { - return lhs[0] == Char(rhs[0]) && lhs[1] == Char(rhs[1]); -} -inline auto equal2(const char* lhs, const char* rhs) -> bool { - return memcmp(lhs, rhs, 2) == 0; -} - -// Copies two characters from src to dst. -template -FMT_CONSTEXPR20 FMT_INLINE void copy2(Char* dst, const char* src) { - if (!is_constant_evaluated() && sizeof(Char) == sizeof(char)) { - memcpy(dst, src, 2); - return; - } - *dst++ = static_cast(*src++); - *dst = static_cast(*src); -} - -template struct format_decimal_result { - Iterator begin; - Iterator end; -}; - -// Formats a decimal unsigned integer value writing into out pointing to a -// buffer of specified size. The caller must ensure that the buffer is large -// enough. -template -FMT_CONSTEXPR20 auto format_decimal(Char* out, UInt value, int size) - -> format_decimal_result { - FMT_ASSERT(size >= count_digits(value), "invalid digit count"); - out += size; - Char* end = out; - while (value >= 100) { - // Integer division is slow so do it for a group of two digits instead - // of for every digit. The idea comes from the talk by Alexandrescu - // "Three Optimization Tips for C++". See speed-test for a comparison. - out -= 2; - copy2(out, digits2(static_cast(value % 100))); - value /= 100; - } - if (value < 10) { - *--out = static_cast('0' + value); - return {out, end}; - } - out -= 2; - copy2(out, digits2(static_cast(value))); - return {out, end}; -} - -template >::value)> -FMT_CONSTEXPR inline auto format_decimal(Iterator out, UInt value, int size) - -> format_decimal_result { - // Buffer is large enough to hold all digits (digits10 + 1). - Char buffer[digits10() + 1] = {}; - auto end = format_decimal(buffer, value, size).end; - return {out, detail::copy_noinline(buffer, end, out)}; -} - -template -FMT_CONSTEXPR auto format_uint(Char* buffer, UInt value, int num_digits, - bool upper = false) -> Char* { - buffer += num_digits; - Char* end = buffer; - do { - const char* digits = upper ? "0123456789ABCDEF" : "0123456789abcdef"; - unsigned digit = static_cast(value & ((1 << BASE_BITS) - 1)); - *--buffer = static_cast(BASE_BITS < 4 ? static_cast('0' + digit) - : digits[digit]); - } while ((value >>= BASE_BITS) != 0); - return end; -} - -template -FMT_CONSTEXPR inline auto format_uint(It out, UInt value, int num_digits, - bool upper = false) -> It { - if (auto ptr = to_pointer(out, to_unsigned(num_digits))) { - format_uint(ptr, value, num_digits, upper); - return out; - } - // Buffer should be large enough to hold all digits (digits / BASE_BITS + 1). - char buffer[num_bits() / BASE_BITS + 1] = {}; - format_uint(buffer, value, num_digits, upper); - return detail::copy_noinline(buffer, buffer + num_digits, out); -} - -// A converter from UTF-8 to UTF-16. -class utf8_to_utf16 { - private: - basic_memory_buffer buffer_; - - public: - FMT_API explicit utf8_to_utf16(string_view s); - operator basic_string_view() const { return {&buffer_[0], size()}; } - auto size() const -> size_t { return buffer_.size() - 1; } - auto c_str() const -> const wchar_t* { return &buffer_[0]; } - auto str() const -> std::wstring { return {&buffer_[0], size()}; } -}; - -enum class to_utf8_error_policy { abort, replace }; - -// A converter from UTF-16/UTF-32 (host endian) to UTF-8. -template class to_utf8 { - private: - Buffer buffer_; - - public: - to_utf8() {} - explicit to_utf8(basic_string_view s, - to_utf8_error_policy policy = to_utf8_error_policy::abort) { - static_assert(sizeof(WChar) == 2 || sizeof(WChar) == 4, - "Expect utf16 or utf32"); - if (!convert(s, policy)) - FMT_THROW(std::runtime_error(sizeof(WChar) == 2 ? "invalid utf16" - : "invalid utf32")); - } - operator string_view() const { return string_view(&buffer_[0], size()); } - auto size() const -> size_t { return buffer_.size() - 1; } - auto c_str() const -> const char* { return &buffer_[0]; } - auto str() const -> std::string { return std::string(&buffer_[0], size()); } - - // Performs conversion returning a bool instead of throwing exception on - // conversion error. This method may still throw in case of memory allocation - // error. - auto convert(basic_string_view s, - to_utf8_error_policy policy = to_utf8_error_policy::abort) - -> bool { - if (!convert(buffer_, s, policy)) return false; - buffer_.push_back(0); - return true; - } - static auto convert(Buffer& buf, basic_string_view s, - to_utf8_error_policy policy = to_utf8_error_policy::abort) - -> bool { - for (auto p = s.begin(); p != s.end(); ++p) { - uint32_t c = static_cast(*p); - if (sizeof(WChar) == 2 && c >= 0xd800 && c <= 0xdfff) { - // Handle a surrogate pair. - ++p; - if (p == s.end() || (c & 0xfc00) != 0xd800 || (*p & 0xfc00) != 0xdc00) { - if (policy == to_utf8_error_policy::abort) return false; - buf.append(string_view("\xEF\xBF\xBD")); - --p; - } else { - c = (c << 10) + static_cast(*p) - 0x35fdc00; - } - } else if (c < 0x80) { - buf.push_back(static_cast(c)); - } else if (c < 0x800) { - buf.push_back(static_cast(0xc0 | (c >> 6))); - buf.push_back(static_cast(0x80 | (c & 0x3f))); - } else if ((c >= 0x800 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xffff)) { - buf.push_back(static_cast(0xe0 | (c >> 12))); - buf.push_back(static_cast(0x80 | ((c & 0xfff) >> 6))); - buf.push_back(static_cast(0x80 | (c & 0x3f))); - } else if (c >= 0x10000 && c <= 0x10ffff) { - buf.push_back(static_cast(0xf0 | (c >> 18))); - buf.push_back(static_cast(0x80 | ((c & 0x3ffff) >> 12))); - buf.push_back(static_cast(0x80 | ((c & 0xfff) >> 6))); - buf.push_back(static_cast(0x80 | (c & 0x3f))); - } else { - return false; - } - } - return true; - } -}; - -// Computes 128-bit result of multiplication of two 64-bit unsigned integers. -inline auto umul128(uint64_t x, uint64_t y) noexcept -> uint128_fallback { -#if FMT_USE_INT128 - auto p = static_cast(x) * static_cast(y); - return {static_cast(p >> 64), static_cast(p)}; -#elif defined(_MSC_VER) && defined(_M_X64) - auto hi = uint64_t(); - auto lo = _umul128(x, y, &hi); - return {hi, lo}; -#else - const uint64_t mask = static_cast(max_value()); - - uint64_t a = x >> 32; - uint64_t b = x & mask; - uint64_t c = y >> 32; - uint64_t d = y & mask; - - uint64_t ac = a * c; - uint64_t bc = b * c; - uint64_t ad = a * d; - uint64_t bd = b * d; - - uint64_t intermediate = (bd >> 32) + (ad & mask) + (bc & mask); - - return {ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32), - (intermediate << 32) + (bd & mask)}; -#endif -} - -namespace dragonbox { -// Computes floor(log10(pow(2, e))) for e in [-2620, 2620] using the method from -// https://fmt.dev/papers/Dragonbox.pdf#page=28, section 6.1. -inline auto floor_log10_pow2(int e) noexcept -> int { - FMT_ASSERT(e <= 2620 && e >= -2620, "too large exponent"); - static_assert((-1 >> 1) == -1, "right shift is not arithmetic"); - return (e * 315653) >> 20; -} - -inline auto floor_log2_pow10(int e) noexcept -> int { - FMT_ASSERT(e <= 1233 && e >= -1233, "too large exponent"); - return (e * 1741647) >> 19; -} - -// Computes upper 64 bits of multiplication of two 64-bit unsigned integers. -inline auto umul128_upper64(uint64_t x, uint64_t y) noexcept -> uint64_t { -#if FMT_USE_INT128 - auto p = static_cast(x) * static_cast(y); - return static_cast(p >> 64); -#elif defined(_MSC_VER) && defined(_M_X64) - return __umulh(x, y); -#else - return umul128(x, y).high(); -#endif -} - -// Computes upper 128 bits of multiplication of a 64-bit unsigned integer and a -// 128-bit unsigned integer. -inline auto umul192_upper128(uint64_t x, uint128_fallback y) noexcept - -> uint128_fallback { - uint128_fallback r = umul128(x, y.high()); - r += umul128_upper64(x, y.low()); - return r; -} - -FMT_API auto get_cached_power(int k) noexcept -> uint128_fallback; - -// Type-specific information that Dragonbox uses. -template struct float_info; - -template <> struct float_info { - using carrier_uint = uint32_t; - static const int exponent_bits = 8; - static const int kappa = 1; - static const int big_divisor = 100; - static const int small_divisor = 10; - static const int min_k = -31; - static const int max_k = 46; - static const int shorter_interval_tie_lower_threshold = -35; - static const int shorter_interval_tie_upper_threshold = -35; -}; - -template <> struct float_info { - using carrier_uint = uint64_t; - static const int exponent_bits = 11; - static const int kappa = 2; - static const int big_divisor = 1000; - static const int small_divisor = 100; - static const int min_k = -292; - static const int max_k = 341; - static const int shorter_interval_tie_lower_threshold = -77; - static const int shorter_interval_tie_upper_threshold = -77; -}; - -// An 80- or 128-bit floating point number. -template -struct float_info::digits == 64 || - std::numeric_limits::digits == 113 || - is_float128::value>> { - using carrier_uint = detail::uint128_t; - static const int exponent_bits = 15; -}; - -// A double-double floating point number. -template -struct float_info::value>> { - using carrier_uint = detail::uint128_t; -}; - -template struct decimal_fp { - using significand_type = typename float_info::carrier_uint; - significand_type significand; - int exponent; -}; - -template FMT_API auto to_decimal(T x) noexcept -> decimal_fp; -} // namespace dragonbox - -// Returns true iff Float has the implicit bit which is not stored. -template constexpr auto has_implicit_bit() -> bool { - // An 80-bit FP number has a 64-bit significand an no implicit bit. - return std::numeric_limits::digits != 64; -} - -// Returns the number of significand bits stored in Float. The implicit bit is -// not counted since it is not stored. -template constexpr auto num_significand_bits() -> int { - // std::numeric_limits may not support __float128. - return is_float128() ? 112 - : (std::numeric_limits::digits - - (has_implicit_bit() ? 1 : 0)); -} - -template -constexpr auto exponent_mask() -> - typename dragonbox::float_info::carrier_uint { - using float_uint = typename dragonbox::float_info::carrier_uint; - return ((float_uint(1) << dragonbox::float_info::exponent_bits) - 1) - << num_significand_bits(); -} -template constexpr auto exponent_bias() -> int { - // std::numeric_limits may not support __float128. - return is_float128() ? 16383 - : std::numeric_limits::max_exponent - 1; -} - -// Writes the exponent exp in the form "[+-]d{2,3}" to buffer. -template -FMT_CONSTEXPR auto write_exponent(int exp, It it) -> It { - FMT_ASSERT(-10000 < exp && exp < 10000, "exponent out of range"); - if (exp < 0) { - *it++ = static_cast('-'); - exp = -exp; - } else { - *it++ = static_cast('+'); - } - if (exp >= 100) { - const char* top = digits2(to_unsigned(exp / 100)); - if (exp >= 1000) *it++ = static_cast(top[0]); - *it++ = static_cast(top[1]); - exp %= 100; - } - const char* d = digits2(to_unsigned(exp)); - *it++ = static_cast(d[0]); - *it++ = static_cast(d[1]); - return it; -} - -// A floating-point number f * pow(2, e) where F is an unsigned type. -template struct basic_fp { - F f; - int e; - - static constexpr const int num_significand_bits = - static_cast(sizeof(F) * num_bits()); - - constexpr basic_fp() : f(0), e(0) {} - constexpr basic_fp(uint64_t f_val, int e_val) : f(f_val), e(e_val) {} - - // Constructs fp from an IEEE754 floating-point number. - template FMT_CONSTEXPR basic_fp(Float n) { assign(n); } - - // Assigns n to this and return true iff predecessor is closer than successor. - template ::value)> - FMT_CONSTEXPR auto assign(Float n) -> bool { - static_assert(std::numeric_limits::digits <= 113, "unsupported FP"); - // Assume Float is in the format [sign][exponent][significand]. - using carrier_uint = typename dragonbox::float_info::carrier_uint; - const auto num_float_significand_bits = - detail::num_significand_bits(); - const auto implicit_bit = carrier_uint(1) << num_float_significand_bits; - const auto significand_mask = implicit_bit - 1; - auto u = bit_cast(n); - f = static_cast(u & significand_mask); - auto biased_e = static_cast((u & exponent_mask()) >> - num_float_significand_bits); - // The predecessor is closer if n is a normalized power of 2 (f == 0) - // other than the smallest normalized number (biased_e > 1). - auto is_predecessor_closer = f == 0 && biased_e > 1; - if (biased_e == 0) - biased_e = 1; // Subnormals use biased exponent 1 (min exponent). - else if (has_implicit_bit()) - f += static_cast(implicit_bit); - e = biased_e - exponent_bias() - num_float_significand_bits; - if (!has_implicit_bit()) ++e; - return is_predecessor_closer; - } - - template ::value)> - FMT_CONSTEXPR auto assign(Float n) -> bool { - static_assert(std::numeric_limits::is_iec559, "unsupported FP"); - return assign(static_cast(n)); - } -}; - -using fp = basic_fp; - -// Normalizes the value converted from double and multiplied by (1 << SHIFT). -template -FMT_CONSTEXPR auto normalize(basic_fp value) -> basic_fp { - // Handle subnormals. - const auto implicit_bit = F(1) << num_significand_bits(); - const auto shifted_implicit_bit = implicit_bit << SHIFT; - while ((value.f & shifted_implicit_bit) == 0) { - value.f <<= 1; - --value.e; - } - // Subtract 1 to account for hidden bit. - const auto offset = basic_fp::num_significand_bits - - num_significand_bits() - SHIFT - 1; - value.f <<= offset; - value.e -= offset; - return value; -} - -// Computes lhs * rhs / pow(2, 64) rounded to nearest with half-up tie breaking. -FMT_CONSTEXPR inline auto multiply(uint64_t lhs, uint64_t rhs) -> uint64_t { -#if FMT_USE_INT128 - auto product = static_cast<__uint128_t>(lhs) * rhs; - auto f = static_cast(product >> 64); - return (static_cast(product) & (1ULL << 63)) != 0 ? f + 1 : f; -#else - // Multiply 32-bit parts of significands. - uint64_t mask = (1ULL << 32) - 1; - uint64_t a = lhs >> 32, b = lhs & mask; - uint64_t c = rhs >> 32, d = rhs & mask; - uint64_t ac = a * c, bc = b * c, ad = a * d, bd = b * d; - // Compute mid 64-bit of result and round. - uint64_t mid = (bd >> 32) + (ad & mask) + (bc & mask) + (1U << 31); - return ac + (ad >> 32) + (bc >> 32) + (mid >> 32); -#endif -} - -FMT_CONSTEXPR inline auto operator*(fp x, fp y) -> fp { - return {multiply(x.f, y.f), x.e + y.e + 64}; -} - -template () == num_bits()> -using convert_float_result = - conditional_t::value || doublish, double, T>; - -template -constexpr auto convert_float(T value) -> convert_float_result { - return static_cast>(value); -} - -template -FMT_NOINLINE FMT_CONSTEXPR auto fill(OutputIt it, size_t n, const fill_t& fill) - -> OutputIt { - auto fill_size = fill.size(); - if (fill_size == 1) return detail::fill_n(it, n, fill.template get()); - if (const Char* data = fill.template data()) { - for (size_t i = 0; i < n; ++i) it = copy(data, data + fill_size, it); - } - return it; -} - -// Writes the output of f, padded according to format specifications in specs. -// size: output size in code units. -// width: output display width in (terminal) column positions. -template -FMT_CONSTEXPR auto write_padded(OutputIt out, const format_specs& specs, - size_t size, size_t width, F&& f) -> OutputIt { - static_assert(align == align::left || align == align::right, ""); - unsigned spec_width = to_unsigned(specs.width); - size_t padding = spec_width > width ? spec_width - width : 0; - // Shifts are encoded as string literals because static constexpr is not - // supported in constexpr functions. - auto* shifts = align == align::left ? "\x1f\x1f\x00\x01" : "\x00\x1f\x00\x01"; - size_t left_padding = padding >> shifts[specs.align]; - size_t right_padding = padding - left_padding; - auto it = reserve(out, size + padding * specs.fill.size()); - if (left_padding != 0) it = fill(it, left_padding, specs.fill); - it = f(it); - if (right_padding != 0) it = fill(it, right_padding, specs.fill); - return base_iterator(out, it); -} - -template -constexpr auto write_padded(OutputIt out, const format_specs& specs, - size_t size, F&& f) -> OutputIt { - return write_padded(out, specs, size, size, f); -} - -template -FMT_CONSTEXPR auto write_bytes(OutputIt out, string_view bytes, - const format_specs& specs = {}) -> OutputIt { - return write_padded( - out, specs, bytes.size(), [bytes](reserve_iterator it) { - const char* data = bytes.data(); - return copy(data, data + bytes.size(), it); - }); -} - -template -auto write_ptr(OutputIt out, UIntPtr value, const format_specs* specs) - -> OutputIt { - int num_digits = count_digits<4>(value); - auto size = to_unsigned(num_digits) + size_t(2); - auto write = [=](reserve_iterator it) { - *it++ = static_cast('0'); - *it++ = static_cast('x'); - return format_uint<4, Char>(it, value, num_digits); - }; - return specs ? write_padded(out, *specs, size, write) - : base_iterator(out, write(reserve(out, size))); -} - -// Returns true iff the code point cp is printable. -FMT_API auto is_printable(uint32_t cp) -> bool; - -inline auto needs_escape(uint32_t cp) -> bool { - return cp < 0x20 || cp == 0x7f || cp == '"' || cp == '\\' || - !is_printable(cp); -} - -template struct find_escape_result { - const Char* begin; - const Char* end; - uint32_t cp; -}; - -template -auto find_escape(const Char* begin, const Char* end) - -> find_escape_result { - for (; begin != end; ++begin) { - uint32_t cp = static_cast>(*begin); - if (const_check(sizeof(Char) == 1) && cp >= 0x80) continue; - if (needs_escape(cp)) return {begin, begin + 1, cp}; - } - return {begin, nullptr, 0}; -} - -inline auto find_escape(const char* begin, const char* end) - -> find_escape_result { - if (!use_utf8()) return find_escape(begin, end); - auto result = find_escape_result{end, nullptr, 0}; - for_each_codepoint(string_view(begin, to_unsigned(end - begin)), - [&](uint32_t cp, string_view sv) { - if (needs_escape(cp)) { - result = {sv.begin(), sv.end(), cp}; - return false; - } - return true; - }); - return result; -} - -#define FMT_STRING_IMPL(s, base, explicit) \ - [] { \ - /* Use the hidden visibility as a workaround for a GCC bug (#1973). */ \ - /* Use a macro-like name to avoid shadowing warnings. */ \ - struct FMT_VISIBILITY("hidden") FMT_COMPILE_STRING : base { \ - using char_type FMT_MAYBE_UNUSED = fmt::remove_cvref_t; \ - FMT_MAYBE_UNUSED FMT_CONSTEXPR explicit \ - operator fmt::basic_string_view() const { \ - return fmt::detail_exported::compile_string_to_view(s); \ - } \ - }; \ - return FMT_COMPILE_STRING(); \ - }() - -/** - * Constructs a compile-time format string from a string literal `s`. - * - * **Example**: - * - * // A compile-time error because 'd' is an invalid specifier for strings. - * std::string s = fmt::format(FMT_STRING("{:d}"), "foo"); - */ -#define FMT_STRING(s) FMT_STRING_IMPL(s, fmt::detail::compile_string, ) - -template -auto write_codepoint(OutputIt out, char prefix, uint32_t cp) -> OutputIt { - *out++ = static_cast('\\'); - *out++ = static_cast(prefix); - Char buf[width]; - fill_n(buf, width, static_cast('0')); - format_uint<4>(buf, cp, width); - return copy(buf, buf + width, out); -} - -template -auto write_escaped_cp(OutputIt out, const find_escape_result& escape) - -> OutputIt { - auto c = static_cast(escape.cp); - switch (escape.cp) { - case '\n': - *out++ = static_cast('\\'); - c = static_cast('n'); - break; - case '\r': - *out++ = static_cast('\\'); - c = static_cast('r'); - break; - case '\t': - *out++ = static_cast('\\'); - c = static_cast('t'); - break; - case '"': - FMT_FALLTHROUGH; - case '\'': - FMT_FALLTHROUGH; - case '\\': - *out++ = static_cast('\\'); - break; - default: - if (escape.cp < 0x100) return write_codepoint<2, Char>(out, 'x', escape.cp); - if (escape.cp < 0x10000) - return write_codepoint<4, Char>(out, 'u', escape.cp); - if (escape.cp < 0x110000) - return write_codepoint<8, Char>(out, 'U', escape.cp); - for (Char escape_char : basic_string_view( - escape.begin, to_unsigned(escape.end - escape.begin))) { - out = write_codepoint<2, Char>(out, 'x', - static_cast(escape_char) & 0xFF); - } - return out; - } - *out++ = c; - return out; -} - -template -auto write_escaped_string(OutputIt out, basic_string_view str) - -> OutputIt { - *out++ = static_cast('"'); - auto begin = str.begin(), end = str.end(); - do { - auto escape = find_escape(begin, end); - out = copy(begin, escape.begin, out); - begin = escape.end; - if (!begin) break; - out = write_escaped_cp(out, escape); - } while (begin != end); - *out++ = static_cast('"'); - return out; -} - -template -auto write_escaped_char(OutputIt out, Char v) -> OutputIt { - Char v_array[1] = {v}; - *out++ = static_cast('\''); - if ((needs_escape(static_cast(v)) && v != static_cast('"')) || - v == static_cast('\'')) { - out = write_escaped_cp(out, - find_escape_result{v_array, v_array + 1, - static_cast(v)}); - } else { - *out++ = v; - } - *out++ = static_cast('\''); - return out; -} - -template -FMT_CONSTEXPR auto write_char(OutputIt out, Char value, - const format_specs& specs) -> OutputIt { - bool is_debug = specs.type == presentation_type::debug; - return write_padded(out, specs, 1, [=](reserve_iterator it) { - if (is_debug) return write_escaped_char(it, value); - *it++ = value; - return it; - }); -} -template -FMT_CONSTEXPR auto write(OutputIt out, Char value, const format_specs& specs, - locale_ref loc = {}) -> OutputIt { - // char is formatted as unsigned char for consistency across platforms. - using unsigned_type = - conditional_t::value, unsigned char, unsigned>; - return check_char_specs(specs) - ? write_char(out, value, specs) - : write(out, static_cast(value), specs, loc); -} - -// Data for write_int that doesn't depend on output iterator type. It is used to -// avoid template code bloat. -template struct write_int_data { - size_t size; - size_t padding; - - FMT_CONSTEXPR write_int_data(int num_digits, unsigned prefix, - const format_specs& specs) - : size((prefix >> 24) + to_unsigned(num_digits)), padding(0) { - if (specs.align == align::numeric) { - auto width = to_unsigned(specs.width); - if (width > size) { - padding = width - size; - size = width; - } - } else if (specs.precision > num_digits) { - size = (prefix >> 24) + to_unsigned(specs.precision); - padding = to_unsigned(specs.precision - num_digits); - } - } -}; - -// Writes an integer in the format -// -// where are written by write_digits(it). -// prefix contains chars in three lower bytes and the size in the fourth byte. -template -FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, int num_digits, - unsigned prefix, - const format_specs& specs, - W write_digits) -> OutputIt { - // Slightly faster check for specs.width == 0 && specs.precision == -1. - if ((specs.width | (specs.precision + 1)) == 0) { - auto it = reserve(out, to_unsigned(num_digits) + (prefix >> 24)); - if (prefix != 0) { - for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) - *it++ = static_cast(p & 0xff); - } - return base_iterator(out, write_digits(it)); - } - auto data = write_int_data(num_digits, prefix, specs); - return write_padded( - out, specs, data.size, [=](reserve_iterator it) { - for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) - *it++ = static_cast(p & 0xff); - it = detail::fill_n(it, data.padding, static_cast('0')); - return write_digits(it); - }); -} - -template class digit_grouping { - private: - std::string grouping_; - std::basic_string thousands_sep_; - - struct next_state { - std::string::const_iterator group; - int pos; - }; - auto initial_state() const -> next_state { return {grouping_.begin(), 0}; } - - // Returns the next digit group separator position. - auto next(next_state& state) const -> int { - if (thousands_sep_.empty()) return max_value(); - if (state.group == grouping_.end()) return state.pos += grouping_.back(); - if (*state.group <= 0 || *state.group == max_value()) - return max_value(); - state.pos += *state.group++; - return state.pos; - } - - public: - explicit digit_grouping(locale_ref loc, bool localized = true) { - if (!localized) return; - auto sep = thousands_sep(loc); - grouping_ = sep.grouping; - if (sep.thousands_sep) thousands_sep_.assign(1, sep.thousands_sep); - } - digit_grouping(std::string grouping, std::basic_string sep) - : grouping_(std::move(grouping)), thousands_sep_(std::move(sep)) {} - - auto has_separator() const -> bool { return !thousands_sep_.empty(); } - - auto count_separators(int num_digits) const -> int { - int count = 0; - auto state = initial_state(); - while (num_digits > next(state)) ++count; - return count; - } - - // Applies grouping to digits and write the output to out. - template - auto apply(Out out, basic_string_view digits) const -> Out { - auto num_digits = static_cast(digits.size()); - auto separators = basic_memory_buffer(); - separators.push_back(0); - auto state = initial_state(); - while (int i = next(state)) { - if (i >= num_digits) break; - separators.push_back(i); - } - for (int i = 0, sep_index = static_cast(separators.size() - 1); - i < num_digits; ++i) { - if (num_digits - i == separators[sep_index]) { - out = copy(thousands_sep_.data(), - thousands_sep_.data() + thousands_sep_.size(), out); - --sep_index; - } - *out++ = static_cast(digits[to_unsigned(i)]); - } - return out; - } -}; - -FMT_CONSTEXPR inline void prefix_append(unsigned& prefix, unsigned value) { - prefix |= prefix != 0 ? value << 8 : value; - prefix += (1u + (value > 0xff ? 1 : 0)) << 24; -} - -// Writes a decimal integer with digit grouping. -template -auto write_int(OutputIt out, UInt value, unsigned prefix, - const format_specs& specs, const digit_grouping& grouping) - -> OutputIt { - static_assert(std::is_same, UInt>::value, ""); - int num_digits = 0; - auto buffer = memory_buffer(); - switch (specs.type) { - default: - FMT_ASSERT(false, ""); - FMT_FALLTHROUGH; - case presentation_type::none: - case presentation_type::dec: - num_digits = count_digits(value); - format_decimal(appender(buffer), value, num_digits); - break; - case presentation_type::hex: - if (specs.alt) - prefix_append(prefix, unsigned(specs.upper ? 'X' : 'x') << 8 | '0'); - num_digits = count_digits<4>(value); - format_uint<4, char>(appender(buffer), value, num_digits, specs.upper); - break; - case presentation_type::oct: - num_digits = count_digits<3>(value); - // Octal prefix '0' is counted as a digit, so only add it if precision - // is not greater than the number of digits. - if (specs.alt && specs.precision <= num_digits && value != 0) - prefix_append(prefix, '0'); - format_uint<3, char>(appender(buffer), value, num_digits); - break; - case presentation_type::bin: - if (specs.alt) - prefix_append(prefix, unsigned(specs.upper ? 'B' : 'b') << 8 | '0'); - num_digits = count_digits<1>(value); - format_uint<1, char>(appender(buffer), value, num_digits); - break; - case presentation_type::chr: - return write_char(out, static_cast(value), specs); - } - - unsigned size = (prefix != 0 ? prefix >> 24 : 0) + to_unsigned(num_digits) + - to_unsigned(grouping.count_separators(num_digits)); - return write_padded( - out, specs, size, size, [&](reserve_iterator it) { - for (unsigned p = prefix & 0xffffff; p != 0; p >>= 8) - *it++ = static_cast(p & 0xff); - return grouping.apply(it, string_view(buffer.data(), buffer.size())); - }); -} - -// Writes a localized value. -FMT_API auto write_loc(appender out, loc_value value, const format_specs& specs, - locale_ref loc) -> bool; -template -inline auto write_loc(OutputIt, loc_value, const format_specs&, locale_ref) - -> bool { - return false; -} - -template struct write_int_arg { - UInt abs_value; - unsigned prefix; -}; - -template -FMT_CONSTEXPR auto make_write_int_arg(T value, sign_t sign) - -> write_int_arg> { - auto prefix = 0u; - auto abs_value = static_cast>(value); - if (is_negative(value)) { - prefix = 0x01000000 | '-'; - abs_value = 0 - abs_value; - } else { - constexpr const unsigned prefixes[4] = {0, 0, 0x1000000u | '+', - 0x1000000u | ' '}; - prefix = prefixes[sign]; - } - return {abs_value, prefix}; -} - -template struct loc_writer { - basic_appender out; - const format_specs& specs; - std::basic_string sep; - std::string grouping; - std::basic_string decimal_point; - - template ::value)> - auto operator()(T value) -> bool { - auto arg = make_write_int_arg(value, specs.sign); - write_int(out, static_cast>(arg.abs_value), arg.prefix, - specs, digit_grouping(grouping, sep)); - return true; - } - - template ::value)> - auto operator()(T) -> bool { - return false; - } -}; - -template -FMT_CONSTEXPR FMT_INLINE auto write_int(OutputIt out, write_int_arg arg, - const format_specs& specs, locale_ref) - -> OutputIt { - static_assert(std::is_same>::value, ""); - auto abs_value = arg.abs_value; - auto prefix = arg.prefix; - switch (specs.type) { - default: - FMT_ASSERT(false, ""); - FMT_FALLTHROUGH; - case presentation_type::none: - case presentation_type::dec: { - int num_digits = count_digits(abs_value); - return write_int( - out, num_digits, prefix, specs, [=](reserve_iterator it) { - return format_decimal(it, abs_value, num_digits).end; - }); - } - case presentation_type::hex: { - if (specs.alt) - prefix_append(prefix, unsigned(specs.upper ? 'X' : 'x') << 8 | '0'); - int num_digits = count_digits<4>(abs_value); - return write_int( - out, num_digits, prefix, specs, [=](reserve_iterator it) { - return format_uint<4, Char>(it, abs_value, num_digits, specs.upper); - }); - } - case presentation_type::oct: { - int num_digits = count_digits<3>(abs_value); - // Octal prefix '0' is counted as a digit, so only add it if precision - // is not greater than the number of digits. - if (specs.alt && specs.precision <= num_digits && abs_value != 0) - prefix_append(prefix, '0'); - return write_int( - out, num_digits, prefix, specs, [=](reserve_iterator it) { - return format_uint<3, Char>(it, abs_value, num_digits); - }); - } - case presentation_type::bin: { - if (specs.alt) - prefix_append(prefix, unsigned(specs.upper ? 'B' : 'b') << 8 | '0'); - int num_digits = count_digits<1>(abs_value); - return write_int( - out, num_digits, prefix, specs, [=](reserve_iterator it) { - return format_uint<1, Char>(it, abs_value, num_digits); - }); - } - case presentation_type::chr: - return write_char(out, static_cast(abs_value), specs); - } -} -template -FMT_CONSTEXPR FMT_NOINLINE auto write_int_noinline(OutputIt out, - write_int_arg arg, - const format_specs& specs, - locale_ref loc) -> OutputIt { - return write_int(out, arg, specs, loc); -} -template ::value && - !std::is_same::value && - !std::is_same::value)> -FMT_CONSTEXPR FMT_INLINE auto write(basic_appender out, T value, - const format_specs& specs, locale_ref loc) - -> basic_appender { - if (specs.localized && write_loc(out, value, specs, loc)) return out; - return write_int_noinline(out, make_write_int_arg(value, specs.sign), - specs, loc); -} -// An inlined version of write used in format string compilation. -template ::value && - !std::is_same::value && - !std::is_same::value && - !std::is_same>::value)> -FMT_CONSTEXPR FMT_INLINE auto write(OutputIt out, T value, - const format_specs& specs, locale_ref loc) - -> OutputIt { - if (specs.localized && write_loc(out, value, specs, loc)) return out; - return write_int(out, make_write_int_arg(value, specs.sign), specs, - loc); -} - -// An output iterator that counts the number of objects written to it and -// discards them. -class counting_iterator { - private: - size_t count_; - - public: - using iterator_category = std::output_iterator_tag; - using difference_type = std::ptrdiff_t; - using pointer = void; - using reference = void; - FMT_UNCHECKED_ITERATOR(counting_iterator); - - struct value_type { - template FMT_CONSTEXPR void operator=(const T&) {} - }; - - FMT_CONSTEXPR counting_iterator() : count_(0) {} - - FMT_CONSTEXPR auto count() const -> size_t { return count_; } - - FMT_CONSTEXPR auto operator++() -> counting_iterator& { - ++count_; - return *this; - } - FMT_CONSTEXPR auto operator++(int) -> counting_iterator { - auto it = *this; - ++*this; - return it; - } - - FMT_CONSTEXPR friend auto operator+(counting_iterator it, difference_type n) - -> counting_iterator { - it.count_ += static_cast(n); - return it; - } - - FMT_CONSTEXPR auto operator*() const -> value_type { return {}; } -}; - -template -FMT_CONSTEXPR auto write(OutputIt out, basic_string_view s, - const format_specs& specs) -> OutputIt { - auto data = s.data(); - auto size = s.size(); - if (specs.precision >= 0 && to_unsigned(specs.precision) < size) - size = code_point_index(s, to_unsigned(specs.precision)); - bool is_debug = specs.type == presentation_type::debug; - size_t width = 0; - - if (is_debug) size = write_escaped_string(counting_iterator{}, s).count(); - - if (specs.width != 0) { - if (is_debug) - width = size; - else - width = compute_width(basic_string_view(data, size)); - } - return write_padded(out, specs, size, width, - [=](reserve_iterator it) { - if (is_debug) return write_escaped_string(it, s); - return copy(data, data + size, it); - }); -} -template -FMT_CONSTEXPR auto write(OutputIt out, - basic_string_view> s, - const format_specs& specs, locale_ref) -> OutputIt { - return write(out, s, specs); -} -template -FMT_CONSTEXPR auto write(OutputIt out, const Char* s, const format_specs& specs, - locale_ref) -> OutputIt { - if (specs.type == presentation_type::pointer) - return write_ptr(out, bit_cast(s), &specs); - if (!s) report_error("string pointer is null"); - return write(out, basic_string_view(s), specs, {}); -} - -template ::value && - !std::is_same::value && - !std::is_same::value)> -FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt { - auto abs_value = static_cast>(value); - bool negative = is_negative(value); - // Don't do -abs_value since it trips unsigned-integer-overflow sanitizer. - if (negative) abs_value = ~abs_value + 1; - int num_digits = count_digits(abs_value); - auto size = (negative ? 1 : 0) + static_cast(num_digits); - if (auto ptr = to_pointer(out, size)) { - if (negative) *ptr++ = static_cast('-'); - format_decimal(ptr, abs_value, num_digits); - return out; - } - if (negative) *out++ = static_cast('-'); - return format_decimal(out, abs_value, num_digits).end; -} - -// DEPRECATED! -template -FMT_CONSTEXPR auto parse_align(const Char* begin, const Char* end, - format_specs& specs) -> const Char* { - FMT_ASSERT(begin != end, ""); - auto align = align::none; - auto p = begin + code_point_length(begin); - if (end - p <= 0) p = begin; - for (;;) { - switch (to_ascii(*p)) { - case '<': - align = align::left; - break; - case '>': - align = align::right; - break; - case '^': - align = align::center; - break; - } - if (align != align::none) { - if (p != begin) { - auto c = *begin; - if (c == '}') return begin; - if (c == '{') { - report_error("invalid fill character '{'"); - return begin; - } - specs.fill = basic_string_view(begin, to_unsigned(p - begin)); - begin = p + 1; - } else { - ++begin; - } - break; - } else if (p == begin) { - break; - } - p = begin; - } - specs.align = align; - return begin; -} - -// A floating-point presentation format. -enum class float_format : unsigned char { - general, // General: exponent notation or fixed point based on magnitude. - exp, // Exponent notation with the default precision of 6, e.g. 1.2e-3. - fixed // Fixed point with the default precision of 6, e.g. 0.0012. -}; - -struct float_specs { - int precision; - float_format format : 8; - sign_t sign : 8; - bool locale : 1; - bool binary32 : 1; - bool showpoint : 1; -}; - -// DEPRECATED! -FMT_CONSTEXPR inline auto parse_float_type_spec(const format_specs& specs) - -> float_specs { - auto result = float_specs(); - result.showpoint = specs.alt; - result.locale = specs.localized; - switch (specs.type) { - default: - FMT_FALLTHROUGH; - case presentation_type::none: - result.format = float_format::general; - break; - case presentation_type::exp: - result.format = float_format::exp; - result.showpoint |= specs.precision != 0; - break; - case presentation_type::fixed: - result.format = float_format::fixed; - result.showpoint |= specs.precision != 0; - break; - case presentation_type::general: - result.format = float_format::general; - break; - } - return result; -} - -template -FMT_CONSTEXPR20 auto write_nonfinite(OutputIt out, bool isnan, - format_specs specs, sign_t sign) - -> OutputIt { - auto str = - isnan ? (specs.upper ? "NAN" : "nan") : (specs.upper ? "INF" : "inf"); - constexpr size_t str_size = 3; - auto size = str_size + (sign ? 1 : 0); - // Replace '0'-padding with space for non-finite values. - const bool is_zero_fill = - specs.fill.size() == 1 && specs.fill.template get() == '0'; - if (is_zero_fill) specs.fill = ' '; - return write_padded(out, specs, size, - [=](reserve_iterator it) { - if (sign) *it++ = detail::sign(sign); - return copy(str, str + str_size, it); - }); -} - -// A decimal floating-point number significand * pow(10, exp). -struct big_decimal_fp { - const char* significand; - int significand_size; - int exponent; -}; - -constexpr auto get_significand_size(const big_decimal_fp& f) -> int { - return f.significand_size; -} -template -inline auto get_significand_size(const dragonbox::decimal_fp& f) -> int { - return count_digits(f.significand); -} - -template -constexpr auto write_significand(OutputIt out, const char* significand, - int significand_size) -> OutputIt { - return copy(significand, significand + significand_size, out); -} -template -inline auto write_significand(OutputIt out, UInt significand, - int significand_size) -> OutputIt { - return format_decimal(out, significand, significand_size).end; -} -template -FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, - int significand_size, int exponent, - const Grouping& grouping) -> OutputIt { - if (!grouping.has_separator()) { - out = write_significand(out, significand, significand_size); - return detail::fill_n(out, exponent, static_cast('0')); - } - auto buffer = memory_buffer(); - write_significand(appender(buffer), significand, significand_size); - detail::fill_n(appender(buffer), exponent, '0'); - return grouping.apply(out, string_view(buffer.data(), buffer.size())); -} - -template ::value)> -inline auto write_significand(Char* out, UInt significand, int significand_size, - int integral_size, Char decimal_point) -> Char* { - if (!decimal_point) - return format_decimal(out, significand, significand_size).end; - out += significand_size + 1; - Char* end = out; - int floating_size = significand_size - integral_size; - for (int i = floating_size / 2; i > 0; --i) { - out -= 2; - copy2(out, digits2(static_cast(significand % 100))); - significand /= 100; - } - if (floating_size % 2 != 0) { - *--out = static_cast('0' + significand % 10); - significand /= 10; - } - *--out = decimal_point; - format_decimal(out - integral_size, significand, integral_size); - return end; -} - -template >::value)> -inline auto write_significand(OutputIt out, UInt significand, - int significand_size, int integral_size, - Char decimal_point) -> OutputIt { - // Buffer is large enough to hold digits (digits10 + 1) and a decimal point. - Char buffer[digits10() + 2]; - auto end = write_significand(buffer, significand, significand_size, - integral_size, decimal_point); - return detail::copy_noinline(buffer, end, out); -} - -template -FMT_CONSTEXPR auto write_significand(OutputIt out, const char* significand, - int significand_size, int integral_size, - Char decimal_point) -> OutputIt { - out = detail::copy_noinline(significand, significand + integral_size, - out); - if (!decimal_point) return out; - *out++ = decimal_point; - return detail::copy_noinline(significand + integral_size, - significand + significand_size, out); -} - -template -FMT_CONSTEXPR20 auto write_significand(OutputIt out, T significand, - int significand_size, int integral_size, - Char decimal_point, - const Grouping& grouping) -> OutputIt { - if (!grouping.has_separator()) { - return write_significand(out, significand, significand_size, integral_size, - decimal_point); - } - auto buffer = basic_memory_buffer(); - write_significand(basic_appender(buffer), significand, significand_size, - integral_size, decimal_point); - grouping.apply( - out, basic_string_view(buffer.data(), to_unsigned(integral_size))); - return detail::copy_noinline(buffer.data() + integral_size, - buffer.end(), out); -} - -template > -FMT_CONSTEXPR20 auto do_write_float(OutputIt out, const DecimalFP& f, - const format_specs& specs, - float_specs fspecs, locale_ref loc) - -> OutputIt { - auto significand = f.significand; - int significand_size = get_significand_size(f); - const Char zero = static_cast('0'); - auto sign = fspecs.sign; - size_t size = to_unsigned(significand_size) + (sign ? 1 : 0); - using iterator = reserve_iterator; - - Char decimal_point = - fspecs.locale ? detail::decimal_point(loc) : static_cast('.'); - - int output_exp = f.exponent + significand_size - 1; - auto use_exp_format = [=]() { - if (fspecs.format == float_format::exp) return true; - if (fspecs.format != float_format::general) return false; - // Use the fixed notation if the exponent is in [exp_lower, exp_upper), - // e.g. 0.0001 instead of 1e-04. Otherwise use the exponent notation. - const int exp_lower = -4, exp_upper = 16; - return output_exp < exp_lower || - output_exp >= (fspecs.precision > 0 ? fspecs.precision : exp_upper); - }; - if (use_exp_format()) { - int num_zeros = 0; - if (fspecs.showpoint) { - num_zeros = fspecs.precision - significand_size; - if (num_zeros < 0) num_zeros = 0; - size += to_unsigned(num_zeros); - } else if (significand_size == 1) { - decimal_point = Char(); - } - auto abs_output_exp = output_exp >= 0 ? output_exp : -output_exp; - int exp_digits = 2; - if (abs_output_exp >= 100) exp_digits = abs_output_exp >= 1000 ? 4 : 3; - - size += to_unsigned((decimal_point ? 1 : 0) + 2 + exp_digits); - char exp_char = specs.upper ? 'E' : 'e'; - auto write = [=](iterator it) { - if (sign) *it++ = detail::sign(sign); - // Insert a decimal point after the first digit and add an exponent. - it = write_significand(it, significand, significand_size, 1, - decimal_point); - if (num_zeros > 0) it = detail::fill_n(it, num_zeros, zero); - *it++ = static_cast(exp_char); - return write_exponent(output_exp, it); - }; - return specs.width > 0 - ? write_padded(out, specs, size, write) - : base_iterator(out, write(reserve(out, size))); - } - - int exp = f.exponent + significand_size; - if (f.exponent >= 0) { - // 1234e5 -> 123400000[.0+] - size += to_unsigned(f.exponent); - int num_zeros = fspecs.precision - exp; - abort_fuzzing_if(num_zeros > 5000); - if (fspecs.showpoint) { - ++size; - if (num_zeros <= 0 && fspecs.format != float_format::fixed) num_zeros = 0; - if (num_zeros > 0) size += to_unsigned(num_zeros); - } - auto grouping = Grouping(loc, fspecs.locale); - size += to_unsigned(grouping.count_separators(exp)); - return write_padded(out, specs, size, [&](iterator it) { - if (sign) *it++ = detail::sign(sign); - it = write_significand(it, significand, significand_size, - f.exponent, grouping); - if (!fspecs.showpoint) return it; - *it++ = decimal_point; - return num_zeros > 0 ? detail::fill_n(it, num_zeros, zero) : it; - }); - } else if (exp > 0) { - // 1234e-2 -> 12.34[0+] - int num_zeros = fspecs.showpoint ? fspecs.precision - significand_size : 0; - size += 1 + to_unsigned(num_zeros > 0 ? num_zeros : 0); - auto grouping = Grouping(loc, fspecs.locale); - size += to_unsigned(grouping.count_separators(exp)); - return write_padded(out, specs, size, [&](iterator it) { - if (sign) *it++ = detail::sign(sign); - it = write_significand(it, significand, significand_size, exp, - decimal_point, grouping); - return num_zeros > 0 ? detail::fill_n(it, num_zeros, zero) : it; - }); - } - // 1234e-6 -> 0.001234 - int num_zeros = -exp; - if (significand_size == 0 && fspecs.precision >= 0 && - fspecs.precision < num_zeros) { - num_zeros = fspecs.precision; - } - bool pointy = num_zeros != 0 || significand_size != 0 || fspecs.showpoint; - size += 1 + (pointy ? 1 : 0) + to_unsigned(num_zeros); - return write_padded(out, specs, size, [&](iterator it) { - if (sign) *it++ = detail::sign(sign); - *it++ = zero; - if (!pointy) return it; - *it++ = decimal_point; - it = detail::fill_n(it, num_zeros, zero); - return write_significand(it, significand, significand_size); - }); -} - -template class fallback_digit_grouping { - public: - constexpr fallback_digit_grouping(locale_ref, bool) {} - - constexpr auto has_separator() const -> bool { return false; } - - constexpr auto count_separators(int) const -> int { return 0; } - - template - constexpr auto apply(Out out, basic_string_view) const -> Out { - return out; - } -}; - -template -FMT_CONSTEXPR20 auto write_float(OutputIt out, const DecimalFP& f, - const format_specs& specs, float_specs fspecs, - locale_ref loc) -> OutputIt { - if (is_constant_evaluated()) { - return do_write_float>(out, f, specs, fspecs, - loc); - } else { - return do_write_float(out, f, specs, fspecs, loc); - } -} - -template constexpr auto isnan(T value) -> bool { - return value != value; // std::isnan doesn't support __float128. -} - -template -struct has_isfinite : std::false_type {}; - -template -struct has_isfinite> - : std::true_type {}; - -template ::value&& - has_isfinite::value)> -FMT_CONSTEXPR20 auto isfinite(T value) -> bool { - constexpr T inf = T(std::numeric_limits::infinity()); - if (is_constant_evaluated()) - return !detail::isnan(value) && value < inf && value > -inf; - return std::isfinite(value); -} -template ::value)> -FMT_CONSTEXPR auto isfinite(T value) -> bool { - T inf = T(std::numeric_limits::infinity()); - // std::isfinite doesn't support __float128. - return !detail::isnan(value) && value < inf && value > -inf; -} - -template ::value)> -FMT_INLINE FMT_CONSTEXPR bool signbit(T value) { - if (is_constant_evaluated()) { -#ifdef __cpp_if_constexpr - if constexpr (std::numeric_limits::is_iec559) { - auto bits = detail::bit_cast(static_cast(value)); - return (bits >> (num_bits() - 1)) != 0; - } -#endif - } - return std::signbit(static_cast(value)); -} - -inline FMT_CONSTEXPR20 void adjust_precision(int& precision, int exp10) { - // Adjust fixed precision by exponent because it is relative to decimal - // point. - if (exp10 > 0 && precision > max_value() - exp10) - FMT_THROW(format_error("number is too big")); - precision += exp10; -} - -class bigint { - private: - // A bigint is stored as an array of bigits (big digits), with bigit at index - // 0 being the least significant one. - using bigit = uint32_t; - using double_bigit = uint64_t; - enum { bigits_capacity = 32 }; - basic_memory_buffer bigits_; - int exp_; - - FMT_CONSTEXPR20 auto operator[](int index) const -> bigit { - return bigits_[to_unsigned(index)]; - } - FMT_CONSTEXPR20 auto operator[](int index) -> bigit& { - return bigits_[to_unsigned(index)]; - } - - static constexpr const int bigit_bits = num_bits(); - - friend struct formatter; - - FMT_CONSTEXPR20 void subtract_bigits(int index, bigit other, bigit& borrow) { - auto result = static_cast((*this)[index]) - other - borrow; - (*this)[index] = static_cast(result); - borrow = static_cast(result >> (bigit_bits * 2 - 1)); - } - - FMT_CONSTEXPR20 void remove_leading_zeros() { - int num_bigits = static_cast(bigits_.size()) - 1; - while (num_bigits > 0 && (*this)[num_bigits] == 0) --num_bigits; - bigits_.resize(to_unsigned(num_bigits + 1)); - } - - // Computes *this -= other assuming aligned bigints and *this >= other. - FMT_CONSTEXPR20 void subtract_aligned(const bigint& other) { - FMT_ASSERT(other.exp_ >= exp_, "unaligned bigints"); - FMT_ASSERT(compare(*this, other) >= 0, ""); - bigit borrow = 0; - int i = other.exp_ - exp_; - for (size_t j = 0, n = other.bigits_.size(); j != n; ++i, ++j) - subtract_bigits(i, other.bigits_[j], borrow); - while (borrow > 0) subtract_bigits(i, 0, borrow); - remove_leading_zeros(); - } - - FMT_CONSTEXPR20 void multiply(uint32_t value) { - const double_bigit wide_value = value; - bigit carry = 0; - for (size_t i = 0, n = bigits_.size(); i < n; ++i) { - double_bigit result = bigits_[i] * wide_value + carry; - bigits_[i] = static_cast(result); - carry = static_cast(result >> bigit_bits); - } - if (carry != 0) bigits_.push_back(carry); - } - - template ::value || - std::is_same::value)> - FMT_CONSTEXPR20 void multiply(UInt value) { - using half_uint = - conditional_t::value, uint64_t, uint32_t>; - const int shift = num_bits() - bigit_bits; - const UInt lower = static_cast(value); - const UInt upper = value >> num_bits(); - UInt carry = 0; - for (size_t i = 0, n = bigits_.size(); i < n; ++i) { - UInt result = lower * bigits_[i] + static_cast(carry); - carry = (upper * bigits_[i] << shift) + (result >> bigit_bits) + - (carry >> bigit_bits); - bigits_[i] = static_cast(result); - } - while (carry != 0) { - bigits_.push_back(static_cast(carry)); - carry >>= bigit_bits; - } - } - - template ::value || - std::is_same::value)> - FMT_CONSTEXPR20 void assign(UInt n) { - size_t num_bigits = 0; - do { - bigits_[num_bigits++] = static_cast(n); - n >>= bigit_bits; - } while (n != 0); - bigits_.resize(num_bigits); - exp_ = 0; - } - - public: - FMT_CONSTEXPR20 bigint() : exp_(0) {} - explicit bigint(uint64_t n) { assign(n); } - - bigint(const bigint&) = delete; - void operator=(const bigint&) = delete; - - FMT_CONSTEXPR20 void assign(const bigint& other) { - auto size = other.bigits_.size(); - bigits_.resize(size); - auto data = other.bigits_.data(); - copy(data, data + size, bigits_.data()); - exp_ = other.exp_; - } - - template FMT_CONSTEXPR20 void operator=(Int n) { - FMT_ASSERT(n > 0, ""); - assign(uint64_or_128_t(n)); - } - - FMT_CONSTEXPR20 auto num_bigits() const -> int { - return static_cast(bigits_.size()) + exp_; - } - - FMT_NOINLINE FMT_CONSTEXPR20 auto operator<<=(int shift) -> bigint& { - FMT_ASSERT(shift >= 0, ""); - exp_ += shift / bigit_bits; - shift %= bigit_bits; - if (shift == 0) return *this; - bigit carry = 0; - for (size_t i = 0, n = bigits_.size(); i < n; ++i) { - bigit c = bigits_[i] >> (bigit_bits - shift); - bigits_[i] = (bigits_[i] << shift) + carry; - carry = c; - } - if (carry != 0) bigits_.push_back(carry); - return *this; - } - - template - FMT_CONSTEXPR20 auto operator*=(Int value) -> bigint& { - FMT_ASSERT(value > 0, ""); - multiply(uint32_or_64_or_128_t(value)); - return *this; - } - - friend FMT_CONSTEXPR20 auto compare(const bigint& lhs, const bigint& rhs) - -> int { - int num_lhs_bigits = lhs.num_bigits(), num_rhs_bigits = rhs.num_bigits(); - if (num_lhs_bigits != num_rhs_bigits) - return num_lhs_bigits > num_rhs_bigits ? 1 : -1; - int i = static_cast(lhs.bigits_.size()) - 1; - int j = static_cast(rhs.bigits_.size()) - 1; - int end = i - j; - if (end < 0) end = 0; - for (; i >= end; --i, --j) { - bigit lhs_bigit = lhs[i], rhs_bigit = rhs[j]; - if (lhs_bigit != rhs_bigit) return lhs_bigit > rhs_bigit ? 1 : -1; - } - if (i != j) return i > j ? 1 : -1; - return 0; - } - - // Returns compare(lhs1 + lhs2, rhs). - friend FMT_CONSTEXPR20 auto add_compare(const bigint& lhs1, - const bigint& lhs2, const bigint& rhs) - -> int { - auto minimum = [](int a, int b) { return a < b ? a : b; }; - auto maximum = [](int a, int b) { return a > b ? a : b; }; - int max_lhs_bigits = maximum(lhs1.num_bigits(), lhs2.num_bigits()); - int num_rhs_bigits = rhs.num_bigits(); - if (max_lhs_bigits + 1 < num_rhs_bigits) return -1; - if (max_lhs_bigits > num_rhs_bigits) return 1; - auto get_bigit = [](const bigint& n, int i) -> bigit { - return i >= n.exp_ && i < n.num_bigits() ? n[i - n.exp_] : 0; - }; - double_bigit borrow = 0; - int min_exp = minimum(minimum(lhs1.exp_, lhs2.exp_), rhs.exp_); - for (int i = num_rhs_bigits - 1; i >= min_exp; --i) { - double_bigit sum = - static_cast(get_bigit(lhs1, i)) + get_bigit(lhs2, i); - bigit rhs_bigit = get_bigit(rhs, i); - if (sum > rhs_bigit + borrow) return 1; - borrow = rhs_bigit + borrow - sum; - if (borrow > 1) return -1; - borrow <<= bigit_bits; - } - return borrow != 0 ? -1 : 0; - } - - // Assigns pow(10, exp) to this bigint. - FMT_CONSTEXPR20 void assign_pow10(int exp) { - FMT_ASSERT(exp >= 0, ""); - if (exp == 0) return *this = 1; - // Find the top bit. - int bitmask = 1; - while (exp >= bitmask) bitmask <<= 1; - bitmask >>= 1; - // pow(10, exp) = pow(5, exp) * pow(2, exp). First compute pow(5, exp) by - // repeated squaring and multiplication. - *this = 5; - bitmask >>= 1; - while (bitmask != 0) { - square(); - if ((exp & bitmask) != 0) *this *= 5; - bitmask >>= 1; - } - *this <<= exp; // Multiply by pow(2, exp) by shifting. - } - - FMT_CONSTEXPR20 void square() { - int num_bigits = static_cast(bigits_.size()); - int num_result_bigits = 2 * num_bigits; - basic_memory_buffer n(std::move(bigits_)); - bigits_.resize(to_unsigned(num_result_bigits)); - auto sum = uint128_t(); - for (int bigit_index = 0; bigit_index < num_bigits; ++bigit_index) { - // Compute bigit at position bigit_index of the result by adding - // cross-product terms n[i] * n[j] such that i + j == bigit_index. - for (int i = 0, j = bigit_index; j >= 0; ++i, --j) { - // Most terms are multiplied twice which can be optimized in the future. - sum += static_cast(n[i]) * n[j]; - } - (*this)[bigit_index] = static_cast(sum); - sum >>= num_bits(); // Compute the carry. - } - // Do the same for the top half. - for (int bigit_index = num_bigits; bigit_index < num_result_bigits; - ++bigit_index) { - for (int j = num_bigits - 1, i = bigit_index - j; i < num_bigits;) - sum += static_cast(n[i++]) * n[j--]; - (*this)[bigit_index] = static_cast(sum); - sum >>= num_bits(); - } - remove_leading_zeros(); - exp_ *= 2; - } - - // If this bigint has a bigger exponent than other, adds trailing zero to make - // exponents equal. This simplifies some operations such as subtraction. - FMT_CONSTEXPR20 void align(const bigint& other) { - int exp_difference = exp_ - other.exp_; - if (exp_difference <= 0) return; - int num_bigits = static_cast(bigits_.size()); - bigits_.resize(to_unsigned(num_bigits + exp_difference)); - for (int i = num_bigits - 1, j = i + exp_difference; i >= 0; --i, --j) - bigits_[j] = bigits_[i]; - memset(bigits_.data(), 0, to_unsigned(exp_difference) * sizeof(bigit)); - exp_ -= exp_difference; - } - - // Divides this bignum by divisor, assigning the remainder to this and - // returning the quotient. - FMT_CONSTEXPR20 auto divmod_assign(const bigint& divisor) -> int { - FMT_ASSERT(this != &divisor, ""); - if (compare(*this, divisor) < 0) return 0; - FMT_ASSERT(divisor.bigits_[divisor.bigits_.size() - 1u] != 0, ""); - align(divisor); - int quotient = 0; - do { - subtract_aligned(divisor); - ++quotient; - } while (compare(*this, divisor) >= 0); - return quotient; - } -}; - -// format_dragon flags. -enum dragon { - predecessor_closer = 1, - fixup = 2, // Run fixup to correct exp10 which can be off by one. - fixed = 4, -}; - -// Formats a floating-point number using a variation of the Fixed-Precision -// Positive Floating-Point Printout ((FPP)^2) algorithm by Steele & White: -// https://fmt.dev/papers/p372-steele.pdf. -FMT_CONSTEXPR20 inline void format_dragon(basic_fp value, - unsigned flags, int num_digits, - buffer& buf, int& exp10) { - bigint numerator; // 2 * R in (FPP)^2. - bigint denominator; // 2 * S in (FPP)^2. - // lower and upper are differences between value and corresponding boundaries. - bigint lower; // (M^- in (FPP)^2). - bigint upper_store; // upper's value if different from lower. - bigint* upper = nullptr; // (M^+ in (FPP)^2). - // Shift numerator and denominator by an extra bit or two (if lower boundary - // is closer) to make lower and upper integers. This eliminates multiplication - // by 2 during later computations. - bool is_predecessor_closer = (flags & dragon::predecessor_closer) != 0; - int shift = is_predecessor_closer ? 2 : 1; - if (value.e >= 0) { - numerator = value.f; - numerator <<= value.e + shift; - lower = 1; - lower <<= value.e; - if (is_predecessor_closer) { - upper_store = 1; - upper_store <<= value.e + 1; - upper = &upper_store; - } - denominator.assign_pow10(exp10); - denominator <<= shift; - } else if (exp10 < 0) { - numerator.assign_pow10(-exp10); - lower.assign(numerator); - if (is_predecessor_closer) { - upper_store.assign(numerator); - upper_store <<= 1; - upper = &upper_store; - } - numerator *= value.f; - numerator <<= shift; - denominator = 1; - denominator <<= shift - value.e; - } else { - numerator = value.f; - numerator <<= shift; - denominator.assign_pow10(exp10); - denominator <<= shift - value.e; - lower = 1; - if (is_predecessor_closer) { - upper_store = 1ULL << 1; - upper = &upper_store; - } - } - int even = static_cast((value.f & 1) == 0); - if (!upper) upper = &lower; - bool shortest = num_digits < 0; - if ((flags & dragon::fixup) != 0) { - if (add_compare(numerator, *upper, denominator) + even <= 0) { - --exp10; - numerator *= 10; - if (num_digits < 0) { - lower *= 10; - if (upper != &lower) *upper *= 10; - } - } - if ((flags & dragon::fixed) != 0) adjust_precision(num_digits, exp10 + 1); - } - // Invariant: value == (numerator / denominator) * pow(10, exp10). - if (shortest) { - // Generate the shortest representation. - num_digits = 0; - char* data = buf.data(); - for (;;) { - int digit = numerator.divmod_assign(denominator); - bool low = compare(numerator, lower) - even < 0; // numerator <[=] lower. - // numerator + upper >[=] pow10: - bool high = add_compare(numerator, *upper, denominator) + even > 0; - data[num_digits++] = static_cast('0' + digit); - if (low || high) { - if (!low) { - ++data[num_digits - 1]; - } else if (high) { - int result = add_compare(numerator, numerator, denominator); - // Round half to even. - if (result > 0 || (result == 0 && (digit % 2) != 0)) - ++data[num_digits - 1]; - } - buf.try_resize(to_unsigned(num_digits)); - exp10 -= num_digits - 1; - return; - } - numerator *= 10; - lower *= 10; - if (upper != &lower) *upper *= 10; - } - } - // Generate the given number of digits. - exp10 -= num_digits - 1; - if (num_digits <= 0) { - auto digit = '0'; - if (num_digits == 0) { - denominator *= 10; - digit = add_compare(numerator, numerator, denominator) > 0 ? '1' : '0'; - } - buf.push_back(digit); - return; - } - buf.try_resize(to_unsigned(num_digits)); - for (int i = 0; i < num_digits - 1; ++i) { - int digit = numerator.divmod_assign(denominator); - buf[i] = static_cast('0' + digit); - numerator *= 10; - } - int digit = numerator.divmod_assign(denominator); - auto result = add_compare(numerator, numerator, denominator); - if (result > 0 || (result == 0 && (digit % 2) != 0)) { - if (digit == 9) { - const auto overflow = '0' + 10; - buf[num_digits - 1] = overflow; - // Propagate the carry. - for (int i = num_digits - 1; i > 0 && buf[i] == overflow; --i) { - buf[i] = '0'; - ++buf[i - 1]; - } - if (buf[0] == overflow) { - buf[0] = '1'; - if ((flags & dragon::fixed) != 0) - buf.push_back('0'); - else - ++exp10; - } - return; - } - ++digit; - } - buf[num_digits - 1] = static_cast('0' + digit); -} - -// Formats a floating-point number using the hexfloat format. -template ::value)> -FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs, - buffer& buf) { - // float is passed as double to reduce the number of instantiations and to - // simplify implementation. - static_assert(!std::is_same::value, ""); - - using info = dragonbox::float_info; - - // Assume Float is in the format [sign][exponent][significand]. - using carrier_uint = typename info::carrier_uint; - - constexpr auto num_float_significand_bits = - detail::num_significand_bits(); - - basic_fp f(value); - f.e += num_float_significand_bits; - if (!has_implicit_bit()) --f.e; - - constexpr auto num_fraction_bits = - num_float_significand_bits + (has_implicit_bit() ? 1 : 0); - constexpr auto num_xdigits = (num_fraction_bits + 3) / 4; - - constexpr auto leading_shift = ((num_xdigits - 1) * 4); - const auto leading_mask = carrier_uint(0xF) << leading_shift; - const auto leading_xdigit = - static_cast((f.f & leading_mask) >> leading_shift); - if (leading_xdigit > 1) f.e -= (32 - countl_zero(leading_xdigit) - 1); - - int print_xdigits = num_xdigits - 1; - if (specs.precision >= 0 && print_xdigits > specs.precision) { - const int shift = ((print_xdigits - specs.precision - 1) * 4); - const auto mask = carrier_uint(0xF) << shift; - const auto v = static_cast((f.f & mask) >> shift); - - if (v >= 8) { - const auto inc = carrier_uint(1) << (shift + 4); - f.f += inc; - f.f &= ~(inc - 1); - } - - // Check long double overflow - if (!has_implicit_bit()) { - const auto implicit_bit = carrier_uint(1) << num_float_significand_bits; - if ((f.f & implicit_bit) == implicit_bit) { - f.f >>= 4; - f.e += 4; - } - } - - print_xdigits = specs.precision; - } - - char xdigits[num_bits() / 4]; - detail::fill_n(xdigits, sizeof(xdigits), '0'); - format_uint<4>(xdigits, f.f, num_xdigits, specs.upper); - - // Remove zero tail - while (print_xdigits > 0 && xdigits[print_xdigits] == '0') --print_xdigits; - - buf.push_back('0'); - buf.push_back(specs.upper ? 'X' : 'x'); - buf.push_back(xdigits[0]); - if (specs.alt || print_xdigits > 0 || print_xdigits < specs.precision) - buf.push_back('.'); - buf.append(xdigits + 1, xdigits + 1 + print_xdigits); - for (; print_xdigits < specs.precision; ++print_xdigits) buf.push_back('0'); - - buf.push_back(specs.upper ? 'P' : 'p'); - - uint32_t abs_e; - if (f.e < 0) { - buf.push_back('-'); - abs_e = static_cast(-f.e); - } else { - buf.push_back('+'); - abs_e = static_cast(f.e); - } - format_decimal(appender(buf), abs_e, detail::count_digits(abs_e)); -} - -template ::value)> -FMT_CONSTEXPR20 void format_hexfloat(Float value, format_specs specs, - buffer& buf) { - format_hexfloat(static_cast(value), specs, buf); -} - -constexpr auto fractional_part_rounding_thresholds(int index) -> uint32_t { - // For checking rounding thresholds. - // The kth entry is chosen to be the smallest integer such that the - // upper 32-bits of 10^(k+1) times it is strictly bigger than 5 * 10^k. - // It is equal to ceil(2^31 + 2^32/10^(k + 1)). - // These are stored in a string literal because we cannot have static arrays - // in constexpr functions and non-static ones are poorly optimized. - return U"\x9999999a\x828f5c29\x80418938\x80068db9\x8000a7c6\x800010c7" - U"\x800001ae\x8000002b"[index]; -} - -template -FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs, - buffer& buf) -> int { - // float is passed as double to reduce the number of instantiations. - static_assert(!std::is_same::value, ""); - FMT_ASSERT(value >= 0, "value is negative"); - auto converted_value = convert_float(value); - - const bool fixed = specs.format == float_format::fixed; - if (value <= 0) { // <= instead of == to silence a warning. - if (precision <= 0 || !fixed) { - buf.push_back('0'); - return 0; - } - buf.try_resize(to_unsigned(precision)); - fill_n(buf.data(), precision, '0'); - return -precision; - } - - int exp = 0; - bool use_dragon = true; - unsigned dragon_flags = 0; - if (!is_fast_float() || is_constant_evaluated()) { - const auto inv_log2_10 = 0.3010299956639812; // 1 / log2(10) - using info = dragonbox::float_info; - const auto f = basic_fp(converted_value); - // Compute exp, an approximate power of 10, such that - // 10^(exp - 1) <= value < 10^exp or 10^exp <= value < 10^(exp + 1). - // This is based on log10(value) == log2(value) / log2(10) and approximation - // of log2(value) by e + num_fraction_bits idea from double-conversion. - auto e = (f.e + count_digits<1>(f.f) - 1) * inv_log2_10 - 1e-10; - exp = static_cast(e); - if (e > exp) ++exp; // Compute ceil. - dragon_flags = dragon::fixup; - } else if (precision < 0) { - // Use Dragonbox for the shortest format. - if (specs.binary32) { - auto dec = dragonbox::to_decimal(static_cast(value)); - write(appender(buf), dec.significand); - return dec.exponent; - } - auto dec = dragonbox::to_decimal(static_cast(value)); - write(appender(buf), dec.significand); - return dec.exponent; - } else { - // Extract significand bits and exponent bits. - using info = dragonbox::float_info; - auto br = bit_cast(static_cast(value)); - - const uint64_t significand_mask = - (static_cast(1) << num_significand_bits()) - 1; - uint64_t significand = (br & significand_mask); - int exponent = static_cast((br & exponent_mask()) >> - num_significand_bits()); - - if (exponent != 0) { // Check if normal. - exponent -= exponent_bias() + num_significand_bits(); - significand |= - (static_cast(1) << num_significand_bits()); - significand <<= 1; - } else { - // Normalize subnormal inputs. - FMT_ASSERT(significand != 0, "zeros should not appear here"); - int shift = countl_zero(significand); - FMT_ASSERT(shift >= num_bits() - num_significand_bits(), - ""); - shift -= (num_bits() - num_significand_bits() - 2); - exponent = (std::numeric_limits::min_exponent - - num_significand_bits()) - - shift; - significand <<= shift; - } - - // Compute the first several nonzero decimal significand digits. - // We call the number we get the first segment. - const int k = info::kappa - dragonbox::floor_log10_pow2(exponent); - exp = -k; - const int beta = exponent + dragonbox::floor_log2_pow10(k); - uint64_t first_segment; - bool has_more_segments; - int digits_in_the_first_segment; - { - const auto r = dragonbox::umul192_upper128( - significand << beta, dragonbox::get_cached_power(k)); - first_segment = r.high(); - has_more_segments = r.low() != 0; - - // The first segment can have 18 ~ 19 digits. - if (first_segment >= 1000000000000000000ULL) { - digits_in_the_first_segment = 19; - } else { - // When it is of 18-digits, we align it to 19-digits by adding a bogus - // zero at the end. - digits_in_the_first_segment = 18; - first_segment *= 10; - } - } - - // Compute the actual number of decimal digits to print. - if (fixed) adjust_precision(precision, exp + digits_in_the_first_segment); - - // Use Dragon4 only when there might be not enough digits in the first - // segment. - if (digits_in_the_first_segment > precision) { - use_dragon = false; - - if (precision <= 0) { - exp += digits_in_the_first_segment; - - if (precision < 0) { - // Nothing to do, since all we have are just leading zeros. - buf.try_resize(0); - } else { - // We may need to round-up. - buf.try_resize(1); - if ((first_segment | static_cast(has_more_segments)) > - 5000000000000000000ULL) { - buf[0] = '1'; - } else { - buf[0] = '0'; - } - } - } // precision <= 0 - else { - exp += digits_in_the_first_segment - precision; - - // When precision > 0, we divide the first segment into three - // subsegments, each with 9, 9, and 0 ~ 1 digits so that each fits - // in 32-bits which usually allows faster calculation than in - // 64-bits. Since some compiler (e.g. MSVC) doesn't know how to optimize - // division-by-constant for large 64-bit divisors, we do it here - // manually. The magic number 7922816251426433760 below is equal to - // ceil(2^(64+32) / 10^10). - const uint32_t first_subsegment = static_cast( - dragonbox::umul128_upper64(first_segment, 7922816251426433760ULL) >> - 32); - const uint64_t second_third_subsegments = - first_segment - first_subsegment * 10000000000ULL; - - uint64_t prod; - uint32_t digits; - bool should_round_up; - int number_of_digits_to_print = precision > 9 ? 9 : precision; - - // Print a 9-digits subsegment, either the first or the second. - auto print_subsegment = [&](uint32_t subsegment, char* buffer) { - int number_of_digits_printed = 0; - - // If we want to print an odd number of digits from the subsegment, - if ((number_of_digits_to_print & 1) != 0) { - // Convert to 64-bit fixed-point fractional form with 1-digit - // integer part. The magic number 720575941 is a good enough - // approximation of 2^(32 + 24) / 10^8; see - // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case - // for details. - prod = ((subsegment * static_cast(720575941)) >> 24) + 1; - digits = static_cast(prod >> 32); - *buffer = static_cast('0' + digits); - number_of_digits_printed++; - } - // If we want to print an even number of digits from the - // first_subsegment, - else { - // Convert to 64-bit fixed-point fractional form with 2-digits - // integer part. The magic number 450359963 is a good enough - // approximation of 2^(32 + 20) / 10^7; see - // https://jk-jeon.github.io/posts/2022/12/fixed-precision-formatting/#fixed-length-case - // for details. - prod = ((subsegment * static_cast(450359963)) >> 20) + 1; - digits = static_cast(prod >> 32); - copy2(buffer, digits2(digits)); - number_of_digits_printed += 2; - } - - // Print all digit pairs. - while (number_of_digits_printed < number_of_digits_to_print) { - prod = static_cast(prod) * static_cast(100); - digits = static_cast(prod >> 32); - copy2(buffer + number_of_digits_printed, digits2(digits)); - number_of_digits_printed += 2; - } - }; - - // Print first subsegment. - print_subsegment(first_subsegment, buf.data()); - - // Perform rounding if the first subsegment is the last subsegment to - // print. - if (precision <= 9) { - // Rounding inside the subsegment. - // We round-up if: - // - either the fractional part is strictly larger than 1/2, or - // - the fractional part is exactly 1/2 and the last digit is odd. - // We rely on the following observations: - // - If fractional_part >= threshold, then the fractional part is - // strictly larger than 1/2. - // - If the MSB of fractional_part is set, then the fractional part - // must be at least 1/2. - // - When the MSB of fractional_part is set, either - // second_third_subsegments being nonzero or has_more_segments - // being true means there are further digits not printed, so the - // fractional part is strictly larger than 1/2. - if (precision < 9) { - uint32_t fractional_part = static_cast(prod); - should_round_up = - fractional_part >= fractional_part_rounding_thresholds( - 8 - number_of_digits_to_print) || - ((fractional_part >> 31) & - ((digits & 1) | (second_third_subsegments != 0) | - has_more_segments)) != 0; - } - // Rounding at the subsegment boundary. - // In this case, the fractional part is at least 1/2 if and only if - // second_third_subsegments >= 5000000000ULL, and is strictly larger - // than 1/2 if we further have either second_third_subsegments > - // 5000000000ULL or has_more_segments == true. - else { - should_round_up = second_third_subsegments > 5000000000ULL || - (second_third_subsegments == 5000000000ULL && - ((digits & 1) != 0 || has_more_segments)); - } - } - // Otherwise, print the second subsegment. - else { - // Compilers are not aware of how to leverage the maximum value of - // second_third_subsegments to find out a better magic number which - // allows us to eliminate an additional shift. 1844674407370955162 = - // ceil(2^64/10) < ceil(2^64*(10^9/(10^10 - 1))). - const uint32_t second_subsegment = - static_cast(dragonbox::umul128_upper64( - second_third_subsegments, 1844674407370955162ULL)); - const uint32_t third_subsegment = - static_cast(second_third_subsegments) - - second_subsegment * 10; - - number_of_digits_to_print = precision - 9; - print_subsegment(second_subsegment, buf.data() + 9); - - // Rounding inside the subsegment. - if (precision < 18) { - // The condition third_subsegment != 0 implies that the segment was - // of 19 digits, so in this case the third segment should be - // consisting of a genuine digit from the input. - uint32_t fractional_part = static_cast(prod); - should_round_up = - fractional_part >= fractional_part_rounding_thresholds( - 8 - number_of_digits_to_print) || - ((fractional_part >> 31) & - ((digits & 1) | (third_subsegment != 0) | - has_more_segments)) != 0; - } - // Rounding at the subsegment boundary. - else { - // In this case, the segment must be of 19 digits, thus - // the third subsegment should be consisting of a genuine digit from - // the input. - should_round_up = third_subsegment > 5 || - (third_subsegment == 5 && - ((digits & 1) != 0 || has_more_segments)); - } - } - - // Round-up if necessary. - if (should_round_up) { - ++buf[precision - 1]; - for (int i = precision - 1; i > 0 && buf[i] > '9'; --i) { - buf[i] = '0'; - ++buf[i - 1]; - } - if (buf[0] > '9') { - buf[0] = '1'; - if (fixed) - buf[precision++] = '0'; - else - ++exp; - } - } - buf.try_resize(to_unsigned(precision)); - } - } // if (digits_in_the_first_segment > precision) - else { - // Adjust the exponent for its use in Dragon4. - exp += digits_in_the_first_segment - 1; - } - } - if (use_dragon) { - auto f = basic_fp(); - bool is_predecessor_closer = specs.binary32 - ? f.assign(static_cast(value)) - : f.assign(converted_value); - if (is_predecessor_closer) dragon_flags |= dragon::predecessor_closer; - if (fixed) dragon_flags |= dragon::fixed; - // Limit precision to the maximum possible number of significant digits in - // an IEEE754 double because we don't need to generate zeros. - const int max_double_digits = 767; - if (precision > max_double_digits) precision = max_double_digits; - format_dragon(f, dragon_flags, precision, buf, exp); - } - if (!fixed && !specs.showpoint) { - // Remove trailing zeros. - auto num_digits = buf.size(); - while (num_digits > 0 && buf[num_digits - 1] == '0') { - --num_digits; - ++exp; - } - buf.try_resize(num_digits); - } - return exp; -} - -template -FMT_CONSTEXPR20 auto write_float(OutputIt out, T value, format_specs specs, - locale_ref loc) -> OutputIt { - sign_t sign = specs.sign; - if (detail::signbit(value)) { // value < 0 is false for NaN so use signbit. - sign = sign::minus; - value = -value; - } else if (sign == sign::minus) { - sign = sign::none; - } - - if (!detail::isfinite(value)) - return write_nonfinite(out, detail::isnan(value), specs, sign); - - if (specs.align == align::numeric && sign) { - auto it = reserve(out, 1); - *it++ = detail::sign(sign); - out = base_iterator(out, it); - sign = sign::none; - if (specs.width != 0) --specs.width; - } - - memory_buffer buffer; - if (specs.type == presentation_type::hexfloat) { - if (sign) buffer.push_back(detail::sign(sign)); - format_hexfloat(convert_float(value), specs, buffer); - return write_bytes(out, {buffer.data(), buffer.size()}, - specs); - } - - int precision = specs.precision >= 0 || specs.type == presentation_type::none - ? specs.precision - : 6; - if (specs.type == presentation_type::exp) { - if (precision == max_value()) - report_error("number is too big"); - else - ++precision; - } else if (specs.type != presentation_type::fixed && precision == 0) { - precision = 1; - } - float_specs fspecs = parse_float_type_spec(specs); - fspecs.sign = sign; - if (const_check(std::is_same())) fspecs.binary32 = true; - int exp = format_float(convert_float(value), precision, fspecs, buffer); - fspecs.precision = precision; - auto f = big_decimal_fp{buffer.data(), static_cast(buffer.size()), exp}; - return write_float(out, f, specs, fspecs, loc); -} - -template ::value)> -FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs specs, - locale_ref loc = {}) -> OutputIt { - if (const_check(!is_supported_floating_point(value))) return out; - return specs.localized && write_loc(out, value, specs, loc) - ? out - : write_float(out, value, specs, loc); -} - -template ::value)> -FMT_CONSTEXPR20 auto write(OutputIt out, T value) -> OutputIt { - if (is_constant_evaluated()) return write(out, value, format_specs()); - if (const_check(!is_supported_floating_point(value))) return out; - - auto sign = sign_t::none; - if (detail::signbit(value)) { - sign = sign::minus; - value = -value; - } - - constexpr auto specs = format_specs(); - using floaty = conditional_t::value, double, T>; - using floaty_uint = typename dragonbox::float_info::carrier_uint; - floaty_uint mask = exponent_mask(); - if ((bit_cast(value) & mask) == mask) - return write_nonfinite(out, std::isnan(value), specs, sign); - - auto fspecs = float_specs(); - fspecs.sign = sign; - auto dec = dragonbox::to_decimal(static_cast(value)); - return write_float(out, dec, specs, fspecs, {}); -} - -template ::value && - !is_fast_float::value)> -inline auto write(OutputIt out, T value) -> OutputIt { - return write(out, value, format_specs()); -} - -template -auto write(OutputIt out, monostate, format_specs = {}, locale_ref = {}) - -> OutputIt { - FMT_ASSERT(false, ""); - return out; -} - -template -FMT_CONSTEXPR auto write(OutputIt out, basic_string_view value) - -> OutputIt { - return copy_noinline(value.begin(), value.end(), out); -} - -template ::value)> -constexpr auto write(OutputIt out, const T& value) -> OutputIt { - return write(out, to_string_view(value)); -} - -// FMT_ENABLE_IF() condition separated to workaround an MSVC bug. -template < - typename Char, typename OutputIt, typename T, - bool check = - std::is_enum::value && !std::is_same::value && - mapped_type_constant>::value != - type::custom_type, - FMT_ENABLE_IF(check)> -FMT_CONSTEXPR auto write(OutputIt out, T value) -> OutputIt { - return write(out, static_cast>(value)); -} - -template ::value)> -FMT_CONSTEXPR auto write(OutputIt out, T value, const format_specs& specs = {}, - locale_ref = {}) -> OutputIt { - return specs.type != presentation_type::none && - specs.type != presentation_type::string - ? write(out, value ? 1 : 0, specs, {}) - : write_bytes(out, value ? "true" : "false", specs); -} - -template -FMT_CONSTEXPR auto write(OutputIt out, Char value) -> OutputIt { - auto it = reserve(out, 1); - *it++ = value; - return base_iterator(out, it); -} - -template -FMT_CONSTEXPR20 auto write(OutputIt out, const Char* value) -> OutputIt { - if (value) return write(out, basic_string_view(value)); - report_error("string pointer is null"); - return out; -} - -template ::value)> -auto write(OutputIt out, const T* value, const format_specs& specs = {}, - locale_ref = {}) -> OutputIt { - return write_ptr(out, bit_cast(value), &specs); -} - -// A write overload that handles implicit conversions. -template > -FMT_CONSTEXPR auto write(OutputIt out, const T& value) -> enable_if_t< - std::is_class::value && !has_to_string_view::value && - !is_floating_point::value && !std::is_same::value && - !std::is_same().map( - value))>>::value, - OutputIt> { - return write(out, arg_mapper().map(value)); -} - -template > -FMT_CONSTEXPR auto write(OutputIt out, const T& value) - -> enable_if_t::value == - type::custom_type && - !std::is_fundamental::value, - OutputIt> { - auto formatter = typename Context::template formatter_type(); - auto parse_ctx = typename Context::parse_context_type({}); - formatter.parse(parse_ctx); - auto ctx = Context(out, {}, {}); - return formatter.format(value, ctx); -} - -// An argument visitor that formats the argument and writes it via the output -// iterator. It's a class and not a generic lambda for compatibility with C++11. -template struct default_arg_formatter { - using iterator = basic_appender; - using context = buffered_context; - - iterator out; - basic_format_args args; - locale_ref loc; - - template auto operator()(T value) -> iterator { - return write(out, value); - } - auto operator()(typename basic_format_arg::handle h) -> iterator { - basic_format_parse_context parse_ctx({}); - context format_ctx(out, args, loc); - h.format(parse_ctx, format_ctx); - return format_ctx.out(); - } -}; - -template struct arg_formatter { - using iterator = basic_appender; - using context = buffered_context; - - iterator out; - const format_specs& specs; - locale_ref locale; - - template - FMT_CONSTEXPR FMT_INLINE auto operator()(T value) -> iterator { - return detail::write(out, value, specs, locale); - } - auto operator()(typename basic_format_arg::handle) -> iterator { - // User-defined types are handled separately because they require access - // to the parse context. - return out; - } -}; - -struct width_checker { - template ::value)> - FMT_CONSTEXPR auto operator()(T value) -> unsigned long long { - if (is_negative(value)) report_error("negative width"); - return static_cast(value); - } - - template ::value)> - FMT_CONSTEXPR auto operator()(T) -> unsigned long long { - report_error("width is not integer"); - return 0; - } -}; - -struct precision_checker { - template ::value)> - FMT_CONSTEXPR auto operator()(T value) -> unsigned long long { - if (is_negative(value)) report_error("negative precision"); - return static_cast(value); - } - - template ::value)> - FMT_CONSTEXPR auto operator()(T) -> unsigned long long { - report_error("precision is not integer"); - return 0; - } -}; - -template -FMT_CONSTEXPR auto get_dynamic_spec(FormatArg arg) -> int { - unsigned long long value = arg.visit(Handler()); - if (value > to_unsigned(max_value())) report_error("number is too big"); - return static_cast(value); -} - -template -FMT_CONSTEXPR auto get_arg(Context& ctx, ID id) -> decltype(ctx.arg(id)) { - auto arg = ctx.arg(id); - if (!arg) report_error("argument not found"); - return arg; -} - -template -FMT_CONSTEXPR void handle_dynamic_spec(int& value, - arg_ref ref, - Context& ctx) { - switch (ref.kind) { - case arg_id_kind::none: - break; - case arg_id_kind::index: - value = detail::get_dynamic_spec(get_arg(ctx, ref.val.index)); - break; - case arg_id_kind::name: - value = detail::get_dynamic_spec(get_arg(ctx, ref.val.name)); - break; - } -} - -#if FMT_USE_USER_DEFINED_LITERALS -# if FMT_USE_NONTYPE_TEMPLATE_ARGS -template Str> -struct statically_named_arg : view { - static constexpr auto name = Str.data; - - const T& value; - statically_named_arg(const T& v) : value(v) {} -}; - -template Str> -struct is_named_arg> : std::true_type {}; - -template Str> -struct is_statically_named_arg> - : std::true_type {}; - -template Str> -struct udl_arg { - template auto operator=(T&& value) const { - return statically_named_arg(std::forward(value)); - } -}; -# else -template struct udl_arg { - const Char* str; - - template auto operator=(T&& value) const -> named_arg { - return {str, std::forward(value)}; - } -}; -# endif -#endif // FMT_USE_USER_DEFINED_LITERALS - -template -auto vformat(const Locale& loc, basic_string_view fmt, - typename detail::vformat_args::type args) - -> std::basic_string { - auto buf = basic_memory_buffer(); - detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); - return {buf.data(), buf.size()}; -} - -using format_func = void (*)(detail::buffer&, int, const char*); - -FMT_API void format_error_code(buffer& out, int error_code, - string_view message) noexcept; - -using fmt::report_error; -FMT_API void report_error(format_func func, int error_code, - const char* message) noexcept; -} // namespace detail - -FMT_BEGIN_EXPORT -FMT_API auto vsystem_error(int error_code, string_view format_str, - format_args args) -> std::system_error; - -/** - * Constructs `std::system_error` with a message formatted with - * `fmt::format(fmt, args...)`. - * `error_code` is a system error code as given by `errno`. - * - * **Example**: - * - * // This throws std::system_error with the description - * // cannot open file 'madeup': No such file or directory - * // or similar (system message may vary). - * const char* filename = "madeup"; - * std::FILE* file = std::fopen(filename, "r"); - * if (!file) - * throw fmt::system_error(errno, "cannot open file '{}'", filename); - */ -template -auto system_error(int error_code, format_string fmt, T&&... args) - -> std::system_error { - return vsystem_error(error_code, fmt, fmt::make_format_args(args...)); -} - -/** - * Formats an error message for an error returned by an operating system or a - * language runtime, for example a file opening error, and writes it to `out`. - * The format is the same as the one used by `std::system_error(ec, message)` - * where `ec` is `std::error_code(error_code, std::generic_category())`. - * It is implementation-defined but normally looks like: - * - * : - * - * where `` is the passed message and `` is the system - * message corresponding to the error code. - * `error_code` is a system error code as given by `errno`. - */ -FMT_API void format_system_error(detail::buffer& out, int error_code, - const char* message) noexcept; - -// Reports a system error without throwing an exception. -// Can be used to report errors from destructors. -FMT_API void report_system_error(int error_code, const char* message) noexcept; - -/// A fast integer formatter. -class format_int { - private: - // Buffer should be large enough to hold all digits (digits10 + 1), - // a sign and a null character. - enum { buffer_size = std::numeric_limits::digits10 + 3 }; - mutable char buffer_[buffer_size]; - char* str_; - - template - FMT_CONSTEXPR20 auto format_unsigned(UInt value) -> char* { - auto n = static_cast>(value); - return detail::format_decimal(buffer_, n, buffer_size - 1).begin; - } - - template - FMT_CONSTEXPR20 auto format_signed(Int value) -> char* { - auto abs_value = static_cast>(value); - bool negative = value < 0; - if (negative) abs_value = 0 - abs_value; - auto begin = format_unsigned(abs_value); - if (negative) *--begin = '-'; - return begin; - } - - public: - explicit FMT_CONSTEXPR20 format_int(int value) : str_(format_signed(value)) {} - explicit FMT_CONSTEXPR20 format_int(long value) - : str_(format_signed(value)) {} - explicit FMT_CONSTEXPR20 format_int(long long value) - : str_(format_signed(value)) {} - explicit FMT_CONSTEXPR20 format_int(unsigned value) - : str_(format_unsigned(value)) {} - explicit FMT_CONSTEXPR20 format_int(unsigned long value) - : str_(format_unsigned(value)) {} - explicit FMT_CONSTEXPR20 format_int(unsigned long long value) - : str_(format_unsigned(value)) {} - - /// Returns the number of characters written to the output buffer. - FMT_CONSTEXPR20 auto size() const -> size_t { - return detail::to_unsigned(buffer_ - str_ + buffer_size - 1); - } - - /// Returns a pointer to the output buffer content. No terminating null - /// character is appended. - FMT_CONSTEXPR20 auto data() const -> const char* { return str_; } - - /// Returns a pointer to the output buffer content with terminating null - /// character appended. - FMT_CONSTEXPR20 auto c_str() const -> const char* { - buffer_[buffer_size - 1] = '\0'; - return str_; - } - - /// Returns the content of the output buffer as an `std::string`. - auto str() const -> std::string { return std::string(str_, size()); } -}; - -template -struct formatter::value>> - : formatter, Char> { - template - auto format(const T& value, FormatContext& ctx) const -> decltype(ctx.out()) { - auto&& val = format_as(value); // Make an lvalue reference for format. - return formatter, Char>::format(val, ctx); - } -}; - -#define FMT_FORMAT_AS(Type, Base) \ - template \ - struct formatter : formatter { \ - template \ - auto format(Type value, FormatContext& ctx) const -> decltype(ctx.out()) { \ - return formatter::format(value, ctx); \ - } \ - } - -FMT_FORMAT_AS(signed char, int); -FMT_FORMAT_AS(unsigned char, unsigned); -FMT_FORMAT_AS(short, int); -FMT_FORMAT_AS(unsigned short, unsigned); -FMT_FORMAT_AS(long, detail::long_type); -FMT_FORMAT_AS(unsigned long, detail::ulong_type); -FMT_FORMAT_AS(Char*, const Char*); -FMT_FORMAT_AS(std::nullptr_t, const void*); -FMT_FORMAT_AS(detail::std_string_view, basic_string_view); -FMT_FORMAT_AS(void*, const void*); - -template -class formatter, Char> - : public formatter, Char> {}; - -template -struct formatter : formatter, Char> {}; - -/** - * Converts `p` to `const void*` for pointer formatting. - * - * **Example**: - * - * auto s = fmt::format("{}", fmt::ptr(p)); - */ -template auto ptr(T p) -> const void* { - static_assert(std::is_pointer::value, ""); - return detail::bit_cast(p); -} - -/** - * Converts `e` to the underlying type. - * - * **Example**: - * - * enum class color { red, green, blue }; - * auto s = fmt::format("{}", fmt::underlying(color::red)); - */ -template -constexpr auto underlying(Enum e) noexcept -> underlying_t { - return static_cast>(e); -} - -namespace enums { -template ::value)> -constexpr auto format_as(Enum e) noexcept -> underlying_t { - return static_cast>(e); -} -} // namespace enums - -class bytes { - private: - string_view data_; - friend struct formatter; - - public: - explicit bytes(string_view data) : data_(data) {} -}; - -template <> struct formatter { - private: - detail::dynamic_format_specs<> specs_; - - public: - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* { - return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, - detail::type::string_type); - } - - template - auto format(bytes b, FormatContext& ctx) const -> decltype(ctx.out()) { - auto specs = specs_; - detail::handle_dynamic_spec(specs.width, - specs.width_ref, ctx); - detail::handle_dynamic_spec( - specs.precision, specs.precision_ref, ctx); - return detail::write_bytes(ctx.out(), b.data_, specs); - } -}; - -// group_digits_view is not derived from view because it copies the argument. -template struct group_digits_view { - T value; -}; - -/** - * Returns a view that formats an integer value using ',' as a - * locale-independent thousands separator. - * - * **Example**: - * - * fmt::print("{}", fmt::group_digits(12345)); - * // Output: "12,345" - */ -template auto group_digits(T value) -> group_digits_view { - return {value}; -} - -template struct formatter> : formatter { - private: - detail::dynamic_format_specs<> specs_; - - public: - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const char* { - return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, - detail::type::int_type); - } - - template - auto format(group_digits_view t, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto specs = specs_; - detail::handle_dynamic_spec(specs.width, - specs.width_ref, ctx); - detail::handle_dynamic_spec( - specs.precision, specs.precision_ref, ctx); - auto arg = detail::make_write_int_arg(t.value, specs.sign); - return detail::write_int( - ctx.out(), static_cast>(arg.abs_value), - arg.prefix, specs, detail::digit_grouping("\3", ",")); - } -}; - -template struct nested_view { - const formatter* fmt; - const T* value; -}; - -template -struct formatter, Char> { - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } - template - auto format(nested_view view, FormatContext& ctx) const - -> decltype(ctx.out()) { - return view.fmt->format(*view.value, ctx); - } -}; - -template struct nested_formatter { - private: - int width_; - detail::fill_t fill_; - align_t align_ : 4; - formatter formatter_; - - public: - constexpr nested_formatter() : width_(0), align_(align_t::none) {} - - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto specs = detail::dynamic_format_specs(); - auto it = parse_format_specs(ctx.begin(), ctx.end(), specs, ctx, - detail::type::none_type); - width_ = specs.width; - fill_ = specs.fill; - align_ = specs.align; - ctx.advance_to(it); - return formatter_.parse(ctx); - } - - template - auto write_padded(FormatContext& ctx, F write) const -> decltype(ctx.out()) { - if (width_ == 0) return write(ctx.out()); - auto buf = basic_memory_buffer(); - write(basic_appender(buf)); - auto specs = format_specs(); - specs.width = width_; - specs.fill = fill_; - specs.align = align_; - return detail::write( - ctx.out(), basic_string_view(buf.data(), buf.size()), specs); - } - - auto nested(const T& value) const -> nested_view { - return nested_view{&formatter_, &value}; - } -}; - -/** - * Converts `value` to `std::string` using the default format for type `T`. - * - * **Example**: - * - * std::string answer = fmt::to_string(42); - */ -template ::value && - !detail::has_format_as::value)> -inline auto to_string(const T& value) -> std::string { - auto buffer = memory_buffer(); - detail::write(appender(buffer), value); - return {buffer.data(), buffer.size()}; -} - -template ::value)> -FMT_NODISCARD inline auto to_string(T value) -> std::string { - // The buffer should be large enough to store the number including the sign - // or "false" for bool. - constexpr int max_size = detail::digits10() + 2; - char buffer[max_size > 5 ? static_cast(max_size) : 5]; - char* begin = buffer; - return std::string(begin, detail::write(begin, value)); -} - -template -FMT_NODISCARD auto to_string(const basic_memory_buffer& buf) - -> std::basic_string { - auto size = buf.size(); - detail::assume(size < std::basic_string().max_size()); - return std::basic_string(buf.data(), size); -} - -template ::value && - detail::has_format_as::value)> -inline auto to_string(const T& value) -> std::string { - return to_string(format_as(value)); -} - -FMT_END_EXPORT - -namespace detail { - -template -void vformat_to(buffer& buf, basic_string_view fmt, - typename vformat_args::type args, locale_ref loc) { - auto out = basic_appender(buf); - if (fmt.size() == 2 && equal2(fmt.data(), "{}")) { - auto arg = args.get(0); - if (!arg) report_error("argument not found"); - arg.visit(default_arg_formatter{out, args, loc}); - return; - } - - struct format_handler { - basic_format_parse_context parse_context; - buffered_context context; - - format_handler(basic_appender p_out, basic_string_view str, - basic_format_args> p_args, - locale_ref p_loc) - : parse_context(str), context(p_out, p_args, p_loc) {} - - void on_text(const Char* begin, const Char* end) { - auto text = basic_string_view(begin, to_unsigned(end - begin)); - context.advance_to(write(context.out(), text)); - } - - FMT_CONSTEXPR auto on_arg_id() -> int { - return parse_context.next_arg_id(); - } - FMT_CONSTEXPR auto on_arg_id(int id) -> int { - parse_context.check_arg_id(id); - return id; - } - FMT_CONSTEXPR auto on_arg_id(basic_string_view id) -> int { - parse_context.check_arg_id(id); - int arg_id = context.arg_id(id); - if (arg_id < 0) report_error("argument not found"); - return arg_id; - } - - FMT_INLINE void on_replacement_field(int id, const Char*) { - auto arg = get_arg(context, id); - context.advance_to(arg.visit(default_arg_formatter{ - context.out(), context.args(), context.locale()})); - } - - auto on_format_specs(int id, const Char* begin, const Char* end) - -> const Char* { - auto arg = get_arg(context, id); - // Not using a visitor for custom types gives better codegen. - if (arg.format_custom(begin, parse_context, context)) - return parse_context.begin(); - auto specs = detail::dynamic_format_specs(); - begin = parse_format_specs(begin, end, specs, parse_context, arg.type()); - detail::handle_dynamic_spec( - specs.width, specs.width_ref, context); - detail::handle_dynamic_spec( - specs.precision, specs.precision_ref, context); - if (begin == end || *begin != '}') - report_error("missing '}' in format string"); - context.advance_to(arg.visit( - arg_formatter{context.out(), specs, context.locale()})); - return begin; - } - - FMT_NORETURN void on_error(const char* message) { report_error(message); } - }; - detail::parse_format_string(fmt, format_handler(out, fmt, args, loc)); -} - -FMT_BEGIN_EXPORT - -#ifndef FMT_HEADER_ONLY -extern template FMT_API void vformat_to(buffer&, string_view, - typename vformat_args<>::type, - locale_ref); -extern template FMT_API auto thousands_sep_impl(locale_ref) - -> thousands_sep_result; -extern template FMT_API auto thousands_sep_impl(locale_ref) - -> thousands_sep_result; -extern template FMT_API auto decimal_point_impl(locale_ref) -> char; -extern template FMT_API auto decimal_point_impl(locale_ref) -> wchar_t; -#endif // FMT_HEADER_ONLY - -FMT_END_EXPORT - -template -template -FMT_CONSTEXPR FMT_INLINE auto native_formatter::format( - const T& val, FormatContext& ctx) const -> decltype(ctx.out()) { - if (specs_.width_ref.kind == arg_id_kind::none && - specs_.precision_ref.kind == arg_id_kind::none) { - return write(ctx.out(), val, specs_, ctx.locale()); - } - auto specs = specs_; - handle_dynamic_spec(specs.width, specs.width_ref, ctx); - handle_dynamic_spec(specs.precision, specs.precision_ref, - ctx); - return write(ctx.out(), val, specs, ctx.locale()); -} - -} // namespace detail - -FMT_BEGIN_EXPORT - -template -struct formatter - : detail::native_formatter {}; - -#if FMT_USE_USER_DEFINED_LITERALS -inline namespace literals { -/** - * User-defined literal equivalent of `fmt::arg`. - * - * **Example**: - * - * using namespace fmt::literals; - * fmt::print("The answer is {answer}.", "answer"_a=42); - */ -# if FMT_USE_NONTYPE_TEMPLATE_ARGS -template constexpr auto operator""_a() { - using char_t = remove_cvref_t; - return detail::udl_arg(); -} -# else -constexpr auto operator""_a(const char* s, size_t) -> detail::udl_arg { - return {s}; -} -# endif -} // namespace literals -#endif // FMT_USE_USER_DEFINED_LITERALS - -FMT_API auto vformat(string_view fmt, format_args args) -> std::string; - -/** - * Formats `args` according to specifications in `fmt` and returns the result - * as a string. - * - * **Example**: - * - * #include - * std::string message = fmt::format("The answer is {}.", 42); - */ -template -FMT_NODISCARD FMT_INLINE auto format(format_string fmt, T&&... args) - -> std::string { - return vformat(fmt, fmt::make_format_args(args...)); -} - -template ::value)> -inline auto vformat(const Locale& loc, string_view fmt, format_args args) - -> std::string { - return detail::vformat(loc, fmt, args); -} - -template ::value)> -inline auto format(const Locale& loc, format_string fmt, T&&... args) - -> std::string { - return fmt::vformat(loc, string_view(fmt), fmt::make_format_args(args...)); -} - -template ::value&& - detail::is_locale::value)> -auto vformat_to(OutputIt out, const Locale& loc, string_view fmt, - format_args args) -> OutputIt { - using detail::get_buffer; - auto&& buf = get_buffer(out); - detail::vformat_to(buf, fmt, args, detail::locale_ref(loc)); - return detail::get_iterator(buf, out); -} - -template ::value&& - detail::is_locale::value)> -FMT_INLINE auto format_to(OutputIt out, const Locale& loc, - format_string fmt, T&&... args) -> OutputIt { - return vformat_to(out, loc, fmt, fmt::make_format_args(args...)); -} - -template ::value)> -FMT_NODISCARD FMT_INLINE auto formatted_size(const Locale& loc, - format_string fmt, - T&&... args) -> size_t { - auto buf = detail::counting_buffer<>(); - detail::vformat_to(buf, fmt, fmt::make_format_args(args...), - detail::locale_ref(loc)); - return buf.count(); -} - -FMT_END_EXPORT - -FMT_END_NAMESPACE - -#ifdef FMT_HEADER_ONLY -# define FMT_FUNC inline -# include "format-inl.h" -#else -# define FMT_FUNC -#endif - -// Restore _LIBCPP_REMOVE_TRANSITIVE_INCLUDES. -#ifdef FMT_REMOVE_TRANSITIVE_INCLUDES -# undef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES -#endif - -#endif // FMT_FORMAT_H_ diff --git a/include/spdlog/fmt/bundled/locale.h b/include/spdlog/fmt/bundled/locale.h deleted file mode 100644 index 7571b52..0000000 --- a/include/spdlog/fmt/bundled/locale.h +++ /dev/null @@ -1,2 +0,0 @@ -#include "xchar.h" -#warning fmt/locale.h is deprecated, include fmt/format.h or fmt/xchar.h instead diff --git a/include/spdlog/fmt/bundled/os.h b/include/spdlog/fmt/bundled/os.h deleted file mode 100644 index 5c85ea0..0000000 --- a/include/spdlog/fmt/bundled/os.h +++ /dev/null @@ -1,439 +0,0 @@ -// Formatting library for C++ - optional OS-specific functionality -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_OS_H_ -#define FMT_OS_H_ - -#include "format.h" - -#ifndef FMT_MODULE -# include -# include -# include -# include // std::system_error - -# if FMT_HAS_INCLUDE() -# include // LC_NUMERIC_MASK on macOS -# endif -#endif // FMT_MODULE - -#ifndef FMT_USE_FCNTL -// UWP doesn't provide _pipe. -# if FMT_HAS_INCLUDE("winapifamily.h") -# include -# endif -# if (FMT_HAS_INCLUDE() || defined(__APPLE__) || \ - defined(__linux__)) && \ - (!defined(WINAPI_FAMILY) || \ - (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)) -# include // for O_RDONLY -# define FMT_USE_FCNTL 1 -# else -# define FMT_USE_FCNTL 0 -# endif -#endif - -#ifndef FMT_POSIX -# if defined(_WIN32) && !defined(__MINGW32__) -// Fix warnings about deprecated symbols. -# define FMT_POSIX(call) _##call -# else -# define FMT_POSIX(call) call -# endif -#endif - -// Calls to system functions are wrapped in FMT_SYSTEM for testability. -#ifdef FMT_SYSTEM -# define FMT_HAS_SYSTEM -# define FMT_POSIX_CALL(call) FMT_SYSTEM(call) -#else -# define FMT_SYSTEM(call) ::call -# ifdef _WIN32 -// Fix warnings about deprecated symbols. -# define FMT_POSIX_CALL(call) ::_##call -# else -# define FMT_POSIX_CALL(call) ::call -# endif -#endif - -// Retries the expression while it evaluates to error_result and errno -// equals to EINTR. -#ifndef _WIN32 -# define FMT_RETRY_VAL(result, expression, error_result) \ - do { \ - (result) = (expression); \ - } while ((result) == (error_result) && errno == EINTR) -#else -# define FMT_RETRY_VAL(result, expression, error_result) result = (expression) -#endif - -#define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1) - -FMT_BEGIN_NAMESPACE -FMT_BEGIN_EXPORT - -/** - * A reference to a null-terminated string. It can be constructed from a C - * string or `std::string`. - * - * You can use one of the following type aliases for common character types: - * - * +---------------+-----------------------------+ - * | Type | Definition | - * +===============+=============================+ - * | cstring_view | basic_cstring_view | - * +---------------+-----------------------------+ - * | wcstring_view | basic_cstring_view | - * +---------------+-----------------------------+ - * - * This class is most useful as a parameter type for functions that wrap C APIs. - */ -template class basic_cstring_view { - private: - const Char* data_; - - public: - /// Constructs a string reference object from a C string. - basic_cstring_view(const Char* s) : data_(s) {} - - /// Constructs a string reference from an `std::string` object. - basic_cstring_view(const std::basic_string& s) : data_(s.c_str()) {} - - /// Returns the pointer to a C string. - auto c_str() const -> const Char* { return data_; } -}; - -using cstring_view = basic_cstring_view; -using wcstring_view = basic_cstring_view; - -#ifdef _WIN32 -FMT_API const std::error_category& system_category() noexcept; - -namespace detail { -FMT_API void format_windows_error(buffer& out, int error_code, - const char* message) noexcept; -} - -FMT_API std::system_error vwindows_error(int error_code, string_view format_str, - format_args args); - -/** - * Constructs a `std::system_error` object with the description of the form - * - * : - * - * where `` is the formatted message and `` is the - * system message corresponding to the error code. - * `error_code` is a Windows error code as given by `GetLastError`. - * If `error_code` is not a valid error code such as -1, the system message - * will look like "error -1". - * - * **Example**: - * - * // This throws a system_error with the description - * // cannot open file 'madeup': The system cannot find the file - * specified. - * // or similar (system message may vary). - * const char *filename = "madeup"; - * LPOFSTRUCT of = LPOFSTRUCT(); - * HFILE file = OpenFile(filename, &of, OF_READ); - * if (file == HFILE_ERROR) { - * throw fmt::windows_error(GetLastError(), - * "cannot open file '{}'", filename); - * } - */ -template -std::system_error windows_error(int error_code, string_view message, - const Args&... args) { - return vwindows_error(error_code, message, fmt::make_format_args(args...)); -} - -// Reports a Windows error without throwing an exception. -// Can be used to report errors from destructors. -FMT_API void report_windows_error(int error_code, const char* message) noexcept; -#else -inline auto system_category() noexcept -> const std::error_category& { - return std::system_category(); -} -#endif // _WIN32 - -// std::system is not available on some platforms such as iOS (#2248). -#ifdef __OSX__ -template > -void say(const S& format_str, Args&&... args) { - std::system(format("say \"{}\"", format(format_str, args...)).c_str()); -} -#endif - -// A buffered file. -class buffered_file { - private: - FILE* file_; - - friend class file; - - explicit buffered_file(FILE* f) : file_(f) {} - - public: - buffered_file(const buffered_file&) = delete; - void operator=(const buffered_file&) = delete; - - // Constructs a buffered_file object which doesn't represent any file. - buffered_file() noexcept : file_(nullptr) {} - - // Destroys the object closing the file it represents if any. - FMT_API ~buffered_file() noexcept; - - public: - buffered_file(buffered_file&& other) noexcept : file_(other.file_) { - other.file_ = nullptr; - } - - auto operator=(buffered_file&& other) -> buffered_file& { - close(); - file_ = other.file_; - other.file_ = nullptr; - return *this; - } - - // Opens a file. - FMT_API buffered_file(cstring_view filename, cstring_view mode); - - // Closes the file. - FMT_API void close(); - - // Returns the pointer to a FILE object representing this file. - auto get() const noexcept -> FILE* { return file_; } - - FMT_API auto descriptor() const -> int; - - template - inline void print(string_view fmt, const T&... args) { - const auto& vargs = fmt::make_format_args(args...); - detail::is_locking() ? fmt::vprint_buffered(file_, fmt, vargs) - : fmt::vprint(file_, fmt, vargs); - } -}; - -#if FMT_USE_FCNTL - -// A file. Closed file is represented by a file object with descriptor -1. -// Methods that are not declared with noexcept may throw -// fmt::system_error in case of failure. Note that some errors such as -// closing the file multiple times will cause a crash on Windows rather -// than an exception. You can get standard behavior by overriding the -// invalid parameter handler with _set_invalid_parameter_handler. -class FMT_API file { - private: - int fd_; // File descriptor. - - // Constructs a file object with a given descriptor. - explicit file(int fd) : fd_(fd) {} - - friend struct pipe; - - public: - // Possible values for the oflag argument to the constructor. - enum { - RDONLY = FMT_POSIX(O_RDONLY), // Open for reading only. - WRONLY = FMT_POSIX(O_WRONLY), // Open for writing only. - RDWR = FMT_POSIX(O_RDWR), // Open for reading and writing. - CREATE = FMT_POSIX(O_CREAT), // Create if the file doesn't exist. - APPEND = FMT_POSIX(O_APPEND), // Open in append mode. - TRUNC = FMT_POSIX(O_TRUNC) // Truncate the content of the file. - }; - - // Constructs a file object which doesn't represent any file. - file() noexcept : fd_(-1) {} - - // Opens a file and constructs a file object representing this file. - file(cstring_view path, int oflag); - - public: - file(const file&) = delete; - void operator=(const file&) = delete; - - file(file&& other) noexcept : fd_(other.fd_) { other.fd_ = -1; } - - // Move assignment is not noexcept because close may throw. - auto operator=(file&& other) -> file& { - close(); - fd_ = other.fd_; - other.fd_ = -1; - return *this; - } - - // Destroys the object closing the file it represents if any. - ~file() noexcept; - - // Returns the file descriptor. - auto descriptor() const noexcept -> int { return fd_; } - - // Closes the file. - void close(); - - // Returns the file size. The size has signed type for consistency with - // stat::st_size. - auto size() const -> long long; - - // Attempts to read count bytes from the file into the specified buffer. - auto read(void* buffer, size_t count) -> size_t; - - // Attempts to write count bytes from the specified buffer to the file. - auto write(const void* buffer, size_t count) -> size_t; - - // Duplicates a file descriptor with the dup function and returns - // the duplicate as a file object. - static auto dup(int fd) -> file; - - // Makes fd be the copy of this file descriptor, closing fd first if - // necessary. - void dup2(int fd); - - // Makes fd be the copy of this file descriptor, closing fd first if - // necessary. - void dup2(int fd, std::error_code& ec) noexcept; - - // Creates a buffered_file object associated with this file and detaches - // this file object from the file. - auto fdopen(const char* mode) -> buffered_file; - -# if defined(_WIN32) && !defined(__MINGW32__) - // Opens a file and constructs a file object representing this file by - // wcstring_view filename. Windows only. - static file open_windows_file(wcstring_view path, int oflag); -# endif -}; - -struct FMT_API pipe { - file read_end; - file write_end; - - // Creates a pipe setting up read_end and write_end file objects for reading - // and writing respectively. - pipe(); -}; - -// Returns the memory page size. -auto getpagesize() -> long; - -namespace detail { - -struct buffer_size { - buffer_size() = default; - size_t value = 0; - auto operator=(size_t val) const -> buffer_size { - auto bs = buffer_size(); - bs.value = val; - return bs; - } -}; - -struct ostream_params { - int oflag = file::WRONLY | file::CREATE | file::TRUNC; - size_t buffer_size = BUFSIZ > 32768 ? BUFSIZ : 32768; - - ostream_params() {} - - template - ostream_params(T... params, int new_oflag) : ostream_params(params...) { - oflag = new_oflag; - } - - template - ostream_params(T... params, detail::buffer_size bs) - : ostream_params(params...) { - this->buffer_size = bs.value; - } - -// Intel has a bug that results in failure to deduce a constructor -// for empty parameter packs. -# if defined(__INTEL_COMPILER) && __INTEL_COMPILER < 2000 - ostream_params(int new_oflag) : oflag(new_oflag) {} - ostream_params(detail::buffer_size bs) : buffer_size(bs.value) {} -# endif -}; - -class file_buffer final : public buffer { - private: - file file_; - - FMT_API static void grow(buffer& buf, size_t); - - public: - FMT_API file_buffer(cstring_view path, const ostream_params& params); - FMT_API file_buffer(file_buffer&& other) noexcept; - FMT_API ~file_buffer(); - - void flush() { - if (size() == 0) return; - file_.write(data(), size() * sizeof(data()[0])); - clear(); - } - - void close() { - flush(); - file_.close(); - } -}; - -} // namespace detail - -constexpr auto buffer_size = detail::buffer_size(); - -/// A fast output stream for writing from a single thread. Writing from -/// multiple threads without external synchronization may result in a data race. -class FMT_API ostream { - private: - FMT_MSC_WARNING(suppress : 4251) - detail::file_buffer buffer_; - - ostream(cstring_view path, const detail::ostream_params& params) - : buffer_(path, params) {} - - public: - ostream(ostream&& other) : buffer_(std::move(other.buffer_)) {} - - ~ostream(); - - void flush() { buffer_.flush(); } - - template - friend auto output_file(cstring_view path, T... params) -> ostream; - - void close() { buffer_.close(); } - - /// Formats `args` according to specifications in `fmt` and writes the - /// output to the file. - template void print(format_string fmt, T&&... args) { - vformat_to(appender(buffer_), fmt, fmt::make_format_args(args...)); - } -}; - -/** - * Opens a file for writing. Supported parameters passed in `params`: - * - * - ``: Flags passed to [open]( - * https://pubs.opengroup.org/onlinepubs/007904875/functions/open.html) - * (`file::WRONLY | file::CREATE | file::TRUNC` by default) - * - `buffer_size=`: Output buffer size - * - * **Example**: - * - * auto out = fmt::output_file("guide.txt"); - * out.print("Don't {}", "Panic"); - */ -template -inline auto output_file(cstring_view path, T... params) -> ostream { - return {path, detail::ostream_params(params...)}; -} -#endif // FMT_USE_FCNTL - -FMT_END_EXPORT -FMT_END_NAMESPACE - -#endif // FMT_OS_H_ diff --git a/include/spdlog/fmt/bundled/ostream.h b/include/spdlog/fmt/bundled/ostream.h deleted file mode 100644 index 98faef6..0000000 --- a/include/spdlog/fmt/bundled/ostream.h +++ /dev/null @@ -1,211 +0,0 @@ -// Formatting library for C++ - std::ostream support -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_OSTREAM_H_ -#define FMT_OSTREAM_H_ - -#ifndef FMT_MODULE -# include // std::filebuf -#endif - -#ifdef _WIN32 -# ifdef __GLIBCXX__ -# include -# include -# endif -# include -#endif - -#include "chrono.h" // formatbuf - -FMT_BEGIN_NAMESPACE -namespace detail { - -// Generate a unique explicit instantion in every translation unit using a tag -// type in an anonymous namespace. -namespace { -struct file_access_tag {}; -} // namespace -template -class file_access { - friend auto get_file(BufType& obj) -> FILE* { return obj.*FileMemberPtr; } -}; - -#if FMT_MSC_VERSION -template class file_access; -auto get_file(std::filebuf&) -> FILE*; -#endif - -inline auto write_ostream_unicode(std::ostream& os, fmt::string_view data) - -> bool { - FILE* f = nullptr; -#if FMT_MSC_VERSION && FMT_USE_RTTI - if (auto* buf = dynamic_cast(os.rdbuf())) - f = get_file(*buf); - else - return false; -#elif defined(_WIN32) && defined(__GLIBCXX__) && FMT_USE_RTTI - auto* rdbuf = os.rdbuf(); - if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf*>(rdbuf)) - f = sfbuf->file(); - else if (auto* fbuf = dynamic_cast<__gnu_cxx::stdio_filebuf*>(rdbuf)) - f = fbuf->file(); - else - return false; -#else - ignore_unused(os, data, f); -#endif -#ifdef _WIN32 - if (f) { - int fd = _fileno(f); - if (_isatty(fd)) { - os.flush(); - return write_console(fd, data); - } - } -#endif - return false; -} -inline auto write_ostream_unicode(std::wostream&, - fmt::basic_string_view) -> bool { - return false; -} - -// Write the content of buf to os. -// It is a separate function rather than a part of vprint to simplify testing. -template -void write_buffer(std::basic_ostream& os, buffer& buf) { - const Char* buf_data = buf.data(); - using unsigned_streamsize = std::make_unsigned::type; - unsigned_streamsize size = buf.size(); - unsigned_streamsize max_size = to_unsigned(max_value()); - do { - unsigned_streamsize n = size <= max_size ? size : max_size; - os.write(buf_data, static_cast(n)); - buf_data += n; - size -= n; - } while (size != 0); -} - -template -void format_value(buffer& buf, const T& value) { - auto&& format_buf = formatbuf>(buf); - auto&& output = std::basic_ostream(&format_buf); -#if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) - output.imbue(std::locale::classic()); // The default is always unlocalized. -#endif - output << value; - output.exceptions(std::ios_base::failbit | std::ios_base::badbit); -} - -template struct streamed_view { - const T& value; -}; - -} // namespace detail - -// Formats an object of type T that has an overloaded ostream operator<<. -template -struct basic_ostream_formatter : formatter, Char> { - void set_debug_format() = delete; - - template - auto format(const T& value, Context& ctx) const -> decltype(ctx.out()) { - auto buffer = basic_memory_buffer(); - detail::format_value(buffer, value); - return formatter, Char>::format( - {buffer.data(), buffer.size()}, ctx); - } -}; - -using ostream_formatter = basic_ostream_formatter; - -template -struct formatter, Char> - : basic_ostream_formatter { - template - auto format(detail::streamed_view view, Context& ctx) const - -> decltype(ctx.out()) { - return basic_ostream_formatter::format(view.value, ctx); - } -}; - -/** - * Returns a view that formats `value` via an ostream `operator<<`. - * - * **Example**: - * - * fmt::print("Current thread id: {}\n", - * fmt::streamed(std::this_thread::get_id())); - */ -template -constexpr auto streamed(const T& value) -> detail::streamed_view { - return {value}; -} - -namespace detail { - -inline void vprint_directly(std::ostream& os, string_view format_str, - format_args args) { - auto buffer = memory_buffer(); - detail::vformat_to(buffer, format_str, args); - detail::write_buffer(os, buffer); -} - -} // namespace detail - -FMT_EXPORT template -void vprint(std::basic_ostream& os, - basic_string_view> format_str, - typename detail::vformat_args::type args) { - auto buffer = basic_memory_buffer(); - detail::vformat_to(buffer, format_str, args); - if (detail::write_ostream_unicode(os, {buffer.data(), buffer.size()})) return; - detail::write_buffer(os, buffer); -} - -/** - * Prints formatted data to the stream `os`. - * - * **Example**: - * - * fmt::print(cerr, "Don't {}!", "panic"); - */ -FMT_EXPORT template -void print(std::ostream& os, format_string fmt, T&&... args) { - const auto& vargs = fmt::make_format_args(args...); - if (detail::use_utf8()) - vprint(os, fmt, vargs); - else - detail::vprint_directly(os, fmt, vargs); -} - -FMT_EXPORT -template -void print(std::wostream& os, - basic_format_string...> fmt, - Args&&... args) { - vprint(os, fmt, fmt::make_format_args>(args...)); -} - -FMT_EXPORT template -void println(std::ostream& os, format_string fmt, T&&... args) { - fmt::print(os, "{}\n", fmt::format(fmt, std::forward(args)...)); -} - -FMT_EXPORT -template -void println(std::wostream& os, - basic_format_string...> fmt, - Args&&... args) { - print(os, L"{}\n", fmt::format(fmt, std::forward(args)...)); -} - -FMT_END_NAMESPACE - -#endif // FMT_OSTREAM_H_ diff --git a/include/spdlog/fmt/bundled/printf.h b/include/spdlog/fmt/bundled/printf.h deleted file mode 100644 index 072cc6b..0000000 --- a/include/spdlog/fmt/bundled/printf.h +++ /dev/null @@ -1,656 +0,0 @@ -// Formatting library for C++ - legacy printf implementation -// -// Copyright (c) 2012 - 2016, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_PRINTF_H_ -#define FMT_PRINTF_H_ - -#ifndef FMT_MODULE -# include // std::max -# include // std::numeric_limits -#endif - -#include "format.h" - -FMT_BEGIN_NAMESPACE -FMT_BEGIN_EXPORT - -template struct printf_formatter { - printf_formatter() = delete; -}; - -template class basic_printf_context { - private: - basic_appender out_; - basic_format_args args_; - - static_assert(std::is_same::value || - std::is_same::value, - "Unsupported code unit type."); - - public: - using char_type = Char; - using parse_context_type = basic_format_parse_context; - template using formatter_type = printf_formatter; - - /// Constructs a `printf_context` object. References to the arguments are - /// stored in the context object so make sure they have appropriate lifetimes. - basic_printf_context(basic_appender out, - basic_format_args args) - : out_(out), args_(args) {} - - auto out() -> basic_appender { return out_; } - void advance_to(basic_appender) {} - - auto locale() -> detail::locale_ref { return {}; } - - auto arg(int id) const -> basic_format_arg { - return args_.get(id); - } -}; - -namespace detail { - -// Checks if a value fits in int - used to avoid warnings about comparing -// signed and unsigned integers. -template struct int_checker { - template static auto fits_in_int(T value) -> bool { - unsigned max = to_unsigned(max_value()); - return value <= max; - } - static auto fits_in_int(bool) -> bool { return true; } -}; - -template <> struct int_checker { - template static auto fits_in_int(T value) -> bool { - return value >= (std::numeric_limits::min)() && - value <= max_value(); - } - static auto fits_in_int(int) -> bool { return true; } -}; - -struct printf_precision_handler { - template ::value)> - auto operator()(T value) -> int { - if (!int_checker::is_signed>::fits_in_int(value)) - report_error("number is too big"); - return (std::max)(static_cast(value), 0); - } - - template ::value)> - auto operator()(T) -> int { - report_error("precision is not integer"); - return 0; - } -}; - -// An argument visitor that returns true iff arg is a zero integer. -struct is_zero_int { - template ::value)> - auto operator()(T value) -> bool { - return value == 0; - } - - template ::value)> - auto operator()(T) -> bool { - return false; - } -}; - -template struct make_unsigned_or_bool : std::make_unsigned {}; - -template <> struct make_unsigned_or_bool { - using type = bool; -}; - -template class arg_converter { - private: - using char_type = typename Context::char_type; - - basic_format_arg& arg_; - char_type type_; - - public: - arg_converter(basic_format_arg& arg, char_type type) - : arg_(arg), type_(type) {} - - void operator()(bool value) { - if (type_ != 's') operator()(value); - } - - template ::value)> - void operator()(U value) { - bool is_signed = type_ == 'd' || type_ == 'i'; - using target_type = conditional_t::value, U, T>; - if (const_check(sizeof(target_type) <= sizeof(int))) { - // Extra casts are used to silence warnings. - if (is_signed) { - auto n = static_cast(static_cast(value)); - arg_ = detail::make_arg(n); - } else { - using unsigned_type = typename make_unsigned_or_bool::type; - auto n = static_cast(static_cast(value)); - arg_ = detail::make_arg(n); - } - } else { - if (is_signed) { - // glibc's printf doesn't sign extend arguments of smaller types: - // std::printf("%lld", -42); // prints "4294967254" - // but we don't have to do the same because it's a UB. - auto n = static_cast(value); - arg_ = detail::make_arg(n); - } else { - auto n = static_cast::type>(value); - arg_ = detail::make_arg(n); - } - } - } - - template ::value)> - void operator()(U) {} // No conversion needed for non-integral types. -}; - -// Converts an integer argument to T for printf, if T is an integral type. -// If T is void, the argument is converted to corresponding signed or unsigned -// type depending on the type specifier: 'd' and 'i' - signed, other - -// unsigned). -template -void convert_arg(basic_format_arg& arg, Char type) { - arg.visit(arg_converter(arg, type)); -} - -// Converts an integer argument to char for printf. -template class char_converter { - private: - basic_format_arg& arg_; - - public: - explicit char_converter(basic_format_arg& arg) : arg_(arg) {} - - template ::value)> - void operator()(T value) { - auto c = static_cast(value); - arg_ = detail::make_arg(c); - } - - template ::value)> - void operator()(T) {} // No conversion needed for non-integral types. -}; - -// An argument visitor that return a pointer to a C string if argument is a -// string or null otherwise. -template struct get_cstring { - template auto operator()(T) -> const Char* { return nullptr; } - auto operator()(const Char* s) -> const Char* { return s; } -}; - -// Checks if an argument is a valid printf width specifier and sets -// left alignment if it is negative. -class printf_width_handler { - private: - format_specs& specs_; - - public: - explicit printf_width_handler(format_specs& specs) : specs_(specs) {} - - template ::value)> - auto operator()(T value) -> unsigned { - auto width = static_cast>(value); - if (detail::is_negative(value)) { - specs_.align = align::left; - width = 0 - width; - } - unsigned int_max = to_unsigned(max_value()); - if (width > int_max) report_error("number is too big"); - return static_cast(width); - } - - template ::value)> - auto operator()(T) -> unsigned { - report_error("width is not integer"); - return 0; - } -}; - -// Workaround for a bug with the XL compiler when initializing -// printf_arg_formatter's base class. -template -auto make_arg_formatter(basic_appender iter, format_specs& s) - -> arg_formatter { - return {iter, s, locale_ref()}; -} - -// The `printf` argument formatter. -template -class printf_arg_formatter : public arg_formatter { - private: - using base = arg_formatter; - using context_type = basic_printf_context; - - context_type& context_; - - void write_null_pointer(bool is_string = false) { - auto s = this->specs; - s.type = presentation_type::none; - write_bytes(this->out, is_string ? "(null)" : "(nil)", s); - } - - public: - printf_arg_formatter(basic_appender iter, format_specs& s, - context_type& ctx) - : base(make_arg_formatter(iter, s)), context_(ctx) {} - - void operator()(monostate value) { base::operator()(value); } - - template ::value)> - void operator()(T value) { - // MSVC2013 fails to compile separate overloads for bool and Char so use - // std::is_same instead. - if (!std::is_same::value) { - base::operator()(value); - return; - } - format_specs s = this->specs; - if (s.type != presentation_type::none && s.type != presentation_type::chr) { - return (*this)(static_cast(value)); - } - s.sign = sign::none; - s.alt = false; - s.fill = ' '; // Ignore '0' flag for char types. - // align::numeric needs to be overwritten here since the '0' flag is - // ignored for non-numeric types - if (s.align == align::none || s.align == align::numeric) - s.align = align::right; - write(this->out, static_cast(value), s); - } - - template ::value)> - void operator()(T value) { - base::operator()(value); - } - - void operator()(const char* value) { - if (value) - base::operator()(value); - else - write_null_pointer(this->specs.type != presentation_type::pointer); - } - - void operator()(const wchar_t* value) { - if (value) - base::operator()(value); - else - write_null_pointer(this->specs.type != presentation_type::pointer); - } - - void operator()(basic_string_view value) { base::operator()(value); } - - void operator()(const void* value) { - if (value) - base::operator()(value); - else - write_null_pointer(); - } - - void operator()(typename basic_format_arg::handle handle) { - auto parse_ctx = basic_format_parse_context({}); - handle.format(parse_ctx, context_); - } -}; - -template -void parse_flags(format_specs& specs, const Char*& it, const Char* end) { - for (; it != end; ++it) { - switch (*it) { - case '-': - specs.align = align::left; - break; - case '+': - specs.sign = sign::plus; - break; - case '0': - specs.fill = '0'; - break; - case ' ': - if (specs.sign != sign::plus) specs.sign = sign::space; - break; - case '#': - specs.alt = true; - break; - default: - return; - } - } -} - -template -auto parse_header(const Char*& it, const Char* end, format_specs& specs, - GetArg get_arg) -> int { - int arg_index = -1; - Char c = *it; - if (c >= '0' && c <= '9') { - // Parse an argument index (if followed by '$') or a width possibly - // preceded with '0' flag(s). - int value = parse_nonnegative_int(it, end, -1); - if (it != end && *it == '$') { // value is an argument index - ++it; - arg_index = value != -1 ? value : max_value(); - } else { - if (c == '0') specs.fill = '0'; - if (value != 0) { - // Nonzero value means that we parsed width and don't need to - // parse it or flags again, so return now. - if (value == -1) report_error("number is too big"); - specs.width = value; - return arg_index; - } - } - } - parse_flags(specs, it, end); - // Parse width. - if (it != end) { - if (*it >= '0' && *it <= '9') { - specs.width = parse_nonnegative_int(it, end, -1); - if (specs.width == -1) report_error("number is too big"); - } else if (*it == '*') { - ++it; - specs.width = static_cast( - get_arg(-1).visit(detail::printf_width_handler(specs))); - } - } - return arg_index; -} - -inline auto parse_printf_presentation_type(char c, type t, bool& upper) - -> presentation_type { - using pt = presentation_type; - constexpr auto integral_set = sint_set | uint_set | bool_set | char_set; - switch (c) { - case 'd': - return in(t, integral_set) ? pt::dec : pt::none; - case 'o': - return in(t, integral_set) ? pt::oct : pt::none; - case 'X': - upper = true; - FMT_FALLTHROUGH; - case 'x': - return in(t, integral_set) ? pt::hex : pt::none; - case 'E': - upper = true; - FMT_FALLTHROUGH; - case 'e': - return in(t, float_set) ? pt::exp : pt::none; - case 'F': - upper = true; - FMT_FALLTHROUGH; - case 'f': - return in(t, float_set) ? pt::fixed : pt::none; - case 'G': - upper = true; - FMT_FALLTHROUGH; - case 'g': - return in(t, float_set) ? pt::general : pt::none; - case 'A': - upper = true; - FMT_FALLTHROUGH; - case 'a': - return in(t, float_set) ? pt::hexfloat : pt::none; - case 'c': - return in(t, integral_set) ? pt::chr : pt::none; - case 's': - return in(t, string_set | cstring_set) ? pt::string : pt::none; - case 'p': - return in(t, pointer_set | cstring_set) ? pt::pointer : pt::none; - default: - return pt::none; - } -} - -template -void vprintf(buffer& buf, basic_string_view format, - basic_format_args args) { - using iterator = basic_appender; - auto out = iterator(buf); - auto context = basic_printf_context(out, args); - auto parse_ctx = basic_format_parse_context(format); - - // Returns the argument with specified index or, if arg_index is -1, the next - // argument. - auto get_arg = [&](int arg_index) { - if (arg_index < 0) - arg_index = parse_ctx.next_arg_id(); - else - parse_ctx.check_arg_id(--arg_index); - return detail::get_arg(context, arg_index); - }; - - const Char* start = parse_ctx.begin(); - const Char* end = parse_ctx.end(); - auto it = start; - while (it != end) { - if (!find(it, end, '%', it)) { - it = end; // find leaves it == nullptr if it doesn't find '%'. - break; - } - Char c = *it++; - if (it != end && *it == c) { - write(out, basic_string_view(start, to_unsigned(it - start))); - start = ++it; - continue; - } - write(out, basic_string_view(start, to_unsigned(it - 1 - start))); - - auto specs = format_specs(); - specs.align = align::right; - - // Parse argument index, flags and width. - int arg_index = parse_header(it, end, specs, get_arg); - if (arg_index == 0) report_error("argument not found"); - - // Parse precision. - if (it != end && *it == '.') { - ++it; - c = it != end ? *it : 0; - if ('0' <= c && c <= '9') { - specs.precision = parse_nonnegative_int(it, end, 0); - } else if (c == '*') { - ++it; - specs.precision = - static_cast(get_arg(-1).visit(printf_precision_handler())); - } else { - specs.precision = 0; - } - } - - auto arg = get_arg(arg_index); - // For d, i, o, u, x, and X conversion specifiers, if a precision is - // specified, the '0' flag is ignored - if (specs.precision >= 0 && arg.is_integral()) { - // Ignore '0' for non-numeric types or if '-' present. - specs.fill = ' '; - } - if (specs.precision >= 0 && arg.type() == type::cstring_type) { - auto str = arg.visit(get_cstring()); - auto str_end = str + specs.precision; - auto nul = std::find(str, str_end, Char()); - auto sv = basic_string_view( - str, to_unsigned(nul != str_end ? nul - str : specs.precision)); - arg = make_arg>(sv); - } - if (specs.alt && arg.visit(is_zero_int())) specs.alt = false; - if (specs.fill.template get() == '0') { - if (arg.is_arithmetic() && specs.align != align::left) - specs.align = align::numeric; - else - specs.fill = ' '; // Ignore '0' flag for non-numeric types or if '-' - // flag is also present. - } - - // Parse length and convert the argument to the required type. - c = it != end ? *it++ : 0; - Char t = it != end ? *it : 0; - switch (c) { - case 'h': - if (t == 'h') { - ++it; - t = it != end ? *it : 0; - convert_arg(arg, t); - } else { - convert_arg(arg, t); - } - break; - case 'l': - if (t == 'l') { - ++it; - t = it != end ? *it : 0; - convert_arg(arg, t); - } else { - convert_arg(arg, t); - } - break; - case 'j': - convert_arg(arg, t); - break; - case 'z': - convert_arg(arg, t); - break; - case 't': - convert_arg(arg, t); - break; - case 'L': - // printf produces garbage when 'L' is omitted for long double, no - // need to do the same. - break; - default: - --it; - convert_arg(arg, c); - } - - // Parse type. - if (it == end) report_error("invalid format string"); - char type = static_cast(*it++); - if (arg.is_integral()) { - // Normalize type. - switch (type) { - case 'i': - case 'u': - type = 'd'; - break; - case 'c': - arg.visit(char_converter>(arg)); - break; - } - } - bool upper = false; - specs.type = parse_printf_presentation_type(type, arg.type(), upper); - if (specs.type == presentation_type::none) - report_error("invalid format specifier"); - specs.upper = upper; - - start = it; - - // Format argument. - arg.visit(printf_arg_formatter(out, specs, context)); - } - write(out, basic_string_view(start, to_unsigned(it - start))); -} -} // namespace detail - -using printf_context = basic_printf_context; -using wprintf_context = basic_printf_context; - -using printf_args = basic_format_args; -using wprintf_args = basic_format_args; - -/// Constructs an `format_arg_store` object that contains references to -/// arguments and can be implicitly converted to `printf_args`. -template -inline auto make_printf_args(T&... args) - -> decltype(fmt::make_format_args>(args...)) { - return fmt::make_format_args>(args...); -} - -template struct vprintf_args { - using type = basic_format_args>; -}; - -template -inline auto vsprintf(basic_string_view fmt, - typename vprintf_args::type args) - -> std::basic_string { - auto buf = basic_memory_buffer(); - detail::vprintf(buf, fmt, args); - return to_string(buf); -} - -/** - * Formats `args` according to specifications in `fmt` and returns the result - * as as string. - * - * **Example**: - * - * std::string message = fmt::sprintf("The answer is %d", 42); - */ -template > -inline auto sprintf(const S& fmt, const T&... args) -> std::basic_string { - return vsprintf(detail::to_string_view(fmt), - fmt::make_format_args>(args...)); -} - -template -inline auto vfprintf(std::FILE* f, basic_string_view fmt, - typename vprintf_args::type args) -> int { - auto buf = basic_memory_buffer(); - detail::vprintf(buf, fmt, args); - size_t size = buf.size(); - return std::fwrite(buf.data(), sizeof(Char), size, f) < size - ? -1 - : static_cast(size); -} - -/** - * Formats `args` according to specifications in `fmt` and writes the output - * to `f`. - * - * **Example**: - * - * fmt::fprintf(stderr, "Don't %s!", "panic"); - */ -template > -inline auto fprintf(std::FILE* f, const S& fmt, const T&... args) -> int { - return vfprintf(f, detail::to_string_view(fmt), - make_printf_args(args...)); -} - -template -FMT_DEPRECATED inline auto vprintf(basic_string_view fmt, - typename vprintf_args::type args) - -> int { - return vfprintf(stdout, fmt, args); -} - -/** - * Formats `args` according to specifications in `fmt` and writes the output - * to `stdout`. - * - * **Example**: - * - * fmt::printf("Elapsed time: %.2f seconds", 1.23); - */ -template -inline auto printf(string_view fmt, const T&... args) -> int { - return vfprintf(stdout, fmt, make_printf_args(args...)); -} -template -FMT_DEPRECATED inline auto printf(basic_string_view fmt, - const T&... args) -> int { - return vfprintf(stdout, fmt, make_printf_args(args...)); -} - -FMT_END_EXPORT -FMT_END_NAMESPACE - -#endif // FMT_PRINTF_H_ diff --git a/include/spdlog/fmt/bundled/ranges.h b/include/spdlog/fmt/bundled/ranges.h deleted file mode 100644 index 0d3dfbd..0000000 --- a/include/spdlog/fmt/bundled/ranges.h +++ /dev/null @@ -1,882 +0,0 @@ -// Formatting library for C++ - range and tuple support -// -// Copyright (c) 2012 - present, Victor Zverovich and {fmt} contributors -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_RANGES_H_ -#define FMT_RANGES_H_ - -#ifndef FMT_MODULE -# include -# include -# include -# include -# include -# include -#endif - -#include "format.h" - -FMT_BEGIN_NAMESPACE - -FMT_EXPORT -enum class range_format { disabled, map, set, sequence, string, debug_string }; - -namespace detail { - -template class is_map { - template static auto check(U*) -> typename U::mapped_type; - template static void check(...); - - public: - static constexpr const bool value = - !std::is_void(nullptr))>::value; -}; - -template class is_set { - template static auto check(U*) -> typename U::key_type; - template static void check(...); - - public: - static constexpr const bool value = - !std::is_void(nullptr))>::value && !is_map::value; -}; - -template struct conditional_helper {}; - -template struct is_range_ : std::false_type {}; - -#if !FMT_MSC_VERSION || FMT_MSC_VERSION > 1800 - -# define FMT_DECLTYPE_RETURN(val) \ - ->decltype(val) { return val; } \ - static_assert( \ - true, "") // This makes it so that a semicolon is required after the - // macro, which helps clang-format handle the formatting. - -// C array overload -template -auto range_begin(const T (&arr)[N]) -> const T* { - return arr; -} -template -auto range_end(const T (&arr)[N]) -> const T* { - return arr + N; -} - -template -struct has_member_fn_begin_end_t : std::false_type {}; - -template -struct has_member_fn_begin_end_t().begin()), - decltype(std::declval().end())>> - : std::true_type {}; - -// Member function overloads. -template -auto range_begin(T&& rng) FMT_DECLTYPE_RETURN(static_cast(rng).begin()); -template -auto range_end(T&& rng) FMT_DECLTYPE_RETURN(static_cast(rng).end()); - -// ADL overloads. Only participate in overload resolution if member functions -// are not found. -template -auto range_begin(T&& rng) - -> enable_if_t::value, - decltype(begin(static_cast(rng)))> { - return begin(static_cast(rng)); -} -template -auto range_end(T&& rng) -> enable_if_t::value, - decltype(end(static_cast(rng)))> { - return end(static_cast(rng)); -} - -template -struct has_const_begin_end : std::false_type {}; -template -struct has_mutable_begin_end : std::false_type {}; - -template -struct has_const_begin_end< - T, void_t&>())), - decltype(detail::range_end( - std::declval&>()))>> - : std::true_type {}; - -template -struct has_mutable_begin_end< - T, void_t())), - decltype(detail::range_end(std::declval())), - // the extra int here is because older versions of MSVC don't - // SFINAE properly unless there are distinct types - int>> : std::true_type {}; - -template -struct is_range_ - : std::integral_constant::value || - has_mutable_begin_end::value)> {}; -# undef FMT_DECLTYPE_RETURN -#endif - -// tuple_size and tuple_element check. -template class is_tuple_like_ { - template - static auto check(U* p) -> decltype(std::tuple_size::value, int()); - template static void check(...); - - public: - static constexpr const bool value = - !std::is_void(nullptr))>::value; -}; - -// Check for integer_sequence -#if defined(__cpp_lib_integer_sequence) || FMT_MSC_VERSION >= 1900 -template -using integer_sequence = std::integer_sequence; -template using index_sequence = std::index_sequence; -template using make_index_sequence = std::make_index_sequence; -#else -template struct integer_sequence { - using value_type = T; - - static FMT_CONSTEXPR auto size() -> size_t { return sizeof...(N); } -}; - -template using index_sequence = integer_sequence; - -template -struct make_integer_sequence : make_integer_sequence {}; -template -struct make_integer_sequence : integer_sequence {}; - -template -using make_index_sequence = make_integer_sequence; -#endif - -template -using tuple_index_sequence = make_index_sequence::value>; - -template ::value> -class is_tuple_formattable_ { - public: - static constexpr const bool value = false; -}; -template class is_tuple_formattable_ { - template - static auto all_true(index_sequence, - integer_sequence= 0)...>) -> std::true_type; - static auto all_true(...) -> std::false_type; - - template - static auto check(index_sequence) -> decltype(all_true( - index_sequence{}, - integer_sequence::type, - C>::value)...>{})); - - public: - static constexpr const bool value = - decltype(check(tuple_index_sequence{}))::value; -}; - -template -FMT_CONSTEXPR void for_each(index_sequence, Tuple&& t, F&& f) { - using std::get; - // Using a free function get(Tuple) now. - const int unused[] = {0, ((void)f(get(t)), 0)...}; - ignore_unused(unused); -} - -template -FMT_CONSTEXPR void for_each(Tuple&& t, F&& f) { - for_each(tuple_index_sequence>(), - std::forward(t), std::forward(f)); -} - -template -void for_each2(index_sequence, Tuple1&& t1, Tuple2&& t2, F&& f) { - using std::get; - const int unused[] = {0, ((void)f(get(t1), get(t2)), 0)...}; - ignore_unused(unused); -} - -template -void for_each2(Tuple1&& t1, Tuple2&& t2, F&& f) { - for_each2(tuple_index_sequence>(), - std::forward(t1), std::forward(t2), - std::forward(f)); -} - -namespace tuple { -// Workaround a bug in MSVC 2019 (v140). -template -using result_t = std::tuple, Char>...>; - -using std::get; -template -auto get_formatters(index_sequence) - -> result_t(std::declval()))...>; -} // namespace tuple - -#if FMT_MSC_VERSION && FMT_MSC_VERSION < 1920 -// Older MSVC doesn't get the reference type correctly for arrays. -template struct range_reference_type_impl { - using type = decltype(*detail::range_begin(std::declval())); -}; - -template struct range_reference_type_impl { - using type = T&; -}; - -template -using range_reference_type = typename range_reference_type_impl::type; -#else -template -using range_reference_type = - decltype(*detail::range_begin(std::declval())); -#endif - -// We don't use the Range's value_type for anything, but we do need the Range's -// reference type, with cv-ref stripped. -template -using uncvref_type = remove_cvref_t>; - -template -FMT_CONSTEXPR auto maybe_set_debug_format(Formatter& f, bool set) - -> decltype(f.set_debug_format(set)) { - f.set_debug_format(set); -} -template -FMT_CONSTEXPR void maybe_set_debug_format(Formatter&, ...) {} - -template -struct range_format_kind_ - : std::integral_constant, T>::value - ? range_format::disabled - : is_map::value ? range_format::map - : is_set::value ? range_format::set - : range_format::sequence> {}; - -template -using range_format_constant = std::integral_constant; - -// These are not generic lambdas for compatibility with C++11. -template struct parse_empty_specs { - template FMT_CONSTEXPR void operator()(Formatter& f) { - f.parse(ctx); - detail::maybe_set_debug_format(f, true); - } - ParseContext& ctx; -}; -template struct format_tuple_element { - using char_type = typename FormatContext::char_type; - - template - void operator()(const formatter& f, const T& v) { - if (i > 0) ctx.advance_to(detail::copy(separator, ctx.out())); - ctx.advance_to(f.format(v, ctx)); - ++i; - } - - int i; - FormatContext& ctx; - basic_string_view separator; -}; - -} // namespace detail - -template struct is_tuple_like { - static constexpr const bool value = - detail::is_tuple_like_::value && !detail::is_range_::value; -}; - -template struct is_tuple_formattable { - static constexpr const bool value = - detail::is_tuple_formattable_::value; -}; - -template -struct formatter::value && - fmt::is_tuple_formattable::value>> { - private: - decltype(detail::tuple::get_formatters( - detail::tuple_index_sequence())) formatters_; - - basic_string_view separator_ = detail::string_literal{}; - basic_string_view opening_bracket_ = - detail::string_literal{}; - basic_string_view closing_bracket_ = - detail::string_literal{}; - - public: - FMT_CONSTEXPR formatter() {} - - FMT_CONSTEXPR void set_separator(basic_string_view sep) { - separator_ = sep; - } - - FMT_CONSTEXPR void set_brackets(basic_string_view open, - basic_string_view close) { - opening_bracket_ = open; - closing_bracket_ = close; - } - - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - auto it = ctx.begin(); - if (it != ctx.end() && *it != '}') report_error("invalid format specifier"); - detail::for_each(formatters_, detail::parse_empty_specs{ctx}); - return it; - } - - template - auto format(const Tuple& value, FormatContext& ctx) const - -> decltype(ctx.out()) { - ctx.advance_to(detail::copy(opening_bracket_, ctx.out())); - detail::for_each2( - formatters_, value, - detail::format_tuple_element{0, ctx, separator_}); - return detail::copy(closing_bracket_, ctx.out()); - } -}; - -template struct is_range { - static constexpr const bool value = - detail::is_range_::value && !detail::has_to_string_view::value; -}; - -namespace detail { -template struct range_mapper { - using mapper = arg_mapper; - - template , Context>::value)> - static auto map(T&& value) -> T&& { - return static_cast(value); - } - template , Context>::value)> - static auto map(T&& value) - -> decltype(mapper().map(static_cast(value))) { - return mapper().map(static_cast(value)); - } -}; - -template -using range_formatter_type = - formatter>{} - .map(std::declval()))>, - Char>; - -template -using maybe_const_range = - conditional_t::value, const R, R>; - -// Workaround a bug in MSVC 2015 and earlier. -#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910 -template -struct is_formattable_delayed - : is_formattable>, Char> {}; -#endif -} // namespace detail - -template struct conjunction : std::true_type {}; -template struct conjunction

: P {}; -template -struct conjunction - : conditional_t, P1> {}; - -template -struct range_formatter; - -template -struct range_formatter< - T, Char, - enable_if_t>, - is_formattable>::value>> { - private: - detail::range_formatter_type underlying_; - basic_string_view separator_ = detail::string_literal{}; - basic_string_view opening_bracket_ = - detail::string_literal{}; - basic_string_view closing_bracket_ = - detail::string_literal{}; - bool is_debug = false; - - template ::value)> - auto write_debug_string(Output& out, It it, Sentinel end) const -> Output { - auto buf = basic_memory_buffer(); - for (; it != end; ++it) buf.push_back(*it); - auto specs = format_specs(); - specs.type = presentation_type::debug; - return detail::write( - out, basic_string_view(buf.data(), buf.size()), specs); - } - - template ::value)> - auto write_debug_string(Output& out, It, Sentinel) const -> Output { - return out; - } - - public: - FMT_CONSTEXPR range_formatter() {} - - FMT_CONSTEXPR auto underlying() -> detail::range_formatter_type& { - return underlying_; - } - - FMT_CONSTEXPR void set_separator(basic_string_view sep) { - separator_ = sep; - } - - FMT_CONSTEXPR void set_brackets(basic_string_view open, - basic_string_view close) { - opening_bracket_ = open; - closing_bracket_ = close; - } - - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - auto it = ctx.begin(); - auto end = ctx.end(); - detail::maybe_set_debug_format(underlying_, true); - if (it == end) return underlying_.parse(ctx); - - switch (detail::to_ascii(*it)) { - case 'n': - set_brackets({}, {}); - ++it; - break; - case '?': - is_debug = true; - set_brackets({}, {}); - ++it; - if (it == end || *it != 's') report_error("invalid format specifier"); - FMT_FALLTHROUGH; - case 's': - if (!std::is_same::value) - report_error("invalid format specifier"); - if (!is_debug) { - set_brackets(detail::string_literal{}, - detail::string_literal{}); - set_separator({}); - detail::maybe_set_debug_format(underlying_, false); - } - ++it; - return it; - } - - if (it != end && *it != '}') { - if (*it != ':') report_error("invalid format specifier"); - detail::maybe_set_debug_format(underlying_, false); - ++it; - } - - ctx.advance_to(it); - return underlying_.parse(ctx); - } - - template - auto format(R&& range, FormatContext& ctx) const -> decltype(ctx.out()) { - auto mapper = detail::range_mapper>(); - auto out = ctx.out(); - auto it = detail::range_begin(range); - auto end = detail::range_end(range); - if (is_debug) return write_debug_string(out, std::move(it), end); - - out = detail::copy(opening_bracket_, out); - int i = 0; - for (; it != end; ++it) { - if (i > 0) out = detail::copy(separator_, out); - ctx.advance_to(out); - auto&& item = *it; // Need an lvalue - out = underlying_.format(mapper.map(item), ctx); - ++i; - } - out = detail::copy(closing_bracket_, out); - return out; - } -}; - -FMT_EXPORT -template -struct range_format_kind - : conditional_t< - is_range::value, detail::range_format_kind_, - std::integral_constant> {}; - -template -struct formatter< - R, Char, - enable_if_t::value != range_format::disabled && - range_format_kind::value != range_format::map && - range_format_kind::value != range_format::string && - range_format_kind::value != range_format::debug_string> -// Workaround a bug in MSVC 2015 and earlier. -#if !FMT_MSC_VERSION || FMT_MSC_VERSION >= 1910 - , - detail::is_formattable_delayed -#endif - >::value>> { - private: - using range_type = detail::maybe_const_range; - range_formatter, Char> range_formatter_; - - public: - using nonlocking = void; - - FMT_CONSTEXPR formatter() { - if (detail::const_check(range_format_kind::value != - range_format::set)) - return; - range_formatter_.set_brackets(detail::string_literal{}, - detail::string_literal{}); - } - - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return range_formatter_.parse(ctx); - } - - template - auto format(range_type& range, FormatContext& ctx) const - -> decltype(ctx.out()) { - return range_formatter_.format(range, ctx); - } -}; - -// A map formatter. -template -struct formatter< - R, Char, - enable_if_t::value == range_format::map>> { - private: - using map_type = detail::maybe_const_range; - using element_type = detail::uncvref_type; - - decltype(detail::tuple::get_formatters( - detail::tuple_index_sequence())) formatters_; - bool no_delimiters_ = false; - - public: - FMT_CONSTEXPR formatter() {} - - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - auto it = ctx.begin(); - auto end = ctx.end(); - if (it != end) { - if (detail::to_ascii(*it) == 'n') { - no_delimiters_ = true; - ++it; - } - if (it != end && *it != '}') { - if (*it != ':') report_error("invalid format specifier"); - ++it; - } - ctx.advance_to(it); - } - detail::for_each(formatters_, detail::parse_empty_specs{ctx}); - return it; - } - - template - auto format(map_type& map, FormatContext& ctx) const -> decltype(ctx.out()) { - auto out = ctx.out(); - basic_string_view open = detail::string_literal{}; - if (!no_delimiters_) out = detail::copy(open, out); - int i = 0; - auto mapper = detail::range_mapper>(); - basic_string_view sep = detail::string_literal{}; - for (auto&& value : map) { - if (i > 0) out = detail::copy(sep, out); - ctx.advance_to(out); - detail::for_each2(formatters_, mapper.map(value), - detail::format_tuple_element{ - 0, ctx, detail::string_literal{}}); - ++i; - } - basic_string_view close = detail::string_literal{}; - if (!no_delimiters_) out = detail::copy(close, out); - return out; - } -}; - -// A (debug_)string formatter. -template -struct formatter< - R, Char, - enable_if_t::value == range_format::string || - range_format_kind::value == - range_format::debug_string>> { - private: - using range_type = detail::maybe_const_range; - using string_type = - conditional_t, - decltype(detail::range_begin(std::declval())), - decltype(detail::range_end(std::declval()))>::value, - detail::std_string_view, std::basic_string>; - - formatter underlying_; - - public: - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return underlying_.parse(ctx); - } - - template - auto format(range_type& range, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - if (detail::const_check(range_format_kind::value == - range_format::debug_string)) - *out++ = '"'; - out = underlying_.format( - string_type{detail::range_begin(range), detail::range_end(range)}, ctx); - if (detail::const_check(range_format_kind::value == - range_format::debug_string)) - *out++ = '"'; - return out; - } -}; - -template -struct join_view : detail::view { - It begin; - Sentinel end; - basic_string_view sep; - - join_view(It b, Sentinel e, basic_string_view s) - : begin(std::move(b)), end(e), sep(s) {} -}; - -template -struct formatter, Char> { - private: - using value_type = -#ifdef __cpp_lib_ranges - std::iter_value_t; -#else - typename std::iterator_traits::value_type; -#endif - formatter, Char> value_formatter_; - - using view_ref = conditional_t::value, - const join_view&, - join_view&&>; - - public: - using nonlocking = void; - - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> const Char* { - return value_formatter_.parse(ctx); - } - - template - auto format(view_ref& value, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto it = std::forward(value).begin; - auto out = ctx.out(); - if (it == value.end) return out; - out = value_formatter_.format(*it, ctx); - ++it; - while (it != value.end) { - out = detail::copy(value.sep.begin(), value.sep.end(), out); - ctx.advance_to(out); - out = value_formatter_.format(*it, ctx); - ++it; - } - return out; - } -}; - -/// Returns a view that formats the iterator range `[begin, end)` with elements -/// separated by `sep`. -template -auto join(It begin, Sentinel end, string_view sep) -> join_view { - return {std::move(begin), end, sep}; -} - -/** - * Returns a view that formats `range` with elements separated by `sep`. - * - * **Example**: - * - * auto v = std::vector{1, 2, 3}; - * fmt::print("{}", fmt::join(v, ", ")); - * // Output: 1, 2, 3 - * - * `fmt::join` applies passed format specifiers to the range elements: - * - * fmt::print("{:02}", fmt::join(v, ", ")); - * // Output: 01, 02, 03 - */ -template -auto join(Range&& r, string_view sep) - -> join_view { - return {detail::range_begin(r), detail::range_end(r), sep}; -} - -template struct tuple_join_view : detail::view { - const std::tuple& tuple; - basic_string_view sep; - - tuple_join_view(const std::tuple& t, basic_string_view s) - : tuple(t), sep{s} {} -}; - -// Define FMT_TUPLE_JOIN_SPECIFIERS to enable experimental format specifiers -// support in tuple_join. It is disabled by default because of issues with -// the dynamic width and precision. -#ifndef FMT_TUPLE_JOIN_SPECIFIERS -# define FMT_TUPLE_JOIN_SPECIFIERS 0 -#endif - -template -struct formatter, Char> { - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return do_parse(ctx, std::integral_constant()); - } - - template - auto format(const tuple_join_view& value, - FormatContext& ctx) const -> typename FormatContext::iterator { - return do_format(value, ctx, - std::integral_constant()); - } - - private: - std::tuple::type, Char>...> formatters_; - - template - FMT_CONSTEXPR auto do_parse(ParseContext& ctx, - std::integral_constant) - -> decltype(ctx.begin()) { - return ctx.begin(); - } - - template - FMT_CONSTEXPR auto do_parse(ParseContext& ctx, - std::integral_constant) - -> decltype(ctx.begin()) { - auto end = ctx.begin(); -#if FMT_TUPLE_JOIN_SPECIFIERS - end = std::get(formatters_).parse(ctx); - if (N > 1) { - auto end1 = do_parse(ctx, std::integral_constant()); - if (end != end1) - report_error("incompatible format specs for tuple elements"); - } -#endif - return end; - } - - template - auto do_format(const tuple_join_view&, FormatContext& ctx, - std::integral_constant) const -> - typename FormatContext::iterator { - return ctx.out(); - } - - template - auto do_format(const tuple_join_view& value, FormatContext& ctx, - std::integral_constant) const -> - typename FormatContext::iterator { - auto out = std::get(formatters_) - .format(std::get(value.tuple), ctx); - if (N <= 1) return out; - out = detail::copy(value.sep, out); - ctx.advance_to(out); - return do_format(value, ctx, std::integral_constant()); - } -}; - -namespace detail { -// Check if T has an interface like a container adaptor (e.g. std::stack, -// std::queue, std::priority_queue). -template class is_container_adaptor_like { - template static auto check(U* p) -> typename U::container_type; - template static void check(...); - - public: - static constexpr const bool value = - !std::is_void(nullptr))>::value; -}; - -template struct all { - const Container& c; - auto begin() const -> typename Container::const_iterator { return c.begin(); } - auto end() const -> typename Container::const_iterator { return c.end(); } -}; -} // namespace detail - -template -struct formatter< - T, Char, - enable_if_t, - bool_constant::value == - range_format::disabled>>::value>> - : formatter, Char> { - using all = detail::all; - template - auto format(const T& t, FormatContext& ctx) const -> decltype(ctx.out()) { - struct getter : T { - static auto get(const T& t) -> all { - return {t.*(&getter::c)}; // Access c through the derived class. - } - }; - return formatter::format(getter::get(t), ctx); - } -}; - -FMT_BEGIN_EXPORT - -/** - * Returns an object that formats `std::tuple` with elements separated by `sep`. - * - * **Example**: - * - * auto t = std::tuple{1, 'a'}; - * fmt::print("{}", fmt::join(t, ", ")); - * // Output: 1, a - */ -template -FMT_CONSTEXPR auto join(const std::tuple& tuple, string_view sep) - -> tuple_join_view { - return {tuple, sep}; -} - -/** - * Returns an object that formats `std::initializer_list` with elements - * separated by `sep`. - * - * **Example**: - * - * fmt::print("{}", fmt::join({1, 2, 3}, ", ")); - * // Output: "1, 2, 3" - */ -template -auto join(std::initializer_list list, string_view sep) - -> join_view { - return join(std::begin(list), std::end(list), sep); -} - -FMT_END_EXPORT -FMT_END_NAMESPACE - -#endif // FMT_RANGES_H_ diff --git a/include/spdlog/fmt/bundled/std.h b/include/spdlog/fmt/bundled/std.h deleted file mode 100644 index fb43940..0000000 --- a/include/spdlog/fmt/bundled/std.h +++ /dev/null @@ -1,699 +0,0 @@ -// Formatting library for C++ - formatters for standard library types -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_STD_H_ -#define FMT_STD_H_ - -#include "format.h" -#include "ostream.h" - -#ifndef FMT_MODULE -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include -# include - -// Check FMT_CPLUSPLUS to suppress a bogus warning in MSVC. -# if FMT_CPLUSPLUS >= 201703L -# if FMT_HAS_INCLUDE() -# include -# endif -# if FMT_HAS_INCLUDE() -# include -# endif -# if FMT_HAS_INCLUDE() -# include -# endif -# endif -// Use > instead of >= in the version check because may be -// available after C++17 but before C++20 is marked as implemented. -# if FMT_CPLUSPLUS > 201703L && FMT_HAS_INCLUDE() -# include -# endif -# if FMT_CPLUSPLUS > 202002L && FMT_HAS_INCLUDE() -# include -# endif -#endif // FMT_MODULE - -#if FMT_HAS_INCLUDE() -# include -#endif - -// GCC 4 does not support FMT_HAS_INCLUDE. -#if FMT_HAS_INCLUDE() || defined(__GLIBCXX__) -# include -// Android NDK with gabi++ library on some architectures does not implement -// abi::__cxa_demangle(). -# ifndef __GABIXX_CXXABI_H__ -# define FMT_HAS_ABI_CXA_DEMANGLE -# endif -#endif - -// For older Xcode versions, __cpp_lib_xxx flags are inaccurately defined. -#ifndef FMT_CPP_LIB_FILESYSTEM -# ifdef __cpp_lib_filesystem -# define FMT_CPP_LIB_FILESYSTEM __cpp_lib_filesystem -# else -# define FMT_CPP_LIB_FILESYSTEM 0 -# endif -#endif - -#ifndef FMT_CPP_LIB_VARIANT -# ifdef __cpp_lib_variant -# define FMT_CPP_LIB_VARIANT __cpp_lib_variant -# else -# define FMT_CPP_LIB_VARIANT 0 -# endif -#endif - -#if FMT_CPP_LIB_FILESYSTEM -FMT_BEGIN_NAMESPACE - -namespace detail { - -template -auto get_path_string(const std::filesystem::path& p, - const std::basic_string& native) { - if constexpr (std::is_same_v && std::is_same_v) - return to_utf8(native, to_utf8_error_policy::replace); - else - return p.string(); -} - -template -void write_escaped_path(basic_memory_buffer& quoted, - const std::filesystem::path& p, - const std::basic_string& native) { - if constexpr (std::is_same_v && - std::is_same_v) { - auto buf = basic_memory_buffer(); - write_escaped_string(std::back_inserter(buf), native); - bool valid = to_utf8::convert(quoted, {buf.data(), buf.size()}); - FMT_ASSERT(valid, "invalid utf16"); - } else if constexpr (std::is_same_v) { - write_escaped_string( - std::back_inserter(quoted), native); - } else { - write_escaped_string(std::back_inserter(quoted), p.string()); - } -} - -} // namespace detail - -FMT_EXPORT -template struct formatter { - private: - format_specs specs_; - detail::arg_ref width_ref_; - bool debug_ = false; - char path_type_ = 0; - - public: - FMT_CONSTEXPR void set_debug_format(bool set = true) { debug_ = set; } - - template FMT_CONSTEXPR auto parse(ParseContext& ctx) { - auto it = ctx.begin(), end = ctx.end(); - if (it == end) return it; - - it = detail::parse_align(it, end, specs_); - if (it == end) return it; - - it = detail::parse_dynamic_spec(it, end, specs_.width, width_ref_, ctx); - if (it != end && *it == '?') { - debug_ = true; - ++it; - } - if (it != end && (*it == 'g')) path_type_ = detail::to_ascii(*it++); - return it; - } - - template - auto format(const std::filesystem::path& p, FormatContext& ctx) const { - auto specs = specs_; - auto path_string = - !path_type_ ? p.native() - : p.generic_string(); - - detail::handle_dynamic_spec(specs.width, width_ref_, - ctx); - if (!debug_) { - auto s = detail::get_path_string(p, path_string); - return detail::write(ctx.out(), basic_string_view(s), specs); - } - auto quoted = basic_memory_buffer(); - detail::write_escaped_path(quoted, p, path_string); - return detail::write(ctx.out(), - basic_string_view(quoted.data(), quoted.size()), - specs); - } -}; - -class path : public std::filesystem::path { - public: - auto display_string() const -> std::string { - const std::filesystem::path& base = *this; - return fmt::format(FMT_STRING("{}"), base); - } - auto system_string() const -> std::string { return string(); } - - auto generic_display_string() const -> std::string { - const std::filesystem::path& base = *this; - return fmt::format(FMT_STRING("{:g}"), base); - } - auto generic_system_string() const -> std::string { return generic_string(); } -}; - -FMT_END_NAMESPACE -#endif // FMT_CPP_LIB_FILESYSTEM - -FMT_BEGIN_NAMESPACE -FMT_EXPORT -template -struct formatter, Char> : nested_formatter { - private: - // Functor because C++11 doesn't support generic lambdas. - struct writer { - const std::bitset& bs; - - template - FMT_CONSTEXPR auto operator()(OutputIt out) -> OutputIt { - for (auto pos = N; pos > 0; --pos) { - out = detail::write(out, bs[pos - 1] ? Char('1') : Char('0')); - } - - return out; - } - }; - - public: - template - auto format(const std::bitset& bs, FormatContext& ctx) const - -> decltype(ctx.out()) { - return write_padded(ctx, writer{bs}); - } -}; - -FMT_EXPORT -template -struct formatter : basic_ostream_formatter {}; -FMT_END_NAMESPACE - -#ifdef __cpp_lib_optional -FMT_BEGIN_NAMESPACE -FMT_EXPORT -template -struct formatter, Char, - std::enable_if_t::value>> { - private: - formatter underlying_; - static constexpr basic_string_view optional = - detail::string_literal{}; - static constexpr basic_string_view none = - detail::string_literal{}; - - template - FMT_CONSTEXPR static auto maybe_set_debug_format(U& u, bool set) - -> decltype(u.set_debug_format(set)) { - u.set_debug_format(set); - } - - template - FMT_CONSTEXPR static void maybe_set_debug_format(U&, ...) {} - - public: - template FMT_CONSTEXPR auto parse(ParseContext& ctx) { - maybe_set_debug_format(underlying_, true); - return underlying_.parse(ctx); - } - - template - auto format(const std::optional& opt, FormatContext& ctx) const - -> decltype(ctx.out()) { - if (!opt) return detail::write(ctx.out(), none); - - auto out = ctx.out(); - out = detail::write(out, optional); - ctx.advance_to(out); - out = underlying_.format(*opt, ctx); - return detail::write(out, ')'); - } -}; -FMT_END_NAMESPACE -#endif // __cpp_lib_optional - -#if defined(__cpp_lib_expected) || FMT_CPP_LIB_VARIANT - -FMT_BEGIN_NAMESPACE -namespace detail { - -template -auto write_escaped_alternative(OutputIt out, const T& v) -> OutputIt { - if constexpr (has_to_string_view::value) - return write_escaped_string(out, detail::to_string_view(v)); - if constexpr (std::is_same_v) return write_escaped_char(out, v); - return write(out, v); -} - -} // namespace detail - -FMT_END_NAMESPACE -#endif - -#ifdef __cpp_lib_expected -FMT_BEGIN_NAMESPACE - -FMT_EXPORT -template -struct formatter, Char, - std::enable_if_t::value && - is_formattable::value>> { - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } - - template - auto format(const std::expected& value, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - - if (value.has_value()) { - out = detail::write(out, "expected("); - out = detail::write_escaped_alternative(out, *value); - } else { - out = detail::write(out, "unexpected("); - out = detail::write_escaped_alternative(out, value.error()); - } - *out++ = ')'; - return out; - } -}; -FMT_END_NAMESPACE -#endif // __cpp_lib_expected - -#ifdef __cpp_lib_source_location -FMT_BEGIN_NAMESPACE -FMT_EXPORT -template <> struct formatter { - template FMT_CONSTEXPR auto parse(ParseContext& ctx) { - return ctx.begin(); - } - - template - auto format(const std::source_location& loc, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - out = detail::write(out, loc.file_name()); - out = detail::write(out, ':'); - out = detail::write(out, loc.line()); - out = detail::write(out, ':'); - out = detail::write(out, loc.column()); - out = detail::write(out, ": "); - out = detail::write(out, loc.function_name()); - return out; - } -}; -FMT_END_NAMESPACE -#endif - -#if FMT_CPP_LIB_VARIANT -FMT_BEGIN_NAMESPACE -namespace detail { - -template -using variant_index_sequence = - std::make_index_sequence::value>; - -template struct is_variant_like_ : std::false_type {}; -template -struct is_variant_like_> : std::true_type {}; - -// formattable element check. -template class is_variant_formattable_ { - template - static std::conjunction< - is_formattable, C>...> - check(std::index_sequence); - - public: - static constexpr const bool value = - decltype(check(variant_index_sequence{}))::value; -}; - -} // namespace detail - -template struct is_variant_like { - static constexpr const bool value = detail::is_variant_like_::value; -}; - -template struct is_variant_formattable { - static constexpr const bool value = - detail::is_variant_formattable_::value; -}; - -FMT_EXPORT -template struct formatter { - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } - - template - auto format(const std::monostate&, FormatContext& ctx) const - -> decltype(ctx.out()) { - return detail::write(ctx.out(), "monostate"); - } -}; - -FMT_EXPORT -template -struct formatter< - Variant, Char, - std::enable_if_t, is_variant_formattable>>> { - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } - - template - auto format(const Variant& value, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - - out = detail::write(out, "variant("); - FMT_TRY { - std::visit( - [&](const auto& v) { - out = detail::write_escaped_alternative(out, v); - }, - value); - } - FMT_CATCH(const std::bad_variant_access&) { - detail::write(out, "valueless by exception"); - } - *out++ = ')'; - return out; - } -}; -FMT_END_NAMESPACE -#endif // FMT_CPP_LIB_VARIANT - -FMT_BEGIN_NAMESPACE -FMT_EXPORT -template struct formatter { - template - FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } - - template - FMT_CONSTEXPR auto format(const std::error_code& ec, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); - out = detail::write_bytes(out, ec.category().name(), format_specs()); - out = detail::write(out, Char(':')); - out = detail::write(out, ec.value()); - return out; - } -}; - -#if FMT_USE_RTTI -namespace detail { - -template -auto write_demangled_name(OutputIt out, const std::type_info& ti) -> OutputIt { -# ifdef FMT_HAS_ABI_CXA_DEMANGLE - int status = 0; - std::size_t size = 0; - std::unique_ptr demangled_name_ptr( - abi::__cxa_demangle(ti.name(), nullptr, &size, &status), &std::free); - - string_view demangled_name_view; - if (demangled_name_ptr) { - demangled_name_view = demangled_name_ptr.get(); - - // Normalization of stdlib inline namespace names. - // libc++ inline namespaces. - // std::__1::* -> std::* - // std::__1::__fs::* -> std::* - // libstdc++ inline namespaces. - // std::__cxx11::* -> std::* - // std::filesystem::__cxx11::* -> std::filesystem::* - if (demangled_name_view.starts_with("std::")) { - char* begin = demangled_name_ptr.get(); - char* to = begin + 5; // std:: - for (char *from = to, *end = begin + demangled_name_view.size(); - from < end;) { - // This is safe, because demangled_name is NUL-terminated. - if (from[0] == '_' && from[1] == '_') { - char* next = from + 1; - while (next < end && *next != ':') next++; - if (next[0] == ':' && next[1] == ':') { - from = next + 2; - continue; - } - } - *to++ = *from++; - } - demangled_name_view = {begin, detail::to_unsigned(to - begin)}; - } - } else { - demangled_name_view = string_view(ti.name()); - } - return detail::write_bytes(out, demangled_name_view); -# elif FMT_MSC_VERSION - const string_view demangled_name(ti.name()); - for (std::size_t i = 0; i < demangled_name.size(); ++i) { - auto sub = demangled_name; - sub.remove_prefix(i); - if (sub.starts_with("enum ")) { - i += 4; - continue; - } - if (sub.starts_with("class ") || sub.starts_with("union ")) { - i += 5; - continue; - } - if (sub.starts_with("struct ")) { - i += 6; - continue; - } - if (*sub.begin() != ' ') *out++ = *sub.begin(); - } - return out; -# else - return detail::write_bytes(out, string_view(ti.name())); -# endif -} - -} // namespace detail - -FMT_EXPORT -template -struct formatter { - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - return ctx.begin(); - } - - template - auto format(const std::type_info& ti, Context& ctx) const - -> decltype(ctx.out()) { - return detail::write_demangled_name(ctx.out(), ti); - } -}; -#endif - -FMT_EXPORT -template -struct formatter< - T, Char, // DEPRECATED! Mixing code unit types. - typename std::enable_if::value>::type> { - private: - bool with_typename_ = false; - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - auto it = ctx.begin(); - auto end = ctx.end(); - if (it == end || *it == '}') return it; - if (*it == 't') { - ++it; - with_typename_ = FMT_USE_RTTI != 0; - } - return it; - } - - template - auto format(const std::exception& ex, Context& ctx) const - -> decltype(ctx.out()) { - auto out = ctx.out(); -#if FMT_USE_RTTI - if (with_typename_) { - out = detail::write_demangled_name(out, typeid(ex)); - *out++ = ':'; - *out++ = ' '; - } -#endif - return detail::write_bytes(out, string_view(ex.what())); - } -}; - -namespace detail { - -template -struct has_flip : std::false_type {}; - -template -struct has_flip().flip())>> - : std::true_type {}; - -template struct is_bit_reference_like { - static constexpr const bool value = - std::is_convertible::value && - std::is_nothrow_assignable::value && has_flip::value; -}; - -#ifdef _LIBCPP_VERSION - -// Workaround for libc++ incompatibility with C++ standard. -// According to the Standard, `bitset::operator[] const` returns bool. -template -struct is_bit_reference_like> { - static constexpr const bool value = true; -}; - -#endif - -} // namespace detail - -// We can't use std::vector::reference and -// std::bitset::reference because the compiler can't deduce Allocator and N -// in partial specialization. -FMT_EXPORT -template -struct formatter::value>> - : formatter { - template - FMT_CONSTEXPR auto format(const BitRef& v, FormatContext& ctx) const - -> decltype(ctx.out()) { - return formatter::format(v, ctx); - } -}; - -template -auto ptr(const std::unique_ptr& p) -> const void* { - return p.get(); -} -template auto ptr(const std::shared_ptr& p) -> const void* { - return p.get(); -} - -FMT_EXPORT -template -struct formatter, Char, - enable_if_t::value>> - : formatter { - template - auto format(const std::atomic& v, FormatContext& ctx) const - -> decltype(ctx.out()) { - return formatter::format(v.load(), ctx); - } -}; - -#ifdef __cpp_lib_atomic_flag_test -FMT_EXPORT -template -struct formatter : formatter { - template - auto format(const std::atomic_flag& v, FormatContext& ctx) const - -> decltype(ctx.out()) { - return formatter::format(v.test(), ctx); - } -}; -#endif // __cpp_lib_atomic_flag_test - -FMT_EXPORT -template struct formatter, Char> { - private: - detail::dynamic_format_specs specs_; - - template - FMT_CONSTEXPR auto do_format(const std::complex& c, - detail::dynamic_format_specs& specs, - FormatContext& ctx, OutputIt out) const - -> OutputIt { - if (c.real() != 0) { - *out++ = Char('('); - out = detail::write(out, c.real(), specs, ctx.locale()); - specs.sign = sign::plus; - out = detail::write(out, c.imag(), specs, ctx.locale()); - if (!detail::isfinite(c.imag())) *out++ = Char(' '); - *out++ = Char('i'); - *out++ = Char(')'); - return out; - } - out = detail::write(out, c.imag(), specs, ctx.locale()); - if (!detail::isfinite(c.imag())) *out++ = Char(' '); - *out++ = Char('i'); - return out; - } - - public: - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - if (ctx.begin() == ctx.end() || *ctx.begin() == '}') return ctx.begin(); - return parse_format_specs(ctx.begin(), ctx.end(), specs_, ctx, - detail::type_constant::value); - } - - template - auto format(const std::complex& c, FormatContext& ctx) const - -> decltype(ctx.out()) { - auto specs = specs_; - if (specs.width_ref.kind != detail::arg_id_kind::none || - specs.precision_ref.kind != detail::arg_id_kind::none) { - detail::handle_dynamic_spec(specs.width, - specs.width_ref, ctx); - detail::handle_dynamic_spec( - specs.precision, specs.precision_ref, ctx); - } - - if (specs.width == 0) return do_format(c, specs, ctx, ctx.out()); - auto buf = basic_memory_buffer(); - - auto outer_specs = format_specs(); - outer_specs.width = specs.width; - outer_specs.fill = specs.fill; - outer_specs.align = specs.align; - - specs.width = 0; - specs.fill = {}; - specs.align = align::none; - - do_format(c, specs, ctx, basic_appender(buf)); - return detail::write(ctx.out(), - basic_string_view(buf.data(), buf.size()), - outer_specs); - } -}; - -FMT_END_NAMESPACE -#endif // FMT_STD_H_ diff --git a/include/spdlog/fmt/bundled/xchar.h b/include/spdlog/fmt/bundled/xchar.h deleted file mode 100644 index b1f39ed..0000000 --- a/include/spdlog/fmt/bundled/xchar.h +++ /dev/null @@ -1,322 +0,0 @@ -// Formatting library for C++ - optional wchar_t and exotic character support -// -// Copyright (c) 2012 - present, Victor Zverovich -// All rights reserved. -// -// For the license information refer to format.h. - -#ifndef FMT_XCHAR_H_ -#define FMT_XCHAR_H_ - -#include "color.h" -#include "format.h" -#include "ranges.h" - -#ifndef FMT_MODULE -# include -# if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) -# include -# endif -#endif - -FMT_BEGIN_NAMESPACE -namespace detail { - -template -using is_exotic_char = bool_constant::value>; - -template struct format_string_char {}; - -template -struct format_string_char< - S, void_t())))>> { - using type = char_t; -}; - -template -struct format_string_char::value>> { - using type = typename S::char_type; -}; - -template -using format_string_char_t = typename format_string_char::type; - -inline auto write_loc(basic_appender out, loc_value value, - const format_specs& specs, locale_ref loc) -> bool { -#ifndef FMT_STATIC_THOUSANDS_SEPARATOR - auto& numpunct = - std::use_facet>(loc.get()); - auto separator = std::wstring(); - auto grouping = numpunct.grouping(); - if (!grouping.empty()) separator = std::wstring(1, numpunct.thousands_sep()); - return value.visit(loc_writer{out, specs, separator, grouping, {}}); -#endif - return false; -} -} // namespace detail - -FMT_BEGIN_EXPORT - -using wstring_view = basic_string_view; -using wformat_parse_context = basic_format_parse_context; -using wformat_context = buffered_context; -using wformat_args = basic_format_args; -using wmemory_buffer = basic_memory_buffer; - -#if FMT_GCC_VERSION && FMT_GCC_VERSION < 409 -// Workaround broken conversion on older gcc. -template using wformat_string = wstring_view; -inline auto runtime(wstring_view s) -> wstring_view { return s; } -#else -template -using wformat_string = basic_format_string...>; -inline auto runtime(wstring_view s) -> runtime_format_string { - return {{s}}; -} -#endif - -template <> struct is_char : std::true_type {}; -template <> struct is_char : std::true_type {}; -template <> struct is_char : std::true_type {}; - -#ifdef __cpp_char8_t -template <> -struct is_char : bool_constant {}; -#endif - -template -constexpr auto make_wformat_args(T&... args) - -> decltype(fmt::make_format_args(args...)) { - return fmt::make_format_args(args...); -} - -inline namespace literals { -#if FMT_USE_USER_DEFINED_LITERALS && !FMT_USE_NONTYPE_TEMPLATE_ARGS -constexpr auto operator""_a(const wchar_t* s, size_t) - -> detail::udl_arg { - return {s}; -} -#endif -} // namespace literals - -template -auto join(It begin, Sentinel end, wstring_view sep) - -> join_view { - return {begin, end, sep}; -} - -template -auto join(Range&& range, wstring_view sep) - -> join_view, detail::sentinel_t, - wchar_t> { - return join(std::begin(range), std::end(range), sep); -} - -template -auto join(std::initializer_list list, wstring_view sep) - -> join_view { - return join(std::begin(list), std::end(list), sep); -} - -template -auto join(const std::tuple& tuple, basic_string_view sep) - -> tuple_join_view { - return {tuple, sep}; -} - -template ::value)> -auto vformat(basic_string_view format_str, - typename detail::vformat_args::type args) - -> std::basic_string { - auto buf = basic_memory_buffer(); - detail::vformat_to(buf, format_str, args); - return to_string(buf); -} - -template -auto format(wformat_string fmt, T&&... args) -> std::wstring { - return vformat(fmt::wstring_view(fmt), fmt::make_wformat_args(args...)); -} - -template -auto format_to(OutputIt out, wformat_string fmt, T&&... args) - -> OutputIt { - return vformat_to(out, fmt::wstring_view(fmt), - fmt::make_wformat_args(args...)); -} - -// Pass char_t as a default template parameter instead of using -// std::basic_string> to reduce the symbol size. -template , - FMT_ENABLE_IF(!std::is_same::value && - !std::is_same::value)> -auto format(const S& format_str, T&&... args) -> std::basic_string { - return vformat(detail::to_string_view(format_str), - fmt::make_format_args>(args...)); -} - -template , - FMT_ENABLE_IF(detail::is_locale::value&& - detail::is_exotic_char::value)> -inline auto vformat(const Locale& loc, const S& format_str, - typename detail::vformat_args::type args) - -> std::basic_string { - return detail::vformat(loc, detail::to_string_view(format_str), args); -} - -template , - FMT_ENABLE_IF(detail::is_locale::value&& - detail::is_exotic_char::value)> -inline auto format(const Locale& loc, const S& format_str, T&&... args) - -> std::basic_string { - return detail::vformat( - loc, detail::to_string_view(format_str), - fmt::make_format_args>(args...)); -} - -template , - FMT_ENABLE_IF(detail::is_output_iterator::value&& - detail::is_exotic_char::value)> -auto vformat_to(OutputIt out, const S& format_str, - typename detail::vformat_args::type args) -> OutputIt { - auto&& buf = detail::get_buffer(out); - detail::vformat_to(buf, detail::to_string_view(format_str), args); - return detail::get_iterator(buf, out); -} - -template , - FMT_ENABLE_IF(detail::is_output_iterator::value && - !std::is_same::value && - !std::is_same::value)> -inline auto format_to(OutputIt out, const S& fmt, T&&... args) -> OutputIt { - return vformat_to(out, detail::to_string_view(fmt), - fmt::make_format_args>(args...)); -} - -template , - FMT_ENABLE_IF(detail::is_output_iterator::value&& - detail::is_locale::value&& - detail::is_exotic_char::value)> -inline auto vformat_to(OutputIt out, const Locale& loc, const S& format_str, - typename detail::vformat_args::type args) - -> OutputIt { - auto&& buf = detail::get_buffer(out); - vformat_to(buf, detail::to_string_view(format_str), args, - detail::locale_ref(loc)); - return detail::get_iterator(buf, out); -} - -template , - bool enable = detail::is_output_iterator::value && - detail::is_locale::value && - detail::is_exotic_char::value> -inline auto format_to(OutputIt out, const Locale& loc, const S& format_str, - T&&... args) -> - typename std::enable_if::type { - return vformat_to(out, loc, detail::to_string_view(format_str), - fmt::make_format_args>(args...)); -} - -template ::value&& - detail::is_exotic_char::value)> -inline auto vformat_to_n(OutputIt out, size_t n, - basic_string_view format_str, - typename detail::vformat_args::type args) - -> format_to_n_result { - using traits = detail::fixed_buffer_traits; - auto buf = detail::iterator_buffer(out, n); - detail::vformat_to(buf, format_str, args); - return {buf.out(), buf.count()}; -} - -template , - FMT_ENABLE_IF(detail::is_output_iterator::value&& - detail::is_exotic_char::value)> -inline auto format_to_n(OutputIt out, size_t n, const S& fmt, T&&... args) - -> format_to_n_result { - return vformat_to_n(out, n, fmt::basic_string_view(fmt), - fmt::make_format_args>(args...)); -} - -template , - FMT_ENABLE_IF(detail::is_exotic_char::value)> -inline auto formatted_size(const S& fmt, T&&... args) -> size_t { - auto buf = detail::counting_buffer(); - detail::vformat_to(buf, detail::to_string_view(fmt), - fmt::make_format_args>(args...)); - return buf.count(); -} - -inline void vprint(std::FILE* f, wstring_view fmt, wformat_args args) { - auto buf = wmemory_buffer(); - detail::vformat_to(buf, fmt, args); - buf.push_back(L'\0'); - if (std::fputws(buf.data(), f) == -1) - FMT_THROW(system_error(errno, FMT_STRING("cannot write to file"))); -} - -inline void vprint(wstring_view fmt, wformat_args args) { - vprint(stdout, fmt, args); -} - -template -void print(std::FILE* f, wformat_string fmt, T&&... args) { - return vprint(f, wstring_view(fmt), fmt::make_wformat_args(args...)); -} - -template void print(wformat_string fmt, T&&... args) { - return vprint(wstring_view(fmt), fmt::make_wformat_args(args...)); -} - -template -void println(std::FILE* f, wformat_string fmt, T&&... args) { - return print(f, L"{}\n", fmt::format(fmt, std::forward(args)...)); -} - -template void println(wformat_string fmt, T&&... args) { - return print(L"{}\n", fmt::format(fmt, std::forward(args)...)); -} - -inline auto vformat(const text_style& ts, wstring_view fmt, wformat_args args) - -> std::wstring { - auto buf = wmemory_buffer(); - detail::vformat_to(buf, ts, fmt, args); - return fmt::to_string(buf); -} - -template -inline auto format(const text_style& ts, wformat_string fmt, T&&... args) - -> std::wstring { - return fmt::vformat(ts, fmt, fmt::make_wformat_args(args...)); -} - -template -FMT_DEPRECATED void print(std::FILE* f, const text_style& ts, - wformat_string fmt, const T&... args) { - vprint(f, ts, fmt, fmt::make_wformat_args(args...)); -} - -template -FMT_DEPRECATED void print(const text_style& ts, wformat_string fmt, - const T&... args) { - return print(stdout, ts, fmt, args...); -} - -/// Converts `value` to `std::wstring` using the default format for type `T`. -template inline auto to_wstring(const T& value) -> std::wstring { - return format(FMT_STRING(L"{}"), value); -} -FMT_END_EXPORT -FMT_END_NAMESPACE - -#endif // FMT_XCHAR_H_ diff --git a/include/spdlog/fmt/chrono.h b/include/spdlog/fmt/chrono.h deleted file mode 100644 index a72a5bd..0000000 --- a/include/spdlog/fmt/chrono.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright(c) 2016 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once -// -// include bundled or external copy of fmtlib's chrono support -// -#include - -#if !defined(SPDLOG_USE_STD_FORMAT) - #if !defined(SPDLOG_FMT_EXTERNAL) - #ifdef SPDLOG_HEADER_ONLY - #ifndef FMT_HEADER_ONLY - #define FMT_HEADER_ONLY - #endif - #endif - #include - #else - #include - #endif -#endif diff --git a/include/spdlog/fmt/compile.h b/include/spdlog/fmt/compile.h deleted file mode 100644 index 3c9c25d..0000000 --- a/include/spdlog/fmt/compile.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright(c) 2016 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once -// -// include bundled or external copy of fmtlib's compile-time support -// -#include - -#if !defined(SPDLOG_USE_STD_FORMAT) - #if !defined(SPDLOG_FMT_EXTERNAL) - #ifdef SPDLOG_HEADER_ONLY - #ifndef FMT_HEADER_ONLY - #define FMT_HEADER_ONLY - #endif - #endif - #include - #else - #include - #endif -#endif diff --git a/include/spdlog/fmt/fmt.h b/include/spdlog/fmt/fmt.h deleted file mode 100644 index 2f09c15..0000000 --- a/include/spdlog/fmt/fmt.h +++ /dev/null @@ -1,31 +0,0 @@ -// -// Copyright(c) 2016-2018 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once - -// -// Include a bundled header-only copy of fmtlib or an external one. -// By default spdlog include its own copy. -// -#include - -#if defined(SPDLOG_USE_STD_FORMAT) // SPDLOG_USE_STD_FORMAT is defined - use std::format - #include -#elif !defined(SPDLOG_FMT_EXTERNAL) - #if !defined(SPDLOG_COMPILED_LIB) && !defined(FMT_HEADER_ONLY) - #define FMT_HEADER_ONLY - #endif - #ifndef FMT_USE_WINDOWS_H - #define FMT_USE_WINDOWS_H 0 - #endif - - #include - #include - -#else // SPDLOG_FMT_EXTERNAL is defined - use external fmtlib - #include - #include - #include -#endif diff --git a/include/spdlog/fmt/ostr.h b/include/spdlog/fmt/ostr.h deleted file mode 100644 index 2b90105..0000000 --- a/include/spdlog/fmt/ostr.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright(c) 2016 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once -// -// include bundled or external copy of fmtlib's ostream support -// -#include - -#if !defined(SPDLOG_USE_STD_FORMAT) - #if !defined(SPDLOG_FMT_EXTERNAL) - #ifdef SPDLOG_HEADER_ONLY - #ifndef FMT_HEADER_ONLY - #define FMT_HEADER_ONLY - #endif - #endif - #include - #else - #include - #endif -#endif diff --git a/include/spdlog/fmt/ranges.h b/include/spdlog/fmt/ranges.h deleted file mode 100644 index 5bb91e9..0000000 --- a/include/spdlog/fmt/ranges.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright(c) 2016 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once -// -// include bundled or external copy of fmtlib's ranges support -// -#include - -#if !defined(SPDLOG_USE_STD_FORMAT) - #if !defined(SPDLOG_FMT_EXTERNAL) - #ifdef SPDLOG_HEADER_ONLY - #ifndef FMT_HEADER_ONLY - #define FMT_HEADER_ONLY - #endif - #endif - #include - #else - #include - #endif -#endif diff --git a/include/spdlog/fmt/std.h b/include/spdlog/fmt/std.h deleted file mode 100644 index dabe6f6..0000000 --- a/include/spdlog/fmt/std.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright(c) 2016 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once -// -// include bundled or external copy of fmtlib's std support (for formatting e.g. -// std::filesystem::path, std::thread::id, std::monostate, std::variant, ...) -// -#include - -#if !defined(SPDLOG_USE_STD_FORMAT) - #if !defined(SPDLOG_FMT_EXTERNAL) - #ifdef SPDLOG_HEADER_ONLY - #ifndef FMT_HEADER_ONLY - #define FMT_HEADER_ONLY - #endif - #endif - #include - #else - #include - #endif -#endif diff --git a/include/spdlog/fmt/xchar.h b/include/spdlog/fmt/xchar.h deleted file mode 100644 index 2525f05..0000000 --- a/include/spdlog/fmt/xchar.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright(c) 2016 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once -// -// include bundled or external copy of fmtlib's xchar support -// -#include - -#if !defined(SPDLOG_USE_STD_FORMAT) - #if !defined(SPDLOG_FMT_EXTERNAL) - #ifdef SPDLOG_HEADER_ONLY - #ifndef FMT_HEADER_ONLY - #define FMT_HEADER_ONLY - #endif - #endif - #include - #else - #include - #endif -#endif diff --git a/include/spdlog/formatter.h b/include/spdlog/formatter.h deleted file mode 100644 index 4d482f8..0000000 --- a/include/spdlog/formatter.h +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -namespace spdlog { - -class formatter { -public: - virtual ~formatter() = default; - virtual void format(const details::log_msg &msg, memory_buf_t &dest) = 0; - virtual std::unique_ptr clone() const = 0; -}; -} // namespace spdlog diff --git a/include/spdlog/fwd.h b/include/spdlog/fwd.h deleted file mode 100644 index 647b16b..0000000 --- a/include/spdlog/fwd.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -namespace spdlog { -class logger; -class formatter; - -namespace sinks { -class sink; -} - -namespace level { -enum level_enum : int; -} - -} // namespace spdlog diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h deleted file mode 100644 index 5218fe4..0000000 --- a/include/spdlog/logger-inl.h +++ /dev/null @@ -1,198 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include -#include - -#include - -namespace spdlog { - -// public methods -SPDLOG_INLINE logger::logger(const logger &other) - : name_(other.name_), - sinks_(other.sinks_), - level_(other.level_.load(std::memory_order_relaxed)), - flush_level_(other.flush_level_.load(std::memory_order_relaxed)), - custom_err_handler_(other.custom_err_handler_), - tracer_(other.tracer_) {} - -SPDLOG_INLINE logger::logger(logger &&other) SPDLOG_NOEXCEPT - : name_(std::move(other.name_)), - sinks_(std::move(other.sinks_)), - level_(other.level_.load(std::memory_order_relaxed)), - flush_level_(other.flush_level_.load(std::memory_order_relaxed)), - custom_err_handler_(std::move(other.custom_err_handler_)), - tracer_(std::move(other.tracer_)) - -{} - -SPDLOG_INLINE logger &logger::operator=(logger other) SPDLOG_NOEXCEPT { - this->swap(other); - return *this; -} - -SPDLOG_INLINE void logger::swap(spdlog::logger &other) SPDLOG_NOEXCEPT { - name_.swap(other.name_); - sinks_.swap(other.sinks_); - - // swap level_ - auto other_level = other.level_.load(); - auto my_level = level_.exchange(other_level); - other.level_.store(my_level); - - // swap flush level_ - other_level = other.flush_level_.load(); - my_level = flush_level_.exchange(other_level); - other.flush_level_.store(my_level); - - custom_err_handler_.swap(other.custom_err_handler_); - std::swap(tracer_, other.tracer_); -} - -SPDLOG_INLINE void swap(logger &a, logger &b) { a.swap(b); } - -SPDLOG_INLINE void logger::set_level(level::level_enum log_level) { level_.store(log_level); } - -SPDLOG_INLINE level::level_enum logger::level() const { - return static_cast(level_.load(std::memory_order_relaxed)); -} - -SPDLOG_INLINE const std::string &logger::name() const { return name_; } - -// set formatting for the sinks in this logger. -// each sink will get a separate instance of the formatter object. -SPDLOG_INLINE void logger::set_formatter(std::unique_ptr f) { - for (auto it = sinks_.begin(); it != sinks_.end(); ++it) { - if (std::next(it) == sinks_.end()) { - // last element - we can be move it. - (*it)->set_formatter(std::move(f)); - break; // to prevent clang-tidy warning - } else { - (*it)->set_formatter(f->clone()); - } - } -} - -SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type) { - auto new_formatter = details::make_unique(std::move(pattern), time_type); - set_formatter(std::move(new_formatter)); -} - -// create new backtrace sink and move to it all our child sinks -SPDLOG_INLINE void logger::enable_backtrace(size_t n_messages) { tracer_.enable(n_messages); } - -// restore orig sinks and level and delete the backtrace sink -SPDLOG_INLINE void logger::disable_backtrace() { tracer_.disable(); } - -SPDLOG_INLINE void logger::dump_backtrace() { dump_backtrace_(); } - -// flush functions -SPDLOG_INLINE void logger::flush() { flush_(); } - -SPDLOG_INLINE void logger::flush_on(level::level_enum log_level) { flush_level_.store(log_level); } - -SPDLOG_INLINE level::level_enum logger::flush_level() const { - return static_cast(flush_level_.load(std::memory_order_relaxed)); -} - -// sinks -SPDLOG_INLINE const std::vector &logger::sinks() const { return sinks_; } - -SPDLOG_INLINE std::vector &logger::sinks() { return sinks_; } - -// error handler -SPDLOG_INLINE void logger::set_error_handler(err_handler handler) { - custom_err_handler_ = std::move(handler); -} - -// create new logger with same sinks and configuration. -SPDLOG_INLINE std::shared_ptr logger::clone(std::string logger_name) { - auto cloned = std::make_shared(*this); - cloned->name_ = std::move(logger_name); - return cloned; -} - -// protected methods -SPDLOG_INLINE void logger::log_it_(const spdlog::details::log_msg &log_msg, - bool log_enabled, - bool traceback_enabled) { - if (log_enabled) { - sink_it_(log_msg); - } - if (traceback_enabled) { - tracer_.push_back(log_msg); - } -} - -SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { - for (auto &sink : sinks_) { - if (sink->should_log(msg.level)) { - SPDLOG_TRY { sink->log(msg); } - SPDLOG_LOGGER_CATCH(msg.source) - } - } - - if (should_flush_(msg)) { - flush_(); - } -} - -SPDLOG_INLINE void logger::flush_() { - for (auto &sink : sinks_) { - SPDLOG_TRY { sink->flush(); } - SPDLOG_LOGGER_CATCH(source_loc()) - } -} - -SPDLOG_INLINE void logger::dump_backtrace_() { - using details::log_msg; - if (tracer_.enabled() && !tracer_.empty()) { - sink_it_( - log_msg{name(), level::info, "****************** Backtrace Start ******************"}); - tracer_.foreach_pop([this](const log_msg &msg) { this->sink_it_(msg); }); - sink_it_( - log_msg{name(), level::info, "****************** Backtrace End ********************"}); - } -} - -SPDLOG_INLINE bool logger::should_flush_(const details::log_msg &msg) { - auto flush_level = flush_level_.load(std::memory_order_relaxed); - return (msg.level >= flush_level) && (msg.level != level::off); -} - -SPDLOG_INLINE void logger::err_handler_(const std::string &msg) { - if (custom_err_handler_) { - custom_err_handler_(msg); - } else { - using std::chrono::system_clock; - static std::mutex mutex; - static std::chrono::system_clock::time_point last_report_time; - static size_t err_counter = 0; - std::lock_guard lk{mutex}; - auto now = system_clock::now(); - err_counter++; - if (now - last_report_time < std::chrono::seconds(1)) { - return; - } - last_report_time = now; - auto tm_time = details::os::localtime(system_clock::to_time_t(now)); - char date_buf[64]; - std::strftime(date_buf, sizeof(date_buf), "%Y-%m-%d %H:%M:%S", &tm_time); -#if defined(USING_R) && defined(R_R_H) // if in R environment - REprintf("[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, name().c_str(), - msg.c_str()); -#else - std::fprintf(stderr, "[*** LOG ERROR #%04zu ***] [%s] [%s] %s\n", err_counter, date_buf, - name().c_str(), msg.c_str()); -#endif - } -} -} // namespace spdlog diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h deleted file mode 100644 index f49bdc0..0000000 --- a/include/spdlog/logger.h +++ /dev/null @@ -1,379 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// Thread safe logger (except for set_error_handler()) -// Has name, log level, vector of std::shared sink pointers and formatter -// Upon each log write the logger: -// 1. Checks if its log level is enough to log the message and if yes: -// 2. Call the underlying sinks to do the job. -// 3. Each sink use its own private copy of a formatter to format the message -// and send to its destination. -// -// The use of private formatter per sink provides the opportunity to cache some -// formatted data, and support for different format per sink. - -#include -#include -#include - -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - #ifndef _WIN32 - #error SPDLOG_WCHAR_TO_UTF8_SUPPORT only supported on windows - #endif - #include -#endif - -#include - -#ifndef SPDLOG_NO_EXCEPTIONS - #define SPDLOG_LOGGER_CATCH(location) \ - catch (const std::exception &ex) { \ - if (location.filename) { \ - err_handler_(fmt_lib::format(SPDLOG_FMT_STRING("{} [{}({})]"), ex.what(), \ - location.filename, location.line)); \ - } else { \ - err_handler_(ex.what()); \ - } \ - } \ - catch (...) { \ - err_handler_("Rethrowing unknown exception in logger"); \ - throw; \ - } -#else - #define SPDLOG_LOGGER_CATCH(location) -#endif - -namespace spdlog { - -class SPDLOG_API logger { -public: - // Empty logger - explicit logger(std::string name) - : name_(std::move(name)), - sinks_() {} - - // Logger with range on sinks - template - logger(std::string name, It begin, It end) - : name_(std::move(name)), - sinks_(begin, end) {} - - // Logger with single sink - logger(std::string name, sink_ptr single_sink) - : logger(std::move(name), {std::move(single_sink)}) {} - - // Logger with sinks init list - logger(std::string name, sinks_init_list sinks) - : logger(std::move(name), sinks.begin(), sinks.end()) {} - - virtual ~logger() = default; - - logger(const logger &other); - logger(logger &&other) SPDLOG_NOEXCEPT; - logger &operator=(logger other) SPDLOG_NOEXCEPT; - void swap(spdlog::logger &other) SPDLOG_NOEXCEPT; - - template - void log(source_loc loc, level::level_enum lvl, format_string_t fmt, Args &&...args) { - log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); - } - - template - void log(level::level_enum lvl, format_string_t fmt, Args &&...args) { - log(source_loc{}, lvl, fmt, std::forward(args)...); - } - - template - void log(level::level_enum lvl, const T &msg) { - log(source_loc{}, lvl, msg); - } - - // T cannot be statically converted to format string (including string_view/wstring_view) - template ::value, - int>::type = 0> - void log(source_loc loc, level::level_enum lvl, const T &msg) { - log(loc, lvl, "{}", msg); - } - - void log(log_clock::time_point log_time, - source_loc loc, - level::level_enum lvl, - string_view_t msg) { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) { - return; - } - - details::log_msg log_msg(log_time, loc, name_, lvl, msg); - log_it_(log_msg, log_enabled, traceback_enabled); - } - - void log(source_loc loc, level::level_enum lvl, string_view_t msg) { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) { - return; - } - - details::log_msg log_msg(loc, name_, lvl, msg); - log_it_(log_msg, log_enabled, traceback_enabled); - } - - void log(level::level_enum lvl, string_view_t msg) { log(source_loc{}, lvl, msg); } - - template - void trace(format_string_t fmt, Args &&...args) { - log(level::trace, fmt, std::forward(args)...); - } - - template - void debug(format_string_t fmt, Args &&...args) { - log(level::debug, fmt, std::forward(args)...); - } - - template - void info(format_string_t fmt, Args &&...args) { - log(level::info, fmt, std::forward(args)...); - } - - template - void warn(format_string_t fmt, Args &&...args) { - log(level::warn, fmt, std::forward(args)...); - } - - template - void error(format_string_t fmt, Args &&...args) { - log(level::err, fmt, std::forward(args)...); - } - - template - void critical(format_string_t fmt, Args &&...args) { - log(level::critical, fmt, std::forward(args)...); - } - -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - template - void log(source_loc loc, level::level_enum lvl, wformat_string_t fmt, Args &&...args) { - log_(loc, lvl, details::to_string_view(fmt), std::forward(args)...); - } - - template - void log(level::level_enum lvl, wformat_string_t fmt, Args &&...args) { - log(source_loc{}, lvl, fmt, std::forward(args)...); - } - - void log(log_clock::time_point log_time, - source_loc loc, - level::level_enum lvl, - wstring_view_t msg) { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) { - return; - } - - memory_buf_t buf; - details::os::wstr_to_utf8buf(wstring_view_t(msg.data(), msg.size()), buf); - details::log_msg log_msg(log_time, loc, name_, lvl, string_view_t(buf.data(), buf.size())); - log_it_(log_msg, log_enabled, traceback_enabled); - } - - void log(source_loc loc, level::level_enum lvl, wstring_view_t msg) { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) { - return; - } - - memory_buf_t buf; - details::os::wstr_to_utf8buf(wstring_view_t(msg.data(), msg.size()), buf); - details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - log_it_(log_msg, log_enabled, traceback_enabled); - } - - void log(level::level_enum lvl, wstring_view_t msg) { log(source_loc{}, lvl, msg); } - - template - void trace(wformat_string_t fmt, Args &&...args) { - log(level::trace, fmt, std::forward(args)...); - } - - template - void debug(wformat_string_t fmt, Args &&...args) { - log(level::debug, fmt, std::forward(args)...); - } - - template - void info(wformat_string_t fmt, Args &&...args) { - log(level::info, fmt, std::forward(args)...); - } - - template - void warn(wformat_string_t fmt, Args &&...args) { - log(level::warn, fmt, std::forward(args)...); - } - - template - void error(wformat_string_t fmt, Args &&...args) { - log(level::err, fmt, std::forward(args)...); - } - - template - void critical(wformat_string_t fmt, Args &&...args) { - log(level::critical, fmt, std::forward(args)...); - } -#endif - - template - void trace(const T &msg) { - log(level::trace, msg); - } - - template - void debug(const T &msg) { - log(level::debug, msg); - } - - template - void info(const T &msg) { - log(level::info, msg); - } - - template - void warn(const T &msg) { - log(level::warn, msg); - } - - template - void error(const T &msg) { - log(level::err, msg); - } - - template - void critical(const T &msg) { - log(level::critical, msg); - } - - // return true logging is enabled for the given level. - bool should_log(level::level_enum msg_level) const { - return msg_level >= level_.load(std::memory_order_relaxed); - } - - // return true if backtrace logging is enabled. - bool should_backtrace() const { return tracer_.enabled(); } - - void set_level(level::level_enum log_level); - - level::level_enum level() const; - - const std::string &name() const; - - // set formatting for the sinks in this logger. - // each sink will get a separate instance of the formatter object. - void set_formatter(std::unique_ptr f); - - // set formatting for the sinks in this logger. - // equivalent to - // set_formatter(make_unique(pattern, time_type)) - // Note: each sink will get a new instance of a formatter object, replacing the old one. - void set_pattern(std::string pattern, pattern_time_type time_type = pattern_time_type::local); - - // backtrace support. - // efficiently store all debug/trace messages in a circular buffer until needed for debugging. - void enable_backtrace(size_t n_messages); - void disable_backtrace(); - void dump_backtrace(); - - // flush functions - void flush(); - void flush_on(level::level_enum log_level); - level::level_enum flush_level() const; - - // sinks - const std::vector &sinks() const; - - std::vector &sinks(); - - // error handler - void set_error_handler(err_handler); - - // create new logger with same sinks and configuration. - virtual std::shared_ptr clone(std::string logger_name); - -protected: - std::string name_; - std::vector sinks_; - spdlog::level_t level_{level::info}; - spdlog::level_t flush_level_{level::off}; - err_handler custom_err_handler_{nullptr}; - details::backtracer tracer_; - - // common implementation for after templated public api has been resolved - template - void log_(source_loc loc, level::level_enum lvl, string_view_t fmt, Args &&...args) { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) { - return; - } - SPDLOG_TRY { - memory_buf_t buf; -#ifdef SPDLOG_USE_STD_FORMAT - fmt_lib::vformat_to(std::back_inserter(buf), fmt, fmt_lib::make_format_args(args...)); -#else - fmt::vformat_to(fmt::appender(buf), fmt, fmt::make_format_args(args...)); -#endif - - details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - log_it_(log_msg, log_enabled, traceback_enabled); - } - SPDLOG_LOGGER_CATCH(loc) - } - -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - template - void log_(source_loc loc, level::level_enum lvl, wstring_view_t fmt, Args &&...args) { - bool log_enabled = should_log(lvl); - bool traceback_enabled = tracer_.enabled(); - if (!log_enabled && !traceback_enabled) { - return; - } - SPDLOG_TRY { - // format to wmemory_buffer and convert to utf8 - wmemory_buf_t wbuf; - fmt_lib::vformat_to(std::back_inserter(wbuf), fmt, - fmt_lib::make_format_args(args...)); - - memory_buf_t buf; - details::os::wstr_to_utf8buf(wstring_view_t(wbuf.data(), wbuf.size()), buf); - details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); - log_it_(log_msg, log_enabled, traceback_enabled); - } - SPDLOG_LOGGER_CATCH(loc) - } -#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT - - // log the given message (if the given log level is high enough), - // and save backtrace (if backtrace is enabled). - void log_it_(const details::log_msg &log_msg, bool log_enabled, bool traceback_enabled); - virtual void sink_it_(const details::log_msg &msg); - virtual void flush_(); - void dump_backtrace_(); - bool should_flush_(const details::log_msg &msg); - - // handle errors during logging. - // default handler prints the error to stderr at max rate of 1 message/sec. - void err_handler_(const std::string &msg); -}; - -void swap(logger &a, logger &b); - -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "logger-inl.h" -#endif diff --git a/include/spdlog/mdc.h b/include/spdlog/mdc.h deleted file mode 100644 index 80b6f25..0000000 --- a/include/spdlog/mdc.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#if defined(SPDLOG_NO_TLS) - #error "This header requires thread local storage support, but SPDLOG_NO_TLS is defined." -#endif - -#include -#include - -#include - -// MDC is a simple map of key->string values stored in thread local storage whose content will be printed by the loggers. -// Note: Not supported in async mode (thread local storage - so the async thread pool have different copy). -// -// Usage example: -// spdlog::mdc::put("mdc_key_1", "mdc_value_1"); -// spdlog::info("Hello, {}", "World!"); // => [2024-04-26 02:08:05.040] [info] [mdc_key_1:mdc_value_1] Hello, World! - -namespace spdlog { -class SPDLOG_API mdc { -public: - using mdc_map_t = std::map; - - static void put(const std::string &key, const std::string &value) { - get_context()[key] = value; - } - - static std::string get(const std::string &key) { - auto &context = get_context(); - auto it = context.find(key); - if (it != context.end()) { - return it->second; - } - return ""; - } - - static void remove(const std::string &key) { get_context().erase(key); } - - static void clear() { get_context().clear(); } - - static mdc_map_t &get_context() { - static thread_local mdc_map_t context; - return context; - } -}; - -} // namespace spdlog diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h deleted file mode 100644 index b53d805..0000000 --- a/include/spdlog/pattern_formatter-inl.h +++ /dev/null @@ -1,1338 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include -#include - -#ifndef SPDLOG_NO_TLS - #include -#endif - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace spdlog { -namespace details { - -/////////////////////////////////////////////////////////////////////// -// name & level pattern appender -/////////////////////////////////////////////////////////////////////// - -class scoped_padder { -public: - scoped_padder(size_t wrapped_size, const padding_info &padinfo, memory_buf_t &dest) - : padinfo_(padinfo), - dest_(dest) { - remaining_pad_ = static_cast(padinfo.width_) - static_cast(wrapped_size); - if (remaining_pad_ <= 0) { - return; - } - - if (padinfo_.side_ == padding_info::pad_side::left) { - pad_it(remaining_pad_); - remaining_pad_ = 0; - } else if (padinfo_.side_ == padding_info::pad_side::center) { - auto half_pad = remaining_pad_ / 2; - auto reminder = remaining_pad_ & 1; - pad_it(half_pad); - remaining_pad_ = half_pad + reminder; // for the right side - } - } - - template - static unsigned int count_digits(T n) { - return fmt_helper::count_digits(n); - } - - ~scoped_padder() { - if (remaining_pad_ >= 0) { - pad_it(remaining_pad_); - } else if (padinfo_.truncate_) { - long new_size = static_cast(dest_.size()) + remaining_pad_; - dest_.resize(static_cast(new_size)); - } - } - -private: - void pad_it(long count) { - fmt_helper::append_string_view(string_view_t(spaces_.data(), static_cast(count)), - dest_); - } - - const padding_info &padinfo_; - memory_buf_t &dest_; - long remaining_pad_; - string_view_t spaces_{" ", 64}; -}; - -struct null_scoped_padder { - null_scoped_padder(size_t /*wrapped_size*/, - const padding_info & /*padinfo*/, - memory_buf_t & /*dest*/) {} - - template - static unsigned int count_digits(T /* number */) { - return 0; - } -}; - -template -class name_formatter final : public flag_formatter { -public: - explicit name_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - ScopedPadder p(msg.logger_name.size(), padinfo_, dest); - fmt_helper::append_string_view(msg.logger_name, dest); - } -}; - -// log level appender -template -class level_formatter final : public flag_formatter { -public: - explicit level_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - const string_view_t &level_name = level::to_string_view(msg.level); - ScopedPadder p(level_name.size(), padinfo_, dest); - fmt_helper::append_string_view(level_name, dest); - } -}; - -// short log level appender -template -class short_level_formatter final : public flag_formatter { -public: - explicit short_level_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - string_view_t level_name{level::to_short_c_str(msg.level)}; - ScopedPadder p(level_name.size(), padinfo_, dest); - fmt_helper::append_string_view(level_name, dest); - } -}; - -/////////////////////////////////////////////////////////////////////// -// Date time pattern appenders -/////////////////////////////////////////////////////////////////////// - -static const char *ampm(const tm &t) { return t.tm_hour >= 12 ? "PM" : "AM"; } - -static int to12h(const tm &t) { return t.tm_hour > 12 ? t.tm_hour - 12 : t.tm_hour; } - -// Abbreviated weekday name -static std::array days{{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}}; - -template -class a_formatter final : public flag_formatter { -public: - explicit a_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{days[static_cast(tm_time.tm_wday)]}; - ScopedPadder p(field_value.size(), padinfo_, dest); - fmt_helper::append_string_view(field_value, dest); - } -}; - -// Full weekday name -static std::array full_days{ - {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}}; - -template -class A_formatter : public flag_formatter { -public: - explicit A_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{full_days[static_cast(tm_time.tm_wday)]}; - ScopedPadder p(field_value.size(), padinfo_, dest); - fmt_helper::append_string_view(field_value, dest); - } -}; - -// Abbreviated month -static const std::array months{ - {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}}; - -template -class b_formatter final : public flag_formatter { -public: - explicit b_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{months[static_cast(tm_time.tm_mon)]}; - ScopedPadder p(field_value.size(), padinfo_, dest); - fmt_helper::append_string_view(field_value, dest); - } -}; - -// Full month name -static const std::array full_months{{"January", "February", "March", "April", - "May", "June", "July", "August", "September", - "October", "November", "December"}}; - -template -class B_formatter final : public flag_formatter { -public: - explicit B_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - string_view_t field_value{full_months[static_cast(tm_time.tm_mon)]}; - ScopedPadder p(field_value.size(), padinfo_, dest); - fmt_helper::append_string_view(field_value, dest); - } -}; - -// Date and time representation (Thu Aug 23 15:35:46 2014) -template -class c_formatter final : public flag_formatter { -public: - explicit c_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 24; - ScopedPadder p(field_size, padinfo_, dest); - - fmt_helper::append_string_view(days[static_cast(tm_time.tm_wday)], dest); - dest.push_back(' '); - fmt_helper::append_string_view(months[static_cast(tm_time.tm_mon)], dest); - dest.push_back(' '); - fmt_helper::append_int(tm_time.tm_mday, dest); - dest.push_back(' '); - // time - - fmt_helper::pad2(tm_time.tm_hour, dest); - dest.push_back(':'); - fmt_helper::pad2(tm_time.tm_min, dest); - dest.push_back(':'); - fmt_helper::pad2(tm_time.tm_sec, dest); - dest.push_back(' '); - fmt_helper::append_int(tm_time.tm_year + 1900, dest); - } -}; - -// year - 2 digit -template -class C_formatter final : public flag_formatter { -public: - explicit C_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad2(tm_time.tm_year % 100, dest); - } -}; - -// Short MM/DD/YY date, equivalent to %m/%d/%y 08/23/01 -template -class D_formatter final : public flag_formatter { -public: - explicit D_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 10; - ScopedPadder p(field_size, padinfo_, dest); - - fmt_helper::pad2(tm_time.tm_mon + 1, dest); - dest.push_back('/'); - fmt_helper::pad2(tm_time.tm_mday, dest); - dest.push_back('/'); - fmt_helper::pad2(tm_time.tm_year % 100, dest); - } -}; - -// year - 4 digit -template -class Y_formatter final : public flag_formatter { -public: - explicit Y_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 4; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::append_int(tm_time.tm_year + 1900, dest); - } -}; - -// month 1-12 -template -class m_formatter final : public flag_formatter { -public: - explicit m_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad2(tm_time.tm_mon + 1, dest); - } -}; - -// day of month 1-31 -template -class d_formatter final : public flag_formatter { -public: - explicit d_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad2(tm_time.tm_mday, dest); - } -}; - -// hours in 24 format 0-23 -template -class H_formatter final : public flag_formatter { -public: - explicit H_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad2(tm_time.tm_hour, dest); - } -}; - -// hours in 12 format 1-12 -template -class I_formatter final : public flag_formatter { -public: - explicit I_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad2(to12h(tm_time), dest); - } -}; - -// minutes 0-59 -template -class M_formatter final : public flag_formatter { -public: - explicit M_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad2(tm_time.tm_min, dest); - } -}; - -// seconds 0-59 -template -class S_formatter final : public flag_formatter { -public: - explicit S_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad2(tm_time.tm_sec, dest); - } -}; - -// milliseconds -template -class e_formatter final : public flag_formatter { -public: - explicit e_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - auto millis = fmt_helper::time_fraction(msg.time); - const size_t field_size = 3; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad3(static_cast(millis.count()), dest); - } -}; - -// microseconds -template -class f_formatter final : public flag_formatter { -public: - explicit f_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - auto micros = fmt_helper::time_fraction(msg.time); - - const size_t field_size = 6; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad6(static_cast(micros.count()), dest); - } -}; - -// nanoseconds -template -class F_formatter final : public flag_formatter { -public: - explicit F_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - auto ns = fmt_helper::time_fraction(msg.time); - const size_t field_size = 9; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::pad9(static_cast(ns.count()), dest); - } -}; - -// seconds since epoch -template -class E_formatter final : public flag_formatter { -public: - explicit E_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - const size_t field_size = 10; - ScopedPadder p(field_size, padinfo_, dest); - auto duration = msg.time.time_since_epoch(); - auto seconds = std::chrono::duration_cast(duration).count(); - fmt_helper::append_int(seconds, dest); - } -}; - -// AM/PM -template -class p_formatter final : public flag_formatter { -public: - explicit p_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 2; - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::append_string_view(ampm(tm_time), dest); - } -}; - -// 12 hour clock 02:55:02 pm -template -class r_formatter final : public flag_formatter { -public: - explicit r_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 11; - ScopedPadder p(field_size, padinfo_, dest); - - fmt_helper::pad2(to12h(tm_time), dest); - dest.push_back(':'); - fmt_helper::pad2(tm_time.tm_min, dest); - dest.push_back(':'); - fmt_helper::pad2(tm_time.tm_sec, dest); - dest.push_back(' '); - fmt_helper::append_string_view(ampm(tm_time), dest); - } -}; - -// 24-hour HH:MM time, equivalent to %H:%M -template -class R_formatter final : public flag_formatter { -public: - explicit R_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 5; - ScopedPadder p(field_size, padinfo_, dest); - - fmt_helper::pad2(tm_time.tm_hour, dest); - dest.push_back(':'); - fmt_helper::pad2(tm_time.tm_min, dest); - } -}; - -// ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S -template -class T_formatter final : public flag_formatter { -public: - explicit T_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 8; - ScopedPadder p(field_size, padinfo_, dest); - - fmt_helper::pad2(tm_time.tm_hour, dest); - dest.push_back(':'); - fmt_helper::pad2(tm_time.tm_min, dest); - dest.push_back(':'); - fmt_helper::pad2(tm_time.tm_sec, dest); - } -}; - -// ISO 8601 offset from UTC in timezone (+-HH:MM) -template -class z_formatter final : public flag_formatter { -public: - explicit z_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - z_formatter() = default; - z_formatter(const z_formatter &) = delete; - z_formatter &operator=(const z_formatter &) = delete; - - void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override { - const size_t field_size = 6; - ScopedPadder p(field_size, padinfo_, dest); - - auto total_minutes = get_cached_offset(msg, tm_time); - bool is_negative = total_minutes < 0; - if (is_negative) { - total_minutes = -total_minutes; - dest.push_back('-'); - } else { - dest.push_back('+'); - } - - fmt_helper::pad2(total_minutes / 60, dest); // hours - dest.push_back(':'); - fmt_helper::pad2(total_minutes % 60, dest); // minutes - } - -private: - log_clock::time_point last_update_{std::chrono::seconds(0)}; - int offset_minutes_{0}; - - int get_cached_offset(const log_msg &msg, const std::tm &tm_time) { - // refresh every 10 seconds - if (msg.time - last_update_ >= std::chrono::seconds(10)) { - offset_minutes_ = os::utc_minutes_offset(tm_time); - last_update_ = msg.time; - } - return offset_minutes_; - } -}; - -// Thread id -template -class t_formatter final : public flag_formatter { -public: - explicit t_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - const auto field_size = ScopedPadder::count_digits(msg.thread_id); - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::append_int(msg.thread_id, dest); - } -}; - -// Current pid -template -class pid_formatter final : public flag_formatter { -public: - explicit pid_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { - const auto pid = static_cast(details::os::pid()); - auto field_size = ScopedPadder::count_digits(pid); - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::append_int(pid, dest); - } -}; - -template -class v_formatter final : public flag_formatter { -public: - explicit v_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - ScopedPadder p(msg.payload.size(), padinfo_, dest); - fmt_helper::append_string_view(msg.payload, dest); - } -}; - -class ch_formatter final : public flag_formatter { -public: - explicit ch_formatter(char ch) - : ch_(ch) {} - - void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { - dest.push_back(ch_); - } - -private: - char ch_; -}; - -// aggregate user chars to display as is -class aggregate_formatter final : public flag_formatter { -public: - aggregate_formatter() = default; - - void add_ch(char ch) { str_ += ch; } - void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { - fmt_helper::append_string_view(str_, dest); - } - -private: - std::string str_; -}; - -// mark the color range. expect it to be in the form of "%^colored text%$" -class color_start_formatter final : public flag_formatter { -public: - explicit color_start_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - msg.color_range_start = dest.size(); - } -}; - -class color_stop_formatter final : public flag_formatter { -public: - explicit color_stop_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - msg.color_range_end = dest.size(); - } -}; - -// print source location -template -class source_location_formatter final : public flag_formatter { -public: - explicit source_location_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - if (msg.source.empty()) { - ScopedPadder p(0, padinfo_, dest); - return; - } - - size_t text_size; - if (padinfo_.enabled()) { - // calc text size for padding based on "filename:line" - text_size = std::char_traits::length(msg.source.filename) + - ScopedPadder::count_digits(msg.source.line) + 1; - } else { - text_size = 0; - } - - ScopedPadder p(text_size, padinfo_, dest); - fmt_helper::append_string_view(msg.source.filename, dest); - dest.push_back(':'); - fmt_helper::append_int(msg.source.line, dest); - } -}; - -// print source filename -template -class source_filename_formatter final : public flag_formatter { -public: - explicit source_filename_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - if (msg.source.empty()) { - ScopedPadder p(0, padinfo_, dest); - return; - } - size_t text_size = - padinfo_.enabled() ? std::char_traits::length(msg.source.filename) : 0; - ScopedPadder p(text_size, padinfo_, dest); - fmt_helper::append_string_view(msg.source.filename, dest); - } -}; - -template -class short_filename_formatter final : public flag_formatter { -public: - explicit short_filename_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - -#ifdef _MSC_VER - #pragma warning(push) - #pragma warning(disable : 4127) // consider using 'if constexpr' instead -#endif // _MSC_VER - static const char *basename(const char *filename) { - // if the size is 2 (1 character + null terminator) we can use the more efficient strrchr - // the branch will be elided by optimizations - if (sizeof(os::folder_seps) == 2) { - const char *rv = std::strrchr(filename, os::folder_seps[0]); - return rv != nullptr ? rv + 1 : filename; - } else { - const std::reverse_iterator begin(filename + std::strlen(filename)); - const std::reverse_iterator end(filename); - - const auto it = std::find_first_of(begin, end, std::begin(os::folder_seps), - std::end(os::folder_seps) - 1); - return it != end ? it.base() : filename; - } - } -#ifdef _MSC_VER - #pragma warning(pop) -#endif // _MSC_VER - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - if (msg.source.empty()) { - ScopedPadder p(0, padinfo_, dest); - return; - } - auto filename = basename(msg.source.filename); - size_t text_size = padinfo_.enabled() ? std::char_traits::length(filename) : 0; - ScopedPadder p(text_size, padinfo_, dest); - fmt_helper::append_string_view(filename, dest); - } -}; - -template -class source_linenum_formatter final : public flag_formatter { -public: - explicit source_linenum_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - if (msg.source.empty()) { - ScopedPadder p(0, padinfo_, dest); - return; - } - - auto field_size = ScopedPadder::count_digits(msg.source.line); - ScopedPadder p(field_size, padinfo_, dest); - fmt_helper::append_int(msg.source.line, dest); - } -}; - -// print source funcname -template -class source_funcname_formatter final : public flag_formatter { -public: - explicit source_funcname_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - if (msg.source.empty()) { - ScopedPadder p(0, padinfo_, dest); - return; - } - size_t text_size = - padinfo_.enabled() ? std::char_traits::length(msg.source.funcname) : 0; - ScopedPadder p(text_size, padinfo_, dest); - fmt_helper::append_string_view(msg.source.funcname, dest); - } -}; - -// print elapsed time since last message -template -class elapsed_formatter final : public flag_formatter { -public: - using DurationUnits = Units; - - explicit elapsed_formatter(padding_info padinfo) - : flag_formatter(padinfo), - last_message_time_(log_clock::now()) {} - - void format(const details::log_msg &msg, const std::tm &, memory_buf_t &dest) override { - auto delta = (std::max)(msg.time - last_message_time_, log_clock::duration::zero()); - auto delta_units = std::chrono::duration_cast(delta); - last_message_time_ = msg.time; - auto delta_count = static_cast(delta_units.count()); - auto n_digits = static_cast(ScopedPadder::count_digits(delta_count)); - ScopedPadder p(n_digits, padinfo_, dest); - fmt_helper::append_int(delta_count, dest); - } - -private: - log_clock::time_point last_message_time_; -}; - -// Class for formatting Mapped Diagnostic Context (MDC) in log messages. -// Example: [logger-name] [info] [mdc_key_1:mdc_value_1 mdc_key_2:mdc_value_2] some message -#ifndef SPDLOG_NO_TLS -template -class mdc_formatter : public flag_formatter { -public: - explicit mdc_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &, const std::tm &, memory_buf_t &dest) override { - auto &mdc_map = mdc::get_context(); - if (mdc_map.empty()) { - ScopedPadder p(0, padinfo_, dest); - return; - } else { - format_mdc(mdc_map, dest); - } - } - - void format_mdc(const mdc::mdc_map_t &mdc_map, memory_buf_t &dest) { - auto last_element = --mdc_map.end(); - for (auto it = mdc_map.begin(); it != mdc_map.end(); ++it) { - auto &pair = *it; - const auto &key = pair.first; - const auto &value = pair.second; - size_t content_size = key.size() + value.size() + 1; // 1 for ':' - - if (it != last_element) { - content_size++; // 1 for ' ' - } - - ScopedPadder p(content_size, padinfo_, dest); - fmt_helper::append_string_view(key, dest); - fmt_helper::append_string_view(":", dest); - fmt_helper::append_string_view(value, dest); - if (it != last_element) { - fmt_helper::append_string_view(" ", dest); - } - } - } -}; -#endif - -// Full info formatter -// pattern: [%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v -class full_formatter final : public flag_formatter { -public: - explicit full_formatter(padding_info padinfo) - : flag_formatter(padinfo) {} - - void format(const details::log_msg &msg, const std::tm &tm_time, memory_buf_t &dest) override { - using std::chrono::duration_cast; - using std::chrono::milliseconds; - using std::chrono::seconds; - - // cache the date/time part for the next second. - auto duration = msg.time.time_since_epoch(); - auto secs = duration_cast(duration); - - if (cache_timestamp_ != secs || cached_datetime_.size() == 0) { - cached_datetime_.clear(); - cached_datetime_.push_back('['); - fmt_helper::append_int(tm_time.tm_year + 1900, cached_datetime_); - cached_datetime_.push_back('-'); - - fmt_helper::pad2(tm_time.tm_mon + 1, cached_datetime_); - cached_datetime_.push_back('-'); - - fmt_helper::pad2(tm_time.tm_mday, cached_datetime_); - cached_datetime_.push_back(' '); - - fmt_helper::pad2(tm_time.tm_hour, cached_datetime_); - cached_datetime_.push_back(':'); - - fmt_helper::pad2(tm_time.tm_min, cached_datetime_); - cached_datetime_.push_back(':'); - - fmt_helper::pad2(tm_time.tm_sec, cached_datetime_); - cached_datetime_.push_back('.'); - - cache_timestamp_ = secs; - } - dest.append(cached_datetime_.begin(), cached_datetime_.end()); - - auto millis = fmt_helper::time_fraction(msg.time); - fmt_helper::pad3(static_cast(millis.count()), dest); - dest.push_back(']'); - dest.push_back(' '); - - // append logger name if exists - if (msg.logger_name.size() > 0) { - dest.push_back('['); - fmt_helper::append_string_view(msg.logger_name, dest); - dest.push_back(']'); - dest.push_back(' '); - } - - dest.push_back('['); - // wrap the level name with color - msg.color_range_start = dest.size(); - // fmt_helper::append_string_view(level::to_c_str(msg.level), dest); - fmt_helper::append_string_view(level::to_string_view(msg.level), dest); - msg.color_range_end = dest.size(); - dest.push_back(']'); - dest.push_back(' '); - - // add source location if present - if (!msg.source.empty()) { - dest.push_back('['); - const char *filename = - details::short_filename_formatter::basename( - msg.source.filename); - fmt_helper::append_string_view(filename, dest); - dest.push_back(':'); - fmt_helper::append_int(msg.source.line, dest); - dest.push_back(']'); - dest.push_back(' '); - } - -#ifndef SPDLOG_NO_TLS - // add mdc if present - auto &mdc_map = mdc::get_context(); - if (!mdc_map.empty()) { - dest.push_back('['); - mdc_formatter_.format_mdc(mdc_map, dest); - dest.push_back(']'); - dest.push_back(' '); - } -#endif - // fmt_helper::append_string_view(msg.msg(), dest); - fmt_helper::append_string_view(msg.payload, dest); - } - -private: - std::chrono::seconds cache_timestamp_{0}; - memory_buf_t cached_datetime_; - -#ifndef SPDLOG_NO_TLS - mdc_formatter mdc_formatter_{padding_info{}}; -#endif - -}; - -} // namespace details - -SPDLOG_INLINE pattern_formatter::pattern_formatter(std::string pattern, - pattern_time_type time_type, - std::string eol, - custom_flags custom_user_flags) - : pattern_(std::move(pattern)), - eol_(std::move(eol)), - pattern_time_type_(time_type), - need_localtime_(false), - last_log_secs_(0), - custom_handlers_(std::move(custom_user_flags)) { - std::memset(&cached_tm_, 0, sizeof(cached_tm_)); - compile_pattern_(pattern_); -} - -// use by default full formatter for if pattern is not given -SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, std::string eol) - : pattern_("%+"), - eol_(std::move(eol)), - pattern_time_type_(time_type), - need_localtime_(true), - last_log_secs_(0) { - std::memset(&cached_tm_, 0, sizeof(cached_tm_)); - formatters_.push_back(details::make_unique(details::padding_info{})); -} - -SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const { - custom_flags cloned_custom_formatters; - for (auto &it : custom_handlers_) { - cloned_custom_formatters[it.first] = it.second->clone(); - } - auto cloned = details::make_unique(pattern_, pattern_time_type_, eol_, - std::move(cloned_custom_formatters)); - cloned->need_localtime(need_localtime_); -#if defined(__GNUC__) && __GNUC__ < 5 - return std::move(cloned); -#else - return cloned; -#endif -} - -SPDLOG_INLINE void pattern_formatter::format(const details::log_msg &msg, memory_buf_t &dest) { - if (need_localtime_) { - const auto secs = - std::chrono::duration_cast(msg.time.time_since_epoch()); - if (secs != last_log_secs_) { - cached_tm_ = get_time_(msg); - last_log_secs_ = secs; - } - } - - for (auto &f : formatters_) { - f->format(msg, cached_tm_, dest); - } - // write eol - details::fmt_helper::append_string_view(eol_, dest); -} - -SPDLOG_INLINE void pattern_formatter::set_pattern(std::string pattern) { - pattern_ = std::move(pattern); - need_localtime_ = false; - compile_pattern_(pattern_); -} - -SPDLOG_INLINE void pattern_formatter::need_localtime(bool need) { need_localtime_ = need; } - -SPDLOG_INLINE std::tm pattern_formatter::get_time_(const details::log_msg &msg) { - if (pattern_time_type_ == pattern_time_type::local) { - return details::os::localtime(log_clock::to_time_t(msg.time)); - } - return details::os::gmtime(log_clock::to_time_t(msg.time)); -} - -template -SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_info padding) { - // process custom flags - auto it = custom_handlers_.find(flag); - if (it != custom_handlers_.end()) { - auto custom_handler = it->second->clone(); - custom_handler->set_padding_info(padding); - formatters_.push_back(std::move(custom_handler)); - return; - } - - // process built-in flags - switch (flag) { - case ('+'): // default formatter - formatters_.push_back(details::make_unique(padding)); - need_localtime_ = true; - break; - - case 'n': // logger name - formatters_.push_back(details::make_unique>(padding)); - break; - - case 'l': // level - formatters_.push_back(details::make_unique>(padding)); - break; - - case 'L': // short level - formatters_.push_back( - details::make_unique>(padding)); - break; - - case ('t'): // thread id - formatters_.push_back(details::make_unique>(padding)); - break; - - case ('v'): // the message text - formatters_.push_back(details::make_unique>(padding)); - break; - - case ('a'): // weekday - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('A'): // short weekday - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('b'): - case ('h'): // month - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('B'): // short month - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('c'): // datetime - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('C'): // year 2 digits - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('Y'): // year 4 digits - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('D'): - case ('x'): // datetime MM/DD/YY - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('m'): // month 1-12 - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('d'): // day of month 1-31 - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('H'): // hours 24 - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('I'): // hours 12 - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('M'): // minutes - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('S'): // seconds - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('e'): // milliseconds - formatters_.push_back(details::make_unique>(padding)); - break; - - case ('f'): // microseconds - formatters_.push_back(details::make_unique>(padding)); - break; - - case ('F'): // nanoseconds - formatters_.push_back(details::make_unique>(padding)); - break; - - case ('E'): // seconds since epoch - formatters_.push_back(details::make_unique>(padding)); - break; - - case ('p'): // am/pm - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('r'): // 12 hour clock 02:55:02 pm - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('R'): // 24-hour HH:MM time - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('T'): - case ('X'): // ISO 8601 time format (HH:MM:SS) - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('z'): // timezone - formatters_.push_back(details::make_unique>(padding)); - need_localtime_ = true; - break; - - case ('P'): // pid - formatters_.push_back(details::make_unique>(padding)); - break; - - case ('^'): // color range start - formatters_.push_back(details::make_unique(padding)); - break; - - case ('$'): // color range end - formatters_.push_back(details::make_unique(padding)); - break; - - case ('@'): // source location (filename:filenumber) - formatters_.push_back( - details::make_unique>(padding)); - break; - - case ('s'): // short source filename - without directory name - formatters_.push_back( - details::make_unique>(padding)); - break; - - case ('g'): // full source filename - formatters_.push_back( - details::make_unique>(padding)); - break; - - case ('#'): // source line number - formatters_.push_back( - details::make_unique>(padding)); - break; - - case ('!'): // source funcname - formatters_.push_back( - details::make_unique>(padding)); - break; - - case ('%'): // % char - formatters_.push_back(details::make_unique('%')); - break; - - case ('u'): // elapsed time since last log message in nanos - formatters_.push_back( - details::make_unique>( - padding)); - break; - - case ('i'): // elapsed time since last log message in micros - formatters_.push_back( - details::make_unique>( - padding)); - break; - - case ('o'): // elapsed time since last log message in millis - formatters_.push_back( - details::make_unique>( - padding)); - break; - - case ('O'): // elapsed time since last log message in seconds - formatters_.push_back( - details::make_unique>( - padding)); - break; - -#ifndef SPDLOG_NO_TLS // mdc formatter requires TLS support - case ('&'): - formatters_.push_back(details::make_unique>(padding)); - break; -#endif - - default: // Unknown flag appears as is - auto unknown_flag = details::make_unique(); - - if (!padding.truncate_) { - unknown_flag->add_ch('%'); - unknown_flag->add_ch(flag); - formatters_.push_back((std::move(unknown_flag))); - } - // fix issue #1617 (prev char was '!' and should have been treated as funcname flag - // instead of truncating flag) spdlog::set_pattern("[%10!] %v") => "[ main] some - // message" spdlog::set_pattern("[%3!!] %v") => "[mai] some message" - else { - padding.truncate_ = false; - formatters_.push_back( - details::make_unique>(padding)); - unknown_flag->add_ch(flag); - formatters_.push_back((std::move(unknown_flag))); - } - - break; - } -} - -// Extract given pad spec (e.g. %8X, %=8X, %-8!X, %8!X, %=8!X, %-8!X, %+8!X) -// Advance the given it pass the end of the padding spec found (if any) -// Return padding. -SPDLOG_INLINE details::padding_info pattern_formatter::handle_padspec_( - std::string::const_iterator &it, std::string::const_iterator end) { - using details::padding_info; - using details::scoped_padder; - const size_t max_width = 64; - if (it == end) { - return padding_info{}; - } - - padding_info::pad_side side; - switch (*it) { - case '-': - side = padding_info::pad_side::right; - ++it; - break; - case '=': - side = padding_info::pad_side::center; - ++it; - break; - default: - side = details::padding_info::pad_side::left; - break; - } - - if (it == end || !std::isdigit(static_cast(*it))) { - return padding_info{}; // no padding if no digit found here - } - - auto width = static_cast(*it) - '0'; - for (++it; it != end && std::isdigit(static_cast(*it)); ++it) { - auto digit = static_cast(*it) - '0'; - width = width * 10 + digit; - } - - // search for the optional truncate marker '!' - bool truncate; - if (it != end && *it == '!') { - truncate = true; - ++it; - } else { - truncate = false; - } - return details::padding_info{std::min(width, max_width), side, truncate}; -} - -SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &pattern) { - auto end = pattern.end(); - std::unique_ptr user_chars; - formatters_.clear(); - for (auto it = pattern.begin(); it != end; ++it) { - if (*it == '%') { - if (user_chars) // append user chars found so far - { - formatters_.push_back(std::move(user_chars)); - } - - auto padding = handle_padspec_(++it, end); - - if (it != end) { - if (padding.enabled()) { - handle_flag_(*it, padding); - } else { - handle_flag_(*it, padding); - } - } else { - break; - } - } else // chars not following the % sign should be displayed as is - { - if (!user_chars) { - user_chars = details::make_unique(); - } - user_chars->add_ch(*it); - } - } - if (user_chars) // append raw chars found so far - { - formatters_.push_back(std::move(user_chars)); - } -} -} // namespace spdlog diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h deleted file mode 100644 index ececd67..0000000 --- a/include/spdlog/pattern_formatter.h +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include - -namespace spdlog { -namespace details { - -// padding information. -struct padding_info { - enum class pad_side { left, right, center }; - - padding_info() = default; - padding_info(size_t width, padding_info::pad_side side, bool truncate) - : width_(width), - side_(side), - truncate_(truncate), - enabled_(true) {} - - bool enabled() const { return enabled_; } - size_t width_ = 0; - pad_side side_ = pad_side::left; - bool truncate_ = false; - bool enabled_ = false; -}; - -class SPDLOG_API flag_formatter { -public: - explicit flag_formatter(padding_info padinfo) - : padinfo_(padinfo) {} - flag_formatter() = default; - virtual ~flag_formatter() = default; - virtual void format(const details::log_msg &msg, - const std::tm &tm_time, - memory_buf_t &dest) = 0; - -protected: - padding_info padinfo_; -}; - -} // namespace details - -class SPDLOG_API custom_flag_formatter : public details::flag_formatter { -public: - virtual std::unique_ptr clone() const = 0; - - void set_padding_info(const details::padding_info &padding) { - flag_formatter::padinfo_ = padding; - } -}; - -class SPDLOG_API pattern_formatter final : public formatter { -public: - using custom_flags = std::unordered_map>; - - explicit pattern_formatter(std::string pattern, - pattern_time_type time_type = pattern_time_type::local, - std::string eol = spdlog::details::os::default_eol, - custom_flags custom_user_flags = custom_flags()); - - // use default pattern is not given - explicit pattern_formatter(pattern_time_type time_type = pattern_time_type::local, - std::string eol = spdlog::details::os::default_eol); - - pattern_formatter(const pattern_formatter &other) = delete; - pattern_formatter &operator=(const pattern_formatter &other) = delete; - - std::unique_ptr clone() const override; - void format(const details::log_msg &msg, memory_buf_t &dest) override; - - template - pattern_formatter &add_flag(char flag, Args &&...args) { - custom_handlers_[flag] = details::make_unique(std::forward(args)...); - return *this; - } - void set_pattern(std::string pattern); - void need_localtime(bool need = true); - -private: - std::string pattern_; - std::string eol_; - pattern_time_type pattern_time_type_; - bool need_localtime_; - std::tm cached_tm_; - std::chrono::seconds last_log_secs_; - std::vector> formatters_; - custom_flags custom_handlers_; - - std::tm get_time_(const details::log_msg &msg); - template - void handle_flag_(char flag, details::padding_info padding); - - // Extract given pad spec (e.g. %8X) - // Advance the given it pass the end of the padding spec found (if any) - // Return padding. - static details::padding_info handle_padspec_(std::string::const_iterator &it, - std::string::const_iterator end); - - void compile_pattern_(const std::string &pattern); -}; -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "pattern_formatter-inl.h" -#endif diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h deleted file mode 100644 index 4435a56..0000000 --- a/include/spdlog/sinks/android_sink.h +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifdef __ANDROID__ - - #include - #include - #include - #include - #include - - #include - #include - #include - #include - #include - #include - - #if !defined(SPDLOG_ANDROID_RETRIES) - #define SPDLOG_ANDROID_RETRIES 2 - #endif - -namespace spdlog { -namespace sinks { - -/* - * Android sink - * (logging using __android_log_write or __android_log_buf_write depending on the specified - * BufferID) - */ -template -class android_sink final : public base_sink { -public: - explicit android_sink(std::string tag = "spdlog", bool use_raw_msg = false) - : tag_(std::move(tag)), - use_raw_msg_(use_raw_msg) {} - -protected: - void sink_it_(const details::log_msg &msg) override { - const android_LogPriority priority = convert_to_android_(msg.level); - memory_buf_t formatted; - if (use_raw_msg_) { - details::fmt_helper::append_string_view(msg.payload, formatted); - } else { - base_sink::formatter_->format(msg, formatted); - } - formatted.push_back('\0'); - const char *msg_output = formatted.data(); - - // See system/core/liblog/logger_write.c for explanation of return value - int ret = android_log(priority, tag_.c_str(), msg_output); - if (ret == -EPERM) { - return; // !__android_log_is_loggable - } - int retry_count = 0; - while ((ret == -11 /*EAGAIN*/) && (retry_count < SPDLOG_ANDROID_RETRIES)) { - details::os::sleep_for_millis(5); - ret = android_log(priority, tag_.c_str(), msg_output); - retry_count++; - } - - if (ret < 0) { - throw_spdlog_ex("logging to Android failed", ret); - } - } - - void flush_() override {} - -private: - // There might be liblog versions used, that do not support __android_log_buf_write. So we only - // compile and link against - // __android_log_buf_write, if user explicitly provides a non-default log buffer. Otherwise, - // when using the default log buffer, always log via __android_log_write. - template - typename std::enable_if(log_id::LOG_ID_MAIN), int>::type android_log( - int prio, const char *tag, const char *text) { - return __android_log_write(prio, tag, text); - } - - template - typename std::enable_if(log_id::LOG_ID_MAIN), int>::type android_log( - int prio, const char *tag, const char *text) { - return __android_log_buf_write(ID, prio, tag, text); - } - - static android_LogPriority convert_to_android_(spdlog::level::level_enum level) { - switch (level) { - case spdlog::level::trace: - return ANDROID_LOG_VERBOSE; - case spdlog::level::debug: - return ANDROID_LOG_DEBUG; - case spdlog::level::info: - return ANDROID_LOG_INFO; - case spdlog::level::warn: - return ANDROID_LOG_WARN; - case spdlog::level::err: - return ANDROID_LOG_ERROR; - case spdlog::level::critical: - return ANDROID_LOG_FATAL; - default: - return ANDROID_LOG_DEFAULT; - } - } - - std::string tag_; - bool use_raw_msg_; -}; - -using android_sink_mt = android_sink; -using android_sink_st = android_sink; - -template -using android_sink_buf_mt = android_sink; -template -using android_sink_buf_st = android_sink; - -} // namespace sinks - -// Create and register android syslog logger - -template -inline std::shared_ptr android_logger_mt(const std::string &logger_name, - const std::string &tag = "spdlog") { - return Factory::template create(logger_name, tag); -} - -template -inline std::shared_ptr android_logger_st(const std::string &logger_name, - const std::string &tag = "spdlog") { - return Factory::template create(logger_name, tag); -} - -} // namespace spdlog - -#endif // __ANDROID__ diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h deleted file mode 100644 index 8fd3078..0000000 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ /dev/null @@ -1,135 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -namespace spdlog { -namespace sinks { - -template -SPDLOG_INLINE ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) - : target_file_(target_file), - mutex_(ConsoleMutex::mutex()), - formatter_(details::make_unique()) - -{ - set_color_mode(mode); - colors_.at(level::trace) = to_string_(white); - colors_.at(level::debug) = to_string_(cyan); - colors_.at(level::info) = to_string_(green); - colors_.at(level::warn) = to_string_(yellow_bold); - colors_.at(level::err) = to_string_(red_bold); - colors_.at(level::critical) = to_string_(bold_on_red); - colors_.at(level::off) = to_string_(reset); -} - -template -SPDLOG_INLINE void ansicolor_sink::set_color(level::level_enum color_level, - string_view_t color) { - std::lock_guard lock(mutex_); - colors_.at(static_cast(color_level)) = to_string_(color); -} - -template -SPDLOG_INLINE void ansicolor_sink::log(const details::log_msg &msg) { - // Wrap the originally formatted message in color codes. - // If color is not supported in the terminal, log as is instead. - std::lock_guard lock(mutex_); - msg.color_range_start = 0; - msg.color_range_end = 0; - memory_buf_t formatted; - formatter_->format(msg, formatted); - if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { - // before color range - print_range_(formatted, 0, msg.color_range_start); - // in color range - print_ccode_(colors_.at(static_cast(msg.level))); - print_range_(formatted, msg.color_range_start, msg.color_range_end); - print_ccode_(reset); - // after color range - print_range_(formatted, msg.color_range_end, formatted.size()); - } else // no color - { - print_range_(formatted, 0, formatted.size()); - } - fflush(target_file_); -} - -template -SPDLOG_INLINE void ansicolor_sink::flush() { - std::lock_guard lock(mutex_); - fflush(target_file_); -} - -template -SPDLOG_INLINE void ansicolor_sink::set_pattern(const std::string &pattern) { - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - -template -SPDLOG_INLINE void ansicolor_sink::set_formatter( - std::unique_ptr sink_formatter) { - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); -} - -template -SPDLOG_INLINE bool ansicolor_sink::should_color() { - return should_do_colors_; -} - -template -SPDLOG_INLINE void ansicolor_sink::set_color_mode(color_mode mode) { - switch (mode) { - case color_mode::always: - should_do_colors_ = true; - return; - case color_mode::automatic: - should_do_colors_ = - details::os::in_terminal(target_file_) && details::os::is_color_terminal(); - return; - case color_mode::never: - should_do_colors_ = false; - return; - default: - should_do_colors_ = false; - } -} - -template -SPDLOG_INLINE void ansicolor_sink::print_ccode_(const string_view_t &color_code) { - details::os::fwrite_bytes(color_code.data(), color_code.size(), target_file_); -} - -template -SPDLOG_INLINE void ansicolor_sink::print_range_(const memory_buf_t &formatted, - size_t start, - size_t end) { - details::os::fwrite_bytes(formatted.data() + start, end - start, target_file_); -} - -template -SPDLOG_INLINE std::string ansicolor_sink::to_string_(const string_view_t &sv) { - return std::string(sv.data(), sv.size()); -} - -// ansicolor_stdout_sink -template -SPDLOG_INLINE ansicolor_stdout_sink::ansicolor_stdout_sink(color_mode mode) - : ansicolor_sink(stdout, mode) {} - -// ansicolor_stderr_sink -template -SPDLOG_INLINE ansicolor_stderr_sink::ansicolor_stderr_sink(color_mode mode) - : ansicolor_sink(stderr, mode) {} - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/ansicolor_sink.h b/include/spdlog/sinks/ansicolor_sink.h deleted file mode 100644 index d0dadd7..0000000 --- a/include/spdlog/sinks/ansicolor_sink.h +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -/** - * This sink prefixes the output with an ANSI escape sequence color code - * depending on the severity - * of the message. - * If no color terminal detected, omit the escape codes. - */ - -template -class ansicolor_sink : public sink { -public: - using mutex_t = typename ConsoleMutex::mutex_t; - ansicolor_sink(FILE *target_file, color_mode mode); - ~ansicolor_sink() override = default; - - ansicolor_sink(const ansicolor_sink &other) = delete; - ansicolor_sink(ansicolor_sink &&other) = delete; - - ansicolor_sink &operator=(const ansicolor_sink &other) = delete; - ansicolor_sink &operator=(ansicolor_sink &&other) = delete; - - void set_color(level::level_enum color_level, string_view_t color); - void set_color_mode(color_mode mode); - bool should_color(); - - void log(const details::log_msg &msg) override; - void flush() override; - void set_pattern(const std::string &pattern) final override; - void set_formatter(std::unique_ptr sink_formatter) override; - - // Formatting codes - const string_view_t reset = "\033[m"; - const string_view_t bold = "\033[1m"; - const string_view_t dark = "\033[2m"; - const string_view_t underline = "\033[4m"; - const string_view_t blink = "\033[5m"; - const string_view_t reverse = "\033[7m"; - const string_view_t concealed = "\033[8m"; - const string_view_t clear_line = "\033[K"; - - // Foreground colors - const string_view_t black = "\033[30m"; - const string_view_t red = "\033[31m"; - const string_view_t green = "\033[32m"; - const string_view_t yellow = "\033[33m"; - const string_view_t blue = "\033[34m"; - const string_view_t magenta = "\033[35m"; - const string_view_t cyan = "\033[36m"; - const string_view_t white = "\033[37m"; - - /// Background colors - const string_view_t on_black = "\033[40m"; - const string_view_t on_red = "\033[41m"; - const string_view_t on_green = "\033[42m"; - const string_view_t on_yellow = "\033[43m"; - const string_view_t on_blue = "\033[44m"; - const string_view_t on_magenta = "\033[45m"; - const string_view_t on_cyan = "\033[46m"; - const string_view_t on_white = "\033[47m"; - - /// Bold colors - const string_view_t yellow_bold = "\033[33m\033[1m"; - const string_view_t red_bold = "\033[31m\033[1m"; - const string_view_t bold_on_red = "\033[1m\033[41m"; - -private: - FILE *target_file_; - mutex_t &mutex_; - bool should_do_colors_; - std::unique_ptr formatter_; - std::array colors_; - void print_ccode_(const string_view_t &color_code); - void print_range_(const memory_buf_t &formatted, size_t start, size_t end); - static std::string to_string_(const string_view_t &sv); -}; - -template -class ansicolor_stdout_sink : public ansicolor_sink { -public: - explicit ansicolor_stdout_sink(color_mode mode = color_mode::automatic); -}; - -template -class ansicolor_stderr_sink : public ansicolor_sink { -public: - explicit ansicolor_stderr_sink(color_mode mode = color_mode::automatic); -}; - -using ansicolor_stdout_sink_mt = ansicolor_stdout_sink; -using ansicolor_stdout_sink_st = ansicolor_stdout_sink; - -using ansicolor_stderr_sink_mt = ansicolor_stderr_sink; -using ansicolor_stderr_sink_st = ansicolor_stderr_sink; - -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "ansicolor_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h deleted file mode 100644 index ada161b..0000000 --- a/include/spdlog/sinks/base_sink-inl.h +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -#include -#include - -template -SPDLOG_INLINE spdlog::sinks::base_sink::base_sink() - : formatter_{details::make_unique()} {} - -template -SPDLOG_INLINE spdlog::sinks::base_sink::base_sink( - std::unique_ptr formatter) - : formatter_{std::move(formatter)} {} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::log(const details::log_msg &msg) { - std::lock_guard lock(mutex_); - sink_it_(msg); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::flush() { - std::lock_guard lock(mutex_); - flush_(); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern(const std::string &pattern) { - std::lock_guard lock(mutex_); - set_pattern_(pattern); -} - -template -void SPDLOG_INLINE -spdlog::sinks::base_sink::set_formatter(std::unique_ptr sink_formatter) { - std::lock_guard lock(mutex_); - set_formatter_(std::move(sink_formatter)); -} - -template -void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) { - set_formatter_(details::make_unique(pattern)); -} - -template -void SPDLOG_INLINE -spdlog::sinks::base_sink::set_formatter_(std::unique_ptr sink_formatter) { - formatter_ = std::move(sink_formatter); -} diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h deleted file mode 100644 index 1b4bb06..0000000 --- a/include/spdlog/sinks/base_sink.h +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once -// -// base sink templated over a mutex (either dummy or real) -// concrete implementation should override the sink_it_() and flush_() methods. -// locking is taken care of in this class - no locking needed by the -// implementers.. -// - -#include -#include -#include - -namespace spdlog { -namespace sinks { -template -class SPDLOG_API base_sink : public sink { -public: - base_sink(); - explicit base_sink(std::unique_ptr formatter); - ~base_sink() override = default; - - base_sink(const base_sink &) = delete; - base_sink(base_sink &&) = delete; - - base_sink &operator=(const base_sink &) = delete; - base_sink &operator=(base_sink &&) = delete; - - void log(const details::log_msg &msg) final override; - void flush() final override; - void set_pattern(const std::string &pattern) final override; - void set_formatter(std::unique_ptr sink_formatter) final override; - -protected: - // sink formatter - std::unique_ptr formatter_; - Mutex mutex_; - - virtual void sink_it_(const details::log_msg &msg) = 0; - virtual void flush_() = 0; - virtual void set_pattern_(const std::string &pattern); - virtual void set_formatter_(std::unique_ptr sink_formatter); -}; -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "base_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/basic_file_sink-inl.h b/include/spdlog/sinks/basic_file_sink-inl.h deleted file mode 100644 index ce0ddad..0000000 --- a/include/spdlog/sinks/basic_file_sink-inl.h +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -namespace spdlog { -namespace sinks { - -template -SPDLOG_INLINE basic_file_sink::basic_file_sink(const filename_t &filename, - bool truncate, - const file_event_handlers &event_handlers) - : file_helper_{event_handlers} { - file_helper_.open(filename, truncate); -} - -template -SPDLOG_INLINE const filename_t &basic_file_sink::filename() const { - return file_helper_.filename(); -} - -template -SPDLOG_INLINE void basic_file_sink::truncate() { - std::lock_guard lock(base_sink::mutex_); - file_helper_.reopen(true); -} - -template -SPDLOG_INLINE void basic_file_sink::sink_it_(const details::log_msg &msg) { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - file_helper_.write(formatted); -} - -template -SPDLOG_INLINE void basic_file_sink::flush_() { - file_helper_.flush(); -} - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/basic_file_sink.h b/include/spdlog/sinks/basic_file_sink.h deleted file mode 100644 index 48c0767..0000000 --- a/include/spdlog/sinks/basic_file_sink.h +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#include - -namespace spdlog { -namespace sinks { -/* - * Trivial file sink with single file as target - */ -template -class basic_file_sink final : public base_sink { -public: - explicit basic_file_sink(const filename_t &filename, - bool truncate = false, - const file_event_handlers &event_handlers = {}); - const filename_t &filename() const; - void truncate(); - -protected: - void sink_it_(const details::log_msg &msg) override; - void flush_() override; - -private: - details::file_helper file_helper_; -}; - -using basic_file_sink_mt = basic_file_sink; -using basic_file_sink_st = basic_file_sink; - -} // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr basic_logger_mt(const std::string &logger_name, - const filename_t &filename, - bool truncate = false, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, truncate, - event_handlers); -} - -template -inline std::shared_ptr basic_logger_st(const std::string &logger_name, - const filename_t &filename, - bool truncate = false, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, truncate, - event_handlers); -} - -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "basic_file_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/callback_sink.h b/include/spdlog/sinks/callback_sink.h deleted file mode 100644 index 5f8b6bc..0000000 --- a/include/spdlog/sinks/callback_sink.h +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include - -#include -#include - -namespace spdlog { - -// callbacks type -typedef std::function custom_log_callback; - -namespace sinks { -/* - * Trivial callback sink, gets a callback function and calls it on each log - */ -template -class callback_sink final : public base_sink { -public: - explicit callback_sink(const custom_log_callback &callback) - : callback_{callback} {} - -protected: - void sink_it_(const details::log_msg &msg) override { callback_(msg); } - void flush_() override{} - -private: - custom_log_callback callback_; -}; - -using callback_sink_mt = callback_sink; -using callback_sink_st = callback_sink; - -} // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr callback_logger_mt(const std::string &logger_name, - const custom_log_callback &callback) { - return Factory::template create(logger_name, callback); -} - -template -inline std::shared_ptr callback_logger_st(const std::string &logger_name, - const custom_log_callback &callback) { - return Factory::template create(logger_name, callback); -} - -} // namespace spdlog diff --git a/include/spdlog/sinks/daily_file_sink.h b/include/spdlog/sinks/daily_file_sink.h deleted file mode 100644 index 615c9f7..0000000 --- a/include/spdlog/sinks/daily_file_sink.h +++ /dev/null @@ -1,254 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -/* - * Generator of daily log file names in format basename.YYYY-MM-DD.ext - */ -struct daily_filename_calculator { - // Create filename for the form basename.YYYY-MM-DD - static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { - filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}")), - basename, now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, - ext); - } -}; - -/* - * Generator of daily log file names with strftime format. - * Usages: - * auto sink = - * std::make_shared("myapp-%Y-%m-%d:%H:%M:%S.log", hour, - * minute);" auto logger = spdlog::daily_logger_format_mt("loggername, "myapp-%Y-%m-%d:%X.log", - * hour, minute)" - * - */ -struct daily_filename_format_calculator { - static filename_t calc_filename(const filename_t &file_path, const tm &now_tm) { -#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) - std::wstringstream stream; -#else - std::stringstream stream; -#endif - stream << std::put_time(&now_tm, file_path.c_str()); - return stream.str(); - } -}; - -/* - * Rotating file sink based on date. - * If truncate != false , the created file will be truncated. - * If max_files > 0, retain only the last max_files and delete previous. - * Note that old log files from previous executions will not be deleted by this class, - * rotation and deletion is only applied while the program is running. - */ -template -class daily_file_sink final : public base_sink { -public: - // create daily file sink which rotates on given time - daily_file_sink(filename_t base_filename, - int rotation_hour, - int rotation_minute, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) - : base_filename_(std::move(base_filename)), - rotation_h_(rotation_hour), - rotation_m_(rotation_minute), - file_helper_{event_handlers}, - truncate_(truncate), - max_files_(max_files), - filenames_q_() { - if (rotation_hour < 0 || rotation_hour > 23 || rotation_minute < 0 || - rotation_minute > 59) { - throw_spdlog_ex("daily_file_sink: Invalid rotation time in ctor"); - } - - auto now = log_clock::now(); - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - file_helper_.open(filename, truncate_); - rotation_tp_ = next_rotation_tp_(); - - if (max_files_ > 0) { - init_filenames_q_(); - } - } - - filename_t filename() { - std::lock_guard lock(base_sink::mutex_); - return file_helper_.filename(); - } - -protected: - void sink_it_(const details::log_msg &msg) override { - auto time = msg.time; - bool should_rotate = time >= rotation_tp_; - if (should_rotate) { - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); - file_helper_.open(filename, truncate_); - rotation_tp_ = next_rotation_tp_(); - } - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - file_helper_.write(formatted); - - // Do the cleaning only at the end because it might throw on failure. - if (should_rotate && max_files_ > 0) { - delete_old_(); - } - } - - void flush_() override { file_helper_.flush(); } - -private: - void init_filenames_q_() { - using details::os::path_exists; - - filenames_q_ = details::circular_q(static_cast(max_files_)); - std::vector filenames; - auto now = log_clock::now(); - while (filenames.size() < max_files_) { - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - if (!path_exists(filename)) { - break; - } - filenames.emplace_back(filename); - now -= std::chrono::hours(24); - } - for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) { - filenames_q_.push_back(std::move(*iter)); - } - } - - tm now_tm(log_clock::time_point tp) { - time_t tnow = log_clock::to_time_t(tp); - return spdlog::details::os::localtime(tnow); - } - - log_clock::time_point next_rotation_tp_() { - auto now = log_clock::now(); - tm date = now_tm(now); - date.tm_hour = rotation_h_; - date.tm_min = rotation_m_; - date.tm_sec = 0; - auto rotation_time = log_clock::from_time_t(std::mktime(&date)); - if (rotation_time > now) { - return rotation_time; - } - return {rotation_time + std::chrono::hours(24)}; - } - - // Delete the file N rotations ago. - // Throw spdlog_ex on failure to delete the old file. - void delete_old_() { - using details::os::filename_to_str; - using details::os::remove_if_exists; - - filename_t current_file = file_helper_.filename(); - if (filenames_q_.full()) { - auto old_filename = std::move(filenames_q_.front()); - filenames_q_.pop_front(); - bool ok = remove_if_exists(old_filename) == 0; - if (!ok) { - filenames_q_.push_back(std::move(current_file)); - throw_spdlog_ex("Failed removing daily file " + filename_to_str(old_filename), - errno); - } - } - filenames_q_.push_back(std::move(current_file)); - } - - filename_t base_filename_; - int rotation_h_; - int rotation_m_; - log_clock::time_point rotation_tp_; - details::file_helper file_helper_; - bool truncate_; - uint16_t max_files_; - details::circular_q filenames_q_; -}; - -using daily_file_sink_mt = daily_file_sink; -using daily_file_sink_st = daily_file_sink; -using daily_file_format_sink_mt = daily_file_sink; -using daily_file_format_sink_st = - daily_file_sink; - -} // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr daily_logger_mt(const std::string &logger_name, - const filename_t &filename, - int hour = 0, - int minute = 0, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, hour, minute, - truncate, max_files, event_handlers); -} - -template -inline std::shared_ptr daily_logger_format_mt( - const std::string &logger_name, - const filename_t &filename, - int hour = 0, - int minute = 0, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create( - logger_name, filename, hour, minute, truncate, max_files, event_handlers); -} - -template -inline std::shared_ptr daily_logger_st(const std::string &logger_name, - const filename_t &filename, - int hour = 0, - int minute = 0, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, hour, minute, - truncate, max_files, event_handlers); -} - -template -inline std::shared_ptr daily_logger_format_st( - const std::string &logger_name, - const filename_t &filename, - int hour = 0, - int minute = 0, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create( - logger_name, filename, hour, minute, truncate, max_files, event_handlers); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h deleted file mode 100644 index 69c4971..0000000 --- a/include/spdlog/sinks/dist_sink.h +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include "base_sink.h" -#include -#include -#include - -#include -#include -#include -#include - -// Distribution sink (mux). Stores a vector of sinks which get called when log -// is called - -namespace spdlog { -namespace sinks { - -template -class dist_sink : public base_sink { -public: - dist_sink() = default; - explicit dist_sink(std::vector> sinks) - : sinks_(sinks) {} - - dist_sink(const dist_sink &) = delete; - dist_sink &operator=(const dist_sink &) = delete; - - void add_sink(std::shared_ptr sub_sink) { - std::lock_guard lock(base_sink::mutex_); - sinks_.push_back(sub_sink); - } - - void remove_sink(std::shared_ptr sub_sink) { - std::lock_guard lock(base_sink::mutex_); - sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sub_sink), sinks_.end()); - } - - void set_sinks(std::vector> sinks) { - std::lock_guard lock(base_sink::mutex_); - sinks_ = std::move(sinks); - } - - std::vector> &sinks() { return sinks_; } - -protected: - void sink_it_(const details::log_msg &msg) override { - for (auto &sub_sink : sinks_) { - if (sub_sink->should_log(msg.level)) { - sub_sink->log(msg); - } - } - } - - void flush_() override { - for (auto &sub_sink : sinks_) { - sub_sink->flush(); - } - } - - void set_pattern_(const std::string &pattern) override { - set_formatter_(details::make_unique(pattern)); - } - - void set_formatter_(std::unique_ptr sink_formatter) override { - base_sink::formatter_ = std::move(sink_formatter); - for (auto &sub_sink : sinks_) { - sub_sink->set_formatter(base_sink::formatter_->clone()); - } - } - std::vector> sinks_; -}; - -using dist_sink_mt = dist_sink; -using dist_sink_st = dist_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/dup_filter_sink.h b/include/spdlog/sinks/dup_filter_sink.h deleted file mode 100644 index 1498142..0000000 --- a/include/spdlog/sinks/dup_filter_sink.h +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include "dist_sink.h" -#include -#include - -#include -#include -#include -#include - -// Duplicate message removal sink. -// Skip the message if previous one is identical and less than "max_skip_duration" have passed -// -// Example: -// -// #include -// -// int main() { -// auto dup_filter = std::make_shared(std::chrono::seconds(5), -// level::info); dup_filter->add_sink(std::make_shared()); -// spdlog::logger l("logger", dup_filter); -// l.info("Hello"); -// l.info("Hello"); -// l.info("Hello"); -// l.info("Different Hello"); -// } -// -// Will produce: -// [2019-06-25 17:50:56.511] [logger] [info] Hello -// [2019-06-25 17:50:56.512] [logger] [info] Skipped 3 duplicate messages.. -// [2019-06-25 17:50:56.512] [logger] [info] Different Hello - -namespace spdlog { -namespace sinks { -template -class dup_filter_sink : public dist_sink { -public: - template - explicit dup_filter_sink(std::chrono::duration max_skip_duration, - level::level_enum notification_level = level::info) - : max_skip_duration_{max_skip_duration}, - log_level_{notification_level} {} - -protected: - std::chrono::microseconds max_skip_duration_; - log_clock::time_point last_msg_time_; - std::string last_msg_payload_; - size_t skip_counter_ = 0; - level::level_enum log_level_; - - void sink_it_(const details::log_msg &msg) override { - bool filtered = filter_(msg); - if (!filtered) { - skip_counter_ += 1; - return; - } - - // log the "skipped.." message - if (skip_counter_ > 0) { - char buf[64]; - auto msg_size = ::snprintf(buf, sizeof(buf), "Skipped %u duplicate messages..", - static_cast(skip_counter_)); - if (msg_size > 0 && static_cast(msg_size) < sizeof(buf)) { - details::log_msg skipped_msg{msg.source, msg.logger_name, log_level_, - string_view_t{buf, static_cast(msg_size)}}; - dist_sink::sink_it_(skipped_msg); - } - } - - // log current message - dist_sink::sink_it_(msg); - last_msg_time_ = msg.time; - skip_counter_ = 0; - last_msg_payload_.assign(msg.payload.data(), msg.payload.data() + msg.payload.size()); - } - - // return whether the log msg should be displayed (true) or skipped (false) - bool filter_(const details::log_msg &msg) { - auto filter_duration = msg.time - last_msg_time_; - return (filter_duration > max_skip_duration_) || (msg.payload != last_msg_payload_); - } -}; - -using dup_filter_sink_mt = dup_filter_sink; -using dup_filter_sink_st = dup_filter_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/hourly_file_sink.h b/include/spdlog/sinks/hourly_file_sink.h deleted file mode 100644 index 3e61872..0000000 --- a/include/spdlog/sinks/hourly_file_sink.h +++ /dev/null @@ -1,193 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -/* - * Generator of Hourly log file names in format basename.YYYY-MM-DD-HH.ext - */ -struct hourly_filename_calculator { - // Create filename for the form basename.YYYY-MM-DD-H - static filename_t calc_filename(const filename_t &filename, const tm &now_tm) { - filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - return fmt_lib::format(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}{}"), basename, - now_tm.tm_year + 1900, now_tm.tm_mon + 1, now_tm.tm_mday, - now_tm.tm_hour, ext); - } -}; - -/* - * Rotating file sink based on time. - * If truncate != false , the created file will be truncated. - * If max_files > 0, retain only the last max_files and delete previous. - * Note that old log files from previous executions will not be deleted by this class, - * rotation and deletion is only applied while the program is running. - */ -template -class hourly_file_sink final : public base_sink { -public: - // create hourly file sink which rotates on given time - hourly_file_sink(filename_t base_filename, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) - : base_filename_(std::move(base_filename)), - file_helper_{event_handlers}, - truncate_(truncate), - max_files_(max_files), - filenames_q_() { - auto now = log_clock::now(); - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - file_helper_.open(filename, truncate_); - remove_init_file_ = file_helper_.size() == 0; - rotation_tp_ = next_rotation_tp_(); - - if (max_files_ > 0) { - init_filenames_q_(); - } - } - - filename_t filename() { - std::lock_guard lock(base_sink::mutex_); - return file_helper_.filename(); - } - -protected: - void sink_it_(const details::log_msg &msg) override { - auto time = msg.time; - bool should_rotate = time >= rotation_tp_; - if (should_rotate) { - if (remove_init_file_) { - file_helper_.close(); - details::os::remove(file_helper_.filename()); - } - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(time)); - file_helper_.open(filename, truncate_); - rotation_tp_ = next_rotation_tp_(); - } - remove_init_file_ = false; - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - file_helper_.write(formatted); - - // Do the cleaning only at the end because it might throw on failure. - if (should_rotate && max_files_ > 0) { - delete_old_(); - } - } - - void flush_() override { file_helper_.flush(); } - -private: - void init_filenames_q_() { - using details::os::path_exists; - - filenames_q_ = details::circular_q(static_cast(max_files_)); - std::vector filenames; - auto now = log_clock::now(); - while (filenames.size() < max_files_) { - auto filename = FileNameCalc::calc_filename(base_filename_, now_tm(now)); - if (!path_exists(filename)) { - break; - } - filenames.emplace_back(filename); - now -= std::chrono::hours(1); - } - for (auto iter = filenames.rbegin(); iter != filenames.rend(); ++iter) { - filenames_q_.push_back(std::move(*iter)); - } - } - - tm now_tm(log_clock::time_point tp) { - time_t tnow = log_clock::to_time_t(tp); - return spdlog::details::os::localtime(tnow); - } - - log_clock::time_point next_rotation_tp_() { - auto now = log_clock::now(); - tm date = now_tm(now); - date.tm_min = 0; - date.tm_sec = 0; - auto rotation_time = log_clock::from_time_t(std::mktime(&date)); - if (rotation_time > now) { - return rotation_time; - } - return {rotation_time + std::chrono::hours(1)}; - } - - // Delete the file N rotations ago. - // Throw spdlog_ex on failure to delete the old file. - void delete_old_() { - using details::os::filename_to_str; - using details::os::remove_if_exists; - - filename_t current_file = file_helper_.filename(); - if (filenames_q_.full()) { - auto old_filename = std::move(filenames_q_.front()); - filenames_q_.pop_front(); - bool ok = remove_if_exists(old_filename) == 0; - if (!ok) { - filenames_q_.push_back(std::move(current_file)); - SPDLOG_THROW(spdlog_ex( - "Failed removing hourly file " + filename_to_str(old_filename), errno)); - } - } - filenames_q_.push_back(std::move(current_file)); - } - - filename_t base_filename_; - log_clock::time_point rotation_tp_; - details::file_helper file_helper_; - bool truncate_; - uint16_t max_files_; - details::circular_q filenames_q_; - bool remove_init_file_; -}; - -using hourly_file_sink_mt = hourly_file_sink; -using hourly_file_sink_st = hourly_file_sink; - -} // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr hourly_logger_mt(const std::string &logger_name, - const filename_t &filename, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, truncate, - max_files, event_handlers); -} - -template -inline std::shared_ptr hourly_logger_st(const std::string &logger_name, - const filename_t &filename, - bool truncate = false, - uint16_t max_files = 0, - const file_event_handlers &event_handlers = {}) { - return Factory::template create(logger_name, filename, truncate, - max_files, event_handlers); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/kafka_sink.h b/include/spdlog/sinks/kafka_sink.h deleted file mode 100644 index 91e9878..0000000 --- a/include/spdlog/sinks/kafka_sink.h +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// -// Custom sink for kafka -// Building and using requires librdkafka library. -// For building librdkafka library check the url below -// https://github.com/confluentinc/librdkafka -// - -#include "spdlog/async.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/details/synchronous_factory.h" -#include "spdlog/sinks/base_sink.h" -#include -#include - -// kafka header -#include - -namespace spdlog { -namespace sinks { - -struct kafka_sink_config { - std::string server_addr; - std::string produce_topic; - int32_t flush_timeout_ms = 1000; - - kafka_sink_config(std::string addr, std::string topic, int flush_timeout_ms = 1000) - : server_addr{std::move(addr)}, - produce_topic{std::move(topic)}, - flush_timeout_ms(flush_timeout_ms) {} -}; - -template -class kafka_sink : public base_sink { -public: - kafka_sink(kafka_sink_config config) - : config_{std::move(config)} { - try { - std::string errstr; - conf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_GLOBAL)); - RdKafka::Conf::ConfResult confRes = - conf_->set("bootstrap.servers", config_.server_addr, errstr); - if (confRes != RdKafka::Conf::CONF_OK) { - throw_spdlog_ex( - fmt_lib::format("conf set bootstrap.servers failed err:{}", errstr)); - } - - tconf_.reset(RdKafka::Conf::create(RdKafka::Conf::CONF_TOPIC)); - if (tconf_ == nullptr) { - throw_spdlog_ex(fmt_lib::format("create topic config failed")); - } - - producer_.reset(RdKafka::Producer::create(conf_.get(), errstr)); - if (producer_ == nullptr) { - throw_spdlog_ex(fmt_lib::format("create producer failed err:{}", errstr)); - } - topic_.reset(RdKafka::Topic::create(producer_.get(), config_.produce_topic, - tconf_.get(), errstr)); - if (topic_ == nullptr) { - throw_spdlog_ex(fmt_lib::format("create topic failed err:{}", errstr)); - } - } catch (const std::exception &e) { - throw_spdlog_ex(fmt_lib::format("error create kafka instance: {}", e.what())); - } - } - - ~kafka_sink() { producer_->flush(config_.flush_timeout_ms); } - -protected: - void sink_it_(const details::log_msg &msg) override { - producer_->produce(topic_.get(), 0, RdKafka::Producer::RK_MSG_COPY, - (void *)msg.payload.data(), msg.payload.size(), NULL, NULL); - } - - void flush_() override { producer_->flush(config_.flush_timeout_ms); } - -private: - kafka_sink_config config_; - std::unique_ptr producer_ = nullptr; - std::unique_ptr conf_ = nullptr; - std::unique_ptr tconf_ = nullptr; - std::unique_ptr topic_ = nullptr; -}; - -using kafka_sink_mt = kafka_sink; -using kafka_sink_st = kafka_sink; - -} // namespace sinks - -template -inline std::shared_ptr kafka_logger_mt(const std::string &logger_name, - spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - -template -inline std::shared_ptr kafka_logger_st(const std::string &logger_name, - spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - -template -inline std::shared_ptr kafka_logger_async_mt( - std::string logger_name, spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - -template -inline std::shared_ptr kafka_logger_async_st( - std::string logger_name, spdlog::sinks::kafka_sink_config config) { - return Factory::template create(logger_name, config); -} - -} // namespace spdlog diff --git a/include/spdlog/sinks/mongo_sink.h b/include/spdlog/sinks/mongo_sink.h deleted file mode 100644 index c5b38ab..0000000 --- a/include/spdlog/sinks/mongo_sink.h +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// -// Custom sink for mongodb -// Building and using requires mongocxx library. -// For building mongocxx library check the url below -// http://mongocxx.org/mongocxx-v3/installation/ -// - -#include "spdlog/common.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/sinks/base_sink.h" -#include - -#include -#include -#include - -#include -#include -#include - -namespace spdlog { -namespace sinks { -template -class mongo_sink : public base_sink { -public: - mongo_sink(const std::string &db_name, - const std::string &collection_name, - const std::string &uri = "mongodb://localhost:27017") try - : mongo_sink(std::make_shared(), db_name, collection_name, uri) { - } catch (const std::exception &e) { - throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what())); - } - - mongo_sink(std::shared_ptr instance, - const std::string &db_name, - const std::string &collection_name, - const std::string &uri = "mongodb://localhost:27017") - : instance_(std::move(instance)), - db_name_(db_name), - coll_name_(collection_name) { - try { - client_ = spdlog::details::make_unique(mongocxx::uri{uri}); - } catch (const std::exception &e) { - throw_spdlog_ex(fmt_lib::format("Error opening database: {}", e.what())); - } - } - - ~mongo_sink() { flush_(); } - -protected: - void sink_it_(const details::log_msg &msg) override { - using bsoncxx::builder::stream::document; - using bsoncxx::builder::stream::finalize; - - if (client_ != nullptr) { - auto doc = document{} << "timestamp" << bsoncxx::types::b_date(msg.time) << "level" - << level::to_string_view(msg.level).data() << "level_num" - << msg.level << "message" - << std::string(msg.payload.begin(), msg.payload.end()) - << "logger_name" - << std::string(msg.logger_name.begin(), msg.logger_name.end()) - << "thread_id" << static_cast(msg.thread_id) << finalize; - client_->database(db_name_).collection(coll_name_).insert_one(doc.view()); - } - } - - void flush_() override {} - -private: - std::shared_ptr instance_; - std::string db_name_; - std::string coll_name_; - std::unique_ptr client_ = nullptr; -}; - -#include "spdlog/details/null_mutex.h" -#include -using mongo_sink_mt = mongo_sink; -using mongo_sink_st = mongo_sink; - -} // namespace sinks - -template -inline std::shared_ptr mongo_logger_mt( - const std::string &logger_name, - const std::string &db_name, - const std::string &collection_name, - const std::string &uri = "mongodb://localhost:27017") { - return Factory::template create(logger_name, db_name, collection_name, - uri); -} - -template -inline std::shared_ptr mongo_logger_st( - const std::string &logger_name, - const std::string &db_name, - const std::string &collection_name, - const std::string &uri = "mongodb://localhost:27017") { - return Factory::template create(logger_name, db_name, collection_name, - uri); -} - -} // namespace spdlog diff --git a/include/spdlog/sinks/msvc_sink.h b/include/spdlog/sinks/msvc_sink.h deleted file mode 100644 index c28d6eb..0000000 --- a/include/spdlog/sinks/msvc_sink.h +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright(c) 2016 Alexander Dalshov & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#if defined(_WIN32) - - #include - #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) - #include - #endif - #include - - #include - #include - - // Avoid including windows.h (https://stackoverflow.com/a/30741042) - #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) -extern "C" __declspec(dllimport) void __stdcall OutputDebugStringW(const wchar_t *lpOutputString); - #else -extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString); - #endif -extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); - -namespace spdlog { -namespace sinks { -/* - * MSVC sink (logging using OutputDebugStringA) - */ -template -class msvc_sink : public base_sink { -public: - msvc_sink() = default; - msvc_sink(bool check_debugger_present) - : check_debugger_present_{check_debugger_present} {} - -protected: - void sink_it_(const details::log_msg &msg) override { - if (check_debugger_present_ && !IsDebuggerPresent()) { - return; - } - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - formatted.push_back('\0'); // add a null terminator for OutputDebugString - #if defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) - wmemory_buf_t wformatted; - details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), wformatted); - OutputDebugStringW(wformatted.data()); - #else - OutputDebugStringA(formatted.data()); - #endif - } - - void flush_() override {} - - bool check_debugger_present_ = true; -}; - -using msvc_sink_mt = msvc_sink; -using msvc_sink_st = msvc_sink; - -using windebug_sink_mt = msvc_sink_mt; -using windebug_sink_st = msvc_sink_st; - -} // namespace sinks -} // namespace spdlog - -#endif diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h deleted file mode 100644 index 74530b5..0000000 --- a/include/spdlog/sinks/null_sink.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include - -#include - -namespace spdlog { -namespace sinks { - -template -class null_sink final : public base_sink { -protected: - void sink_it_(const details::log_msg &) override {} - void flush_() override {} -}; - -using null_sink_mt = null_sink; -using null_sink_st = null_sink; - -} // namespace sinks - -template -inline std::shared_ptr null_logger_mt(const std::string &logger_name) { - auto null_logger = Factory::template create(logger_name); - null_logger->set_level(level::off); - return null_logger; -} - -template -inline std::shared_ptr null_logger_st(const std::string &logger_name) { - auto null_logger = Factory::template create(logger_name); - null_logger->set_level(level::off); - return null_logger; -} - -} // namespace spdlog diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h deleted file mode 100644 index 6af9dd0..0000000 --- a/include/spdlog/sinks/ostream_sink.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -#include -#include - -namespace spdlog { -namespace sinks { -template -class ostream_sink final : public base_sink { -public: - explicit ostream_sink(std::ostream &os, bool force_flush = false) - : ostream_(os), - force_flush_(force_flush) {} - ostream_sink(const ostream_sink &) = delete; - ostream_sink &operator=(const ostream_sink &) = delete; - -protected: - void sink_it_(const details::log_msg &msg) override { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - ostream_.write(formatted.data(), static_cast(formatted.size())); - if (force_flush_) { - ostream_.flush(); - } - } - - void flush_() override { ostream_.flush(); } - - std::ostream &ostream_; - bool force_flush_; -}; - -using ostream_sink_mt = ostream_sink; -using ostream_sink_st = ostream_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/qt_sinks.h b/include/spdlog/sinks/qt_sinks.h deleted file mode 100644 index d319e84..0000000 --- a/include/spdlog/sinks/qt_sinks.h +++ /dev/null @@ -1,304 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman, mguludag and spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -// -// Custom sink for QPlainTextEdit or QTextEdit and its children (QTextBrowser... -// etc) Building and using requires Qt library. -// -// Warning: the qt_sink won't be notified if the target widget is destroyed. -// If the widget's lifetime can be shorter than the logger's one, you should provide some permanent -// QObject, and then use a standard signal/slot. -// - -#include "spdlog/common.h" -#include "spdlog/details/log_msg.h" -#include "spdlog/details/synchronous_factory.h" -#include "spdlog/sinks/base_sink.h" -#include - -#include -#include - -// -// qt_sink class -// -namespace spdlog { -namespace sinks { -template -class qt_sink : public base_sink { -public: - qt_sink(QObject *qt_object, std::string meta_method) - : qt_object_(qt_object), - meta_method_(std::move(meta_method)) { - if (!qt_object_) { - throw_spdlog_ex("qt_sink: qt_object is null"); - } - } - - ~qt_sink() { flush_(); } - -protected: - void sink_it_(const details::log_msg &msg) override { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - const string_view_t str = string_view_t(formatted.data(), formatted.size()); - QMetaObject::invokeMethod( - qt_object_, meta_method_.c_str(), Qt::AutoConnection, - Q_ARG(QString, QString::fromUtf8(str.data(), static_cast(str.size())).trimmed())); - } - - void flush_() override {} - -private: - QObject *qt_object_ = nullptr; - std::string meta_method_; -}; - -// Qt color sink to QTextEdit. -// Color location is determined by the sink log pattern like in the rest of spdlog sinks. -// Colors can be modified if needed using sink->set_color(level, qtTextCharFormat). -// max_lines is the maximum number of lines that the sink will hold before removing the oldest -// lines. By default, only ascii (latin1) is supported by this sink. Set is_utf8 to true if utf8 -// support is needed. -template -class qt_color_sink : public base_sink { -public: - qt_color_sink(QTextEdit *qt_text_edit, - int max_lines, - bool dark_colors = false, - bool is_utf8 = false) - : qt_text_edit_(qt_text_edit), - max_lines_(max_lines), - is_utf8_(is_utf8) { - if (!qt_text_edit_) { - throw_spdlog_ex("qt_color_text_sink: text_edit is null"); - } - - default_color_ = qt_text_edit_->currentCharFormat(); - // set colors - QTextCharFormat format; - // trace - format.setForeground(dark_colors ? Qt::darkGray : Qt::gray); - colors_.at(level::trace) = format; - // debug - format.setForeground(dark_colors ? Qt::darkCyan : Qt::cyan); - colors_.at(level::debug) = format; - // info - format.setForeground(dark_colors ? Qt::darkGreen : Qt::green); - colors_.at(level::info) = format; - // warn - format.setForeground(dark_colors ? Qt::darkYellow : Qt::yellow); - colors_.at(level::warn) = format; - // err - format.setForeground(Qt::red); - colors_.at(level::err) = format; - // critical - format.setForeground(Qt::white); - format.setBackground(Qt::red); - colors_.at(level::critical) = format; - } - - ~qt_color_sink() { flush_(); } - - void set_default_color(QTextCharFormat format) { - // std::lock_guard lock(base_sink::mutex_); - default_color_ = format; - } - - void set_level_color(level::level_enum color_level, QTextCharFormat format) { - // std::lock_guard lock(base_sink::mutex_); - colors_.at(static_cast(color_level)) = format; - } - - QTextCharFormat &get_level_color(level::level_enum color_level) { - std::lock_guard lock(base_sink::mutex_); - return colors_.at(static_cast(color_level)); - } - - QTextCharFormat &get_default_color() { - std::lock_guard lock(base_sink::mutex_); - return default_color_; - } - -protected: - struct invoke_params { - invoke_params(int max_lines, - QTextEdit *q_text_edit, - QString payload, - QTextCharFormat default_color, - QTextCharFormat level_color, - int color_range_start, - int color_range_end) - : max_lines(max_lines), - q_text_edit(q_text_edit), - payload(std::move(payload)), - default_color(default_color), - level_color(level_color), - color_range_start(color_range_start), - color_range_end(color_range_end) {} - int max_lines; - QTextEdit *q_text_edit; - QString payload; - QTextCharFormat default_color; - QTextCharFormat level_color; - int color_range_start; - int color_range_end; - }; - - void sink_it_(const details::log_msg &msg) override { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - - const string_view_t str = string_view_t(formatted.data(), formatted.size()); - // apply the color to the color range in the formatted message. - QString payload; - int color_range_start = static_cast(msg.color_range_start); - int color_range_end = static_cast(msg.color_range_end); - if (is_utf8_) { - payload = QString::fromUtf8(str.data(), static_cast(str.size())); - // convert color ranges from byte index to character index. - if (msg.color_range_start < msg.color_range_end) { - color_range_start = QString::fromUtf8(str.data(), msg.color_range_start).size(); - color_range_end = QString::fromUtf8(str.data(), msg.color_range_end).size(); - } - } else { - payload = QString::fromLatin1(str.data(), static_cast(str.size())); - } - - invoke_params params{max_lines_, // max lines - qt_text_edit_, // text edit to append to - std::move(payload), // text to append - default_color_, // default color - colors_.at(msg.level), // color to apply - color_range_start, // color range start - color_range_end}; // color range end - - QMetaObject::invokeMethod( - qt_text_edit_, [params]() { invoke_method_(params); }, Qt::AutoConnection); - } - - void flush_() override {} - - // Add colored text to the text edit widget. This method is invoked in the GUI thread. - // It is a static method to ensure that it is handled correctly even if the sink is destroyed - // prematurely before it is invoked. - - static void invoke_method_(invoke_params params) { - auto *document = params.q_text_edit->document(); - QTextCursor cursor(document); - - // remove first blocks if number of blocks exceeds max_lines - while (document->blockCount() > params.max_lines) { - cursor.select(QTextCursor::BlockUnderCursor); - cursor.removeSelectedText(); - cursor.deleteChar(); // delete the newline after the block - } - - cursor.movePosition(QTextCursor::End); - cursor.setCharFormat(params.default_color); - - // if color range not specified or not not valid, just append the text with default color - if (params.color_range_end <= params.color_range_start) { - cursor.insertText(params.payload); - return; - } - - // insert the text before the color range - cursor.insertText(params.payload.left(params.color_range_start)); - - // insert the colorized text - cursor.setCharFormat(params.level_color); - cursor.insertText(params.payload.mid(params.color_range_start, - params.color_range_end - params.color_range_start)); - - // insert the text after the color range with default format - cursor.setCharFormat(params.default_color); - cursor.insertText(params.payload.mid(params.color_range_end)); - } - - QTextEdit *qt_text_edit_; - int max_lines_; - bool is_utf8_; - QTextCharFormat default_color_; - std::array colors_; -}; - -#include "spdlog/details/null_mutex.h" -#include - -using qt_sink_mt = qt_sink; -using qt_sink_st = qt_sink; -using qt_color_sink_mt = qt_color_sink; -using qt_color_sink_st = qt_color_sink; -} // namespace sinks - -// -// Factory functions -// - -// log to QTextEdit -template -inline std::shared_ptr qt_logger_mt(const std::string &logger_name, - QTextEdit *qt_object, - const std::string &meta_method = "append") { - return Factory::template create(logger_name, qt_object, meta_method); -} - -template -inline std::shared_ptr qt_logger_st(const std::string &logger_name, - QTextEdit *qt_object, - const std::string &meta_method = "append") { - return Factory::template create(logger_name, qt_object, meta_method); -} - -// log to QPlainTextEdit -template -inline std::shared_ptr qt_logger_mt(const std::string &logger_name, - QPlainTextEdit *qt_object, - const std::string &meta_method = "appendPlainText") { - return Factory::template create(logger_name, qt_object, meta_method); -} - -template -inline std::shared_ptr qt_logger_st(const std::string &logger_name, - QPlainTextEdit *qt_object, - const std::string &meta_method = "appendPlainText") { - return Factory::template create(logger_name, qt_object, meta_method); -} -// log to QObject -template -inline std::shared_ptr qt_logger_mt(const std::string &logger_name, - QObject *qt_object, - const std::string &meta_method) { - return Factory::template create(logger_name, qt_object, meta_method); -} - -template -inline std::shared_ptr qt_logger_st(const std::string &logger_name, - QObject *qt_object, - const std::string &meta_method) { - return Factory::template create(logger_name, qt_object, meta_method); -} - -// log to QTextEdit with colorized output -template -inline std::shared_ptr qt_color_logger_mt(const std::string &logger_name, - QTextEdit *qt_text_edit, - int max_lines, - bool is_utf8 = false) { - return Factory::template create(logger_name, qt_text_edit, max_lines, - false, is_utf8); -} - -template -inline std::shared_ptr qt_color_logger_st(const std::string &logger_name, - QTextEdit *qt_text_edit, - int max_lines, - bool is_utf8 = false) { - return Factory::template create(logger_name, qt_text_edit, max_lines, - false, is_utf8); -} - -} // namespace spdlog diff --git a/include/spdlog/sinks/ringbuffer_sink.h b/include/spdlog/sinks/ringbuffer_sink.h deleted file mode 100644 index 6156c6a..0000000 --- a/include/spdlog/sinks/ringbuffer_sink.h +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include "spdlog/details/circular_q.h" -#include "spdlog/details/log_msg_buffer.h" -#include "spdlog/details/null_mutex.h" -#include "spdlog/sinks/base_sink.h" - -#include -#include -#include - -namespace spdlog { -namespace sinks { -/* - * Ring buffer sink - */ -template -class ringbuffer_sink final : public base_sink { -public: - explicit ringbuffer_sink(size_t n_items) - : q_{n_items} {} - - std::vector last_raw(size_t lim = 0) { - std::lock_guard lock(base_sink::mutex_); - auto items_available = q_.size(); - auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; - std::vector ret; - ret.reserve(n_items); - for (size_t i = (items_available - n_items); i < items_available; i++) { - ret.push_back(q_.at(i)); - } - return ret; - } - - std::vector last_formatted(size_t lim = 0) { - std::lock_guard lock(base_sink::mutex_); - auto items_available = q_.size(); - auto n_items = lim > 0 ? (std::min)(lim, items_available) : items_available; - std::vector ret; - ret.reserve(n_items); - for (size_t i = (items_available - n_items); i < items_available; i++) { - memory_buf_t formatted; - base_sink::formatter_->format(q_.at(i), formatted); - ret.push_back(SPDLOG_BUF_TO_STRING(formatted)); - } - return ret; - } - -protected: - void sink_it_(const details::log_msg &msg) override { - q_.push_back(details::log_msg_buffer{msg}); - } - void flush_() override {} - -private: - details::circular_q q_; -}; - -using ringbuffer_sink_mt = ringbuffer_sink; -using ringbuffer_sink_st = ringbuffer_sink; - -} // namespace sinks - -} // namespace spdlog diff --git a/include/spdlog/sinks/rotating_file_sink-inl.h b/include/spdlog/sinks/rotating_file_sink-inl.h deleted file mode 100644 index 420bafb..0000000 --- a/include/spdlog/sinks/rotating_file_sink-inl.h +++ /dev/null @@ -1,150 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { - -template -SPDLOG_INLINE rotating_file_sink::rotating_file_sink( - filename_t base_filename, - std::size_t max_size, - std::size_t max_files, - bool rotate_on_open, - const file_event_handlers &event_handlers) - : base_filename_(std::move(base_filename)), - max_size_(max_size), - max_files_(max_files), - file_helper_{event_handlers} { - if (max_size == 0) { - throw_spdlog_ex("rotating sink constructor: max_size arg cannot be zero"); - } - - if (max_files > 200000) { - throw_spdlog_ex("rotating sink constructor: max_files arg cannot exceed 200000"); - } - file_helper_.open(calc_filename(base_filename_, 0)); - current_size_ = file_helper_.size(); // expensive. called only once - if (rotate_on_open && current_size_ > 0) { - rotate_(); - current_size_ = 0; - } -} - -// calc filename according to index and file extension if exists. -// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt". -template -SPDLOG_INLINE filename_t rotating_file_sink::calc_filename(const filename_t &filename, - std::size_t index) { - if (index == 0u) { - return filename; - } - - filename_t basename, ext; - std::tie(basename, ext) = details::file_helper::split_by_extension(filename); - return fmt_lib::format(SPDLOG_FMT_STRING(SPDLOG_FILENAME_T("{}.{}{}")), basename, index, ext); -} - -template -SPDLOG_INLINE filename_t rotating_file_sink::filename() { - std::lock_guard lock(base_sink::mutex_); - return file_helper_.filename(); -} - -template -SPDLOG_INLINE void rotating_file_sink::rotate_now() { - std::lock_guard lock(base_sink::mutex_); - rotate_(); -} - -template -SPDLOG_INLINE void rotating_file_sink::sink_it_(const details::log_msg &msg) { - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - auto new_size = current_size_ + formatted.size(); - - // rotate if the new estimated file size exceeds max size. - // rotate only if the real size > 0 to better deal with full disk (see issue #2261). - // we only check the real size when new_size > max_size_ because it is relatively expensive. - if (new_size > max_size_) { - file_helper_.flush(); - if (file_helper_.size() > 0) { - rotate_(); - new_size = formatted.size(); - } - } - file_helper_.write(formatted); - current_size_ = new_size; -} - -template -SPDLOG_INLINE void rotating_file_sink::flush_() { - file_helper_.flush(); -} - -// Rotate files: -// log.txt -> log.1.txt -// log.1.txt -> log.2.txt -// log.2.txt -> log.3.txt -// log.3.txt -> delete -template -SPDLOG_INLINE void rotating_file_sink::rotate_() { - using details::os::filename_to_str; - using details::os::path_exists; - - file_helper_.close(); - for (auto i = max_files_; i > 0; --i) { - filename_t src = calc_filename(base_filename_, i - 1); - if (!path_exists(src)) { - continue; - } - filename_t target = calc_filename(base_filename_, i); - - if (!rename_file_(src, target)) { - // if failed try again after a small delay. - // this is a workaround to a windows issue, where very high rotation - // rates can cause the rename to fail with permission denied (because of antivirus?). - details::os::sleep_for_millis(100); - if (!rename_file_(src, target)) { - file_helper_.reopen( - true); // truncate the log file anyway to prevent it to grow beyond its limit! - current_size_ = 0; - throw_spdlog_ex("rotating_file_sink: failed renaming " + filename_to_str(src) + - " to " + filename_to_str(target), - errno); - } - } - } - file_helper_.reopen(true); -} - -// delete the target if exists, and rename the src file to target -// return true on success, false otherwise. -template -SPDLOG_INLINE bool rotating_file_sink::rename_file_(const filename_t &src_filename, - const filename_t &target_filename) { - // try to delete the target file in case it already exists. - (void)details::os::remove(target_filename); - return details::os::rename(src_filename, target_filename) == 0; -} - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/rotating_file_sink.h b/include/spdlog/sinks/rotating_file_sink.h deleted file mode 100644 index 42bd376..0000000 --- a/include/spdlog/sinks/rotating_file_sink.h +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#include -#include - -namespace spdlog { -namespace sinks { - -// -// Rotating file sink based on size -// -template -class rotating_file_sink final : public base_sink { -public: - rotating_file_sink(filename_t base_filename, - std::size_t max_size, - std::size_t max_files, - bool rotate_on_open = false, - const file_event_handlers &event_handlers = {}); - static filename_t calc_filename(const filename_t &filename, std::size_t index); - filename_t filename(); - void rotate_now(); - -protected: - void sink_it_(const details::log_msg &msg) override; - void flush_() override; - -private: - // Rotate files: - // log.txt -> log.1.txt - // log.1.txt -> log.2.txt - // log.2.txt -> log.3.txt - // log.3.txt -> delete - void rotate_(); - - // delete the target if exists, and rename the src file to target - // return true on success, false otherwise. - bool rename_file_(const filename_t &src_filename, const filename_t &target_filename); - - filename_t base_filename_; - std::size_t max_size_; - std::size_t max_files_; - std::size_t current_size_; - details::file_helper file_helper_; -}; - -using rotating_file_sink_mt = rotating_file_sink; -using rotating_file_sink_st = rotating_file_sink; - -} // namespace sinks - -// -// factory functions -// - -template -inline std::shared_ptr rotating_logger_mt(const std::string &logger_name, - const filename_t &filename, - size_t max_file_size, - size_t max_files, - bool rotate_on_open = false, - const file_event_handlers &event_handlers = {}) { - return Factory::template create( - logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); -} - -template -inline std::shared_ptr rotating_logger_st(const std::string &logger_name, - const filename_t &filename, - size_t max_file_size, - size_t max_files, - bool rotate_on_open = false, - const file_event_handlers &event_handlers = {}) { - return Factory::template create( - logger_name, filename, max_file_size, max_files, rotate_on_open, event_handlers); -} -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "rotating_file_sink-inl.h" -#endif diff --git a/include/spdlog/sinks/sink-inl.h b/include/spdlog/sinks/sink-inl.h deleted file mode 100644 index e4b2714..0000000 --- a/include/spdlog/sinks/sink-inl.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include - -SPDLOG_INLINE bool spdlog::sinks::sink::should_log(spdlog::level::level_enum msg_level) const { - return msg_level >= level_.load(std::memory_order_relaxed); -} - -SPDLOG_INLINE void spdlog::sinks::sink::set_level(level::level_enum log_level) { - level_.store(log_level, std::memory_order_relaxed); -} - -SPDLOG_INLINE spdlog::level::level_enum spdlog::sinks::sink::level() const { - return static_cast(level_.load(std::memory_order_relaxed)); -} diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h deleted file mode 100644 index 5850685..0000000 --- a/include/spdlog/sinks/sink.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -namespace spdlog { - -namespace sinks { -class SPDLOG_API sink { -public: - virtual ~sink() = default; - virtual void log(const details::log_msg &msg) = 0; - virtual void flush() = 0; - virtual void set_pattern(const std::string &pattern) = 0; - virtual void set_formatter(std::unique_ptr sink_formatter) = 0; - - void set_level(level::level_enum log_level); - level::level_enum level() const; - bool should_log(level::level_enum msg_level) const; - -protected: - // sink log level - default is all - level_t level_{level::trace}; -}; - -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "sink-inl.h" -#endif diff --git a/include/spdlog/sinks/stdout_color_sinks-inl.h b/include/spdlog/sinks/stdout_color_sinks-inl.h deleted file mode 100644 index 166e386..0000000 --- a/include/spdlog/sinks/stdout_color_sinks-inl.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -namespace spdlog { - -template -SPDLOG_INLINE std::shared_ptr stdout_color_mt(const std::string &logger_name, - color_mode mode) { - return Factory::template create(logger_name, mode); -} - -template -SPDLOG_INLINE std::shared_ptr stdout_color_st(const std::string &logger_name, - color_mode mode) { - return Factory::template create(logger_name, mode); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_color_mt(const std::string &logger_name, - color_mode mode) { - return Factory::template create(logger_name, mode); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_color_st(const std::string &logger_name, - color_mode mode) { - return Factory::template create(logger_name, mode); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/stdout_color_sinks.h b/include/spdlog/sinks/stdout_color_sinks.h deleted file mode 100644 index 72991fe..0000000 --- a/include/spdlog/sinks/stdout_color_sinks.h +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifdef _WIN32 - #include -#else - #include -#endif - -#include - -namespace spdlog { -namespace sinks { -#ifdef _WIN32 -using stdout_color_sink_mt = wincolor_stdout_sink_mt; -using stdout_color_sink_st = wincolor_stdout_sink_st; -using stderr_color_sink_mt = wincolor_stderr_sink_mt; -using stderr_color_sink_st = wincolor_stderr_sink_st; -#else -using stdout_color_sink_mt = ansicolor_stdout_sink_mt; -using stdout_color_sink_st = ansicolor_stdout_sink_st; -using stderr_color_sink_mt = ansicolor_stderr_sink_mt; -using stderr_color_sink_st = ansicolor_stderr_sink_st; -#endif -} // namespace sinks - -template -std::shared_ptr stdout_color_mt(const std::string &logger_name, - color_mode mode = color_mode::automatic); - -template -std::shared_ptr stdout_color_st(const std::string &logger_name, - color_mode mode = color_mode::automatic); - -template -std::shared_ptr stderr_color_mt(const std::string &logger_name, - color_mode mode = color_mode::automatic); - -template -std::shared_ptr stderr_color_st(const std::string &logger_name, - color_mode mode = color_mode::automatic); - -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "stdout_color_sinks-inl.h" -#endif diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h deleted file mode 100644 index dcb21d8..0000000 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include -#include -#include - -#ifdef _WIN32 - // under windows using fwrite to non-binary stream results in \r\r\n (see issue #1675) - // so instead we use ::FileWrite - #include - - #ifndef _USING_V110_SDK71_ // fileapi.h doesn't exist in winxp - #include // WriteFile (..) - #endif - - #include // _get_osfhandle(..) - #include // _fileno(..) -#endif // _WIN32 - -namespace spdlog { - -namespace sinks { - -template -SPDLOG_INLINE stdout_sink_base::stdout_sink_base(FILE *file) - : mutex_(ConsoleMutex::mutex()), - file_(file), - formatter_(details::make_unique()) { -#ifdef _WIN32 - // get windows handle from the FILE* object - - handle_ = reinterpret_cast(::_get_osfhandle(::_fileno(file_))); - - // don't throw to support cases where no console is attached, - // and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE). - // throw only if non stdout/stderr target is requested (probably regular file and not console). - if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) { - throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno); - } -#endif // _WIN32 -} - -template -SPDLOG_INLINE void stdout_sink_base::log(const details::log_msg &msg) { -#ifdef _WIN32 - if (handle_ == INVALID_HANDLE_VALUE) { - return; - } - std::lock_guard lock(mutex_); - memory_buf_t formatted; - formatter_->format(msg, formatted); - auto size = static_cast(formatted.size()); - DWORD bytes_written = 0; - bool ok = ::WriteFile(handle_, formatted.data(), size, &bytes_written, nullptr) != 0; - if (!ok) { - throw_spdlog_ex("stdout_sink_base: WriteFile() failed. GetLastError(): " + - std::to_string(::GetLastError())); - } -#else - std::lock_guard lock(mutex_); - memory_buf_t formatted; - formatter_->format(msg, formatted); - details::os::fwrite_bytes(formatted.data(), formatted.size(), file_); -#endif // _WIN32 - ::fflush(file_); // flush every line to terminal -} - -template -SPDLOG_INLINE void stdout_sink_base::flush() { - std::lock_guard lock(mutex_); - fflush(file_); -} - -template -SPDLOG_INLINE void stdout_sink_base::set_pattern(const std::string &pattern) { - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - -template -SPDLOG_INLINE void stdout_sink_base::set_formatter( - std::unique_ptr sink_formatter) { - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); -} - -// stdout sink -template -SPDLOG_INLINE stdout_sink::stdout_sink() - : stdout_sink_base(stdout) {} - -// stderr sink -template -SPDLOG_INLINE stderr_sink::stderr_sink() - : stdout_sink_base(stderr) {} - -} // namespace sinks - -// factory methods -template -SPDLOG_INLINE std::shared_ptr stdout_logger_mt(const std::string &logger_name) { - return Factory::template create(logger_name); -} - -template -SPDLOG_INLINE std::shared_ptr stdout_logger_st(const std::string &logger_name) { - return Factory::template create(logger_name); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_logger_mt(const std::string &logger_name) { - return Factory::template create(logger_name); -} - -template -SPDLOG_INLINE std::shared_ptr stderr_logger_st(const std::string &logger_name) { - return Factory::template create(logger_name); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h deleted file mode 100644 index 6ef0996..0000000 --- a/include/spdlog/sinks/stdout_sinks.h +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#ifdef _WIN32 - #include -#endif - -namespace spdlog { - -namespace sinks { - -template -class stdout_sink_base : public sink { -public: - using mutex_t = typename ConsoleMutex::mutex_t; - explicit stdout_sink_base(FILE *file); - ~stdout_sink_base() override = default; - - stdout_sink_base(const stdout_sink_base &other) = delete; - stdout_sink_base(stdout_sink_base &&other) = delete; - - stdout_sink_base &operator=(const stdout_sink_base &other) = delete; - stdout_sink_base &operator=(stdout_sink_base &&other) = delete; - - void log(const details::log_msg &msg) override; - void flush() override; - void set_pattern(const std::string &pattern) override; - - void set_formatter(std::unique_ptr sink_formatter) override; - -protected: - mutex_t &mutex_; - FILE *file_; - std::unique_ptr formatter_; -#ifdef _WIN32 - HANDLE handle_; -#endif // WIN32 -}; - -template -class stdout_sink : public stdout_sink_base { -public: - stdout_sink(); -}; - -template -class stderr_sink : public stdout_sink_base { -public: - stderr_sink(); -}; - -using stdout_sink_mt = stdout_sink; -using stdout_sink_st = stdout_sink; - -using stderr_sink_mt = stderr_sink; -using stderr_sink_st = stderr_sink; - -} // namespace sinks - -// factory methods -template -std::shared_ptr stdout_logger_mt(const std::string &logger_name); - -template -std::shared_ptr stdout_logger_st(const std::string &logger_name); - -template -std::shared_ptr stderr_logger_mt(const std::string &logger_name); - -template -std::shared_ptr stderr_logger_st(const std::string &logger_name); - -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "stdout_sinks-inl.h" -#endif diff --git a/include/spdlog/sinks/syslog_sink.h b/include/spdlog/sinks/syslog_sink.h deleted file mode 100644 index 913d41b..0000000 --- a/include/spdlog/sinks/syslog_sink.h +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include - -#include -#include -#include - -namespace spdlog { -namespace sinks { -/** - * Sink that write to syslog using the `syscall()` library call. - */ -template -class syslog_sink : public base_sink { -public: - syslog_sink(std::string ident, int syslog_option, int syslog_facility, bool enable_formatting) - : enable_formatting_{enable_formatting}, - syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, - /* spdlog::level::debug */ LOG_DEBUG, - /* spdlog::level::info */ LOG_INFO, - /* spdlog::level::warn */ LOG_WARNING, - /* spdlog::level::err */ LOG_ERR, - /* spdlog::level::critical */ LOG_CRIT, - /* spdlog::level::off */ LOG_INFO}}, - ident_{std::move(ident)} { - // set ident to be program name if empty - ::openlog(ident_.empty() ? nullptr : ident_.c_str(), syslog_option, syslog_facility); - } - - ~syslog_sink() override { ::closelog(); } - - syslog_sink(const syslog_sink &) = delete; - syslog_sink &operator=(const syslog_sink &) = delete; - -protected: - void sink_it_(const details::log_msg &msg) override { - string_view_t payload; - memory_buf_t formatted; - if (enable_formatting_) { - base_sink::formatter_->format(msg, formatted); - payload = string_view_t(formatted.data(), formatted.size()); - } else { - payload = msg.payload; - } - - size_t length = payload.size(); - // limit to max int - if (length > static_cast(std::numeric_limits::max())) { - length = static_cast(std::numeric_limits::max()); - } - - ::syslog(syslog_prio_from_level(msg), "%.*s", static_cast(length), payload.data()); - } - - void flush_() override {} - bool enable_formatting_ = false; - - // - // Simply maps spdlog's log level to syslog priority level. - // - virtual int syslog_prio_from_level(const details::log_msg &msg) const { - return syslog_levels_.at(static_cast(msg.level)); - } - - using levels_array = std::array; - levels_array syslog_levels_; - -private: - // must store the ident because the man says openlog might use the pointer as - // is and not a string copy - const std::string ident_; -}; - -using syslog_sink_mt = syslog_sink; -using syslog_sink_st = syslog_sink; -} // namespace sinks - -// Create and register a syslog logger -template -inline std::shared_ptr syslog_logger_mt(const std::string &logger_name, - const std::string &syslog_ident = "", - int syslog_option = 0, - int syslog_facility = LOG_USER, - bool enable_formatting = false) { - return Factory::template create(logger_name, syslog_ident, syslog_option, - syslog_facility, enable_formatting); -} - -template -inline std::shared_ptr syslog_logger_st(const std::string &logger_name, - const std::string &syslog_ident = "", - int syslog_option = 0, - int syslog_facility = LOG_USER, - bool enable_formatting = false) { - return Factory::template create(logger_name, syslog_ident, syslog_option, - syslog_facility, enable_formatting); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h deleted file mode 100644 index d2cd55f..0000000 --- a/include/spdlog/sinks/systemd_sink.h +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright(c) 2019 ZVYAGIN.Alexander@gmail.com -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#ifndef SD_JOURNAL_SUPPRESS_LOCATION - #define SD_JOURNAL_SUPPRESS_LOCATION -#endif -#include - -namespace spdlog { -namespace sinks { - -/** - * Sink that write to systemd journal using the `sd_journal_send()` library call. - */ -template -class systemd_sink : public base_sink { -public: - systemd_sink(std::string ident = "", bool enable_formatting = false) - : ident_{std::move(ident)}, - enable_formatting_{enable_formatting}, - syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, - /* spdlog::level::debug */ LOG_DEBUG, - /* spdlog::level::info */ LOG_INFO, - /* spdlog::level::warn */ LOG_WARNING, - /* spdlog::level::err */ LOG_ERR, - /* spdlog::level::critical */ LOG_CRIT, - /* spdlog::level::off */ LOG_INFO}} {} - - ~systemd_sink() override {} - - systemd_sink(const systemd_sink &) = delete; - systemd_sink &operator=(const systemd_sink &) = delete; - -protected: - const std::string ident_; - bool enable_formatting_ = false; - using levels_array = std::array; - levels_array syslog_levels_; - - void sink_it_(const details::log_msg &msg) override { - int err; - string_view_t payload; - memory_buf_t formatted; - if (enable_formatting_) { - base_sink::formatter_->format(msg, formatted); - payload = string_view_t(formatted.data(), formatted.size()); - } else { - payload = msg.payload; - } - - size_t length = payload.size(); - // limit to max int - if (length > static_cast(std::numeric_limits::max())) { - length = static_cast(std::numeric_limits::max()); - } - - const string_view_t syslog_identifier = ident_.empty() ? msg.logger_name : ident_; - - // Do not send source location if not available - if (msg.source.empty()) { - // Note: function call inside '()' to avoid macro expansion - err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), - "PRIORITY=%d", syslog_level(msg.level), -#ifndef SPDLOG_NO_THREAD_ID - "TID=%zu", msg.thread_id, -#endif - "SYSLOG_IDENTIFIER=%.*s", - static_cast(syslog_identifier.size()), - syslog_identifier.data(), nullptr); - } else { - err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), - "PRIORITY=%d", syslog_level(msg.level), -#ifndef SPDLOG_NO_THREAD_ID - "TID=%zu", msg.thread_id, -#endif - "SYSLOG_IDENTIFIER=%.*s", - static_cast(syslog_identifier.size()), - syslog_identifier.data(), "CODE_FILE=%s", msg.source.filename, - "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", - msg.source.funcname, nullptr); - } - - if (err) { - throw_spdlog_ex("Failed writing to systemd", errno); - } - } - - int syslog_level(level::level_enum l) { - return syslog_levels_.at(static_cast(l)); - } - - void flush_() override {} -}; - -using systemd_sink_mt = systemd_sink; -using systemd_sink_st = systemd_sink; -} // namespace sinks - -// Create and register a syslog logger -template -inline std::shared_ptr systemd_logger_mt(const std::string &logger_name, - const std::string &ident = "", - bool enable_formatting = false) { - return Factory::template create(logger_name, ident, enable_formatting); -} - -template -inline std::shared_ptr systemd_logger_st(const std::string &logger_name, - const std::string &ident = "", - bool enable_formatting = false) { - return Factory::template create(logger_name, ident, enable_formatting); -} -} // namespace spdlog diff --git a/include/spdlog/sinks/tcp_sink.h b/include/spdlog/sinks/tcp_sink.h deleted file mode 100644 index 2534964..0000000 --- a/include/spdlog/sinks/tcp_sink.h +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#ifdef _WIN32 - #include -#else - #include -#endif - -#include -#include -#include -#include - -#pragma once - -// Simple tcp client sink -// Connects to remote address and send the formatted log. -// Will attempt to reconnect if connection drops. -// If more complicated behaviour is needed (i.e get responses), you can inherit it and override the -// sink_it_ method. - -namespace spdlog { -namespace sinks { - -struct tcp_sink_config { - std::string server_host; - int server_port; - bool lazy_connect = false; // if true connect on first log call instead of on construction - - tcp_sink_config(std::string host, int port) - : server_host{std::move(host)}, - server_port{port} {} -}; - -template -class tcp_sink : public spdlog::sinks::base_sink { -public: - // connect to tcp host/port or throw if failed - // host can be hostname or ip address - - explicit tcp_sink(tcp_sink_config sink_config) - : config_{std::move(sink_config)} { - if (!config_.lazy_connect) { - this->client_.connect(config_.server_host, config_.server_port); - } - } - - ~tcp_sink() override = default; - -protected: - void sink_it_(const spdlog::details::log_msg &msg) override { - spdlog::memory_buf_t formatted; - spdlog::sinks::base_sink::formatter_->format(msg, formatted); - if (!client_.is_connected()) { - client_.connect(config_.server_host, config_.server_port); - } - client_.send(formatted.data(), formatted.size()); - } - - void flush_() override {} - tcp_sink_config config_; - details::tcp_client client_; -}; - -using tcp_sink_mt = tcp_sink; -using tcp_sink_st = tcp_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/udp_sink.h b/include/spdlog/sinks/udp_sink.h deleted file mode 100644 index 4bff0fd..0000000 --- a/include/spdlog/sinks/udp_sink.h +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#ifdef _WIN32 - #include -#else - #include -#endif - -#include -#include -#include -#include - -// Simple udp client sink -// Sends formatted log via udp - -namespace spdlog { -namespace sinks { - -struct udp_sink_config { - std::string server_host; - uint16_t server_port; - - udp_sink_config(std::string host, uint16_t port) - : server_host{std::move(host)}, - server_port{port} {} -}; - -template -class udp_sink : public spdlog::sinks::base_sink { -public: - // host can be hostname or ip address - explicit udp_sink(udp_sink_config sink_config) - : client_{sink_config.server_host, sink_config.server_port} {} - - ~udp_sink() override = default; - -protected: - void sink_it_(const spdlog::details::log_msg &msg) override { - spdlog::memory_buf_t formatted; - spdlog::sinks::base_sink::formatter_->format(msg, formatted); - client_.send(formatted.data(), formatted.size()); - } - - void flush_() override {} - details::udp_client client_; -}; - -using udp_sink_mt = udp_sink; -using udp_sink_st = udp_sink; - -} // namespace sinks - -// -// factory functions -// -template -inline std::shared_ptr udp_logger_mt(const std::string &logger_name, - sinks::udp_sink_config skin_config) { - return Factory::template create(logger_name, skin_config); -} - -} // namespace spdlog diff --git a/include/spdlog/sinks/win_eventlog_sink.h b/include/spdlog/sinks/win_eventlog_sink.h deleted file mode 100644 index 2c9b582..0000000 --- a/include/spdlog/sinks/win_eventlog_sink.h +++ /dev/null @@ -1,260 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -// Writing to Windows Event Log requires the registry entries below to be present, with the -// following modifications: -// 1. should be replaced with your log name (e.g. your application name) -// 2. should be replaced with the specific source name and the key should be -// duplicated for -// each source used in the application -// -// Since typically modifications of this kind require elevation, it's better to do it as a part of -// setup procedure. The snippet below uses mscoree.dll as the message file as it exists on most of -// the Windows systems anyway and happens to contain the needed resource. -// -// You can also specify a custom message file if needed. -// Please refer to Event Log functions descriptions in MSDN for more details on custom message -// files. - -/*--------------------------------------------------------------------------------------- - -Windows Registry Editor Version 5.00 - -[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\] - -[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\\] -"TypesSupported"=dword:00000007 -"EventMessageFile"=hex(2):25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,6f,\ - 00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,\ - 5c,00,6d,00,73,00,63,00,6f,00,72,00,65,00,65,00,2e,00,64,00,6c,00,6c,00,00,\ - 00 - ------------------------------------------------------------------------------------------*/ - -#pragma once - -#include -#include - -#include -#include - -#include -#include -#include - -namespace spdlog { -namespace sinks { - -namespace win_eventlog { - -namespace internal { - -struct local_alloc_t { - HLOCAL hlocal_; - - SPDLOG_CONSTEXPR local_alloc_t() SPDLOG_NOEXCEPT : hlocal_(nullptr) {} - - local_alloc_t(local_alloc_t const &) = delete; - local_alloc_t &operator=(local_alloc_t const &) = delete; - - ~local_alloc_t() SPDLOG_NOEXCEPT { - if (hlocal_) { - LocalFree(hlocal_); - } - } -}; - -/** Windows error */ -struct win32_error : public spdlog_ex { - /** Formats an error report line: "user-message: error-code (system message)" */ - static std::string format(std::string const &user_message, DWORD error_code = GetLastError()) { - std::string system_message; - - local_alloc_t format_message_result{}; - auto format_message_succeeded = - ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - nullptr, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&format_message_result.hlocal_, 0, nullptr); - - if (format_message_succeeded && format_message_result.hlocal_) { - system_message = fmt_lib::format(" ({})", (LPSTR)format_message_result.hlocal_); - } - - return fmt_lib::format("{}: {}{}", user_message, error_code, system_message); - } - - explicit win32_error(std::string const &func_name, DWORD error = GetLastError()) - : spdlog_ex(format(func_name, error)) {} -}; - -/** Wrapper for security identifiers (SID) on Windows */ -struct sid_t { - std::vector buffer_; - -public: - sid_t() {} - - /** creates a wrapped SID copy */ - static sid_t duplicate_sid(PSID psid) { - if (!::IsValidSid(psid)) { - throw_spdlog_ex("sid_t::sid_t(): invalid SID received"); - } - - auto const sid_length{::GetLengthSid(psid)}; - - sid_t result; - result.buffer_.resize(sid_length); - if (!::CopySid(sid_length, (PSID)result.as_sid(), psid)) { - SPDLOG_THROW(win32_error("CopySid")); - } - - return result; - } - - /** Retrieves pointer to the internal buffer contents as SID* */ - SID *as_sid() const { return buffer_.empty() ? nullptr : (SID *)buffer_.data(); } - - /** Get SID for the current user */ - static sid_t get_current_user_sid() { - /* create and init RAII holder for process token */ - struct process_token_t { - HANDLE token_handle_ = INVALID_HANDLE_VALUE; - explicit process_token_t(HANDLE process) { - if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_)) { - SPDLOG_THROW(win32_error("OpenProcessToken")); - } - } - - ~process_token_t() { ::CloseHandle(token_handle_); } - - } current_process_token( - ::GetCurrentProcess()); // GetCurrentProcess returns pseudohandle, no leak here! - - // Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return - // the token size - DWORD tusize = 0; - if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, - &tusize)) { - SPDLOG_THROW(win32_error("GetTokenInformation should fail")); - } - - // get user token - std::vector buffer(static_cast(tusize)); - if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, - (LPVOID)buffer.data(), tusize, &tusize)) { - SPDLOG_THROW(win32_error("GetTokenInformation")); - } - - // create a wrapper of the SID data as stored in the user token - return sid_t::duplicate_sid(((TOKEN_USER *)buffer.data())->User.Sid); - } -}; - -struct eventlog { - static WORD get_event_type(details::log_msg const &msg) { - switch (msg.level) { - case level::trace: - case level::debug: - return EVENTLOG_SUCCESS; - - case level::info: - return EVENTLOG_INFORMATION_TYPE; - - case level::warn: - return EVENTLOG_WARNING_TYPE; - - case level::err: - case level::critical: - case level::off: - return EVENTLOG_ERROR_TYPE; - - default: - return EVENTLOG_INFORMATION_TYPE; - } - } - - static WORD get_event_category(details::log_msg const &msg) { return (WORD)msg.level; } -}; - -} // namespace internal - -/* - * Windows Event Log sink - */ -template -class win_eventlog_sink : public base_sink { -private: - HANDLE hEventLog_{NULL}; - internal::sid_t current_user_sid_; - std::string source_; - DWORD event_id_; - - HANDLE event_log_handle() { - if (!hEventLog_) { - hEventLog_ = ::RegisterEventSourceA(nullptr, source_.c_str()); - if (!hEventLog_ || hEventLog_ == (HANDLE)ERROR_ACCESS_DENIED) { - SPDLOG_THROW(internal::win32_error("RegisterEventSource")); - } - } - - return hEventLog_; - } - -protected: - void sink_it_(const details::log_msg &msg) override { - using namespace internal; - - bool succeeded; - memory_buf_t formatted; - base_sink::formatter_->format(msg, formatted); - formatted.push_back('\0'); - -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT - wmemory_buf_t buf; - details::os::utf8_to_wstrbuf(string_view_t(formatted.data(), formatted.size()), buf); - - LPCWSTR lp_wstr = buf.data(); - succeeded = static_cast(::ReportEventW( - event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), - event_id_, current_user_sid_.as_sid(), 1, 0, &lp_wstr, nullptr)); -#else - LPCSTR lp_str = formatted.data(); - succeeded = static_cast(::ReportEventA( - event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg), - event_id_, current_user_sid_.as_sid(), 1, 0, &lp_str, nullptr)); -#endif - - if (!succeeded) { - SPDLOG_THROW(win32_error("ReportEvent")); - } - } - - void flush_() override {} - -public: - win_eventlog_sink(std::string const &source, - DWORD event_id = 1000 /* according to mscoree.dll */) - : source_(source), - event_id_(event_id) { - try { - current_user_sid_ = internal::sid_t::get_current_user_sid(); - } catch (...) { - // get_current_user_sid() is unlikely to fail and if it does, we can still proceed - // without current_user_sid but in the event log the record will have no user name - } - } - - ~win_eventlog_sink() { - if (hEventLog_) DeregisterEventSource(hEventLog_); - } -}; - -} // namespace win_eventlog - -using win_eventlog_sink_mt = win_eventlog::win_eventlog_sink; -using win_eventlog_sink_st = win_eventlog::win_eventlog_sink; - -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h deleted file mode 100644 index 696db56..0000000 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ /dev/null @@ -1,172 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -#include -#include - -namespace spdlog { -namespace sinks { -template -SPDLOG_INLINE wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) - : out_handle_(out_handle), - mutex_(ConsoleMutex::mutex()), - formatter_(details::make_unique()) { - set_color_mode_impl(mode); - // set level colors - colors_[level::trace] = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; // white - colors_[level::debug] = FOREGROUND_GREEN | FOREGROUND_BLUE; // cyan - colors_[level::info] = FOREGROUND_GREEN; // green - colors_[level::warn] = - FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; // intense yellow - colors_[level::err] = FOREGROUND_RED | FOREGROUND_INTENSITY; // intense red - colors_[level::critical] = BACKGROUND_RED | FOREGROUND_RED | FOREGROUND_GREEN | - FOREGROUND_BLUE | - FOREGROUND_INTENSITY; // intense white on red background - colors_[level::off] = 0; -} - -template -SPDLOG_INLINE wincolor_sink::~wincolor_sink() { - this->flush(); -} - -// change the color for the given level -template -void SPDLOG_INLINE wincolor_sink::set_color(level::level_enum level, - std::uint16_t color) { - std::lock_guard lock(mutex_); - colors_[static_cast(level)] = color; -} - -template -void SPDLOG_INLINE wincolor_sink::log(const details::log_msg &msg) { - if (out_handle_ == nullptr || out_handle_ == INVALID_HANDLE_VALUE) { - return; - } - - std::lock_guard lock(mutex_); - msg.color_range_start = 0; - msg.color_range_end = 0; - memory_buf_t formatted; - formatter_->format(msg, formatted); - if (should_do_colors_ && msg.color_range_end > msg.color_range_start) { - // before color range - print_range_(formatted, 0, msg.color_range_start); - // in color range - auto orig_attribs = - static_cast(set_foreground_color_(colors_[static_cast(msg.level)])); - print_range_(formatted, msg.color_range_start, msg.color_range_end); - // reset to orig colors - ::SetConsoleTextAttribute(static_cast(out_handle_), orig_attribs); - print_range_(formatted, msg.color_range_end, formatted.size()); - } else // print without colors if color range is invalid (or color is disabled) - { - write_to_file_(formatted); - } -} - -template -void SPDLOG_INLINE wincolor_sink::flush() { - // windows console always flushed? -} - -template -void SPDLOG_INLINE wincolor_sink::set_pattern(const std::string &pattern) { - std::lock_guard lock(mutex_); - formatter_ = std::unique_ptr(new pattern_formatter(pattern)); -} - -template -void SPDLOG_INLINE -wincolor_sink::set_formatter(std::unique_ptr sink_formatter) { - std::lock_guard lock(mutex_); - formatter_ = std::move(sink_formatter); -} - -template -void SPDLOG_INLINE wincolor_sink::set_color_mode(color_mode mode) { - std::lock_guard lock(mutex_); - set_color_mode_impl(mode); -} - -template -void SPDLOG_INLINE wincolor_sink::set_color_mode_impl(color_mode mode) { - if (mode == color_mode::automatic) { - // should do colors only if out_handle_ points to actual console. - DWORD console_mode; - bool in_console = ::GetConsoleMode(static_cast(out_handle_), &console_mode) != 0; - should_do_colors_ = in_console; - } else { - should_do_colors_ = mode == color_mode::always ? true : false; - } -} - -// set foreground color and return the orig console attributes (for resetting later) -template -std::uint16_t SPDLOG_INLINE -wincolor_sink::set_foreground_color_(std::uint16_t attribs) { - CONSOLE_SCREEN_BUFFER_INFO orig_buffer_info; - if (!::GetConsoleScreenBufferInfo(static_cast(out_handle_), &orig_buffer_info)) { - // just return white if failed getting console info - return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; - } - - // change only the foreground bits (lowest 4 bits) - auto new_attribs = static_cast(attribs) | (orig_buffer_info.wAttributes & 0xfff0); - auto ignored = - ::SetConsoleTextAttribute(static_cast(out_handle_), static_cast(new_attribs)); - (void)(ignored); - return static_cast(orig_buffer_info.wAttributes); // return orig attribs -} - -// print a range of formatted message to console -template -void SPDLOG_INLINE wincolor_sink::print_range_(const memory_buf_t &formatted, - size_t start, - size_t end) { - if (end > start) { -#if defined(SPDLOG_UTF8_TO_WCHAR_CONSOLE) - wmemory_buf_t wformatted; - details::os::utf8_to_wstrbuf(string_view_t(formatted.data() + start, end - start), - wformatted); - auto size = static_cast(wformatted.size()); - auto ignored = ::WriteConsoleW(static_cast(out_handle_), wformatted.data(), size, - nullptr, nullptr); -#else - auto size = static_cast(end - start); - auto ignored = ::WriteConsoleA(static_cast(out_handle_), formatted.data() + start, - size, nullptr, nullptr); -#endif - (void)(ignored); - } -} - -template -void SPDLOG_INLINE wincolor_sink::write_to_file_(const memory_buf_t &formatted) { - auto size = static_cast(formatted.size()); - DWORD bytes_written = 0; - auto ignored = ::WriteFile(static_cast(out_handle_), formatted.data(), size, - &bytes_written, nullptr); - (void)(ignored); -} - -// wincolor_stdout_sink -template -SPDLOG_INLINE wincolor_stdout_sink::wincolor_stdout_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_OUTPUT_HANDLE), mode) {} - -// wincolor_stderr_sink -template -SPDLOG_INLINE wincolor_stderr_sink::wincolor_stderr_sink(color_mode mode) - : wincolor_sink(::GetStdHandle(STD_ERROR_HANDLE), mode) {} -} // namespace sinks -} // namespace spdlog diff --git a/include/spdlog/sinks/wincolor_sink.h b/include/spdlog/sinks/wincolor_sink.h deleted file mode 100644 index 8ba594c..0000000 --- a/include/spdlog/sinks/wincolor_sink.h +++ /dev/null @@ -1,82 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace spdlog { -namespace sinks { -/* - * Windows color console sink. Uses WriteConsoleA to write to the console with - * colors - */ -template -class wincolor_sink : public sink { -public: - wincolor_sink(void *out_handle, color_mode mode); - ~wincolor_sink() override; - - wincolor_sink(const wincolor_sink &other) = delete; - wincolor_sink &operator=(const wincolor_sink &other) = delete; - - // change the color for the given level - void set_color(level::level_enum level, std::uint16_t color); - void log(const details::log_msg &msg) final override; - void flush() final override; - void set_pattern(const std::string &pattern) override final; - void set_formatter(std::unique_ptr sink_formatter) override final; - void set_color_mode(color_mode mode); - -protected: - using mutex_t = typename ConsoleMutex::mutex_t; - void *out_handle_; - mutex_t &mutex_; - bool should_do_colors_; - std::unique_ptr formatter_; - std::array colors_; - - // set foreground color and return the orig console attributes (for resetting later) - std::uint16_t set_foreground_color_(std::uint16_t attribs); - - // print a range of formatted message to console - void print_range_(const memory_buf_t &formatted, size_t start, size_t end); - - // in case we are redirected to file (not in console mode) - void write_to_file_(const memory_buf_t &formatted); - - void set_color_mode_impl(color_mode mode); -}; - -template -class wincolor_stdout_sink : public wincolor_sink { -public: - explicit wincolor_stdout_sink(color_mode mode = color_mode::automatic); -}; - -template -class wincolor_stderr_sink : public wincolor_sink { -public: - explicit wincolor_stderr_sink(color_mode mode = color_mode::automatic); -}; - -using wincolor_stdout_sink_mt = wincolor_stdout_sink; -using wincolor_stdout_sink_st = wincolor_stdout_sink; - -using wincolor_stderr_sink_mt = wincolor_stderr_sink; -using wincolor_stderr_sink_st = wincolor_stderr_sink; -} // namespace sinks -} // namespace spdlog - -#ifdef SPDLOG_HEADER_ONLY - #include "wincolor_sink-inl.h" -#endif diff --git a/include/spdlog/spdlog-inl.h b/include/spdlog/spdlog-inl.h deleted file mode 100644 index 97c3622..0000000 --- a/include/spdlog/spdlog-inl.h +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#ifndef SPDLOG_HEADER_ONLY - #include -#endif - -#include -#include - -namespace spdlog { - -SPDLOG_INLINE void initialize_logger(std::shared_ptr logger) { - details::registry::instance().initialize_logger(std::move(logger)); -} - -SPDLOG_INLINE std::shared_ptr get(const std::string &name) { - return details::registry::instance().get(name); -} - -SPDLOG_INLINE void set_formatter(std::unique_ptr formatter) { - details::registry::instance().set_formatter(std::move(formatter)); -} - -SPDLOG_INLINE void set_pattern(std::string pattern, pattern_time_type time_type) { - set_formatter( - std::unique_ptr(new pattern_formatter(std::move(pattern), time_type))); -} - -SPDLOG_INLINE void enable_backtrace(size_t n_messages) { - details::registry::instance().enable_backtrace(n_messages); -} - -SPDLOG_INLINE void disable_backtrace() { details::registry::instance().disable_backtrace(); } - -SPDLOG_INLINE void dump_backtrace() { default_logger_raw()->dump_backtrace(); } - -SPDLOG_INLINE level::level_enum get_level() { return default_logger_raw()->level(); } - -SPDLOG_INLINE bool should_log(level::level_enum log_level) { - return default_logger_raw()->should_log(log_level); -} - -SPDLOG_INLINE void set_level(level::level_enum log_level) { - details::registry::instance().set_level(log_level); -} - -SPDLOG_INLINE void flush_on(level::level_enum log_level) { - details::registry::instance().flush_on(log_level); -} - -SPDLOG_INLINE void set_error_handler(void (*handler)(const std::string &msg)) { - details::registry::instance().set_error_handler(handler); -} - -SPDLOG_INLINE void register_logger(std::shared_ptr logger) { - details::registry::instance().register_logger(std::move(logger)); -} - -SPDLOG_INLINE void apply_all(const std::function)> &fun) { - details::registry::instance().apply_all(fun); -} - -SPDLOG_INLINE void drop(const std::string &name) { details::registry::instance().drop(name); } - -SPDLOG_INLINE void drop_all() { details::registry::instance().drop_all(); } - -SPDLOG_INLINE void shutdown() { details::registry::instance().shutdown(); } - -SPDLOG_INLINE void set_automatic_registration(bool automatic_registration) { - details::registry::instance().set_automatic_registration(automatic_registration); -} - -SPDLOG_INLINE std::shared_ptr default_logger() { - return details::registry::instance().default_logger(); -} - -SPDLOG_INLINE spdlog::logger *default_logger_raw() { - return details::registry::instance().get_default_raw(); -} - -SPDLOG_INLINE void set_default_logger(std::shared_ptr default_logger) { - details::registry::instance().set_default_logger(std::move(default_logger)); -} - -SPDLOG_INLINE void apply_logger_env_levels(std::shared_ptr logger) { - details::registry::instance().apply_logger_env_levels(std::move(logger)); -} - -} // namespace spdlog diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h deleted file mode 100644 index a8afbce..0000000 --- a/include/spdlog/spdlog.h +++ /dev/null @@ -1,352 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -// spdlog main header file. -// see example.cpp for usage example - -#ifndef SPDLOG_H -#define SPDLOG_H - -#pragma once - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -namespace spdlog { - -using default_factory = synchronous_factory; - -// Create and register a logger with a templated sink type -// The logger's level, formatter and flush level will be set according the -// global settings. -// -// Example: -// spdlog::create("logger_name", "dailylog_filename", 11, 59); -template -inline std::shared_ptr create(std::string logger_name, SinkArgs &&...sink_args) { - return default_factory::create(std::move(logger_name), - std::forward(sink_args)...); -} - -// Initialize and register a logger, -// formatter and flush level will be set according the global settings. -// -// Useful for initializing manually created loggers with the global settings. -// -// Example: -// auto mylogger = std::make_shared("mylogger", ...); -// spdlog::initialize_logger(mylogger); -SPDLOG_API void initialize_logger(std::shared_ptr logger); - -// Return an existing logger or nullptr if a logger with such name doesn't -// exist. -// example: spdlog::get("my_logger")->info("hello {}", "world"); -SPDLOG_API std::shared_ptr get(const std::string &name); - -// Set global formatter. Each sink in each logger will get a clone of this object -SPDLOG_API void set_formatter(std::unique_ptr formatter); - -// Set global format string. -// example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v"); -SPDLOG_API void set_pattern(std::string pattern, - pattern_time_type time_type = pattern_time_type::local); - -// enable global backtrace support -SPDLOG_API void enable_backtrace(size_t n_messages); - -// disable global backtrace support -SPDLOG_API void disable_backtrace(); - -// call dump backtrace on default logger -SPDLOG_API void dump_backtrace(); - -// Get global logging level -SPDLOG_API level::level_enum get_level(); - -// Set global logging level -SPDLOG_API void set_level(level::level_enum log_level); - -// Determine whether the default logger should log messages with a certain level -SPDLOG_API bool should_log(level::level_enum lvl); - -// Set global flush level -SPDLOG_API void flush_on(level::level_enum log_level); - -// Start/Restart a periodic flusher thread -// Warning: Use only if all your loggers are thread safe! -template -inline void flush_every(std::chrono::duration interval) { - details::registry::instance().flush_every(interval); -} - -// Set global error handler -SPDLOG_API void set_error_handler(void (*handler)(const std::string &msg)); - -// Register the given logger with the given name -SPDLOG_API void register_logger(std::shared_ptr logger); - -// Apply a user defined function on all registered loggers -// Example: -// spdlog::apply_all([&](std::shared_ptr l) {l->flush();}); -SPDLOG_API void apply_all(const std::function)> &fun); - -// Drop the reference to the given logger -SPDLOG_API void drop(const std::string &name); - -// Drop all references from the registry -SPDLOG_API void drop_all(); - -// stop any running threads started by spdlog and clean registry loggers -SPDLOG_API void shutdown(); - -// Automatic registration of loggers when using spdlog::create() or spdlog::create_async -SPDLOG_API void set_automatic_registration(bool automatic_registration); - -// API for using default logger (stdout_color_mt), -// e.g: spdlog::info("Message {}", 1); -// -// The default logger object can be accessed using the spdlog::default_logger(): -// For example, to add another sink to it: -// spdlog::default_logger()->sinks().push_back(some_sink); -// -// The default logger can replaced using spdlog::set_default_logger(new_logger). -// For example, to replace it with a file logger. -// -// IMPORTANT: -// The default API is thread safe (for _mt loggers), but: -// set_default_logger() *should not* be used concurrently with the default API. -// e.g do not call set_default_logger() from one thread while calling spdlog::info() from another. - -SPDLOG_API std::shared_ptr default_logger(); - -SPDLOG_API spdlog::logger *default_logger_raw(); - -SPDLOG_API void set_default_logger(std::shared_ptr default_logger); - -// Initialize logger level based on environment configs. -// -// Useful for applying SPDLOG_LEVEL to manually created loggers. -// -// Example: -// auto mylogger = std::make_shared("mylogger", ...); -// spdlog::apply_logger_env_levels(mylogger); -SPDLOG_API void apply_logger_env_levels(std::shared_ptr logger); - -template -inline void log(source_loc source, - level::level_enum lvl, - format_string_t fmt, - Args &&...args) { - default_logger_raw()->log(source, lvl, fmt, std::forward(args)...); -} - -template -inline void log(level::level_enum lvl, format_string_t fmt, Args &&...args) { - default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); -} - -template -inline void trace(format_string_t fmt, Args &&...args) { - default_logger_raw()->trace(fmt, std::forward(args)...); -} - -template -inline void debug(format_string_t fmt, Args &&...args) { - default_logger_raw()->debug(fmt, std::forward(args)...); -} - -template -inline void info(format_string_t fmt, Args &&...args) { - default_logger_raw()->info(fmt, std::forward(args)...); -} - -template -inline void warn(format_string_t fmt, Args &&...args) { - default_logger_raw()->warn(fmt, std::forward(args)...); -} - -template -inline void error(format_string_t fmt, Args &&...args) { - default_logger_raw()->error(fmt, std::forward(args)...); -} - -template -inline void critical(format_string_t fmt, Args &&...args) { - default_logger_raw()->critical(fmt, std::forward(args)...); -} - -template -inline void log(source_loc source, level::level_enum lvl, const T &msg) { - default_logger_raw()->log(source, lvl, msg); -} - -template -inline void log(level::level_enum lvl, const T &msg) { - default_logger_raw()->log(lvl, msg); -} - -#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT -template -inline void log(source_loc source, - level::level_enum lvl, - wformat_string_t fmt, - Args &&...args) { - default_logger_raw()->log(source, lvl, fmt, std::forward(args)...); -} - -template -inline void log(level::level_enum lvl, wformat_string_t fmt, Args &&...args) { - default_logger_raw()->log(source_loc{}, lvl, fmt, std::forward(args)...); -} - -template -inline void trace(wformat_string_t fmt, Args &&...args) { - default_logger_raw()->trace(fmt, std::forward(args)...); -} - -template -inline void debug(wformat_string_t fmt, Args &&...args) { - default_logger_raw()->debug(fmt, std::forward(args)...); -} - -template -inline void info(wformat_string_t fmt, Args &&...args) { - default_logger_raw()->info(fmt, std::forward(args)...); -} - -template -inline void warn(wformat_string_t fmt, Args &&...args) { - default_logger_raw()->warn(fmt, std::forward(args)...); -} - -template -inline void error(wformat_string_t fmt, Args &&...args) { - default_logger_raw()->error(fmt, std::forward(args)...); -} - -template -inline void critical(wformat_string_t fmt, Args &&...args) { - default_logger_raw()->critical(fmt, std::forward(args)...); -} -#endif - -template -inline void trace(const T &msg) { - default_logger_raw()->trace(msg); -} - -template -inline void debug(const T &msg) { - default_logger_raw()->debug(msg); -} - -template -inline void info(const T &msg) { - default_logger_raw()->info(msg); -} - -template -inline void warn(const T &msg) { - default_logger_raw()->warn(msg); -} - -template -inline void error(const T &msg) { - default_logger_raw()->error(msg); -} - -template -inline void critical(const T &msg) { - default_logger_raw()->critical(msg); -} - -} // namespace spdlog - -// -// enable/disable log calls at compile time according to global level. -// -// define SPDLOG_ACTIVE_LEVEL to one of those (before including spdlog.h): -// SPDLOG_LEVEL_TRACE, -// SPDLOG_LEVEL_DEBUG, -// SPDLOG_LEVEL_INFO, -// SPDLOG_LEVEL_WARN, -// SPDLOG_LEVEL_ERROR, -// SPDLOG_LEVEL_CRITICAL, -// SPDLOG_LEVEL_OFF -// - -#ifndef SPDLOG_NO_SOURCE_LOC - #define SPDLOG_LOGGER_CALL(logger, level, ...) \ - (logger)->log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, level, __VA_ARGS__) -#else - #define SPDLOG_LOGGER_CALL(logger, level, ...) \ - (logger)->log(spdlog::source_loc{}, level, __VA_ARGS__) -#endif - -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE - #define SPDLOG_LOGGER_TRACE(logger, ...) \ - SPDLOG_LOGGER_CALL(logger, spdlog::level::trace, __VA_ARGS__) - #define SPDLOG_TRACE(...) SPDLOG_LOGGER_TRACE(spdlog::default_logger_raw(), __VA_ARGS__) -#else - #define SPDLOG_LOGGER_TRACE(logger, ...) (void)0 - #define SPDLOG_TRACE(...) (void)0 -#endif - -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG - #define SPDLOG_LOGGER_DEBUG(logger, ...) \ - SPDLOG_LOGGER_CALL(logger, spdlog::level::debug, __VA_ARGS__) - #define SPDLOG_DEBUG(...) SPDLOG_LOGGER_DEBUG(spdlog::default_logger_raw(), __VA_ARGS__) -#else - #define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0 - #define SPDLOG_DEBUG(...) (void)0 -#endif - -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO - #define SPDLOG_LOGGER_INFO(logger, ...) \ - SPDLOG_LOGGER_CALL(logger, spdlog::level::info, __VA_ARGS__) - #define SPDLOG_INFO(...) SPDLOG_LOGGER_INFO(spdlog::default_logger_raw(), __VA_ARGS__) -#else - #define SPDLOG_LOGGER_INFO(logger, ...) (void)0 - #define SPDLOG_INFO(...) (void)0 -#endif - -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN - #define SPDLOG_LOGGER_WARN(logger, ...) \ - SPDLOG_LOGGER_CALL(logger, spdlog::level::warn, __VA_ARGS__) - #define SPDLOG_WARN(...) SPDLOG_LOGGER_WARN(spdlog::default_logger_raw(), __VA_ARGS__) -#else - #define SPDLOG_LOGGER_WARN(logger, ...) (void)0 - #define SPDLOG_WARN(...) (void)0 -#endif - -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR - #define SPDLOG_LOGGER_ERROR(logger, ...) \ - SPDLOG_LOGGER_CALL(logger, spdlog::level::err, __VA_ARGS__) - #define SPDLOG_ERROR(...) SPDLOG_LOGGER_ERROR(spdlog::default_logger_raw(), __VA_ARGS__) -#else - #define SPDLOG_LOGGER_ERROR(logger, ...) (void)0 - #define SPDLOG_ERROR(...) (void)0 -#endif - -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL - #define SPDLOG_LOGGER_CRITICAL(logger, ...) \ - SPDLOG_LOGGER_CALL(logger, spdlog::level::critical, __VA_ARGS__) - #define SPDLOG_CRITICAL(...) SPDLOG_LOGGER_CRITICAL(spdlog::default_logger_raw(), __VA_ARGS__) -#else - #define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0 - #define SPDLOG_CRITICAL(...) (void)0 -#endif - -#ifdef SPDLOG_HEADER_ONLY - #include "spdlog-inl.h" -#endif - -#endif // SPDLOG_H diff --git a/include/spdlog/stopwatch.h b/include/spdlog/stopwatch.h deleted file mode 100644 index 54ab3d3..0000000 --- a/include/spdlog/stopwatch.h +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#include -#include - -// Stopwatch support for spdlog (using std::chrono::steady_clock). -// Displays elapsed seconds since construction as double. -// -// Usage: -// -// spdlog::stopwatch sw; -// ... -// spdlog::debug("Elapsed: {} seconds", sw); => "Elapsed 0.005116733 seconds" -// spdlog::info("Elapsed: {:.6} seconds", sw); => "Elapsed 0.005163 seconds" -// -// -// If other units are needed (e.g. millis instead of double), include "fmt/chrono.h" and use -// "duration_cast<..>(sw.elapsed())": -// -// #include -//.. -// using std::chrono::duration_cast; -// using std::chrono::milliseconds; -// spdlog::info("Elapsed {}", duration_cast(sw.elapsed())); => "Elapsed 5ms" - -namespace spdlog { -class stopwatch { - using clock = std::chrono::steady_clock; - std::chrono::time_point start_tp_; - -public: - stopwatch() - : start_tp_{clock::now()} {} - - std::chrono::duration elapsed() const { - return std::chrono::duration(clock::now() - start_tp_); - } - - std::chrono::milliseconds elapsed_ms() const { - return std::chrono::duration_cast(clock::now() - start_tp_); - } - - void reset() { start_tp_ = clock::now(); } -}; -} // namespace spdlog - -// Support for fmt formatting (e.g. "{:012.9}" or just "{}") -namespace -#ifdef SPDLOG_USE_STD_FORMAT - std -#else - fmt -#endif -{ - -template <> -struct formatter : formatter { - template - auto format(const spdlog::stopwatch &sw, FormatContext &ctx) const -> decltype(ctx.out()) { - return formatter::format(sw.elapsed().count(), ctx); - } -}; -} // namespace std diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h deleted file mode 100644 index a47a907..0000000 --- a/include/spdlog/tweakme.h +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -/////////////////////////////////////////////////////////////////////////////// -// -// Edit this file to squeeze more performance, and to customize supported -// features -// -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Under Linux, the much faster CLOCK_REALTIME_COARSE clock can be used. -// This clock is less accurate - can be off by dozens of millis - depending on -// the kernel HZ. -// Uncomment to use it instead of the regular clock. -// -// #define SPDLOG_CLOCK_COARSE -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment if source location logging is not needed. -// This will prevent spdlog from using __FILE__, __LINE__ and SPDLOG_FUNCTION -// -// #define SPDLOG_NO_SOURCE_LOC -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment if thread id logging is not needed (i.e. no %t in the log pattern). -// This will prevent spdlog from querying the thread id on each log call. -// -// WARNING: If the log pattern contains thread id (i.e, %t) while this flag is -// on, zero will be logged as thread id. -// -// #define SPDLOG_NO_THREAD_ID -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to prevent spdlog from using thread local storage. -// -// WARNING: if your program forks, UNCOMMENT this flag to prevent undefined -// thread ids in the children logs. -// -// #define SPDLOG_NO_TLS -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to avoid spdlog's usage of atomic log levels -// Use only if your code never modifies a logger's log levels concurrently by -// different threads. -// -// #define SPDLOG_NO_ATOMIC_LEVELS -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to enable usage of wchar_t for file names on Windows. -// -// #define SPDLOG_WCHAR_FILENAMES -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to override default eol ("\n" or "\r\n" under Linux/Windows) -// -// #define SPDLOG_EOL ";-)\n" -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to override default folder separators ("/" or "\\/" under -// Linux/Windows). Each character in the string is treated as a different -// separator. -// -// #define SPDLOG_FOLDER_SEPS "\\" -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to use your own copy of the fmt library instead of spdlog's copy. -// In this case spdlog will try to include so set your -I flag -// accordingly. -// -// #define SPDLOG_FMT_EXTERNAL -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to use C++20 std::format instead of fmt. -// -// #define SPDLOG_USE_STD_FORMAT -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to enable wchar_t support (convert to utf8) -// -// #define SPDLOG_WCHAR_TO_UTF8_SUPPORT -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to prevent child processes from inheriting log file descriptors -// -// #define SPDLOG_PREVENT_CHILD_FD -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to customize level names (e.g. "MY TRACE") -// -// #define SPDLOG_LEVEL_NAMES { "MY TRACE", "MY DEBUG", "MY INFO", "MY WARNING", "MY ERROR", "MY -// CRITICAL", "OFF" } -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to customize short level names (e.g. "MT") -// These can be longer than one character. -// -// #define SPDLOG_SHORT_LEVEL_NAMES { "T", "D", "I", "W", "E", "C", "O" } -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment to disable default logger creation. -// This might save some (very) small initialization time if no default logger is needed. -// -// #define SPDLOG_DISABLE_DEFAULT_LOGGER -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment and set to compile time level with zero cost (default is INFO). -// Macros like SPDLOG_DEBUG(..), SPDLOG_INFO(..) will expand to empty statements if not enabled -// -// #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO -/////////////////////////////////////////////////////////////////////////////// - -/////////////////////////////////////////////////////////////////////////////// -// Uncomment (and change if desired) macro to use for function names. -// This is compiler dependent. -// __PRETTY_FUNCTION__ might be nicer in clang/gcc, and __FUNCTION__ in msvc. -// Defaults to __FUNCTION__ (should work on all compilers) if not defined. -// -// #ifdef __PRETTY_FUNCTION__ -// # define SPDLOG_FUNCTION __PRETTY_FUNCTION__ -// #else -// # define SPDLOG_FUNCTION __FUNCTION__ -// #endif -/////////////////////////////////////////////////////////////////////////////// diff --git a/include/spdlog/version.h b/include/spdlog/version.h deleted file mode 100644 index 7c5e129..0000000 --- a/include/spdlog/version.h +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright(c) 2015-present, Gabi Melman & spdlog contributors. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) - -#pragma once - -#define SPDLOG_VER_MAJOR 1 -#define SPDLOG_VER_MINOR 15 -#define SPDLOG_VER_PATCH 0 - -#define SPDLOG_TO_VERSION(major, minor, patch) (major * 10000 + minor * 100 + patch) -#define SPDLOG_VERSION SPDLOG_TO_VERSION(SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH) diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index 2712396..f8c59c9 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -11,6 +11,9 @@ add_executable(ocvsmd-cli target_link_libraries(ocvsmd-cli PUBLIC ocvsmd_sdk ) +target_include_directories(ocvsmd-cli SYSTEM + PUBLIC ${submodules_dir}/spdlog/include +) add_dependencies(ocvsmd-cli ocvsmd ) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index fd28879..8032ea3 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -38,6 +38,7 @@ target_include_directories(ocvsmd_common target_include_directories(ocvsmd_common SYSTEM PUBLIC ${submodules_dir}/cetl/include PUBLIC ${submodules_dir}/libcyphal/include + PUBLIC ${submodules_dir}/spdlog/include ) add_dependencies(ocvsmd_common ${common_transpiled} diff --git a/submodules/spdlog b/submodules/spdlog new file mode 160000 index 0000000..f355b3d --- /dev/null +++ b/submodules/spdlog @@ -0,0 +1 @@ +Subproject commit f355b3d58f7067eee1706ff3c801c2361011f3d5 From a1591017feeadad01177b65455e92d080924ed4a Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 10 Feb 2025 14:15:37 +0200 Subject: [PATCH 125/156] make Toml11 v4.3.0 dependency as submodule --- .gitmodules | 3 + include/toml.hpp | 62 - include/toml11/LICENSE | 21 - include/toml11/color.hpp | 10 - include/toml11/comments.hpp | 10 - include/toml11/compat.hpp | 810 ---- include/toml11/context.hpp | 68 - include/toml11/conversion.hpp | 203 - include/toml11/datetime.hpp | 10 - include/toml11/error_info.hpp | 10 - include/toml11/exception.hpp | 17 - include/toml11/find.hpp | 548 --- include/toml11/format.hpp | 10 - include/toml11/from.hpp | 17 - include/toml11/fwd/color_fwd.hpp | 88 - include/toml11/fwd/comments_fwd.hpp | 451 --- include/toml11/fwd/datetime_fwd.hpp | 261 -- include/toml11/fwd/error_info_fwd.hpp | 97 - include/toml11/fwd/format_fwd.hpp | 250 -- include/toml11/fwd/literal_fwd.hpp | 33 - include/toml11/fwd/location_fwd.hpp | 149 - include/toml11/fwd/region_fwd.hpp | 110 - include/toml11/fwd/scanner_fwd.hpp | 391 -- include/toml11/fwd/source_location_fwd.hpp | 148 - include/toml11/fwd/syntax_fwd.hpp | 357 -- include/toml11/fwd/value_t_fwd.hpp | 117 - include/toml11/get.hpp | 632 --- include/toml11/impl/color_impl.hpp | 76 - include/toml11/impl/comments_impl.hpp | 46 - include/toml11/impl/datetime_impl.hpp | 518 --- include/toml11/impl/error_info_impl.hpp | 75 - include/toml11/impl/format_impl.hpp | 297 -- include/toml11/impl/literal_impl.hpp | 174 - include/toml11/impl/location_impl.hpp | 209 - include/toml11/impl/region_impl.hpp | 246 -- include/toml11/impl/scanner_impl.hpp | 473 --- include/toml11/impl/source_location_impl.hpp | 211 - include/toml11/impl/syntax_impl.hpp | 732 ---- include/toml11/impl/value_t_impl.hpp | 40 - include/toml11/into.hpp | 17 - include/toml11/literal.hpp | 10 - include/toml11/location.hpp | 10 - include/toml11/ordered_map.hpp | 265 -- include/toml11/parser.hpp | 3829 ------------------ include/toml11/region.hpp | 10 - include/toml11/result.hpp | 486 --- include/toml11/scanner.hpp | 10 - include/toml11/serializer.hpp | 1275 ------ include/toml11/skip.hpp | 392 -- include/toml11/source_location.hpp | 10 - include/toml11/spec.hpp | 121 - include/toml11/storage.hpp | 49 - include/toml11/syntax.hpp | 10 - include/toml11/traits.hpp | 254 -- include/toml11/types.hpp | 374 -- include/toml11/utility.hpp | 170 - include/toml11/value.hpp | 2257 ----------- include/toml11/value_t.hpp | 10 - include/toml11/version.hpp | 121 - include/toml11/visit.hpp | 136 - src/daemon/engine/CMakeLists.txt | 1 + submodules/toml11 | 1 + 62 files changed, 5 insertions(+), 17793 deletions(-) delete mode 100644 include/toml.hpp delete mode 100644 include/toml11/LICENSE delete mode 100644 include/toml11/color.hpp delete mode 100644 include/toml11/comments.hpp delete mode 100644 include/toml11/compat.hpp delete mode 100644 include/toml11/context.hpp delete mode 100644 include/toml11/conversion.hpp delete mode 100644 include/toml11/datetime.hpp delete mode 100644 include/toml11/error_info.hpp delete mode 100644 include/toml11/exception.hpp delete mode 100644 include/toml11/find.hpp delete mode 100644 include/toml11/format.hpp delete mode 100644 include/toml11/from.hpp delete mode 100644 include/toml11/fwd/color_fwd.hpp delete mode 100644 include/toml11/fwd/comments_fwd.hpp delete mode 100644 include/toml11/fwd/datetime_fwd.hpp delete mode 100644 include/toml11/fwd/error_info_fwd.hpp delete mode 100644 include/toml11/fwd/format_fwd.hpp delete mode 100644 include/toml11/fwd/literal_fwd.hpp delete mode 100644 include/toml11/fwd/location_fwd.hpp delete mode 100644 include/toml11/fwd/region_fwd.hpp delete mode 100644 include/toml11/fwd/scanner_fwd.hpp delete mode 100644 include/toml11/fwd/source_location_fwd.hpp delete mode 100644 include/toml11/fwd/syntax_fwd.hpp delete mode 100644 include/toml11/fwd/value_t_fwd.hpp delete mode 100644 include/toml11/get.hpp delete mode 100644 include/toml11/impl/color_impl.hpp delete mode 100644 include/toml11/impl/comments_impl.hpp delete mode 100644 include/toml11/impl/datetime_impl.hpp delete mode 100644 include/toml11/impl/error_info_impl.hpp delete mode 100644 include/toml11/impl/format_impl.hpp delete mode 100644 include/toml11/impl/literal_impl.hpp delete mode 100644 include/toml11/impl/location_impl.hpp delete mode 100644 include/toml11/impl/region_impl.hpp delete mode 100644 include/toml11/impl/scanner_impl.hpp delete mode 100644 include/toml11/impl/source_location_impl.hpp delete mode 100644 include/toml11/impl/syntax_impl.hpp delete mode 100644 include/toml11/impl/value_t_impl.hpp delete mode 100644 include/toml11/into.hpp delete mode 100644 include/toml11/literal.hpp delete mode 100644 include/toml11/location.hpp delete mode 100644 include/toml11/ordered_map.hpp delete mode 100644 include/toml11/parser.hpp delete mode 100644 include/toml11/region.hpp delete mode 100644 include/toml11/result.hpp delete mode 100644 include/toml11/scanner.hpp delete mode 100644 include/toml11/serializer.hpp delete mode 100644 include/toml11/skip.hpp delete mode 100644 include/toml11/source_location.hpp delete mode 100644 include/toml11/spec.hpp delete mode 100644 include/toml11/storage.hpp delete mode 100644 include/toml11/syntax.hpp delete mode 100644 include/toml11/traits.hpp delete mode 100644 include/toml11/types.hpp delete mode 100644 include/toml11/utility.hpp delete mode 100644 include/toml11/value.hpp delete mode 100644 include/toml11/value_t.hpp delete mode 100644 include/toml11/version.hpp delete mode 100644 include/toml11/visit.hpp create mode 160000 submodules/toml11 diff --git a/.gitmodules b/.gitmodules index 90ba1a8..8795069 100644 --- a/.gitmodules +++ b/.gitmodules @@ -20,3 +20,6 @@ path = submodules/spdlog url = https://github.com/gabime/spdlog.git branch = v1.x +[submodule "submodules/toml11"] + path = submodules/toml11 + url = https://github.com/ToruNiina/toml11.git diff --git a/include/toml.hpp b/include/toml.hpp deleted file mode 100644 index 75cc95b..0000000 --- a/include/toml.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef TOML11_TOML_HPP -#define TOML11_TOML_HPP - -// The MIT License (MIT) -// -// Copyright (c) 2017-now Toru Niina -// -// 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. - -// IWYU pragma: begin_exports -#include "toml11/color.hpp" -#include "toml11/comments.hpp" -#include "toml11/compat.hpp" -#include "toml11/context.hpp" -#include "toml11/conversion.hpp" -#include "toml11/datetime.hpp" -#include "toml11/error_info.hpp" -#include "toml11/exception.hpp" -#include "toml11/find.hpp" -#include "toml11/format.hpp" -#include "toml11/from.hpp" -#include "toml11/get.hpp" -#include "toml11/into.hpp" -#include "toml11/literal.hpp" -#include "toml11/location.hpp" -#include "toml11/ordered_map.hpp" -#include "toml11/parser.hpp" -#include "toml11/region.hpp" -#include "toml11/result.hpp" -#include "toml11/scanner.hpp" -#include "toml11/serializer.hpp" -#include "toml11/skip.hpp" -#include "toml11/source_location.hpp" -#include "toml11/spec.hpp" -#include "toml11/storage.hpp" -#include "toml11/syntax.hpp" -#include "toml11/traits.hpp" -#include "toml11/types.hpp" -#include "toml11/utility.hpp" -#include "toml11/value.hpp" -#include "toml11/value_t.hpp" -#include "toml11/version.hpp" -#include "toml11/visit.hpp" -// IWYU pragma: end_exports - -#endif// TOML11_TOML_HPP diff --git a/include/toml11/LICENSE b/include/toml11/LICENSE deleted file mode 100644 index f55c511..0000000 --- a/include/toml11/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2017 Toru Niina - -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. diff --git a/include/toml11/color.hpp b/include/toml11/color.hpp deleted file mode 100644 index d40d7f8..0000000 --- a/include/toml11/color.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_COLOR_HPP -#define TOML11_COLOR_HPP - -#include "fwd/color_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/color_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_COLOR_HPP diff --git a/include/toml11/comments.hpp b/include/toml11/comments.hpp deleted file mode 100644 index 4697f4e..0000000 --- a/include/toml11/comments.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_COMMENTS_HPP -#define TOML11_COMMENTS_HPP - -#include "fwd/comments_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/comments_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_COMMENTS_HPP diff --git a/include/toml11/compat.hpp b/include/toml11/compat.hpp deleted file mode 100644 index 15a00af..0000000 --- a/include/toml11/compat.hpp +++ /dev/null @@ -1,810 +0,0 @@ -#ifndef TOML11_COMPAT_HPP -#define TOML11_COMPAT_HPP - -#include "version.hpp" - -#include -#include -#include -#include -#include - -#include - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE -# if __has_include() -# include -# endif -#endif - -#include - -// ---------------------------------------------------------------------------- - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE -# if __has_cpp_attribute(deprecated) -# define TOML11_HAS_ATTR_DEPRECATED 1 -# endif -#endif - -#if defined(TOML11_HAS_ATTR_DEPRECATED) -# define TOML11_DEPRECATED(msg) [[deprecated(msg)]] -#elif defined(__GNUC__) -# define TOML11_DEPRECATED(msg) __attribute__((deprecated(msg))) -#elif defined(_MSC_VER) -# define TOML11_DEPRECATED(msg) __declspec(deprecated(msg)) -#else -# define TOML11_DEPRECATED(msg) -#endif - -// ---------------------------------------------------------------------------- - -#if defined(__cpp_if_constexpr) -# if __cpp_if_constexpr >= 201606L -# define TOML11_HAS_CONSTEXPR_IF 1 -# endif -#endif - -#if defined(TOML11_HAS_CONSTEXPR_IF) -# define TOML11_CONSTEXPR_IF if constexpr -#else -# define TOML11_CONSTEXPR_IF if -#endif - -// ---------------------------------------------------------------------------- - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE -# if defined(__cpp_lib_make_unique) -# if __cpp_lib_make_unique >= 201304L -# define TOML11_HAS_STD_MAKE_UNIQUE 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ - -#if defined(TOML11_HAS_STD_MAKE_UNIQUE) - -using std::make_unique; - -#else - -template -std::unique_ptr make_unique(Ts&& ... args) -{ - return std::unique_ptr(new T(std::forward(args)...)); -} - -#endif // TOML11_HAS_STD_MAKE_UNIQUE - -} // cxx -} // toml - -// --------------------------------------------------------------------------- - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE -# if defined(__cpp_lib_make_reverse_iterator) -# if __cpp_lib_make_reverse_iterator >= 201402L -# define TOML11_HAS_STD_MAKE_REVERSE_ITERATOR 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -# if defined(TOML11_HAS_STD_MAKE_REVERSE_ITERATOR) - -using std::make_reverse_iterator; - -#else - -template -std::reverse_iterator make_reverse_iterator(Iterator iter) -{ - return std::reverse_iterator(iter); -} - -#endif // TOML11_HAS_STD_MAKE_REVERSE_ITERATOR - -} // cxx -} // toml - -// --------------------------------------------------------------------------- - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE -# if defined(__cpp_lib_clamp) -# if __cpp_lib_clamp >= 201603L -# define TOML11_HAS_STD_CLAMP 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_CLAMP) - -using std::clamp; - -#else - -template -T clamp(const T& x, const T& low, const T& high) noexcept -{ - assert(low <= high); - return (std::min)((std::max)(x, low), high); -} - -#endif // TOML11_HAS_STD_CLAMP - -} // cxx -} // toml - -// --------------------------------------------------------------------------- - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE -# if defined(__cpp_lib_bit_cast) -# if __cpp_lib_bit_cast >= 201806L -# define TOML11_HAS_STD_BIT_CAST 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_BIT_CAST) - -using std::bit_cast; - -#else - -template -U bit_cast(const T& x) noexcept -{ - static_assert(sizeof(T) == sizeof(U), ""); - static_assert(std::is_default_constructible::value, ""); - - U z; - std::memcpy(reinterpret_cast(std::addressof(z)), - reinterpret_cast(std::addressof(x)), - sizeof(T)); - - return z; -} - -#endif // TOML11_HAS_STD_BIT_CAST - -} // cxx -} // toml - -// --------------------------------------------------------------------------- -// C++20 remove_cvref_t - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX20_VALUE -# if defined(__cpp_lib_remove_cvref) -# if __cpp_lib_remove_cvref >= 201711L -# define TOML11_HAS_STD_REMOVE_CVREF 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_REMOVE_CVREF) - -using std::remove_cvref; -using std::remove_cvref_t; - -#else - -template -struct remove_cvref -{ - using type = typename std::remove_cv< - typename std::remove_reference::type>::type; -}; - -template -using remove_cvref_t = typename remove_cvref::type; - -#endif // TOML11_HAS_STD_REMOVE_CVREF - -} // cxx -} // toml - -// --------------------------------------------------------------------------- -// C++17 and/or/not - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if defined(__cpp_lib_logical_traits) -# if __cpp_lib_logical_traits >= 201510L -# define TOML11_HAS_STD_CONJUNCTION 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_CONJUNCTION) - -using std::conjunction; -using std::disjunction; -using std::negation; - -#else - -template struct conjunction : std::true_type{}; -template struct conjunction : T{}; -template -struct conjunction : - std::conditional(T::value), conjunction, T>::type -{}; - -template struct disjunction : std::false_type{}; -template struct disjunction : T {}; -template -struct disjunction : - std::conditional(T::value), T, disjunction>::type -{}; - -template -struct negation : std::integral_constant(T::value)>{}; - -#endif // TOML11_HAS_STD_CONJUNCTION - -} // cxx -} // toml - -// --------------------------------------------------------------------------- -// C++14 index_sequence - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE -# if defined(__cpp_lib_integer_sequence) -# if __cpp_lib_integer_sequence >= 201304L -# define TOML11_HAS_STD_INTEGER_SEQUENCE 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_INTEGER_SEQUENCE) - -using std::index_sequence; -using std::make_index_sequence; - -#else - -template struct index_sequence{}; - -template -struct double_index_sequence; - -template -struct double_index_sequence> -{ - using type = index_sequence; -}; -template -struct double_index_sequence> -{ - using type = index_sequence; -}; - -template -struct index_sequence_maker -{ - using type = typename double_index_sequence< - N % 2 == 1, N/2, typename index_sequence_maker::type - >::type; -}; -template<> -struct index_sequence_maker<0> -{ - using type = index_sequence<>; -}; - -template -using make_index_sequence = typename index_sequence_maker::type; - -#endif // TOML11_HAS_STD_INTEGER_SEQUENCE - -} // cxx -} // toml - -// --------------------------------------------------------------------------- -// C++14 enable_if_t - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE -# if defined(__cpp_lib_transformation_trait_aliases) -# if __cpp_lib_transformation_trait_aliases >= 201304L -# define TOML11_HAS_STD_ENABLE_IF_T 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_ENABLE_IF_T) - -using std::enable_if_t; - -#else - -template -using enable_if_t = typename std::enable_if::type; - -#endif // TOML11_HAS_STD_ENABLE_IF_T - -} // cxx -} // toml - -// --------------------------------------------------------------------------- -// return_type_of_t - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if defined(__cpp_lib_is_invocable) -# if __cpp_lib_is_invocable >= 201703 -# define TOML11_HAS_STD_INVOKE_RESULT 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_INVOKE_RESULT) - -template -using return_type_of_t = std::invoke_result_t; - -#else - -// result_of is deprecated after C++17 -template -using return_type_of_t = typename std::result_of::type; - -#endif // TOML11_HAS_STD_INVOKE_RESULT - -} // cxx -} // toml - -// --------------------------------------------------------------------------- -// C++17 void_t - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if defined(__cpp_lib_void_t) -# if __cpp_lib_void_t >= 201411L -# define TOML11_HAS_STD_VOID_T 1 -# endif -# endif -#endif - -namespace toml -{ -namespace cxx -{ -#if defined(TOML11_HAS_STD_VOID_T) - -using std::void_t; - -#else - -template -using void_t = void; - -#endif // TOML11_HAS_STD_VOID_T - -} // cxx -} // toml - -// ---------------------------------------------------------------------------- -// (subset of) source_location - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= 202002L -# if __has_include() -# define TOML11_HAS_STD_SOURCE_LOCATION -# endif // has_include -#endif // c++20 - -#if ! defined(TOML11_HAS_STD_SOURCE_LOCATION) -# if defined(__GNUC__) && ! defined(__clang__) -# if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX14_VALUE -# if __has_include() -# define TOML11_HAS_EXPERIMENTAL_SOURCE_LOCATION -# endif -# endif -# endif // GNU g++ -#endif // not TOML11_HAS_STD_SOURCE_LOCATION - -#if ! defined(TOML11_HAS_STD_SOURCE_LOCATION) && ! defined(TOML11_HAS_EXPERIMENTAL_SOURCE_LOCATION) -# if defined(__GNUC__) && ! defined(__clang__) -# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) -# define TOML11_HAS_BUILTIN_FILE_LINE 1 -# define TOML11_BUILTIN_LINE_TYPE int -# endif -# elif defined(__clang__) // clang 9.0.0 implements builtin_FILE/LINE -# if __has_builtin(__builtin_FILE) && __has_builtin(__builtin_LINE) -# define TOML11_HAS_BUILTIN_FILE_LINE 1 -# define TOML11_BUILTIN_LINE_TYPE unsigned int -# endif -# elif defined(_MSVC_LANG) && defined(_MSC_VER) -# if _MSC_VER > 1926 -# define TOML11_HAS_BUILTIN_FILE_LINE 1 -# define TOML11_BUILTIN_LINE_TYPE int -# endif -# endif -#endif - -#if defined(TOML11_HAS_STD_SOURCE_LOCATION) -#include -namespace toml -{ -namespace cxx -{ -using source_location = std::source_location; - -inline std::string to_string(const source_location& loc) -{ - const char* fname = loc.file_name(); - if(fname) - { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in file ") + std::string(fname); - } - else - { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in unknown file"); - } -} - -} // cxx -} // toml -#elif defined(TOML11_HAS_EXPERIMENTAL_SOURCE_LOCATION) -#include -namespace toml -{ -namespace cxx -{ -using source_location = std::experimental::source_location; - -inline std::string to_string(const source_location& loc) -{ - const char* fname = loc.file_name(); - if(fname) - { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in file ") + std::string(fname); - } - else - { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in unknown file"); - } -} - -} // cxx -} // toml -#elif defined(TOML11_HAS_BUILTIN_FILE_LINE) -namespace toml -{ -namespace cxx -{ -struct source_location -{ - using line_type = TOML11_BUILTIN_LINE_TYPE; - static source_location current(const line_type line = __builtin_LINE(), - const char* file = __builtin_FILE()) - { - return source_location(line, file); - } - - source_location(const line_type line, const char* file) - : line_(line), file_name_(file) - {} - - line_type line() const noexcept {return line_;} - const char* file_name() const noexcept {return file_name_;} - - private: - - line_type line_; - const char* file_name_; -}; - -inline std::string to_string(const source_location& loc) -{ - const char* fname = loc.file_name(); - if(fname) - { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in file ") + std::string(fname); - } - else - { - return std::string(" at line ") + std::to_string(loc.line()) + - std::string(" in unknown file"); - } -} - -} // cxx -} // toml -#else // no builtin -namespace toml -{ -namespace cxx -{ -struct source_location -{ - static source_location current() { return source_location{}; } -}; - -inline std::string to_string(const source_location&) -{ - return std::string(""); -} -} // cxx -} // toml -#endif // TOML11_HAS_STD_SOURCE_LOCATION - -// ---------------------------------------------------------------------------- -// (subset of) optional - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if __has_include() -# include -# endif // has_include(optional) -#endif // C++17 - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if defined(__cpp_lib_optional) -# if __cpp_lib_optional >= 201606L -# define TOML11_HAS_STD_OPTIONAL 1 -# endif -# endif -#endif - -#if defined(TOML11_HAS_STD_OPTIONAL) - -namespace toml -{ -namespace cxx -{ -using std::optional; - -inline std::nullopt_t make_nullopt() {return std::nullopt;} - -template -std::basic_ostream& -operator<<(std::basic_ostream& os, const std::nullopt_t&) -{ - os << "nullopt"; - return os; -} - -} // cxx -} // toml - -#else // TOML11_HAS_STD_OPTIONAL - -namespace toml -{ -namespace cxx -{ - -struct nullopt_t{}; -inline nullopt_t make_nullopt() {return nullopt_t{};} - -inline bool operator==(const nullopt_t&, const nullopt_t&) noexcept {return true;} -inline bool operator!=(const nullopt_t&, const nullopt_t&) noexcept {return false;} -inline bool operator< (const nullopt_t&, const nullopt_t&) noexcept {return false;} -inline bool operator<=(const nullopt_t&, const nullopt_t&) noexcept {return true;} -inline bool operator> (const nullopt_t&, const nullopt_t&) noexcept {return false;} -inline bool operator>=(const nullopt_t&, const nullopt_t&) noexcept {return true;} - -template -std::basic_ostream& -operator<<(std::basic_ostream& os, const nullopt_t&) -{ - os << "nullopt"; - return os; -} - -template -class optional -{ - public: - - using value_type = T; - - public: - - optional() noexcept : has_value_(false), null_('\0') {} - optional(nullopt_t) noexcept : has_value_(false), null_('\0') {} - - optional(const T& x): has_value_(true), value_(x) {} - optional(T&& x): has_value_(true), value_(std::move(x)) {} - - template::value, std::nullptr_t> = nullptr> - explicit optional(U&& x): has_value_(true), value_(std::forward(x)) {} - - optional(const optional& rhs): has_value_(rhs.has_value_) - { - if(rhs.has_value_) - { - this->assigner(rhs.value_); - } - } - optional(optional&& rhs): has_value_(rhs.has_value_) - { - if(this->has_value_) - { - this->assigner(std::move(rhs.value_)); - } - } - - optional& operator=(const optional& rhs) - { - if(this == std::addressof(rhs)) {return *this;} - - this->cleanup(); - this->has_value_ = rhs.has_value_; - if(this->has_value_) - { - this->assigner(rhs.value_); - } - return *this; - } - optional& operator=(optional&& rhs) - { - if(this == std::addressof(rhs)) {return *this;} - - this->cleanup(); - this->has_value_ = rhs.has_value_; - if(this->has_value_) - { - this->assigner(std::move(rhs.value_)); - } - return *this; - } - - template>, std::is_constructible - >::value, std::nullptr_t> = nullptr> - explicit optional(const optional& rhs): has_value_(rhs.has_value_), null_('\0') - { - if(rhs.has_value_) - { - this->assigner(rhs.value_); - } - } - template>, std::is_constructible - >::value, std::nullptr_t> = nullptr> - explicit optional(optional&& rhs): has_value_(rhs.has_value_), null_('\0') - { - if(this->has_value_) - { - this->assigner(std::move(rhs.value_)); - } - } - - template>, std::is_constructible - >::value, std::nullptr_t> = nullptr> - optional& operator=(const optional& rhs) - { - if(this == std::addressof(rhs)) {return *this;} - - this->cleanup(); - this->has_value_ = rhs.has_value_; - if(this->has_value_) - { - this->assigner(rhs.value_); - } - return *this; - } - - template>, std::is_constructible - >::value, std::nullptr_t> = nullptr> - optional& operator=(optional&& rhs) - { - if(this == std::addressof(rhs)) {return *this;} - - this->cleanup(); - this->has_value_ = rhs.has_value_; - if(this->has_value_) - { - this->assigner(std::move(rhs.value_)); - } - return *this; - } - ~optional() noexcept - { - this->cleanup(); - } - - explicit operator bool() const noexcept - { - return has_value_; - } - - bool has_value() const noexcept {return has_value_;} - - value_type const& value(source_location loc = source_location::current()) const - { - if( ! this->has_value_) - { - throw std::runtime_error("optional::value(): bad_unwrap" + to_string(loc)); - } - return this->value_; - } - value_type& value(source_location loc = source_location::current()) - { - if( ! this->has_value_) - { - throw std::runtime_error("optional::value(): bad_unwrap" + to_string(loc)); - } - return this->value_; - } - - value_type const& value_or(const value_type& opt) const - { - if(this->has_value_) {return this->value_;} else {return opt;} - } - value_type& value_or(value_type& opt) - { - if(this->has_value_) {return this->value_;} else {return opt;} - } - - private: - - void cleanup() noexcept - { - if(this->has_value_) - { - value_.~T(); - } - } - - template - void assigner(U&& x) - { - const auto tmp = ::new(std::addressof(this->value_)) value_type(std::forward(x)); - assert(tmp == std::addressof(this->value_)); - (void)tmp; - } - - private: - - bool has_value_; - union - { - char null_; - T value_; - }; -}; -} // cxx -} // toml -#endif // TOML11_HAS_STD_OPTIONAL - -#endif // TOML11_COMPAT_HPP diff --git a/include/toml11/context.hpp b/include/toml11/context.hpp deleted file mode 100644 index cda038f..0000000 --- a/include/toml11/context.hpp +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef TOML11_CONTEXT_HPP -#define TOML11_CONTEXT_HPP - -#include "error_info.hpp" -#include "spec.hpp" - -#include - -namespace toml -{ -namespace detail -{ - -template -class context -{ - public: - - explicit context(const spec& toml_spec) - : toml_spec_(toml_spec), errors_{} - {} - - bool has_error() const noexcept {return !errors_.empty();} - - std::vector const& errors() const noexcept {return errors_;} - - semantic_version& toml_version() noexcept {return toml_spec_.version;} - semantic_version const& toml_version() const noexcept {return toml_spec_.version;} - - spec& toml_spec() noexcept {return toml_spec_;} - spec const& toml_spec() const noexcept {return toml_spec_;} - - void report_error(error_info err) - { - this->errors_.push_back(std::move(err)); - } - - error_info pop_last_error() - { - assert( ! errors_.empty()); - auto e = std::move(errors_.back()); - errors_.pop_back(); - return e; - } - - private: - - spec toml_spec_; - std::vector errors_; -}; - -} // detail -} // toml - -#if defined(TOML11_COMPILE_SOURCES) -namespace toml -{ -struct type_config; -struct ordered_type_config; -namespace detail -{ -extern template class context<::toml::type_config>; -extern template class context<::toml::ordered_type_config>; -} // detail -} // toml -#endif // TOML11_COMPILE_SOURCES - -#endif // TOML11_CONTEXT_HPP diff --git a/include/toml11/conversion.hpp b/include/toml11/conversion.hpp deleted file mode 100644 index 819a765..0000000 --- a/include/toml11/conversion.hpp +++ /dev/null @@ -1,203 +0,0 @@ -#ifndef TOML11_CONVERSION_HPP -#define TOML11_CONVERSION_HPP - -#include "find.hpp" -#include "from.hpp" // IWYU pragma: keep -#include "into.hpp" // IWYU pragma: keep - -#if defined(TOML11_HAS_OPTIONAL) - -#include - -namespace toml -{ -namespace detail -{ - -template -inline constexpr bool is_optional_v = false; - -template -inline constexpr bool is_optional_v> = true; - -template -void find_member_variable_from_value(T& obj, const basic_value& v, const char* var_name) -{ - if constexpr(is_optional_v) - { - if(v.contains(var_name)) - { - obj = toml::find(v, var_name); - } - else - { - obj = std::nullopt; - } - } - else - { - obj = toml::find(v, var_name); - } -} - -template -void assign_member_variable_to_value(const T& obj, basic_value& v, const char* var_name) -{ - if constexpr(is_optional_v) - { - if(obj.has_value()) - { - v[var_name] = obj.value(); - } - } - else - { - v[var_name] = obj; - } -} - -} // detail -} // toml - -#else - -namespace toml -{ -namespace detail -{ - -template -void find_member_variable_from_value(T& obj, const basic_value& v, const char* var_name) -{ - obj = toml::find(v, var_name); -} - -template -void assign_member_variable_to_value(const T& obj, basic_value& v, const char* var_name) -{ - v[var_name] = obj; -} - -} // detail -} // toml - -#endif // optional - -// use it in the following way. -// ```cpp -// namespace foo -// { -// struct Foo -// { -// std::string s; -// double d; -// int i; -// }; -// } // foo -// -// TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(foo::Foo, s, d, i) -// ``` -// -// And then you can use `toml::get(v)` and `toml::find(file, "foo");` -// - -#define TOML11_STRINGIZE_AUX(x) #x -#define TOML11_STRINGIZE(x) TOML11_STRINGIZE_AUX(x) - -#define TOML11_CONCATENATE_AUX(x, y) x##y -#define TOML11_CONCATENATE(x, y) TOML11_CONCATENATE_AUX(x, y) - -// ============================================================================ -// TOML11_DEFINE_CONVERSION_NON_INTRUSIVE - -#ifndef TOML11_WITHOUT_DEFINE_NON_INTRUSIVE - -// ---------------------------------------------------------------------------- -// TOML11_ARGS_SIZE - -#define TOML11_INDEX_RSEQ() \ - 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, \ - 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 -#define TOML11_ARGS_SIZE_IMPL(\ - ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8, ARG9, ARG10, \ - ARG11, ARG12, ARG13, ARG14, ARG15, ARG16, ARG17, ARG18, ARG19, ARG20, \ - ARG21, ARG22, ARG23, ARG24, ARG25, ARG26, ARG27, ARG28, ARG29, ARG30, \ - ARG31, ARG32, N, ...) N -#define TOML11_ARGS_SIZE_AUX(...) TOML11_ARGS_SIZE_IMPL(__VA_ARGS__) -#define TOML11_ARGS_SIZE(...) TOML11_ARGS_SIZE_AUX(__VA_ARGS__, TOML11_INDEX_RSEQ()) - -// ---------------------------------------------------------------------------- -// TOML11_FOR_EACH_VA_ARGS - -#define TOML11_FOR_EACH_VA_ARGS_AUX_1( FUNCTOR, ARG1 ) FUNCTOR(ARG1) -#define TOML11_FOR_EACH_VA_ARGS_AUX_2( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_1( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_3( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_2( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_4( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_3( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_5( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_4( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_6( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_5( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_7( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_6( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_8( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_7( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_9( FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_8( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_10(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_9( FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_11(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_10(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_12(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_11(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_13(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_12(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_14(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_13(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_15(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_14(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_16(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_15(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_17(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_16(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_18(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_17(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_19(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_18(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_20(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_19(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_21(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_20(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_22(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_21(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_23(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_22(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_24(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_23(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_25(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_24(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_26(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_25(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_27(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_26(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_28(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_27(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_29(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_28(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_30(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_29(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_31(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_30(FUNCTOR, __VA_ARGS__) -#define TOML11_FOR_EACH_VA_ARGS_AUX_32(FUNCTOR, ARG1, ...) FUNCTOR(ARG1) TOML11_FOR_EACH_VA_ARGS_AUX_31(FUNCTOR, __VA_ARGS__) - -#define TOML11_FOR_EACH_VA_ARGS(FUNCTOR, ...)\ - TOML11_CONCATENATE(TOML11_FOR_EACH_VA_ARGS_AUX_, TOML11_ARGS_SIZE(__VA_ARGS__))(FUNCTOR, __VA_ARGS__) - - -#define TOML11_FIND_MEMBER_VARIABLE_FROM_VALUE(VAR_NAME)\ - toml::detail::find_member_variable_from_value(obj.VAR_NAME, v, TOML11_STRINGIZE(VAR_NAME)); - -#define TOML11_ASSIGN_MEMBER_VARIABLE_TO_VALUE(VAR_NAME)\ - toml::detail::assign_member_variable_to_value(obj.VAR_NAME, v, TOML11_STRINGIZE(VAR_NAME)); - -#define TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(NAME, ...)\ - namespace toml { \ - template<> \ - struct from \ - { \ - template \ - static NAME from_toml(const basic_value& v) \ - { \ - NAME obj; \ - TOML11_FOR_EACH_VA_ARGS(TOML11_FIND_MEMBER_VARIABLE_FROM_VALUE, __VA_ARGS__) \ - return obj; \ - } \ - }; \ - template<> \ - struct into \ - { \ - template \ - static basic_value into_toml(const NAME& obj) \ - { \ - ::toml::basic_value v = typename ::toml::basic_value::table_type{}; \ - TOML11_FOR_EACH_VA_ARGS(TOML11_ASSIGN_MEMBER_VARIABLE_TO_VALUE, __VA_ARGS__) \ - return v; \ - } \ - }; \ - } /* toml */ - -#endif// TOML11_WITHOUT_DEFINE_NON_INTRUSIVE - -#endif // TOML11_CONVERSION_HPP diff --git a/include/toml11/datetime.hpp b/include/toml11/datetime.hpp deleted file mode 100644 index 0841180..0000000 --- a/include/toml11/datetime.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_DATETIME_HPP -#define TOML11_DATETIME_HPP - -#include "fwd/datetime_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/datetime_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_DATETIME_HPP diff --git a/include/toml11/error_info.hpp b/include/toml11/error_info.hpp deleted file mode 100644 index 4575f7a..0000000 --- a/include/toml11/error_info.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_ERROR_INFO_HPP -#define TOML11_ERROR_INFO_HPP - -#include "fwd/error_info_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/error_info_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_ERROR_INFO_HPP diff --git a/include/toml11/exception.hpp b/include/toml11/exception.hpp deleted file mode 100644 index 7149eb3..0000000 --- a/include/toml11/exception.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TOML11_EXCEPTION_HPP -#define TOML11_EXCEPTION_HPP - -#include - -namespace toml -{ - -struct exception : public std::exception -{ - public: - virtual ~exception() noexcept override = default; - virtual const char* what() const noexcept override {return "";} -}; - -} // toml -#endif // TOMl11_EXCEPTION_HPP diff --git a/include/toml11/find.hpp b/include/toml11/find.hpp deleted file mode 100644 index 1d0658d..0000000 --- a/include/toml11/find.hpp +++ /dev/null @@ -1,548 +0,0 @@ -#ifndef TOML11_FIND_HPP -#define TOML11_FIND_HPP - -#include - -#include "get.hpp" -#include "value.hpp" - -#if defined(TOML11_HAS_STRING_VIEW) -#include -#endif - -namespace toml -{ - -// ---------------------------------------------------------------------------- -// find(value, key); - -template -decltype(::toml::get(std::declval const&>())) -find(const basic_value& v, const typename basic_value::key_type& ky) -{ - return ::toml::get(v.at(ky)); -} - -template -decltype(::toml::get(std::declval&>())) -find(basic_value& v, const typename basic_value::key_type& ky) -{ - return ::toml::get(v.at(ky)); -} - -template -decltype(::toml::get(std::declval&&>())) -find(basic_value&& v, const typename basic_value::key_type& ky) -{ - return ::toml::get(std::move(v.at(ky))); -} - -// ---------------------------------------------------------------------------- -// find(value, idx) - -template -decltype(::toml::get(std::declval const&>())) -find(const basic_value& v, const std::size_t idx) -{ - return ::toml::get(v.at(idx)); -} -template -decltype(::toml::get(std::declval&>())) -find(basic_value& v, const std::size_t idx) -{ - return ::toml::get(v.at(idx)); -} -template -decltype(::toml::get(std::declval&&>())) -find(basic_value&& v, const std::size_t idx) -{ - return ::toml::get(std::move(v.at(idx))); -} - -// ---------------------------------------------------------------------------- -// find(value, key/idx), w/o conversion - -template -cxx::enable_if_t::value, basic_value>& -find(basic_value& v, const typename basic_value::key_type& ky) -{ - return v.at(ky); -} -template -cxx::enable_if_t::value, basic_value> const& -find(basic_value const& v, const typename basic_value::key_type& ky) -{ - return v.at(ky); -} -template -cxx::enable_if_t::value, basic_value> -find(basic_value&& v, const typename basic_value::key_type& ky) -{ - return basic_value(std::move(v.at(ky))); -} - -template -cxx::enable_if_t::value, basic_value>& -find(basic_value& v, const std::size_t idx) -{ - return v.at(idx); -} -template -cxx::enable_if_t::value, basic_value> const& -find(basic_value const& v, const std::size_t idx) -{ - return v.at(idx); -} -template -cxx::enable_if_t::value, basic_value> -find(basic_value&& v, const std::size_t idx) -{ - return basic_value(std::move(v.at(idx))); -} - -// -------------------------------------------------------------------------- -// find> - -#if defined(TOML11_HAS_OPTIONAL) -template -cxx::enable_if_t::value, T> -find(const basic_value& v, const typename basic_value::key_type& ky) -{ - if(v.contains(ky)) - { - return ::toml::get(v.at(ky)); - } - else - { - return std::nullopt; - } -} - -template -cxx::enable_if_t::value, T> -find(basic_value& v, const typename basic_value::key_type& ky) -{ - if(v.contains(ky)) - { - return ::toml::get(v.at(ky)); - } - else - { - return std::nullopt; - } -} - -template -cxx::enable_if_t::value, T> -find(basic_value&& v, const typename basic_value::key_type& ky) -{ - if(v.contains(ky)) - { - return ::toml::get(std::move(v.at(ky))); - } - else - { - return std::nullopt; - } -} - -template -cxx::enable_if_t::value && std::is_integral::value, T> -find(const basic_value& v, const K& k) -{ - if(static_cast(k) < v.size()) - { - return ::toml::get(v.at(static_cast(k))); - } - else - { - return std::nullopt; - } -} - -template -cxx::enable_if_t::value && std::is_integral::value, T> -find(basic_value& v, const K& k) -{ - if(static_cast(k) < v.size()) - { - return ::toml::get(v.at(static_cast(k))); - } - else - { - return std::nullopt; - } -} - -template -cxx::enable_if_t::value && std::is_integral::value, T> -find(basic_value&& v, const K& k) -{ - if(static_cast(k) < v.size()) - { - return ::toml::get(std::move(v.at(static_cast(k)))); - } - else - { - return std::nullopt; - } -} -#endif // optional - -// -------------------------------------------------------------------------- -// toml::find(toml::value, toml::key, Ts&& ... keys) - -namespace detail -{ - -// It suppresses warnings by -Wsign-conversion when we pass integer literal -// to toml::find. integer literal `0` is deduced as an int, and will be -// converted to std::size_t. This causes sign-conversion. - -template -std::size_t key_cast(const std::size_t& v) noexcept -{ - return v; -} -template -cxx::enable_if_t>::value, std::size_t> -key_cast(const T& v) noexcept -{ - return static_cast(v); -} - -// for string-like (string, string literal, string_view) - -template -typename basic_value::key_type const& -key_cast(const typename basic_value::key_type& v) noexcept -{ - return v; -} -template -typename basic_value::key_type -key_cast(const typename basic_value::key_type::value_type* v) -{ - return typename basic_value::key_type(v); -} -#if defined(TOML11_HAS_STRING_VIEW) -template -typename basic_value::key_type -key_cast(const std::string_view v) -{ - return typename basic_value::key_type(v); -} -#endif // string_view - -} // detail - -// ---------------------------------------------------------------------------- -// find(v, keys...) - -template -cxx::enable_if_t::value, basic_value> const& -find(const basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); -} -template -cxx::enable_if_t::value, basic_value>& -find(basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); -} -template -cxx::enable_if_t::value, basic_value> -find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - return find(std::move(v.at(detail::key_cast(k1))), detail::key_cast(k2), ks...); -} - -// ---------------------------------------------------------------------------- -// find(v, keys...) - -template -decltype(::toml::get(std::declval&>())) -find(const basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); -} -template -decltype(::toml::get(std::declval&>())) -find(basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - return find(v.at(detail::key_cast(k1)), detail::key_cast(k2), ks...); -} -template -decltype(::toml::get(std::declval&&>())) -find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - return find(std::move(v.at(detail::key_cast(k1))), detail::key_cast(k2), ks...); -} - -#if defined(TOML11_HAS_OPTIONAL) -template -cxx::enable_if_t::value, T> -find(const basic_value& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) -{ - if(v.contains(k1)) - { - return find(v.at(k1), detail::key_cast(k2), ks...); - } - else - { - return std::nullopt; - } -} -template -cxx::enable_if_t::value, T> -find(basic_value& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) -{ - if(v.contains(k1)) - { - return find(v.at(k1), detail::key_cast(k2), ks...); - } - else - { - return std::nullopt; - } -} -template -cxx::enable_if_t::value, T> -find(basic_value&& v, const typename basic_value::key_type& k1, const K2& k2, const Ks& ... ks) -{ - if(v.contains(k1)) - { - return find(v.at(k1), detail::key_cast(k2), ks...); - } - else - { - return std::nullopt; - } -} - -template -cxx::enable_if_t::value && std::is_integral::value, T> -find(const basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - if(static_cast(k1) < v.size()) - { - return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); - } - else - { - return std::nullopt; - } -} -template -cxx::enable_if_t::value && std::is_integral::value, T> -find(basic_value& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - if(static_cast(k1) < v.size()) - { - return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); - } - else - { - return std::nullopt; - } -} -template -cxx::enable_if_t::value && std::is_integral::value, T> -find(basic_value&& v, const K1& k1, const K2& k2, const Ks& ... ks) -{ - if(static_cast(k1) < v.size()) - { - return find(v.at(static_cast(k1)), detail::key_cast(k2), ks...); - } - else - { - return std::nullopt; - } -} -#endif // optional - -// =========================================================================== -// find_or(value, key, fallback) - -// --------------------------------------------------------------------------- -// find_or(v, key, other_v) - -template -cxx::enable_if_t::value, basic_value>& -find_or(basic_value& v, const K& k, basic_value& opt) noexcept -{ - try - { - return ::toml::find(v, detail::key_cast(k)); - } - catch(...) - { - return opt; - } -} -template -cxx::enable_if_t::value, basic_value> const& -find_or(const basic_value& v, const K& k, const basic_value& opt) noexcept -{ - try - { - return ::toml::find(v, detail::key_cast(k)); - } - catch(...) - { - return opt; - } -} -template -cxx::enable_if_t::value, basic_value> -find_or(basic_value&& v, const K& k, basic_value&& opt) noexcept -{ - try - { - return ::toml::find(v, detail::key_cast(k)); - } - catch(...) - { - return opt; - } -} - -// --------------------------------------------------------------------------- -// toml types (return type can be a reference) - -template -cxx::enable_if_t>::value, - cxx::remove_cvref_t const&> -find_or(const basic_value& v, const K& k, const T& opt) -{ - try - { - return ::toml::get(v.at(detail::key_cast(k))); - } - catch(...) - { - return opt; - } -} - -template -cxx::enable_if_t>, - detail::is_exact_toml_type> - >::value, cxx::remove_cvref_t&> -find_or(basic_value& v, const K& k, T& opt) -{ - try - { - return ::toml::get(v.at(detail::key_cast(k))); - } - catch(...) - { - return opt; - } -} - -template -cxx::enable_if_t>::value, - cxx::remove_cvref_t> -find_or(basic_value&& v, const K& k, T opt) -{ - try - { - return ::toml::get(std::move(v.at(detail::key_cast(k)))); - } - catch(...) - { - return T(std::move(opt)); - } -} - -// --------------------------------------------------------------------------- -// string literal (deduced as std::string) - -// XXX to avoid confusion when T is explicitly specified in find_or(), -// we restrict the string type as std::string. -template -cxx::enable_if_t::value, std::string> -find_or(const basic_value& v, const K& k, const char* opt) -{ - try - { - return ::toml::get(v.at(detail::key_cast(k))); - } - catch(...) - { - return std::string(opt); - } -} - -// --------------------------------------------------------------------------- -// other types (requires type conversion and return type cannot be a reference) - -template -cxx::enable_if_t>>, - detail::is_not_toml_type, basic_value>, - cxx::negation, - const typename basic_value::string_type::value_type*>> - >::value, cxx::remove_cvref_t> -find_or(const basic_value& v, const K& ky, T opt) -{ - try - { - return ::toml::get>(v.at(detail::key_cast(ky))); - } - catch(...) - { - return cxx::remove_cvref_t(std::move(opt)); - } -} - -// ---------------------------------------------------------------------------- -// recursive - -namespace detail -{ - -template -auto last_one(Ts&&... args) - -> decltype(std::get(std::forward_as_tuple(std::forward(args)...))) -{ - return std::get(std::forward_as_tuple(std::forward(args)...)); -} - -} // detail - -template -auto find_or(Value&& v, const K1& k1, const K2& k2, K3&& k3, Ks&& ... keys) noexcept - -> cxx::enable_if_t< - detail::is_basic_value>::value, - decltype(find_or(v, k2, std::forward(k3), std::forward(keys)...)) - > -{ - try - { - return find_or(v.at(k1), k2, std::forward(k3), std::forward(keys)...); - } - catch(...) - { - return detail::last_one(k3, keys...); - } -} - -template -T find_or(const basic_value& v, const K1& k1, const K2& k2, const K3& k3, const Ks& ... keys) noexcept -{ - try - { - return find_or(v.at(k1), k2, k3, keys...); - } - catch(...) - { - return static_cast(detail::last_one(k3, keys...)); - } -} - -} // toml -#endif // TOML11_FIND_HPP diff --git a/include/toml11/format.hpp b/include/toml11/format.hpp deleted file mode 100644 index 6662220..0000000 --- a/include/toml11/format.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_FORMAT_HPP -#define TOML11_FORMAT_HPP - -#include "fwd/format_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/format_impl.hpp" // IWYU pragma: export -#endif - -#endif// TOML11_FORMAT_HPP diff --git a/include/toml11/from.hpp b/include/toml11/from.hpp deleted file mode 100644 index d2e0e13..0000000 --- a/include/toml11/from.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TOML11_FROM_HPP -#define TOML11_FROM_HPP - -namespace toml -{ - -template -struct from; -// { -// static T from_toml(const toml::value& v) -// { -// // User-defined conversions ... -// } -// }; - -} // toml -#endif // TOML11_FROM_HPP diff --git a/include/toml11/fwd/color_fwd.hpp b/include/toml11/fwd/color_fwd.hpp deleted file mode 100644 index ed711c0..0000000 --- a/include/toml11/fwd/color_fwd.hpp +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef TOML11_COLOR_FWD_HPP -#define TOML11_COLOR_FWD_HPP - -#include - -#ifdef TOML11_COLORIZE_ERROR_MESSAGE -#define TOML11_ERROR_MESSAGE_COLORIZED true -#else -#define TOML11_ERROR_MESSAGE_COLORIZED false -#endif - -#ifdef TOML11_USE_THREAD_LOCAL_COLORIZATION -#define TOML11_THREAD_LOCAL_COLORIZATION thread_local -#else -#define TOML11_THREAD_LOCAL_COLORIZATION -#endif - -namespace toml -{ -namespace color -{ -// put ANSI escape sequence to ostream -inline namespace ansi -{ -namespace detail -{ - -// Control color mode globally -class color_mode -{ - public: - - void enable() noexcept - { - should_color_ = true; - } - void disable() noexcept - { - should_color_ = false; - } - bool should_color() const noexcept - { - return should_color_; - } - - private: - - bool should_color_ = TOML11_ERROR_MESSAGE_COLORIZED; -}; - -inline color_mode& color_status() noexcept -{ - static TOML11_THREAD_LOCAL_COLORIZATION color_mode status; - return status; -} - -} // detail - -std::ostream& reset (std::ostream& os); -std::ostream& bold (std::ostream& os); -std::ostream& grey (std::ostream& os); -std::ostream& gray (std::ostream& os); -std::ostream& red (std::ostream& os); -std::ostream& green (std::ostream& os); -std::ostream& yellow (std::ostream& os); -std::ostream& blue (std::ostream& os); -std::ostream& magenta(std::ostream& os); -std::ostream& cyan (std::ostream& os); -std::ostream& white (std::ostream& os); - -} // ansi - -inline void enable() -{ - return detail::color_status().enable(); -} -inline void disable() -{ - return detail::color_status().disable(); -} -inline bool should_color() -{ - return detail::color_status().should_color(); -} - -} // color -} // toml -#endif // TOML11_COLOR_FWD_HPP diff --git a/include/toml11/fwd/comments_fwd.hpp b/include/toml11/fwd/comments_fwd.hpp deleted file mode 100644 index bbc9926..0000000 --- a/include/toml11/fwd/comments_fwd.hpp +++ /dev/null @@ -1,451 +0,0 @@ -#ifndef TOML11_COMMENTS_FWD_HPP -#define TOML11_COMMENTS_FWD_HPP - -// to use __has_builtin -#include "../version.hpp" // IWYU pragma: keep - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// This file provides mainly two classes, `preserve_comments` and `discard_comments`. -// Those two are a container that have the same interface as `std::vector` -// but bahaves in the opposite way. `preserve_comments` is just the same as -// `std::vector` and each `std::string` corresponds to a comment line. -// Conversely, `discard_comments` discards all the strings and ignores everything -// assigned in it. `discard_comments` is always empty and you will encounter an -// error whenever you access to the element. -namespace toml -{ -class discard_comments; // forward decl - -class preserve_comments -{ - public: - // `container_type` is not provided in discard_comments. - // do not use this inner-type in a generic code. - using container_type = std::vector; - - using size_type = container_type::size_type; - using difference_type = container_type::difference_type; - using value_type = container_type::value_type; - using reference = container_type::reference; - using const_reference = container_type::const_reference; - using pointer = container_type::pointer; - using const_pointer = container_type::const_pointer; - using iterator = container_type::iterator; - using const_iterator = container_type::const_iterator; - using reverse_iterator = container_type::reverse_iterator; - using const_reverse_iterator = container_type::const_reverse_iterator; - - public: - - preserve_comments() = default; - ~preserve_comments() = default; - preserve_comments(preserve_comments const&) = default; - preserve_comments(preserve_comments &&) = default; - preserve_comments& operator=(preserve_comments const&) = default; - preserve_comments& operator=(preserve_comments &&) = default; - - explicit preserve_comments(const std::vector& c): comments(c){} - explicit preserve_comments(std::vector&& c) - : comments(std::move(c)) - {} - preserve_comments& operator=(const std::vector& c) - { - comments = c; - return *this; - } - preserve_comments& operator=(std::vector&& c) - { - comments = std::move(c); - return *this; - } - - explicit preserve_comments(const discard_comments&) {} - - explicit preserve_comments(size_type n): comments(n) {} - preserve_comments(size_type n, const std::string& x): comments(n, x) {} - preserve_comments(std::initializer_list x): comments(x) {} - template - preserve_comments(InputIterator first, InputIterator last) - : comments(first, last) - {} - - template - void assign(InputIterator first, InputIterator last) {comments.assign(first, last);} - void assign(std::initializer_list ini) {comments.assign(ini);} - void assign(size_type n, const std::string& val) {comments.assign(n, val);} - - // Related to the issue #97. - // - // `std::vector::insert` and `std::vector::erase` in the STL implementation - // included in GCC 4.8.5 takes `std::vector::iterator` instead of - // `std::vector::const_iterator`. It causes compilation error in GCC 4.8.5. -#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__) && !defined(__clang__) -# if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) <= 40805 -# define TOML11_WORKAROUND_GCC_4_8_X_STANDARD_LIBRARY_IMPLEMENTATION -# endif -#endif - -#ifdef TOML11_WORKAROUND_GCC_4_8_X_STANDARD_LIBRARY_IMPLEMENTATION - iterator insert(iterator p, const std::string& x) - { - return comments.insert(p, x); - } - iterator insert(iterator p, std::string&& x) - { - return comments.insert(p, std::move(x)); - } - void insert(iterator p, size_type n, const std::string& x) - { - return comments.insert(p, n, x); - } - template - void insert(iterator p, InputIterator first, InputIterator last) - { - return comments.insert(p, first, last); - } - void insert(iterator p, std::initializer_list ini) - { - return comments.insert(p, ini); - } - - template - iterator emplace(iterator p, Ts&& ... args) - { - return comments.emplace(p, std::forward(args)...); - } - - iterator erase(iterator pos) {return comments.erase(pos);} - iterator erase(iterator first, iterator last) - { - return comments.erase(first, last); - } -#else - iterator insert(const_iterator p, const std::string& x) - { - return comments.insert(p, x); - } - iterator insert(const_iterator p, std::string&& x) - { - return comments.insert(p, std::move(x)); - } - iterator insert(const_iterator p, size_type n, const std::string& x) - { - return comments.insert(p, n, x); - } - template - iterator insert(const_iterator p, InputIterator first, InputIterator last) - { - return comments.insert(p, first, last); - } - iterator insert(const_iterator p, std::initializer_list ini) - { - return comments.insert(p, ini); - } - - template - iterator emplace(const_iterator p, Ts&& ... args) - { - return comments.emplace(p, std::forward(args)...); - } - - iterator erase(const_iterator pos) {return comments.erase(pos);} - iterator erase(const_iterator first, const_iterator last) - { - return comments.erase(first, last); - } -#endif - - void swap(preserve_comments& other) {comments.swap(other.comments);} - - void push_back(const std::string& v) {comments.push_back(v);} - void push_back(std::string&& v) {comments.push_back(std::move(v));} - void pop_back() {comments.pop_back();} - - template - void emplace_back(Ts&& ... args) {comments.emplace_back(std::forward(args)...);} - - void clear() {comments.clear();} - - size_type size() const noexcept {return comments.size();} - size_type max_size() const noexcept {return comments.max_size();} - size_type capacity() const noexcept {return comments.capacity();} - bool empty() const noexcept {return comments.empty();} - - void reserve(size_type n) {comments.reserve(n);} - void resize(size_type n) {comments.resize(n);} - void resize(size_type n, const std::string& c) {comments.resize(n, c);} - void shrink_to_fit() {comments.shrink_to_fit();} - - reference operator[](const size_type n) noexcept {return comments[n];} - const_reference operator[](const size_type n) const noexcept {return comments[n];} - reference at(const size_type n) {return comments.at(n);} - const_reference at(const size_type n) const {return comments.at(n);} - reference front() noexcept {return comments.front();} - const_reference front() const noexcept {return comments.front();} - reference back() noexcept {return comments.back();} - const_reference back() const noexcept {return comments.back();} - - pointer data() noexcept {return comments.data();} - const_pointer data() const noexcept {return comments.data();} - - iterator begin() noexcept {return comments.begin();} - iterator end() noexcept {return comments.end();} - const_iterator begin() const noexcept {return comments.begin();} - const_iterator end() const noexcept {return comments.end();} - const_iterator cbegin() const noexcept {return comments.cbegin();} - const_iterator cend() const noexcept {return comments.cend();} - - reverse_iterator rbegin() noexcept {return comments.rbegin();} - reverse_iterator rend() noexcept {return comments.rend();} - const_reverse_iterator rbegin() const noexcept {return comments.rbegin();} - const_reverse_iterator rend() const noexcept {return comments.rend();} - const_reverse_iterator crbegin() const noexcept {return comments.crbegin();} - const_reverse_iterator crend() const noexcept {return comments.crend();} - - friend bool operator==(const preserve_comments&, const preserve_comments&); - friend bool operator!=(const preserve_comments&, const preserve_comments&); - friend bool operator< (const preserve_comments&, const preserve_comments&); - friend bool operator<=(const preserve_comments&, const preserve_comments&); - friend bool operator> (const preserve_comments&, const preserve_comments&); - friend bool operator>=(const preserve_comments&, const preserve_comments&); - - friend void swap(preserve_comments&, std::vector&); - friend void swap(std::vector&, preserve_comments&); - - private: - - container_type comments; -}; - -bool operator==(const preserve_comments& lhs, const preserve_comments& rhs); -bool operator!=(const preserve_comments& lhs, const preserve_comments& rhs); -bool operator< (const preserve_comments& lhs, const preserve_comments& rhs); -bool operator<=(const preserve_comments& lhs, const preserve_comments& rhs); -bool operator> (const preserve_comments& lhs, const preserve_comments& rhs); -bool operator>=(const preserve_comments& lhs, const preserve_comments& rhs); - -void swap(preserve_comments& lhs, preserve_comments& rhs); -void swap(preserve_comments& lhs, std::vector& rhs); -void swap(std::vector& lhs, preserve_comments& rhs); - -std::ostream& operator<<(std::ostream& os, const preserve_comments& com); - -namespace detail -{ - -// To provide the same interface with `preserve_comments`, `discard_comments` -// should have an iterator. But it does not contain anything, so we need to -// add an iterator that points nothing. -// -// It always points null, so DO NOT unwrap this iterator. It always crashes -// your program. -template -struct empty_iterator -{ - using value_type = T; - using reference_type = typename std::conditional::type; - using pointer_type = typename std::conditional::type; - using difference_type = std::ptrdiff_t; - using iterator_category = std::random_access_iterator_tag; - - empty_iterator() = default; - ~empty_iterator() = default; - empty_iterator(empty_iterator const&) = default; - empty_iterator(empty_iterator &&) = default; - empty_iterator& operator=(empty_iterator const&) = default; - empty_iterator& operator=(empty_iterator &&) = default; - - // DO NOT call these operators. - reference_type operator*() const noexcept {std::terminate();} - pointer_type operator->() const noexcept {return nullptr;} - reference_type operator[](difference_type) const noexcept {return this->operator*();} - - // These operators do nothing. - empty_iterator& operator++() noexcept {return *this;} - empty_iterator operator++(int) noexcept {return *this;} - empty_iterator& operator--() noexcept {return *this;} - empty_iterator operator--(int) noexcept {return *this;} - - empty_iterator& operator+=(difference_type) noexcept {return *this;} - empty_iterator& operator-=(difference_type) noexcept {return *this;} - - empty_iterator operator+(difference_type) const noexcept {return *this;} - empty_iterator operator-(difference_type) const noexcept {return *this;} -}; - -template -bool operator==(const empty_iterator&, const empty_iterator&) noexcept {return true;} -template -bool operator!=(const empty_iterator&, const empty_iterator&) noexcept {return false;} -template -bool operator< (const empty_iterator&, const empty_iterator&) noexcept {return false;} -template -bool operator<=(const empty_iterator&, const empty_iterator&) noexcept {return true;} -template -bool operator> (const empty_iterator&, const empty_iterator&) noexcept {return false;} -template -bool operator>=(const empty_iterator&, const empty_iterator&) noexcept {return true;} - -template -typename empty_iterator::difference_type -operator-(const empty_iterator&, const empty_iterator&) noexcept {return 0;} - -template -empty_iterator -operator+(typename empty_iterator::difference_type, const empty_iterator& rhs) noexcept {return rhs;} -template -empty_iterator -operator+(const empty_iterator& lhs, typename empty_iterator::difference_type) noexcept {return lhs;} - -} // detail - -// The default comment type. It discards all the comments. It requires only one -// byte to contain, so the memory footprint is smaller than preserve_comments. -// -// It just ignores `push_back`, `insert`, `erase`, and any other modifications. -// IT always returns size() == 0, the iterator taken by `begin()` is always the -// same as that of `end()`, and accessing through `operator[]` or iterators -// always causes a segmentation fault. DO NOT access to the element of this. -// -// Why this is chose as the default type is because the last version (2.x.y) -// does not contain any comments in a value. To minimize the impact on the -// efficiency, this is chosen as a default. -// -// To reduce the memory footprint, later we can try empty base optimization (EBO). -class discard_comments -{ - public: - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; - using value_type = std::string; - using reference = std::string&; - using const_reference = std::string const&; - using pointer = std::string*; - using const_pointer = std::string const*; - using iterator = detail::empty_iterator; - using const_iterator = detail::empty_iterator; - using reverse_iterator = detail::empty_iterator; - using const_reverse_iterator = detail::empty_iterator; - - public: - discard_comments() = default; - ~discard_comments() = default; - discard_comments(discard_comments const&) = default; - discard_comments(discard_comments &&) = default; - discard_comments& operator=(discard_comments const&) = default; - discard_comments& operator=(discard_comments &&) = default; - - explicit discard_comments(const std::vector&) noexcept {} - explicit discard_comments(std::vector&&) noexcept {} - discard_comments& operator=(const std::vector&) noexcept {return *this;} - discard_comments& operator=(std::vector&&) noexcept {return *this;} - - explicit discard_comments(const preserve_comments&) noexcept {} - - explicit discard_comments(size_type) noexcept {} - discard_comments(size_type, const std::string&) noexcept {} - discard_comments(std::initializer_list) noexcept {} - template - discard_comments(InputIterator, InputIterator) noexcept {} - - template - void assign(InputIterator, InputIterator) noexcept {} - void assign(std::initializer_list) noexcept {} - void assign(size_type, const std::string&) noexcept {} - - iterator insert(const_iterator, const std::string&) {return iterator{};} - iterator insert(const_iterator, std::string&&) {return iterator{};} - iterator insert(const_iterator, size_type, const std::string&) {return iterator{};} - template - iterator insert(const_iterator, InputIterator, InputIterator) {return iterator{};} - iterator insert(const_iterator, std::initializer_list) {return iterator{};} - - template - iterator emplace(const_iterator, Ts&& ...) {return iterator{};} - iterator erase(const_iterator) {return iterator{};} - iterator erase(const_iterator, const_iterator) {return iterator{};} - - void swap(discard_comments&) {return;} - - void push_back(const std::string&) {return;} - void push_back(std::string&& ) {return;} - void pop_back() {return;} - - template - void emplace_back(Ts&& ...) {return;} - - void clear() {return;} - - size_type size() const noexcept {return 0;} - size_type max_size() const noexcept {return 0;} - size_type capacity() const noexcept {return 0;} - bool empty() const noexcept {return true;} - - void reserve(size_type) {return;} - void resize(size_type) {return;} - void resize(size_type, const std::string&) {return;} - void shrink_to_fit() {return;} - - // DO NOT access to the element of this container. This container is always - // empty, so accessing through operator[], front/back, data causes address - // error. - - reference operator[](const size_type) noexcept {never_call("toml::discard_comment::operator[]");} - const_reference operator[](const size_type) const noexcept {never_call("toml::discard_comment::operator[]");} - reference at(const size_type) {throw std::out_of_range("toml::discard_comment is always empty.");} - const_reference at(const size_type) const {throw std::out_of_range("toml::discard_comment is always empty.");} - reference front() noexcept {never_call("toml::discard_comment::front");} - const_reference front() const noexcept {never_call("toml::discard_comment::front");} - reference back() noexcept {never_call("toml::discard_comment::back");} - const_reference back() const noexcept {never_call("toml::discard_comment::back");} - - pointer data() noexcept {return nullptr;} - const_pointer data() const noexcept {return nullptr;} - - iterator begin() noexcept {return iterator{};} - iterator end() noexcept {return iterator{};} - const_iterator begin() const noexcept {return const_iterator{};} - const_iterator end() const noexcept {return const_iterator{};} - const_iterator cbegin() const noexcept {return const_iterator{};} - const_iterator cend() const noexcept {return const_iterator{};} - - reverse_iterator rbegin() noexcept {return iterator{};} - reverse_iterator rend() noexcept {return iterator{};} - const_reverse_iterator rbegin() const noexcept {return const_iterator{};} - const_reverse_iterator rend() const noexcept {return const_iterator{};} - const_reverse_iterator crbegin() const noexcept {return const_iterator{};} - const_reverse_iterator crend() const noexcept {return const_iterator{};} - - private: - - [[noreturn]] static void never_call(const char *const this_function) - { -#if __has_builtin(__builtin_unreachable) - __builtin_unreachable(); -#endif - throw std::logic_error{this_function}; - } -}; - -inline bool operator==(const discard_comments&, const discard_comments&) noexcept {return true;} -inline bool operator!=(const discard_comments&, const discard_comments&) noexcept {return false;} -inline bool operator< (const discard_comments&, const discard_comments&) noexcept {return false;} -inline bool operator<=(const discard_comments&, const discard_comments&) noexcept {return true;} -inline bool operator> (const discard_comments&, const discard_comments&) noexcept {return false;} -inline bool operator>=(const discard_comments&, const discard_comments&) noexcept {return true;} - -inline void swap(const discard_comments&, const discard_comments&) noexcept {return;} - -inline std::ostream& operator<<(std::ostream& os, const discard_comments&) {return os;} - -} // toml11 -#endif // TOML11_COMMENTS_FWD_HPP diff --git a/include/toml11/fwd/datetime_fwd.hpp b/include/toml11/fwd/datetime_fwd.hpp deleted file mode 100644 index 44616a1..0000000 --- a/include/toml11/fwd/datetime_fwd.hpp +++ /dev/null @@ -1,261 +0,0 @@ -#ifndef TOML11_DATETIME_FWD_HPP -#define TOML11_DATETIME_FWD_HPP - -#include -#include -#include - -#include -#include -#include - -namespace toml -{ - -enum class month_t : std::uint8_t -{ - Jan = 0, - Feb = 1, - Mar = 2, - Apr = 3, - May = 4, - Jun = 5, - Jul = 6, - Aug = 7, - Sep = 8, - Oct = 9, - Nov = 10, - Dec = 11 -}; - -// ---------------------------------------------------------------------------- - -struct local_date -{ - std::int16_t year{0}; // A.D. (like, 2018) - std::uint8_t month{0}; // [0, 11] - std::uint8_t day{0}; // [1, 31] - - local_date(int y, month_t m, int d) - : year {static_cast(y)}, - month{static_cast(m)}, - day {static_cast(d)} - {} - - explicit local_date(const std::tm& t) - : year {static_cast(t.tm_year + 1900)}, - month{static_cast(t.tm_mon)}, - day {static_cast(t.tm_mday)} - {} - - explicit local_date(const std::chrono::system_clock::time_point& tp); - explicit local_date(const std::time_t t); - - operator std::chrono::system_clock::time_point() const; - operator std::time_t() const; - - local_date() = default; - ~local_date() = default; - local_date(local_date const&) = default; - local_date(local_date&&) = default; - local_date& operator=(local_date const&) = default; - local_date& operator=(local_date&&) = default; -}; -bool operator==(const local_date& lhs, const local_date& rhs); -bool operator!=(const local_date& lhs, const local_date& rhs); -bool operator< (const local_date& lhs, const local_date& rhs); -bool operator<=(const local_date& lhs, const local_date& rhs); -bool operator> (const local_date& lhs, const local_date& rhs); -bool operator>=(const local_date& lhs, const local_date& rhs); - -std::ostream& operator<<(std::ostream& os, const local_date& date); -std::string to_string(const local_date& date); - -// ----------------------------------------------------------------------------- - -struct local_time -{ - std::uint8_t hour{0}; // [0, 23] - std::uint8_t minute{0}; // [0, 59] - std::uint8_t second{0}; // [0, 60] - std::uint16_t millisecond{0}; // [0, 999] - std::uint16_t microsecond{0}; // [0, 999] - std::uint16_t nanosecond{0}; // [0, 999] - - local_time(int h, int m, int s, - int ms = 0, int us = 0, int ns = 0) - : hour {static_cast(h)}, - minute{static_cast(m)}, - second{static_cast(s)}, - millisecond{static_cast(ms)}, - microsecond{static_cast(us)}, - nanosecond {static_cast(ns)} - {} - - explicit local_time(const std::tm& t) - : hour {static_cast(t.tm_hour)}, - minute{static_cast(t.tm_min )}, - second{static_cast(t.tm_sec )}, - millisecond{0}, microsecond{0}, nanosecond{0} - {} - - template - explicit local_time(const std::chrono::duration& t) - { - const auto h = std::chrono::duration_cast(t); - this->hour = static_cast(h.count()); - const auto t2 = t - h; - const auto m = std::chrono::duration_cast(t2); - this->minute = static_cast(m.count()); - const auto t3 = t2 - m; - const auto s = std::chrono::duration_cast(t3); - this->second = static_cast(s.count()); - const auto t4 = t3 - s; - const auto ms = std::chrono::duration_cast(t4); - this->millisecond = static_cast(ms.count()); - const auto t5 = t4 - ms; - const auto us = std::chrono::duration_cast(t5); - this->microsecond = static_cast(us.count()); - const auto t6 = t5 - us; - const auto ns = std::chrono::duration_cast(t6); - this->nanosecond = static_cast(ns.count()); - } - - operator std::chrono::nanoseconds() const; - - local_time() = default; - ~local_time() = default; - local_time(local_time const&) = default; - local_time(local_time&&) = default; - local_time& operator=(local_time const&) = default; - local_time& operator=(local_time&&) = default; -}; - -bool operator==(const local_time& lhs, const local_time& rhs); -bool operator!=(const local_time& lhs, const local_time& rhs); -bool operator< (const local_time& lhs, const local_time& rhs); -bool operator<=(const local_time& lhs, const local_time& rhs); -bool operator> (const local_time& lhs, const local_time& rhs); -bool operator>=(const local_time& lhs, const local_time& rhs); - -std::ostream& operator<<(std::ostream& os, const local_time& time); -std::string to_string(const local_time& time); - -// ---------------------------------------------------------------------------- - -struct time_offset -{ - std::int8_t hour{0}; // [-12, 12] - std::int8_t minute{0}; // [-59, 59] - - time_offset(int h, int m) - : hour {static_cast(h)}, - minute{static_cast(m)} - {} - - operator std::chrono::minutes() const; - - time_offset() = default; - ~time_offset() = default; - time_offset(time_offset const&) = default; - time_offset(time_offset&&) = default; - time_offset& operator=(time_offset const&) = default; - time_offset& operator=(time_offset&&) = default; -}; - -bool operator==(const time_offset& lhs, const time_offset& rhs); -bool operator!=(const time_offset& lhs, const time_offset& rhs); -bool operator< (const time_offset& lhs, const time_offset& rhs); -bool operator<=(const time_offset& lhs, const time_offset& rhs); -bool operator> (const time_offset& lhs, const time_offset& rhs); -bool operator>=(const time_offset& lhs, const time_offset& rhs); - -std::ostream& operator<<(std::ostream& os, const time_offset& offset); - -std::string to_string(const time_offset& offset); - -// ----------------------------------------------------------------------------- - -struct local_datetime -{ - local_date date{}; - local_time time{}; - - local_datetime(local_date d, local_time t): date{d}, time{t} {} - - explicit local_datetime(const std::tm& t): date{t}, time{t}{} - - explicit local_datetime(const std::chrono::system_clock::time_point& tp); - explicit local_datetime(const std::time_t t); - - operator std::chrono::system_clock::time_point() const; - operator std::time_t() const; - - local_datetime() = default; - ~local_datetime() = default; - local_datetime(local_datetime const&) = default; - local_datetime(local_datetime&&) = default; - local_datetime& operator=(local_datetime const&) = default; - local_datetime& operator=(local_datetime&&) = default; -}; - -bool operator==(const local_datetime& lhs, const local_datetime& rhs); -bool operator!=(const local_datetime& lhs, const local_datetime& rhs); -bool operator< (const local_datetime& lhs, const local_datetime& rhs); -bool operator<=(const local_datetime& lhs, const local_datetime& rhs); -bool operator> (const local_datetime& lhs, const local_datetime& rhs); -bool operator>=(const local_datetime& lhs, const local_datetime& rhs); - -std::ostream& operator<<(std::ostream& os, const local_datetime& dt); - -std::string to_string(const local_datetime& dt); - -// ----------------------------------------------------------------------------- - -struct offset_datetime -{ - local_date date{}; - local_time time{}; - time_offset offset{}; - - offset_datetime(local_date d, local_time t, time_offset o) - : date{d}, time{t}, offset{o} - {} - offset_datetime(const local_datetime& dt, time_offset o) - : date{dt.date}, time{dt.time}, offset{o} - {} - // use the current local timezone offset - explicit offset_datetime(const local_datetime& ld); - explicit offset_datetime(const std::chrono::system_clock::time_point& tp); - explicit offset_datetime(const std::time_t& t); - explicit offset_datetime(const std::tm& t); - - operator std::chrono::system_clock::time_point() const; - - operator std::time_t() const; - - offset_datetime() = default; - ~offset_datetime() = default; - offset_datetime(offset_datetime const&) = default; - offset_datetime(offset_datetime&&) = default; - offset_datetime& operator=(offset_datetime const&) = default; - offset_datetime& operator=(offset_datetime&&) = default; - - private: - - static time_offset get_local_offset(const std::time_t* tp); -}; - -bool operator==(const offset_datetime& lhs, const offset_datetime& rhs); -bool operator!=(const offset_datetime& lhs, const offset_datetime& rhs); -bool operator< (const offset_datetime& lhs, const offset_datetime& rhs); -bool operator<=(const offset_datetime& lhs, const offset_datetime& rhs); -bool operator> (const offset_datetime& lhs, const offset_datetime& rhs); -bool operator>=(const offset_datetime& lhs, const offset_datetime& rhs); - -std::ostream& operator<<(std::ostream& os, const offset_datetime& dt); - -std::string to_string(const offset_datetime& dt); - -}//toml -#endif // TOML11_DATETIME_FWD_HPP diff --git a/include/toml11/fwd/error_info_fwd.hpp b/include/toml11/fwd/error_info_fwd.hpp deleted file mode 100644 index 5b8600c..0000000 --- a/include/toml11/fwd/error_info_fwd.hpp +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef TOML11_ERROR_INFO_FWD_HPP -#define TOML11_ERROR_INFO_FWD_HPP - -#include "../source_location.hpp" -#include "../utility.hpp" - -namespace toml -{ - -// error info returned from parser. -struct error_info -{ - error_info(std::string t, source_location l, std::string m, std::string s = "") - : title_(std::move(t)), locations_{std::make_pair(std::move(l), std::move(m))}, - suffix_(std::move(s)) - {} - - error_info(std::string t, std::vector> l, - std::string s = "") - : title_(std::move(t)), locations_(std::move(l)), suffix_(std::move(s)) - {} - - std::string const& title() const noexcept {return title_;} - std::string & title() noexcept {return title_;} - - std::vector> const& - locations() const noexcept {return locations_;} - - void add_locations(source_location loc, std::string msg) noexcept - { - locations_.emplace_back(std::move(loc), std::move(msg)); - } - - std::string const& suffix() const noexcept {return suffix_;} - std::string & suffix() noexcept {return suffix_;} - - private: - - std::string title_; - std::vector> locations_; - std::string suffix_; // hint or something like that -}; - -// forward decl -template -class basic_value; - -namespace detail -{ -inline error_info make_error_info_rec(error_info e) -{ - return e; -} -inline error_info make_error_info_rec(error_info e, std::string s) -{ - e.suffix() = s; - return e; -} - -template -error_info make_error_info_rec(error_info e, - const basic_value& v, std::string msg, Ts&& ... tail); - -template -error_info make_error_info_rec(error_info e, - source_location loc, std::string msg, Ts&& ... tail) -{ - e.add_locations(std::move(loc), std::move(msg)); - return make_error_info_rec(std::move(e), std::forward(tail)...); -} - -} // detail - -template -error_info make_error_info( - std::string title, source_location loc, std::string msg, Ts&& ... tail) -{ - error_info ei(std::move(title), std::move(loc), std::move(msg)); - return detail::make_error_info_rec(ei, std::forward(tail) ... ); -} - -std::string format_error(const std::string& errkind, const error_info& err); -std::string format_error(const error_info& err); - -// for custom error message -template -std::string format_error(std::string title, - source_location loc, std::string msg, Ts&& ... tail) -{ - return format_error("", make_error_info(std::move(title), - std::move(loc), std::move(msg), std::forward(tail)...)); -} - -std::ostream& operator<<(std::ostream& os, const error_info& e); - -} // toml -#endif // TOML11_ERROR_INFO_FWD_HPP diff --git a/include/toml11/fwd/format_fwd.hpp b/include/toml11/fwd/format_fwd.hpp deleted file mode 100644 index d478d96..0000000 --- a/include/toml11/fwd/format_fwd.hpp +++ /dev/null @@ -1,250 +0,0 @@ -#ifndef TOML11_FORMAT_FWD_HPP -#define TOML11_FORMAT_FWD_HPP - -#include -#include -#include - -#include -#include - -namespace toml -{ - -// toml types with serialization info - -enum class indent_char : std::uint8_t -{ - space, // use space - tab, // use tab - none // no indent -}; - -std::ostream& operator<<(std::ostream& os, const indent_char& c); -std::string to_string(const indent_char c); - -// ---------------------------------------------------------------------------- -// boolean - -struct boolean_format_info -{ - // nothing, for now -}; - -inline bool operator==(const boolean_format_info&, const boolean_format_info&) noexcept -{ - return true; -} -inline bool operator!=(const boolean_format_info&, const boolean_format_info&) noexcept -{ - return false; -} - -// ---------------------------------------------------------------------------- -// integer - -enum class integer_format : std::uint8_t -{ - dec = 0, - bin = 1, - oct = 2, - hex = 3, -}; - -std::ostream& operator<<(std::ostream& os, const integer_format f); -std::string to_string(const integer_format); - -struct integer_format_info -{ - integer_format fmt = integer_format::dec; - bool uppercase = true; // hex with uppercase - std::size_t width = 0; // minimal width (may exceed) - std::size_t spacer = 0; // position of `_` (if 0, no spacer) - std::string suffix = ""; // _suffix (library extension) -}; - -bool operator==(const integer_format_info&, const integer_format_info&) noexcept; -bool operator!=(const integer_format_info&, const integer_format_info&) noexcept; - -// ---------------------------------------------------------------------------- -// floating - -enum class floating_format : std::uint8_t -{ - defaultfloat = 0, - fixed = 1, // does not include exponential part - scientific = 2, // always include exponential part - hex = 3 // hexfloat extension -}; - -std::ostream& operator<<(std::ostream& os, const floating_format f); -std::string to_string(const floating_format); - -struct floating_format_info -{ - floating_format fmt = floating_format::defaultfloat; - std::size_t prec = 0; // precision (if 0, use the default) - std::string suffix = ""; // 1.0e+2_suffix (library extension) -}; - -bool operator==(const floating_format_info&, const floating_format_info&) noexcept; -bool operator!=(const floating_format_info&, const floating_format_info&) noexcept; - -// ---------------------------------------------------------------------------- -// string - -enum class string_format : std::uint8_t -{ - basic = 0, - literal = 1, - multiline_basic = 2, - multiline_literal = 3 -}; - -std::ostream& operator<<(std::ostream& os, const string_format f); -std::string to_string(const string_format); - -struct string_format_info -{ - string_format fmt = string_format::basic; - bool start_with_newline = false; -}; - -bool operator==(const string_format_info&, const string_format_info&) noexcept; -bool operator!=(const string_format_info&, const string_format_info&) noexcept; - -// ---------------------------------------------------------------------------- -// datetime - -enum class datetime_delimiter_kind : std::uint8_t -{ - upper_T = 0, - lower_t = 1, - space = 2, -}; -std::ostream& operator<<(std::ostream& os, const datetime_delimiter_kind d); -std::string to_string(const datetime_delimiter_kind); - -struct offset_datetime_format_info -{ - datetime_delimiter_kind delimiter = datetime_delimiter_kind::upper_T; - bool has_seconds = true; - std::size_t subsecond_precision = 6; // [us] -}; - -bool operator==(const offset_datetime_format_info&, const offset_datetime_format_info&) noexcept; -bool operator!=(const offset_datetime_format_info&, const offset_datetime_format_info&) noexcept; - -struct local_datetime_format_info -{ - datetime_delimiter_kind delimiter = datetime_delimiter_kind::upper_T; - bool has_seconds = true; - std::size_t subsecond_precision = 6; // [us] -}; - -bool operator==(const local_datetime_format_info&, const local_datetime_format_info&) noexcept; -bool operator!=(const local_datetime_format_info&, const local_datetime_format_info&) noexcept; - -struct local_date_format_info -{ - // nothing, for now -}; - -bool operator==(const local_date_format_info&, const local_date_format_info&) noexcept; -bool operator!=(const local_date_format_info&, const local_date_format_info&) noexcept; - -struct local_time_format_info -{ - bool has_seconds = true; - std::size_t subsecond_precision = 6; // [us] -}; - -bool operator==(const local_time_format_info&, const local_time_format_info&) noexcept; -bool operator!=(const local_time_format_info&, const local_time_format_info&) noexcept; - -// ---------------------------------------------------------------------------- -// array - -enum class array_format : std::uint8_t -{ - default_format = 0, - oneline = 1, - multiline = 2, - array_of_tables = 3 // [[format.in.this.way]] -}; - -std::ostream& operator<<(std::ostream& os, const array_format f); -std::string to_string(const array_format); - -struct array_format_info -{ - array_format fmt = array_format::default_format; - indent_char indent_type = indent_char::space; - std::int32_t body_indent = 4; // indent in case of multiline - std::int32_t closing_indent = 0; // indent of `]` -}; - -bool operator==(const array_format_info&, const array_format_info&) noexcept; -bool operator!=(const array_format_info&, const array_format_info&) noexcept; - -// ---------------------------------------------------------------------------- -// table - -enum class table_format : std::uint8_t -{ - multiline = 0, // [foo] \n bar = "baz" - oneline = 1, // foo = {bar = "baz"} - dotted = 2, // foo.bar = "baz" - multiline_oneline = 3, // foo = { \n bar = "baz" \n } - implicit = 4 // [x] defined by [x.y.z]. skip in serializer. -}; - -std::ostream& operator<<(std::ostream& os, const table_format f); -std::string to_string(const table_format); - -struct table_format_info -{ - table_format fmt = table_format::multiline; - indent_char indent_type = indent_char::space; - std::int32_t body_indent = 0; // indent of values - std::int32_t name_indent = 0; // indent of [table] - std::int32_t closing_indent = 0; // in case of {inline-table} -}; - -bool operator==(const table_format_info&, const table_format_info&) noexcept; -bool operator!=(const table_format_info&, const table_format_info&) noexcept; - -// ---------------------------------------------------------------------------- -// wrapper - -namespace detail -{ -template -struct value_with_format -{ - using value_type = T; - using format_type = F; - - value_with_format() = default; - ~value_with_format() = default; - value_with_format(const value_with_format&) = default; - value_with_format(value_with_format&&) = default; - value_with_format& operator=(const value_with_format&) = default; - value_with_format& operator=(value_with_format&&) = default; - - value_with_format(value_type v, format_type f) - : value{std::move(v)}, format{std::move(f)} - {} - - template - value_with_format(value_with_format other) - : value{std::move(other.value)}, format{std::move(other.format)} - {} - - value_type value; - format_type format; -}; -} // detail - -} // namespace toml -#endif // TOML11_FORMAT_FWD_HPP diff --git a/include/toml11/fwd/literal_fwd.hpp b/include/toml11/fwd/literal_fwd.hpp deleted file mode 100644 index e46612c..0000000 --- a/include/toml11/fwd/literal_fwd.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef TOML11_LITERAL_FWD_HPP -#define TOML11_LITERAL_FWD_HPP - -#include "../location.hpp" -#include "../types.hpp" -#include "../version.hpp" // IWYU pragma: keep for TOML11_HAS_CHAR8_T - -namespace toml -{ - -namespace detail -{ -// implementation -::toml::value literal_internal_impl(location loc); -} // detail - -inline namespace literals -{ -inline namespace toml_literals -{ - -::toml::value operator"" _toml(const char* str, std::size_t len); - -#if defined(TOML11_HAS_CHAR8_T) -// value of u8"" literal has been changed from char to char8_t and char8_t is -// NOT compatible to char -::toml::value operator"" _toml(const char8_t* str, std::size_t len); -#endif - -} // toml_literals -} // literals -} // toml -#endif // TOML11_LITERAL_FWD_HPP diff --git a/include/toml11/fwd/location_fwd.hpp b/include/toml11/fwd/location_fwd.hpp deleted file mode 100644 index 395b96c..0000000 --- a/include/toml11/fwd/location_fwd.hpp +++ /dev/null @@ -1,149 +0,0 @@ -#ifndef TOML11_LOCATION_FWD_HPP -#define TOML11_LOCATION_FWD_HPP - -#include "../result.hpp" - -#include -#include -#include - -namespace toml -{ -namespace detail -{ - -class region; // fwd decl - -// -// To represent where we are reading in the parse functions. -// Since it "points" somewhere in the input stream, the length is always 1. -// -class location -{ - public: - - using char_type = unsigned char; // must be unsigned - using container_type = std::vector; - using difference_type = typename container_type::difference_type; // to suppress sign-conversion warning - using source_ptr = std::shared_ptr; - - public: - - location(source_ptr src, std::string src_name) - : source_(std::move(src)), source_name_(std::move(src_name)), - location_(0), line_number_(1), column_number_(1) - {} - - location(const location&) = default; - location(location&&) = default; - location& operator=(const location&) = default; - location& operator=(location&&) = default; - ~location() = default; - - void advance(std::size_t n = 1) noexcept; - void retrace() noexcept; - - bool is_ok() const noexcept { return static_cast(this->source_); } - - bool eof() const noexcept; - char_type current() const; - - char_type peek(); - - std::size_t get_location() const noexcept - { - return this->location_; - } - - std::size_t line_number() const noexcept - { - return this->line_number_; - } - std::size_t column_number() const noexcept - { - return this->column_number_; - } - std::string get_line() const; - - source_ptr const& source() const noexcept {return this->source_;} - std::string const& source_name() const noexcept {return this->source_name_;} - - private: - - void advance_impl(const std::size_t n); - void retrace_impl(); - std::size_t calc_column_number() const noexcept; - - private: - - friend region; - - private: - - source_ptr source_; - std::string source_name_; - std::size_t location_; // std::vector<>::difference_type is signed - std::size_t line_number_; - std::size_t column_number_; -}; - -bool operator==(const location& lhs, const location& rhs) noexcept; -bool operator!=(const location& lhs, const location& rhs); - -location prev(const location& loc); -location next(const location& loc); -location make_temporary_location(const std::string& str) noexcept; - -template -result -find_if(const location& first, const location& last, const F& func) noexcept -{ - if(first.source() != last.source()) { return err(); } - if(first.get_location() >= last.get_location()) { return err(); } - - auto loc = first; - while(loc.get_location() != last.get_location()) - { - if(func(loc.current())) - { - return ok(loc); - } - loc.advance(); - } - return err(); -} - -template -result -rfind_if(location first, const location& last, const F& func) -{ - if(first.source() != last.source()) { return err(); } - if(first.get_location() >= last.get_location()) { return err(); } - - auto loc = last; - while(loc.get_location() != first.get_location()) - { - if(func(loc.current())) - { - return ok(loc); - } - loc.retrace(); - } - if(func(first.current())) - { - return ok(first); - } - return err(); -} - -result find(const location& first, const location& last, - const location::char_type val); -result rfind(const location& first, const location& last, - const location::char_type val); - -std::size_t count(const location& first, const location& last, - const location::char_type& c); - -} // detail -} // toml -#endif // TOML11_LOCATION_FWD_HPP diff --git a/include/toml11/fwd/region_fwd.hpp b/include/toml11/fwd/region_fwd.hpp deleted file mode 100644 index 04ce56f..0000000 --- a/include/toml11/fwd/region_fwd.hpp +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef TOML11_REGION_FWD_HPP -#define TOML11_REGION_FWD_HPP - -#include "../location.hpp" - -#include -#include - -#include - -namespace toml -{ -namespace detail -{ - -// -// To represent where is a toml::value defined, or where does an error occur. -// Stored in toml::value. source_location will be constructed based on this. -// -class region -{ - public: - - using char_type = location::char_type; - using container_type = location::container_type; - using difference_type = location::difference_type; - using source_ptr = location::source_ptr; - - using iterator = typename container_type::iterator; - using const_iterator = typename container_type::const_iterator; - - public: - - // a value that is constructed manually does not have input stream info - region() - : source_(nullptr), source_name_(""), length_(0), - first_(0), first_line_(0), first_column_(0), last_(0), last_line_(0), - last_column_(0) - {} - - // a value defined in [first, last). - // Those source must be the same. Instread, `region` does not make sense. - region(const location& first, const location& last); - - // shorthand of [loc, loc+1) - explicit region(const location& loc); - - ~region() = default; - region(const region&) = default; - region(region&&) = default; - region& operator=(const region&) = default; - region& operator=(region&&) = default; - - bool is_ok() const noexcept { return static_cast(this->source_); } - - operator bool() const noexcept { return this->is_ok(); } - - std::size_t length() const noexcept {return this->length_;} - - std::size_t first_line_number() const noexcept - { - return this->first_line_; - } - std::size_t first_column_number() const noexcept - { - return this->first_column_; - } - std::size_t last_line_number() const noexcept - { - return this->last_line_; - } - std::size_t last_column_number() const noexcept - { - return this->last_column_; - } - - char_type at(std::size_t i) const; - - const_iterator begin() const noexcept; - const_iterator end() const noexcept; - const_iterator cbegin() const noexcept; - const_iterator cend() const noexcept; - - std::string as_string() const; - std::vector> as_lines() const; - - source_ptr const& source() const noexcept {return this->source_;} - std::string const& source_name() const noexcept {return this->source_name_;} - - private: - - std::pair - take_line(const_iterator begin, const_iterator end) const; - - private: - - source_ptr source_; - std::string source_name_; - std::size_t length_; - std::size_t first_; - std::size_t first_line_; - std::size_t first_column_; - std::size_t last_; - std::size_t last_line_; - std::size_t last_column_; -}; - -} // namespace detail -} // namespace toml -#endif // TOML11_REGION_FWD_HPP diff --git a/include/toml11/fwd/scanner_fwd.hpp b/include/toml11/fwd/scanner_fwd.hpp deleted file mode 100644 index a886727..0000000 --- a/include/toml11/fwd/scanner_fwd.hpp +++ /dev/null @@ -1,391 +0,0 @@ -#ifndef TOML11_SCANNER_FWD_HPP -#define TOML11_SCANNER_FWD_HPP - -#include "../region.hpp" - -#include -#include -#include -#include - -#include -#include -#include - -namespace toml -{ -namespace detail -{ - -class scanner_base -{ - public: - virtual ~scanner_base() = default; - virtual region scan(location& loc) const = 0; - virtual scanner_base* clone() const = 0; - - // returns expected character or set of characters or literal. - // to show the error location, it changes loc (in `sequence`, especially). - virtual std::string expected_chars(location& loc) const = 0; - virtual std::string name() const = 0; -}; - -// make `scanner*` copyable -struct scanner_storage -{ - template>::value, - std::nullptr_t> = nullptr> - explicit scanner_storage(Scanner&& s) - : scanner_(cxx::make_unique>(std::forward(s))) - {} - ~scanner_storage() = default; - - scanner_storage(const scanner_storage& other); - scanner_storage& operator=(const scanner_storage& other); - scanner_storage(scanner_storage&&) = default; - scanner_storage& operator=(scanner_storage&&) = default; - - bool is_ok() const noexcept {return static_cast(scanner_);} - - region scan(location& loc) const; - - std::string expected_chars(location& loc) const; - - scanner_base& get() const noexcept; - - std::string name() const; - - private: - - std::unique_ptr scanner_; -}; - -// ---------------------------------------------------------------------------- - -class character final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit character(const char_type c) noexcept - : value_(c) - {} - ~character() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location&) const override; - - scanner_base* clone() const override; - - std::string name() const override; - - private: - char_type value_; -}; - -// ---------------------------------------------------------------------------- - -class character_either final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit character_either(std::initializer_list cs) noexcept - : chars_(std::move(cs)) - { - assert(! this->chars_.empty()); - } - - template - explicit character_either(const char (&cs)[N]) noexcept - : chars_(N-1, '\0') - { - static_assert(N >= 1, ""); - for(std::size_t i=0; i+1 chars_; -}; - -// ---------------------------------------------------------------------------- - -class character_in_range final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit character_in_range(const char_type from, const char_type to) noexcept - : from_(from), to_(to) - {} - ~character_in_range() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location&) const override; - - scanner_base* clone() const override; - - std::string name() const override; - - private: - char_type from_; - char_type to_; -}; - -// ---------------------------------------------------------------------------- - -class literal final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - template - explicit literal(const char (&cs)[N]) noexcept - : value_(cs), size_(N-1) // remove null character at the end - {} - ~literal() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location&) const override; - - scanner_base* clone() const override; - - std::string name() const override; - - private: - const char* value_; - std::size_t size_; -}; - -// ---------------------------------------------------------------------------- - -class sequence final: public scanner_base -{ - public: - using char_type = location::char_type; - - public: - - template - explicit sequence(Ts&& ... args) - { - push_back_all(std::forward(args)...); - } - sequence(const sequence&) = default; - sequence(sequence&&) = default; - sequence& operator=(const sequence&) = default; - sequence& operator=(sequence&&) = default; - ~sequence() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location& loc) const override; - - scanner_base* clone() const override; - - template - void push_back(Scanner&& other_scanner) - { - this->others_.emplace_back(std::forward(other_scanner)); - } - - std::string name() const override; - - private: - - void push_back_all() - { - return; - } - template - void push_back_all(T&& head, Ts&& ... args) - { - others_.emplace_back(std::forward(head)); - push_back_all(std::forward(args)...); - return; - } - - private: - std::vector others_; -}; - -// ---------------------------------------------------------------------------- - -class either final: public scanner_base -{ - public: - using char_type = location::char_type; - - public: - - template - explicit either(Ts&& ... args) - { - push_back_all(std::forward(args)...); - } - either(const either&) = default; - either(either&&) = default; - either& operator=(const either&) = default; - either& operator=(either&&) = default; - ~either() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location& loc) const override; - - scanner_base* clone() const override; - - template - void push_back(Scanner&& other_scanner) - { - this->others_.emplace_back(std::forward(other_scanner)); - } - - std::string name() const override; - - private: - - void push_back_all() - { - return; - } - template - void push_back_all(T&& head, Ts&& ... args) - { - others_.emplace_back(std::forward(head)); - push_back_all(std::forward(args)...); - return; - } - - private: - std::vector others_; -}; - -// ---------------------------------------------------------------------------- - -class repeat_exact final: public scanner_base -{ - public: - using char_type = location::char_type; - - public: - - template - repeat_exact(const std::size_t length, Scanner&& other) - : length_(length), other_(std::forward(other)) - {} - repeat_exact(const repeat_exact&) = default; - repeat_exact(repeat_exact&&) = default; - repeat_exact& operator=(const repeat_exact&) = default; - repeat_exact& operator=(repeat_exact&&) = default; - ~repeat_exact() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location& loc) const override; - - scanner_base* clone() const override; - - std::string name() const override; - - private: - std::size_t length_; - scanner_storage other_; -}; - -// ---------------------------------------------------------------------------- - -class repeat_at_least final: public scanner_base -{ - public: - using char_type = location::char_type; - - public: - - template - repeat_at_least(const std::size_t length, Scanner&& s) - : length_(length), other_(std::forward(s)) - {} - repeat_at_least(const repeat_at_least&) = default; - repeat_at_least(repeat_at_least&&) = default; - repeat_at_least& operator=(const repeat_at_least&) = default; - repeat_at_least& operator=(repeat_at_least&&) = default; - ~repeat_at_least() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location& loc) const override; - - scanner_base* clone() const override; - - std::string name() const override; - - private: - std::size_t length_; - scanner_storage other_; -}; - -// ---------------------------------------------------------------------------- - -class maybe final: public scanner_base -{ - public: - using char_type = location::char_type; - - public: - - template - explicit maybe(Scanner&& s) - : other_(std::forward(s)) - {} - maybe(const maybe&) = default; - maybe(maybe&&) = default; - maybe& operator=(const maybe&) = default; - maybe& operator=(maybe&&) = default; - ~maybe() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location&) const override; - - scanner_base* clone() const override; - - std::string name() const override; - - private: - scanner_storage other_; -}; - -} // detail -} // toml -#endif // TOML11_SCANNER_FWD_HPP diff --git a/include/toml11/fwd/source_location_fwd.hpp b/include/toml11/fwd/source_location_fwd.hpp deleted file mode 100644 index 7022689..0000000 --- a/include/toml11/fwd/source_location_fwd.hpp +++ /dev/null @@ -1,148 +0,0 @@ -#ifndef TOML11_SOURCE_LOCATION_FWD_HPP -#define TOML11_SOURCE_LOCATION_FWD_HPP - -#include "../region.hpp" - -#include -#include -#include - -namespace toml -{ - -// -// A struct to contain location in a toml file. -// -// To reduce memory consumption, it omits unrelated parts of long lines. like: -// -// 1. one long line, short region -// ``` -// | -// 1 | ... "foo", "bar", baz, "qux", "foobar", ... -// | ^-- unknown value -// ``` -// 2. long region -// ``` -// | -// 1 | array = [ "foo", ... "bar" ] -// | ^^^^^^^^^^^^^^^^^^^^- in this array -// ``` -// 3. many lines -// | -// 1 | array = [ "foo", -// | ^^^^^^^^ -// | ... -// | ^^^ -// | -// 10 | , "bar"] -// | ^^^^^^^^- in this array -// ``` -// -struct source_location -{ - public: - - explicit source_location(const detail::region& r); - ~source_location() = default; - source_location(source_location const&) = default; - source_location(source_location &&) = default; - source_location& operator=(source_location const&) = default; - source_location& operator=(source_location &&) = default; - - bool is_ok() const noexcept {return this->is_ok_;} - std::size_t length() const noexcept {return this->length_;} - - std::size_t first_line_number() const noexcept {return this->first_line_;} - std::size_t first_column_number() const noexcept {return this->first_column_;} - std::size_t last_line_number() const noexcept {return this->last_line_;} - std::size_t last_column_number() const noexcept {return this->last_column_;} - - std::string const& file_name() const noexcept {return this->file_name_;} - - std::size_t num_lines() const noexcept {return this->line_str_.size();} - - std::string const& first_line() const; - std::string const& last_line() const; - - std::vector const& lines() const noexcept {return line_str_;} - - // for internal use - std::size_t first_column_offset() const noexcept {return this->first_offset_;} - std::size_t last_column_offset() const noexcept {return this->last_offset_;} - - private: - - bool is_ok_; - std::size_t first_line_; - std::size_t first_column_; // column num in the actual file - std::size_t first_offset_; // column num in the shown line - std::size_t last_line_; - std::size_t last_column_; // column num in the actual file - std::size_t last_offset_; // column num in the shown line - std::size_t length_; - std::string file_name_; - std::vector line_str_; -}; - -namespace detail -{ - -std::size_t integer_width_base10(std::size_t i) noexcept; - -inline std::size_t line_width() noexcept {return 0;} - -template -std::size_t line_width(const source_location& loc, const std::string& /*msg*/, - const Ts& ... tail) noexcept -{ - return (std::max)( - integer_width_base10(loc.last_line_number()), line_width(tail...)); -} - -std::ostringstream& -format_filename(std::ostringstream& oss, const source_location& loc); - -std::ostringstream& -format_empty_line(std::ostringstream& oss, const std::size_t lnw); - -std::ostringstream& format_line(std::ostringstream& oss, - const std::size_t lnw, const std::size_t linenum, const std::string& line); - -std::ostringstream& format_underline(std::ostringstream& oss, - const std::size_t lnw, const std::size_t col, const std::size_t len, - const std::string& msg); - -std::string format_location_impl(const std::size_t lnw, - const std::string& prev_fname, - const source_location& loc, const std::string& msg); - -inline std::string format_location_rec(const std::size_t, const std::string&) -{ - return ""; -} - -template -std::string format_location_rec(const std::size_t lnw, - const std::string& prev_fname, - const source_location& loc, const std::string& msg, - const Ts& ... tail) -{ - return format_location_impl(lnw, prev_fname, loc, msg) + - format_location_rec(lnw, loc.file_name(), tail...); -} - -} // namespace detail - -// format a location info without title -template -std::string format_location( - const source_location& loc, const std::string& msg, const Ts& ... tail) -{ - const auto lnw = detail::line_width(loc, msg, tail...); - - const std::string f(""); // at the 1st iteration, no prev_filename is given - return detail::format_location_rec(lnw, f, loc, msg, tail...); -} - -} // toml -#endif // TOML11_SOURCE_LOCATION_FWD_HPP diff --git a/include/toml11/fwd/syntax_fwd.hpp b/include/toml11/fwd/syntax_fwd.hpp deleted file mode 100644 index 3560821..0000000 --- a/include/toml11/fwd/syntax_fwd.hpp +++ /dev/null @@ -1,357 +0,0 @@ -#ifndef TOML11_SYNTAX_FWD_HPP -#define TOML11_SYNTAX_FWD_HPP - -#include "../scanner.hpp" -#include "../spec.hpp" - -namespace toml -{ -namespace detail -{ -namespace syntax -{ - -using char_type = location::char_type; - -// =========================================================================== -// UTF-8 - -// avoid redundant representation and out-of-unicode sequence - -character_in_range utf8_1byte (const spec&); -sequence utf8_2bytes(const spec&); -sequence utf8_3bytes(const spec&); -sequence utf8_4bytes(const spec&); - -class non_ascii final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit non_ascii(const spec& s) noexcept; - ~non_ascii() override = default; - - region scan(location& loc) const override - { - return scanner_.scan(loc); - } - - std::string expected_chars(location&) const override - { - return "non-ascii utf-8 bytes"; - } - - scanner_base* clone() const override - { - return new non_ascii(*this); - } - - std::string name() const override - { - return "non_ascii"; - } - - private: - - either scanner_; -}; - -// =========================================================================== -// Whitespace - -character_either wschar(const spec&); - -repeat_at_least ws(const spec& s); - -// =========================================================================== -// Newline - -either newline(const spec&); - -// =========================================================================== -// Comments - -either allowed_comment_char(const spec& s); - -// XXX Note that it does not take newline -sequence comment(const spec& s); - -// =========================================================================== -// Boolean - -either boolean(const spec&); - -// =========================================================================== -// Integer - -class digit final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit digit(const spec&) noexcept; - ~digit() override = default; - - region scan(location& loc) const override - { - return scanner_.scan(loc); - } - - std::string expected_chars(location&) const override - { - return "digit [0-9]"; - } - - scanner_base* clone() const override - { - return new digit(*this); - } - - std::string name() const override - { - return "digit"; - } - - private: - - character_in_range scanner_; -}; - -class alpha final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit alpha(const spec&) noexcept; - ~alpha() override = default; - - region scan(location& loc) const override - { - return scanner_.scan(loc); - } - - std::string expected_chars(location&) const override - { - return "alpha [a-zA-Z]"; - } - - scanner_base* clone() const override - { - return new alpha(*this); - } - - std::string name() const override - { - return "alpha"; - } - - private: - - either scanner_; -}; - -class hexdig final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit hexdig(const spec& s) noexcept; - ~hexdig() override = default; - - region scan(location& loc) const override - { - return scanner_.scan(loc); - } - - std::string expected_chars(location&) const override - { - return "hex [0-9a-fA-F]"; - } - - scanner_base* clone() const override - { - return new hexdig(*this); - } - - std::string name() const override - { - return "hexdig"; - } - - private: - - either scanner_; -}; - -sequence num_suffix(const spec& s); - -sequence dec_int(const spec& s); -sequence hex_int(const spec& s); -sequence oct_int(const spec&); -sequence bin_int(const spec&); -either integer(const spec& s); - -// =========================================================================== -// Floating - -sequence zero_prefixable_int(const spec& s); -sequence fractional_part(const spec& s); -sequence exponent_part(const spec& s); -sequence hex_floating(const spec& s); -either floating(const spec& s); - -// =========================================================================== -// Datetime - -sequence local_date(const spec& s); -sequence local_time(const spec& s); -either time_offset(const spec& s); -sequence full_time(const spec& s); -character_either time_delim(const spec&); -sequence local_datetime(const spec& s); -sequence offset_datetime(const spec& s); - -// =========================================================================== -// String - -sequence escaped(const spec& s); - -either basic_char(const spec& s); - -sequence basic_string(const spec& s); - -// --------------------------------------------------------------------------- -// multiline string - -sequence escaped_newline(const spec& s); -sequence ml_basic_string(const spec& s); - -// --------------------------------------------------------------------------- -// literal string - -either literal_char(const spec& s); -sequence literal_string(const spec& s); - -sequence ml_literal_string(const spec& s); - -either string(const spec& s); - -// =========================================================================== -// Keys - -// to keep `expected_chars` simple -class non_ascii_key_char final : public scanner_base -{ - public: - - using char_type = location::char_type; - - private: - - using in_range = character_in_range; // make definition short - - public: - - explicit non_ascii_key_char(const spec& s) noexcept; - ~non_ascii_key_char() override = default; - - region scan(location& loc) const override; - - std::string expected_chars(location&) const override - { - return "bare key non-ASCII script"; - } - - scanner_base* clone() const override - { - return new non_ascii_key_char(*this); - } - - std::string name() const override - { - return "non-ASCII bare key"; - } - - private: - - std::uint32_t read_utf8(location& loc) const; -}; - - -repeat_at_least unquoted_key(const spec& s); - -either quoted_key(const spec& s); - -either simple_key(const spec& s); - -sequence dot_sep(const spec& s); - -sequence dotted_key(const spec& s); - - -class key final : public scanner_base -{ - public: - - using char_type = location::char_type; - - public: - - explicit key(const spec& s) noexcept; - ~key() override = default; - - region scan(location& loc) const override - { - return scanner_.scan(loc); - } - - std::string expected_chars(location&) const override - { - return "basic key([a-zA-Z0-9_-]) or quoted key(\" or ')"; - } - - scanner_base* clone() const override - { - return new key(*this); - } - - std::string name() const override - { - return "key"; - } - - private: - - either scanner_; -}; - -sequence keyval_sep(const spec& s); - -// =========================================================================== -// Table key - -sequence std_table(const spec& s); - -sequence array_table(const spec& s); - -// =========================================================================== -// extension: null - -literal null_value(const spec&); - -} // namespace syntax -} // namespace detail -} // namespace toml -#endif // TOML11_SYNTAX_FWD_HPP diff --git a/include/toml11/fwd/value_t_fwd.hpp b/include/toml11/fwd/value_t_fwd.hpp deleted file mode 100644 index fddf843..0000000 --- a/include/toml11/fwd/value_t_fwd.hpp +++ /dev/null @@ -1,117 +0,0 @@ -#ifndef TOML11_VALUE_T_FWD_HPP -#define TOML11_VALUE_T_FWD_HPP - -#include "../compat.hpp" -#include "../format.hpp" - -#include -#include -#include - -#include - -namespace toml -{ - -// forward decl -template -class basic_value; - -// ---------------------------------------------------------------------------- -// enum representing toml types - -enum class value_t : std::uint8_t -{ - empty = 0, - boolean = 1, - integer = 2, - floating = 3, - string = 4, - offset_datetime = 5, - local_datetime = 6, - local_date = 7, - local_time = 8, - array = 9, - table = 10 -}; - -std::ostream& operator<<(std::ostream& os, value_t t); -std::string to_string(value_t t); - - -// ---------------------------------------------------------------------------- -// meta functions for internal use - -namespace detail -{ - -template -using value_t_constant = std::integral_constant; - -template -struct type_to_enum : value_t_constant {}; - -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; -template struct type_to_enum : value_t_constant {}; - -template -struct enum_to_type { using type = void; }; - -template struct enum_to_type { using type = typename V::boolean_type ; }; -template struct enum_to_type { using type = typename V::integer_type ; }; -template struct enum_to_type { using type = typename V::floating_type ; }; -template struct enum_to_type { using type = typename V::string_type ; }; -template struct enum_to_type { using type = typename V::offset_datetime_type; }; -template struct enum_to_type { using type = typename V::local_datetime_type ; }; -template struct enum_to_type { using type = typename V::local_date_type ; }; -template struct enum_to_type { using type = typename V::local_time_type ; }; -template struct enum_to_type { using type = typename V::array_type ; }; -template struct enum_to_type { using type = typename V::table_type ; }; - -template -using enum_to_type_t = typename enum_to_type::type; - -template -struct enum_to_fmt_type { using type = void; }; - -template<> struct enum_to_fmt_type { using type = boolean_format_info ; }; -template<> struct enum_to_fmt_type { using type = integer_format_info ; }; -template<> struct enum_to_fmt_type { using type = floating_format_info ; }; -template<> struct enum_to_fmt_type { using type = string_format_info ; }; -template<> struct enum_to_fmt_type { using type = offset_datetime_format_info; }; -template<> struct enum_to_fmt_type { using type = local_datetime_format_info ; }; -template<> struct enum_to_fmt_type { using type = local_date_format_info ; }; -template<> struct enum_to_fmt_type { using type = local_time_format_info ; }; -template<> struct enum_to_fmt_type { using type = array_format_info ; }; -template<> struct enum_to_fmt_type { using type = table_format_info ; }; - -template -using enum_to_fmt_type_t = typename enum_to_fmt_type::type; - -template -struct is_exact_toml_type0 : cxx::disjunction< - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same, - std::is_same - >{}; -template struct is_exact_toml_type: is_exact_toml_type0, V> {}; -template struct is_not_toml_type : cxx::negation> {}; - -} // namespace detail -} // namespace toml -#endif // TOML11_VALUE_T_FWD_HPP diff --git a/include/toml11/get.hpp b/include/toml11/get.hpp deleted file mode 100644 index 3805d09..0000000 --- a/include/toml11/get.hpp +++ /dev/null @@ -1,632 +0,0 @@ -#ifndef TOML11_GET_HPP -#define TOML11_GET_HPP - -#include - -#include "from.hpp" -#include "types.hpp" -#include "value.hpp" - -#if defined(TOML11_HAS_STRING_VIEW) -#include -#endif // string_view - -namespace toml -{ - -// ============================================================================ -// T is toml::value; identity transformation. - -template -cxx::enable_if_t>::value, T>& -get(basic_value& v) -{ - return v; -} - -template -cxx::enable_if_t>::value, T> const& -get(const basic_value& v) -{ - return v; -} - -template -cxx::enable_if_t>::value, T> -get(basic_value&& v) -{ - return basic_value(std::move(v)); -} - -// ============================================================================ -// exact toml::* type - -template -cxx::enable_if_t>::value, T> & -get(basic_value& v) -{ - constexpr auto ty = detail::type_to_enum>::value; - return detail::getter::get(v); -} - -template -cxx::enable_if_t>::value, T> const& -get(const basic_value& v) -{ - constexpr auto ty = detail::type_to_enum>::value; - return detail::getter::get(v); -} - -template -cxx::enable_if_t>::value, T> -get(basic_value&& v) -{ - constexpr auto ty = detail::type_to_enum>::value; - return detail::getter::get(std::move(v)); -} - -// ============================================================================ -// T is toml::basic_value - -template -cxx::enable_if_t, - cxx::negation>> - >::value, T> -get(basic_value v) -{ - return T(std::move(v)); -} - -// ============================================================================ -// integer convertible from toml::value::integer_type - -template -cxx::enable_if_t, - cxx::negation>, - detail::is_not_toml_type>, - cxx::negation>, - cxx::negation> - >::value, T> -get(const basic_value& v) -{ - return static_cast(v.as_integer()); -} - -// ============================================================================ -// floating point convertible from toml::value::floating_type - -template -cxx::enable_if_t, - detail::is_not_toml_type>, - cxx::negation>, - cxx::negation> - >::value, T> -get(const basic_value& v) -{ - return static_cast(v.as_floating()); -} - -// ============================================================================ -// std::string with different char/trait/allocator - -template -cxx::enable_if_t>, - detail::is_1byte_std_basic_string - >::value, T> -get(const basic_value& v) -{ - return detail::string_conv>(v.as_string()); -} - -// ============================================================================ -// std::string_view - -#if defined(TOML11_HAS_STRING_VIEW) - -template -cxx::enable_if_t::string_type>::value, T> -get(const basic_value& v) -{ - return T(v.as_string()); -} - -#endif // string_view - -// ============================================================================ -// std::chrono::duration from toml::local_time - -template -cxx::enable_if_t::value, T> -get(const basic_value& v) -{ - return std::chrono::duration_cast( - std::chrono::nanoseconds(v.as_local_time())); -} - -// ============================================================================ -// std::chrono::system_clock::time_point from toml::datetime variants - -template -cxx::enable_if_t< - std::is_same::value, T> -get(const basic_value& v) -{ - switch(v.type()) - { - case value_t::local_date: - { - return std::chrono::system_clock::time_point(v.as_local_date()); - } - case value_t::local_datetime: - { - return std::chrono::system_clock::time_point(v.as_local_datetime()); - } - case value_t::offset_datetime: - { - return std::chrono::system_clock::time_point(v.as_offset_datetime()); - } - default: - { - const auto loc = v.location(); - throw type_error(format_error("toml::get: " - "bad_cast to std::chrono::system_clock::time_point", loc, - "the actual type is " + to_string(v.type())), loc); - } - } -} - -// ============================================================================ -// forward declaration to use this recursively. ignore this and go ahead. - -// array-like (w/ push_back) -template -cxx::enable_if_t, // T is a container - detail::has_push_back_method, // .push_back() works - detail::is_not_toml_type>, // but not toml::array - cxx::negation>, // but not std::basic_string -#if defined(TOML11_HAS_STRING_VIEW) - cxx::negation>, // but not std::basic_string_view -#endif - cxx::negation>, // no T.from_toml() - cxx::negation>, // no toml::from - cxx::negation&>> - >::value, T> -get(const basic_value&); - -// std::array -template -cxx::enable_if_t::value, T> -get(const basic_value&); - -// std::forward_list -template -cxx::enable_if_t::value, T> -get(const basic_value&); - -// std::pair -template -cxx::enable_if_t::value, T> -get(const basic_value&); - -// std::tuple -template -cxx::enable_if_t::value, T> -get(const basic_value&); - -// std::map (key is convertible from toml::value::key_type) -template -cxx::enable_if_t, // T is map - detail::is_not_toml_type>, // but not toml::table - std::is_convertible::key_type, - typename T::key_type>, // keys are convertible - cxx::negation>, // no T.from_toml() - cxx::negation>, // no toml::from - cxx::negation&>> - >::value, T> -get(const basic_value& v); - -// std::map (key is not convertible from toml::value::key_type, but -// is a std::basic_string) -template -cxx::enable_if_t, // T is map - detail::is_not_toml_type>, // but not toml::table - cxx::negation::key_type, - typename T::key_type>>, // keys are NOT convertible - detail::is_1byte_std_basic_string, // is std::basic_string - cxx::negation>, // no T.from_toml() - cxx::negation>, // no toml::from - cxx::negation&>> - >::value, T> -get(const basic_value& v); - -// toml::from::from_toml(v) -template -cxx::enable_if_t::value, T> -get(const basic_value&); - -// has T.from_toml(v) but no from -template -cxx::enable_if_t, // has T.from_toml() - cxx::negation>, // no toml::from - std::is_default_constructible // T{} works - >::value, T> -get(const basic_value&); - -// T(const toml::value&) and T is not toml::basic_value, -// and it does not have `from` nor `from_toml`. -template -cxx::enable_if_t&>, // has T(const basic_value&) - cxx::negation>, // but not basic_value itself - cxx::negation>, // no .from_toml() - cxx::negation> // no toml::from - >::value, T> -get(const basic_value&); - -// ============================================================================ -// array-like types; most likely STL container, like std::vector, etc. - -template -cxx::enable_if_t, // T is a container - detail::has_push_back_method, // .push_back() works - detail::is_not_toml_type>, // but not toml::array - cxx::negation>, // but not std::basic_string -#if defined(TOML11_HAS_STRING_VIEW) - cxx::negation>, // but not std::basic_string_view -#endif - cxx::negation>, // no T.from_toml() - cxx::negation>, // no toml::from - cxx::negation&>> - >::value, T> -get(const basic_value& v) -{ - using value_type = typename T::value_type; - const auto& a = v.as_array(); - - T container; - detail::try_reserve(container, a.size()); // if T has .reserve(), call it - - for(const auto& elem : a) - { - container.push_back(get(elem)); - } - return container; -} - -// ============================================================================ -// std::array - -template -cxx::enable_if_t::value, T> -get(const basic_value& v) -{ - using value_type = typename T::value_type; - const auto& a = v.as_array(); - - T container; - if(a.size() != container.size()) - { - const auto loc = v.location(); - throw std::out_of_range(format_error("toml::get: while converting to an array: " - " array size is " + std::to_string(container.size()) + - " but there are " + std::to_string(a.size()) + " elements in toml array.", - loc, "here")); - } - for(std::size_t i=0; i(a.at(i)); - } - return container; -} - -// ============================================================================ -// std::forward_list - -template -cxx::enable_if_t::value, T> -get(const basic_value& v) -{ - using value_type = typename T::value_type; - - T container; - for(const auto& elem : v.as_array()) - { - container.push_front(get(elem)); - } - container.reverse(); - return container; -} - -// ============================================================================ -// std::pair - -template -cxx::enable_if_t::value, T> -get(const basic_value& v) -{ - using first_type = typename T::first_type; - using second_type = typename T::second_type; - - const auto& ar = v.as_array(); - if(ar.size() != 2) - { - const auto loc = v.location(); - throw std::out_of_range(format_error("toml::get: while converting std::pair: " - " but there are " + std::to_string(ar.size()) + " > 2 elements in toml array.", - loc, "here")); - } - return std::make_pair(::toml::get(ar.at(0)), - ::toml::get(ar.at(1))); -} - -// ============================================================================ -// std::tuple. - -namespace detail -{ -template -T get_tuple_impl(const Array& a, cxx::index_sequence) -{ - return std::make_tuple( - ::toml::get::type>(a.at(I))...); -} -} // detail - -template -cxx::enable_if_t::value, T> -get(const basic_value& v) -{ - const auto& ar = v.as_array(); - if(ar.size() != std::tuple_size::value) - { - const auto loc = v.location(); - throw std::out_of_range(format_error("toml::get: while converting std::tuple: " - " there are " + std::to_string(ar.size()) + " > " + - std::to_string(std::tuple_size::value) + " elements in toml array.", - loc, "here")); - } - return detail::get_tuple_impl(ar, - cxx::make_index_sequence::value>{}); -} - -// ============================================================================ -// map-like types; most likely STL map, like std::map or std::unordered_map. - -// key is convertible from toml::value::key_type -template -cxx::enable_if_t, // T is map - detail::is_not_toml_type>, // but not toml::table - std::is_convertible::key_type, - typename T::key_type>, // keys are convertible - cxx::negation>, // no T.from_toml() - cxx::negation>, // no toml::from - cxx::negation&>> - >::value, T> -get(const basic_value& v) -{ - using key_type = typename T::key_type; - using mapped_type = typename T::mapped_type; - static_assert( - std::is_convertible::key_type, key_type>::value, - "toml::get only supports map type of which key_type is " - "convertible from toml::basic_value::key_type."); - - T m; - for(const auto& kv : v.as_table()) - { - m.emplace(key_type(kv.first), get(kv.second)); - } - return m; -} - -// key is NOT convertible from toml::value::key_type but std::basic_string -template -cxx::enable_if_t, // T is map - detail::is_not_toml_type>, // but not toml::table - cxx::negation::key_type, - typename T::key_type>>, // keys are NOT convertible - detail::is_1byte_std_basic_string, // is std::basic_string - cxx::negation>, // no T.from_toml() - cxx::negation>, // no toml::from - cxx::negation&>> - >::value, T> -get(const basic_value& v) -{ - using key_type = typename T::key_type; - using mapped_type = typename T::mapped_type; - - T m; - for(const auto& kv : v.as_table()) - { - m.emplace(detail::string_conv(kv.first), get(kv.second)); - } - return m; -} - -// ============================================================================ -// user-defined, but convertible types. - -// toml::from -template -cxx::enable_if_t::value, T> -get(const basic_value& v) -{ - return ::toml::from::from_toml(v); -} - -// has T.from_toml(v) but no from -template -cxx::enable_if_t, // has T.from_toml() - cxx::negation>, // no toml::from - std::is_default_constructible // T{} works - >::value, T> -get(const basic_value& v) -{ - T ud; - ud.from_toml(v); - return ud; -} - -// T(const toml::value&) and T is not toml::basic_value, -// and it does not have `from` nor `from_toml`. -template -cxx::enable_if_t&>, // has T(const basic_value&) - cxx::negation>, // but not basic_value itself - cxx::negation>, // no .from_toml() - cxx::negation> // no toml::from - >::value, T> -get(const basic_value& v) -{ - return T(v); -} - -// ============================================================================ -// get_or(value, fallback) - -template -cxx::enable_if_t::value, basic_value> const& -get_or(const basic_value& v, const basic_value&) -{ - return v; -} - -template -cxx::enable_if_t::value, basic_value>& -get_or(basic_value& v, basic_value&) -{ - return v; -} - -template -cxx::enable_if_t::value, basic_value> -get_or(basic_value&& v, basic_value&&) -{ - return v; -} - -// ---------------------------------------------------------------------------- -// specialization for the exact toml types (return type becomes lvalue ref) - -template -cxx::enable_if_t< - detail::is_exact_toml_type>::value, T> const& -get_or(const basic_value& v, const T& opt) noexcept -{ - try - { - return get>(v); - } - catch(...) - { - return opt; - } -} -template -cxx::enable_if_t>, - detail::is_exact_toml_type> - >::value, T>& -get_or(basic_value& v, T& opt) noexcept -{ - try - { - return get>(v); - } - catch(...) - { - return opt; - } -} -template -cxx::enable_if_t, - basic_value>::value, cxx::remove_cvref_t> -get_or(basic_value&& v, T&& opt) noexcept -{ - try - { - return get>(std::move(v)); - } - catch(...) - { - return cxx::remove_cvref_t(std::forward(opt)); - } -} - -// ---------------------------------------------------------------------------- -// specialization for string literal - -// template -// typename basic_value::string_type -// get_or(const basic_value& v, -// const typename basic_value::string_type::value_type (&opt)[N]) -// { -// try -// { -// return v.as_string(); -// } -// catch(...) -// { -// return typename basic_value::string_type(opt); -// } -// } -// -// The above only matches to the literal, like `get_or(v, "foo");` but not -// ```cpp -// const auto opt = "foo"; -// const auto str = get_or(v, opt); -// ``` -// . And the latter causes an error. -// To match to both `"foo"` and `const auto opt = "foo"`, we take a pointer to -// a character here. - -template -typename basic_value::string_type -get_or(const basic_value& v, - const typename basic_value::string_type::value_type* opt) -{ - try - { - return v.as_string(); - } - catch(...) - { - return typename basic_value::string_type(opt); - } -} - -// ---------------------------------------------------------------------------- -// others (require type conversion and return type cannot be lvalue reference) - -template -cxx::enable_if_t>, - cxx::negation>>, - cxx::negation, typename basic_value::string_type::value_type const*>> - >::value, cxx::remove_cvref_t> -get_or(const basic_value& v, T&& opt) -{ - try - { - return get>(v); - } - catch(...) - { - return cxx::remove_cvref_t(std::forward(opt)); - } -} - -} // toml -#endif // TOML11_GET_HPP diff --git a/include/toml11/impl/color_impl.hpp b/include/toml11/impl/color_impl.hpp deleted file mode 100644 index 4215587..0000000 --- a/include/toml11/impl/color_impl.hpp +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef TOML11_COLOR_IMPL_HPP -#define TOML11_COLOR_IMPL_HPP - -#include "../fwd/color_fwd.hpp" -#include "../version.hpp" - -#include - -namespace toml -{ -namespace color -{ -// put ANSI escape sequence to ostream -inline namespace ansi -{ - -TOML11_INLINE std::ostream& reset(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[00m";} - return os; -} -TOML11_INLINE std::ostream& bold(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[01m";} - return os; -} -TOML11_INLINE std::ostream& grey(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[30m";} - return os; -} -TOML11_INLINE std::ostream& gray(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[30m";} - return os; -} -TOML11_INLINE std::ostream& red(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[31m";} - return os; -} -TOML11_INLINE std::ostream& green(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[32m";} - return os; -} -TOML11_INLINE std::ostream& yellow(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[33m";} - return os; -} -TOML11_INLINE std::ostream& blue(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[34m";} - return os; -} -TOML11_INLINE std::ostream& magenta(std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[35m";} - return os; -} -TOML11_INLINE std::ostream& cyan (std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[36m";} - return os; -} -TOML11_INLINE std::ostream& white (std::ostream& os) -{ - if(detail::color_status().should_color()) {os << "\033[37m";} - return os; -} - -} // ansi -} // color -} // toml -#endif // TOML11_COLOR_IMPL_HPP diff --git a/include/toml11/impl/comments_impl.hpp b/include/toml11/impl/comments_impl.hpp deleted file mode 100644 index 25023eb..0000000 --- a/include/toml11/impl/comments_impl.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef TOML11_COMMENTS_IMPL_HPP -#define TOML11_COMMENTS_IMPL_HPP - -#include "../fwd/comments_fwd.hpp" // IWYU pragma: keep - -namespace toml -{ - -TOML11_INLINE bool operator==(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments == rhs.comments;} -TOML11_INLINE bool operator!=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments != rhs.comments;} -TOML11_INLINE bool operator< (const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments < rhs.comments;} -TOML11_INLINE bool operator<=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments <= rhs.comments;} -TOML11_INLINE bool operator> (const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments > rhs.comments;} -TOML11_INLINE bool operator>=(const preserve_comments& lhs, const preserve_comments& rhs) {return lhs.comments >= rhs.comments;} - -TOML11_INLINE void swap(preserve_comments& lhs, preserve_comments& rhs) -{ - lhs.swap(rhs); - return; -} -TOML11_INLINE void swap(preserve_comments& lhs, std::vector& rhs) -{ - lhs.comments.swap(rhs); - return; -} -TOML11_INLINE void swap(std::vector& lhs, preserve_comments& rhs) -{ - lhs.swap(rhs.comments); - return; -} - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const preserve_comments& com) -{ - for(const auto& c : com) - { - if(c.front() != '#') - { - os << '#'; - } - os << c << '\n'; - } - return os; -} - -} // toml11 -#endif // TOML11_COMMENTS_IMPL_HPP diff --git a/include/toml11/impl/datetime_impl.hpp b/include/toml11/impl/datetime_impl.hpp deleted file mode 100644 index 50c0455..0000000 --- a/include/toml11/impl/datetime_impl.hpp +++ /dev/null @@ -1,518 +0,0 @@ -#ifndef TOML11_DATETIME_IMPL_HPP -#define TOML11_DATETIME_IMPL_HPP - -#include "../fwd/datetime_fwd.hpp" -#include "../version.hpp" - -#include -#include -#include -#include -#include - -#include -#include - -namespace toml -{ - -// To avoid non-threadsafe std::localtime. In C11 (not C++11!), localtime_s is -// provided in the absolutely same purpose, but C++11 is actually not compatible -// with C11. We need to dispatch the function depending on the OS. -namespace detail -{ -// TODO: find more sophisticated way to handle this -#if defined(_MSC_VER) -TOML11_INLINE std::tm localtime_s(const std::time_t* src) -{ - std::tm dst; - const auto result = ::localtime_s(&dst, src); - if (result) { throw std::runtime_error("localtime_s failed."); } - return dst; -} -TOML11_INLINE std::tm gmtime_s(const std::time_t* src) -{ - std::tm dst; - const auto result = ::gmtime_s(&dst, src); - if (result) { throw std::runtime_error("gmtime_s failed."); } - return dst; -} -#elif (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 1) || defined(_XOPEN_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_POSIX_SOURCE) -TOML11_INLINE std::tm localtime_s(const std::time_t* src) -{ - std::tm dst; - const auto result = ::localtime_r(src, &dst); - if (!result) { throw std::runtime_error("localtime_r failed."); } - return dst; -} -TOML11_INLINE std::tm gmtime_s(const std::time_t* src) -{ - std::tm dst; - const auto result = ::gmtime_r(src, &dst); - if (!result) { throw std::runtime_error("gmtime_r failed."); } - return dst; -} -#else // fallback. not threadsafe -TOML11_INLINE std::tm localtime_s(const std::time_t* src) -{ - const auto result = std::localtime(src); - if (!result) { throw std::runtime_error("localtime failed."); } - return *result; -} -TOML11_INLINE std::tm gmtime_s(const std::time_t* src) -{ - const auto result = std::gmtime(src); - if (!result) { throw std::runtime_error("gmtime failed."); } - return *result; -} -#endif -} // detail - -// ---------------------------------------------------------------------------- - -TOML11_INLINE local_date::local_date(const std::chrono::system_clock::time_point& tp) -{ - const auto t = std::chrono::system_clock::to_time_t(tp); - const auto time = detail::localtime_s(&t); - *this = local_date(time); -} - -TOML11_INLINE local_date::local_date(const std::time_t t) - : local_date{std::chrono::system_clock::from_time_t(t)} -{} - -TOML11_INLINE local_date::operator std::chrono::system_clock::time_point() const -{ - // std::mktime returns date as local time zone. no conversion needed - std::tm t; - t.tm_sec = 0; - t.tm_min = 0; - t.tm_hour = 0; - t.tm_mday = static_cast(this->day); - t.tm_mon = static_cast(this->month); - t.tm_year = static_cast(this->year) - 1900; - t.tm_wday = 0; // the value will be ignored - t.tm_yday = 0; // the value will be ignored - t.tm_isdst = -1; - return std::chrono::system_clock::from_time_t(std::mktime(&t)); -} - -TOML11_INLINE local_date::operator std::time_t() const -{ - return std::chrono::system_clock::to_time_t( - std::chrono::system_clock::time_point(*this)); -} - -TOML11_INLINE bool operator==(const local_date& lhs, const local_date& rhs) -{ - return std::make_tuple(lhs.year, lhs.month, lhs.day) == - std::make_tuple(rhs.year, rhs.month, rhs.day); -} -TOML11_INLINE bool operator!=(const local_date& lhs, const local_date& rhs) -{ - return !(lhs == rhs); -} -TOML11_INLINE bool operator< (const local_date& lhs, const local_date& rhs) -{ - return std::make_tuple(lhs.year, lhs.month, lhs.day) < - std::make_tuple(rhs.year, rhs.month, rhs.day); -} -TOML11_INLINE bool operator<=(const local_date& lhs, const local_date& rhs) -{ - return (lhs < rhs) || (lhs == rhs); -} -TOML11_INLINE bool operator> (const local_date& lhs, const local_date& rhs) -{ - return !(lhs <= rhs); -} -TOML11_INLINE bool operator>=(const local_date& lhs, const local_date& rhs) -{ - return !(lhs < rhs); -} - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const local_date& date) -{ - os << std::setfill('0') << std::setw(4) << static_cast(date.year ) << '-'; - os << std::setfill('0') << std::setw(2) << static_cast(date.month) + 1 << '-'; - os << std::setfill('0') << std::setw(2) << static_cast(date.day ) ; - return os; -} - -TOML11_INLINE std::string to_string(const local_date& date) -{ - std::ostringstream oss; - oss.imbue(std::locale::classic()); - oss << date; - return oss.str(); -} - -// ----------------------------------------------------------------------------- - -TOML11_INLINE local_time::operator std::chrono::nanoseconds() const -{ - return std::chrono::nanoseconds (this->nanosecond) + - std::chrono::microseconds(this->microsecond) + - std::chrono::milliseconds(this->millisecond) + - std::chrono::seconds(this->second) + - std::chrono::minutes(this->minute) + - std::chrono::hours(this->hour); -} - -TOML11_INLINE bool operator==(const local_time& lhs, const local_time& rhs) -{ - return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) == - std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond); -} -TOML11_INLINE bool operator!=(const local_time& lhs, const local_time& rhs) -{ - return !(lhs == rhs); -} -TOML11_INLINE bool operator< (const local_time& lhs, const local_time& rhs) -{ - return std::make_tuple(lhs.hour, lhs.minute, lhs.second, lhs.millisecond, lhs.microsecond, lhs.nanosecond) < - std::make_tuple(rhs.hour, rhs.minute, rhs.second, rhs.millisecond, rhs.microsecond, rhs.nanosecond); -} -TOML11_INLINE bool operator<=(const local_time& lhs, const local_time& rhs) -{ - return (lhs < rhs) || (lhs == rhs); -} -TOML11_INLINE bool operator> (const local_time& lhs, const local_time& rhs) -{ - return !(lhs <= rhs); -} -TOML11_INLINE bool operator>=(const local_time& lhs, const local_time& rhs) -{ - return !(lhs < rhs); -} - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const local_time& time) -{ - os << std::setfill('0') << std::setw(2) << static_cast(time.hour ) << ':'; - os << std::setfill('0') << std::setw(2) << static_cast(time.minute) << ':'; - os << std::setfill('0') << std::setw(2) << static_cast(time.second); - if(time.millisecond != 0 || time.microsecond != 0 || time.nanosecond != 0) - { - os << '.'; - os << std::setfill('0') << std::setw(3) << static_cast(time.millisecond); - if(time.microsecond != 0 || time.nanosecond != 0) - { - os << std::setfill('0') << std::setw(3) << static_cast(time.microsecond); - if(time.nanosecond != 0) - { - os << std::setfill('0') << std::setw(3) << static_cast(time.nanosecond); - } - } - } - return os; -} - -TOML11_INLINE std::string to_string(const local_time& time) -{ - std::ostringstream oss; - oss.imbue(std::locale::classic()); - oss << time; - return oss.str(); -} - -// ---------------------------------------------------------------------------- - -TOML11_INLINE time_offset::operator std::chrono::minutes() const -{ - return std::chrono::minutes(this->minute) + - std::chrono::hours(this->hour); -} - -TOML11_INLINE bool operator==(const time_offset& lhs, const time_offset& rhs) -{ - return std::make_tuple(lhs.hour, lhs.minute) == - std::make_tuple(rhs.hour, rhs.minute); -} -TOML11_INLINE bool operator!=(const time_offset& lhs, const time_offset& rhs) -{ - return !(lhs == rhs); -} -TOML11_INLINE bool operator< (const time_offset& lhs, const time_offset& rhs) -{ - return std::make_tuple(lhs.hour, lhs.minute) < - std::make_tuple(rhs.hour, rhs.minute); -} -TOML11_INLINE bool operator<=(const time_offset& lhs, const time_offset& rhs) -{ - return (lhs < rhs) || (lhs == rhs); -} -TOML11_INLINE bool operator> (const time_offset& lhs, const time_offset& rhs) -{ - return !(lhs <= rhs); -} -TOML11_INLINE bool operator>=(const time_offset& lhs, const time_offset& rhs) -{ - return !(lhs < rhs); -} - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const time_offset& offset) -{ - if(offset.hour == 0 && offset.minute == 0) - { - os << 'Z'; - return os; - } - int minute = static_cast(offset.hour) * 60 + offset.minute; - if(minute < 0){os << '-'; minute = std::abs(minute);} else {os << '+';} - os << std::setfill('0') << std::setw(2) << minute / 60 << ':'; - os << std::setfill('0') << std::setw(2) << minute % 60; - return os; -} - -TOML11_INLINE std::string to_string(const time_offset& offset) -{ - std::ostringstream oss; - oss.imbue(std::locale::classic()); - oss << offset; - return oss.str(); -} - -// ----------------------------------------------------------------------------- - -TOML11_INLINE local_datetime::local_datetime(const std::chrono::system_clock::time_point& tp) -{ - const auto t = std::chrono::system_clock::to_time_t(tp); - std::tm ltime = detail::localtime_s(&t); - - this->date = local_date(ltime); - this->time = local_time(ltime); - - // std::tm lacks subsecond information, so diff between tp and tm - // can be used to get millisecond & microsecond information. - const auto t_diff = tp - - std::chrono::system_clock::from_time_t(std::mktime(<ime)); - this->time.millisecond = static_cast( - std::chrono::duration_cast(t_diff).count()); - this->time.microsecond = static_cast( - std::chrono::duration_cast(t_diff).count()); - this->time.nanosecond = static_cast( - std::chrono::duration_cast(t_diff).count()); -} - -TOML11_INLINE local_datetime::local_datetime(const std::time_t t) - : local_datetime{std::chrono::system_clock::from_time_t(t)} -{} - -TOML11_INLINE local_datetime::operator std::chrono::system_clock::time_point() const -{ - using internal_duration = - typename std::chrono::system_clock::time_point::duration; - - // Normally DST begins at A.M. 3 or 4. If we re-use conversion operator - // of local_date and local_time independently, the conversion fails if - // it is the day when DST begins or ends. Since local_date considers the - // time is 00:00 A.M. and local_time does not consider DST because it - // does not have any date information. We need to consider both date and - // time information at the same time to convert it correctly. - - std::tm t; - t.tm_sec = static_cast(this->time.second); - t.tm_min = static_cast(this->time.minute); - t.tm_hour = static_cast(this->time.hour); - t.tm_mday = static_cast(this->date.day); - t.tm_mon = static_cast(this->date.month); - t.tm_year = static_cast(this->date.year) - 1900; - t.tm_wday = 0; // the value will be ignored - t.tm_yday = 0; // the value will be ignored - t.tm_isdst = -1; - - // std::mktime returns date as local time zone. no conversion needed - auto dt = std::chrono::system_clock::from_time_t(std::mktime(&t)); - dt += std::chrono::duration_cast( - std::chrono::milliseconds(this->time.millisecond) + - std::chrono::microseconds(this->time.microsecond) + - std::chrono::nanoseconds (this->time.nanosecond)); - return dt; -} - -TOML11_INLINE local_datetime::operator std::time_t() const -{ - return std::chrono::system_clock::to_time_t( - std::chrono::system_clock::time_point(*this)); -} - -TOML11_INLINE bool operator==(const local_datetime& lhs, const local_datetime& rhs) -{ - return std::make_tuple(lhs.date, lhs.time) == - std::make_tuple(rhs.date, rhs.time); -} -TOML11_INLINE bool operator!=(const local_datetime& lhs, const local_datetime& rhs) -{ - return !(lhs == rhs); -} -TOML11_INLINE bool operator< (const local_datetime& lhs, const local_datetime& rhs) -{ - return std::make_tuple(lhs.date, lhs.time) < - std::make_tuple(rhs.date, rhs.time); -} -TOML11_INLINE bool operator<=(const local_datetime& lhs, const local_datetime& rhs) -{ - return (lhs < rhs) || (lhs == rhs); -} -TOML11_INLINE bool operator> (const local_datetime& lhs, const local_datetime& rhs) -{ - return !(lhs <= rhs); -} -TOML11_INLINE bool operator>=(const local_datetime& lhs, const local_datetime& rhs) -{ - return !(lhs < rhs); -} - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const local_datetime& dt) -{ - os << dt.date << 'T' << dt.time; - return os; -} - -TOML11_INLINE std::string to_string(const local_datetime& dt) -{ - std::ostringstream oss; - oss.imbue(std::locale::classic()); - oss << dt; - return oss.str(); -} - -// ----------------------------------------------------------------------------- - - -TOML11_INLINE offset_datetime::offset_datetime(const local_datetime& ld) - : date{ld.date}, time{ld.time}, offset{get_local_offset(nullptr)} - // use the current local timezone offset -{} -TOML11_INLINE offset_datetime::offset_datetime(const std::chrono::system_clock::time_point& tp) - : offset{0, 0} // use gmtime -{ - const auto timet = std::chrono::system_clock::to_time_t(tp); - const auto tm = detail::gmtime_s(&timet); - this->date = local_date(tm); - this->time = local_time(tm); -} -TOML11_INLINE offset_datetime::offset_datetime(const std::time_t& t) - : offset{0, 0} // use gmtime -{ - const auto tm = detail::gmtime_s(&t); - this->date = local_date(tm); - this->time = local_time(tm); -} -TOML11_INLINE offset_datetime::offset_datetime(const std::tm& t) - : offset{0, 0} // assume gmtime -{ - this->date = local_date(t); - this->time = local_time(t); -} - -TOML11_INLINE offset_datetime::operator std::chrono::system_clock::time_point() const -{ - // get date-time - using internal_duration = - typename std::chrono::system_clock::time_point::duration; - - // first, convert it to local date-time information in the same way as - // local_datetime does. later we will use time_t to adjust time offset. - std::tm t; - t.tm_sec = static_cast(this->time.second); - t.tm_min = static_cast(this->time.minute); - t.tm_hour = static_cast(this->time.hour); - t.tm_mday = static_cast(this->date.day); - t.tm_mon = static_cast(this->date.month); - t.tm_year = static_cast(this->date.year) - 1900; - t.tm_wday = 0; // the value will be ignored - t.tm_yday = 0; // the value will be ignored - t.tm_isdst = -1; - const std::time_t tp_loc = std::mktime(std::addressof(t)); - - auto tp = std::chrono::system_clock::from_time_t(tp_loc); - tp += std::chrono::duration_cast( - std::chrono::milliseconds(this->time.millisecond) + - std::chrono::microseconds(this->time.microsecond) + - std::chrono::nanoseconds (this->time.nanosecond)); - - // Since mktime uses local time zone, it should be corrected. - // `12:00:00+09:00` means `03:00:00Z`. So mktime returns `03:00:00Z` if - // we are in `+09:00` timezone. To represent `12:00:00Z` there, we need - // to add `+09:00` to `03:00:00Z`. - // Here, it uses the time_t converted from date-time info to handle - // daylight saving time. - const auto ofs = get_local_offset(std::addressof(tp_loc)); - tp += std::chrono::hours (ofs.hour); - tp += std::chrono::minutes(ofs.minute); - - // We got `12:00:00Z` by correcting local timezone applied by mktime. - // Then we will apply the offset. Let's say `12:00:00-08:00` is given. - // And now, we have `12:00:00Z`. `12:00:00-08:00` means `20:00:00Z`. - // So we need to subtract the offset. - tp -= std::chrono::minutes(this->offset); - return tp; -} - -TOML11_INLINE offset_datetime::operator std::time_t() const -{ - return std::chrono::system_clock::to_time_t( - std::chrono::system_clock::time_point(*this)); -} - -TOML11_INLINE time_offset offset_datetime::get_local_offset(const std::time_t* tp) -{ - // get local timezone with the same date-time information as mktime - const auto t = detail::localtime_s(tp); - - std::array buf; - const auto result = std::strftime(buf.data(), 6, "%z", &t); // +hhmm\0 - if(result != 5) - { - throw std::runtime_error("toml::offset_datetime: cannot obtain " - "timezone information of current env"); - } - const int ofs = std::atoi(buf.data()); - const int ofs_h = ofs / 100; - const int ofs_m = ofs - (ofs_h * 100); - return time_offset(ofs_h, ofs_m); -} - -TOML11_INLINE bool operator==(const offset_datetime& lhs, const offset_datetime& rhs) -{ - return std::make_tuple(lhs.date, lhs.time, lhs.offset) == - std::make_tuple(rhs.date, rhs.time, rhs.offset); -} -TOML11_INLINE bool operator!=(const offset_datetime& lhs, const offset_datetime& rhs) -{ - return !(lhs == rhs); -} -TOML11_INLINE bool operator< (const offset_datetime& lhs, const offset_datetime& rhs) -{ - return std::make_tuple(lhs.date, lhs.time, lhs.offset) < - std::make_tuple(rhs.date, rhs.time, rhs.offset); -} -TOML11_INLINE bool operator<=(const offset_datetime& lhs, const offset_datetime& rhs) -{ - return (lhs < rhs) || (lhs == rhs); -} -TOML11_INLINE bool operator> (const offset_datetime& lhs, const offset_datetime& rhs) -{ - return !(lhs <= rhs); -} -TOML11_INLINE bool operator>=(const offset_datetime& lhs, const offset_datetime& rhs) -{ - return !(lhs < rhs); -} - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const offset_datetime& dt) -{ - os << dt.date << 'T' << dt.time << dt.offset; - return os; -} - -TOML11_INLINE std::string to_string(const offset_datetime& dt) -{ - std::ostringstream oss; - oss.imbue(std::locale::classic()); - oss << dt; - return oss.str(); -} - -}//toml -#endif // TOML11_DATETIME_IMPL_HPP diff --git a/include/toml11/impl/error_info_impl.hpp b/include/toml11/impl/error_info_impl.hpp deleted file mode 100644 index b87f477..0000000 --- a/include/toml11/impl/error_info_impl.hpp +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef TOML11_ERROR_INFO_IMPL_HPP -#define TOML11_ERROR_INFO_IMPL_HPP - -#include "../fwd/error_info_fwd.hpp" -#include "../fwd/color_fwd.hpp" - -#include - -namespace toml -{ - -TOML11_INLINE std::string format_error(const std::string& errkind, const error_info& err) -{ - std::string errmsg; - if( ! errkind.empty()) - { - errmsg = errkind; - errmsg += ' '; - } - errmsg += err.title(); - errmsg += '\n'; - - const auto lnw = [&err]() { - std::size_t width = 0; - for(const auto& l : err.locations()) - { - width = (std::max)(detail::integer_width_base10(l.first.last_line_number()), width); - } - return width; - }(); - - bool first = true; - std::string prev_fname; - for(const auto& lm : err.locations()) - { - if( ! first) - { - std::ostringstream oss; - oss << detail::make_string(lnw + 1, ' ') - << color::bold << color::blue << " |" << color::reset - << color::bold << " ...\n" << color::reset; - oss << detail::make_string(lnw + 1, ' ') - << color::bold << color::blue << " |\n" << color::reset; - errmsg += oss.str(); - } - - const auto& l = lm.first; - const auto& m = lm.second; - - errmsg += detail::format_location_impl(lnw, prev_fname, l, m); - - prev_fname = l.file_name(); - first = false; - } - - errmsg += err.suffix(); - - return errmsg; -} - -TOML11_INLINE std::string format_error(const error_info& err) -{ - std::ostringstream oss; - oss << color::red << color::bold << "[error]" << color::reset; - return format_error(oss.str(), err); -} - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const error_info& e) -{ - os << format_error(e); - return os; -} - -} // toml -#endif // TOML11_ERROR_INFO_IMPL_HPP diff --git a/include/toml11/impl/format_impl.hpp b/include/toml11/impl/format_impl.hpp deleted file mode 100644 index c4985fa..0000000 --- a/include/toml11/impl/format_impl.hpp +++ /dev/null @@ -1,297 +0,0 @@ -#ifndef TOML11_FORMAT_IMPL_HPP -#define TOML11_FORMAT_IMPL_HPP - -#include "../fwd/format_fwd.hpp" -#include "../version.hpp" - -#include -#include - -namespace toml -{ - -// toml types with serialization info - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const indent_char& c) -{ - switch(c) - { - case indent_char::space: {os << "space" ; break;} - case indent_char::tab: {os << "tab" ; break;} - case indent_char::none: {os << "none" ; break;} - default: - { - os << "unknown indent char: " << static_cast(c); - } - } - return os; -} - -TOML11_INLINE std::string to_string(const indent_char c) -{ - std::ostringstream oss; - oss << c; - return oss.str(); -} - -// ---------------------------------------------------------------------------- -// boolean - -// ---------------------------------------------------------------------------- -// integer - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const integer_format f) -{ - switch(f) - { - case integer_format::dec: {os << "dec"; break;} - case integer_format::bin: {os << "bin"; break;} - case integer_format::oct: {os << "oct"; break;} - case integer_format::hex: {os << "hex"; break;} - default: - { - os << "unknown integer_format: " << static_cast(f); - break; - } - } - return os; -} -TOML11_INLINE std::string to_string(const integer_format c) -{ - std::ostringstream oss; - oss << c; - return oss.str(); -} - - -TOML11_INLINE bool operator==(const integer_format_info& lhs, const integer_format_info& rhs) noexcept -{ - return lhs.fmt == rhs.fmt && - lhs.uppercase == rhs.uppercase && - lhs.width == rhs.width && - lhs.spacer == rhs.spacer && - lhs.suffix == rhs.suffix ; -} -TOML11_INLINE bool operator!=(const integer_format_info& lhs, const integer_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -// ---------------------------------------------------------------------------- -// floating - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const floating_format f) -{ - switch(f) - { - case floating_format::defaultfloat: {os << "defaultfloat"; break;} - case floating_format::fixed : {os << "fixed" ; break;} - case floating_format::scientific : {os << "scientific" ; break;} - case floating_format::hex : {os << "hex" ; break;} - default: - { - os << "unknown floating_format: " << static_cast(f); - break; - } - } - return os; -} -TOML11_INLINE std::string to_string(const floating_format c) -{ - std::ostringstream oss; - oss << c; - return oss.str(); -} - -TOML11_INLINE bool operator==(const floating_format_info& lhs, const floating_format_info& rhs) noexcept -{ - return lhs.fmt == rhs.fmt && - lhs.prec == rhs.prec && - lhs.suffix == rhs.suffix ; -} -TOML11_INLINE bool operator!=(const floating_format_info& lhs, const floating_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -// ---------------------------------------------------------------------------- -// string - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const string_format f) -{ - switch(f) - { - case string_format::basic : {os << "basic" ; break;} - case string_format::literal : {os << "literal" ; break;} - case string_format::multiline_basic : {os << "multiline_basic" ; break;} - case string_format::multiline_literal: {os << "multiline_literal"; break;} - default: - { - os << "unknown string_format: " << static_cast(f); - break; - } - } - return os; -} -TOML11_INLINE std::string to_string(const string_format c) -{ - std::ostringstream oss; - oss << c; - return oss.str(); -} - -TOML11_INLINE bool operator==(const string_format_info& lhs, const string_format_info& rhs) noexcept -{ - return lhs.fmt == rhs.fmt && - lhs.start_with_newline == rhs.start_with_newline ; -} -TOML11_INLINE bool operator!=(const string_format_info& lhs, const string_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} -// ---------------------------------------------------------------------------- -// datetime - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const datetime_delimiter_kind d) -{ - switch(d) - { - case datetime_delimiter_kind::upper_T: { os << "upper_T, "; break; } - case datetime_delimiter_kind::lower_t: { os << "lower_t, "; break; } - case datetime_delimiter_kind::space: { os << "space, "; break; } - default: - { - os << "unknown datetime delimiter: " << static_cast(d); - break; - } - } - return os; -} -TOML11_INLINE std::string to_string(const datetime_delimiter_kind c) -{ - std::ostringstream oss; - oss << c; - return oss.str(); -} - -TOML11_INLINE bool operator==(const offset_datetime_format_info& lhs, const offset_datetime_format_info& rhs) noexcept -{ - return lhs.delimiter == rhs.delimiter && - lhs.has_seconds == rhs.has_seconds && - lhs.subsecond_precision == rhs.subsecond_precision ; -} -TOML11_INLINE bool operator!=(const offset_datetime_format_info& lhs, const offset_datetime_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -TOML11_INLINE bool operator==(const local_datetime_format_info& lhs, const local_datetime_format_info& rhs) noexcept -{ - return lhs.delimiter == rhs.delimiter && - lhs.has_seconds == rhs.has_seconds && - lhs.subsecond_precision == rhs.subsecond_precision ; -} -TOML11_INLINE bool operator!=(const local_datetime_format_info& lhs, const local_datetime_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -TOML11_INLINE bool operator==(const local_date_format_info&, const local_date_format_info&) noexcept -{ - return true; -} -TOML11_INLINE bool operator!=(const local_date_format_info& lhs, const local_date_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -TOML11_INLINE bool operator==(const local_time_format_info& lhs, const local_time_format_info& rhs) noexcept -{ - return lhs.has_seconds == rhs.has_seconds && - lhs.subsecond_precision == rhs.subsecond_precision ; -} -TOML11_INLINE bool operator!=(const local_time_format_info& lhs, const local_time_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -// ---------------------------------------------------------------------------- -// array - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const array_format f) -{ - switch(f) - { - case array_format::default_format : {os << "default_format" ; break;} - case array_format::oneline : {os << "oneline" ; break;} - case array_format::multiline : {os << "multiline" ; break;} - case array_format::array_of_tables: {os << "array_of_tables"; break;} - default: - { - os << "unknown array_format: " << static_cast(f); - break; - } - } - return os; -} -TOML11_INLINE std::string to_string(const array_format c) -{ - std::ostringstream oss; - oss << c; - return oss.str(); -} - -TOML11_INLINE bool operator==(const array_format_info& lhs, const array_format_info& rhs) noexcept -{ - return lhs.fmt == rhs.fmt && - lhs.indent_type == rhs.indent_type && - lhs.body_indent == rhs.body_indent && - lhs.closing_indent == rhs.closing_indent ; -} -TOML11_INLINE bool operator!=(const array_format_info& lhs, const array_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -// ---------------------------------------------------------------------------- -// table - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, const table_format f) -{ - switch(f) - { - case table_format::multiline : {os << "multiline" ; break;} - case table_format::oneline : {os << "oneline" ; break;} - case table_format::dotted : {os << "dotted" ; break;} - case table_format::multiline_oneline: {os << "multiline_oneline"; break;} - case table_format::implicit : {os << "implicit" ; break;} - default: - { - os << "unknown table_format: " << static_cast(f); - break; - } - } - return os; -} -TOML11_INLINE std::string to_string(const table_format c) -{ - std::ostringstream oss; - oss << c; - return oss.str(); -} - -TOML11_INLINE bool operator==(const table_format_info& lhs, const table_format_info& rhs) noexcept -{ - return lhs.fmt == rhs.fmt && - lhs.indent_type == rhs.indent_type && - lhs.body_indent == rhs.body_indent && - lhs.name_indent == rhs.name_indent && - lhs.closing_indent == rhs.closing_indent ; -} -TOML11_INLINE bool operator!=(const table_format_info& lhs, const table_format_info& rhs) noexcept -{ - return !(lhs == rhs); -} - -} // namespace toml -#endif // TOML11_FORMAT_IMPL_HPP diff --git a/include/toml11/impl/literal_impl.hpp b/include/toml11/impl/literal_impl.hpp deleted file mode 100644 index e8298c2..0000000 --- a/include/toml11/impl/literal_impl.hpp +++ /dev/null @@ -1,174 +0,0 @@ -#ifndef TOML11_LITERAL_IMPL_HPP -#define TOML11_LITERAL_IMPL_HPP - -#include "../fwd/literal_fwd.hpp" -#include "../parser.hpp" -#include "../syntax.hpp" - -namespace toml -{ - -namespace detail -{ -// implementation -TOML11_INLINE ::toml::value literal_internal_impl(location loc) -{ - const auto s = ::toml::spec::default_version(); - context ctx(s); - - const auto front = loc; - - // ------------------------------------------------------------------------ - // check if it is a raw value. - - // skip empty lines and comment lines - auto sp = skip_multiline_spacer(loc, ctx); - if(loc.eof()) - { - ::toml::value val; - if(sp.has_value()) - { - for(std::size_t i=0; i(str), - reinterpret_cast(str + len), - c.begin()); - if( ! c.empty() && c.back()) - { - c.push_back('\n'); // to make it easy to parse comment, we add newline - } - - return literal_internal_impl(::toml::detail::location( - std::make_shared(std::move(c)), - "TOML literal encoded in a C++ code")); -} - -#if defined(__cpp_char8_t) -# if __cpp_char8_t >= 201811L -# define TOML11_HAS_CHAR8_T 1 -# endif -#endif - -#if defined(TOML11_HAS_CHAR8_T) -// value of u8"" literal has been changed from char to char8_t and char8_t is -// NOT compatible to char -TOML11_INLINE ::toml::value -operator"" _toml(const char8_t* str, std::size_t len) -{ - if(len == 0) - { - return ::toml::value{}; - } - - ::toml::detail::location::container_type c(len); - std::copy(reinterpret_cast(str), - reinterpret_cast(str + len), - c.begin()); - if( ! c.empty() && c.back()) - { - c.push_back('\n'); // to make it easy to parse comment, we add newline - } - - return literal_internal_impl(::toml::detail::location( - std::make_shared(std::move(c)), - "TOML literal encoded in a C++ code")); -} -#endif - -} // toml_literals -} // literals -} // toml -#endif // TOML11_LITERAL_IMPL_HPP diff --git a/include/toml11/impl/location_impl.hpp b/include/toml11/impl/location_impl.hpp deleted file mode 100644 index b996ab6..0000000 --- a/include/toml11/impl/location_impl.hpp +++ /dev/null @@ -1,209 +0,0 @@ -#ifndef TOML11_LOCATION_IMPL_HPP -#define TOML11_LOCATION_IMPL_HPP - -#include "../fwd/location_fwd.hpp" -#include "../utility.hpp" -#include "../version.hpp" - -namespace toml -{ -namespace detail -{ - -TOML11_INLINE void location::advance(std::size_t n) noexcept -{ - assert(this->is_ok()); - if(this->location_ + n < this->source_->size()) - { - this->advance_impl(n); - } - else - { - this->advance_impl(this->source_->size() - this->location_); - - assert(this->location_ == this->source_->size()); - } -} -TOML11_INLINE void location::retrace(/*restricted to n=1*/) noexcept -{ - assert(this->is_ok()); - if(this->location_ == 0) - { - this->location_ = 0; - this->line_number_ = 1; - this->column_number_ = 1; - } - else - { - this->retrace_impl(); - } -} - -TOML11_INLINE bool location::eof() const noexcept -{ - assert(this->is_ok()); - return this->location_ >= this->source_->size(); -} -TOML11_INLINE location::char_type location::current() const -{ - assert(this->is_ok()); - if(this->eof()) {return '\0';} - - assert(this->location_ < this->source_->size()); - return this->source_->at(this->location_); -} - -TOML11_INLINE location::char_type location::peek() -{ - assert(this->is_ok()); - if(this->location_ >= this->source_->size()) - { - return '\0'; - } - else - { - return this->source_->at(this->location_ + 1); - } -} - -TOML11_INLINE std::string location::get_line() const -{ - assert(this->is_ok()); - const auto iter = std::next(this->source_->cbegin(), static_cast(this->location_)); - const auto riter = cxx::make_reverse_iterator(iter); - - const auto prev = std::find(riter, this->source_->crend(), char_type('\n')); - const auto next = std::find(iter, this->source_->cend(), char_type('\n')); - - return make_string(std::next(prev.base()), next); -} - -TOML11_INLINE std::size_t location::calc_column_number() const noexcept -{ - assert(this->is_ok()); - const auto iter = std::next(this->source_->cbegin(), static_cast(this->location_)); - const auto riter = cxx::make_reverse_iterator(iter); - const auto prev = std::find(riter, this->source_->crend(), char_type('\n')); - - assert(prev.base() <= iter); - return static_cast(std::distance(prev.base(), iter) + 1); // 1-origin -} - -TOML11_INLINE void location::advance_impl(const std::size_t n) -{ - assert(this->is_ok()); - assert(this->location_ + n <= this->source_->size()); - - auto iter = this->source_->cbegin(); - std::advance(iter, static_cast(this->location_)); - - for(std::size_t i=0; iline_number_ += 1; - this->column_number_ = 1; - } - else - { - this->column_number_ += 1; - } - iter++; - } - this->location_ += n; - return; -} -TOML11_INLINE void location::retrace_impl(/*n == 1*/) -{ - assert(this->is_ok()); - assert(this->location_ != 0); - - this->location_ -= 1; - - auto iter = this->source_->cbegin(); - std::advance(iter, static_cast(this->location_)); - if(*iter == '\n') - { - this->line_number_ -= 1; - this->column_number_ = this->calc_column_number(); - } - return; -} - -TOML11_INLINE bool operator==(const location& lhs, const location& rhs) noexcept -{ - if( ! lhs.is_ok() || ! rhs.is_ok()) - { - return (!lhs.is_ok()) && (!rhs.is_ok()); - } - return lhs.source() == rhs.source() && - lhs.source_name() == rhs.source_name() && - lhs.get_location() == rhs.get_location(); -} -TOML11_INLINE bool operator!=(const location& lhs, const location& rhs) -{ - return !(lhs == rhs); -} - -TOML11_INLINE location prev(const location& loc) -{ - location p(loc); - p.retrace(); - return p; -} -TOML11_INLINE location next(const location& loc) -{ - location p(loc); - p.advance(1); - return p; -} - -TOML11_INLINE location make_temporary_location(const std::string& str) noexcept -{ - location::container_type cont(str.size()); - std::transform(str.begin(), str.end(), cont.begin(), - [](const std::string::value_type& c) { - return cxx::bit_cast(c); - }); - return location(std::make_shared( - std::move(cont)), "internal temporary"); -} - -TOML11_INLINE result -find(const location& first, const location& last, const location::char_type val) -{ - return find_if(first, last, [val](const location::char_type c) { - return c == val; - }); -} -TOML11_INLINE result -rfind(const location& first, const location& last, const location::char_type val) -{ - return rfind_if(first, last, [val](const location::char_type c) { - return c == val; - }); -} - -TOML11_INLINE std::size_t -count(const location& first, const location& last, const location::char_type& c) -{ - if(first.source() != last.source()) { return 0; } - if(first.get_location() >= last.get_location()) { return 0; } - - auto loc = first; - std::size_t num = 0; - while(loc.get_location() != last.get_location()) - { - if(loc.current() == c) - { - num += 1; - } - loc.advance(); - } - return num; -} - -} // detail -} // toml -#endif // TOML11_LOCATION_HPP diff --git a/include/toml11/impl/region_impl.hpp b/include/toml11/impl/region_impl.hpp deleted file mode 100644 index 28242c8..0000000 --- a/include/toml11/impl/region_impl.hpp +++ /dev/null @@ -1,246 +0,0 @@ -#ifndef TOML11_REGION_IMPL_HPP -#define TOML11_REGION_IMPL_HPP - -#include "../fwd/region_fwd.hpp" -#include "../utility.hpp" - -#include -#include -#include -#include -#include -#include - -namespace toml -{ -namespace detail -{ - -// a value defined in [first, last). -// Those source must be the same. Instread, `region` does not make sense. -TOML11_INLINE region::region(const location& first, const location& last) - : source_(first.source()), source_name_(first.source_name()), - length_(last.get_location() - first.get_location()), - first_(first.get_location()), - first_line_(first.line_number()), - first_column_(first.column_number()), - last_(last.get_location()), - last_line_(last.line_number()), - last_column_(last.column_number()) -{ - assert(first.source() == last.source()); - assert(first.source_name() == last.source_name()); -} - - // shorthand of [loc, loc+1) -TOML11_INLINE region::region(const location& loc) - : source_(loc.source()), source_name_(loc.source_name()), length_(0), - first_line_(0), first_column_(0), last_line_(0), last_column_(0) -{ - // if the file ends with LF, the resulting region points no char. - if(loc.eof()) - { - if(loc.get_location() == 0) - { - this->length_ = 0; - this->first_ = 0; - this->first_line_ = 0; - this->first_column_ = 0; - this->last_ = 0; - this->last_line_ = 0; - this->last_column_ = 0; - } - else - { - const auto first = prev(loc); - this->first_ = first.get_location(); - this->first_line_ = first.line_number(); - this->first_column_ = first.column_number(); - this->last_ = loc.get_location(); - this->last_line_ = loc.line_number(); - this->last_column_ = loc.column_number(); - this->length_ = 1; - } - } - else - { - this->first_ = loc.get_location(); - this->first_line_ = loc.line_number(); - this->first_column_ = loc.column_number(); - this->last_ = loc.get_location() + 1; - this->last_line_ = loc.line_number(); - this->last_column_ = loc.column_number() + 1; - this->length_ = 1; - } -} - -TOML11_INLINE region::char_type region::at(std::size_t i) const -{ - if(this->last_ <= this->first_ + i) - { - throw std::out_of_range("range::at: index " + std::to_string(i) + - " exceeds length " + std::to_string(this->length_)); - } - const auto iter = std::next(this->source_->cbegin(), - static_cast(this->first_ + i)); - return *iter; -} - -TOML11_INLINE region::const_iterator region::begin() const noexcept -{ - return std::next(this->source_->cbegin(), - static_cast(this->first_)); -} -TOML11_INLINE region::const_iterator region::end() const noexcept -{ - return std::next(this->source_->cbegin(), - static_cast(this->last_)); -} -TOML11_INLINE region::const_iterator region::cbegin() const noexcept -{ - return std::next(this->source_->cbegin(), - static_cast(this->first_)); -} -TOML11_INLINE region::const_iterator region::cend() const noexcept -{ - return std::next(this->source_->cbegin(), - static_cast(this->last_)); -} - -TOML11_INLINE std::string region::as_string() const -{ - if(this->is_ok()) - { - const auto begin = std::next(this->source_->cbegin(), static_cast(this->first_)); - const auto end = std::next(this->source_->cbegin(), static_cast(this->last_ )); - return ::toml::detail::make_string(begin, end); - } - else - { - return std::string(""); - } -} - -TOML11_INLINE std::pair -region::take_line(const_iterator begin, const_iterator end) const -{ - // To omit long line, we cap region by before/after 30 chars - const auto dist_before = std::distance(source_->cbegin(), begin); - const auto dist_after = std::distance(end, source_->cend()); - - const const_iterator capped_begin = (dist_before <= 30) ? source_->cbegin() : std::prev(begin, 30); - const const_iterator capped_end = (dist_after <= 30) ? source_->cend() : std::next(end, 30); - - const auto lf = char_type('\n'); - const auto lf_before = std::find(cxx::make_reverse_iterator(begin), - cxx::make_reverse_iterator(capped_begin), lf); - const auto lf_after = std::find(end, capped_end, lf); - - auto offset = static_cast(std::distance(lf_before.base(), begin)); - - std::string retval = make_string(lf_before.base(), lf_after); - - if(lf_before.base() != source_->cbegin() && *lf_before != lf) - { - retval = "... " + retval; - offset += 4; - } - - if(lf_after != source_->cend() && *lf_after != lf) - { - retval = retval + " ..."; - } - - return std::make_pair(retval, offset); -} - -TOML11_INLINE std::vector> region::as_lines() const -{ - assert(this->is_ok()); - if(this->length_ == 0) - { - return std::vector>{ - std::make_pair("", std::size_t(0)) - }; - } - - // Consider the following toml file - // ``` - // array = [ - // 1, 2, 3, - // ] # comment - // ``` - // and the region represnets - // ``` - // [ - // 1, 2, 3, - // ] - // ``` - // but we want to show the following. - // ``` - // array = [ - // 1, 2, 3, - // ] # comment - // ``` - // So we need to find LFs before `begin` and after `end`. - // - // But, if region ends with LF, it should not include the next line. - // ``` - // a = 42 - // ^^^- with the last LF - // ``` - // So we start from `end-1` when looking for LF. - - const auto begin_idx = static_cast(this->first_); - const auto end_idx = static_cast(this->last_) - 1; - - // length_ != 0, so begin < end. then begin <= end-1 - assert(begin_idx <= end_idx); - - const auto begin = std::next(this->source_->cbegin(), begin_idx); - const auto end = std::next(this->source_->cbegin(), end_idx); - - assert(this->first_line_number() <= this->last_line_number()); - - if(this->first_line_number() == this->last_line_number()) - { - return std::vector>{ - this->take_line(begin, end) - }; - } - - // we have multiple lines. `begin` and `end` points different lines. - // that means that there is at least one `LF` between `begin` and `end`. - - const auto after_begin = std::distance(begin, this->source_->cend()); - const auto before_end = std::distance(this->source_->cbegin(), end); - - const_iterator capped_file_end = this->source_->cend(); - const_iterator capped_file_begin = this->source_->cbegin(); - if(60 < after_begin) {capped_file_end = std::next(begin, 50);} - if(60 < before_end) {capped_file_begin = std::prev(end, 50);} - - const auto lf = char_type('\n'); - const auto first_line_end = std::find(begin, capped_file_end, lf); - const auto last_line_begin = std::find(capped_file_begin, end, lf); - - const auto first_line = this->take_line(begin, first_line_end); - const auto last_line = this->take_line(last_line_begin, end); - - if(this->first_line_number() + 1 == this->last_line_number()) - { - return std::vector>{ - first_line, last_line - }; - } - else - { - return std::vector>{ - first_line, std::make_pair("...", 0), last_line - }; - } -} - -} // namespace detail -} // namespace toml -#endif // TOML11_REGION_IMPL_HPP diff --git a/include/toml11/impl/scanner_impl.hpp b/include/toml11/impl/scanner_impl.hpp deleted file mode 100644 index d97d317..0000000 --- a/include/toml11/impl/scanner_impl.hpp +++ /dev/null @@ -1,473 +0,0 @@ -#ifndef TOML11_SCANNER_IMPL_HPP -#define TOML11_SCANNER_IMPL_HPP - -#include "../fwd/scanner_fwd.hpp" -#include "../utility.hpp" - -namespace toml -{ -namespace detail -{ - -TOML11_INLINE scanner_storage::scanner_storage(const scanner_storage& other) - : scanner_(nullptr) -{ - if(other.is_ok()) - { - scanner_.reset(other.get().clone()); - } -} -TOML11_INLINE scanner_storage& scanner_storage::operator=(const scanner_storage& other) -{ - if(this == std::addressof(other)) {return *this;} - if(other.is_ok()) - { - scanner_.reset(other.get().clone()); - } - return *this; -} - -TOML11_INLINE region scanner_storage::scan(location& loc) const -{ - assert(this->is_ok()); - return this->scanner_->scan(loc); -} - -TOML11_INLINE std::string scanner_storage::expected_chars(location& loc) const -{ - assert(this->is_ok()); - return this->scanner_->expected_chars(loc); -} - -TOML11_INLINE scanner_base& scanner_storage::get() const noexcept -{ - assert(this->is_ok()); - return *scanner_; -} - -TOML11_INLINE std::string scanner_storage::name() const -{ - assert(this->is_ok()); - return this->scanner_->name(); -} - -// ---------------------------------------------------------------------------- - -TOML11_INLINE region character::scan(location& loc) const -{ - if(loc.eof()) {return region{};} - - if(loc.current() == this->value_) - { - const auto first = loc; - loc.advance(1); - return region(first, loc); - } - return region{}; -} - -TOML11_INLINE std::string character::expected_chars(location&) const -{ - return show_char(value_); -} - -TOML11_INLINE scanner_base* character::clone() const -{ - return new character(*this); -} - -TOML11_INLINE std::string character::name() const -{ - return "character{" + show_char(value_) + "}"; -} - -// ---------------------------------------------------------------------------- - -TOML11_INLINE region character_either::scan(location& loc) const -{ - if(loc.eof()) {return region{};} - - for(const auto c : this->chars_) - { - if(loc.current() == c) - { - const auto first = loc; - loc.advance(1); - return region(first, loc); - } - } - return region{}; -} - -TOML11_INLINE std::string character_either::expected_chars(location&) const -{ - assert( ! chars_.empty()); - - std::string expected; - if(chars_.size() == 1) - { - expected += show_char(chars_.at(0)); - } - else if(chars_.size() == 2) - { - expected += show_char(chars_.at(0)) + " or " + show_char(chars_.at(1)); - } - else - { - for(std::size_t i=0; ichars_) - { - n += show_char(c); - n += ", "; - } - if( ! this->chars_.empty()) - { - n.pop_back(); - n.pop_back(); - } - n += "}"; - return n; -} - -// ---------------------------------------------------------------------------- -// character_in_range - -TOML11_INLINE region character_in_range::scan(location& loc) const -{ - if(loc.eof()) {return region{};} - - const auto curr = loc.current(); - if(this->from_ <= curr && curr <= this->to_) - { - const auto first = loc; - loc.advance(1); - return region(first, loc); - } - return region{}; -} - -TOML11_INLINE std::string character_in_range::expected_chars(location&) const -{ - std::string expected("from `"); - expected += show_char(from_); - expected += "` to `"; - expected += show_char(to_); - expected += "`"; - return expected; -} - -TOML11_INLINE scanner_base* character_in_range::clone() const -{ - return new character_in_range(*this); -} - -TOML11_INLINE std::string character_in_range::name() const -{ - return "character_in_range{" + show_char(from_) + "," + show_char(to_) + "}"; -} - -// ---------------------------------------------------------------------------- -// literal - -TOML11_INLINE region literal::scan(location& loc) const -{ - const auto first = loc; - for(std::size_t i=0; iothers_.empty()) - { - n.pop_back(); - n.pop_back(); - } - n += "}"; - return n; -} - -// ---------------------------------------------------------------------------- -// either - -TOML11_INLINE region either::scan(location& loc) const -{ - for(const auto& other : others_) - { - const auto reg = other.scan(loc); - if(reg.is_ok()) - { - return reg; - } - } - return region{}; -} - -TOML11_INLINE std::string either::expected_chars(location& loc) const -{ - assert( ! others_.empty()); - - std::string expected = others_.at(0).expected_chars(loc); - if(others_.size() == 2) - { - expected += " or "; - expected += others_.at(1).expected_chars(loc); - } - else - { - for(std::size_t i=1; iothers_.empty()) - { - n.pop_back(); - n.pop_back(); - } - n += "}"; - return n; -} - -// ---------------------------------------------------------------------------- -// repeat_exact - -TOML11_INLINE region repeat_exact::scan(location& loc) const -{ - const auto first = loc; - for(std::size_t i=0; i -#include -#include -#include - -#include - -namespace toml -{ - -TOML11_INLINE source_location::source_location(const detail::region& r) - : is_ok_(false), - first_line_(1), - first_column_(1), - first_offset_(1), - last_line_(1), - last_column_(1), - last_offset_(1), - length_(0), - file_name_("unknown file") -{ - if(r.is_ok()) - { - this->is_ok_ = true; - this->file_name_ = r.source_name(); - this->first_line_ = r.first_line_number(); - this->first_column_ = r.first_column_number(); - this->last_line_ = r.last_line_number(); - this->last_column_ = r.last_column_number(); - this->length_ = r.length(); - - const auto lines = r.as_lines(); - assert( ! lines.empty()); - - for(const auto& l : lines) - { - this->line_str_.push_back(l.first); - } - - this->first_offset_ = lines.at( 0).second + 1; // to 1-origin - this->last_offset_ = lines.at(lines.size()-1).second + 1; - } -} - -TOML11_INLINE std::string const& source_location::first_line() const -{ - if(this->line_str_.size() == 0) - { - throw std::out_of_range("toml::source_location::first_line: `lines` is empty"); - } - return this->line_str_.front(); -} -TOML11_INLINE std::string const& source_location::last_line() const -{ - if(this->line_str_.size() == 0) - { - throw std::out_of_range("toml::source_location::first_line: `lines` is empty"); - } - return this->line_str_.back(); -} - -namespace detail -{ - -TOML11_INLINE std::size_t integer_width_base10(std::size_t i) noexcept -{ - std::size_t width = 0; - while(i != 0) - { - i /= 10; - width += 1; - } - return width; -} - -TOML11_INLINE std::ostringstream& -format_filename(std::ostringstream& oss, const source_location& loc) -{ - // --> example.toml - oss << color::bold << color::blue << " --> " << color::reset - << color::bold << loc.file_name() << '\n' << color::reset; - return oss; -} - -TOML11_INLINE std::ostringstream& format_empty_line(std::ostringstream& oss, - const std::size_t lnw) -{ - // | - oss << detail::make_string(lnw + 1, ' ') - << color::bold << color::blue << " |\n" << color::reset; - return oss; -} - -TOML11_INLINE std::ostringstream& format_line(std::ostringstream& oss, - const std::size_t lnw, const std::size_t linenum, const std::string& line) -{ - // 10 | key = "value" - oss << ' ' << color::bold << color::blue - << std::setw(static_cast(lnw)) - << std::right << linenum << " | " << color::reset; - for(const char c : line) - { - if(std::isgraph(c) || c == ' ') - { - oss << c; - } - else - { - oss << show_char(c); - } - } - oss << '\n'; - return oss; -} -TOML11_INLINE std::ostringstream& format_underline(std::ostringstream& oss, - const std::size_t lnw, const std::size_t col, const std::size_t len, - const std::string& msg) -{ - // | ^^^^^^^-- this part - oss << make_string(lnw + 1, ' ') - << color::bold << color::blue << " | " << color::reset; - - // in case col is 0, so we don't create a string with size_t max length - const std::size_t sanitized_col = col == 0 ? 0 : col - 1 /*1-origin*/; - oss << make_string(sanitized_col, ' ') - << color::bold << color::red - << make_string(len, '^') << "-- " - << color::reset << msg << '\n'; - - return oss; -} - -TOML11_INLINE std::string format_location_impl(const std::size_t lnw, - const std::string& prev_fname, - const source_location& loc, const std::string& msg) -{ - std::ostringstream oss; - - if(loc.file_name() != prev_fname) - { - format_filename(oss, loc); - if( ! loc.lines().empty()) - { - format_empty_line(oss, lnw); - } - } - - if(loc.lines().size() == 1) - { - // when column points LF, it exceeds the size of the first line. - std::size_t underline_limit = 1; - if(loc.first_line().size() < loc.first_column_offset()) - { - underline_limit = 1; - } - else - { - underline_limit = loc.first_line().size() - loc.first_column_offset() + 1; - } - const auto underline_len = (std::min)(underline_limit, loc.length()); - - format_line(oss, lnw, loc.first_line_number(), loc.first_line()); - format_underline(oss, lnw, loc.first_column_offset(), underline_len, msg); - } - else if(loc.lines().size() == 2) - { - const auto first_underline_len = - loc.first_line().size() - loc.first_column_offset() + 1; - format_line(oss, lnw, loc.first_line_number(), loc.first_line()); - format_underline(oss, lnw, loc.first_column_offset(), - first_underline_len, ""); - - format_line(oss, lnw, loc.last_line_number(), loc.last_line()); - format_underline(oss, lnw, 1, loc.last_column_offset(), msg); - } - else if(loc.lines().size() > 2) - { - const auto first_underline_len = - loc.first_line().size() - loc.first_column_offset() + 1; - format_line(oss, lnw, loc.first_line_number(), loc.first_line()); - format_underline(oss, lnw, loc.first_column_offset(), - first_underline_len, "and"); - - if(loc.lines().size() == 3) - { - format_line(oss, lnw, loc.first_line_number()+1, loc.lines().at(1)); - format_underline(oss, lnw, 1, loc.lines().at(1).size(), "and"); - } - else - { - format_line(oss, lnw, loc.first_line_number()+1, " ..."); - format_empty_line(oss, lnw); - } - format_line(oss, lnw, loc.last_line_number(), loc.last_line()); - format_underline(oss, lnw, 1, loc.last_column_offset(), msg); - } - // if loc is empty, do nothing. - return oss.str(); -} - -} // namespace detail -} // toml -#endif // TOML11_SOURCE_LOCATION_IMPL_HPP diff --git a/include/toml11/impl/syntax_impl.hpp b/include/toml11/impl/syntax_impl.hpp deleted file mode 100644 index 7dd3b81..0000000 --- a/include/toml11/impl/syntax_impl.hpp +++ /dev/null @@ -1,732 +0,0 @@ -#ifndef TOML11_SYNTAX_IMPL_HPP -#define TOML11_SYNTAX_IMPL_HPP - -#include "../fwd/syntax_fwd.hpp" -#include "../scanner.hpp" -#include "../spec.hpp" - -namespace toml -{ -namespace detail -{ -namespace syntax -{ - -using char_type = location::char_type; - -// =========================================================================== -// UTF-8 - -// avoid redundant representation and out-of-unicode sequence - -TOML11_INLINE character_in_range utf8_1byte(const spec&) -{ - return character_in_range(0x00, 0x7F); -} - -TOML11_INLINE sequence utf8_2bytes(const spec&) -{ - return sequence(character_in_range(0xC2, 0xDF), - character_in_range(0x80, 0xBF)); -} - -TOML11_INLINE sequence utf8_3bytes(const spec&) -{ - return sequence(/*1~2 bytes = */either( - sequence(character (0xE0), character_in_range(0xA0, 0xBF)), - sequence(character_in_range(0xE1, 0xEC), character_in_range(0x80, 0xBF)), - sequence(character (0xED), character_in_range(0x80, 0x9F)), - sequence(character_in_range(0xEE, 0xEF), character_in_range(0x80, 0xBF)) - ), /*3rd byte = */ character_in_range(0x80, 0xBF)); -} - -TOML11_INLINE sequence utf8_4bytes(const spec&) -{ - return sequence(/*1~2 bytes = */either( - sequence(character (0xF0), character_in_range(0x90, 0xBF)), - sequence(character_in_range(0xF1, 0xF3), character_in_range(0x80, 0xBF)), - sequence(character (0xF4), character_in_range(0x80, 0x8F)) - ), character_in_range(0x80, 0xBF), character_in_range(0x80, 0xBF)); -} - -TOML11_INLINE non_ascii::non_ascii(const spec& s) noexcept - : scanner_(utf8_2bytes(s), utf8_3bytes(s), utf8_4bytes(s)) -{} - - -// =========================================================================== -// Whitespace - -TOML11_INLINE character_either wschar(const spec&) -{ - return character_either{char_type(' '), char_type('\t')}; -} - -TOML11_INLINE repeat_at_least ws(const spec& s) -{ - return repeat_at_least(0, wschar(s)); -} - -// =========================================================================== -// Newline - -TOML11_INLINE either newline(const spec&) -{ - return either(character(char_type('\n')), literal("\r\n")); -} - -// =========================================================================== -// Comments - -TOML11_INLINE either allowed_comment_char(const spec& s) -{ - if(s.v1_1_0_allow_control_characters_in_comments) - { - return either( - character_in_range(0x01, 0x09), - character_in_range(0x0E, 0x7F), - non_ascii(s) - ); - } - else - { - return either( - character(0x09), - character_in_range(0x20, 0x7E), - non_ascii(s) - ); - } -} - -// XXX Note that it does not take newline -TOML11_INLINE sequence comment(const spec& s) -{ - return sequence(character(char_type('#')), - repeat_at_least(0, allowed_comment_char(s))); -} - -// =========================================================================== -// Boolean - -TOML11_INLINE either boolean(const spec&) -{ - return either(literal("true"), literal("false")); -} - -// =========================================================================== -// Integer - -TOML11_INLINE digit::digit(const spec&) noexcept - : scanner_(char_type('0'), char_type('9')) -{} - -TOML11_INLINE alpha::alpha(const spec&) noexcept - : scanner_( - character_in_range(char_type('a'), char_type('z')), - character_in_range(char_type('A'), char_type('Z')) - ) -{} - -TOML11_INLINE hexdig::hexdig(const spec& s) noexcept - : scanner_( - digit(s), - character_in_range(char_type('a'), char_type('f')), - character_in_range(char_type('A'), char_type('F')) - ) -{} - -// non-digit-graph = ([a-zA-Z]|unicode mb char) -// graph = ([a-zA-Z0-9]|unicode mb char) -// suffix = _ non-digit-graph (graph | _graph) -TOML11_INLINE sequence num_suffix(const spec& s) -{ - const auto non_digit_graph = [&s]() { - return either( - alpha(s), - non_ascii(s) - ); - }; - const auto graph = [&s]() { - return either( - alpha(s), - digit(s), - non_ascii(s) - ); - }; - - return sequence( - character(char_type('_')), - non_digit_graph(), - repeat_at_least(0, - either( - sequence(character(char_type('_')), graph()), - graph() - ) - ) - ); -} - -TOML11_INLINE sequence dec_int(const spec& s) -{ - const auto digit19 = []() { - return character_in_range(char_type('1'), char_type('9')); - }; - return sequence( - maybe(character_either{char_type('-'), char_type('+')}), - either( - sequence( - digit19(), - repeat_at_least(1, - either( - digit(s), - sequence(character(char_type('_')), digit(s)) - ) - ) - ), - digit(s) - ) - ); -} - -TOML11_INLINE sequence hex_int(const spec& s) -{ - return sequence( - literal("0x"), - hexdig(s), - repeat_at_least(0, - either( - hexdig(s), - sequence(character(char_type('_')), hexdig(s)) - ) - ) - ); -} - -TOML11_INLINE sequence oct_int(const spec&) -{ - const auto digit07 = []() { - return character_in_range(char_type('0'), char_type('7')); - }; - return sequence( - literal("0o"), - digit07(), - repeat_at_least(0, - either( - digit07(), - sequence(character(char_type('_')), digit07()) - ) - ) - ); -} - -TOML11_INLINE sequence bin_int(const spec&) -{ - const auto digit01 = []() { - return character_either{char_type('0'), char_type('1')}; - }; - return sequence( - literal("0b"), - digit01(), - repeat_at_least(0, - either( - digit01(), - sequence(character(char_type('_')), digit01()) - ) - ) - ); -} - -TOML11_INLINE either integer(const spec& s) -{ - return either( - hex_int(s), - oct_int(s), - bin_int(s), - dec_int(s) - ); -} - - -// =========================================================================== -// Floating - -TOML11_INLINE sequence zero_prefixable_int(const spec& s) -{ - return sequence( - digit(s), - repeat_at_least(0, - either( - digit(s), - sequence(character('_'), digit(s)) - ) - ) - ); -} - -TOML11_INLINE sequence fractional_part(const spec& s) -{ - return sequence( - character('.'), - zero_prefixable_int(s) - ); -} - -TOML11_INLINE sequence exponent_part(const spec& s) -{ - return sequence( - character_either{char_type('e'), char_type('E')}, - maybe(character_either{char_type('+'), char_type('-')}), - zero_prefixable_int(s) - ); -} - -TOML11_INLINE sequence hex_floating(const spec& s) -{ - // C99 hexfloat (%a) - // [+-]? 0x ( [0-9a-fA-F]*\.[0-9a-fA-F]+ | [0-9a-fA-F]+\.? ) [pP] [+-]? [0-9]+ - - // - 0x(int).(frac)p[+-](int) - // - 0x(int).p[+-](int) - // - 0x.(frac)p[+-](int) - // - 0x(int)p[+-](int) - - return sequence( - maybe(character_either{char_type('+'), char_type('-')}), - character('0'), - character_either{char_type('x'), char_type('X')}, - either( - sequence( - repeat_at_least(0, hexdig(s)), - character('.'), - repeat_at_least(1, hexdig(s)) - ), - sequence( - repeat_at_least(1, hexdig(s)), - maybe(character('.')) - ) - ), - character_either{char_type('p'), char_type('P')}, - maybe(character_either{char_type('+'), char_type('-')}), - repeat_at_least(1, character_in_range('0', '9')) - ); -} - -TOML11_INLINE either floating(const spec& s) -{ - return either( - sequence( - dec_int(s), - either( - exponent_part(s), - sequence(fractional_part(s), maybe(exponent_part(s))) - ) - ), - sequence( - maybe(character_either{char_type('-'), char_type('+')}), - either(literal("inf"), literal("nan")) - ) - ); -} - -// =========================================================================== -// Datetime - -TOML11_INLINE sequence local_date(const spec& s) -{ - return sequence( - repeat_exact(4, digit(s)), - character('-'), - repeat_exact(2, digit(s)), - character('-'), - repeat_exact(2, digit(s)) - ); -} -TOML11_INLINE sequence local_time(const spec& s) -{ - auto time = sequence( - repeat_exact(2, digit(s)), - character(':'), - repeat_exact(2, digit(s)) - ); - - if(s.v1_1_0_make_seconds_optional) - { - time.push_back(maybe(sequence( - character(':'), - repeat_exact(2, digit(s)), - maybe(sequence(character('.'), repeat_at_least(1, digit(s)))) - ))); - } - else - { - time.push_back(character(':')); - time.push_back(repeat_exact(2, digit(s))); - time.push_back( - maybe(sequence(character('.'), repeat_at_least(1, digit(s)))) - ); - } - - return time; -} -TOML11_INLINE either time_offset(const spec& s) -{ - return either( - character_either{'Z', 'z'}, - sequence(character_either{'+', '-'}, - repeat_exact(2, digit(s)), - character(':'), - repeat_exact(2, digit(s)) - ) - ); -} -TOML11_INLINE sequence full_time(const spec& s) -{ - return sequence(local_time(s), time_offset(s)); -} -TOML11_INLINE character_either time_delim(const spec&) -{ - return character_either{'T', 't', ' '}; -} -TOML11_INLINE sequence local_datetime(const spec& s) -{ - return sequence(local_date(s), time_delim(s), local_time(s)); -} -TOML11_INLINE sequence offset_datetime(const spec& s) -{ - return sequence(local_date(s), time_delim(s), full_time(s)); -} - -// =========================================================================== -// String - -TOML11_INLINE sequence escaped(const spec& s) -{ - character_either escape_char{ - '\"','\\', 'b', 'f', 'n', 'r', 't' - }; - if(s.v1_1_0_add_escape_sequence_e) - { - escape_char.push_back(char_type('e')); - } - - either escape_seq( - std::move(escape_char), - sequence(character('u'), repeat_exact(4, hexdig(s))), - sequence(character('U'), repeat_exact(8, hexdig(s))) - ); - - if(s.v1_1_0_add_escape_sequence_x) - { - escape_seq.push_back( - sequence(character('x'), repeat_exact(2, hexdig(s))) - ); - } - - return sequence( - character('\\'), - std::move(escape_seq) - ); -} - -TOML11_INLINE either basic_char(const spec& s) -{ - const auto basic_unescaped = [&s]() { - return either( - wschar(s), - character(0x21), // 22 is " - character_in_range(0x23, 0x5B), // 5C is backslash - character_in_range(0x5D, 0x7E), // 7F is DEL - non_ascii(s) - ); - }; - return either(basic_unescaped(), escaped(s)); -} - -TOML11_INLINE sequence basic_string(const spec& s) -{ - return sequence( - character('"'), - repeat_at_least(0, basic_char(s)), - character('"') - ); -} - -// --------------------------------------------------------------------------- -// multiline string - -TOML11_INLINE sequence escaped_newline(const spec& s) -{ - return sequence( - character('\\'), ws(s), newline(s), - repeat_at_least(0, either(wschar(s), newline(s))) - ); -} - -TOML11_INLINE sequence ml_basic_string(const spec& s) -{ - const auto mlb_content = [&s]() { - return either(basic_char(s), newline(s), escaped_newline(s)); - }; - const auto mlb_quotes = []() { - return either(literal("\"\""), character('\"')); - }; - - return sequence( - literal("\"\"\""), - maybe(newline(s)), - repeat_at_least(0, mlb_content()), - repeat_at_least(0, - sequence( - mlb_quotes(), - repeat_at_least(1, mlb_content()) - ) - ), - // XXX """ and mlb_quotes are intentionally reordered to avoid - // unexpected match of mlb_quotes - literal("\"\"\""), - maybe(mlb_quotes()) - ); -} - -// --------------------------------------------------------------------------- -// literal string - -TOML11_INLINE either literal_char(const spec& s) -{ - return either( - character (0x09), - character_in_range(0x20, 0x26), - character_in_range(0x28, 0x7E), - non_ascii(s) - ); -} - -TOML11_INLINE sequence literal_string(const spec& s) -{ - return sequence( - character('\''), - repeat_at_least(0, literal_char(s)), - character('\'') - ); -} - -TOML11_INLINE sequence ml_literal_string(const spec& s) -{ - const auto mll_quotes = []() { - return either(literal("''"), character('\'')); - }; - const auto mll_content = [&s]() { - return either(literal_char(s), newline(s)); - }; - - return sequence( - literal("'''"), - maybe(newline(s)), - repeat_at_least(0, mll_content()), - repeat_at_least(0, sequence( - mll_quotes(), - repeat_at_least(1, mll_content()) - ) - ), - literal("'''"), - maybe(mll_quotes()) - // XXX ''' and mll_quotes are intentionally reordered to avoid - // unexpected match of mll_quotes - ); -} - -TOML11_INLINE either string(const spec& s) -{ - return either( - ml_basic_string(s), - ml_literal_string(s), - basic_string(s), - literal_string(s) - ); -} - -// =========================================================================== -// Keys - -// to keep `expected_chars` simple -TOML11_INLINE non_ascii_key_char::non_ascii_key_char(const spec& s) noexcept -{ - assert(s.v1_1_0_allow_non_english_in_bare_keys); - (void)s; // for NDEBUG -} - -TOML11_INLINE std::uint32_t non_ascii_key_char::read_utf8(location& loc) const -{ - // U+0000 ... U+0079 ; 0xxx_xxxx - // U+0080 ... U+07FF ; 110y_yyyx 10xx_xxxx; - // U+0800 ... U+FFFF ; 1110_yyyy 10yx_xxxx 10xx_xxxx - // U+010000 ... U+10FFFF; 1111_0yyy 10yy_xxxx 10xx_xxxx 10xx_xxxx - - const unsigned char b1 = loc.current(); loc.advance(1); - if(b1 < 0x80) - { - return static_cast(b1); - } - else if((b1 >> 5) == 6) // 0b110 == 6 - { - const auto b2 = loc.current(); loc.advance(1); - - const std::uint32_t c1 = b1 & ((1 << 5) - 1); - const std::uint32_t c2 = b2 & ((1 << 6) - 1); - const std::uint32_t codep = (c1 << 6) + c2; - - if(codep < 0x80) - { - return 0xFFFFFFFF; - } - return codep; - } - else if((b1 >> 4) == 14) // 0b1110 == 14 - { - const auto b2 = loc.current(); loc.advance(1); if(loc.eof()) {return 0xFFFFFFFF;} - const auto b3 = loc.current(); loc.advance(1); - - const std::uint32_t c1 = b1 & ((1 << 4) - 1); - const std::uint32_t c2 = b2 & ((1 << 6) - 1); - const std::uint32_t c3 = b3 & ((1 << 6) - 1); - - const std::uint32_t codep = (c1 << 12) + (c2 << 6) + c3; - if(codep < 0x800) - { - return 0xFFFFFFFF; - } - return codep; - } - else if((b1 >> 3) == 30) // 0b11110 == 30 - { - const auto b2 = loc.current(); loc.advance(1); if(loc.eof()) {return 0xFFFFFFFF;} - const auto b3 = loc.current(); loc.advance(1); if(loc.eof()) {return 0xFFFFFFFF;} - const auto b4 = loc.current(); loc.advance(1); - - const std::uint32_t c1 = b1 & ((1 << 3) - 1); - const std::uint32_t c2 = b2 & ((1 << 6) - 1); - const std::uint32_t c3 = b3 & ((1 << 6) - 1); - const std::uint32_t c4 = b4 & ((1 << 6) - 1); - const std::uint32_t codep = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4; - - if(codep < 0x10000) - { - return 0xFFFFFFFF; - } - return codep; - } - else // not a Unicode codepoint in UTF-8 - { - return 0xFFFFFFFF; - } -} - -TOML11_INLINE region non_ascii_key_char::scan(location& loc) const -{ - if(loc.eof()) {return region{};} - - const auto first = loc; - - const auto cp = read_utf8(loc); - - if(cp == 0xFFFFFFFF) - { - return region{}; - } - - // ALPHA / DIGIT / %x2D / %x5F ; a-z A-Z 0-9 - _ - // / %xB2 / %xB3 / %xB9 / %xBC-BE ; superscript digits, fractions - // / %xC0-D6 / %xD8-F6 / %xF8-37D ; non-symbol chars in Latin block - // / %x37F-1FFF ; exclude GREEK QUESTION MARK, which is basically a semi-colon - // / %x200C-200D / %x203F-2040 ; from General Punctuation Block, include the two tie symbols and ZWNJ, ZWJ - // / %x2070-218F / %x2460-24FF ; include super-/subscripts, letterlike/numberlike forms, enclosed alphanumerics - // / %x2C00-2FEF / %x3001-D7FF ; skip arrows, math, box drawing etc, skip 2FF0-3000 ideographic up/down markers and spaces - // / %xF900-FDCF / %xFDF0-FFFD ; skip D800-DFFF surrogate block, E000-F8FF Private Use area, FDD0-FDEF intended for process-internal use (unicode) - // / %x10000-EFFFF ; all chars outside BMP range, excluding Private Use planes (F0000-10FFFF) - - if(cp == 0xB2 || cp == 0xB3 || cp == 0xB9 || (0xBC <= cp && cp <= 0xBE) || - (0xC0 <= cp && cp <= 0xD6 ) || (0xD8 <= cp && cp <= 0xF6) || (0xF8 <= cp && cp <= 0x37D) || - (0x37F <= cp && cp <= 0x1FFF) || - (0x200C <= cp && cp <= 0x200D) || (0x203F <= cp && cp <= 0x2040) || - (0x2070 <= cp && cp <= 0x218F) || (0x2460 <= cp && cp <= 0x24FF) || - (0x2C00 <= cp && cp <= 0x2FEF) || (0x3001 <= cp && cp <= 0xD7FF) || - (0xF900 <= cp && cp <= 0xFDCF) || (0xFDF0 <= cp && cp <= 0xFFFD) || - (0x10000 <= cp && cp <= 0xEFFFF) ) - { - return region(first, loc); - } - loc = first; - return region{}; -} - -TOML11_INLINE repeat_at_least unquoted_key(const spec& s) -{ - auto keychar = either( - alpha(s), digit(s), character{0x2D}, character{0x5F} - ); - - if(s.v1_1_0_allow_non_english_in_bare_keys) - { - keychar.push_back(non_ascii_key_char(s)); - } - - return repeat_at_least(1, std::move(keychar)); -} - -TOML11_INLINE either quoted_key(const spec& s) -{ - return either(basic_string(s), literal_string(s)); -} - -TOML11_INLINE either simple_key(const spec& s) -{ - return either(unquoted_key(s), quoted_key(s)); -} - -TOML11_INLINE sequence dot_sep(const spec& s) -{ - return sequence(ws(s), character('.'), ws(s)); -} - -TOML11_INLINE sequence dotted_key(const spec& s) -{ - return sequence( - simple_key(s), - repeat_at_least(1, sequence(dot_sep(s), simple_key(s))) - ); -} - -TOML11_INLINE key::key(const spec& s) noexcept - : scanner_(dotted_key(s), simple_key(s)) -{} - -TOML11_INLINE sequence keyval_sep(const spec& s) -{ - return sequence(ws(s), character('='), ws(s)); -} - -// =========================================================================== -// Table key - -TOML11_INLINE sequence std_table(const spec& s) -{ - return sequence(character('['), ws(s), key(s), ws(s), character(']')); -} - -TOML11_INLINE sequence array_table(const spec& s) -{ - return sequence(literal("[["), ws(s), key(s), ws(s), literal("]]")); -} - -// =========================================================================== -// extension: null - -TOML11_INLINE literal null_value(const spec&) -{ - return literal("null"); -} - -} // namespace syntax -} // namespace detail -} // namespace toml -#endif // TOML11_SYNTAX_IMPL_HPP diff --git a/include/toml11/impl/value_t_impl.hpp b/include/toml11/impl/value_t_impl.hpp deleted file mode 100644 index 784dc8b..0000000 --- a/include/toml11/impl/value_t_impl.hpp +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef TOML11_VALUE_T_IMPL_HPP -#define TOML11_VALUE_T_IMPL_HPP - -#include "../fwd/value_t_fwd.hpp" - -#include -#include -#include - -namespace toml -{ - -TOML11_INLINE std::ostream& operator<<(std::ostream& os, value_t t) -{ - switch(t) - { - case value_t::boolean : os << "boolean"; return os; - case value_t::integer : os << "integer"; return os; - case value_t::floating : os << "floating"; return os; - case value_t::string : os << "string"; return os; - case value_t::offset_datetime : os << "offset_datetime"; return os; - case value_t::local_datetime : os << "local_datetime"; return os; - case value_t::local_date : os << "local_date"; return os; - case value_t::local_time : os << "local_time"; return os; - case value_t::array : os << "array"; return os; - case value_t::table : os << "table"; return os; - case value_t::empty : os << "empty"; return os; - default : os << "unknown"; return os; - } -} - -TOML11_INLINE std::string to_string(value_t t) -{ - std::ostringstream oss; - oss << t; - return oss.str(); -} - -} // namespace toml -#endif // TOML11_VALUE_T_IMPL_HPP diff --git a/include/toml11/into.hpp b/include/toml11/into.hpp deleted file mode 100644 index 86a0020..0000000 --- a/include/toml11/into.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TOML11_INTO_HPP -#define TOML11_INTO_HPP - -namespace toml -{ - -template -struct into; -// { -// static toml::value into_toml(const T& user_defined_type) -// { -// // User-defined conversions ... -// } -// }; - -} // toml -#endif // TOML11_INTO_HPP diff --git a/include/toml11/literal.hpp b/include/toml11/literal.hpp deleted file mode 100644 index e30e7a8..0000000 --- a/include/toml11/literal.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_LITERAL_HPP -#define TOML11_LITERAL_HPP - -#include "fwd/literal_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/literal_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_LITERAL_HPP diff --git a/include/toml11/location.hpp b/include/toml11/location.hpp deleted file mode 100644 index fd23274..0000000 --- a/include/toml11/location.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_LOCATION_HPP -#define TOML11_LOCATION_HPP - -#include "fwd/location_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/location_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_LOCATION_HPP diff --git a/include/toml11/ordered_map.hpp b/include/toml11/ordered_map.hpp deleted file mode 100644 index b9cd304..0000000 --- a/include/toml11/ordered_map.hpp +++ /dev/null @@ -1,265 +0,0 @@ -#ifndef TOML11_ORDERED_MAP_HPP -#define TOML11_ORDERED_MAP_HPP - -#include -#include -#include -#include - -namespace toml -{ - -namespace detail -{ -template -struct ordered_map_ebo_container -{ - Cmp cmp_; // empty base optimization for empty Cmp type -}; -} // detail - -template, - typename Allocator = std::allocator>> -class ordered_map : detail::ordered_map_ebo_container -{ - public: - using key_type = Key; - using mapped_type = Val; - using value_type = std::pair; - - using key_compare = Cmp; - using allocator_type = Allocator; - - using container_type = std::vector; - using reference = typename container_type::reference; - using pointer = typename container_type::pointer; - using const_reference = typename container_type::const_reference; - using const_pointer = typename container_type::const_pointer; - using iterator = typename container_type::iterator; - using const_iterator = typename container_type::const_iterator; - using size_type = typename container_type::size_type; - using difference_type = typename container_type::difference_type; - - private: - - using ebo_base = detail::ordered_map_ebo_container; - - public: - - ordered_map() = default; - ~ordered_map() = default; - ordered_map(const ordered_map&) = default; - ordered_map(ordered_map&&) = default; - ordered_map& operator=(const ordered_map&) = default; - ordered_map& operator=(ordered_map&&) = default; - - ordered_map(const ordered_map& other, const Allocator& alloc) - : container_(other.container_, alloc) - {} - ordered_map(ordered_map&& other, const Allocator& alloc) - : container_(std::move(other.container_), alloc) - {} - - explicit ordered_map(const Cmp& cmp, const Allocator& alloc = Allocator()) - : ebo_base{cmp}, container_(alloc) - {} - explicit ordered_map(const Allocator& alloc) - : container_(alloc) - {} - - template - ordered_map(InputIterator first, InputIterator last, const Cmp& cmp = Cmp(), const Allocator& alloc = Allocator()) - : ebo_base{cmp}, container_(first, last, alloc) - {} - template - ordered_map(InputIterator first, InputIterator last, const Allocator& alloc) - : container_(first, last, alloc) - {} - - ordered_map(std::initializer_list v, const Cmp& cmp = Cmp(), const Allocator& alloc = Allocator()) - : ebo_base{cmp}, container_(std::move(v), alloc) - {} - ordered_map(std::initializer_list v, const Allocator& alloc) - : container_(std::move(v), alloc) - {} - ordered_map& operator=(std::initializer_list v) - { - this->container_ = std::move(v); - return *this; - } - - iterator begin() noexcept {return container_.begin();} - iterator end() noexcept {return container_.end();} - const_iterator begin() const noexcept {return container_.begin();} - const_iterator end() const noexcept {return container_.end();} - const_iterator cbegin() const noexcept {return container_.cbegin();} - const_iterator cend() const noexcept {return container_.cend();} - - bool empty() const noexcept {return container_.empty();} - std::size_t size() const noexcept {return container_.size();} - std::size_t max_size() const noexcept {return container_.max_size();} - - void clear() {container_.clear();} - - void push_back(const value_type& v) - { - if(this->contains(v.first)) - { - throw std::out_of_range("ordered_map: value already exists"); - } - container_.push_back(v); - } - void push_back(value_type&& v) - { - if(this->contains(v.first)) - { - throw std::out_of_range("ordered_map: value already exists"); - } - container_.push_back(std::move(v)); - } - void emplace_back(key_type k, mapped_type v) - { - if(this->contains(k)) - { - throw std::out_of_range("ordered_map: value already exists"); - } - container_.emplace_back(std::move(k), std::move(v)); - } - void pop_back() {container_.pop_back();} - - void insert(value_type kv) - { - if(this->contains(kv.first)) - { - throw std::out_of_range("ordered_map: value already exists"); - } - container_.push_back(std::move(kv)); - } - void emplace(key_type k, mapped_type v) - { - if(this->contains(k)) - { - throw std::out_of_range("ordered_map: value already exists"); - } - container_.emplace_back(std::move(k), std::move(v)); - } - - std::size_t count(const key_type& key) const - { - if(this->find(key) != this->end()) - { - return 1; - } - else - { - return 0; - } - } - bool contains(const key_type& key) const - { - return this->find(key) != this->end(); - } - iterator find(const key_type& key) noexcept - { - return std::find_if(this->begin(), this->end(), - [&key, this](const value_type& v) {return this->cmp_(v.first, key);}); - } - const_iterator find(const key_type& key) const noexcept - { - return std::find_if(this->begin(), this->end(), - [&key, this](const value_type& v) {return this->cmp_(v.first, key);}); - } - - mapped_type& at(const key_type& k) - { - const auto iter = this->find(k); - if(iter == this->end()) - { - throw std::out_of_range("ordered_map: no such element"); - } - return iter->second; - } - mapped_type const& at(const key_type& k) const - { - const auto iter = this->find(k); - if(iter == this->end()) - { - throw std::out_of_range("ordered_map: no such element"); - } - return iter->second; - } - - mapped_type& operator[](const key_type& k) - { - const auto iter = this->find(k); - if(iter == this->end()) - { - this->container_.emplace_back(k, mapped_type{}); - return this->container_.back().second; - } - return iter->second; - } - - mapped_type const& operator[](const key_type& k) const - { - const auto iter = this->find(k); - if(iter == this->end()) - { - throw std::out_of_range("ordered_map: no such element"); - } - return iter->second; - } - - key_compare key_comp() const {return this->cmp_;} - - void swap(ordered_map& other) - { - container_.swap(other.container_); - } - - private: - - container_type container_; -}; - -template -bool operator==(const ordered_map& lhs, const ordered_map& rhs) -{ - return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); -} -template -bool operator!=(const ordered_map& lhs, const ordered_map& rhs) -{ - return !(lhs == rhs); -} -template -bool operator<(const ordered_map& lhs, const ordered_map& rhs) -{ - return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); -} -template -bool operator>(const ordered_map& lhs, const ordered_map& rhs) -{ - return rhs < lhs; -} -template -bool operator<=(const ordered_map& lhs, const ordered_map& rhs) -{ - return !(lhs > rhs); -} -template -bool operator>=(const ordered_map& lhs, const ordered_map& rhs) -{ - return !(lhs < rhs); -} - -template -void swap(ordered_map& lhs, ordered_map& rhs) -{ - lhs.swap(rhs); - return; -} - - -} // toml -#endif // TOML11_ORDERED_MAP_HPP diff --git a/include/toml11/parser.hpp b/include/toml11/parser.hpp deleted file mode 100644 index 3915650..0000000 --- a/include/toml11/parser.hpp +++ /dev/null @@ -1,3829 +0,0 @@ -#ifndef TOML11_PARSER_HPP -#define TOML11_PARSER_HPP - -#include "context.hpp" -#include "datetime.hpp" -#include "error_info.hpp" -#include "region.hpp" -#include "result.hpp" -#include "scanner.hpp" -#include "skip.hpp" -#include "syntax.hpp" -#include "value.hpp" - -#include -#include - -#include -#include - -#if defined(TOML11_HAS_FILESYSTEM) && TOML11_HAS_FILESYSTEM -#include -#endif - -namespace toml -{ - -struct syntax_error final : public ::toml::exception -{ - public: - syntax_error(std::string what_arg, std::vector err) - : what_(std::move(what_arg)), err_(std::move(err)) - {} - ~syntax_error() noexcept override = default; - - const char* what() const noexcept override {return what_.c_str();} - - std::vector const& errors() const noexcept - { - return err_; - } - - private: - std::string what_; - std::vector err_; -}; - -struct file_io_error final : public ::toml::exception -{ - public: - - file_io_error(const std::string& msg, const std::string& fname) - : errno_(cxx::make_nullopt()), - what_(msg + " \"" + fname + "\"") - {} - file_io_error(int errnum, const std::string& msg, const std::string& fname) - : errno_(errnum), - what_(msg + " \"" + fname + "\": errno=" + std::to_string(errnum)) - {} - ~file_io_error() noexcept override = default; - - const char* what() const noexcept override {return what_.c_str();} - - bool has_errno() const noexcept {return errno_.has_value();} - int get_errno() const noexcept {return errno_.value_or(0);} - - private: - - cxx::optional errno_; - std::string what_; -}; - -namespace detail -{ - -/* ============================================================================ - * __ ___ _ __ _ __ ___ _ _ - * / _/ _ \ ' \| ' \/ _ \ ' \ - * \__\___/_|_|_|_|_|_\___/_||_| - */ - -template -error_info make_syntax_error(std::string title, - const S& scanner, location loc, std::string suffix = "") -{ - auto msg = std::string("expected ") + scanner.expected_chars(loc); - auto src = source_location(region(loc)); - return make_error_info( - std::move(title), std::move(src), std::move(msg), std::move(suffix)); -} - - -/* ============================================================================ - * _ - * __ ___ _ __ _ __ ___ _ _| |_ - * / _/ _ \ ' \| ' \/ -_) ' \ _| - * \__\___/_|_|_|_|_|_\___|_||_\__| - */ - -template -result, error_info> -parse_comment_line(location& loc, context& ctx) -{ - const auto& spec = ctx.toml_spec(); - const auto first = loc; - - skip_whitespace(loc, ctx); - - const auto com_reg = syntax::comment(spec).scan(loc); - if(com_reg.is_ok()) - { - // once comment started, newline must follow (or reach EOF). - if( ! loc.eof() && ! syntax::newline(spec).scan(loc).is_ok()) - { - while( ! loc.eof()) // skip until newline to continue parsing - { - loc.advance(); - if(loc.current() == '\n') { /*skip LF*/ loc.advance(); break; } - } - return err(make_error_info("toml::parse_comment_line: " - "newline (LF / CRLF) or EOF is expected", - source_location(region(loc)), "but got this", - "Hint: most of the control characters are not allowed in comments")); - } - return ok(cxx::optional(com_reg.as_string())); - } - else - { - loc = first; // rollback whitespace to parse indent - return ok(cxx::optional(cxx::make_nullopt())); - } -} - -/* ============================================================================ - * ___ _ - * | _ ) ___ ___| |___ __ _ _ _ - * | _ \/ _ \/ _ \ / -_) _` | ' \ - * |___/\___/\___/_\___\__,_|_||_| - */ - -template -result, error_info> -parse_boolean(location& loc, const context& ctx) -{ - const auto& spec = ctx.toml_spec(); - - // ---------------------------------------------------------------------- - // check syntax - auto reg = syntax::boolean(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_boolean: " - "invalid boolean: boolean must be `true` or `false`, in lowercase. " - "string must be surrounded by `\"`", syntax::boolean(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - const auto str = reg.as_string(); - const auto val = [&str]() { - if(str == "true") - { - return true; - } - else - { - assert(str == "false"); - return false; - } - }(); - - // ---------------------------------------------------------------------- - // no format info for boolean - boolean_format_info fmt; - - return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); -} - -/* ============================================================================ - * ___ _ - * |_ _|_ _| |_ ___ __ _ ___ _ _ - * | || ' \ _/ -_) _` / -_) '_| - * |___|_||_\__\___\__, \___|_| - * |___/ - */ - -template -result, error_info> -parse_bin_integer(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - auto reg = syntax::bin_int(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_bin_integer: " - "invalid integer: bin_int must be like: 0b0101, 0b1111_0000", - syntax::bin_int(spec), loc)); - } - - auto str = reg.as_string(); - - integer_format_info fmt; - fmt.fmt = integer_format::bin; - fmt.width = str.size() - 2 - static_cast(std::count(str.begin(), str.end(), '_')); - - const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); - if(first_underscore != str.rend()) - { - fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); - } - - // skip prefix `0b` and zeros and underscores at the MSB - str.erase(str.begin(), std::find(std::next(str.begin(), 2), str.end(), '1')); - - // remove all `_` before calling TC::parse_int - str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); - - // 0b0000_0000 becomes empty. - if(str.empty()) { str = "0"; } - - const auto val = TC::parse_int(str, source_location(region(loc)), 2); - if(val.is_ok()) - { - return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); - } - else - { - loc = first; - return err(val.as_err()); - } -} - -// ---------------------------------------------------------------------------- - -template -result, error_info> -parse_oct_integer(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - auto reg = syntax::oct_int(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_oct_integer: " - "invalid integer: oct_int must be like: 0o775, 0o04_44", - syntax::oct_int(spec), loc)); - } - - auto str = reg.as_string(); - - integer_format_info fmt; - fmt.fmt = integer_format::oct; - fmt.width = str.size() - 2 - static_cast(std::count(str.begin(), str.end(), '_')); - - const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); - if(first_underscore != str.rend()) - { - fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); - } - - // skip prefix `0o` and zeros and underscores at the MSB - str.erase(str.begin(), std::find_if( - std::next(str.begin(), 2), str.end(), [](const char c) { - return c != '0' && c != '_'; - })); - - // remove all `_` before calling TC::parse_int - str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); - - // 0o0000_0000 becomes empty. - if(str.empty()) { str = "0"; } - - const auto val = TC::parse_int(str, source_location(region(loc)), 8); - if(val.is_ok()) - { - return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); - } - else - { - loc = first; - return err(val.as_err()); - } -} - -template -result, error_info> -parse_hex_integer(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - auto reg = syntax::hex_int(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_hex_integer: " - "invalid integer: hex_int must be like: 0xC0FFEE, 0xdead_beef", - syntax::hex_int(spec), loc)); - } - - auto str = reg.as_string(); - - integer_format_info fmt; - fmt.fmt = integer_format::hex; - fmt.width = str.size() - 2 - static_cast(std::count(str.begin(), str.end(), '_')); - - const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); - if(first_underscore != str.rend()) - { - fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); - } - - // skip prefix `0x` and zeros and underscores at the MSB - str.erase(str.begin(), std::find_if( - std::next(str.begin(), 2), str.end(), [](const char c) { - return c != '0' && c != '_'; - })); - - // remove all `_` before calling TC::parse_int - str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); - - // 0x0000_0000 becomes empty. - if(str.empty()) { str = "0"; } - - // prefix zero and _ is removed. check if it uses upper/lower case. - // if both upper and lower case letters are found, set upper=true. - const auto lower_not_found = std::find_if(str.begin(), str.end(), - [](const char c) { return std::islower(static_cast(c)) != 0; }) == str.end(); - const auto upper_found = std::find_if(str.begin(), str.end(), - [](const char c) { return std::isupper(static_cast(c)) != 0; }) != str.end(); - fmt.uppercase = lower_not_found || upper_found; - - const auto val = TC::parse_int(str, source_location(region(loc)), 16); - if(val.is_ok()) - { - return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); - } - else - { - loc = first; - return err(val.as_err()); - } -} - -template -result, error_info> -parse_dec_integer(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - // ---------------------------------------------------------------------- - // check syntax - auto reg = syntax::dec_int(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_dec_integer: " - "invalid integer: dec_int must be like: 42, 123_456_789", - syntax::dec_int(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - auto str = reg.as_string(); - - integer_format_info fmt; - fmt.fmt = integer_format::dec; - fmt.width = str.size() - static_cast(std::count(str.begin(), str.end(), '_')); - - const auto first_underscore = std::find(str.rbegin(), str.rend(), '_'); - if(first_underscore != str.rend()) - { - fmt.spacer = static_cast(std::distance(str.rbegin(), first_underscore)); - } - - // remove all `_` before calling TC::parse_int - str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); - - auto src = source_location(region(loc)); - const auto val = TC::parse_int(str, src, 10); - if(val.is_err()) - { - loc = first; - return err(val.as_err()); - } - - // ---------------------------------------------------------------------- - // parse suffix (extension) - - if(spec.ext_num_suffix && loc.current() == '_') - { - const auto sfx_reg = syntax::num_suffix(spec).scan(loc); - if( ! sfx_reg.is_ok()) - { - loc = first; - return err(make_error_info("toml::parse_dec_integer: " - "invalid suffix: should be `_ non-digit-graph (graph | _graph)`", - source_location(region(loc)), "here")); - } - auto sfx = sfx_reg.as_string(); - assert( ! sfx.empty() && sfx.front() == '_'); - sfx.erase(sfx.begin()); // remove the first `_` - - fmt.suffix = sfx; - } - - return ok(basic_value(val.as_ok(), std::move(fmt), {}, std::move(reg))); -} - -template -result, error_info> -parse_integer(location& loc, const context& ctx) -{ - const auto first = loc; - - if( ! loc.eof() && (loc.current() == '+' || loc.current() == '-')) - { - // skip +/- to diagnose +0xDEADBEEF or -0b0011 (invalid). - // without this, +0xDEAD_BEEF will be parsed as a decimal int and - // unexpected "xDEAD_BEEF" will appear after integer "+0". - loc.advance(); - } - - if( ! loc.eof() && loc.current() == '0') - { - loc.advance(); - if(loc.eof()) - { - // `[+-]?0`. parse as an decimal integer. - loc = first; - return parse_dec_integer(loc, ctx); - } - - const auto prefix = loc.current(); - auto prefix_src = source_location(region(loc)); - - loc = first; - - if(prefix == 'b') {return parse_bin_integer(loc, ctx);} - if(prefix == 'o') {return parse_oct_integer(loc, ctx);} - if(prefix == 'x') {return parse_hex_integer(loc, ctx);} - - if(std::isdigit(prefix)) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_integer: " - "leading zero in an decimal integer is not allowed", - std::move(src), "leading zero")); - } - } - - loc = first; - return parse_dec_integer(loc, ctx); -} - -/* ============================================================================ - * ___ _ _ _ - * | __| |___ __ _| |_(_)_ _ __ _ - * | _|| / _ \/ _` | _| | ' \/ _` | - * |_| |_\___/\__,_|\__|_|_||_\__, | - * |___/ - */ - -template -result, error_info> -parse_floating(location& loc, const context& ctx) -{ - using floating_type = typename basic_value::floating_type; - - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - // ---------------------------------------------------------------------- - // check syntax - bool is_hex = false; - std::string str; - region reg; - if(spec.ext_hex_float && sequence(character('0'), character('x')).scan(loc).is_ok()) - { - loc = first; - is_hex = true; - - reg = syntax::hex_floating(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_floating: " - "invalid hex floating: float must be like: 0xABCp-3f", - syntax::floating(spec), loc)); - } - str = reg.as_string(); - } - else - { - reg = syntax::floating(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_floating: " - "invalid floating: float must be like: -3.14159_26535, 6.022e+23, " - "inf, or nan (lowercase).", syntax::floating(spec), loc)); - } - str = reg.as_string(); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - - floating_format_info fmt; - - if(is_hex) - { - fmt.fmt = floating_format::hex; - } - else - { - // since we already checked that the string conforms the TOML standard. - if(std::find(str.begin(), str.end(), 'e') != str.end() || - std::find(str.begin(), str.end(), 'E') != str.end()) - { - fmt.fmt = floating_format::scientific; // use exponent part - } - else - { - fmt.fmt = floating_format::fixed; // do not use exponent part - } - } - - str.erase(std::remove(str.begin(), str.end(), '_'), str.end()); - - floating_type val{0}; - - if(str == "inf" || str == "+inf") - { - TOML11_CONSTEXPR_IF(std::numeric_limits::has_infinity) - { - val = std::numeric_limits::infinity(); - } - else - { - return err(make_error_info("toml::parse_floating: inf value found" - " but the current environment does not support inf. Please" - " make sure that the floating-point implementation conforms" - " IEEE 754/ISO 60559 international standard.", - source_location(region(loc)), - "floating_type: inf is not supported")); - } - } - else if(str == "-inf") - { - TOML11_CONSTEXPR_IF(std::numeric_limits::has_infinity) - { - val = -std::numeric_limits::infinity(); - } - else - { - return err(make_error_info("toml::parse_floating: inf value found" - " but the current environment does not support inf. Please" - " make sure that the floating-point implementation conforms" - " IEEE 754/ISO 60559 international standard.", - source_location(region(loc)), - "floating_type: inf is not supported")); - } - } - else if(str == "nan" || str == "+nan") - { - TOML11_CONSTEXPR_IF(std::numeric_limits::has_quiet_NaN) - { - val = std::numeric_limits::quiet_NaN(); - } - else TOML11_CONSTEXPR_IF(std::numeric_limits::has_signaling_NaN) - { - val = std::numeric_limits::signaling_NaN(); - } - else - { - return err(make_error_info("toml::parse_floating: NaN value found" - " but the current environment does not support NaN. Please" - " make sure that the floating-point implementation conforms" - " IEEE 754/ISO 60559 international standard.", - source_location(region(loc)), - "floating_type: NaN is not supported")); - } - } - else if(str == "-nan") - { - using std::copysign; - TOML11_CONSTEXPR_IF(std::numeric_limits::has_quiet_NaN) - { - val = copysign(std::numeric_limits::quiet_NaN(), floating_type(-1)); - } - else TOML11_CONSTEXPR_IF(std::numeric_limits::has_signaling_NaN) - { - val = copysign(std::numeric_limits::signaling_NaN(), floating_type(-1)); - } - else - { - return err(make_error_info("toml::parse_floating: NaN value found" - " but the current environment does not support NaN. Please" - " make sure that the floating-point implementation conforms" - " IEEE 754/ISO 60559 international standard.", - source_location(region(loc)), - "floating_type: NaN is not supported")); - } - } - else - { - // set precision - const auto has_sign = ! str.empty() && (str.front() == '+' || str.front() == '-'); - const auto decpoint = std::find(str.begin(), str.end(), '.'); - const auto exponent = std::find_if(str.begin(), str.end(), - [](const char c) { return c == 'e' || c == 'E'; }); - if(decpoint != str.end() && exponent != str.end()) - { - assert(decpoint < exponent); - } - - if(fmt.fmt == floating_format::scientific) - { - // total width - fmt.prec = static_cast(std::distance(str.begin(), exponent)); - if(has_sign) - { - fmt.prec -= 1; - } - if(decpoint != str.end()) - { - fmt.prec -= 1; - } - } - else if(fmt.fmt == floating_format::hex) - { - fmt.prec = std::numeric_limits::max_digits10; - } - else - { - // width after decimal point - fmt.prec = static_cast(std::distance(std::next(decpoint), exponent)); - } - - auto src = source_location(region(loc)); - const auto res = TC::parse_float(str, src, is_hex); - if(res.is_ok()) - { - val = res.as_ok(); - } - else - { - return err(res.as_err()); - } - } - - // ---------------------------------------------------------------------- - // parse suffix (extension) - - if(spec.ext_num_suffix && loc.current() == '_') - { - const auto sfx_reg = syntax::num_suffix(spec).scan(loc); - if( ! sfx_reg.is_ok()) - { - auto src = source_location(region(loc)); - loc = first; - return err(make_error_info("toml::parse_floating: " - "invalid suffix: should be `_ non-digit-graph (graph | _graph)`", - std::move(src), "here")); - } - auto sfx = sfx_reg.as_string(); - assert( ! sfx.empty() && sfx.front() == '_'); - sfx.erase(sfx.begin()); // remove the first `_` - - fmt.suffix = sfx; - } - - return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); -} - -/* ============================================================================ - * ___ _ _ _ - * | \ __ _| |_ ___| |_(_)_ __ ___ - * | |) / _` | _/ -_) _| | ' \/ -_) - * |___/\__,_|\__\___|\__|_|_|_|_\___| - */ - -// all the offset_datetime, local_datetime, local_date parses date part. -template -result, error_info> -parse_local_date_only(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - local_date_format_info fmt; - - // ---------------------------------------------------------------------- - // check syntax - auto reg = syntax::local_date(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_local_date: " - "invalid date: date must be like: 1234-05-06, yyyy-mm-dd.", - syntax::local_date(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - const auto str = reg.as_string(); - - // 0123456789 - // yyyy-mm-dd - const auto year_r = from_string(str.substr(0, 4)); - const auto month_r = from_string(str.substr(5, 2)); - const auto day_r = from_string(str.substr(8, 2)); - - if(year_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_date: " - "failed to read year `" + str.substr(0, 4) + "`", - std::move(src), "here")); - } - if(month_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_date: " - "failed to read month `" + str.substr(5, 2) + "`", - std::move(src), "here")); - } - if(day_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_date: " - "failed to read day `" + str.substr(8, 2) + "`", - std::move(src), "here")); - } - - const auto year = year_r.unwrap(); - const auto month = month_r.unwrap(); - const auto day = day_r.unwrap(); - - { - // We briefly check whether the input date is valid or not. - // Actually, because of the historical reasons, there are several - // edge cases, such as 1582/10/5-1582/10/14 (only in several countries). - // But here, we do not care about it. - // It makes the code complicated and there is only low probability - // that such a specific date is needed in practice. If someone need to - // validate date accurately, that means that the one need a specialized - // library for their purpose in another layer. - - const bool is_leap = (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); - const auto max_day = [month, is_leap]() { - if(month == 2) - { - return is_leap ? 29 : 28; - } - if(month == 4 || month == 6 || month == 9 || month == 11) - { - return 30; - } - return 31; - }(); - - if((month < 1 || 12 < month) || (day < 1 || max_day < day)) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_date: invalid date.", - std::move(src), "month must be 01-12, day must be any of " - "01-28,29,30,31 depending on the month/year.")); - } - } - - return ok(std::make_tuple( - local_date(year, static_cast(month - 1), day), - std::move(fmt), std::move(reg) - )); -} - -template -result, error_info> -parse_local_date(location& loc, const context& ctx) -{ - auto val_fmt_reg = parse_local_date_only(loc, ctx); - if(val_fmt_reg.is_err()) - { - return err(val_fmt_reg.unwrap_err()); - } - - auto val = std::move(std::get<0>(val_fmt_reg.unwrap())); - auto fmt = std::move(std::get<1>(val_fmt_reg.unwrap())); - auto reg = std::move(std::get<2>(val_fmt_reg.unwrap())); - - return ok(basic_value(std::move(val), std::move(fmt), {}, std::move(reg))); -} - -// all the offset_datetime, local_datetime, local_time parses date part. -template -result, error_info> -parse_local_time_only(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - local_time_format_info fmt; - - // ---------------------------------------------------------------------- - // check syntax - auto reg = syntax::local_time(spec).scan(loc); - if( ! reg.is_ok()) - { - if(spec.v1_1_0_make_seconds_optional) - { - return err(make_syntax_error("toml::parse_local_time: " - "invalid time: time must be HH:MM(:SS.sss) (seconds are optional)", - syntax::local_time(spec), loc)); - } - else - { - return err(make_syntax_error("toml::parse_local_time: " - "invalid time: time must be HH:MM:SS(.sss) (subseconds are optional)", - syntax::local_time(spec), loc)); - } - } - - // ---------------------------------------------------------------------- - // it matches. gen value - const auto str = reg.as_string(); - - // at least we have HH:MM. - // 01234 - // HH:MM - const auto hour_r = from_string(str.substr(0, 2)); - const auto minute_r = from_string(str.substr(3, 2)); - - if(hour_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: " - "failed to read hour `" + str.substr(0, 2) + "`", - std::move(src), "here")); - } - if(minute_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: " - "failed to read minute `" + str.substr(3, 2) + "`", - std::move(src), "here")); - } - - const auto hour = hour_r.unwrap(); - const auto minute = minute_r.unwrap(); - - if((hour < 0 || 24 <= hour) || (minute < 0 || 60 <= minute)) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: invalid time.", - std::move(src), "hour must be 00-23, minute must be 00-59.")); - } - - // ----------------------------------------------------------------------- - // we have hour and minute. - // Since toml v1.1.0, second and subsecond part becomes optional. - // Check the version and return if second does not exist. - - if(str.size() == 5 && spec.v1_1_0_make_seconds_optional) - { - fmt.has_seconds = false; - fmt.subsecond_precision = 0; - return ok(std::make_tuple(local_time(hour, minute, 0), std::move(fmt), std::move(reg))); - } - assert(str.at(5) == ':'); - - // we have at least `:SS` part. `.subseconds` are optional. - - // 0 1 - // 012345678901234 - // HH:MM:SS.subsec - const auto sec_r = from_string(str.substr(6, 2)); - if(sec_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: " - "failed to read second `" + str.substr(6, 2) + "`", - std::move(src), "here")); - } - const auto sec = sec_r.unwrap(); - - if(sec < 0 || 60 < sec) // :60 is allowed - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: invalid time.", - std::move(src), "second must be 00-60.")); - } - - if(str.size() == 8) - { - fmt.has_seconds = true; - fmt.subsecond_precision = 0; - return ok(std::make_tuple(local_time(hour, minute, sec), std::move(fmt), std::move(reg))); - } - - assert(str.at(8) == '.'); - - auto secfrac = str.substr(9, str.size() - 9); - - fmt.has_seconds = true; - fmt.subsecond_precision = secfrac.size(); - - while(secfrac.size() < 9) - { - secfrac += '0'; - } - assert(9 <= secfrac.size()); - const auto ms_r = from_string(secfrac.substr(0, 3)); - const auto us_r = from_string(secfrac.substr(3, 3)); - const auto ns_r = from_string(secfrac.substr(6, 3)); - - if(ms_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: " - "failed to read milliseconds `" + secfrac.substr(0, 3) + "`", - std::move(src), "here")); - } - if(us_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: " - "failed to read microseconds`" + str.substr(3, 3) + "`", - std::move(src), "here")); - } - if(ns_r.is_err()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_local_time: " - "failed to read nanoseconds`" + str.substr(6, 3) + "`", - std::move(src), "here")); - } - const auto ms = ms_r.unwrap(); - const auto us = us_r.unwrap(); - const auto ns = ns_r.unwrap(); - - return ok(std::make_tuple(local_time(hour, minute, sec, ms, us, ns), std::move(fmt), std::move(reg))); -} - -template -result, error_info> -parse_local_time(location& loc, const context& ctx) -{ - const auto first = loc; - - auto val_fmt_reg = parse_local_time_only(loc, ctx); - if(val_fmt_reg.is_err()) - { - return err(val_fmt_reg.unwrap_err()); - } - - auto val = std::move(std::get<0>(val_fmt_reg.unwrap())); - auto fmt = std::move(std::get<1>(val_fmt_reg.unwrap())); - auto reg = std::move(std::get<2>(val_fmt_reg.unwrap())); - - return ok(basic_value(std::move(val), std::move(fmt), {}, std::move(reg))); -} - -template -result, error_info> -parse_local_datetime(location& loc, const context& ctx) -{ - using char_type = location::char_type; - - const auto first = loc; - - local_datetime_format_info fmt; - - // ---------------------------------------------------------------------- - - auto date_fmt_reg = parse_local_date_only(loc, ctx); - if(date_fmt_reg.is_err()) - { - return err(date_fmt_reg.unwrap_err()); - } - - if(loc.current() == char_type('T')) - { - loc.advance(); - fmt.delimiter = datetime_delimiter_kind::upper_T; - } - else if(loc.current() == char_type('t')) - { - loc.advance(); - fmt.delimiter = datetime_delimiter_kind::lower_t; - } - else if(loc.current() == char_type(' ')) - { - loc.advance(); - fmt.delimiter = datetime_delimiter_kind::space; - } - else - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_local_datetime: " - "expect date-time delimiter `T`, `t` or ` `(space).", - std::move(src), "here")); - } - - auto time_fmt_reg = parse_local_time_only(loc, ctx); - if(time_fmt_reg.is_err()) - { - return err(time_fmt_reg.unwrap_err()); - } - - fmt.has_seconds = std::get<1>(time_fmt_reg.unwrap()).has_seconds; - fmt.subsecond_precision = std::get<1>(time_fmt_reg.unwrap()).subsecond_precision; - - // ---------------------------------------------------------------------- - - region reg(first, loc); - local_datetime val(std::get<0>(date_fmt_reg.unwrap()), - std::get<0>(time_fmt_reg.unwrap())); - - return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); -} - -template -result, error_info> -parse_offset_datetime(location& loc, const context& ctx) -{ - using char_type = location::char_type; - - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - offset_datetime_format_info fmt; - - // ---------------------------------------------------------------------- - // date part - - auto date_fmt_reg = parse_local_date_only(loc, ctx); - if(date_fmt_reg.is_err()) - { - return err(date_fmt_reg.unwrap_err()); - } - - // ---------------------------------------------------------------------- - // delimiter - - if(loc.current() == char_type('T')) - { - loc.advance(); - fmt.delimiter = datetime_delimiter_kind::upper_T; - } - else if(loc.current() == char_type('t')) - { - loc.advance(); - fmt.delimiter = datetime_delimiter_kind::lower_t; - } - else if(loc.current() == char_type(' ')) - { - loc.advance(); - fmt.delimiter = datetime_delimiter_kind::space; - } - else - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_offset_datetime: " - "expect date-time delimiter `T` or ` `(space).", std::move(src), "here" - )); - } - - // ---------------------------------------------------------------------- - // time part - - auto time_fmt_reg = parse_local_time_only(loc, ctx); - if(time_fmt_reg.is_err()) - { - return err(time_fmt_reg.unwrap_err()); - } - - fmt.has_seconds = std::get<1>(time_fmt_reg.unwrap()).has_seconds; - fmt.subsecond_precision = std::get<1>(time_fmt_reg.unwrap()).subsecond_precision; - - // ---------------------------------------------------------------------- - // offset part - - const auto ofs_reg = syntax::time_offset(spec).scan(loc); - if( ! ofs_reg.is_ok()) - { - return err(make_syntax_error("toml::parse_offset_datetime: " - "invalid offset: offset must be like: Z, +01:00, or -10:00.", - syntax::time_offset(spec), loc)); - } - - const auto ofs_str = ofs_reg.as_string(); - - time_offset offset(0, 0); - - assert(ofs_str.size() != 0); - - if(ofs_str.at(0) == char_type('+') || ofs_str.at(0) == char_type('-')) - { - const auto hour_r = from_string(ofs_str.substr(1, 2)); - const auto minute_r = from_string(ofs_str.substr(4, 2)); - if(hour_r.is_err()) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_offset_datetime: " - "Failed to read offset hour part", std::move(src), "here")); - } - if(minute_r.is_err()) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_offset_datetime: " - "Failed to read offset minute part", std::move(src), "here")); - } - const auto hour = hour_r.unwrap(); - const auto minute = minute_r.unwrap(); - - if(ofs_str.at(0) == '+') - { - offset = time_offset(hour, minute); - } - else - { - offset = time_offset(-hour, -minute); - } - } - else - { - assert(ofs_str.at(0) == char_type('Z') || ofs_str.at(0) == char_type('z')); - } - - if (offset.hour < -24 || 24 < offset.hour || - offset.minute < -60 || 60 < offset.minute) - { - return err(make_error_info("toml::parse_offset_datetime: " - "too large offset: |hour| <= 24, |minute| <= 60", - source_location(region(first, loc)), "here")); - } - - - // ---------------------------------------------------------------------- - - region reg(first, loc); - offset_datetime val(local_datetime(std::get<0>(date_fmt_reg.unwrap()), - std::get<0>(time_fmt_reg.unwrap())), - offset); - - return ok(basic_value(val, std::move(fmt), {}, std::move(reg))); -} - -/* ============================================================================ - * ___ _ _ - * / __| |_ _ _(_)_ _ __ _ - * \__ \ _| '_| | ' \/ _` | - * |___/\__|_| |_|_||_\__, | - * |___/ - */ - -template -result::string_type, error_info> -parse_utf8_codepoint(const region& reg) -{ - using string_type = typename basic_value::string_type; - using char_type = typename string_type::value_type; - - // assert(reg.as_lines().size() == 1); // XXX heavy check - - const auto str = reg.as_string(); - assert( ! str.empty()); - assert(str.front() == 'u' || str.front() == 'U' || str.front() == 'x'); - - std::uint_least32_t codepoint; - std::istringstream iss(str.substr(1)); - iss >> std::hex >> codepoint; - - const auto to_char = [](const std::uint_least32_t i) noexcept -> char_type { - const auto uc = static_cast(i & 0xFF); - return cxx::bit_cast(uc); - }; - - string_type character; - if(codepoint < 0x80) // U+0000 ... U+0079 ; just an ASCII. - { - character += static_cast(codepoint); - } - else if(codepoint < 0x800) //U+0080 ... U+07FF - { - // 110yyyyx 10xxxxxx; 0x3f == 0b0011'1111 - character += to_char(0xC0|(codepoint >> 6 )); - character += to_char(0x80|(codepoint & 0x3F)); - } - else if(codepoint < 0x10000) // U+0800...U+FFFF - { - if(0xD800 <= codepoint && codepoint <= 0xDFFF) - { - auto src = source_location(reg); - return err(make_error_info("toml::parse_utf8_codepoint: " - "[0xD800, 0xDFFF] is not a valid UTF-8", - std::move(src), "here")); - } - assert(codepoint < 0xD800 || 0xDFFF < codepoint); - // 1110yyyy 10yxxxxx 10xxxxxx - character += to_char(0xE0| (codepoint >> 12)); - character += to_char(0x80|((codepoint >> 6 ) & 0x3F)); - character += to_char(0x80|((codepoint ) & 0x3F)); - } - else if(codepoint < 0x110000) // U+010000 ... U+10FFFF - { - // 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx - character += to_char(0xF0| (codepoint >> 18)); - character += to_char(0x80|((codepoint >> 12) & 0x3F)); - character += to_char(0x80|((codepoint >> 6 ) & 0x3F)); - character += to_char(0x80|((codepoint ) & 0x3F)); - } - else // out of UTF-8 region - { - auto src = source_location(reg); - return err(make_error_info("toml::parse_utf8_codepoint: " - "input codepoint is too large.", - std::move(src), "must be in range [0x00, 0x10FFFF]")); - } - return ok(character); -} - -template -result::string_type, error_info> -parse_escape_sequence(location& loc, const context& ctx) -{ - using string_type = typename basic_value::string_type; - using char_type = typename string_type::value_type; - - const auto& spec = ctx.toml_spec(); - - assert( ! loc.eof()); - assert(loc.current() == '\\'); - loc.advance(); // consume the first backslash - - string_type retval; - - if (loc.current() == '\\') { retval += char_type('\\'); loc.advance(); } - else if(loc.current() == '"') { retval += char_type('\"'); loc.advance(); } - else if(loc.current() == 'b') { retval += char_type('\b'); loc.advance(); } - else if(loc.current() == 'f') { retval += char_type('\f'); loc.advance(); } - else if(loc.current() == 'n') { retval += char_type('\n'); loc.advance(); } - else if(loc.current() == 'r') { retval += char_type('\r'); loc.advance(); } - else if(loc.current() == 't') { retval += char_type('\t'); loc.advance(); } - else if(spec.v1_1_0_add_escape_sequence_e && loc.current() == 'e') - { - retval += char_type('\x1b'); - loc.advance(); - } - else if(spec.v1_1_0_add_escape_sequence_x && loc.current() == 'x') - { - auto scanner = sequence(character('x'), repeat_exact(2, syntax::hexdig(spec))); - const auto reg = scanner.scan(loc); - if( ! reg.is_ok()) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_escape_sequence: " - "invalid token found in UTF-8 codepoint \\xhh", - std::move(src), "here")); - } - const auto utf8 = parse_utf8_codepoint(reg); - if(utf8.is_err()) - { - return err(utf8.as_err()); - } - retval += utf8.unwrap(); - } - else if(loc.current() == 'u') - { - auto scanner = sequence(character('u'), repeat_exact(4, syntax::hexdig(spec))); - const auto reg = scanner.scan(loc); - if( ! reg.is_ok()) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_escape_sequence: " - "invalid token found in UTF-8 codepoint \\uhhhh", - std::move(src), "here")); - } - const auto utf8 = parse_utf8_codepoint(reg); - if(utf8.is_err()) - { - return err(utf8.as_err()); - } - retval += utf8.unwrap(); - } - else if(loc.current() == 'U') - { - auto scanner = sequence(character('U'), repeat_exact(8, syntax::hexdig(spec))); - const auto reg = scanner.scan(loc); - if( ! reg.is_ok()) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_escape_sequence: " - "invalid token found in UTF-8 codepoint \\Uhhhhhhhh", - std::move(src), "here")); - } - const auto utf8 = parse_utf8_codepoint(reg); - if(utf8.is_err()) - { - return err(utf8.as_err()); - } - retval += utf8.unwrap(); - } - else - { - auto src = source_location(region(loc)); - std::string escape_seqs = "allowed escape seqs: \\\\, \\\", \\b, \\f, \\n, \\r, \\t"; - if(spec.v1_1_0_add_escape_sequence_e) - { - escape_seqs += ", \\e"; - } - if(spec.v1_1_0_add_escape_sequence_x) - { - escape_seqs += ", \\xhh"; - } - escape_seqs += ", \\uhhhh, or \\Uhhhhhhhh"; - - return err(make_error_info("toml::parse_escape_sequence: " - "unknown escape sequence.", std::move(src), escape_seqs)); - } - return ok(retval); -} - -template -result, error_info> -parse_ml_basic_string(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - string_format_info fmt; - fmt.fmt = string_format::multiline_basic; - - auto reg = syntax::ml_basic_string(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_ml_basic_string: " - "invalid string format", - syntax::ml_basic_string(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - - auto str = reg.as_string(); - - // we already checked that it starts with """ and ends with """. - assert(str.substr(0, 3) == "\"\"\""); - str.erase(0, 3); - - assert(str.size() >= 3); - assert(str.substr(str.size()-3, 3) == "\"\"\""); - str.erase(str.size()-3, 3); - - // the first newline just after """ is trimmed - if(str.size() >= 1 && str.at(0) == '\n') - { - str.erase(0, 1); - fmt.start_with_newline = true; - } - else if(str.size() >= 2 && str.at(0) == '\r' && str.at(1) == '\n') - { - str.erase(0, 2); - fmt.start_with_newline = true; - } - - using string_type = typename basic_value::string_type; - string_type val; - { - auto iter = str.cbegin(); - while(iter != str.cend()) - { - if(*iter == '\\') // remove whitespaces around escaped-newline - { - // we assume that the string is not too long to copy - auto loc2 = make_temporary_location(make_string(iter, str.cend())); - if(syntax::escaped_newline(spec).scan(loc2).is_ok()) - { - std::advance(iter, loc2.get_location()); // skip escaped newline and indent - // now iter points non-WS char - assert(iter == str.end() || (*iter != ' ' && *iter != '\t')); - } - else // normal escape seq. - { - auto esc = parse_escape_sequence(loc2, ctx); - - // syntax does not check its value. the unicode codepoint may be - // invalid, e.g. out-of-bound, [0xD800, 0xDFFF] - if(esc.is_err()) - { - return err(esc.unwrap_err()); - } - - val += esc.unwrap(); - std::advance(iter, loc2.get_location()); - } - } - else // we already checked the syntax. we don't need to check it again. - { - val += static_cast(*iter); - ++iter; - } - } - } - - return ok(basic_value( - std::move(val), std::move(fmt), {}, std::move(reg) - )); -} - -template -result::string_type, region>, error_info> -parse_basic_string_only(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - auto reg = syntax::basic_string(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_basic_string: " - "invalid string format", - syntax::basic_string(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - - auto str = reg.as_string(); - - assert(str.back() == '\"'); - str.pop_back(); - assert(str.at(0) == '\"'); - str.erase(0, 1); - - using string_type = typename basic_value::string_type; - using char_type = typename string_type::value_type; - string_type val; - - { - auto iter = str.begin(); - while(iter != str.end()) - { - if(*iter == '\\') - { - auto loc2 = make_temporary_location(make_string(iter, str.end())); - - auto esc = parse_escape_sequence(loc2, ctx); - - // syntax does not check its value. the unicode codepoint may be - // invalid, e.g. out-of-bound, [0xD800, 0xDFFF] - if(esc.is_err()) - { - return err(esc.unwrap_err()); - } - - val += esc.unwrap(); - std::advance(iter, loc2.get_location()); - } - else - { - val += char_type(*iter); // we already checked the syntax. - ++iter; - } - } - } - return ok(std::make_pair(val, reg)); -} - -template -result, error_info> -parse_basic_string(location& loc, const context& ctx) -{ - const auto first = loc; - - string_format_info fmt; - fmt.fmt = string_format::basic; - - auto val_res = parse_basic_string_only(loc, ctx); - if(val_res.is_err()) - { - return err(std::move(val_res.unwrap_err())); - } - auto val = std::move(val_res.unwrap().first ); - auto reg = std::move(val_res.unwrap().second); - - return ok(basic_value(std::move(val), std::move(fmt), {}, std::move(reg))); -} - -template -result, error_info> -parse_ml_literal_string(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - string_format_info fmt; - fmt.fmt = string_format::multiline_literal; - - auto reg = syntax::ml_literal_string(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_ml_literal_string: " - "invalid string format", - syntax::ml_literal_string(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - - auto str = reg.as_string(); - - assert(str.substr(0, 3) == "'''"); - assert(str.substr(str.size()-3, 3) == "'''"); - str.erase(0, 3); - str.erase(str.size()-3, 3); - - // the first newline just after """ is trimmed - if(str.size() >= 1 && str.at(0) == '\n') - { - str.erase(0, 1); - fmt.start_with_newline = true; - } - else if(str.size() >= 2 && str.at(0) == '\r' && str.at(1) == '\n') - { - str.erase(0, 2); - fmt.start_with_newline = true; - } - - using string_type = typename basic_value::string_type; - string_type val(str.begin(), str.end()); - - return ok(basic_value( - std::move(val), std::move(fmt), {}, std::move(reg) - )); -} - -template -result::string_type, region>, error_info> -parse_literal_string_only(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - auto reg = syntax::literal_string(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_literal_string: " - "invalid string format", - syntax::literal_string(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - - auto str = reg.as_string(); - - assert(str.back() == '\''); - str.pop_back(); - assert(str.at(0) == '\''); - str.erase(0, 1); - - using string_type = typename basic_value::string_type; - string_type val(str.begin(), str.end()); - - return ok(std::make_pair(std::move(val), std::move(reg))); -} - -template -result, error_info> -parse_literal_string(location& loc, const context& ctx) -{ - const auto first = loc; - - string_format_info fmt; - fmt.fmt = string_format::literal; - - auto val_res = parse_literal_string_only(loc, ctx); - if(val_res.is_err()) - { - return err(std::move(val_res.unwrap_err())); - } - auto val = std::move(val_res.unwrap().first ); - auto reg = std::move(val_res.unwrap().second); - - return ok(basic_value( - std::move(val), std::move(fmt), {}, std::move(reg) - )); -} - -template -result, error_info> -parse_string(location& loc, const context& ctx) -{ - const auto first = loc; - - if( ! loc.eof() && loc.current() == '"') - { - if(literal("\"\"\"").scan(loc).is_ok()) - { - loc = first; - return parse_ml_basic_string(loc, ctx); - } - else - { - loc = first; - return parse_basic_string(loc, ctx); - } - } - else if( ! loc.eof() && loc.current() == '\'') - { - if(literal("'''").scan(loc).is_ok()) - { - loc = first; - return parse_ml_literal_string(loc, ctx); - } - else - { - loc = first; - return parse_literal_string(loc, ctx); - } - } - else - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_string: " - "not a string", std::move(src), "here")); - } -} - -template -result, error_info> -parse_null(location& loc, const context& ctx) -{ - const auto& spec = ctx.toml_spec(); - if( ! spec.ext_null_value) - { - return err(make_error_info("toml::parse_null: " - "invalid spec: spec.ext_null_value must be true.", - source_location(region(loc)), "here")); - } - - // ---------------------------------------------------------------------- - // check syntax - auto reg = syntax::null_value(spec).scan(loc); - if( ! reg.is_ok()) - { - return err(make_syntax_error("toml::parse_null: " - "invalid null: null must be lowercase. ", - syntax::null_value(spec), loc)); - } - - // ---------------------------------------------------------------------- - // it matches. gen value - - // ---------------------------------------------------------------------- - // no format info for boolean - - return ok(basic_value(detail::none_t{}, std::move(reg))); -} - -/* ============================================================================ - * _ __ - * | |/ /___ _ _ - * | ' -result::key_type, error_info> -parse_simple_key(location& loc, const context& ctx) -{ - using key_type = typename basic_value::key_type; - const auto& spec = ctx.toml_spec(); - - if(loc.current() == '\"') - { - auto str_res = parse_basic_string_only(loc, ctx); - if(str_res.is_ok()) - { - return ok(std::move(str_res.unwrap().first)); - } - else - { - return err(std::move(str_res.unwrap_err())); - } - } - else if(loc.current() == '\'') - { - auto str_res = parse_literal_string_only(loc, ctx); - if(str_res.is_ok()) - { - return ok(std::move(str_res.unwrap().first)); - } - else - { - return err(std::move(str_res.unwrap_err())); - } - } - - // bare key. - - if(const auto bare = syntax::unquoted_key(spec).scan(loc)) - { - return ok(string_conv(bare.as_string())); - } - else - { - std::string postfix; - if(spec.v1_1_0_allow_non_english_in_bare_keys) - { - postfix = "Hint: Not all Unicode characters are allowed as bare key.\n"; - } - else - { - postfix = "Hint: non-ASCII scripts are allowed in toml v1.1.0, but not in v1.0.0.\n"; - } - return err(make_syntax_error("toml::parse_simple_key: " - "invalid key: key must be \"quoted\", 'quoted-literal', or bare key.", - syntax::unquoted_key(spec), loc, postfix)); - } -} - -// dotted key become vector of keys -template -result::key_type>, region>, error_info> -parse_key(location& loc, const context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - using key_type = typename basic_value::key_type; - std::vector keys; - while( ! loc.eof()) - { - auto key = parse_simple_key(loc, ctx); - if( ! key.is_ok()) - { - return err(key.unwrap_err()); - } - keys.push_back(std::move(key.unwrap())); - - auto reg = syntax::dot_sep(spec).scan(loc); - if( ! reg.is_ok()) - { - break; - } - } - if(keys.empty()) - { - auto src = source_location(region(first)); - return err(make_error_info("toml::parse_key: expected a new key, " - "but got nothing", std::move(src), "reached EOF")); - } - - return ok(std::make_pair(std::move(keys), region(first, loc))); -} - -// ============================================================================ - -// forward-decl to implement parse_array and parse_table -template -result, error_info> -parse_value(location&, context& ctx); - -template -result::key_type>, region>, - basic_value - >, error_info> -parse_key_value_pair(location& loc, context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - auto key_res = parse_key(loc, ctx); - if(key_res.is_err()) - { - loc = first; - return err(key_res.unwrap_err()); - } - - if( ! syntax::keyval_sep(spec).scan(loc).is_ok()) - { - auto e = make_syntax_error("toml::parse_key_value_pair: " - "invalid key value separator `=`", syntax::keyval_sep(spec), loc); - loc = first; - return err(std::move(e)); - } - - auto v_res = parse_value(loc, ctx); - if(v_res.is_err()) - { - // loc = first; - return err(v_res.unwrap_err()); - } - return ok(std::make_pair(std::move(key_res.unwrap()), std::move(v_res.unwrap()))); -} - -/* ============================================================================ - * __ _ _ _ _ _ __ _ _ _ - * / _` | '_| '_/ _` | || | - * \__,_|_| |_| \__,_|\_, | - * |__/ - */ - -// array(and multiline inline table with `{` and `}`) has the following format. -// `[` -// (ws|newline|comment-line)? (value) (ws|newline|comment-line)? `,` -// (ws|newline|comment-line)? (value) (ws|newline|comment-line)? `,` -// ... -// (ws|newline|comment-line)? (value) (ws|newline|comment-line)? (`,`)? -// (ws|newline|comment-line)? `]` -// it skips (ws|newline|comment-line) and returns the token. -template -struct multiline_spacer -{ - using comment_type = typename TC::comment_type; - bool newline_found; - indent_char indent_type; - std::int32_t indent; - comment_type comments; -}; -template -std::ostream& operator<<(std::ostream& os, const multiline_spacer& sp) -{ - os << "{newline=" << sp.newline_found << ", "; - os << "indent_type=" << sp.indent_type << ", "; - os << "indent=" << sp.indent << ", "; - os << "comments=" << sp.comments.size() << "}"; - return os; -} - -template -cxx::optional> -skip_multiline_spacer(location& loc, context& ctx, const bool newline_found = false) -{ - const auto& spec = ctx.toml_spec(); - - multiline_spacer spacer; - spacer.newline_found = newline_found; - spacer.indent_type = indent_char::none; - spacer.indent = 0; - spacer.comments.clear(); - - bool spacer_found = false; - while( ! loc.eof()) - { - if(auto comm = sequence(syntax::comment(spec), syntax::newline(spec)).scan(loc)) - { - spacer.newline_found = true; - auto comment = comm.as_string(); - if( ! comment.empty() && comment.back() == '\n') - { - comment.pop_back(); - if (!comment.empty() && comment.back() == '\r') - { - comment.pop_back(); - } - } - - spacer.comments.push_back(std::move(comment)); - spacer.indent_type = indent_char::none; - spacer.indent = 0; - spacer_found = true; - } - else if(auto nl = syntax::newline(spec).scan(loc)) - { - spacer.newline_found = true; - spacer.comments.clear(); - spacer.indent_type = indent_char::none; - spacer.indent = 0; - spacer_found = true; - } - else if(auto sp = repeat_at_least(1, character(cxx::bit_cast(' '))).scan(loc)) - { - spacer.indent_type = indent_char::space; - spacer.indent = static_cast(sp.length()); - spacer_found = true; - } - else if(auto tabs = repeat_at_least(1, character(cxx::bit_cast('\t'))).scan(loc)) - { - spacer.indent_type = indent_char::tab; - spacer.indent = static_cast(tabs.length()); - spacer_found = true; - } - else - { - break; // done - } - } - if( ! spacer_found) - { - return cxx::make_nullopt(); - } - return spacer; -} - -// not an [[array.of.tables]]. It parses ["this", "type"] -template -result, error_info> -parse_array(location& loc, context& ctx) -{ - const auto num_errors = ctx.errors().size(); - - const auto first = loc; - - if(loc.eof() || loc.current() != '[') - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_array: " - "The next token is not an array", std::move(src), "here")); - } - loc.advance(); - - typename basic_value::array_type val; - - array_format_info fmt; - fmt.fmt = array_format::oneline; - fmt.indent_type = indent_char::none; - - auto spacer = skip_multiline_spacer(loc, ctx); - if(spacer.has_value() && spacer.value().newline_found) - { - fmt.fmt = array_format::multiline; - } - - bool comma_found = true; - while( ! loc.eof()) - { - if(loc.current() == location::char_type(']')) - { - if(spacer.has_value() && spacer.value().newline_found && - spacer.value().indent_type != indent_char::none) - { - fmt.indent_type = spacer.value().indent_type; - fmt.closing_indent = spacer.value().indent; - } - break; - } - - if( ! comma_found) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_array: " - "expected value-separator `,` or closing `]`", - std::move(src), "here")); - } - - if(spacer.has_value() && spacer.value().newline_found && - spacer.value().indent_type != indent_char::none) - { - fmt.indent_type = spacer.value().indent_type; - fmt.body_indent = spacer.value().indent; - } - - if(auto elem_res = parse_value(loc, ctx)) - { - auto elem = std::move(elem_res.unwrap()); - - if(spacer.has_value()) // copy previous comments to value - { - elem.comments() = std::move(spacer.value().comments); - } - - // parse spaces between a value and a comma - // array = [ - // 42 , # the answer - // ^^^^ - // 3.14 # pi - // , 2.71 ^^^^ - // ^^ - spacer = skip_multiline_spacer(loc, ctx); - if(spacer.has_value()) - { - for(std::size_t i=0; i( - std::move(val), std::move(fmt), {}, region(first, loc) - )); -} - -/* ============================================================================ - * _ _ _ _ _ _ - * (_)_ _ | (_)_ _ ___ | |_ __ _| |__| |___ - * | | ' \| | | ' \/ -_) | _/ _` | '_ \ / -_) - * |_|_||_|_|_|_||_\___| \__\__,_|_.__/_\___| - */ - -// ---------------------------------------------------------------------------- -// insert_value is the most complicated part of the toml spec. -// -// To parse a toml file correctly, we sometimes need to check an exising value -// is appendable or not. -// -// For example while parsing an inline array of tables, -// -// ```toml -// aot = [ -// {a = "foo"}, -// {a = "bar", b = "baz"}, -// ] -// ``` -// -// this `aot` is appendable until parser reaches to `]`. After that, it becomes -// non-appendable. -// -// On the other hand, a normal array of tables, such as -// -// ```toml -// [[aot]] -// a = "foo" -// -// [[aot]] -// a = "bar" -// b = "baz" -// ``` -// This `[[aot]]` is appendable until the parser reaches to the EOF. -// -// -// It becomes a bit more difficult in case of dotted keys. -// In TOML, it is allowed to append a key-value pair to a table that is -// *implicitly* defined by a subtable definitino. -// -// ```toml -// [x.y.z] -// w = 123 -// -// [x] -// a = "foo" # OK. x is defined implicitly by `[x.y.z]`. -// ``` -// -// But if the table is defined by a dotted keys, it is not appendable. -// -// ```toml -// [x] -// y.z.w = 123 -// -// [x.y] -// # ERROR. x.y is already defined by a dotted table in the previous table. -// ``` -// -// Also, reopening a table using dotted keys is invalid. -// -// ```toml -// [x.y.z] -// w = 123 -// -// [x] -// y.z.v = 42 # ERROR. [x.y.z] is already defined. -// ``` -// -// -// ```toml -// [a] -// b.c = "foo" -// b.d = "bar" -// ``` -// -// -// ```toml -// a.b = "foo" -// [a] -// c = "bar" # ERROR -// ``` -// -// In summary, -// - a table must be defined only once. -// - assignment to an exising table is possible only when: -// - defining a subtable [x.y] to an existing table [x]. -// - defining supertable [x] explicitly after [x.y]. -// - adding dotted keys in the same table. - -enum class inserting_value_kind : std::uint8_t -{ - std_table, // insert [standard.table] - array_table, // insert [[array.of.tables]] - dotted_keys // insert a.b.c = "this" -}; - -template -result*, error_info> -insert_value(const inserting_value_kind kind, - typename basic_value::table_type* current_table_ptr, - const std::vector::key_type>& keys, region key_reg, - basic_value val) -{ - using value_type = basic_value; - using array_type = typename basic_value::array_type; - using table_type = typename basic_value::table_type; - - auto key_loc = source_location(key_reg); - - assert( ! keys.empty()); - - // dotted key can insert to dotted key tables defined at the same level. - // dotted key can NOT reopen a table even if it is implcitly-defined one. - // - // [x.y.z] # define x and x.y implicitly. - // a = 42 - // - // [x] # reopening implcitly defined table - // r.s.t = 3.14 # VALID r and r.s are new tables. - // r.s.u = 2.71 # VALID r and r.s are dotted-key tables. valid. - // - // y.z.b = "foo" # INVALID x.y.z are multiline table, not a dotted key. - // y.c = "bar" # INVALID x.y is implicit multiline table, not a dotted key. - - // a table cannot reopen dotted-key tables. - // - // [t1] - // t2.t3.v = 0 - // [t1.t2] # INVALID t1.t2 is defined as a dotted-key table. - - for(std::size_t i=0; i{}, key_reg)); - - assert(current_table.at(key).is_table()); - current_table_ptr = std::addressof(current_table.at(key).as_table()); - } - else if (found->second.is_table()) - { - const auto fmt = found->second.as_table_fmt().fmt; - if(fmt == table_format::oneline || fmt == table_format::multiline_oneline) - { - // foo = {bar = "baz"} or foo = { \n bar = "baz" \n } - return err(make_error_info("toml::insert_value: " - "failed to insert a value: inline table is immutable", - key_loc, "inserting this", - found->second.location(), "to this table")); - } - // dotted key cannot reopen a table. - if(kind ==inserting_value_kind::dotted_keys && fmt != table_format::dotted) - { - return err(make_error_info("toml::insert_value: " - "reopening a table using dotted keys", - key_loc, "dotted key cannot reopen a table", - found->second.location(), "this table is already closed")); - } - assert(found->second.is_table()); - current_table_ptr = std::addressof(found->second.as_table()); - } - else if(found->second.is_array_of_tables()) - { - // aot = [{this = "type", of = "aot"}] # cannot be reopened - if(found->second.as_array_fmt().fmt != array_format::array_of_tables) - { - return err(make_error_info("toml::insert_value:" - "inline array of tables are immutable", - key_loc, "inserting this", - found->second.location(), "inline array of tables")); - } - // appending to [[aot]] - - if(kind == inserting_value_kind::dotted_keys) - { - // [[array.of.tables]] - // [array.of] # reopening supertable is okay - // tables.x = "foo" # appending `x` to the first table - return err(make_error_info("toml::insert_value:" - "dotted key cannot reopen an array-of-tables", - key_loc, "inserting this", - found->second.location(), "to this array-of-tables.")); - } - - // insert_value_by_dotkeys::std_table - // [[array.of.tables]] - // [array.of.tables.subtable] # appending to the last aot - // - // insert_value_by_dotkeys::array_table - // [[array.of.tables]] - // [[array.of.tables.subtable]] # appending to the last aot - auto& current_array_table = found->second.as_array().back(); - - assert(current_array_table.is_table()); - current_table_ptr = std::addressof(current_array_table.as_table()); - } - else - { - return err(make_error_info("toml::insert_value: " - "failed to insert a value, value already exists", - key_loc, "while inserting this", - found->second.location(), "non-table value already exists")); - } - } - else // this is the last key. insert a new value. - { - switch(kind) - { - case inserting_value_kind::dotted_keys: - { - if(current_table.find(key) != current_table.end()) - { - return err(make_error_info("toml::insert_value: " - "failed to insert a value, value already exists", - key_loc, "inserting this", - current_table.at(key).location(), "but value already exists")); - } - current_table.emplace(key, std::move(val)); - return ok(std::addressof(current_table.at(key))); - } - case inserting_value_kind::std_table: - { - // defining a new table or reopening supertable - auto found = current_table.find(key); - if(found == current_table.end()) // define a new aot - { - current_table.emplace(key, std::move(val)); - return ok(std::addressof(current_table.at(key))); - } - else // the table is already defined, reopen it - { - // assigning a [std.table]. it must be an implicit table. - auto& target = found->second; - if( ! target.is_table() || // could be an array-of-tables - target.as_table_fmt().fmt != table_format::implicit) - { - return err(make_error_info("toml::insert_value: " - "failed to insert a table, table already defined", - key_loc, "inserting this", - target.location(), "this table is explicitly defined")); - } - - // merge table - for(const auto& kv : val.as_table()) - { - if(target.contains(kv.first)) - { - // [x.y.z] - // w = "foo" - // [x] - // y = "bar" - return err(make_error_info("toml::insert_value: " - "failed to insert a table, table keys conflict to each other", - key_loc, "inserting this table", - kv.second.location(), "having this value", - target.at(kv.first).location(), "already defined here")); - } - else - { - target[kv.first] = kv.second; - } - } - // change implicit -> explicit - target.as_table_fmt().fmt = table_format::multiline; - // change definition region - change_region_of_value(target, val); - - return ok(std::addressof(current_table.at(key))); - } - } - case inserting_value_kind::array_table: - { - auto found = current_table.find(key); - if(found == current_table.end()) // define a new aot - { - array_format_info fmt; - fmt.fmt = array_format::array_of_tables; - fmt.indent_type = indent_char::none; - - current_table.emplace(key, value_type( - array_type{ std::move(val) }, std::move(fmt), - std::vector{}, std::move(key_reg) - )); - - assert( ! current_table.at(key).as_array().empty()); - return ok(std::addressof(current_table.at(key).as_array().back())); - } - else // the array is already defined, append to it - { - if( ! found->second.is_array_of_tables()) - { - return err(make_error_info("toml::insert_value: " - "failed to insert an array of tables, value already exists", - key_loc, "while inserting this", - found->second.location(), "non-table value already exists")); - } - if(found->second.as_array_fmt().fmt != array_format::array_of_tables) - { - return err(make_error_info("toml::insert_value: " - "failed to insert a table, inline array of tables is immutable", - key_loc, "while inserting this", - found->second.location(), "this is inline array-of-tables")); - } - found->second.as_array().push_back(std::move(val)); - assert( ! current_table.at(key).as_array().empty()); - return ok(std::addressof(current_table.at(key).as_array().back())); - } - } - default: {assert(false);} - } - } - } - return err(make_error_info("toml::insert_key: no keys found", - std::move(key_loc), "here")); -} - -// ---------------------------------------------------------------------------- - -template -result, error_info> -parse_inline_table(location& loc, context& ctx) -{ - using table_type = typename basic_value::table_type; - - const auto num_errors = ctx.errors().size(); - - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - if(loc.eof() || loc.current() != '{') - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_inline_table: " - "The next token is not an inline table", std::move(src), "here")); - } - loc.advance(); - - table_type table; - table_format_info fmt; - fmt.fmt = table_format::oneline; - fmt.indent_type = indent_char::none; - - cxx::optional> spacer(cxx::make_nullopt()); - - if(spec.v1_1_0_allow_newlines_in_inline_tables) - { - spacer = skip_multiline_spacer(loc, ctx); - if(spacer.has_value() && spacer.value().newline_found) - { - fmt.fmt = table_format::multiline_oneline; - } - } - else - { - skip_whitespace(loc, ctx); - } - - bool still_empty = true; - bool comma_found = false; - while( ! loc.eof()) - { - // closing! - if(loc.current() == '}') - { - if(comma_found && ! spec.v1_1_0_allow_trailing_comma_in_inline_tables) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_inline_table: trailing " - "comma is not allowed in TOML-v1.0.0)", std::move(src), "here")); - } - - if(spec.v1_1_0_allow_newlines_in_inline_tables) - { - if(spacer.has_value() && spacer.value().newline_found && - spacer.value().indent_type != indent_char::none) - { - fmt.indent_type = spacer.value().indent_type; - fmt.closing_indent = spacer.value().indent; - } - } - break; - } - - // if we already found a value and didn't found `,` nor `}`, error. - if( ! comma_found && ! still_empty) - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_inline_table: " - "expected value-separator `,` or closing `}`", - std::move(src), "here")); - } - - // parse indent. - if(spacer.has_value() && spacer.value().newline_found && - spacer.value().indent_type != indent_char::none) - { - fmt.indent_type = spacer.value().indent_type; - fmt.body_indent = spacer.value().indent; - } - - still_empty = false; // parsing a value... - if(auto kv_res = parse_key_value_pair(loc, ctx)) - { - auto keys = std::move(kv_res.unwrap().first.first); - auto key_reg = std::move(kv_res.unwrap().first.second); - auto val = std::move(kv_res.unwrap().second); - - auto ins_res = insert_value(inserting_value_kind::dotted_keys, - std::addressof(table), keys, std::move(key_reg), std::move(val)); - if(ins_res.is_err()) - { - ctx.report_error(std::move(ins_res.unwrap_err())); - // we need to skip until the next value (or end of the table) - // because we don't have valid kv pair. - while( ! loc.eof()) - { - const auto c = loc.current(); - if(c == ',' || c == '\n' || c == '}') - { - comma_found = (c == ','); - break; - } - loc.advance(); - } - continue; - } - - // if comment line follows immediately(without newline) after `,`, then - // the comment is for the elem. we need to check if comment follows `,`. - // - // (key) = (val) (ws|newline|comment-line)? `,` (ws)? (comment)? - - if(spec.v1_1_0_allow_newlines_in_inline_tables) - { - if(spacer.has_value()) // copy previous comments to value - { - for(std::size_t i=0; icomments().push_back(spacer.value().comments.at(i)); - } - } - spacer = skip_multiline_spacer(loc, ctx); - if(spacer.has_value()) - { - for(std::size_t i=0; icomments().push_back(spacer.value().comments.at(i)); - } - if(spacer.value().newline_found) - { - fmt.fmt = table_format::multiline_oneline; - if(spacer.value().indent_type != indent_char::none) - { - fmt.indent_type = spacer.value().indent_type; - fmt.body_indent = spacer.value().indent; - } - } - } - } - else - { - skip_whitespace(loc, ctx); - } - - comma_found = character(',').scan(loc).is_ok(); - - if(spec.v1_1_0_allow_newlines_in_inline_tables) - { - auto com_res = parse_comment_line(loc, ctx); - if(com_res.is_err()) - { - ctx.report_error(com_res.unwrap_err()); - } - const bool comment_found = com_res.is_ok() && com_res.unwrap().has_value(); - if(comment_found) - { - fmt.fmt = table_format::multiline_oneline; - ins_res.unwrap()->comments().push_back(com_res.unwrap().value()); - } - if(comma_found) - { - spacer = skip_multiline_spacer(loc, ctx, comment_found); - if(spacer.has_value() && spacer.value().newline_found) - { - fmt.fmt = table_format::multiline_oneline; - } - } - } - else - { - skip_whitespace(loc, ctx); - } - } - else - { - ctx.report_error(std::move(kv_res.unwrap_err())); - while( ! loc.eof()) - { - if(loc.current() == '}') - { - break; - } - if( ! spec.v1_1_0_allow_newlines_in_inline_tables && loc.current() == '\n') - { - break; - } - loc.advance(); - } - break; - } - } - - if(loc.current() != '}') - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_inline_table: " - "missing closing bracket `}`", - std::move(src), "expected `}`, reached line end")); - } - else - { - loc.advance(); // skip } - } - - // any error reported from this function - if(num_errors < ctx.errors().size()) - { - assert(ctx.has_error()); // already reported - return err(ctx.pop_last_error()); - } - - basic_value retval( - std::move(table), std::move(fmt), {}, region(first, loc)); - - return ok(std::move(retval)); -} - -/* ============================================================================ - * _ - * __ ____ _| |_ _ ___ - * \ V / _` | | || / -_) - * \_/\__,_|_|\_,_\___| - */ - -template -result -guess_number_type(const location& first, const context& ctx) -{ - const auto& spec = ctx.toml_spec(); - location loc = first; - - if(syntax::offset_datetime(spec).scan(loc).is_ok()) - { - return ok(value_t::offset_datetime); - } - loc = first; - - if(syntax::local_datetime(spec).scan(loc).is_ok()) - { - const auto curr = loc.current(); - // if offset_datetime contains bad offset, it syntax::offset_datetime - // fails to scan it. - if(curr == '+' || curr == '-') - { - return err(make_syntax_error("bad offset: must be [+-]HH:MM or Z", - syntax::time_offset(spec), loc, std::string( - "Hint: valid : +09:00, -05:30\n" - "Hint: invalid: +9:00, -5:30\n"))); - } - return ok(value_t::local_datetime); - } - loc = first; - - if(syntax::local_date(spec).scan(loc).is_ok()) - { - // bad time may appear after this. - - if( ! loc.eof()) - { - const auto c = loc.current(); - if(c == 'T' || c == 't') - { - loc.advance(); - - return err(make_syntax_error("bad time: must be HH:MM:SS.subsec", - syntax::local_time(spec), loc, std::string( - "Hint: valid : 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999\n" - "Hint: invalid: 1979-05-27T7:32:00, 1979-05-27 17:32\n"))); - } - if(c == ' ') - { - // A space is allowed as a delimiter between local time. - // But there is a case where bad time follows a space. - // - invalid: 2019-06-16 7:00:00 - // - valid : 2019-06-16 07:00:00 - loc.advance(); - if( ! loc.eof() && ('0' <= loc.current() && loc.current() <= '9')) - { - return err(make_syntax_error("bad time: must be HH:MM:SS.subsec", - syntax::local_time(spec), loc, std::string( - "Hint: valid : 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999\n" - "Hint: invalid: 1979-05-27T7:32:00, 1979-05-27 17:32\n"))); - } - } - if('0' <= c && c <= '9') - { - return err(make_syntax_error("bad datetime: missing T or space", - character_either{'T', 't', ' '}, loc, std::string( - "Hint: valid : 1979-05-27T07:32:00, 1979-05-27 07:32:00.999999\n" - "Hint: invalid: 1979-05-27T7:32:00, 1979-05-27 17:32\n"))); - } - } - return ok(value_t::local_date); - } - loc = first; - - if(syntax::local_time(spec).scan(loc).is_ok()) - { - return ok(value_t::local_time); - } - loc = first; - - if(syntax::floating(spec).scan(loc).is_ok()) - { - if( ! loc.eof() && loc.current() == '_') - { - if(spec.ext_num_suffix && syntax::num_suffix(spec).scan(loc).is_ok()) - { - return ok(value_t::floating); - } - auto src = source_location(region(loc)); - return err(make_error_info( - "bad float: `_` must be surrounded by digits", - std::move(src), "invalid underscore", - "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" - "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n")); - } - return ok(value_t::floating); - } - loc = first; - - if(spec.ext_hex_float) - { - if(syntax::hex_floating(spec).scan(loc).is_ok()) - { - if( ! loc.eof() && loc.current() == '_') - { - if(spec.ext_num_suffix && syntax::num_suffix(spec).scan(loc).is_ok()) - { - return ok(value_t::floating); - } - auto src = source_location(region(loc)); - return err(make_error_info( - "bad float: `_` must be surrounded by digits", - std::move(src), "invalid underscore", - "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" - "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n")); - } - return ok(value_t::floating); - } - loc = first; - } - - if(auto int_reg = syntax::integer(spec).scan(loc)) - { - if( ! loc.eof()) - { - const auto c = loc.current(); - if(c == '_') - { - if(spec.ext_num_suffix && syntax::num_suffix(spec).scan(loc).is_ok()) - { - return ok(value_t::integer); - } - - if(int_reg.length() <= 2 && (int_reg.as_string() == "0" || - int_reg.as_string() == "-0" || int_reg.as_string() == "+0")) - { - auto src = source_location(region(loc)); - return err(make_error_info( - "bad integer: leading zero is not allowed in decimal int", - std::move(src), "leading zero", - "Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" - "Hint: invalid: _42, 1__000, 0123\n")); - } - else - { - auto src = source_location(region(loc)); - return err(make_error_info( - "bad integer: `_` must be surrounded by digits", - std::move(src), "invalid underscore", - "Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" - "Hint: invalid: _42, 1__000, 0123\n")); - } - } - if('0' <= c && c <= '9') - { - if(loc.current() == '0') - { - loc.retrace(); - return err(make_error_info( - "bad integer: leading zero", - source_location(region(loc)), "leading zero is not allowed", - std::string("Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" - "Hint: invalid: _42, 1__000, 0123\n") - )); - } - else // invalid digits, especially in oct/bin ints. - { - return err(make_error_info( - "bad integer: invalid digit after an integer", - source_location(region(loc)), "this digit is not allowed", - std::string("Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" - "Hint: invalid: _42, 1__000, 0123\n") - )); - } - } - if(c == ':' || c == '-') - { - auto src = source_location(region(loc)); - return err(make_error_info("bad datetime: invalid format", - std::move(src), "here", - std::string("Hint: valid : 1979-05-27T07:32:00-07:00, 1979-05-27 07:32:00.999999Z\n" - "Hint: invalid: 1979-05-27T7:32:00-7:00, 1979-05-27 7:32-00:30") - )); - } - if(c == '.' || c == 'e' || c == 'E') - { - auto src = source_location(region(loc)); - return err(make_error_info("bad float: invalid format", - std::move(src), "here", std::string( - "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" - "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n"))); - } - } - return ok(value_t::integer); - } - if( ! loc.eof() && loc.current() == '.') - { - auto src = source_location(region(loc)); - return err(make_error_info("bad float: integer part is required before decimal point", - std::move(src), "missing integer part", std::string( - "Hint: valid : +1.0, -2e-2, 3.141_592_653_589, inf, nan\n" - "Hint: invalid: .0, 1., _1.0, 1.0_, 1_.0, 1.0__0\n") - )); - } - if( ! loc.eof() && loc.current() == '_') - { - auto src = source_location(region(loc)); - return err(make_error_info("bad number: `_` must be surrounded by digits", - std::move(src), "digits required before `_`", std::string( - "Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755\n" - "Hint: invalid: _42, 1__000, 0123\n") - )); - } - - auto src = source_location(region(loc)); - return err(make_error_info("bad format: unknown value appeared", - std::move(src), "here")); -} - -template -result -guess_value_type(const location& loc, const context& ctx) -{ - const auto& sp = ctx.toml_spec(); - location inner(loc); - - switch(loc.current()) - { - case '"' : {return ok(value_t::string); } - case '\'': {return ok(value_t::string); } - case '[' : {return ok(value_t::array); } - case '{' : {return ok(value_t::table); } - case 't' : - { - return ok(value_t::boolean); - } - case 'f' : - { - return ok(value_t::boolean); - } - case 'T' : // invalid boolean. - { - return err(make_syntax_error("toml::parse_value: " - "`true` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::boolean(sp), inner)); - } - case 'F' : - { - return err(make_syntax_error("toml::parse_value: " - "`false` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::boolean(sp), inner)); - } - case 'i' : // inf or string without quotes(syntax error). - { - if(literal("inf").scan(inner).is_ok()) - { - return ok(value_t::floating); - } - else - { - return err(make_syntax_error("toml::parse_value: " - "`inf` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::floating(sp), inner)); - } - } - case 'I' : // Inf or string without quotes(syntax error). - { - return err(make_syntax_error("toml::parse_value: " - "`inf` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::floating(sp), inner)); - } - case 'n' : // nan or null-extension - { - if(sp.ext_null_value) - { - if(literal("nan").scan(inner).is_ok()) - { - return ok(value_t::floating); - } - else if(literal("null").scan(inner).is_ok()) - { - return ok(value_t::empty); - } - else - { - return err(make_syntax_error("toml::parse_value: " - "Both `nan` and `null` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::floating(sp), inner)); - } - } - else // must be nan. - { - if(literal("nan").scan(inner).is_ok()) - { - return ok(value_t::floating); - } - else - { - return err(make_syntax_error("toml::parse_value: " - "`nan` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::floating(sp), inner)); - } - } - } - case 'N' : // nan or null-extension - { - if(sp.ext_null_value) - { - return err(make_syntax_error("toml::parse_value: " - "Both `nan` and `null` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::floating(sp), inner)); - } - else - { - return err(make_syntax_error("toml::parse_value: " - "`nan` must be in lowercase. " - "A string must be surrounded by quotes.", - syntax::floating(sp), inner)); - } - } - default : - { - return guess_number_type(loc, ctx); - } - } -} - -template -result, error_info> -parse_value(location& loc, context& ctx) -{ - const auto ty_res = guess_value_type(loc, ctx); - if(ty_res.is_err()) - { - return err(ty_res.unwrap_err()); - } - - switch(ty_res.unwrap()) - { - case value_t::empty: - { - if(ctx.toml_spec().ext_null_value) - { - return parse_null(loc, ctx); - } - else - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_value: unknown value appeared", - std::move(src), "here")); - } - } - case value_t::boolean : {return parse_boolean (loc, ctx);} - case value_t::integer : {return parse_integer (loc, ctx);} - case value_t::floating : {return parse_floating (loc, ctx);} - case value_t::string : {return parse_string (loc, ctx);} - case value_t::offset_datetime: {return parse_offset_datetime(loc, ctx);} - case value_t::local_datetime : {return parse_local_datetime (loc, ctx);} - case value_t::local_date : {return parse_local_date (loc, ctx);} - case value_t::local_time : {return parse_local_time (loc, ctx);} - case value_t::array : {return parse_array (loc, ctx);} - case value_t::table : {return parse_inline_table (loc, ctx);} - default: - { - auto src = source_location(region(loc)); - return err(make_error_info("toml::parse_value: unknown value appeared", - std::move(src), "here")); - } - } -} - -/* ============================================================================ - * _____ _ _ - * |_ _|_ _| |__| |___ - * | |/ _` | '_ \ / -_) - * |_|\__,_|_.__/_\___| - */ - -template -result::key_type>, region>, error_info> -parse_table_key(location& loc, context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - auto reg = syntax::std_table(spec).scan(loc); - if(!reg.is_ok()) - { - return err(make_syntax_error("toml::parse_table_key: invalid table key", - syntax::std_table(spec), loc)); - } - - loc = first; - loc.advance(); // skip [ - skip_whitespace(loc, ctx); - - auto keys_res = parse_key(loc, ctx); - if(keys_res.is_err()) - { - return err(std::move(keys_res.unwrap_err())); - } - - skip_whitespace(loc, ctx); - loc.advance(); // ] - - return ok(std::make_pair(std::move(keys_res.unwrap().first), std::move(reg))); -} - -template -result::key_type>, region>, error_info> -parse_array_table_key(location& loc, context& ctx) -{ - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - auto reg = syntax::array_table(spec).scan(loc); - if(!reg.is_ok()) - { - return err(make_syntax_error("toml::parse_array_table_key: invalid array-of-tables key", - syntax::array_table(spec), loc)); - } - - loc = first; - loc.advance(); // [ - loc.advance(); // [ - skip_whitespace(loc, ctx); - - auto keys_res = parse_key(loc, ctx); - if(keys_res.is_err()) - { - return err(std::move(keys_res.unwrap_err())); - } - - skip_whitespace(loc, ctx); - loc.advance(); // ] - loc.advance(); // ] - - return ok(std::make_pair(std::move(keys_res.unwrap().first), std::move(reg))); -} - -// called after reading [table.keys] and comments around it. -// Since table may already contain a subtable ([x.y.z] can be defined before [x]), -// the table that is being parsed is passed as an argument. -template -result -parse_table(location& loc, context& ctx, basic_value& table) -{ - assert(table.is_table()); - - const auto num_errors = ctx.errors().size(); - const auto& spec = ctx.toml_spec(); - - // clear indent info - table.as_table_fmt().indent_type = indent_char::none; - - bool newline_found = true; - while( ! loc.eof()) - { - const auto start = loc; - - auto sp = skip_multiline_spacer(loc, ctx, newline_found); - - // if reached to EOF, the table ends here. return. - if(loc.eof()) - { - break; - } - // if next table is comming, return. - if(sequence(syntax::ws(spec), character('[')).scan(loc).is_ok()) - { - loc = start; - break; - } - // otherwise, it should be a key-value pair. - newline_found = newline_found || (sp.has_value() && sp.value().newline_found); - if( ! newline_found) - { - return err(make_error_info("toml::parse_table: " - "newline (LF / CRLF) or EOF is expected", - source_location(region(loc)), "here")); - } - if(sp.has_value() && sp.value().indent_type != indent_char::none) - { - table.as_table_fmt().indent_type = sp.value().indent_type; - table.as_table_fmt().body_indent = sp.value().indent; - } - - newline_found = false; // reset - if(auto kv_res = parse_key_value_pair(loc, ctx)) - { - auto keys = std::move(kv_res.unwrap().first.first); - auto key_reg = std::move(kv_res.unwrap().first.second); - auto val = std::move(kv_res.unwrap().second); - - if(sp.has_value()) - { - for(const auto& com : sp.value().comments) - { - val.comments().push_back(com); - } - } - - if(auto com_res = parse_comment_line(loc, ctx)) - { - if(auto com_opt = com_res.unwrap()) - { - val.comments().push_back(com_opt.value()); - newline_found = true; // comment includes newline at the end - } - } - else - { - ctx.report_error(std::move(com_res.unwrap_err())); - } - - auto ins_res = insert_value(inserting_value_kind::dotted_keys, - std::addressof(table.as_table()), - keys, std::move(key_reg), std::move(val)); - if(ins_res.is_err()) - { - ctx.report_error(std::move(ins_res.unwrap_err())); - } - } - else - { - ctx.report_error(std::move(kv_res.unwrap_err())); - skip_key_value_pair(loc, ctx); - } - } - - if(num_errors < ctx.errors().size()) - { - assert(ctx.has_error()); // already reported - return err(ctx.pop_last_error()); - } - return ok(); -} - -template -result, std::vector> -parse_file(location& loc, context& ctx) -{ - using value_type = basic_value; - using table_type = typename value_type::table_type; - - const auto first = loc; - const auto& spec = ctx.toml_spec(); - - if(loc.eof()) - { - return ok(value_type(table_type(), table_format_info{}, {}, region(loc))); - } - - value_type root(table_type(), table_format_info{}, {}, region(loc)); - root.as_table_fmt().fmt = table_format::multiline; - root.as_table_fmt().indent_type = indent_char::none; - - // parse top comment. - // - // ```toml - // # this is a comment for the top-level table. - // - // key = "the first value" - // ``` - // - // ```toml - // # this is a comment for "the first value". - // key = "the first value" - // ``` - while( ! loc.eof()) - { - if(auto com_res = parse_comment_line(loc, ctx)) - { - if(auto com_opt = com_res.unwrap()) - { - root.comments().push_back(std::move(com_opt.value())); - } - else // no comment found. - { - // if it is not an empty line, clear the root comment. - if( ! sequence(syntax::ws(spec), syntax::newline(spec)).scan(loc).is_ok()) - { - loc = first; - root.comments().clear(); - } - break; - } - } - else - { - ctx.report_error(std::move(com_res.unwrap_err())); - skip_comment_block(loc, ctx); - } - } - - // parse root table - { - const auto res = parse_table(loc, ctx, root); - if(res.is_err()) - { - ctx.report_error(std::move(res.unwrap_err())); - skip_until_next_table(loc, ctx); - } - } - - // parse tables - - while( ! loc.eof()) - { - auto sp = skip_multiline_spacer(loc, ctx, /*newline_found=*/true); - - if(auto key_res = parse_array_table_key(loc, ctx)) - { - auto key = std::move(std::get<0>(key_res.unwrap())); - auto reg = std::move(std::get<1>(key_res.unwrap())); - - std::vector com; - if(sp.has_value()) - { - for(std::size_t i=0; i(table_type()); - auto res = parse_table(loc, ctx, tmp); - if(res.is_err()) - { - ctx.report_error(res.unwrap_err()); - skip_until_next_table(loc, ctx); - } - continue; - } - - auto tab_ptr = inserted.unwrap(); - assert(tab_ptr); - - const auto tab_res = parse_table(loc, ctx, *tab_ptr); - if(tab_res.is_err()) - { - ctx.report_error(tab_res.unwrap_err()); - skip_until_next_table(loc, ctx); - } - - // parse_table first clears `indent_type`. - // to keep header indent info, we must store it later. - if(sp.has_value() && sp.value().indent_type != indent_char::none) - { - tab_ptr->as_table_fmt().indent_type = sp.value().indent_type; - tab_ptr->as_table_fmt().name_indent = sp.value().indent; - } - continue; - } - if(auto key_res = parse_table_key(loc, ctx)) - { - auto key = std::move(std::get<0>(key_res.unwrap())); - auto reg = std::move(std::get<1>(key_res.unwrap())); - - std::vector com; - if(sp.has_value()) - { - for(std::size_t i=0; i(table_type()); - auto res = parse_table(loc, ctx, tmp); - if(res.is_err()) - { - ctx.report_error(res.unwrap_err()); - skip_until_next_table(loc, ctx); - } - continue; - } - - auto tab_ptr = inserted.unwrap(); - assert(tab_ptr); - - const auto tab_res = parse_table(loc, ctx, *tab_ptr); - if(tab_res.is_err()) - { - ctx.report_error(tab_res.unwrap_err()); - skip_until_next_table(loc, ctx); - } - if(sp.has_value() && sp.value().indent_type != indent_char::none) - { - tab_ptr->as_table_fmt().indent_type = sp.value().indent_type; - tab_ptr->as_table_fmt().name_indent = sp.value().indent; - } - continue; - } - - // does not match array_table nor std_table. report an error. - const auto keytop = loc; - const auto maybe_array_of_tables = literal("[[").scan(loc).is_ok(); - loc = keytop; - - if(maybe_array_of_tables) - { - ctx.report_error(make_syntax_error("toml::parse_file: invalid array-table key", - syntax::array_table(spec), loc)); - } - else - { - ctx.report_error(make_syntax_error("toml::parse_file: invalid table key", - syntax::std_table(spec), loc)); - } - skip_until_next_table(loc, ctx); - } - - if( ! ctx.errors().empty()) - { - return err(std::move(ctx.errors())); - } - return ok(std::move(root)); -} - -template -result, std::vector> -parse_impl(std::vector cs, std::string fname, const spec& s) -{ - using value_type = basic_value; - using table_type = typename value_type::table_type; - - // an empty file is a valid toml file. - if(cs.empty()) - { - auto src = std::make_shared>(std::move(cs)); - location loc(std::move(src), std::move(fname)); - return ok(value_type(table_type(), table_format_info{}, std::vector{}, region(loc))); - } - - // to simplify parser, add newline at the end if there is no LF. - // But, if it has raw CR, the file is invalid (in TOML, CR is not a valid - // newline char). if it ends with CR, do not add LF and report it. - if(cs.back() != '\n' && cs.back() != '\r') - { - cs.push_back('\n'); - } - - auto src = std::make_shared>(std::move(cs)); - - location loc(std::move(src), std::move(fname)); - - // skip BOM if found - if(loc.source()->size() >= 3) - { - auto first = loc; - - const auto c0 = loc.current(); loc.advance(); - const auto c1 = loc.current(); loc.advance(); - const auto c2 = loc.current(); loc.advance(); - - const auto bom_found = (c0 == 0xEF) && (c1 == 0xBB) && (c2 == 0xBF); - if( ! bom_found) - { - loc = first; - } - } - - context ctx(s); - - return parse_file(loc, ctx); -} - -} // detail - -// ----------------------------------------------------------------------------- -// parse(byte array) - -template -result, std::vector> -try_parse(std::vector content, std::string filename, - spec s = spec::default_version()) -{ - return detail::parse_impl(std::move(content), std::move(filename), std::move(s)); -} -template -basic_value -parse(std::vector content, std::string filename, - spec s = spec::default_version()) -{ - auto res = try_parse(std::move(content), std::move(filename), std::move(s)); - if(res.is_ok()) - { - return res.unwrap(); - } - else - { - std::string msg; - for(const auto& err : res.unwrap_err()) - { - msg += format_error(err); - } - throw syntax_error(std::move(msg), std::move(res.unwrap_err())); - } -} - -// ----------------------------------------------------------------------------- -// parse(istream) - -template -result, std::vector> -try_parse(std::istream& is, std::string fname = "unknown file", spec s = spec::default_version()) -{ - const auto beg = is.tellg(); - is.seekg(0, std::ios::end); - const auto end = is.tellg(); - const auto fsize = end - beg; - is.seekg(beg); - - // read whole file as a sequence of char - assert(fsize >= 0); - std::vector letters(static_cast(fsize), '\0'); - is.read(reinterpret_cast(letters.data()), static_cast(fsize)); - - return detail::parse_impl(std::move(letters), std::move(fname), std::move(s)); -} - -template -basic_value parse(std::istream& is, std::string fname = "unknown file", spec s = spec::default_version()) -{ - auto res = try_parse(is, std::move(fname), std::move(s)); - if(res.is_ok()) - { - return res.unwrap(); - } - else - { - std::string msg; - for(const auto& err : res.unwrap_err()) - { - msg += format_error(err); - } - throw syntax_error(std::move(msg), std::move(res.unwrap_err())); - } -} - -// ----------------------------------------------------------------------------- -// parse(filename) - -template -result, std::vector> -try_parse(std::string fname, spec s = spec::default_version()) -{ - std::ifstream ifs(fname, std::ios_base::binary); - if(!ifs.good()) - { - std::vector e; - e.push_back(error_info("toml::parse: Error opening file \"" + fname + "\"", {})); - return err(std::move(e)); - } - ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); - - return try_parse(ifs, std::move(fname), std::move(s)); -} - -template -basic_value parse(std::string fname, spec s = spec::default_version()) -{ - std::ifstream ifs(fname, std::ios_base::binary); - if(!ifs.good()) - { - throw file_io_error("toml::parse: error opening file", fname); - } - ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); - - return parse(ifs, std::move(fname), std::move(s)); -} - -template -result, std::vector> -try_parse(const char (&fname)[N], spec s = spec::default_version()) -{ - return try_parse(std::string(fname), std::move(s)); -} - -template -basic_value parse(const char (&fname)[N], spec s = spec::default_version()) -{ - return parse(std::string(fname), std::move(s)); -} - -// ---------------------------------------------------------------------------- -// parse_str - -template -result, std::vector> -try_parse_str(std::string content, spec s = spec::default_version(), - cxx::source_location loc = cxx::source_location::current()) -{ - std::istringstream iss(std::move(content)); - std::string name("internal string" + cxx::to_string(loc)); - return try_parse(iss, std::move(name), std::move(s)); -} - -template -basic_value parse_str(std::string content, spec s = spec::default_version(), - cxx::source_location loc = cxx::source_location::current()) -{ - auto res = try_parse_str(std::move(content), std::move(s), std::move(loc)); - if(res.is_ok()) - { - return res.unwrap(); - } - else - { - std::string msg; - for(const auto& err : res.unwrap_err()) - { - msg += format_error(err); - } - throw syntax_error(std::move(msg), std::move(res.unwrap_err())); - } -} - -// ---------------------------------------------------------------------------- -// filesystem - -#if defined(TOML11_HAS_FILESYSTEM) - -template -cxx::enable_if_t::value, - result, std::vector>> -try_parse(const FSPATH& fpath, spec s = spec::default_version()) -{ - std::ifstream ifs(fpath, std::ios_base::binary); - if(!ifs.good()) - { - std::vector e; - e.push_back(error_info("toml::parse: Error opening file \"" + fpath.string() + "\"", {})); - return err(std::move(e)); - } - ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); - - return try_parse(ifs, fpath.string(), std::move(s)); -} - -template -cxx::enable_if_t::value, - basic_value> -parse(const FSPATH& fpath, spec s = spec::default_version()) -{ - std::ifstream ifs(fpath, std::ios_base::binary); - if(!ifs.good()) - { - throw file_io_error("toml::parse: error opening file", fpath.string()); - } - ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit); - - return parse(ifs, fpath.string(), std::move(s)); -} -#endif - -// ----------------------------------------------------------------------------- -// FILE* - -template -result, std::vector> -try_parse(FILE* fp, std::string filename, spec s = spec::default_version()) -{ - const long beg = std::ftell(fp); - if (beg == -1L) - { - return err(std::vector{error_info( - std::string("Failed to access: \"") + filename + - "\", errno = " + std::to_string(errno), {} - )}); - } - - const int res_seekend = std::fseek(fp, 0, SEEK_END); - if (res_seekend != 0) - { - return err(std::vector{error_info( - std::string("Failed to seek: \"") + filename + - "\", errno = " + std::to_string(errno), {} - )}); - } - - const long end = std::ftell(fp); - if (end == -1L) - { - return err(std::vector{error_info( - std::string("Failed to access: \"") + filename + - "\", errno = " + std::to_string(errno), {} - )}); - } - - const auto fsize = end - beg; - - const auto res_seekbeg = std::fseek(fp, beg, SEEK_SET); - if (res_seekbeg != 0) - { - return err(std::vector{error_info( - std::string("Failed to seek: \"") + filename + - "\", errno = " + std::to_string(errno), {} - )}); - - } - - // read whole file as a sequence of char - assert(fsize >= 0); - std::vector letters(static_cast(fsize)); - const auto actual = std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); - if(actual != static_cast(fsize)) - { - return err(std::vector{error_info( - std::string("File size changed: \"") + filename + - std::string("\" make sure that FILE* is in binary mode " - "to avoid LF <-> CRLF conversion"), {} - )}); - } - - return detail::parse_impl(std::move(letters), std::move(filename), std::move(s)); -} - -template -basic_value -parse(FILE* fp, std::string filename, spec s = spec::default_version()) -{ - const long beg = std::ftell(fp); - if (beg == -1L) - { - throw file_io_error(errno, "Failed to access", filename); - } - - const int res_seekend = std::fseek(fp, 0, SEEK_END); - if (res_seekend != 0) - { - throw file_io_error(errno, "Failed to seek", filename); - } - - const long end = std::ftell(fp); - if (end == -1L) - { - throw file_io_error(errno, "Failed to access", filename); - } - - const auto fsize = end - beg; - - const auto res_seekbeg = std::fseek(fp, beg, SEEK_SET); - if (res_seekbeg != 0) - { - throw file_io_error(errno, "Failed to seek", filename); - } - - // read whole file as a sequence of char - assert(fsize >= 0); - std::vector letters(static_cast(fsize)); - const auto actual = std::fread(letters.data(), sizeof(char), static_cast(fsize), fp); - if(actual != static_cast(fsize)) - { - throw file_io_error(errno, "File size changed; make sure that " - "FILE* is in binary mode to avoid LF <-> CRLF conversion", filename); - } - - auto res = detail::parse_impl(std::move(letters), std::move(filename), std::move(s)); - if(res.is_ok()) - { - return res.unwrap(); - } - else - { - std::string msg; - for(const auto& err : res.unwrap_err()) - { - msg += format_error(err); - } - throw syntax_error(std::move(msg), std::move(res.unwrap_err())); - } -} - -} // namespace toml - -#if defined(TOML11_COMPILE_SOURCES) -namespace toml -{ -struct type_config; -struct ordered_type_config; - -extern template result, std::vector> try_parse(std::vector, std::string, spec); -extern template result, std::vector> try_parse(std::istream&, std::string, spec); -extern template result, std::vector> try_parse(std::string, spec); -extern template result, std::vector> try_parse(FILE*, std::string, spec); -extern template result, std::vector> try_parse_str(std::string, spec, cxx::source_location); - -extern template basic_value parse(std::vector, std::string, spec); -extern template basic_value parse(std::istream&, std::string, spec); -extern template basic_value parse(std::string, spec); -extern template basic_value parse(FILE*, std::string, spec); -extern template basic_value parse_str(std::string, spec, cxx::source_location); - -extern template result, std::vector> try_parse(std::vector, std::string, spec); -extern template result, std::vector> try_parse(std::istream&, std::string, spec); -extern template result, std::vector> try_parse(std::string, spec); -extern template result, std::vector> try_parse(FILE*, std::string, spec); -extern template result, std::vector> try_parse_str(std::string, spec, cxx::source_location); - -extern template basic_value parse(std::vector, std::string, spec); -extern template basic_value parse(std::istream&, std::string, spec); -extern template basic_value parse(std::string, spec); -extern template basic_value parse(FILE*, std::string, spec); -extern template basic_value parse_str(std::string, spec, cxx::source_location); - -#if defined(TOML11_HAS_FILESYSTEM) -extern template cxx::enable_if_t::value, result, std::vector>> try_parse(const std::filesystem::path&, spec); -extern template cxx::enable_if_t::value, result, std::vector>> try_parse(const std::filesystem::path&, spec); -extern template cxx::enable_if_t::value, basic_value > parse (const std::filesystem::path&, spec); -extern template cxx::enable_if_t::value, basic_value > parse (const std::filesystem::path&, spec); -#endif // filesystem - -} // toml -#endif // TOML11_COMPILE_SOURCES - -#endif // TOML11_PARSER_HPP diff --git a/include/toml11/region.hpp b/include/toml11/region.hpp deleted file mode 100644 index b48300c..0000000 --- a/include/toml11/region.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_REGION_HPP -#define TOML11_REGION_HPP - -#include "fwd/region_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/region_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_REGION_HPP diff --git a/include/toml11/result.hpp b/include/toml11/result.hpp deleted file mode 100644 index e847c4b..0000000 --- a/include/toml11/result.hpp +++ /dev/null @@ -1,486 +0,0 @@ -#ifndef TOML11_RESULT_HPP -#define TOML11_RESULT_HPP - -#include "compat.hpp" -#include "exception.hpp" - -#include -#include -#include -#include - -#include - -namespace toml -{ - -struct bad_result_access final : public ::toml::exception -{ - public: - explicit bad_result_access(std::string what_arg) - : what_(std::move(what_arg)) - {} - ~bad_result_access() noexcept override = default; - const char* what() const noexcept override {return what_.c_str();} - - private: - std::string what_; -}; - -// ----------------------------------------------------------------------------- - -template -struct success -{ - static_assert( ! std::is_same::value, ""); - - using value_type = T; - - explicit success(value_type v) - noexcept(std::is_nothrow_move_constructible::value) - : value(std::move(v)) - {} - - template, T>::value, - std::nullptr_t> = nullptr> - explicit success(U&& v): value(std::forward(v)) {} - - template - explicit success(success v): value(std::move(v.value)) {} - - ~success() = default; - success(const success&) = default; - success(success&&) = default; - success& operator=(const success&) = default; - success& operator=(success&&) = default; - - value_type& get() noexcept {return value;} - value_type const& get() const noexcept {return value;} - - private: - - value_type value; -}; - -template -struct success> -{ - static_assert( ! std::is_same::value, ""); - - using value_type = T; - - explicit success(std::reference_wrapper v) noexcept - : value(std::move(v)) - {} - - ~success() = default; - success(const success&) = default; - success(success&&) = default; - success& operator=(const success&) = default; - success& operator=(success&&) = default; - - value_type& get() noexcept {return value.get();} - value_type const& get() const noexcept {return value.get();} - - private: - - std::reference_wrapper value; -}; - -template -success::type> ok(T&& v) -{ - return success::type>(std::forward(v)); -} -template -success ok(const char (&literal)[N]) -{ - return success(std::string(literal)); -} - -// ----------------------------------------------------------------------------- - -template -struct failure -{ - using value_type = T; - - explicit failure(value_type v) - noexcept(std::is_nothrow_move_constructible::value) - : value(std::move(v)) - {} - - template, T>::value, - std::nullptr_t> = nullptr> - explicit failure(U&& v): value(std::forward(v)) {} - - template - explicit failure(failure v): value(std::move(v.value)) {} - - ~failure() = default; - failure(const failure&) = default; - failure(failure&&) = default; - failure& operator=(const failure&) = default; - failure& operator=(failure&&) = default; - - value_type& get() noexcept {return value;} - value_type const& get() const noexcept {return value;} - - private: - - value_type value; -}; - -template -struct failure> -{ - using value_type = T; - - explicit failure(std::reference_wrapper v) noexcept - : value(std::move(v)) - {} - - ~failure() = default; - failure(const failure&) = default; - failure(failure&&) = default; - failure& operator=(const failure&) = default; - failure& operator=(failure&&) = default; - - value_type& get() noexcept {return value.get();} - value_type const& get() const noexcept {return value.get();} - - private: - - std::reference_wrapper value; -}; - -template -failure::type> err(T&& v) -{ - return failure::type>(std::forward(v)); -} - -template -failure err(const char (&literal)[N]) -{ - return failure(std::string(literal)); -} - -/* ============================================================================ - * _ _ - * _ _ ___ ____ _| | |_ - * | '_/ -_|_-< || | | _| - * |_| \___/__/\_,_|_|\__| - */ - -template -struct result -{ - using success_type = success; - using failure_type = failure; - using value_type = typename success_type::value_type; - using error_type = typename failure_type::value_type; - - result(success_type s): is_ok_(true), succ_(std::move(s)) {} - result(failure_type f): is_ok_(false), fail_(std::move(f)) {} - - template, value_type>>, - std::is_convertible, value_type> - >::value, std::nullptr_t> = nullptr> - result(success s): is_ok_(true), succ_(std::move(s.value)) {} - - template, error_type>>, - std::is_convertible, error_type> - >::value, std::nullptr_t> = nullptr> - result(failure f): is_ok_(false), fail_(std::move(f.value)) {} - - result& operator=(success_type s) - { - this->cleanup(); - this->is_ok_ = true; - auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(s)); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - return *this; - } - result& operator=(failure_type f) - { - this->cleanup(); - this->is_ok_ = false; - auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(f)); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - return *this; - } - - template - result& operator=(success s) - { - this->cleanup(); - this->is_ok_ = true; - auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(s.value)); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - return *this; - } - template - result& operator=(failure f) - { - this->cleanup(); - this->is_ok_ = false; - auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(f.value)); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - return *this; - } - - ~result() noexcept {this->cleanup();} - - result(const result& other): is_ok_(other.is_ok()) - { - if(other.is_ok()) - { - auto tmp = ::new(std::addressof(this->succ_)) success_type(other.succ_); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - } - else - { - auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.fail_); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - } - } - result(result&& other): is_ok_(other.is_ok()) - { - if(other.is_ok()) - { - auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.succ_)); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - } - else - { - auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.fail_)); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - } - } - - result& operator=(const result& other) - { - this->cleanup(); - if(other.is_ok()) - { - auto tmp = ::new(std::addressof(this->succ_)) success_type(other.succ_); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - } - else - { - auto tmp = ::new(std::addressof(this->fail_)) failure_type(other.fail_); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - } - is_ok_ = other.is_ok(); - return *this; - } - result& operator=(result&& other) - { - this->cleanup(); - if(other.is_ok()) - { - auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.succ_)); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - } - else - { - auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.fail_)); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - } - is_ok_ = other.is_ok(); - return *this; - } - - template, value_type>>, - cxx::negation, error_type>>, - std::is_convertible, value_type>, - std::is_convertible, error_type> - >::value, std::nullptr_t> = nullptr> - result(result other): is_ok_(other.is_ok()) - { - if(other.is_ok()) - { - auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.as_ok())); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - } - else - { - auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.as_err())); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - } - } - - template, value_type>>, - cxx::negation, error_type>>, - std::is_convertible, value_type>, - std::is_convertible, error_type> - >::value, std::nullptr_t> = nullptr> - result& operator=(result other) - { - this->cleanup(); - if(other.is_ok()) - { - auto tmp = ::new(std::addressof(this->succ_)) success_type(std::move(other.as_ok())); - assert(tmp == std::addressof(this->succ_)); - (void)tmp; - } - else - { - auto tmp = ::new(std::addressof(this->fail_)) failure_type(std::move(other.as_err())); - assert(tmp == std::addressof(this->fail_)); - (void)tmp; - } - is_ok_ = other.is_ok(); - return *this; - } - - bool is_ok() const noexcept {return is_ok_;} - bool is_err() const noexcept {return !is_ok_;} - - explicit operator bool() const noexcept {return is_ok_;} - - value_type& unwrap(cxx::source_location loc = cxx::source_location::current()) - { - if(this->is_err()) - { - throw bad_result_access("toml::result: bad unwrap" + cxx::to_string(loc)); - } - return this->succ_.get(); - } - value_type const& unwrap(cxx::source_location loc = cxx::source_location::current()) const - { - if(this->is_err()) - { - throw bad_result_access("toml::result: bad unwrap" + cxx::to_string(loc)); - } - return this->succ_.get(); - } - - value_type& unwrap_or(value_type& opt) noexcept - { - if(this->is_err()) {return opt;} - return this->succ_.get(); - } - value_type const& unwrap_or(value_type const& opt) const noexcept - { - if(this->is_err()) {return opt;} - return this->succ_.get(); - } - - error_type& unwrap_err(cxx::source_location loc = cxx::source_location::current()) - { - if(this->is_ok()) - { - throw bad_result_access("toml::result: bad unwrap_err" + cxx::to_string(loc)); - } - return this->fail_.get(); - } - error_type const& unwrap_err(cxx::source_location loc = cxx::source_location::current()) const - { - if(this->is_ok()) - { - throw bad_result_access("toml::result: bad unwrap_err" + cxx::to_string(loc)); - } - return this->fail_.get(); - } - - value_type& as_ok() noexcept - { - assert(this->is_ok()); - return this->succ_.get(); - } - value_type const& as_ok() const noexcept - { - assert(this->is_ok()); - return this->succ_.get(); - } - - error_type& as_err() noexcept - { - assert(this->is_err()); - return this->fail_.get(); - } - error_type const& as_err() const noexcept - { - assert(this->is_err()); - return this->fail_.get(); - } - - private: - - void cleanup() noexcept - { -#if defined(__GNUC__) && ! defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wduplicated-branches" -#endif - - if(this->is_ok_) {this->succ_.~success_type();} - else {this->fail_.~failure_type();} - -#if defined(__GNUC__) && ! defined(__clang__) -#pragma GCC diagnostic pop -#endif - return; - } - - private: - - bool is_ok_; - union - { - success_type succ_; - failure_type fail_; - }; -}; - -// ---------------------------------------------------------------------------- - -namespace detail -{ -struct none_t {}; -inline bool operator==(const none_t&, const none_t&) noexcept {return true;} -inline bool operator!=(const none_t&, const none_t&) noexcept {return false;} -inline bool operator< (const none_t&, const none_t&) noexcept {return false;} -inline bool operator<=(const none_t&, const none_t&) noexcept {return true;} -inline bool operator> (const none_t&, const none_t&) noexcept {return false;} -inline bool operator>=(const none_t&, const none_t&) noexcept {return true;} -inline std::ostream& operator<<(std::ostream& os, const none_t&) -{ - os << "none"; - return os; -} -} // detail - -inline success ok() noexcept -{ - return success(detail::none_t{}); -} -inline failure err() noexcept -{ - return failure(detail::none_t{}); -} - -} // toml -#endif // TOML11_RESULT_HPP diff --git a/include/toml11/scanner.hpp b/include/toml11/scanner.hpp deleted file mode 100644 index 85812d9..0000000 --- a/include/toml11/scanner.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_SCANNER_HPP -#define TOML11_SCANNER_HPP - -#include "fwd/scanner_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/scanner_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_SCANNER_HPP diff --git a/include/toml11/serializer.hpp b/include/toml11/serializer.hpp deleted file mode 100644 index a3f148e..0000000 --- a/include/toml11/serializer.hpp +++ /dev/null @@ -1,1275 +0,0 @@ -#ifndef TOML11_SERIALIZER_HPP -#define TOML11_SERIALIZER_HPP - -#include "comments.hpp" -#include "error_info.hpp" -#include "exception.hpp" -#include "source_location.hpp" -#include "spec.hpp" -#include "syntax.hpp" -#include "types.hpp" -#include "utility.hpp" -#include "value.hpp" - -#include -#include -#include - -#include -#include - -namespace toml -{ - -struct serialization_error final : public ::toml::exception -{ - public: - explicit serialization_error(std::string what_arg, source_location loc) - : what_(std::move(what_arg)), loc_(std::move(loc)) - {} - ~serialization_error() noexcept override = default; - - const char* what() const noexcept override {return what_.c_str();} - source_location const& location() const noexcept {return loc_;} - - private: - std::string what_; - source_location loc_; -}; - -namespace detail -{ -template -class serializer -{ - public: - - using value_type = basic_value; - - using key_type = typename value_type::key_type ; - using comment_type = typename value_type::comment_type ; - using boolean_type = typename value_type::boolean_type ; - using integer_type = typename value_type::integer_type ; - using floating_type = typename value_type::floating_type ; - using string_type = typename value_type::string_type ; - using local_time_type = typename value_type::local_time_type ; - using local_date_type = typename value_type::local_date_type ; - using local_datetime_type = typename value_type::local_datetime_type ; - using offset_datetime_type = typename value_type::offset_datetime_type; - using array_type = typename value_type::array_type ; - using table_type = typename value_type::table_type ; - - using char_type = typename string_type::value_type; - - public: - - explicit serializer(const spec& sp) - : spec_(sp), force_inline_(false), current_indent_(0) - {} - - string_type operator()(const std::vector& ks, const value_type& v) - { - for(const auto& k : ks) - { - this->keys_.push_back(k); - } - return (*this)(v); - } - - string_type operator()(const key_type& k, const value_type& v) - { - this->keys_.push_back(k); - return (*this)(v); - } - - string_type operator()(const value_type& v) - { - switch(v.type()) - { - case value_t::boolean : {return (*this)(v.as_boolean (), v.as_boolean_fmt (), v.location());} - case value_t::integer : {return (*this)(v.as_integer (), v.as_integer_fmt (), v.location());} - case value_t::floating : {return (*this)(v.as_floating (), v.as_floating_fmt (), v.location());} - case value_t::string : {return (*this)(v.as_string (), v.as_string_fmt (), v.location());} - case value_t::offset_datetime: {return (*this)(v.as_offset_datetime(), v.as_offset_datetime_fmt(), v.location());} - case value_t::local_datetime : {return (*this)(v.as_local_datetime (), v.as_local_datetime_fmt (), v.location());} - case value_t::local_date : {return (*this)(v.as_local_date (), v.as_local_date_fmt (), v.location());} - case value_t::local_time : {return (*this)(v.as_local_time (), v.as_local_time_fmt (), v.location());} - case value_t::array : - { - return (*this)(v.as_array(), v.as_array_fmt(), v.comments(), v.location()); - } - case value_t::table : - { - string_type retval; - if(this->keys_.empty()) // it might be the root table. emit comments here. - { - retval += format_comments(v.comments(), v.as_table_fmt().indent_type); - } - if( ! retval.empty()) // we have comment. - { - retval += char_type('\n'); - } - - retval += (*this)(v.as_table(), v.as_table_fmt(), v.comments(), v.location()); - return retval; - } - case value_t::empty: - { - if(this->spec_.ext_null_value) - { - return string_conv("null"); - } - break; - } - default: - { - break; - } - } - throw serialization_error(format_error( - "[error] toml::serializer: toml::basic_value " - "does not have any valid type.", v.location(), "here"), v.location()); - } - - private: - - string_type operator()(const boolean_type& b, const boolean_format_info&, const source_location&) // {{{ - { - if(b) - { - return string_conv("true"); - } - else - { - return string_conv("false"); - } - } // }}} - - string_type operator()(const integer_type i, const integer_format_info& fmt, const source_location& loc) // {{{ - { - std::ostringstream oss; - this->set_locale(oss); - - const auto insert_spacer = [&fmt](std::string s) -> std::string { - if(fmt.spacer == 0) {return s;} - - std::string sign; - if( ! s.empty() && (s.at(0) == '+' || s.at(0) == '-')) - { - sign += s.at(0); - s.erase(s.begin()); - } - - std::string spaced; - std::size_t counter = 0; - for(auto iter = s.rbegin(); iter != s.rend(); ++iter) - { - if(counter != 0 && counter % fmt.spacer == 0) - { - spaced += '_'; - } - spaced += *iter; - counter += 1; - } - if(!spaced.empty() && spaced.back() == '_') {spaced.pop_back();} - - s.clear(); - std::copy(spaced.rbegin(), spaced.rend(), std::back_inserter(s)); - return sign + s; - }; - - std::string retval; - if(fmt.fmt == integer_format::dec) - { - oss << std::setw(static_cast(fmt.width)) << std::dec << i; - retval = insert_spacer(oss.str()); - - if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) - { - retval += '_'; - retval += fmt.suffix; - } - } - else - { - if(i < 0) - { - throw serialization_error(format_error("binary, octal, hexadecimal " - "integer does not allow negative value", loc, "here"), loc); - } - switch(fmt.fmt) - { - case integer_format::hex: - { - oss << std::noshowbase - << std::setw(static_cast(fmt.width)) - << std::setfill('0') - << std::hex; - if(fmt.uppercase) - { - oss << std::uppercase; - } - else - { - oss << std::nouppercase; - } - oss << i; - retval = std::string("0x") + insert_spacer(oss.str()); - break; - } - case integer_format::oct: - { - oss << std::setw(static_cast(fmt.width)) << std::setfill('0') << std::oct << i; - retval = std::string("0o") + insert_spacer(oss.str()); - break; - } - case integer_format::bin: - { - integer_type x{i}; - std::string tmp; - std::size_t bits(0); - while(x != 0) - { - if(fmt.spacer != 0) - { - if(bits != 0 && (bits % fmt.spacer) == 0) {tmp += '_';} - } - if(x % 2 == 1) { tmp += '1'; } else { tmp += '0'; } - x >>= 1; - bits += 1; - } - for(; bits < fmt.width; ++bits) - { - if(fmt.spacer != 0) - { - if(bits != 0 && (bits % fmt.spacer) == 0) {tmp += '_';} - } - tmp += '0'; - } - for(auto iter = tmp.rbegin(); iter != tmp.rend(); ++iter) - { - oss << *iter; - } - retval = std::string("0b") + oss.str(); - break; - } - default: - { - throw serialization_error(format_error( - "none of dec, hex, oct, bin: " + to_string(fmt.fmt), - loc, "here"), loc); - } - } - } - return string_conv(retval); - } // }}} - - string_type operator()(const floating_type f, const floating_format_info& fmt, const source_location&) // {{{ - { - using std::isnan; - using std::isinf; - using std::signbit; - - std::ostringstream oss; - this->set_locale(oss); - - if(isnan(f)) - { - if(signbit(f)) - { - oss << '-'; - } - oss << "nan"; - if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) - { - oss << '_'; - oss << fmt.suffix; - } - return string_conv(oss.str()); - } - - if(isinf(f)) - { - if(signbit(f)) - { - oss << '-'; - } - oss << "inf"; - if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) - { - oss << '_'; - oss << fmt.suffix; - } - return string_conv(oss.str()); - } - - switch(fmt.fmt) - { - case floating_format::defaultfloat: - { - if(fmt.prec != 0) - { - oss << std::setprecision(static_cast(fmt.prec)); - } - oss << f; - // since defaultfloat may omit point, we need to add it - std::string s = oss.str(); - if (s.find('.') == std::string::npos && - s.find('e') == std::string::npos && - s.find('E') == std::string::npos ) - { - s += ".0"; - } - if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) - { - s += '_'; - s += fmt.suffix; - } - return string_conv(s); - } - case floating_format::fixed: - { - if(fmt.prec != 0) - { - oss << std::setprecision(static_cast(fmt.prec)); - } - oss << std::fixed << f; - if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) - { - oss << '_' << fmt.suffix; - } - return string_conv(oss.str()); - } - case floating_format::scientific: - { - if(fmt.prec != 0) - { - oss << std::setprecision(static_cast(fmt.prec)); - } - oss << std::scientific << f; - if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) - { - oss << '_' << fmt.suffix; - } - return string_conv(oss.str()); - } - case floating_format::hex: - { - if(this->spec_.ext_hex_float) - { - oss << std::hexfloat << f; - // suffix is only for decimal numbers. - return string_conv(oss.str()); - } - else // no hex allowed. output with max precision. - { - oss << std::setprecision(std::numeric_limits::max_digits10) - << std::scientific << f; - // suffix is only for decimal numbers. - return string_conv(oss.str()); - } - } - default: - { - if(this->spec_.ext_num_suffix && ! fmt.suffix.empty()) - { - oss << '_' << fmt.suffix; - } - return string_conv(oss.str()); - } - } - } // }}} - - string_type operator()(string_type s, const string_format_info& fmt, const source_location& loc) // {{{ - { - string_type retval; - switch(fmt.fmt) - { - case string_format::basic: - { - retval += char_type('"'); - retval += this->escape_basic_string(s); - retval += char_type('"'); - return retval; - } - case string_format::literal: - { - if(std::find(s.begin(), s.end(), char_type('\n')) != s.end()) - { - throw serialization_error(format_error("toml::serializer: " - "(non-multiline) literal string cannot have a newline", - loc, "here"), loc); - } - retval += char_type('\''); - retval += s; - retval += char_type('\''); - return retval; - } - case string_format::multiline_basic: - { - retval += string_conv("\"\"\""); - if(fmt.start_with_newline) - { - retval += char_type('\n'); - } - - retval += this->escape_ml_basic_string(s); - - retval += string_conv("\"\"\""); - return retval; - } - case string_format::multiline_literal: - { - retval += string_conv("'''"); - if(fmt.start_with_newline) - { - retval += char_type('\n'); - } - retval += s; - retval += string_conv("'''"); - return retval; - } - default: - { - throw serialization_error(format_error( - "[error] toml::serializer::operator()(string): " - "invalid string_format value", loc, "here"), loc); - } - } - } // }}} - - string_type operator()(const local_date_type& d, const local_date_format_info&, const source_location&) // {{{ - { - std::ostringstream oss; - oss << d; - return string_conv(oss.str()); - } // }}} - - string_type operator()(const local_time_type& t, const local_time_format_info& fmt, const source_location&) // {{{ - { - return this->format_local_time(t, fmt.has_seconds, fmt.subsecond_precision); - } // }}} - - string_type operator()(const local_datetime_type& dt, const local_datetime_format_info& fmt, const source_location&) // {{{ - { - std::ostringstream oss; - oss << dt.date; - switch(fmt.delimiter) - { - case datetime_delimiter_kind::upper_T: { oss << 'T'; break; } - case datetime_delimiter_kind::lower_t: { oss << 't'; break; } - case datetime_delimiter_kind::space: { oss << ' '; break; } - default: { oss << 'T'; break; } - } - return string_conv(oss.str()) + - this->format_local_time(dt.time, fmt.has_seconds, fmt.subsecond_precision); - } // }}} - - string_type operator()(const offset_datetime_type& odt, const offset_datetime_format_info& fmt, const source_location&) // {{{ - { - std::ostringstream oss; - oss << odt.date; - switch(fmt.delimiter) - { - case datetime_delimiter_kind::upper_T: { oss << 'T'; break; } - case datetime_delimiter_kind::lower_t: { oss << 't'; break; } - case datetime_delimiter_kind::space: { oss << ' '; break; } - default: { oss << 'T'; break; } - } - oss << string_conv(this->format_local_time(odt.time, fmt.has_seconds, fmt.subsecond_precision)); - oss << odt.offset; - return string_conv(oss.str()); - } // }}} - - string_type operator()(const array_type& a, const array_format_info& fmt, const comment_type& com, const source_location& loc) // {{{ - { - array_format f = fmt.fmt; - if(fmt.fmt == array_format::default_format) - { - // [[in.this.form]], you cannot add a comment to the array itself - // (but you can add a comment to each table). - // To keep comments, we need to avoid multiline array-of-tables - // if array itself has a comment. - if( ! this->keys_.empty() && - ! a.empty() && - com.empty() && - std::all_of(a.begin(), a.end(), [](const value_type& e) {return e.is_table();})) - { - f = array_format::array_of_tables; - } - else - { - f = array_format::oneline; - - // check if it becomes long - std::size_t approx_len = 0; - for(const auto& e : a) - { - // have a comment. cannot be inlined - if( ! e.comments().empty()) - { - f = array_format::multiline; - break; - } - // possibly long types ... - if(e.is_array() || e.is_table() || e.is_offset_datetime() || e.is_local_datetime()) - { - f = array_format::multiline; - break; - } - else if(e.is_boolean()) - { - approx_len += (*this)(e.as_boolean(), e.as_boolean_fmt(), e.location()).size(); - } - else if(e.is_integer()) - { - approx_len += (*this)(e.as_integer(), e.as_integer_fmt(), e.location()).size(); - } - else if(e.is_floating()) - { - approx_len += (*this)(e.as_floating(), e.as_floating_fmt(), e.location()).size(); - } - else if(e.is_string()) - { - if(e.as_string_fmt().fmt == string_format::multiline_basic || - e.as_string_fmt().fmt == string_format::multiline_literal) - { - f = array_format::multiline; - break; - } - approx_len += 2 + (*this)(e.as_string(), e.as_string_fmt(), e.location()).size(); - } - else if(e.is_local_date()) - { - approx_len += 10; // 1234-56-78 - } - else if(e.is_local_time()) - { - approx_len += 15; // 12:34:56.789012 - } - - if(approx_len > 60) // key, ` = `, `[...]` < 80 - { - f = array_format::multiline; - break; - } - approx_len += 2; // `, ` - } - } - } - if(this->force_inline_ && f == array_format::array_of_tables) - { - f = array_format::multiline; - } - if(a.empty() && f == array_format::array_of_tables) - { - f = array_format::oneline; - } - - // -------------------------------------------------------------------- - - if(f == array_format::array_of_tables) - { - if(this->keys_.empty()) - { - throw serialization_error("array of table must have its key. " - "use format(key, v)", loc); - } - string_type retval; - for(const auto& e : a) - { - assert(e.is_table()); - - this->current_indent_ += e.as_table_fmt().name_indent; - retval += this->format_comments(e.comments(), e.as_table_fmt().indent_type); - retval += this->format_indent(e.as_table_fmt().indent_type); - this->current_indent_ -= e.as_table_fmt().name_indent; - - retval += string_conv("[["); - retval += this->format_keys(this->keys_).value(); - retval += string_conv("]]\n"); - - retval += this->format_ml_table(e.as_table(), e.as_table_fmt()); - } - return retval; - } - else if(f == array_format::oneline) - { - // ignore comments. we cannot emit comments - string_type retval; - retval += char_type('['); - for(const auto& e : a) - { - this->force_inline_ = true; - retval += (*this)(e); - retval += string_conv(", "); - } - if( ! a.empty()) - { - retval.pop_back(); // ` ` - retval.pop_back(); // `,` - } - retval += char_type(']'); - this->force_inline_ = false; - return retval; - } - else - { - assert(f == array_format::multiline); - - string_type retval; - retval += string_conv("[\n"); - - for(const auto& e : a) - { - this->current_indent_ += fmt.body_indent; - retval += this->format_comments(e.comments(), fmt.indent_type); - retval += this->format_indent(fmt.indent_type); - this->current_indent_ -= fmt.body_indent; - - this->force_inline_ = true; - retval += (*this)(e); - retval += string_conv(",\n"); - } - this->force_inline_ = false; - - this->current_indent_ += fmt.closing_indent; - retval += this->format_indent(fmt.indent_type); - this->current_indent_ -= fmt.closing_indent; - - retval += char_type(']'); - return retval; - } - } // }}} - - string_type operator()(const table_type& t, const table_format_info& fmt, const comment_type& com, const source_location& loc) // {{{ - { - if(this->force_inline_) - { - if(fmt.fmt == table_format::multiline_oneline) - { - return this->format_ml_inline_table(t, fmt); - } - else - { - return this->format_inline_table(t, fmt); - } - } - else - { - if(fmt.fmt == table_format::multiline) - { - string_type retval; - // comment is emitted inside format_ml_table - if(auto k = this->format_keys(this->keys_)) - { - this->current_indent_ += fmt.name_indent; - retval += this->format_comments(com, fmt.indent_type); - retval += this->format_indent(fmt.indent_type); - this->current_indent_ -= fmt.name_indent; - retval += char_type('['); - retval += k.value(); - retval += string_conv("]\n"); - } - // otherwise, its the root. - - retval += this->format_ml_table(t, fmt); - return retval; - } - else if(fmt.fmt == table_format::oneline) - { - return this->format_inline_table(t, fmt); - } - else if(fmt.fmt == table_format::multiline_oneline) - { - return this->format_ml_inline_table(t, fmt); - } - else if(fmt.fmt == table_format::dotted) - { - std::vector keys; - if(this->keys_.empty()) - { - throw serialization_error(format_error("toml::serializer: " - "dotted table must have its key. use format(key, v)", - loc, "here"), loc); - } - keys.push_back(this->keys_.back()); - - const auto retval = this->format_dotted_table(t, fmt, loc, keys); - keys.pop_back(); - return retval; - } - else - { - assert(fmt.fmt == table_format::implicit); - - string_type retval; - for(const auto& kv : t) - { - const auto& k = kv.first; - const auto& v = kv.second; - - if( ! v.is_table() && ! v.is_array_of_tables()) - { - throw serialization_error(format_error("toml::serializer: " - "an implicit table cannot have non-table value.", - v.location(), "here"), v.location()); - } - if(v.is_table()) - { - if(v.as_table_fmt().fmt != table_format::multiline && - v.as_table_fmt().fmt != table_format::implicit) - { - throw serialization_error(format_error("toml::serializer: " - "an implicit table cannot have non-multiline table", - v.location(), "here"), v.location()); - } - } - else - { - assert(v.is_array()); - for(const auto& e : v.as_array()) - { - if(e.as_table_fmt().fmt != table_format::multiline && - v.as_table_fmt().fmt != table_format::implicit) - { - throw serialization_error(format_error("toml::serializer: " - "an implicit table cannot have non-multiline table", - e.location(), "here"), e.location()); - } - } - } - - keys_.push_back(k); - retval += (*this)(v); - keys_.pop_back(); - } - return retval; - } - } - } // }}} - - private: - - string_type escape_basic_string(const string_type& s) const // {{{ - { - string_type retval; - for(const char_type c : s) - { - switch(c) - { - case char_type('\\'): {retval += string_conv("\\\\"); break;} - case char_type('\"'): {retval += string_conv("\\\""); break;} - case char_type('\b'): {retval += string_conv("\\b" ); break;} - case char_type('\t'): {retval += string_conv("\\t" ); break;} - case char_type('\f'): {retval += string_conv("\\f" ); break;} - case char_type('\n'): {retval += string_conv("\\n" ); break;} - case char_type('\r'): {retval += string_conv("\\r" ); break;} - default : - { - if(c == char_type(0x1B) && spec_.v1_1_0_add_escape_sequence_e) - { - retval += string_conv("\\e"); - } - else if((char_type(0x00) <= c && c <= char_type(0x08)) || - (char_type(0x0A) <= c && c <= char_type(0x1F)) || - c == char_type(0x7F)) - { - if(spec_.v1_1_0_add_escape_sequence_x) - { - retval += string_conv("\\x"); - } - else - { - retval += string_conv("\\u00"); - } - const auto c1 = c / 16; - const auto c2 = c % 16; - retval += static_cast('0' + c1); - if(c2 < 10) - { - retval += static_cast('0' + c2); - } - else // 10 <= c2 - { - retval += static_cast('A' + (c2 - 10)); - } - } - else - { - retval += c; - } - } - } - } - return retval; - } // }}} - - string_type escape_ml_basic_string(const string_type& s) // {{{ - { - string_type retval; - for(const char_type c : s) - { - switch(c) - { - case char_type('\\'): {retval += string_conv("\\\\"); break;} - case char_type('\b'): {retval += string_conv("\\b" ); break;} - case char_type('\t'): {retval += string_conv("\\t" ); break;} - case char_type('\f'): {retval += string_conv("\\f" ); break;} - case char_type('\n'): {retval += string_conv("\n" ); break;} - case char_type('\r'): {retval += string_conv("\\r" ); break;} - default : - { - if(c == char_type(0x1B) && spec_.v1_1_0_add_escape_sequence_e) - { - retval += string_conv("\\e"); - } - else if((char_type(0x00) <= c && c <= char_type(0x08)) || - (char_type(0x0A) <= c && c <= char_type(0x1F)) || - c == char_type(0x7F)) - { - if(spec_.v1_1_0_add_escape_sequence_x) - { - retval += string_conv("\\x"); - } - else - { - retval += string_conv("\\u00"); - } - const auto c1 = c / 16; - const auto c2 = c % 16; - retval += static_cast('0' + c1); - if(c2 < 10) - { - retval += static_cast('0' + c2); - } - else // 10 <= c2 - { - retval += static_cast('A' + (c2 - 10)); - } - } - else - { - retval += c; - } - } - } - } - // Only 1 or 2 consecutive `"`s are allowed in multiline basic string. - // 3 consecutive `"`s are considered as a closing delimiter. - // We need to check if there are 3 or more consecutive `"`s and insert - // backslash to break them down into several short `"`s like the `str6` - // in the following example. - // ```toml - // str4 = """Here are two quotation marks: "". Simple enough.""" - // # str5 = """Here are three quotation marks: """.""" # INVALID - // str5 = """Here are three quotation marks: ""\".""" - // str6 = """Here are fifteen quotation marks: ""\"""\"""\"""\"""\".""" - // ``` - auto found_3_quotes = retval.find(string_conv("\"\"\"")); - while(found_3_quotes != string_type::npos) - { - retval.replace(found_3_quotes, 3, string_conv("\"\"\\\"")); - found_3_quotes = retval.find(string_conv("\"\"\"")); - } - return retval; - } // }}} - - string_type format_local_time(const local_time_type& t, const bool has_seconds, const std::size_t subsec_prec) // {{{ - { - std::ostringstream oss; - oss << std::setfill('0') << std::setw(2) << static_cast(t.hour); - oss << ':'; - oss << std::setfill('0') << std::setw(2) << static_cast(t.minute); - if(has_seconds) - { - oss << ':'; - oss << std::setfill('0') << std::setw(2) << static_cast(t.second); - if(subsec_prec != 0) - { - std::ostringstream subsec; - subsec << std::setfill('0') << std::setw(3) << static_cast(t.millisecond); - subsec << std::setfill('0') << std::setw(3) << static_cast(t.microsecond); - subsec << std::setfill('0') << std::setw(3) << static_cast(t.nanosecond); - std::string subsec_str = subsec.str(); - oss << '.' << subsec_str.substr(0, subsec_prec); - } - } - return string_conv(oss.str()); - } // }}} - - string_type format_ml_table(const table_type& t, const table_format_info& fmt) // {{{ - { - const auto format_later = [](const value_type& v) -> bool { - - const bool is_ml_table = v.is_table() && - v.as_table_fmt().fmt != table_format::oneline && - v.as_table_fmt().fmt != table_format::multiline_oneline && - v.as_table_fmt().fmt != table_format::dotted ; - - const bool is_ml_array_table = v.is_array_of_tables() && - v.as_array_fmt().fmt != array_format::oneline && - v.as_array_fmt().fmt != array_format::multiline; - - return is_ml_table || is_ml_array_table; - }; - - string_type retval; - this->current_indent_ += fmt.body_indent; - for(const auto& kv : t) - { - const auto& key = kv.first; - const auto& val = kv.second; - if(format_later(val)) - { - continue; - } - this->keys_.push_back(key); - - retval += format_comments(val.comments(), fmt.indent_type); - retval += format_indent(fmt.indent_type); - if(val.is_table() && val.as_table_fmt().fmt == table_format::dotted) - { - retval += (*this)(val); - } - else - { - retval += format_key(key); - retval += string_conv(" = "); - retval += (*this)(val); - retval += char_type('\n'); - } - this->keys_.pop_back(); - } - this->current_indent_ -= fmt.body_indent; - - if( ! retval.empty()) - { - retval += char_type('\n'); // for readability, add empty line between tables - } - for(const auto& kv : t) - { - if( ! format_later(kv.second)) - { - continue; - } - // must be a [multiline.table] or [[multiline.array.of.tables]]. - // comments will be generated inside it. - this->keys_.push_back(kv.first); - retval += (*this)(kv.second); - this->keys_.pop_back(); - } - return retval; - } // }}} - - string_type format_inline_table(const table_type& t, const table_format_info&) // {{{ - { - // comments are ignored because we cannot write without newline - string_type retval; - retval += char_type('{'); - for(const auto& kv : t) - { - this->force_inline_ = true; - retval += this->format_key(kv.first); - retval += string_conv(" = "); - retval += (*this)(kv.second); - retval += string_conv(", "); - } - if( ! t.empty()) - { - retval.pop_back(); // ' ' - retval.pop_back(); // ',' - } - retval += char_type('}'); - this->force_inline_ = false; - return retval; - } // }}} - - string_type format_ml_inline_table(const table_type& t, const table_format_info& fmt) // {{{ - { - string_type retval; - retval += string_conv("{\n"); - this->current_indent_ += fmt.body_indent; - for(const auto& kv : t) - { - this->force_inline_ = true; - retval += format_comments(kv.second.comments(), fmt.indent_type); - retval += format_indent(fmt.indent_type); - retval += kv.first; - retval += string_conv(" = "); - - this->force_inline_ = true; - retval += (*this)(kv.second); - - retval += string_conv(",\n"); - } - if( ! t.empty()) - { - retval.pop_back(); // '\n' - retval.pop_back(); // ',' - } - this->current_indent_ -= fmt.body_indent; - this->force_inline_ = false; - - this->current_indent_ += fmt.closing_indent; - retval += format_indent(fmt.indent_type); - this->current_indent_ -= fmt.closing_indent; - - retval += char_type('}'); - return retval; - } // }}} - - string_type format_dotted_table(const table_type& t, const table_format_info& fmt, // {{{ - const source_location&, std::vector& keys) - { - // lets say we have: `{"a": {"b": {"c": {"d": "foo", "e": "bar"} } }` - // and `a` and `b` are `dotted`. - // - // - in case if `c` is `oneline`: - // ```toml - // a.b.c = {d = "foo", e = "bar"} - // ``` - // - // - in case if and `c` is `dotted`: - // ```toml - // a.b.c.d = "foo" - // a.b.c.e = "bar" - // ``` - - string_type retval; - - for(const auto& kv : t) - { - const auto& key = kv.first; - const auto& val = kv.second; - - keys.push_back(key); - - // format recursive dotted table? - if (val.is_table() && - val.as_table_fmt().fmt != table_format::oneline && - val.as_table_fmt().fmt != table_format::multiline_oneline) - { - retval += this->format_dotted_table(val.as_table(), val.as_table_fmt(), val.location(), keys); - } - else // non-table or inline tables. format normally - { - retval += format_comments(val.comments(), fmt.indent_type); - retval += format_indent(fmt.indent_type); - retval += format_keys(keys).value(); - retval += string_conv(" = "); - this->force_inline_ = true; // sub-table must be inlined - retval += (*this)(val); - retval += char_type('\n'); - this->force_inline_ = false; - } - keys.pop_back(); - } - return retval; - } // }}} - - string_type format_key(const key_type& key) // {{{ - { - if(key.empty()) - { - return string_conv("\"\""); - } - - // check the key can be a bare (unquoted) key - auto loc = detail::make_temporary_location(string_conv(key)); - auto reg = detail::syntax::unquoted_key(this->spec_).scan(loc); - if(reg.is_ok() && loc.eof()) - { - return key; - } - - //if it includes special characters, then format it in a "quoted" key. - string_type formatted = string_conv("\""); - for(const char_type c : key) - { - switch(c) - { - case char_type('\\'): {formatted += string_conv("\\\\"); break;} - case char_type('\"'): {formatted += string_conv("\\\""); break;} - case char_type('\b'): {formatted += string_conv("\\b" ); break;} - case char_type('\t'): {formatted += string_conv("\\t" ); break;} - case char_type('\f'): {formatted += string_conv("\\f" ); break;} - case char_type('\n'): {formatted += string_conv("\\n" ); break;} - case char_type('\r'): {formatted += string_conv("\\r" ); break;} - default : - { - // ASCII ctrl char - if( (char_type(0x00) <= c && c <= char_type(0x08)) || - (char_type(0x0A) <= c && c <= char_type(0x1F)) || - c == char_type(0x7F)) - { - if(spec_.v1_1_0_add_escape_sequence_x) - { - formatted += string_conv("\\x"); - } - else - { - formatted += string_conv("\\u00"); - } - const auto c1 = c / 16; - const auto c2 = c % 16; - formatted += static_cast('0' + c1); - if(c2 < 10) - { - formatted += static_cast('0' + c2); - } - else // 10 <= c2 - { - formatted += static_cast('A' + (c2 - 10)); - } - } - else - { - formatted += c; - } - break; - } - } - } - formatted += string_conv("\""); - return formatted; - } // }}} - cxx::optional format_keys(const std::vector& keys) // {{{ - { - if(keys.empty()) - { - return cxx::make_nullopt(); - } - - string_type formatted; - for(const auto& ky : keys) - { - formatted += format_key(ky); - formatted += char_type('.'); - } - formatted.pop_back(); // remove the last dot '.' - return formatted; - } // }}} - - string_type format_comments(const discard_comments&, const indent_char) const // {{{ - { - return string_conv(""); - } // }}} - string_type format_comments(const preserve_comments& comments, const indent_char indent_type) const // {{{ - { - string_type retval; - for(const auto& c : comments) - { - if(c.empty()) {continue;} - retval += format_indent(indent_type); - if(c.front() != '#') {retval += char_type('#');} - retval += string_conv(c); - if(c.back() != '\n') {retval += char_type('\n');} - } - return retval; - } // }}} - - string_type format_indent(const indent_char indent_type) const // {{{ - { - const auto indent = static_cast((std::max)(0, this->current_indent_)); - if(indent_type == indent_char::space) - { - return string_conv(make_string(indent, ' ')); - } - else if(indent_type == indent_char::tab) - { - return string_conv(make_string(indent, '\t')); - } - else - { - return string_type{}; - } - } // }}} - - std::locale set_locale(std::ostream& os) const - { - return os.imbue(std::locale::classic()); - } - - private: - - spec spec_; - bool force_inline_; // table inside an array without fmt specification - std::int32_t current_indent_; - std::vector keys_; -}; -} // detail - -template -typename basic_value::string_type -format(const basic_value& v, const spec s = spec::default_version()) -{ - detail::serializer ser(s); - return ser(v); -} -template -typename basic_value::string_type -format(const typename basic_value::key_type& k, - const basic_value& v, - const spec s = spec::default_version()) -{ - detail::serializer ser(s); - return ser(k, v); -} -template -typename basic_value::string_type -format(const std::vector::key_type>& ks, - const basic_value& v, - const spec s = spec::default_version()) -{ - detail::serializer ser(s); - return ser(ks, v); -} - -template -std::ostream& operator<<(std::ostream& os, const basic_value& v) -{ - os << format(v); - return os; -} - -} // toml - -#if defined(TOML11_COMPILE_SOURCES) -namespace toml -{ -struct type_config; -struct ordered_type_config; - -extern template typename basic_value::string_type -format(const basic_value&, const spec); - -extern template typename basic_value::string_type -format(const typename basic_value::key_type& k, - const basic_value& v, const spec); - -extern template typename basic_value::string_type -format(const std::vector::key_type>& ks, - const basic_value& v, const spec s); - -extern template typename basic_value::string_type -format(const basic_value&, const spec); - -extern template typename basic_value::string_type -format(const typename basic_value::key_type& k, - const basic_value& v, const spec); - -extern template typename basic_value::string_type -format(const std::vector::key_type>& ks, - const basic_value& v, const spec s); - -namespace detail -{ -extern template class serializer<::toml::type_config>; -extern template class serializer<::toml::ordered_type_config>; -} // detail -} // toml -#endif // TOML11_COMPILE_SOURCES - - -#endif // TOML11_SERIALIZER_HPP diff --git a/include/toml11/skip.hpp b/include/toml11/skip.hpp deleted file mode 100644 index 5a3b1b7..0000000 --- a/include/toml11/skip.hpp +++ /dev/null @@ -1,392 +0,0 @@ -#ifndef TOML11_SKIP_HPP -#define TOML11_SKIP_HPP - -#include "context.hpp" -#include "region.hpp" -#include "scanner.hpp" -#include "syntax.hpp" -#include "types.hpp" - -#include - -namespace toml -{ -namespace detail -{ - -template -bool skip_whitespace(location& loc, const context& ctx) -{ - return syntax::ws(ctx.toml_spec()).scan(loc).is_ok(); -} - -template -bool skip_empty_lines(location& loc, const context& ctx) -{ - return repeat_at_least(1, sequence( - syntax::ws(ctx.toml_spec()), - syntax::newline(ctx.toml_spec()) - )).scan(loc).is_ok(); -} - -// For error recovery. -// -// In case if a comment line contains an invalid character, we need to skip it -// to advance parsing. -template -void skip_comment_block(location& loc, const context& ctx) -{ - while( ! loc.eof()) - { - skip_whitespace(loc, ctx); - if(loc.current() == '#') - { - while( ! loc.eof()) - { - // both CRLF and LF ends with LF. - if(loc.current() == '\n') - { - loc.advance(); - break; - } - } - } - else if(syntax::newline(ctx.toml_spec()).scan(loc).is_ok()) - { - ; // an empty line. skip this also - } - else - { - // the next token is neither a comment nor empty line. - return ; - } - } - return ; -} - -template -void skip_empty_or_comment_lines(location& loc, const context& ctx) -{ - const auto& spec = ctx.toml_spec(); - repeat_at_least(0, sequence( - syntax::ws(spec), - maybe(syntax::comment(spec)), - syntax::newline(spec)) - ).scan(loc); - return ; -} - -// For error recovery. -// -// Sometimes we need to skip a value and find a delimiter, like `,`, `]`, or `}`. -// To find delimiter, we need to skip delimiters in a string. -// Since we are skipping invalid value while error recovery, we don't need -// to check the syntax. Here we just skip string-like region until closing quote -// is found. -template -void skip_string_like(location& loc, const context&) -{ - // if """ is found, skip until the closing """ is found. - if(literal("\"\"\"").scan(loc).is_ok()) - { - while( ! loc.eof()) - { - if(literal("\"\"\"").scan(loc).is_ok()) - { - return; - } - loc.advance(); - } - } - else if(literal("'''").scan(loc).is_ok()) - { - while( ! loc.eof()) - { - if(literal("'''").scan(loc).is_ok()) - { - return; - } - loc.advance(); - } - } - // if " is found, skip until the closing " or newline is found. - else if(loc.current() == '"') - { - while( ! loc.eof()) - { - loc.advance(); - if(loc.current() == '"' || loc.current() == '\n') - { - loc.advance(); - return; - } - } - } - else if(loc.current() == '\'') - { - while( ! loc.eof()) - { - loc.advance(); - if(loc.current() == '\'' || loc.current() == '\n') - { - loc.advance(); - return ; - } - } - } - return; -} - -template -void skip_value(location& loc, const context& ctx); -template -void skip_array_like(location& loc, const context& ctx); -template -void skip_inline_table_like(location& loc, const context& ctx); -template -void skip_key_value_pair(location& loc, const context& ctx); - -template -result -guess_value_type(const location& loc, const context& ctx); - -template -void skip_array_like(location& loc, const context& ctx) -{ - const auto& spec = ctx.toml_spec(); - assert(loc.current() == '['); - loc.advance(); - - while( ! loc.eof()) - { - if(loc.current() == '\"' || loc.current() == '\'') - { - skip_string_like(loc, ctx); - } - else if(loc.current() == '#') - { - skip_comment_block(loc, ctx); - } - else if(loc.current() == '{') - { - skip_inline_table_like(loc, ctx); - } - else if(loc.current() == '[') - { - const auto checkpoint = loc; - if(syntax::std_table(spec).scan(loc).is_ok() || - syntax::array_table(spec).scan(loc).is_ok()) - { - loc = checkpoint; - break; - } - // if it is not a table-definition, then it is an array. - skip_array_like(loc, ctx); - } - else if(loc.current() == '=') - { - // key-value pair cannot be inside the array. - // guessing the error is "missing closing bracket `]`". - // find the previous key just before `=`. - while(loc.get_location() != 0) - { - loc.retrace(); - if(loc.current() == '\n') - { - loc.advance(); - break; - } - } - break; - } - else if(loc.current() == ']') - { - break; // found closing bracket - } - else - { - loc.advance(); - } - } - return ; -} - -template -void skip_inline_table_like(location& loc, const context& ctx) -{ - assert(loc.current() == '{'); - loc.advance(); - - const auto& spec = ctx.toml_spec(); - - while( ! loc.eof()) - { - if(loc.current() == '\n' && ! spec.v1_1_0_allow_newlines_in_inline_tables) - { - break; // missing closing `}`. - } - else if(loc.current() == '\"' || loc.current() == '\'') - { - skip_string_like(loc, ctx); - } - else if(loc.current() == '#') - { - skip_comment_block(loc, ctx); - if( ! spec.v1_1_0_allow_newlines_in_inline_tables) - { - // comment must end with newline. - break; // missing closing `}`. - } - } - else if(loc.current() == '[') - { - const auto checkpoint = loc; - if(syntax::std_table(spec).scan(loc).is_ok() || - syntax::array_table(spec).scan(loc).is_ok()) - { - loc = checkpoint; - break; // missing closing `}`. - } - // if it is not a table-definition, then it is an array. - skip_array_like(loc, ctx); - } - else if(loc.current() == '{') - { - skip_inline_table_like(loc, ctx); - } - else if(loc.current() == '}') - { - // closing brace found. guessing the error is inside the table. - break; - } - else - { - // skip otherwise. - loc.advance(); - } - } - return ; -} - -template -void skip_value(location& loc, const context& ctx) -{ - value_t ty = guess_value_type(loc, ctx).unwrap_or(value_t::empty); - if(ty == value_t::string) - { - skip_string_like(loc, ctx); - } - else if(ty == value_t::array) - { - skip_array_like(loc, ctx); - } - else if(ty == value_t::table) - { - // In case of multiline tables, it may skip key-value pair but not the - // whole table. - skip_inline_table_like(loc, ctx); - } - else // others are an "in-line" values. skip until the next line - { - while( ! loc.eof()) - { - if(loc.current() == '\n') - { - break; - } - else if(loc.current() == ',' || loc.current() == ']' || loc.current() == '}') - { - break; - } - loc.advance(); - } - } - return; -} - -template -void skip_key_value_pair(location& loc, const context& ctx) -{ - while( ! loc.eof()) - { - if(loc.current() == '=') - { - skip_whitespace(loc, ctx); - skip_value(loc, ctx); - return; - } - else if(loc.current() == '\n') - { - // newline is found before finding `=`. assuming "missing `=`". - return; - } - loc.advance(); - } - return ; -} - -template -void skip_until_next_table(location& loc, const context& ctx) -{ - const auto& spec = ctx.toml_spec(); - while( ! loc.eof()) - { - if(loc.current() == '\n') - { - loc.advance(); - const auto line_begin = loc; - - skip_whitespace(loc, ctx); - if(syntax::std_table(spec).scan(loc).is_ok()) - { - loc = line_begin; - return ; - } - if(syntax::array_table(spec).scan(loc).is_ok()) - { - loc = line_begin; - return ; - } - } - loc.advance(); - } -} - -} // namespace detail -} // namespace toml - -#if defined(TOML11_COMPILE_SOURCES) -namespace toml -{ -struct type_config; -struct ordered_type_config; - -namespace detail -{ -extern template bool skip_whitespace (location& loc, const context&); -extern template bool skip_empty_lines (location& loc, const context&); -extern template void skip_comment_block (location& loc, const context&); -extern template void skip_empty_or_comment_lines(location& loc, const context&); -extern template void skip_string_like (location& loc, const context&); -extern template void skip_array_like (location& loc, const context&); -extern template void skip_inline_table_like (location& loc, const context&); -extern template void skip_value (location& loc, const context&); -extern template void skip_key_value_pair (location& loc, const context&); -extern template void skip_until_next_table (location& loc, const context&); - -extern template bool skip_whitespace (location& loc, const context&); -extern template bool skip_empty_lines (location& loc, const context&); -extern template void skip_comment_block (location& loc, const context&); -extern template void skip_empty_or_comment_lines(location& loc, const context&); -extern template void skip_string_like (location& loc, const context&); -extern template void skip_array_like (location& loc, const context&); -extern template void skip_inline_table_like (location& loc, const context&); -extern template void skip_value (location& loc, const context&); -extern template void skip_key_value_pair (location& loc, const context&); -extern template void skip_until_next_table (location& loc, const context&); - -} // detail -} // toml -#endif // TOML11_COMPILE_SOURCES - -#endif // TOML11_SKIP_HPP diff --git a/include/toml11/source_location.hpp b/include/toml11/source_location.hpp deleted file mode 100644 index 02e6285..0000000 --- a/include/toml11/source_location.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_SOURCE_LOCATION_HPP -#define TOML11_SOURCE_LOCATION_HPP - -#include "fwd/source_location_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/source_location_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_SOURCE_LOCATION_HPP diff --git a/include/toml11/spec.hpp b/include/toml11/spec.hpp deleted file mode 100644 index ca0a996..0000000 --- a/include/toml11/spec.hpp +++ /dev/null @@ -1,121 +0,0 @@ -#ifndef TOML11_SPEC_HPP -#define TOML11_SPEC_HPP - -#include -#include - -#include - -namespace toml -{ - -struct semantic_version -{ - constexpr semantic_version(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept - : major{mjr}, minor{mnr}, patch{p} - {} - - std::uint32_t major; - std::uint32_t minor; - std::uint32_t patch; -}; - -constexpr inline semantic_version -make_semver(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept -{ - return semantic_version(mjr, mnr, p); -} - -constexpr inline bool -operator==(const semantic_version& lhs, const semantic_version& rhs) noexcept -{ - return lhs.major == rhs.major && - lhs.minor == rhs.minor && - lhs.patch == rhs.patch; -} -constexpr inline bool -operator!=(const semantic_version& lhs, const semantic_version& rhs) noexcept -{ - return !(lhs == rhs); -} -constexpr inline bool -operator<(const semantic_version& lhs, const semantic_version& rhs) noexcept -{ - return lhs.major < rhs.major || - (lhs.major == rhs.major && lhs.minor < rhs.minor) || - (lhs.major == rhs.major && lhs.minor == rhs.minor && lhs.patch < rhs.patch); -} -constexpr inline bool -operator>(const semantic_version& lhs, const semantic_version& rhs) noexcept -{ - return rhs < lhs; -} -constexpr inline bool -operator<=(const semantic_version& lhs, const semantic_version& rhs) noexcept -{ - return !(lhs > rhs); -} -constexpr inline bool -operator>=(const semantic_version& lhs, const semantic_version& rhs) noexcept -{ - return !(lhs < rhs); -} - -inline std::ostream& operator<<(std::ostream& os, const semantic_version& v) -{ - os << v.major << '.' << v.minor << '.' << v.patch; - return os; -} - -inline std::string to_string(const semantic_version& v) -{ - std::ostringstream oss; - oss << v; - return oss.str(); -} - -struct spec -{ - constexpr static spec default_version() noexcept - { - return spec::v(1, 0, 0); - } - - constexpr static spec v(std::uint32_t mjr, std::uint32_t mnr, std::uint32_t p) noexcept - { - return spec(make_semver(mjr, mnr, p)); - } - - constexpr explicit spec(const semantic_version& semver) noexcept - : version{semver}, - v1_1_0_allow_control_characters_in_comments {semantic_version{1, 1, 0} <= semver}, - v1_1_0_allow_newlines_in_inline_tables {semantic_version{1, 1, 0} <= semver}, - v1_1_0_allow_trailing_comma_in_inline_tables{semantic_version{1, 1, 0} <= semver}, - v1_1_0_allow_non_english_in_bare_keys {semantic_version{1, 1, 0} <= semver}, - v1_1_0_add_escape_sequence_e {semantic_version{1, 1, 0} <= semver}, - v1_1_0_add_escape_sequence_x {semantic_version{1, 1, 0} <= semver}, - v1_1_0_make_seconds_optional {semantic_version{1, 1, 0} <= semver}, - ext_hex_float {false}, - ext_num_suffix{false}, - ext_null_value{false} - {} - - semantic_version version; // toml version - - // diff from v1.0.0 -> v1.1.0 - bool v1_1_0_allow_control_characters_in_comments; - bool v1_1_0_allow_newlines_in_inline_tables; - bool v1_1_0_allow_trailing_comma_in_inline_tables; - bool v1_1_0_allow_non_english_in_bare_keys; - bool v1_1_0_add_escape_sequence_e; - bool v1_1_0_add_escape_sequence_x; - bool v1_1_0_make_seconds_optional; - - // library extensions - bool ext_hex_float; // allow hex float (in C++ style) - bool ext_num_suffix; // allow number suffix (in C++ style) - bool ext_null_value; // allow `null` as a value -}; - -} // namespace toml -#endif // TOML11_SPEC_HPP diff --git a/include/toml11/storage.hpp b/include/toml11/storage.hpp deleted file mode 100644 index b63e24f..0000000 --- a/include/toml11/storage.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef TOML11_STORAGE_HPP -#define TOML11_STORAGE_HPP - -#include "compat.hpp" - -namespace toml -{ -namespace detail -{ - -// It owns a pointer to T. It does deep-copy when copied. -// This struct is introduced to implement a recursive type. -// -// `toml::value` contains `std::vector` to represent a toml array. -// But, in the definition of `toml::value`, `toml::value` is still incomplete. -// `std::vector` of an incomplete type is not allowed in C++11 (it is allowed -// after C++17). To avoid this, we need to use a pointer to `toml::value`, like -// `std::vector>`. Although `std::unique_ptr` is -// noncopyable, we want to make `toml::value` copyable. `storage` is introduced -// to resolve those problems. -template -struct storage -{ - using value_type = T; - - explicit storage(value_type v): ptr_(cxx::make_unique(std::move(v))) {} - ~storage() = default; - - storage(const storage& rhs): ptr_(cxx::make_unique(*rhs.ptr_)) {} - storage& operator=(const storage& rhs) - { - this->ptr_ = cxx::make_unique(*rhs.ptr_); - return *this; - } - - storage(storage&&) = default; - storage& operator=(storage&&) = default; - - bool is_ok() const noexcept {return static_cast(ptr_);} - - value_type& get() const noexcept {return *ptr_;} - - private: - std::unique_ptr ptr_; -}; - -} // detail -} // toml -#endif // TOML11_STORAGE_HPP diff --git a/include/toml11/syntax.hpp b/include/toml11/syntax.hpp deleted file mode 100644 index 912535d..0000000 --- a/include/toml11/syntax.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_SYNTAX_HPP -#define TOML11_SYNTAX_HPP - -#include "fwd/syntax_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/syntax_impl.hpp" // IWYU pragma: export -#endif - -#endif// TOML11_SYNTAX_HPP diff --git a/include/toml11/traits.hpp b/include/toml11/traits.hpp deleted file mode 100644 index 7a34fbb..0000000 --- a/include/toml11/traits.hpp +++ /dev/null @@ -1,254 +0,0 @@ -#ifndef TOML11_TRAITS_HPP -#define TOML11_TRAITS_HPP - -#include "from.hpp" -#include "into.hpp" -#include "compat.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#if defined(TOML11_HAS_STRING_VIEW) -#include -#endif - -#if defined(TOML11_HAS_OPTIONAL) -#include -#endif - -namespace toml -{ -template -class basic_value; - -namespace detail -{ -// --------------------------------------------------------------------------- -// check whether type T is a kind of container/map class - -struct has_iterator_impl -{ - template static std::true_type check(typename T::iterator*); - template static std::false_type check(...); -}; -struct has_value_type_impl -{ - template static std::true_type check(typename T::value_type*); - template static std::false_type check(...); -}; -struct has_key_type_impl -{ - template static std::true_type check(typename T::key_type*); - template static std::false_type check(...); -}; -struct has_mapped_type_impl -{ - template static std::true_type check(typename T::mapped_type*); - template static std::false_type check(...); -}; -struct has_reserve_method_impl -{ - template static std::false_type check(...); - template static std::true_type check( - decltype(std::declval().reserve(std::declval()))*); -}; -struct has_push_back_method_impl -{ - template static std::false_type check(...); - template static std::true_type check( - decltype(std::declval().push_back(std::declval()))*); -}; -struct is_comparable_impl -{ - template static std::false_type check(...); - template static std::true_type check( - decltype(std::declval() < std::declval())*); -}; - -struct has_from_toml_method_impl -{ - template - static std::true_type check( - decltype(std::declval().from_toml(std::declval<::toml::basic_value>()))*); - - template - static std::false_type check(...); -}; -struct has_into_toml_method_impl -{ - template - static std::true_type check(decltype(std::declval().into_toml())*); - template - static std::false_type check(...); -}; - -struct has_template_into_toml_method_impl -{ - template - static std::true_type check(decltype(std::declval().template into_toml())*); - template - static std::false_type check(...); -}; - -struct has_specialized_from_impl -{ - template - static std::false_type check(...); - template)> - static std::true_type check(::toml::from*); -}; -struct has_specialized_into_impl -{ - template - static std::false_type check(...); - template)> - static std::true_type check(::toml::into*); -}; - - -/// Intel C++ compiler can not use decltype in parent class declaration, here -/// is a hack to work around it. https://stackoverflow.com/a/23953090/4692076 -#ifdef __INTEL_COMPILER -#define decltype(...) std::enable_if::type -#endif - -template -struct has_iterator: decltype(has_iterator_impl::check(nullptr)){}; -template -struct has_value_type: decltype(has_value_type_impl::check(nullptr)){}; -template -struct has_key_type: decltype(has_key_type_impl::check(nullptr)){}; -template -struct has_mapped_type: decltype(has_mapped_type_impl::check(nullptr)){}; -template -struct has_reserve_method: decltype(has_reserve_method_impl::check(nullptr)){}; -template -struct has_push_back_method: decltype(has_push_back_method_impl::check(nullptr)){}; -template -struct is_comparable: decltype(is_comparable_impl::check(nullptr)){}; - -template -struct has_from_toml_method: decltype(has_from_toml_method_impl::check(nullptr)){}; - -template -struct has_into_toml_method: decltype(has_into_toml_method_impl::check(nullptr)){}; - -template -struct has_template_into_toml_method: decltype(has_template_into_toml_method_impl::check(nullptr)){}; - -template -struct has_specialized_from: decltype(has_specialized_from_impl::check(nullptr)){}; -template -struct has_specialized_into: decltype(has_specialized_into_impl::check(nullptr)){}; - -#ifdef __INTEL_COMPILER -#undef decltype -#endif - -// --------------------------------------------------------------------------- -// type checkers - -template struct is_std_pair_impl : std::false_type{}; -template -struct is_std_pair_impl> : std::true_type{}; -template -using is_std_pair = is_std_pair_impl>; - -template struct is_std_tuple_impl : std::false_type{}; -template -struct is_std_tuple_impl> : std::true_type{}; -template -using is_std_tuple = is_std_tuple_impl>; - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if __has_include() -template struct is_std_optional_impl : std::false_type{}; -template -struct is_std_optional_impl> : std::true_type{}; -template -using is_std_optional = is_std_optional_impl>; -# endif // -#endif // > C++17 - -template struct is_std_array_impl : std::false_type{}; -template -struct is_std_array_impl> : std::true_type{}; -template -using is_std_array = is_std_array_impl>; - -template struct is_std_forward_list_impl : std::false_type{}; -template -struct is_std_forward_list_impl> : std::true_type{}; -template -using is_std_forward_list = is_std_forward_list_impl>; - -template struct is_std_basic_string_impl : std::false_type{}; -template -struct is_std_basic_string_impl> : std::true_type{}; -template -using is_std_basic_string = is_std_basic_string_impl>; - -template struct is_1byte_std_basic_string_impl : std::false_type{}; -template -struct is_1byte_std_basic_string_impl> - : std::integral_constant {}; -template -using is_1byte_std_basic_string = is_std_basic_string_impl>; - -#if defined(TOML11_HAS_STRING_VIEW) -template struct is_std_basic_string_view_impl : std::false_type{}; -template -struct is_std_basic_string_view_impl> : std::true_type{}; -template -using is_std_basic_string_view = is_std_basic_string_view_impl>; - -template -struct is_string_view_of : std::false_type {}; -template -struct is_string_view_of, std::basic_string> : std::true_type {}; -#endif - -template struct is_chrono_duration_impl: std::false_type{}; -template -struct is_chrono_duration_impl>: std::true_type{}; -template -using is_chrono_duration = is_chrono_duration_impl>; - -template -struct is_map_impl : cxx::conjunction< // map satisfies all the following conditions - has_iterator, // has T::iterator - has_value_type, // has T::value_type - has_key_type, // has T::key_type - has_mapped_type // has T::mapped_type - >{}; -template -using is_map = is_map_impl>; - -template -struct is_container_impl : cxx::conjunction< - cxx::negation>, // not a map - cxx::negation>, // not a std::string -#ifdef TOML11_HAS_STRING_VIEW - cxx::negation>, // not a std::string_view -#endif - has_iterator, // has T::iterator - has_value_type // has T::value_type - >{}; -template -using is_container = is_container_impl>; - -template -struct is_basic_value_impl: std::false_type{}; -template -struct is_basic_value_impl<::toml::basic_value>: std::true_type{}; -template -using is_basic_value = is_basic_value_impl>; - -}// detail -}//toml -#endif // TOML11_TRAITS_HPP diff --git a/include/toml11/types.hpp b/include/toml11/types.hpp deleted file mode 100644 index 6d59b99..0000000 --- a/include/toml11/types.hpp +++ /dev/null @@ -1,374 +0,0 @@ -#ifndef TOML11_TYPES_HPP -#define TOML11_TYPES_HPP - -#include "comments.hpp" -#include "compat.hpp" -#include "error_info.hpp" -#include "format.hpp" -#include "ordered_map.hpp" -#include "value.hpp" - -#include -#include -#include -#include -#include -#include - -#include -#include - -namespace toml -{ - -// forward decl -template -class basic_value; - -// when you use a special integer type as toml::value::integer_type, parse must -// be able to read it. So, type_config has static member functions that read the -// integer_type as {dec, hex, oct, bin}-integer. But, in most cases, operator<< -// is enough. To make config easy, we provide the default read functions. -// -// Before this functions is called, syntax is checked and prefix(`0x` etc) and -// spacer(`_`) are removed. - -template -result -read_dec_int(const std::string& str, const source_location src) -{ - constexpr auto max_digits = std::numeric_limits::digits; - assert( ! str.empty()); - - T val{0}; - std::istringstream iss(str); - iss >> val; - if(iss.fail()) - { - return err(make_error_info("toml::parse_dec_integer: " - "too large integer: current max digits = 2^" + std::to_string(max_digits), - std::move(src), "must be < 2^" + std::to_string(max_digits))); - } - return ok(val); -} - -template -result -read_hex_int(const std::string& str, const source_location src) -{ - constexpr auto max_digits = std::numeric_limits::digits; - assert( ! str.empty()); - - T val{0}; - std::istringstream iss(str); - iss >> std::hex >> val; - if(iss.fail()) - { - return err(make_error_info("toml::parse_hex_integer: " - "too large integer: current max value = 2^" + std::to_string(max_digits), - std::move(src), "must be < 2^" + std::to_string(max_digits))); - } - return ok(val); -} - -template -result -read_oct_int(const std::string& str, const source_location src) -{ - constexpr auto max_digits = std::numeric_limits::digits; - assert( ! str.empty()); - - T val{0}; - std::istringstream iss(str); - iss >> std::oct >> val; - if(iss.fail()) - { - return err(make_error_info("toml::parse_oct_integer: " - "too large integer: current max value = 2^" + std::to_string(max_digits), - std::move(src), "must be < 2^" + std::to_string(max_digits))); - } - return ok(val); -} - -template -result -read_bin_int(const std::string& str, const source_location src) -{ - constexpr auto is_bounded = std::numeric_limits::is_bounded; - constexpr auto max_digits = std::numeric_limits::digits; - const auto max_value = (std::numeric_limits::max)(); - - T val{0}; - T base{1}; - for(auto i = str.rbegin(); i != str.rend(); ++i) - { - const auto c = *i; - if(c == '1') - { - val += base; - // prevent `base` from overflow - if(is_bounded && max_value / 2 < base && std::next(i) != str.rend()) - { - base = 0; - } - else - { - base *= 2; - } - } - else - { - assert(c == '0'); - - if(is_bounded && max_value / 2 < base && std::next(i) != str.rend()) - { - base = 0; - } - else - { - base *= 2; - } - } - } - if(base == 0) - { - return err(make_error_info("toml::parse_bin_integer: " - "too large integer: current max value = 2^" + std::to_string(max_digits), - std::move(src), "must be < 2^" + std::to_string(max_digits))); - } - return ok(val); -} - -template -result -read_int(const std::string& str, const source_location src, const std::uint8_t base) -{ - assert(base == 10 || base == 16 || base == 8 || base == 2); - switch(base) - { - case 2: { return read_bin_int(str, src); } - case 8: { return read_oct_int(str, src); } - case 16: { return read_hex_int(str, src); } - default: - { - assert(base == 10); - return read_dec_int(str, src); - } - } -} - -inline result -read_hex_float(const std::string& str, const source_location src, float val) -{ -#if defined(_MSC_VER) && ! defined(__clang__) - const auto res = ::sscanf_s(str.c_str(), "%a", std::addressof(val)); -#else - const auto res = std::sscanf(str.c_str(), "%a", std::addressof(val)); -#endif - if(res != 1) - { - return err(make_error_info("toml::parse_floating: " - "failed to read hexadecimal floating point value ", - std::move(src), "here")); - } - return ok(val); -} -inline result -read_hex_float(const std::string& str, const source_location src, double val) -{ -#if defined(_MSC_VER) && ! defined(__clang__) - const auto res = ::sscanf_s(str.c_str(), "%la", std::addressof(val)); -#else - const auto res = std::sscanf(str.c_str(), "%la", std::addressof(val)); -#endif - if(res != 1) - { - return err(make_error_info("toml::parse_floating: " - "failed to read hexadecimal floating point value ", - std::move(src), "here")); - } - return ok(val); -} -template -cxx::enable_if_t, double>>, - cxx::negation, float>> - >::value, result> -read_hex_float(const std::string&, const source_location src, T) -{ - return err(make_error_info("toml::parse_floating: failed to read " - "floating point value because of unknown type in type_config", - std::move(src), "here")); -} - -template -result -read_dec_float(const std::string& str, const source_location src) -{ - T val; - std::istringstream iss(str); - iss >> val; - if(iss.fail()) - { - return err(make_error_info("toml::parse_floating: " - "failed to read floating point value from stream", - std::move(src), "here")); - } - return ok(val); -} - -template -result -read_float(const std::string& str, const source_location src, const bool is_hex) -{ - if(is_hex) - { - return read_hex_float(str, src, T{}); - } - else - { - return read_dec_float(str, src); - } -} - -struct type_config -{ - using comment_type = preserve_comments; - - using boolean_type = bool; - using integer_type = std::int64_t; - using floating_type = double; - using string_type = std::string; - - template - using array_type = std::vector; - template - using table_type = std::unordered_map; - - static result - parse_int(const std::string& str, const source_location src, const std::uint8_t base) - { - return read_int(str, src, base); - } - static result - parse_float(const std::string& str, const source_location src, const bool is_hex) - { - return read_float(str, src, is_hex); - } -}; - -using value = basic_value; -using table = typename value::table_type; -using array = typename value::array_type; - -struct ordered_type_config -{ - using comment_type = preserve_comments; - - using boolean_type = bool; - using integer_type = std::int64_t; - using floating_type = double; - using string_type = std::string; - - template - using array_type = std::vector; - template - using table_type = ordered_map; - - static result - parse_int(const std::string& str, const source_location src, const std::uint8_t base) - { - return read_int(str, src, base); - } - static result - parse_float(const std::string& str, const source_location src, const bool is_hex) - { - return read_float(str, src, is_hex); - } -}; - -using ordered_value = basic_value; -using ordered_table = typename ordered_value::table_type; -using ordered_array = typename ordered_value::array_type; - -// ---------------------------------------------------------------------------- -// meta functions for internal use - -namespace detail -{ - -// ---------------------------------------------------------------------------- -// check if type T has all the needed member types - -template -struct has_comment_type: std::false_type{}; -template -struct has_comment_type>: std::true_type{}; - -template -struct has_integer_type: std::false_type{}; -template -struct has_integer_type>: std::true_type{}; - -template -struct has_floating_type: std::false_type{}; -template -struct has_floating_type>: std::true_type{}; - -template -struct has_string_type: std::false_type{}; -template -struct has_string_type>: std::true_type{}; - -template -struct has_array_type: std::false_type{}; -template -struct has_array_type>>: std::true_type{}; - -template -struct has_table_type: std::false_type{}; -template -struct has_table_type>>: std::true_type{}; - -template -struct has_parse_int: std::false_type{}; -template -struct has_parse_int().parse_int( - std::declval(), - std::declval<::toml::source_location const&>(), - std::declval() - ))>>: std::true_type{}; - -template -struct has_parse_float: std::false_type{}; -template -struct has_parse_float().parse_float( - std::declval(), - std::declval<::toml::source_location const&>(), - std::declval() - ))>>: std::true_type{}; - -template -using is_type_config = cxx::conjunction< - has_comment_type, - has_integer_type, - has_floating_type, - has_string_type, - has_array_type, - has_table_type, - has_parse_int, - has_parse_float - >; - -} // namespace detail -} // namespace toml - -#if defined(TOML11_COMPILE_SOURCES) -namespace toml -{ -extern template class basic_value; -extern template class basic_value; -} // toml -#endif // TOML11_COMPILE_SOURCES - -#endif // TOML11_TYPES_HPP diff --git a/include/toml11/utility.hpp b/include/toml11/utility.hpp deleted file mode 100644 index 9e30ccb..0000000 --- a/include/toml11/utility.hpp +++ /dev/null @@ -1,170 +0,0 @@ -#ifndef TOML11_UTILITY_HPP -#define TOML11_UTILITY_HPP - -#include "result.hpp" -#include "traits.hpp" - -#include -#include - -#include -#include -#include - -namespace toml -{ -namespace detail -{ - -// to output character in an error message. -inline std::string show_char(const int c) -{ - using char_type = unsigned char; - if(std::isgraph(c)) - { - return std::string(1, static_cast(c)); - } - else - { - std::array buf; - buf.fill('\0'); - const auto r = std::snprintf(buf.data(), buf.size(), "0x%02x", c & 0xFF); - assert(r == static_cast(buf.size()) - 1); - (void) r; // Unused variable warning - auto in_hex = std::string(buf.data()); - switch(c) - { - case char_type('\0'): {in_hex += "(NUL)"; break;} - case char_type(' ') : {in_hex += "(SPACE)"; break;} - case char_type('\n'): {in_hex += "(LINE FEED)"; break;} - case char_type('\r'): {in_hex += "(CARRIAGE RETURN)"; break;} - case char_type('\t'): {in_hex += "(TAB)"; break;} - case char_type('\v'): {in_hex += "(VERTICAL TAB)"; break;} - case char_type('\f'): {in_hex += "(FORM FEED)"; break;} - case char_type('\x1B'): {in_hex += "(ESCAPE)"; break;} - default: break; - } - return in_hex; - } -} - -// --------------------------------------------------------------------------- - -template -void try_reserve_impl(Container& container, std::size_t N, std::true_type) -{ - container.reserve(N); - return; -} -template -void try_reserve_impl(Container&, std::size_t, std::false_type) noexcept -{ - return; -} - -template -void try_reserve(Container& container, std::size_t N) -{ - try_reserve_impl(container, N, has_reserve_method{}); - return; -} - -// --------------------------------------------------------------------------- - -template -result from_string(const std::string& str) -{ - T v; - std::istringstream iss(str); - iss >> v; - if(iss.fail()) - { - return err(); - } - return ok(v); -} - -// --------------------------------------------------------------------------- - -// helper function to avoid std::string(0, 'c') or std::string(iter, iter) -template -std::string make_string(Iterator first, Iterator last) -{ - if(first == last) {return "";} - return std::string(first, last); -} -inline std::string make_string(std::size_t len, char c) -{ - if(len == 0) {return "";} - return std::string(len, c); -} - -// --------------------------------------------------------------------------- - -template -struct string_conv_impl -{ - static_assert(sizeof(Char) == sizeof(char), ""); - static_assert(sizeof(Char2) == sizeof(char), ""); - - static std::basic_string invoke(std::basic_string s) - { - std::basic_string retval; - std::transform(s.begin(), s.end(), std::back_inserter(retval), - [](const Char2 c) {return static_cast(c);}); - return retval; - } - template - static std::basic_string invoke(const Char2 (&s)[N]) - { - std::basic_string retval; - // "string literal" has null-char at the end. to skip it, we use prev. - std::transform(std::begin(s), std::prev(std::end(s)), std::back_inserter(retval), - [](const Char2 c) {return static_cast(c);}); - return retval; - } -}; - -template -struct string_conv_impl -{ - static_assert(sizeof(Char) == sizeof(char), ""); - - static std::basic_string invoke(std::basic_string s) - { - return s; - } - template - static std::basic_string invoke(const Char (&s)[N]) - { - return std::basic_string(s); - } -}; - -template -cxx::enable_if_t::value, S> -string_conv(std::basic_string s) -{ - using C = typename S::value_type; - using T = typename S::traits_type; - using A = typename S::allocator_type; - return string_conv_impl::invoke(std::move(s)); -} -template -cxx::enable_if_t::value, S> -string_conv(const char (&s)[N]) -{ - using C = typename S::value_type; - using T = typename S::traits_type; - using A = typename S::allocator_type; - using C2 = char; - using T2 = std::char_traits; - using A2 = std::allocator; - - return string_conv_impl::template invoke(s); -} - -} // namespace detail -} // namespace toml -#endif // TOML11_UTILITY_HPP diff --git a/include/toml11/value.hpp b/include/toml11/value.hpp deleted file mode 100644 index d945fa0..0000000 --- a/include/toml11/value.hpp +++ /dev/null @@ -1,2257 +0,0 @@ -#ifndef TOML11_VALUE_HPP -#define TOML11_VALUE_HPP - -#include "color.hpp" -#include "datetime.hpp" -#include "exception.hpp" -#include "error_info.hpp" -#include "format.hpp" -#include "region.hpp" -#include "source_location.hpp" -#include "storage.hpp" -#include "traits.hpp" -#include "value_t.hpp" -#include "version.hpp" // IWYU pragma: keep < TOML11_HAS_STRING_VIEW - -#ifdef TOML11_HAS_STRING_VIEW -#include -#endif - -#include - -namespace toml -{ -template -class basic_value; - -struct type_error final : public ::toml::exception -{ - public: - type_error(std::string what_arg, source_location loc) - : what_(std::move(what_arg)), loc_(std::move(loc)) - {} - ~type_error() noexcept override = default; - - const char* what() const noexcept override {return what_.c_str();} - - source_location const& location() const noexcept {return loc_;} - - private: - std::string what_; - source_location loc_; -}; - -// only for internal use -namespace detail -{ -template -error_info make_type_error(const basic_value&, const std::string&, const value_t); - -template -error_info make_not_found_error(const basic_value&, const std::string&, const typename basic_value::key_type&); - -template -void change_region_of_value(basic_value&, const basic_value&); - -template -struct getter; -} // detail - -template -class basic_value -{ - public: - - using config_type = TypeConfig; - using key_type = typename config_type::string_type; - using value_type = basic_value; - using boolean_type = typename config_type::boolean_type; - using integer_type = typename config_type::integer_type; - using floating_type = typename config_type::floating_type; - using string_type = typename config_type::string_type; - using local_time_type = ::toml::local_time; - using local_date_type = ::toml::local_date; - using local_datetime_type = ::toml::local_datetime; - using offset_datetime_type = ::toml::offset_datetime; - using array_type = typename config_type::template array_type; - using table_type = typename config_type::template table_type; - using comment_type = typename config_type::comment_type; - using char_type = typename string_type::value_type; - - private: - - using region_type = detail::region; - - public: - - basic_value() noexcept - : type_(value_t::empty), empty_('\0'), region_{}, comments_{} - {} - ~basic_value() noexcept {this->cleanup();} - - // copy/move constructor/assigner ===================================== {{{ - - basic_value(const basic_value& v) - : type_(v.type_), region_(v.region_), comments_(v.comments_) - { - switch(this->type_) - { - case value_t::boolean : assigner(boolean_ , v.boolean_ ); break; - case value_t::integer : assigner(integer_ , v.integer_ ); break; - case value_t::floating : assigner(floating_ , v.floating_ ); break; - case value_t::string : assigner(string_ , v.string_ ); break; - case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break; - case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break; - case value_t::local_date : assigner(local_date_ , v.local_date_ ); break; - case value_t::local_time : assigner(local_time_ , v.local_time_ ); break; - case value_t::array : assigner(array_ , v.array_ ); break; - case value_t::table : assigner(table_ , v.table_ ); break; - default : assigner(empty_ , '\0' ); break; - } - } - basic_value(basic_value&& v) - : type_(v.type()), region_(std::move(v.region_)), - comments_(std::move(v.comments_)) - { - switch(this->type_) - { - case value_t::boolean : assigner(boolean_ , std::move(v.boolean_ )); break; - case value_t::integer : assigner(integer_ , std::move(v.integer_ )); break; - case value_t::floating : assigner(floating_ , std::move(v.floating_ )); break; - case value_t::string : assigner(string_ , std::move(v.string_ )); break; - case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break; - case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break; - case value_t::local_date : assigner(local_date_ , std::move(v.local_date_ )); break; - case value_t::local_time : assigner(local_time_ , std::move(v.local_time_ )); break; - case value_t::array : assigner(array_ , std::move(v.array_ )); break; - case value_t::table : assigner(table_ , std::move(v.table_ )); break; - default : assigner(empty_ , '\0' ); break; - } - } - - basic_value& operator=(const basic_value& v) - { - if(this == std::addressof(v)) {return *this;} - - this->cleanup(); - this->type_ = v.type_; - this->region_ = v.region_; - this->comments_ = v.comments_; - switch(this->type_) - { - case value_t::boolean : assigner(boolean_ , v.boolean_ ); break; - case value_t::integer : assigner(integer_ , v.integer_ ); break; - case value_t::floating : assigner(floating_ , v.floating_ ); break; - case value_t::string : assigner(string_ , v.string_ ); break; - case value_t::offset_datetime: assigner(offset_datetime_, v.offset_datetime_); break; - case value_t::local_datetime : assigner(local_datetime_ , v.local_datetime_ ); break; - case value_t::local_date : assigner(local_date_ , v.local_date_ ); break; - case value_t::local_time : assigner(local_time_ , v.local_time_ ); break; - case value_t::array : assigner(array_ , v.array_ ); break; - case value_t::table : assigner(table_ , v.table_ ); break; - default : assigner(empty_ , '\0' ); break; - } - return *this; - } - basic_value& operator=(basic_value&& v) - { - if(this == std::addressof(v)) {return *this;} - - this->cleanup(); - this->type_ = v.type_; - this->region_ = std::move(v.region_); - this->comments_ = std::move(v.comments_); - switch(this->type_) - { - case value_t::boolean : assigner(boolean_ , std::move(v.boolean_ )); break; - case value_t::integer : assigner(integer_ , std::move(v.integer_ )); break; - case value_t::floating : assigner(floating_ , std::move(v.floating_ )); break; - case value_t::string : assigner(string_ , std::move(v.string_ )); break; - case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break; - case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break; - case value_t::local_date : assigner(local_date_ , std::move(v.local_date_ )); break; - case value_t::local_time : assigner(local_time_ , std::move(v.local_time_ )); break; - case value_t::array : assigner(array_ , std::move(v.array_ )); break; - case value_t::table : assigner(table_ , std::move(v.table_ )); break; - default : assigner(empty_ , '\0' ); break; - } - return *this; - } - // }}} - - // constructor to overwrite commnets ================================== {{{ - - basic_value(basic_value v, std::vector com) - : type_(v.type()), region_(std::move(v.region_)), - comments_(std::move(com)) - { - switch(this->type_) - { - case value_t::boolean : assigner(boolean_ , std::move(v.boolean_ )); break; - case value_t::integer : assigner(integer_ , std::move(v.integer_ )); break; - case value_t::floating : assigner(floating_ , std::move(v.floating_ )); break; - case value_t::string : assigner(string_ , std::move(v.string_ )); break; - case value_t::offset_datetime: assigner(offset_datetime_, std::move(v.offset_datetime_)); break; - case value_t::local_datetime : assigner(local_datetime_ , std::move(v.local_datetime_ )); break; - case value_t::local_date : assigner(local_date_ , std::move(v.local_date_ )); break; - case value_t::local_time : assigner(local_time_ , std::move(v.local_time_ )); break; - case value_t::array : assigner(array_ , std::move(v.array_ )); break; - case value_t::table : assigner(table_ , std::move(v.table_ )); break; - default : assigner(empty_ , '\0' ); break; - } - } - // }}} - - // conversion between different basic_values ========================== {{{ - - template - basic_value(basic_value other) - : type_(other.type_), - region_(std::move(other.region_)), - comments_(std::move(other.comments_)) - { - switch(other.type_) - { - // use auto-convert in constructor - case value_t::boolean : assigner(boolean_ , std::move(other.boolean_ )); break; - case value_t::integer : assigner(integer_ , std::move(other.integer_ )); break; - case value_t::floating : assigner(floating_ , std::move(other.floating_ )); break; - case value_t::string : assigner(string_ , std::move(other.string_ )); break; - case value_t::offset_datetime: assigner(offset_datetime_, std::move(other.offset_datetime_)); break; - case value_t::local_datetime : assigner(local_datetime_ , std::move(other.local_datetime_ )); break; - case value_t::local_date : assigner(local_date_ , std::move(other.local_date_ )); break; - case value_t::local_time : assigner(local_time_ , std::move(other.local_time_ )); break; - - // may have different container type - case value_t::array : - { - array_type tmp( - std::make_move_iterator(other.array_.value.get().begin()), - std::make_move_iterator(other.array_.value.get().end())); - assigner(array_, array_storage( - detail::storage(std::move(tmp)), - other.array_.format - )); - break; - } - case value_t::table : - { - table_type tmp( - std::make_move_iterator(other.table_.value.get().begin()), - std::make_move_iterator(other.table_.value.get().end())); - assigner(table_, table_storage( - detail::storage(std::move(tmp)), - other.table_.format - )); - break; - } - default: break; - } - } - - template - basic_value(basic_value other, std::vector com) - : type_(other.type_), - region_(std::move(other.region_)), - comments_(std::move(com)) - { - switch(other.type_) - { - // use auto-convert in constructor - case value_t::boolean : assigner(boolean_ , std::move(other.boolean_ )); break; - case value_t::integer : assigner(integer_ , std::move(other.integer_ )); break; - case value_t::floating : assigner(floating_ , std::move(other.floating_ )); break; - case value_t::string : assigner(string_ , std::move(other.string_ )); break; - case value_t::offset_datetime: assigner(offset_datetime_, std::move(other.offset_datetime_)); break; - case value_t::local_datetime : assigner(local_datetime_ , std::move(other.local_datetime_ )); break; - case value_t::local_date : assigner(local_date_ , std::move(other.local_date_ )); break; - case value_t::local_time : assigner(local_time_ , std::move(other.local_time_ )); break; - - // may have different container type - case value_t::array : - { - array_type tmp( - std::make_move_iterator(other.array_.value.get().begin()), - std::make_move_iterator(other.array_.value.get().end())); - assigner(array_, array_storage( - detail::storage(std::move(tmp)), - other.array_.format - )); - break; - } - case value_t::table : - { - table_type tmp( - std::make_move_iterator(other.table_.value.get().begin()), - std::make_move_iterator(other.table_.value.get().end())); - assigner(table_, table_storage( - detail::storage(std::move(tmp)), - other.table_.format - )); - break; - } - default: break; - } - } - template - basic_value& operator=(basic_value other) - { - this->cleanup(); - this->region_ = other.region_; - this->comments_ = comment_type(other.comments_); - this->type_ = other.type_; - switch(other.type_) - { - // use auto-convert in constructor - case value_t::boolean : assigner(boolean_ , std::move(other.boolean_ )); break; - case value_t::integer : assigner(integer_ , std::move(other.integer_ )); break; - case value_t::floating : assigner(floating_ , std::move(other.floating_ )); break; - case value_t::string : assigner(string_ , std::move(other.string_ )); break; - case value_t::offset_datetime: assigner(offset_datetime_, std::move(other.offset_datetime_)); break; - case value_t::local_datetime : assigner(local_datetime_ , std::move(other.local_datetime_ )); break; - case value_t::local_date : assigner(local_date_ , std::move(other.local_date_ )); break; - case value_t::local_time : assigner(local_time_ , std::move(other.local_time_ )); break; - - // may have different container type - case value_t::array : - { - array_type tmp( - std::make_move_iterator(other.array_.value.get().begin()), - std::make_move_iterator(other.array_.value.get().end())); - assigner(array_, array_storage( - detail::storage(std::move(tmp)), - other.array_.format - )); - break; - } - case value_t::table : - { - table_type tmp( - std::make_move_iterator(other.table_.value.get().begin()), - std::make_move_iterator(other.table_.value.get().end())); - assigner(table_, table_storage( - detail::storage(std::move(tmp)), - other.table_.format - )); - break; - } - default: break; - } - return *this; - } - // }}} - - // constructor (boolean) ============================================== {{{ - - basic_value(boolean_type x) - : basic_value(x, boolean_format_info{}, std::vector{}, region_type{}) - {} - basic_value(boolean_type x, boolean_format_info fmt) - : basic_value(x, fmt, std::vector{}, region_type{}) - {} - basic_value(boolean_type x, std::vector com) - : basic_value(x, boolean_format_info{}, std::move(com), region_type{}) - {} - basic_value(boolean_type x, boolean_format_info fmt, std::vector com) - : basic_value(x, fmt, std::move(com), region_type{}) - {} - basic_value(boolean_type x, boolean_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::boolean), boolean_(boolean_storage(x, fmt)), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(boolean_type x) - { - boolean_format_info fmt; - if(this->is_boolean()) - { - fmt = this->as_boolean_fmt(); - } - this->cleanup(); - this->type_ = value_t::boolean; - this->region_ = region_type{}; - assigner(this->boolean_, boolean_storage(x, fmt)); - return *this; - } - - // }}} - - // constructor (integer) ============================================== {{{ - - basic_value(integer_type x) - : basic_value(std::move(x), integer_format_info{}, std::vector{}, region_type{}) - {} - basic_value(integer_type x, integer_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - basic_value(integer_type x, std::vector com) - : basic_value(std::move(x), integer_format_info{}, std::move(com), region_type{}) - {} - basic_value(integer_type x, integer_format_info fmt, std::vector com) - : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) - {} - basic_value(integer_type x, integer_format_info fmt, std::vector com, region_type reg) - : type_(value_t::integer), integer_(integer_storage(std::move(x), std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(integer_type x) - { - integer_format_info fmt; - if(this->is_integer()) - { - fmt = this->as_integer_fmt(); - } - this->cleanup(); - this->type_ = value_t::integer; - this->region_ = region_type{}; - assigner(this->integer_, integer_storage(std::move(x), std::move(fmt))); - return *this; - } - - private: - - template - using enable_if_integer_like_t = cxx::enable_if_t, boolean_type>>, - cxx::negation, integer_type>>, - std::is_integral> - >::value, std::nullptr_t>; - - public: - - template = nullptr> - basic_value(T x) - : basic_value(std::move(x), integer_format_info{}, std::vector{}, region_type{}) - {} - template = nullptr> - basic_value(T x, integer_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - template = nullptr> - basic_value(T x, std::vector com) - : basic_value(std::move(x), integer_format_info{}, std::move(com), region_type{}) - {} - template = nullptr> - basic_value(T x, integer_format_info fmt, std::vector com) - : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) - {} - template = nullptr> - basic_value(T x, integer_format_info fmt, std::vector com, region_type reg) - : type_(value_t::integer), integer_(integer_storage(std::move(x), std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - template = nullptr> - basic_value& operator=(T x) - { - integer_format_info fmt; - if(this->is_integer()) - { - fmt = this->as_integer_fmt(); - } - this->cleanup(); - this->type_ = value_t::integer; - this->region_ = region_type{}; - assigner(this->integer_, integer_storage(x, std::move(fmt))); - return *this; - } - - // }}} - - // constructor (floating) ============================================= {{{ - - basic_value(floating_type x) - : basic_value(std::move(x), floating_format_info{}, std::vector{}, region_type{}) - {} - basic_value(floating_type x, floating_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - basic_value(floating_type x, std::vector com) - : basic_value(std::move(x), floating_format_info{}, std::move(com), region_type{}) - {} - basic_value(floating_type x, floating_format_info fmt, std::vector com) - : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) - {} - basic_value(floating_type x, floating_format_info fmt, std::vector com, region_type reg) - : type_(value_t::floating), floating_(floating_storage(std::move(x), std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(floating_type x) - { - floating_format_info fmt; - if(this->is_floating()) - { - fmt = this->as_floating_fmt(); - } - this->cleanup(); - this->type_ = value_t::floating; - this->region_ = region_type{}; - assigner(this->floating_, floating_storage(std::move(x), std::move(fmt))); - return *this; - } - - private: - - template - using enable_if_floating_like_t = cxx::enable_if_t, floating_type>>, - std::is_floating_point> - >::value, std::nullptr_t>; - - public: - - template = nullptr> - basic_value(T x) - : basic_value(x, floating_format_info{}, std::vector{}, region_type{}) - {} - - template = nullptr> - basic_value(T x, floating_format_info fmt) - : basic_value(x, std::move(fmt), std::vector{}, region_type{}) - {} - - template = nullptr> - basic_value(T x, std::vector com) - : basic_value(x, floating_format_info{}, std::move(com), region_type{}) - {} - - template = nullptr> - basic_value(T x, floating_format_info fmt, std::vector com) - : basic_value(x, std::move(fmt), std::move(com), region_type{}) - {} - - template = nullptr> - basic_value(T x, floating_format_info fmt, std::vector com, region_type reg) - : type_(value_t::floating), floating_(floating_storage(x, std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - - template = nullptr> - basic_value& operator=(T x) - { - floating_format_info fmt; - if(this->is_floating()) - { - fmt = this->as_floating_fmt(); - } - this->cleanup(); - this->type_ = value_t::floating; - this->region_ = region_type{}; - assigner(this->floating_, floating_storage(x, std::move(fmt))); - return *this; - } - - // }}} - - // constructor (string) =============================================== {{{ - - basic_value(string_type x) - : basic_value(std::move(x), string_format_info{}, std::vector{}, region_type{}) - {} - basic_value(string_type x, string_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - basic_value(string_type x, std::vector com) - : basic_value(std::move(x), string_format_info{}, std::move(com), region_type{}) - {} - basic_value(string_type x, string_format_info fmt, std::vector com) - : basic_value(std::move(x), std::move(fmt), std::move(com), region_type{}) - {} - basic_value(string_type x, string_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::string), string_(string_storage(std::move(x), std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(string_type x) - { - string_format_info fmt; - if(this->is_string()) - { - fmt = this->as_string_fmt(); - } - this->cleanup(); - this->type_ = value_t::string; - this->region_ = region_type{}; - assigner(this->string_, string_storage(x, std::move(fmt))); - return *this; - } - - // "string literal" - - basic_value(const typename string_type::value_type* x) - : basic_value(x, string_format_info{}, std::vector{}, region_type{}) - {} - basic_value(const typename string_type::value_type* x, string_format_info fmt) - : basic_value(x, std::move(fmt), std::vector{}, region_type{}) - {} - basic_value(const typename string_type::value_type* x, std::vector com) - : basic_value(x, string_format_info{}, std::move(com), region_type{}) - {} - basic_value(const typename string_type::value_type* x, string_format_info fmt, std::vector com) - : basic_value(x, std::move(fmt), std::move(com), region_type{}) - {} - basic_value(const typename string_type::value_type* x, string_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::string), string_(string_storage(string_type(x), std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(const typename string_type::value_type* x) - { - string_format_info fmt; - if(this->is_string()) - { - fmt = this->as_string_fmt(); - } - this->cleanup(); - this->type_ = value_t::string; - this->region_ = region_type{}; - assigner(this->string_, string_storage(string_type(x), std::move(fmt))); - return *this; - } - -#if defined(TOML11_HAS_STRING_VIEW) - using string_view_type = std::basic_string_view< - typename string_type::value_type, typename string_type::traits_type>; - - basic_value(string_view_type x) - : basic_value(x, string_format_info{}, std::vector{}, region_type{}) - {} - basic_value(string_view_type x, string_format_info fmt) - : basic_value(x, std::move(fmt), std::vector{}, region_type{}) - {} - basic_value(string_view_type x, std::vector com) - : basic_value(x, string_format_info{}, std::move(com), region_type{}) - {} - basic_value(string_view_type x, string_format_info fmt, std::vector com) - : basic_value(x, std::move(fmt), std::move(com), region_type{}) - {} - basic_value(string_view_type x, string_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::string), string_(string_storage(string_type(x), std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(string_view_type x) - { - string_format_info fmt; - if(this->is_string()) - { - fmt = this->as_string_fmt(); - } - this->cleanup(); - this->type_ = value_t::string; - this->region_ = region_type{}; - assigner(this->string_, string_storage(string_type(x), std::move(fmt))); - return *this; - } - -#endif // TOML11_HAS_STRING_VIEW - - template, string_type>>, - detail::is_1byte_std_basic_string - >::value, std::nullptr_t> = nullptr> - basic_value(const T& x) - : basic_value(x, string_format_info{}, std::vector{}, region_type{}) - {} - template, string_type>>, - detail::is_1byte_std_basic_string - >::value, std::nullptr_t> = nullptr> - basic_value(const T& x, string_format_info fmt) - : basic_value(x, std::move(fmt), std::vector{}, region_type{}) - {} - template, string_type>>, - detail::is_1byte_std_basic_string - >::value, std::nullptr_t> = nullptr> - basic_value(const T& x, std::vector com) - : basic_value(x, string_format_info{}, std::move(com), region_type{}) - {} - template, string_type>>, - detail::is_1byte_std_basic_string - >::value, std::nullptr_t> = nullptr> - basic_value(const T& x, string_format_info fmt, std::vector com) - : basic_value(x, std::move(fmt), std::move(com), region_type{}) - {} - template, string_type>>, - detail::is_1byte_std_basic_string - >::value, std::nullptr_t> = nullptr> - basic_value(const T& x, string_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::string), - string_(string_storage(detail::string_conv(x), std::move(fmt))), - region_(std::move(reg)), comments_(std::move(com)) - {} - template, string_type>>, - detail::is_1byte_std_basic_string - >::value, std::nullptr_t> = nullptr> - basic_value& operator=(const T& x) - { - string_format_info fmt; - if(this->is_string()) - { - fmt = this->as_string_fmt(); - } - this->cleanup(); - this->type_ = value_t::string; - this->region_ = region_type{}; - assigner(this->string_, string_storage(detail::string_conv(x), std::move(fmt))); - return *this; - } - - // }}} - - // constructor (local_date) =========================================== {{{ - - basic_value(local_date_type x) - : basic_value(x, local_date_format_info{}, std::vector{}, region_type{}) - {} - basic_value(local_date_type x, local_date_format_info fmt) - : basic_value(x, fmt, std::vector{}, region_type{}) - {} - basic_value(local_date_type x, std::vector com) - : basic_value(x, local_date_format_info{}, std::move(com), region_type{}) - {} - basic_value(local_date_type x, local_date_format_info fmt, std::vector com) - : basic_value(x, fmt, std::move(com), region_type{}) - {} - basic_value(local_date_type x, local_date_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::local_date), local_date_(local_date_storage(x, fmt)), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(local_date_type x) - { - local_date_format_info fmt; - if(this->is_local_date()) - { - fmt = this->as_local_date_fmt(); - } - this->cleanup(); - this->type_ = value_t::local_date; - this->region_ = region_type{}; - assigner(this->local_date_, local_date_storage(x, fmt)); - return *this; - } - - // }}} - - // constructor (local_time) =========================================== {{{ - - basic_value(local_time_type x) - : basic_value(x, local_time_format_info{}, std::vector{}, region_type{}) - {} - basic_value(local_time_type x, local_time_format_info fmt) - : basic_value(x, fmt, std::vector{}, region_type{}) - {} - basic_value(local_time_type x, std::vector com) - : basic_value(x, local_time_format_info{}, std::move(com), region_type{}) - {} - basic_value(local_time_type x, local_time_format_info fmt, std::vector com) - : basic_value(x, fmt, std::move(com), region_type{}) - {} - basic_value(local_time_type x, local_time_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::local_time), local_time_(local_time_storage(x, fmt)), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(local_time_type x) - { - local_time_format_info fmt; - if(this->is_local_time()) - { - fmt = this->as_local_time_fmt(); - } - this->cleanup(); - this->type_ = value_t::local_time; - this->region_ = region_type{}; - assigner(this->local_time_, local_time_storage(x, fmt)); - return *this; - } - - template - basic_value(const std::chrono::duration& x) - : basic_value(local_time_type(x), local_time_format_info{}, std::vector{}, region_type{}) - {} - template - basic_value(const std::chrono::duration& x, local_time_format_info fmt) - : basic_value(local_time_type(x), std::move(fmt), std::vector{}, region_type{}) - {} - template - basic_value(const std::chrono::duration& x, std::vector com) - : basic_value(local_time_type(x), local_time_format_info{}, std::move(com), region_type{}) - {} - template - basic_value(const std::chrono::duration& x, local_time_format_info fmt, std::vector com) - : basic_value(local_time_type(x), std::move(fmt), std::move(com), region_type{}) - {} - template - basic_value(const std::chrono::duration& x, - local_time_format_info fmt, - std::vector com, region_type reg) - : basic_value(local_time_type(x), std::move(fmt), std::move(com), std::move(reg)) - {} - template - basic_value& operator=(const std::chrono::duration& x) - { - local_time_format_info fmt; - if(this->is_local_time()) - { - fmt = this->as_local_time_fmt(); - } - this->cleanup(); - this->type_ = value_t::local_time; - this->region_ = region_type{}; - assigner(this->local_time_, local_time_storage(local_time_type(x), std::move(fmt))); - return *this; - } - - // }}} - - // constructor (local_datetime) =========================================== {{{ - - basic_value(local_datetime_type x) - : basic_value(x, local_datetime_format_info{}, std::vector{}, region_type{}) - {} - basic_value(local_datetime_type x, local_datetime_format_info fmt) - : basic_value(x, fmt, std::vector{}, region_type{}) - {} - basic_value(local_datetime_type x, std::vector com) - : basic_value(x, local_datetime_format_info{}, std::move(com), region_type{}) - {} - basic_value(local_datetime_type x, local_datetime_format_info fmt, std::vector com) - : basic_value(x, fmt, std::move(com), region_type{}) - {} - basic_value(local_datetime_type x, local_datetime_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::local_datetime), local_datetime_(local_datetime_storage(x, fmt)), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(local_datetime_type x) - { - local_datetime_format_info fmt; - if(this->is_local_datetime()) - { - fmt = this->as_local_datetime_fmt(); - } - this->cleanup(); - this->type_ = value_t::local_datetime; - this->region_ = region_type{}; - assigner(this->local_datetime_, local_datetime_storage(x, fmt)); - return *this; - } - - // }}} - - // constructor (offset_datetime) =========================================== {{{ - - basic_value(offset_datetime_type x) - : basic_value(x, offset_datetime_format_info{}, std::vector{}, region_type{}) - {} - basic_value(offset_datetime_type x, offset_datetime_format_info fmt) - : basic_value(x, fmt, std::vector{}, region_type{}) - {} - basic_value(offset_datetime_type x, std::vector com) - : basic_value(x, offset_datetime_format_info{}, std::move(com), region_type{}) - {} - basic_value(offset_datetime_type x, offset_datetime_format_info fmt, std::vector com) - : basic_value(x, fmt, std::move(com), region_type{}) - {} - basic_value(offset_datetime_type x, offset_datetime_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::offset_datetime), offset_datetime_(offset_datetime_storage(x, fmt)), - region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(offset_datetime_type x) - { - offset_datetime_format_info fmt; - if(this->is_offset_datetime()) - { - fmt = this->as_offset_datetime_fmt(); - } - this->cleanup(); - this->type_ = value_t::offset_datetime; - this->region_ = region_type{}; - assigner(this->offset_datetime_, offset_datetime_storage(x, fmt)); - return *this; - } - - // system_clock::time_point - - basic_value(std::chrono::system_clock::time_point x) - : basic_value(offset_datetime_type(x), offset_datetime_format_info{}, std::vector{}, region_type{}) - {} - basic_value(std::chrono::system_clock::time_point x, offset_datetime_format_info fmt) - : basic_value(offset_datetime_type(x), fmt, std::vector{}, region_type{}) - {} - basic_value(std::chrono::system_clock::time_point x, std::vector com) - : basic_value(offset_datetime_type(x), offset_datetime_format_info{}, std::move(com), region_type{}) - {} - basic_value(std::chrono::system_clock::time_point x, offset_datetime_format_info fmt, std::vector com) - : basic_value(offset_datetime_type(x), fmt, std::move(com), region_type{}) - {} - basic_value(std::chrono::system_clock::time_point x, offset_datetime_format_info fmt, - std::vector com, region_type reg) - : basic_value(offset_datetime_type(x), std::move(fmt), std::move(com), std::move(reg)) - {} - basic_value& operator=(std::chrono::system_clock::time_point x) - { - offset_datetime_format_info fmt; - if(this->is_offset_datetime()) - { - fmt = this->as_offset_datetime_fmt(); - } - this->cleanup(); - this->type_ = value_t::offset_datetime; - this->region_ = region_type{}; - assigner(this->offset_datetime_, offset_datetime_storage(offset_datetime_type(x), fmt)); - return *this; - } - - // }}} - - // constructor (array) ================================================ {{{ - - basic_value(array_type x) - : basic_value(std::move(x), array_format_info{}, std::vector{}, region_type{}) - {} - basic_value(array_type x, array_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - basic_value(array_type x, std::vector com) - : basic_value(std::move(x), array_format_info{}, std::move(com), region_type{}) - {} - basic_value(array_type x, array_format_info fmt, std::vector com) - : basic_value(std::move(x), fmt, std::move(com), region_type{}) - {} - basic_value(array_type x, array_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::array), array_(array_storage( - detail::storage(std::move(x)), std::move(fmt) - )), region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(array_type x) - { - array_format_info fmt; - if(this->is_array()) - { - fmt = this->as_array_fmt(); - } - this->cleanup(); - this->type_ = value_t::array; - this->region_ = region_type{}; - assigner(this->array_, array_storage( - detail::storage(std::move(x)), std::move(fmt))); - return *this; - } - - private: - - template - using enable_if_array_like_t = cxx::enable_if_t, - cxx::negation>, - cxx::negation>, -#if defined(TOML11_HAS_STRING_VIEW) - cxx::negation>, -#endif - cxx::negation>, - cxx::negation> - >::value, std::nullptr_t>; - - public: - - template = nullptr> - basic_value(T x) - : basic_value(std::move(x), array_format_info{}, std::vector{}, region_type{}) - {} - template = nullptr> - basic_value(T x, array_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - template = nullptr> - basic_value(T x, std::vector com) - : basic_value(std::move(x), array_format_info{}, std::move(com), region_type{}) - {} - template = nullptr> - basic_value(T x, array_format_info fmt, std::vector com) - : basic_value(std::move(x), fmt, std::move(com), region_type{}) - {} - template = nullptr> - basic_value(T x, array_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::array), array_(array_storage( - detail::storage(array_type( - std::make_move_iterator(x.begin()), - std::make_move_iterator(x.end())) - ), std::move(fmt) - )), region_(std::move(reg)), comments_(std::move(com)) - {} - template = nullptr> - basic_value& operator=(T x) - { - array_format_info fmt; - if(this->is_array()) - { - fmt = this->as_array_fmt(); - } - this->cleanup(); - this->type_ = value_t::array; - this->region_ = region_type{}; - - array_type a(std::make_move_iterator(x.begin()), - std::make_move_iterator(x.end())); - assigner(this->array_, array_storage( - detail::storage(std::move(a)), std::move(fmt))); - return *this; - } - - // }}} - - // constructor (table) ================================================ {{{ - - basic_value(table_type x) - : basic_value(std::move(x), table_format_info{}, std::vector{}, region_type{}) - {} - basic_value(table_type x, table_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - basic_value(table_type x, std::vector com) - : basic_value(std::move(x), table_format_info{}, std::move(com), region_type{}) - {} - basic_value(table_type x, table_format_info fmt, std::vector com) - : basic_value(std::move(x), fmt, std::move(com), region_type{}) - {} - basic_value(table_type x, table_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::table), table_(table_storage( - detail::storage(std::move(x)), std::move(fmt) - )), region_(std::move(reg)), comments_(std::move(com)) - {} - basic_value& operator=(table_type x) - { - table_format_info fmt; - if(this->is_table()) - { - fmt = this->as_table_fmt(); - } - this->cleanup(); - this->type_ = value_t::table; - this->region_ = region_type{}; - assigner(this->table_, table_storage( - detail::storage(std::move(x)), std::move(fmt))); - return *this; - } - - // table-like - - private: - - template - using enable_if_table_like_t = cxx::enable_if_t>, - detail::is_map, - cxx::negation>, - cxx::negation> - >::value, std::nullptr_t>; - - public: - - template = nullptr> - basic_value(T x) - : basic_value(std::move(x), table_format_info{}, std::vector{}, region_type{}) - {} - template = nullptr> - basic_value(T x, table_format_info fmt) - : basic_value(std::move(x), std::move(fmt), std::vector{}, region_type{}) - {} - template = nullptr> - basic_value(T x, std::vector com) - : basic_value(std::move(x), table_format_info{}, std::move(com), region_type{}) - {} - template = nullptr> - basic_value(T x, table_format_info fmt, std::vector com) - : basic_value(std::move(x), fmt, std::move(com), region_type{}) - {} - template = nullptr> - basic_value(T x, table_format_info fmt, - std::vector com, region_type reg) - : type_(value_t::table), table_(table_storage( - detail::storage(table_type( - std::make_move_iterator(x.begin()), - std::make_move_iterator(x.end()) - )), std::move(fmt) - )), region_(std::move(reg)), comments_(std::move(com)) - {} - template = nullptr> - basic_value& operator=(T x) - { - table_format_info fmt; - if(this->is_table()) - { - fmt = this->as_table_fmt(); - } - this->cleanup(); - this->type_ = value_t::table; - this->region_ = region_type{}; - - table_type t(std::make_move_iterator(x.begin()), - std::make_move_iterator(x.end())); - assigner(this->table_, table_storage( - detail::storage(std::move(t)), std::move(fmt))); - return *this; - } - - // }}} - - // constructor (user_defined) ========================================= {{{ - - template::value, std::nullptr_t> = nullptr> - basic_value(const T& ud) - : basic_value( - into>::template into_toml(ud)) - {} - template::value, std::nullptr_t> = nullptr> - basic_value(const T& ud, std::vector com) - : basic_value( - into>::template into_toml(ud), - std::move(com)) - {} - template::value, std::nullptr_t> = nullptr> - basic_value& operator=(const T& ud) - { - *this = into>::template into_toml(ud); - return *this; - } - - template, - cxx::negation> - >::value, std::nullptr_t> = nullptr> - basic_value(const T& ud): basic_value(ud.into_toml()) {} - - template, - cxx::negation> - >::value, std::nullptr_t> = nullptr> - basic_value(const T& ud, std::vector com) - : basic_value(ud.into_toml(), std::move(com)) - {} - template, - cxx::negation> - >::value, std::nullptr_t> = nullptr> - basic_value& operator=(const T& ud) - { - *this = ud.into_toml(); - return *this; - } - - template, - cxx::negation> - >::value, std::nullptr_t> = nullptr> - basic_value(const T& ud): basic_value(ud.template into_toml()) {} - - template, - cxx::negation> - >::value, std::nullptr_t> = nullptr> - basic_value(const T& ud, std::vector com) - : basic_value(ud.template into_toml(), std::move(com)) - {} - template, - cxx::negation> - >::value, std::nullptr_t> = nullptr> - basic_value& operator=(const T& ud) - { - *this = ud.template into_toml(); - return *this; - } - // }}} - - // empty value with region info ======================================= {{{ - - // mainly for `null` extension - basic_value(detail::none_t, region_type reg) noexcept - : type_(value_t::empty), empty_('\0'), region_(std::move(reg)), comments_{} - {} - - // }}} - - // type checking ====================================================== {{{ - - template, value_type>::value, - std::nullptr_t> = nullptr> - bool is() const noexcept - { - return detail::type_to_enum::value == this->type_; - } - bool is(value_t t) const noexcept {return t == this->type_;} - - bool is_empty() const noexcept {return this->is(value_t::empty );} - bool is_boolean() const noexcept {return this->is(value_t::boolean );} - bool is_integer() const noexcept {return this->is(value_t::integer );} - bool is_floating() const noexcept {return this->is(value_t::floating );} - bool is_string() const noexcept {return this->is(value_t::string );} - bool is_offset_datetime() const noexcept {return this->is(value_t::offset_datetime);} - bool is_local_datetime() const noexcept {return this->is(value_t::local_datetime );} - bool is_local_date() const noexcept {return this->is(value_t::local_date );} - bool is_local_time() const noexcept {return this->is(value_t::local_time );} - bool is_array() const noexcept {return this->is(value_t::array );} - bool is_table() const noexcept {return this->is(value_t::table );} - - bool is_array_of_tables() const noexcept - { - if( ! this->is_array()) {return false;} - const auto& a = this->as_array(std::nothrow); // already checked. - - // when you define [[array.of.tables]], at least one empty table will be - // assigned. In case of array of inline tables, `array_of_tables = []`, - // there is no reason to consider this as an array of *tables*. - // So empty array is not an array-of-tables. - if(a.empty()) {return false;} - - // since toml v1.0.0 allows array of heterogeneous types, we need to - // check all the elements. if any of the elements is not a table, it - // is a heterogeneous array and cannot be expressed by `[[aot]]` form. - for(const auto& e : a) - { - if( ! e.is_table()) {return false;} - } - return true; - } - - value_t type() const noexcept {return type_;} - - // }}} - - // as_xxx (noexcept) version ========================================== {{{ - - template - detail::enum_to_type_t> const& - as(const std::nothrow_t&) const noexcept - { - return detail::getter::get_nothrow(*this); - } - template - detail::enum_to_type_t>& - as(const std::nothrow_t&) noexcept - { - return detail::getter::get_nothrow(*this); - } - - boolean_type const& as_boolean (const std::nothrow_t&) const noexcept {return this->boolean_.value;} - integer_type const& as_integer (const std::nothrow_t&) const noexcept {return this->integer_.value;} - floating_type const& as_floating (const std::nothrow_t&) const noexcept {return this->floating_.value;} - string_type const& as_string (const std::nothrow_t&) const noexcept {return this->string_.value;} - offset_datetime_type const& as_offset_datetime(const std::nothrow_t&) const noexcept {return this->offset_datetime_.value;} - local_datetime_type const& as_local_datetime (const std::nothrow_t&) const noexcept {return this->local_datetime_.value;} - local_date_type const& as_local_date (const std::nothrow_t&) const noexcept {return this->local_date_.value;} - local_time_type const& as_local_time (const std::nothrow_t&) const noexcept {return this->local_time_.value;} - array_type const& as_array (const std::nothrow_t&) const noexcept {return this->array_.value.get();} - table_type const& as_table (const std::nothrow_t&) const noexcept {return this->table_.value.get();} - - boolean_type & as_boolean (const std::nothrow_t&) noexcept {return this->boolean_.value;} - integer_type & as_integer (const std::nothrow_t&) noexcept {return this->integer_.value;} - floating_type & as_floating (const std::nothrow_t&) noexcept {return this->floating_.value;} - string_type & as_string (const std::nothrow_t&) noexcept {return this->string_.value;} - offset_datetime_type& as_offset_datetime(const std::nothrow_t&) noexcept {return this->offset_datetime_.value;} - local_datetime_type & as_local_datetime (const std::nothrow_t&) noexcept {return this->local_datetime_.value;} - local_date_type & as_local_date (const std::nothrow_t&) noexcept {return this->local_date_.value;} - local_time_type & as_local_time (const std::nothrow_t&) noexcept {return this->local_time_.value;} - array_type & as_array (const std::nothrow_t&) noexcept {return this->array_.value.get();} - table_type & as_table (const std::nothrow_t&) noexcept {return this->table_.value.get();} - - // }}} - - // as_xxx (throw) ===================================================== {{{ - - template - detail::enum_to_type_t> const& as() const - { - return detail::getter::get(*this); - } - template - detail::enum_to_type_t>& as() - { - return detail::getter::get(*this); - } - - boolean_type const& as_boolean() const - { - if(this->type_ != value_t::boolean) - { - this->throw_bad_cast("toml::value::as_boolean()", value_t::boolean); - } - return this->boolean_.value; - } - integer_type const& as_integer() const - { - if(this->type_ != value_t::integer) - { - this->throw_bad_cast("toml::value::as_integer()", value_t::integer); - } - return this->integer_.value; - } - floating_type const& as_floating() const - { - if(this->type_ != value_t::floating) - { - this->throw_bad_cast("toml::value::as_floating()", value_t::floating); - } - return this->floating_.value; - } - string_type const& as_string() const - { - if(this->type_ != value_t::string) - { - this->throw_bad_cast("toml::value::as_string()", value_t::string); - } - return this->string_.value; - } - offset_datetime_type const& as_offset_datetime() const - { - if(this->type_ != value_t::offset_datetime) - { - this->throw_bad_cast("toml::value::as_offset_datetime()", value_t::offset_datetime); - } - return this->offset_datetime_.value; - } - local_datetime_type const& as_local_datetime() const - { - if(this->type_ != value_t::local_datetime) - { - this->throw_bad_cast("toml::value::as_local_datetime()", value_t::local_datetime); - } - return this->local_datetime_.value; - } - local_date_type const& as_local_date() const - { - if(this->type_ != value_t::local_date) - { - this->throw_bad_cast("toml::value::as_local_date()", value_t::local_date); - } - return this->local_date_.value; - } - local_time_type const& as_local_time() const - { - if(this->type_ != value_t::local_time) - { - this->throw_bad_cast("toml::value::as_local_time()", value_t::local_time); - } - return this->local_time_.value; - } - array_type const& as_array() const - { - if(this->type_ != value_t::array) - { - this->throw_bad_cast("toml::value::as_array()", value_t::array); - } - return this->array_.value.get(); - } - table_type const& as_table() const - { - if(this->type_ != value_t::table) - { - this->throw_bad_cast("toml::value::as_table()", value_t::table); - } - return this->table_.value.get(); - } - - // ------------------------------------------------------------------------ - // nonconst reference - - boolean_type& as_boolean() - { - if(this->type_ != value_t::boolean) - { - this->throw_bad_cast("toml::value::as_boolean()", value_t::boolean); - } - return this->boolean_.value; - } - integer_type& as_integer() - { - if(this->type_ != value_t::integer) - { - this->throw_bad_cast("toml::value::as_integer()", value_t::integer); - } - return this->integer_.value; - } - floating_type& as_floating() - { - if(this->type_ != value_t::floating) - { - this->throw_bad_cast("toml::value::as_floating()", value_t::floating); - } - return this->floating_.value; - } - string_type& as_string() - { - if(this->type_ != value_t::string) - { - this->throw_bad_cast("toml::value::as_string()", value_t::string); - } - return this->string_.value; - } - offset_datetime_type& as_offset_datetime() - { - if(this->type_ != value_t::offset_datetime) - { - this->throw_bad_cast("toml::value::as_offset_datetime()", value_t::offset_datetime); - } - return this->offset_datetime_.value; - } - local_datetime_type& as_local_datetime() - { - if(this->type_ != value_t::local_datetime) - { - this->throw_bad_cast("toml::value::as_local_datetime()", value_t::local_datetime); - } - return this->local_datetime_.value; - } - local_date_type& as_local_date() - { - if(this->type_ != value_t::local_date) - { - this->throw_bad_cast("toml::value::as_local_date()", value_t::local_date); - } - return this->local_date_.value; - } - local_time_type& as_local_time() - { - if(this->type_ != value_t::local_time) - { - this->throw_bad_cast("toml::value::as_local_time()", value_t::local_time); - } - return this->local_time_.value; - } - array_type& as_array() - { - if(this->type_ != value_t::array) - { - this->throw_bad_cast("toml::value::as_array()", value_t::array); - } - return this->array_.value.get(); - } - table_type& as_table() - { - if(this->type_ != value_t::table) - { - this->throw_bad_cast("toml::value::as_table()", value_t::table); - } - return this->table_.value.get(); - } - - // }}} - - // format accessors (noexcept) ======================================== {{{ - - template - detail::enum_to_fmt_type_t const& - as_fmt(const std::nothrow_t&) const noexcept - { - return detail::getter::get_fmt_nothrow(*this); - } - template - detail::enum_to_fmt_type_t& - as_fmt(const std::nothrow_t&) noexcept - { - return detail::getter::get_fmt_nothrow(*this); - } - - boolean_format_info & as_boolean_fmt (const std::nothrow_t&) noexcept {return this->boolean_.format;} - integer_format_info & as_integer_fmt (const std::nothrow_t&) noexcept {return this->integer_.format;} - floating_format_info & as_floating_fmt (const std::nothrow_t&) noexcept {return this->floating_.format;} - string_format_info & as_string_fmt (const std::nothrow_t&) noexcept {return this->string_.format;} - offset_datetime_format_info& as_offset_datetime_fmt(const std::nothrow_t&) noexcept {return this->offset_datetime_.format;} - local_datetime_format_info & as_local_datetime_fmt (const std::nothrow_t&) noexcept {return this->local_datetime_.format;} - local_date_format_info & as_local_date_fmt (const std::nothrow_t&) noexcept {return this->local_date_.format;} - local_time_format_info & as_local_time_fmt (const std::nothrow_t&) noexcept {return this->local_time_.format;} - array_format_info & as_array_fmt (const std::nothrow_t&) noexcept {return this->array_.format;} - table_format_info & as_table_fmt (const std::nothrow_t&) noexcept {return this->table_.format;} - - boolean_format_info const& as_boolean_fmt (const std::nothrow_t&) const noexcept {return this->boolean_.format;} - integer_format_info const& as_integer_fmt (const std::nothrow_t&) const noexcept {return this->integer_.format;} - floating_format_info const& as_floating_fmt (const std::nothrow_t&) const noexcept {return this->floating_.format;} - string_format_info const& as_string_fmt (const std::nothrow_t&) const noexcept {return this->string_.format;} - offset_datetime_format_info const& as_offset_datetime_fmt(const std::nothrow_t&) const noexcept {return this->offset_datetime_.format;} - local_datetime_format_info const& as_local_datetime_fmt (const std::nothrow_t&) const noexcept {return this->local_datetime_.format;} - local_date_format_info const& as_local_date_fmt (const std::nothrow_t&) const noexcept {return this->local_date_.format;} - local_time_format_info const& as_local_time_fmt (const std::nothrow_t&) const noexcept {return this->local_time_.format;} - array_format_info const& as_array_fmt (const std::nothrow_t&) const noexcept {return this->array_.format;} - table_format_info const& as_table_fmt (const std::nothrow_t&) const noexcept {return this->table_.format;} - - // }}} - - // format accessors (throw) =========================================== {{{ - - template - detail::enum_to_fmt_type_t const& as_fmt() const - { - return detail::getter::get_fmt(*this); - } - template - detail::enum_to_fmt_type_t& as_fmt() - { - return detail::getter::get_fmt(*this); - } - - boolean_format_info const& as_boolean_fmt() const - { - if(this->type_ != value_t::boolean) - { - this->throw_bad_cast("toml::value::as_boolean_fmt()", value_t::boolean); - } - return this->boolean_.format; - } - integer_format_info const& as_integer_fmt() const - { - if(this->type_ != value_t::integer) - { - this->throw_bad_cast("toml::value::as_integer_fmt()", value_t::integer); - } - return this->integer_.format; - } - floating_format_info const& as_floating_fmt() const - { - if(this->type_ != value_t::floating) - { - this->throw_bad_cast("toml::value::as_floating_fmt()", value_t::floating); - } - return this->floating_.format; - } - string_format_info const& as_string_fmt() const - { - if(this->type_ != value_t::string) - { - this->throw_bad_cast("toml::value::as_string_fmt()", value_t::string); - } - return this->string_.format; - } - offset_datetime_format_info const& as_offset_datetime_fmt() const - { - if(this->type_ != value_t::offset_datetime) - { - this->throw_bad_cast("toml::value::as_offset_datetime_fmt()", value_t::offset_datetime); - } - return this->offset_datetime_.format; - } - local_datetime_format_info const& as_local_datetime_fmt() const - { - if(this->type_ != value_t::local_datetime) - { - this->throw_bad_cast("toml::value::as_local_datetime_fmt()", value_t::local_datetime); - } - return this->local_datetime_.format; - } - local_date_format_info const& as_local_date_fmt() const - { - if(this->type_ != value_t::local_date) - { - this->throw_bad_cast("toml::value::as_local_date_fmt()", value_t::local_date); - } - return this->local_date_.format; - } - local_time_format_info const& as_local_time_fmt() const - { - if(this->type_ != value_t::local_time) - { - this->throw_bad_cast("toml::value::as_local_time_fmt()", value_t::local_time); - } - return this->local_time_.format; - } - array_format_info const& as_array_fmt() const - { - if(this->type_ != value_t::array) - { - this->throw_bad_cast("toml::value::as_array_fmt()", value_t::array); - } - return this->array_.format; - } - table_format_info const& as_table_fmt() const - { - if(this->type_ != value_t::table) - { - this->throw_bad_cast("toml::value::as_table_fmt()", value_t::table); - } - return this->table_.format; - } - - // ------------------------------------------------------------------------ - // nonconst reference - - boolean_format_info& as_boolean_fmt() - { - if(this->type_ != value_t::boolean) - { - this->throw_bad_cast("toml::value::as_boolean_fmt()", value_t::boolean); - } - return this->boolean_.format; - } - integer_format_info& as_integer_fmt() - { - if(this->type_ != value_t::integer) - { - this->throw_bad_cast("toml::value::as_integer_fmt()", value_t::integer); - } - return this->integer_.format; - } - floating_format_info& as_floating_fmt() - { - if(this->type_ != value_t::floating) - { - this->throw_bad_cast("toml::value::as_floating_fmt()", value_t::floating); - } - return this->floating_.format; - } - string_format_info& as_string_fmt() - { - if(this->type_ != value_t::string) - { - this->throw_bad_cast("toml::value::as_string_fmt()", value_t::string); - } - return this->string_.format; - } - offset_datetime_format_info& as_offset_datetime_fmt() - { - if(this->type_ != value_t::offset_datetime) - { - this->throw_bad_cast("toml::value::as_offset_datetime_fmt()", value_t::offset_datetime); - } - return this->offset_datetime_.format; - } - local_datetime_format_info& as_local_datetime_fmt() - { - if(this->type_ != value_t::local_datetime) - { - this->throw_bad_cast("toml::value::as_local_datetime_fmt()", value_t::local_datetime); - } - return this->local_datetime_.format; - } - local_date_format_info& as_local_date_fmt() - { - if(this->type_ != value_t::local_date) - { - this->throw_bad_cast("toml::value::as_local_date_fmt()", value_t::local_date); - } - return this->local_date_.format; - } - local_time_format_info& as_local_time_fmt() - { - if(this->type_ != value_t::local_time) - { - this->throw_bad_cast("toml::value::as_local_time_fmt()", value_t::local_time); - } - return this->local_time_.format; - } - array_format_info& as_array_fmt() - { - if(this->type_ != value_t::array) - { - this->throw_bad_cast("toml::value::as_array_fmt()", value_t::array); - } - return this->array_.format; - } - table_format_info& as_table_fmt() - { - if(this->type_ != value_t::table) - { - this->throw_bad_cast("toml::value::as_table_fmt()", value_t::table); - } - return this->table_.format; - } - // }}} - - // table accessors ==================================================== {{{ - - value_type& at(const key_type& k) - { - if(!this->is_table()) - { - this->throw_bad_cast("toml::value::at(key_type)", value_t::table); - } - auto& table = this->as_table(std::nothrow); - const auto found = table.find(k); - if(found == table.end()) - { - this->throw_key_not_found_error("toml::value::at", k); - } - assert(found->first == k); - return found->second; - } - value_type const& at(const key_type& k) const - { - if(!this->is_table()) - { - this->throw_bad_cast("toml::value::at(key_type)", value_t::table); - } - const auto& table = this->as_table(std::nothrow); - const auto found = table.find(k); - if(found == table.end()) - { - this->throw_key_not_found_error("toml::value::at", k); - } - assert(found->first == k); - return found->second; - } - value_type& operator[](const key_type& k) - { - if(this->is_empty()) - { - (*this) = table_type{}; - } - else if( ! this->is_table()) // initialized, but not a table - { - this->throw_bad_cast("toml::value::operator[](key_type)", value_t::table); - } - return (this->as_table(std::nothrow))[k]; - } - std::size_t count(const key_type& k) const - { - if(!this->is_table()) - { - this->throw_bad_cast("toml::value::count(key_type)", value_t::table); - } - return this->as_table(std::nothrow).count(k); - } - bool contains(const key_type& k) const - { - if(!this->is_table()) - { - this->throw_bad_cast("toml::value::contains(key_type)", value_t::table); - } - const auto& table = this->as_table(std::nothrow); - return table.find(k) != table.end(); - } - // }}} - - // array accessors ==================================================== {{{ - - value_type& at(const std::size_t idx) - { - if(!this->is_array()) - { - this->throw_bad_cast("toml::value::at(idx)", value_t::array); - } - auto& ar = this->as_array(std::nothrow); - - if(ar.size() <= idx) - { - std::ostringstream oss; - oss << "actual length (" << ar.size() - << ") is shorter than the specified index (" << idx << ")."; - throw std::out_of_range(format_error( - "toml::value::at(idx): no element corresponding to the index", - this->location(), oss.str() - )); - } - return ar.at(idx); - } - value_type const& at(const std::size_t idx) const - { - if(!this->is_array()) - { - this->throw_bad_cast("toml::value::at(idx)", value_t::array); - } - const auto& ar = this->as_array(std::nothrow); - - if(ar.size() <= idx) - { - std::ostringstream oss; - oss << "actual length (" << ar.size() - << ") is shorter than the specified index (" << idx << ")."; - - throw std::out_of_range(format_error( - "toml::value::at(idx): no element corresponding to the index", - this->location(), oss.str() - )); - } - return ar.at(idx); - } - - value_type& operator[](const std::size_t idx) noexcept - { - // no check... - return this->as_array(std::nothrow)[idx]; - } - value_type const& operator[](const std::size_t idx) const noexcept - { - // no check... - return this->as_array(std::nothrow)[idx]; - } - - void push_back(const value_type& x) - { - if(!this->is_array()) - { - this->throw_bad_cast("toml::value::push_back(idx)", value_t::array); - } - this->as_array(std::nothrow).push_back(x); - return; - } - void push_back(value_type&& x) - { - if(!this->is_array()) - { - this->throw_bad_cast("toml::value::push_back(idx)", value_t::array); - } - this->as_array(std::nothrow).push_back(std::move(x)); - return; - } - - template - value_type& emplace_back(Ts&& ... args) - { - if(!this->is_array()) - { - this->throw_bad_cast("toml::value::emplace_back(idx)", value_t::array); - } - auto& ar = this->as_array(std::nothrow); - ar.emplace_back(std::forward(args) ...); - return ar.back(); - } - - std::size_t size() const - { - switch(this->type_) - { - case value_t::array: - { - return this->as_array(std::nothrow).size(); - } - case value_t::table: - { - return this->as_table(std::nothrow).size(); - } - case value_t::string: - { - return this->as_string(std::nothrow).size(); - } - default: - { - throw type_error(format_error( - "toml::value::size(): bad_cast to container types", - this->location(), - "the actual type is " + to_string(this->type_) - ), this->location()); - } - } - } - - // }}} - - source_location location() const - { - return source_location(this->region_); - } - - comment_type const& comments() const noexcept {return this->comments_;} - comment_type& comments() noexcept {return this->comments_;} - - private: - - // private helper functions =========================================== {{{ - - void cleanup() noexcept - { - switch(this->type_) - { - case value_t::boolean : { boolean_ .~boolean_storage (); break; } - case value_t::integer : { integer_ .~integer_storage (); break; } - case value_t::floating : { floating_ .~floating_storage (); break; } - case value_t::string : { string_ .~string_storage (); break; } - case value_t::offset_datetime : { offset_datetime_.~offset_datetime_storage (); break; } - case value_t::local_datetime : { local_datetime_ .~local_datetime_storage (); break; } - case value_t::local_date : { local_date_ .~local_date_storage (); break; } - case value_t::local_time : { local_time_ .~local_time_storage (); break; } - case value_t::array : { array_ .~array_storage (); break; } - case value_t::table : { table_ .~table_storage (); break; } - default : { break; } - } - this->type_ = value_t::empty; - return; - } - - template - static void assigner(T& dst, U&& v) - { - const auto tmp = ::new(std::addressof(dst)) T(std::forward(v)); - assert(tmp == std::addressof(dst)); - (void)tmp; - } - - [[noreturn]] - void throw_bad_cast(const std::string& funcname, const value_t ty) const - { - throw type_error(format_error(detail::make_type_error(*this, funcname, ty)), - this->location()); - } - - [[noreturn]] - void throw_key_not_found_error(const std::string& funcname, const key_type& key) const - { - throw std::out_of_range(format_error( - detail::make_not_found_error(*this, funcname, key))); - } - - template - friend void detail::change_region_of_value(basic_value&, const basic_value&); - - template - friend class basic_value; - - // }}} - - private: - - using boolean_storage = detail::value_with_format; - using integer_storage = detail::value_with_format; - using floating_storage = detail::value_with_format; - using string_storage = detail::value_with_format; - using offset_datetime_storage = detail::value_with_format; - using local_datetime_storage = detail::value_with_format; - using local_date_storage = detail::value_with_format; - using local_time_storage = detail::value_with_format; - using array_storage = detail::value_with_format, array_format_info >; - using table_storage = detail::value_with_format, table_format_info >; - - private: - - value_t type_; - union - { - char empty_; // the smallest type - boolean_storage boolean_; - integer_storage integer_; - floating_storage floating_; - string_storage string_; - offset_datetime_storage offset_datetime_; - local_datetime_storage local_datetime_; - local_date_storage local_date_; - local_time_storage local_time_; - array_storage array_; - table_storage table_; - }; - region_type region_; - comment_type comments_; -}; - -template -bool operator==(const basic_value& lhs, const basic_value& rhs) -{ - if(lhs.type() != rhs.type()) {return false;} - if(lhs.comments() != rhs.comments()) {return false;} - - switch(lhs.type()) - { - case value_t::boolean : - { - return lhs.as_boolean() == rhs.as_boolean(); - } - case value_t::integer : - { - return lhs.as_integer() == rhs.as_integer(); - } - case value_t::floating : - { - return lhs.as_floating() == rhs.as_floating(); - } - case value_t::string : - { - return lhs.as_string() == rhs.as_string(); - } - case value_t::offset_datetime: - { - return lhs.as_offset_datetime() == rhs.as_offset_datetime(); - } - case value_t::local_datetime: - { - return lhs.as_local_datetime() == rhs.as_local_datetime(); - } - case value_t::local_date: - { - return lhs.as_local_date() == rhs.as_local_date(); - } - case value_t::local_time: - { - return lhs.as_local_time() == rhs.as_local_time(); - } - case value_t::array : - { - return lhs.as_array() == rhs.as_array(); - } - case value_t::table : - { - return lhs.as_table() == rhs.as_table(); - } - case value_t::empty : {return true; } - default: {return false;} - } -} - -template -bool operator!=(const basic_value& lhs, const basic_value& rhs) -{ - return !(lhs == rhs); -} - -template -cxx::enable_if_t::array_type>, - detail::is_comparable::table_type> - >::value, bool> -operator<(const basic_value& lhs, const basic_value& rhs) -{ - if(lhs.type() != rhs.type()) - { - return (lhs.type() < rhs.type()); - } - switch(lhs.type()) - { - case value_t::boolean : - { - return lhs.as_boolean() < rhs.as_boolean() || - (lhs.as_boolean() == rhs.as_boolean() && - lhs.comments() < rhs.comments()); - } - case value_t::integer : - { - return lhs.as_integer() < rhs.as_integer() || - (lhs.as_integer() == rhs.as_integer() && - lhs.comments() < rhs.comments()); - } - case value_t::floating : - { - return lhs.as_floating() < rhs.as_floating() || - (lhs.as_floating() == rhs.as_floating() && - lhs.comments() < rhs.comments()); - } - case value_t::string : - { - return lhs.as_string() < rhs.as_string() || - (lhs.as_string() == rhs.as_string() && - lhs.comments() < rhs.comments()); - } - case value_t::offset_datetime: - { - return lhs.as_offset_datetime() < rhs.as_offset_datetime() || - (lhs.as_offset_datetime() == rhs.as_offset_datetime() && - lhs.comments() < rhs.comments()); - } - case value_t::local_datetime: - { - return lhs.as_local_datetime() < rhs.as_local_datetime() || - (lhs.as_local_datetime() == rhs.as_local_datetime() && - lhs.comments() < rhs.comments()); - } - case value_t::local_date: - { - return lhs.as_local_date() < rhs.as_local_date() || - (lhs.as_local_date() == rhs.as_local_date() && - lhs.comments() < rhs.comments()); - } - case value_t::local_time: - { - return lhs.as_local_time() < rhs.as_local_time() || - (lhs.as_local_time() == rhs.as_local_time() && - lhs.comments() < rhs.comments()); - } - case value_t::array : - { - return lhs.as_array() < rhs.as_array() || - (lhs.as_array() == rhs.as_array() && - lhs.comments() < rhs.comments()); - } - case value_t::table : - { - return lhs.as_table() < rhs.as_table() || - (lhs.as_table() == rhs.as_table() && - lhs.comments() < rhs.comments()); - } - case value_t::empty : - { - return lhs.comments() < rhs.comments(); - } - default: - { - return lhs.comments() < rhs.comments(); - } - } -} - -template -cxx::enable_if_t::array_type>, - detail::is_comparable::table_type> - >::value, bool> -operator<=(const basic_value& lhs, const basic_value& rhs) -{ - return (lhs < rhs) || (lhs == rhs); -} -template -cxx::enable_if_t::array_type>, - detail::is_comparable::table_type> - >::value, bool> -operator>(const basic_value& lhs, const basic_value& rhs) -{ - return !(lhs <= rhs); -} -template -cxx::enable_if_t::array_type>, - detail::is_comparable::table_type> - >::value, bool> -operator>=(const basic_value& lhs, const basic_value& rhs) -{ - return !(lhs < rhs); -} - -// error_info helper -namespace detail -{ -template -error_info make_error_info_rec(error_info e, - const basic_value& v, std::string msg, Ts&& ... tail) -{ - return make_error_info_rec(std::move(e), v.location(), std::move(msg), std::forward(tail)...); -} -} // detail - -template -error_info make_error_info( - std::string title, const basic_value& v, std::string msg, Ts&& ... tail) -{ - return make_error_info(std::move(title), - v.location(), std::move(msg), std::forward(tail)...); -} -template -std::string format_error(std::string title, - const basic_value& v, std::string msg, Ts&& ... tail) -{ - return format_error(std::move(title), - v.location(), std::move(msg), std::forward(tail)...); -} - -namespace detail -{ - -template -error_info make_type_error(const basic_value& v, const std::string& fname, const value_t ty) -{ - return make_error_info(fname + ": bad_cast to " + to_string(ty), - v.location(), "the actual type is " + to_string(v.type())); -} -template -error_info make_not_found_error(const basic_value& v, const std::string& fname, const typename basic_value::key_type& key) -{ - const auto loc = v.location(); - const std::string title = fname + ": key \"" + string_conv(key) + "\" not found"; - - std::vector> locs; - if( ! loc.is_ok()) - { - return error_info(title, locs); - } - - if(loc.first_line_number() == 1 && loc.first_column_number() == 1 && loc.length() == 1) - { - // The top-level table has its region at the 0th character of the file. - // That means that, in the case when a key is not found in the top-level - // table, the error message points to the first character. If the file has - // the first table at the first line, the error message would be like this. - // ```console - // [error] key "a" not found - // --> example.toml - // | - // 1 | [table] - // | ^------ in this table - // ``` - // It actually points to the top-level table at the first character, not - // `[table]`. But it is too confusing. To avoid the confusion, the error - // message should explicitly say "key not found in the top-level table". - locs.emplace_back(v.location(), "at the top-level table"); - } - else - { - locs.emplace_back(v.location(), "in this table"); - } - return error_info(title, locs); -} - -#define TOML11_DETAIL_GENERATE_COMPTIME_GETTER(ty) \ - template \ - struct getter \ - { \ - using value_type = basic_value; \ - using result_type = enum_to_type_t; \ - using format_type = enum_to_fmt_type_t; \ - \ - static result_type& get(value_type& v) \ - { \ - return v.as_ ## ty(); \ - } \ - static result_type const& get(const value_type& v) \ - { \ - return v.as_ ## ty(); \ - } \ - \ - static result_type& get_nothrow(value_type& v) noexcept \ - { \ - return v.as_ ## ty(std::nothrow); \ - } \ - static result_type const& get_nothrow(const value_type& v) noexcept \ - { \ - return v.as_ ## ty(std::nothrow); \ - } \ - \ - static format_type& get_fmt(value_type& v) \ - { \ - return v.as_ ## ty ## _fmt(); \ - } \ - static format_type const& get_fmt(const value_type& v) \ - { \ - return v.as_ ## ty ## _fmt(); \ - } \ - \ - static format_type& get_fmt_nothrow(value_type& v) noexcept \ - { \ - return v.as_ ## ty ## _fmt(std::nothrow); \ - } \ - static format_type const& get_fmt_nothrow(const value_type& v) noexcept \ - { \ - return v.as_ ## ty ## _fmt(std::nothrow); \ - } \ - }; - -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(boolean ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(integer ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(floating ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(string ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(offset_datetime) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(local_datetime ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(local_date ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(local_time ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(array ) -TOML11_DETAIL_GENERATE_COMPTIME_GETTER(table ) - -#undef TOML11_DETAIL_GENERATE_COMPTIME_GETTER - -template -void change_region_of_value(basic_value& dst, const basic_value& src) -{ - dst.region_ = std::move(src.region_); - return; -} - -} // namespace detail -} // namespace toml -#endif // TOML11_VALUE_HPP diff --git a/include/toml11/value_t.hpp b/include/toml11/value_t.hpp deleted file mode 100644 index ed0fbe9..0000000 --- a/include/toml11/value_t.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef TOML11_VALUE_T_HPP -#define TOML11_VALUE_T_HPP - -#include "fwd/value_t_fwd.hpp" // IWYU pragma: export - -#if ! defined(TOML11_COMPILE_SOURCES) -#include "impl/value_t_impl.hpp" // IWYU pragma: export -#endif - -#endif // TOML11_VALUE_T_HPP diff --git a/include/toml11/version.hpp b/include/toml11/version.hpp deleted file mode 100644 index 24fcd7d..0000000 --- a/include/toml11/version.hpp +++ /dev/null @@ -1,121 +0,0 @@ -#ifndef TOML11_VERSION_HPP -#define TOML11_VERSION_HPP - -#define TOML11_VERSION_MAJOR 4 -#define TOML11_VERSION_MINOR 3 -#define TOML11_VERSION_PATCH 0 - -#ifndef __cplusplus -# error "__cplusplus is not defined" -#endif - -// Since MSVC does not define `__cplusplus` correctly unless you pass -// `/Zc:__cplusplus` when compiling, the workaround macros are added. -// -// The value of `__cplusplus` macro is defined in the C++ standard spec, but -// MSVC ignores the value, maybe because of backward compatibility. Instead, -// MSVC defines _MSVC_LANG that has the same value as __cplusplus defined in -// the C++ standard. So we check if _MSVC_LANG is defined before using `__cplusplus`. -// -// FYI: https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170 -// https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170 -// - -#if defined(_MSVC_LANG) && defined(_MSC_VER) && 190024210 <= _MSC_FULL_VER -# define TOML11_CPLUSPLUS_STANDARD_VERSION _MSVC_LANG -#else -# define TOML11_CPLUSPLUS_STANDARD_VERSION __cplusplus -#endif - -#if TOML11_CPLUSPLUS_STANDARD_VERSION < 201103L -# error "toml11 requires C++11 or later." -#endif - -#if ! defined(__has_include) -# define __has_include(x) 0 -#endif - -#if ! defined(__has_cpp_attribute) -# define __has_cpp_attribute(x) 0 -#endif - -#if ! defined(__has_builtin) -# define __has_builtin(x) 0 -#endif - -// hard to remember - -#ifndef TOML11_CXX14_VALUE -#define TOML11_CXX14_VALUE 201402L -#endif//TOML11_CXX14_VALUE - -#ifndef TOML11_CXX17_VALUE -#define TOML11_CXX17_VALUE 201703L -#endif//TOML11_CXX17_VALUE - -#ifndef TOML11_CXX20_VALUE -#define TOML11_CXX20_VALUE 202002L -#endif//TOML11_CXX20_VALUE - -#if defined(__cpp_char8_t) -# if __cpp_char8_t >= 201811L -# define TOML11_HAS_CHAR8_T 1 -# endif -#endif - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if __has_include() -# define TOML11_HAS_STRING_VIEW 1 -# endif -#endif - -#ifndef TOML11_DISABLE_STD_FILESYSTEM -# if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if __has_include() -# define TOML11_HAS_FILESYSTEM 1 -# endif -# endif -#endif - -#if TOML11_CPLUSPLUS_STANDARD_VERSION >= TOML11_CXX17_VALUE -# if __has_include() -# define TOML11_HAS_OPTIONAL 1 -# endif -#endif - -#if defined(TOML11_COMPILE_SOURCES) -# define TOML11_INLINE -#else -# define TOML11_INLINE inline -#endif - -namespace toml -{ - -inline const char* license_notice() noexcept -{ - return R"(The MIT License (MIT) - -Copyright (c) 2017-now Toru Niina - -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.)"; -} - -} // toml -#endif // TOML11_VERSION_HPP diff --git a/include/toml11/visit.hpp b/include/toml11/visit.hpp deleted file mode 100644 index 715b403..0000000 --- a/include/toml11/visit.hpp +++ /dev/null @@ -1,136 +0,0 @@ -#ifndef TOML11_VISIT_HPP -#define TOML11_VISIT_HPP - -#include "exception.hpp" -#include "traits.hpp" -#include "value.hpp" - -namespace toml -{ - -namespace detail -{ - -template -using visit_result_t = decltype(std::declval()(std::declval().as_boolean() ...)); - -template -struct front_binder -{ - template - auto operator()(Args&& ... args) -> decltype(std::declval()(std::declval(), std::forward(args)...)) - { - return func(std::move(front), std::forward(args)...); - } - F func; - T front; -}; - -template -front_binder, cxx::remove_cvref_t> -bind_front(F&& f, T&& t) -{ - return front_binder, cxx::remove_cvref_t>{ - std::forward(f), std::forward(t) - }; -} - -template -visit_result_t&, Args...> -visit_impl(Visitor&& visitor, const basic_value& v, Args&& ... args); - -template -visit_result_t&, Args...> -visit_impl(Visitor&& visitor, basic_value& v, Args&& ... args); - -template -visit_result_t, Args...> -visit_impl(Visitor&& visitor, basic_value&& v, Args&& ... args); - - -template -visit_result_t visit_impl(Visitor&& visitor) -{ - return visitor(); -} - -template -visit_result_t&, Args...> -visit_impl(Visitor&& visitor, basic_value& v, Args&& ... args) -{ - switch(v.type()) - { - case value_t::boolean : {return visit_impl(bind_front(visitor, std::ref(v.as_boolean ())), std::forward(args)...);} - case value_t::integer : {return visit_impl(bind_front(visitor, std::ref(v.as_integer ())), std::forward(args)...);} - case value_t::floating : {return visit_impl(bind_front(visitor, std::ref(v.as_floating ())), std::forward(args)...);} - case value_t::string : {return visit_impl(bind_front(visitor, std::ref(v.as_string ())), std::forward(args)...);} - case value_t::offset_datetime: {return visit_impl(bind_front(visitor, std::ref(v.as_offset_datetime())), std::forward(args)...);} - case value_t::local_datetime : {return visit_impl(bind_front(visitor, std::ref(v.as_local_datetime ())), std::forward(args)...);} - case value_t::local_date : {return visit_impl(bind_front(visitor, std::ref(v.as_local_date ())), std::forward(args)...);} - case value_t::local_time : {return visit_impl(bind_front(visitor, std::ref(v.as_local_time ())), std::forward(args)...);} - case value_t::array : {return visit_impl(bind_front(visitor, std::ref(v.as_array ())), std::forward(args)...);} - case value_t::table : {return visit_impl(bind_front(visitor, std::ref(v.as_table ())), std::forward(args)...);} - case value_t::empty : break; - default: break; - } - throw type_error(format_error("[error] toml::visit: toml::basic_value " - "does not have any valid type.", v.location(), "here"), v.location()); -} - -template -visit_result_t&, Args...> -visit_impl(Visitor&& visitor, const basic_value& v, Args&& ... args) -{ - switch(v.type()) - { - case value_t::boolean : {return visit_impl(bind_front(visitor, std::cref(v.as_boolean ())), std::forward(args)...);} - case value_t::integer : {return visit_impl(bind_front(visitor, std::cref(v.as_integer ())), std::forward(args)...);} - case value_t::floating : {return visit_impl(bind_front(visitor, std::cref(v.as_floating ())), std::forward(args)...);} - case value_t::string : {return visit_impl(bind_front(visitor, std::cref(v.as_string ())), std::forward(args)...);} - case value_t::offset_datetime: {return visit_impl(bind_front(visitor, std::cref(v.as_offset_datetime())), std::forward(args)...);} - case value_t::local_datetime : {return visit_impl(bind_front(visitor, std::cref(v.as_local_datetime ())), std::forward(args)...);} - case value_t::local_date : {return visit_impl(bind_front(visitor, std::cref(v.as_local_date ())), std::forward(args)...);} - case value_t::local_time : {return visit_impl(bind_front(visitor, std::cref(v.as_local_time ())), std::forward(args)...);} - case value_t::array : {return visit_impl(bind_front(visitor, std::cref(v.as_array ())), std::forward(args)...);} - case value_t::table : {return visit_impl(bind_front(visitor, std::cref(v.as_table ())), std::forward(args)...);} - case value_t::empty : break; - default: break; - } - throw type_error(format_error("[error] toml::visit: toml::basic_value " - "does not have any valid type.", v.location(), "here"), v.location()); -} - -template -visit_result_t, Args...> -visit_impl(Visitor&& visitor, basic_value&& v, Args&& ... args) -{ - switch(v.type()) - { - case value_t::boolean : {return visit_impl(bind_front(visitor, std::move(v.as_boolean ())), std::forward(args)...);} - case value_t::integer : {return visit_impl(bind_front(visitor, std::move(v.as_integer ())), std::forward(args)...);} - case value_t::floating : {return visit_impl(bind_front(visitor, std::move(v.as_floating ())), std::forward(args)...);} - case value_t::string : {return visit_impl(bind_front(visitor, std::move(v.as_string ())), std::forward(args)...);} - case value_t::offset_datetime: {return visit_impl(bind_front(visitor, std::move(v.as_offset_datetime())), std::forward(args)...);} - case value_t::local_datetime : {return visit_impl(bind_front(visitor, std::move(v.as_local_datetime ())), std::forward(args)...);} - case value_t::local_date : {return visit_impl(bind_front(visitor, std::move(v.as_local_date ())), std::forward(args)...);} - case value_t::local_time : {return visit_impl(bind_front(visitor, std::move(v.as_local_time ())), std::forward(args)...);} - case value_t::array : {return visit_impl(bind_front(visitor, std::move(v.as_array ())), std::forward(args)...);} - case value_t::table : {return visit_impl(bind_front(visitor, std::move(v.as_table ())), std::forward(args)...);} - case value_t::empty : break; - default: break; - } - throw type_error(format_error("[error] toml::visit: toml::basic_value " - "does not have any valid type.", v.location(), "here"), v.location()); -} - -} // detail - -template -detail::visit_result_t -visit(Visitor&& visitor, Args&& ... args) -{ - return detail::visit_impl(std::forward(visitor), std::forward(args)...); -} - -} // toml -#endif // TOML11_VISIT_HPP diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 56ef3e1..38d8bec 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -63,6 +63,7 @@ target_include_directories(ocvsmd_engine target_include_directories(ocvsmd_engine SYSTEM PUBLIC ${submodules_dir}/cetl/include PUBLIC ${submodules_dir}/libcyphal/include + PRIVATE ${submodules_dir}/toml11/include ) add_dependencies(ocvsmd_engine ${engine_transpiled} diff --git a/submodules/toml11 b/submodules/toml11 new file mode 160000 index 0000000..499be3c --- /dev/null +++ b/submodules/toml11 @@ -0,0 +1 @@ +Subproject commit 499be3c177bcf9b42848d5d9567153e4edfcbc8a From 34a5143d31541e84d7b56b1443ed0712a8ea9d5d Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 10 Feb 2025 18:30:45 +0200 Subject: [PATCH 126/156] implemented FS 'ListRoots' ipc --- .clang-tidy | 1 + include/ocvsmd/sdk/daemon.hpp | 4 +- include/ocvsmd/sdk/file_server.hpp | 51 ++++++++ src/cli/main.cpp | 28 ++++- .../common/svc/file_server/ListRoots.0.1.dsdl | 8 ++ .../svc/file_server/list_roots_spec.hpp | 38 ++++++ src/daemon/engine/CMakeLists.txt | 1 + src/daemon/engine/cyphal/file_provider.cpp | 23 +++- src/daemon/engine/cyphal/file_provider.hpp | 4 + src/daemon/engine/engine.cpp | 2 + .../svc/file_server/list_roots_service.cpp | 89 ++++++++++++++ .../svc/file_server/list_roots_service.hpp | 37 ++++++ .../engine/svc/node/exec_cmd_service.cpp | 4 +- src/sdk/CMakeLists.txt | 2 + src/sdk/daemon.cpp | 9 +- src/sdk/file_server.cpp | 80 ++++++++++++ src/sdk/sdk_factory.hpp | 4 + src/sdk/svc/file_server/list_roots_client.cpp | 115 ++++++++++++++++++ src/sdk/svc/file_server/list_roots_client.hpp | 72 +++++++++++ 19 files changed, 563 insertions(+), 9 deletions(-) create mode 100644 include/ocvsmd/sdk/file_server.hpp create mode 100644 src/common/dsdl/ocvsmd/common/svc/file_server/ListRoots.0.1.dsdl create mode 100644 src/common/svc/file_server/list_roots_spec.hpp create mode 100644 src/daemon/engine/svc/file_server/list_roots_service.cpp create mode 100644 src/daemon/engine/svc/file_server/list_roots_service.hpp create mode 100644 src/sdk/file_server.cpp create mode 100644 src/sdk/svc/file_server/list_roots_client.cpp create mode 100644 src/sdk/svc/file_server/list_roots_client.hpp diff --git a/.clang-tidy b/.clang-tidy index 3cc20b7..1b0a439 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -27,6 +27,7 @@ Checks: >- -readability-avoid-const-params-in-decls, -readability-identifier-length, -*-use-designated-initializers, + -*-use-ranges, -*-use-trailing-return-type, -*-named-parameter, -misc-include-cleaner, diff --git a/include/ocvsmd/sdk/daemon.hpp b/include/ocvsmd/sdk/daemon.hpp index 81a5fb3..968c285 100644 --- a/include/ocvsmd/sdk/daemon.hpp +++ b/include/ocvsmd/sdk/daemon.hpp @@ -6,6 +6,7 @@ #ifndef OCVSMD_SDK_DAEMON_HPP_INCLUDED #define OCVSMD_SDK_DAEMON_HPP_INCLUDED +#include "file_server.hpp" #include "node_command_client.hpp" #include @@ -38,7 +39,8 @@ class Daemon virtual ~Daemon() = default; - virtual NodeCommandClient::Ptr getNodeCommandClient() = 0; + virtual FileServer::Ptr getFileServer() const = 0; + virtual NodeCommandClient::Ptr getNodeCommandClient() const = 0; protected: Daemon() = default; diff --git a/include/ocvsmd/sdk/file_server.hpp b/include/ocvsmd/sdk/file_server.hpp new file mode 100644 index 0000000..0b7c196 --- /dev/null +++ b/include/ocvsmd/sdk/file_server.hpp @@ -0,0 +1,51 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_FILE_SERVER_HPP_INCLUDED +#define OCVSMD_SDK_FILE_SERVER_HPP_INCLUDED + +#include "execution.hpp" + +#include + +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ + +class FileServer +{ +public: + using Ptr = std::shared_ptr; + + FileServer(FileServer&&) = delete; + FileServer(const FileServer&) = delete; + FileServer& operator=(FileServer&&) = delete; + FileServer& operator=(const FileServer&) = delete; + + virtual ~FileServer() = default; + + struct ListRoots final + { + using Success = std::vector; + using Failure = int; // `errno`-like error code. + using Result = cetl::variant; + }; + virtual SenderOf::Ptr listRoots() = 0; + +protected: + FileServer() = default; + +}; // FileServer + +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_FILE_SERVER_HPP_INCLUDED diff --git a/src/cli/main.cpp b/src/cli/main.cpp index e62b3c6..cf550c9 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -12,6 +12,7 @@ #include +#include #include #include @@ -83,17 +84,18 @@ int main(const int argc, const char** const argv) return EXIT_FAILURE; } +#if 0 // NOLINT` + // Demo of daemon's node command client, sending a command to node 42, 43 & 44. { using Command = ocvsmd::sdk::NodeCommandClient::Command; auto node_cmd_client = daemon->getNodeCommandClient(); - const std::vector node_ids = {42}; + const std::vector node_ids = {42, 43, 44}; // auto sender = node_cmd_client->restart({node_ids.data(), node_ids.size()}); auto sender = node_cmd_client->beginSoftwareUpdate({node_ids.data(), node_ids.size()}, "firmware.bin"); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); - if (const auto* const err = cetl::get_if(&cmd_result)) { spdlog::error("Failed to send command: {}", std::strerror(*err)); @@ -109,6 +111,28 @@ int main(const int argc, const char** const argv) } } } +#endif +#if 1 // NOLINT` + + // Demo of daemon's file server, getting the list of roots. + { + using ListRoots = ocvsmd::sdk::FileServer::ListRoots; + + auto file_server = daemon->getFileServer(); + + auto sender = file_server->listRoots(); + auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); + if (const auto* const err = cetl::get_if(&cmd_result)) + { + spdlog::error("Failed to list FS roots: {}", std::strerror(*err)); + } + else + { + const auto roots_list = cetl::get(std::move(cmd_result)); + spdlog::info("File Server responded with list of roots: {}.", roots_list); + } + } +#endif if (g_running == 0) { diff --git a/src/common/dsdl/ocvsmd/common/svc/file_server/ListRoots.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/file_server/ListRoots.0.1.dsdl new file mode 100644 index 0000000..fc15d42 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/svc/file_server/ListRoots.0.1.dsdl @@ -0,0 +1,8 @@ + +@extent 512 * 8 + +--- + +uavcan.file.Path.2.0 item + +@extent 512 * 8 diff --git a/src/common/svc/file_server/list_roots_spec.hpp b/src/common/svc/file_server/list_roots_spec.hpp new file mode 100644 index 0000000..2987e58 --- /dev/null +++ b/src/common/svc/file_server/list_roots_spec.hpp @@ -0,0 +1,38 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_SVC_FILE_SERVER_LIST_ROOTS_SPEC_HPP_INCLUDED +#define OCVSMD_COMMON_SVC_FILE_SERVER_LIST_ROOTS_SPEC_HPP_INCLUDED + +#include "ocvsmd/common/svc/file_server/ListRoots_0_1.hpp" + +namespace ocvsmd +{ +namespace common +{ +namespace svc +{ +namespace file_server +{ + +struct ListRootsSpec +{ + using Request = ListRoots::Request_0_1; + using Response = ListRoots::Response_0_1; + + constexpr auto static svc_full_name() + { + return "ocvsmd.svc.file_server.list_roots"; + } + + ListRootsSpec() = delete; +}; + +} // namespace file_server +} // namespace svc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_SVC_FILE_SERVER_LIST_ROOTS_SPEC_HPP_INCLUDED diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 38d8bec..49e54e2 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -49,6 +49,7 @@ add_library(ocvsmd_engine engine.cpp platform/can/socketcan.c platform/udp/udp.c + svc/file_server/list_roots_service.cpp svc/node/exec_cmd_service.cpp ) target_link_libraries(ocvsmd_engine diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index c20ad27..161fdd2 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -4,10 +4,10 @@ // #include "config.hpp" -#include "file_provider.hpp" - #include "engine_helpers.hpp" +#include "file_provider.hpp" #include "logging.hpp" +#include "svc/file_server/list_roots_spec.hpp" #include #include @@ -101,6 +101,9 @@ class FileProviderImpl final : public FileProvider , modify_srv_{std::move(modify_srv)} , get_info_srv_{std::move(get_info_srv)} { + using ListRootsSpec = common::svc::file_server::ListRootsSpec; + constexpr auto MaxRootLen = ListRootsSpec::Response::_traits_::TypeOf::item::_traits_::ArrayCapacity::path; + logger_->trace("FileProviderImpl()."); roots_ = config_->getFileServerRoots(); @@ -109,7 +112,14 @@ class FileProviderImpl final : public FileProvider { if (const auto real_root = canonicalizePath(roots_[i])) { - logger_->trace("{:4} '{}' → '{}'", i, roots_[i], *real_root); + if (roots_[i].size() <= MaxRootLen) + { + logger_->trace("{:4} '{}' → '{}'", i, roots_[i], *real_root); + } + else + { + logger_->warn("{:4} 🟡 too long '{}' → '{}'", i, roots_[i], *real_root); + } } else { @@ -139,6 +149,13 @@ class FileProviderImpl final : public FileProvider // FileProvider + /// Gets the list of roots that the file provider is serving. + /// + const std::vector& getListOfRoots() const override + { + return roots_; + } + private: using Presentation = libcyphal::presentation::Presentation; diff --git a/src/daemon/engine/cyphal/file_provider.hpp b/src/daemon/engine/cyphal/file_provider.hpp index 286c50f..324dc05 100644 --- a/src/daemon/engine/cyphal/file_provider.hpp +++ b/src/daemon/engine/cyphal/file_provider.hpp @@ -13,6 +13,8 @@ #include #include +#include +#include namespace ocvsmd { @@ -48,6 +50,8 @@ class FileProvider virtual ~FileProvider() = default; + virtual const std::vector& getListOfRoots() const = 0; + protected: FileProvider() = default; diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 29f4159..42b1f59 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -13,6 +13,7 @@ #include "ipc/pipe/server_pipe.hpp" #include "ipc/pipe/socket_server.hpp" #include "ipc/server_router.hpp" +#include "svc/file_server/list_roots_service.hpp" #include "svc/node/exec_cmd_service.hpp" #include "svc/svc_helpers.hpp" @@ -140,6 +141,7 @@ cetl::optional Engine::init() // const svc::ScvContext svc_context{memory_, executor_, *ipc_router_, *presentation_}; svc::node::ExecCmdService::registerWithContext(svc_context); + svc::file_server::ListRootService::registerWithContext(svc_context, *file_provider_); // if (0 != ipc_router_->start()) { diff --git a/src/daemon/engine/svc/file_server/list_roots_service.cpp b/src/daemon/engine/svc/file_server/list_roots_service.cpp new file mode 100644 index 0000000..261472d --- /dev/null +++ b/src/daemon/engine/svc/file_server/list_roots_service.cpp @@ -0,0 +1,89 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "list_roots_service.hpp" + +#include "cyphal/file_provider.hpp" +#include "ipc/channel.hpp" +#include "ipc/server_router.hpp" +#include "logging.hpp" +#include "svc/file_server/list_roots_spec.hpp" +#include "svc/svc_helpers.hpp" + +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ +namespace +{ + +class ListRootServiceImpl final +{ +public: + using Spec = common::svc::file_server::ListRootsSpec; + using Channel = common::ipc::Channel; + + explicit ListRootServiceImpl(const ScvContext& context, cyphal::FileProvider& file_provider) + : context_{context} + , file_provider_{file_provider} + { + } + + void operator()(Channel&& channel, const Spec::Request&) const + { + logger_->debug("New '{}' service channel.", Spec::svc_full_name()); + + Spec::Response ipc_response{&context_.memory}; + const auto& roots = file_provider_.getListOfRoots(); + for (const auto& root : roots) + { + constexpr auto MaxRootLen = Spec::Response::_traits_::TypeOf::item::_traits_::ArrayCapacity::path; + if (root.size() > MaxRootLen) + { + logger_->warn("ListRootSvc: Can't list too long path (max_len={}, root='{}').", MaxRootLen, root); + continue; + } + + ipc_response.item.path.clear(); + std::copy(root.cbegin(), root.cend(), std::back_inserter(ipc_response.item.path)); + if (const auto err = channel.send(ipc_response)) + { + logger_->warn("ListRootSvc: failed to send ipc response (err={}).", err); + } + } + + channel.complete(0); + } + +private: + const ScvContext context_; + cyphal::FileProvider& file_provider_; + common::LoggerPtr logger_{common::getLogger("engine")}; + +}; // ExecCmdServiceImpl + +} // namespace + +void ListRootService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) +{ + using Impl = ListRootServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context, file_provider}); +} + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd diff --git a/src/daemon/engine/svc/file_server/list_roots_service.hpp b/src/daemon/engine/svc/file_server/list_roots_service.hpp new file mode 100644 index 0000000..9079200 --- /dev/null +++ b/src/daemon/engine/svc/file_server/list_roots_service.hpp @@ -0,0 +1,37 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_LIST_ROOTS_SERVICE_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_LIST_ROOTS_SERVICE_HPP_INCLUDED + +#include "cyphal/file_provider.hpp" +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ + +class ListRootService +{ +public: + ListRootService() = delete; + static void registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider); + +}; // ListRootService + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_LIST_ROOTS_SERVICE_HPP_INCLUDED diff --git a/src/daemon/engine/svc/node/exec_cmd_service.cpp b/src/daemon/engine/svc/node/exec_cmd_service.cpp index 6d6a32c..52676ae 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.cpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.cpp @@ -50,12 +50,12 @@ class ExecCmdServiceImpl final { } - void operator()(Channel&& ch, const Spec::Request& request) + void operator()(Channel&& channel, const Spec::Request& request) { const auto fsm_id = next_fsm_id_++; logger_->debug("New '{}' service channel (fsm={}).", Spec::svc_full_name(), fsm_id); - auto fsm = std::make_shared(*this, fsm_id, std::move(ch)); + auto fsm = std::make_shared(*this, fsm_id, std::move(channel)); id_to_fsm_[fsm_id] = fsm; fsm->start(request); diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index e912e71..5270786 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -18,8 +18,10 @@ add_cyphal_library( add_library(ocvsmd_sdk daemon.cpp + file_server.cpp node_command_client.cpp svc/node/exec_cmd_client.cpp + svc/file_server/list_roots_client.cpp ) target_link_libraries(ocvsmd_sdk PUBLIC ${sdk_transpiled} diff --git a/src/sdk/daemon.cpp b/src/sdk/daemon.cpp index 09416d7..6b82925 100644 --- a/src/sdk/daemon.cpp +++ b/src/sdk/daemon.cpp @@ -59,6 +59,7 @@ class DaemonImpl final : public Daemon ipc_router_ = common::ipc::ClientRouter::make(memory_, std::move(client_pipe)); + file_server_ = Factory::makeFileServer(memory_, ipc_router_); node_command_client_ = Factory::makeNodeCommandClient(memory_, ipc_router_); if (const int err = ipc_router_->start()) @@ -73,7 +74,12 @@ class DaemonImpl final : public Daemon // Daemon - NodeCommandClient::Ptr getNodeCommandClient() override + FileServer::Ptr getFileServer() const override + { + return file_server_; + } + + NodeCommandClient::Ptr getNodeCommandClient() const override { return node_command_client_; } @@ -83,6 +89,7 @@ class DaemonImpl final : public Daemon libcyphal::IExecutor& executor_; common::LoggerPtr logger_; common::ipc::ClientRouter::Ptr ipc_router_; + FileServer::Ptr file_server_; NodeCommandClient::Ptr node_command_client_; }; // DaemonImpl diff --git a/src/sdk/file_server.cpp b/src/sdk/file_server.cpp new file mode 100644 index 0000000..6c378a6 --- /dev/null +++ b/src/sdk/file_server.cpp @@ -0,0 +1,80 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include + +#include "ipc/channel.hpp" +#include "ipc/client_router.hpp" +#include "logging.hpp" +#include "sdk_factory.hpp" +#include "svc/file_server/list_roots_client.hpp" + +namespace ocvsmd +{ +namespace sdk +{ +namespace +{ + +class FileServerImpl final : public FileServer +{ +public: + FileServerImpl(cetl::pmr::memory_resource& memory, common::ipc::ClientRouter::Ptr ipc_router) + : memory_{memory} + , ipc_router_{std::move(ipc_router)} + , logger_{common::getLogger("sdk")} + { + } + + // FileServer + + SenderOf::Ptr listRoots() override + { + common::svc::file_server::ListRootsSpec::Request request{}; + auto svc_client = ListRootsClient::make(memory_, ipc_router_, std::move(request)); + + return std::make_unique(std::move(svc_client)); + } + +private: + using ListRootsClient = svc::file_server::ListRootsClient; + + class ListRootsSender final : public SenderOf + { + public: + explicit ListRootsSender(ListRootsClient::Ptr svc_client) + : svc_client_{std::move(svc_client)} + { + } + + void submitImpl(std::function&& receiver) override + { + svc_client_->submit([receiver = std::move(receiver)](ListRootsClient::Result&& result) mutable { + // + receiver(std::move(result)); + }); + } + + private: + ListRootsClient::Ptr svc_client_; + + }; // ListRootsSender + + cetl::pmr::memory_resource& memory_; + common::LoggerPtr logger_; + common::ipc::ClientRouter::Ptr ipc_router_; + +}; // FileServerImpl + +} // namespace + +CETL_NODISCARD FileServer::Ptr Factory::makeFileServer(cetl::pmr::memory_resource& memory, + common::ipc::ClientRouter::Ptr ipc_router) +{ + return std::make_shared(memory, std::move(ipc_router)); +} + +} // namespace sdk +} // namespace ocvsmd diff --git a/src/sdk/sdk_factory.hpp b/src/sdk/sdk_factory.hpp index 21eb107..9e6372a 100644 --- a/src/sdk/sdk_factory.hpp +++ b/src/sdk/sdk_factory.hpp @@ -6,6 +6,7 @@ #ifndef OCVSMD_SDK_FACTORY_HPP_INCLUDED #define OCVSMD_SDK_FACTORY_HPP_INCLUDED +#include #include #include "ipc/client_router.hpp" @@ -20,6 +21,9 @@ namespace sdk struct Factory { + CETL_NODISCARD static FileServer::Ptr makeFileServer(cetl::pmr::memory_resource& memory, + common::ipc::ClientRouter::Ptr ipc_router); + CETL_NODISCARD static NodeCommandClient::Ptr makeNodeCommandClient(cetl::pmr::memory_resource& memory, common::ipc::ClientRouter::Ptr ipc_router); diff --git a/src/sdk/svc/file_server/list_roots_client.cpp b/src/sdk/svc/file_server/list_roots_client.cpp new file mode 100644 index 0000000..ec4e80e --- /dev/null +++ b/src/sdk/svc/file_server/list_roots_client.cpp @@ -0,0 +1,115 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "list_roots_client.hpp" + +#include "ipc/channel.hpp" +#include "ipc/client_router.hpp" +#include "ipc/ipc_types.hpp" +#include "logging.hpp" +#include "svc/file_server/list_roots_spec.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace file_server +{ +namespace +{ + +class ListRootsClientImpl final : public ListRootsClient +{ +public: + ListRootsClientImpl(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + Spec::Request&& request) + : memory_{memory} + , logger_{common::getLogger("svc")} + , request_{std::move(request)} + , channel_{ipc_router->makeChannel(Spec::svc_full_name())} + { + } + + void submitImpl(std::function&& receiver) override + { + receiver_ = std::move(receiver); + + channel_.subscribe([this](const auto& event_var) { + // + cetl::visit([this](const auto& event) { handleEvent(event); }, event_var); + }); + } + +private: + using Channel = common::ipc::Channel; + + void handleEvent(const Channel::Connected& connected) + { + logger_->trace("ListRootsClient::handleEvent({}).", connected); + + if (const auto err = channel_.send(request_)) + { + CETL_DEBUG_ASSERT(receiver_, ""); + + receiver_(Failure{err}); + } + } + + void handleEvent(const Channel::Input& input) + { + logger_->trace("ListRootsClient::handleEvent(Input)."); + + items_.emplace_back(input.item.path.begin(), input.item.path.end()); + } + + void handleEvent(const Channel::Completed& completed) + { + CETL_DEBUG_ASSERT(receiver_, ""); + + logger_->debug("ListRootsClient::handleEvent({}).", completed); + + if (completed.error_code != common::ipc::ErrorCode::Success) + { + receiver_(static_cast(completed.error_code)); + return; + } + receiver_(Success{std::move(items_)}); + } + + cetl::pmr::memory_resource& memory_; + common::LoggerPtr logger_; + Spec::Request request_; + Channel channel_; + std::function receiver_; + std::vector items_; + +}; // ListRootsClientImpl + +} // namespace + +CETL_NODISCARD ListRootsClient::Ptr ListRootsClient::make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + Spec::Request&& request) +{ + return std::make_shared(memory, ipc_router, std::move(request)); +} + +} // namespace file_server +} // namespace svc +} // namespace sdk +} // namespace ocvsmd diff --git a/src/sdk/svc/file_server/list_roots_client.hpp b/src/sdk/svc/file_server/list_roots_client.hpp new file mode 100644 index 0000000..28f0f28 --- /dev/null +++ b/src/sdk/svc/file_server/list_roots_client.hpp @@ -0,0 +1,72 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_SVC_FILE_SERVER_LIST_ROOTS_CLIENT_HPP_INCLUDED +#define OCVSMD_SDK_SVC_FILE_SERVER_LIST_ROOTS_CLIENT_HPP_INCLUDED + +#include "ipc/client_router.hpp" +#include "svc/file_server/list_roots_spec.hpp" + +#include +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace file_server +{ + +class ListRootsClient +{ +public: + using Ptr = std::shared_ptr; + using Spec = common::svc::file_server::ListRootsSpec; + + using Success = std::vector; + using Failure = int; // `errno`-like error code + using Result = cetl::variant; + + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + Spec::Request&& request); + + ListRootsClient(ListRootsClient&&) = delete; + ListRootsClient(const ListRootsClient&) = delete; + ListRootsClient& operator=(ListRootsClient&&) = delete; + ListRootsClient& operator=(const ListRootsClient&) = delete; + + virtual ~ListRootsClient() = default; + + template + void submit(Receiver&& receiver) + { + submitImpl([receive = std::forward(receiver)](Result&& result) mutable { + // + receive(std::move(result)); + }); + } + +protected: + ListRootsClient() = default; + + virtual void submitImpl(std::function&& receiver) = 0; + +}; // ListRootsClient + +} // namespace file_server +} // namespace svc +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_SVC_FILE_SERVER_LIST_ROOTS_CLIENT_HPP_INCLUDED From 8a3269f34fca16958988aa84187a606111b5e554 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 11:51:30 +0200 Subject: [PATCH 127/156] fix build --- src/cli/main.cpp | 12 ++++++++---- .../engine/svc/file_server/list_roots_service.cpp | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index cf550c9..1306858 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -84,7 +84,7 @@ int main(const int argc, const char** const argv) return EXIT_FAILURE; } -#if 0 // NOLINT` +#if 0 // NOLINT // Demo of daemon's node command client, sending a command to node 42, 43 & 44. { @@ -112,7 +112,7 @@ int main(const int argc, const char** const argv) } } #endif -#if 1 // NOLINT` +#if 1 // NOLINT // Demo of daemon's file server, getting the list of roots. { @@ -128,8 +128,12 @@ int main(const int argc, const char** const argv) } else { - const auto roots_list = cetl::get(std::move(cmd_result)); - spdlog::info("File Server responded with list of roots: {}.", roots_list); + const auto roots = cetl::get(std::move(cmd_result)); + spdlog::info("File Server responded with list of roots (cnt={}):", roots.size()); + for (std::size_t i = 0; i < roots.size(); ++i) + { + spdlog::info("{:4} → '{}'", i, roots[i]); + } } } #endif diff --git a/src/daemon/engine/svc/file_server/list_roots_service.cpp b/src/daemon/engine/svc/file_server/list_roots_service.cpp index 261472d..5259f5c 100644 --- a/src/daemon/engine/svc/file_server/list_roots_service.cpp +++ b/src/daemon/engine/svc/file_server/list_roots_service.cpp @@ -41,7 +41,7 @@ class ListRootServiceImpl final { } - void operator()(Channel&& channel, const Spec::Request&) const + void operator()(Channel channel, const Spec::Request&) const { logger_->debug("New '{}' service channel.", Spec::svc_full_name()); From 217bec0239eb2e81b7902b640bde4f696fb3047e Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 12:08:27 +0200 Subject: [PATCH 128/156] fix build --- src/sdk/file_server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdk/file_server.cpp b/src/sdk/file_server.cpp index 6c378a6..89d4178 100644 --- a/src/sdk/file_server.cpp +++ b/src/sdk/file_server.cpp @@ -32,7 +32,7 @@ class FileServerImpl final : public FileServer SenderOf::Ptr listRoots() override { - common::svc::file_server::ListRootsSpec::Request request{}; + common::svc::file_server::ListRootsSpec::Request request{&memory_}; auto svc_client = ListRootsClient::make(memory_, ipc_router_, std::move(request)); return std::make_unique(std::move(svc_client)); From f3a582529c2bda72c9bbe293c5b7d33f3d1cc796 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 13:10:28 +0200 Subject: [PATCH 129/156] fix build --- src/sdk/file_server.cpp | 4 ++-- src/sdk/svc/file_server/list_roots_client.cpp | 8 ++++---- src/sdk/svc/file_server/list_roots_client.hpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sdk/file_server.cpp b/src/sdk/file_server.cpp index 89d4178..ecf45f6 100644 --- a/src/sdk/file_server.cpp +++ b/src/sdk/file_server.cpp @@ -32,8 +32,8 @@ class FileServerImpl final : public FileServer SenderOf::Ptr listRoots() override { - common::svc::file_server::ListRootsSpec::Request request{&memory_}; - auto svc_client = ListRootsClient::make(memory_, ipc_router_, std::move(request)); + const common::svc::file_server::ListRootsSpec::Request request{&memory_}; + auto svc_client = ListRootsClient::make(memory_, ipc_router_, request); return std::make_unique(std::move(svc_client)); } diff --git a/src/sdk/svc/file_server/list_roots_client.cpp b/src/sdk/svc/file_server/list_roots_client.cpp index ec4e80e..7844c06 100644 --- a/src/sdk/svc/file_server/list_roots_client.cpp +++ b/src/sdk/svc/file_server/list_roots_client.cpp @@ -37,10 +37,10 @@ class ListRootsClientImpl final : public ListRootsClient public: ListRootsClientImpl(cetl::pmr::memory_resource& memory, const common::ipc::ClientRouter::Ptr& ipc_router, - Spec::Request&& request) + const Spec::Request& request) : memory_{memory} , logger_{common::getLogger("svc")} - , request_{std::move(request)} + , request_{request} , channel_{ipc_router->makeChannel(Spec::svc_full_name())} { } @@ -104,9 +104,9 @@ class ListRootsClientImpl final : public ListRootsClient CETL_NODISCARD ListRootsClient::Ptr ListRootsClient::make(cetl::pmr::memory_resource& memory, const common::ipc::ClientRouter::Ptr& ipc_router, - Spec::Request&& request) + const Spec::Request& request) { - return std::make_shared(memory, ipc_router, std::move(request)); + return std::make_shared(memory, ipc_router, request); } } // namespace file_server diff --git a/src/sdk/svc/file_server/list_roots_client.hpp b/src/sdk/svc/file_server/list_roots_client.hpp index 28f0f28..9571ff8 100644 --- a/src/sdk/svc/file_server/list_roots_client.hpp +++ b/src/sdk/svc/file_server/list_roots_client.hpp @@ -39,7 +39,7 @@ class ListRootsClient CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, const common::ipc::ClientRouter::Ptr& ipc_router, - Spec::Request&& request); + const Spec::Request& request); ListRootsClient(ListRootsClient&&) = delete; ListRootsClient(const ListRootsClient&) = delete; From d8b5bb33298192246429ada218ffd53225d477e1 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 17:36:27 +0200 Subject: [PATCH 130/156] Implemented `pushRoot` & `popRoot` of `FileServer`. --- include/ocvsmd/sdk/execution.hpp | 31 ++++- include/ocvsmd/sdk/file_server.hpp | 16 +++ src/cli/main.cpp | 46 +++++++- .../common/svc/file_server/PopRoot.0.1.dsdl | 9 ++ .../common/svc/file_server/PushRoot.0.1.dsdl | 9 ++ src/common/ipc/server_router.cpp | 2 +- src/common/svc/file_server/pop_root_spec.hpp | 38 ++++++ src/common/svc/file_server/push_root_spec.hpp | 38 ++++++ src/daemon/engine/CMakeLists.txt | 4 + src/daemon/engine/cyphal/file_provider.cpp | 31 ++++- src/daemon/engine/cyphal/file_provider.hpp | 4 +- src/daemon/engine/engine.cpp | 13 ++- .../svc/file_server/list_roots_service.cpp | 14 +-- .../svc/file_server/list_roots_service.hpp | 6 +- .../svc/file_server/pop_root_service.cpp | 73 ++++++++++++ .../svc/file_server/pop_root_service.hpp | 37 ++++++ .../svc/file_server/push_root_service.cpp | 73 ++++++++++++ .../svc/file_server/push_root_service.hpp | 37 ++++++ .../engine/svc/file_server/services.cpp | 36 ++++++ .../engine/svc/file_server/services.hpp | 31 +++++ src/daemon/engine/svc/node/services.cpp | 31 +++++ src/daemon/engine/svc/node/services.hpp | 31 +++++ src/sdk/CMakeLists.txt | 2 + src/sdk/file_server.cpp | 75 +++++++++--- src/sdk/svc/as_sender.hpp | 55 +++++++++ src/sdk/svc/file_server/list_roots_client.hpp | 1 + src/sdk/svc/file_server/pop_root_client.cpp | 109 ++++++++++++++++++ src/sdk/svc/file_server/pop_root_client.hpp | 71 ++++++++++++ src/sdk/svc/file_server/push_root_client.cpp | 109 ++++++++++++++++++ src/sdk/svc/file_server/push_root_client.hpp | 71 ++++++++++++ 30 files changed, 1063 insertions(+), 40 deletions(-) create mode 100644 src/common/dsdl/ocvsmd/common/svc/file_server/PopRoot.0.1.dsdl create mode 100644 src/common/dsdl/ocvsmd/common/svc/file_server/PushRoot.0.1.dsdl create mode 100644 src/common/svc/file_server/pop_root_spec.hpp create mode 100644 src/common/svc/file_server/push_root_spec.hpp create mode 100644 src/daemon/engine/svc/file_server/pop_root_service.cpp create mode 100644 src/daemon/engine/svc/file_server/pop_root_service.hpp create mode 100644 src/daemon/engine/svc/file_server/push_root_service.cpp create mode 100644 src/daemon/engine/svc/file_server/push_root_service.hpp create mode 100644 src/daemon/engine/svc/file_server/services.cpp create mode 100644 src/daemon/engine/svc/file_server/services.hpp create mode 100644 src/daemon/engine/svc/node/services.cpp create mode 100644 src/daemon/engine/svc/node/services.hpp create mode 100644 src/sdk/svc/as_sender.hpp create mode 100644 src/sdk/svc/file_server/pop_root_client.cpp create mode 100644 src/sdk/svc/file_server/pop_root_client.hpp create mode 100644 src/sdk/svc/file_server/push_root_client.cpp create mode 100644 src/sdk/svc/file_server/push_root_client.hpp diff --git a/include/ocvsmd/sdk/execution.hpp b/include/ocvsmd/sdk/execution.hpp index 372964d..32ed16a 100644 --- a/include/ocvsmd/sdk/execution.hpp +++ b/include/ocvsmd/sdk/execution.hpp @@ -112,9 +112,38 @@ class SenderOf }; // SenderOf +/// A sender factory that returns a sender which completes immediately +/// by calling the receiver `operator()` with the given pack of arguments. +/// +template +typename SenderOf::Ptr just(Args&&... args) +{ + class Sender final : public SenderOf + { + public: + explicit Sender(Args&&... args) + : result_{std::forward(args)...} + { + } + + private: + // SenderOf + + void submitImpl(std::function&& receiver) override + { + std::move(receiver)(std::move(result_)); + } + + Result result_; + + }; // Sender + + return std::make_unique(std::forward(args)...); +} + /// Initiates an operation execution by submitting a given receiver to the sender. /// -/// The submit "consumes" the receiver (no longer usable after this call). +/// Submit "consumes" the receiver (no longer usable after this call). /// template void submit(Sender& sender, Receiver&& receiver) diff --git a/include/ocvsmd/sdk/file_server.hpp b/include/ocvsmd/sdk/file_server.hpp index 0b7c196..d486201 100644 --- a/include/ocvsmd/sdk/file_server.hpp +++ b/include/ocvsmd/sdk/file_server.hpp @@ -40,6 +40,22 @@ class FileServer }; virtual SenderOf::Ptr listRoots() = 0; + struct PopRoot final + { + using Success = cetl::monostate; + using Failure = int; // `errno`-like error code. + using Result = cetl::variant; + }; + virtual SenderOf::Ptr popRoot(const std::string& path, const bool back) = 0; + + struct PushRoot final + { + using Success = cetl::monostate; + using Failure = int; // `errno`-like error code. + using Result = cetl::variant; + }; + virtual SenderOf::Ptr pushRoot(const std::string& path, const bool back) = 0; + protected: FileServer() = default; diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 1306858..682a28c 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -86,7 +86,7 @@ int main(const int argc, const char** const argv) #if 0 // NOLINT - // Demo of daemon's node command client, sending a command to node 42, 43 & 44. + // Demo of daemon's node command client - sending a command to node 42, 43 & 44. { using Command = ocvsmd::sdk::NodeCommandClient::Command; @@ -114,7 +114,49 @@ int main(const int argc, const char** const argv) #endif #if 1 // NOLINT - // Demo of daemon's file server, getting the list of roots. + // Demo of daemon's file server - push root. + { + using PushRoot = ocvsmd::sdk::FileServer::PushRoot; + + auto file_server = daemon->getFileServer(); + + const std::string path{"/0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd"}; + auto sender = file_server->pushRoot(path, true); + auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); + if (const auto* const err = cetl::get_if(&cmd_result)) + { + spdlog::error("Failed to push FS root (path='{}'): {}.", path, std::strerror(*err)); + } + else + { + spdlog::info("File Server responded ok on 'PushRoot'."); + } + } +#endif +#if 0 // NOLINT + + // Demo of daemon's file server - pop root. + { + using PopRoot = ocvsmd::sdk::FileServer::PopRoot; + + auto file_server = daemon->getFileServer(); + + const std::string path{"key"}; + auto sender = file_server->popRoot(path, true); + auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); + if (const auto* const err = cetl::get_if(&cmd_result)) + { + spdlog::error("Failed to pop FS root (path='{}'): {}.", path, std::strerror(*err)); + } + else + { + spdlog::info("File Server responded ok on 'PopRoot'."); + } + } +#endif +#if 1 // NOLINT + + // Demo of daemon's file server - getting the list of roots. { using ListRoots = ocvsmd::sdk::FileServer::ListRoots; diff --git a/src/common/dsdl/ocvsmd/common/svc/file_server/PopRoot.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/file_server/PopRoot.0.1.dsdl new file mode 100644 index 0000000..4084dc3 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/svc/file_server/PopRoot.0.1.dsdl @@ -0,0 +1,9 @@ + +uavcan.file.Path.2.0 item # The path to the root directory to be pop from the list of roots. +bool is_back # Determines whether the path is searched from the front or the back of the list. + +@extent 512 * 8 + +--- + +@extent 512 * 8 diff --git a/src/common/dsdl/ocvsmd/common/svc/file_server/PushRoot.0.1.dsdl b/src/common/dsdl/ocvsmd/common/svc/file_server/PushRoot.0.1.dsdl new file mode 100644 index 0000000..f8599a0 --- /dev/null +++ b/src/common/dsdl/ocvsmd/common/svc/file_server/PushRoot.0.1.dsdl @@ -0,0 +1,9 @@ + +uavcan.file.Path.2.0 item # The path to the root directory to be pushed into the list of roots. +bool is_back # Determines whether the path is searched from the front or the back of the list. + +@extent 512 * 8 + +--- + +@extent 512 * 8 diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 401280b..ae589b1 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -70,7 +70,7 @@ class ServerRouterImpl final : public ServerRouter void registerChannelFactory(const detail::ServiceDesc service_desc, // TypeErasedChannelFactory channel_factory) override { - logger_->debug("Registering '{}' service (id=0x{:X}).", service_desc.name, service_desc.id); + logger_->trace("Registering '{}' service (id=0x{:X}).", service_desc.name, service_desc.id); service_id_to_channel_factory_[service_desc.id] = std::move(channel_factory); } diff --git a/src/common/svc/file_server/pop_root_spec.hpp b/src/common/svc/file_server/pop_root_spec.hpp new file mode 100644 index 0000000..2c68535 --- /dev/null +++ b/src/common/svc/file_server/pop_root_spec.hpp @@ -0,0 +1,38 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_SVC_FILE_SERVER_POP_ROOT_SPEC_HPP_INCLUDED +#define OCVSMD_COMMON_SVC_FILE_SERVER_POP_ROOT_SPEC_HPP_INCLUDED + +#include "ocvsmd/common/svc/file_server/PopRoot_0_1.hpp" + +namespace ocvsmd +{ +namespace common +{ +namespace svc +{ +namespace file_server +{ + +struct PopRootSpec +{ + using Request = PopRoot::Request_0_1; + using Response = PopRoot::Response_0_1; + + constexpr auto static svc_full_name() + { + return "ocvsmd.svc.file_server.pop_root"; + } + + PopRootSpec() = delete; +}; + +} // namespace file_server +} // namespace svc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_SVC_FILE_SERVER_POP_ROOT_SPEC_HPP_INCLUDED diff --git a/src/common/svc/file_server/push_root_spec.hpp b/src/common/svc/file_server/push_root_spec.hpp new file mode 100644 index 0000000..24abc75 --- /dev/null +++ b/src/common/svc/file_server/push_root_spec.hpp @@ -0,0 +1,38 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_SVC_FILE_SERVER_PUSH_ROOT_SPEC_HPP_INCLUDED +#define OCVSMD_COMMON_SVC_FILE_SERVER_PUSH_ROOT_SPEC_HPP_INCLUDED + +#include "ocvsmd/common/svc/file_server/PushRoot_0_1.hpp" + +namespace ocvsmd +{ +namespace common +{ +namespace svc +{ +namespace file_server +{ + +struct PushRootSpec +{ + using Request = PushRoot::Request_0_1; + using Response = PushRoot::Response_0_1; + + constexpr auto static svc_full_name() + { + return "ocvsmd.svc.file_server.push_root"; + } + + PushRootSpec() = delete; +}; + +} // namespace file_server +} // namespace svc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_SVC_FILE_SERVER_PUSH_ROOT_SPEC_HPP_INCLUDED diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 49e54e2..2374122 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -50,7 +50,11 @@ add_library(ocvsmd_engine platform/can/socketcan.c platform/udp/udp.c svc/file_server/list_roots_service.cpp + svc/file_server/pop_root_service.cpp + svc/file_server/push_root_service.cpp + svc/file_server/services.cpp svc/node/exec_cmd_service.cpp + svc/node/services.cpp ) target_link_libraries(ocvsmd_engine PUBLIC udpard diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 161fdd2..3b84a74 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -151,11 +151,40 @@ class FileProviderImpl final : public FileProvider /// Gets the list of roots that the file provider is serving. /// - const std::vector& getListOfRoots() const override + auto getListOfRoots() const -> const std::vector& override { return roots_; } + void popRoot(const std::string& path, const bool back) override + { + decltype(roots_.end()) it; + if (back) + { + // Search in reverse order, and convert back to forward iterator. + // + const auto rev_it = std::find(roots_.rbegin(), roots_.rend(), path); + it = rev_it.base() - 1; + } + else + { + // Search in forward order. + it = std::find(roots_.begin(), roots_.end(), path); + } + + if (it != roots_.end()) + { + roots_.erase(it); + config_->setFileServerRoots(roots_); + } + } + + void pushRoot(const std::string& path, const bool back) override + { + roots_.insert(back ? roots_.end() : roots_.begin(), path); + config_->setFileServerRoots(roots_); + } + private: using Presentation = libcyphal::presentation::Presentation; diff --git a/src/daemon/engine/cyphal/file_provider.hpp b/src/daemon/engine/cyphal/file_provider.hpp index 324dc05..799a5a2 100644 --- a/src/daemon/engine/cyphal/file_provider.hpp +++ b/src/daemon/engine/cyphal/file_provider.hpp @@ -50,7 +50,9 @@ class FileProvider virtual ~FileProvider() = default; - virtual const std::vector& getListOfRoots() const = 0; + virtual auto getListOfRoots() const -> const std::vector& = 0; + virtual void popRoot(const std::string& path, const bool back) = 0; + virtual void pushRoot(const std::string& path, const bool back) = 0; protected: FileProvider() = default; diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 42b1f59..1449b8f 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -9,12 +9,13 @@ #include "cyphal/can_transport_bag.hpp" #include "cyphal/file_provider.hpp" #include "cyphal/udp_transport_bag.hpp" +#include "engine_helpers.hpp" #include "io/socket_address.hpp" #include "ipc/pipe/server_pipe.hpp" #include "ipc/pipe/socket_server.hpp" #include "ipc/server_router.hpp" -#include "svc/file_server/list_roots_service.hpp" -#include "svc/node/exec_cmd_service.hpp" +#include "svc/file_server/services.hpp" +#include "svc/node/services.hpp" #include "svc/svc_helpers.hpp" #include @@ -140,8 +141,8 @@ cetl::optional Engine::init() ipc_router_ = common::ipc::ServerRouter::make(memory_, std::move(server_pipe)); // const svc::ScvContext svc_context{memory_, executor_, *ipc_router_, *presentation_}; - svc::node::ExecCmdService::registerWithContext(svc_context); - svc::file_server::ListRootService::registerWithContext(svc_context, *file_provider_); + svc::node::registerAllServices(svc_context); + svc::file_server::registerAllServices(svc_context, *file_provider_); // if (0 != ipc_router_->start()) { @@ -171,9 +172,9 @@ void Engine::runWhile(const std::function& loop_predicate) timeout = std::min(timeout, spin_result.next_exec_time.value() - executor_.now()); } - if (const auto maybe_poll_failure = executor_.pollAwaitableResourcesFor(cetl::make_optional(timeout))) + if (const auto poll_failure = executor_.pollAwaitableResourcesFor(cetl::make_optional(timeout))) { - spdlog::warn("Failed to poll awaitable resources."); + spdlog::warn("Failed to poll awaitable resources (err={}).", failureToErrorCode(*poll_failure)); } } spdlog::debug("Run loop predicate is fulfilled (worst_lateness={}us).", diff --git a/src/daemon/engine/svc/file_server/list_roots_service.cpp b/src/daemon/engine/svc/file_server/list_roots_service.cpp index 5259f5c..0ca8edd 100644 --- a/src/daemon/engine/svc/file_server/list_roots_service.cpp +++ b/src/daemon/engine/svc/file_server/list_roots_service.cpp @@ -29,13 +29,13 @@ namespace file_server namespace { -class ListRootServiceImpl final +class ListRootsServiceImpl final { public: using Spec = common::svc::file_server::ListRootsSpec; using Channel = common::ipc::Channel; - explicit ListRootServiceImpl(const ScvContext& context, cyphal::FileProvider& file_provider) + explicit ListRootsServiceImpl(const ScvContext& context, cyphal::FileProvider& file_provider) : context_{context} , file_provider_{file_provider} { @@ -52,7 +52,7 @@ class ListRootServiceImpl final constexpr auto MaxRootLen = Spec::Response::_traits_::TypeOf::item::_traits_::ArrayCapacity::path; if (root.size() > MaxRootLen) { - logger_->warn("ListRootSvc: Can't list too long path (max_len={}, root='{}').", MaxRootLen, root); + logger_->warn("ListRootsSvc: Can't list too long path (max_len={}, root='{}').", MaxRootLen, root); continue; } @@ -60,7 +60,7 @@ class ListRootServiceImpl final std::copy(root.cbegin(), root.cend(), std::back_inserter(ipc_response.item.path)); if (const auto err = channel.send(ipc_response)) { - logger_->warn("ListRootSvc: failed to send ipc response (err={}).", err); + logger_->warn("ListRootsSvc: failed to send ipc response (err={}).", err); } } @@ -72,13 +72,13 @@ class ListRootServiceImpl final cyphal::FileProvider& file_provider_; common::LoggerPtr logger_{common::getLogger("engine")}; -}; // ExecCmdServiceImpl +}; // ListRootsServiceImpl } // namespace -void ListRootService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) +void ListRootsService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) { - using Impl = ListRootServiceImpl; + using Impl = ListRootsServiceImpl; context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context, file_provider}); } diff --git a/src/daemon/engine/svc/file_server/list_roots_service.hpp b/src/daemon/engine/svc/file_server/list_roots_service.hpp index 9079200..e253db9 100644 --- a/src/daemon/engine/svc/file_server/list_roots_service.hpp +++ b/src/daemon/engine/svc/file_server/list_roots_service.hpp @@ -20,13 +20,13 @@ namespace svc namespace file_server { -class ListRootService +class ListRootsService { public: - ListRootService() = delete; + ListRootsService() = delete; static void registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider); -}; // ListRootService +}; // ListRootsService } // namespace file_server } // namespace svc diff --git a/src/daemon/engine/svc/file_server/pop_root_service.cpp b/src/daemon/engine/svc/file_server/pop_root_service.cpp new file mode 100644 index 0000000..ad6cdb9 --- /dev/null +++ b/src/daemon/engine/svc/file_server/pop_root_service.cpp @@ -0,0 +1,73 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "pop_root_service.hpp" + +#include "cyphal/file_provider.hpp" +#include "ipc/channel.hpp" +#include "ipc/server_router.hpp" +#include "logging.hpp" +#include "svc/file_server/pop_root_spec.hpp" +#include "svc/svc_helpers.hpp" + +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ +namespace +{ + +class PopRootServiceImpl final +{ +public: + using Spec = common::svc::file_server::PopRootSpec; + using Channel = common::ipc::Channel; + + explicit PopRootServiceImpl(const ScvContext& context, cyphal::FileProvider& file_provider) + : context_{context} + , file_provider_{file_provider} + { + } + + void operator()(Channel channel, const Spec::Request& request) const + { + logger_->debug("New '{}' service channel.", Spec::svc_full_name()); + + std::string path; + std::copy(request.item.path.cbegin(), request.item.path.cend(), std::back_inserter(path)); + file_provider_.popRoot(path, request.is_back); + channel.complete(0); + } + +private: + const ScvContext context_; + cyphal::FileProvider& file_provider_; + common::LoggerPtr logger_{common::getLogger("engine")}; + +}; // PopRootServiceImpl + +} // namespace + +void PopRootService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) +{ + using Impl = PopRootServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context, file_provider}); +} + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd diff --git a/src/daemon/engine/svc/file_server/pop_root_service.hpp b/src/daemon/engine/svc/file_server/pop_root_service.hpp new file mode 100644 index 0000000..437acc4 --- /dev/null +++ b/src/daemon/engine/svc/file_server/pop_root_service.hpp @@ -0,0 +1,37 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_POP_ROOT_SERVICE_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_POP_ROOT_SERVICE_HPP_INCLUDED + +#include "cyphal/file_provider.hpp" +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ + +class PopRootService +{ +public: + PopRootService() = delete; + static void registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider); + +}; // PopRootService + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_POP_ROOT_SERVICE_HPP_INCLUDED diff --git a/src/daemon/engine/svc/file_server/push_root_service.cpp b/src/daemon/engine/svc/file_server/push_root_service.cpp new file mode 100644 index 0000000..887d923 --- /dev/null +++ b/src/daemon/engine/svc/file_server/push_root_service.cpp @@ -0,0 +1,73 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "push_root_service.hpp" + +#include "cyphal/file_provider.hpp" +#include "ipc/channel.hpp" +#include "ipc/server_router.hpp" +#include "logging.hpp" +#include "svc/file_server/push_root_spec.hpp" +#include "svc/svc_helpers.hpp" + +#include + +#include + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ +namespace +{ + +class PushRootServiceImpl final +{ +public: + using Spec = common::svc::file_server::PushRootSpec; + using Channel = common::ipc::Channel; + + explicit PushRootServiceImpl(const ScvContext& context, cyphal::FileProvider& file_provider) + : context_{context} + , file_provider_{file_provider} + { + } + + void operator()(Channel channel, const Spec::Request& request) const + { + logger_->debug("New '{}' service channel.", Spec::svc_full_name()); + + std::string path; + std::copy(request.item.path.cbegin(), request.item.path.cend(), std::back_inserter(path)); + file_provider_.pushRoot(path, request.is_back); + channel.complete(0); + } + +private: + const ScvContext context_; + cyphal::FileProvider& file_provider_; + common::LoggerPtr logger_{common::getLogger("engine")}; + +}; // PushRootServiceImpl + +} // namespace + +void PushRootService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) +{ + using Impl = PushRootServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context, file_provider}); +} + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd diff --git a/src/daemon/engine/svc/file_server/push_root_service.hpp b/src/daemon/engine/svc/file_server/push_root_service.hpp new file mode 100644 index 0000000..4508a40 --- /dev/null +++ b/src/daemon/engine/svc/file_server/push_root_service.hpp @@ -0,0 +1,37 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_PUSH_ROOT_SERVICE_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_PUSH_ROOT_SERVICE_HPP_INCLUDED + +#include "cyphal/file_provider.hpp" +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ + +class PushRootService +{ +public: + PushRootService() = delete; + static void registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider); + +}; // PushRootService + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_PUSH_ROOT_SERVICE_HPP_INCLUDED diff --git a/src/daemon/engine/svc/file_server/services.cpp b/src/daemon/engine/svc/file_server/services.cpp new file mode 100644 index 0000000..5ffbcab --- /dev/null +++ b/src/daemon/engine/svc/file_server/services.cpp @@ -0,0 +1,36 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "services.hpp" + +#include "cyphal/file_provider.hpp" +#include "list_roots_service.hpp" +#include "pop_root_service.hpp" +#include "push_root_service.hpp" +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ + +void registerAllServices(const ScvContext& context, cyphal::FileProvider& file_provider) +{ + ListRootsService::registerWithContext(context, file_provider); + PopRootService::registerWithContext(context, file_provider); + PushRootService::registerWithContext(context, file_provider); +} + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd diff --git a/src/daemon/engine/svc/file_server/services.hpp b/src/daemon/engine/svc/file_server/services.hpp new file mode 100644 index 0000000..588fcb7 --- /dev/null +++ b/src/daemon/engine/svc/file_server/services.hpp @@ -0,0 +1,31 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_SERVICES_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_SERVICES_HPP_INCLUDED + +#include "cyphal/file_provider.hpp" +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace file_server +{ + +void registerAllServices(const ScvContext& context, cyphal::FileProvider& file_provider); + +} // namespace file_server +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_SVC_FILE_SERVER_SERVICES_HPP_INCLUDED diff --git a/src/daemon/engine/svc/node/services.cpp b/src/daemon/engine/svc/node/services.cpp new file mode 100644 index 0000000..afeb77d --- /dev/null +++ b/src/daemon/engine/svc/node/services.cpp @@ -0,0 +1,31 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "services.hpp" + +#include "exec_cmd_service.hpp" +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace node +{ + +void registerAllServices(const ScvContext& context) +{ + ExecCmdService::registerWithContext(context); +} + +} // namespace node +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd diff --git a/src/daemon/engine/svc/node/services.hpp b/src/daemon/engine/svc/node/services.hpp new file mode 100644 index 0000000..32973ed --- /dev/null +++ b/src/daemon/engine/svc/node/services.hpp @@ -0,0 +1,31 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_DAEMON_ENGINE_SVC_NODE_SERVICES_HPP_INCLUDED +#define OCVSMD_DAEMON_ENGINE_SVC_NODE_SERVICES_HPP_INCLUDED + +#include "cyphal/file_provider.hpp" +#include "svc/svc_helpers.hpp" + +namespace ocvsmd +{ +namespace daemon +{ +namespace engine +{ +namespace svc +{ +namespace node +{ + +void registerAllServices(const ScvContext& context); + +} // namespace node +} // namespace svc +} // namespace engine +} // namespace daemon +} // namespace ocvsmd + +#endif // OCVSMD_DAEMON_ENGINE_SVC_NODE_SERVICES_HPP_INCLUDED diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index 5270786..d465308 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -22,6 +22,8 @@ add_library(ocvsmd_sdk node_command_client.cpp svc/node/exec_cmd_client.cpp svc/file_server/list_roots_client.cpp + svc/file_server/pop_root_client.cpp + svc/file_server/push_root_client.cpp ) target_link_libraries(ocvsmd_sdk PUBLIC ${sdk_transpiled} diff --git a/src/sdk/file_server.cpp b/src/sdk/file_server.cpp index ecf45f6..75a4e48 100644 --- a/src/sdk/file_server.cpp +++ b/src/sdk/file_server.cpp @@ -5,11 +5,17 @@ #include +#include "svc/as_sender.hpp" + #include "ipc/channel.hpp" #include "ipc/client_router.hpp" #include "logging.hpp" #include "sdk_factory.hpp" #include "svc/file_server/list_roots_client.hpp" +#include "svc/file_server/pop_root_client.hpp" +#include "svc/file_server/push_root_client.hpp" + +#include namespace ocvsmd { @@ -32,36 +38,69 @@ class FileServerImpl final : public FileServer SenderOf::Ptr listRoots() override { - const common::svc::file_server::ListRootsSpec::Request request{&memory_}; - auto svc_client = ListRootsClient::make(memory_, ipc_router_, request); + using ListRootsClient = svc::file_server::ListRootsClient; + using Request = common::svc::file_server::ListRootsSpec::Request; - return std::make_unique(std::move(svc_client)); - } + logger_->trace("FileServer: Making sender of `listRoots()`."); -private: - using ListRootsClient = svc::file_server::ListRootsClient; + const Request request{&memory_}; + auto svc_client = ListRootsClient::make(memory_, ipc_router_, request); - class ListRootsSender final : public SenderOf + return std::make_unique>( // + "FileServer::listRoots", + std::move(svc_client), + logger_); + } + + SenderOf::Ptr popRoot(const std::string& path, const bool back) override { - public: - explicit ListRootsSender(ListRootsClient::Ptr svc_client) - : svc_client_{std::move(svc_client)} + using PopRootClient = svc::file_server::PopRootClient; + using Request = common::svc::file_server::PopRootSpec::Request; + + logger_->trace("FileServer: Making sender of `popRoot(path='{}', back={})`.", path, back); + + if (path.size() > Request::_traits_::TypeOf::item::_traits_::ArrayCapacity::path) { + logger_->error("Too long path '{}'.", path); + return just(EINVAL); } - void submitImpl(std::function&& receiver) override + common::svc::file_server::PopRootSpec::Request request{&memory_}; + std::copy(path.cbegin(), path.cend(), std::back_inserter(request.item.path)); + request.is_back = back; + auto svc_client = PopRootClient::make(memory_, ipc_router_, request); + + return std::make_unique>( // + "FileServer::popRoot", + std::move(svc_client), + logger_); + } + + SenderOf::Ptr pushRoot(const std::string& path, const bool back) override + { + using PushRootClient = svc::file_server::PushRootClient; + using Request = common::svc::file_server::PushRootSpec::Request; + + logger_->trace("FileServer: Making sender of `pushRoot(path='{}', back={})`.", path, back); + + if (path.size() > Request::_traits_::TypeOf::item::_traits_::ArrayCapacity::path) { - svc_client_->submit([receiver = std::move(receiver)](ListRootsClient::Result&& result) mutable { - // - receiver(std::move(result)); - }); + logger_->error("Too long path '{}'.", path); + return just(EINVAL); } - private: - ListRootsClient::Ptr svc_client_; + Request request{&memory_}; + std::copy(path.cbegin(), path.cend(), std::back_inserter(request.item.path)); + request.is_back = back; + auto svc_client = PushRootClient::make(memory_, ipc_router_, request); - }; // ListRootsSender + return std::make_unique>( // + "FileServer::pushRoot", + std::move(svc_client), + logger_); + } +private: cetl::pmr::memory_resource& memory_; common::LoggerPtr logger_; common::ipc::ClientRouter::Ptr ipc_router_; diff --git a/src/sdk/svc/as_sender.hpp b/src/sdk/svc/as_sender.hpp new file mode 100644 index 0000000..9743d4c --- /dev/null +++ b/src/sdk/svc/as_sender.hpp @@ -0,0 +1,55 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_SVC_AS_SENDER_HPP_INCLUDED +#define OCVSMD_SDK_SVC_AS_SENDER_HPP_INCLUDED + +#include "logging.hpp" + +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ + +/// Adapter for an IPC service client to be used as a sender. +/// +template +class AsSender final : public SenderOf +{ +public: + AsSender(cetl::string_view op_name, typename SvcClient::Ptr&& svc_client, common::LoggerPtr logger) + : op_name_{op_name} + , svc_client_{std::forward(svc_client)} + , logger_{std::move(logger)} + { + } + + void submitImpl(std::function&& receiver) override + { + logger_->trace("Submitting `{}` operation.", op_name_); + + svc_client_->submit([this, receiver = std::move(receiver)](typename SvcClient::Result&& result) mutable { + // + logger_->trace("Received result of `{}` operation.", op_name_); + receiver(std::move(result)); + }); + } + +private: + cetl::string_view op_name_; + typename SvcClient::Ptr svc_client_; + common::LoggerPtr logger_; + +}; // AsSender + +} // namespace svc +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_SVC_AS_SENDER_HPP_INCLUDED diff --git a/src/sdk/svc/file_server/list_roots_client.hpp b/src/sdk/svc/file_server/list_roots_client.hpp index 9571ff8..5bcdeb7 100644 --- a/src/sdk/svc/file_server/list_roots_client.hpp +++ b/src/sdk/svc/file_server/list_roots_client.hpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include diff --git a/src/sdk/svc/file_server/pop_root_client.cpp b/src/sdk/svc/file_server/pop_root_client.cpp new file mode 100644 index 0000000..f2cc6de --- /dev/null +++ b/src/sdk/svc/file_server/pop_root_client.cpp @@ -0,0 +1,109 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "pop_root_client.hpp" + +#include "ipc/channel.hpp" +#include "ipc/client_router.hpp" +#include "ipc/ipc_types.hpp" +#include "logging.hpp" +#include "svc/file_server/pop_root_spec.hpp" + +#include +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace file_server +{ +namespace +{ + +class PopRootClientImpl final : public PopRootClient +{ +public: + PopRootClientImpl(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + const Spec::Request& request) + : memory_{memory} + , logger_{common::getLogger("svc")} + , request_{request} + , channel_{ipc_router->makeChannel(Spec::svc_full_name())} + { + } + + void submitImpl(std::function&& receiver) override + { + receiver_ = std::move(receiver); + + channel_.subscribe([this](const auto& event_var) { + // + cetl::visit([this](const auto& event) { handleEvent(event); }, event_var); + }); + } + +private: + using Channel = common::ipc::Channel; + + void handleEvent(const Channel::Connected& connected) + { + logger_->trace("PopRootClient::handleEvent({}).", connected); + + if (const auto err = channel_.send(request_)) + { + CETL_DEBUG_ASSERT(receiver_, ""); + + receiver_(Failure{err}); + } + } + + void handleEvent(const Channel::Input&) + { + logger_->trace("PopRootClient::handleEvent(Input)."); + } + + void handleEvent(const Channel::Completed& completed) + { + CETL_DEBUG_ASSERT(receiver_, ""); + + if (completed.error_code != common::ipc::ErrorCode::Success) + { + receiver_(static_cast(completed.error_code)); + return; + } + receiver_({}); + } + + cetl::pmr::memory_resource& memory_; + common::LoggerPtr logger_; + Spec::Request request_; + Channel channel_; + std::function receiver_; + +}; // PopRootClientImpl + +} // namespace + +CETL_NODISCARD PopRootClient::Ptr PopRootClient::make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + const Spec::Request& request) +{ + return std::make_shared(memory, ipc_router, request); +} + +} // namespace file_server +} // namespace svc +} // namespace sdk +} // namespace ocvsmd diff --git a/src/sdk/svc/file_server/pop_root_client.hpp b/src/sdk/svc/file_server/pop_root_client.hpp new file mode 100644 index 0000000..88e691b --- /dev/null +++ b/src/sdk/svc/file_server/pop_root_client.hpp @@ -0,0 +1,71 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_SVC_FILE_SERVER_POP_ROOT_CLIENT_HPP_INCLUDED +#define OCVSMD_SDK_SVC_FILE_SERVER_POP_ROOT_CLIENT_HPP_INCLUDED + +#include "ipc/client_router.hpp" +#include "svc/file_server/pop_root_spec.hpp" + +#include +#include + +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace file_server +{ + +class PopRootClient +{ +public: + using Ptr = std::shared_ptr; + using Spec = common::svc::file_server::PopRootSpec; + + using Success = cetl::monostate; + using Failure = int; // `errno`-like error code + using Result = cetl::variant; + + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + const Spec::Request& request); + + PopRootClient(PopRootClient&&) = delete; + PopRootClient(const PopRootClient&) = delete; + PopRootClient& operator=(PopRootClient&&) = delete; + PopRootClient& operator=(const PopRootClient&) = delete; + + virtual ~PopRootClient() = default; + + template + void submit(Receiver&& receiver) + { + submitImpl([receive = std::forward(receiver)](Result&& result) mutable { + // + receive(std::move(result)); + }); + } + +protected: + PopRootClient() = default; + + virtual void submitImpl(std::function&& receiver) = 0; + +}; // PopRootClient + +} // namespace file_server +} // namespace svc +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_SVC_FILE_SERVER_POP_ROOT_CLIENT_HPP_INCLUDED diff --git a/src/sdk/svc/file_server/push_root_client.cpp b/src/sdk/svc/file_server/push_root_client.cpp new file mode 100644 index 0000000..e62ee43 --- /dev/null +++ b/src/sdk/svc/file_server/push_root_client.cpp @@ -0,0 +1,109 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "push_root_client.hpp" + +#include "ipc/channel.hpp" +#include "ipc/client_router.hpp" +#include "ipc/ipc_types.hpp" +#include "logging.hpp" +#include "svc/file_server/push_root_spec.hpp" + +#include +#include + +#include +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace file_server +{ +namespace +{ + +class PushRootClientImpl final : public PushRootClient +{ +public: + PushRootClientImpl(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + const Spec::Request& request) + : memory_{memory} + , logger_{common::getLogger("svc")} + , request_{request} + , channel_{ipc_router->makeChannel(Spec::svc_full_name())} + { + } + + void submitImpl(std::function&& receiver) override + { + receiver_ = std::move(receiver); + + channel_.subscribe([this](const auto& event_var) { + // + cetl::visit([this](const auto& event) { handleEvent(event); }, event_var); + }); + } + +private: + using Channel = common::ipc::Channel; + + void handleEvent(const Channel::Connected& connected) + { + logger_->trace("PushRootClient::handleEvent({}).", connected); + + if (const auto err = channel_.send(request_)) + { + CETL_DEBUG_ASSERT(receiver_, ""); + + receiver_(Failure{err}); + } + } + + void handleEvent(const Channel::Input&) + { + logger_->trace("PushRootClient::handleEvent(Input)."); + } + + void handleEvent(const Channel::Completed& completed) + { + CETL_DEBUG_ASSERT(receiver_, ""); + + if (completed.error_code != common::ipc::ErrorCode::Success) + { + receiver_(static_cast(completed.error_code)); + return; + } + receiver_({}); + } + + cetl::pmr::memory_resource& memory_; + common::LoggerPtr logger_; + Spec::Request request_; + Channel channel_; + std::function receiver_; + +}; // PushRootClientImpl + +} // namespace + +CETL_NODISCARD PushRootClient::Ptr PushRootClient::make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + const Spec::Request& request) +{ + return std::make_shared(memory, ipc_router, request); +} + +} // namespace file_server +} // namespace svc +} // namespace sdk +} // namespace ocvsmd diff --git a/src/sdk/svc/file_server/push_root_client.hpp b/src/sdk/svc/file_server/push_root_client.hpp new file mode 100644 index 0000000..c8e1715 --- /dev/null +++ b/src/sdk/svc/file_server/push_root_client.hpp @@ -0,0 +1,71 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_SDK_SVC_FILE_SERVER_PUSH_ROOT_CLIENT_HPP_INCLUDED +#define OCVSMD_SDK_SVC_FILE_SERVER_PUSH_ROOT_CLIENT_HPP_INCLUDED + +#include "ipc/client_router.hpp" +#include "svc/file_server/push_root_spec.hpp" + +#include +#include + +#include +#include +#include +#include + +namespace ocvsmd +{ +namespace sdk +{ +namespace svc +{ +namespace file_server +{ + +class PushRootClient +{ +public: + using Ptr = std::shared_ptr; + using Spec = common::svc::file_server::PushRootSpec; + + using Success = cetl::monostate; + using Failure = int; // `errno`-like error code + using Result = cetl::variant; + + CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, + const common::ipc::ClientRouter::Ptr& ipc_router, + const Spec::Request& request); + + PushRootClient(PushRootClient&&) = delete; + PushRootClient(const PushRootClient&) = delete; + PushRootClient& operator=(PushRootClient&&) = delete; + PushRootClient& operator=(const PushRootClient&) = delete; + + virtual ~PushRootClient() = default; + + template + void submit(Receiver&& receiver) + { + submitImpl([receive = std::forward(receiver)](Result&& result) mutable { + // + receive(std::move(result)); + }); + } + +protected: + PushRootClient() = default; + + virtual void submitImpl(std::function&& receiver) = 0; + +}; // PushRootClient + +} // namespace file_server +} // namespace svc +} // namespace sdk +} // namespace ocvsmd + +#endif // OCVSMD_SDK_SVC_FILE_SERVER_PUSH_ROOT_CLIENT_HPP_INCLUDED From 09cd69fb9459a3126148a11ae7378630671aab67 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 17:37:12 +0200 Subject: [PATCH 131/156] fix style --- src/cli/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index 682a28c..cff7c00 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -120,7 +120,7 @@ int main(const int argc, const char** const argv) auto file_server = daemon->getFileServer(); - const std::string path{"/0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcd"}; + const std::string path{"key"}; auto sender = file_server->pushRoot(path, true); auto cmd_result = ocvsmd::sdk::sync_wait(executor, std::move(sender)); if (const auto* const err = cetl::get_if(&cmd_result)) From a8a5ae75f501703e974765a1362068e3f5148f8b Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 18:10:23 +0200 Subject: [PATCH 132/156] fix build --- src/sdk/svc/file_server/push_root_client.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sdk/svc/file_server/push_root_client.cpp b/src/sdk/svc/file_server/push_root_client.cpp index e62ee43..e499f18 100644 --- a/src/sdk/svc/file_server/push_root_client.cpp +++ b/src/sdk/svc/file_server/push_root_client.cpp @@ -36,10 +36,10 @@ class PushRootClientImpl final : public PushRootClient public: PushRootClientImpl(cetl::pmr::memory_resource& memory, const common::ipc::ClientRouter::Ptr& ipc_router, - const Spec::Request& request) + Spec::Request request) : memory_{memory} , logger_{common::getLogger("svc")} - , request_{request} + , request_{std::move(request)} , channel_{ipc_router->makeChannel(Spec::svc_full_name())} { } From 50f0254dc7deaf741d141ed00e80e8bcb6bb3c07 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 18:19:18 +0200 Subject: [PATCH 133/156] fix style --- src/sdk/svc/as_sender.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sdk/svc/as_sender.hpp b/src/sdk/svc/as_sender.hpp index 9743d4c..98c379a 100644 --- a/src/sdk/svc/as_sender.hpp +++ b/src/sdk/svc/as_sender.hpp @@ -25,8 +25,8 @@ class AsSender final : public SenderOf public: AsSender(cetl::string_view op_name, typename SvcClient::Ptr&& svc_client, common::LoggerPtr logger) : op_name_{op_name} - , svc_client_{std::forward(svc_client)} - , logger_{std::move(logger)} + , svc_client_{std::forward(svc_client)} + , logger_{std::move(logger)} { } @@ -52,4 +52,4 @@ class AsSender final : public SenderOf } // namespace sdk } // namespace ocvsmd -#endif // OCVSMD_SDK_SVC_AS_SENDER_HPP_INCLUDED +#endif // OCVSMD_SDK_SVC_AS_SENDER_HPP_INCLUDED From 0fc86e9c4dc99572834fb5a1c54f07703fcf9046 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 11 Feb 2025 18:46:19 +0200 Subject: [PATCH 134/156] fix build --- src/daemon/engine/cyphal/can_transport_bag.hpp | 2 +- src/daemon/engine/cyphal/udp_transport_bag.hpp | 2 +- src/sdk/svc/file_server/pop_root_client.cpp | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/daemon/engine/cyphal/can_transport_bag.hpp b/src/daemon/engine/cyphal/can_transport_bag.hpp index fc22b37..9923a79 100644 --- a/src/daemon/engine/cyphal/can_transport_bag.hpp +++ b/src/daemon/engine/cyphal/can_transport_bag.hpp @@ -64,7 +64,7 @@ class CanTransportBag final : public AnyTransportBag can_ifaces += ","; } } - common::getLogger("io")->trace("Attempting to create CAN transport (ifaces='{}')…", can_ifaces); + common::getLogger("io")->trace("Attempting to create CAN transport (ifaces=[{}])…", can_ifaces); auto transport_bag = std::make_unique(Spec{}, memory, executor); diff --git a/src/daemon/engine/cyphal/udp_transport_bag.hpp b/src/daemon/engine/cyphal/udp_transport_bag.hpp index 540e884..299fd61 100644 --- a/src/daemon/engine/cyphal/udp_transport_bag.hpp +++ b/src/daemon/engine/cyphal/udp_transport_bag.hpp @@ -66,7 +66,7 @@ class UdpTransportBag final : public AnyTransportBag udp_ifaces += ","; } } - common::getLogger("io")->trace("Attempting to create UDP transport (ifaces='{}')…", udp_ifaces); + common::getLogger("io")->trace("Attempting to create UDP transport (ifaces=[{}])…", udp_ifaces); auto transport_bag = std::make_unique(Spec{}, memory, executor); diff --git a/src/sdk/svc/file_server/pop_root_client.cpp b/src/sdk/svc/file_server/pop_root_client.cpp index f2cc6de..cef49b4 100644 --- a/src/sdk/svc/file_server/pop_root_client.cpp +++ b/src/sdk/svc/file_server/pop_root_client.cpp @@ -36,10 +36,10 @@ class PopRootClientImpl final : public PopRootClient public: PopRootClientImpl(cetl::pmr::memory_resource& memory, const common::ipc::ClientRouter::Ptr& ipc_router, - const Spec::Request& request) + Spec::Request request) : memory_{memory} , logger_{common::getLogger("svc")} - , request_{request} + , request_{std::move(request)} , channel_{ipc_router->makeChannel(Spec::svc_full_name())} { } From 66c11d44f182c3b1d7c922e1134927efd9eca8ee Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 12 Feb 2025 09:29:49 +0200 Subject: [PATCH 135/156] minor fix --- src/daemon/engine/svc/node/services.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/daemon/engine/svc/node/services.hpp b/src/daemon/engine/svc/node/services.hpp index 32973ed..37f6cdc 100644 --- a/src/daemon/engine/svc/node/services.hpp +++ b/src/daemon/engine/svc/node/services.hpp @@ -6,7 +6,6 @@ #ifndef OCVSMD_DAEMON_ENGINE_SVC_NODE_SERVICES_HPP_INCLUDED #define OCVSMD_DAEMON_ENGINE_SVC_NODE_SERVICES_HPP_INCLUDED -#include "cyphal/file_provider.hpp" #include "svc/svc_helpers.hpp" namespace ocvsmd From 98cd9130400f3aaa2ff2dcccb0422cb6e36bb52f Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Wed, 12 Feb 2025 15:05:20 +0200 Subject: [PATCH 136/156] more docs --- .../bsd/kqueue_single_threaded_executor.hpp | 2 +- include/ocvsmd/platform/defines.hpp | 9 ++-- .../linux/epoll_single_threaded_executor.hpp | 2 +- include/ocvsmd/platform/posix_utils.hpp | 2 + include/ocvsmd/sdk/daemon.hpp | 27 ++++++++++- include/ocvsmd/sdk/execution.hpp | 39 +++++++++++---- include/ocvsmd/sdk/file_server.hpp | 48 +++++++++++++++++-- include/ocvsmd/sdk/node_command_client.hpp | 34 +++++++++++-- .../engine/svc/file_server/services.hpp | 2 + .../engine/svc/node/exec_cmd_service.cpp | 20 +++++++- .../engine/svc/node/exec_cmd_service.hpp | 2 + src/daemon/engine/svc/node/services.hpp | 2 + 12 files changed, 163 insertions(+), 26 deletions(-) diff --git a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp index 82f6605..a59489d 100644 --- a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp @@ -40,7 +40,7 @@ namespace platform namespace bsd { -/// @brief Defines BSD Linux platform specific single-threaded executor based on `kqueue` mechanism. +/// @brief Defines BSD Linux platform-specific single-threaded executor based on `kqueue` mechanism. /// class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThreadedExecutor, public IPosixExecutorExtension diff --git a/include/ocvsmd/platform/defines.hpp b/include/ocvsmd/platform/defines.hpp index d0218f9..38ec371 100644 --- a/include/ocvsmd/platform/defines.hpp +++ b/include/ocvsmd/platform/defines.hpp @@ -31,6 +31,8 @@ using SingleThreadedExecutor = bsd::KqueueSingleThreadedExecutor; using SingleThreadedExecutor = Linux::EpollSingleThreadedExecutor; #endif +/// Waits for the predicate to be fulfilled by spinning the executor and its awaitable resources. +/// template void waitPollingUntil(Executor& executor, Predicate predicate) { @@ -55,13 +57,14 @@ void waitPollingUntil(Executor& executor, Predicate predicate) timeout = std::min(timeout, spin_result.next_exec_time.value() - executor.now()); } - if (const auto maybe_poll_failure = executor.pollAwaitableResourcesFor(cetl::make_optional(timeout))) + if (const auto poll_failure = executor.pollAwaitableResourcesFor(cetl::make_optional(timeout))) { - spdlog::warn("Failed to poll awaitable resources."); + (void) poll_failure; + spdlog::warn("Failed to poll awaitable resources."); // TODO: Log the error. } } - spdlog::debug("Predicate is fulfilled (worst_lateness={}us).", + spdlog::trace("Predicate is fulfilled (worst_lateness={}us).", std::chrono::duration_cast(worst_lateness).count()); } diff --git a/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp b/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp index 02ccbb9..fe9a525 100644 --- a/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp @@ -38,7 +38,7 @@ namespace platform namespace Linux { -/// @brief Defines Linux platform specific single-threaded executor based on `epoll` mechanism. +/// @brief Defines Linux platform-specific single-threaded executor based on `epoll` mechanism. /// class EpollSingleThreadedExecutor final : public libcyphal::platform::SingleThreadedExecutor, public IPosixExecutorExtension diff --git a/include/ocvsmd/platform/posix_utils.hpp b/include/ocvsmd/platform/posix_utils.hpp index ad28adb..d92ef94 100644 --- a/include/ocvsmd/platform/posix_utils.hpp +++ b/include/ocvsmd/platform/posix_utils.hpp @@ -13,6 +13,8 @@ namespace ocvsmd namespace platform { +/// Wraps a POSIX syscall and retries it if it was interrupted by a signal. +/// template int posixSyscallError(const Call& call) { diff --git a/include/ocvsmd/sdk/daemon.hpp b/include/ocvsmd/sdk/daemon.hpp index 968c285..ed5185b 100644 --- a/include/ocvsmd/sdk/daemon.hpp +++ b/include/ocvsmd/sdk/daemon.hpp @@ -26,12 +26,26 @@ namespace sdk class Daemon { public: + /// Defines the shared pointer type for the factory. + /// using Ptr = std::shared_ptr; + /// Creates a new instance of the factory, and establishes a connection to the daemon. + /// + /// @param memory The memory resource to use for the factory and its subcomponents. + /// The memory resource must outlive the factory. + /// In use for IPC (de)serialization only; other functionality uses usual c++ heap. + /// @param executor The executor to use for the factory and its subcomponents. + /// Instance of the executor must outlive the factory. + /// Should support `IPosixExecutorExtension` interface (via `cetl::rtti`). + /// @return Shared pointer to the successfully created factory. + /// `nullptr` on failure (see logs for the reason of failure). + /// CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, libcyphal::IExecutor& executor, const std::string& connection); + // No copy/move semantics. Daemon(Daemon&&) = delete; Daemon(const Daemon&) = delete; Daemon& operator=(Daemon&&) = delete; @@ -39,7 +53,18 @@ class Daemon virtual ~Daemon() = default; - virtual FileServer::Ptr getFileServer() const = 0; + /// Gets a pointer to the shared entity which represents the File Server component of the OCVSMD engine. + /// + /// @return Shared pointer to the client side of the File Server component. + /// The component is always present in the OCVSMD engine, so the result is never `nullptr`. + /// + virtual FileServer::Ptr getFileServer() const = 0; + + /// Gets a pointer to the shared entity which represents the Node Exec Command component of the OCVSMD engine. + /// + /// @return Shared pointer to the client side of the Node Exec Command component. + /// The component is always present in the OCVSMD engine, so the result is never `nullptr`. + /// virtual NodeCommandClient::Ptr getNodeCommandClient() const = 0; protected: diff --git a/include/ocvsmd/sdk/execution.hpp b/include/ocvsmd/sdk/execution.hpp index 32ed16a..f49484c 100644 --- a/include/ocvsmd/sdk/execution.hpp +++ b/include/ocvsmd/sdk/execution.hpp @@ -24,9 +24,11 @@ namespace sdk namespace detail { +// Defines various internal types for `sync_wait` implementation. +// template class StateOf; - +// template class ReceiverOf final { @@ -46,7 +48,7 @@ class ReceiverOf final std::shared_ptr> state_; }; // ReceiverOf - +// template class StateOf final : public std::enable_shared_from_this> { @@ -78,13 +80,23 @@ class StateOf final : public std::enable_shared_from_this> /// Abstract interface of a result sender. /// +/// There is no (at least for now) support of async failures via C++ exceptions in the SDK. +/// So, if an async sender might fail, then the `Result` subtype +/// is expected to cover both success and failure cases (f.e. using `variant`). +/// template class SenderOf { public: - using Ptr = std::unique_ptr; + /// Defines the shared pointer type for the interface. + /// + using Ptr = std::unique_ptr; + + /// Defines the result type of the sender. + /// using Result = Result_; + // No copy/move semantics. SenderOf(SenderOf&&) = delete; SenderOf(const SenderOf&) = delete; SenderOf& operator=(SenderOf&&) = delete; @@ -94,7 +106,9 @@ class SenderOf /// Initiates an operation execution by submitting a given receiver to this sender. /// - /// The submit "consumes" the receiver (no longer usable after this call). + /// @tparam Receiver The type of the receiver functor. Should be callable with the result of the sender. + /// @param receiver The receiver of the sender's result. + /// Method "consumes" the receiver (no longer usable after this call). /// template void submit(Receiver&& receiver) @@ -108,20 +122,27 @@ class SenderOf protected: SenderOf() = default; + /// Implementation extension point for the derived classes. + /// virtual void submitImpl(std::function&& receiver) = 0; }; // SenderOf /// A sender factory that returns a sender which completes immediately -/// by calling the receiver `operator()` with the given pack of arguments. +/// by calling the receiver `operator()` with a `Result` type instance built using the given pack of arguments. +/// +/// @param args The arguments to construct the result of the sender. +/// The result is constructed immediately (during this `just` call), +/// and later (on `SenderOf::submit`) is `move`-d to the receiver. /// template typename SenderOf::Ptr just(Args&&... args) { - class Sender final : public SenderOf + // Private implementation of the sender. + class JustSender final : public SenderOf { public: - explicit Sender(Args&&... args) + explicit JustSender(Args&&... args) : result_{std::forward(args)...} { } @@ -136,9 +157,9 @@ typename SenderOf::Ptr just(Args&&... args) Result result_; - }; // Sender + }; // JustSender - return std::make_unique(std::forward(args)...); + return std::make_unique(std::forward(args)...); } /// Initiates an operation execution by submitting a given receiver to the sender. diff --git a/include/ocvsmd/sdk/file_server.hpp b/include/ocvsmd/sdk/file_server.hpp index d486201..c370d48 100644 --- a/include/ocvsmd/sdk/file_server.hpp +++ b/include/ocvsmd/sdk/file_server.hpp @@ -20,11 +20,16 @@ namespace ocvsmd namespace sdk { +/// Defines client side interface of the OCVSMD File Server component. +/// class FileServer { public: + /// Defines the shared pointer type for the interface. + /// using Ptr = std::shared_ptr; + // No copy/move semantics. FileServer(FileServer&&) = delete; FileServer(const FileServer&) = delete; FileServer& operator=(FileServer&&) = delete; @@ -32,28 +37,61 @@ class FileServer virtual ~FileServer() = default; + /// Makes async sender which emits a current list of the File Server root paths. + /// + /// @return An execution sender which emits the async result of the operation. + /// The returned paths are the same values as they were added by `pushRoot`. + /// The entries are not unique, and the order is preserved. + /// struct ListRoots final { using Success = std::vector; - using Failure = int; // `errno`-like error code. + using Failure = int; // `errno`-like error code using Result = cetl::variant; }; virtual SenderOf::Ptr listRoots() = 0; + /// Does nothing if such root path does not exist (no error reported). + /// If such a path is listed more than once, only one copy is removed. + /// The `back` flag determines whether the path is searched from the front or the back of the list. + /// The flag has no effect if there are no duplicates. + /// The changed list of paths is persisted by the daemon (in its configuration; on its exit), + /// so the list will be automatically restored on the next daemon start. + /// struct PopRoot final { - using Success = cetl::monostate; - using Failure = int; // `errno`-like error code. + using Success = cetl::monostate; // like `void` + using Failure = int; // `errno`-like error code using Result = cetl::variant; }; + /// Removes a root directory from the list of directories that the file server will serve. + /// + /// @return An execution sender which emits the async result of the operation. + /// virtual SenderOf::Ptr popRoot(const std::string& path, const bool back) = 0; + /// When the file server handles a request, it will attempt to locate the path relative to each of its root + /// directories. See Yakut for a hands-on example. + /// The `path` could be a relative or an absolute path. In case of a relative path + /// (without leading `/`), the daemon will resolve it against its current working directory. + /// The daemon will internally canonicalize the path and resolve symlinks, + /// and use real the file system path when matching and serving Cyphal network 'File' requests. + /// The same path may be added multiple times to avoid interference across different clients. + /// Currently, the path should be a directory (later we might support a direct file as well). + /// The `back` flag determines whether the path is added to the front or the back of the list. + /// The changed list of paths is persisted by the daemon (in its configuration; on its exit), + /// so the list will be automatically restored on the next daemon start. + /// struct PushRoot final { - using Success = cetl::monostate; - using Failure = int; // `errno`-like error code. + using Success = cetl::monostate; // like `void` + using Failure = int; // `errno`-like error code using Result = cetl::variant; }; + /// Adds a new root directory to the list of directories that the file server will serve. + /// + /// @return An execution sender which emits the async result of the operation. + /// virtual SenderOf::Ptr pushRoot(const std::string& path, const bool back) = 0; protected: diff --git a/include/ocvsmd/sdk/node_command_client.hpp b/include/ocvsmd/sdk/node_command_client.hpp index 8d071bb..6b7824a 100644 --- a/include/ocvsmd/sdk/node_command_client.hpp +++ b/include/ocvsmd/sdk/node_command_client.hpp @@ -23,9 +23,13 @@ namespace ocvsmd namespace sdk { +/// Defines client side interface of the OCVSMD Node Exec Command component. +/// class NodeCommandClient { public: + /// Defines the shared pointer type for the interface. + /// using Ptr = std::shared_ptr; NodeCommandClient(NodeCommandClient&&) = delete; @@ -35,6 +39,11 @@ class NodeCommandClient virtual ~NodeCommandClient() = default; + /// Defines the result type of the command execution. + /// + /// On success, the result is a map of node IDs to their responses (`status` and `output` params). + /// Missing Cyphal nodes (or failed to respond in a given timeout) are not included in the map. + /// struct Command final { using NodeRequest = uavcan::node::ExecuteCommand_1_3::Request; @@ -46,10 +55,16 @@ class NodeCommandClient }; // Command - /// Request is sent concurrently to all nodes. - /// Duplicate node IDs are ignored. - /// Result will be available when the last response has arrived, - /// or the timeout has expired. + /// Sends a Cyphal command to the specified Cyphal network nodes. + /// + /// On the OCVSMD engine side, the `node_request` is sent concurrently to all specified Cyphal nodes. + /// Responses are sent back to the client side as they arrive. + /// Result will be available when the last response has arrived, or the timeout has expired. + /// + /// @param node_ids The list of Cyphal node IDs to send the command to. Duplicates are ignored. + /// @param node_request The Cyphal command request to send (aka broadcast) to the `node_ids`. + /// @param timeout The maximum time to wait for all Cyphal node responses to arrive. + /// @return An execution sender which emits the async overall result of the operation. /// virtual SenderOf::Ptr sendCommand(const cetl::span node_ids, const Command::NodeRequest& node_request, @@ -57,12 +72,21 @@ class NodeCommandClient /// A convenience method for invoking `sendCommand` with COMMAND_RESTART. /// + /// @param node_ids The list of Cyphal node IDs to send the command to. Duplicates are ignored. + /// @param timeout The maximum time to wait for all Cyphal node responses to arrive. Default is 1 second. + /// @return An execution sender which emits the async result of the operation. + /// SenderOf::Ptr restart( // const cetl::span node_ids, const std::chrono::microseconds timeout = std::chrono::seconds{1}); /// A convenience method for invoking `sendCommand` with COMMAND_BEGIN_SOFTWARE_UPDATE. - /// The file_path is relative to one of the roots configured in the file server. + /// + /// @param node_ids The list of Cyphal node IDs to send the command to. Duplicates are ignored. + /// @param file_path The path to the software update file. Limited to 255 characters. + /// Relative to one of the roots configured in the file server. + /// @param timeout The maximum time to wait for all Cyphal node responses to arrive. Default is 1 second. + /// @return An execution sender which emits the async result of the operation. /// SenderOf::Ptr beginSoftwareUpdate( // const cetl::span node_ids, diff --git a/src/daemon/engine/svc/file_server/services.hpp b/src/daemon/engine/svc/file_server/services.hpp index 588fcb7..13ab806 100644 --- a/src/daemon/engine/svc/file_server/services.hpp +++ b/src/daemon/engine/svc/file_server/services.hpp @@ -20,6 +20,8 @@ namespace svc namespace file_server { +/// Registers all "file_server"-related services. +/// void registerAllServices(const ScvContext& context, cyphal::FileProvider& file_provider); } // namespace file_server diff --git a/src/daemon/engine/svc/node/exec_cmd_service.cpp b/src/daemon/engine/svc/node/exec_cmd_service.cpp index 52676ae..284f598 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.cpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.cpp @@ -39,6 +39,11 @@ namespace node namespace { +/// Defines 'Execute Command' service implementation. +/// +/// It's passed (as a functor) to the IPC server router to handle incoming service requests. +/// See `ipc::ServerRouter::registerChannel` for details, and below `operator()` for the actual implementation. +/// class ExecCmdServiceImpl final { public: @@ -50,6 +55,10 @@ class ExecCmdServiceImpl final { } + /// Handles the initial `node::ExecCmd` service request of a new IPC channel. + /// + /// Defined as a functor operator - as it's required/expected by the IPC server router. + /// void operator()(Channel&& channel, const Spec::Request& request) { const auto fsm_id = next_fsm_id_++; @@ -62,7 +71,16 @@ class ExecCmdServiceImpl final } private: - class Fsm // Finite State Machine + // Defines private Finite State Machine (FSM) which tracks the progress of a single service request. + // There is one FSM per each service request channel. + // + // 1. On its `start` a set of Cyphal RPC clients is created (one per each node ID in the request), + // and a command request is sent to each of them. + // 2. Then, the FSM waits for the RPC responses from the Cyphal nodes, collecting them. + // 3. Finally, FSM sends the accumulated result (when all responses are received or timed out) + // and completes the channel. + // + class Fsm final { public: using Id = std::uint64_t; diff --git a/src/daemon/engine/svc/node/exec_cmd_service.hpp b/src/daemon/engine/svc/node/exec_cmd_service.hpp index 89788f0..d55b608 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.hpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.hpp @@ -19,6 +19,8 @@ namespace svc namespace node { +/// Defines registration factory of an 'Exec Command' service. +/// class ExecCmdService { public: diff --git a/src/daemon/engine/svc/node/services.hpp b/src/daemon/engine/svc/node/services.hpp index 37f6cdc..d520536 100644 --- a/src/daemon/engine/svc/node/services.hpp +++ b/src/daemon/engine/svc/node/services.hpp @@ -19,6 +19,8 @@ namespace svc namespace node { +/// Registers all "node"-related services. +/// void registerAllServices(const ScvContext& context); } // namespace node From 6f8886f5dc889d8e4ca17983fa785ab69b382db2 Mon Sep 17 00:00:00 2001 From: Sergei Date: Wed, 12 Feb 2025 18:59:51 +0200 Subject: [PATCH 137/156] more docs --- src/common/ipc/channel.hpp | 18 +++++++++++++++++- src/common/ipc/client_router.cpp | 11 +++++++++++ src/common/ipc/client_router.hpp | 3 +++ src/common/ipc/gateway.hpp | 6 ++++++ src/common/ipc/server_router.cpp | 17 ++++++++++++++--- src/common/ipc/server_router.hpp | 3 +++ .../svc/file_server/list_roots_service.cpp | 18 +++++++++++++++++- .../svc/file_server/list_roots_service.hpp | 2 ++ .../svc/file_server/pop_root_service.cpp | 15 ++++++++++++++- .../svc/file_server/pop_root_service.hpp | 2 ++ .../svc/file_server/push_root_service.cpp | 15 ++++++++++++++- .../svc/file_server/push_root_service.hpp | 2 ++ .../engine/svc/node/exec_cmd_service.cpp | 3 ++- .../engine/svc/node/exec_cmd_service.hpp | 2 +- src/daemon/engine/svc/svc_helpers.hpp | 4 ++++ src/sdk/node_command_client.cpp | 3 --- src/sdk/svc/file_server/list_roots_client.cpp | 2 ++ src/sdk/svc/file_server/list_roots_client.hpp | 2 ++ src/sdk/svc/file_server/pop_root_client.cpp | 2 ++ src/sdk/svc/file_server/pop_root_client.hpp | 2 ++ src/sdk/svc/file_server/push_root_client.cpp | 2 ++ src/sdk/svc/file_server/push_root_client.hpp | 2 ++ src/sdk/svc/node/exec_cmd_client.hpp | 2 ++ 23 files changed, 126 insertions(+), 12 deletions(-) diff --git a/src/common/ipc/channel.hpp b/src/common/ipc/channel.hpp index f970d6c..aa353d1 100644 --- a/src/common/ipc/channel.hpp +++ b/src/common/ipc/channel.hpp @@ -59,6 +59,20 @@ class AnyChannel }; // AnyChannel +/// Defines an abstract bidirectional communication channel. +/// +/// Supports sending of messages (plural!) of type `Output`, +/// and receiving messages of type `Input` via `EventHandler` callback. +/// +/// Channel could be opened permanently (f.e. for receiving some kind of notifications, like status updates), +/// but usually it represents one RPC session, which could be completed (finished) with an optional error code. +/// Such completion normally done at server side (when RPC request has been fulfilled), +/// but client could also complete the channel. Any unexpected IPC communication error (like f.e. sudden death of +/// either client or server process) also leads to channel completion (with `ipc::ErrorCode::Disconnected` error). +/// +/// Channel could be moved, but not copied. +/// Channel lifetime is managed by its owner - an IPC service client or server. +/// template class Channel final : public AnyChannel { @@ -69,10 +83,12 @@ class Channel final : public AnyChannel using EventVar = cetl::variant; using EventHandler = std::function; + // Move-only. ~Channel() = default; Channel(Channel&& other) noexcept = default; Channel& operator=(Channel&& other) noexcept = default; + // No copy. Channel(const Channel&) = delete; Channel& operator=(const Channel&) = delete; @@ -89,7 +105,7 @@ class Channel final : public AnyChannel }); } - void complete(const int error_code) + void complete(const int error_code = 0) { return gateway_->complete(error_code); } diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index dd155c2..d546dc8 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -38,6 +38,11 @@ namespace ipc namespace { +/// Defines implementation of the IPC client-side router. +/// +/// It subscribes to the client pipe events and dispatches them to corresponding target gateway (by matching tags). +/// In case of the pipe disconnection, it is broadcast-ed to all currently existing gateways (aka channels). +/// class ClientRouterImpl final : public ClientRouter { public: @@ -88,6 +93,11 @@ class ClientRouterImpl final : public ClientRouter const Tag tag; }; + /// Defines private IPC gateway entity of this client-side IPC router. + /// + /// Gateway is a glue between this IPC router and a service channel. + /// Lifetime of a gateway is exactly the same as the lifetime of the associated service channel. + /// class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway { struct Private @@ -180,6 +190,7 @@ class ClientRouterImpl final : public ClientRouter }; // GatewayImpl + // Lifetime of a gateway is strictly managed by its channel. But router needs to "weakly" keep track of them. using MapOfWeakGateways = std::unordered_map; CETL_NODISCARD bool isConnected(const Endpoint&) const noexcept diff --git a/src/common/ipc/client_router.hpp b/src/common/ipc/client_router.hpp index fd604a2..a71e9e1 100644 --- a/src/common/ipc/client_router.hpp +++ b/src/common/ipc/client_router.hpp @@ -22,6 +22,8 @@ namespace common namespace ipc { +/// Defines interface of the IPC client-side router. +/// class ClientRouter { public: @@ -29,6 +31,7 @@ class ClientRouter CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, pipe::ClientPipe::Ptr client_pipe); + // No copy/move. ClientRouter(const ClientRouter&) = delete; ClientRouter(ClientRouter&&) noexcept = delete; ClientRouter& operator=(const ClientRouter&) = delete; diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index c2e59ed..72cc1b8 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -35,6 +35,11 @@ struct ServiceDesc }; // ServiceDesc +/// Defines internal interface for the IPC gateway. +/// +/// Gateway is a glue between the IPC router and a service channel. +/// Lifetime of a gateway is exactly the same as the lifetime of the associated service channel. +/// class Gateway { public: @@ -61,6 +66,7 @@ class Gateway using EventHandler = std::function; + // No copying or moving. Gateway(const Gateway&) = delete; Gateway(Gateway&&) noexcept = delete; Gateway& operator=(const Gateway&) = delete; diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index ae589b1..670de6d 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -36,6 +36,11 @@ namespace ipc namespace { +/// Defines implementation of the IPC server-side router. +/// +/// It subscribes to the server pipe events and dispatches them to the registered channel factories. +/// In case of the pipe disconnection, it is broadcast-ed to all currently existing gateways (aka channels). +/// class ServerRouterImpl final : public ServerRouter { public: @@ -84,6 +89,11 @@ class ServerRouterImpl final : public ServerRouter const ClientId client_id; }; + /// Defines private IPC gateway entity of this server-side IPC router. + /// + /// Gateway is a glue between this IPC router and a service channel. + /// Lifetime of a gateway is exactly the same as the lifetime of the associated service channel. + /// class GatewayImpl final : public std::enable_shared_from_this, public detail::Gateway { struct Private @@ -177,9 +187,10 @@ class ServerRouterImpl final : public ServerRouter }; // GatewayImpl - using ServiceIdToChannelFactory = std::unordered_map; + // Lifetime of a gateway is strictly managed by its channel. But router needs to "weakly" keep track of them. using MapOfWeakGateways = std::unordered_map; using ClientIdToMapOfGateways = std::unordered_map; + using ServiceIdToChannelFactory = std::unordered_map; CETL_NODISCARD bool isConnected(const Endpoint& endpoint) const noexcept { @@ -220,7 +231,7 @@ class ServerRouterImpl final : public ServerRouter return 0; } - void onGatewaySubscription(const Endpoint endpoint) + void onGatewaySubscription(const Endpoint endpoint) const { if (isConnected(endpoint)) { @@ -266,7 +277,7 @@ class ServerRouterImpl final : public ServerRouter } } - CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Connected& pipe_conn) + CETL_NODISCARD int handlePipeEvent(const pipe::ServerPipe::Event::Connected& pipe_conn) const { logger_->debug("Pipe is connected (cl={}).", pipe_conn.client_id); diff --git a/src/common/ipc/server_router.hpp b/src/common/ipc/server_router.hpp index 667aa5e..07965ab 100644 --- a/src/common/ipc/server_router.hpp +++ b/src/common/ipc/server_router.hpp @@ -24,6 +24,8 @@ namespace common namespace ipc { +/// Defines interface of the IPC server-side router. +/// class ServerRouter { public: @@ -31,6 +33,7 @@ class ServerRouter CETL_NODISCARD static Ptr make(cetl::pmr::memory_resource& memory, pipe::ServerPipe::Ptr server_pipe); + // No copy/move. ServerRouter(const ServerRouter&) = delete; ServerRouter(ServerRouter&&) noexcept = delete; ServerRouter& operator=(const ServerRouter&) = delete; diff --git a/src/daemon/engine/svc/file_server/list_roots_service.cpp b/src/daemon/engine/svc/file_server/list_roots_service.cpp index 0ca8edd..901b116 100644 --- a/src/daemon/engine/svc/file_server/list_roots_service.cpp +++ b/src/daemon/engine/svc/file_server/list_roots_service.cpp @@ -29,6 +29,11 @@ namespace file_server namespace { +/// Defines 'File Server: List Roots' service implementation. +/// +/// It's passed (as a functor) to the IPC server router to handle incoming service requests. +/// See `ipc::ServerRouter::registerChannel` for details, and below `operator()` for the actual implementation. +/// class ListRootsServiceImpl final { public: @@ -41,6 +46,13 @@ class ListRootsServiceImpl final { } + /// Handles the `file_server::ListRoots` service request of a new IPC channel. + /// + /// The service itself is stateless (the state is stored inside the given file provider), has no async operations, + /// sends multiple responses (per each root), and then completes the channel immediately. + /// + /// Defined as a functor operator - as it's required/expected by the IPC server router. + /// void operator()(Channel channel, const Spec::Request&) const { logger_->debug("New '{}' service channel.", Spec::svc_full_name()); @@ -49,6 +61,9 @@ class ListRootsServiceImpl final const auto& roots = file_provider_.getListOfRoots(); for (const auto& root : roots) { + // Check if a root path is too long. Such paths could be read from the configuration file - + // they will work fine in terms of the file system, but we can't send them over IPC. + // constexpr auto MaxRootLen = Spec::Response::_traits_::TypeOf::item::_traits_::ArrayCapacity::path; if (root.size() > MaxRootLen) { @@ -64,7 +79,7 @@ class ListRootsServiceImpl final } } - channel.complete(0); + channel.complete(); } private: @@ -79,6 +94,7 @@ class ListRootsServiceImpl final void ListRootsService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) { using Impl = ListRootsServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context, file_provider}); } diff --git a/src/daemon/engine/svc/file_server/list_roots_service.hpp b/src/daemon/engine/svc/file_server/list_roots_service.hpp index e253db9..a04e7db 100644 --- a/src/daemon/engine/svc/file_server/list_roots_service.hpp +++ b/src/daemon/engine/svc/file_server/list_roots_service.hpp @@ -20,6 +20,8 @@ namespace svc namespace file_server { +/// Defines registration factory of the 'File Server: List Roots' service. +/// class ListRootsService { public: diff --git a/src/daemon/engine/svc/file_server/pop_root_service.cpp b/src/daemon/engine/svc/file_server/pop_root_service.cpp index ad6cdb9..a03afa6 100644 --- a/src/daemon/engine/svc/file_server/pop_root_service.cpp +++ b/src/daemon/engine/svc/file_server/pop_root_service.cpp @@ -29,6 +29,11 @@ namespace file_server namespace { +/// Defines 'File Server: Pop Root' service implementation. +/// +/// It's passed (as a functor) to the IPC server router to handle incoming service requests. +/// See `ipc::ServerRouter::registerChannel` for details, and below `operator()` for the actual implementation. +/// class PopRootServiceImpl final { public: @@ -41,6 +46,13 @@ class PopRootServiceImpl final { } + /// Handles the initial `file_server::PopRoot` service request of a new IPC channel. + /// + /// The service itself is stateless (the state is stored inside the given file provider), + /// has no async operations, and completes the channel immediately. + /// + /// Defined as a functor operator - as it's required/expected by the IPC server router. + /// void operator()(Channel channel, const Spec::Request& request) const { logger_->debug("New '{}' service channel.", Spec::svc_full_name()); @@ -48,7 +60,7 @@ class PopRootServiceImpl final std::string path; std::copy(request.item.path.cbegin(), request.item.path.cend(), std::back_inserter(path)); file_provider_.popRoot(path, request.is_back); - channel.complete(0); + channel.complete(); } private: @@ -63,6 +75,7 @@ class PopRootServiceImpl final void PopRootService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) { using Impl = PopRootServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context, file_provider}); } diff --git a/src/daemon/engine/svc/file_server/pop_root_service.hpp b/src/daemon/engine/svc/file_server/pop_root_service.hpp index 437acc4..8a51174 100644 --- a/src/daemon/engine/svc/file_server/pop_root_service.hpp +++ b/src/daemon/engine/svc/file_server/pop_root_service.hpp @@ -20,6 +20,8 @@ namespace svc namespace file_server { +/// Defines registration factory of the 'File Server: Pop Root' service. +/// class PopRootService { public: diff --git a/src/daemon/engine/svc/file_server/push_root_service.cpp b/src/daemon/engine/svc/file_server/push_root_service.cpp index 887d923..bd29fc2 100644 --- a/src/daemon/engine/svc/file_server/push_root_service.cpp +++ b/src/daemon/engine/svc/file_server/push_root_service.cpp @@ -29,6 +29,11 @@ namespace file_server namespace { +/// Defines 'File Server: Push Root' service implementation. +/// +/// It's passed (as a functor) to the IPC server router to handle incoming service requests. +/// See `ipc::ServerRouter::registerChannel` for details, and below `operator()` for the actual implementation. +/// class PushRootServiceImpl final { public: @@ -41,6 +46,13 @@ class PushRootServiceImpl final { } + /// Handles the initial `file_server::PushRoot` service request of a new IPC channel. + /// + /// The service itself is stateless (the state is stored inside the given file provider), + /// has no async operations, and completes the channel immediately. + /// + /// Defined as a functor operator - as it's required/expected by the IPC server router. + /// void operator()(Channel channel, const Spec::Request& request) const { logger_->debug("New '{}' service channel.", Spec::svc_full_name()); @@ -48,7 +60,7 @@ class PushRootServiceImpl final std::string path; std::copy(request.item.path.cbegin(), request.item.path.cend(), std::back_inserter(path)); file_provider_.pushRoot(path, request.is_back); - channel.complete(0); + channel.complete(); } private: @@ -63,6 +75,7 @@ class PushRootServiceImpl final void PushRootService::registerWithContext(const ScvContext& context, cyphal::FileProvider& file_provider) { using Impl = PushRootServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context, file_provider}); } diff --git a/src/daemon/engine/svc/file_server/push_root_service.hpp b/src/daemon/engine/svc/file_server/push_root_service.hpp index 4508a40..ae73506 100644 --- a/src/daemon/engine/svc/file_server/push_root_service.hpp +++ b/src/daemon/engine/svc/file_server/push_root_service.hpp @@ -20,6 +20,8 @@ namespace svc namespace file_server { +/// Defines registration factory of the 'File Server: Push Root' service. +/// class PushRootService { public: diff --git a/src/daemon/engine/svc/node/exec_cmd_service.cpp b/src/daemon/engine/svc/node/exec_cmd_service.cpp index 284f598..c9f51c9 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.cpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.cpp @@ -39,7 +39,7 @@ namespace node namespace { -/// Defines 'Execute Command' service implementation. +/// Defines 'Node: Execute Command' service implementation. /// /// It's passed (as a functor) to the IPC server router to handle incoming service requests. /// See `ipc::ServerRouter::registerChannel` for details, and below `operator()` for the actual implementation. @@ -273,6 +273,7 @@ class ExecCmdServiceImpl final void ExecCmdService::registerWithContext(const ScvContext& context) { using Impl = ExecCmdServiceImpl; + context.ipc_router.registerChannel(Impl::Spec::svc_full_name(), Impl{context}); } diff --git a/src/daemon/engine/svc/node/exec_cmd_service.hpp b/src/daemon/engine/svc/node/exec_cmd_service.hpp index d55b608..813cc1e 100644 --- a/src/daemon/engine/svc/node/exec_cmd_service.hpp +++ b/src/daemon/engine/svc/node/exec_cmd_service.hpp @@ -19,7 +19,7 @@ namespace svc namespace node { -/// Defines registration factory of an 'Exec Command' service. +/// Defines registration factory of the 'Node: Exec Command' service. /// class ExecCmdService { diff --git a/src/daemon/engine/svc/svc_helpers.hpp b/src/daemon/engine/svc/svc_helpers.hpp index 3ba09d6..95bb077 100644 --- a/src/daemon/engine/svc/svc_helpers.hpp +++ b/src/daemon/engine/svc/svc_helpers.hpp @@ -21,6 +21,10 @@ namespace engine namespace svc { +/// Defines common context for all server-side services. +/// +/// Contains references to the core engine components. Most (if not all) services require these. +/// struct ScvContext { cetl::pmr::memory_resource& memory; diff --git a/src/sdk/node_command_client.cpp b/src/sdk/node_command_client.cpp index 85c63fe..fbd76d5 100644 --- a/src/sdk/node_command_client.cpp +++ b/src/sdk/node_command_client.cpp @@ -129,9 +129,6 @@ SenderOf::Ptr NodeCommandClient::restart( / return sendCommand(node_ids, node_request, timeout); } -/// A convenience method for invoking `sendCommand` with COMMAND_BEGIN_SOFTWARE_UPDATE. -/// The file_path is relative to one of the roots configured in the file server. -/// SenderOf::Ptr NodeCommandClient::beginSoftwareUpdate( // const cetl::span node_ids, const cetl::string_view file_path, diff --git a/src/sdk/svc/file_server/list_roots_client.cpp b/src/sdk/svc/file_server/list_roots_client.cpp index 7844c06..ff6f5a4 100644 --- a/src/sdk/svc/file_server/list_roots_client.cpp +++ b/src/sdk/svc/file_server/list_roots_client.cpp @@ -32,6 +32,8 @@ namespace file_server namespace { +/// Defines implementation of the 'File Server: List Roots' service client. +/// class ListRootsClientImpl final : public ListRootsClient { public: diff --git a/src/sdk/svc/file_server/list_roots_client.hpp b/src/sdk/svc/file_server/list_roots_client.hpp index 5bcdeb7..705ae36 100644 --- a/src/sdk/svc/file_server/list_roots_client.hpp +++ b/src/sdk/svc/file_server/list_roots_client.hpp @@ -28,6 +28,8 @@ namespace svc namespace file_server { +/// Defines interface of the 'File Server: List Roots' service client. +/// class ListRootsClient { public: diff --git a/src/sdk/svc/file_server/pop_root_client.cpp b/src/sdk/svc/file_server/pop_root_client.cpp index cef49b4..ee3c26c 100644 --- a/src/sdk/svc/file_server/pop_root_client.cpp +++ b/src/sdk/svc/file_server/pop_root_client.cpp @@ -31,6 +31,8 @@ namespace file_server namespace { +/// Defines implementation of the 'File Server: Pop Root' service client. +/// class PopRootClientImpl final : public PopRootClient { public: diff --git a/src/sdk/svc/file_server/pop_root_client.hpp b/src/sdk/svc/file_server/pop_root_client.hpp index 88e691b..e127142 100644 --- a/src/sdk/svc/file_server/pop_root_client.hpp +++ b/src/sdk/svc/file_server/pop_root_client.hpp @@ -26,6 +26,8 @@ namespace svc namespace file_server { +/// Defines interface of the 'File Server: Pop Root' service client. +/// class PopRootClient { public: diff --git a/src/sdk/svc/file_server/push_root_client.cpp b/src/sdk/svc/file_server/push_root_client.cpp index e499f18..ecccd02 100644 --- a/src/sdk/svc/file_server/push_root_client.cpp +++ b/src/sdk/svc/file_server/push_root_client.cpp @@ -31,6 +31,8 @@ namespace file_server namespace { +/// Defines implementation of the 'File Server: Push Root' service client. +/// class PushRootClientImpl final : public PushRootClient { public: diff --git a/src/sdk/svc/file_server/push_root_client.hpp b/src/sdk/svc/file_server/push_root_client.hpp index c8e1715..8d81283 100644 --- a/src/sdk/svc/file_server/push_root_client.hpp +++ b/src/sdk/svc/file_server/push_root_client.hpp @@ -26,6 +26,8 @@ namespace svc namespace file_server { +/// Defines interface of the 'File Server: Push Root' service client. +/// class PushRootClient { public: diff --git a/src/sdk/svc/node/exec_cmd_client.hpp b/src/sdk/svc/node/exec_cmd_client.hpp index 1166afe..6b43048 100644 --- a/src/sdk/svc/node/exec_cmd_client.hpp +++ b/src/sdk/svc/node/exec_cmd_client.hpp @@ -28,6 +28,8 @@ namespace svc namespace node { +/// Defines interface of the 'Node: Exec Command' service client. +/// class ExecCmdClient { public: From 579222c13e3f36c174e169600b989376732b0fba Mon Sep 17 00:00:00 2001 From: Sergei Date: Thu, 13 Feb 2025 03:47:20 +0200 Subject: [PATCH 138/156] added unit tests for `ExecCmdServiceImpl` service. --- src/common/ipc/gateway.hpp | 2 +- test/common/ipc/gateway_mock.hpp | 72 +++ test/common/ipc/ipc_gtest_helpers.hpp | 101 +++- test/common/ipc/pipe/client_pipe_mock.hpp | 8 +- test/common/ipc/pipe/server_pipe_mock.hpp | 8 +- test/common/ipc/server_router_mock.hpp | 67 +++ test/common/ipc/test_client_router.cpp | 10 +- test/common/ipc/test_server_router.cpp | 8 +- test/daemon/engine/CMakeLists.txt | 1 + .../engine/cyphal/svc_sessions_mock.hpp | 198 ++++++++ .../engine/cyphal/transport_gtest_helpers.hpp | 430 ++++++++++++++++++ test/daemon/engine/cyphal/transport_mock.hpp | 69 +++ .../engine/cyphal/unique_ptr_ref_wrapper.hpp} | 19 +- .../engine/svc/node/test_exec_cmd_service.cpp | 295 ++++++++++++ test/gtest_printer.hpp | 32 +- test/ref_wrapper.hpp | 42 ++ test/virtual_time_scheduler.hpp | 163 +++++++ 17 files changed, 1473 insertions(+), 52 deletions(-) create mode 100644 test/common/ipc/gateway_mock.hpp create mode 100644 test/common/ipc/server_router_mock.hpp create mode 100644 test/daemon/engine/cyphal/svc_sessions_mock.hpp create mode 100644 test/daemon/engine/cyphal/transport_gtest_helpers.hpp create mode 100644 test/daemon/engine/cyphal/transport_mock.hpp rename test/{unique_ptr_refwrapper.hpp => daemon/engine/cyphal/unique_ptr_ref_wrapper.hpp} (63%) create mode 100644 test/daemon/engine/svc/node/test_exec_cmd_service.cpp create mode 100644 test/ref_wrapper.hpp create mode 100644 test/virtual_time_scheduler.hpp diff --git a/src/common/ipc/gateway.hpp b/src/common/ipc/gateway.hpp index 72cc1b8..e2053a6 100644 --- a/src/common/ipc/gateway.hpp +++ b/src/common/ipc/gateway.hpp @@ -73,7 +73,7 @@ class Gateway Gateway& operator=(Gateway&&) noexcept = delete; CETL_NODISCARD virtual int send(const ServiceDesc::Id service_id, const Payload payload) = 0; - virtual void complete(int error_code) = 0; + virtual void complete(const int error_code) = 0; CETL_NODISCARD virtual int event(const Event::Var& event) = 0; virtual void subscribe(EventHandler event_handler) = 0; diff --git a/test/common/ipc/gateway_mock.hpp b/test/common/ipc/gateway_mock.hpp new file mode 100644 index 0000000..073a434 --- /dev/null +++ b/test/common/ipc/gateway_mock.hpp @@ -0,0 +1,72 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_GATEWAY_MOCK_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_GATEWAY_MOCK_HPP_INCLUDED + +#include "ipc/gateway.hpp" +#include "ref_wrapper.hpp" + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ +namespace detail +{ + +class GatewayMock : public Gateway +{ +public: + struct Wrapper final : RefWrapper + { + using RefWrapper::RefWrapper; + + // MARK: Gateway + + CETL_NODISCARD int send(const ServiceDesc::Id service_id, const Payload payload) override + { + return reference().send(service_id, payload); + } + + void complete(const int error_code) override + { + reference().complete(error_code); + } + + CETL_NODISCARD int event(const Event::Var& event) override + { + return reference().event(event); + } + + void subscribe(EventHandler event_handler) override + { + reference().event_handler_ = event_handler; + reference().subscribe(event_handler); + } + + }; // Wrapper + + MOCK_METHOD(void, deinit, (), (const)); + MOCK_METHOD(int, send, (const ServiceDesc::Id service_id, const Payload payload), (override)); + MOCK_METHOD(void, complete, (const int error_code), (override)); + MOCK_METHOD(int, event, (const Event::Var& event), (override)); + MOCK_METHOD(void, subscribe, (EventHandler event_handler), (override)); + + // NOLINTBEGIN + EventHandler event_handler_; + // NOLINTEND + +}; // GatewayMock + +} // namespace detail +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_GATEWAY_MOCK_HPP_INCLUDED diff --git a/test/common/ipc/ipc_gtest_helpers.hpp b/test/common/ipc/ipc_gtest_helpers.hpp index 1a78e56..c92ca1b 100644 --- a/test/common/ipc/ipc_gtest_helpers.hpp +++ b/test/common/ipc/ipc_gtest_helpers.hpp @@ -94,22 +94,81 @@ inline bool operator==(const RouteChannelEnd_0_1& lhs, const RouteChannelEnd_0_1 // MARK: - GTest Matchers: -template +template class PayloadMatcher { public: - explicit PayloadMatcher(testing::Matcher matcher, - cetl::pmr::memory_resource& memory) - : matcher_(std::move(matcher)) - , memory_{memory} + PayloadMatcher(cetl::pmr::memory_resource& memory, testing::Matcher matcher) + : memory_{memory} + , matcher_(std::move(matcher)) + { + } + + bool MatchAndExplain(const Payload& payload, testing::MatchResultListener* listener) const + { + Msg msg{&memory_}; + const auto result = tryDeserializePayload(payload, msg); + if (!result) + { + if (listener->IsInterested()) + { + *listener << "Failed to deserialize the payload."; + } + return false; + } + + const bool match = matcher_.MatchAndExplain(msg, listener); + if (!match && listener->IsInterested()) + { + *listener << ".\n Payload: "; + *listener << testing::PrintToString(msg); + } + return match; + } + + bool MatchAndExplain(const Payloads& payloads, testing::MatchResultListener* listener) const + { + std::vector flatten; + for (const auto& payload : payloads) + { + flatten.insert(flatten.end(), payload.begin(), payload.end()); + } + return MatchAndExplain({flatten.data(), flatten.size()}, listener); + } + + void DescribeTo(std::ostream* os) const + { + *os << "is a value of type '" << "GetTypeName()" << "' and the value "; + matcher_.DescribeTo(os); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is a value of type other than '" << "GetTypeName()" << "' or the value "; + matcher_.DescribeNegationTo(os); + } + +private: + cetl::pmr::memory_resource& memory_; + const testing::Matcher matcher_; + +}; // PayloadMatcher + +template +class PayloadVariantMatcher +{ +public: + PayloadVariantMatcher(cetl::pmr::memory_resource& memory, + testing::Matcher matcher) + : memory_{memory} + , matcher_(std::move(matcher)) { } bool MatchAndExplain(const Payload& payload, testing::MatchResultListener* listener) const { - T msg{&memory_}; - const auto result = tryDeserializePayload(payload, msg); + Msg msg{&memory_}; + const auto result = tryDeserializePayload(payload, msg); if (!result) { if (listener->IsInterested()) @@ -151,18 +210,24 @@ class PayloadMatcher } private: - const testing::Matcher matcher_; - cetl::pmr::memory_resource& memory_; + cetl::pmr::memory_resource& memory_; + const testing::Matcher matcher_; -}; // PayloadMatcher +}; // PayloadVariantMatcher -template -testing::PolymorphicMatcher> PayloadWith( +template +testing::PolymorphicMatcher> PayloadWith(cetl::pmr::memory_resource& mr, + const testing::Matcher& matcher) +{ + return testing::MakePolymorphicMatcher(PayloadMatcher(mr, matcher)); +} - const testing::Matcher& matcher, - cetl::pmr::memory_resource& memory) +template +testing::PolymorphicMatcher> PayloadVariantWith( + cetl::pmr::memory_resource& mr, + const testing::Matcher& matcher) { - return testing::MakePolymorphicMatcher(PayloadMatcher(matcher, memory)); + return testing::MakePolymorphicMatcher(PayloadVariantMatcher(mr, matcher)); } inline auto PayloadOfRouteConnect(cetl::pmr::memory_resource& mr, @@ -171,7 +236,7 @@ inline auto PayloadOfRouteConnect(cetl::pmr::memory_resource& mr, ErrorCode error_code = ErrorCode::Success) { const RouteConnect_0_1 route_conn{{ver_major, ver_minor, &mr}, static_cast(error_code), &mr}; - return PayloadWith(testing::VariantWith(route_conn), mr); + return PayloadVariantWith(mr, testing::VariantWith(route_conn)); } template @@ -190,7 +255,7 @@ auto PayloadOfRouteChannelMsg(const Msg& msg, return 0; }), 0); - return PayloadWith(testing::VariantWith(route_ch_msg), mr); + return PayloadVariantWith(mr, testing::VariantWith(route_ch_msg)); } inline auto PayloadOfRouteChannelEnd(cetl::pmr::memory_resource& mr, // @@ -198,7 +263,7 @@ inline auto PayloadOfRouteChannelEnd(cetl::pmr::memory_resource& mr, // const ErrorCode error_code) { const RouteChannelEnd_0_1 ch_end{{tag, static_cast(error_code), &mr}, &mr}; - return PayloadWith(testing::VariantWith(ch_end), mr); + return PayloadVariantWith(mr, testing::VariantWith(ch_end)); } } // namespace ipc diff --git a/test/common/ipc/pipe/client_pipe_mock.hpp b/test/common/ipc/pipe/client_pipe_mock.hpp index 0ac1f57..e58ef99 100644 --- a/test/common/ipc/pipe/client_pipe_mock.hpp +++ b/test/common/ipc/pipe/client_pipe_mock.hpp @@ -9,7 +9,7 @@ #include "ipc/pipe/client_pipe.hpp" #include "ipc/ipc_types.hpp" -#include "unique_ptr_refwrapper.hpp" +#include "ref_wrapper.hpp" #include @@ -25,9 +25,9 @@ namespace pipe class ClientPipeMock : public ClientPipe { public: - struct RefWrapper final : UniquePtrRefWrapper + struct Wrapper final : RefWrapper { - using UniquePtrRefWrapper::UniquePtrRefWrapper; + using RefWrapper::RefWrapper; // MARK: ClientPipe @@ -41,7 +41,7 @@ class ClientPipeMock : public ClientPipe return reference().send(payloads); } - }; // RefWrapper + }; // Wrapper MOCK_METHOD(void, deinit, (), (const)); MOCK_METHOD(int, start, (EventHandler event_handler), (override)); diff --git a/test/common/ipc/pipe/server_pipe_mock.hpp b/test/common/ipc/pipe/server_pipe_mock.hpp index deb1186..a78f803 100644 --- a/test/common/ipc/pipe/server_pipe_mock.hpp +++ b/test/common/ipc/pipe/server_pipe_mock.hpp @@ -9,7 +9,7 @@ #include "ipc/pipe/server_pipe.hpp" #include "ipc/ipc_types.hpp" -#include "unique_ptr_refwrapper.hpp" +#include "ref_wrapper.hpp" #include @@ -25,9 +25,9 @@ namespace pipe class ServerPipeMock : public ServerPipe { public: - struct RefWrapper final : UniquePtrRefWrapper + struct Wrapper final : RefWrapper { - using UniquePtrRefWrapper::UniquePtrRefWrapper; + using RefWrapper::RefWrapper; // MARK: ServerPipe @@ -42,7 +42,7 @@ class ServerPipeMock : public ServerPipe return reference().send(client_id, payloads); } - }; // RefWrapper + }; // Wrapper MOCK_METHOD(void, deinit, (), (const)); MOCK_METHOD(int, start, (EventHandler event_handler), (override)); diff --git a/test/common/ipc/server_router_mock.hpp b/test/common/ipc/server_router_mock.hpp new file mode 100644 index 0000000..2db9368 --- /dev/null +++ b/test/common/ipc/server_router_mock.hpp @@ -0,0 +1,67 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_COMMON_IPC_SERVER_ROUTER_MOCK_HPP_INCLUDED +#define OCVSMD_COMMON_IPC_SERVER_ROUTER_MOCK_HPP_INCLUDED + +#include "ipc/server_router.hpp" + +#include + +namespace ocvsmd +{ +namespace common +{ +namespace ipc +{ + +class ServerRouterMock : public ServerRouter +{ +public: + using ServiceIdToChannelFactory = std::unordered_map; + + explicit ServerRouterMock(cetl::pmr::memory_resource& memory) + : memory_{memory} + { + } + + MOCK_METHOD(void, registerChannelFactoryByName, (const std::string& service_name), (const)); + + TypeErasedChannelFactory* getChannelFactory(const detail::ServiceDesc& svc_desc) + { + const auto it = service_id_to_channel_factory_.find(svc_desc.id); + return (it != service_id_to_channel_factory_.end()) ? &it->second : nullptr; + } + + // ServerRouter + + MOCK_METHOD(int, start, (), (override)); + + cetl::pmr::memory_resource& memory() override + { + return memory_; + } + + void registerChannelFactory(const detail::ServiceDesc service_desc, // + TypeErasedChannelFactory channel_factory) override + { + registerChannelFactoryByName(std::string{service_desc.name.data(), service_desc.name.size()}); + service_id_to_channel_factory_[service_desc.id] = std::move(channel_factory); + } + + // MARK: Data members: + + // NOLINTBEGIN + cetl::pmr::memory_resource& memory_; + ServiceIdToChannelFactory service_id_to_channel_factory_; + // NOLINTEND + +}; // ServerRouterMock + +} // namespace ipc +} // namespace common +} // namespace ocvsmd + +#endif // OCVSMD_COMMON_IPC_SERVER_ROUTER_MOCK_HPP_INCLUDED diff --git a/test/common/ipc/test_client_router.cpp b/test/common/ipc/test_client_router.cpp index b71a893..5004597 100644 --- a/test/common/ipc/test_client_router.cpp +++ b/test/common/ipc/test_client_router.cpp @@ -129,7 +129,7 @@ TEST_F(TestClientRouter, make) const auto client_router = ClientRouter::make( // mr_, - std::make_unique(client_pipe_mock)); + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_THAT(client_pipe_mock.event_handler_, IsFalse()); @@ -142,7 +142,7 @@ TEST_F(TestClientRouter, start) const auto client_router = ClientRouter::make( // mr_, - std::make_unique(client_pipe_mock)); + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_THAT(client_pipe_mock.event_handler_, IsFalse()); @@ -163,7 +163,7 @@ TEST_F(TestClientRouter, makeChannel) const auto client_router = ClientRouter::make( // mr_, - std::make_unique(client_pipe_mock)); + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); @@ -183,7 +183,7 @@ TEST_F(TestClientRouter, makeChannel_send) const auto client_router = ClientRouter::make( // mr_, - std::make_unique(client_pipe_mock)); + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); @@ -220,7 +220,7 @@ TEST_F(TestClientRouter, makeChannel_receive_events) const auto client_router = ClientRouter::make( // mr_, - std::make_unique(client_pipe_mock)); + std::make_unique(client_pipe_mock)); ASSERT_THAT(client_router, NotNull()); EXPECT_CALL(client_pipe_mock, start(_)).Times(1); diff --git a/test/common/ipc/test_server_router.cpp b/test/common/ipc/test_server_router.cpp index 4fcc837..c119447 100644 --- a/test/common/ipc/test_server_router.cpp +++ b/test/common/ipc/test_server_router.cpp @@ -128,7 +128,7 @@ TEST_F(TestServerRouter, make) const auto server_router = ServerRouter::make( // mr_, - std::make_unique(server_pipe_mock)); + std::make_unique(server_pipe_mock)); ASSERT_THAT(server_router, NotNull()); EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); } @@ -140,7 +140,7 @@ TEST_F(TestServerRouter, start) const auto server_router = ServerRouter::make( // mr_, - std::make_unique(server_pipe_mock)); + std::make_unique(server_pipe_mock)); ASSERT_THAT(server_router, NotNull()); EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); @@ -159,7 +159,7 @@ TEST_F(TestServerRouter, registerChannel) const auto server_router = ServerRouter::make( // mr_, - std::make_unique(server_pipe_mock)); + std::make_unique(server_pipe_mock)); ASSERT_THAT(server_router, NotNull()); EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); @@ -180,7 +180,7 @@ TEST_F(TestServerRouter, channel_send) const auto server_router = ServerRouter::make( // mr_, - std::make_unique(server_pipe_mock)); + std::make_unique(server_pipe_mock)); ASSERT_THAT(server_router, NotNull()); EXPECT_THAT(server_pipe_mock.event_handler_, IsFalse()); diff --git a/test/daemon/engine/CMakeLists.txt b/test/daemon/engine/CMakeLists.txt index 0639903..d5351a2 100644 --- a/test/daemon/engine/CMakeLists.txt +++ b/test/daemon/engine/CMakeLists.txt @@ -7,6 +7,7 @@ cmake_minimum_required(VERSION 3.27) add_executable(engine_tests main.cpp + svc/node/test_exec_cmd_service.cpp ) target_link_libraries(engine_tests ocvsmd_engine diff --git a/test/daemon/engine/cyphal/svc_sessions_mock.hpp b/test/daemon/engine/cyphal/svc_sessions_mock.hpp new file mode 100644 index 0000000..516f8cb --- /dev/null +++ b/test/daemon/engine/cyphal/svc_sessions_mock.hpp @@ -0,0 +1,198 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef LIBCYPHAL_TRANSPORT_SVC_SESSIONS_MOCK_HPP_INCLUDED +#define LIBCYPHAL_TRANSPORT_SVC_SESSIONS_MOCK_HPP_INCLUDED + +#include "unique_ptr_ref_wrapper.hpp" + +#include +#include +#include +#include + +#include + +#include + +namespace libcyphal +{ +namespace transport +{ + +class RequestRxSessionMock : public IRequestRxSession +{ +public: + struct RefWrapper final : UniquePtrRefWrapper + { + using UniquePtrRefWrapper::UniquePtrRefWrapper; + + // MARK: IRequestRxSession + + void setTransferIdTimeout(const Duration timeout) override + { + reference().setTransferIdTimeout(timeout); + } + RequestRxParams getParams() const noexcept override + { + return reference().getParams(); + } + cetl::optional receive() override + { + return reference().receive(); + } + void setOnReceiveCallback(OnReceiveCallback::Function&& function) override + { + reference().setOnReceiveCallback(std::move(function)); + } + + }; // RefWrapper + + RequestRxSessionMock() = default; + virtual ~RequestRxSessionMock() = default; + + RequestRxSessionMock(const RequestRxSessionMock&) = delete; + RequestRxSessionMock(RequestRxSessionMock&&) noexcept = delete; + RequestRxSessionMock& operator=(const RequestRxSessionMock&) = delete; + RequestRxSessionMock& operator=(RequestRxSessionMock&&) noexcept = delete; + + MOCK_METHOD(void, setTransferIdTimeout, (const Duration timeout), (override)); + MOCK_METHOD(RequestRxParams, getParams, (), (const, noexcept, override)); + MOCK_METHOD(cetl::optional, receive, (), (override)); + MOCK_METHOD(void, setOnReceiveCallback, (OnReceiveCallback::Function&&), (override)); + MOCK_METHOD(void, deinit, (), (noexcept)); // NOLINT(*-exception-escape) + +}; // RequestRxSessionMock + +// MARK: - + +class RequestTxSessionMock : public IRequestTxSession +{ +public: + struct RefWrapper final : UniquePtrRefWrapper + { + using UniquePtrRefWrapper::UniquePtrRefWrapper; + + // MARK: IRequestTxSession + + RequestTxParams getParams() const noexcept override + { + return reference().getParams(); + } + cetl::optional send(const TransferTxMetadata& metadata, + const PayloadFragments payload_fragments) override + { + return reference().send(metadata, payload_fragments); + } + + }; // RefWrapper + + RequestTxSessionMock() = default; + virtual ~RequestTxSessionMock() = default; + + RequestTxSessionMock(const RequestTxSessionMock&) = delete; + RequestTxSessionMock(RequestTxSessionMock&&) noexcept = delete; + RequestTxSessionMock& operator=(const RequestTxSessionMock&) = delete; + RequestTxSessionMock& operator=(RequestTxSessionMock&&) noexcept = delete; + + MOCK_METHOD(RequestTxParams, getParams, (), (const, noexcept, override)); + MOCK_METHOD(cetl::optional, + send, + (const TransferTxMetadata& metadata, const PayloadFragments payload_fragments), + (override)); + MOCK_METHOD(void, deinit, (), ()); + +}; // RequestTxSessionMock + +// MARK: - + +class ResponseRxSessionMock : public IResponseRxSession +{ +public: + struct RefWrapper final : UniquePtrRefWrapper + { + using UniquePtrRefWrapper::UniquePtrRefWrapper; + + // MARK: IResponseRxSession + + void setTransferIdTimeout(const Duration timeout) override + { + reference().setTransferIdTimeout(timeout); + } + ResponseRxParams getParams() const noexcept override + { + return reference().getParams(); + } + cetl::optional receive() override + { + return reference().receive(); + } + void setOnReceiveCallback(OnReceiveCallback::Function&& function) override + { + reference().setOnReceiveCallback(std::move(function)); + } + + }; // RefWrapper + + ResponseRxSessionMock() = default; + virtual ~ResponseRxSessionMock() = default; + + ResponseRxSessionMock(const ResponseRxSessionMock&) = delete; + ResponseRxSessionMock(ResponseRxSessionMock&&) noexcept = delete; + ResponseRxSessionMock& operator=(const ResponseRxSessionMock&) = delete; + ResponseRxSessionMock& operator=(ResponseRxSessionMock&&) noexcept = delete; + + MOCK_METHOD(void, setTransferIdTimeout, (const Duration timeout), (override)); + MOCK_METHOD(ResponseRxParams, getParams, (), (const, noexcept, override)); + MOCK_METHOD(cetl::optional, receive, (), (override)); + MOCK_METHOD(void, setOnReceiveCallback, (OnReceiveCallback::Function&&), (override)); + MOCK_METHOD(void, deinit, (), (noexcept)); // NOLINT(*-exception-escape) + +}; // ResponseRxSessionMock + +// MARK: - + +class ResponseTxSessionMock : public IResponseTxSession +{ +public: + struct RefWrapper final : UniquePtrRefWrapper + { + using UniquePtrRefWrapper::UniquePtrRefWrapper; + + // MARK: IResponseTxSession + + ResponseTxParams getParams() const noexcept override + { + return reference().getParams(); + } + cetl::optional send(const ServiceTxMetadata& metadata, + const PayloadFragments payload_fragments) override + { + return reference().send(metadata, payload_fragments); + } + + }; // RefWrapper + + ResponseTxSessionMock() = default; + virtual ~ResponseTxSessionMock() = default; + + ResponseTxSessionMock(const ResponseTxSessionMock&) = delete; + ResponseTxSessionMock(ResponseTxSessionMock&&) noexcept = delete; + ResponseTxSessionMock& operator=(const ResponseTxSessionMock&) = delete; + ResponseTxSessionMock& operator=(ResponseTxSessionMock&&) noexcept = delete; + + MOCK_METHOD(ResponseTxParams, getParams, (), (const, noexcept, override)); + MOCK_METHOD(cetl::optional, + send, + (const ServiceTxMetadata& metadata, const PayloadFragments payload_fragments), + (override)); + MOCK_METHOD(void, deinit, (), ()); + +}; // ResponseTxSessionMock + +} // namespace transport +} // namespace libcyphal + +#endif // LIBCYPHAL_TRANSPORT_SVC_SESSIONS_MOCK_HPP_INCLUDED diff --git a/test/daemon/engine/cyphal/transport_gtest_helpers.hpp b/test/daemon/engine/cyphal/transport_gtest_helpers.hpp new file mode 100644 index 0000000..9cabb31 --- /dev/null +++ b/test/daemon/engine/cyphal/transport_gtest_helpers.hpp @@ -0,0 +1,430 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef LIBCYPHAL_TRANSPORT_GTEST_HELPERS_HPP_INCLUDED +#define LIBCYPHAL_TRANSPORT_GTEST_HELPERS_HPP_INCLUDED + +#include +#include +#include +#include + +#include +#include +#include + +#include + +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +namespace libcyphal +{ +namespace transport +{ + +// MARK: - GTest Printers: + +inline void PrintTo(const Priority priority, std::ostream* os) +{ + switch (priority) + { + case Priority::Exceptional: + *os << "Exceptional(0)"; + break; + case Priority::Immediate: + *os << "Immediate(1)"; + break; + case Priority::Fast: + *os << "Fast(2)"; + break; + case Priority::High: + *os << "High(3)"; + break; + case Priority::Nominal: + *os << "Nominal(4)"; + break; + case Priority::Low: + *os << "Low(5)"; + break; + case Priority::Slow: + *os << "Slow(6)"; + break; + case Priority::Optional: + *os << "Optional(7)"; + break; + } +} + +inline void PrintTo(const MessageRxParams& params, std::ostream* os) +{ + *os << "MessageRxParams{extent_bytes=" << params.extent_bytes; + *os << ", subject_id=" << params.subject_id << "}"; +} + +inline void PrintTo(const MessageTxParams& params, std::ostream* os) +{ + *os << "MessageTxParams{subject_id=" << params.subject_id << "}"; +} + +inline void PrintTo(const RequestRxParams& params, std::ostream* os) +{ + *os << "RequestRxParams{extent_bytes=" << params.extent_bytes; + *os << ", service_id=" << params.service_id << "}"; +} + +inline void PrintTo(const RequestTxParams& params, std::ostream* os) +{ + *os << "RequestTxParams{service_id=" << params.service_id; + *os << ", server_node_id=" << params.server_node_id << "}"; +} + +inline void PrintTo(const ResponseRxParams& params, std::ostream* os) +{ + *os << "ResponseRxParams{extent_bytes=" << params.extent_bytes; + *os << ", service_id=" << params.service_id; + *os << ", server_node_id=" << params.server_node_id << "}"; +} + +inline void PrintTo(const ResponseTxParams& params, std::ostream* os) +{ + *os << "ResponseTxParams{service_id=" << params.service_id << "}"; +} + +inline void PrintTo(const TransferMetadata& meta, std::ostream* os) +{ + *os << "TransferMetadata{transfer_id=" << meta.transfer_id << ", priority=" << testing::PrintToString(meta.priority) + << "}"; +} + +inline void PrintTo(const TransferRxMetadata& meta, std::ostream* os) +{ + *os << "TransferRxMetadata{base=" << testing::PrintToString(meta.base) + << ", timestamp=" << testing::PrintToString(meta.timestamp) << "}"; +} + +inline void PrintTo(const TransferTxMetadata& meta, std::ostream* os) +{ + *os << "TransferTxMetadata{base=" << testing::PrintToString(meta.base) + << ", deadline=" << testing::PrintToString(meta.deadline) << "}"; +} + +inline void PrintTo(const ServiceRxMetadata& meta, std::ostream* os) +{ + *os << "SvcRxMetadata{rx_meta=" << testing::PrintToString(meta.rx_meta) + << ", remote_node_id=" << meta.remote_node_id << "}"; +} + +inline void PrintTo(const ServiceTxMetadata& meta, std::ostream* os) +{ + *os << "SvcTxMetadata{tx_meta=" << testing::PrintToString(meta.tx_meta) + << ", remote_node_id=" << meta.remote_node_id << "}"; +} + +inline void PrintTo(const ScatteredBuffer& buffer, std::ostream* os) +{ + *os << "ScatteredBuffer{size=" << buffer.size() << "}"; +} + +// MARK: - GTest Matchers: + +class MessageRxParamsMatcher +{ +public: + using is_gtest_matcher = void; + + explicit MessageRxParamsMatcher(const MessageRxParams& params) + : params_{params} + { + } + + bool MatchAndExplain(const MessageRxParams& params, std::ostream*) const + { + return params.extent_bytes == params_.extent_bytes && params.subject_id == params_.subject_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(params_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(params_); + } + +private: + const MessageRxParams params_; +}; +inline testing::Matcher MessageRxParamsEq(const MessageRxParams& params) +{ + return MessageRxParamsMatcher(params); + +} // MessageRxParamsMatcher + +class MessageTxParamsMatcher +{ +public: + using is_gtest_matcher = void; + + explicit MessageTxParamsMatcher(const MessageTxParams& params) + : params_{params} + { + } + + bool MatchAndExplain(const MessageTxParams& params, std::ostream*) const + { + return params.subject_id == params_.subject_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(params_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(params_); + } + +private: + const MessageTxParams params_; +}; +inline testing::Matcher MessageTxParamsEq(const MessageTxParams& params) +{ + return MessageTxParamsMatcher(params); + +} // MessageTxParamsMatcher + +class RequestRxParamsMatcher +{ +public: + using is_gtest_matcher = void; + + explicit RequestRxParamsMatcher(const RequestRxParams& params) + : params_{params} + { + } + + bool MatchAndExplain(const RequestRxParams& params, std::ostream*) const + { + return params.extent_bytes == params_.extent_bytes && params.service_id == params_.service_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(params_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(params_); + } + +private: + const RequestRxParams params_; +}; +inline testing::Matcher RequestRxParamsEq(const RequestRxParams& params) +{ + return RequestRxParamsMatcher(params); + +} // RequestRxParamsMatcher + +class RequestTxParamsMatcher +{ +public: + using is_gtest_matcher = void; + + explicit RequestTxParamsMatcher(const RequestTxParams& params) + : params_{params} + { + } + + bool MatchAndExplain(const RequestTxParams& params, std::ostream*) const + { + return params.service_id == params_.service_id && params.server_node_id == params_.server_node_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(params_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(params_); + } + +private: + const RequestTxParams params_; +}; +inline testing::Matcher RequestTxParamsEq(const RequestTxParams& params) +{ + return RequestTxParamsMatcher(params); + +} // RequestTxParamsMatcher + +class ResponseRxParamsMatcher +{ +public: + using is_gtest_matcher = void; + + explicit ResponseRxParamsMatcher(const ResponseRxParams& params) + : params_{params} + { + } + + bool MatchAndExplain(const ResponseRxParams& params, std::ostream*) const + { + return params.extent_bytes == params_.extent_bytes && params.service_id == params_.service_id && + params.server_node_id == params_.server_node_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(params_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(params_); + } + +private: + const ResponseRxParams params_; +}; +inline testing::Matcher ResponseRxParamsEq(const ResponseRxParams& params) +{ + return ResponseRxParamsMatcher(params); + +} // ResponseRxParamsMatcher + +class ResponseTxParamsMatcher +{ +public: + using is_gtest_matcher = void; + + explicit ResponseTxParamsMatcher(const ResponseTxParams& params) + : params_{params} + { + } + + bool MatchAndExplain(const ResponseTxParams& params, std::ostream*) const + { + return params.service_id == params_.service_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(params_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(params_); + } + +private: + const ResponseTxParams params_; +}; +inline testing::Matcher ResponseTxParamsEq(const ResponseTxParams& params) +{ + return ResponseTxParamsMatcher(params); + +} // ResponseTxParamsMatcher + +class ServiceRxMetadataMatcher +{ +public: + using is_gtest_matcher = void; + + explicit ServiceRxMetadataMatcher(const ServiceRxMetadata& meta) + : meta_{meta} + { + } + + bool MatchAndExplain(const ServiceRxMetadata& meta, std::ostream*) const + { + return meta.rx_meta.base.transfer_id == meta_.rx_meta.base.transfer_id && + meta.rx_meta.base.priority == meta_.rx_meta.base.priority && + meta.rx_meta.timestamp == meta_.rx_meta.timestamp && meta.remote_node_id == meta_.remote_node_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(meta_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(meta_); + } + +private: + const ServiceRxMetadata meta_; +}; +inline testing::Matcher ServiceRxMetadataEq(const ServiceRxMetadata& meta) +{ + return ServiceRxMetadataMatcher(meta); + +} // ServiceRxMetadataMatcher + +class TransferTxMetadataMatcher +{ +public: + using is_gtest_matcher = void; + + explicit TransferTxMetadataMatcher(const TransferTxMetadata& meta) + : meta_{meta} + { + } + + bool MatchAndExplain(const TransferTxMetadata& meta, std::ostream*) const + { + return meta.base.transfer_id == meta_.base.transfer_id && meta.base.priority == meta_.base.priority && + meta.deadline == meta_.deadline; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(meta_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(meta_); + } + +private: + const TransferTxMetadata meta_; +}; +inline testing::Matcher TransferTxMetadataEq(const TransferTxMetadata& meta) +{ + return TransferTxMetadataMatcher(meta); + +} // TransferTxMetadataMatcher + +class ServiceTxMetadataMatcher +{ +public: + using is_gtest_matcher = void; + + explicit ServiceTxMetadataMatcher(const ServiceTxMetadata& meta) + : meta_{meta} + { + } + + bool MatchAndExplain(const ServiceTxMetadata& meta, std::ostream*) const + { + return testing::Value(meta.tx_meta, TransferTxMetadataEq(meta_.tx_meta)) && + meta.remote_node_id == meta_.remote_node_id; + } + void DescribeTo(std::ostream* os) const + { + *os << "is " << testing::PrintToString(meta_); + } + void DescribeNegationTo(std::ostream* os) const + { + *os << "is NOT " << testing::PrintToString(meta_); + } + +private: + const ServiceTxMetadata meta_; +}; +inline testing::Matcher ServiceTxMetadataEq(const ServiceTxMetadata& meta) +{ + return ServiceTxMetadataMatcher(meta); + +} // ServiceTxMetadataMatcher + +} // namespace transport +} // namespace libcyphal + +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +#endif // LIBCYPHAL_TRANSPORT_GTEST_HELPERS_HPP_INCLUDED diff --git a/test/daemon/engine/cyphal/transport_mock.hpp b/test/daemon/engine/cyphal/transport_mock.hpp new file mode 100644 index 0000000..2bf27d6 --- /dev/null +++ b/test/daemon/engine/cyphal/transport_mock.hpp @@ -0,0 +1,69 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef LIBCYPHAL_TRANSPORT_TRANSPORT_MOCK_HPP_INCLUDED +#define LIBCYPHAL_TRANSPORT_TRANSPORT_MOCK_HPP_INCLUDED + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace libcyphal +{ +namespace transport +{ + +class TransportMock : public ITransport +{ +public: + TransportMock() = default; + virtual ~TransportMock() = default; + + TransportMock(const TransportMock&) = delete; + TransportMock(TransportMock&&) noexcept = delete; + TransportMock& operator=(const TransportMock&) = delete; + TransportMock& operator=(TransportMock&&) noexcept = delete; + + MOCK_METHOD(ProtocolParams, getProtocolParams, (), (const, noexcept, override)); + MOCK_METHOD(cetl::optional, getLocalNodeId, (), (const, noexcept, override)); + MOCK_METHOD(cetl::optional, setLocalNodeId, (const NodeId), (noexcept, override)); + MOCK_METHOD((Expected, AnyFailure>), + makeMessageRxSession, + (const MessageRxParams&), + (override)); + MOCK_METHOD((Expected, AnyFailure>), + makeMessageTxSession, + (const MessageTxParams& params), + (override)); + MOCK_METHOD((Expected, AnyFailure>), + makeRequestRxSession, + (const RequestRxParams& params), + (override)); + MOCK_METHOD((Expected, AnyFailure>), + makeRequestTxSession, + (const RequestTxParams& params), + (override)); + MOCK_METHOD((Expected, AnyFailure>), + makeResponseRxSession, + (const ResponseRxParams& params), + (override)); + MOCK_METHOD((Expected, AnyFailure>), + makeResponseTxSession, + (const ResponseTxParams& params), + (override)); + +}; // TransportMock + +} // namespace transport +} // namespace libcyphal + +#endif // LIBCYPHAL_TRANSPORT_TRANSPORT_MOCK_HPP_INCLUDED diff --git a/test/unique_ptr_refwrapper.hpp b/test/daemon/engine/cyphal/unique_ptr_ref_wrapper.hpp similarity index 63% rename from test/unique_ptr_refwrapper.hpp rename to test/daemon/engine/cyphal/unique_ptr_ref_wrapper.hpp index 8415bd4..bd4ff49 100644 --- a/test/unique_ptr_refwrapper.hpp +++ b/test/daemon/engine/cyphal/unique_ptr_ref_wrapper.hpp @@ -3,15 +3,20 @@ // SPDX-License-Identifier: MIT // -#ifndef OCVSMD_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED -#define OCVSMD_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED +#ifndef LIBCYPHAL_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED +#define LIBCYPHAL_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED -namespace ocvsmd +#include + +namespace libcyphal { -template +template struct UniquePtrRefWrapper : Interface { + struct Spec : detail::UniquePtrSpec + {}; + explicit UniquePtrRefWrapper(Reference& reference) : reference_{reference} { @@ -27,7 +32,7 @@ struct UniquePtrRefWrapper : Interface reference_.deinit(); } - Reference& reference() + Reference& reference() const { return reference_; } @@ -37,6 +42,6 @@ struct UniquePtrRefWrapper : Interface }; // UniquePtrRefWrapper -} // namespace ocvsmd +} // namespace libcyphal -#endif // OCVSMD_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED +#endif // LIBCYPHAL_UNIQUE_PTR_REF_WRAPPER_HPP_INCLUDED diff --git a/test/daemon/engine/svc/node/test_exec_cmd_service.cpp b/test/daemon/engine/svc/node/test_exec_cmd_service.cpp new file mode 100644 index 0000000..6e76c24 --- /dev/null +++ b/test/daemon/engine/svc/node/test_exec_cmd_service.cpp @@ -0,0 +1,295 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#include "svc/node/exec_cmd_service.hpp" + +#include "common/ipc/gateway_mock.hpp" +#include "common/ipc/ipc_gtest_helpers.hpp" +#include "common/ipc/server_router_mock.hpp" +#include "daemon/engine/cyphal/svc_sessions_mock.hpp" +#include "daemon/engine/cyphal/transport_gtest_helpers.hpp" +#include "daemon/engine/cyphal/transport_mock.hpp" +#include "ipc/channel.hpp" +#include "svc/node/exec_cmd_spec.hpp" +#include "svc/svc_helpers.hpp" +#include "tracking_memory_resource.hpp" +#include "virtual_time_scheduler.hpp" + +#include +#include + +#include +#include + +#include +#include + +namespace +{ + +using namespace ocvsmd::common; // NOLINT This our main concern here in the unit tests. +using namespace ocvsmd::daemon::engine::svc; // NOLINT This our main concern here in the unit tests. + +using testing::_; +using testing::Invoke; +using testing::IsNull; +using testing::Return; +using testing::IsEmpty; +using testing::NotNull; +using testing::StrictMock; + +// https://github.com/llvm/llvm-project/issues/53444 +// NOLINTBEGIN(misc-unused-using-decls, misc-include-cleaner) +using std::literals::chrono_literals::operator""s; +using std::literals::chrono_literals::operator""ms; +// NOLINTEND(misc-unused-using-decls, misc-include-cleaner) + +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +class TestExecCmdService : public testing::Test +{ +protected: + using ExecCmdSpec = svc::node::ExecCmdSpec; + using GatewayMock = ipc::detail::GatewayMock; + + using CyService = uavcan::node::ExecuteCommand_1_3; + using CyPresentation = libcyphal::presentation::Presentation; + using CyProtocolParams = libcyphal::transport::ProtocolParams; + using CyServiceRxTransfer = libcyphal::transport::ServiceRxTransfer; + using CyRequestTxSessionMock = StrictMock; + using CyResponseRxSessionMock = StrictMock; + using CyUniquePtrReqTxSpec = CyRequestTxSessionMock::RefWrapper::Spec; + using CyUniquePtrResRxSpec = CyResponseRxSessionMock::RefWrapper::Spec; + struct CySvcSessions + { + CyRequestTxSessionMock req_tx_mock; + CyResponseRxSessionMock res_rx_mock; + CyResponseRxSessionMock::OnReceiveCallback::Function res_rx_cb_fn; + }; + + void SetUp() override + { + cetl::pmr::set_default_resource(&mr_); + + EXPECT_CALL(cy_transport_mock_, getProtocolParams()) + .WillRepeatedly( + Return(CyProtocolParams{std::numeric_limits::max(), 0, 0})); + } + + void TearDown() override + { + EXPECT_THAT(mr_.allocations, IsEmpty()); + EXPECT_THAT(mr_.total_allocated_bytes, mr_.total_deallocated_bytes); + } + + libcyphal::TimePoint now() const + { + return scheduler_.now(); + } + + void expectCySvcSessions(CySvcSessions& cy_sess_mocks, const libcyphal::transport::NodeId node_id) + { + const libcyphal::transport::RequestTxParams tx_params{CyService::Request::_traits_::FixedPortId, node_id}; + const libcyphal::transport::ResponseRxParams rx_params{CyService::Response::_traits_::ExtentBytes, + tx_params.service_id, + tx_params.server_node_id}; + + EXPECT_CALL(cy_transport_mock_, makeRequestTxSession(RequestTxParamsEq(tx_params))) // + .WillOnce(Invoke([&](const auto&) { // + return libcyphal::detail::makeUniquePtr(mr_, cy_sess_mocks.req_tx_mock); + })); + EXPECT_CALL(cy_sess_mocks.req_tx_mock, deinit()).Times(1); + + EXPECT_CALL(cy_sess_mocks.res_rx_mock, getParams()) // + .WillOnce(Return(rx_params)); + EXPECT_CALL(cy_sess_mocks.res_rx_mock, setTransferIdTimeout(_)) // + .WillOnce(Return()); + EXPECT_CALL(cy_sess_mocks.res_rx_mock, setOnReceiveCallback(_)) // + .WillRepeatedly(Invoke([&](auto&& cb_fn) { // + cy_sess_mocks.res_rx_cb_fn = std::forward(cb_fn); + })); + EXPECT_CALL(cy_transport_mock_, makeResponseRxSession(ResponseRxParamsEq(rx_params))) // + .WillOnce(Invoke([&](const auto&) { // + return libcyphal::detail::makeUniquePtr(mr_, cy_sess_mocks.res_rx_mock); + })); + EXPECT_CALL(cy_sess_mocks.res_rx_mock, deinit()).Times(1); + } + + // NOLINTBEGIN + ocvsmd::TrackingMemoryResource mr_; + ocvsmd::VirtualTimeScheduler scheduler_{}; + StrictMock cy_transport_mock_; + StrictMock ipc_router_mock_{mr_}; + const std::string svc_name_{ExecCmdSpec::svc_full_name()}; + const ipc::detail::ServiceDesc svc_desc_{ipc::AnyChannel::getServiceDesc(svc_name_)}; + + // NOLINTEND +}; + +// MARK: - Tests: + +TEST_F(TestExecCmdService, registerWithContext) +{ + CyPresentation cy_presentation{mr_, scheduler_, cy_transport_mock_}; + const ScvContext svc_context{mr_, scheduler_, ipc_router_mock_, cy_presentation}; + + EXPECT_THAT(ipc_router_mock_.getChannelFactory(svc_desc_), IsNull()); + + EXPECT_CALL(ipc_router_mock_, registerChannelFactoryByName(svc_name_)).WillOnce(Return()); + node::ExecCmdService::registerWithContext(svc_context); + + EXPECT_THAT(ipc_router_mock_.getChannelFactory(svc_desc_), NotNull()); +} + +TEST_F(TestExecCmdService, empty_request) +{ + CyPresentation cy_presentation{mr_, scheduler_, cy_transport_mock_}; + const ScvContext svc_context{mr_, scheduler_, ipc_router_mock_, cy_presentation}; + + EXPECT_CALL(ipc_router_mock_, registerChannelFactoryByName(_)).WillOnce(Return()); + node::ExecCmdService::registerWithContext(svc_context); + + auto* const ch_factory = ipc_router_mock_.getChannelFactory(svc_desc_); + ASSERT_THAT(ch_factory, NotNull()); + + { + StrictMock gateway_mock; + auto gateway = std::make_shared(gateway_mock); + + const ExecCmdSpec::Request request{&mr_}; + + EXPECT_CALL(gateway_mock, subscribe(_)).Times(1); + EXPECT_CALL(gateway_mock, complete(0)).Times(1); + EXPECT_CALL(gateway_mock, deinit()).Times(1); + // + const auto result = tryPerformOnSerialized(request, [&](const auto payload) { + // + (*ch_factory)(std::move(gateway), payload); + return 0; + }); + EXPECT_THAT(result, 0); + } +} + +TEST_F(TestExecCmdService, two_nodes_request) +{ + CyPresentation cy_presentation{mr_, scheduler_, cy_transport_mock_}; + const ScvContext svc_context{mr_, scheduler_, ipc_router_mock_, cy_presentation}; + + EXPECT_CALL(ipc_router_mock_, registerChannelFactoryByName(_)).WillOnce(Return()); + node::ExecCmdService::registerWithContext(svc_context); + + auto* const ch_factory = ipc_router_mock_.getChannelFactory(svc_desc_); + ASSERT_THAT(ch_factory, NotNull()); + + StrictMock gateway_mock; + + ExecCmdSpec::Request request{&mr_}; + request.timeout_us = 1'000'000; + request.node_ids.push_back(42); + request.node_ids.push_back(43); + request.node_ids.push_back(42); // Duplicate node ID. + + CySvcSessions cy_sess_42; + CySvcSessions cy_sess_43; + EXPECT_CALL(cy_sess_42.req_tx_mock, send(_, _)).WillOnce(Return(cetl::nullopt)); + EXPECT_CALL(cy_sess_43.req_tx_mock, send(_, _)).WillOnce(Return(cetl::nullopt)); + + scheduler_.scheduleAt(1s, [&](const auto&) { + // + // Emulate service request. + EXPECT_CALL(gateway_mock, subscribe(_)).Times(1); + expectCySvcSessions(cy_sess_42, 42); + expectCySvcSessions(cy_sess_43, 43); + const auto result = tryPerformOnSerialized(request, [&](const auto payload) { + // + (*ch_factory)(std::make_shared(gateway_mock), payload); + return 0; + }); + EXPECT_THAT(result, 0); + }); + scheduler_.scheduleAt(1s + 100ms, [&](const auto&) { + // + // Emulate that node 42 has responded in time (after 100ms). + ExecCmdSpec::Response expected_response{&mr_}; + expected_response.node_id = 42; + EXPECT_CALL(gateway_mock, send(_, ipc::PayloadWith(mr_, expected_response))).Times(1); + CyServiceRxTransfer transfer{{{{0, libcyphal::transport::Priority::Nominal}, now()}, 42}, {}}; + cy_sess_42.res_rx_cb_fn({transfer}); + }); + scheduler_.scheduleAt(2s, [&](const auto&) { + // + EXPECT_CALL(gateway_mock, complete(0)).Times(1); + EXPECT_CALL(gateway_mock, deinit()).Times(1); + }); + scheduler_.scheduleAt(2s + 1ms, [&](const auto&) { + // + testing::Mock::VerifyAndClearExpectations(&gateway_mock); + testing::Mock::VerifyAndClearExpectations(&cy_sess_42.req_tx_mock); + testing::Mock::VerifyAndClearExpectations(&cy_sess_42.res_rx_mock); + testing::Mock::VerifyAndClearExpectations(&cy_sess_43.req_tx_mock); + testing::Mock::VerifyAndClearExpectations(&cy_sess_43.res_rx_mock); + }); + scheduler_.spinFor(10s); +} + +TEST_F(TestExecCmdService, out_of_memory) +{ + CyPresentation cy_presentation{mr_, scheduler_, cy_transport_mock_}; + const ScvContext svc_context{mr_, scheduler_, ipc_router_mock_, cy_presentation}; + + EXPECT_CALL(ipc_router_mock_, registerChannelFactoryByName(_)).WillOnce(Return()); + node::ExecCmdService::registerWithContext(svc_context); + + auto* const ch_factory = ipc_router_mock_.getChannelFactory(svc_desc_); + ASSERT_THAT(ch_factory, NotNull()); + + { + StrictMock gateway_mock; + auto gateway = std::make_shared(gateway_mock); + + ExecCmdSpec::Request request{&mr_}; + request.node_ids.push_back(13); + request.node_ids.push_back(31); + + EXPECT_CALL(cy_transport_mock_, makeRequestTxSession(_)).WillOnce(Return(libcyphal::MemoryError{})); + + EXPECT_CALL(gateway_mock, subscribe(_)).Times(1); + EXPECT_CALL(gateway_mock, complete(ENOMEM)).Times(1); + EXPECT_CALL(gateway_mock, deinit()).Times(1); + // + const auto result = tryPerformOnSerialized(request, [&](const auto payload) { + // + (*ch_factory)(std::move(gateway), payload); + return 0; + }); + EXPECT_THAT(result, 0); + } +} + +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers) + +} // namespace + +namespace ocvsmd +{ +namespace common +{ +namespace svc +{ +namespace node +{ +static void PrintTo(const ExecCmdSvcResponse_0_1& res, std::ostream* os) // NOLINT +{ + *os << "ExecCmdSvcResponse_0_1{node_id=" << res.node_id << "}"; +} +static bool operator==(const ExecCmdSvcResponse_0_1& lhs, const ExecCmdSvcResponse_0_1& rhs) // NOLINT +{ + return lhs.node_id == rhs.node_id; +} +} // namespace node +} // namespace svc +} // namespace common +} // namespace ocvsmd diff --git a/test/gtest_printer.hpp b/test/gtest_printer.hpp index b8584df..b5411d1 100644 --- a/test/gtest_printer.hpp +++ b/test/gtest_printer.hpp @@ -73,19 +73,33 @@ class GtestPrinter final : public testing::EmptyTestEventListener // Called after a failed assertion or a SUCCESS(). void OnTestPartResult(const testing::TestPartResult& test_part_result) override { - if (test_part_result.failed()) + if (const auto* const file_name = test_part_result.file_name()) { - spdlog::error("TEST Failure in {}:{} ❌\n{}", - test_part_result.file_name(), - test_part_result.line_number(), - test_part_result.summary()); + if (test_part_result.failed()) + { + spdlog::error("TEST Failure in {}:{} ❌\n{}", + file_name, + test_part_result.line_number(), + test_part_result.summary()); + } + else + { + spdlog::debug("TEST Success in {}:{}\n{}", + file_name, + test_part_result.line_number(), + test_part_result.summary()); + } } else { - spdlog::debug("TEST Success in {}:{}\n{}", - test_part_result.file_name(), - test_part_result.line_number(), - test_part_result.summary()); + if (test_part_result.failed()) + { + spdlog::error("TEST Failure ❌\n{}", test_part_result.summary()); + } + else + { + spdlog::debug("TEST Success \n{}", test_part_result.summary()); + } } } diff --git a/test/ref_wrapper.hpp b/test/ref_wrapper.hpp new file mode 100644 index 0000000..efdfecb --- /dev/null +++ b/test/ref_wrapper.hpp @@ -0,0 +1,42 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_REF_WRAPPER_HPP_INCLUDED +#define OCVSMD_REF_WRAPPER_HPP_INCLUDED + +namespace ocvsmd +{ + +template +struct RefWrapper : Interface +{ + explicit RefWrapper(Reference& reference) + : reference_{reference} + { + } + + RefWrapper(const RefWrapper& other) = delete; + RefWrapper(RefWrapper&&) noexcept = delete; + RefWrapper& operator=(const RefWrapper&) = delete; + RefWrapper& operator=(RefWrapper&&) noexcept = delete; + + virtual ~RefWrapper() + { + reference_.deinit(); + } + + Reference& reference() + { + return reference_; + } + +private: + Reference& reference_; + +}; // RefWrapper + +} // namespace ocvsmd + +#endif // OCVSMD_REF_WRAPPER_HPP_INCLUDED diff --git a/test/virtual_time_scheduler.hpp b/test/virtual_time_scheduler.hpp new file mode 100644 index 0000000..985d04c --- /dev/null +++ b/test/virtual_time_scheduler.hpp @@ -0,0 +1,163 @@ +// +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: MIT +// + +#ifndef OCVSMD_VIRTUAL_TIME_SCHEDULER_HPP_INCLUDED +#define OCVSMD_VIRTUAL_TIME_SCHEDULER_HPP_INCLUDED + +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +namespace ocvsmd +{ + +class VirtualTimeScheduler final : public libcyphal::platform::SingleThreadedExecutor +{ +public: + explicit VirtualTimeScheduler(const libcyphal::TimePoint initial_now = {}) + : now_{initial_now} + { + } + + void scheduleAt(const libcyphal::TimePoint exec_time, Callback::Function&& function) + { + auto callback = registerCallback(std::move(function)); + callback.schedule(Callback::Schedule::Once{exec_time}); + callbacks_bag_.emplace_back(std::move(callback)); + } + + void scheduleAt(const libcyphal::Duration duration, Callback::Function&& function) + { + scheduleAt(libcyphal::TimePoint{} + duration, std::move(function)); + } + + void spinFor(const libcyphal::Duration duration) + { + const auto end_time = now_ + duration; + + while (now() < end_time) + { + const auto spin_result = spinOnce(); + if (testing::Test::HasFatalFailure()) + { + break; + } + + if (!spin_result.next_exec_time) + { + break; + } + + now_ = *spin_result.next_exec_time; + } + + now_ = end_time; + } + + CETL_NODISCARD Callback::Any registerNamedCallback(const std::string& name, Callback::Function&& function) + { + NamedCallbackNode new_cb_node{*this, std::move(function), name}; + insertCallbackNode(new_cb_node); + named_cb_interfaces_[name] = &new_cb_node; + return {std::move(new_cb_node)}; + } + void scheduleNamedCallback(const std::string& name) const + { + scheduleNamedCallback(name, Callback::Schedule::Once{now_}); + } + void scheduleNamedCallback(const std::string& name, const libcyphal::TimePoint time_point) const + { + scheduleNamedCallback(name, Callback::Schedule::Once{time_point}); + } + void scheduleNamedCallback(const std::string& name, const Callback::Schedule::Variant& schedule) const + { + named_cb_interfaces_.at(name)->schedule(schedule); + } + CETL_NODISCARD Callback::Any registerAndScheduleNamedCallback(const std::string& name, + const libcyphal::TimePoint time_point, + Callback::Function&& function) + { + return registerAndScheduleNamedCallback(name, Callback::Schedule::Once{time_point}, std::move(function)); + } + CETL_NODISCARD Callback::Any registerAndScheduleNamedCallback(const std::string& name, + const Callback::Schedule::Variant& schedule, + Callback::Function&& function) + { + auto callback = registerNamedCallback(name, std::move(function)); + callback.schedule(schedule); + return callback; + } + CETL_NODISCARD bool hasNamedCallback(const std::string& name) + { + return named_cb_interfaces_.find(name) != named_cb_interfaces_.end(); + } + + // MARK: - ITimeProvider + + libcyphal::TimePoint now() const noexcept override + { + return now_; + } + +private: + using Self = VirtualTimeScheduler; + using Base = SingleThreadedExecutor; + + class NamedCallbackNode final : public CallbackNode + { + public: + NamedCallbackNode(Self& executor, Callback::Function&& function, std::string name) + : CallbackNode{executor, std::move(function)} + , name_{std::move(name)} + { + } + + ~NamedCallbackNode() override + { + if (!name_.empty()) + { + getExecutor().named_cb_interfaces_.erase(name_); + } + } + + NamedCallbackNode(NamedCallbackNode&& other) noexcept + : CallbackNode(std::move(static_cast(other))) + , name_{std::exchange(other.name_, "")} + { + getExecutor().named_cb_interfaces_[name_] = this; + } + + NamedCallbackNode(const NamedCallbackNode&) = delete; + NamedCallbackNode& operator=(const NamedCallbackNode&) = delete; + NamedCallbackNode& operator=(NamedCallbackNode&& other) noexcept = delete; + + private: + Self& getExecutor() noexcept + { + return static_cast(executor()); + } + + // MARK: Data members: + std::string name_; + + }; // NamedCallbackNode + + libcyphal::TimePoint now_; + std::vector callbacks_bag_; + std::map named_cb_interfaces_; + +}; // VirtualTimeScheduler + +} // namespace ocvsmd + +#endif // OCVSMD_VIRTUAL_TIME_SCHEDULER_HPP_INCLUDED From 0e88ed9692188cabed47206ed2f10e337f9951d7 Mon Sep 17 00:00:00 2001 From: Sergei Date: Thu, 13 Feb 2025 10:58:47 +0200 Subject: [PATCH 139/156] a bit more coverage --- test/daemon/engine/svc/node/test_exec_cmd_service.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/daemon/engine/svc/node/test_exec_cmd_service.cpp b/test/daemon/engine/svc/node/test_exec_cmd_service.cpp index 6e76c24..3f696bf 100644 --- a/test/daemon/engine/svc/node/test_exec_cmd_service.cpp +++ b/test/daemon/engine/svc/node/test_exec_cmd_service.cpp @@ -13,6 +13,7 @@ #include "daemon/engine/cyphal/transport_mock.hpp" #include "ipc/channel.hpp" #include "svc/node/exec_cmd_spec.hpp" +#include "svc/node/services.hpp" #include "svc/svc_helpers.hpp" #include "tracking_memory_resource.hpp" #include "virtual_time_scheduler.hpp" @@ -138,7 +139,7 @@ TEST_F(TestExecCmdService, registerWithContext) EXPECT_THAT(ipc_router_mock_.getChannelFactory(svc_desc_), IsNull()); EXPECT_CALL(ipc_router_mock_, registerChannelFactoryByName(svc_name_)).WillOnce(Return()); - node::ExecCmdService::registerWithContext(svc_context); + node::registerAllServices(svc_context); EXPECT_THAT(ipc_router_mock_.getChannelFactory(svc_desc_), NotNull()); } From fbac0b008e1bdd4dd5dc0dc3230d5c04f6effb2b Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Thu, 13 Feb 2025 18:30:37 +0200 Subject: [PATCH 140/156] remove not in use stuff --- src/cli/main.cpp | 1 - src/daemon/engine/cyphal/file_provider.cpp | 23 +--------------------- src/daemon/engine/cyphal/file_provider.hpp | 7 ++----- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index cff7c00..b603922 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -12,7 +12,6 @@ #include -#include #include #include diff --git a/src/daemon/engine/cyphal/file_provider.cpp b/src/daemon/engine/cyphal/file_provider.cpp index 3b84a74..8307dbb 100644 --- a/src/daemon/engine/cyphal/file_provider.cpp +++ b/src/daemon/engine/cyphal/file_provider.cpp @@ -15,11 +15,8 @@ #include #include -#include -#include #include #include -#include #include @@ -55,10 +52,7 @@ class FileProviderImpl final : public FileProvider }; struct Svc { - using List = SvcSpec; using Read = SvcSpec; - using Write = SvcSpec; - using Modify = SvcSpec; using GetInfo = SvcSpec; }; // Svc @@ -68,37 +62,25 @@ class FileProviderImpl final : public FileProvider libcyphal::presentation::Presentation& presentation, Config::Ptr config) { - auto list_srv = makeServer("List", presentation); auto read_srv = makeServer("Read", presentation); - auto write_srv = makeServer("Write", presentation); - auto modify_srv = makeServer("Modify", presentation); auto get_info_srv = makeServer("GetInfo", presentation); - if (!list_srv || !read_srv || !write_srv || !modify_srv || !get_info_srv) + if (!read_srv || !get_info_srv) { return nullptr; } return std::make_unique(memory, std::move(config), - std::move(*list_srv), std::move(*read_srv), - std::move(*write_srv), - std::move(*modify_srv), std::move(*get_info_srv)); } FileProviderImpl(cetl::pmr::memory_resource& memory, Config::Ptr config, - Svc::List::Server&& list_srv, Svc::Read::Server&& read_srv, - Svc::Write::Server&& write_srv, - Svc::Modify::Server&& modify_srv, Svc::GetInfo::Server&& get_info_srv) : memory_{memory} , config_{std::move(config)} - , list_srv_{std::move(list_srv)} , read_srv_{std::move(read_srv)} - , write_srv_{std::move(write_srv)} - , modify_srv_{std::move(modify_srv)} , get_info_srv_{std::move(get_info_srv)} { using ListRootsSpec = common::svc::file_server::ListRootsSpec; @@ -446,10 +428,7 @@ class FileProviderImpl final : public FileProvider cetl::pmr::memory_resource& memory_; Config::Ptr config_; - Svc::List::Server list_srv_; Svc::Read::Server read_srv_; - Svc::Write::Server write_srv_; - Svc::Modify::Server modify_srv_; Svc::GetInfo::Server get_info_srv_; common::LoggerPtr logger_{common::getLogger("engine")}; std::vector roots_; diff --git a/src/daemon/engine/cyphal/file_provider.hpp b/src/daemon/engine/cyphal/file_provider.hpp index 799a5a2..8c7a5c5 100644 --- a/src/daemon/engine/cyphal/file_provider.hpp +++ b/src/daemon/engine/cyphal/file_provider.hpp @@ -28,11 +28,8 @@ namespace cyphal /// @brief Defines 'File' provider component for the application node. /// /// Internally uses several `uavcan.file` cyphal servers: -// - 'GetInfo' -// - 'List' -// - 'Modify' -// - 'Read' -// - 'Write' +/// - 'Read' +/// - 'GetInfo' /// class FileProvider { From 65fbda547804695572498b21ff1f850ebb17b45c Mon Sep 17 00:00:00 2001 From: Sergei Date: Thu, 13 Feb 2025 23:34:18 +0200 Subject: [PATCH 141/156] a bit more tracing --- src/cli/main.cpp | 4 ++-- src/common/ipc/pipe/socket_base.cpp | 37 ++++++++++++++++++++++------- src/common/ipc/pipe/socket_base.hpp | 3 +-- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/cli/main.cpp b/src/cli/main.cpp index b603922..f6bffc8 100644 --- a/src/cli/main.cpp +++ b/src/cli/main.cpp @@ -83,7 +83,7 @@ int main(const int argc, const char** const argv) return EXIT_FAILURE; } -#if 0 // NOLINT +#if 1 // NOLINT // Demo of daemon's node command client - sending a command to node 42, 43 & 44. { @@ -111,7 +111,7 @@ int main(const int argc, const char** const argv) } } #endif -#if 1 // NOLINT +#if 0 // NOLINT // Demo of daemon's file server - push root. { diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp index 07ab556..78cc068 100644 --- a/src/common/ipc/pipe/socket_base.cpp +++ b/src/common/ipc/pipe/socket_base.cpp @@ -46,7 +46,7 @@ constexpr std::size_t MsgMaxSize = 1ULL << 20ULL; // 1 MB } // namespace -int SocketBase::send(const State& state, const Payloads payloads) +int SocketBase::send(const State& state, const Payloads payloads) const { // 1. Write the message header (signature and total size of the following fragments). // @@ -64,6 +64,7 @@ int SocketBase::send(const State& state, const Payloads payloads) return ::send(state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT); })) { + logger_->error("SocketBase: Failed to send msg header (fd={}): {}.", state.fd.get(), std::strerror(err)); return err; } @@ -76,6 +77,7 @@ int SocketBase::send(const State& state, const Payloads payloads) return ::send(state.fd.get(), payload.data(), payload.size(), MSG_DONTWAIT); })) { + logger_->error("SocketBase: Failed to send msg payload (fd={}): {}.", state.fd.get(), std::strerror(err)); return err; } } @@ -97,20 +99,32 @@ int SocketBase::receiveMessage(State& state, std::function&& actio { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { + logger_->trace("Msg header read would block (fd={}).", state.fd.get()); return 0; // no data available yet } - logger_->error("Failed to read message header (fd={}): {}.", state.fd.get(), std::strerror(err)); + logger_->error("Failed to read msg header (fd={}): {}.", state.fd.get(), std::strerror(err)); return err; } if (bytes_read == 0) { + logger_->debug("Zero bytes of msg header read - end of stream (fd={}).", state.fd.get()); return -1; // EOF } - if ((bytes_read != sizeof(msg_header)) || (msg_header.signature != MsgSignature) // - || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) + if (bytes_read != sizeof(msg_header)) { + logger_->warn("Incomplete msg header read - closing invalid stream (fd={}, read={}, expected={}).", + state.fd.get(), + bytes_read, + sizeof(msg_header)); + return EINVAL; + } + if ((msg_header.signature != MsgSignature) || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) + { + logger_->error("Invalid msg header read - closing invalid stream (fd={}, size={}).", + state.fd.get(), + msg_header.size); return EINVAL; } @@ -125,21 +139,26 @@ int SocketBase::receiveMessage(State& state, std::function&& actio auto read_and_act = [this, &state, act = std::move(action)]( // const cetl::span buf_span) { // - ssize_t read = 0; - if (const auto err = platform::posixSyscallError([this, &state, buf_span, &read] { + ssize_t bytes_read = 0; + if (const auto err = platform::posixSyscallError([this, &state, buf_span, &bytes_read] { // - return read = ::recv(state.fd.get(), buf_span.data(), buf_span.size(), MSG_DONTWAIT); + return bytes_read = ::recv(state.fd.get(), buf_span.data(), buf_span.size(), MSG_DONTWAIT); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { + logger_->trace("Msg payload read would block (fd={}).", state.fd.get()); return 0; // no data available } - logger_->error("Failed to read message payload (fd={}): {}.", state.fd.get(), std::strerror(err)); + logger_->error("Failed to read msg payload (fd={}): {}.", state.fd.get(), std::strerror(err)); return err; } - if (read != buf_span.size()) + if (bytes_read != buf_span.size()) { + logger_->warn("Incomplete msg payload read - closing invalid stream (fd={}, read={}, expected={}).", + state.fd.get(), + bytes_read, + buf_span.size()); return EINVAL; } diff --git a/src/common/ipc/pipe/socket_base.hpp b/src/common/ipc/pipe/socket_base.hpp index c8143e4..11e3169 100644 --- a/src/common/ipc/pipe/socket_base.hpp +++ b/src/common/ipc/pipe/socket_base.hpp @@ -57,8 +57,7 @@ class SocketBase return *logger_; } - CETL_NODISCARD static int send(const State& state, const Payloads payloads); - + CETL_NODISCARD int send(const State& state, const Payloads payloads) const; CETL_NODISCARD int receiveMessage(State& state, std::function&& action) const; private: From ea7c823af975d7ae6c7ec3d0b144410650bdd296 Mon Sep 17 00:00:00 2001 From: Sergei Date: Fri, 14 Feb 2025 11:33:05 +0200 Subject: [PATCH 142/156] implemented partial reading --- src/common/ipc/pipe/client_context.hpp | 10 +- src/common/ipc/pipe/socket_base.cpp | 193 ++++++++++++++----------- src/common/ipc/pipe/socket_base.hpp | 29 ++-- src/common/ipc/pipe/socket_client.cpp | 41 +++--- src/common/ipc/pipe/socket_client.hpp | 2 +- src/common/ipc/pipe/socket_server.cpp | 10 +- 6 files changed, 162 insertions(+), 123 deletions(-) diff --git a/src/common/ipc/pipe/client_context.hpp b/src/common/ipc/pipe/client_context.hpp index 6766c00..256b7c9 100644 --- a/src/common/ipc/pipe/client_context.hpp +++ b/src/common/ipc/pipe/client_context.hpp @@ -40,12 +40,12 @@ class ClientContext final CETL_DEBUG_ASSERT(fd.get() != -1, ""); logger_.trace("ClientContext(fd={}, id={}).", fd.get(), id_); - state_.fd = std::move(fd); + io_state_.fd = std::move(fd); } ~ClientContext() { - logger_.trace("~ClientContext(fd={}, id={}).", state_.fd.get(), id_); + logger_.trace("~ClientContext(fd={}, id={}).", io_state_.fd.get(), id_); } ClientContext(const ClientContext&) = delete; @@ -53,9 +53,9 @@ class ClientContext final ClientContext& operator=(const ClientContext&) = delete; ClientContext& operator=(ClientContext&&) noexcept = delete; - SocketBase::State& state() noexcept + SocketBase::IoState& state() noexcept { - return state_; + return io_state_; } void setCallback(libcyphal::IExecutor::Callback::Any&& fd_callback) @@ -66,7 +66,7 @@ class ClientContext final private: const ServerPipe::ClientId id_; Logger& logger_; - SocketBase::State state_; + SocketBase::IoState io_state_; libcyphal::IExecutor::Callback::Any fd_callback_; }; // ClientContext diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp index 78cc068..d23f792 100644 --- a/src/common/ipc/pipe/socket_base.cpp +++ b/src/common/ipc/pipe/socket_base.cpp @@ -8,9 +8,6 @@ #include "ipc/ipc_types.hpp" #include "ocvsmd/platform/posix_utils.hpp" -#include - -#include #include #include #include @@ -34,23 +31,16 @@ namespace pipe namespace { -struct MsgHeader final -{ - std::uint32_t signature; - std::uint32_t size; -}; - -constexpr std::size_t MsgSmallPayloadSize = 256; -constexpr std::uint32_t MsgSignature = 0x5356434F; // 'OCVS' -constexpr std::size_t MsgMaxSize = 1ULL << 20ULL; // 1 MB +constexpr std::uint32_t MsgHeaderSignature = 0x5356434F; // 'OCVS' +constexpr std::size_t MsgPayloadMaxSize = 1ULL << 20ULL; // 1 MB } // namespace -int SocketBase::send(const State& state, const Payloads payloads) const +int SocketBase::send(const IoState& io_state, const Payloads payloads) const { // 1. Write the message header (signature and total size of the following fragments). // - const std::size_t total_size = std::accumulate( // NOLINT + const std::size_t total_payload_size = std::accumulate( // NOLINT payloads.begin(), payloads.end(), 0ULL, @@ -58,13 +48,13 @@ int SocketBase::send(const State& state, const Payloads payloads) const // return acc + payload.size(); }); - if (const int err = platform::posixSyscallError([total_size, &state] { + if (const int err = platform::posixSyscallError([total_payload_size, &io_state] { // - const MsgHeader msg_header{MsgSignature, static_cast(total_size)}; - return ::send(state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT); + const IoState::MsgHeader msg_header{MsgHeaderSignature, static_cast(total_payload_size)}; + return ::send(io_state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT); })) { - logger_->error("SocketBase: Failed to send msg header (fd={}): {}.", state.fd.get(), std::strerror(err)); + logger_->error("SocketBase: Failed to send msg header (fd={}): {}.", io_state.fd.get(), std::strerror(err)); return err; } @@ -72,110 +62,145 @@ int SocketBase::send(const State& state, const Payloads payloads) const // for (const auto payload : payloads) { - if (const int err = platform::posixSyscallError([payload, &state] { + if (const int err = platform::posixSyscallError([payload, &io_state] { // - return ::send(state.fd.get(), payload.data(), payload.size(), MSG_DONTWAIT); + return ::send(io_state.fd.get(), payload.data(), payload.size(), MSG_DONTWAIT); })) { - logger_->error("SocketBase: Failed to send msg payload (fd={}): {}.", state.fd.get(), std::strerror(err)); + logger_->error("SocketBase: Failed to send msg payload (fd={}): {}.", + io_state.fd.get(), + std::strerror(err)); return err; } } return 0; } -int SocketBase::receiveMessage(State& state, std::function&& action) const +int SocketBase::receiveData(IoState& io_state) const { // 1. Receive and validate the message header. // - if (state.read_phase == State::ReadPhase::Header) + if (auto* const msg_header_ptr = cetl::get_if(&io_state.rx_msg_part)) { - MsgHeader msg_header{}; - ssize_t bytes_read = 0; - if (const auto err = platform::posixSyscallError([&state, &msg_header, &bytes_read] { - // - return bytes_read = ::recv(state.fd.get(), &msg_header, sizeof(msg_header), MSG_DONTWAIT); - })) + auto& msg_header = *msg_header_ptr; + + CETL_DEBUG_ASSERT(io_state.rx_partial_size < sizeof(msg_header), ""); + if (io_state.rx_partial_size < sizeof(msg_header)) { - if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + // Try read remaining part of the message header. + // + ssize_t bytes_read = 0; + if (const auto err = platform::posixSyscallError([&io_state, &bytes_read, &msg_header] { + // + // No lint b/c of low-level (potentially partial) reading. + // NOLINTNEXTLINE(*-reinterpret-cast, *-pointer-arithmetic) + auto* const dst_buf = reinterpret_cast(&msg_header) + io_state.rx_partial_size; + // + const auto bytes_to_read = sizeof(msg_header) - io_state.rx_partial_size; + return bytes_read = ::recv(io_state.fd.get(), dst_buf, bytes_to_read, MSG_DONTWAIT); + })) { - logger_->trace("Msg header read would block (fd={}).", state.fd.get()); - return 0; // no data available yet + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) + { + // No data available yet - that's ok, the next attempt will try to read again. + // + logger_->trace("Msg header read would block (fd={}).", io_state.fd.get()); + return 0; + } + logger_->error("Failed to read msg header (fd={}): {}.", io_state.fd.get(), std::strerror(err)); + return err; } - logger_->error("Failed to read msg header (fd={}): {}.", state.fd.get(), std::strerror(err)); - return err; - } - if (bytes_read == 0) - { - logger_->debug("Zero bytes of msg header read - end of stream (fd={}).", state.fd.get()); - return -1; // EOF - } + // Progress the partial read state. + // + io_state.rx_partial_size += bytes_read; + CETL_DEBUG_ASSERT(io_state.rx_partial_size <= sizeof(msg_header), ""); + if (bytes_read == 0) + { + logger_->debug("Zero bytes of msg header read - end of stream (fd={}).", io_state.fd.get()); + return -1; // EOF + } + if (io_state.rx_partial_size < sizeof(msg_header)) + { + // Not enough data yet - that's ok, the next attempt will try to read the rest. + return 0; + } - if (bytes_read != sizeof(msg_header)) - { - logger_->warn("Incomplete msg header read - closing invalid stream (fd={}, read={}, expected={}).", - state.fd.get(), - bytes_read, - sizeof(msg_header)); - return EINVAL; - } - if ((msg_header.signature != MsgSignature) || (msg_header.size == 0) || (msg_header.size > MsgMaxSize)) - { - logger_->error("Invalid msg header read - closing invalid stream (fd={}, size={}).", - state.fd.get(), - msg_header.size); - return EINVAL; + // Validate the message header. + // Just in case validate also the payload size to be within the reasonable limits. + // Zero payload size is also considered invalid (b/c we always expect non-empty `Route` payload). + // + if ((msg_header.signature != MsgHeaderSignature) // + || (msg_header.payload_size == 0) || (msg_header.payload_size > MsgPayloadMaxSize)) + { + logger_->error("Invalid msg header read - closing invalid stream (fd={}, payload_size={}).", + io_state.fd.get(), + msg_header.payload_size); + return EINVAL; + } } - state.read_msg_size = msg_header.size; - state.read_phase = State::ReadPhase::Payload; + // Message header has been read and validated. + // Switch to the next part - message payload. + // + io_state.rx_partial_size = 0; + auto payload_buffer = std::make_unique(msg_header.payload_size); // NOLINT(*-avoid-c-arrays) + io_state.rx_msg_part.emplace(msg_header.payload_size, std::move(payload_buffer)); } // 2. Read message payload. // - if (state.read_phase == State::ReadPhase::Payload) + if (auto* const msg_payload_ptr = cetl::get_if(&io_state.rx_msg_part)) { - auto read_and_act = [this, &state, act = std::move(action)]( // - const cetl::span buf_span) { - // + auto& msg_payload = *msg_payload_ptr; + + CETL_DEBUG_ASSERT(io_state.rx_partial_size < msg_payload.size, ""); + if (io_state.rx_partial_size < msg_payload.size) + { ssize_t bytes_read = 0; - if (const auto err = platform::posixSyscallError([this, &state, buf_span, &bytes_read] { + if (const auto err = platform::posixSyscallError([&io_state, &bytes_read, &msg_payload] { // - return bytes_read = ::recv(state.fd.get(), buf_span.data(), buf_span.size(), MSG_DONTWAIT); + std::uint8_t* const dst_buf = msg_payload.buffer.get() + io_state.rx_partial_size; + // + const auto bytes_to_read = msg_payload.size - io_state.rx_partial_size; + return bytes_read = ::recv(io_state.fd.get(), dst_buf, bytes_to_read, MSG_DONTWAIT); })) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { - logger_->trace("Msg payload read would block (fd={}).", state.fd.get()); - return 0; // no data available + // No data available yet - that's ok, the next attempt will try to read again. + // + logger_->trace("Msg payload read would block (fd={}).", io_state.fd.get()); + return 0; } - logger_->error("Failed to read msg payload (fd={}): {}.", state.fd.get(), std::strerror(err)); + logger_->error("Failed to read msg payload (fd={}): {}.", io_state.fd.get(), std::strerror(err)); return err; } - if (bytes_read != buf_span.size()) + + // Progress the partial read state. + // + io_state.rx_partial_size += bytes_read; + CETL_DEBUG_ASSERT(io_state.rx_partial_size <= msg_payload.size, ""); + if (bytes_read == 0) { - logger_->warn("Incomplete msg payload read - closing invalid stream (fd={}, read={}, expected={}).", - state.fd.get(), - bytes_read, - buf_span.size()); - return EINVAL; + logger_->debug("Zero bytes of msg payload read - end of stream (fd={}).", io_state.fd.get()); + return -1; // EOF + } + if (io_state.rx_partial_size < msg_payload.size) + { + // Not enough data yet - that's ok, the next attempt will try to read the rest. + return 0; } - - state.read_phase = State::ReadPhase::Header; - - const cetl::span const_buf_span{buf_span}; - return act(const_buf_span); - }; - if (state.read_msg_size <= MsgSmallPayloadSize) // on stack buffer? - { - std::array buffer; // NOLINT(*-member-init) - return read_and_act({buffer.data(), state.read_msg_size}); } - // NOLINTNEXTLINE(*-avoid-c-arrays) - const std::unique_ptr buffer{new std::uint8_t[state.read_msg_size]}; - return read_and_act({buffer.get(), state.read_msg_size}); + // Message payload has been completely received. + // Switch to the first part - the message header again. + // + io_state.rx_partial_size = 0; + const auto payload = std::move(msg_payload); + io_state.rx_msg_part.emplace(); + + io_state.on_rx_msg_payload(Payload{payload.buffer.get(), payload.size}); } return 0; diff --git a/src/common/ipc/pipe/socket_base.hpp b/src/common/ipc/pipe/socket_base.hpp index 11e3169..945f35e 100644 --- a/src/common/ipc/pipe/socket_base.hpp +++ b/src/common/ipc/pipe/socket_base.hpp @@ -11,11 +11,13 @@ #include "logging.hpp" #include +#include #include #include #include #include +#include namespace ocvsmd { @@ -29,19 +31,26 @@ namespace pipe class SocketBase { public: - struct State final + struct IoState final { - enum class ReadPhase : std::uint8_t + struct MsgHeader final { - Header, - Payload + std::uint32_t signature{0}; + std::uint32_t payload_size{0}; }; + struct MsgPayload final + { + std::size_t size{0}; + std::unique_ptr buffer; // NOLINT(*-avoid-c-arrays) + }; + using MsgPart = cetl::variant; - io::OwnFd fd{}; - std::size_t read_msg_size{0}; - ReadPhase read_phase{ReadPhase::Header}; + io::OwnFd fd; + std::size_t rx_partial_size{0}; + MsgPart rx_msg_part{MsgHeader{}}; + std::function on_rx_msg_payload; - }; // State + }; // IoState SocketBase(const SocketBase&) = delete; SocketBase(SocketBase&&) noexcept = delete; @@ -57,8 +66,8 @@ class SocketBase return *logger_; } - CETL_NODISCARD int send(const State& state, const Payloads payloads) const; - CETL_NODISCARD int receiveMessage(State& state, std::function&& action) const; + CETL_NODISCARD int send(const IoState& io_state, const Payloads payloads) const; + CETL_NODISCARD int receiveData(IoState& io_state) const; private: LoggerPtr logger_{getLogger("ipc")}; diff --git a/src/common/ipc/pipe/socket_client.cpp b/src/common/ipc/pipe/socket_client.cpp index 785bb32..d2ef44e 100644 --- a/src/common/ipc/pipe/socket_client.cpp +++ b/src/common/ipc/pipe/socket_client.cpp @@ -35,12 +35,17 @@ SocketClient::SocketClient(libcyphal::IExecutor& executor, const io::SocketAddre , posix_executor_ext_{cetl::rtti_cast(&executor)} { CETL_DEBUG_ASSERT(posix_executor_ext_ != nullptr, ""); + + io_state_.on_rx_msg_payload = [this](const Payload payload) { + // + return event_handler_(Event::Message{payload}); + }; } int SocketClient::start(EventHandler event_handler) { CETL_DEBUG_ASSERT(event_handler, ""); - CETL_DEBUG_ASSERT(state_.fd.get() == -1, ""); + CETL_DEBUG_ASSERT(io_state_.fd.get() == -1, ""); event_handler_ = std::move(event_handler); @@ -55,7 +60,7 @@ int SocketClient::start(EventHandler event_handler) // handle_connect(); }, - platform::IPosixExecutorExtension::Trigger::Writable{state_.fd.get()}); + platform::IPosixExecutorExtension::Trigger::Writable{io_state_.fd.get()}); return 0; } @@ -65,31 +70,30 @@ int SocketClient::makeSocketHandle() using SocketResult = io::SocketAddress::SocketResult; auto maybe_socket = socket_address_.socket(SOCK_STREAM); - if (auto* const err = cetl::get_if(&maybe_socket)) + if (const auto* const err = cetl::get_if(&maybe_socket)) { - logger().error("Failed to create client socket: {}.", std::strerror(*err)); + logger().error("Failed to create client socket (err={}): {}.", *err, std::strerror(*err)); return *err; } auto socket_fd = cetl::get(std::move(maybe_socket)); CETL_DEBUG_ASSERT(socket_fd.get() != -1, ""); - const int err = socket_address_.connect(socket_fd); - if (err != 0) + if (const int err = socket_address_.connect(socket_fd)) { if (err != EINPROGRESS) { - logger().error("Failed to connect to server: {}.", std::strerror(err)); + logger().error("Failed to connect to server (err={}): {}.", err, std::strerror(err)); return err; } } - state_.fd = std::move(socket_fd); + io_state_.fd = std::move(socket_fd); return 0; } int SocketClient::send(const Payloads payloads) { - return SocketBase::send(state_, payloads); + return SocketBase::send(io_state_, payloads); } int SocketClient::connectSocket(const int fd, const void* const addr_ptr, const std::size_t addr_size) const @@ -116,7 +120,7 @@ void SocketClient::handle_connect() if (const auto err = platform::posixSyscallError([this, &so_error] { // socklen_t len = sizeof(so_error); - return ::getsockopt(state_.fd.get(), SOL_SOCKET, SO_ERROR, &so_error, &len); + return ::getsockopt(io_state_.fd.get(), SOL_SOCKET, SO_ERROR, &so_error, &len); })) { logger().warn("Failed to query socket error: {}.", std::strerror(err)); @@ -134,18 +138,14 @@ void SocketClient::handle_connect() // handle_receive(); }, - platform::IPosixExecutorExtension::Trigger::Readable{state_.fd.get()}); + platform::IPosixExecutorExtension::Trigger::Readable{io_state_.fd.get()}); - state_.read_phase = State::ReadPhase::Header; event_handler_(Event::Connected{}); } void SocketClient::handle_receive() { - if (const auto err = receiveMessage(state_, [this](const auto payload) { - // - return event_handler_(Event::Message{payload}); - })) + if (const auto err = receiveData(io_state_)) { if (err == -1) { @@ -153,7 +153,9 @@ void SocketClient::handle_receive() } else { - logger().warn("Failed to handle server response - closing connection: {}.", std::strerror(err)); + logger().warn("Failed to handle server response - closing connection (err={}): {}.", + err, + std::strerror(err)); } handle_disconnect(); @@ -164,8 +166,9 @@ void SocketClient::handle_disconnect() { socket_callback_.reset(); - state_.fd.reset(); - state_.read_phase = State::ReadPhase::Header; + io_state_.fd.reset(); + io_state_.rx_partial_size = 0; + io_state_.rx_msg_part.emplace(); event_handler_(Event::Disconnected{}); } diff --git a/src/common/ipc/pipe/socket_client.hpp b/src/common/ipc/pipe/socket_client.hpp index dd1d037..06fd351 100644 --- a/src/common/ipc/pipe/socket_client.hpp +++ b/src/common/ipc/pipe/socket_client.hpp @@ -52,7 +52,7 @@ class SocketClient final : public SocketBase, public ClientPipe io::SocketAddress socket_address_; platform::IPosixExecutorExtension* const posix_executor_ext_; - State state_; + IoState io_state_; libcyphal::IExecutor::Callback::Any socket_callback_; EventHandler event_handler_; diff --git a/src/common/ipc/pipe/socket_server.cpp b/src/common/ipc/pipe/socket_server.cpp index d56b4c5..1d86825 100644 --- a/src/common/ipc/pipe/socket_server.cpp +++ b/src/common/ipc/pipe/socket_server.cpp @@ -150,6 +150,11 @@ void SocketServer::handleAccept() handleClientRequest(new_client_id); }, platform::IPosixExecutorExtension::Trigger::Readable{raw_fd})); + // + client_context->state().on_rx_msg_payload = [this, new_client_id](const Payload payload) { + // + return event_handler_(Event::Message{new_client_id, payload}); + }; client_id_to_context_.emplace(new_client_id, std::move(client_context)); @@ -163,10 +168,7 @@ void SocketServer::handleClientRequest(const ClientId client_id) CETL_DEBUG_ASSERT(client_context, ""); auto& state = client_context->state(); - if (const auto err = receiveMessage(state, [this, client_id](const auto payload) { - // - return event_handler_(Event::Message{client_id, payload}); - })) + if (const auto err = receiveData(state)) { if (err == -1) { From bfde72e25de9e2af718bdb1fb2135d8996437ac8 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 14 Feb 2025 14:08:20 +0200 Subject: [PATCH 143/156] RPi32: reduce cmake requirement 3.27 -> 3.25 --- .gitmodules | 3 ++- CMakeLists.txt | 2 +- src/CMakeLists.txt | 2 +- src/cli/CMakeLists.txt | 2 +- src/common/CMakeLists.txt | 2 +- src/daemon/CMakeLists.txt | 2 +- src/daemon/engine/CMakeLists.txt | 2 +- src/sdk/CMakeLists.txt | 2 +- submodules/libcyphal | 2 +- submodules/nunavut | 2 +- test/CMakeLists.txt | 2 +- test/cli/CMakeLists.txt | 2 +- test/common/CMakeLists.txt | 2 +- test/daemon/CMakeLists.txt | 2 +- test/daemon/engine/CMakeLists.txt | 2 +- test/sdk/CMakeLists.txt | 2 +- 16 files changed, 17 insertions(+), 16 deletions(-) diff --git a/.gitmodules b/.gitmodules index 8795069..f3c1187 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,7 @@ [submodule "submodules/libcyphal"] path = submodules/libcyphal url = https://github.com/OpenCyphal-Garage/libcyphal.git + branch = sshirokov/rpi32 [submodule "submodules/libudpard"] path = submodules/libudpard url = https://github.com/OpenCyphal/libudpard @@ -12,7 +13,7 @@ [submodule "submodules/nunavut"] path = submodules/nunavut url = https://github.com/OpenCyphal/nunavut.git - branch = 3.0.preview + branch = sshirokov/rpi32 [submodule "submodules/cetl"] path = submodules/cetl url = https://github.com/OpenCyphal/CETL.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ecb046..6b78adf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) project(ocvsmd LANGUAGES C CXX diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7d31c07..d2915f7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) add_subdirectory(common) add_subdirectory(daemon) diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index f8c59c9..26c1d2a 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) add_executable(ocvsmd-cli main.cpp diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 8032ea3..c769d09 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) set(dsdl_ocvsmd_dir ${CMAKE_CURRENT_SOURCE_DIR}/dsdl/ocvsmd) file(GLOB_RECURSE dsdl_ocvsmd_files CONFIGURE_DEPENDS ${dsdl_ocvsmd_dir}/*.dsdl) diff --git a/src/daemon/CMakeLists.txt b/src/daemon/CMakeLists.txt index 9a0f8e6..7f6fd92 100644 --- a/src/daemon/CMakeLists.txt +++ b/src/daemon/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) add_subdirectory(engine) diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 2374122..0bb5371 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) # Define type generation and header library all in one go. # diff --git a/src/sdk/CMakeLists.txt b/src/sdk/CMakeLists.txt index d465308..8032866 100644 --- a/src/sdk/CMakeLists.txt +++ b/src/sdk/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) add_cyphal_library( NAME sdk diff --git a/submodules/libcyphal b/submodules/libcyphal index a3be9d3..5da584d 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit a3be9d3eef99c64d31adf23955ca2046c9a79f3b +Subproject commit 5da584dcc8d19b8ff5900157bf3e471a994174e3 diff --git a/submodules/nunavut b/submodules/nunavut index 948b75d..cd5f563 160000 --- a/submodules/nunavut +++ b/submodules/nunavut @@ -1 +1 @@ -Subproject commit 948b75de19145eb8de01b9485aa2cd23a1494029 +Subproject commit cd5f563bd1587990cfa59fa1a7932c42be7f0dca diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6f25600..8121215 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) include(FetchContent) FetchContent_Declare( diff --git a/test/cli/CMakeLists.txt b/test/cli/CMakeLists.txt index 2b27a6c..fc7dd19 100644 --- a/test/cli/CMakeLists.txt +++ b/test/cli/CMakeLists.txt @@ -3,4 +3,4 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) diff --git a/test/common/CMakeLists.txt b/test/common/CMakeLists.txt index 359e9b1..03a8a11 100644 --- a/test/common/CMakeLists.txt +++ b/test/common/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) add_executable(common_tests main.cpp diff --git a/test/daemon/CMakeLists.txt b/test/daemon/CMakeLists.txt index 572acc6..a297490 100644 --- a/test/daemon/CMakeLists.txt +++ b/test/daemon/CMakeLists.txt @@ -3,6 +3,6 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) add_subdirectory(engine) diff --git a/test/daemon/engine/CMakeLists.txt b/test/daemon/engine/CMakeLists.txt index d5351a2..6b56dd4 100644 --- a/test/daemon/engine/CMakeLists.txt +++ b/test/daemon/engine/CMakeLists.txt @@ -3,7 +3,7 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) add_executable(engine_tests main.cpp diff --git a/test/sdk/CMakeLists.txt b/test/sdk/CMakeLists.txt index 2b27a6c..fc7dd19 100644 --- a/test/sdk/CMakeLists.txt +++ b/test/sdk/CMakeLists.txt @@ -3,4 +3,4 @@ # SPDX-License-Identifier: MIT # -cmake_minimum_required(VERSION 3.27) +cmake_minimum_required(VERSION 3.25) From 68b313e921a5d3374938e56d57af2dbc076c5c4e Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 14 Feb 2025 14:42:10 +0200 Subject: [PATCH 144/156] fix build --- src/common/ipc/client_router.cpp | 6 +++--- src/common/ipc/pipe/socket_base.cpp | 7 ++++--- src/common/ipc/pipe/socket_base.hpp | 4 ++-- src/common/ipc/server_router.cpp | 12 ++++++------ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/common/ipc/client_router.cpp b/src/common/ipc/client_router.cpp index d546dc8..39c829a 100644 --- a/src/common/ipc/client_router.cpp +++ b/src/common/ipc/client_router.cpp @@ -125,12 +125,12 @@ class ClientRouterImpl final : public ClientRouter GatewayImpl& operator=(const GatewayImpl&) = delete; GatewayImpl& operator=(GatewayImpl&&) noexcept = delete; - ~GatewayImpl() + virtual ~GatewayImpl() { - router_.logger_->trace("~Gateway(tag={}, err={}).", endpoint_.tag, completion_error_code_); - performWithoutThrowing([this] { // + router_.logger_->trace("~Gateway(tag={}, err={}).", endpoint_.tag, completion_error_code_); + // `next_sequence_ == 0` means that this gateway was never used for sending messages, // and so remote router never knew about it (its tag) - no need to post "ChEnd" event. router_.onGatewayDisposal(endpoint_, next_sequence_ > 0, completion_error_code_); diff --git a/src/common/ipc/pipe/socket_base.cpp b/src/common/ipc/pipe/socket_base.cpp index d23f792..6dffe50 100644 --- a/src/common/ipc/pipe/socket_base.cpp +++ b/src/common/ipc/pipe/socket_base.cpp @@ -145,7 +145,8 @@ int SocketBase::receiveData(IoState& io_state) const // io_state.rx_partial_size = 0; auto payload_buffer = std::make_unique(msg_header.payload_size); // NOLINT(*-avoid-c-arrays) - io_state.rx_msg_part.emplace(msg_header.payload_size, std::move(payload_buffer)); + io_state.rx_msg_part.emplace( + IoState::MsgPayload{msg_header.payload_size, std::move(payload_buffer)}); } // 2. Read message payload. @@ -196,8 +197,8 @@ int SocketBase::receiveData(IoState& io_state) const // Message payload has been completely received. // Switch to the first part - the message header again. // - io_state.rx_partial_size = 0; - const auto payload = std::move(msg_payload); + io_state.rx_partial_size = 0; + const auto payload = std::move(msg_payload); io_state.rx_msg_part.emplace(); io_state.on_rx_msg_payload(Payload{payload.buffer.get(), payload.size}); diff --git a/src/common/ipc/pipe/socket_base.hpp b/src/common/ipc/pipe/socket_base.hpp index 945f35e..b44e311 100644 --- a/src/common/ipc/pipe/socket_base.hpp +++ b/src/common/ipc/pipe/socket_base.hpp @@ -40,8 +40,8 @@ class SocketBase }; struct MsgPayload final { - std::size_t size{0}; - std::unique_ptr buffer; // NOLINT(*-avoid-c-arrays) + std::uint32_t size{0}; + std::unique_ptr buffer{nullptr}; // NOLINT(*-avoid-c-arrays) }; using MsgPart = cetl::variant; diff --git a/src/common/ipc/server_router.cpp b/src/common/ipc/server_router.cpp index 670de6d..ccd8a10 100644 --- a/src/common/ipc/server_router.cpp +++ b/src/common/ipc/server_router.cpp @@ -121,15 +121,15 @@ class ServerRouterImpl final : public ServerRouter GatewayImpl& operator=(const GatewayImpl&) = delete; GatewayImpl& operator=(GatewayImpl&&) noexcept = delete; - ~GatewayImpl() + virtual ~GatewayImpl() { - router_.logger_->trace("~Gateway(cl={}, tag={}, err={}).", - endpoint_.client_id, - endpoint_.tag, - completion_error_code_); - performWithoutThrowing([this] { // + router_.logger_->trace("~Gateway(cl={}, tag={}, err={}).", + endpoint_.client_id, + endpoint_.tag, + completion_error_code_); + router_.onGatewayDisposal(endpoint_, completion_error_code_); }); } From cc10ec767de405229610630a6afbc0d99c0b4485 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 14 Feb 2025 15:55:36 +0200 Subject: [PATCH 145/156] latest libcyphal and spdlog --- submodules/libcyphal | 2 +- submodules/spdlog | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/submodules/libcyphal b/submodules/libcyphal index 5da584d..a8f9c1a 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit 5da584dcc8d19b8ff5900157bf3e471a994174e3 +Subproject commit a8f9c1a7d23eb240f6b6d455c066c7bb6ab2bfa0 diff --git a/submodules/spdlog b/submodules/spdlog index f355b3d..3335c38 160000 --- a/submodules/spdlog +++ b/submodules/spdlog @@ -1 +1 @@ -Subproject commit f355b3d58f7067eee1706ff3c801c2361011f3d5 +Subproject commit 3335c380a08c5e0f5117a66622df6afdb3d74959 From 6be53e633462f0431976e4483502607f5267e160 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 14 Feb 2025 16:00:51 +0200 Subject: [PATCH 146/156] Disable PSABI warnings on GCC (found on Rpi32) --- CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b78adf..e122fd3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,13 @@ set(test_dir "${CMAKE_SOURCE_DIR}/test") set(include_dir "${CMAKE_SOURCE_DIR}/include") set(submodules_dir "${CMAKE_SOURCE_DIR}/submodules") +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + # Disable PSABI warnings in GCC (on RPi). + list(APPEND CXX_FLAG_SET "-Wno-psabi") +endif() + +add_compile_options("$<$:${CXX_FLAG_SET}>") + # clang-format find_program(clang_format NAMES clang-format) if (NOT clang_format) From 5a745d66d790e4ced353f5573e5fc8ed25d6cf8b Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Fri, 14 Feb 2025 17:33:01 +0200 Subject: [PATCH 147/156] latest libcyphal --- .gitmodules | 1 - submodules/libcyphal | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index f3c1187..605a658 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,7 +1,6 @@ [submodule "submodules/libcyphal"] path = submodules/libcyphal url = https://github.com/OpenCyphal-Garage/libcyphal.git - branch = sshirokov/rpi32 [submodule "submodules/libudpard"] path = submodules/libudpard url = https://github.com/OpenCyphal/libudpard diff --git a/submodules/libcyphal b/submodules/libcyphal index a8f9c1a..c26d86e 160000 --- a/submodules/libcyphal +++ b/submodules/libcyphal @@ -1 +1 @@ -Subproject commit a8f9c1a7d23eb240f6b6d455c066c7bb6ab2bfa0 +Subproject commit c26d86e2ab44a13fe5aa6066717a34abe1a9a6af From 45481f3c428bddb6aa122a9e6cc6b9653d5a4671 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 17 Feb 2025 12:09:22 +0200 Subject: [PATCH 148/156] fixed macos build --- CMakeLists.txt | 2 +- init.d/ocvsmd.toml | 2 +- src/daemon/engine/CMakeLists.txt | 24 ++++++++++++------- .../engine/cyphal/transport_helpers.hpp | 9 ++++++- src/daemon/engine/engine.cpp | 7 +++++- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e122fd3..a114e59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,11 +26,11 @@ set(test_dir "${CMAKE_SOURCE_DIR}/test") set(include_dir "${CMAKE_SOURCE_DIR}/include") set(submodules_dir "${CMAKE_SOURCE_DIR}/submodules") +set(CXX_FLAG_SET "") if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # Disable PSABI warnings in GCC (on RPi). list(APPEND CXX_FLAG_SET "-Wno-psabi") endif() - add_compile_options("$<$:${CXX_FLAG_SET}>") # clang-format diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index da0f34c..0704807 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -14,7 +14,7 @@ unique_id = [] # UDP has priorioty over CAN if both types are present. # Supported formats: # - 'udp://' -# - 'socketcan:' +# - 'socketcan:' (linux only) interfaces = [ 'udp://127.0.0.1', ] diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 0bb5371..add652b 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -36,18 +36,19 @@ target_include_directories(udpard INTERFACE SYSTEM ${submodules_dir}/libudpard/libudpard ) -add_library(canard - ${submodules_dir}/libcanard/libcanard/canard.c -) -target_include_directories(canard - INTERFACE SYSTEM ${submodules_dir}/libcanard/libcanard -) +if (${PLATFORM_OS_TYPE} STREQUAL "linux") + add_library(canard + ${submodules_dir}/libcanard/libcanard/canard.c + ) + target_include_directories(canard + INTERFACE SYSTEM ${submodules_dir}/libcanard/libcanard + ) +endif () add_library(ocvsmd_engine config.cpp cyphal/file_provider.cpp engine.cpp - platform/can/socketcan.c platform/udp/udp.c svc/file_server/list_roots_service.cpp svc/file_server/pop_root_service.cpp @@ -58,10 +59,17 @@ add_library(ocvsmd_engine ) target_link_libraries(ocvsmd_engine PUBLIC udpard - PUBLIC canard PUBLIC ${engine_transpiled} PUBLIC ocvsmd_common ) +if (${PLATFORM_OS_TYPE} STREQUAL "linux") + target_sources(ocvsmd_engine + platform/can/socketcan.c + ) + target_link_libraries(ocvsmd_engine + PUBLIC canard + ) +endif () target_include_directories(ocvsmd_engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/src/daemon/engine/cyphal/transport_helpers.hpp b/src/daemon/engine/cyphal/transport_helpers.hpp index ba5f53c..7c79a82 100644 --- a/src/daemon/engine/cyphal/transport_helpers.hpp +++ b/src/daemon/engine/cyphal/transport_helpers.hpp @@ -10,12 +10,15 @@ #include #include -#include #include #include #include +#ifdef __linux__ +#include +#endif + namespace ocvsmd { namespace daemon @@ -61,6 +64,8 @@ struct TransportHelpers }; // Printers +#ifdef __linux__ + struct CanTransientErrorReporter { using Report = libcyphal::transport::can::ICanTransport::TransientErrorReport; @@ -111,6 +116,8 @@ struct TransportHelpers }; // CanTransientErrorReporter +#endif // __linux__ + struct UdpTransientErrorReporter { using Report = libcyphal::transport::udp::IUdpTransport::TransientErrorReport; diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 1449b8f..b087cfc 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -6,7 +6,6 @@ #include "engine.hpp" #include "config.hpp" -#include "cyphal/can_transport_bag.hpp" #include "cyphal/file_provider.hpp" #include "cyphal/udp_transport_bag.hpp" #include "engine_helpers.hpp" @@ -34,6 +33,10 @@ #include #include +#ifdef __linux__ +#include "cyphal/can_transport_bag.hpp" +#endif + namespace ocvsmd { namespace daemon @@ -59,11 +62,13 @@ cetl::optional Engine::init() } else { +#ifdef __linux__ if (auto maybe_can_transport_bag = cyphal::CanTransportBag::make(memory_, executor_, config_)) { any_transport_bag_ = std::move(maybe_can_transport_bag); } else +#endif // __linux__ { std::string msg = "Failed to create Cyphal transport."; logger_->error(msg); From b511e65f0c3fff79a36f3b199bd606ea99f4cf08 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 17 Feb 2025 15:30:13 +0200 Subject: [PATCH 149/156] fixed incorrect kqueue executor implementation a) can't combine filters (like `| EVFILT_VNODE`) b) wrong `EV_CLEAR` usage c) wrong `NOTE_xxx` usage --- .../bsd/kqueue_single_threaded_executor.hpp | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp index a59489d..6dbc7a7 100644 --- a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp @@ -185,7 +185,7 @@ class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThr AwaitableNode(Self& executor, Callback::Function&& function) : CallbackNode{executor, std::move(function)} , fd_{-1} - , events_{0} + , filter_{0} { } @@ -194,7 +194,7 @@ class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThr if (fd_ >= 0) { KEvent ev{}; - EV_SET(&ev, fd_, events_, EV_DELETE, NOTE_DELETE, 0, 0); + EV_SET(&ev, fd_, filter_, EV_DELETE, 0, 0, 0); ::kevent(getExecutor().kqueuefd_, &ev, 1, nullptr, 0, nullptr); getExecutor().total_awaitables_--; } @@ -203,14 +203,14 @@ class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThr AwaitableNode(AwaitableNode&& other) noexcept : CallbackNode(std::move(static_cast(other))) , fd_{std::exchange(other.fd_, -1)} - , events_{std::exchange(other.events_, 0)} + , filter_{std::exchange(other.filter_, 0)} { if (fd_ >= 0) { KEvent ev{}; - EV_SET(&ev, fd_, events_, EV_DELETE, NOTE_DELETE, 0, 0); + EV_SET(&ev, fd_, filter_, EV_DELETE, 0, 0, 0); ::kevent(getExecutor().kqueuefd_, &ev, 1, nullptr, 0, nullptr); - EV_SET(&ev, fd_, events_, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, this); + EV_SET(&ev, fd_, filter_, EV_ADD, 0, 0, this); ::kevent(getExecutor().kqueuefd_, &ev, 1, nullptr, 0, nullptr); } } @@ -224,22 +224,22 @@ class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThr return fd_; } - std::uint32_t events() const noexcept + std::int16_t filter() const noexcept { - return events_; + return filter_; } - void setup(const int fd, const std::uint32_t events) noexcept + void setup(const int fd, const std::int16_t filter) noexcept { CETL_DEBUG_ASSERT(fd >= 0, ""); - CETL_DEBUG_ASSERT(events != 0, ""); + CETL_DEBUG_ASSERT(filter != 0, ""); fd_ = fd; - events_ = events | EVFILT_VNODE; + filter_ = filter; getExecutor().total_awaitables_++; KEvent ev{}; - EV_SET(&ev, fd, events_, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, this); + EV_SET(&ev, fd, filter_, EV_ADD, 0, 0, this); ::kevent(getExecutor().kqueuefd_, &ev, 1, nullptr, 0, nullptr); } @@ -253,8 +253,8 @@ class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThr // MARK: Data members: - int fd_; - std::uint32_t events_; + int fd_; + std::int16_t filter_; }; // AwaitableNode From fde2ffb07f589e94d6a68c75fc2c3ac960e49c2c Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 18 Feb 2025 19:45:30 +0200 Subject: [PATCH 150/156] silence `EINTR` "error" condition --- .../ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp | 6 ++++++ .../platform/linux/epoll_single_threaded_executor.hpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp index 6dbc7a7..473e071 100644 --- a/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/bsd/kqueue_single_threaded_executor.hpp @@ -108,6 +108,12 @@ class KqueueSingleThreadedExecutor final : public libcyphal::platform::SingleThr if (kqueue_result < 0) { const auto err = errno; + if (err == EINTR) + { + // Normally, we would just retry a system call (`::kevent`), + // but we need updated timeout (from the main loop). + return cetl::nullopt; + } return libcyphal::transport::PlatformError{PosixPlatformError{err}}; } if (kqueue_result == 0) diff --git a/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp b/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp index fe9a525..e54c137 100644 --- a/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp +++ b/include/ocvsmd/platform/linux/epoll_single_threaded_executor.hpp @@ -99,6 +99,12 @@ class EpollSingleThreadedExecutor final : public libcyphal::platform::SingleThre if (epoll_result < 0) { const auto err = errno; + if (err == EINTR) + { + // Normally, we would just retry a system call (`::epoll_wait`), + // but we need updated timeout (from the main loop). + return cetl::nullopt; + } return libcyphal::transport::PlatformError{PosixPlatformError{err}}; } if (epoll_result == 0) From bd07684724700d3c45cb46db7d8dbf5b876fc3ad Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Mar 2025 08:14:48 +0200 Subject: [PATCH 151/156] remove vendor "jetbrains.com/clion" setting - needed only for old XCode on old MacOS. --- CMakePresets.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CMakePresets.json b/CMakePresets.json index 434904a..e77475b 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -75,11 +75,6 @@ "cacheVariables": { "CMAKE_C_COMPILER": "clang", "CMAKE_CXX_COMPILER": "clang++" - }, - "vendor": { - "jetbrains.com/clion": { - "toolchain": "Clang19" - } } } ], From 94834438ceb72c189ce2f19f3fa80e6a802a8358 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Mar 2025 10:20:02 +0200 Subject: [PATCH 152/156] latest toml11 --- submodules/toml11 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/submodules/toml11 b/submodules/toml11 index 499be3c..be08ba2 160000 --- a/submodules/toml11 +++ b/submodules/toml11 @@ -1 +1 @@ -Subproject commit 499be3c177bcf9b42848d5d9567153e4edfcbc8a +Subproject commit be08ba2be2a964edcdb3d3e3ea8d100abc26f286 From 9cb63ded395080c41da9aba964ed355fa4dc67fe Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Mar 2025 10:29:34 +0200 Subject: [PATCH 153/156] Fix build --- src/daemon/engine/CMakeLists.txt | 2 +- src/daemon/engine/cyphal/transport_helpers.hpp | 2 +- src/daemon/engine/engine.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index add652b..9731518 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -64,7 +64,7 @@ target_link_libraries(ocvsmd_engine ) if (${PLATFORM_OS_TYPE} STREQUAL "linux") target_sources(ocvsmd_engine - platform/can/socketcan.c + PRIVATE platform/can/socketcan.c ) target_link_libraries(ocvsmd_engine PUBLIC canard diff --git a/src/daemon/engine/cyphal/transport_helpers.hpp b/src/daemon/engine/cyphal/transport_helpers.hpp index 7c79a82..38c53dc 100644 --- a/src/daemon/engine/cyphal/transport_helpers.hpp +++ b/src/daemon/engine/cyphal/transport_helpers.hpp @@ -16,7 +16,7 @@ #include #ifdef __linux__ -#include +# include #endif namespace ocvsmd diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index b087cfc..0055076 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -34,7 +34,7 @@ #include #ifdef __linux__ -#include "cyphal/can_transport_bag.hpp" +# include "cyphal/can_transport_bag.hpp" #endif namespace ocvsmd From 273741725cfe43bde9692602907961e147be92f4 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Mon, 3 Mar 2025 13:55:22 +0200 Subject: [PATCH 154/156] revert back to Nunavut `3.0.preview` --- .gitmodules | 2 +- submodules/nunavut | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 605a658..8795069 100644 --- a/.gitmodules +++ b/.gitmodules @@ -12,7 +12,7 @@ [submodule "submodules/nunavut"] path = submodules/nunavut url = https://github.com/OpenCyphal/nunavut.git - branch = sshirokov/rpi32 + branch = 3.0.preview [submodule "submodules/cetl"] path = submodules/cetl url = https://github.com/OpenCyphal/CETL.git diff --git a/submodules/nunavut b/submodules/nunavut index cd5f563..948b75d 160000 --- a/submodules/nunavut +++ b/submodules/nunavut @@ -1 +1 @@ -Subproject commit cd5f563bd1587990cfa59fa1a7932c42be7f0dca +Subproject commit 948b75de19145eb8de01b9485aa2cd23a1494029 From 22db993c2672f4b22a371233e8d1762299684b91 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 4 Mar 2025 11:14:59 +0200 Subject: [PATCH 155/156] PR review fixes --- init.d/ocvsmd.toml | 2 +- src/daemon/engine/CMakeLists.txt | 18 +++++++----------- src/daemon/engine/engine.cpp | 5 +---- 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/init.d/ocvsmd.toml b/init.d/ocvsmd.toml index 0704807..da0f34c 100644 --- a/init.d/ocvsmd.toml +++ b/init.d/ocvsmd.toml @@ -14,7 +14,7 @@ unique_id = [] # UDP has priorioty over CAN if both types are present. # Supported formats: # - 'udp://' -# - 'socketcan:' (linux only) +# - 'socketcan:' interfaces = [ 'udp://127.0.0.1', ] diff --git a/src/daemon/engine/CMakeLists.txt b/src/daemon/engine/CMakeLists.txt index 9731518..8eaea87 100644 --- a/src/daemon/engine/CMakeLists.txt +++ b/src/daemon/engine/CMakeLists.txt @@ -36,14 +36,12 @@ target_include_directories(udpard INTERFACE SYSTEM ${submodules_dir}/libudpard/libudpard ) -if (${PLATFORM_OS_TYPE} STREQUAL "linux") - add_library(canard - ${submodules_dir}/libcanard/libcanard/canard.c - ) - target_include_directories(canard - INTERFACE SYSTEM ${submodules_dir}/libcanard/libcanard - ) -endif () +add_library(canard + ${submodules_dir}/libcanard/libcanard/canard.c +) +target_include_directories(canard + INTERFACE SYSTEM ${submodules_dir}/libcanard/libcanard +) add_library(ocvsmd_engine config.cpp @@ -59,6 +57,7 @@ add_library(ocvsmd_engine ) target_link_libraries(ocvsmd_engine PUBLIC udpard + PUBLIC canard PUBLIC ${engine_transpiled} PUBLIC ocvsmd_common ) @@ -66,9 +65,6 @@ if (${PLATFORM_OS_TYPE} STREQUAL "linux") target_sources(ocvsmd_engine PRIVATE platform/can/socketcan.c ) - target_link_libraries(ocvsmd_engine - PUBLIC canard - ) endif () target_include_directories(ocvsmd_engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/src/daemon/engine/engine.cpp b/src/daemon/engine/engine.cpp index 0055076..96db3de 100644 --- a/src/daemon/engine/engine.cpp +++ b/src/daemon/engine/engine.cpp @@ -6,6 +6,7 @@ #include "engine.hpp" #include "config.hpp" +#include "cyphal/can_transport_bag.hpp" #include "cyphal/file_provider.hpp" #include "cyphal/udp_transport_bag.hpp" #include "engine_helpers.hpp" @@ -33,10 +34,6 @@ #include #include -#ifdef __linux__ -# include "cyphal/can_transport_bag.hpp" -#endif - namespace ocvsmd { namespace daemon From 0a0cb9c84e8557a1d737bc595bbb364f2fbfdf29 Mon Sep 17 00:00:00 2001 From: Sergei Shirokov Date: Tue, 4 Mar 2025 11:17:32 +0200 Subject: [PATCH 156/156] PR review fixes --- src/daemon/engine/cyphal/transport_helpers.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/daemon/engine/cyphal/transport_helpers.hpp b/src/daemon/engine/cyphal/transport_helpers.hpp index 38c53dc..3a0f771 100644 --- a/src/daemon/engine/cyphal/transport_helpers.hpp +++ b/src/daemon/engine/cyphal/transport_helpers.hpp @@ -10,15 +10,12 @@ #include #include +#include #include #include #include -#ifdef __linux__ -# include -#endif - namespace ocvsmd { namespace daemon