From 74116cb709466025b6519c0fb2f69fa90b5eeb86 Mon Sep 17 00:00:00 2001 From: sitaowang1998 Date: Tue, 7 Jan 2025 17:31:24 -0500 Subject: [PATCH] build: Add headers specification in CMakeLists.txt and remove spdlog in public headers (#46) --- src/spider/CMakeLists.txt | 18 ++++++++++-- src/spider/worker/FunctionManager.cpp | 42 +++++++++++++++++++++++++++ src/spider/worker/FunctionManager.hpp | 41 ++------------------------ tests/worker/worker-test.cpp | 10 +++---- tests/worker/worker-test.hpp | 4 +-- 5 files changed, 67 insertions(+), 48 deletions(-) diff --git a/src/spider/CMakeLists.txt b/src/spider/CMakeLists.txt index 35f7fb1..dde50cd 100644 --- a/src/spider/CMakeLists.txt +++ b/src/spider/CMakeLists.txt @@ -32,7 +32,14 @@ set(SPIDER_CORE_HEADERS add_library(spider_core) target_sources(spider_core PRIVATE ${SPIDER_CORE_SOURCES}) -target_sources(spider_core PUBLIC ${SPIDER_CORE_HEADERS}) +target_sources( + spider_core + PUBLIC + FILE_SET spider_core_headers + TYPE HEADERS + BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/.. + FILES ${SPIDER_CORE_HEADERS} +) target_link_libraries( spider_core PUBLIC @@ -150,7 +157,14 @@ set(SPIDER_CLIENT_SHARED_HEADERS add_library(spider_client) target_sources(spider_client PRIVATE ${SPIDER_CLIENT_SHARED_SOURCES}) -target_sources(spider_client PUBLIC ${SPIDER_CLIENT_SHARED_HEADERS}) +target_sources( + spider_client + PUBLIC + FILE_SET spider_client_shared_headers + TYPE HEADERS + BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/.. + FILES ${SPIDER_CLIENT_SHARED_HEADERS} +) target_link_libraries( spider_client PUBLIC diff --git a/src/spider/worker/FunctionManager.cpp b/src/spider/worker/FunctionManager.cpp index 99af40b..63c3c75 100644 --- a/src/spider/worker/FunctionManager.cpp +++ b/src/spider/worker/FunctionManager.cpp @@ -1,10 +1,14 @@ #include "FunctionManager.hpp" +#include #include #include #include +#include +#include #include +#include #include "../io/MsgPack.hpp" // IWYU pragma: keep #include "TaskExecutorMessage.hpp" @@ -60,6 +64,44 @@ void create_error_buffer( packer.pack(message); } +auto response_get_result_buffers(msgpack::sbuffer const& buffer +) -> std::optional> { + // NOLINTBEGIN(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic) + try { + std::vector result_buffers; + msgpack::object_handle const handle = msgpack::unpack(buffer.data(), buffer.size()); + msgpack::object const object = handle.get(); + + if (msgpack::type::ARRAY != object.type || object.via.array.size < 2) { + spdlog::error("Cannot split result into buffers: Wrong type"); + return std::nullopt; + } + + if (worker::TaskExecutorResponseType::Result + != object.via.array.ptr[0].as()) + { + spdlog::error( + "Cannot split result into buffers: Wrong response type {}", + static_cast>( + object.via.array.ptr[0].as() + ) + ); + return std::nullopt; + } + + for (size_t i = 1; i < object.via.array.size; ++i) { + msgpack::object const& obj = object.via.array.ptr[i]; + result_buffers.emplace_back(); + msgpack::pack(result_buffers.back(), obj); + } + return result_buffers; + } catch (msgpack::type_error& e) { + spdlog::error("Cannot split result into buffers: {}", e.what()); + return std::nullopt; + } + // NOLINTEND(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic) +} + auto FunctionManager::get_function(std::string const& name) const -> Function const* { if (auto const func_iter = m_function_map.find(name); func_iter != m_function_map.end()) { return &(func_iter->second); diff --git a/src/spider/worker/FunctionManager.hpp b/src/spider/worker/FunctionManager.hpp index 28571e6..4d5b7f9 100644 --- a/src/spider/worker/FunctionManager.hpp +++ b/src/spider/worker/FunctionManager.hpp @@ -1,7 +1,6 @@ #ifndef SPIDER_WORKER_FUNCTIONMANAGER_HPP #define SPIDER_WORKER_FUNCTIONMANAGER_HPP -#include #include #include #include @@ -16,7 +15,6 @@ #include #include #include -#include #include "../client/Data.hpp" #include "../client/task.hpp" @@ -162,43 +160,8 @@ auto response_get_result(msgpack::sbuffer const& buffer) -> std::optional std::optional> { - // NOLINTBEGIN(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic) - try { - std::vector result_buffers; - msgpack::object_handle const handle = msgpack::unpack(buffer.data(), buffer.size()); - msgpack::object const object = handle.get(); - - if (msgpack::type::ARRAY != object.type || object.via.array.size < 2) { - spdlog::error("Cannot split result into buffers: Wrong type"); - return std::nullopt; - } - - if (worker::TaskExecutorResponseType::Result - != object.via.array.ptr[0].as()) - { - spdlog::error( - "Cannot split result into buffers: Wrong response type {}", - static_cast>( - object.via.array.ptr[0].as() - ) - ); - return std::nullopt; - } - - for (size_t i = 1; i < object.via.array.size; ++i) { - msgpack::object const& obj = object.via.array.ptr[i]; - result_buffers.emplace_back(); - msgpack::pack(result_buffers.back(), obj); - } - return result_buffers; - } catch (msgpack::type_error& e) { - spdlog::error("Cannot split result into buffers: {}", e.what()); - return std::nullopt; - } - // NOLINTEND(cppcoreguidelines-pro-type-union-access,cppcoreguidelines-pro-bounds-pointer-arithmetic) -} +auto response_get_result_buffers(msgpack::sbuffer const& buffer +) -> std::optional>; template auto create_result_response(T const& t) -> msgpack::sbuffer { diff --git a/tests/worker/worker-test.cpp b/tests/worker/worker-test.cpp index 9e52c51..9934030 100644 --- a/tests/worker/worker-test.cpp +++ b/tests/worker/worker-test.cpp @@ -4,11 +4,11 @@ #include #include -#include "../../src/spider/client/Data.hpp" -#include "../../src/spider/client/Driver.hpp" -#include "../../src/spider/client/Job.hpp" -#include "../../src/spider/client/TaskContext.hpp" -#include "../../src/spider/client/TaskGraph.hpp" +#include +#include +#include +#include +#include auto sum_test(spider::TaskContext& /*context*/, int const x, int const y) -> int { std::cerr << x << " + " << y << " = " << x + y << "\n"; diff --git a/tests/worker/worker-test.hpp b/tests/worker/worker-test.hpp index ae85097..1ae0002 100644 --- a/tests/worker/worker-test.hpp +++ b/tests/worker/worker-test.hpp @@ -1,8 +1,8 @@ #ifndef SPIDER_TEST_WORKER_TEST_HPP #define SPIDER_TEST_WORKER_TEST_HPP -#include "../../src/spider/client/Data.hpp" -#include "../../src/spider/client/TaskContext.hpp" +#include +#include auto sum_test(spider::TaskContext& /*context*/, int x, int y) -> int;