Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/bsoncxx/builder/basic/sub_document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ void value_append(core* core, T&& t);
/// Users should almost always construct a builder::basic::document instead.
///
class BSONCXX_API sub_document {
template <typename T, typename U>
struct is_same_stripped_type
: public std::is_same<
typename std::remove_reference<typename std::remove_cv<T>::type>::type, U> {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just make this std::is_same<typename std::decay<T>::type, U>?

If that works you can just get rid of this helper and use std::is_same directly


public:
BSONCXX_INLINE sub_document(core* core) : _core(core) {
}
Expand All @@ -51,31 +56,33 @@ class BSONCXX_API sub_document {
///
/// Appends a basic::kvp where the key is a non-owning string view.
///
template <typename T>
template <typename K, typename V>
BSONCXX_INLINE
void append(std::tuple<stdx::string_view, T>&& t) {
_core->key_view(std::get<0>(t));
impl::value_append(_core, std::forward<T>(std::get<1>(t)));
typename std::enable_if<is_same_stripped_type<K, stdx::string_view>::value>::type
append(std::tuple<K, V>&& t) {
_core->key_view(std::forward<K>(std::get<0>(t)));
impl::value_append(_core, std::forward<V>(std::get<1>(t)));
}

///
/// Appends a basic::kvp where the key is an owning STL string.
///
template <typename T>
template <typename K, typename V>
BSONCXX_INLINE
void append(std::tuple<std::string, T>&& t) {
_core->key_owned(std::get<0>(t));
impl::value_append(_core, std::forward<T>(std::get<1>(t)));
typename std::enable_if<is_same_stripped_type<K, std::string>::value>::type
append(std::tuple<K, V>&& t) {
_core->key_owned(std::forward<K>(std::get<0>(t)));
impl::value_append(_core, std::forward<V>(std::get<1>(t)));
}

///
/// Appends a basic::kvp where the key is a string literal
///
template <std::size_t n, typename T>
template <std::size_t n, typename V>
BSONCXX_INLINE
void append(std::tuple<const char (&)[n], T>&& t) {
void append(std::tuple<const char (&)[n], V>&& t) {
_core->key_view(stdx::string_view{std::get<0>(t), n - 1});
impl::value_append(_core, std::forward<T>(std::get<1>(t)));
impl::value_append(_core, std::forward<V>(std::get<1>(t)));
}

private:
Expand Down
23 changes: 23 additions & 0 deletions src/bsoncxx/test/bson_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,29 @@ TEST_CASE("basic document builder works", "[bsoncxx::builder::basic]") {
viewable_eq_viewable(stream, basic);
}

SECTION("kvp works with std::string key/value") {
{
using namespace builder::basic;

std::string a("hello");
std::string b("world");
basic.append(kvp(a, b));
}

viewable_eq_viewable(stream, basic);
}

SECTION("kvp works with stdx::string_view key/value") {
{
using namespace builder::basic;

stdx::string_view a("hello");
stdx::string_view b("world");
basic.append(kvp(a, b));
}

viewable_eq_viewable(stream, basic);
}
SECTION("variadic works") {
{
using namespace builder::stream;
Expand Down