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 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 111 | /** |
| 112 | * Custom class for holding the arguments for a string flag. |
| 113 | * Publicly only has accessors so the strings cannot be modified. |
| 114 | */ |
| 115 | class StringArray { |
| 116 | public: |
| 117 | const char* operator[](int i) const { |
| 118 | SkASSERT(i >= 0 && i < fStrings.count()); |
| 119 | return fStrings[i].c_str(); |
| 120 | } |
| 121 | |
| 122 | int count() const { |
| 123 | return fStrings.count(); |
| 124 | } |
| 125 | |
| 126 | bool isEmpty() const { return this->count() == 0; } |
| 127 | |
epoger@google.com | defc487 | 2013-09-19 06:18:27 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Returns true iff string is equal to one of the strings in this array. |
| 130 | */ |
| 131 | bool contains(const char* string) const { |
| 132 | for (int i = 0; i < fStrings.count(); i++) { |
| 133 | if (fStrings[i].equals(string)) { |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 140 | void set(int i, const char* str) { |
| 141 | fStrings[i].set(str); |
| 142 | } |
| 143 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 144 | private: |
| 145 | void reset() { fStrings.reset(); } |
| 146 | |
| 147 | void append(const char* string) { |
| 148 | fStrings.push_back().set(string); |
| 149 | } |
| 150 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 151 | void append(const char* string, size_t length) { |
| 152 | fStrings.push_back().set(string, length); |
| 153 | } |
| 154 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 155 | SkTArray<SkString> fStrings; |
| 156 | |
| 157 | friend class SkFlagInfo; |
| 158 | }; |
| 159 | |
commit-bot@chromium.org | a6f37e7 | 2013-08-30 15:52:46 +0000 | [diff] [blame] | 160 | /* Takes a list of the form [~][^]match[$] |
| 161 | ~ causes a matching test to always be skipped |
| 162 | ^ requires the start of the test to match |
| 163 | $ requires the end of the test to match |
| 164 | ^ and $ requires an exact match |
| 165 | If a test does not match any list entry, it is skipped unless some list entry starts with ~ |
| 166 | */ |
| 167 | static bool ShouldSkip(const SkTDArray<const char*>& strings, const char* name); |
| 168 | static bool ShouldSkip(const StringArray& strings, const char* name); |
| 169 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 170 | private: |
| 171 | static SkFlagInfo* gHead; |
| 172 | static SkString gUsage; |
| 173 | |
| 174 | // For access to gHead. |
| 175 | friend class SkFlagInfo; |
| 176 | }; |
| 177 | |
| 178 | #define TO_STRING2(s) #s |
| 179 | #define TO_STRING(s) TO_STRING2(s) |
| 180 | |
| 181 | #define DEFINE_bool(name, defaultValue, helpString) \ |
| 182 | bool FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 183 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 184 | nullptr, \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 185 | &FLAGS_##name, \ |
| 186 | defaultValue, \ |
| 187 | helpString) |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 188 | |
| 189 | // bool 2 allows specifying a short name. No check is done to ensure that shortName |
| 190 | // is actually shorter than name. |
| 191 | #define DEFINE_bool2(name, shortName, defaultValue, helpString) \ |
| 192 | bool FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 193 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
| 194 | TO_STRING(shortName),\ |
| 195 | &FLAGS_##name, \ |
| 196 | defaultValue, \ |
| 197 | helpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 198 | |
| 199 | #define DECLARE_bool(name) extern bool FLAGS_##name; |
| 200 | |
| 201 | #define DEFINE_string(name, defaultValue, helpString) \ |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 202 | SkCommandLineFlags::StringArray FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 203 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 204 | nullptr, \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 205 | &FLAGS_##name, \ |
| 206 | defaultValue, \ |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 207 | helpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 208 | |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 209 | // string2 allows specifying a short name. There is an assert that shortName |
| 210 | // is only 1 character. |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 211 | #define DEFINE_string2(name, shortName, defaultValue, helpString) \ |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 212 | SkCommandLineFlags::StringArray FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 213 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
| 214 | TO_STRING(shortName), \ |
| 215 | &FLAGS_##name, \ |
| 216 | defaultValue, \ |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 217 | helpString) |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 218 | |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 219 | #define DECLARE_string(name) extern SkCommandLineFlags::StringArray FLAGS_##name; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 220 | |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 221 | |
| 222 | |
| 223 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 224 | #define DEFINE_int32(name, defaultValue, helpString) \ |
| 225 | int32_t FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 226 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
| 227 | &FLAGS_##name, \ |
| 228 | defaultValue, \ |
| 229 | helpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 230 | |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 231 | #define DEFINE_int32_2(name, shortName, defaultValue, helpString) \ |
| 232 | int32_t FLAGS_##name; \ |
| 233 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
| 234 | TO_STRING(shortName), \ |
| 235 | &FLAGS_##name, \ |
| 236 | defaultValue, \ |
| 237 | helpString) |
| 238 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 239 | #define DECLARE_int32(name) extern int32_t FLAGS_##name; |
| 240 | |
| 241 | #define DEFINE_double(name, defaultValue, helpString) \ |
| 242 | double FLAGS_##name; \ |
fmalita | 523beb7 | 2015-06-10 10:46:50 -0700 | [diff] [blame] | 243 | SK_UNUSED static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \ |
| 244 | &FLAGS_##name, \ |
| 245 | defaultValue, \ |
| 246 | helpString) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 247 | |
| 248 | #define DECLARE_double(name) extern double FLAGS_##name; |
| 249 | |
| 250 | class SkFlagInfo { |
| 251 | |
| 252 | public: |
| 253 | enum FlagTypes { |
| 254 | kBool_FlagType, |
| 255 | kString_FlagType, |
| 256 | kInt_FlagType, |
| 257 | kDouble_FlagType, |
| 258 | }; |
| 259 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 260 | /** |
| 261 | * Each Create<Type>Flag function creates an SkFlagInfo of the specified type. The SkFlagInfo |
| 262 | * object is appended to a list, which is deleted when SkCommandLineFlags::Parse is called. |
| 263 | * Therefore, each call should be made before the call to ::Parse. They are not intended |
| 264 | * to be called directly. Instead, use the macros described above. |
| 265 | * @param name Long version (at least 2 characters) of the name of the flag. This name can |
| 266 | * be referenced on the command line as "--name" to set the value of this flag. |
| 267 | * @param shortName Short version (one character) of the name of the flag. This name can |
| 268 | * be referenced on the command line as "-shortName" to set the value of this flag. |
| 269 | * @param p<Type> Pointer to a global variable which holds the value set by SkCommandLineFlags. |
| 270 | * @param defaultValue The default value of this flag. The variable pointed to by p<Type> will |
| 271 | * be set to this value initially. This is also displayed as part of the help output. |
| 272 | * @param helpString Explanation of what this flag changes in the program. |
| 273 | */ |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 274 | static bool CreateBoolFlag(const char* name, const char* shortName, bool* pBool, |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 275 | bool defaultValue, const char* helpString) { |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 276 | SkFlagInfo* info = new SkFlagInfo(name, shortName, kBool_FlagType, helpString); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 277 | info->fBoolValue = pBool; |
| 278 | *info->fBoolValue = info->fDefaultBool = defaultValue; |
| 279 | return true; |
| 280 | } |
| 281 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 282 | /** |
| 283 | * See comments for CreateBoolFlag. |
| 284 | * @param pStrings Unlike the others, this is a pointer to an array of values. |
| 285 | * @param defaultValue Thise default will be parsed so that strings separated by spaces |
| 286 | * will be added to pStrings. |
| 287 | */ |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 288 | static bool CreateStringFlag(const char* name, const char* shortName, |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 289 | SkCommandLineFlags::StringArray* pStrings, |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 290 | const char* defaultValue, const char* helpString); |
skia.committer@gmail.com | 075b089 | 2013-03-05 07:09:08 +0000 | [diff] [blame] | 291 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 292 | /** |
| 293 | * See comments for CreateBoolFlag. |
| 294 | */ |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 295 | static bool CreateIntFlag(const char* name, int32_t* pInt, |
| 296 | int32_t defaultValue, const char* helpString) { |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 297 | SkFlagInfo* info = new SkFlagInfo(name, nullptr, kInt_FlagType, helpString); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 298 | info->fIntValue = pInt; |
| 299 | *info->fIntValue = info->fDefaultInt = defaultValue; |
| 300 | return true; |
| 301 | } |
| 302 | |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 303 | static bool CreateIntFlag(const char* name, const char* shortName, int32_t* pInt, |
| 304 | int32_t defaultValue, const char* helpString) { |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 305 | SkFlagInfo* info = new SkFlagInfo(name, shortName, kInt_FlagType, helpString); |
robertphillips | 5ce341f | 2015-09-18 09:04:43 -0700 | [diff] [blame] | 306 | info->fIntValue = pInt; |
| 307 | *info->fIntValue = info->fDefaultInt = defaultValue; |
| 308 | return true; |
| 309 | } |
| 310 | |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 311 | /** |
| 312 | * See comments for CreateBoolFlag. |
| 313 | */ |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 314 | static bool CreateDoubleFlag(const char* name, double* pDouble, |
| 315 | double defaultValue, const char* helpString) { |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 316 | SkFlagInfo* info = new SkFlagInfo(name, nullptr, kDouble_FlagType, helpString); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 317 | info->fDoubleValue = pDouble; |
| 318 | *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | /** |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 323 | * Returns true if the string matches this flag. |
| 324 | * For a boolean flag, also sets the value, since a boolean flag can be set in a number of ways |
| 325 | * without looking at the following string: |
| 326 | * --name |
| 327 | * --noname |
| 328 | * --name=true |
| 329 | * --name=false |
| 330 | * --name=1 |
| 331 | * --name=0 |
| 332 | * --name=TRUE |
| 333 | * --name=FALSE |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 334 | */ |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 335 | bool match(const char* string); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 336 | |
| 337 | FlagTypes getFlagType() const { return fFlagType; } |
| 338 | |
| 339 | void resetStrings() { |
| 340 | if (kString_FlagType == fFlagType) { |
| 341 | fStrings->reset(); |
| 342 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 343 | SkDEBUGFAIL("Can only call resetStrings on kString_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | void append(const char* string) { |
| 348 | if (kString_FlagType == fFlagType) { |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 349 | fStrings->append(string); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 350 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 351 | SkDEBUGFAIL("Can only append to kString_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
| 355 | void setInt(int32_t value) { |
| 356 | if (kInt_FlagType == fFlagType) { |
| 357 | *fIntValue = value; |
| 358 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 359 | SkDEBUGFAIL("Can only call setInt on kInt_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | void setDouble(double value) { |
| 364 | if (kDouble_FlagType == fFlagType) { |
| 365 | *fDoubleValue = value; |
| 366 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 367 | SkDEBUGFAIL("Can only call setDouble on kDouble_FlagType"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 371 | void setBool(bool value) { |
| 372 | if (kBool_FlagType == fFlagType) { |
| 373 | *fBoolValue = value; |
| 374 | } else { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 375 | SkDEBUGFAIL("Can only call setBool on kBool_FlagType"); |
scroggo@google.com | 5dc4ca1 | 2013-03-21 13:10:59 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 379 | SkFlagInfo* next() { return fNext; } |
| 380 | |
| 381 | const SkString& name() const { return fName; } |
| 382 | |
scroggo@google.com | 8366df0 | 2013-03-20 19:50:41 +0000 | [diff] [blame] | 383 | const SkString& shortName() const { return fShortName; } |
| 384 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 385 | const SkString& help() const { return fHelpString; } |
| 386 | |
| 387 | SkString defaultValue() const { |
| 388 | SkString result; |
| 389 | switch (fFlagType) { |
| 390 | case SkFlagInfo::kBool_FlagType: |
| 391 | result.printf("%s", fDefaultBool ? "true" : "false"); |
| 392 | break; |
| 393 | case SkFlagInfo::kString_FlagType: |
| 394 | return fDefaultString; |
| 395 | case SkFlagInfo::kInt_FlagType: |
| 396 | result.printf("%i", fDefaultInt); |
| 397 | break; |
| 398 | case SkFlagInfo::kDouble_FlagType: |
| 399 | result.printf("%2.2f", fDefaultDouble); |
| 400 | break; |
| 401 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 402 | SkDEBUGFAIL("Invalid flag type"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 403 | } |
| 404 | return result; |
| 405 | } |
| 406 | |
| 407 | SkString typeAsString() const { |
| 408 | switch (fFlagType) { |
| 409 | case SkFlagInfo::kBool_FlagType: |
| 410 | return SkString("bool"); |
| 411 | case SkFlagInfo::kString_FlagType: |
| 412 | return SkString("string"); |
| 413 | case SkFlagInfo::kInt_FlagType: |
| 414 | return SkString("int"); |
| 415 | case SkFlagInfo::kDouble_FlagType: |
| 416 | return SkString("double"); |
| 417 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 418 | SkDEBUGFAIL("Invalid flag type"); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 419 | return SkString(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | private: |
reed | 4ff653c | 2015-12-14 05:58:25 -0800 | [diff] [blame^] | 424 | 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] | 425 | : fName(name) |
scroggo@google.com | 604e0c2 | 2013-04-09 21:25:46 +0000 | [diff] [blame] | 426 | , fShortName(shortName) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 427 | , fFlagType(type) |
| 428 | , fHelpString(helpString) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 429 | , fBoolValue(nullptr) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 430 | , fDefaultBool(false) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 431 | , fIntValue(nullptr) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 432 | , fDefaultInt(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 433 | , fDoubleValue(nullptr) |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 434 | , fDefaultDouble(0) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 435 | , fStrings(nullptr) { |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 436 | fNext = SkCommandLineFlags::gHead; |
| 437 | SkCommandLineFlags::gHead = this; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 438 | SkASSERT(name && strlen(name) > 1); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 439 | SkASSERT(nullptr == shortName || 1 == strlen(shortName)); |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 440 | } |
scroggo@google.com | 58104a9 | 2013-04-24 19:25:26 +0000 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * Set a StringArray to hold the values stored in defaultStrings. |
| 444 | * @param array The StringArray to modify. |
| 445 | * @param defaultStrings Space separated list of strings that should be inserted into array |
| 446 | * individually. |
| 447 | */ |
| 448 | static void SetDefaultStrings(SkCommandLineFlags::StringArray* array, |
| 449 | const char* defaultStrings); |
| 450 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 451 | // Name of the flag, without initial dashes |
| 452 | SkString fName; |
scroggo@google.com | 09fd4d2 | 2013-03-20 14:20:18 +0000 | [diff] [blame] | 453 | SkString fShortName; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 454 | FlagTypes fFlagType; |
| 455 | SkString fHelpString; |
| 456 | bool* fBoolValue; |
| 457 | bool fDefaultBool; |
| 458 | int32_t* fIntValue; |
| 459 | int32_t fDefaultInt; |
| 460 | double* fDoubleValue; |
| 461 | double fDefaultDouble; |
scroggo@google.com | b7dbf63 | 2013-04-23 15:38:09 +0000 | [diff] [blame] | 462 | SkCommandLineFlags::StringArray* fStrings; |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 463 | // Both for the help string and in case fStrings is empty. |
| 464 | SkString fDefaultString; |
| 465 | |
| 466 | // In order to keep a linked list. |
| 467 | SkFlagInfo* fNext; |
| 468 | }; |
scroggo@google.com | d9ba9a0 | 2013-03-21 19:43:15 +0000 | [diff] [blame] | 469 | #endif // SK_COMMAND_LINE_FLAGS_H |