Blame src/base/commandlineflags.h

Packit Service 9def5d
// Copyright (c) 2008, Google Inc.
Packit Service 9def5d
// All rights reserved.
Packit Service 9def5d
// 
Packit Service 9def5d
// Redistribution and use in source and binary forms, with or without
Packit Service 9def5d
// modification, are permitted provided that the following conditions are
Packit Service 9def5d
// met:
Packit Service 9def5d
// 
Packit Service 9def5d
//     * Redistributions of source code must retain the above copyright
Packit Service 9def5d
// notice, this list of conditions and the following disclaimer.
Packit Service 9def5d
//     * Redistributions in binary form must reproduce the above
Packit Service 9def5d
// copyright notice, this list of conditions and the following disclaimer
Packit Service 9def5d
// in the documentation and/or other materials provided with the
Packit Service 9def5d
// distribution.
Packit Service 9def5d
//     * Neither the name of Google Inc. nor the names of its
Packit Service 9def5d
// contributors may be used to endorse or promote products derived from
Packit Service 9def5d
// this software without specific prior written permission.
Packit Service 9def5d
// 
Packit Service 9def5d
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 9def5d
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 9def5d
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 9def5d
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 9def5d
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 9def5d
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 9def5d
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 9def5d
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 9def5d
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 9def5d
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 9def5d
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 9def5d
Packit Service 9def5d
// ---
Packit Service 9def5d
// This file is a compatibility layer that defines Google's version of
Packit Service 9def5d
// command line flags that are used for configuration.
Packit Service 9def5d
//
Packit Service 9def5d
// We put flags into their own namespace.  It is purposefully
Packit Service 9def5d
// named in an opaque way that people should have trouble typing
Packit Service 9def5d
// directly.  The idea is that DEFINE puts the flag in the weird
Packit Service 9def5d
// namespace, and DECLARE imports the flag from there into the
Packit Service 9def5d
// current namespace.  The net result is to force people to use
Packit Service 9def5d
// DECLARE to get access to a flag, rather than saying
Packit Service 9def5d
//   extern bool FLAGS_logtostderr;
Packit Service 9def5d
// or some such instead.  We want this so we can put extra
Packit Service 9def5d
// functionality (like sanity-checking) in DECLARE if we want,
Packit Service 9def5d
// and make sure it is picked up everywhere.
Packit Service 9def5d
//
Packit Service 9def5d
// We also put the type of the variable in the namespace, so that
Packit Service 9def5d
// people can't DECLARE_int32 something that they DEFINE_bool'd
Packit Service 9def5d
// elsewhere.
Packit Service 9def5d
#ifndef BASE_COMMANDLINEFLAGS_H__
Packit Service 9def5d
#define BASE_COMMANDLINEFLAGS_H__
Packit Service 9def5d
Packit Service 9def5d
#include "config.h"
Packit Service 9def5d
#include <string>
Packit Service 9def5d
#include <string.h>               // for memchr
Packit Service 9def5d
#include <stdlib.h>               // for getenv
Packit Service 9def5d
Packit Service 9def5d
#ifdef HAVE_LIB_GFLAGS
Packit Service 9def5d
Packit Service 9def5d
#include <gflags/gflags.h>
Packit Service 9def5d
Packit Service 9def5d
#else
Packit Service 9def5d
Packit Service 9def5d
#include "glog/logging.h"
Packit Service 9def5d
Packit Service 9def5d
#define DECLARE_VARIABLE(type, shorttype, name, tn)                     \
Packit Service 9def5d
  namespace fL##shorttype {                                             \
Packit Service 9def5d
    extern GOOGLE_GLOG_DLL_DECL type FLAGS_##name;                      \
Packit Service 9def5d
  }                                                                     \
Packit Service 9def5d
  using fL##shorttype::FLAGS_##name
Packit Service 9def5d
#define DEFINE_VARIABLE(type, shorttype, name, value, meaning, tn)      \
Packit Service 9def5d
  namespace fL##shorttype {                                             \
Packit Service 9def5d
    GOOGLE_GLOG_DLL_DECL type FLAGS_##name(value);                      \
Packit Service 9def5d
    char FLAGS_no##name;                                                \
Packit Service 9def5d
  }                                                                     \
Packit Service 9def5d
  using fL##shorttype::FLAGS_##name
