|
#ifndef C10_UTIL_FLAGS_H_ |
|
#define C10_UTIL_FLAGS_H_ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <string> |
|
|
|
#include <c10/macros/Macros.h> |
|
#include <c10/util/Registry.h> |
|
|
|
namespace c10 { |
|
|
|
|
|
|
|
C10_API void SetUsageMessage(const std::string& str); |
|
|
|
|
|
|
|
|
|
C10_API const char* UsageMessage(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C10_API bool ParseCommandLineFlags(int* pargc, char*** pargv); |
|
|
|
|
|
|
|
|
|
C10_API bool CommandLineFlagsHasBeenParsed(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef C10_USE_GFLAGS |
|
|
|
|
|
|
|
|
|
#include <gflags/gflags.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef GFLAGS_DLL_DEFINE_FLAG |
|
#undef GFLAGS_DLL_DEFINE_FLAG |
|
#endif |
|
#ifdef GFLAGS_DLL_DECLARE_FLAG |
|
#undef GFLAGS_DLL_DECLARE_FLAG |
|
#endif |
|
#define GFLAGS_DLL_DEFINE_FLAG C10_EXPORT |
|
#define GFLAGS_DLL_DECLARE_FLAG C10_IMPORT |
|
|
|
|
|
|
|
#ifndef GFLAGS_GFLAGS_H_ |
|
namespace gflags = google; |
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define C10_GFLAGS_DEF_WRAPPER(type, real_type, name, default_value, help_str) \ |
|
DEFINE_##type(name, default_value, help_str); |
|
|
|
#define C10_DEFINE_int(name, default_value, help_str) \ |
|
C10_GFLAGS_DEF_WRAPPER(int32, gflags::int32, name, default_value, help_str) |
|
#define C10_DEFINE_int32(name, default_value, help_str) \ |
|
C10_DEFINE_int(name, default_value, help_str) |
|
#define C10_DEFINE_int64(name, default_value, help_str) \ |
|
C10_GFLAGS_DEF_WRAPPER(int64, gflags::int64, name, default_value, help_str) |
|
#define C10_DEFINE_double(name, default_value, help_str) \ |
|
C10_GFLAGS_DEF_WRAPPER(double, double, name, default_value, help_str) |
|
#define C10_DEFINE_bool(name, default_value, help_str) \ |
|
C10_GFLAGS_DEF_WRAPPER(bool, bool, name, default_value, help_str) |
|
#define C10_DEFINE_string(name, default_value, help_str) \ |
|
C10_GFLAGS_DEF_WRAPPER(string, ::fLS::clstring, name, default_value, help_str) |
|
|
|
|
|
#define C10_GFLAGS_DECLARE_WRAPPER(type, real_type, name) DECLARE_##type(name); |
|
|
|
#define C10_DECLARE_int(name) \ |
|
C10_GFLAGS_DECLARE_WRAPPER(int32, gflags::int32, name) |
|
#define C10_DECLARE_int32(name) C10_DECLARE_int(name) |
|
#define C10_DECLARE_int64(name) \ |
|
C10_GFLAGS_DECLARE_WRAPPER(int64, gflags::int64, name) |
|
#define C10_DECLARE_double(name) \ |
|
C10_GFLAGS_DECLARE_WRAPPER(double, double, name) |
|
#define C10_DECLARE_bool(name) C10_GFLAGS_DECLARE_WRAPPER(bool, bool, name) |
|
#define C10_DECLARE_string(name) \ |
|
C10_GFLAGS_DECLARE_WRAPPER(string, ::fLS::clstring, name) |
|
|
|
|
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
|
|
|
|
namespace c10 { |
|
|
|
class C10_API C10FlagParser { |
|
public: |
|
bool success() { |
|
return success_; |
|
} |
|
|
|
protected: |
|
template <typename T> |
|
bool Parse(const std::string& content, T* value); |
|
bool success_{false}; |
|
}; |
|
|
|
C10_DECLARE_REGISTRY(C10FlagsRegistry, C10FlagParser, const std::string&); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#define C10_DEFINE_typed_var(type, name, default_value, help_str) \ |
|
C10_EXPORT type FLAGS_##name = default_value; \ |
|
namespace c10 { \ |
|
namespace { \ |
|
class C10FlagParser_##name : public C10FlagParser { \ |
|
public: \ |
|
explicit C10FlagParser_##name(const std::string& content) { \ |
|
success_ = C10FlagParser::Parse<type>(content, &FLAGS_##name); \ |
|
} \ |
|
}; \ |
|
} \ |
|
RegistererC10FlagsRegistry g_C10FlagsRegistry_##name( \ |
|
#name, \ |
|
C10FlagsRegistry(), \ |
|
RegistererC10FlagsRegistry::DefaultCreator<C10FlagParser_##name>, \ |
|
"(" #type ", default " #default_value ") " help_str); \ |
|
} |
|
|
|
#define C10_DEFINE_int(name, default_value, help_str) \ |
|
C10_DEFINE_typed_var(int, name, default_value, help_str) |
|
#define C10_DEFINE_int32(name, default_value, help_str) \ |
|
C10_DEFINE_int(name, default_value, help_str) |
|
#define C10_DEFINE_int64(name, default_value, help_str) \ |
|
C10_DEFINE_typed_var(int64_t, name, default_value, help_str) |
|
#define C10_DEFINE_double(name, default_value, help_str) \ |
|
C10_DEFINE_typed_var(double, name, default_value, help_str) |
|
#define C10_DEFINE_bool(name, default_value, help_str) \ |
|
C10_DEFINE_typed_var(bool, name, default_value, help_str) |
|
#define C10_DEFINE_string(name, default_value, help_str) \ |
|
C10_DEFINE_typed_var(std::string, name, default_value, help_str) |
|
|
|
|
|
#define C10_DECLARE_typed_var(type, name) C10_IMPORT extern type FLAGS_##name |
|
|
|
#define C10_DECLARE_int(name) C10_DECLARE_typed_var(int, name) |
|
#define C10_DECLARE_int32(name) C10_DECLARE_int(name) |
|
#define C10_DECLARE_int64(name) C10_DECLARE_typed_var(int64_t, name) |
|
#define C10_DECLARE_double(name) C10_DECLARE_typed_var(double, name) |
|
#define C10_DECLARE_bool(name) C10_DECLARE_typed_var(bool, name) |
|
#define C10_DECLARE_string(name) C10_DECLARE_typed_var(std::string, name) |
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
|
#endif |
|
|