From f894f04c9d03878116de61c11d4591da67c10378 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 30 Nov 2023 10:13:09 +0100 Subject: [PATCH] QObject: remove the operator comma hack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It was the easiest way to implement this back in the day without an explosion of combinatorics. Change-Id: Ia930b1a2ed1e465a826ffffd179c5bb4657d0562 Reviewed-by: MÃ¥rten Nordheim --- src/corelib/kernel/qobjectdefs_impl.h | 76 +++++++++++++++++---------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/src/corelib/kernel/qobjectdefs_impl.h b/src/corelib/kernel/qobjectdefs_impl.h index ec9f12f5f16..1e953f29b6c 100644 --- a/src/corelib/kernel/qobjectdefs_impl.h +++ b/src/corelib/kernel/qobjectdefs_impl.h @@ -51,25 +51,27 @@ namespace QtPrivate { template struct List_Left { typedef List<> Value; }; /* - Trick to set the return value of a slot that works even if the signal or the slot returns void - to be used like - function(), ApplyReturnValue(&return_value) - if function() returns a value, the operator,(T, ApplyReturnValue) is called, but if it - returns void, the built-in one is used without an error. - */ - template - struct ApplyReturnValue { - void *data; - explicit ApplyReturnValue(void *data_) : data(data_) {} + This is used to store the return value from a slot, whether the caller + wants to store this value (QMetaObject::invokeMethod() with + qReturnArg() or non-void signal ) or not. + */ + struct FunctorCallBase + { + template + static void call_internal(void **args, Lambda &&fn) noexcept(noexcept(fn())) + { + using SlotRet = decltype(fn()); + if constexpr (std::is_void_v || std::is_void_v) { + Q_UNUSED(args); + } else { + if (args[0]) { + *reinterpret_cast(args[0]) = fn(); + return; + } + } + fn(); + } }; - template - void operator,(T &&value, const ApplyReturnValue &container) { - if (container.data) - *reinterpret_cast(container.data) = std::forward(value); - } - template - void operator,(T, const ApplyReturnValue &) {} - /* The FunctionPointer struct is a type trait for function pointer. @@ -132,41 +134,57 @@ namespace QtPrivate { template struct FunctorCall; template - struct FunctorCall, List, R, Function> { - static void call(Function &f, void **arg) { - f((*reinterpret_cast::Type *>(arg[II+1]))...), ApplyReturnValue(arg[0]); + struct FunctorCall, List, R, Function> : FunctorCallBase + { + static void call(Function &f, void **arg) + { + call_internal(arg, [&] { + return f((*reinterpret_cast::Type *>(arg[II+1]))...); + }); } }; template - struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...)> { + struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...)> : FunctorCallBase + { static void call(SlotRet (Obj::*f)(SlotArgs...), Obj *o, void **arg) { assertObjectType(o); - (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...), ApplyReturnValue(arg[0]); + call_internal(arg, [&] { + return (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...); + }); } }; template - struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...) const> { + struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...) const> : FunctorCallBase + { static void call(SlotRet (Obj::*f)(SlotArgs...) const, Obj *o, void **arg) { assertObjectType(o); - (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...), ApplyReturnValue(arg[0]); + call_internal(arg, [&] { + return (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...); + }); } }; template - struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...) noexcept> { + struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...) noexcept> : FunctorCallBase + { static void call(SlotRet (Obj::*f)(SlotArgs...) noexcept, Obj *o, void **arg) { assertObjectType(o); - (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...), ApplyReturnValue(arg[0]); + call_internal(arg, [&]() noexcept { + return (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...); + }); } }; template - struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...) const noexcept> { + struct FunctorCall, List, R, SlotRet (Obj::*)(SlotArgs...) const noexcept> : FunctorCallBase + { static void call(SlotRet (Obj::*f)(SlotArgs...) const noexcept, Obj *o, void **arg) { assertObjectType(o); - (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...), ApplyReturnValue(arg[0]); + call_internal(arg, [&]() noexcept { + return (o->*f)((*reinterpret_cast::Type *>(arg[II+1]))...); + }); } };