Skip to content

Commit

Permalink
MMDevice: Add ActionLambda
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marktsuchida committed Dec 6, 2023
1 parent 482281e commit 5602d99
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions MMDevice/Property.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
#define _MMPROPERTY_H_

#include "MMDeviceConstants.h"
#include <string>
#include <cstring>

#include <cstdlib>
#include <vector>
#include <cstring>
#include <functional>
#include <map>
#include <string>
#include <vector>

namespace MM {

Expand Down Expand Up @@ -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<int(PropertyBase*, ActionType)> func_;

public:
template <typename F> 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.
*/
Expand Down

0 comments on commit 5602d99

Please sign in to comment.