From 587f8b25dbd324bd1fb44a5da5822f291d4906ab Mon Sep 17 00:00:00 2001 From: sha512sum Date: Tue, 19 Mar 2024 16:04:26 +0000 Subject: [PATCH] Add options to component system --- include/cserver/clients/http/component.hpp | 4 +- .../cserver/engine/basic/task_processor.hpp | 4 +- include/cserver/engine/components.hpp | 58 +++++++++++++------ .../server/handlers/http_handler_base.hpp | 6 +- include/cserver/server/server/server.hpp | 15 +---- 5 files changed, 49 insertions(+), 38 deletions(-) diff --git a/include/cserver/clients/http/component.hpp b/include/cserver/clients/http/component.hpp index 10d4b6e..0e0d32c 100644 --- a/include/cserver/clients/http/component.hpp +++ b/include/cserver/clients/http/component.hpp @@ -34,11 +34,11 @@ struct HttpClient { ctx(boost::asio::ssl::context::method::sslv23_client), resolver(this->taskProcessor.ioContext) {} - template + template static consteval auto Adder(const T& context) { using Type = std::remove_cvref_t())>; return context.TransformComponents( - [&](const ComponentConfig>&) -> ComponentConfig> { + [&](const ComponentConfig, Options>&) -> ComponentConfig, Options> { return {}; }); }; diff --git a/include/cserver/engine/basic/task_processor.hpp b/include/cserver/engine/basic/task_processor.hpp index f223e89..fa979c6 100644 --- a/include/cserver/engine/basic/task_processor.hpp +++ b/include/cserver/engine/basic/task_processor.hpp @@ -15,11 +15,11 @@ struct TaskProcessor { pool{} { }; - template + template static consteval auto Adder(const T& context) { constexpr std::size_t Count = T::kConfig.template Get().template Get<"threadPoolSize">(); return context.TransformComponents( - [&](const ComponentConfig>&) -> ComponentConfig> { + [&](const ComponentConfig, Options>&) -> ComponentConfig, Options> { return {}; }); }; diff --git a/include/cserver/engine/components.hpp b/include/cserver/engine/components.hpp index 46f5774..e7e62b3 100644 --- a/include/cserver/engine/components.hpp +++ b/include/cserver/engine/components.hpp @@ -3,14 +3,30 @@ #include namespace cserver { +struct SeparatedComponent {}; +template +using Options = utempl::TypeList; + +template +struct ComponentConfig {}; + +namespace impl { template struct NamedValue { T value; }; -template -struct ComponentConfig {}; +template +using GetTypeFromComponentConfig = decltype( + [](const ComponentConfig&) -> TT { + }(std::declval())); + +template +inline constexpr utempl::ConstexprString kNameFromComponentConfig = + decltype([](const ComponentConfig&) { + return utempl::Wrapper{}; + }(std::declval()))::kValue; template inline constexpr auto TransformIfOk(T&& value, auto&& f) { @@ -22,12 +38,14 @@ inline constexpr auto TransformIfOk(T&& value, auto&& f) { }; +} // namespace impl + template struct ConstexprConfig { utempl::Tuple data; template inline constexpr auto Get() const -> auto { - return [&](const ConstexprConfig...>&){ + return [&](const ConstexprConfig...>&){ constexpr auto list = utempl::TypeList...>{}; constexpr std::size_t I = Find>(list); if constexpr(I < sizeof...(Ts)) { @@ -36,22 +54,23 @@ struct ConstexprConfig { }(*this); }; template - inline constexpr auto Append(T&& value) const -> ConstexprConfig>> { - return {.data = TupleCat(this->data, utempl::MakeTuple(NamedValue>{std::forward(value)}))}; + inline constexpr auto Append(T&& value) const -> ConstexprConfig>> { + return {.data = TupleCat(this->data, utempl::MakeTuple(impl::NamedValue>{std::forward(value)}))}; }; }; -template +template struct ServiceContext { - utempl::Tuple storage; static constexpr auto kConfig = config; static constexpr auto kList = utempl::TypeList{}; + static constexpr auto kThreadsCount = config.template Get<"threads">(); + utempl::Tuple storage; inline constexpr ServiceContext() : storage{ [&](std::index_sequence) -> utempl::Tuple { return {[&](utempl::Wrapper) { return [&]() -> std::remove_cvref_t(storage))> { - return {decltype(Get(names)){}, *this}; + return {utempl::Wrapper(names)>{}, *this}; }; }(utempl::Wrapper{})...}; }(std::index_sequence_for()) @@ -67,7 +86,7 @@ struct ServiceContext { }; template inline constexpr auto FindComponent() -> auto& { - constexpr auto I = utempl::Find>(names); + constexpr auto I = utempl::Find(names, name); return Get(this->storage); }; template @@ -84,14 +103,14 @@ template struct ServiceContextBuilder { static constexpr auto kList = utempl::kTypeList; static constexpr auto kConfig = config; - template - static consteval auto Append() -> decltype(T::template Adder(ServiceContextBuilder>{})) { + template + static consteval auto Append(Options = {}) -> decltype(T::template Adder{}>(ServiceContextBuilder{}>>{})) { return {}; }; - template - static consteval auto Append() -> ServiceContextBuilder> - requires (!requires(ServiceContextBuilder> builder) {T::template Adder(builder);}) { + template + static consteval auto Append(Options = {}) -> ServiceContextBuilder{}>> + requires (!requires(ServiceContextBuilder{}>> builder) {T::template Adder{}>(builder);}) { return {}; }; @@ -102,13 +121,13 @@ struct ServiceContextBuilder { }; template - static consteval auto TransformComponents(F&& f) -> ServiceContextBuilder { + static consteval auto TransformComponents(F&& f) -> ServiceContextBuilder { return {}; }; template static consteval auto FindComponent() { - return [] - (const ServiceContextBuilder...>&) + return [] + (const ServiceContextBuilder...>&) -> decltype(utempl::Get>(utempl::TypeList...>{})>(utempl::TypeList{})) { std::unreachable(); }(ServiceContextBuilder{}); @@ -119,8 +138,9 @@ struct ServiceContextBuilder { }; static constexpr auto Run() -> void { - [](utempl::TypeList...>) { - ServiceContext...>, TTs...> context; + [] + (utempl::TypeList...>) { + ServiceContext context; context.Run(); for(;;) { std::this_thread::sleep_for(std::chrono::minutes(1)); diff --git a/include/cserver/server/handlers/http_handler_base.hpp b/include/cserver/server/handlers/http_handler_base.hpp index a3eaa98..12a199c 100644 --- a/include/cserver/server/handlers/http_handler_base.hpp +++ b/include/cserver/server/handlers/http_handler_base.hpp @@ -8,10 +8,10 @@ namespace cserver::server::handlers { struct HTTPHandlerBase { - template + template static consteval auto Adder(const auto& context) { - return context.TransformComponents([](const ComponentConfig) { - return ComponentConfig>>{}; + return context.TransformComponents([](const ComponentConfig) { + return ComponentConfig>, Options>{}; }); }; template diff --git a/include/cserver/server/server/server.hpp b/include/cserver/server/server/server.hpp index 13cfe25..f864ca5 100644 --- a/include/cserver/server/server/server.hpp +++ b/include/cserver/server/server/server.hpp @@ -8,16 +8,7 @@ namespace cserver::server::server { -namespace impl { -template -using GetTypeFromComponentConfig = decltype([](const ComponentConfig&) -> TT {}(std::declval())); -template -inline constexpr utempl::ConstexprString kNameFromComponentConfig = - decltype([](const ComponentConfig&) { - return utempl::Wrapper{}; - } (std::declval()))::kValue; -} // namespace impl template struct Server { @@ -27,7 +18,7 @@ struct Server { unsigned short port; static constexpr utempl::Tuple kNames = {impl::kNameFromComponentConfig...}; static constexpr utempl::Tuple kPaths = {impl::GetTypeFromComponentConfig::kPath...}; - template + template static consteval auto Adder(const T& context) { constexpr utempl::ConstexprString tpName = [&]{ if constexpr(requires{T::kConfig.template Get().template Get<"taskProcessor">();}) { @@ -38,7 +29,7 @@ struct Server { }(); using TP = decltype(context.template FindComponent()); return context.TransformComponents( - [&](const ComponentConfig>&) -> ComponentConfig> { + [&](const ComponentConfig, Options>&) -> ComponentConfig, Options> { return {}; }); }; @@ -88,7 +79,7 @@ struct Server { co_await boost::asio::async_write(socket, boost::asio::buffer(error404.data(), error404.size()), boost::asio::use_awaitable); }; }; - auto Task() -> cserver::Task { + auto Task() -> Task { auto executor = co_await boost::asio::this_coro::executor; boost::asio::ip::tcp::acceptor acceptor{executor, {boost::asio::ip::tcp::v6(), this->port}}; for(;;) {