Skip to content

Commit 6bc1103

Browse files
committed
CXX-792 Support boost for C++11 driver polyfills
1 parent a63863b commit 6bc1103

File tree

14 files changed

+84
-15
lines changed

14 files changed

+84
-15
lines changed

src/bsoncxx/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(BSONCXX_HEADER_INSTALL_DIR "include/bsoncxx/${BSONCXX_DIRECTORY_PREFIX}" CAC
2020
option(BSONCXX_POLY_USE_STD_EXPERIMENTAL "Use <experimental/ for stdx polyfills" OFF)
2121
option(BSONCXX_POLY_USE_MNMLSTC "Use mnmlstc/core for stdx polyfills" ON)
2222
option(BSONCXX_POLY_USE_SYSTEM_MNMLSTC "Obtain mnmlstc/core from system" OFF)
23+
option(BSONCXX_POLY_USE_BOOST "Use boost for stdx polyfills" OFF)
2324

2425
pkg_check_modules(LIBBSON REQUIRED libbson-${LIBBSON_REQUIRED_ABI_VERSION})
2526

@@ -56,6 +57,8 @@ link_directories(${LIBBSON_LIBRARY_DIRS})
5657
if (BSONCXX_POLY_USE_MNMLSTC AND NOT BSONCXX_POLY_USE_SYSTEM_MNMLSTC)
5758
ExternalProject_Get_Property(EP_mnmlstc_core source_dir)
5859
include_directories(${source_dir}/include)
60+
elseif (BSONCXX_POLY_USE_BOOST)
61+
find_package(Boost REQUIRED)
5962
endif()
6063

6164
add_library(bsoncxx_static STATIC
@@ -92,6 +95,9 @@ generate_export_header(bsoncxx
9295
if (BSONCXX_POLY_USE_MNMLSTC AND NOT BSONCXX_POLY_USE_SYSTEM_MNMLSTC)
9396
add_dependencies(bsoncxx_static EP_mnmlstc_core)
9497
add_dependencies(bsoncxx EP_mnmlstc_core)
98+
elseif (BSONCXX_POLY_USE_BOOST)
99+
target_include_directories(bsoncxx_static PUBLIC ${Boost_INCLUDE_DIRS})
100+
target_include_directories(bsoncxx PUBLIC ${Boost_INCLUDE_DIRS})
95101
endif()
96102

97103
target_link_libraries(bsoncxx_static ${bsoncxx_libs})

src/bsoncxx/config/config.hpp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#cmakedefine BSONCXX_POLY_USE_STD_EXPERIMENTAL
1616
#cmakedefine BSONCXX_POLY_USE_MNMLSTC
1717
#cmakedefine BSONCXX_POLY_USE_SYSTEM_MNMLSTC
18+
#cmakedefine BSONCXX_POLY_USE_BOOST
1819

1920
#define BSONCXX_INLINE_NAMESPACE_BEGIN inline namespace @BSONCXX_INLINE_NAMESPACE@ {
2021

src/bsoncxx/config/postlude.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#pragma pop_macro("BSONCXX_POLY_USE_STD_EXPERIMENTAL")
3232
#undef BSONCXX_POLY_USE_SYSTEM_MNMLSTC
3333
#pragma pop_macro("BSONCXX_POLY_USE_SYSTEM_MNMLSTC")
34+
#undef BSONCXX_POLY_USE_BOOST
35+
#pragma pop_macro("BSONCXX_POLY_USE_BOOST")
3436

3537
// src/bsoncxx/config/version.hpp.in
3638
#undef BSONCXX_VERSION_EXTRA

src/bsoncxx/config/prelude.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#undef BSONCXX_POLY_USE_STD_EXPERIMENTAL
3232
#pragma push_macro("BSONCXX_POLY_USE_SYSTEM_MNMLSTC")
3333
#undef BSONCXX_POLY_USE_SYSTEM_MNMLSTC
34+
#pragma push_macro("BSONCXX_POLY_USE_BOOST")
35+
#undef BSONCXX_POLY_USE_BOOST
3436

3537
// src/bsoncxx/config/version.hpp.in
3638
#pragma push_macro("BSONCXX_VERSION_EXTRA")

src/bsoncxx/stdx/make_unique.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,25 @@ using ::core::make_unique;
3232
} // namespace stdx
3333
} // namespace bsoncxx
3434

35-
#elif __cplusplus >= 201402L
35+
#elif defined(BSONCXX_POLY_USE_BOOST)
36+
37+
#include <boost/smart_ptr/make_unique.hpp>
3638

3739
namespace bsoncxx {
3840
namespace stdx {
3941

42+
using ::boost::make_unique;
43+
44+
} // namespace stdx
45+
} // namespace bsoncxx
46+
47+
#elif __cplusplus >= 201402L
48+
4049
#include <memory>
4150

51+
namespace bsoncxx {
52+
namespace stdx {
53+
4254
using ::std::make_unique;
4355

4456
} // namespace stdx

src/bsoncxx/stdx/optional.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,24 @@ using ::core::make_optional;
3434
} // namespace stdx
3535
} // namespace bsoncxx
3636

37+
#elif defined(BSONCXX_POLY_USE_BOOST)
38+
39+
#include <boost/none.hpp>
40+
#include <boost/optional/optional.hpp>
41+
42+
namespace bsoncxx {
43+
namespace stdx {
44+
45+
using ::boost::optional;
46+
using nullopt_t = ::boost::none_t;
47+
constexpr nullopt_t nullopt{};
48+
using ::boost::make_optional;
49+
50+
} // namespace stdx
51+
} // namespace bsoncxx
52+
3753
#elif defined(BSONCXX_POLY_USE_STD_EXPERIMENTAL)
54+
3855
#include <experimental/optional>
3956

