diff --git a/src/mongocxx/include/mongocxx/v1/find_one_and_update_options.hpp b/src/mongocxx/include/mongocxx/v1/find_one_and_update_options.hpp index ffaa0a6a1e..24b763def2 100644 --- a/src/mongocxx/include/mongocxx/v1/find_one_and_update_options.hpp +++ b/src/mongocxx/include/mongocxx/v1/find_one_and_update_options.hpp @@ -183,7 +183,7 @@ class find_one_and_update_options { /// /// Set the "returnDocument" field. /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update_options&) return_document(return_document return_document); + MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update_options&) return_document(v1::return_document return_document); /// /// Return the current "returnDocument" field. @@ -229,6 +229,8 @@ class find_one_and_update_options { /// Return the current "arrayFilters" field. /// MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v1::stdx::optional) array_filters() const; + + class internal; }; } // namespace v1 diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update-fwd.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update-fwd.hpp index 2463038611..c25b9a99ec 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update-fwd.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update-fwd.hpp @@ -14,6 +14,8 @@ #pragma once +#include // IWYU pragma: export + #include namespace mongocxx { @@ -29,7 +31,7 @@ class find_one_and_update; namespace mongocxx { namespace options { -using ::mongocxx::v_noabi::options::find_one_and_update; +using v_noabi::options::find_one_and_update; } // namespace options } // namespace mongocxx @@ -40,3 +42,6 @@ using ::mongocxx::v_noabi::options::find_one_and_update; /// @file /// Declares @ref mongocxx::v_noabi::options::find_one_and_update. /// +/// @par Includes +/// - @ref mongocxx/v1/find_one_and_update_options-fwd.hpp +/// diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update.hpp index 8177b7087c..51a6903f25 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/find_one_and_update.hpp @@ -14,14 +14,26 @@ #pragma once +#include // IWYU pragma: export + +// + +#include +#include +#include + +#include // IWYU pragma: export + #include #include // IWYU pragma: keep: backward compatibility, to be removed. +#include -#include // IWYU pragma: export - +#include #include +#include #include #include +#include #include #include @@ -39,6 +51,76 @@ namespace options { /// class find_one_and_update { public: + /// + /// Default initialization. + /// + find_one_and_update() = default; + + /// + /// Construct with the @ref mongocxx::v1 equivalent. + /// + /* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() find_one_and_update(v1::find_one_and_update_options opts); + + /// + /// Convert to the @ref mongocxx::v1 equivalent. + /// + explicit operator v1::find_one_and_update_options() const { + using bsoncxx::v_noabi::to_v1; + using mongocxx::v_noabi::to_v1; + + v1::find_one_and_update_options ret; + + if (_bypass_document_validation) { + ret.bypass_document_validation(*_bypass_document_validation); + } + + if (_collation) { + ret.collation(bsoncxx::v1::document::value{to_v1(_collation->view())}); + } + + if (_hint) { + ret.hint(to_v1(*_hint)); + } + + if (_let) { + ret.let(bsoncxx::v1::document::value{to_v1(_let->view())}); + } + + if (_comment) { + ret.comment(bsoncxx::v1::types::value{to_v1(_comment->view())}); + } + + if (_max_time) { + ret.max_time(*_max_time); + } + + if (_projection) { + ret.projection(bsoncxx::v1::document::value{to_v1(_projection->view())}); + } + + if (_return_document) { + ret.return_document(*_return_document); + } + + if (_ordering) { + ret.sort(bsoncxx::v1::document::value{to_v1(_ordering->view())}); + } + + if (_upsert) { + ret.upsert(*_upsert); + } + + if (_write_concern) { + ret.write_concern(to_v1(*_write_concern)); + } + + if (_array_filters) { + ret.array_filters(bsoncxx::v1::array::value{to_v1(_array_filters->view())}); + } + + return ret; + } + /// Sets the collation for this operation. /// /// @param collation @@ -51,8 +133,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - collation(bsoncxx::v_noabi::document::view_or_value collation); + find_one_and_update& collation(bsoncxx::v_noabi::document::view_or_value collation) { + _collation = std::move(collation); + return *this; + } /// /// Retrieves the current collation for this operation. @@ -63,8 +147,9 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - collation() const; + bsoncxx::v_noabi::stdx::optional const& collation() const { + return _collation; + } /// /// Whether or not to bypass document validation for this operation. @@ -83,8 +168,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - bypass_document_validation(bool bypass_document_validation); + find_one_and_update& bypass_document_validation(bool bypass_document_validation) { + _bypass_document_validation = std::move(bypass_document_validation); + return *this; + } /// /// The current setting for bypassing document validation. @@ -94,8 +181,9 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - bypass_document_validation() const; + bsoncxx::v_noabi::stdx::optional const& bypass_document_validation() const { + return _bypass_document_validation; + } /// /// Sets the index to use for this operation. @@ -111,15 +199,19 @@ class find_one_and_update { /// A reference to the object on which this member function is being called. This facilitates /// method chaining. /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) hint(mongocxx::v_noabi::hint index_hint); + find_one_and_update& hint(v_noabi::hint index_hint) { + _hint = std::move(index_hint); + return *this; + } /// /// Gets the current hint. /// /// @return The current hint, if one is set. /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - hint() const; + bsoncxx::v_noabi::stdx::optional const& hint() const { + return _hint; + } /// /// Set the value of the let option. @@ -131,8 +223,10 @@ class find_one_and_update { /// A reference to the object on which this member function is being called. This facilitates /// method chaining. /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - let(bsoncxx::v_noabi::document::view_or_value let); + find_one_and_update& let(bsoncxx::v_noabi::document::view_or_value let) { + _let = std::move(let); + return *this; + } /// /// Gets the current value of the let option. @@ -140,8 +234,9 @@ class find_one_and_update { /// @return /// The current let option. /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const) - let() const; + bsoncxx::v_noabi::stdx::optional const let() const { + return _let; + } /// /// Set the value of the comment option. @@ -153,8 +248,10 @@ class find_one_and_update { /// A reference to the object on which this member function is being called. This facilitates /// method chaining. /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - comment(bsoncxx::v_noabi::types::bson_value::view_or_value comment); + find_one_and_update& comment(bsoncxx::v_noabi::types::bson_value::view_or_value comment) { + _comment = std::move(comment); + return *this; + } /// /// Gets the current value of the comment option. @@ -162,8 +259,9 @@ class find_one_and_update { /// @return /// The current comment option. /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const) - comment() const; + bsoncxx::v_noabi::stdx::optional const comment() const { + return _comment; + } /// /// Sets the maximum amount of time for this operation to run (server-side) in milliseconds. @@ -178,7 +276,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) max_time(std::chrono::milliseconds max_time); + find_one_and_update& max_time(std::chrono::milliseconds max_time) { + _max_time = std::move(max_time); + return *this; + } /// /// The current max_time setting. @@ -188,8 +289,9 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - max_time() const; + bsoncxx::v_noabi::stdx::optional const& max_time() const { + return _max_time; + } /// /// Sets a projection, which limits the fields to return. @@ -204,8 +306,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - projection(bsoncxx::v_noabi::document::view_or_value projection); + find_one_and_update& projection(bsoncxx::v_noabi::document::view_or_value projection) { + _projection = std::move(projection); + return *this; + } /// /// Gets the current projection for this operation. @@ -215,8 +319,9 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - projection() const; + bsoncxx::v_noabi::stdx::optional const& projection() const { + return _projection; + } /// /// Set the desired version of the updated document to return, either the original @@ -233,8 +338,10 @@ class find_one_and_update { /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - @ref mongocxx::v_noabi::options::return_document /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - return_document(return_document return_document); + find_one_and_update& return_document(return_document return_document) { + _return_document = std::move(return_document); + return *this; + } /// /// Which version of the updated document to return. @@ -245,8 +352,9 @@ class find_one_and_update { /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - @ref mongocxx::v_noabi::options::return_document /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - return_document() const; + bsoncxx::v_noabi::stdx::optional const& return_document() const { + return _return_document; + } /// /// Sets the order by which to search the collection for a matching document. @@ -264,8 +372,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - sort(bsoncxx::v_noabi::document::view_or_value ordering); + find_one_and_update& sort(bsoncxx::v_noabi::document::view_or_value ordering) { + _ordering = std::move(ordering); + return *this; + } /// /// Gets the current sort ordering. @@ -275,8 +385,9 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - sort() const; + bsoncxx::v_noabi::stdx::optional const& sort() const { + return _ordering; + } /// /// Sets the upsert flag on the operation. When @c true, the operation creates a new document if @@ -293,7 +404,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) upsert(bool upsert); + find_one_and_update& upsert(bool upsert) { + _upsert = std::move(upsert); + return *this; + } /// /// Gets the current upsert setting. @@ -303,7 +417,9 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) upsert() const; + bsoncxx::v_noabi::stdx::optional const& upsert() const { + return _upsert; + } /// /// Sets the write concern for this operation. @@ -318,8 +434,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - write_concern(mongocxx::v_noabi::write_concern write_concern); + find_one_and_update& write_concern(v_noabi::write_concern write_concern) { + _write_concern = std::move(write_concern); + return *this; + } /// /// Gets the current write concern. @@ -330,8 +448,9 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - write_concern() const; + bsoncxx::v_noabi::stdx::optional const& write_concern() const { + return _write_concern; + } /// /// Set array filters for this operation. @@ -346,8 +465,10 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(find_one_and_update&) - array_filters(bsoncxx::v_noabi::array::view_or_value array_filters); + find_one_and_update& array_filters(bsoncxx::v_noabi::array::view_or_value array_filters) { + _array_filters = std::move(array_filters); + return *this; + } /// /// Get array filters for this operation. @@ -358,21 +479,22 @@ class find_one_and_update { /// @see /// - https://www.mongodb.com/docs/manual/reference/command/findAndModify/ /// - MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional const&) - array_filters() const; + bsoncxx::v_noabi::stdx::optional const& array_filters() const { + return _array_filters; + } private: bsoncxx::v_noabi::stdx::optional _bypass_document_validation; bsoncxx::v_noabi::stdx::optional _collation; - bsoncxx::v_noabi::stdx::optional _hint; + bsoncxx::v_noabi::stdx::optional _hint; bsoncxx::v_noabi::stdx::optional _let; bsoncxx::v_noabi::stdx::optional _comment; bsoncxx::v_noabi::stdx::optional _max_time; bsoncxx::v_noabi::stdx::optional _projection; - bsoncxx::v_noabi::stdx::optional _return_document; + bsoncxx::v_noabi::stdx::optional _return_document; bsoncxx::v_noabi::stdx::optional _ordering; bsoncxx::v_noabi::stdx::optional _upsert; - bsoncxx::v_noabi::stdx::optional _write_concern; + bsoncxx::v_noabi::stdx::optional _write_concern; bsoncxx::v_noabi::stdx::optional _array_filters; }; @@ -380,9 +502,32 @@ class find_one_and_update { } // namespace v_noabi } // namespace mongocxx +namespace mongocxx { +namespace v_noabi { + +/// +/// Convert to the @ref mongocxx::v_noabi equivalent of `v`. +/// +inline v_noabi::options::find_one_and_update from_v1(v1::find_one_and_update_options v) { + return {std::move(v)}; +} + +/// +/// Convert to the @ref mongocxx::v1 equivalent of `v`. +/// +inline v1::find_one_and_update_options to_v1(v_noabi::options::find_one_and_update const& v) { + return v1::find_one_and_update_options{v}; +} + +} // namespace v_noabi +} // namespace mongocxx + #include /// /// @file /// Provides @ref mongocxx::v_noabi::options::find_one_and_update. /// +/// @par Includes +/// - @ref mongocxx/v1/find_one_and_update_options.hpp +/// diff --git a/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp b/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp index 8d1baee10e..680605ef1c 100644 --- a/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp +++ b/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.cpp @@ -12,4 +12,236 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include + +// + +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include + +namespace mongocxx { +namespace v1 { + +class find_one_and_update_options::impl { + public: + bsoncxx::v1::stdx::optional _collation; + bsoncxx::v1::stdx::optional _bypass_document_validation; + bsoncxx::v1::stdx::optional _hint; + bsoncxx::v1::stdx::optional _let; + bsoncxx::v1::stdx::optional _comment; + bsoncxx::v1::stdx::optional _max_time; + bsoncxx::v1::stdx::optional _projection; + bsoncxx::v1::stdx::optional _return_document; + bsoncxx::v1::stdx::optional _sort; + bsoncxx::v1::stdx::optional _upsert; + bsoncxx::v1::stdx::optional _write_concern; + bsoncxx::v1::stdx::optional _array_filters; + + static impl const& with(find_one_and_update_options const& self) { + return *static_cast(self._impl); + } + + static impl const* with(find_one_and_update_options const* self) { + return static_cast(self->_impl); + } + + static impl& with(find_one_and_update_options& self) { + return *static_cast(self._impl); + } + + static impl* with(find_one_and_update_options* self) { + return static_cast(self->_impl); + } + + static impl* with(void* ptr) { + return static_cast(ptr); + } +}; + +find_one_and_update_options::~find_one_and_update_options() { + delete impl::with(this); +} + +find_one_and_update_options::find_one_and_update_options(find_one_and_update_options&& other) noexcept + : _impl{exchange(other._impl, nullptr)} {} + +find_one_and_update_options& find_one_and_update_options::operator=(find_one_and_update_options&& other) noexcept { + if (this != &other) { + delete impl::with(exchange(_impl, exchange(other._impl, nullptr))); + } + + return *this; +} + +find_one_and_update_options::find_one_and_update_options(find_one_and_update_options const& other) + : _impl{new impl{impl::with(other)}} {} + +find_one_and_update_options& find_one_and_update_options::operator=(find_one_and_update_options const& other) { + if (this != &other) { + delete impl::with(exchange(_impl, new impl{impl::with(other)})); + } + + return *this; +} + +find_one_and_update_options::find_one_and_update_options() : _impl{new impl{}} {} + +find_one_and_update_options& find_one_and_update_options::collation(bsoncxx::v1::document::value collation) { + impl::with(this)->_collation = std::move(collation); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::collation() const { + return impl::with(this)->_collation; +} + +find_one_and_update_options& find_one_and_update_options::bypass_document_validation(bool bypass_document_validation) { + impl::with(this)->_bypass_document_validation = std::move(bypass_document_validation); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::bypass_document_validation() const { + return impl::with(this)->_bypass_document_validation; +} + +find_one_and_update_options& find_one_and_update_options::hint(v1::hint index_hint) { + impl::with(this)->_hint = std::move(index_hint); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::hint() const { + return impl::with(this)->_hint; +} + +find_one_and_update_options& find_one_and_update_options::let(bsoncxx::v1::document::value let) { + impl::with(this)->_let = std::move(let); + return *this; +} + +bsoncxx::v1::stdx::optional const find_one_and_update_options::let() const { + return impl::with(this)->_let; +} + +find_one_and_update_options& find_one_and_update_options::comment(bsoncxx::v1::types::value comment) { + impl::with(this)->_comment = std::move(comment); + return *this; +} + +bsoncxx::v1::stdx::optional const find_one_and_update_options::comment() const { + return impl::with(this)->_comment; +} + +find_one_and_update_options& find_one_and_update_options::max_time(std::chrono::milliseconds max_time) { + impl::with(this)->_max_time = std::move(max_time); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::max_time() const { + return impl::with(this)->_max_time; +} + +find_one_and_update_options& find_one_and_update_options::projection(bsoncxx::v1::document::value projection) { + impl::with(this)->_projection = std::move(projection); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::projection() const { + return impl::with(this)->_projection; +} + +find_one_and_update_options& find_one_and_update_options::return_document(v1::return_document return_document) { + impl::with(this)->_return_document = std::move(return_document); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::return_document() const { + return impl::with(this)->_return_document; +} + +find_one_and_update_options& find_one_and_update_options::sort(bsoncxx::v1::document::value ordering) { + impl::with(this)->_sort = std::move(ordering); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::sort() const { + return impl::with(this)->_sort; +} + +find_one_and_update_options& find_one_and_update_options::upsert(bool upsert) { + impl::with(this)->_upsert = std::move(upsert); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::upsert() const { + return impl::with(this)->_upsert; +} + +find_one_and_update_options& find_one_and_update_options::write_concern(v1::write_concern write_concern) { + impl::with(this)->_write_concern = std::move(write_concern); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::write_concern() const { + return impl::with(this)->_write_concern; +} + +find_one_and_update_options& find_one_and_update_options::array_filters(bsoncxx::v1::array::value array_filters) { + impl::with(this)->_array_filters = std::move(array_filters); + return *this; +} + +bsoncxx::v1::stdx::optional find_one_and_update_options::array_filters() const { + return impl::with(this)->_array_filters; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::collation( + find_one_and_update_options& self) { + return impl::with(self)._collation; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::hint(find_one_and_update_options& self) { + return impl::with(self)._hint; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::let( + find_one_and_update_options& self) { + return impl::with(self)._let; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::comment( + find_one_and_update_options& self) { + return impl::with(self)._comment; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::projection( + find_one_and_update_options& self) { + return impl::with(self)._projection; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::sort( + find_one_and_update_options& self) { + return impl::with(self)._sort; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::write_concern( + find_one_and_update_options& self) { + return impl::with(self)._write_concern; +} + +bsoncxx::v1::stdx::optional& find_one_and_update_options::internal::array_filters( + find_one_and_update_options& self) { + return impl::with(self)._array_filters; +} + +} // namespace v1 +} // namespace mongocxx diff --git a/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.hh b/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.hh new file mode 100644 index 0000000000..02b03de6d2 --- /dev/null +++ b/src/mongocxx/lib/mongocxx/v1/find_one_and_update_options.hh @@ -0,0 +1,46 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include // IWYU pragma: export + +// + +#include +#include +#include + +#include +#include + +#include + +namespace mongocxx { +namespace v1 { + +class find_one_and_update_options::internal { + public: + static bsoncxx::v1::stdx::optional& collation(find_one_and_update_options& self); + static bsoncxx::v1::stdx::optional& hint(find_one_and_update_options& self); + static bsoncxx::v1::stdx::optional& let(find_one_and_update_options& self); + static bsoncxx::v1::stdx::optional& comment(find_one_and_update_options& self); + static bsoncxx::v1::stdx::optional& projection(find_one_and_update_options& self); + static bsoncxx::v1::stdx::optional& sort(find_one_and_update_options& self); + static bsoncxx::v1::stdx::optional& write_concern(find_one_and_update_options& self); + static bsoncxx::v1::stdx::optional& array_filters(find_one_and_update_options& self); +}; + +} // namespace v1 +} // namespace mongocxx diff --git a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/find_one_and_update.cpp b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/find_one_and_update.cpp index a1c4b9021e..85a0a97fb9 100644 --- a/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/find_one_and_update.cpp +++ b/src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/find_one_and_update.cpp @@ -12,125 +12,59 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include -namespace mongocxx { -namespace v_noabi { -namespace options { - -find_one_and_update& find_one_and_update::bypass_document_validation(bool bypass_document_validation) { - _bypass_document_validation = bypass_document_validation; - return *this; -} - -find_one_and_update& find_one_and_update::collation(bsoncxx::v_noabi::document::view_or_value collation) { - _collation = std::move(collation); - return *this; -} - -find_one_and_update& find_one_and_update::hint(mongocxx::v_noabi::hint index_hint) { - _hint = std::move(index_hint); - return *this; -} - -find_one_and_update& find_one_and_update::let(bsoncxx::v_noabi::document::view_or_value let) { - _let = let; - return *this; -} - -find_one_and_update& find_one_and_update::comment(bsoncxx::v_noabi::types::bson_value::view_or_value comment) { - _comment = std::move(comment); - return *this; -} - -find_one_and_update& find_one_and_update::max_time(std::chrono::milliseconds max_time) { - _max_time = std::move(max_time); - return *this; -} - -find_one_and_update& find_one_and_update::projection(bsoncxx::v_noabi::document::view_or_value projection) { - _projection = std::move(projection); - return *this; -} - -find_one_and_update& find_one_and_update::return_document(mongocxx::v_noabi::options::return_document return_document) { - _return_document = return_document; - return *this; -} - -find_one_and_update& find_one_and_update::sort(bsoncxx::v_noabi::document::view_or_value ordering) { - _ordering = std::move(ordering); - return *this; -} - -find_one_and_update& find_one_and_update::upsert(bool upsert) { - _upsert = upsert; - return *this; -} - -find_one_and_update& find_one_and_update::write_concern(mongocxx::v_noabi::write_concern write_concern) { - _write_concern = std::move(write_concern); - return *this; -} - -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::bypass_document_validation() const { - return _bypass_document_validation; -} - -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::collation() - const { - return _collation; -} - -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::hint() const { - return _hint; -} - -bsoncxx::v_noabi::stdx::optional const find_one_and_update::let() const { - return _let; -} - -bsoncxx::v_noabi::stdx::optional const -find_one_and_update::comment() const { - return _comment; -} - -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::max_time() const { - return _max_time; -} - -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::projection() - const { - return _projection; -} - -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::return_document() const { - return _return_document; -} +// -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::sort() const { - return _ordering; -} +#include -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::upsert() const { - return _upsert; -} +#include -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::write_concern() const { - return _write_concern; -} +#include +#include -find_one_and_update& find_one_and_update::array_filters(bsoncxx::v_noabi::array::view_or_value array_filters) { - _array_filters = std::move(array_filters); - return *this; -} +namespace mongocxx { +namespace v_noabi { +namespace options { -bsoncxx::v_noabi::stdx::optional const& find_one_and_update::array_filters() - const { - return _array_filters; -} +find_one_and_update::find_one_and_update(v1::find_one_and_update_options opts) + : _bypass_document_validation{opts.bypass_document_validation()}, + _collation{[&]() -> decltype(_collation) { + if (auto& opt = v1::find_one_and_update_options::internal::collation(opts)) { + return bsoncxx::v_noabi::from_v1(std::move(*opt)); + } + return {}; + }()}, + _hint{std::move(v1::find_one_and_update_options::internal::hint(opts))}, + _let{[&]() -> decltype(_let) { + if (auto& opt = v1::find_one_and_update_options::internal::let(opts)) { + return bsoncxx::v_noabi::from_v1(std::move(*opt)); + } + return {}; + }()}, + _comment{std::move(v1::find_one_and_update_options::internal::comment(opts))}, + _max_time{opts.max_time()}, + _projection{[&]() -> decltype(_projection) { + if (auto& opt = v1::find_one_and_update_options::internal::projection(opts)) { + return bsoncxx::v_noabi::from_v1(std::move(*opt)); + } + return {}; + }()}, + _return_document{opts.return_document()}, + _ordering{[&]() -> decltype(_ordering) { + if (auto& opt = v1::find_one_and_update_options::internal::sort(opts)) { + return bsoncxx::v_noabi::from_v1(std::move(*opt)); + } + return {}; + }()}, + _upsert{opts.upsert()}, + _write_concern{std::move(v1::find_one_and_update_options::internal::write_concern(opts))}, + _array_filters{[&]() -> decltype(_array_filters) { + if (auto& opt = v1::find_one_and_update_options::internal::array_filters(opts)) { + return bsoncxx::v_noabi::from_v1(std::move(*opt)); + } + return {}; + }()} {} } // namespace options } // namespace v_noabi diff --git a/src/mongocxx/test/CMakeLists.txt b/src/mongocxx/test/CMakeLists.txt index c87793f1d2..233cbea9a5 100644 --- a/src/mongocxx/test/CMakeLists.txt +++ b/src/mongocxx/test/CMakeLists.txt @@ -112,6 +112,7 @@ set(mongocxx_test_sources_v1 v1/events.cpp v1/exception.cpp v1/find_one_and_delete_options.cpp + v1/find_one_and_update_options.cpp v1/find_options.cpp v1/hint.cpp v1/insert_many_options.cpp diff --git a/src/mongocxx/test/v1/find_one_and_update_options.cpp b/src/mongocxx/test/v1/find_one_and_update_options.cpp new file mode 100644 index 0000000000..3f1893278b --- /dev/null +++ b/src/mongocxx/test/v1/find_one_and_update_options.cpp @@ -0,0 +1,211 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +// + +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include + +namespace mongocxx { +namespace v1 { + +TEST_CASE("ownership", "[mongocxx][v1][find_one_and_update_options]") { + find_one_and_update_options source; + find_one_and_update_options target; + + source.comment(bsoncxx::v1::types::value{"source"}); + target.comment(bsoncxx::v1::types::value{"target"}); + + REQUIRE(source.comment() == bsoncxx::v1::types::b_string{"source"}); + REQUIRE(target.comment() == bsoncxx::v1::types::b_string{"target"}); + + auto const source_value = source.comment(); + + SECTION("move") { + auto move = std::move(source); + + // source is in an assign-or-move-only state. + + CHECK(move.comment() == source_value); + + target = std::move(move); + + // source is in an assign-or-move-only state. + + CHECK(target.comment() == source_value); + } + + SECTION("copy") { + auto copy = source; + + CHECK(source.comment() == source_value); + CHECK(copy.comment() == source_value); + + target = copy; + + CHECK(copy.comment() == source_value); + CHECK(target.comment() == source_value); + } +} + +TEST_CASE("default", "[mongocxx][v1][find_one_and_update_options]") { + find_one_and_update_options const opts; + + CHECK_FALSE(opts.collation().has_value()); + CHECK_FALSE(opts.bypass_document_validation().has_value()); + CHECK_FALSE(opts.hint().has_value()); + CHECK_FALSE(opts.let().has_value()); + CHECK_FALSE(opts.comment().has_value()); + CHECK_FALSE(opts.max_time().has_value()); + CHECK_FALSE(opts.projection().has_value()); + CHECK_FALSE(opts.return_document().has_value()); + CHECK_FALSE(opts.sort().has_value()); + CHECK_FALSE(opts.upsert().has_value()); + CHECK_FALSE(opts.write_concern().has_value()); + CHECK_FALSE(opts.array_filters().has_value()); +} + +TEST_CASE("collation", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(values({ + scoped_bson{}, + scoped_bson{R"({"x": 1})"}, + })); + + CHECK(find_one_and_update_options{}.collation(v.value()).collation() == v.view()); +} + +TEST_CASE("bypass_document_validation", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(false, true); + + CHECK(find_one_and_update_options{}.bypass_document_validation(v).bypass_document_validation() == v); +} + +TEST_CASE("hint", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(values({ + v1::hint{"abc"}, + v1::hint{scoped_bson{R"({"x": 1})"}.value()}, + })); + + CHECK(find_one_and_update_options{}.hint(v).hint() == v); +} + +TEST_CASE("let", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(values({ + scoped_bson{}, + scoped_bson{R"({"x": 1})"}, + })); + + CHECK(find_one_and_update_options{}.let(v.value()).let() == v.view()); +} + +TEST_CASE("comment", "[mongocxx][v1][find_one_and_update_options]") { + using T = bsoncxx::v1::types::value; + + auto const v = GENERATE(values({ + T{}, + T{std::int32_t{123}}, + T{std::int64_t{456}}, + T{123.456}, + T{"abc"}, + })); + + CHECK(find_one_and_update_options{}.comment(v).comment() == v); +} + +TEST_CASE("max_time", "[mongocxx][v1][find_one_and_update_options]") { + using T = std::chrono::milliseconds; + + auto const v = GENERATE(values({ + T{T::min()}, + T{-1}, + T{0}, + T{1}, + T{T::max()}, + })); + + CHECK(find_one_and_update_options{}.max_time(v).max_time() == v); +} + +TEST_CASE("projection", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(values({ + scoped_bson{}, + scoped_bson{R"({"x": 1})"}, + })); + + CHECK(find_one_and_update_options{}.projection(v.value()).projection() == v.view()); +} + +TEST_CASE("return_document", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(v1::return_document::k_before, v1::return_document::k_after); + + CHECK(find_one_and_update_options{}.return_document(v).return_document() == v); +} + +TEST_CASE("sort", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(values({ + scoped_bson{}, + scoped_bson{R"({"x": 1})"}, + })); + + CHECK(find_one_and_update_options{}.sort(v.value()).sort() == v.view()); +} + +TEST_CASE("upsert", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(false, true); + + CHECK(find_one_and_update_options{}.upsert(v).upsert() == v); +} + +TEST_CASE("write_concern", "[mongocxx][v1][find_one_and_update_options]") { + using T = v1::write_concern; + + auto const v = GENERATE(values({ + T{}, + T{}.acknowledge_level(T::level::k_majority), + T{}.tag("abc"), + })); + + CHECK(find_one_and_update_options{}.write_concern(v).write_concern() == v); +} + +TEST_CASE("array_filters", "[mongocxx][v1][find_one_and_update_options]") { + auto const v = GENERATE(values({ + scoped_bson{}, + scoped_bson{R"([1, 2.0, "three"])"}, + })); + + CHECK( + find_one_and_update_options{}.array_filters(bsoncxx::v1::array::value{v.array_view()}).array_filters() == + v.array_view()); +} + +} // namespace v1 +} // namespace mongocxx