From e794f155fa13842c08d0392ac252b285475f76ab Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Wed, 12 Jun 2024 11:52:10 +0200 Subject: Store services in ServeApi by value. * No stackoverflow is possible: there is just one instance of ServeApi and services are relatively 'light'; * Services are not optional and cannot be changed during their use; * operator-> is not free. --- src/buildtool/serve_api/remote/serve_api.hpp | 53 +++++++++++++--------------- 1 file changed, 25 insertions(+), 28 deletions(-) (limited to 'src/buildtool/serve_api/remote/serve_api.hpp') diff --git a/src/buildtool/serve_api/remote/serve_api.hpp b/src/buildtool/serve_api/remote/serve_api.hpp index b7c40a7f..a97b0bed 100644 --- a/src/buildtool/serve_api/remote/serve_api.hpp +++ b/src/buildtool/serve_api/remote/serve_api.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "src/buildtool/common/artifact.hpp" #include "src/buildtool/common/artifact_digest.hpp" @@ -31,22 +32,22 @@ class ServeApi final { public: - ServeApi(ServeApi const&) = delete; - ~ServeApi() = default; - - auto operator=(ServeApi const&) -> ServeApi& = delete; - auto operator=(ServeApi&&) -> ServeApi& = delete; - - [[nodiscard]] static auto Instance() noexcept -> ServeApi& { + [[nodiscard]] static auto Instance() noexcept -> ServeApi const& { static ServeApi instance = ServeApi::init(); return instance; } + ~ServeApi() noexcept = default; + ServeApi(ServeApi const&) = delete; + ServeApi(ServeApi&&) = delete; + auto operator=(ServeApi const&) -> ServeApi& = delete; + auto operator=(ServeApi&&) -> ServeApi& = delete; + [[nodiscard]] auto RetrieveTreeFromCommit(std::string const& commit, std::string const& subdir = ".", bool sync_tree = false) const noexcept -> std::variant { - return stc_->ServeCommitTree(commit, subdir, sync_tree); + return stc_.ServeCommitTree(commit, subdir, sync_tree); } [[nodiscard]] auto RetrieveTreeFromArchive( @@ -56,7 +57,7 @@ class ServeApi final { std::optional const& resolve_symlinks = std::nullopt, bool sync_tree = false) const noexcept -> std::variant { - return stc_->ServeArchiveTree( + return stc_.ServeArchiveTree( content, archive_type, subdir, resolve_symlinks, sync_tree); } @@ -65,71 +66,67 @@ class ServeApi final { distfiles, bool sync_tree = false) const noexcept -> std::variant { - return stc_->ServeDistdirTree(distfiles, sync_tree); + return stc_.ServeDistdirTree(distfiles, sync_tree); } [[nodiscard]] auto RetrieveTreeFromForeignFile( const std::string& content, const std::string& name, bool executable) const noexcept -> std::variant { - return stc_->ServeForeignFileTree(content, name, executable); + return stc_.ServeForeignFileTree(content, name, executable); } [[nodiscard]] auto ContentInRemoteCAS( std::string const& content) const noexcept -> bool { - return stc_->ServeContent(content); + return stc_.ServeContent(content); } [[nodiscard]] auto TreeInRemoteCAS( std::string const& tree_id) const noexcept -> bool { - return stc_->ServeTree(tree_id); + return stc_.ServeTree(tree_id); } [[nodiscard]] auto CheckRootTree(std::string const& tree_id) const noexcept -> std::optional { - return stc_->CheckRootTree(tree_id); + return stc_.CheckRootTree(tree_id); } [[nodiscard]] auto GetTreeFromRemote( std::string const& tree_id) const noexcept -> bool { - return stc_->GetRemoteTree(tree_id); + return stc_.GetRemoteTree(tree_id); } [[nodiscard]] auto ServeTargetVariables(std::string const& target_root_id, std::string const& target_file, std::string const& target) const noexcept -> std::optional> { - return tc_->ServeTargetVariables(target_root_id, target_file, target); + return tc_.ServeTargetVariables(target_root_id, target_file, target); } [[nodiscard]] auto ServeTargetDescription(std::string const& target_root_id, std::string const& target_file, std::string const& target) const noexcept -> std::optional { - return tc_->ServeTargetDescription(target_root_id, target_file, target); + return tc_.ServeTargetDescription(target_root_id, target_file, target); } [[nodiscard]] auto ServeTarget(const TargetCacheKey& key, const std::string& repo_key) const noexcept -> std::optional { - return tc_->ServeTarget(key, repo_key); + return tc_.ServeTarget(key, repo_key); } [[nodiscard]] auto CheckServeRemoteExecution() const noexcept -> bool { - return cc_->CheckServeRemoteExecution(); + return cc_.CheckServeRemoteExecution(); } [[nodiscard]] auto IsCompatible() const noexcept -> std::optional { - return cc_->IsCompatible(); + return cc_.IsCompatible(); } private: ServeApi(std::string const& host, Port port) noexcept - : stc_{std::make_unique(host, port)}, - tc_{std::make_unique(host, port)}, - cc_{std::make_unique(host, port)} {} - - ServeApi(ServeApi&& other) noexcept = default; + : stc_{host, port}, tc_{host, port}, cc_{host, port} {} [[nodiscard]] static auto init() noexcept -> ServeApi { auto sadd = RemoteServeConfig::Instance().RemoteAddress(); @@ -137,11 +134,11 @@ class ServeApi final { } // source tree service client - std::unique_ptr stc_; + SourceTreeClient const stc_; // target service client - std::unique_ptr tc_; + TargetClient const tc_; // configuration service client - std::unique_ptr cc_; + ConfigurationClient const cc_; }; #endif // INCLUDED_SRC_BUILDTOOL_SERVE_API_REMOTE_SERVE_API_HPP -- cgit v1.2.3