-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperty.cpp
More file actions
501 lines (415 loc) · 11.6 KB
/
Property.cpp
File metadata and controls
501 lines (415 loc) · 11.6 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
///////////////////////////////////////////////////////////////////////////////
// FILE: Property.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: MMDevice - Device adapter kit
//-----------------------------------------------------------------------------
// DESCRIPTION: This class implements the basic property mechanism in
// Micro-Manager devices.
// AUTHOR: Nenad Amodaj, [email protected], 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.
// CVS: $Id$
#include "Property.h"
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstdio>
static const int BUFSIZE = 60; // For number-to-string conversion
std::vector<std::string> MM::Property::GetAllowedValues() const
{
std::vector<std::string> vals;
std::map<std::string, long>::const_iterator it;
for (it=values_.begin(); it != values_.end(); it++)
vals.push_back(it->first);
return vals;
}
void MM::Property::AddAllowedValue(const char* value)
{
values_.insert(std::make_pair(value, 0L));
limits_ = false;
}
void MM::Property::AddAllowedValue(const char* value, long data)
{
values_.insert(std::make_pair(value, data));
hasData_ = true;
limits_ = false;
}
bool MM::Property::IsAllowed(const char* value) const
{
if (values_.size() == 0)
return true; // any value is allowed
std::map<std::string, long>::const_iterator it = values_.find(value);
if (it == values_.end())
return false; // not found
else
return true;
}
bool MM::Property::GetData(const char* value, long& data) const
{
if (!hasData_)
return false;
std::map<std::string, long>::const_iterator it = values_.find(value);
if (it == values_.end())
return false; // not found
else
{
data = it->second;
return true;
}
}
void MM::Property::SetSequenceable(long sequenceMaxSize)
{
sequenceable_ = (sequenceMaxSize != 0);
sequenceMaxSize_ = sequenceMaxSize;
}
///////////////////////////////////////////////////////////////////////////////
// MM::StringProperty
// ~~~~~~~~~~~~~~~~~
//
bool MM::StringProperty::Set(double val)
{
char buf[BUFSIZE];
std::snprintf(buf, BUFSIZE, "%.2g", val);
value_ = buf;
return true;
}
bool MM::StringProperty::Set(long val)
{
char buf[BUFSIZE];
std::snprintf(buf, BUFSIZE, "%ld", val);
value_ = buf;
return true;
}
bool MM::StringProperty::Set(const char* val)
{
value_ = val;
return true;
}
bool MM::StringProperty::Get(double& val) const
{
val = std::atof(value_.c_str());
return true;
}
bool MM::StringProperty::Get(long& val) const
{
val = std::atol(value_.c_str());
return true;
}
bool MM::StringProperty::Get(std::string& strVal) const
{
strVal = value_;
return true;
}
///////////////////////////////////////////////////////////////////////////////
// MM::FloatProperty
// ~~~~~~~~~~~~~~~~
//
double MM::FloatProperty::Truncate(double dVal)
{
if (dVal >= 0)
return std::floor(dVal * reciprocalMinimalStep_ + 0.5) / reciprocalMinimalStep_;
else
return std::ceil(dVal * reciprocalMinimalStep_ - 0.5) / reciprocalMinimalStep_;
}
double MM::FloatProperty::TruncateDown(double dVal)
{
return std::floor(dVal * reciprocalMinimalStep_) / reciprocalMinimalStep_;
}
double MM::FloatProperty::TruncateUp(double dVal)
{
return std::ceil(dVal * reciprocalMinimalStep_) / reciprocalMinimalStep_;
}
bool MM::FloatProperty::Set(double dVal)
{
double val = Truncate(dVal);
if (limits_)
{
if (val < lowerLimit_ || val > upperLimit_)
return false;
}
value_ = val;
return true;
}
bool MM::FloatProperty::Set(long lVal)
{
return Set((double)lVal);
}
bool MM::FloatProperty::Set(const char* pszVal)
{
return Set(atof(pszVal));
}
bool MM::FloatProperty::Get(double& dVal) const
{
dVal = value_;
return true;
}
bool MM::FloatProperty::Get(long& lVal) const
{
lVal = (long)value_;
return true;
}
bool MM::FloatProperty::Get(std::string& strVal) const
{
char fmtStr[20];
char buf[BUFSIZE];
std::snprintf(fmtStr, sizeof(fmtStr), "%%.%df", decimalPlaces_);
std::snprintf(buf, BUFSIZE, fmtStr, value_);
strVal = buf;
return true;
}
bool MM::FloatProperty::SetLimits(double lowerLimit, double upperLimit)
{
return MM::Property::SetLimits(TruncateUp(lowerLimit), TruncateDown(upperLimit));
}
///////////////////////////////////////////////////////////////////////////////
// MM::IntegerProperty
// ~~~~~~~~~~~~~~~~~~
//
bool MM::IntegerProperty::Set(double dVal)
{
return Set((long)dVal);
}
bool MM::IntegerProperty::Set(long lVal)
{
if (limits_)
{
if (lVal < lowerLimit_ || lVal > upperLimit_)
return false;
}
value_ = lVal;
return true;
}
bool MM::IntegerProperty::Set(const char* pszVal)
{
return Set(std::atol(pszVal));
}
bool MM::IntegerProperty::Get(double& dVal) const
{
dVal = (double)value_;
return true;
}
bool MM::IntegerProperty::Get(long& lVal) const
{
lVal = value_;
return true;
}
bool MM::IntegerProperty::Get(std::string& strVal) const
{
char pszBuf[BUFSIZE];
std::snprintf(pszBuf, BUFSIZE, "%ld", value_);
strVal = pszBuf;
return true;
}
///////////////////////////////////////////////////////////////////////////////
// MM::PropertyCollection
// ~~~~~~~~~~~~~~~~~~~~~
//
MM::PropertyCollection::PropertyCollection()
{
}
MM::PropertyCollection::~PropertyCollection()
{
CPropArray::const_iterator it;
for (it = properties_.begin(); it != properties_.end(); it++)
delete it->second;
}
int MM::PropertyCollection::Set(const char* pszPropName, const char* pszValue)
{
MM::Property* pProp = Find(pszPropName);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
if (pProp->GetReadOnly())
return DEVICE_OK;
// NOTE: if the property is read-only we silently refuse to change the data
// and return OK code. Should this be an an error?
if (pProp->IsAllowed(pszValue))
{
// check property limits
if (!pProp->Set(pszValue))
return DEVICE_INVALID_PROPERTY_VALUE;
return pProp->Apply();
}
else
return DEVICE_INVALID_PROPERTY_VALUE;
}
int MM::PropertyCollection::Get(const char* pszPropName, std::string& strValue) const
{
MM::Property* pProp = Find(pszPropName);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
if (!pProp->GetCached())
{
int nRet = pProp->Update();
if (nRet != DEVICE_OK)
return nRet;
}
pProp->Get(strValue);
return DEVICE_OK;
}
MM::Property* MM::PropertyCollection::Find(const char* pszName) const
{
CPropArray::const_iterator it = properties_.find(pszName);
if (it == properties_.end())
return 0; // not found
return it->second;
}
std::vector<std::string> MM::PropertyCollection::GetNames() const
{
std::vector<std::string> nameList;
CPropArray::const_iterator it;
for (it = properties_.begin(); it != properties_.end(); it++)
nameList.push_back(it->first);
return nameList;
}
unsigned MM::PropertyCollection::GetSize() const
{
return (unsigned) properties_.size();
}
int MM::PropertyCollection::CreateProperty(const char* pszName, const char* pszValue, MM::PropertyType eType, bool bReadOnly, MM::ActionFunctor* pAct, bool isPreInitProperty)
{
// check if the name already exists
if (Find(pszName))
return DEVICE_DUPLICATE_PROPERTY;
MM::Property* pProp=0;
switch(eType)
{
case MM::String:
pProp = new MM::StringProperty(pszName);
break;
case MM::Integer:
pProp = new MM::IntegerProperty(pszName);
break;
case MM::Float:
pProp = new MM::FloatProperty(pszName);
break;
default:
return DEVICE_INVALID_PROPERTY_TYPE;
}
if (!pProp->Set(pszValue))
return false;
pProp->SetReadOnly(bReadOnly);
pProp->SetInitStatus(isPreInitProperty);
properties_[pszName] = pProp;
// assign action functor
pProp->RegisterAction(pAct);
return DEVICE_OK;
}
int MM::PropertyCollection::SetAllowedValues(const char* pszName, std::vector<std::string>& values)
{
MM::Property* pProp = Find(pszName);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
pProp->ClearAllowedValues();
for (unsigned i=0; i<values.size(); i++)
pProp->AddAllowedValue(values[i].c_str());
return DEVICE_OK;
}
int MM::PropertyCollection::ClearAllowedValues(const char* pszName)
{
MM::Property* pProp = Find(pszName);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
pProp->ClearAllowedValues();
return DEVICE_OK;
}
int MM::PropertyCollection::AddAllowedValue(const char* pszName, const char* value, long data)
{
MM::Property* pProp = Find(pszName);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
pProp->AddAllowedValue(value, data);
return DEVICE_OK;
}
int MM::PropertyCollection::AddAllowedValue(const char* pszName, const char* value)
{
MM::Property* pProp = Find(pszName);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
pProp->AddAllowedValue(value);
return DEVICE_OK;
}
int MM::PropertyCollection::GetPropertyData(const char* name, const char* value, long& data)
{
MM::Property* pProp = Find(name);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
if (!pProp->GetData(value, data))
return DEVICE_NO_PROPERTY_DATA;
return DEVICE_OK;
}
int MM::PropertyCollection::GetCurrentPropertyData(const char* name, long& data)
{
MM::Property* pProp = Find(name);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
std::string value;
pProp->Get(value);
if (!pProp->GetData(value.c_str(), data))
return DEVICE_NO_PROPERTY_DATA;
return DEVICE_OK;
}
bool MM::PropertyCollection::GetName(unsigned uIdx, std::string& strName) const
{
if (uIdx >= properties_.size())
return false; // unknown index
CPropArray::const_iterator it = properties_.begin();
for (unsigned i=0; i<uIdx; i++)
it++;
strName = it->first;
return true;
}
int MM::PropertyCollection::RegisterAction(const char* pszName, MM::ActionFunctor* fpAct)
{
MM::Property* pProp = Find(pszName);
if (!pProp)
return DEVICE_INVALID_PROPERTY; // name not found
pProp->RegisterAction(fpAct);
return true;
}
int MM::PropertyCollection::UpdateAll()
{
CPropArray::const_iterator it;
for (it=properties_.begin(); it!=properties_.end(); it++)
{
int nRet;
nRet = it->second->Update();
if (nRet != DEVICE_OK)
return nRet;
}
return DEVICE_OK;
}
int MM::PropertyCollection::ApplyAll()
{
CPropArray::const_iterator it;
for (it=properties_.begin(); it!=properties_.end(); it++)
{
int nRet;
nRet = it->second->Apply();
if (nRet != DEVICE_OK)
return nRet;
}
return DEVICE_OK;
}
int MM::PropertyCollection::Update(const char* pszName)
{
MM::Property* pProp = Find(pszName);
if (!pProp)
return DEVICE_INVALID_PROPERTY;
return pProp->Update();
}
int MM::PropertyCollection::Apply(const char* pszName)
{
MM::Property* pProp = Find(pszName);
if (!pProp)
return DEVICE_INVALID_PROPERTY;
return pProp->Apply();
}