MMDevice
Loading...
Searching...
No Matches
Property.h
Go to the documentation of this file.
1
2// FILE: Property.h
3// PROJECT: Micro-Manager
4// SUBSYSTEM: MMDevice - Device adapter kit
5//-----------------------------------------------------------------------------
6// DESCRIPTION: This class implements the basic property mechanism in
7// Micro-Manager devices.
8// AUTHOR: Nenad Amodaj, nenad@amodaj.com, 08/05/2005
9// COPYRIGHT: University of California, San Francisco, 2006
10// LICENSE: This file is distributed under the BSD license.
11// License text is included with the source distribution.
12//
13// This file is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty
15// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16//
17// IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES.
20//
21
22#pragma once
23
24#include "MMDeviceConstants.h"
25
26#include <functional>
27#include <map>
28#include <string>
29#include <vector>
30
31namespace MM {
32
39{
40public:
41 virtual ~PropertyBase() {}
42
43 // property type
44 virtual PropertyType GetType() = 0;
45
46 // setting and getting values
47 virtual bool Set(double dVal) = 0;
48 virtual bool Set(long lVal) = 0;
49 virtual bool Set(const char* Val) = 0;
50
51 virtual bool Get(double& dVal) const = 0;
52 virtual bool Get(long& lVal) const = 0;
53 virtual bool Get(std::string& strVal) const = 0;
54
55 // Limits
56 virtual bool HasLimits() const = 0;
57 virtual double GetLowerLimit() const = 0;
58 virtual double GetUpperLimit() const = 0;
59 virtual bool SetLimits(double lowerLimit, double upperLimit) = 0;
60
61 // Sequence
62 virtual void SetSequenceable(long sequenceSize) = 0;
63 virtual long GetSequenceMaxSize() const = 0;
64 virtual std::vector<std::string> GetSequence() const = 0;
65 virtual int ClearSequence() = 0;
66 virtual int AddToSequence(const char* value) = 0;
67 virtual int SendSequence() = 0;
68
69 virtual std::string GetName() const = 0;
70};
71
76{
77public:
78 virtual ~ActionFunctor() {}
79 virtual int Execute(PropertyBase* pProp, ActionType eAct) = 0;
80};
81
85template <class T>
86class Action : public ActionFunctor
87{
88private:
89 T* pObj_;
90 int (T::*fpt_)(PropertyBase* pProp, ActionType eAct);
91
92public:
93 Action(T* pObj, int(T::*fpt)(PropertyBase* pProp, ActionType eAct) ) :
94 pObj_(pObj), fpt_(fpt) {}
96
98 { return (*pObj_.*fpt_)(pProp, eAct);};
99};
100
107template <class T>
109{
110private:
111 T* pObj_;
112 int (T::*fpt_)(PropertyBase* pProp, ActionType eAct, long param);
113 long param_;
114
115public:
116 ActionEx(T* pObj, int(T::*fpt)(PropertyBase* pProp, ActionType eAct, long data), long data) :
117 pObj_(pObj), fpt_(fpt), param_(data) {};
120 { return (*pObj_.*fpt_)(pProp, eAct, param_);};
121};
122
129class ActionLambda final : public ActionFunctor
130{
131 std::function<int(PropertyBase*, ActionType)> func_;
132
133public:
134 template <typename F> explicit ActionLambda(F func) : func_(func) {}
135
136 int Execute(PropertyBase* pProp, ActionType eAct) final {
137 return func_(pProp, eAct);
138 }
139};
140
144class Property : public PropertyBase
145{
146public:
147 Property(const char* name) :
148 readOnly_(false),
149 fpAction_(0),
150 cached_(false),
151 hasData_(false),
152 initStatus_(true),
153 limits_(false),
154 sequenceable_(false),
157 lowerLimit_(0.0),
158 upperLimit_(0.0),
159 name_(name)
160 {}
161
162 virtual ~Property()
163 {
164 delete fpAction_;
165 }
166
167 bool GetCached()const {return cached_;}
168 void SetCached(bool bState=true) {cached_ = bState;}
169
170 bool GetReadOnly()const {return readOnly_;}
171 void SetReadOnly(bool bState=true) {readOnly_ = bState;}
172
173 bool GetInitStatus() const {return initStatus_;}
174 void SetInitStatus(bool init) {initStatus_ = init;}
175
176 void RegisterAction(ActionFunctor* fpAction) {delete fpAction_; fpAction_ = fpAction;}
177 int Update()
178 {
179 if (fpAction_)
180 return fpAction_->Execute(this, BeforeGet);
181 else
182 return DEVICE_OK;
183 }
184 int Apply()
185 {
186 if (fpAction_)
187 return fpAction_->Execute(this, AfterSet);
188 else
189 return DEVICE_OK;
190 }
191
192 // discrete set of allowed values
193 std::vector<std::string> GetAllowedValues() const;
194
196 {
197 values_.clear();
198 }
199
200 void AddAllowedValue(const char* value);
201 void AddAllowedValue(const char* value, long data);
202 bool IsAllowed(const char* value) const;
203 bool GetData(const char* value, long& data) const;
204
205 bool HasLimits() const
206 {
207 return limits_;
208 }
209
210 double GetLowerLimit() const
211 {
212 return limits_ ? lowerLimit_ : 0.0;
213 }
214
215 double GetUpperLimit() const
216 {
217 return limits_ ? upperLimit_ : 0.0;
218 }
219
220 bool SetLimits(double lowerLimit, double upperLimit)
221 {
222 limits_ = true;
223 // do not allow limits for properties with discrete values defined
224 if (values_.size() > 0)
225 limits_ = false;
226
227 if (lowerLimit >= upperLimit)
228 limits_ = false;
229
230 lowerLimit_ = lowerLimit;
231 upperLimit_ = upperLimit;
232
233 return limits_;
234 }
235
237 {
238 if (fpAction_)
240 return sequenceable_;
241 }
242
243 void SetSequenceable(long sequenceMaxSize);
244
245 long GetSequenceMaxSize() const
246 {
247 return sequenceMaxSize_;
248 }
249
251 {
252 try
253 {
254 if (sequenceEvents_.size() > 0){
255 sequenceEvents_.clear();
256 }
257 } catch (...)
258 {
259 return DEVICE_ERR;
260 }
261
262 return DEVICE_OK;
263 }
264
265 int AddToSequence(const char* value)
266 {
267 try
268 {
269 sequenceEvents_.push_back(value);
270 if (sequenceEvents_.size() > (unsigned) GetSequenceMaxSize())
272 } catch (...)
273 {
274 return DEVICE_ERR;
275 }
276
277 return DEVICE_OK;
278 }
279
281 {
282 if (fpAction_)
283 return fpAction_->Execute(this, AfterLoadSequence);
284
285 return DEVICE_OK; // Return an error instead???
286 }
287
288 std::string GetName() const
289 {
290 return name_;
291 }
292
293 std::vector<std::string> GetSequence() const
294 {
295 return sequenceEvents_;
296 }
297
299 {
300 if (fpAction_)
301 return fpAction_->Execute(this, MM::StartSequence);
302 return DEVICE_OK; // Return an error instead???
303 }
304
306 {
307 if (fpAction_)
308 return fpAction_->Execute(this, MM::StopSequence);
309 return DEVICE_OK; // Return an error instead???
310 }
311
312protected:
321 std::vector<std::string> sequenceEvents_;
324 std::map<std::string, long> values_; // allowed values
325 const std::string name_;
326
327private:
328 Property& operator=(const Property&);
329};
330
335{
336public:
337 StringProperty(const char* name) :
338 Property(name)
339 {}
340 virtual ~StringProperty(){};
341
343
344 // setting and getting values
345 bool Set(double val);
346 bool Set(long val);
347 bool Set(const char* val);
348
349 bool Get(double& val) const;
350 bool Get(long& val) const ;
351 bool Get(std::string& val) const;
352
353 bool SetLimits(double /*lowerLimit*/, double /*upperLimit*/) {return false;}
354
355private:
356 StringProperty& operator=(const StringProperty&);
357
358 std::string value_;
359};
360
366{
367public:
368 FloatProperty(const char* name) :
369 Property(name),
370 value_(0.0),
371 decimalPlaces_(4)
372 {
373 int rms = 1;
374 for (int i = 0; i < decimalPlaces_; ++i)
375 rms *= 10;
376 reciprocalMinimalStep_ = rms;
377 }
378
379 virtual ~FloatProperty() {}
380
382
383 // setting and getting values
384 bool Set(double val);
385 bool Set(long val);
386 bool Set(const char* val);
387
388 bool Get(double& val) const;
389 bool Get(long& val) const ;
390 bool Get(std::string& val) const;
391
392 bool SetLimits(double lowerLimit, double upperLimit);
393
394private:
395 FloatProperty& operator=(const FloatProperty&);
396
397 double Truncate(double dVal);
398 double TruncateDown(double dVal);
399 double TruncateUp(double dVal);
400 double value_;
401 int decimalPlaces_;
402 double reciprocalMinimalStep_;
403};
404
409{
410public:
411 IntegerProperty(const char* name) :
412 Property(name),
413 value_(0)
414 {}
416
418
419 // setting and getting values
420 bool Set(double val);
421 bool Set(long val);
422 bool Set(const char* val);
423
424 bool Get(double& val) const;
425 bool Get(long& val) const ;
426 bool Get(std::string& val) const;
427
428private:
429 IntegerProperty& operator=(const IntegerProperty&);
430
431 long value_;
432};
433
438{
439public:
442
443 int CreateProperty(const char* name, const char* value, PropertyType eType, bool bReadOnly, ActionFunctor* pAct=0, bool isPreInitProperty=false);
444 int RegisterAction(const char* name, ActionFunctor* fpAct);
445 int SetAllowedValues(const char* name, std::vector<std::string>& values);
446 int ClearAllowedValues(const char* name);
447 int AddAllowedValue(const char* name, const char* value, long data);
448 int AddAllowedValue(const char* name, const char* value);
449 int GetPropertyData(const char* name, const char* value, long& data);
450 int GetCurrentPropertyData(const char* name, long& data);
451 int Set(const char* propName, const char* Value);
452 int Get(const char* propName, std::string& val) const;
453 Property* Find(const char* name) const;
454 std::vector<std::string> GetNames() const;
455 unsigned GetSize() const;
456 bool GetName(unsigned uIdx, std::string& strName) const;
457 int UpdateAll();
458 int ApplyAll();
459 int Update(const char* Name);
460 int Apply(const char* Name);
461
462private:
463 typedef std::map<std::string, Property*> CPropArray;
464 CPropArray properties_;
465};
466
467
468} // namespace MM
#define DEVICE_SEQUENCE_TOO_LARGE
Definition MMDeviceConstants.h:86
#define DEVICE_ERR
Definition MMDeviceConstants.h:48
#define DEVICE_OK
Definition MMDeviceConstants.h:47
Extended device action implementation.
Definition Property.h:109
int Execute(MM::PropertyBase *pProp, MM::ActionType eAct)
Definition Property.h:119
~ActionEx()
Definition Property.h:118
ActionEx(T *pObj, int(T::*fpt)(PropertyBase *pProp, ActionType eAct, long data), long data)
Definition Property.h:116
Abstract interface to invoke specific action in the device.
Definition Property.h:76
virtual ~ActionFunctor()
Definition Property.h:78
virtual int Execute(PropertyBase *pProp, ActionType eAct)=0
Device action implementation.
Definition Property.h:87
int Execute(PropertyBase *pProp, ActionType eAct)
Definition Property.h:97
~Action()
Definition Property.h:95
Action(T *pObj, int(T::*fpt)(PropertyBase *pProp, ActionType eAct))
Definition Property.h:93
Action implementation using std::function to wrap arbitrary callables.
Definition Property.h:130
int Execute(PropertyBase *pProp, ActionType eAct) final
Definition Property.h:136
ActionLambda(F func)
Definition Property.h:134
Floating point property class (uses double type for value representation).
Definition Property.h:366
FloatProperty(const char *name)
Definition Property.h:368
bool SetLimits(double lowerLimit, double upperLimit)
Definition Property.cpp:200
PropertyType GetType()
Definition Property.h:381
bool Get(double &val) const
Definition Property.cpp:178
virtual ~FloatProperty()
Definition Property.h:379
bool Set(double val)
Definition Property.cpp:156
Integer property class.
Definition Property.h:409
bool Set(double val)
Definition Property.cpp:210
IntegerProperty(const char *name)
Definition Property.h:411
~IntegerProperty()
Definition Property.h:415
bool Get(double &val) const
Definition Property.cpp:231
PropertyType GetType()
Definition Property.h:417
Base API for all device properties.
Definition Property.h:39
virtual double GetLowerLimit() const =0
virtual int AddToSequence(const char *value)=0
virtual bool SetLimits(double lowerLimit, double upperLimit)=0
virtual bool Set(const char *Val)=0
virtual bool HasLimits() const =0
virtual void SetSequenceable(long sequenceSize)=0
virtual double GetUpperLimit() const =0
virtual ~PropertyBase()
Definition Property.h:41
virtual std::string GetName() const =0
virtual long GetSequenceMaxSize() const =0
virtual int SendSequence()=0
virtual bool Get(long &lVal) const =0
virtual std::vector< std::string > GetSequence() const =0
virtual bool Get(double &dVal) const =0
virtual bool Set(double dVal)=0
virtual int ClearSequence()=0
virtual bool Set(long lVal)=0
virtual PropertyType GetType()=0
virtual bool Get(std::string &strVal) const =0
An array of properties supported by a device.
Definition Property.h:438
bool GetName(unsigned uIdx, std::string &strName) const
Definition Property.cpp:436
int ClearAllowedValues(const char *name)
Definition Property.cpp:379
int UpdateAll()
Definition Property.cpp:459
int GetCurrentPropertyData(const char *name, long &data)
Definition Property.cpp:422
int GetPropertyData(const char *name, const char *value, long &data)
Definition Property.cpp:410
int RegisterAction(const char *name, ActionFunctor *fpAct)
Definition Property.cpp:448
std::vector< std::string > GetNames() const
Definition Property.cpp:313
int ApplyAll()
Definition Property.cpp:472
int AddAllowedValue(const char *name, const char *value, long data)
Definition Property.cpp:389
int Set(const char *propName, const char *Value)
Definition Property.cpp:266
int Get(const char *propName, std::string &val) const
Definition Property.cpp:289
~PropertyCollection()
Definition Property.cpp:259
int CreateProperty(const char *name, const char *value, PropertyType eType, bool bReadOnly, ActionFunctor *pAct=0, bool isPreInitProperty=false)
Definition Property.cpp:329
PropertyCollection()
Definition Property.cpp:255
int Update(const char *Name)
Definition Property.cpp:485
Property * Find(const char *name) const
Definition Property.cpp:305
int Apply(const char *Name)
Definition Property.cpp:494
int SetAllowedValues(const char *name, std::vector< std::string > &values)
Definition Property.cpp:366
unsigned GetSize() const
Definition Property.cpp:324
Property API with most of the Property mechanism implemented.
Definition Property.h:145
void SetCached(bool bState=true)
Definition Property.h:168
virtual ~Property()
Definition Property.h:162
double GetUpperLimit() const
Definition Property.h:215
bool GetReadOnly() const
Definition Property.h:170
void SetReadOnly(bool bState=true)
Definition Property.h:171
bool IsSequenceable()
Definition Property.h:236
long GetSequenceMaxSize() const
Definition Property.h:245
int ClearSequence()
Definition Property.h:250
void SetSequenceable(long sequenceMaxSize)
Definition Property.cpp:82
bool sequenceable_
Definition Property.h:319
long sequenceMaxSize_
Definition Property.h:320
int StopSequence()
Definition Property.h:305
std::vector< std::string > GetSequence() const
Definition Property.h:293
ActionFunctor * fpAction_
Definition Property.h:314
Property(const char *name)
Definition Property.h:147
int Update()
Definition Property.h:177
bool hasData_
Definition Property.h:316
bool readOnly_
Definition Property.h:313
double lowerLimit_
Definition Property.h:322
const std::string name_
Definition Property.h:325
bool cached_
Definition Property.h:315
bool IsAllowed(const char *value) const
Definition Property.cpp:55
std::map< std::string, long > values_
Definition Property.h:324
double upperLimit_
Definition Property.h:323
bool initStatus_
Definition Property.h:317
int Apply()
Definition Property.h:184
void AddAllowedValue(const char *value)
Definition Property.cpp:41
bool SetLimits(double lowerLimit, double upperLimit)
Definition Property.h:220
std::vector< std::string > sequenceEvents_
Definition Property.h:321
double GetLowerLimit() const
Definition Property.h:210
bool HasLimits() const
Definition Property.h:205
int AddToSequence(const char *value)
Definition Property.h:265
void SetInitStatus(bool init)
Definition Property.h:174
bool GetInitStatus() const
Definition Property.h:173
std::string GetName() const
Definition Property.h:288
void ClearAllowedValues()
Definition Property.h:195
std::vector< std::string > GetAllowedValues() const
Definition Property.cpp:32
int StartSequence()
Definition Property.h:298
bool GetData(const char *value, long &data) const
Definition Property.cpp:67
bool limits_
Definition Property.h:318
void RegisterAction(ActionFunctor *fpAction)
Definition Property.h:176
bool GetCached() const
Definition Property.h:167
int SendSequence()
Definition Property.h:280
String property class.
Definition Property.h:335
bool Set(double val)
Definition Property.cpp:93
bool Get(double &val) const
Definition Property.cpp:115
StringProperty(const char *name)
Definition Property.h:337
bool SetLimits(double, double)
Definition Property.h:353
PropertyType GetType()
Definition Property.h:342
virtual ~StringProperty()
Definition Property.h:340
Definition CameraImageMetadata.h:25
PropertyType
Definition MMDeviceConstants.h:258
@ Float
Definition MMDeviceConstants.h:261
@ String
Definition MMDeviceConstants.h:260
@ Integer
Definition MMDeviceConstants.h:262
ActionType
Definition MMDeviceConstants.h:265
@ IsSequenceable
Definition MMDeviceConstants.h:269
@ AfterSet
Definition MMDeviceConstants.h:268
@ StopSequence
Definition MMDeviceConstants.h:272
@ AfterLoadSequence
Definition MMDeviceConstants.h:270
@ StartSequence
Definition MMDeviceConstants.h:271
@ BeforeGet
Definition MMDeviceConstants.h:267