| 1 |
// Scintilla source code edit control |
| 2 |
/** @file OptionSet.h |
| 3 |
** Manage descriptive information about an options struct for a lexer. |
| 4 |
** Hold the names, positions, and descriptions of boolean, integer and string options and |
| 5 |
** allow setting options and retrieving metadata about the options. |
| 6 |
**/ |
| 7 |
// Copyright 2010 by Neil Hodgson <[email protected]> |
| 8 |
// The License.txt file describes the conditions under which this software may be distributed. |
| 9 |
|
| 10 |
#ifndef OPTIONSET_H |
| 11 |
#define OPTIONSET_H |
| 12 |
|
| 13 |
namespace Lexilla { |
| 14 |
|
| 15 |
template <typename T> |
| 16 |
class OptionSet { |
| 17 |
typedef T Target; |
| 18 |
typedef bool T::*plcob; |
| 19 |
typedef int T::*plcoi; |
| 20 |
typedef std::string T::*plcos; |
| 21 |
struct Option { |
| 22 |
int opType; |
| 23 |
union { |
| 24 |
plcob pb; |
| 25 |
plcoi pi; |
| 26 |
plcos ps; |
| 27 |
}; |
| 28 |
std::string value; |
| 29 |
std::string description; |
| 30 |
Option() : |
| 31 |
opType(SC_TYPE_BOOLEAN), pb(nullptr) { |
| 32 |
} |
| 33 |
Option(plcob pb_, std::string_view description_="") : |
| 34 |
opType(SC_TYPE_BOOLEAN), pb(pb_), description(description_) { |
| 35 |
} |
| 36 |
Option(plcoi pi_, std::string_view description_) : |
| 37 |
opType(SC_TYPE_INTEGER), pi(pi_), description(description_) { |
| 38 |
} |
| 39 |
Option(plcos ps_, std::string_view description_) : |
| 40 |
opType(SC_TYPE_STRING), ps(ps_), description(description_) { |
| 41 |
} |
| 42 |
bool Set(T *base, const char *val) { |
| 43 |
value = val; |
| 44 |
switch (opType) { |
| 45 |
case SC_TYPE_BOOLEAN: { |
| 46 |
const bool option = atoi(val) != 0; |
| 47 |
if ((*base).*pb != option) { |
| 48 |
(*base).*pb = option; |
| 49 |
return true; |
| 50 |
} |
| 51 |
break; |
| 52 |
} |
| 53 |
case SC_TYPE_INTEGER: { |
| 54 |
const int option = atoi(val); |
| 55 |
if ((*base).*pi != option) { |
| 56 |
(*base).*pi = option; |
| 57 |
return true; |
| 58 |
} |
| 59 |
break; |
| 60 |
} |
| 61 |
case SC_TYPE_STRING: { |
| 62 |
if ((*base).*ps != val) { |
| 63 |
(*base).*ps = val; |
| 64 |
return true; |
| 65 |
} |
| 66 |
break; |
| 67 |
} |
| 68 |
default: |
| 69 |
break; |
| 70 |
} |
| 71 |
return false; |
| 72 |
} |
| 73 |
const char *Get() const noexcept { |
| 74 |
return value.c_str(); |
| 75 |
} |
| 76 |
}; |
| 77 |
typedef std::map<std::string, Option, std::less<>> OptionMap; |
| 78 |
OptionMap nameToDef; |
| 79 |
std::string names; |
| 80 |
std::string wordLists; |
| 81 |
|
| 82 |
void AppendName(const char *name) { |
| 83 |
if (!names.empty()) |
| 84 |
names += "\n"; |
| 85 |
names += name; |
| 86 |
} |
| 87 |
public: |
| 88 |
void DefineProperty(const char *name, plcob pb, std::string_view description="") { |
| 89 |
nameToDef[name] = Option(pb, description); |
| 90 |
AppendName(name); |
| 91 |
} |
| 92 |
void DefineProperty(const char *name, plcoi pi, std::string_view description="") { |
| 93 |
nameToDef[name] = Option(pi, description); |
| 94 |
AppendName(name); |
| 95 |
} |
| 96 |
void DefineProperty(const char *name, plcos ps, std::string_view description="") { |
| 97 |
nameToDef[name] = Option(ps, description); |
| 98 |
AppendName(name); |
| 99 |
} |
| 100 |
template <typename E> |
| 101 |
void DefineProperty(const char *name, E T::*pe, std::string_view description="") { |
| 102 |
static_assert(std::is_enum<E>::value); |
| 103 |
plcoi pi {}; |
| 104 |
static_assert(sizeof(pe) == sizeof(pi)); |
| 105 |
memcpy(&pi, &pe, sizeof(pe)); |
| 106 |
nameToDef[name] = Option(pi, description); |
| 107 |
AppendName(name); |
| 108 |
} |
| 109 |
const char *PropertyNames() const noexcept { |
| 110 |
return names.c_str(); |
| 111 |
} |
| 112 |
int PropertyType(const char *name) const { |
| 113 |
typename OptionMap::const_iterator const it = nameToDef.find(name); |
| 114 |
if (it != nameToDef.end()) { |
| 115 |
return it->second.opType; |
| 116 |
} |
| 117 |
return SC_TYPE_BOOLEAN; |
| 118 |
} |
| 119 |
const char *DescribeProperty(const char *name) const { |
| 120 |
typename OptionMap::const_iterator const it = nameToDef.find(name); |
| 121 |
if (it != nameToDef.end()) { |
| 122 |
return it->second.description.c_str(); |
| 123 |
} |
| 124 |
return ""; |
| 125 |
} |
| 126 |
|
| 127 |
bool PropertySet(T *base, const char *name, const char *val) { |
| 128 |
typename OptionMap::iterator const it = nameToDef.find(name); |
| 129 |
if (it != nameToDef.end()) { |
| 130 |
return it->second.Set(base, val); |
| 131 |
} |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
const char *PropertyGet(const char *name) const { |
| 136 |
typename OptionMap::const_iterator const it = nameToDef.find(name); |
| 137 |
if (it != nameToDef.end()) { |
| 138 |
return it->second.Get(); |
| 139 |
} |
| 140 |
return nullptr; |
| 141 |
} |
| 142 |
|
| 143 |
void DefineWordListSets(const char * const wordListDescriptions[]) { |
| 144 |
if (wordListDescriptions) { |
| 145 |
for (size_t wl = 0; wordListDescriptions[wl]; wl++) { |
| 146 |
if (wl > 0) |
| 147 |
wordLists += "\n"; |
| 148 |
wordLists += wordListDescriptions[wl]; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
const char *DescribeWordListSets() const noexcept { |
| 154 |
return wordLists.c_str(); |
| 155 |
} |
| 156 |
}; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
#endif |