| 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 |
|
| 24 |
#ifndef BASE_ENUMERATION_H |
| 25 |
#define BASE_ENUMERATION_H |
| 26 |
|
| 27 |
#include <memory> |
| 28 |
#include <string> |
| 29 |
#include <vector> |
| 30 |
#include <FCGlobal.h> |
| 31 |
|
| 32 |
namespace App |
| 33 |
{ |
| 34 |
/// A bidirectional string-integer mapping |
| 35 |
/*! |
| 36 |
* This is mainly intended for two purposes: working around the difficulty |
| 37 |
* in C++ of sharing enumerations between different source files, |
| 38 |
* namespaces, etc. and as the data type stored by App::PropertyEnumeration |
| 39 |
* |
| 40 |
* Internally, Enumeration maintains |
| 41 |
* -# Either a const pointer to an array of C-style strings, or a vector |
| 42 |
* of C++ std::strings |
| 43 |
* -# An integer index into that array/vector representing the string |
| 44 |
* representing the instance's value. |
| 45 |
* |
| 46 |
* If built with FC_DEBUG defined, some boundaries of passed in pointers |
| 47 |
* will be checked. Otherwise, the caller has the responsibility of |
| 48 |
* checking the limits of given indices. |
| 49 |
* |
| 50 |
* \todo Implement lazy copy |
| 51 |
*/ |
| 52 |
class AppExport Enumeration |
| 53 |
{ |
| 54 |
public: |
| 55 |
class Object |
| 56 |
{ |
| 57 |
public: |
| 58 |
virtual ~Object() = default; |
| 59 |
virtual const char* data() const = 0; |
| 60 |
virtual bool isEqual(const char*) const = 0; |
| 61 |
virtual bool isCustom() const = 0; |
| 62 |
}; |
| 63 |
|
| 64 |
public: |
| 65 |
/// Constructs an empty Enumeration object |
| 66 |
Enumeration(); |
| 67 |
|
| 68 |
/// Standard copy constructor |
| 69 |
Enumeration(const Enumeration& other); |
| 70 |
|
| 71 |
/// Constructs an Enumeration with a single element |
| 72 |
explicit Enumeration(const char* valStr); |
| 73 |
|
| 74 |
/// Constructs an Enumeration using val within list |
| 75 |
Enumeration(const char** list, const char* valStr); |
| 76 |
|
| 77 |
/// Standard destructor |
| 78 |
~Enumeration(); |
| 79 |
|
| 80 |
/** Sets the enumeration string list |
| 81 |
* The list is a NULL terminated array of pointers to const |
| 82 |
* char* strings. |
| 83 |
* \code |
| 84 |
* const char enums[] = {"Black","White","Other",NULL} |
| 85 |
* \endcode |
| 86 |
* |
| 87 |
* If Enumeration was already valid, will attempt to preserve |
| 88 |
* the string-representation value of the Enumeration |
| 89 |
* |
| 90 |
* Enumeration does not take ownership of the passed object |
| 91 |
*/ |
| 92 |
void setEnums(const char** plEnums); |
| 93 |
|
| 94 |
/// Set all enum values as vector of strings |
| 95 |
/*! |
| 96 |
* This method causes the Enumeration to dynamically allocate |
| 97 |
* it's own array of C Strings, which will be deleted by the |
| 98 |
* destructor or subsequent calls to setEnums(). So, it is |
| 99 |
* important to make sure the Enumeration stays in scope as |
| 100 |
* long as values returned by getCStr are in use. |
| 101 |
* |
| 102 |
* If Enumeration was already valid, will attempt to preserve |
| 103 |
* the string-representation value of the Enumeration |
| 104 |
*/ |
| 105 |
void setEnums(const std::vector<std::string>& values); |
| 106 |
|
| 107 |
/// Set the enum using a C string |
| 108 |
void setValue(const char* value); |
| 109 |
|
| 110 |
/// Overload of setValue(const char *value) |
| 111 |
void setValue(const std::string& value) |
| 112 |
{ |
| 113 |
setValue(value.c_str()); |
| 114 |
} |
| 115 |
|
| 116 |
/// Set the enum using a long |
| 117 |
/*! |
| 118 |
* if checkRange is set to true, throws Base::ValueError when |
| 119 |
* values are set out of range |
| 120 |
* |
| 121 |
* Checks for boundaries via assert() |
| 122 |
*/ |
| 123 |
void setValue(long value, bool checkRange = false); |
| 124 |
|
| 125 |
/// Checks if the property is set to a certain string value |
| 126 |
bool isValue(const char* value) const; |
| 127 |
|
| 128 |
/// Checks if a string is included in the enumeration |
| 129 |
bool contains(const char* value) const; |
| 130 |
|
| 131 |
/// Return the value as C string |
| 132 |
/*! |
| 133 |
* Returns NULL if the enumeration is invalid. |
| 134 |
*/ |
| 135 |
const char* getCStr() const; |
| 136 |
|
| 137 |
/// Return value as integer |
| 138 |
/*! |
| 139 |
* Returns -1 if the Enumeration isn't valid |
| 140 |
*/ |
| 141 |
int getInt() const; |
| 142 |
|
| 143 |
/// get all possible enum values as vector of strings |
| 144 |
std::vector<std::string> getEnumVector() const; |
| 145 |
|
| 146 |
/// returns true if the enum list is non-empty, false otherwise |
| 147 |
bool hasEnums() const; |
| 148 |
|
| 149 |
/// Returns true if the instance is in a usable state |
| 150 |
bool isValid() const; |
| 151 |
|
| 152 |
/// Returns the highest usable integer value for this enum |
| 153 |
/*! |
| 154 |
* Returns -1 if the enumeration is not valid according to isValid() |
| 155 |
*/ |
| 156 |
int maxValue() const; |
| 157 |
|
| 158 |
/// Returns true if any of the items is a user-defined string |
| 159 |
bool isCustom() const; |
| 160 |
|
| 161 |
/// Assignment operator |
| 162 |
Enumeration& operator=(const Enumeration& other); |
| 163 |
|
| 164 |
/// true iff our string representation matches other's |
| 165 |
/*! |
| 166 |
* Returns false if either Enumeration is not valid. |
| 167 |
*/ |
| 168 |
bool operator==(const Enumeration& other) const; |
| 169 |
|
| 170 |
/// true iff our string representation matches other |
| 171 |
/*! |
| 172 |
* Returns false if Enumeration is not valid. |
| 173 |
*/ |
| 174 |
bool operator==(const char* other) const; |
| 175 |
|
| 176 |
protected: |
| 177 |
/// Number of items |
| 178 |
int countItems() const; |
| 179 |
|
| 180 |
private: |
| 181 |
/// Handle to C Strings of possible enumeration values |
| 182 |
using ObjectPtr = std::shared_ptr<Object>; |
| 183 |
std::vector<ObjectPtr> enumArray; |
| 184 |
|
| 185 |
/// Integer value of the enumeration |
| 186 |
/*! |
| 187 |
* This serves as an index into enumArray to get the string |
| 188 |
* representation. |
| 189 |
*/ |
| 190 |
int _index; |
| 191 |
|
| 192 |
friend class PropertyEnumeration; |
| 193 |
}; // class Enumeration |
| 194 |
} // namespace App |
| 195 |
|
| 196 |
#endif // #ifndef BASE_ENUMERATION_H |