Skip to content

Commit 2bfd2a9

Browse files
authored
CXX-3163 Remove workarounds for core::optional<T> (#1269)
1 parent 30cf5e5 commit 2bfd2a9

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ Changes prior to 3.9.0 are documented as [release notes on GitHub](https://githu
99

1010
## 4.0.0 [Unreleased]
1111

12+
## Added
13+
14+
- Getter for the `start_at_operation_time` option in `mongocxx::v_noabi::options::change_stream`.
15+
1216
## Changed
1317

1418
- CMake option `ENABLE_TESTS` is now `OFF` by default.
1519
- Set `ENABLE_TEST=ON` to re-enable building test targets.
1620
- Set `BUILD_TESTING=ON` to include test targets in the "all" target when `ENABLE_TESTS=ON` (since 3.9.0, `OFF` by default).
21+
- Layout of `mongocxx::v_noabi::options::change_stream` to support the new optional `start_at_operation_time` accessor.
1722

1823
## Removed
1924

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ class change_stream {
275275
MONGOCXX_ABI_EXPORT_CDECL(change_stream&)
276276
start_at_operation_time(bsoncxx::v_noabi::types::b_timestamp timestamp);
277277

278+
///
279+
/// The current start_at_operation_time setting.
280+
///
281+
/// @return
282+
/// The current startAtOperationTime option.
283+
///
284+
MONGOCXX_ABI_EXPORT_CDECL(const bsoncxx::stdx::optional<bsoncxx::v_noabi::types::b_timestamp>&)
285+
start_at_operation_time() const;
286+
278287
private:
279288
friend ::mongocxx::v_noabi::client;
280289
friend ::mongocxx::v_noabi::collection;
@@ -291,11 +300,7 @@ class change_stream {
291300
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view_or_value> _resume_after;
292301
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view_or_value> _start_after;
293302
bsoncxx::v_noabi::stdx::optional<std::chrono::milliseconds> _max_await_time;
294-
// _start_at_operation_time is not wrapped in a bsoncxx::v_noabi::stdx::optional because of a
295-
// longstanding bug in the MNMLSTC polyfill that has been fixed on master, but not in the latest
296-
// release: https://github.com/mnmlstc/core/pull/23
297-
bsoncxx::v_noabi::types::b_timestamp _start_at_operation_time = {};
298-
bool _start_at_operation_time_set = false;
303+
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::types::b_timestamp> _start_at_operation_time;
299304
};
300305

301306
} // namespace options

src/mongocxx/lib/mongocxx/v_noabi/mongocxx/options/change_stream.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ const bsoncxx::v_noabi::stdx::optional<std::chrono::milliseconds>& change_stream
112112

113113
change_stream& change_stream::start_at_operation_time(
114114
bsoncxx::v_noabi::types::b_timestamp timestamp) {
115-
_start_at_operation_time = timestamp;
116-
_start_at_operation_time_set = true;
115+
_start_at_operation_time = std::move(timestamp);
117116
return *this;
118117
}
119118

119+
const bsoncxx::stdx::optional<bsoncxx::v_noabi::types::b_timestamp>&
120+
change_stream::start_at_operation_time() const {
121+
return _start_at_operation_time;
122+
}
123+
120124
namespace {
121125
template <typename T>
122126
inline void append_if(bsoncxx::v_noabi::builder::basic::document& doc,
@@ -139,10 +143,7 @@ bsoncxx::v_noabi::document::value change_stream::as_bson() const {
139143
append_if(out, "batchSize", batch_size());
140144
append_if(out, "collation", collation());
141145
append_if(out, "comment", comment());
142-
if (_start_at_operation_time_set) {
143-
out.append(bsoncxx::v_noabi::builder::basic::kvp("startAtOperationTime",
144-
_start_at_operation_time));
145-
}
146+
append_if(out, "startAtOperationTime", start_at_operation_time());
146147

147148
if (max_await_time()) {
148149
auto count = max_await_time().value().count();

src/mongocxx/test/spec/gridfs.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,10 @@
4646
#include <mongocxx/test/client_helpers.hh>
4747
#include <mongocxx/test/spec/util.hh>
4848

49-
namespace {
5049
using namespace bsoncxx;
5150
using namespace mongocxx;
5251

53-
// This function is a workaround for clang 3.8 and mnmlstc/core, where `return
54-
// optional<item_t>{an_item_t};` fails to compile.
55-
bsoncxx::stdx::optional<test_util::item_t> make_optional(test_util::item_t item) {
56-
bsoncxx::stdx::optional<test_util::item_t> option{};
57-
option = item;
58-
59-
return option;
60-
}
52+
namespace {
6153

6254
// Query the GridFS files collection and fetch the length of the file.
6355
//
@@ -80,20 +72,20 @@ std::int64_t get_length_of_gridfs_file(gridfs::bucket bucket, types::bson_value:
8072
bsoncxx::stdx::optional<test_util::item_t> transform_hex(test_util::item_t pair,
8173
builder::basic::array* context) {
8274
if (!pair.first) {
83-
return make_optional(pair);
75+
return {pair};
8476
}
8577

8678
auto key = *(pair.first);
8779
auto value = pair.second;
8880

8981
if (bsoncxx::string::to_string(key) != "data" || value.type() != type::k_document) {
90-
return make_optional(pair);
82+
return {pair};
9183
}
9284

9385
auto data = value.get_document().value;
9486

9587
if (!data["$hex"] || data["$hex"].type() != type::k_string) {
96-
return make_optional(pair);
88+
return {pair};
9789
}
9890

9991
std::basic_string<std::uint8_t> bytes =
@@ -106,8 +98,8 @@ bsoncxx::stdx::optional<test_util::item_t> transform_hex(test_util::item_t pair,
10698
auto view = context->view();
10799
auto length = std::distance(view.cbegin(), view.cend());
108100

109-
return make_optional(std::make_pair(bsoncxx::stdx::optional<bsoncxx::stdx::string_view>("data"),
110-
view[static_cast<std::uint32_t>(length - 1)].get_value()));
101+
return {std::make_pair(bsoncxx::stdx::optional<bsoncxx::stdx::string_view>("data"),
102+
view[static_cast<std::uint32_t>(length - 1)].get_value())};
111103
}
112104

113105
// The GridFS spec specifies the expected binary data in the form of { $hex: "<hexadecimal string>"
@@ -120,21 +112,20 @@ document::value convert_hex_data_to_binary(document::view document) {
120112
bsoncxx::stdx::optional<test_util::item_t> convert_length_to_int64(test_util::item_t pair,
121113
builder::basic::array*) {
122114
if (!pair.first) {
123-
return make_optional(pair);
115+
return {pair};
124116
}
125117

126118
auto key = *(pair.first);
127119
auto value = pair.second;
128120

129121
if (bsoncxx::string::to_string(key) != "length" || value.type() != type::k_int32) {
130-
return make_optional(pair);
122+
return {pair};
131123
}
132124

133125
types::b_int64 length = {value.get_int32()};
134126

135-
return make_optional(
136-
std::make_pair(bsoncxx::stdx::optional<bsoncxx::stdx::string_view>("length"),
137-
types::bson_value::view{length}));
127+
return {std::make_pair(bsoncxx::stdx::optional<bsoncxx::stdx::string_view>("length"),
128+
types::bson_value::view{length})};
138129
}
139130

140131
void compare_collections(database db) {
@@ -306,7 +297,7 @@ void test_upload(database db,
306297
[id](test_util::item_t pair,
307298
builder::basic::array* context) -> bsoncxx::stdx::optional<test_util::item_t> {
308299
if (!pair.first) {
309-
return make_optional(pair);
300+
return {pair};
310301
}
311302

312303
auto key = *(pair.first);
@@ -336,10 +327,10 @@ void test_upload(database db,
336327
}
337328

338329
if (id_str != "*result") {
339-
return make_optional(pair);
330+
return {pair};
340331
}
341332

342-
return make_optional(std::make_pair(pair.first, types::bson_value::view{id}));
333+
return {std::make_pair(pair.first, types::bson_value::view{id})};
343334
});
344335

345336
db.run_command(transformed_data.view());

0 commit comments

Comments
 (0)