diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-02-08 00:24:14 +0200 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2019-02-13 13:23:43 +0200 |
commit | 98bd9cb8c9f370c977dc3b6cc14f3d1ebfd24563 (patch) | |
tree | 037e1a54f52db97ee12ab89d8343fcab80cf5c15 /include/libcamera/object.h | |
parent | 73a1bea709fd14cab11f35a99e9feaa98a42c3cc (diff) |
libcamera: signal: Disconnect signal automatically on slot deletion
When a signal is connected to a member function slot, the slot is not
disconnected when the slot object is deleted. This can lead to calling a
member function of a deleted object if the signal isn't disconnected
manually by the slot object's destructor.
Make signal handling easier by implementing a base Object class that
tracks all connected signals and disconnects from them automatically
when the object is deleted, using template specialization resolution in
the Signal class.
As inheriting from the Object class may to a too harsh requirement for
Signal usage in applications, keep the existing behaviour working if the
slot doesn't inherit from the Object class. We may reconsider this later
and require all slot objects to inherit from the Object class.
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'include/libcamera/object.h')
-rw-r--r-- | include/libcamera/object.h | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/include/libcamera/object.h b/include/libcamera/object.h new file mode 100644 index 00000000..eadd41f9 --- /dev/null +++ b/include/libcamera/object.h @@ -0,0 +1,35 @@ +/* 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> + +namespace libcamera { + +class SignalBase; +template<typename... Args> +class Signal; + +class Object +{ +public: + virtual ~Object(); + +private: + template<typename... Args> + friend class Signal; + + void connect(SignalBase *signal); + void disconnect(SignalBase *signal); + + std::list<SignalBase *> signals_; +}; + +}; /* namespace libcamera */ + +#endif /* __LIBCAMERA_OBJECT_H__ */ |