Skip to content

Commit e26798b

Browse files
committed
uniform method - core
1 parent a64bd54 commit e26798b

File tree

9 files changed

+98
-56
lines changed

9 files changed

+98
-56
lines changed

examples/core_api.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ using namespace boost::openmethod;
2727
class BOOST_OPENMETHOD_NAME(poke);
2828

2929
using poke = method<
30-
BOOST_OPENMETHOD_NAME(poke)(std::ostream&, virtual_ptr<Animal>), void>;
30+
BOOST_OPENMETHOD_NAME(poke),
31+
auto(std::ostream&, virtual_ptr<Animal>)->void>;
3132
// end::method[]
3233

3334
// tag::poke_cat[]
@@ -68,7 +69,7 @@ auto pet_dog(std::ostream& os, virtual_ptr<Dog> dog) {
6869
}
6970

7071
using pet = method<
71-
BOOST_OPENMETHOD_NAME(pet)(std::ostream&, virtual_ptr<Animal>), void>;
72+
BOOST_OPENMETHOD_NAME(pet), auto(std::ostream&, virtual_ptr<Animal>)->void>;
7273

7374
BOOST_OPENMETHOD_REGISTER(pet::override<pet_cat, pet_dog>);
7475

examples/slides.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ using namespace nodes;
260260
use_classes<Node, Number, Plus, Times> use_node_classes;
261261

262262
struct value_id;
263-
using value = method<value_id(virtual_ptr<const Node>), int>;
263+
using value = method<value_id, auto (virtual_ptr<const Node>)->int>;
264264

