From a74fb14ed04b0e6ef045ab7a25b9c0543b47e9e5 Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Sat, 2 Mar 2024 00:22:38 +0100 Subject: [PATCH] Allow deriving from connection pools --- include/tao/pq/connection_pool.hpp | 10 ++++++++-- src/lib/pq/connection_pool.cpp | 5 ----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/tao/pq/connection_pool.hpp b/include/tao/pq/connection_pool.hpp index 0b7a785..ff0841c 100644 --- a/include/tao/pq/connection_pool.hpp +++ b/include/tao/pq/connection_pool.hpp @@ -21,7 +21,7 @@ namespace tao::pq { - class connection_pool final + class connection_pool : public internal::pool< connection > { private: @@ -29,6 +29,7 @@ namespace tao::pq std::optional< std::chrono::milliseconds > m_timeout; std::function< poll::callback > m_poll; + protected: [[nodiscard]] auto v_create() const -> std::unique_ptr< pq::connection > override; [[nodiscard]] auto v_is_valid( connection& c ) const noexcept -> bool override @@ -36,6 +37,7 @@ namespace tao::pq return c.is_idle(); } + private: // pass-key idiom class private_key final { @@ -46,7 +48,11 @@ namespace tao::pq public: connection_pool( const private_key /*unused*/, const std::string_view connection_info, std::function< poll::callback > poll_cb ); - [[nodiscard]] static auto create( const std::string_view connection_info, std::function< poll::callback > poll_cb = internal::poll ) -> std::shared_ptr< connection_pool >; + template< typename T = connection_pool > + [[nodiscard]] static auto create( const std::string_view connection_info, std::function< poll::callback > poll_cb = internal::poll ) -> std::shared_ptr< T > + { + return std::make_shared< T >( private_key(), connection_info, std::move( poll_cb ) ); + } [[nodiscard]] auto timeout() const noexcept -> decltype( auto ) { diff --git a/src/lib/pq/connection_pool.cpp b/src/lib/pq/connection_pool.cpp index 9371a05..c42f576 100644 --- a/src/lib/pq/connection_pool.cpp +++ b/src/lib/pq/connection_pool.cpp @@ -16,11 +16,6 @@ namespace tao::pq m_poll( std::move( poll_cb ) ) {} - auto connection_pool::create( const std::string_view connection_info, std::function< poll::callback > poll_cb ) -> std::shared_ptr< connection_pool > - { - return std::make_shared< connection_pool >( private_key(), connection_info, std::move( poll_cb ) ); - } - void connection_pool::set_timeout( const std::chrono::milliseconds timeout ) noexcept { m_timeout = timeout;