Blame src/base/commandlineflags.h

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