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
227 {
228 limits_ = false;
229 lowerLimit_ = 0.0;
230 upperLimit_ = 0.0;
231 }
232
233 bool SetLimits(double lowerLimit, double upperLimit)
234 {
235 limits_ = true;
236 // do not allow limits for properties with discrete values defined
237 if (values_.size() > 0)
238 limits_ = false;
239
240 if (lowerLimit >= upperLimit)
241 limits_ = false;
242
243 lowerLimit_ = lowerLimit;
244 upperLimit_ = upperLimit;
245
246 return limits_;
247 }
248
250 {
251 if (fpAction_)
253 return sequenceable_;
254 }
255
256 void SetSequenceable(long sequenceMaxSize);
257
258 long GetSequenceMaxSize() const
259 {
260 return sequenceMaxSize_;
261 }
262
264 {
265 try
266 {
267 if (sequenceEvents_.size() > 0){
268 sequenceEvents_.clear();
269 }
270 } catch (...)
271 {
272 return DEVICE_ERR;
273 }
274
275 return DEVICE_OK;
276 }
277
278 int AddToSequence(const char* value)
279 {
280 try
281 {
282 sequenceEvents_.push_back(value);
283 if (sequenceEvents_.size() > (unsigned) GetSequenceMaxSize())
285 } catch (...)
286 {
287 return DEVICE_ERR;
288 }
289
290 return DEVICE_OK;
291 }
292
294 {
295 if (fpAction_)
296 return fpAction_->Execute(this, AfterLoadSequence);
297
298 return DEVICE_OK; // Return an error instead???
299 }
300
301 std::string GetName() const
302 {
303 return name_;
304 }
305
306 std::vector<std::string> GetSequence() const
307 {
308 return sequenceEvents_;
309 }
310
312 {
313 if (fpAction_)
314 return fpAction_->Execute(this, MM::StartSequence);
315 return DEVICE_OK; // Return an error instead???
316 }
317
319 {
320 if (fpAction_)
321 return fpAction_->Execute(this, MM::StopSequence);
322 return DEVICE_OK; // Return an error instead???
323 }
324
325protected:
334 std::vector<std::string> sequenceEvents_;
337 std::map<std::string, long> values_; // allowed values
338 const std::string name_;
339
340private:
341 Property& operator=(const Property&);
342};
343
348{
349public:
350 StringProperty(const char* name) :
351 Property(name)
352 {}
353 virtual ~StringProperty(){};
354
356
357 // setting and getting values
358 bool Set(double val);
359 bool Set(long val);
360 bool Set(const char* val);
361
362 bool Get(double& val) const;
363 bool Get(long& val) const ;
364 bool Get(std::string& val) const;
365
366 bool SetLimits(double /*lowerLimit*/, double /*upperLimit*/) {return false;}
367
368private:
369 StringProperty& operator=(const StringProperty&);
370
371 std::string value_;
372};
373
379{
380public:
381 FloatProperty(const char* name) :
382 Property(name),
383 value_(0.0),
384 decimalPlaces_(4)
385 {
386 int rms = 1;
387 for (int i = 0; i < decimalPlaces_; ++i)
388 rms *= 10;
389 reciprocalMinimalStep_ = rms;
390 }
391
392 virtual ~FloatProperty() {}
393
395
396 // setting and getting values
397 bool Set(double val);
398 bool Set(long val);
399 bool Set(const char* val);
400
401 bool Get(double& val) const;
402 bool Get(long& val) const ;
403 bool Get(std::string& val) const;
404
405 bool SetLimits(double lowerLimit, double upperLimit);
406
407private:
408 FloatProperty& operator=(const FloatProperty&);
409
410 double Truncate(double dVal);
411 double TruncateDown(double dVal);
412 double TruncateUp(double dVal);
413 double value_;
414 int decimalPlaces_;
415 double reciprocalMinimalStep_;
416};
417
422{
423public:
424 IntegerProperty(const char* name) :
425 Property(name),
426 value_(0)
427 {}
429
431
432 // setting and getting values
433 bool Set(double val);
434 bool Set(long val);
435 bool Set(const char* val);
436
437 bool Get(double& val) const;
438 bool Get(long& val) const ;
439 bool Get(std::string& val) const;
440
441private:
442 IntegerProperty& operator=(const IntegerProperty&);
443
444 long value_;
445};
446
451{
452public:
455
456 int CreateProperty(const char* name, const char* value, PropertyType eType, bool bReadOnly, ActionFunctor* pAct=0, bool isPreInitProperty=false);
457 int RegisterAction(const char* name, ActionFunctor* fpAct);
458 int SetAllowedValues(const char* name, std::vector<std::string>& values);
459 int ClearAllowedValues(const char* name);
460 int AddAllowedValue(const char* name, const char* value, long data);
461 int AddAllowedValue(const char* name, const char* value);
462 int GetPropertyData(const char* name, const char* value, long& data);
463 int GetCurrentPropertyData(const char* name, long& data);
464 int Set(const char* propName, const char* Value);
465 int Get(const char* propName, std::string& val) const;
466 Property* Find(const char* name) const;
467 std::vector<std::string> GetNames() const;
468 unsigned GetSize() const;
469 bool GetName(unsigned uIdx, std::string& strName) const;
470 int UpdateAll();
471 int ApplyAll();
472 int Update(const char* Name);
473 int Apply(const char* Name);
474
475private:
476 typedef std::map<std::string, Property*> CPropArray;
477 CPropArray properties_;
478};
479
480
481} // 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:379
FloatProperty(const char *name)
Definition Property.h:381
bool SetLimits(double lowerLimit, double upperLimit)
Definition Property.cpp:200
PropertyType GetType()
Definition Property.h:394
bool Get(double &val) const
Definition Property.cpp:178
virtual ~FloatProperty()
Definition Property.h:392
bool Set(double val)
Definition Property.cpp:156
Integer property class.
Definition Property.h:422
bool Set(double val)
Definition Property.cpp:210
IntegerProperty(const char *name)
Definition Property.h:424
~IntegerProperty()
Definition Property.h:428
bool Get(double &val) const
Definition Property.cpp:231
PropertyType GetType()
Definition Property.h:430
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:451
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:249
long GetSequenceMaxSize() const
Definition Property.h:258
int ClearSequence()
Definition Property.h:263
void SetSequenceable(long sequenceMaxSize)
Definition Property.cpp:82
bool sequenceable_
Definition Property.h:332
long sequenceMaxSize_
Definition Property.h:333
int StopSequence()
Definition Property.h:318
std::vector< std::string > GetSequence() const
Definition Property.h:306
ActionFunctor * fpAction_
Definition Property.h:327
Property(const char *name)
Definition Property.h:147
int Update()
Definition Property.h:177
bool hasData_
Definition Property.h:329
bool readOnly_
Definition Property.h:326
double lowerLimit_
Definition Property.h:335
const std::string name_
Definition Property.h:338
bool cached_
Definition Property.h:328
bool IsAllowed(const char *value) const
Definition Property.cpp:55
void ClearLimits()
Definition Property.h:226
std::map< std::string, long > values_
Definition Property.h:337
double upperLimit_
Definition Property.h:336
bool initStatus_
Definition Property.h:330
int Apply()
Definition Property.h:184
void AddAllowedValue(const char *value)
Definition Property.cpp:41
bool SetLimits(double lowerLimit, double upperLimit)
Definition Property.h:233
std::vector< std::string > sequenceEvents_
Definition Property.h:334
double GetLowerLimit() const
Definition Property.h:210
bool HasLimits() const
Definition Property.h:205
int AddToSequence(const char *value)
Definition Property.h:278
void SetInitStatus(bool init)
Definition Property.h:174
bool GetInitStatus() const
Definition Property.h:173
std::string GetName() const
Definition Property.h:301
void ClearAllowedValues()
Definition Property.h:195
std::vector< std::string > GetAllowedValues() const
Definition Property.cpp:32
int StartSequence()
Definition Property.h:311
bool GetData(const char *value, long &data) const
Definition Property.cpp:67
bool limits_
Definition Property.h:331
void RegisterAction(ActionFunctor *fpAction)
Definition Property.h:176
bool GetCached() const
Definition Property.h:167
int SendSequence()
Definition Property.h:293
String property class.
Definition Property.h:348
bool Set(double val)
Definition Property.cpp:93
bool Get(double &val) const
Definition Property.cpp:115
StringProperty(const char *name)
Definition Property.h:350
bool SetLimits(double, double)
Definition Property.h:366
PropertyType GetType()
Definition Property.h:355
virtual ~StringProperty()
Definition Property.h:353
Definition CameraImageMetadata.h:27
PropertyType
Definition MMDeviceConstants.h:267
@ Float
Definition MMDeviceConstants.h:270
@ String
Definition MMDeviceConstants.h:269
@ Integer
Definition MMDeviceConstants.h:271
ActionType
Definition MMDeviceConstants.h:274
@ IsSequenceable
Definition MMDeviceConstants.h:278
@ AfterSet
Definition MMDeviceConstants.h:277
@ StopSequence
Definition MMDeviceConstants.h:281
@ AfterLoadSequence
Definition MMDeviceConstants.h:279
@ StartSequence
Definition MMDeviceConstants.h:280
@ BeforeGet
Definition MMDeviceConstants.h:276