| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 8 | #ifndef SK_COMMAND_LINE_FLAGS_H |
| 9 | #define SK_COMMAND_LINE_FLAGS_H |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 10 | |
| 11 | #include "SkString.h" |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 12 | #include "SkTArray.h" |
| sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 13 | #include "SkTDArray.h" |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 14 | |
| 15 | /** |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 16 | * Including this file (and compiling SkCommandLineFlags.cpp) provides command line |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 17 | * parsing. In order to use it, use the following macros in global |
| 18 | * namespace: |
| 19 | * |
| 20 | * DEFINE_bool(name, defaultValue, helpString); |
| 21 | * DEFINE_string(name, defaultValue, helpString); |
| 22 | * DEFINE_int32(name, defaultValue, helpString); |
| 23 | * DEFINE_double(name, defaultValue, helpString); |
| 24 | * |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 25 | * Then, in main, call SkCommandLineFlags::SetUsage() to describe usage and call |
| 26 | * SkCommandLineFlags::Parse() to parse the flags. Henceforth, each flag can |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 27 | * be referenced using |
| 28 | * |
| 29 | * FLAGS_name |
| 30 | * |
| 31 | * For example, the line |
| 32 | * |
| 33 | * DEFINE_bool(boolean, false, "The variable boolean does such and such"); |
| 34 | * |
| 35 | * will create the following variable: |
| 36 | * |
| 37 | * bool FLAGS_boolean; |
| 38 | * |
| 39 | * which will initially be set to false, and can be set to true by using the |
| 40 | * flag "--boolean" on the commandline. "--noboolean" will set FLAGS_boolean |
| scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 41 | * to false. FLAGS_boolean can also be set using "--boolean=true" or |
| 42 | * "--boolean true" (where "true" can be replaced by "false", "TRUE", "FALSE", |
| 43 | * "1" or "0"). |
| 44 | * |
| scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 45 | * The helpString will be printed if the help flag (-h or -help) is used. |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 46 | * |
| 47 | * Similarly, the line |
| 48 | * |
| 49 | * DEFINE_int32(integer, .., ..); |
| 50 | * |
| 51 | * will create |
| 52 | * |
| 53 | * int32_t FLAGS_integer; |
| 54 | * |
| 55 | * and |
| 56 | * |
| 57 | * DEFINE_double(real, .., ..); |
| 58 | * |
| 59 | * will create |
| 60 | * |
| 61 | * double FLAGS_real; |
| 62 | * |
| 63 | * These flags can be set by specifying, for example, "--integer 7" and |
| 64 | * "--real 3.14" on the command line. |
| 65 | * |
| 66 | * Unlike the others, the line |
| 67 | * |
| 68 | * DEFINE_string(args, .., ..); |
| 69 | * |
| 70 | * creates an array: |
| 71 | * |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 72 | * SkCommandLineFlags::StringArray FLAGS_args; |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 73 | * |
| 74 | * If the default value is the empty string, FLAGS_args will default to a size |
| 75 | * of zero. Otherwise it will default to a size of 1 with the default string |
| 76 | * as its value. All strings that follow the flag on the command line (until |
| 77 | * a string that begins with '-') will be entries in the array. |
| 78 | * |
| 79 | * Any flag can be referenced from another file after using the following: |
| 80 | * |
| 81 | * DECLARE_x(name); |
| 82 | * |
| 83 | * (where 'x' is the type specified in the DEFINE). |
| 84 | * |
| 85 | * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as |
| 86 | * robust as gflags, but suits our purposes. For example, allows creating |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 87 | * a flag -h or -help which will never be used, since SkCommandLineFlags handles it. |
| 88 | * SkCommandLineFlags will also allow creating --flag and --noflag. Uses the same input |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 89 | * format as gflags and creates similarly named variables (i.e. FLAGS_name). |
| 90 | * Strings are handled differently (resulting variable will be an array of |
| 91 | * strings) so that a flag can be followed by multiple parameters. |
| 92 | */ |
| 93 | |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 94 | class SkFlagInfo; |
| 95 | |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 96 | class SkCommandLineFlags { |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 97 | |
| 98 | public: |
| 99 | /** |
| 100 | * Call to set the help message to be displayed. Should be called before |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 101 | * Parse. |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 102 | */ |
| 103 | static void SetUsage(const char* usage); |
| 104 | |
| 105 | /** |
| 106 | * Call at the beginning of main to parse flags created by DEFINE_x, above. |
| 107 | * Must only be called once. |
| 108 | */ |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 109 | static void Parse(int argc, char** argv); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 110 | |
| sglez@google.com | 586db93 | 2013-07-24 17:24:23 +0000 | [diff] [blame] | 111 | /* Takes a list of the form [~][^]match[$] |
| 112 | ~ causes a matching test to always be skipped |
| 113 | ^ requires the start of the test to match |
| 114 | $ requires the end of the test to match |
| 115 | ^ and $ requires an exact match |
| 116 | If a test does not match any list entry, it is skipped unless some list entry starts with ~ |
| 117 | */ |
| 118 | static bool ShouldSkip(const SkTDArray<const char*>& strings, const char* name); |
| 119 | |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Custom class for holding the arguments for a string flag. |
| 122 | * Publicly only has accessors so the strings cannot be modified. |
| 123 | */ |
| 124 | class StringArray { |
| 125 | public: |
| 126 | const char* operator[](int i) const { |
| 127 | SkASSERT(i >= 0 && i < fStrings.count()); |
| 128 | return fStrings[i].c_str(); |
| 129 | } |
| 130 | |
| 131 | int count() const { |
| 132 | return fStrings.count(); |
| 133 | } |
| 134 | |
| 135 | bool isEmpty() const { return this->count() == 0; } |
| 136 | |
| 137 | private: |
| 138 | void reset() { fStrings.reset(); } |
| 139 | |
| 140 | void append(const char* string) { |
| 141 | fStrings.push_back().set(string); |
| 142 | } |
| 143 | |
| scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 144 | void append(const char* string, size_t length) { |
| 145 | fStrings.push_back().set(string, length); |
| 146 | } |
| 147 | |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 148 | SkTArray<SkString> fStrings; |
| 149 | |
| 150 | friend class SkFlagInfo; |
| 151 | }; |
| 152 | |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 153 | private: |
| 154 | static SkFlagInfo* gHead; |
| 155 | static SkString gUsage; |
| 156 | |
| 157 | // For access to gHead. |
| 158 | friend class SkFlagInfo; |
| 159 | }; |
| 160 | |
| 161 | #define TO_STRING2(s) #s |
| 162 | #define TO_STRING(s) TO_STRING2(s) |
| 163 | |
| 164 | #define DEFINE_bool(name, defaultValue, helpString) \ |
| 165 | bool FLAGS_##name; \ |
| 166 | static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
| scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 167 | NULL, \ |
| 168 | &FLAGS_##name, \ |
| 169 | defaultValue, \ |
| 170 | helpString) |
| 171 | |
| 172 | // bool 2 allows specifying a short name. No check is done to ensure that shortName |
| 173 | // is actually shorter than name. |
| 174 | #define DEFINE_bool2(name, shortName, defaultValue, helpString) \ |
| 175 | bool FLAGS_##name; \ |
| 176 | static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
| 177 | TO_STRING(shortName),\ |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 178 | &FLAGS_##name, \ |
| 179 | defaultValue, \ |
| 180 | helpString) |
| 181 | |
| 182 | #define DECLARE_bool(name) extern bool FLAGS_##name; |
| 183 | |
| 184 | #define DEFINE_string(name, defaultValue, helpString) \ |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 185 | SkCommandLineFlags::StringArray FLAGS_##name; \ |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 186 | static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
| scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 187 | NULL, \ |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 188 | &FLAGS_##name, \ |
| 189 | defaultValue, \ |
| 190 | helpString) |
| 191 | |
| scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 192 | // string2 allows specifying a short name. There is an assert that shortName |
| 193 | // is only 1 character. |
| scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 194 | #define DEFINE_string2(name, shortName, defaultValue, helpString) \ |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 195 | SkCommandLineFlags::StringArray FLAGS_##name; \ |
| scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 196 | static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
| 197 | TO_STRING(shortName), \ |
| 198 | &FLAGS_##name, \ |
| 199 | defaultValue, \ |
| 200 | helpString) |
| 201 | |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 202 | #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name; |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 203 | |
| 204 | #define DEFINE_int32(name, defaultValue, helpString) \ |
| 205 | int32_t FLAGS_##name; \ |
| 206 | static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
| 207 | &FLAGS_##name, \ |
| 208 | defaultValue, \ |
| 209 | helpString) |
| 210 | |
| 211 | #define DECLARE_int32(name) extern int32_t FLAGS_##name; |
| 212 | |
| 213 | #define DEFINE_double(name, defaultValue, helpString) \ |
| 214 | double FLAGS_##name; \ |
| 215 | static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \ |
| 216 | &FLAGS_##name, \ |
| 217 | defaultValue, \ |
| 218 | helpString) |
| 219 | |
| 220 | #define DECLARE_double(name) extern double FLAGS_##name; |
| 221 | |
| 222 | class SkFlagInfo { |
| 223 | |
| 224 | public: |
| 225 | enum FlagTypes { |
| 226 | kBool_FlagType, |
| 227 | kString_FlagType, |
| 228 | kInt_FlagType, |
| 229 | kDouble_FlagType, |
| 230 | }; |
| 231 | |
| scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 232 | /** |
| 233 | * Each Create<Type>Flag function creates an SkFlagInfo of the specified type. The SkFlagInfo |
| 234 | * object is appended to a list, which is deleted when SkCommandLineFlags::Parse is called. |
| 235 | * Therefore, each call should be made before the call to ::Parse. They are not intended |
| 236 | * to be called directly. Instead, use the macros described above. |
| 237 | * @param name Long version (at least 2 characters) of the name of the flag. This name can |
| 238 | * be referenced on the command line as "--name" to set the value of this flag. |
| 239 | * @param shortName Short version (one character) of the name of the flag. This name can |
| 240 | * be referenced on the command line as "-shortName" to set the value of this flag. |
| 241 | * @param p<Type> Pointer to a global variable which holds the value set by SkCommandLineFlags. |
| 242 | * @param defaultValue The default value of this flag. The variable pointed to by p<Type> will |
| 243 | * be set to this value initially. This is also displayed as part of the help output. |
| 244 | * @param helpString Explanation of what this flag changes in the program. |
| 245 | */ |
| scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 246 | static bool CreateBoolFlag(const char* name, const char* shortName, bool* pBool, |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 247 | bool defaultValue, const char* helpString) { |
| scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 248 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, shortName, kBool_FlagType, helpString)); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 249 | info->fBoolValue = pBool; |
| 250 | *info->fBoolValue = info->fDefaultBool = defaultValue; |
| 251 | return true; |
| 252 | } |
| 253 | |
| scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 254 | /** |
| 255 | * See comments for CreateBoolFlag. |
| 256 | * @param pStrings Unlike the others, this is a pointer to an array of values. |
| 257 | * @param defaultValue Thise default will be parsed so that strings separated by spaces |
| 258 | * will be added to pStrings. |
| 259 | */ |
| scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 260 | static bool CreateStringFlag(const char* name, const char* shortName, |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 261 | SkCommandLineFlags::StringArray* pStrings, |
| scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 262 | const char* defaultValue, const char* helpString); |
| skia.committer@gmail.com | 075b089 | 2013-03-05 07:09:08 +0000 | [diff] [blame] | 263 | |
| scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 264 | /** |
| 265 | * See comments for CreateBoolFlag. |
| 266 | */ |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 267 | static bool CreateIntFlag(const char* name, int32_t* pInt, |
| 268 | int32_t defaultValue, const char* helpString) { |
| scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 269 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kInt_FlagType, helpString)); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 270 | info->fIntValue = pInt; |
| 271 | *info->fIntValue = info->fDefaultInt = defaultValue; |
| 272 | return true; |
| 273 | } |
| 274 | |
| scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 275 | /** |
| 276 | * See comments for CreateBoolFlag. |
| 277 | */ |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 278 | static bool CreateDoubleFlag(const char* name, double* pDouble, |
| 279 | double defaultValue, const char* helpString) { |
| scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 280 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, NULL, kDouble_FlagType, helpString)); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 281 | info->fDoubleValue = pDouble; |
| 282 | *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | /** |
| scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 287 | * Returns true if the string matches this flag. |
| 288 | * For a boolean flag, also sets the value, since a boolean flag can be set in a number of ways |
| 289 | * without looking at the following string: |
| 290 | * --name |
| 291 | * --noname |
| 292 | * --name=true |
| 293 | * --name=false |
| 294 | * --name=1 |
| 295 | * --name=0 |
| 296 | * --name=TRUE |
| 297 | * --name=FALSE |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 298 | */ |
| scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 299 | bool match(const char* string); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 300 | |
| 301 | FlagTypes getFlagType() const { return fFlagType; } |
| 302 | |
| 303 | void resetStrings() { |
| 304 | if (kString_FlagType == fFlagType) { |
| 305 | fStrings->reset(); |
| 306 | } else { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame^] | 307 | SkDEBUGFAIL("Can only call resetStrings on kString_FlagType"); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | void append(const char* string) { |
| 312 | if (kString_FlagType == fFlagType) { |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 313 | fStrings->append(string); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 314 | } else { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame^] | 315 | SkDEBUGFAIL("Can only append to kString_FlagType"); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | void setInt(int32_t value) { |
| 320 | if (kInt_FlagType == fFlagType) { |
| 321 | *fIntValue = value; |
| 322 | } else { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame^] | 323 | SkDEBUGFAIL("Can only call setInt on kInt_FlagType"); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | void setDouble(double value) { |
| 328 | if (kDouble_FlagType == fFlagType) { |
| 329 | *fDoubleValue = value; |
| 330 | } else { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame^] | 331 | SkDEBUGFAIL("Can only call setDouble on kDouble_FlagType"); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 335 | void setBool(bool value) { |
| 336 | if (kBool_FlagType == fFlagType) { |
| 337 | *fBoolValue = value; |
| 338 | } else { |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame^] | 339 | SkDEBUGFAIL("Can only call setBool on kBool_FlagType"); |
| scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 343 | SkFlagInfo* next() { return fNext; } |
| 344 | |
| 345 | const SkString& name() const { return fName; } |
| 346 | |
| scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 347 | const SkString& shortName() const { return fShortName; } |
| 348 | |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 349 | const SkString& help() const { return fHelpString; } |
| 350 | |
| 351 | SkString defaultValue() const { |
| 352 | SkString result; |
| 353 | switch (fFlagType) { |
| 354 | case SkFlagInfo::kBool_FlagType: |
| 355 | result.printf("%s", fDefaultBool ? "true" : "false"); |
| 356 | break; |
| 357 | case SkFlagInfo::kString_FlagType: |
| 358 | return fDefaultString; |
| 359 | case SkFlagInfo::kInt_FlagType: |
| 360 | result.printf("%i", fDefaultInt); |
| 361 | break; |
| 362 | case SkFlagInfo::kDouble_FlagType: |
| 363 | result.printf("%2.2f", fDefaultDouble); |
| 364 | break; |
| 365 | default: |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame^] | 366 | SkDEBUGFAIL("Invalid flag type"); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 367 | } |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | SkString typeAsString() const { |
| 372 | switch (fFlagType) { |
| 373 | case SkFlagInfo::kBool_FlagType: |
| 374 | return SkString("bool"); |
| 375 | case SkFlagInfo::kString_FlagType: |
| 376 | return SkString("string"); |
| 377 | case SkFlagInfo::kInt_FlagType: |
| 378 | return SkString("int"); |
| 379 | case SkFlagInfo::kDouble_FlagType: |
| 380 | return SkString("double"); |
| 381 | default: |
| mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame^] | 382 | SkDEBUGFAIL("Invalid flag type"); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 383 | return SkString(); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | private: |
| scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 388 | SkFlagInfo(const char* name, const char* shortName, FlagTypes type, const char* helpString) |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 389 | : fName(name) |
| scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 390 | , fShortName(shortName) |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 391 | , fFlagType(type) |
| 392 | , fHelpString(helpString) |
| 393 | , fBoolValue(NULL) |
| 394 | , fDefaultBool(false) |
| 395 | , fIntValue(NULL) |
| 396 | , fDefaultInt(0) |
| 397 | , fDoubleValue(NULL) |
| 398 | , fDefaultDouble(0) |
| 399 | , fStrings(NULL) { |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 400 | fNext = SkCommandLineFlags::gHead; |
| 401 | SkCommandLineFlags::gHead = this; |
| scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 402 | SkASSERT(NULL != name && strlen(name) > 1); |
| 403 | SkASSERT(NULL == shortName || 1 == strlen(shortName)); |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 404 | } |
| scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 405 | |
| 406 | /** |
| 407 | * Set a StringArray to hold the values stored in defaultStrings. |
| 408 | * @param array The StringArray to modify. |
| 409 | * @param defaultStrings Space separated list of strings that should be inserted into array |
| 410 | * individually. |
| 411 | */ |
| 412 | static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, |
| 413 | const char* defaultStrings); |
| 414 | |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 415 | // Name of the flag, without initial dashes |
| 416 | SkString fName; |
| scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 417 | SkString fShortName; |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 418 | FlagTypes fFlagType; |
| 419 | SkString fHelpString; |
| 420 | bool* fBoolValue; |
| 421 | bool fDefaultBool; |
| 422 | int32_t* fIntValue; |
| 423 | int32_t fDefaultInt; |
| 424 | double* fDoubleValue; |
| 425 | double fDefaultDouble; |
| scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 426 | SkCommandLineFlags::StringArray* fStrings; |
| scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 427 | // Both for the help string and in case fStrings is empty. |
| 428 | SkString fDefaultString; |
| 429 | |
| 430 | // In order to keep a linked list. |
| 431 | SkFlagInfo* fNext; |
| 432 | }; |
| scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 433 | #endif // SK_COMMAND_LINE_FLAGS_H |