1
/***************************************************************************
2
* Copyright (c) 2015 Ian Rees <ian.rees@gmail.com> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
29
#include <Base/Exception.h>
30
#include "Enumeration.h"
36
struct StringCopy : public Enumeration::Object {
37
explicit StringCopy(const char* str) : d(str) {
39
const char* data() const override {
42
bool isEqual(const char* str) const override {
45
bool isCustom() const override {
53
struct StringView : public Enumeration::Object {
54
explicit StringView(const char* str) : d(str) {
56
const char* data() const override {
59
bool isEqual(const char* str) const override {
62
bool isCustom() const override {
71
Enumeration::Enumeration()
76
Enumeration::Enumeration(const Enumeration &other)
78
enumArray = other.enumArray;
79
_index = other._index;
82
Enumeration::Enumeration(const char *valStr)
85
enumArray.push_back(std::make_shared<StringCopy>(valStr));
89
Enumeration::Enumeration(const char **list, const char *valStr)
92
while (list && *list) {
93
enumArray.push_back(std::make_shared<StringView>(*list));
99
Enumeration::~Enumeration()
104
void Enumeration::setEnums(const char **plEnums)
106
std::string oldValue;
107
bool preserve = (isValid() && plEnums != nullptr);
109
const char* str = getCStr();
115
while (plEnums && *plEnums) {
116
enumArray.push_back(std::make_shared<StringView>(*plEnums));
128
void Enumeration::setEnums(const std::vector<std::string> &values)
130
if (values.empty()) {
135
std::string oldValue;
136
bool preserve = isValid();
138
const char* str = getCStr();
144
for (const auto & it : values) {
145
enumArray.push_back(std::make_shared<StringCopy>(it.c_str()));
156
void Enumeration::setValue(const char *value)
159
for (std::size_t i = 0; i < enumArray.size(); i++) {
160
if (enumArray[i]->isEqual(value)) {
161
_index = static_cast<int>(i);
167
void Enumeration::setValue(long value, bool checkRange)
169
if (value >= 0 && value < countItems()) {
173
throw Base::ValueError("Out of range");
180
bool Enumeration::isValue(const char *value) const
187
return enumArray[i]->isEqual(value);
191
bool Enumeration::contains(const char *value) const
197
for (const auto& it : enumArray) {
198
if (it->isEqual(value))
205
const char * Enumeration::getCStr() const
207
if (!isValid() || _index < 0 || _index >= countItems()) {
211
return enumArray[_index]->data();
214
int Enumeration::getInt() const
216
if (!isValid() || _index < 0 || _index >= countItems()) {
223
std::vector<std::string> Enumeration::getEnumVector() const
225
std::vector<std::string> list;
226
for (const auto& it : enumArray)
227
list.emplace_back(it->data());
231
bool Enumeration::hasEnums() const
233
return (!enumArray.empty());
236
bool Enumeration::isValid() const
238
return (!enumArray.empty() && _index >= 0 && _index < countItems());
241
int Enumeration::maxValue() const
244
if (!enumArray.empty())
245
num = static_cast<int>(enumArray.size()) - 1;
249
bool Enumeration::isCustom() const
251
for (const auto& it : enumArray) {
258
Enumeration & Enumeration::operator=(const Enumeration &other)
263
enumArray = other.enumArray;
264
_index = other._index;
269
bool Enumeration::operator==(const Enumeration &other) const
271
if (_index != other._index || enumArray.size() != other.enumArray.size()) {
274
for (size_t i = 0; i < enumArray.size(); ++i) {
275
if (enumArray[i]->data() == other.enumArray[i]->data())
277
if (!enumArray[i]->data() || !other.enumArray[i]->data())
279
if (!enumArray[i]->isEqual(other.enumArray[i]->data()))
285
bool Enumeration::operator==(const char *other) const
291
return (strcmp(getCStr(), other) == 0);
294
int Enumeration::countItems() const
296
return static_cast<int>(enumArray.size());