/
Enumeration.cpp
334 lines (277 loc) · 7.76 KB
/
Enumeration.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
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
/***************************************************************************
* Copyright (c) Ian Rees (ian.rees@gmail.com) 2015 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <cassert>
# include <cstring>
# include <cstdlib>
#endif
#include <Base/Exception.h>
#include "Enumeration.h"
using namespace App;
Enumeration::Enumeration()
: _EnumArray(NULL), _ownEnumArray(false), _index(0), _maxVal(-1)
{
}
Enumeration::Enumeration(const Enumeration &other)
{
if (other._ownEnumArray) {
setEnums(other.getEnumVector());
} else {
_EnumArray = other._EnumArray;
}
_ownEnumArray = other._ownEnumArray;
_index = other._index;
_maxVal = other._maxVal;
}
Enumeration::Enumeration(const char *valStr)
: _ownEnumArray(true), _index(0), _maxVal(0)
{
_EnumArray = new const char*[2];
#if defined (_MSC_VER)
_EnumArray[0] = _strdup(valStr);
#else
_EnumArray[0] = strdup(valStr);
#endif
_EnumArray[1] = NULL;
}
Enumeration::Enumeration(const char **list, const char *valStr)
: _EnumArray(list), _ownEnumArray(false)
{
findMaxVal();
setValue(valStr);
}
Enumeration::~Enumeration()
{
if (_ownEnumArray) {
if (_EnumArray != NULL) {
tearDown();
}
}
}
void Enumeration::tearDown(void)
{
// Ugly...
char **plEnums = (char **)_EnumArray;
// Delete C Strings first
while (*(plEnums++) != NULL) {
free(*plEnums);
}
delete [] _EnumArray;
_EnumArray = NULL;
_ownEnumArray = false;
_maxVal = -1;
}
void Enumeration::setEnums(const char **plEnums)
{
std::string oldValue;
bool preserve = (isValid() && plEnums != NULL);
if (preserve) {
oldValue = getCStr();
}
// set _ownEnumArray
if (isValid() && _ownEnumArray) {
tearDown();
}
// set...
_EnumArray = plEnums;
// set _maxVal
findMaxVal();
// set _index
_index = 0;
if (preserve) {
setValue(oldValue);
}
}
void Enumeration::setEnums(const std::vector<std::string> &values)
{
std::string oldValue;
bool preserve = isValid();
if (preserve) {
oldValue = getCStr();
}
if (isValid() && _ownEnumArray) {
tearDown();
}
_EnumArray = new const char*[values.size() + 1];
int i = 0;
for (std::vector<std::string>::const_iterator it = values.begin(); it != values.end(); ++it) {
#if defined (_MSC_VER)
_EnumArray[i++] = _strdup(it->c_str());
#else
_EnumArray[i++] = strdup(it->c_str());
#endif
}
_EnumArray[i] = 0; // null termination
// Other state variables
_maxVal = values.size() - 1;
_ownEnumArray = true;
_index = 0;
if (preserve) {
setValue(oldValue);
}
}
void Enumeration::setValue(const char *value)
{
// using string methods without set, use setEnums(const char** plEnums) first!
//assert(_EnumArray);
if (!_EnumArray) {
_index = 0;
return;
}
unsigned int i = 0;
const char **plEnums = _EnumArray;
// search for the right entry
while (1) {
// end of list? set zero
if (*plEnums == NULL) {
_index = 0;
break;
}
if (strcmp(*plEnums, value) == 0) {
_index = i;
break;
}
++plEnums;
++i;
}
}
void Enumeration::setValue(long value, bool checkRange)
{
if (value >= 0 && value <= _maxVal) {
_index = value;
} else {
if (checkRange) {
throw Base::ValueError("Out of range");
} else {
_index = value;
}
}
}
bool Enumeration::isValue(const char *value) const
{
// using string methods without set, use setEnums(const char** plEnums) first!
//assert(_EnumArray);
int i = getInt();
if (i == -1) {
return false;
} else {
return strcmp(_EnumArray[i], value) == 0;
}
}
bool Enumeration::contains(const char *value) const
{
// using string methods without set, use setEnums(const char** plEnums) first!
//assert(_EnumArray);
if (!isValid()) {
return false;
}
const char **plEnums = _EnumArray;
// search for the right entry
while (1) {
// end of list?
if (*plEnums == NULL)
return false;
if (strcmp(*plEnums, value) == 0)
return true;
++plEnums;
}
}
const char * Enumeration::getCStr(void) const
{
// using string methods without set, use setEnums(const char** plEnums) first!
//assert(_EnumArray);
if (!isValid() || _index < 0 || _index > _maxVal) {
return NULL;
}
return _EnumArray[_index];
}
int Enumeration::getInt(void) const
{
if (!isValid() || _index < 0 || _index > _maxVal) {
return -1;
}
return _index;
}
std::vector<std::string> Enumeration::getEnumVector(void) const
{
// using string methods without set, use setEnums(const char** plEnums) first!
if (!_EnumArray)
return std::vector<std::string>();
std::vector<std::string> result;
const char **plEnums = _EnumArray;
// end of list?
while (*plEnums != NULL) {
result.push_back(*plEnums);
++plEnums;
}
return result;
}
const char ** Enumeration::getEnums(void) const
{
return _EnumArray;
}
bool Enumeration::isValid(void) const
{
return (_EnumArray != NULL && _index >= 0 && _index <= _maxVal);
}
Enumeration & Enumeration::operator=(const Enumeration &other)
{
if (other._ownEnumArray) {
setEnums(other.getEnumVector());
} else {
_EnumArray = other._EnumArray;
}
_ownEnumArray = other._ownEnumArray;
_index = other._index;
_maxVal = other._maxVal;
return *this;
}
bool Enumeration::operator==(const Enumeration &other) const
{
if (getCStr() == NULL || other.getCStr() == NULL) {
return false;
}
return (strcmp(getCStr(), other.getCStr()) == 0);
}
bool Enumeration::operator==(const char *other) const
{
if (getCStr() == NULL) {
return false;
}
return (strcmp(getCStr(), other) == 0);
}
void Enumeration::findMaxVal(void)
{
if (_EnumArray == NULL) {
_maxVal = -1;
return;
}
const char **plEnums = _EnumArray;
long i = 0;
while (*(plEnums++) != NULL) {
++i;
// very unlikely to have enums with more then 5000 entries!
assert(i < 5000);
}
_maxVal = i;
}