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 | |
| 8 | #ifndef SK_FLAGS_H |
| 9 | #define SK_FLAGS_H |
| 10 | |
| 11 | #include "SkString.h" |
| 12 | #include "SkTDArray.h" |
| 13 | |
| 14 | /** |
| 15 | * Including this file (and compiling SkFlags.cpp) provides command line |
| 16 | * parsing. In order to use it, use the following macros in global |
| 17 | * namespace: |
| 18 | * |
| 19 | * DEFINE_bool(name, defaultValue, helpString); |
| 20 | * DEFINE_string(name, defaultValue, helpString); |
| 21 | * DEFINE_int32(name, defaultValue, helpString); |
| 22 | * DEFINE_double(name, defaultValue, helpString); |
| 23 | * |
| 24 | * Then, in main, call SkFlags::SetUsage() to describe usage and call |
| 25 | * SkFlags::ParseCommandLine() to parse the flags. Henceforth, each flag can |
| 26 | * be referenced using |
| 27 | * |
| 28 | * FLAGS_name |
| 29 | * |
| 30 | * For example, the line |
| 31 | * |
| 32 | * DEFINE_bool(boolean, false, "The variable boolean does such and such"); |
| 33 | * |
| 34 | * will create the following variable: |
| 35 | * |
| 36 | * bool FLAGS_boolean; |
| 37 | * |
| 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 |
| 40 | * to false. (Single dashes are also permitted for this and other flags.) The |
| 41 | * helpString will be printed if the help flag (-h or -help) is used. |
| 42 | * |
| 43 | * Similarly, the line |
| 44 | * |
| 45 | * DEFINE_int32(integer, .., ..); |
| 46 | * |
| 47 | * will create |
| 48 | * |
| 49 | * int32_t FLAGS_integer; |
| 50 | * |
| 51 | * and |
| 52 | * |
| 53 | * DEFINE_double(real, .., ..); |
| 54 | * |
| 55 | * will create |
| 56 | * |
| 57 | * double FLAGS_real; |
| 58 | * |
| 59 | * These flags can be set by specifying, for example, "--integer 7" and |
| 60 | * "--real 3.14" on the command line. |
| 61 | * |
| 62 | * Unlike the others, the line |
| 63 | * |
| 64 | * DEFINE_string(args, .., ..); |
| 65 | * |
| 66 | * creates an array: |
| 67 | * |
| 68 | * SkTDArray<const char*> FLAGS_args; |
| 69 | * |
| 70 | * If the default value is the empty string, FLAGS_args will default to a size |
| 71 | * of zero. Otherwise it will default to a size of 1 with the default string |
| 72 | * as its value. All strings that follow the flag on the command line (until |
| 73 | * a string that begins with '-') will be entries in the array. |
| 74 | * |
| 75 | * Any flag can be referenced from another file after using the following: |
| 76 | * |
| 77 | * DECLARE_x(name); |
| 78 | * |
| 79 | * (where 'x' is the type specified in the DEFINE). |
| 80 | * |
| 81 | * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as |
| 82 | * robust as gflags, but suits our purposes. For example, allows creating |
| 83 | * a flag -h or -help which will never be used, since SkFlags handles it. |
| 84 | * SkFlags will also allow creating --flag and --noflag. Uses the same input |
| 85 | * format as gflags and creates similarly named variables (i.e. FLAGS_name). |
| 86 | * Strings are handled differently (resulting variable will be an array of |
| 87 | * strings) so that a flag can be followed by multiple parameters. |
| 88 | */ |
| 89 | |
| 90 | |
| 91 | class SkFlagInfo; |
| 92 | |
| 93 | class SkFlags { |
| 94 | |
| 95 | public: |
| 96 | /** |
| 97 | * Call to set the help message to be displayed. Should be called before |
| 98 | * ParseCommandLine. |
| 99 | */ |
| 100 | static void SetUsage(const char* usage); |
| 101 | |
| 102 | /** |
| 103 | * Call at the beginning of main to parse flags created by DEFINE_x, above. |
| 104 | * Must only be called once. |
| 105 | */ |
| 106 | static void ParseCommandLine(int argc, char** argv); |
| 107 | |
| 108 | private: |
| 109 | static SkFlagInfo* gHead; |
| 110 | static SkString gUsage; |
| 111 | |
| 112 | // For access to gHead. |
| 113 | friend class SkFlagInfo; |
| 114 | }; |
| 115 | |
| 116 | #define TO_STRING2(s) #s |
| 117 | #define TO_STRING(s) TO_STRING2(s) |
| 118 | |
| 119 | #define DEFINE_bool(name, defaultValue, helpString) \ |
| 120 | bool FLAGS_##name; \ |
| 121 | static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
| 122 | &FLAGS_##name, \ |
| 123 | defaultValue, \ |
| 124 | helpString) |
| 125 | |
| 126 | #define DECLARE_bool(name) extern bool FLAGS_##name; |
| 127 | |
| 128 | #define DEFINE_string(name, defaultValue, helpString) \ |
| 129 | SkTDArray<const char*> FLAGS_##name; \ |
| 130 | static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
| 131 | &FLAGS_##name, \ |
| 132 | defaultValue, \ |
| 133 | helpString) |
| 134 | |
| 135 | #define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name; |
| 136 | |
| 137 | #define DEFINE_int32(name, defaultValue, helpString) \ |
| 138 | int32_t FLAGS_##name; \ |
| 139 | static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
| 140 | &FLAGS_##name, \ |
| 141 | defaultValue, \ |
| 142 | helpString) |
| 143 | |
| 144 | #define DECLARE_int32(name) extern int32_t FLAGS_##name; |
| 145 | |
| 146 | #define DEFINE_double(name, defaultValue, helpString) \ |
| 147 | double FLAGS_##name; \ |
| 148 | static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \ |
| 149 | &FLAGS_##name, \ |
| 150 | defaultValue, \ |
| 151 | helpString) |
| 152 | |
| 153 | #define DECLARE_double(name) extern double FLAGS_##name; |
| 154 | |
| 155 | class SkFlagInfo { |
| 156 | |
| 157 | public: |
| 158 | enum FlagTypes { |
| 159 | kBool_FlagType, |
| 160 | kString_FlagType, |
| 161 | kInt_FlagType, |
| 162 | kDouble_FlagType, |
| 163 | }; |
| 164 | |
| 165 | // Create flags of the desired type, and append to the list. |
| 166 | static bool CreateBoolFlag(const char* name, bool* pBool, |
| 167 | bool defaultValue, const char* helpString) { |
| 168 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kBool_FlagType, helpString)); |
| 169 | info->fBoolValue = pBool; |
| 170 | *info->fBoolValue = info->fDefaultBool = defaultValue; |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | static bool CreateStringFlag(const char* name, SkTDArray<const char*>* pStrings, |
| 175 | const char* defaultValue, const char* helpString) { |
| 176 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kString_FlagType, helpString)); |
| 177 | info->fDefaultString.set(defaultValue); |
skia.committer@gmail.com | 075b089 | 2013-03-05 07:09:08 +0000 | [diff] [blame] | 178 | |
scroggo@google.com | 161e1ba | 2013-03-04 16:41:06 +0000 | [diff] [blame] | 179 | info->fStrings = pStrings; |
| 180 | info->fStrings->reset(); |
| 181 | // If default is "", leave the array empty. |
| 182 | if (info->fDefaultString.size() > 0) { |
| 183 | info->fStrings->append(1, &defaultValue); |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | static bool CreateIntFlag(const char* name, int32_t* pInt, |
| 189 | int32_t defaultValue, const char* helpString) { |
| 190 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kInt_FlagType, helpString)); |
| 191 | info->fIntValue = pInt; |
| 192 | *info->fIntValue = info->fDefaultInt = defaultValue; |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | static bool CreateDoubleFlag(const char* name, double* pDouble, |
| 197 | double defaultValue, const char* helpString) { |
| 198 | SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kDouble_FlagType, helpString)); |
| 199 | info->fDoubleValue = pDouble; |
| 200 | *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns true if the string matches this flag. For a bool, also sets the |
| 206 | * value, since a bool is specified as true or false by --name or --noname. |
| 207 | */ |
| 208 | bool match(const char* string) { |
| 209 | if (SkStrStartsWith(string, '-')) { |
| 210 | string++; |
| 211 | // Allow one or two dashes |
| 212 | if (SkStrStartsWith(string, '-')) { |
| 213 | string++; |
| 214 | } |
| 215 | if (kBool_FlagType == fFlagType) { |
| 216 | // In this case, go ahead and set the value. |
| 217 | if (fName.equals(string)) { |
| 218 | *fBoolValue = true; |
| 219 | return true; |
| 220 | } |
| 221 | SkString noname(fName); |
| 222 | noname.prepend("no"); |
| 223 | if (noname.equals(string)) { |
| 224 | *fBoolValue = false; |
| 225 | return true; |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | return fName.equals(string); |
| 230 | } else { |
| 231 | // Has no dash |
| 232 | return false; |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | FlagTypes getFlagType() const { return fFlagType; } |
| 238 | |
| 239 | void resetStrings() { |
| 240 | if (kString_FlagType == fFlagType) { |
| 241 | fStrings->reset(); |
| 242 | } else { |
| 243 | SkASSERT(!"Can only call resetStrings on kString_FlagType"); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void append(const char* string) { |
| 248 | if (kString_FlagType == fFlagType) { |
| 249 | fStrings->append(1, &string); |
| 250 | } else { |
| 251 | SkASSERT(!"Can only append to kString_FlagType"); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void setInt(int32_t value) { |
| 256 | if (kInt_FlagType == fFlagType) { |
| 257 | *fIntValue = value; |
| 258 | } else { |
| 259 | SkASSERT(!"Can only call setInt on kInt_FlagType"); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void setDouble(double value) { |
| 264 | if (kDouble_FlagType == fFlagType) { |
| 265 | *fDoubleValue = value; |
| 266 | } else { |
| 267 | SkASSERT(!"Can only call setDouble on kDouble_FlagType"); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | SkFlagInfo* next() { return fNext; } |
| 272 | |
| 273 | const SkString& name() const { return fName; } |
| 274 | |
| 275 | const SkString& help() const { return fHelpString; } |
| 276 | |
| 277 | SkString defaultValue() const { |
| 278 | SkString result; |
| 279 | switch (fFlagType) { |
| 280 | case SkFlagInfo::kBool_FlagType: |
| 281 | result.printf("%s", fDefaultBool ? "true" : "false"); |
| 282 | break; |
| 283 | case SkFlagInfo::kString_FlagType: |
| 284 | return fDefaultString; |
| 285 | case SkFlagInfo::kInt_FlagType: |
| 286 | result.printf("%i", fDefaultInt); |
| 287 | break; |
| 288 | case SkFlagInfo::kDouble_FlagType: |
| 289 | result.printf("%2.2f", fDefaultDouble); |
| 290 | break; |
| 291 | default: |
| 292 | SkASSERT(!"Invalid flag type"); |
| 293 | } |
| 294 | return result; |
| 295 | } |
| 296 | |
| 297 | SkString typeAsString() const { |
| 298 | switch (fFlagType) { |
| 299 | case SkFlagInfo::kBool_FlagType: |
| 300 | return SkString("bool"); |
| 301 | case SkFlagInfo::kString_FlagType: |
| 302 | return SkString("string"); |
| 303 | case SkFlagInfo::kInt_FlagType: |
| 304 | return SkString("int"); |
| 305 | case SkFlagInfo::kDouble_FlagType: |
| 306 | return SkString("double"); |
| 307 | default: |
| 308 | SkASSERT(!"Invalid flag type"); |
| 309 | return SkString(); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | private: |
| 314 | SkFlagInfo(const char* name, FlagTypes type, const char* helpString) |
| 315 | : fName(name) |
| 316 | , fFlagType(type) |
| 317 | , fHelpString(helpString) |
| 318 | , fBoolValue(NULL) |
| 319 | , fDefaultBool(false) |
| 320 | , fIntValue(NULL) |
| 321 | , fDefaultInt(0) |
| 322 | , fDoubleValue(NULL) |
| 323 | , fDefaultDouble(0) |
| 324 | , fStrings(NULL) { |
| 325 | fNext = SkFlags::gHead; |
| 326 | SkFlags::gHead = this; |
| 327 | } |
| 328 | // Name of the flag, without initial dashes |
| 329 | SkString fName; |
| 330 | FlagTypes fFlagType; |
| 331 | SkString fHelpString; |
| 332 | bool* fBoolValue; |
| 333 | bool fDefaultBool; |
| 334 | int32_t* fIntValue; |
| 335 | int32_t fDefaultInt; |
| 336 | double* fDoubleValue; |
| 337 | double fDefaultDouble; |
| 338 | SkTDArray<const char*>* fStrings; |
| 339 | // Both for the help string and in case fStrings is empty. |
| 340 | SkString fDefaultString; |
| 341 | |
| 342 | // In order to keep a linked list. |
| 343 | SkFlagInfo* fNext; |
| 344 | }; |
| 345 | #endif // SK_FLAGS_H |