Skip to content

Commit 5ae8dc8

Browse files
authored
v1::write_concern (CXX-3237, CXX-3238) (#1504)
1 parent 56a7f0e commit 5ae8dc8

File tree

17 files changed

+924
-395
lines changed

17 files changed

+924
-395
lines changed

src/mongocxx/include/mongocxx/v1/write_concern.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ class write_concern {
248248
}
249249
/// @}
250250
///
251+
252+
class internal;
253+
254+
private:
255+
/* explicit(false) */ write_concern(void* impl);
251256
};
252257

253258
} // namespace v1

src/mongocxx/include/mongocxx/v_noabi/mongocxx/write_concern-fwd.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#pragma once
1616

17+
#include <mongocxx/v1/write_concern-fwd.hpp>
18+
1719
#include <mongocxx/config/prelude.hpp>
1820

1921
namespace mongocxx {
@@ -26,7 +28,7 @@ class write_concern;
2628

2729
namespace mongocxx {
2830

29-
using ::mongocxx::v_noabi::write_concern;
31+
using v_noabi::write_concern;
3032

3133
} // namespace mongocxx
3234

@@ -36,3 +38,6 @@ using ::mongocxx::v_noabi::write_concern;
3638
/// @file
3739
/// Declares @ref mongocxx::v_noabi::write_concern.
3840
///
41+
/// @par Includes
42+
/// - @ref mongocxx/v1/write_concern-fwd.hpp
43+
///

src/mongocxx/include/mongocxx/v_noabi/mongocxx/write_concern.hpp

Lines changed: 92 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,31 @@
1414

1515
#pragma once
1616

17+
#include <mongocxx/write_concern-fwd.hpp> // IWYU pragma: export
18+
19+
//
20+
21+
#include <mongocxx/v1/write_concern.hpp>
22+
1723
#include <chrono>
1824
#include <cstdint>
19-
#include <memory>
25+
#include <memory> // IWYU pragma: keep: backward compatibility, to be removed.
2026
#include <stdexcept> // IWYU pragma: keep: backward compatibility, to be removed.
2127
#include <string>
28+
#include <utility>
2229

23-
#include <mongocxx/bulk_write-fwd.hpp>
24-
#include <mongocxx/client-fwd.hpp>
25-
#include <mongocxx/collection-fwd.hpp>
26-
#include <mongocxx/database-fwd.hpp>
27-
#include <mongocxx/options/transaction-fwd.hpp>
28-
#include <mongocxx/uri-fwd.hpp>
29-
#include <mongocxx/write_concern-fwd.hpp> // IWYU pragma: export
30+
#include <mongocxx/bulk_write-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
31+
#include <mongocxx/client-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
32+
#include <mongocxx/collection-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
33+
#include <mongocxx/database-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
34+
#include <mongocxx/options/transaction-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
35+
#include <mongocxx/uri-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
3036

3137
#include <bsoncxx/document/value.hpp>
3238
#include <bsoncxx/stdx/optional.hpp>
3339
#include <bsoncxx/stdx/string_view.hpp>
3440

35-
#include <mongocxx/options/transaction.hpp>
41+
#include <mongocxx/options/transaction.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
3642

3743
#include <mongocxx/config/prelude.hpp>
3844

@@ -46,6 +52,9 @@ namespace v_noabi {
4652
/// - [Write Concern (MongoDB Manual)](https://www.mongodb.com/docs/manual/core/write-concern/)
4753
///
4854
class write_concern {
55+
private:
56+
v1::write_concern _wc;
57+
4958
public:
5059
///
5160
/// A class to represent the write concern level for write operations.
@@ -65,32 +74,31 @@ class write_concern {
6574
///
6675
/// Constructs a new write_concern.
6776
///
68-
MONGOCXX_ABI_EXPORT_CDECL() write_concern();
69-
70-
///
71-
/// Copy constructs a write_concern.
72-
///
73-
MONGOCXX_ABI_EXPORT_CDECL() write_concern(write_concern const&);
77+
write_concern() = default;
7478

7579
///
76-
/// Copy assigns a write_concern.
80+
/// Construct with the @ref mongocxx::v1 equivalent.
7781
///
78-
MONGOCXX_ABI_EXPORT_CDECL(write_concern&) operator=(write_concern const&);
82+
/* explicit(false) */ write_concern(v1::write_concern rc) : _wc{std::move(rc)} {}
7983

8084
///
81-
/// Move constructs a write_concern.
85+
/// Convert to the @ref mongocxx::v1 equivalent.
8286
///
83-
MONGOCXX_ABI_EXPORT_CDECL() write_concern(write_concern&&) noexcept;
84-
87+
/// @par Postconditions:
88+
/// - `*this` is in an assign-or-destroy-only state.
8589
///
86-
/// Move assigns a write_concern.
90+
/// @warning Invalidates all associated views.
8791
///
88-
MONGOCXX_ABI_EXPORT_CDECL(write_concern&) operator=(write_concern&&) noexcept;
92+
explicit operator v1::write_concern() && {
93+
return std::move(_wc);
94+
}
8995

9096
///
91-
/// Destroys a write_concern.
97+
/// Convert to the @ref mongocxx::v1 equivalent.
9298
///
93-
MONGOCXX_ABI_EXPORT_CDECL() ~write_concern();
99+
explicit operator v1::write_concern() const& {
100+
return _wc;
101+
}
94102

95103
///
96104
/// Sets the journal parameter for this write concern.
@@ -100,7 +108,9 @@ class write_concern {
100108
/// before reporting a write operations was successful. This ensures that data is not lost if
101109
/// the mongod instance shuts down unexpectedly.
102110
///
103-
MONGOCXX_ABI_EXPORT_CDECL(void) journal(bool journal);
111+
void journal(bool journal) {
112+
_wc.journal(journal);
113+
}
104114

105115
///
106116
/// Sets the number of nodes that are required to acknowledge the write before the operation is
@@ -155,7 +165,10 @@ class write_concern {
155165
///
156166
/// @throws mongocxx::v_noabi::logic_error for an invalid timeout value.
157167
///
158-
MONGOCXX_ABI_EXPORT_CDECL(void) majority(std::chrono::milliseconds timeout);
168+
void majority(std::chrono::milliseconds timeout) {
169+
this->timeout(timeout);
170+
this->acknowledge_level(level::k_majority);
171+
}
159172

160173
///
161174
/// Sets the name representing the server-side getLastErrorMode entry containing the list of
@@ -166,7 +179,9 @@ class write_concern {
166179
/// @param tag
167180
/// The string representing on of the "getLastErrorModes" in the replica set configuration.
168181
///
169-
MONGOCXX_ABI_EXPORT_CDECL(void) tag(bsoncxx::v_noabi::stdx::string_view tag);
182+
void tag(bsoncxx::v_noabi::stdx::string_view tag) {
183+
_wc.tag(tag);
184+
}
170185

171186
///
172187
/// Sets an upper bound on the time a write concern can take to be satisfied. If the write
@@ -185,7 +200,9 @@ class write_concern {
185200
///
186201
/// @return @c true if journal is required, @c false if not.
187202
///
188-
MONGOCXX_ABI_EXPORT_CDECL(bool) journal() const;
203+
bool journal() const {
204+
return _wc.journal().value_or(false);
205+
}
189206

190207
///
191208
/// Gets the current number of nodes that this write_concern requires operations to reach.
@@ -198,7 +215,9 @@ class write_concern {
198215
///
199216
/// @return The number of required nodes.
200217
///
201-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<std::int32_t>) nodes() const;
218+
bsoncxx::v_noabi::stdx::optional<std::int32_t> nodes() const {
219+
return _wc.nodes();
220+
}
202221

203222
///
204223
/// Gets the current acknowledgment level.
@@ -208,69 +227,90 @@ class write_concern {
208227
///
209228
/// @return The acknowledgment level.
210229
///
211-
MONGOCXX_ABI_EXPORT_CDECL(level) acknowledge_level() const;
230+
level acknowledge_level() const {
231+
return static_cast<level>(_wc.acknowledge_level());
232+
}
212233

213234
///
214235
/// Gets the current getLastErrorMode that is required by this write_concern.
215236
///
216237
/// @return The current getLastErrorMode.
217238
///
218-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<std::string>) tag() const;
239+
bsoncxx::v_noabi::stdx::optional<std::string> tag() const {
240+
bsoncxx::v_noabi::stdx::optional<std::string> ret;
241+
if (auto const opt = _wc.tag()) {
242+
ret.emplace(*opt);
243+
}
244+
return ret;
245+
}
219246

220247
///
221248
/// Gets whether the majority of nodes is currently required by this write_concern.
222249
///
223250
/// @return The current majority setting.
224251
///
225-
MONGOCXX_ABI_EXPORT_CDECL(bool) majority() const;
252+
bool majority() const {
253+
return _wc.acknowledge_level() == v1::write_concern::level::k_majority;
254+
}
226255

227256
///
228257
/// Gets the current timeout for this write_concern.
229258
///
230259
/// @return Current timeout in milliseconds.
231260
///
232-
MONGOCXX_ABI_EXPORT_CDECL(std::chrono::milliseconds) timeout() const;
261+
std::chrono::milliseconds timeout() const {
262+
return _wc.timeout();
263+
}
233264

234265
///
235266
/// Gets whether this write_concern requires an acknowledged write.
236267
///
237268
/// @return Whether this write concern requires an acknowledged write.
238269
///
239-
MONGOCXX_ABI_EXPORT_CDECL(bool) is_acknowledged() const;
270+
bool is_acknowledged() const {
271+
return _wc.is_acknowledged();
272+
}
240273

241274
///
242275
/// Gets the document form of this write_concern.
243276
///
244277
/// @return
245278
/// Document representation of this write_concern.
246279
///
247-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::document::value) to_document() const;
280+
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::document::value) to_document() const {
281+
return bsoncxx::v_noabi::from_v1(_wc.to_document());
282+
}
248283

249284
///
250285
/// @relates mongocxx::v_noabi::write_concern
251286
///
252287
/// Compares two write_concern objects for (in)-equality.
253288
///
254289
/// @{
255-
friend MONGOCXX_ABI_EXPORT_CDECL(bool) operator==(write_concern const&, write_concern const&);
256-
friend MONGOCXX_ABI_EXPORT_CDECL(bool) operator!=(write_concern const&, write_concern const&);
290+
friend MONGOCXX_ABI_EXPORT_CDECL(bool) operator==(write_concern const& lhs, write_concern const& rhs);
291+
292+
friend bool operator!=(write_concern const& lhs, write_concern const& rhs) {
293+
return !(lhs == rhs);
294+
}
257295
/// @}
258296
///
259297

260-
private:
261-
friend ::mongocxx::v_noabi::bulk_write;
262-
friend ::mongocxx::v_noabi::client;
263-
friend ::mongocxx::v_noabi::collection;
264-
friend ::mongocxx::v_noabi::database;
265-
friend ::mongocxx::v_noabi::options::transaction;
266-
friend ::mongocxx::v_noabi::uri;
267-
268-
class impl;
298+
class internal;
299+
};
269300

270-
write_concern(std::unique_ptr<impl>&& implementation);
301+
///
302+
/// Convert to the @ref mongocxx::v_noabi equivalent of `v`.
303+
///
304+
inline v_noabi::write_concern from_v1(v1::write_concern v) {
305+
return {std::move(v)};
306+
}
271307

272-
std::unique_ptr<impl> _impl;
273-
};
308+
///
309+
/// Convert to the @ref mongocxx::v1 equivalent of `v`.
310+
///
311+
inline v1::write_concern to_v1(v_noabi::write_concern v) {
312+
return v1::write_concern{std::move(v)};
313+
}
274314

275315
} // namespace v_noabi
276316
} // namespace mongocxx
@@ -281,3 +321,6 @@ class write_concern {
281321
/// @file
282322
/// Provides @ref mongocxx::v_noabi::write_concern.
283323
///
324+
/// @par Includes
325+
/// - @ref mongocxx/v1/write_concern-fwd.hpp
326+
///

src/mongocxx/lib/mongocxx/private/mongoc.hh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,16 @@ BSONCXX_PRIVATE_WARNINGS_POP();
396396
X(write_concern_get_wmajority) \
397397
X(write_concern_get_wtag) \
398398
X(write_concern_get_wtimeout) \
399+
X(write_concern_get_wtimeout_int64) \
399400
X(write_concern_is_acknowledged) \
400401
X(write_concern_journal_is_set) \
401402
X(write_concern_new) \
402403
X(write_concern_set_journal) \
403404
X(write_concern_set_w) \
404405
X(write_concern_set_wmajority) \
405406
X(write_concern_set_wtag) \
406-
X(write_concern_set_wtimeout)
407+
X(write_concern_set_wtimeout) \
408+
X(write_concern_set_wtimeout_int64)
407409

408410
namespace mongocxx {
409411
namespace libmongoc {

0 commit comments

Comments
 (0)