libcamera/include/libcamera/object.h
Umang Jain 9558886f7a libcamera: object: Add deleteLater() support
This commit adds support to schedule the deletion of an Object to the
thread it is bound to (similar to [1]). An Object getting destroyed
by a different thread is considered as a violation as per the
libcamera threading model.

This will be useful for an Object where its ownership is shared via
shared pointers in different threads. If the thread which drops the
last reference of the Object is a different thread, the destructors
get called in that particular thread, not the one Object is bound to.
Hence, in order to resolve this kind of situation, the creation of
shared pointer can be accompanied by a custom deleter which in turns
use deleteLater() to ensure the Object is destroyed in its own thread.

[1] https://doc.qt.io/qt-5/qobject.html#deleteLater

Signed-off-by: Umang Jain <email@uajain.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
2020-07-31 23:33:32 +03:00

71 lines
1.4 KiB
C++

/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* object.h - Base object
*/
#ifndef __LIBCAMERA_OBJECT_H__
#define __LIBCAMERA_OBJECT_H__
#include <list>
#include <memory>
#include <vector>
#include <libcamera/bound_method.h>
namespace libcamera {
class Message;
template<typename... Args>
class Signal;
class SignalBase;
class Thread;
class Object
{
public:
Object(Object *parent = nullptr);
virtual ~Object();
void deleteLater();
void postMessage(std::unique_ptr<Message> msg);
template<typename T, typename R, typename... FuncArgs, typename... Args,
typename std::enable_if_t<std::is_base_of<Object, T>::value> * = nullptr>
R invokeMethod(R (T::*func)(FuncArgs...), ConnectionType type,
Args... args)
{
T *obj = static_cast<T *>(this);
auto *method = new BoundMethodMember<T, R, FuncArgs...>(obj, this, func, type);
return method->activate(args..., true);
}
Thread *thread() const { return thread_; }
void moveToThread(Thread *thread);
Object *parent() const { return parent_; }
protected:
virtual void message(Message *msg);
private:
friend class SignalBase;
friend class Thread;
void notifyThreadMove();
void connect(SignalBase *signal);
void disconnect(SignalBase *signal);
Object *parent_;
std::vector<Object *> children_;
Thread *thread_;
std::list<SignalBase *> signals_;
unsigned int pendingMessages_;
};
} /* namespace libcamera */
#endif /* __LIBCAMERA_OBJECT_H__ */