Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: tools/SkFlags.h

Issue 12440067: Change the name of SkFlags to SkCommandLineFlags. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: ParseCommandLine -> Parse Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/PictureRenderingFlags.cpp ('k') | tools/SkFlags.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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. FLAGS_boolean can also be set using "--boolean=true" or
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.
47 *
48 * Similarly, the line
49 *
50 * DEFINE_int32(integer, .., ..);
51 *
52 * will create
53 *
54 * int32_t FLAGS_integer;
55 *
56 * and
57 *
58 * DEFINE_double(real, .., ..);
59 *
60 * will create
61 *
62 * double FLAGS_real;
63 *
64 * These flags can be set by specifying, for example, "--integer 7" and
65 * "--real 3.14" on the command line.
66 *
67 * Unlike the others, the line
68 *
69 * DEFINE_string(args, .., ..);
70 *
71 * creates an array:
72 *
73 * SkTDArray<const char*> FLAGS_args;
74 *
75 * If the default value is the empty string, FLAGS_args will default to a size
76 * of zero. Otherwise it will default to a size of 1 with the default string
77 * as its value. All strings that follow the flag on the command line (until
78 * a string that begins with '-') will be entries in the array.
79 *
80 * Any flag can be referenced from another file after using the following:
81 *
82 * DECLARE_x(name);
83 *
84 * (where 'x' is the type specified in the DEFINE).
85 *
86 * Inspired by gflags (https://code.google.com/p/gflags/). Is not quite as
87 * robust as gflags, but suits our purposes. For example, allows creating
88 * a flag -h or -help which will never be used, since SkFlags handles it.
89 * SkFlags will also allow creating --flag and --noflag. Uses the same input
90 * format as gflags and creates similarly named variables (i.e. FLAGS_name).
91 * Strings are handled differently (resulting variable will be an array of
92 * strings) so that a flag can be followed by multiple parameters.
93 */
94
95
96 class SkFlagInfo;
97
98 class SkFlags {
99
100 public:
101 /**
102 * Call to set the help message to be displayed. Should be called before
103 * ParseCommandLine.
104 */
105 static void SetUsage(const char* usage);
106
107 /**
108 * Call at the beginning of main to parse flags created by DEFINE_x, above.
109 * Must only be called once.
110 */
111 static void ParseCommandLine(int argc, char** argv);
112
113 private:
114 static SkFlagInfo* gHead;
115 static SkString gUsage;
116
117 // For access to gHead.
118 friend class SkFlagInfo;
119 };
120
121 #define TO_STRING2(s) #s
122 #define TO_STRING(s) TO_STRING2(s)
123
124 #define DEFINE_bool(name, defaultValue, helpString) \
125 bool FLAGS_##name; \
126 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
127 NULL, \
128 &FLAGS_##name, \
129 defaultValue, \
130 helpString)
131
132 // bool 2 allows specifying a short name. No check is done to ensure that shortN ame
133 // is actually shorter than name.
134 #define DEFINE_bool2(name, shortName, defaultValue, helpString) \
135 bool FLAGS_##name; \
136 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \
137 TO_STRING(shortName),\
138 &FLAGS_##name, \
139 defaultValue, \
140 helpString)
141
142 #define DECLARE_bool(name) extern bool FLAGS_##name;
143
144 #define DEFINE_string(name, defaultValue, helpString) \
145 SkTDArray<const char*> FLAGS_##name; \
146 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
147 NULL, \
148 &FLAGS_##name, \
149 defaultValue, \
150 helpString)
151
152 // string2 allows specifying a short name. No check is done to ensure that short Name
153 // is actually shorter than name.
154 #define DEFINE_string2(name, shortName, defaultValue, helpString) \
155 SkTDArray<const char*> FLAGS_##name; \
156 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \
157 TO_STRING(shortName), \
158 &FLAGS_##name, \
159 defaultValue, \
160 helpString)
161
162 #define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name;
163
164 #define DEFINE_int32(name, defaultValue, helpString) \
165 int32_t FLAGS_##name; \
166 static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \
167 &FLAGS_##name, \
168 defaultValue, \
169 helpString)
170
171 #define DECLARE_int32(name) extern int32_t FLAGS_##name;
172
173 #define DEFINE_double(name, defaultValue, helpString) \
174 double FLAGS_##name; \
175 static bool unused_##name = SkFlagInfo::CreateDoubleFlag(TO_STRING(name), \
176 &FLAGS_##name, \
177 defaultValue, \
178 helpString)
179
180 #define DECLARE_double(name) extern double FLAGS_##name;
181
182 class SkFlagInfo {
183
184 public:
185 enum FlagTypes {
186 kBool_FlagType,
187 kString_FlagType,
188 kInt_FlagType,
189 kDouble_FlagType,
190 };
191
192 // Create flags of the desired type, and append to the list.
193 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB ool,
194 bool defaultValue, const char* helpString) {
195 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kBool_FlagType, helpStr ing));
196 info->fShortName.set(shortName);
197 info->fBoolValue = pBool;
198 *info->fBoolValue = info->fDefaultBool = defaultValue;
199 return true;
200 }
201
202 static bool CreateStringFlag(const char* name, const char* shortName,
203 SkTDArray<const char*>* pStrings,
204 const char* defaultValue, const char* helpStrin g) {
205 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kString_FlagType, helpS tring));
206 info->fShortName.set(shortName);
207 info->fDefaultString.set(defaultValue);
208
209 info->fStrings = pStrings;
210 info->fStrings->reset();
211 // If default is "", leave the array empty.
212 if (info->fDefaultString.size() > 0) {
213 info->fStrings->append(1, &defaultValue);
214 }
215 return true;
216 }
217
218 static bool CreateIntFlag(const char* name, int32_t* pInt,
219 int32_t defaultValue, const char* helpString) {
220 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kInt_FlagType, helpStri ng));
221 info->fIntValue = pInt;
222 *info->fIntValue = info->fDefaultInt = defaultValue;
223 return true;
224 }
225
226 static bool CreateDoubleFlag(const char* name, double* pDouble,
227 double defaultValue, const char* helpString) {
228 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kDouble_FlagType, helpS tring));
229 info->fDoubleValue = pDouble;
230 *info->fDoubleValue = info->fDefaultDouble = defaultValue;
231 return true;
232 }
233
234 /**
235 * Returns true if the string matches this flag.
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
246 */
247 bool match(const char* string);
248
249 FlagTypes getFlagType() const { return fFlagType; }
250
251 void resetStrings() {
252 if (kString_FlagType == fFlagType) {
253 fStrings->reset();
254 } else {
255 SkASSERT(!"Can only call resetStrings on kString_FlagType");
256 }
257 }
258
259 void append(const char* string) {
260 if (kString_FlagType == fFlagType) {
261 fStrings->append(1, &string);
262 } else {
263 SkASSERT(!"Can only append to kString_FlagType");
264 }
265 }
266
267 void setInt(int32_t value) {
268 if (kInt_FlagType == fFlagType) {
269 *fIntValue = value;
270 } else {
271 SkASSERT(!"Can only call setInt on kInt_FlagType");
272 }
273 }
274
275 void setDouble(double value) {
276 if (kDouble_FlagType == fFlagType) {
277 *fDoubleValue = value;
278 } else {
279 SkASSERT(!"Can only call setDouble on kDouble_FlagType");
280 }
281 }
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
291 SkFlagInfo* next() { return fNext; }
292
293 const SkString& name() const { return fName; }
294
295 const SkString& shortName() const { return fShortName; }
296
297 const SkString& help() const { return fHelpString; }
298
299 SkString defaultValue() const {
300 SkString result;
301 switch (fFlagType) {
302 case SkFlagInfo::kBool_FlagType:
303 result.printf("%s", fDefaultBool ? "true" : "false");
304 break;
305 case SkFlagInfo::kString_FlagType:
306 return fDefaultString;
307 case SkFlagInfo::kInt_FlagType:
308 result.printf("%i", fDefaultInt);
309 break;
310 case SkFlagInfo::kDouble_FlagType:
311 result.printf("%2.2f", fDefaultDouble);
312 break;
313 default:
314 SkASSERT(!"Invalid flag type");
315 }
316 return result;
317 }
318
319 SkString typeAsString() const {
320 switch (fFlagType) {
321 case SkFlagInfo::kBool_FlagType:
322 return SkString("bool");
323 case SkFlagInfo::kString_FlagType:
324 return SkString("string");
325 case SkFlagInfo::kInt_FlagType:
326 return SkString("int");
327 case SkFlagInfo::kDouble_FlagType:
328 return SkString("double");
329 default:
330 SkASSERT(!"Invalid flag type");
331 return SkString();
332 }
333 }
334
335 private:
336 SkFlagInfo(const char* name, FlagTypes type, const char* helpString)
337 : fName(name)
338 , fFlagType(type)
339 , fHelpString(helpString)
340 , fBoolValue(NULL)
341 , fDefaultBool(false)
342 , fIntValue(NULL)
343 , fDefaultInt(0)
344 , fDoubleValue(NULL)
345 , fDefaultDouble(0)
346 , fStrings(NULL) {
347 fNext = SkFlags::gHead;
348 SkFlags::gHead = this;
349 }
350 // Name of the flag, without initial dashes
351 SkString fName;
352 SkString fShortName;
353 FlagTypes fFlagType;
354 SkString fHelpString;
355 bool* fBoolValue;
356 bool fDefaultBool;
357 int32_t* fIntValue;
358 int32_t fDefaultInt;
359 double* fDoubleValue;
360 double fDefaultDouble;
361 SkTDArray<const char*>* fStrings;
362 // Both for the help string and in case fStrings is empty.
363 SkString fDefaultString;
364
365 // In order to keep a linked list.
366 SkFlagInfo* fNext;
367 };
368 #endif // SK_FLAGS_H
OLDNEW
« no previous file with comments | « tools/PictureRenderingFlags.cpp ('k') | tools/SkFlags.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698