OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SK_FLAGS_H | 8 #ifndef SK_FLAGS_H |
9 #define SK_FLAGS_H | 9 #define SK_FLAGS_H |
10 | 10 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // For access to gHead. | 112 // For access to gHead. |
113 friend class SkFlagInfo; | 113 friend class SkFlagInfo; |
114 }; | 114 }; |
115 | 115 |
116 #define TO_STRING2(s) #s | 116 #define TO_STRING2(s) #s |
117 #define TO_STRING(s) TO_STRING2(s) | 117 #define TO_STRING(s) TO_STRING2(s) |
118 | 118 |
119 #define DEFINE_bool(name, defaultValue, helpString) \ | 119 #define DEFINE_bool(name, defaultValue, helpString) \ |
120 bool FLAGS_##name; \ | 120 bool FLAGS_##name; \ |
121 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ | 121 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
122 NULL, \ | |
123 &FLAGS_##name, \ | 122 &FLAGS_##name, \ |
124 defaultValue, \ | 123 defaultValue, \ |
125 helpString) | 124 helpString) |
126 | |
127 // bool 2 allows specifying a short name. No check is done to ensure that shortN
ame | |
128 // is actually shorter than name. | |
129 #define DEFINE_bool2(name, shortName, defaultValue, helpString) \ | |
130 bool FLAGS_##name; \ | |
131 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ | |
132 TO_STRING(shortName),\ | |
133 &FLAGS_##name, \ | |
134 defaultValue, \ | |
135 helpString) | |
136 | 125 |
137 #define DECLARE_bool(name) extern bool FLAGS_##name; | 126 #define DECLARE_bool(name) extern bool FLAGS_##name; |
138 | 127 |
139 #define DEFINE_string(name, defaultValue, helpString) \ | 128 #define DEFINE_string(name, defaultValue, helpString) \ |
140 SkTDArray<const char*> FLAGS_##name; \ | 129 SkTDArray<const char*> FLAGS_##name; \ |
141 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ | 130 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
142 NULL, \ | |
143 &FLAGS_##name, \ | 131 &FLAGS_##name, \ |
144 defaultValue, \ | 132 defaultValue, \ |
145 helpString) | 133 helpString) |
146 | 134 |
147 // string2 allows specifying a short name. No check is done to ensure that short
Name | |
148 // is actually shorter than name. | |
149 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ | |
150 SkTDArray<const char*> FLAGS_##name;
\ | |
151 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name),
\ | |
152 TO_STRING(shortName),
\ | |
153 &FLAGS_##name,
\ | |
154 defaultValue,
\ | |
155 helpString) | |
156 | |
157 #define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name; | 135 #define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name; |
158 | 136 |
159 #define DEFINE_int32(name, defaultValue, helpString) \ | 137 #define DEFINE_int32(name, defaultValue, helpString) \ |
160 int32_t FLAGS_##name; \ | 138 int32_t FLAGS_##name; \ |
161 static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ | 139 static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
162 &FLAGS_##name, \ | 140 &FLAGS_##name, \ |
163 defaultValue, \ | 141 defaultValue, \ |
164 helpString) | 142 helpString) |
165 | 143 |
166 #define DECLARE_int32(name) extern int32_t FLAGS_##name; | 144 #define DECLARE_int32(name) extern int32_t FLAGS_##name; |
(...skipping 11 matching lines...) Expand all Loading... |
178 | 156 |
179 public: | 157 public: |
180 enum FlagTypes { | 158 enum FlagTypes { |
181 kBool_FlagType, | 159 kBool_FlagType, |
182 kString_FlagType, | 160 kString_FlagType, |
183 kInt_FlagType, | 161 kInt_FlagType, |
184 kDouble_FlagType, | 162 kDouble_FlagType, |
185 }; | 163 }; |
186 | 164 |
187 // Create flags of the desired type, and append to the list. | 165 // Create flags of the desired type, and append to the list. |
188 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, | 166 static bool CreateBoolFlag(const char* name, bool* pBool, |
189 bool defaultValue, const char* helpString) { | 167 bool defaultValue, const char* helpString) { |
190 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kBool_FlagType, helpStr
ing)); | 168 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kBool_FlagType, helpStr
ing)); |
191 info->fShortName.set(shortName); | |
192 info->fBoolValue = pBool; | 169 info->fBoolValue = pBool; |
193 *info->fBoolValue = info->fDefaultBool = defaultValue; | 170 *info->fBoolValue = info->fDefaultBool = defaultValue; |
194 return true; | 171 return true; |
195 } | 172 } |
196 | 173 |
197 static bool CreateStringFlag(const char* name, const char* shortName, | 174 static bool CreateStringFlag(const char* name, SkTDArray<const char*>* pStri
ngs, |
198 SkTDArray<const char*>* pStrings, | |
199 const char* defaultValue, const char* helpStrin
g) { | 175 const char* defaultValue, const char* helpStrin
g) { |
200 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kString_FlagType, helpS
tring)); | 176 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kString_FlagType, helpS
tring)); |
201 info->fShortName.set(shortName); | |
202 info->fDefaultString.set(defaultValue); | 177 info->fDefaultString.set(defaultValue); |
203 | 178 |
204 info->fStrings = pStrings; | 179 info->fStrings = pStrings; |
205 info->fStrings->reset(); | 180 info->fStrings->reset(); |
206 // If default is "", leave the array empty. | 181 // If default is "", leave the array empty. |
207 if (info->fDefaultString.size() > 0) { | 182 if (info->fDefaultString.size() > 0) { |
208 info->fStrings->append(1, &defaultValue); | 183 info->fStrings->append(1, &defaultValue); |
209 } | 184 } |
210 return true; | 185 return true; |
211 } | 186 } |
(...skipping 12 matching lines...) Expand all Loading... |
224 info->fDoubleValue = pDouble; | 199 info->fDoubleValue = pDouble; |
225 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 200 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
226 return true; | 201 return true; |
227 } | 202 } |
228 | 203 |
229 /** | 204 /** |
230 * Returns true if the string matches this flag. For a bool, also sets the | 205 * Returns true if the string matches this flag. For a bool, also sets the |
231 * value, since a bool is specified as true or false by --name or --noname. | 206 * value, since a bool is specified as true or false by --name or --noname. |
232 */ | 207 */ |
233 bool match(const char* string) { | 208 bool match(const char* string) { |
234 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | 209 if (SkStrStartsWith(string, '-')) { |
235 string++; | 210 string++; |
236 // Allow one or two dashes | 211 // Allow one or two dashes |
237 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | 212 if (SkStrStartsWith(string, '-')) { |
238 string++; | 213 string++; |
239 } | 214 } |
240 if (kBool_FlagType == fFlagType) { | 215 if (kBool_FlagType == fFlagType) { |
241 // In this case, go ahead and set the value. | 216 // In this case, go ahead and set the value. |
242 if (fName.equals(string) || fShortName.equals(string)) { | 217 if (fName.equals(string)) { |
243 *fBoolValue = true; | 218 *fBoolValue = true; |
244 return true; | 219 return true; |
245 } | 220 } |
246 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { | 221 SkString noname(fName); |
247 string += 2; | 222 noname.prepend("no"); |
248 if (fName.equals(string) || fShortName.equals(string)) { | 223 if (noname.equals(string)) { |
249 *fBoolValue = false; | 224 *fBoolValue = false; |
250 return true; | 225 return true; |
251 } | |
252 return false; | |
253 } | 226 } |
| 227 return false; |
254 } | 228 } |
255 return fName.equals(string) || fShortName.equals(string); | 229 return fName.equals(string); |
256 } else { | 230 } else { |
257 // Has no dash | 231 // Has no dash |
258 return false; | 232 return false; |
259 } | 233 } |
260 return false; | 234 return false; |
261 } | 235 } |
262 | 236 |
263 FlagTypes getFlagType() const { return fFlagType; } | 237 FlagTypes getFlagType() const { return fFlagType; } |
264 | 238 |
265 void resetStrings() { | 239 void resetStrings() { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 , fIntValue(NULL) | 320 , fIntValue(NULL) |
347 , fDefaultInt(0) | 321 , fDefaultInt(0) |
348 , fDoubleValue(NULL) | 322 , fDoubleValue(NULL) |
349 , fDefaultDouble(0) | 323 , fDefaultDouble(0) |
350 , fStrings(NULL) { | 324 , fStrings(NULL) { |
351 fNext = SkFlags::gHead; | 325 fNext = SkFlags::gHead; |
352 SkFlags::gHead = this; | 326 SkFlags::gHead = this; |
353 } | 327 } |
354 // Name of the flag, without initial dashes | 328 // Name of the flag, without initial dashes |
355 SkString fName; | 329 SkString fName; |
356 SkString fShortName; | |
357 FlagTypes fFlagType; | 330 FlagTypes fFlagType; |
358 SkString fHelpString; | 331 SkString fHelpString; |
359 bool* fBoolValue; | 332 bool* fBoolValue; |
360 bool fDefaultBool; | 333 bool fDefaultBool; |
361 int32_t* fIntValue; | 334 int32_t* fIntValue; |
362 int32_t fDefaultInt; | 335 int32_t fDefaultInt; |
363 double* fDoubleValue; | 336 double* fDoubleValue; |
364 double fDefaultDouble; | 337 double fDefaultDouble; |
365 SkTDArray<const char*>* fStrings; | 338 SkTDArray<const char*>* fStrings; |
366 // Both for the help string and in case fStrings is empty. | 339 // Both for the help string and in case fStrings is empty. |
367 SkString fDefaultString; | 340 SkString fDefaultString; |
368 | 341 |
369 // In order to keep a linked list. | 342 // In order to keep a linked list. |
370 SkFlagInfo* fNext; | 343 SkFlagInfo* fNext; |
371 }; | 344 }; |
372 #endif // SK_FLAGS_H | 345 #endif // SK_FLAGS_H |
OLD | NEW |