mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 17:54:13 +01:00
Convert is_callable to a backport of std::is_invocable (#3023)
This commit is contained in:
parent
6b773553fc
commit
499625f266
3 changed files with 16 additions and 15 deletions
|
@ -12,10 +12,10 @@ template<typename... X> class TemplatableStringValue : public TemplatableValue<s
|
|||
public:
|
||||
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) {}
|
||||
|
||||
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)
|
||||
: TemplatableValue<std::string, X...>([f](X... x) -> std::string { return to_string(f(x...)); }) {}
|
||||
};
|
||||
|
|
|
@ -21,10 +21,10 @@ template<typename T, typename... X> class TemplatableValue {
|
|||
public:
|
||||
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) {}
|
||||
|
||||
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) {}
|
||||
|
||||
bool has_value() { return this->type_ != EMPTY; }
|
||||
|
|
|
@ -186,17 +186,6 @@ template<typename... Ts> class CallbackManager<void(Ts...)> {
|
|||
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);
|
||||
|
||||
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
|
||||
|
||||
// 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
|
||||
#if __cpp_lib_bit_cast >= 201806
|
||||
using std::bit_cast;
|
||||
|
|
Loading…
Reference in a new issue