Skip to content

[embind] Use std::disjunction for checking policy arguments. #22603

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 1 commit into from
Sep 20, 2024
Merged
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
67 changes: 23 additions & 44 deletions system/include/emscripten/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,27 @@ struct reference : public allow_raw_pointers {};

namespace internal {

#if __cplusplus >= 201703L
template <typename... Args> using conjunction = std::conjunction<Args...>;
template <typename... Args> using disjunction = std::disjunction<Args...>;
#else
// Helper available in C++14.
template <bool _Test, class _T1, class _T2>
using conditional_t = typename std::conditional<_Test, _T1, _T2>::type;

template<class...> struct conjunction : std::true_type {};
template<class B1> struct conjunction<B1> : B1 {};
template<class B1, class... Bn>
struct conjunction<B1, Bn...>
: conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};

template<class...> struct disjunction : std::false_type {};
template<class B1> struct disjunction<B1> : B1 {};
template<class B1, class... Bn>
struct disjunction<B1, Bn...>
: conditional_t<bool(B1::value), disjunction<Bn...>, B1> {};
#endif

template<typename... Policies>
struct isPolicy;

Expand Down Expand Up @@ -428,40 +449,10 @@ struct GetReturnValuePolicy<ReturnType, T, Rest...> {
};

template<typename... Policies>
struct isAsync;

template<typename... Rest>
struct isAsync<async, Rest...> {
static constexpr bool value = true;
};

template<typename T, typename... Rest>
struct isAsync<T, Rest...> {
static constexpr bool value = isAsync<Rest...>::value;
};

template<>
struct isAsync<> {
static constexpr bool value = false;
};
using isAsync = disjunction<std::is_same<async, Policies>...>;

template<typename... Policies>
struct isNonnullReturn;

template<typename... Rest>
struct isNonnullReturn<nonnull<ret_val>, Rest...> {
static constexpr bool value = true;
};

template<typename T, typename... Rest>
struct isNonnullReturn<T, Rest...> {
static constexpr bool value = isNonnullReturn<Rest...>::value;
};

template<>
struct isNonnullReturn<> {
static constexpr bool value = false;
};
using isNonnullReturn = disjunction<std::is_same<nonnull<ret_val>, Policies>...>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

What are async and non_null in the above lines by the way? Are they special values defines somewhere, or just some kind of placeholder?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Policy args are basically options you can pass into function bindings. e.g.

   function('sleep', &sleep, async());
   function('constructor', &constructor, nonnull<ret_val>());


}

Expand Down Expand Up @@ -985,18 +976,6 @@ struct SetterPolicy<PropertyTag<Setter, SetterArgumentType>> {
}
};

// Helper available in C++14.
template <bool _Test, class _T1, class _T2>
using conditional_t = typename std::conditional<_Test, _T1, _T2>::type;

// Conjunction is available in C++17
template<class...> struct conjunction : std::true_type {};
template<class B1> struct conjunction<B1> : B1 {};
template<class B1, class... Bn>
struct conjunction<B1, Bn...>
: conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};


class noncopyable {
protected:
noncopyable() {}
Expand Down
Loading