Skip to content

Commit 208d4f2

Browse files
authored
v1: insert_many_options and insert_one_options (CXX-3237, CXX-3238) (#1529)
1 parent 4e8f2ca commit 208d4f2

File tree

12 files changed

+703
-51
lines changed

12 files changed

+703
-51
lines changed

src/mongocxx/include/mongocxx/v1/insert_many_options.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ class insert_many_options {
132132
/// Return the current "comment" field.
133133
///
134134
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v1::stdx::optional<bsoncxx::v1::types::view>) comment() const;
135+
136+
class internal;
135137
};
136138

137139
} // namespace v1

src/mongocxx/include/mongocxx/v1/insert_one_options.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class insert_one_options {
121121
/// Return the current "comment" field.
122122
///
123123
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v1::stdx::optional<bsoncxx::v1::types::view>) comment() const;
124+
125+
class internal;
124126
};
125127

126128
} // namespace v1

src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/insert-fwd.hpp

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

1515
#pragma once
1616

17+
#include <mongocxx/v1/insert_many_options-fwd.hpp> // IWYU pragma: export
18+
#include <mongocxx/v1/insert_one_options-fwd.hpp> // IWYU pragma: export
19+
1720
#include <mongocxx/config/prelude.hpp>
1821

1922
namespace mongocxx {
@@ -29,7 +32,7 @@ class insert;
2932
namespace mongocxx {
3033
namespace options {
3134

32-
using ::mongocxx::v_noabi::options::insert;
35+
using v_noabi::options::insert;
3336

3437
} // namespace options
3538
} // namespace mongocxx
@@ -40,3 +43,7 @@ using ::mongocxx::v_noabi::options::insert;
4043
/// @file
4144
/// Declares @ref mongocxx::v_noabi::options::insert.
4245
///
46+
/// @par Includes
47+
/// - @ref mongocxx/v1/insert_many_options-fwd.hpp
48+
/// - @ref mongocxx/v1/insert_one_options-fwd.hpp
49+
///

src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/insert.hpp

Lines changed: 140 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@
1616

1717
#include <mongocxx/options/insert-fwd.hpp> // IWYU pragma: export
1818

19+
//
20+
21+
#include <bsoncxx/v1/types/value.hpp>
22+
23+
#include <mongocxx/v1/insert_many_options.hpp> // IWYU pragma: export
24+
#include <mongocxx/v1/insert_one_options.hpp> // IWYU pragma: export
25+
26+
#include <utility>
27+
1928
#include <bsoncxx/document/view.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
2029
#include <bsoncxx/stdx/optional.hpp>
30+
#include <bsoncxx/types/bson_value/view.hpp>
2131
#include <bsoncxx/types/bson_value/view_or_value.hpp>
2232

2333
#include <mongocxx/write_concern.hpp>
@@ -33,6 +43,75 @@ namespace options {
3343
///
3444
class insert {
3545
public:
46+
///
47+
/// Default initialization.
48+
///
49+
insert() = default;
50+
51+
///
52+
/// Construct with the @ref mongocxx::v1 equivalent.
53+
///
54+
/* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() insert(v1::insert_many_options opts);
55+
56+
///
57+
/// Construct with the @ref mongocxx::v1 equivalent.
58+
///
59+
/* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() insert(v1::insert_one_options opts);
60+
61+
///
62+
/// Convert to the @ref mongocxx::v1 equivalent.
63+
///
64+
explicit operator v1::insert_many_options() const {
65+
using bsoncxx::v_noabi::to_v1;
66+
using mongocxx::v_noabi::to_v1;
67+
68+
v1::insert_many_options ret;
69+
70+
if (_write_concern) {
71+
ret.write_concern(to_v1(*_write_concern));
72+
}
73+
74+
if (_bypass_document_validation) {
75+
ret.bypass_document_validation(*_bypass_document_validation);
76+
}
77+
78+
if (_ordered) {
79+
ret.ordered(*_ordered);
80+
}
81+
82+
if (_comment) {
83+
ret.comment(bsoncxx::v1::types::value{to_v1(*_comment)});
84+
}
85+
86+
return ret;
87+
}
88+
89+
///
90+
/// Convert to the @ref mongocxx::v1 equivalent.
91+
///
92+
/// @note The `ordered` field is ignored.
93+
///
94+
explicit operator v1::insert_one_options() const {
95+
using bsoncxx::v_noabi::to_v1;
96+
using mongocxx::v_noabi::to_v1;
97+
98+
v1::insert_one_options ret;
99+
100+
if (_write_concern) {
101+
ret.write_concern(to_v1(*_write_concern));
102+
}
103+
104+
if (_bypass_document_validation) {
105+
ret.bypass_document_validation(*_bypass_document_validation);
106+
}
107+
108+
if (_comment) {
109+
ret.comment(bsoncxx::v1::types::value{to_v1(*_comment)});
110+
}
111+
112+
return ret;
113+
}
114+
36115
///
37116
/// Sets the bypass_document_validation option.
38117
/// If true, allows the write to opt-out of document level validation.
@@ -48,15 +127,19 @@ class insert {
48127
/// A reference to the object on which this member function is being called. This facilitates
49128
/// method chaining.
50129
///
51-
MONGOCXX_ABI_EXPORT_CDECL(insert&) bypass_document_validation(bool bypass_document_validation);
130+
insert& bypass_document_validation(bool bypass_document_validation) {
131+
_bypass_document_validation = bypass_document_validation;
132+
return *this;
133+
}
52134

53135
///
54136
/// Gets the current value of the bypass_document_validation option.
55137
///
56138
/// @return The optional value of the bypass_document_validation option.
57139
///
58-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<bool> const&)
59-
bypass_document_validation() const;
140+
bsoncxx::v_noabi::stdx::optional<bool> const& bypass_document_validation() const {
141+
return _bypass_document_validation;
142+
}
60143

61144
///
62145
/// Sets the write_concern for this operation.
@@ -71,7 +154,10 @@ class insert {
71154
/// A reference to the object on which this member function is being called. This facilitates
72155
/// method chaining.
73156
///
74-
MONGOCXX_ABI_EXPORT_CDECL(insert&) write_concern(mongocxx::v_noabi::write_concern wc);
157+
insert& write_concern(v_noabi::write_concern wc) {
158+
_write_concern = std::move(wc);
159+
return *this;
160+
}
75161

76162
///
77163
/// The current write_concern for this operation.
@@ -81,8 +167,9 @@ class insert {
81167
/// @see
82168
/// - https://www.mongodb.com/docs/manual/core/write-concern/
83169
///
84-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<mongocxx::v_noabi::write_concern> const&)
85-
write_concern() const;
170+
bsoncxx::v_noabi::stdx::optional<v_noabi::write_concern> const& write_concern() const {
171+
return _write_concern;
172+
}
86173

87174
///
88175
/// @note: This applies only to insert_many and is ignored for insert_one.
@@ -102,7 +189,10 @@ class insert {
102189
/// @see
103190
/// - https://www.mongodb.com/docs/manual/reference/command/insert/
104191
///
105-
MONGOCXX_ABI_EXPORT_CDECL(insert&) ordered(bool ordered);
192+
insert& ordered(bool ordered) {
193+
_ordered = ordered;
194+
return *this;
195+
}
106196

107197
///
108198
/// The current ordered value for this operation.
@@ -112,7 +202,9 @@ class insert {
112202
/// @see
113203
/// - https://www.mongodb.com/docs/manual/reference/command/insert/
114204
///
115-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<bool> const&) ordered() const;
205+
bsoncxx::v_noabi::stdx::optional<bool> const& ordered() const {
206+
return _ordered;
207+
}
116208

117209
///
118210
/// Sets the comment for this operation.
@@ -127,8 +219,10 @@ class insert {
127219
/// A reference to the object on which this member function is being called. This facilitates
128220
/// method chaining.
129221
///
130-
MONGOCXX_ABI_EXPORT_CDECL(insert&)
131-
comment(bsoncxx::v_noabi::types::bson_value::view_or_value comment);
222+
insert& comment(bsoncxx::v_noabi::types::bson_value::view_or_value comment) {
223+
_comment = std::move(comment);
224+
return *this;
225+
}
132226

133227
///
134228
/// The current comment for this operation.
@@ -138,11 +232,12 @@ class insert {
138232
/// @see
139233
/// - https://www.mongodb.com/docs/manual/reference/command/insert/
140234
///
141-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::types::bson_value::view_or_value> const&)
142-
comment() const;
235+
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::types::bson_value::view_or_value> const& comment() const {
236+
return _comment;
237+
}
143238

144239
private:
145-
bsoncxx::v_noabi::stdx::optional<mongocxx::v_noabi::write_concern> _write_concern;
240+
bsoncxx::v_noabi::stdx::optional<v_noabi::write_concern> _write_concern;
146241
bsoncxx::v_noabi::stdx::optional<bool> _ordered;
147242
bsoncxx::v_noabi::stdx::optional<bool> _bypass_document_validation;
148243
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::types::bson_value::view_or_value> _comment;
@@ -152,9 +247,41 @@ class insert {
152247
} // namespace v_noabi
153248
} // namespace mongocxx
154249

250+
namespace mongocxx {
251+
namespace v_noabi {
252+
253+
///
254+
/// Convert to the @ref mongocxx::v_noabi equivalent of `v`.
255+
///
256+
inline v_noabi::options::insert from_v1(v1::insert_many_options v) {
257+
return {std::move(v)};
258+
}
259+
260+
///
261+
/// Convert to the @ref mongocxx::v_noabi equivalent of `v`.
262+
///
263+
/// @note The `ordered` field is initialized as unset.
264+
///
265+
inline v_noabi::options::insert from_v1(v1::insert_one_options v) {
266+
return {std::move(v)};
267+
}
268+
269+
// Ambiguous whether `v_noabi::options::insert` should be converted to `v1::insert_many_options` or
270+
// `v1::insert_one_options`. Require users to explicitly cast to the expected type instead.
271+
//
272+
// v1::insert_many_options to_v1(v_noabi::options::insert const& v);
273+
// v1::insert_one_options to_v1(v_noabi::options::insert const& v);
274+
275+
} // namespace v_noabi
276+
} // namespace mongocxx
277+
155278
#include <mongocxx/config/postlude.hpp>
156279

157280
///
158281
/// @file
159282
/// Provides @ref mongocxx::v_noabi::options::insert.
160283
///
284+
/// @par Includes
285+
/// - @ref mongocxx/v1/insert_many_options.hpp
286+
/// - @ref mongocxx/v1/insert_one_options.hpp
287+
///

0 commit comments

Comments
 (0)