Add DEFINE_validator macro for convenient registration of a flag validator.
git-svn-id: http://gflags.googlecode.com/svn/trunk/src@82 6586e3c6-dcc4-952a-343f-ff74eb82781d
diff --git a/gflags/gflags.h.in b/gflags/gflags.h.in
index dd51a2d..eb084f5 100644
--- a/gflags/gflags.h.in
+++ b/gflags/gflags.h.in
@@ -135,6 +135,11 @@
bool (*validate_fn)(const char*,
const std::string&));
+// Convenience macro for the registration of a flag validator
+#define DEFINE_validator(name, validator) \
+ static const bool name##_validator_registered = \
+ @ac_google_namespace@::RegisterFlagValidator(&FLAGS_##name, validator)
+
// --------------------------------------------------------------------
// These methods are the best way to get access to info about the
diff --git a/gflags_unittest.cc b/gflags_unittest.cc
index 08abc1b..cce60d9 100644
--- a/gflags_unittest.cc
+++ b/gflags_unittest.cc
@@ -143,9 +143,7 @@
static bool AlwaysFail(const char* flag, bool value) { return value == false; }
DEFINE_bool(always_fail, false, "will fail to validate when you set it");
-namespace {
-bool dummy = RegisterFlagValidator(&FLAGS_always_fail, AlwaysFail);
-}
+DEFINE_validator(always_fail, AlwaysFail);
// See the comment by GetAllFlags in gflags.h
static bool DeadlockIfCantLockInValidators(const char* flag, bool value) {
@@ -160,10 +158,7 @@
false,
"will deadlock if set to true and "
"if locking of registry in validators fails.");
-namespace {
-bool dummy1 = RegisterFlagValidator(&FLAGS_deadlock_if_cant_lock,
- DeadlockIfCantLockInValidators);
-}
+DEFINE_validator(deadlock_if_cant_lock, DeadlockIfCantLockInValidators);
#define MAKEFLAG(x) DEFINE_int32(test_flag_num##x, x, "Test flag")