summaryrefslogtreecommitdiff
path: root/absl/flags
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2020-06-04 14:05:55 -0700
committerGravatar Gennadiy Rozental <rogeeff@google.com>2020-06-04 20:17:52 -0400
commita8b03d90e0afe03fefa16d4a871ece344a5d52ad (patch)
tree6f36a6a3eb64b3aed28bce3512453b4519c6f478 /absl/flags
parent1d31b5c365e975d3c8a8f90492c3d9de35ef024f (diff)
Export of internal Abseil changes
-- 5b9d5ce21074c0541432555d383d1b9c5898b553 by Gennadiy Rozental <rogeeff@google.com>: Convert FlagSaver into public API PiperOrigin-RevId: 314799739 -- 5f796c9862b1177f161f4b10fe1a84698ebbb5cf by Abseil Team <absl-team@google.com>: Google-internal changes only. PiperOrigin-RevId: 314786474 -- 67f2ae0ac5ae73bcd9d57a058ac4fea8fc1243ba by Gennadiy Rozental <rogeeff@google.com>: Makes sure stacktrace_generic is only used with glibc. Fixes #701 PiperOrigin-RevId: 314726944 -- efb1ef3636b0698b79d3ee3590f12c4dff32a3cb by Samuel Benzaquen <sbenza@google.com>: Take the bits into account when reserving the space for the operation, not just the exponent. PiperOrigin-RevId: 314622744 GitOrigin-RevId: 5b9d5ce21074c0541432555d383d1b9c5898b553 Change-Id: I080a5e333e2dc1545b5aa0417882f7ac7116a20c
Diffstat (limited to 'absl/flags')
-rw-r--r--absl/flags/commandlineflag_test.cc4
-rw-r--r--absl/flags/flag_test.cc2
-rw-r--r--absl/flags/internal/registry.h25
-rw-r--r--absl/flags/internal/usage_test.cc2
-rw-r--r--absl/flags/parse_test.cc2
-rw-r--r--absl/flags/reflection.cc21
-rw-r--r--absl/flags/reflection.h38
-rw-r--r--absl/flags/reflection_test.cc4
8 files changed, 50 insertions, 48 deletions
diff --git a/absl/flags/commandlineflag_test.cc b/absl/flags/commandlineflag_test.cc
index cc8eae91..c5afff61 100644
--- a/absl/flags/commandlineflag_test.cc
+++ b/absl/flags/commandlineflag_test.cc
@@ -47,7 +47,7 @@ class CommandLineFlagTest : public testing::Test {
absl::SetFlagsUsageConfig(default_config);
}
- void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
+ void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
void TearDown() override { flag_saver_.reset(); }
private:
@@ -60,7 +60,7 @@ class CommandLineFlagTest : public testing::Test {
return std::string(fname);
}
- std::unique_ptr<flags::FlagSaver> flag_saver_;
+ std::unique_ptr<absl::FlagSaver> flag_saver_;
};
TEST_F(CommandLineFlagTest, TestAttributesAccessMethods) {
diff --git a/absl/flags/flag_test.cc b/absl/flags/flag_test.cc
index 0c88e24a..2eb2ba71 100644
--- a/absl/flags/flag_test.cc
+++ b/absl/flags/flag_test.cc
@@ -81,7 +81,7 @@ class FlagTest : public testing::Test {
#endif
return std::string(fname);
}
- flags::FlagSaver flag_saver_;
+ absl::FlagSaver flag_saver_;
};
struct S1 {
diff --git a/absl/flags/internal/registry.h b/absl/flags/internal/registry.h
index 1e655a3e..c72eebe2 100644
--- a/absl/flags/internal/registry.h
+++ b/absl/flags/internal/registry.h
@@ -89,31 +89,6 @@ inline bool RetiredFlag(const char* flag_name) {
return flags_internal::Retire(flag_name, base_internal::FastTypeId<T>());
}
-//-----------------------------------------------------------------------------
-// Saves the states (value, default value, whether the user has set
-// the flag, registered validators, etc) of all flags, and restores
-// them when the FlagSaver is destroyed.
-//
-// This class is thread-safe. However, its destructor writes to
-// exactly the set of flags that have changed value during its
-// lifetime, so concurrent _direct_ access to those flags
-// (i.e. FLAGS_foo instead of {Get,Set}CommandLineOption()) is unsafe.
-
-class FlagSaver {
- public:
- FlagSaver();
- ~FlagSaver();
-
- FlagSaver(const FlagSaver&) = delete;
- void operator=(const FlagSaver&) = delete;
-
- // Prevents saver from restoring the saved state of flags.
- void Ignore();
-
- private:
- class FlagSaverImpl* impl_; // we use pimpl here to keep API steady
-};
-
} // namespace flags_internal
ABSL_NAMESPACE_END
} // namespace absl
diff --git a/absl/flags/internal/usage_test.cc b/absl/flags/internal/usage_test.cc
index 46f85b55..6e583fbe 100644
--- a/absl/flags/internal/usage_test.cc
+++ b/absl/flags/internal/usage_test.cc
@@ -89,7 +89,7 @@ class UsageReportingTest : public testing::Test {
}
private:
- flags::FlagSaver flag_saver_;
+ absl::FlagSaver flag_saver_;
};
// --------------------------------------------------------------------
diff --git a/absl/flags/parse_test.cc b/absl/flags/parse_test.cc
index 1ab87690..67ae8aad 100644
--- a/absl/flags/parse_test.cc
+++ b/absl/flags/parse_test.cc
@@ -208,7 +208,7 @@ using testing::ElementsAreArray;
class ParseTest : public testing::Test {
private:
- flags::FlagSaver flag_saver_;
+ absl::FlagSaver flag_saver_;
};
// --------------------------------------------------------------------
diff --git a/absl/flags/reflection.cc b/absl/flags/reflection.cc
index 02bb6c2e..5fc945f2 100644
--- a/absl/flags/reflection.cc
+++ b/absl/flags/reflection.cc
@@ -65,7 +65,8 @@ class FlagRegistry {
static FlagRegistry& GlobalRegistry(); // returns a singleton registry
private:
- friend class FlagSaverImpl; // reads all the flags in order to copy them
+ friend class flags_internal::FlagSaverImpl; // reads all the flags in order
+ // to copy them
friend void ForEachFlagUnlocked(
std::function<void(CommandLineFlag&)> visitor);
@@ -249,15 +250,6 @@ bool Retire(const char* name, FlagFastTypeId type_id) {
// --------------------------------------------------------------------
-// FlagSaver
-// FlagSaverImpl
-// This class stores the states of all flags at construct time,
-// and restores all flags to that state at destruct time.
-// Its major implementation challenge is that it never modifies
-// pointers in the 'main' registry, so global FLAG_* vars always
-// point to the right place.
-// --------------------------------------------------------------------
-
class FlagSaverImpl {
public:
FlagSaverImpl() = default;
@@ -288,11 +280,10 @@ class FlagSaverImpl {
backup_registry_;
};
-FlagSaver::FlagSaver() : impl_(new FlagSaverImpl) { impl_->SaveFromRegistry(); }
+} // namespace flags_internal
-void FlagSaver::Ignore() {
- delete impl_;
- impl_ = nullptr;
+FlagSaver::FlagSaver() : impl_(new flags_internal::FlagSaverImpl) {
+ impl_->SaveFromRegistry();
}
FlagSaver::~FlagSaver() {
@@ -304,8 +295,6 @@ FlagSaver::~FlagSaver() {
// --------------------------------------------------------------------
-} // namespace flags_internal
-
CommandLineFlag* FindCommandLineFlag(absl::string_view name) {
if (name.empty()) return nullptr;
flags_internal::FlagRegistry& registry =
diff --git a/absl/flags/reflection.h b/absl/flags/reflection.h
index e8e24f68..045f9784 100644
--- a/absl/flags/reflection.h
+++ b/absl/flags/reflection.h
@@ -31,6 +31,9 @@
namespace absl {
ABSL_NAMESPACE_BEGIN
+namespace flags_internal {
+class FlagSaverImpl;
+} // namespace flags_internal
// FindCommandLineFlag()
//
@@ -39,6 +42,41 @@ ABSL_NAMESPACE_BEGIN
// 'retired' flag is specified.
CommandLineFlag* FindCommandLineFlag(absl::string_view name);
+//------------------------------------------------------------------------------
+// FlagSaver
+//------------------------------------------------------------------------------
+//
+// A FlagSaver object stores the state of flags in the scope where the FlagSaver
+// is defined, allowing modification of those flags within that scope and
+// automatic restoration of the flags to their previous state upon leaving the
+// scope.
+//
+// A FlagSaver can be used within tests to temporarily change the test
+// environment and restore the test case to its previous state.
+//
+// Example:
+//
+// void MyFunc() {
+// absl::FlagSaver fs;
+// ...
+// absl::SetFlag(FLAGS_myFlag, otherValue);
+// ...
+// } // scope of FlagSaver left, flags return to previous state
+//
+// This class is thread-safe.
+
+class FlagSaver {
+ public:
+ FlagSaver();
+ ~FlagSaver();
+
+ FlagSaver(const FlagSaver&) = delete;
+ void operator=(const FlagSaver&) = delete;
+
+ private:
+ flags_internal::FlagSaverImpl* impl_;
+};
+
//-----------------------------------------------------------------------------
ABSL_NAMESPACE_END
diff --git a/absl/flags/reflection_test.cc b/absl/flags/reflection_test.cc
index 2a137bf7..9781e597 100644
--- a/absl/flags/reflection_test.cc
+++ b/absl/flags/reflection_test.cc
@@ -34,11 +34,11 @@ namespace flags = absl::flags_internal;
class ReflectionTest : public testing::Test {
protected:
- void SetUp() override { flag_saver_ = absl::make_unique<flags::FlagSaver>(); }
+ void SetUp() override { flag_saver_ = absl::make_unique<absl::FlagSaver>(); }
void TearDown() override { flag_saver_.reset(); }
private:
- std::unique_ptr<flags::FlagSaver> flag_saver_;
+ std::unique_ptr<absl::FlagSaver> flag_saver_;
};
// --------------------------------------------------------------------