henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 11 | // Originally comes from shared/commandlineflags/flags.h |
| 12 | |
| 13 | // Flags are defined and declared using DEFINE_xxx and DECLARE_xxx macros, |
| 14 | // where xxx is the flag type. Flags are referred to via FLAG_yyy, |
| 15 | // where yyy is the flag name. For intialization and iteration of flags, |
| 16 | // see the FlagList class. For full programmatic access to any |
| 17 | // flag, see the Flag class. |
| 18 | // |
| 19 | // The implementation only relies and basic C++ functionality |
| 20 | // and needs no special library or STL support. |
| 21 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #ifndef RTC_BASE_FLAGS_H_ |
| 23 | #define RTC_BASE_FLAGS_H_ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 24 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 26 | |
| 27 | #if defined(WEBRTC_WIN) |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/constructor_magic.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 29 | #endif |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 30 | |
| 31 | namespace rtc { |
| 32 | |
| 33 | // Internal use only. |
| 34 | union FlagValue { |
| 35 | // Note: Because in C++ non-bool values are silently converted into |
| 36 | // bool values ('bool b = "false";' results in b == true!), we pass |
| 37 | // and int argument to New_BOOL as this appears to be safer - sigh. |
| 38 | // In particular, it prevents the (not uncommon!) bug where a bool |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 39 | // flag is defined via: WEBRTC_DEFINE_bool(flag, "false", "some comment");. |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 40 | static FlagValue New_BOOL(int b) { |
| 41 | FlagValue v; |
| 42 | v.b = (b != 0); |
| 43 | return v; |
| 44 | } |
| 45 | |
| 46 | static FlagValue New_INT(int i) { |
| 47 | FlagValue v; |
| 48 | v.i = i; |
| 49 | return v; |
| 50 | } |
| 51 | |
| 52 | static FlagValue New_FLOAT(float f) { |
| 53 | FlagValue v; |
| 54 | v.f = f; |
| 55 | return v; |
| 56 | } |
| 57 | |
| 58 | static FlagValue New_STRING(const char* s) { |
| 59 | FlagValue v; |
| 60 | v.s = s; |
| 61 | return v; |
| 62 | } |
| 63 | |
| 64 | bool b; |
| 65 | int i; |
| 66 | double f; |
| 67 | const char* s; |
| 68 | }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 70 | // Each flag can be accessed programmatically via a Flag object. |
| 71 | class Flag { |
| 72 | public: |
| 73 | enum Type { BOOL, INT, FLOAT, STRING }; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 74 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 75 | // Internal use only. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 76 | Flag(const char* file, |
| 77 | const char* name, |
| 78 | const char* comment, |
| 79 | Type type, |
| 80 | void* variable, |
| 81 | FlagValue default_); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 82 | |
| 83 | // General flag information |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 84 | const char* file() const { return file_; } |
| 85 | const char* name() const { return name_; } |
| 86 | const char* comment() const { return comment_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 87 | |
| 88 | // Flag type |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 89 | Type type() const { return type_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 90 | |
| 91 | // Flag variables |
| 92 | bool* bool_variable() const { |
| 93 | RTC_DCHECK_EQ(BOOL, type_); |
| 94 | return &variable_->b; |
| 95 | } |
| 96 | |
| 97 | int* int_variable() const { |
| 98 | RTC_DCHECK_EQ(INT, type_); |
| 99 | return &variable_->i; |
| 100 | } |
| 101 | |
| 102 | double* float_variable() const { |
| 103 | RTC_DCHECK_EQ(FLOAT, type_); |
| 104 | return &variable_->f; |
| 105 | } |
| 106 | |
| 107 | const char** string_variable() const { |
| 108 | RTC_DCHECK_EQ(STRING, type_); |
| 109 | return &variable_->s; |
| 110 | } |
| 111 | |
| 112 | // Default values |
| 113 | bool bool_default() const { |
| 114 | RTC_DCHECK_EQ(BOOL, type_); |
| 115 | return default_.b; |
| 116 | } |
| 117 | |
| 118 | int int_default() const { |
| 119 | RTC_DCHECK_EQ(INT, type_); |
| 120 | return default_.i; |
| 121 | } |
| 122 | |
| 123 | double float_default() const { |
| 124 | RTC_DCHECK_EQ(FLOAT, type_); |
| 125 | return default_.f; |
| 126 | } |
| 127 | |
| 128 | const char* string_default() const { |
| 129 | RTC_DCHECK_EQ(STRING, type_); |
| 130 | return default_.s; |
| 131 | } |
| 132 | |
| 133 | // Resets a flag to its default value |
| 134 | void SetToDefault(); |
| 135 | |
| 136 | // Iteration support |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 137 | Flag* next() const { return next_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 138 | |
| 139 | // Prints flag information. The current flag value is only printed |
| 140 | // if print_current_value is set. |
| 141 | void Print(bool print_current_value); |
| 142 | |
| 143 | private: |
| 144 | const char* file_; |
| 145 | const char* name_; |
| 146 | const char* comment_; |
| 147 | |
| 148 | Type type_; |
| 149 | FlagValue* variable_; |
| 150 | FlagValue default_; |
| 151 | |
| 152 | Flag* next_; |
| 153 | |
| 154 | friend class FlagList; // accesses next_ |
| 155 | }; |
| 156 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 157 | // Internal use only. |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 158 | #define WEBRTC_DEFINE_FLAG(type, c_type, name, default, comment) \ |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 159 | /* define and initialize the flag */ \ |
| 160 | c_type FLAG_##name = (default); \ |
| 161 | /* register the flag */ \ |
| 162 | static rtc::Flag Flag_##name(__FILE__, #name, (comment), rtc::Flag::type, \ |
| 163 | &FLAG_##name, \ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 164 | rtc::FlagValue::New_##type(default)) |
| 165 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 166 | // Internal use only. |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 167 | #define WEBRTC_DECLARE_FLAG(c_type, name) \ |
| 168 | /* declare the external flag */ \ |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 169 | extern c_type FLAG_##name |
| 170 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 171 | // Use the following macros to define a new flag: |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 172 | #define WEBRTC_DEFINE_bool(name, default, comment) \ |
| 173 | WEBRTC_DEFINE_FLAG(BOOL, bool, name, default, comment) |
| 174 | #define WEBRTC_DEFINE_int(name, default, comment) \ |
| 175 | WEBRTC_DEFINE_FLAG(INT, int, name, default, comment) |
| 176 | #define WEBRTC_DEFINE_float(name, default, comment) \ |
| 177 | WEBRTC_DEFINE_FLAG(FLOAT, double, name, default, comment) |
| 178 | #define WEBRTC_DEFINE_string(name, default, comment) \ |
| 179 | WEBRTC_DEFINE_FLAG(STRING, const char*, name, default, comment) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 180 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 181 | // Use the following macros to declare a flag defined elsewhere: |
Mirko Bonadei | 2dfa998 | 2018-10-18 11:35:32 +0200 | [diff] [blame] | 182 | #define WEBRTC_DECLARE_bool(name) WEBRTC_DECLARE_FLAG(bool, name) |
| 183 | #define WEBRTC_DECLARE_int(name) WEBRTC_DECLARE_FLAG(int, name) |
| 184 | #define WEBRTC_DECLARE_float(name) WEBRTC_DECLARE_FLAG(double, name) |
| 185 | #define WEBRTC_DECLARE_string(name) WEBRTC_DECLARE_FLAG(const char*, name) |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 186 | |
| 187 | // The global list of all flags. |
| 188 | class FlagList { |
| 189 | public: |
| 190 | FlagList(); |
| 191 | |
| 192 | // The null-terminated list of all flags. Traverse with Flag::next(). |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 193 | static Flag* list() { return list_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 194 | |
| 195 | // If file != nullptr, prints information for all flags defined in file; |
| 196 | // otherwise prints information for all flags in all files. The current flag |
| 197 | // value is only printed if print_current_value is set. |
| 198 | static void Print(const char* file, bool print_current_value); |
| 199 | |
| 200 | // Lookup a flag by name. Returns the matching flag or null. |
| 201 | static Flag* Lookup(const char* name); |
| 202 | |
| 203 | // Helper function to parse flags: Takes an argument arg and splits it into |
| 204 | // a flag name and flag value (or null if they are missing). is_bool is set |
| 205 | // if the arg started with "-no" or "--no". The buffer may be used to NUL- |
| 206 | // terminate the name, it must be large enough to hold any possible name. |
| 207 | static void SplitArgument(const char* arg, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 208 | char* buffer, |
| 209 | int buffer_size, |
| 210 | const char** name, |
| 211 | const char** value, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 212 | bool* is_bool); |
| 213 | |
| 214 | // Set the flag values by parsing the command line. If remove_flags |
| 215 | // is set, the flags and associated values are removed from (argc, |
| 216 | // argv). Returns 0 if no error occurred. Otherwise, returns the |
| 217 | // argv index > 0 for the argument where an error occurred. In that |
| 218 | // case, (argc, argv) will remain unchanged indepdendent of the |
| 219 | // remove_flags value, and no assumptions about flag settings should |
| 220 | // be made. |
| 221 | // |
| 222 | // The following syntax for flags is accepted (both '-' and '--' are ok): |
| 223 | // |
| 224 | // --flag (bool flags only) |
| 225 | // --noflag (bool flags only) |
| 226 | // --flag=value (non-bool flags only, no spaces around '=') |
| 227 | // --flag value (non-bool flags only) |
| 228 | static int SetFlagsFromCommandLine(int* argc, |
| 229 | const char** argv, |
| 230 | bool remove_flags); |
| 231 | static inline int SetFlagsFromCommandLine(int* argc, |
| 232 | char** argv, |
| 233 | bool remove_flags) { |
| 234 | return SetFlagsFromCommandLine(argc, const_cast<const char**>(argv), |
| 235 | remove_flags); |
| 236 | } |
| 237 | |
| 238 | // Registers a new flag. Called during program initialization. Not |
| 239 | // thread-safe. |
| 240 | static void Register(Flag* flag); |
| 241 | |
| 242 | private: |
| 243 | static Flag* list_; |
| 244 | }; |
| 245 | |
| 246 | #if defined(WEBRTC_WIN) |
| 247 | // A helper class to translate Windows command line arguments into UTF8, |
| 248 | // which then allows us to just pass them to the flags system. |
| 249 | // This encapsulates all the work of getting the command line and translating |
| 250 | // it to an array of 8-bit strings; all you have to do is create one of these, |
| 251 | // and then call argc() and argv(). |
| 252 | class WindowsCommandLineArguments { |
| 253 | public: |
| 254 | WindowsCommandLineArguments(); |
| 255 | ~WindowsCommandLineArguments(); |
| 256 | |
| 257 | int argc() { return argc_; } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 258 | char** argv() { return argv_; } |
| 259 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 260 | private: |
| 261 | int argc_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 262 | char** argv_; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 263 | |
| 264 | private: |
| 265 | RTC_DISALLOW_COPY_AND_ASSIGN(WindowsCommandLineArguments); |
| 266 | }; |
| 267 | #endif // WEBRTC_WIN |
| 268 | |
| 269 | } // namespace rtc |
| 270 | |
Henrik Kjellander | c036276 | 2017-06-29 08:03:04 +0200 | [diff] [blame] | 271 | #endif // SHARED_COMMANDLINEFLAGS_FLAGS_H_ |