userver: userver/storages/postgres/detail/connection_ptr.hpp Source File
Loading...
Searching...
No Matches
connection_ptr.hpp
1#pragma once
2
3#include <cstddef>
4#include <memory>
5#include <optional>
6
7#include <userver/dynamic_config/source.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace storages::postgres::detail {
12
13class Connection;
14class ConnectionPool;
15class StatementStatsStorage;
16
17/// Pointer-like class that controls lifetime of a parent pool by keeping smart
18/// pointer to it.
19class ConnectionPtr {
20 public:
21 explicit ConnectionPtr(std::unique_ptr<Connection>&& conn);
22 ConnectionPtr(Connection* conn, std::shared_ptr<ConnectionPool>&& pool);
23 ~ConnectionPtr();
24
25 ConnectionPtr(ConnectionPtr&&) noexcept;
26 ConnectionPtr& operator=(ConnectionPtr&&) noexcept;
27
28 explicit operator bool() const noexcept;
29 Connection* get() const noexcept;
30
31 Connection& operator*() const;
32 Connection* operator->() const noexcept;
33
34 const StatementStatsStorage* GetStatementStatsStorage() const;
35 std::optional<dynamic_config::Source> GetConfigSource() const;
36
37 private:
38 void Reset(std::unique_ptr<Connection> conn,
39 std::shared_ptr<ConnectionPool> pool);
40 void Release();
41
42 std::shared_ptr<ConnectionPool> pool_;
43 std::unique_ptr<Connection> conn_;
44};
45
46} // namespace storages::postgres::detail
47
48USERVER_NAMESPACE_END