4057
namespace bsoncxx {

src/bsoncxx/stdx/string_view.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,20 @@ using ::core::string_view;
3232
} // namespace stdx
3333
} // namespace bsoncxx
3434

35+
#elif defined(BSONCXX_POLY_USE_BOOST)
36+
37+
#include <boost/utility/string_ref.hpp>
38+
39+
namespace bsoncxx {
40+
namespace stdx {
41+
42+
using string_view = ::boost::string_ref;
43+
44+
} // namespace stdx
45+
} // namespace bsoncxx
46+
3547
#elif defined(BSONCXX_POLY_USE_STD_EXPERIMENTAL)
48+
3649
#include <experimental/string_view>
3750

3851
namespace bsoncxx {

src/bsoncxx/types/value.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,23 @@ namespace bsoncxx {
2727
BSONCXX_INLINE_NAMESPACE_BEGIN
2828
namespace types {
2929

30-
#define BSONCXX_ENUM(name, val) \
31-
value::value(b_##name value) noexcept : _type(static_cast<bsoncxx::type>(val)), \
32-
_b_##name(std::move(value)) { \
33-
static_assert(std::is_nothrow_copy_constructible<b_##name>::value, "Copy may throw"); \
34-
static_assert(std::is_nothrow_copy_assignable<b_##name>::value, "Copy may throw"); \
30+
// Boost doesn't mark the copy constructor and copy-assignment operator of string_ref as noexcept
31+
// so we can't rely on automatic noexcept propagation. It really is though, so it is OK.
32+
#if !defined(BSONCXX_POLY_USE_BOOST)
33+
#define BSONCXX_ENUM(name, val) \
34+
value::value(b_##name value) noexcept : _type(static_cast<bsoncxx::type>(val)), \
35+
_b_##name(std::move(value)) { \
36+
static_assert(std::is_nothrow_copy_constructible<b_##name>::value, "Copy may throw"); \
37+
static_assert(std::is_nothrow_copy_assignable<b_##name>::value, "Copy may throw"); \
3538
static_assert(std::is_nothrow_destructible<b_##name>::value, "Destruction may throw"); \
3639
}
40+
#else
41+
#define BSONCXX_ENUM(name, val) \
42+
value::value(b_##name value) noexcept : _type(static_cast<bsoncxx::type>(val)), \
43+
_b_##name(std::move(value)) { \
44+
static_assert(std::is_nothrow_destructible<b_##name>::value, "Destruction may throw"); \
45+
}
46+
#endif
3747

3848
#include <bsoncxx/enums/type.hpp>
3949
#undef BSONCXX_ENUM

src/mongocxx/client.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ stdx::optional<class read_concern> client::read_concern() const {
7373
if (!libmongoc::read_concern_get_level(rc)) {
7474
return stdx::nullopt;
7575
}
76-
return {
77-
stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))};
76+
77+
return {(class read_concern){
78+
stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
7879
}
7980

8081
void client::read_preference(class read_preference rp) {

src/mongocxx/collection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ stdx::optional<class read_concern> collection::read_concern() const {
714714
if (!libmongoc::read_concern_get_level(rc)) {
715715
return stdx::nullopt;
716716
}
717-
return {stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))};
717+
return {(class read_concern){stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
718718
}
719719

720720
void collection::read_preference(class read_preference rp) {

src/mongocxx/database.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ stdx::optional<class read_concern> database::read_concern() const {
144144
if (!libmongoc::read_concern_get_level(rc)) {
145145
return stdx::nullopt;
146146
}
147-
return {stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))};
147+
return {(class read_concern){stdx::make_unique<read_concern::impl>(libmongoc::read_concern_copy(rc))}};
148148
}
149149

150150
void database::read_preference(class read_preference rp) {

src/mongocxx/pool.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ pool::entry pool::acquire() {
6161

6262
stdx::optional<pool::entry> pool::try_acquire() {
6363
auto cli = libmongoc::client_pool_try_pop(_impl->client_pool_t);
64-
return cli ? stdx::make_optional<entry>({new client(cli), [this](client* client) {
65-
_release(client);
66-
}}) : stdx::nullopt;
64+
if (!cli)
65+
return stdx::nullopt;
66+
67+
return pool::entry{new client(cli), [this](client* client) {
68+
_release(client);
69+
}};
6770
}
6871

6972
void* pool::implementation() const {

src/mongocxx/read_concern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ stdx::optional<stdx::string_view> read_concern::acknowledge_string() const {
8282
if (!level) {
8383
return stdx::nullopt;
8484
}
85-
return {level};
85+
return {stdx::string_view{level}};
8686
}
8787

8888
MONGOCXX_INLINE_NAMESPACE_END

src/mongocxx/test/write_concern.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ TEST_CASE("write_concern fields may be set and retrieved", "[write_concern]") {
7373
SECTION("a tag may be set") {
7474
const std::string tag{"MultipleDC"};
7575
wc.tag(tag);
76-
REQUIRE(tag == wc.tag());
76+
auto maybe_tag = wc.tag();
77+
REQUIRE(maybe_tag);
78+
REQUIRE(tag == *maybe_tag);
7779
}
7880

7981
SECTION("the number of nodes requiring confirmation may be set to a number") {

0 commit comments

Comments
 (0)