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_FLAGS_H | 8 #ifndef SK_FLAGS_H |
9 #define SK_FLAGS_H | 9 #define SK_FLAGS_H |
10 | 10 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // For access to gHead. | 112 // For access to gHead. |
113 friend class SkFlagInfo; | 113 friend class SkFlagInfo; |
114 }; | 114 }; |
115 | 115 |
116 #define TO_STRING2(s) #s | 116 #define TO_STRING2(s) #s |
117 #define TO_STRING(s) TO_STRING2(s) | 117 #define TO_STRING(s) TO_STRING2(s) |
118 | 118 |
119 #define DEFINE_bool(name, defaultValue, helpString) \ | 119 #define DEFINE_bool(name, defaultValue, helpString) \ |
120 bool FLAGS_##name; \ | 120 bool FLAGS_##name; \ |
121 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ | 121 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
| 122 NULL, \ |
122 &FLAGS_##name, \ | 123 &FLAGS_##name, \ |
123 defaultValue, \ | 124 defaultValue, \ |
124 helpString) | 125 helpString) |
| 126 |
| 127 // bool 2 allows specifying a short name. No check is done to ensure that shortN
ame |
| 128 // is actually shorter than name. |
| 129 #define DEFINE_bool2(name, shortName, defaultValue, helpString) \ |
| 130 bool FLAGS_##name; \ |
| 131 static bool unused_##name = SkFlagInfo::CreateBoolFlag(TO_STRING(name), \ |
| 132 TO_STRING(shortName),\ |
| 133 &FLAGS_##name, \ |
| 134 defaultValue, \ |
| 135 helpString) |
125 | 136 |
126 #define DECLARE_bool(name) extern bool FLAGS_##name; | 137 #define DECLARE_bool(name) extern bool FLAGS_##name; |
127 | 138 |
128 #define DEFINE_string(name, defaultValue, helpString) \ | 139 #define DEFINE_string(name, defaultValue, helpString) \ |
129 SkTDArray<const char*> FLAGS_##name; \ | 140 SkTDArray<const char*> FLAGS_##name; \ |
130 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ | 141 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name), \ |
| 142 NULL, \ |
131 &FLAGS_##name, \ | 143 &FLAGS_##name, \ |
132 defaultValue, \ | 144 defaultValue, \ |
133 helpString) | 145 helpString) |
134 | 146 |
| 147 // string2 allows specifying a short name. No check is done to ensure that short
Name |
| 148 // is actually shorter than name. |
| 149 #define DEFINE_string2(name, shortName, defaultValue, helpString)
\ |
| 150 SkTDArray<const char*> FLAGS_##name;
\ |
| 151 static bool unused_##name = SkFlagInfo::CreateStringFlag(TO_STRING(name),
\ |
| 152 TO_STRING(shortName),
\ |
| 153 &FLAGS_##name,
\ |
| 154 defaultValue,
\ |
| 155 helpString) |
| 156 |
135 #define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name; | 157 #define DECLARE_string(name) extern SkTDArray<const char*> FLAGS_##name; |
136 | 158 |
137 #define DEFINE_int32(name, defaultValue, helpString) \ | 159 #define DEFINE_int32(name, defaultValue, helpString) \ |
138 int32_t FLAGS_##name; \ | 160 int32_t FLAGS_##name; \ |
139 static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ | 161 static bool unused_##name = SkFlagInfo::CreateIntFlag(TO_STRING(name), \ |
140 &FLAGS_##name, \ | 162 &FLAGS_##name, \ |
141 defaultValue, \ | 163 defaultValue, \ |
142 helpString) | 164 helpString) |
143 | 165 |
144 #define DECLARE_int32(name) extern int32_t FLAGS_##name; | 166 #define DECLARE_int32(name) extern int32_t FLAGS_##name; |
(...skipping 11 matching lines...) Expand all Loading... |
156 | 178 |
157 public: | 179 public: |
158 enum FlagTypes { | 180 enum FlagTypes { |
159 kBool_FlagType, | 181 kBool_FlagType, |
160 kString_FlagType, | 182 kString_FlagType, |
161 kInt_FlagType, | 183 kInt_FlagType, |
162 kDouble_FlagType, | 184 kDouble_FlagType, |
163 }; | 185 }; |
164 | 186 |
165 // Create flags of the desired type, and append to the list. | 187 // Create flags of the desired type, and append to the list. |
166 static bool CreateBoolFlag(const char* name, bool* pBool, | 188 static bool CreateBoolFlag(const char* name, const char* shortName, bool* pB
ool, |
167 bool defaultValue, const char* helpString) { | 189 bool defaultValue, const char* helpString) { |
168 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kBool_FlagType, helpStr
ing)); | 190 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kBool_FlagType, helpStr
ing)); |
| 191 info->fShortName.set(shortName); |
169 info->fBoolValue = pBool; | 192 info->fBoolValue = pBool; |
170 *info->fBoolValue = info->fDefaultBool = defaultValue; | 193 *info->fBoolValue = info->fDefaultBool = defaultValue; |
171 return true; | 194 return true; |
172 } | 195 } |
173 | 196 |
174 static bool CreateStringFlag(const char* name, SkTDArray<const char*>* pStri
ngs, | 197 static bool CreateStringFlag(const char* name, const char* shortName, |
| 198 SkTDArray<const char*>* pStrings, |
175 const char* defaultValue, const char* helpStrin
g) { | 199 const char* defaultValue, const char* helpStrin
g) { |
176 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kString_FlagType, helpS
tring)); | 200 SkFlagInfo* info = SkNEW_ARGS(SkFlagInfo, (name, kString_FlagType, helpS
tring)); |
| 201 info->fShortName.set(shortName); |
177 info->fDefaultString.set(defaultValue); | 202 info->fDefaultString.set(defaultValue); |
178 | 203 |
179 info->fStrings = pStrings; | 204 info->fStrings = pStrings; |
180 info->fStrings->reset(); | 205 info->fStrings->reset(); |
181 // If default is "", leave the array empty. | 206 // If default is "", leave the array empty. |
182 if (info->fDefaultString.size() > 0) { | 207 if (info->fDefaultString.size() > 0) { |
183 info->fStrings->append(1, &defaultValue); | 208 info->fStrings->append(1, &defaultValue); |
184 } | 209 } |
185 return true; | 210 return true; |
186 } | 211 } |
(...skipping 12 matching lines...) Expand all Loading... |
199 info->fDoubleValue = pDouble; | 224 info->fDoubleValue = pDouble; |
200 *info->fDoubleValue = info->fDefaultDouble = defaultValue; | 225 *info->fDoubleValue = info->fDefaultDouble = defaultValue; |
201 return true; | 226 return true; |
202 } | 227 } |
203 | 228 |
204 /** | 229 /** |
205 * Returns true if the string matches this flag. For a bool, also sets the | 230 * 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. | 231 * value, since a bool is specified as true or false by --name or --noname. |
207 */ | 232 */ |
208 bool match(const char* string) { | 233 bool match(const char* string) { |
209 if (SkStrStartsWith(string, '-')) { | 234 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
210 string++; | 235 string++; |
211 // Allow one or two dashes | 236 // Allow one or two dashes |
212 if (SkStrStartsWith(string, '-')) { | 237 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
213 string++; | 238 string++; |
214 } | 239 } |
215 if (kBool_FlagType == fFlagType) { | 240 if (kBool_FlagType == fFlagType) { |
216 // In this case, go ahead and set the value. | 241 // In this case, go ahead and set the value. |
217 if (fName.equals(string)) { | 242 if (fName.equals(string) || fShortName.equals(string)) { |
218 *fBoolValue = true; | 243 *fBoolValue = true; |
219 return true; | 244 return true; |
220 } | 245 } |
221 SkString noname(fName); | 246 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { |
222 noname.prepend("no"); | 247 string += 2; |
223 if (noname.equals(string)) { | 248 if (fName.equals(string) || fShortName.equals(string)) { |
224 *fBoolValue = false; | 249 *fBoolValue = false; |
225 return true; | 250 return true; |
| 251 } |
| 252 return false; |
226 } | 253 } |
227 return false; | |
228 } | 254 } |
229 return fName.equals(string); | 255 return fName.equals(string) || fShortName.equals(string); |
230 } else { | 256 } else { |
231 // Has no dash | 257 // Has no dash |
232 return false; | 258 return false; |
233 } | 259 } |
234 return false; | 260 return false; |
235 } | 261 } |
236 | 262 |
237 FlagTypes getFlagType() const { return fFlagType; } | 263 FlagTypes getFlagType() const { return fFlagType; } |
238 | 264 |
239 void resetStrings() { | 265 void resetStrings() { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 , fIntValue(NULL) | 346 , fIntValue(NULL) |
321 , fDefaultInt(0) | 347 , fDefaultInt(0) |
322 , fDoubleValue(NULL) | 348 , fDoubleValue(NULL) |
323 , fDefaultDouble(0) | 349 , fDefaultDouble(0) |
324 , fStrings(NULL) { | 350 , fStrings(NULL) { |
325 fNext = SkFlags::gHead; | 351 fNext = SkFlags::gHead; |
326 SkFlags::gHead = this; | 352 SkFlags::gHead = this; |
327 } | 353 } |
328 // Name of the flag, without initial dashes | 354 // Name of the flag, without initial dashes |
329 SkString fName; | 355 SkString fName; |
| 356 SkString fShortName; |
330 FlagTypes fFlagType; | 357 FlagTypes fFlagType; |
331 SkString fHelpString; | 358 SkString fHelpString; |
332 bool* fBoolValue; | 359 bool* fBoolValue; |
333 bool fDefaultBool; | 360 bool fDefaultBool; |
334 int32_t* fIntValue; | 361 int32_t* fIntValue; |
335 int32_t fDefaultInt; | 362 int32_t fDefaultInt; |
336 double* fDoubleValue; | 363 double* fDoubleValue; |
337 double fDefaultDouble; | 364 double fDefaultDouble; |
338 SkTDArray<const char*>* fStrings; | 365 SkTDArray<const char*>* fStrings; |
339 // Both for the help string and in case fStrings is empty. | 366 // Both for the help string and in case fStrings is empty. |
340 SkString fDefaultString; | 367 SkString fDefaultString; |
341 | 368 |
342 // In order to keep a linked list. | 369 // In order to keep a linked list. |
343 SkFlagInfo* fNext; | 370 SkFlagInfo* fNext; |
344 }; | 371 }; |
345 #endif // SK_FLAGS_H | 372 #endif // SK_FLAGS_H |
OLD | NEW |