| 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 19 matching lines...) Expand all Loading... |
| 30 * For example, the line | 30 * For example, the line |
| 31 * | 31 * |
| 32 * DEFINE_bool(boolean, false, "The variable boolean does such and such"); | 32 * DEFINE_bool(boolean, false, "The variable boolean does such and such"); |
| 33 * | 33 * |
| 34 * will create the following variable: | 34 * will create the following variable: |
| 35 * | 35 * |
| 36 * bool FLAGS_boolean; | 36 * bool FLAGS_boolean; |
| 37 * | 37 * |
| 38 * which will initially be set to false, and can be set to true by using the | 38 * which will initially be set to false, and can be set to true by using the |
| 39 * flag "--boolean" on the commandline. "--noboolean" will set FLAGS_boolean | 39 * flag "--boolean" on the commandline. "--noboolean" will set FLAGS_boolean |
| 40 * to false. (Single dashes are also permitted for this and other flags.) The | 40 * to false. FLAGS_boolean can also be set using "--boolean=true" or |
| 41 * helpString will be printed if the help flag (-h or -help) is used. | 41 * "--boolean true" (where "true" can be replaced by "false", "TRUE", "FALSE", |
| 42 * "1" or "0"). |
| 43 * |
| 44 * Single dashes are also permitted for this and other flags. |
| 45 * |
| 46 * The helpString will be printed if the help flag (-h or -help) is used. |
| 42 * | 47 * |
| 43 * Similarly, the line | 48 * Similarly, the line |
| 44 * | 49 * |
| 45 * DEFINE_int32(integer, .., ..); | 50 * DEFINE_int32(integer, .., ..); |
| 46 * | 51 * |
| 47 * will create | 52 * will create |
| 48 * | 53 * |
| 49 * int32_t FLAGS_integer; | 54 * int32_t FLAGS_integer; |
| 50 * | 55 * |
| 51 * and | 56 * and |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 | 225 |
| 221 static bool CreateDoubleFlag(const char* name, double* pDouble, | 226 static bool CreateDoubleFlag(const char* name, double* pDouble, |
| 222 double defaultValue, const char* helpString) { | 227 double defaultValue, const char* helpString) { |
| 223 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kDouble_FlagType, helpS
tring)); | 228 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kDouble_FlagType, helpS
tring)); |
| 224 info->fDoubleValue = pDouble; | 229 info->fDoubleValue = pDouble; |
| 225 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 230 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
| 226 return true; | 231 return true; |
| 227 } | 232 } |
| 228 | 233 |
| 229 /** | 234 /** |
| 230 * Returns true if the string matches this flag. For a bool, also sets the | 235 * Returns true if the string matches this flag. |
| 231 * value, since a bool is specified as true or false by --name or --noname. | 236 * For a boolean flag, also sets the value, since a boolean flag can be set
in a number of ways |
| 237 * without looking at the following string: |
| 238 * --name |
| 239 * --noname |
| 240 * --name=true |
| 241 * --name=false |
| 242 * --name=1 |
| 243 * --name=0 |
| 244 * --name=TRUE |
| 245 * --name=FALSE |
| 232 */ | 246 */ |
| 233 bool match(const char* string) { | 247 bool match(const char* string); |
| 234 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | |
| 235 string++; | |
| 236 // Allow one or two dashes | |
| 237 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { | |
| 238 string++; | |
| 239 } | |
| 240 if (kBool_FlagType == fFlagType) { | |
| 241 // In this case, go ahead and set the value. | |
| 242 if (fName.equals(string) || fShortName.equals(string)) { | |
| 243 *fBoolValue = true; | |
| 244 return true; | |
| 245 } | |
| 246 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { | |
| 247 string += 2; | |
| 248 if (fName.equals(string) || fShortName.equals(string)) { | |
| 249 *fBoolValue = false; | |
| 250 return true; | |
| 251 } | |
| 252 return false; | |
| 253 } | |
| 254 } | |
| 255 return fName.equals(string) || fShortName.equals(string); | |
| 256 } else { | |
| 257 // Has no dash | |
| 258 return false; | |
| 259 } | |
| 260 return false; | |
| 261 } | |
| 262 | 248 |
| 263 FlagTypes getFlagType() const { return fFlagType; } | 249 FlagTypes getFlagType() const { return fFlagType; } |
| 264 | 250 |
| 265 void resetStrings() { | 251 void resetStrings() { |
| 266 if (kString_FlagType == fFlagType) { | 252 if (kString_FlagType == fFlagType) { |
| 267 fStrings->reset(); | 253 fStrings->reset(); |
| 268 } else { | 254 } else { |
| 269 SkASSERT(!"Can only call resetStrings on kString_FlagType"); | 255 SkASSERT(!"Can only call resetStrings on kString_FlagType"); |
| 270 } | 256 } |
| 271 } | 257 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 287 } | 273 } |
| 288 | 274 |
| 289 void setDouble(double value) { | 275 void setDouble(double value) { |
| 290 if (kDouble_FlagType == fFlagType) { | 276 if (kDouble_FlagType == fFlagType) { |
| 291 *fDoubleValue = value; | 277 *fDoubleValue = value; |
| 292 } else { | 278 } else { |
| 293 SkASSERT(!"Can only call setDouble on kDouble_FlagType"); | 279 SkASSERT(!"Can only call setDouble on kDouble_FlagType"); |
| 294 } | 280 } |
| 295 } | 281 } |
| 296 | 282 |
| 283 void setBool(bool value) { |
| 284 if (kBool_FlagType == fFlagType) { |
| 285 *fBoolValue = value; |
| 286 } else { |
| 287 SkASSERT(!"Can only call setBool on kBool_FlagType"); |
| 288 } |
| 289 } |
| 290 |
| 297 SkFlagInfo* next() { return fNext; } | 291 SkFlagInfo* next() { return fNext; } |
| 298 | 292 |
| 299 const SkString& name() const { return fName; } | 293 const SkString& name() const { return fName; } |
| 300 | 294 |
| 301 const SkString& shortName() const { return fShortName; } | 295 const SkString& shortName() const { return fShortName; } |
| 302 | 296 |
| 303 const SkString& help() const { return fHelpString; } | 297 const SkString& help() const { return fHelpString; } |
| 304 | 298 |
| 305 SkString defaultValue() const { | 299 SkString defaultValue() const { |
| 306 SkString result; | 300 SkString result; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 double* fDoubleValue; | 359 double* fDoubleValue; |
| 366 double fDefaultDouble; | 360 double fDefaultDouble; |
| 367 SkTDArray<const char*>* fStrings; | 361 SkTDArray<const char*>* fStrings; |
| 368 // Both for the help string and in case fStrings is empty. | 362 // Both for the help string and in case fStrings is empty. |
| 369 SkString fDefaultString; | 363 SkString fDefaultString; |
| 370 | 364 |
| 371 // In order to keep a linked list. | 365 // In order to keep a linked list. |
| 372 SkFlagInfo* fNext; | 366 SkFlagInfo* fNext; |
| 373 }; | 367 }; |
| 374 #endif // SK_FLAGS_H | 368 #endif // SK_FLAGS_H |
| OLD | NEW |