[libcamera-devel,RFC,2/4] Add BoundMethodFunction

Message ID 20200918152019.784315-3-tomi.valkeinen@iki.fi
State Superseded
Headers show
Series
  • prototype python bindings
Related show

Commit Message

Tomi Valkeinen Sept. 18, 2020, 3:20 p.m. UTC
Note: no way to disconnect, except disconnect all.

Signed-off-by: Tomi Valkeinen <tomi.valkeinen@iki.fi>
---
 include/libcamera/bound_method.h | 25 +++++++++++++++++++++++++
 include/libcamera/signal.h       |  6 ++++++
 2 files changed, 31 insertions(+)

Patch

diff --git a/include/libcamera/bound_method.h b/include/libcamera/bound_method.h
index d1e4448..d720199 100644
--- a/include/libcamera/bound_method.h
+++ b/include/libcamera/bound_method.h
@@ -11,6 +11,7 @@ 
 #include <tuple>
 #include <type_traits>
 #include <utility>
+#include <functional>
 
 namespace libcamera {
 
@@ -228,6 +229,30 @@  private:
 	R (*func_)(Args...);
 };
 
+template<typename R, typename... Args>
+class BoundMethodFunction : public BoundMethodArgs<R, Args...>
+{
+public:
+	BoundMethodFunction(std::function<R(Args...)> func)
+	        : BoundMethodArgs<R, Args...>(nullptr, nullptr, ConnectionTypeAuto),
+	          func_(func)
+	{
+	}
+
+	R activate(Args... args, [[maybe_unused]] bool deleteMethod = false) override
+	{
+		return func_(args...);
+	}
+
+	R invoke(Args...) override
+	{
+		return R();
+	}
+
+private:
+	std::function<R(Args...)> func_;
+};
+
 } /* namespace libcamera */
 
 #endif /* __LIBCAMERA_BOUND_METHOD_H__ */
diff --git a/include/libcamera/signal.h b/include/libcamera/signal.h
index accb797..939169f 100644
--- a/include/libcamera/signal.h
+++ b/include/libcamera/signal.h
@@ -68,6 +68,12 @@  public:
 		SignalBase::connect(new BoundMethodStatic<R, Args...>(func));
 	}
 
+	template<typename R>
+	void connect(std::function<R(Args...)> func)
+	{
+		SignalBase::connect(new BoundMethodFunction<R, Args...>(func));
+	}
+
 	void disconnect()
 	{
 		SignalBase::disconnect([]([[maybe_unused]] SlotList::iterator &iter) {