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
|
// Scintilla source code edit control
/** @file OptionSet.h
** Manage descriptive information about an options struct for a lexer.
** Hold the names, positions, and descriptions of boolean, integer and string options and
** allow setting options and retrieving metadata about the options.
**/
// Copyright 2010 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#ifndef OPTIONSET_H
#define OPTIONSET_H
namespace Lexilla {
template <typename T>
class OptionSet {
typedef T Target;
typedef bool T::*plcob;
typedef int T::*plcoi;
typedef std::string T::*plcos;
struct Option {
int opType;
union {
plcob pb;
plcoi pi;
plcos ps;
};
std::string value;
std::string description;
Option() :
opType(SC_TYPE_BOOLEAN), pb(nullptr) {
}
Option(plcob pb_, std::string_view description_="") :
opType(SC_TYPE_BOOLEAN), pb(pb_), description(description_) {
}
Option(plcoi pi_, std::string_view description_) :
opType(SC_TYPE_INTEGER), pi(pi_), description(description_) {
}
Option(plcos ps_, std::string_view description_) :
opType(SC_TYPE_STRING), ps(ps_), description(description_) {
}
bool Set(T *base, const char *val) {
value = val;
switch (opType) {
case SC_TYPE_BOOLEAN: {
const bool option = atoi(val) != 0;
if ((*base).*pb != option) {
(*base).*pb = option;
return true;
}
break;
}
case SC_TYPE_INTEGER: {
const int option = atoi(val);
if ((*base).*pi != option) {
(*base).*pi = option;
return true;
}
break;
}
case SC_TYPE_STRING: {
if ((*base).*ps != val) {
(*base).*ps = val;
return true;
}
break;
}
default:
break;
}
return false;
}
const char *Get() const noexcept {
return value.c_str();
}
};
typedef std::map<std::string, Option, std::less<>> OptionMap;
OptionMap nameToDef;
std::string names;
std::string wordLists;
void AppendName(const char *name) {
if (!names.empty())
names += "\n";
names += name;
}
public:
void DefineProperty(const char *name, plcob pb, std::string_view description="") {
nameToDef[name] = Option(pb, description);
AppendName(name);
}
void DefineProperty(const char *name, plcoi pi, std::string_view description="") {
nameToDef[name] = Option(pi, description);
AppendName(name);
}
void DefineProperty(const char *name, plcos ps, std::string_view description="") {
nameToDef[name] = Option(ps, description);
AppendName(name);
}
template <typename E>
void DefineProperty(const char *name, E T::*pe, std::string_view description="") {
static_assert(std::is_enum<E>::value);
plcoi pi {};
static_assert(sizeof(pe) == sizeof(pi));
memcpy(&pi, &pe, sizeof(pe));
nameToDef[name] = Option(pi, description);
AppendName(name);
}
const char *PropertyNames() const noexcept {
return names.c_str();
}
int PropertyType(const char *name) const {
typename OptionMap::const_iterator const it = nameToDef.find(name);
if (it != nameToDef.end()) {
return it->second.opType;
}
return SC_TYPE_BOOLEAN;
}
const char *DescribeProperty(const char *name) const {
typename OptionMap::const_iterator const it = nameToDef.find(name);
if (it != nameToDef.end()) {
return it->second.description.c_str();
}
return "";
}
bool PropertySet(T *base, const char *name, const char *val) {
typename OptionMap::iterator const it = nameToDef.find(name);
if (it != nameToDef.end()) {
return it->second.Set(base, val);
}
return false;
}
const char *PropertyGet(const char *name) const {
typename OptionMap::const_iterator const it = nameToDef.find(name);
if (it != nameToDef.end()) {
return it->second.Get();
}
return nullptr;
}
void DefineWordListSets(const char * const wordListDescriptions[]) {
if (wordListDescriptions) {
for (size_t wl = 0; wordListDescriptions[wl]; wl++) {
if (wl > 0)
wordLists += "\n";
wordLists += wordListDescriptions[wl];
}
}
}
const char *DescribeWordListSets() const noexcept {
return wordLists.c_str();
}
};
}
#endif
|