[RFC,v1,1/2] libcamera: base: object: Add `invokeMethod` for lamdas
diff mbox series

Message ID 20250811094926.1308259-2-barnabas.pocze@ideasonboard.com
State Superseded
Headers show
Series
  • libcamera: pipeline: virtual: Move image generation to separate thread
Related show

Commit Message

Barnabás Pőcze Aug. 11, 2025, 9:49 a.m. UTC
Add an overload of `invokeMethod` that works the same way as the already
existing version but instead of invoking a particular member function,
it runs a lambda.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 include/libcamera/base/object.h | 10 ++++++++++
 src/libcamera/base/object.cpp   | 24 +++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 1 deletion(-)

Patch
diff mbox series

diff --git a/include/libcamera/base/object.h b/include/libcamera/base/object.h
index a24f84ff9..3f004499f 100644
--- a/include/libcamera/base/object.h
+++ b/include/libcamera/base/object.h
@@ -43,6 +43,16 @@  public:
 		return method->activate(std::forward<Args>(args)..., true);
 	}
 
+	template<typename Func,
+		 std::enable_if_t<std::is_invocable_v<Func&>> * = nullptr>
+	auto invokeMethod(Func func, ConnectionType type)
+		-> std::invoke_result_t<Func&>
+	{
+		using R = std::invoke_result_t<Func&>;
+		auto *method = new BoundMethodFunctor<Object, R, Func>(this, this, std::move(func), type);
+		return method->activate(true);
+	}
+
 	Thread *thread() const { return thread_; }
 	void moveToThread(Thread *thread);
 
diff --git a/src/libcamera/base/object.cpp b/src/libcamera/base/object.cpp
index 37d133cc7..98937784c 100644
--- a/src/libcamera/base/object.cpp
+++ b/src/libcamera/base/object.cpp
@@ -255,7 +255,7 @@  bool Object::assertThreadBound(const char *message)
 }
 
 /**
- * \fn R Object::invokeMethod()
+ * \fn Object::invokeMethod(R (T::*func)(FuncArgs...), ConnectionType type, Args &&...args)
  * \brief Invoke a method asynchronously on an Object instance
  * \param[in] func The object method to invoke
  * \param[in] type Connection type for method invocation
@@ -280,6 +280,28 @@  bool Object::assertThreadBound(const char *message)
  * connection type ConnectionTypeQueued, return a default-constructed R value.
  */
 
+/**
+ * \fn Object::invokeMethod(Func func, ConnectionType type)
+ * \brief Invoke a lambda in the thread of an Object instance
+ * \param[in] func The object method to invoke
+ * \param[in] type Connection type for method invocation
+ *
+ * This function invokes the lambda \a func without arguments, based on the
+ * connection \a type wrt. the Object instance. Depending on the type, the method
+ * will be called synchronously in the same thread or asynchronously in the
+ * object's thread.
+ *
+ * Due to the asynchronous nature of threads, asynchronous invocations with the
+ * ConnectionTypeQueued type are not guaranteed to be called before the thread
+ * is stopped. See \ref thread-stop for additional information.
+ *
+ * \context This function is \threadsafe.
+ *
+ * \return For connection types ConnectionTypeDirect and
+ * ConnectionTypeBlocking, return the return value of the invoked method. For
+ * connection type ConnectionTypeQueued, return a default-constructed R value.
+ */
+
 /**
  * \fn Object::thread()
  * \brief Retrieve the thread the object is bound to