forked from micro-manager/micro-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.cpp
237 lines (207 loc) · 6.6 KB
/
Configuration.cpp
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
///////////////////////////////////////////////////////////////////////////////
// FILE: Configuration.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: Core
//-----------------------------------------------------------------------------
// DESCRIPTION: Set of properties defined as a high level command
// AUTHOR: Nenad Amodaj, [email protected], 09/08/2005
// COPYRIGHT: University of California, San Francisco, 2006
// LICENSE: This file is distributed under the "Lesser GPL" (LGPL) 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 "Configuration.h"
#include "../MMDevice/MMDevice.h"
#include "Error.h"
#include <assert.h>
#include <sstream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
string PropertySetting::generateKey(const char* device, const char* prop)
{
string key(device);
key += "-";
key += prop;
return key;
}
/**
* Returns verbose description of the object's contents.
*/
string PropertySetting::getVerbose() const
{
ostringstream txt;
txt << deviceLabel_ << ":" << propertyName_ << "=" << value_;
return txt.str();
}
bool PropertySetting::isEqualTo(const PropertySetting& ps)
{
if (ps.deviceLabel_.compare(deviceLabel_) == 0 &&
ps.propertyName_.compare(propertyName_) == 0 &&
ps.value_.compare(value_) == 0)
return true;
else
return false;
}
/**
* Returns verbose description of the object's contents.
*/
std::string Configuration::getVerbose() const
{
std::ostringstream txt;
std::vector<PropertySetting>::const_iterator it;
txt << "<html>";
for (it=settings_.begin(); it!=settings_.end(); it++)
txt << it->getVerbose() << "<br>";
txt << "</html>";
return txt.str();
}
/**
* Returns the setting with specified index.
*/
PropertySetting Configuration::getSetting(size_t index) const throw (CMMError)
{
if (index >= settings_.size())
{
std::ostringstream errTxt;
errTxt << (unsigned int)index << " - invalid configuration setting index";
throw CMMError(errTxt.str().c_str(), MMERR_DEVICE_GENERIC);
}
return settings_[index];
}
/**
* Checks whether the property is included in the configuration.
*/
bool Configuration::isPropertyIncluded(const char* device, const char* prop)
{
map<string, int>::iterator it = index_.find(PropertySetting::generateKey(device, prop));
if (it != index_.end())
return true;
else
return false;
}
/**
* Get the setting with specified device name and property name.
*/
PropertySetting Configuration::getSetting(const char* device, const char* prop)
{
map<string, int>::iterator it = index_.find(PropertySetting::generateKey(device, prop));
if (it == index_.end())
{
std::ostringstream errTxt;
errTxt << "Property " << prop << " not found in device " << device << ".";
throw CMMError(errTxt.str().c_str(), MMERR_DEVICE_GENERIC);
}
if (((unsigned int) it->second) >= settings_.size()) {
std::ostringstream errTxt;
errTxt << "Internal Error locating Property " << prop << " in device " << device << ".";
throw CMMError(errTxt.str().c_str(), MMERR_DEVICE_GENERIC);
}
return settings_[it->second];
}
/**
* Checks whether the setting is included in the configuration.
*/
bool Configuration::isSettingIncluded(const PropertySetting& ps)
{
map<string, int>::iterator it = index_.find(ps.getKey());
if (it != index_.end() && settings_[it->second].getPropertyValue().compare(ps.getPropertyValue()) == 0)
return true;
else
return false;
}
/**
* Checks whether a configuration is included.
* Included means that all devices from the operand configuration are
* included and that settings match
*/
bool Configuration::isConfigurationIncluded(const Configuration& cfg)
{
vector<PropertySetting>::const_iterator it;
for (it=cfg.settings_.begin(); it!=cfg.settings_.end(); ++it)
if (!isSettingIncluded(*it))
return false;
return true;
}
/**
* Adds new property setting to the existing contents.
*/
void Configuration::addSetting(const PropertySetting& setting)
{
map<string, int>::iterator it = index_.find(setting.getKey());
if (it != index_.end())
{
// replace
settings_[it->second] = setting;
}
else
{
// add new
index_[setting.getKey()] = (int)settings_.size();
settings_.push_back(setting);
}
}
/**
* Removes property setting, specified by device and property names, from the configuration.
*/
void Configuration::deleteSetting(const char* device, const char* prop)
{
map<string, int>::iterator it = index_.find(PropertySetting::generateKey(device, prop));
if (it == index_.end())
{
std::ostringstream errTxt;
errTxt << "Property " << prop << " not found in device " << device << ".";
throw CMMError(errTxt.str().c_str(), MMERR_DEVICE_GENERIC);
}
settings_.erase(settings_.begin() + it->second); // The argument of erase produces an iterator at the desired position.
// Re-index
index_.clear();
for (unsigned int i = 0; i < settings_.size(); i++)
{
index_[settings_[i].getKey()] = i;
}
}
/**
* Returns the property pair with specified index.
*/
PropertyPair PropertyBlock::getPair(size_t index) const throw (CMMError)
{
std::map<std::string, PropertyPair>::const_iterator it = pairs_.begin();
if (index >= pairs_.size())
{
std::ostringstream errTxt;
errTxt << (unsigned int)index << " - invalid property pair index";
throw CMMError(errTxt.str().c_str(), MMERR_DEVICE_GENERIC);
}
for (size_t i=0; i<index; i++) it++;
return it->second;
}
/**
* Adds a new pair to the current contents.
*/
void PropertyBlock::addPair(const PropertyPair& pair)
{
pairs_[pair.getPropertyName()] = pair;
}
/**
* Get value of the specified key (property).
*/
std::string PropertyBlock::getValue(const char* key) const throw (CMMError)
{
std::map<std::string, PropertyPair>::const_iterator it;
it = pairs_.find(key);
if (it != pairs_.end())
return it->second.getPropertyValue();
std::ostringstream errTxt;
errTxt << key << " - invalid property name";
throw CMMError(errTxt.str().c_str(), MMERR_DEVICE_GENERIC);
}