Recognize .exp (Expect) files as Tcl
[geany-mirror.git] / scintilla / lexlib / OptionSet.h
blob2935a20897ee3726db0c535c1501662f56a00025
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 <neilh@scintilla.org>
8 // The License.txt file describes the conditions under which this software may be distributed.
10 #ifndef OPTIONSET_H
11 #define OPTIONSET_H
13 #ifdef SCI_NAMESPACE
14 namespace Scintilla {
15 #endif
17 template <typename T>
18 class OptionSet {
19 typedef T Target;
20 typedef bool T::*plcob;
21 typedef int T::*plcoi;
22 typedef std::string T::*plcos;
23 struct Option {
24 int opType;
25 union {
26 plcob pb;
27 plcoi pi;
28 plcos ps;
30 std::string description;
31 Option() :
32 opType(SC_TYPE_BOOLEAN), pb(0), description("") {
34 Option(plcob pb_, std::string description_="") :
35 opType(SC_TYPE_BOOLEAN), pb(pb_), description(description_) {
37 Option(plcoi pi_, std::string description_) :
38 opType(SC_TYPE_INTEGER), pi(pi_), description(description_) {
40 Option(plcos ps_, std::string description_) :
41 opType(SC_TYPE_STRING), ps(ps_), description(description_) {
43 bool Set(T *base, const char *val) const {
44 switch (opType) {
45 case SC_TYPE_BOOLEAN: {
46 bool option = atoi(val) != 0;
47 if ((*base).*pb != option) {
48 (*base).*pb = option;
49 return true;
51 break;
53 case SC_TYPE_INTEGER: {
54 int option = atoi(val);
55 if ((*base).*pi != option) {
56 (*base).*pi = option;
57 return true;
59 break;
61 case SC_TYPE_STRING: {
62 if ((*base).*ps != val) {
63 (*base).*ps = val;
64 return true;
66 break;
69 return false;
72 typedef std::map<std::string, Option> OptionMap;
73 OptionMap nameToDef;
74 std::string names;
75 std::string wordLists;
77 void AppendName(const char *name) {
78 if (!names.empty())
79 names += "\n";
80 names += name;
82 public:
83 virtual ~OptionSet() {
85 void DefineProperty(const char *name, plcob pb, std::string description="") {
86 nameToDef[name] = Option(pb, description);
87 AppendName(name);
89 void DefineProperty(const char *name, plcoi pi, std::string description="") {
90 nameToDef[name] = Option(pi, description);
91 AppendName(name);
93 void DefineProperty(const char *name, plcos ps, std::string description="") {
94 nameToDef[name] = Option(ps, description);
95 AppendName(name);
97 const char *PropertyNames() const {
98 return names.c_str();
100 int PropertyType(const char *name) {
101 typename OptionMap::iterator it = nameToDef.find(name);
102 if (it != nameToDef.end()) {
103 return it->second.opType;
105 return SC_TYPE_BOOLEAN;
107 const char *DescribeProperty(const char *name) {
108 typename OptionMap::iterator it = nameToDef.find(name);
109 if (it != nameToDef.end()) {
110 return it->second.description.c_str();
112 return "";
115 bool PropertySet(T *base, const char *name, const char *val) {
116 typename OptionMap::iterator it = nameToDef.find(name);
117 if (it != nameToDef.end()) {
118 return it->second.Set(base, val);
120 return false;
123 void DefineWordListSets(const char * const wordListDescriptions[]) {
124 if (wordListDescriptions) {
125 for (size_t wl = 0; wordListDescriptions[wl]; wl++) {
126 if (!wordLists.empty())
127 wordLists += "\n";
128 wordLists += wordListDescriptions[wl];
133 const char *DescribeWordListSets() const {
134 return wordLists.c_str();
138 #ifdef SCI_NAMESPACE
140 #endif
142 #endif