Packit Service 9def5d
Packit Service 9def5d
// bool specialization
Packit Service 9def5d
#define DECLARE_bool(name) \
Packit Service 9def5d
  DECLARE_VARIABLE(bool, B, name, bool)
Packit Service 9def5d
#define DEFINE_bool(name, value, meaning) \
Packit Service 9def5d
  DEFINE_VARIABLE(bool, B, name, value, meaning, bool)
Packit Service 9def5d
Packit Service 9def5d
// int32 specialization
Packit Service 9def5d
#define DECLARE_int32(name) \
Packit Service 9def5d
  DECLARE_VARIABLE(GOOGLE_NAMESPACE::int32, I, name, int32)
Packit Service 9def5d
#define DEFINE_int32(name, value, meaning) \
Packit Service 9def5d
  DEFINE_VARIABLE(GOOGLE_NAMESPACE::int32, I, name, value, meaning, int32)
Packit Service 9def5d
Packit Service 9def5d
// Special case for string, because we have to specify the namespace
Packit Service 9def5d
// std::string, which doesn't play nicely with our FLAG__namespace hackery.
Packit Service 9def5d
#define DECLARE_string(name)                                            \
Packit Service 9def5d
  namespace fLS {                                                       \
Packit Service 9def5d
    extern GOOGLE_GLOG_DLL_DECL std::string& FLAGS_##name;              \
Packit Service 9def5d
  }                                                                     \
Packit Service 9def5d
  using fLS::FLAGS_##name
Packit Service 9def5d
#define DEFINE_string(name, value, meaning)                             \
Packit Service 9def5d
  namespace fLS {                                                       \
Packit Service 9def5d
    std::string FLAGS_##name##_buf(value);                              \
Packit Service 9def5d
    GOOGLE_GLOG_DLL_DECL std::string& FLAGS_##name = FLAGS_##name##_buf; \
Packit Service 9def5d
    char FLAGS_no##name;                                                \
Packit Service 9def5d
  }                                                                     \
Packit Service 9def5d
  using fLS::FLAGS_##name
Packit Service 9def5d
Packit Service 9def5d
#endif  // HAVE_LIB_GFLAGS
Packit Service 9def5d
Packit Service 9def5d
// Define GLOG_DEFINE_* using DEFINE_* . By using these macros, we
Packit Service 9def5d
// have GLOG_* environ variables even if we have gflags installed.
Packit Service 9def5d
//
Packit Service 9def5d
// If both an environment variable and a flag are specified, the value
Packit Service 9def5d
// specified by a flag wins. E.g., if GLOG_v=0 and --v=1, the
Packit Service 9def5d
// verbosity will be 1, not 0.
Packit Service 9def5d
Packit Service 9def5d
#define GLOG_DEFINE_bool(name, value, meaning) \
Packit Service 9def5d
  DEFINE_bool(name, EnvToBool("GLOG_" #name, value), meaning)
Packit Service 9def5d
Packit Service 9def5d
#define GLOG_DEFINE_int32(name, value, meaning) \
Packit Service 9def5d
  DEFINE_int32(name, EnvToInt("GLOG_" #name, value), meaning)
Packit Service 9def5d
Packit Service 9def5d
#define GLOG_DEFINE_string(name, value, meaning) \
Packit Service 9def5d
  DEFINE_string(name, EnvToString("GLOG_" #name, value), meaning)
Packit Service 9def5d
Packit Service 9def5d
// These macros (could be functions, but I don't want to bother with a .cc
Packit Service 9def5d
// file), make it easier to initialize flags from the environment.
Packit Service 9def5d
Packit Service 9def5d
#define EnvToString(envname, dflt)   \
Packit Service 9def5d
  (!getenv(envname) ? (dflt) : getenv(envname))
Packit Service 9def5d
Packit Service 9def5d
#define EnvToBool(envname, dflt)   \
Packit Service 9def5d
  (!getenv(envname) ? (dflt) : memchr("tTyY1\0", getenv(envname)[0], 6) != NULL)
Packit Service 9def5d
Packit Service 9def5d
#define EnvToInt(envname, dflt)  \
Packit Service 9def5d
  (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10))
Packit Service 9def5d
Packit Service 9def5d
#endif  // BASE_COMMANDLINEFLAGS_H__