libcamera: base: bound_method: Forward arguments when possible
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 <barnabas.pocze@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
@@ -33,8 +33,9 @@ template<typename R, typename... Args>
|
||||
class BoundMethodPack : public BoundMethodPackBase
|
||||
{
|
||||
public:
|
||||
BoundMethodPack(const Args &... args)
|
||||
: args_(args...)
|
||||
template<typename... Ts>
|
||||
BoundMethodPack(Ts &&...args)
|
||||
: args_(std::forward<Ts>(args)...)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -46,8 +47,9 @@ template<typename... Args>
|
||||
class BoundMethodPack<void, Args...> : public BoundMethodPackBase
|
||||
{
|
||||
public:
|
||||
BoundMethodPack(const Args &... args)
|
||||
: args_(args...)
|
||||
template<typename... Ts>
|
||||
BoundMethodPack(Ts &&...args)
|
||||
: args_(std::forward<Ts>(args)...)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -121,16 +123,16 @@ public:
|
||||
|
||||
BoundMethodFunctor(T *obj, Object *object, Func func,
|
||||
ConnectionType type = ConnectionTypeAuto)
|
||||
: BoundMethodArgs<R, Args...>(obj, object, type), func_(func)
|
||||
: BoundMethodArgs<R, Args...>(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>(args)...);
|
||||
|
||||
auto pack = std::make_shared<PackType>(args...);
|
||||
auto pack = std::make_shared<PackType>(std::forward<Args>(args)...);
|
||||
[[maybe_unused]] bool sync = BoundMethodBase::activatePack(pack, deleteMethod);
|
||||
|
||||
if constexpr (!std::is_void_v<R>)
|
||||
@@ -139,7 +141,7 @@ public:
|
||||
|
||||
R invoke(Args... args) override
|
||||
{
|
||||
return func_(args...);
|
||||
return func_(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -164,10 +166,10 @@ public:
|
||||
{
|
||||
if (!this->object_) {
|
||||
T *obj = static_cast<T *>(this->obj_);
|
||||
return (obj->*func_)(args...);
|
||||
return (obj->*func_)(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
auto pack = std::make_shared<PackType>(args...);
|
||||
auto pack = std::make_shared<PackType>(std::forward<Args>(args)...);
|
||||
[[maybe_unused]] bool sync = BoundMethodBase::activatePack(pack, deleteMethod);
|
||||
|
||||
if constexpr (!std::is_void_v<R>)
|
||||
@@ -177,7 +179,7 @@ public:
|
||||
R invoke(Args... args) override
|
||||
{
|
||||
T *obj = static_cast<T *>(this->obj_);
|
||||
return (obj->*func_)(args...);
|
||||
return (obj->*func_)(std::forward<Args>(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>(args)...);
|
||||
}
|
||||
|
||||
R invoke(Args...) override
|
||||
|
||||
Reference in New Issue
Block a user