Convert is_callable to a backport of std::is_invocable (#3023)

This commit is contained in:
Oxan van Leeuwen 2022-01-09 23:07:37 +01:00 committed by GitHub
parent 6b773553fc
commit 499625f266
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 15 deletions

View file

@ -12,10 +12,10 @@ template<typename... X> class TemplatableStringValue : public TemplatableValue<s
public: public:
TemplatableStringValue() : TemplatableValue<std::string, X...>() {} TemplatableStringValue() : TemplatableValue<std::string, X...>() {}
template<typename F, enable_if_t<!is_callable<F, X...>::value, int> = 0> template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
TemplatableStringValue(F value) : TemplatableValue<std::string, X...>(value) {} TemplatableStringValue(F value) : TemplatableValue<std::string, X...>(value) {}
template<typename F, enable_if_t<is_callable<F, X...>::value, int> = 0> template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
TemplatableStringValue(F f) TemplatableStringValue(F f)
: TemplatableValue<std::string, X...>([f](X... x) -> std::string { return to_string(f(x...)); }) {} : TemplatableValue<std::string, X...>([f](X... x) -> std::string { return to_string(f(x...)); }) {}
}; };

View file

@ -21,10 +21,10 @@ template<typename T, typename... X> class TemplatableValue {
public: public:
TemplatableValue() : type_(EMPTY) {} TemplatableValue() : type_(EMPTY) {}
template<typename F, enable_if_t<!is_callable<F, X...>::value, int> = 0> template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
TemplatableValue(F value) : type_(VALUE), value_(value) {} TemplatableValue(F value) : type_(VALUE), value_(value) {}
template<typename F, enable_if_t<is_callable<F, X...>::value, int> = 0> template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
TemplatableValue(F f) : type_(LAMBDA), f_(f) {} TemplatableValue(F f) : type_(LAMBDA), f_(f) {}
bool has_value() { return this->type_ != EMPTY; } bool has_value() { return this->type_ != EMPTY; }

View file

@ -186,17 +186,6 @@ template<typename... Ts> class CallbackManager<void(Ts...)> {
std::vector<std::function<void(Ts...)>> callbacks_; std::vector<std::function<void(Ts...)>> callbacks_;
}; };
// https://stackoverflow.com/a/37161919/8924614
template<class T, class... Args>
struct is_callable // NOLINT
{
template<class U> static auto test(U *p) -> decltype((*p)(std::declval<Args>()...), void(), std::true_type());
template<class U> static auto test(...) -> decltype(std::false_type());
static constexpr auto value = decltype(test<T>(nullptr))::value; // NOLINT
};
void delay_microseconds_safe(uint32_t us); void delay_microseconds_safe(uint32_t us);
template<typename T> class Deduplicator { template<typename T> class Deduplicator {
@ -274,6 +263,18 @@ template<typename T> constexpr const T &clamp(const T &v, const T &lo, const T &
} }
#endif #endif
// std::is_invocable from C++17
#if __cpp_lib_is_invocable >= 201703
using std::is_invocable;
#else
// https://stackoverflow.com/a/37161919/8924614
template<class T, class... Args> struct is_invocable { // NOLINT(readability-identifier-naming)
template<class U> static auto test(U *p) -> decltype((*p)(std::declval<Args>()...), void(), std::true_type());
template<class U> static auto test(...) -> decltype(std::false_type());
static constexpr auto value = decltype(test<T>(nullptr))::value; // NOLINT
};
#endif
// std::bit_cast from C++20 // std::bit_cast from C++20
#if __cpp_lib_bit_cast >= 201806 #if __cpp_lib_bit_cast >= 201806
using std::bit_cast; using std::bit_cast;