Debian Codegraph @

src/App/Enumeration.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2015 Ian Rees <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <cassert>
24 #include <cstring>
25
26 #include <Base/Exception.h>
27 #include "Enumeration.h"
28 #include <string_view>
29
30 using namespace App;
31
32 namespace
33 {
34 struct StringCopy: public Enumeration::Object
35 {
36 explicit StringCopy(const char* str)
37 : d(str)
38 {}
39 const char* data() const override
40 {
41 return d.data();
42 }
43 bool isEqual(const char* str) const override
44 {
45 return d == str;
46 }
47 bool isCustom() const override
48 {
49 return true;
50 }
51
52 private:
53 std::string d;
54 };
55
56 struct StringView: public Enumeration::Object
57 {
58 explicit StringView(const char* str)
59 : d(str)
60 {}
61 const char* data() const override
62 {
63 return d.data();
64 }
65 bool isEqual(const char* str) const override
66 {
67 return d == str;
68 }
69 bool isCustom() const override
70 {
71 return false;
72 }
73
74 private:
75 std::string_view d;
76 };
77 } // namespace
78
79 Enumeration::Enumeration()
80 : _index(0)
81 {}
82
83 Enumeration::Enumeration(const Enumeration& other)
84 {
85 enumArray = other.enumArray;
86 _index = other._index;
87 }
88
89 Enumeration::Enumeration(const char* valStr)
90 : _index(0)
91 {
92 enumArray.push_back(std::make_shared<StringCopy>(valStr));
93 setValue(valStr);
94 }
95
96 Enumeration::Enumeration(const char** list, const char* valStr)
97 : _index(0)
98 {
99 while (list && *list) {
100 enumArray.push_back(std::make_shared<StringView>(*list));
101 list++;
102 }
103 setValue(valStr);
104 }
105
106 Enumeration::~Enumeration()
107 {
108 enumArray.clear();
109 }
110
111 void Enumeration::setEnums(const char** plEnums)
112 {
113 std::string oldValue;
114 bool preserve = (isValid() && plEnums != nullptr);
115 if (preserve) {
116 const char* str = getCStr();
117 if (str) {
118 oldValue = str;
119 }
120 }
121
122 enumArray.clear();
123 while (plEnums && *plEnums) {
124 enumArray.push_back(std::make_shared<StringView>(*plEnums));
125 plEnums++;
126 }
127
128 // set _index
129 if (_index < 0) {
130 _index = 0;
131 }
132 if (preserve) {
133 setValue(oldValue);
134 }
135 }
136
137 void Enumeration::setEnums(const std::vector<std::string>& values)
138 {
139 if (values.empty()) {
140 setEnums(nullptr);
141 return;
142 }
143
144 std::string oldValue;
145 bool preserve = isValid();
146 if (preserve) {
147 const char* str = getCStr();
148 if (str) {
149 oldValue = str;
150 }
151 }
152
153 enumArray.clear();
154 for (const auto& it : values) {
155 enumArray.push_back(std::make_shared<StringCopy>(it.c_str()));
156 }
157
158 // set _index
159 if (_index < 0) {
160 _index = 0;
161 }
162 if (preserve) {
163 setValue(oldValue);
164 }
165 }
166
167 void Enumeration::setValue(const char* value)
168 {
169 _index = 0;
170 for (std::size_t i = 0; i < enumArray.size(); i++) {
171 if (enumArray[i]->isEqual(value)) {
172 _index = static_cast<int>(i);
173 break;
174 }
175 }
176 }
177
178 void Enumeration::setValue(long value, bool checkRange)
179 {
180 if (value >= 0 && value < countItems()) {
181 _index = value;
182 }
183 else {
184 if (checkRange) {
185 throw Base::ValueError("Out of range");
186 }
187 else {
188 _index = value;
189 }
190 }
191 }
192
193 bool Enumeration::isValue(const char* value) const
194 {
195 int i = getInt();
196
197 if (i == -1) {
198 return false;
199 }
200 else {
201 return enumArray[i]->isEqual(value);
202 }
203 }
204
205 bool Enumeration::contains(const char* value) const
206 {
207 if (!isValid()) {
208 return false;
209 }
210
211 for (const auto& it : enumArray) {
212 if (it->isEqual(value)) {
213 return true;
214 }
215 }
216
217 return false;
218 }
219
220 const char* Enumeration::getCStr() const
221 {
222 if (!isValid() || _index < 0 || _index >= countItems()) {
223 return nullptr;
224 }
225
226 return enumArray[_index]->data();
227 }
228
229 int Enumeration::getInt() const
230 {
231 if (!isValid() || _index < 0 || _index >= countItems()) {
232 return -1;
233 }
234
235 return _index;
236 }
237
238 std::vector<std::string> Enumeration::getEnumVector() const
239 {
240 std::vector<std::string> list;
241 for (const auto& it : enumArray) {
242 list.emplace_back(it->data());
243 }
244 return list;
245 }
246
247 bool Enumeration::hasEnums() const
248 {
249 return (!enumArray.empty());
250 }
251
252 bool Enumeration::isValid() const
253 {
254 return (!enumArray.empty() && _index >= 0 && _index < countItems());
255 }
256
257 int Enumeration::maxValue() const
258 {
259 int num = -1;
260 if (!enumArray.empty()) {
261 num = static_cast<int>(enumArray.size()) - 1;
262 }
263 return num;
264 }
265
266 bool Enumeration::isCustom() const
267 {
268 for (const auto& it : enumArray) {
269 if (it->isCustom()) {
270 return true;
271 }
272 }
273 return false;
274 }
275
276 Enumeration& Enumeration::operator=(const Enumeration& other)
277 {
278 if (this == &other) {
279 return *this;
280 }
281
282 enumArray = other.enumArray;
283 _index = other._index;
284
285 return *this;
286 }
287
288 bool Enumeration::operator==(const Enumeration& other) const
289 {
290 if (_index != other._index || enumArray.size() != other.enumArray.size()) {
291 return false;
292 }
293 for (size_t i = 0; i < enumArray.size(); ++i) {
294 if (enumArray[i]->data() == other.enumArray[i]->data()) {
295 continue;
296 }
297 if (!enumArray[i]->data() || !other.enumArray[i]->data()) {
298 return false;
299 }
300 if (!enumArray[i]->isEqual(other.enumArray[i]->data())) {
301 return false;
302 }
303 }
304 return true;
305 }
306
307 bool Enumeration::operator==(const char* other) const
308 {
309 if (!getCStr()) {
310 return false;
311 }
312
313 return (strcmp(getCStr(), other) == 0);
314 }
315
316 int Enumeration::countItems() const
317 {
318 return static_cast<int>(enumArray.size());
319 }