MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
write.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <mongocxx/model/write-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <mongocxx/v1/bulk_write.hpp> // IWYU pragma: export
22
23#include <cstdint> // IWYU pragma: keep: backward compatibility, to be removed.
24#include <utility>
25
26#include <bsoncxx/stdx/optional.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
27
35
37
38namespace mongocxx {
39namespace v_noabi {
40namespace model {
41
45class write {
46 public:
50 /* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() write(v1::bulk_write::single op);
51
60 explicit MONGOCXX_ABI_EXPORT_CDECL() operator v1::bulk_write::single() &&;
61
65 explicit MONGOCXX_ABI_EXPORT_CDECL() operator v1::bulk_write::single() const&;
66
70 write(insert_one value) : _type{v_noabi::write_type::k_insert_one}, _insert_one{std::move(value)} {}
71
75 write(update_one value) : _type{v_noabi::write_type::k_update_one}, _update_one{std::move(value)} {}
76
80 write(update_many value) : _type{v_noabi::write_type::k_update_many}, _update_many{std::move(value)} {}
81
85 write(delete_one value) : _type{v_noabi::write_type::k_delete_one}, _delete_one{std::move(value)} {}
86
90 write(delete_many value) : _type{v_noabi::write_type::k_delete_many}, _delete_many{std::move(value)} {}
91
95 write(replace_one value) : _type{v_noabi::write_type::k_replace_one}, _replace_one{std::move(value)} {}
96
100 write(write&& rhs) noexcept : _type{rhs._type} {
101 switch (rhs._type) {
103 new (&_insert_one) insert_one(std::move(rhs._insert_one));
104 break;
106 new (&_update_one) update_one(std::move(rhs._update_one));
107 break;
109 new (&_update_many) update_many(std::move(rhs._update_many));
110 break;
112 new (&_delete_one) delete_one(std::move(rhs._delete_one));
113 break;
115 new (&_delete_many) delete_many(std::move(rhs._delete_many));
116 break;
118 new (&_replace_one) replace_one(std::move(rhs._replace_one));
119 break;
120 }
121 }
122
126 write& operator=(write&& rhs) noexcept {
127 if (this != &rhs) {
128 this->destroy_member();
129
130 switch (rhs._type) {
132 new (&_insert_one) insert_one(std::move(rhs._insert_one));
133 break;
135 new (&_update_one) update_one(std::move(rhs._update_one));
136 break;
138 new (&_update_many) update_many(std::move(rhs._update_many));
139 break;
141 new (&_delete_one) delete_one(std::move(rhs._delete_one));
142 break;
144 new (&_delete_many) delete_many(std::move(rhs._delete_many));
145 break;
147 new (&_replace_one) replace_one(std::move(rhs._replace_one));
148 break;
149 }
150
151 _type = rhs._type;
152 }
153
154 return *this;
155 }
156
157 write(write const& rhs) = delete;
158 write& operator=(write const& rhs) = delete;
159
164 this->destroy_member();
165 }
166
170 write_type type() const {
171 return _type;
172 }
173
179 insert_one const& get_insert_one() const {
180 return _insert_one;
181 }
182
188 update_one const& get_update_one() const {
189 return _update_one;
190 }
191
198 return _update_many;
199 }
200
206 delete_one const& get_delete_one() const {
207 return _delete_one;
208 }
209
216 return _delete_many;
217 }
218
225 return _replace_one;
226 }
227
228 private:
229 void destroy_member() noexcept {
230 switch (_type) {
232 _insert_one.~insert_one();
233 break;
235 _update_one.~update_one();
236 break;
238 _update_many.~update_many();
239 break;
241 _delete_one.~delete_one();
242 break;
244 _delete_many.~delete_many();
245 break;
247 _replace_one.~replace_one();
248 break;
249 }
250 }
251
252 write_type _type;
253
254 union {
255 insert_one _insert_one;
256 update_one _update_one;
257 update_many _update_many;
258 delete_one _delete_one;
259 delete_many _delete_many;
260 replace_one _replace_one;
261 };
262};
263
264} // namespace model
265} // namespace v_noabi
266} // namespace mongocxx
267
268namespace mongocxx {
269namespace v_noabi {
270
275 return {std::move(v)};
276}
277
284
285} // namespace v_noabi
286} // namespace mongocxx
287
289
write(v1::bulk_write::single op)
Construct with the mongocxx::v1 equivalent.
A single write operation.
Definition bulk_write.hpp:762
A batch of write operations that can be sent to the server as a group.
Definition bulk_write.hpp:54
A MongoDB delete operation that removes multiple documents.
Definition delete_many.hpp:42
A MongoDB delete operation that removes a single document.
Definition delete_one.hpp:42
A MongoDB insert operation that creates a single document.
Definition insert_one.hpp:40
A MongoDB update operation that replaces a single document.
Definition replace_one.hpp:42
A MongoDB update operation that modifies multiple documents.
Definition update_many.hpp:47
A MongoDB update operation that modifies a single document.
Definition update_one.hpp:47
A single write operation for use with mongocxx::v_noabi::bulk_write.
Definition write.hpp:45
write(write &&rhs) noexcept
Move constructs a write.
Definition write.hpp:100
~write()
Destroys a write.
Definition write.hpp:163
write(replace_one value)
Constructs a write from a model::replace_one.
Definition write.hpp:95
write(update_many value)
Constructs a write from a model::update_many.
Definition write.hpp:80
update_one const & get_update_one() const
Accesses the write as an model::update_one. It is illegal to call this method if the return of type()...
Definition write.hpp:188
delete_one const & get_delete_one() const
Accesses the write as a model::delete_one. It is illegal to call this method if the return of type() ...
Definition write.hpp:206
replace_one const & get_replace_one() const
Accesses the write as a model::replace_one. It is illegal to call this method if the return of type()...
Definition write.hpp:224
write(delete_many value)
Constructs a write from a model::delete_many.
Definition write.hpp:90
delete_many const & get_delete_many() const
Accesses the write as a model::delete_many. It is illegal to call this method if the return of type()...
Definition write.hpp:215
write(delete_one value)
Constructs a write from a model::delete_one.
Definition write.hpp:85
operator v1::bulk_write::single() &&
Convert to the mongocxx::v1 equivalent.
write(v1::bulk_write::single op)
Construct with the mongocxx::v1 equivalent.
write & operator=(write &&rhs) noexcept
Move assigns a write.
Definition write.hpp:126
update_many const & get_update_many() const
Accesses the write as an model::update_many. It is illegal to call this method if the return of type(...
Definition write.hpp:197
write_type type() const
Returns the current type of this write.
Definition write.hpp:170
insert_one const & get_insert_one() const
Accesses the write as a model::insert_one. It is illegal to call this method if the return of type() ...
Definition write.hpp:179
write(update_one value)
Constructs a write from a model::update_one.
Definition write.hpp:75
Provides mongocxx::v_noabi::model::delete_many.
Provides mongocxx::v_noabi::model::delete_one.
Provides mongocxx::v_noabi::model::insert_one.
Provides mongocxx::v_noabi::model::replace_one.
#define MONGOCXX_ABI_EXPORT_CDECL(...)
Equivalent to MONGOCXX_ABI_EXPORT with MONGOCXX_ABI_CDECL.
Definition export.hpp:52
The mongocxx v_noabi macro guard postlude header.
The mongocxx v_noabi macro guard prelude header.
Declares entities whose ABI stability is guaranteed for documented symbols.
Declares entities representing bulk write operations.
Declares entities whose ABI stability is NOT guaranteed.
write_type
Used by mongocxx::v_noabi::model::write.
Definition write_type.hpp:31
@ k_delete_one
Deleting a single document from a collection.
Definition write_type.hpp:36
@ k_insert_one
Inserting a single document into a collection.
Definition write_type.hpp:33
@ k_update_many
Update one or more documents in a collection.
Definition write_type.hpp:45
@ k_update_one
Update a single document in a collection.
Definition write_type.hpp:42
@ k_delete_many
Delete one or more documents from a collection.
Definition write_type.hpp:39
@ k_replace_one
Replace a single document in a collection with a new one.
Definition write_type.hpp:48
v1::bulk_write to_v1(v_noabi::bulk_write v)
Convert to the mongocxx::v1 equivalent of v.
Definition bulk_write.hpp:162
v_noabi::bulk_write from_v1(v1::bulk_write v)
Convert to the mongocxx::v_noabi equivalent of v.
Definition bulk_write.hpp:155
The top-level namespace within which all mongocxx library entities are declared.
Provides mongocxx::v_noabi::model::update_many.
Provides mongocxx::v_noabi::model::update_one.
Provides entities related to write operations.
Provides std::optional-related polyfills for library API usage.
Declares mongocxx::v_noabi::model::write.
Provides mongocxx::v_noabi::write_type.