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