Skip to content

Commit 27b7a03

Browse files
committed
rework overriders
1 parent 44003c2 commit 27b7a03

File tree

2 files changed

+58
-37
lines changed

2 files changed

+58
-37
lines changed

include/yorel/yomm2/core.hpp

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -715,26 +715,65 @@ class method<Name, Return(Parameters...), Options...>
715715
detail::types<OverriderParameters...>>>;
716716
};
717717

718-
template<class Container, bool has_next>
719-
struct override_;
718+
template<auto Function>
719+
struct override_fn_impl {
720+
explicit override_fn_impl(Next* next = nullptr) {
721+
static detail::definition_info info;
720722

721-
friend class generator;
723+
if (info.method) {
724+
BOOST_ASSERT(info.method == &fn);
725+
return;
726+
}
722727

723-
public:
724-
template<auto Function>
725-
struct override_fn;
728+
info.method = &fn;
729+
info.type = Policy::template static_type<decltype(Function)>();
730+
info.next = reinterpret_cast<void**>(next);
731+
using Thunk = thunk<Function, decltype(Function)>;
732+
info.pf = (void*)Thunk::fn;
733+
info.vp_begin = Thunk::OverriderParameterTypeIds::begin;
734+
info.vp_end = Thunk::OverriderParameterTypeIds::end;
735+
fn.specs.push_back(info);
736+
}
737+
};
738+
739+
template<auto Function, typename FunctionType>
740+
struct override_fn_aux;
741+
742+
template<auto Function, typename FnReturnType, typename... FnParameters>
743+
struct override_fn_aux<Function, FnReturnType (*)(FnParameters...)>
744+
: override_fn_impl<Function> {
745+
using override_fn_impl<Function>::override_fn_impl;
746+
};
747+
748+
template<
749+
auto Function, class FnClass, typename FnReturnType,
750+
typename... FnParameters>
751+
struct override_fn_aux<
752+
Function, FnReturnType (FnClass::*)(FnParameters...)> {
753+
static auto fn(FnClass* this_, FnParameters&&... args) -> FnReturnType {
754+
return (this_->*Function)(std::forward<FnParameters>(args)...);
755+
}
756+
757+
override_fn_impl<fn> impl;
758+
};
759+
760+
template<class Container, bool HasNext>
761+
struct override_aux;
726762

727-
private:
728763
template<class Container>
729-
struct override_<Container, false> {
730-
override_fn<Container::fn> override_{nullptr};
764+
struct override_aux<Container, false> : override_fn<Container::fn> {
765+
override_aux() : override_fn<Container::fn>(nullptr) {
766+
}
731767
};
732768

733769
template<class Container>
734-
struct override_<Container, true> {
735-
override_fn<Container::fn> add{&Container::next};
770+
struct override_aux<Container, true> : override_fn<Container::fn> {
771+
override_aux() : override_fn<Container::fn>(&Container::next) {
772+
}
736773
};
737774

775+
friend class generator;
776+
738777
public:
739778
// Public aliases.
740779
using return_type = Return;
@@ -754,38 +793,20 @@ class method<Name, Return(Parameters...), Options...>
754793
using next = detail::next_aux<method, Container>;
755794

756795
template<auto Function>
757-
struct override_fn {
758-
explicit override_fn(Next* next = nullptr) {
759-
static detail::definition_info info;
760-
761-
if (info.method) {
762-
BOOST_ASSERT(info.method == &fn);
763-
return;
764-
}
796+
struct override_fn : override_fn_aux<Function, decltype(Function)> {
797+
using override_fn_aux<Function, decltype(Function)>::override_fn_aux;
798+
};
765799

766-
info.method = &fn;
767-
info.type = Policy::template static_type<decltype(Function)>();
768-
info.next = reinterpret_cast<void**>(next);
769-
using Thunk = thunk<Function, decltype(Function)>;
770-
info.pf = (void*)Thunk::fn;
771-
info.vp_begin = Thunk::OverriderParameterTypeIds::begin;
772-
info.vp_end = Thunk::OverriderParameterTypeIds::end;
773-
fn.specs.push_back(info);
774-
}
800+
template<auto... F>
801+
struct override_fns {
802+
std::tuple<override_fn<F>...> fns;
775803
};
776804

777805
template<class Container>
778806
struct override
779-
: override_<Container, detail::has_next<Container>::value> {
807+
: override_aux<Container, detail::has_next<Container>::value> {
780808
using type = override; // make it a meta-function
781809
};
782-
783-
template<auto F>
784-
struct add_member_function
785-
: override_fn<detail::member_function_thunk<F, decltype(F)>::fn> {};
786-
787-
template<auto... F>
788-
struct add_member_functions : std::tuple<add_member_function<F>...> {};
789810
};
790811

791812
template<

tests/test_member_method.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct Payroll {
4444
}
4545

4646
public:
47-
using pay_functions = Payroll::pay_method::add_member_functions<
47+
using pay_functions = Payroll::pay_method::override_fns<
4848
&Payroll::pay_employee, &Payroll::pay_manager>;
4949
};
5050

0 commit comments

Comments
 (0)