15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/*
25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle
35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2006, Google Inc.
45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without
65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met:
75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer.
105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     and/or other materials provided with the distribution.
135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     derived from this software without specific prior written permission.
155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */
275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Originally comes from shared/commandlineflags/flags.h
305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Flags are defined and declared using DEFINE_xxx and DECLARE_xxx macros,
325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// where xxx is the flag type. Flags are referred to via FLAG_yyy,
335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// where yyy is the flag name. For intialization and iteration of flags,
345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// see the FlagList class. For full programmatic access to any
355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// flag, see the Flag class.
365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org//
375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// The implementation only relies and basic C++ functionality
385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// and needs no special library or STL support.
395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifndef TALK_BASE_FLAGS_H__
415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define TALK_BASE_FLAGS_H__
425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <assert.h>
445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/checks.h"
465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/common.h"
475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Internal use only.
495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgunion FlagValue {
505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Note: Because in C++ non-bool values are silently converted into
515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // bool values ('bool b = "false";' results in b == true!), we pass
525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // and int argument to New_BOOL as this appears to be safer - sigh.
535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // In particular, it prevents the (not uncommon!) bug where a bool
545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // flag is defined via: DEFINE_bool(flag, "false", "some comment");.
555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static FlagValue New_BOOL(int b) {
565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    FlagValue v;
575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    v.b = (b != 0);
585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return v;
595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static FlagValue New_INT(int i) {
625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    FlagValue v;
635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    v.i = i;
645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return v;
655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static FlagValue New_FLOAT(float f) {
685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    FlagValue v;
695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    v.f = f;
705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return v;
715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static FlagValue New_STRING(const char* s) {
745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    FlagValue v;
755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    v.s = s;
765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return v;
775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  bool b;
805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int i;
815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  double f;
825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* s;
835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Each flag can be accessed programmatically via a Flag object.
875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass Flag {
885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public:
895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  enum Type { BOOL, INT, FLOAT, STRING };
905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Internal use only.
925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  Flag(const char* file, const char* name, const char* comment,
935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org       Type type, void* variable, FlagValue default_);
945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // General flag information
965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* file() const  { return file_; }
975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* name() const  { return name_; }
985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* comment() const  { return comment_; }
995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Flag type
1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  Type type() const  { return type_; }
1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Flag variables
1045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  bool* bool_variable() const {
1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == BOOL);
1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return &variable_->b;
1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int* int_variable() const {
1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == INT);
1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return &variable_->i;
1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  double* float_variable() const {
1155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == FLOAT);
1165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return &variable_->f;
1175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char** string_variable() const {
1205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == STRING);
1215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return &variable_->s;
1225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Default values
1255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  bool bool_default() const {
1265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == BOOL);
1275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return default_.b;
1285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int int_default() const {
1315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == INT);
1325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return default_.i;
1335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  double float_default() const {
1365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == FLOAT);
1375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return default_.f;
1385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* string_default() const {
1415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    assert(type_ == STRING);
1425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return default_.s;
1435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
1445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Resets a flag to its default value
1465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  void SetToDefault();
1475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Iteration support
1495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  Flag* next() const  { return next_; }
1505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Prints flag information. The current flag value is only printed
1525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // if print_current_value is set.
1535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  void Print(bool print_current_value);
1545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org private:
1565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* file_;
1575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* name_;
1585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  const char* comment_;
1595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  Type type_;
1615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  FlagValue* variable_;
1625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  FlagValue default_;
1635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  Flag* next_;
1655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  friend class FlagList;  // accesses next_
1675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
1685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Internal use only.
1715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DEFINE_FLAG(type, c_type, name, default, comment) \
1725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  /* define and initialize the flag */                    \
1735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  c_type FLAG_##name = (default);                         \
1745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  /* register the flag */                                 \
1755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static Flag Flag_##name(__FILE__, #name, (comment),   \
1765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                          Flag::type, &FLAG_##name,       \
1775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                          FlagValue::New_##type(default))
1785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Internal use only.
1815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DECLARE_FLAG(c_type, name)              \
1825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  /* declare the external flag */               \
1835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  extern c_type FLAG_##name
1845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Use the following macros to define a new flag:
1875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DEFINE_bool(name, default, comment) \
1885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  DEFINE_FLAG(BOOL, bool, name, default, comment)
1895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DEFINE_int(name, default, comment) \
1905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  DEFINE_FLAG(INT, int, name, default, comment)
1915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DEFINE_float(name, default, comment) \
1925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  DEFINE_FLAG(FLOAT, double, name, default, comment)
1935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DEFINE_string(name, default, comment) \
1945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  DEFINE_FLAG(STRING, const char*, name, default, comment)
1955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Use the following macros to declare a flag defined elsewhere:
1985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DECLARE_bool(name)  DECLARE_FLAG(bool, name)
1995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DECLARE_int(name)  DECLARE_FLAG(int, name)
2005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DECLARE_float(name)  DECLARE_FLAG(double, name)
2015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define DECLARE_string(name)  DECLARE_FLAG(const char*, name)
2025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// The global list of all flags.
2055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass FlagList {
2065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public:
2075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  FlagList();
2085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // The NULL-terminated list of all flags. Traverse with Flag::next().
2105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static Flag* list()  { return list_; }
2115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // If file != NULL, prints information for all flags defined in file;
2135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // otherwise prints information for all flags in all files. The current
2145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // flag value is only printed if print_current_value is set.
2155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static void Print(const char* file, bool print_current_value);
2165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Lookup a flag by name. Returns the matching flag or NULL.
2185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static Flag* Lookup(const char* name);
2195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Helper function to parse flags: Takes an argument arg and splits it into
2215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // a flag name and flag value (or NULL if they are missing). is_bool is set
2225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // if the arg started with "-no" or "--no". The buffer may be used to NUL-
2235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // terminate the name, it must be large enough to hold any possible name.
2245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static void SplitArgument(const char* arg,
2255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                            char* buffer, int buffer_size,
2265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                            const char** name, const char** value,
2275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                            bool* is_bool);
2285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Set the flag values by parsing the command line. If remove_flags
2305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // is set, the flags and associated values are removed from (argc,
2315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // argv). Returns 0 if no error occurred. Otherwise, returns the
2325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // argv index > 0 for the argument where an error occurred. In that
2335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // case, (argc, argv) will remain unchanged indepdendent of the
2345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // remove_flags value, and no assumptions about flag settings should
2355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // be made.
2365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  //
2375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // The following syntax for flags is accepted (both '-' and '--' are ok):
2385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  //
2395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  //   --flag        (bool flags only)
2405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  //   --noflag      (bool flags only)
2415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  //   --flag=value  (non-bool flags only, no spaces around '=')
2425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  //   --flag value  (non-bool flags only)
2435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static int SetFlagsFromCommandLine(int* argc,
2445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                     const char** argv,
2455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                     bool remove_flags);
2465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static inline int SetFlagsFromCommandLine(int* argc,
2475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                            char** argv,
2485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                            bool remove_flags) {
2495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return SetFlagsFromCommandLine(argc, const_cast<const char**>(argv),
2505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                   remove_flags);
2515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
2525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Registers a new flag. Called during program initialization. Not
2545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // thread-safe.
2555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static void Register(Flag* flag);
2565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org private:
2585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static Flag* list_;
2595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
2605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifdef WIN32
2625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// A helper class to translate Windows command line arguments into UTF8,
2635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// which then allows us to just pass them to the flags system.
2645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// This encapsulates all the work of getting the command line and translating
2655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// it to an array of 8-bit strings; all you have to do is create one of these,
2665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// and then call argc() and argv().
2675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass WindowsCommandLineArguments {
2685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public:
2695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  WindowsCommandLineArguments();
2705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  ~WindowsCommandLineArguments();
2715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int argc() { return argc_; }
2735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  char **argv() { return argv_; }
2745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org private:
2755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  int argc_;
2765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  char **argv_;
2775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org private:
2795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  DISALLOW_EVIL_CONSTRUCTORS(WindowsCommandLineArguments);
2805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
2815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif  // WIN32
2825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
2845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif  // SHARED_COMMANDLINEFLAGS_FLAGS_H__
285