From 5602d991aae26ae6770c25462c19bbfb8b29a01e Mon Sep 17 00:00:00 2001 From: "Mark A. Tsuchida" Date: Wed, 6 Dec 2023 11:22:14 -0600 Subject: [PATCH] MMDevice: Add ActionLambda This is like CPropertyAction[Ex], but takes an arbitrary copyable callable (such as a lambda). Defining a property action using a lambda allows one to put all the code related to a single property in one place. --- MMDevice/Property.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/MMDevice/Property.h b/MMDevice/Property.h index 39e865be8..cbc669822 100644 --- a/MMDevice/Property.h +++ b/MMDevice/Property.h @@ -24,11 +24,13 @@ #define _MMPROPERTY_H_ #include "MMDeviceConstants.h" -#include -#include + #include -#include +#include +#include #include +#include +#include namespace MM { @@ -120,6 +122,24 @@ class ActionEx : public ActionFunctor { return (*pObj_.*fpt_)(pProp, eAct, param_);}; }; +/** + * Action implementation using std::function to wrap arbitrary callables. + * + * (It is named "lambda" after its intended use, but can wrap any C++ + * callable.) + */ +class ActionLambda final : public ActionFunctor +{ + std::function func_; + +public: + template explicit ActionLambda(F func) : func_(func) {} + + int Execute(PropertyBase* pProp, ActionType eAct) final { + return func_(pProp, eAct); + } +}; + /** * Property API with most of the Property mechanism implemented. */