Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions src/mongocxx/include/mongocxx/v1/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
#include <mongocxx/v1/uri-fwd.hpp>

#include <bsoncxx/v1/stdx/optional.hpp>
#include <bsoncxx/v1/stdx/string_view.hpp>

#include <mongocxx/v1/client.hpp> // IWYU pragma: export
#include <mongocxx/v1/config/export.hpp>

#include <cstddef>
#include <string>
#include <system_error>
#include <type_traits>

namespace mongocxx {
namespace v1 {
Expand Down Expand Up @@ -80,14 +81,14 @@ class pool {
MONGOCXX_ABI_EXPORT_CDECL(pool&) operator=(pool&& other) noexcept;

///
/// Copy construction.
/// This class is not copyable.
///
MONGOCXX_ABI_EXPORT_CDECL() pool(pool const& other);
pool(pool const& other) = delete;

///
/// Copy assignment.
/// This class is not copyable.
///
MONGOCXX_ABI_EXPORT_CDECL(pool&) operator=(pool const& other);
pool& operator=(pool const& other) = delete;

///
/// Initialize this pool with the given URI.
Expand All @@ -97,9 +98,9 @@ class pool {
/// @throws mongocxx::v1::exception if a client-side error is encountered.
///
/// @{
explicit MONGOCXX_ABI_EXPORT_CDECL() pool(v1::uri const& uri, options const& opts);
MONGOCXX_ABI_EXPORT_CDECL() pool(v1::uri const& uri, options opts);

explicit MONGOCXX_ABI_EXPORT_CDECL() pool(v1::uri const& uri);
/* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() pool(v1::uri const& uri);
/// @}
///

Expand All @@ -112,6 +113,11 @@ class pool {
///
explicit MONGOCXX_ABI_EXPORT_CDECL() pool();

///
/// Return true when `*this` is NOT in an assign-or-destroy-only state.
///
explicit MONGOCXX_ABI_EXPORT_CDECL() operator bool() const;

///
/// Return a client object associated with this pool.
///
Expand Down Expand Up @@ -166,6 +172,11 @@ class pool {
friend std::error_code make_error_code(errc v) {
return {static_cast<int>(v), error_category()};
}

class internal;

private:
/* explicit(false) */ pool(void* impl);
};

///
Expand Down Expand Up @@ -213,7 +224,7 @@ class pool::options {
///
/// Initialize with client options to apply to a pool.
///
MONGOCXX_ABI_EXPORT_CDECL() options(v1::client::options opts);
/* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() options(v1::client::options opts);

///
/// Default initialization.
Expand All @@ -227,6 +238,8 @@ class pool::options {
/// Return the current client options.
///
MONGOCXX_ABI_EXPORT_CDECL(v1::client::options) client_opts() const;

class internal;
};

///
Expand Down Expand Up @@ -262,24 +275,24 @@ class pool::entry {
MONGOCXX_ABI_EXPORT_CDECL(entry&) operator=(entry&& other) noexcept;

///
/// Copy construction.
/// This class is not copyable.
///
MONGOCXX_ABI_EXPORT_CDECL() entry(entry const& other);
entry(entry const& other) = delete;

///
/// Copy assignment.
/// This class is not copyable.
///
MONGOCXX_ABI_EXPORT_CDECL(entry&) operator=(entry const& other);
entry& operator=(entry const& other) = delete;

///
/// Access the managed client.
///
MONGOCXX_ABI_EXPORT_CDECL(client*) operator->();
MONGOCXX_ABI_EXPORT_CDECL(v1::client*) operator->();

///
/// Access the managed client.
///
MONGOCXX_ABI_EXPORT_CDECL(client&) operator*();
MONGOCXX_ABI_EXPORT_CDECL(v1::client&) operator*();

///
/// Explicitly release the managed client object back to the associated pool.
Expand All @@ -297,12 +310,28 @@ class pool::entry {
///
/// Equivalent to `(*this)->database(name)`.
///
mongocxx::v1::database operator[](std::string name);
/// @{
MONGOCXX_ABI_EXPORT_CDECL(v1::database) database(bsoncxx::v1::stdx::string_view name);
MONGOCXX_ABI_EXPORT_CDECL(v1::database) operator[](bsoncxx::v1::stdx::string_view name);
/// @}
///

class internal;

private:
/* explicit(false) */ entry(void* impl);
};

} // namespace v1
} // namespace mongocxx

namespace std {

template <>
struct is_error_code_enum<mongocxx::v1::pool::errc> : true_type {};

} // namespace std

#include <mongocxx/v1/detail/postlude.hpp>

///
Expand Down
2 changes: 2 additions & 0 deletions src/mongocxx/include/mongocxx/v1/server_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ class server_api {
friend std::error_code make_error_code(errc v) {
return {static_cast<int>(v), error_category()};
}

class internal;
};

} // namespace v1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#pragma once

#include <mongocxx/v1/pool-fwd.hpp>

#include <mongocxx/config/prelude.hpp>

