From 9c86a405b72a9eddec57a63f4a4a5f3b70f3949a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Wed, 5 Feb 2025 17:48:22 +0100 Subject: [PATCH] libcamera: base: bound_method: Forward arguments when possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use `std::{forward,move}` to forward the arguments, this enables the use of move constructors, likely leading to less code and better runtime. For example, move constructing a libstdc++ `std::shared_ptr` is noticeably less code than copy constructing one. Signed-off-by: Barnabás Pőcze Reviewed-by: Laurent Pinchart Acked-by: Kieran Bingham --- include/libcamera/base/bound_method.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/include/libcamera/base/bound_method.h b/include/libcamera/base/bound_method.h index e14614bc..91fe8b8c 100644 --- a/include/libcamera/base/bound_method.h +++ b/include/libcamera/base/bound_method.h @@ -33,8 +33,9 @@ template class BoundMethodPack : public BoundMethodPackBase { public: - BoundMethodPack(const Args &... args) - : args_(args...) + template + BoundMethodPack(Ts &&...args) + : args_(std::forward(args)...) { } @@ -46,8 +47,9 @@ template class BoundMethodPack : public BoundMethodPackBase { public: - BoundMethodPack(const Args &... args) - : args_(args...) + template + BoundMethodPack(Ts &&...args) + : args_(std::forward(args)...) { } @@ -121,16 +123,16 @@ public: BoundMethodFunctor(T *obj, Object *object, Func func, ConnectionType type = ConnectionTypeAuto) - : BoundMethodArgs(obj, object, type), func_(func) + : BoundMethodArgs(obj, object, type), func_(std::move(func)) { } R activate(Args... args, bool deleteMethod = false) override { if (!this->object_) - return func_(args...); + return func_(std::forward(args)...); - auto pack = std::make_shared(args...); + auto pack = std::make_shared(std::forward(args)...); [[maybe_unused]] bool sync = BoundMethodBase::activatePack(pack, deleteMethod); if constexpr (!std::is_void_v) @@ -139,7 +141,7 @@ public: R invoke(Args... args) override { - return func_(args...); + return func_(std::forward(args)...); } private: @@ -164,10 +166,10 @@ public: { if (!this->object_) { T *obj = static_cast(this->obj_); - return (obj->*func_)(args...); + return (obj->*func_)(std::forward(args)...); } - auto pack = std::make_shared(args...); + auto pack = std::make_shared(std::forward(args)...); [[maybe_unused]] bool sync = BoundMethodBase::activatePack(pack, deleteMethod); if constexpr (!std::is_void_v) @@ -177,7 +179,7 @@ public: R invoke(Args... args) override { T *obj = static_cast(this->obj_); - return (obj->*func_)(args...); + return (obj->*func_)(std::forward(args)...); } private: @@ -198,7 +200,7 @@ public: R activate(Args... args, [[maybe_unused]] bool deleteMethod = false) override { - return (*func_)(args...); + return (*func_)(std::forward(args)...); } R invoke(Args...) override