Skip to content

[CXX-2625] More type traits + invoke() #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9b87cd6
Bring additional type_traits
vector-of-bool Oct 16, 2023
9c4b2c3
invoke() and friends
vector-of-bool Oct 16, 2023
90e4b49
Trying to fix VS2015
vector-of-bool Oct 18, 2023
9be7bc7
Tweak signature of requires_t
vector-of-bool Oct 18, 2023
dbdd785
Minor formatting
vector-of-bool Oct 18, 2023
7354ee3
More traits usage tweaks
vector-of-bool Oct 18, 2023
30e690a
Tweak invoke() to be a callable
vector-of-bool Oct 19, 2023
e48891f
Incorrect requires_t
vector-of-bool Oct 19, 2023
aa8f847
Merge branch 'master' into more-type_traits
vector-of-bool Oct 19, 2023
f4f60c3
Formatting fixes for lint
vector-of-bool Oct 20, 2023
86e7665
Missing dist listing
vector-of-bool Oct 20, 2023
effc1fa
Merge branch 'master' into more-type_traits
vector-of-bool Oct 30, 2023
8db1259
Rework namespace shape for type_traits to be private. Refactor of
vector-of-bool Nov 2, 2023
10f8740
Missed some traits usages
vector-of-bool Nov 2, 2023
03c898b
VS 2015 Can't handle our scale
vector-of-bool Nov 2, 2023
0c2e0f5
Missed a namespace
vector-of-bool Nov 2, 2023
84b9177
Better diagnostics on non-VS2015
vector-of-bool Nov 2, 2023
d88f258
tt_detail->detail
vector-of-bool Nov 2, 2023
74d92cc
Add back a not_view alias template
vector-of-bool Nov 2, 2023
7977ccd
Error message fix
vector-of-bool Nov 2, 2023
cab0029
Bump MSVC requirement for better requires_t
vector-of-bool Nov 2, 2023
73acf08
Rename namespaces to just use "detail" for type traits
vector-of-bool Nov 6, 2023
7a7b651
Unused glob
vector-of-bool Nov 6, 2023
d6cf410
VS2015 parser is broken here
vector-of-bool Nov 6, 2023
b8108a5
Declare the inline namespace directly
vector-of-bool Nov 6, 2023
d0d5ba4
Merge branch 'master' into more-type_traits
vector-of-bool Nov 7, 2023
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
1 change: 1 addition & 0 deletions src/bsoncxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ set_dist_list(src_bsoncxx_include_DIST
bsoncxx/v_noabi/bsoncxx/stdx/make_unique.hpp
bsoncxx/v_noabi/bsoncxx/stdx/optional.hpp
bsoncxx/v_noabi/bsoncxx/stdx/string_view.hpp
bsoncxx/v_noabi/bsoncxx/stdx/type_traits.hpp
bsoncxx/v_noabi/bsoncxx/string/to_string.hpp
bsoncxx/v_noabi/bsoncxx/string/view_or_value.hpp
bsoncxx/v_noabi/bsoncxx/types.hpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <bsoncxx/builder/basic/sub_array.hpp>
#include <bsoncxx/builder/basic/sub_document.hpp>
#include <bsoncxx/util/functor.hpp>
#include <bsoncxx/stdx/type_traits.hpp>

#include <bsoncxx/config/prelude.hpp>

Expand All @@ -27,31 +27,26 @@ namespace basic {
namespace impl {

template <typename T>
using takes_document = typename util::is_functor<T, void(sub_document)>;

template <typename T>
using takes_array = typename util::is_functor<T, void(sub_array)>;

template <typename T>
BSONCXX_INLINE typename std::enable_if<takes_document<T>::value, void>::type generic_append(
core* core, T&& func) {
BSONCXX_INLINE detail::requires_t<void, detail::is_invocable<T, sub_document>> //
generic_append(core* core, T&& func) {
core->open_document();
func(sub_document(core));
detail::invoke(std::forward<T>(func), sub_document(core));
core->close_document();
}

template <typename T>
BSONCXX_INLINE typename std::enable_if<takes_array<T>::value, void>::type generic_append(core* core,
T&& func) {
template <typename T, typename Placeholder = void> // placeholder 'void' for VS2015 compat
BSONCXX_INLINE detail::requires_t<void, detail::is_invocable<T, sub_array>> //
generic_append(core* core, T&& func) {
core->open_array();
func(sub_array(core));
detail::invoke(std::forward<T>(func), sub_array(core));
core->close_array();
}

template <typename T>
BSONCXX_INLINE
typename std::enable_if<!takes_document<T>::value && !takes_array<T>::value, void>::type
generic_append(core* core, T&& t) {
template <typename T, typename = void, typename = void>
BSONCXX_INLINE detail::requires_not_t<void, //
detail::is_invocable<T, sub_document>,
detail::is_invocable<T, sub_array>>
generic_append(core* core, T&& t) {
core->append(std::forward<T>(t));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <bsoncxx/builder/concatenate.hpp>
#include <bsoncxx/builder/core.hpp>
#include <bsoncxx/stdx/string_view.hpp>
#include <bsoncxx/stdx/type_traits.hpp>

#include <bsoncxx/config/prelude.hpp>

Expand Down Expand Up @@ -59,8 +60,7 @@ class sub_document {
// Appends a basic::kvp where the key is a non-owning string view.
//
template <typename K, typename V>
BSONCXX_INLINE typename std::enable_if<
std::is_same<typename std::decay<K>::type, stdx::string_view>::value>::type
BSONCXX_INLINE detail::requires_t<void, detail::is_alike<K, stdx::string_view>> //
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)));
Expand All @@ -70,8 +70,7 @@ class sub_document {
// Appends a basic::kvp where the key is an owning STL string.
//
template <typename K, typename V>
BSONCXX_INLINE typename std::enable_if<
std::is_same<typename std::decay<K>::type, std::string>::value>::type
BSONCXX_INLINE detail::requires_t<void, detail::is_alike<K, std::string>> //
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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <stdexcept>
#include <type_traits>

#include "bsoncxx/stdx/type_traits.hpp"
#include <bsoncxx/array/value.hpp>
#include <bsoncxx/array/view.hpp>
#include <bsoncxx/document/value.hpp>
Expand Down Expand Up @@ -508,7 +509,7 @@ class BSONCXX_API core {
///
template <typename T>
BSONCXX_INLINE core& append(T* v) {
static_assert(std::is_same<typename std::remove_const<T>::type, char>::value,
static_assert(detail::is_alike<T, char>::value,
"append is disabled for non-char pointer types");
append(types::b_string{v});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <bsoncxx/builder/core.hpp>
#include <bsoncxx/builder/stream/closed_context.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/util/functor.hpp>
#include <bsoncxx/stdx/type_traits.hpp>

#include <bsoncxx/config/prelude.hpp>

Expand Down Expand Up @@ -67,11 +67,10 @@ class array_context {
/// The value to append
///
template <class T>
BSONCXX_INLINE typename std::enable_if<
!(util::is_functor<T, void(array_context<>)>::value ||
util::is_functor<T, void(single_context)>::value ||
std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value),
array_context>::type&
BSONCXX_INLINE detail::requires_not_t<array_context&,
detail::is_invocable<T, array_context<>>,
detail::is_invocable<T, single_context>,
detail::is_alike<T, finalize_type>>
operator<<(T&& t) {
_core->append(std::forward<T>(t));
return *this;
Expand All @@ -86,11 +85,12 @@ class array_context {
/// The callback to invoke
///
template <typename Func>
BSONCXX_INLINE typename std::enable_if<(util::is_functor<Func, void(array_context<>)>::value ||
util::is_functor<Func, void(single_context)>::value),
array_context>::type&
operator<<(Func&& func) {
func(*this);
BSONCXX_INLINE
detail::requires_t<array_context&,
detail::disjunction<detail::is_invocable<Func, array_context>,
detail::is_invocable<Func, single_context>>>
operator<<(Func&& func) {
detail::invoke(std::forward<Func>(func), *this);
return *this;
}

Expand All @@ -105,12 +105,10 @@ class array_context {
/// @return A value type which holds the complete bson document.
///
template <typename T>
BSONCXX_INLINE typename std::enable_if<
std::is_same<base, closed_context>::value &&
std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value,
// TODO(MSVC): This should just be 'array::value', but
// VS2015U1 can't resolve the name.
bsoncxx::array::value>::type
BSONCXX_INLINE detail::requires_t<bsoncxx::array::value,
std::is_same<base, closed_context>,
detail::is_alike<T, finalize_type>>
// VS2015U1 can't resolve the name.
operator<<(T&&) {
return _core->extract_array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <bsoncxx/builder/stream/closed_context.hpp>
#include <bsoncxx/builder/stream/value_context.hpp>
#include <bsoncxx/stdx/string_view.hpp>
#include <bsoncxx/util/functor.hpp>
#include <bsoncxx/stdx/type_traits.hpp>

#include <bsoncxx/config/prelude.hpp>

Expand Down Expand Up @@ -108,10 +108,9 @@ class key_context {
/// The callback to invoke
///
template <typename T>
BSONCXX_INLINE
typename std::enable_if<util::is_functor<T, void(key_context<>)>::value, key_context>::type&
operator<<(T&& func) {
func(*this);
BSONCXX_INLINE detail::requires_t<key_context&, detail::is_invocable<T, key_context>> //
operator<<(T&& func) {
detail::invoke(std::forward<T>(func), *this);
return *this;
}

Expand All @@ -126,12 +125,9 @@ class key_context {
/// @return A value type which holds the complete bson document.
///
template <typename T>
BSONCXX_INLINE typename std::enable_if<
std::is_same<base, closed_context>::value &&
std::is_same<typename std::remove_reference<T>::type, const finalize_type>::value,
// TODO(MSVC): This should just be 'document::value', but
// VS2015U1 can't resolve the name.
bsoncxx::document::value>::type
BSONCXX_INLINE detail::requires_t<bsoncxx::document::value,
std::is_same<base, closed_context>,
detail::is_alike<T, finalize_type>>
operator<<(T&&) {
return _core->extract_document();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <bsoncxx/builder/stream/array_context.hpp>
#include <bsoncxx/builder/stream/closed_context.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/util/functor.hpp>
#include <bsoncxx/stdx/type_traits.hpp>

#include <bsoncxx/config/prelude.hpp>

Expand Down Expand Up @@ -63,9 +63,8 @@ class value_context {
/// The value to append
///
template <class T>
BSONCXX_INLINE
typename std::enable_if<!util::is_functor<T, void(single_context)>::value, base>::type
operator<<(T&& t) {
BSONCXX_INLINE detail::requires_not_t<base, detail::is_invocable<T, single_context>> //
operator<<(T&& t) {
_core->append(std::forward<T>(t));
return unwrap();
}
Expand All @@ -78,10 +77,9 @@ class value_context {
/// The callback to invoke
///
template <typename T>
BSONCXX_INLINE
typename std::enable_if<util::is_functor<T, void(single_context)>::value, base>::type
operator<<(T&& func) {
func(*this);
BSONCXX_INLINE detail::requires_t<base, detail::is_invocable<T, single_context>> //
operator<<(T&& func) {
detail::invoke(std::forward<T>(func), *this);
return unwrap();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

#include <cstdlib>
#include <memory>
#include <type_traits>

#include "bsoncxx/stdx/type_traits.hpp"
#include <bsoncxx/array/view.hpp>
#include <bsoncxx/document/view.hpp>

Expand Down Expand Up @@ -84,8 +86,7 @@ class BSONCXX_API value {
/// @param t
/// A user-defined object to serialize into a BSON object.
///
template <typename T,
typename std::enable_if<!std::is_same<T, typename array::view>::value, int>::type = 0>
template <typename T, detail::requires_not_t<int, std::is_same<T, array::view>> = 0>
explicit value(const T& t) : value({}) {
to_bson(t, *this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <type_traits>
#include <utility>

#include "./type_traits.hpp"

#include <bsoncxx/config/prelude.hpp>

namespace bsoncxx {
Expand Down
Loading