265265
auto number_value(virtual_ptr<const Number> node) -> int {
266266
return node->val;

include/boost/openmethod/core.hpp

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ namespace detail {
4545
template<class Registry, class Class>
4646
constexpr bool is_polymorphic = Registry::rtti::template is_polymorphic<Class>;
4747

48+
using macro_default_registry = BOOST_OPENMETHOD_DEFAULT_REGISTRY;
49+
4850
template<typename...>
4951
struct extract_registry;
5052

@@ -962,10 +964,11 @@ class method;
962964

963965
template<
964966
typename Name, typename... Parameters, typename ReturnType, class Registry>
965-
class method<Name(Parameters...), ReturnType, Registry>
967+
class method<Name, auto(Parameters...)->ReturnType, Registry>
966968
: public detail::method_info {
967969
// Aliases used in implementation only. Everything extracted from template
968970
// arguments is capitalized like the arguments themselves.
971+
using RegistryType = Registry;
969972
using rtti = typename Registry::rtti;
970973
using DeclaredParameters = mp11::mp_list<Parameters...>;
971974
using CallParameters =
@@ -1107,21 +1110,22 @@ class method<Name(Parameters...), ReturnType, Registry>
11071110

11081111
template<
11091112
typename Name, typename... Parameters, typename ReturnType, class Registry>
1110-
method<Name(Parameters...), ReturnType, Registry>
1111-
method<Name(Parameters...), ReturnType, Registry>::fn;
1113+
method<Name, auto(Parameters...)->ReturnType, Registry>
1114+
method<Name, auto(Parameters...)->ReturnType, Registry>::fn;
11121115

11131116
template<
11141117
typename Name, typename... Parameters, typename ReturnType, class Registry>
11151118
template<auto>
1116-
typename method<Name(Parameters...), ReturnType, Registry>::FunctionPointer
1117-
method<Name(Parameters...), ReturnType, Registry>::next;
1119+
typename method<
1120+
Name, auto(Parameters...)->ReturnType, Registry>::FunctionPointer
1121+
method<Name, auto(Parameters...)->ReturnType, Registry>::next;
11181122

11191123
template<typename T>
11201124
constexpr bool is_method = std::is_base_of_v<detail::method_info, T>;
11211125

11221126
template<
11231127
typename Name, typename... Parameters, typename ReturnType, class Registry>
1124-
method<Name(Parameters...), ReturnType, Registry>::method() {
1128+
method<Name, auto(Parameters...)->ReturnType, Registry>::method() {
11251129
method_info::slots_strides_ptr = slots_strides;
11261130

11271131
using virtual_type_ids = detail::type_id_list<
@@ -1140,20 +1144,21 @@ method<Name(Parameters...), ReturnType, Registry>::method() {
11401144

11411145
template<
11421146
typename Name, typename... Parameters, typename ReturnType, class Registry>
1143-
std::size_t method<
1144-
Name(Parameters...), ReturnType, Registry>::slots_strides[2 * Arity - 1];
1147+
std::size_t method<Name, auto(Parameters...)->ReturnType, Registry>::
1148+
slots_strides[2 * Arity - 1];
11451149

11461150
template<
11471151
typename Name, typename... Parameters, typename ReturnType, class Registry>
1148-
method<Name(Parameters...), ReturnType, Registry>::~method() {
1152+
method<Name, auto(Parameters...)->ReturnType, Registry>::~method() {
11491153
Registry::methods.remove(*this);
11501154
}
11511155

11521156
template<
11531157
typename Name, typename... Parameters, typename ReturnType, class Registry>
11541158
template<class Error>
1155-
auto method<Name(Parameters...), ReturnType, Registry>::check_static_offset(
1156-
std::size_t actual, std::size_t expected) const -> void {
1159+
auto method<Name, auto(Parameters...)->ReturnType, Registry>::
1160+
check_static_offset(std::size_t actual, std::size_t expected) const
1161+
-> void {
11571162
using namespace detail;
11581163
using error_handler =
11591164
typename Registry::template policy<policies::error_handler>;
@@ -1177,7 +1182,7 @@ auto method<Name(Parameters...), ReturnType, Registry>::check_static_offset(
11771182
template<
11781183
typename Name, typename... Parameters, typename ReturnType, class Registry>
11791184
BOOST_FORCEINLINE auto
1180-
method<Name(Parameters...), ReturnType, Registry>::operator()(
1185+
method<Name, auto(Parameters...)->ReturnType, Registry>::operator()(
11811186
detail::remove_virtual<Parameters>... args) const -> ReturnType {
11821187
using namespace detail;
11831188
auto pf = resolve(parameter_traits<Parameters, Registry>::peek(args)...);
@@ -1188,10 +1193,10 @@ method<Name(Parameters...), ReturnType, Registry>::operator()(
11881193
template<
11891194
typename Name, typename... Parameters, typename ReturnType, class Registry>
11901195
template<typename... ArgType>
1191-
BOOST_FORCEINLINE
1192-
typename method<Name(Parameters...), ReturnType, Registry>::FunctionPointer
1193-
method<Name(Parameters...), ReturnType, Registry>::resolve(
1194-
const ArgType&... args) const {
1196+
BOOST_FORCEINLINE typename method<
1197+
Name, auto(Parameters...)->ReturnType, Registry>::FunctionPointer
1198+
method<Name, auto(Parameters...)->ReturnType, Registry>::resolve(
1199+
const ArgType&... args) const {
11951200
using namespace detail;
11961201

11971202
std::uintptr_t pf;
@@ -1209,7 +1214,8 @@ BOOST_FORCEINLINE
12091214
template<
12101215
typename Name, typename... Parameters, typename ReturnType, class Registry>
12111216
template<typename ArgType>
1212-
BOOST_FORCEINLINE auto method<Name(Parameters...), ReturnType, Registry>::vptr(
1217+
BOOST_FORCEINLINE auto
1218+
method<Name, auto(Parameters...)->ReturnType, Registry>::vptr(
12131219
const ArgType& arg) const -> vptr_type {
12141220
if constexpr (detail::is_virtual_ptr<ArgType>) {
12151221
return arg.vptr();
@@ -1222,7 +1228,7 @@ template<
12221228
typename Name, typename... Parameters, typename ReturnType, class Registry>
12231229
template<typename MethodArgList, typename ArgType, typename... MoreArgTypes>
12241230
BOOST_FORCEINLINE auto
1225-
method<Name(Parameters...), ReturnType, Registry>::resolve_uni(
1231+
method<Name, auto(Parameters...)->ReturnType, Registry>::resolve_uni(
12261232
const ArgType& arg, const MoreArgTypes&... more_args) const
12271233
-> std::uintptr_t {
12281234

@@ -1251,7 +1257,7 @@ template<
12511257
typename Name, typename... Parameters, typename ReturnType, class Registry>
12521258
template<typename MethodArgList, typename ArgType, typename... MoreArgTypes>
12531259
BOOST_FORCEINLINE auto
1254-
method<Name(Parameters...), ReturnType, Registry>::resolve_multi_first(
1260+
method<Name, auto(Parameters...)->ReturnType, Registry>::resolve_multi_first(
12551261
const ArgType& arg, const MoreArgTypes&... more_args) const
12561262
-> std::uintptr_t {
12571263

@@ -1292,7 +1298,7 @@ template<
12921298
std::size_t VirtualArg, typename MethodArgList, typename ArgType,
12931299
typename... MoreArgTypes>
12941300
BOOST_FORCEINLINE auto
1295-
method<Name(Parameters...), ReturnType, Registry>::resolve_multi_next(
1301+
method<Name, auto(Parameters...)->ReturnType, Registry>::resolve_multi_next(
12961302
vptr_type dispatch, const ArgType& arg,
12971303
const MoreArgTypes&... more_args) const -> std::uintptr_t {
12981304

@@ -1348,9 +1354,9 @@ auto error_type_id(const Class& obj) {
13481354

13491355
template<
13501356
typename Name, typename... Parameters, typename ReturnType, class Registry>
1351-
BOOST_NORETURN auto
1352-
method<Name(Parameters...), ReturnType, Registry>::not_implemented_handler(
1353-
detail::remove_virtual<Parameters>... args) -> ReturnType {
1357+
BOOST_NORETURN auto method<Name, auto(Parameters...)->ReturnType, Registry>::
1358+
not_implemented_handler(detail::remove_virtual<Parameters>... args)
1359+
-> ReturnType {
13541360
if constexpr (Registry::template has_policy<policies::error_handler>) {
13551361
not_implemented_error error;
13561362
error.method = Registry::rtti::template static_type<method>();
@@ -1383,7 +1389,7 @@ template<
13831389
typename Name, typename... Parameters, typename ReturnType, class Registry>
13841390
template<
13851391
auto Overrider, typename OverriderReturn, typename... OverriderParameters>
1386-
auto method<Name(Parameters...), ReturnType, Registry>::
1392+
auto method<Name, auto(Parameters...)->ReturnType, Registry>::
13871393
thunk<Overrider, OverriderReturn (*)(OverriderParameters...)>::fn(
13881394
detail::remove_virtual<Parameters>... arg) -> ReturnType {
13891395
using namespace detail;
@@ -1403,7 +1409,7 @@ auto method<Name(Parameters...), ReturnType, Registry>::
14031409
template<
14041410
typename Name, typename... Parameters, typename ReturnType, class Registry>
14051411
template<auto Function, typename FnReturnType>
1406-
method<Name(Parameters...), ReturnType, Registry>::override_impl<
1412+
method<Name, auto(Parameters...)->ReturnType, Registry>::override_impl<
14071413
Function, FnReturnType>::override_impl(FunctionPointer* p_next) {
14081414
using namespace detail;
14091415

include/boost/openmethod/macros.hpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ struct enable_forwarder<
2222
using type = ReturnType;
2323
};
2424

25+
template<class...>
26+
struct va_args;
27+
28+
template<class ReturnType, class Registry>
29+
struct va_args<ReturnType, Registry> {
30+
using return_type = ReturnType;
31+
using registry = Registry;
32+
};
33+
34+
template<class ReturnType>
35+
struct va_args<ReturnType> {
36+
using return_type = ReturnType;
37+
using registry = macro_default_registry;
38+
};
39+
2540
} // namespace boost::openmethod::detail
2641

2742
#define BOOST_OPENMETHOD_GENSYM BOOST_PP_CAT(openmethod_gensym_, __COUNTER__)
@@ -43,23 +58,39 @@ struct enable_forwarder<
4358
typename ::boost::openmethod::detail::enable_forwarder< \
4459
void, \
4560
::boost::openmethod::method< \
46-
BOOST_OPENMETHOD_NAME(NAME) ARGS, __VA_ARGS__>, \
61+
BOOST_OPENMETHOD_NAME(NAME), \
62+
::boost::openmethod::detail::va_args<__VA_ARGS__>::return_type \
63+
ARGS, \
64+
::boost::openmethod::detail::va_args<__VA_ARGS__>::registry>, \
4765
typename ::boost::openmethod::method< \
48-
BOOST_OPENMETHOD_NAME(NAME) ARGS, __VA_ARGS__>, \
66+
BOOST_OPENMETHOD_NAME(NAME), \
67+
::boost::openmethod::detail::va_args<__VA_ARGS__>::return_type \
68+
ARGS, \
69+
::boost::openmethod::detail::va_args<__VA_ARGS__>::registry>, \
4970
ForwarderParameters...>::type \
5071
BOOST_OPENMETHOD_GUIDE(NAME)(ForwarderParameters && ... args); \
5172
template<typename... ForwarderParameters> \
5273
inline auto NAME(ForwarderParameters&&... args) -> \
5374
typename ::boost::openmethod::detail::enable_forwarder< \
5475
void, \
5576
::boost::openmethod::method< \
56-
BOOST_OPENMETHOD_NAME(NAME) ARGS, __VA_ARGS__>, \
77+
BOOST_OPENMETHOD_NAME(NAME), \
78+
::boost::openmethod::detail::va_args<__VA_ARGS__>::return_type \
79+
ARGS, \
80+
::boost::openmethod::detail::va_args<__VA_ARGS__>::registry>, \
5781
typename ::boost::openmethod::method< \
58-
BOOST_OPENMETHOD_NAME(NAME) ARGS, __VA_ARGS__>::return_type, \
82+
BOOST_OPENMETHOD_NAME(NAME), \
83+
::boost::openmethod::detail::va_args<__VA_ARGS__>::return_type \
84+
ARGS, \
85+
::boost::openmethod::detail::va_args<__VA_ARGS__>::registry>:: \
86+
return_type, \
5987
ForwarderParameters...>::type { \
60-
return ::boost::openmethod:: \
61-
method<BOOST_OPENMETHOD_NAME(NAME) ARGS, __VA_ARGS__>::fn( \
62-
std::forward<ForwarderParameters>(args)...); \
88+
return ::boost::openmethod::method< \
89+
BOOST_OPENMETHOD_NAME(NAME), \
90+
::boost::openmethod::detail::va_args<__VA_ARGS__>::return_type \
91+
ARGS, \
92+
::boost::openmethod::detail::va_args<__VA_ARGS__>::registry>:: \
93+
fn(std::forward<ForwarderParameters>(args)...); \
6394
}
6495

6596
#define BOOST_OPENMETHOD_DETAIL_LOCATE_METHOD(NAME, ARGS) \

test/test_blackbox.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ BOOST_OPENMETHOD_CLASSES(Animal, Dog, Bulldog);
479479

480480
struct BOOST_OPENMETHOD_NAME(poke);
481481
using poke =
482-
method<BOOST_OPENMETHOD_NAME(poke)(virtual_<Animal&>), std::string>;
482+
method<BOOST_OPENMETHOD_NAME(poke), auto(virtual_<Animal&>)->std::string>;
483483

484484
auto poke_dog(Dog&) -> std::string {
485485
return "bark";
@@ -637,10 +637,10 @@ void fn(Class&...) {
637637
BOOST_OPENMETHOD_CLASSES(Animal, Dog, Cat, test_registry);
638638

639639
BOOST_AUTO_TEST_CASE(initialize_report) {
640-
using poke = method<poke_(virtual_<Animal&>), void, test_registry>;
641-
using pet = method<pet_(virtual_<Animal&>), void, test_registry>;
640+
using poke = method<poke_, auto(virtual_<Animal&>)->void, test_registry>;
641+
using pet = method<pet_, auto(virtual_<Animal&>)->void, test_registry>;
642642
using meet = method<
643-
meet_(virtual_<Animal&>, virtual_<Animal&>), void, test_registry>;
643+
meet_, auto(virtual_<Animal&>, virtual_<Animal&>)->void, test_registry>;
644644

645645
auto report = initialize<test_registry>().report;
646646
BOOST_TEST(report.not_implemented == 3u);

test/test_compiler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ struct M;
216216

217217
#define ADD_METHOD(CLASS) \
218218
auto& BOOST_PP_CAT(m_, CLASS) = \
219-
method<CLASS(virtual_<CLASS&>), void, test_registry>::fn;
219+
method<CLASS, auto(virtual_<CLASS&>)->void, test_registry>::fn;
220220

221221
#define ADD_METHOD_N(CLASS, N) \
222222
auto& BOOST_PP_CAT(BOOST_PP_CAT(m_, CLASS), N) = \
223-
method<M<N>(virtual_<CLASS&>), void, test_registry>::fn;
223+
method<M<N>, auto(virtual_<CLASS&>)->void, test_registry>::fn;
224224

225225
BOOST_AUTO_TEST_CASE(test_assign_slots_a_b1_c) {
226226
using test_registry = test_registry_<__COUNTER__>;

test/test_member_method.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ struct Payroll {
3434
private:
3535
struct BOOST_OPENMETHOD_NAME(pay);
3636
using pay_method = method<
37-
BOOST_OPENMETHOD_NAME(pay)(Payroll*, virtual_<const Role&>), void>;
37+
BOOST_OPENMETHOD_NAME(pay),
38+
auto(Payroll*, virtual_<const Role&>)->void>;
3839

3940
void pay_employee(const Employee&) {
4041
balance -= 2000;

test/test_virtual_ptr_dispatch.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,9 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(
214214

215215
BOOST_OPENMETHOD_REGISTER(
216216
use_classes<Player, Warrior, Object, Axe, Bear, Registry>);
217-
;
218217
using poke = method<
219-
BOOST_OPENMETHOD_NAME(poke)(virtual_ptr<Player, Registry>), std::string,
220-
Registry>;
218+
BOOST_OPENMETHOD_NAME(poke),
219+
auto(virtual_ptr<Player, Registry>)->std::string, Registry>;
221220
BOOST_OPENMETHOD_REGISTER(
222221
typename poke::template override<
223222
poke_bear<virtual_ptr<Player, Registry>>>);
@@ -275,17 +274,19 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(
275274
use_classes<Player, Warrior, Object, Axe, Bear, Registry>);
276275

277276
using poke = method<
278-
BOOST_OPENMETHOD_NAME(poke)(virtual_ptr<Player, Registry>), std::string,
279-
Registry>;
277+
BOOST_OPENMETHOD_NAME(poke),
278+
auto(virtual_ptr<Player, Registry>)->std::string, Registry>;
280279
BOOST_OPENMETHOD_REGISTER(
281280
typename poke::template override<
282281
poke_bear<virtual_ptr<Player, Registry>>>);
283282

284283
using fight = method<
285-
BOOST_OPENMETHOD_NAME(fight)(
284+
BOOST_OPENMETHOD_NAME(fight),
285+
auto(
286286
virtual_ptr<Player, Registry>, virtual_ptr<Object, Registry>,
287-
virtual_ptr<Player, Registry>),
288-
std::string, Registry>;
287+
virtual_ptr<Player, Registry>)
288+
->std::string,
289+
Registry>;
289290
BOOST_OPENMETHOD_REGISTER(
290291
typename fight::template override<fight_bear<
291292
virtual_ptr<Player, Registry>, virtual_ptr<Object, Registry>,
@@ -316,19 +317,21 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(
316317
use_classes<Player, Warrior, Object, Axe, Bear, Registry>);
317318

318319
using poke = method<
319-
BOOST_OPENMETHOD_NAME(poke)(shared_virtual_ptr<Player, Registry>),
320-
std::string, Registry>;
320+
BOOST_OPENMETHOD_NAME(poke),
321+
auto(shared_virtual_ptr<Player, Registry>)->std::string, Registry>;
321322

322323
BOOST_OPENMETHOD_REGISTER(
323324
typename poke::template override<
324325
poke_bear<shared_virtual_ptr<Player, Registry>>>);
325326

326327
using fight = method<
327-
BOOST_OPENMETHOD_NAME(fight)(
328+
BOOST_OPENMETHOD_NAME(fight),
329+
auto(
328330
shared_virtual_ptr<Player, Registry>,
329331
shared_virtual_ptr<Object, Registry>,
330-
shared_virtual_ptr<Player, Registry>),
331-
std::string, Registry>;
332+
shared_virtual_ptr<Player, Registry>)
333+
->std::string,
334+
Registry>;
332335

333336
BOOST_OPENMETHOD_REGISTER(
334337
typename fight::template override<fight_bear<

test/test_virtual_ptr_value_semantics.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ template<class Registry>
4040
void init_test() {
4141
BOOST_OPENMETHOD_REGISTER(use_classes<Animal, Cat, Dog, Registry>);
4242
struct id;
43-
(void)&method<id(virtual_ptr<Animal, Registry>), void, Registry>::fn;
43+
(void)&method<id, auto(virtual_ptr<Animal, Registry>)->void, Registry>::fn;
4444
boost::openmethod::initialize<Registry>();
4545
}
4646

0 commit comments

Comments
 (0)