Skip to content

Commit

Permalink
Merge pull request #411 from micro-manager/action-lambda
Browse files Browse the repository at this point in the history
Add ActionLambda to MMDevice
  • Loading branch information
marktsuchida authored Dec 8, 2023
2 parents e0154e7 + 8459a7c commit 61b9dd6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
51 changes: 21 additions & 30 deletions DeviceAdapters/BH_DCC_DCU/DCCDCUDevices.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@
#include <string>
#include <utility>

// This can be moved to DeviceBase.h to make available generally.
class ActionLambda final : public MM::ActionFunctor {
std::function<int(MM::PropertyBase*, MM::ActionType)> f_;

public:
template <typename F> explicit ActionLambda(F f) : f_(f) {}

auto Execute(MM::PropertyBase* pProp, MM::ActionType eAct) -> int {
return f_(pProp, eAct);
}
};

template <DCCOrDCU Model> inline auto ModelName() -> std::string {
if (Model == DCCOrDCU::DCC) {
return "DCC";
Expand Down Expand Up @@ -426,8 +414,9 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
const std::string& name) {
this->CreateStringProperty(
name.c_str(), "Off", false,
new ActionLambda([this, connNo, feature, name](
MM::PropertyBase* pProp, MM::ActionType eAct) {
new MM::ActionLambda([this, connNo, feature,
name](MM::PropertyBase* pProp,
MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
short err{};
const bool flag =
Expand Down Expand Up @@ -467,8 +456,9 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
float maxValue) {
this->CreateFloatProperty(
name.c_str(), minValue, false,
new ActionLambda([this, connNo, feature, name](
MM::PropertyBase* pProp, MM::ActionType eAct) {
new MM::ActionLambda([this, connNo, feature,
name](MM::PropertyBase* pProp,
MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
short err{};
const float value =
Expand Down Expand Up @@ -506,8 +496,9 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
const std::string& name) {
this->CreateIntegerProperty(
name.c_str(), 0, false,
new ActionLambda([this, connNo, feature, name](
MM::PropertyBase* pProp, MM::ActionType eAct) {
new MM::ActionLambda([this, connNo, feature,
name](MM::PropertyBase* pProp,
MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
short err{};
const unsigned value =
Expand Down Expand Up @@ -547,8 +538,8 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
const std::string& name) {
this->CreateStringProperty(
name.c_str(), "Off", false,
new ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
new MM::ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
// There is no readout for enable_outputs, so we rely on the
// last-set value.
if (eAct == MM::AfterSet) {
Expand Down Expand Up @@ -577,8 +568,8 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
void CreateEnableAllOutputsProperty() {
this->CreateStringProperty(
"EnableOutputs", "Off", false,
new ActionLambda([this](MM::PropertyBase* pProp,
MM::ActionType eAct) {
new MM::ActionLambda([this](MM::PropertyBase* pProp,
MM::ActionType eAct) {
// There is no readout for enable_outputs, so we rely on the
// last-set value.
if (eAct == MM::AfterSet) {
Expand Down Expand Up @@ -609,8 +600,8 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
const std::string& name) {
this->CreateStringProperty(
name.c_str(), "", false,
new ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
new MM::ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
if (eAct == MM::AfterSet) {
std::string propVal;
pProp->Get(propVal);
Expand All @@ -634,8 +625,8 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
void CreateClearAllOverloadsProperty() {
this->CreateStringProperty(
"ClearOverloads", "", false,
new ActionLambda([this](MM::PropertyBase* pProp,
MM::ActionType eAct) {
new MM::ActionLambda([this](MM::PropertyBase* pProp,
MM::ActionType eAct) {
if (eAct == MM::AfterSet) {
std::string propVal;
pProp->Get(propVal);
Expand All @@ -660,8 +651,8 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
const std::string& name) {
this->CreateStringProperty(
name.c_str(), "No", true,
new ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
new MM::ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
short err{};
const bool flag = module_->IsOverloaded(connNo, err);
Expand Down Expand Up @@ -691,8 +682,8 @@ class DCCDCUModuleDevice : public CGenericBase<DCCDCUModuleDevice<Model>> {
const std::string& name) {
this->CreateStringProperty(
name.c_str(), "No", true,
new ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
new MM::ActionLambda([this, connNo](MM::PropertyBase* pProp,
MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
short err{};
const bool flag =
Expand Down
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 61b9dd6

Please sign in to comment.