namespace mongocxx {
Expand All @@ -29,7 +31,7 @@ class pool;
namespace mongocxx {
namespace options {

using ::mongocxx::v_noabi::options::pool;
using v_noabi::options::pool;

} // namespace options
} // namespace mongocxx
Expand All @@ -40,3 +42,6 @@ using ::mongocxx::v_noabi::options::pool;
/// @file
/// Declares @ref mongocxx::v_noabi::options::pool.
///
/// @par Includes
/// - @ref mongocxx/v1/pool-fwd.hpp
///
22 changes: 19 additions & 3 deletions src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

#include <mongocxx/options/pool-fwd.hpp> // IWYU pragma: export

//

#include <mongocxx/v1/pool.hpp> // IWYU pragma: export

#include <utility>

#include <mongocxx/options/client.hpp>

#include <mongocxx/config/prelude.hpp>
Expand All @@ -29,24 +35,31 @@ namespace options {
///
class pool {
public:
///
/// Default initialization.
///
pool() = default;

///
/// Constructs a new pool options object. Note that options::pool is implictly convertible from
/// options::client.
///
/// @param client_opts
/// The client options.
///
MONGOCXX_ABI_EXPORT_CDECL() pool(client client_opts = client());
pool(v_noabi::options::client client_opts) : _client_opts{std::move(client_opts)} {}

///
/// The current client options.
///
/// @return The client options.
///
MONGOCXX_ABI_EXPORT_CDECL(client const&) client_opts() const;
v_noabi::options::client const& client_opts() const {
return _client_opts;
}

private:
client _client_opts;
v_noabi::options::client _client_opts;
};

} // namespace options
Expand All @@ -59,3 +72,6 @@ class pool {
/// @file
/// Provides @ref mongocxx::v_noabi::options::pool.
///
/// @par Includes
/// - @ref mongocxx/v1/pool.hpp
///
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#pragma once

#include <mongocxx/v1/pool-fwd.hpp>

#include <mongocxx/config/prelude.hpp>

namespace mongocxx {
Expand All @@ -26,7 +28,7 @@ class pool;

namespace mongocxx {

using ::mongocxx::v_noabi::pool;
using v_noabi::pool;

} // namespace mongocxx

Expand All @@ -36,3 +38,6 @@ using ::mongocxx::v_noabi::pool;
/// @file
/// Declares @ref mongocxx::v_noabi::pool.
///
/// @par Includes
/// - @ref mongocxx/v1/pool-fwd.hpp
///
66 changes: 41 additions & 25 deletions src/mongocxx/include/mongocxx/v_noabi/mongocxx/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@

#pragma once

#include <mongocxx/pool-fwd.hpp> // IWYU pragma: export

//

#include <mongocxx/v1/pool.hpp> // IWYU pragma: export

#include <cstddef>
#include <functional>
#include <memory>
#include <utility>

#include <mongocxx/options/auto_encryption-fwd.hpp>
#include <mongocxx/pool-fwd.hpp> // IWYU pragma: export
#include <mongocxx/options/auto_encryption-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.

#include <bsoncxx/stdx/optional.hpp>
#include <bsoncxx/string/view_or_value.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/database.hpp>
Expand Down Expand Up @@ -52,6 +60,9 @@ namespace v_noabi {
/// connection pool should be used even if the application itself is single-threaded.
///
class pool {
private:
v1::pool _pool;

public:
///
/// Creates a pool associated with a connection string.
Expand All @@ -65,13 +76,13 @@ class pool {
/// or
/// provided client options).
explicit MONGOCXX_ABI_EXPORT_CDECL() pool(
uri const& mongodb_uri = mongocxx::v_noabi::uri(),
options::pool const& options = options::pool());
v_noabi::uri const& mongodb_uri = {},
v_noabi::options::pool const& options = {});

///
/// Destroys a pool.
///
MONGOCXX_ABI_EXPORT_CDECL() ~pool();
~pool() = default;

pool(pool&&) = delete;
pool& operator=(pool&&) = delete;
Expand All @@ -90,35 +101,43 @@ class pool {
class entry {
public:
/// Access a member of the client instance.
MONGOCXX_ABI_EXPORT_CDECL(client*) operator->() const& noexcept;
client* operator->() && = delete;
v_noabi::client* operator->() const& noexcept {
return _client.get();
}
v_noabi::client* operator->() && = delete;

/// Retrieve a reference to the client.
MONGOCXX_ABI_EXPORT_CDECL(client&) operator*() const& noexcept;
client& operator*() && = delete;
v_noabi::client& operator*() const& noexcept {
return *_client;
}
v_noabi::client& operator*() && = delete;

/// Assign nullptr to this entry to release its client to the pool.
MONGOCXX_ABI_EXPORT_CDECL(entry&) operator=(std::nullptr_t) noexcept;
entry& operator=(std::nullptr_t) noexcept {
_client.reset();
return *this;
}

/// Return true if this entry has a client acquired from the pool.
explicit MONGOCXX_ABI_EXPORT_CDECL() operator bool() const noexcept;
explicit operator bool() const noexcept {
return _client != nullptr;
}

// Allows the pool_entry["db_name"] syntax to be used to access a database within the
// entry's underlying client.
mongocxx::v_noabi::database operator[](bsoncxx::v_noabi::string::view_or_value name) const& {
return (**this)[name];
return (*_client)[name];
}

mongocxx::v_noabi::database operator[](bsoncxx::v_noabi::string::view_or_value name) && = delete;

private:
friend ::mongocxx::v_noabi::pool;
class internal;

using unique_client = std::unique_ptr<client, std::function<void MONGOCXX_ABI_CDECL(client*)>>;
private:
using ptr_type = std::unique_ptr<v_noabi::client, std::function<void MONGOCXX_ABI_CDECL(v_noabi::client*)>>;

explicit entry(unique_client);
ptr_type _client;

unique_client _client;
/* explicit(false) */ entry(ptr_type client) : _client{std::move(client)} {}
};

///
Expand All @@ -133,13 +152,7 @@ class pool {
///
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<entry>) try_acquire();

private:
friend ::mongocxx::v_noabi::options::auto_encryption;

void _release(client* client);

class impl;
std::unique_ptr<impl> const _impl;
class internal;
};

} // namespace v_noabi
Expand All @@ -151,3 +164,6 @@ class pool {
/// @file
/// Provides @ref mongocxx::v_noabi::pool.
///
/// @par Includes
/// - @ref mongocxx/v1/pool.hpp
///
Loading