| 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_COMMAND_LINE_FLAGS_H | 8 #ifndef SK_COMMAND_LINE_FLAGS_H |
| 9 #define SK_COMMAND_LINE_FLAGS_H | 9 #define SK_COMMAND_LINE_FLAGS_H |
| 10 | 10 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 125 |
| 126 bool isEmpty() const { return this->count() == 0; } | 126 bool isEmpty() const { return this->count() == 0; } |
| 127 | 127 |
| 128 private: | 128 private: |
| 129 void reset() { fStrings.reset(); } | 129 void reset() { fStrings.reset(); } |
| 130 | 130 |
| 131 void append(const char* string) { | 131 void append(const char* string) { |
| 132 fStrings.push_back().set(string); | 132 fStrings.push_back().set(string); |
| 133 } | 133 } |
| 134 | 134 |
| 135 void append(const char* string, size_t length) { |
| 136 fStrings.push_back().set(string, length); |
| 137 } |
| 138 |
| 135 SkTArray<SkString> fStrings; | 139 SkTArray<SkString> fStrings; |
| 136 | 140 |
| 137 friend class SkFlagInfo; | 141 friend class SkFlagInfo; |
| 138 }; | 142 }; |
| 139 | 143 |
| 140 private: | 144 private: |
| 141 static SkFlagInfo* gHead; | 145 static SkFlagInfo* gHead; |
| 142 static SkString gUsage; | 146 static SkString gUsage; |
| 143 | 147 |
| 144 // For access to gHead. | 148 // For access to gHead. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 class SkFlagInfo { | 213 class SkFlagInfo { |
| 210 | 214 |
| 211 public: | 215 public: |
| 212 enum FlagTypes { | 216 enum FlagTypes { |
| 213 kBool_FlagType, | 217 kBool_FlagType, |
| 214 kString_FlagType, | 218 kString_FlagType, |
| 215 kInt_FlagType, | 219 kInt_FlagType, |
| 216 kDouble_FlagType, | 220 kDouble_FlagType, |
| 217 }; | 221 }; |
| 218 | 222 |
| 219 // Create flags of the desired type, and append to the list. | 223 /** |
| 224 * Each Create<Type>Flag function creates an SkFlagInfo of the specified ty
pe. The SkFlagInfo |
| 225 * object is appended to a list, which is deleted when SkCommandLineFlags::
Parse is called. |
| 226 * Therefore, each call should be made before the call to ::Parse. They are
not intended |
| 227 * to be called directly. Instead, use the macros described above. |
| 228 * @param name Long version (at least 2 characters) of the name of the flag
. This name can |
| 229 * be referenced on the command line as "--name" to set the value of th
is flag. |
| 230 * @param shortName Short version (one character) of the name of the flag.
This name can |
| 231 * be referenced on the command line as "-shortName" to set the value o
f this flag. |
| 232 * @param p<Type> Pointer to a global variable which holds the value set by
SkCommandLineFlags. |
| 233 * @param defaultValue The default value of this flag. The variable pointed
to by p<Type> will |
| 234 * be set to this value initially. This is also displayed as part of th
e help output. |
| 235 * @param helpString Explanation of what this flag changes in the program. |
| 236 */ |
| 220 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, | 237 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, |
| 221 bool defaultValue, const char* helpString) { | 238 bool defaultValue, const char* helpString) { |
| 222 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kBool_FlagTy
pe, helpString)); | 239 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kBool_FlagTy
pe, helpString)); |
| 223 info->fBoolValue = pBool; | 240 info->fBoolValue = pBool; |
| 224 *info->fBoolValue = info->fDefaultBool = defaultValue; | 241 *info->fBoolValue = info->fDefaultBool = defaultValue; |
| 225 return true; | 242 return true; |
| 226 } | 243 } |
| 227 | 244 |
| 245 /** |
| 246 * See comments for CreateBoolFlag. |
| 247 * @param pStrings Unlike the others, this is a pointer to an array of valu
es. |
| 248 * @param defaultValue Thise default will be parsed so that strings separat
ed by spaces |
| 249 * will be added to pStrings. |
| 250 */ |
| 228 static bool CreateStringFlag(const char* name, const char* shortName, | 251 static bool CreateStringFlag(const char* name, const char* shortName, |
| 229 SkCommandLineFlags::StringArray* pStrings, | 252 SkCommandLineFlags::StringArray* pStrings, |
| 230 const char* defaultValue, const char* helpStrin
g) { | 253 const char* defaultValue, const char* helpStrin
g); |
| 231 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kString_Flag
Type, helpString)); | |
| 232 info->fDefaultString.set(defaultValue); | |
| 233 | 254 |
| 234 info->fStrings = pStrings; | 255 /** |
| 235 info->fStrings->reset(); | 256 * See comments for CreateBoolFlag. |
| 236 // If default is "", leave the array empty. | 257 */ |
| 237 if (info->fDefaultString.size() > 0) { | |
| 238 info->fStrings->append(defaultValue); | |
| 239 } | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 243 static bool CreateIntFlag(const char* name, int32_t* pInt, | 258 static bool CreateIntFlag(const char* name, int32_t* pInt, |
| 244 int32_t defaultValue, const char* helpString) { | 259 int32_t defaultValue, const char* helpString) { |
| 245 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kInt_FlagType, he
lpString)); | 260 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kInt_FlagType, he
lpString)); |
| 246 info->fIntValue = pInt; | 261 info->fIntValue = pInt; |
| 247 *info->fIntValue = info->fDefaultInt = defaultValue; | 262 *info->fIntValue = info->fDefaultInt = defaultValue; |
| 248 return true; | 263 return true; |
| 249 } | 264 } |
| 250 | 265 |
| 266 /** |
| 267 * See comments for CreateBoolFlag. |
| 268 */ |
| 251 static bool CreateDoubleFlag(const char* name, double* pDouble, | 269 static bool CreateDoubleFlag(const char* name, double* pDouble, |
| 252 double defaultValue, const char* helpString) { | 270 double defaultValue, const char* helpString) { |
| 253 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kDouble_FlagType,
helpString)); | 271 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kDouble_FlagType,
helpString)); |
| 254 info->fDoubleValue = pDouble; | 272 info->fDoubleValue = pDouble; |
| 255 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 273 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
| 256 return true; | 274 return true; |
| 257 } | 275 } |
| 258 | 276 |
| 259 /** | 277 /** |
| 260 * Returns true if the string matches this flag. | 278 * Returns true if the string matches this flag. |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 , fIntValue(NULL) | 386 , fIntValue(NULL) |
| 369 , fDefaultInt(0) | 387 , fDefaultInt(0) |
| 370 , fDoubleValue(NULL) | 388 , fDoubleValue(NULL) |
| 371 , fDefaultDouble(0) | 389 , fDefaultDouble(0) |
| 372 , fStrings(NULL) { | 390 , fStrings(NULL) { |
| 373 fNext = SkCommandLineFlags::gHead; | 391 fNext = SkCommandLineFlags::gHead; |
| 374 SkCommandLineFlags::gHead = this; | 392 SkCommandLineFlags::gHead = this; |
| 375 SkASSERT(NULL != name && strlen(name) > 1); | 393 SkASSERT(NULL != name && strlen(name) > 1); |
| 376 SkASSERT(NULL == shortName || 1 == strlen(shortName)); | 394 SkASSERT(NULL == shortName || 1 == strlen(shortName)); |
| 377 } | 395 } |
| 396 |
| 397 /** |
| 398 * Set a StringArray to hold the values stored in defaultStrings. |
| 399 * @param array The StringArray to modify. |
| 400 * @param defaultStrings Space separated list of strings that should be ins
erted into array |
| 401 * individually. |
| 402 */ |
| 403 static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, |
| 404 const char* defaultStrings); |
| 405 |
| 378 // Name of the flag, without initial dashes | 406 // Name of the flag, without initial dashes |
| 379 SkString fName; | 407 SkString fName; |
| 380 SkString fShortName; | 408 SkString fShortName; |
| 381 FlagTypes fFlagType; | 409 FlagTypes fFlagType; |
| 382 SkString fHelpString; | 410 SkString fHelpString; |
| 383 bool* fBoolValue; | 411 bool* fBoolValue; |
| 384 bool fDefaultBool; | 412 bool fDefaultBool; |
| 385 int32_t* fIntValue; | 413 int32_t* fIntValue; |
| 386 int32_t fDefaultInt; | 414 int32_t fDefaultInt; |
| 387 double* fDoubleValue; | 415 double* fDoubleValue; |
| 388 double fDefaultDouble; | 416 double fDefaultDouble; |
| 389 SkCommandLineFlags::StringArray* fStrings; | 417 SkCommandLineFlags::StringArray* fStrings; |
| 390 // Both for the help string and in case fStrings is empty. | 418 // Both for the help string and in case fStrings is empty. |
| 391 SkString fDefaultString; | 419 SkString fDefaultString; |
| 392 | 420 |
| 393 // In order to keep a linked list. | 421 // In order to keep a linked list. |
| 394 SkFlagInfo* fNext; | 422 SkFlagInfo* fNext; |
| 395 }; | 423 }; |
| 396 #endif // SK_COMMAND_LINE_FLAGS_H | 424 #endif // SK_COMMAND_LINE_FLAGS_H |
| OLD | NEW |