invoke syntactic sugar

This commit is contained in:
BlackMATov
2021-07-13 13:19:09 +07:00
parent 2ea9a01f32
commit 6677514faf
11 changed files with 263 additions and 35 deletions

View File

@@ -103,7 +103,7 @@ namespace meta_hpp
template < typename... Args >
std::optional<ctor_info> get_ctor() const {
for ( auto&& family_info : ctors_ ) {
if ( family_info.second.is_invocable_with<Args...>() ) {
if ( family_info.second.is_invocable<Args...>() ) {
return family_info.second;
}
}

View File

@@ -131,8 +131,18 @@ namespace meta_hpp
}
}
template < typename R, typename... Args >
R invoke_r(Args&&... args) const {
return invoke(std::forward<Args>(args)...).template cast<R>();
}
template < typename... Args >
bool is_invocable_with() const noexcept {
value operator()(Args&&... args) const {
return invoke(std::forward<Args>(args)...);
}
template < typename... Args >
bool is_invocable() const noexcept {
return ctor_detail::parameters_equal<Args...>(argument_types_);
}

View File

@@ -122,9 +122,23 @@ namespace meta_hpp
return getter_(instance);
}
template < typename R >
R get_r(cinstance instance) const {
return get(instance).cast<R>();
}
template < typename Value >
void set(instance instance, Value&& value) const {
return setter_(instance, std::forward<Value>(value));
setter_(instance, std::forward<Value>(value));
}
value operator()(cinstance instance) const {
return get(instance);
}
template < typename Value >
void operator()(instance instance, Value&& value) const {
set(instance, std::forward<Value>(value));
}
template < typename F >

View File

@@ -134,6 +134,19 @@ namespace meta_hpp
}
}
template < typename R, typename... Args >
std::optional<R> invoke_r(Args&&... args) const {
if ( std::optional<value> r = invoke(std::forward<Args>(args)...) ) {
return std::move(r)->template cast<R>();
}
return std::nullopt;
}
template < typename... Args >
std::optional<value> operator()(Args&&... args) const {
return invoke(std::forward<Args>(args)...);
}
template < typename F >
void each_data(F&& f) const {
for ( auto&& id_info : datas_ ) {

View File

@@ -208,20 +208,31 @@ namespace meta_hpp
std::optional<value> invoke(T& inst, Args&&... args) const {
if constexpr ( sizeof...(Args) > 0u ) {
std::array<value, sizeof...(Args)> vargs{std::forward<Args>(args)...};
return invoke_(inst, vargs.data(), vargs.size());
if constexpr ( std::is_const_v<T> ) {
return cinvoke_(inst, vargs.data(), vargs.size());
} else {
return invoke_(inst, vargs.data(), vargs.size());
}
} else {
return invoke_(inst, nullptr, 0u);
if constexpr ( std::is_const_v<T> ) {
return cinvoke_(inst, nullptr, 0u);
} else {
return invoke_(inst, nullptr, 0u);
}
}
}
template < typename T, typename... Args >
std::optional<value> invoke(const T& inst, Args&&... args) const {
if constexpr ( sizeof...(Args) > 0u ) {
std::array<value, sizeof...(Args)> vargs{std::forward<Args>(args)...};
return cinvoke_(inst, vargs.data(), vargs.size());
} else {
return cinvoke_(inst, nullptr, 0u);
template < typename R, typename T, typename... Args >
std::optional<R> invoke_r(T& inst, Args&&... args) const {
if ( std::optional<value> r = invoke(inst, std::forward<Args>(args)...) ) {
return std::move(r)->template cast<R>();
}
return std::nullopt;
}
template < typename T, typename... Args >
std::optional<value> operator()(T& inst, Args&&... args) const {
return invoke(inst, std::forward<Args>(args)...);
}
template < typename F >

View File

@@ -96,11 +96,25 @@ namespace meta_hpp
return getter_();
}
template < typename R >
R get_r() const {
return get().cast<R>();
}
template < typename Value >
void set(Value&& value) const {
return setter_(std::forward<Value>(value));
}
value operator()() const {
return get();
}
template < typename Value >
void operator()(Value&& value) const {
return set(std::forward<Value>(value));
}
template < typename F >
void each_data(F&& f) const {
for ( auto&& id_info : datas_ ) {