libcamera: bound_method: Decouple from Signal implementation

To make the BoundMethod classes more generic, replace direct access to
private member from Signal classes with accessors or helper functions.
This allows removal of friend statements from the BoundMethod classes.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
This commit is contained in:
Laurent Pinchart 2019-08-12 02:59:19 +03:00
parent 0e65ed8145
commit f83820a5d1
3 changed files with 12 additions and 17 deletions

View file

@ -13,9 +13,6 @@
namespace libcamera { namespace libcamera {
class Object; class Object;
template<typename... Args>
class Signal;
class SignalBase;
class BoundMethodBase class BoundMethodBase
{ {
@ -28,7 +25,7 @@ public:
bool match(T *obj) { return obj == obj_; } bool match(T *obj) { return obj == obj_; }
bool match(Object *object) { return object == object_; } bool match(Object *object) { return object == object_; }
void disconnect(SignalBase *signal); Object *object() const { return object_; }
void activatePack(void *pack); void activatePack(void *pack);
virtual void invokePack(void *pack) = 0; virtual void invokePack(void *pack) = 0;
@ -93,6 +90,8 @@ public:
BoundMemberMethod(T *obj, Object *object, void (T::*func)(Args...)) BoundMemberMethod(T *obj, Object *object, void (T::*func)(Args...))
: BoundMethodArgs<Args...>(obj, object), func_(func) {} : BoundMethodArgs<Args...>(obj, object), func_(func) {}
bool match(void (T::*func)(Args...)) const { return func == func_; }
void activate(Args... args) void activate(Args... args)
{ {
if (this->object_) if (this->object_)
@ -107,7 +106,6 @@ public:
} }
private: private:
friend class Signal<Args...>;
void (T::*func_)(Args...); void (T::*func_)(Args...);
}; };
@ -118,11 +116,12 @@ public:
BoundStaticMethod(void (*func)(Args...)) BoundStaticMethod(void (*func)(Args...))
: BoundMethodArgs<Args...>(nullptr, nullptr), func_(func) {} : BoundMethodArgs<Args...>(nullptr, nullptr), func_(func) {}
bool match(void (*func)(Args...)) const { return func == func_; }
void activate(Args... args) { (*func_)(args...); } void activate(Args... args) { (*func_)(args...); }
void invoke(Args... args) {} void invoke(Args... args) {}
private: private:
friend class Signal<Args...>;
void (*func_)(Args...); void (*func_)(Args...);
}; };

View file

@ -46,7 +46,9 @@ public:
~Signal() ~Signal()
{ {
for (BoundMethodBase *slot : slots_) { for (BoundMethodBase *slot : slots_) {
slot->disconnect(this); Object *object = slot->object();
if (object)
object->disconnect(this);
delete slot; delete slot;
} }
} }
@ -95,11 +97,11 @@ public:
/* /*
* If the object matches the slot, the slot is * If the object matches the slot, the slot is
* guaranteed to be a member slot, so we can safely * guaranteed to be a member slot, so we can safely
* cast it to BoundMemberMethod<T, Args...> and access its * cast it to BoundMemberMethod<T, Args...> to match
* func_ member. * func.
*/ */
if (slot->match(obj) && if (slot->match(obj) &&
static_cast<BoundMemberMethod<T, Args...> *>(slot)->func_ == func) { static_cast<BoundMemberMethod<T, Args...> *>(slot)->match(func)) {
iter = slots_.erase(iter); iter = slots_.erase(iter);
delete slot; delete slot;
} else { } else {
@ -113,7 +115,7 @@ public:
for (auto iter = slots_.begin(); iter != slots_.end(); ) { for (auto iter = slots_.begin(); iter != slots_.end(); ) {
BoundMethodArgs<Args...> *slot = *iter; BoundMethodArgs<Args...> *slot = *iter;
if (slot->match(nullptr) && if (slot->match(nullptr) &&
static_cast<BoundStaticMethod<Args...> *>(slot)->func_ == func) { static_cast<BoundStaticMethod<Args...> *>(slot)->match(func)) {
iter = slots_.erase(iter); iter = slots_.erase(iter);
delete slot; delete slot;
} else { } else {

View file

@ -13,12 +13,6 @@
namespace libcamera { namespace libcamera {
void BoundMethodBase::disconnect(SignalBase *signal)
{
if (object_)
object_->disconnect(signal);
}
void BoundMethodBase::activatePack(void *pack) void BoundMethodBase::activatePack(void *pack)
{ {
if (Thread::current() == object_->thread()) { if (Thread::current() == object_->thread()) {