]>
Commit | Line | Data |
---|---|---|
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. | |
9 | ||
10 | #ifndef OPTIONSET_H | |
11 | #define OPTIONSET_H | |
12 | ||
13 | #ifdef SCI_NAMESPACE | |
14 | namespace Scintilla { | |
15 | #endif | |
16 | ||
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; | |
29 | }; | |
30 | std::string description; | |
31 | Option() : | |
32 | opType(SC_TYPE_BOOLEAN), pb(0), description("") { | |
33 | } | |
34 | Option(plcob pb_, std::string description_="") : | |
35 | opType(SC_TYPE_BOOLEAN), pb(pb_), description(description_) { | |
36 | } | |
37 | Option(plcoi pi_, std::string description_) : | |
38 | opType(SC_TYPE_INTEGER), pi(pi_), description(description_) { | |
39 | } | |
40 | Option(plcos ps_, std::string description_) : | |
41 | opType(SC_TYPE_STRING), ps(ps_), description(description_) { | |
42 | } | |
43 | bool Set(T *base, const char *val) { | |
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; | |
50 | } | |
51 | break; | |
52 | } | |
53 | case SC_TYPE_INTEGER: { | |
54 | 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 | } | |
69 | return false; | |
70 | } | |
71 | }; | |
72 | typedef std::map<std::string, Option> OptionMap; | |
73 | OptionMap nameToDef; | |
74 | std::string names; | |
75 | std::string wordLists; | |
76 | ||
77 | void AppendName(const char *name) { | |
78 | if (!names.empty()) | |
79 | names += "\n"; | |
80 | names += name; | |
81 | } | |
82 | public: | |
83 | virtual ~OptionSet() { | |
84 | } | |
85 | void DefineProperty(const char *name, plcob pb, std::string description="") { | |
86 | nameToDef[name] = Option(pb, description); | |
87 | AppendName(name); | |
88 | } | |
89 | void DefineProperty(const char *name, plcoi pi, std::string description="") { | |
90 | nameToDef[name] = Option(pi, description); | |
91 | AppendName(name); | |
92 | } | |
93 | void DefineProperty(const char *name, plcos ps, std::string description="") { | |
94 | nameToDef[name] = Option(ps, description); | |
95 | AppendName(name); | |
96 | } | |
97 | const char *PropertyNames() { | |
98 | return names.c_str(); | |
99 | } | |
100 | int PropertyType(const char *name) { | |
101 | typename OptionMap::iterator it = nameToDef.find(name); | |
102 | if (it != nameToDef.end()) { | |
103 | return it->second.opType; | |
104 | } | |
105 | return SC_TYPE_BOOLEAN; | |
106 | } | |
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(); | |
111 | } | |
112 | return ""; | |
113 | } | |
114 | ||
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); | |
119 | } | |
120 | return false; | |
121 | } | |
122 | ||
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]; | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | const char *DescribeWordListSets() { | |
134 | return wordLists.c_str(); | |
135 | } | |
136 | }; | |
137 | ||
138 | #ifdef SCI_NAMESPACE | |
139 | } | |
140 | #endif | |
141 | ||
142 | #endif |