12 #include <type_traits> 26 class BoundMethodPackBase
29 virtual ~BoundMethodPackBase() =
default;
32 template<
typename R,
typename... Args>
33 class BoundMethodPack :
public BoundMethodPackBase
36 BoundMethodPack(
const Args &... args)
46 std::tuple<typename std::remove_reference_t<Args>...> args_;
50 template<
typename... Args>
51 class BoundMethodPack<void, Args...> :
public BoundMethodPackBase
54 BoundMethodPack(
const Args &... args)
63 std::tuple<typename std::remove_reference_t<Args>...> args_;
70 : obj_(obj), object_(
object), connectionType_(type)
73 virtual ~BoundMethodBase() =
default;
75 template<typename T, std::enable_if_t<!std::is_same<Object, T>::value> * =
nullptr>
76 bool match(T *obj) {
return obj == obj_; }
77 bool match(Object *
object) {
return object == object_; }
79 Object *object()
const {
return object_; }
81 virtual void invokePack(BoundMethodPackBase *pack) = 0;
84 bool activatePack(std::shared_ptr<BoundMethodPackBase> pack,
94 template<
typename R,
typename... Args>
95 class BoundMethodArgs :
public BoundMethodBase
98 using PackType = BoundMethodPack<R, Args...>;
101 template<std::size_t... I>
102 void invokePack(BoundMethodPackBase *pack, std::index_sequence<I...>)
104 [[maybe_unused]]
auto *args =
static_cast<PackType *
>(pack);
106 if constexpr (!std::is_void_v<R>)
107 args->ret_ = invoke(std::get<I>(args->args_)...);
109 invoke(std::get<I>(args->args_)...);
114 : BoundMethodBase(obj,
object, type) {}
116 void invokePack(BoundMethodPackBase *pack)
override 118 invokePack(pack, std::make_index_sequence<
sizeof...(Args)>{});
121 virtual R activate(Args... args,
bool deleteMethod =
false) = 0;
122 virtual R invoke(Args... args) = 0;
125 template<
typename T,
typename R,
typename Func,
typename... Args>
126 class BoundMethodFunctor :
public BoundMethodArgs<R, Args...>
129 using PackType =
typename BoundMethodArgs<R, Args...>::PackType;
131 BoundMethodFunctor(T *obj, Object *
object, Func func,
133 : BoundMethodArgs<R, Args...>(obj, object, type), func_(func)
137 R activate(Args... args,
bool deleteMethod =
false)
override 140 return func_(args...);
142 auto pack = std::make_shared<PackType>(args...);
143 bool sync = BoundMethodBase::activatePack(pack, deleteMethod);
144 return sync ? pack->returnValue() : R();
147 R invoke(Args... args)
override 149 return func_(args...);
156 template<
typename T,
typename R,
typename... Args>
157 class BoundMethodMember :
public BoundMethodArgs<R, Args...>
160 using PackType =
typename BoundMethodArgs<R, Args...>::PackType;
162 BoundMethodMember(T *obj, Object *
object, R (T::*func)(Args...),
164 : BoundMethodArgs<R, Args...>(obj, object, type), func_(func)
168 bool match(R (T::*func)(Args...))
const {
return func == func_; }
170 R activate(Args... args,
bool deleteMethod =
false)
override 172 if (!this->object_) {
173 T *obj =
static_cast<T *
>(this->obj_);
174 return (obj->*func_)(args...);
177 auto pack = std::make_shared<PackType>(args...);
178 bool sync = BoundMethodBase::activatePack(pack, deleteMethod);
179 return sync ? pack->returnValue() : R();
182 R invoke(Args... args)
override 184 T *obj =
static_cast<T *
>(this->obj_);
185 return (obj->*func_)(args...);
189 R (T::*func_)(Args...);
192 template<
typename R,
typename... Args>
193 class BoundMethodStatic :
public BoundMethodArgs<R, Args...>
196 BoundMethodStatic(R (*func)(Args...))
202 bool match(R (*func)(Args...))
const {
return func == func_; }
204 R activate(Args... args, [[maybe_unused]]
bool deleteMethod =
false)
override 206 return (*func_)(args...);
209 R invoke(Args...)
override ConnectionType
Connection type for asynchronous communication.
Definition: bound_method.h:19
Top-level libcamera namespace.
Definition: bound_method.h:15
The receiver is invoked synchronously.
Definition: bound_method.h:23
The receiver is invoked asynchronously.
Definition: bound_method.h:22
The receiver is invoked immediately and synchronously in the sender's thread.
Definition: bound_method.h:21
If the sender and the receiver live in the same thread, ConnectionTypeDirect is used. Otherwise ConnectionTypeQueued is used.
Definition: bound_method.h:20