blob: e6baf5de4b0c569f8cb96f8a7265e6bffb67fce6 [file] [log] [blame]
Austin Schuhb4691e92020-12-31 12:37:18 -08001//
2// Copyright 2020 The Abseil Authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// -----------------------------------------------------------------------------
17// File: reflection.h
18// -----------------------------------------------------------------------------
19//
20// This file defines the routines to access and operate on an Abseil Flag's
21// reflection handle.
22
23#ifndef ABSL_FLAGS_REFLECTION_H_
24#define ABSL_FLAGS_REFLECTION_H_
25
26#include <string>
27
28#include "absl/base/config.h"
29#include "absl/container/flat_hash_map.h"
30#include "absl/flags/commandlineflag.h"
31#include "absl/flags/internal/commandlineflag.h"
32
33namespace absl {
34ABSL_NAMESPACE_BEGIN
35namespace flags_internal {
36class FlagSaverImpl;
37} // namespace flags_internal
38
39// FindCommandLineFlag()
40//
41// Returns the reflection handle of an Abseil flag of the specified name, or
42// `nullptr` if not found. This function will emit a warning if the name of a
43// 'retired' flag is specified.
44absl::CommandLineFlag* FindCommandLineFlag(absl::string_view name);
45
46// Returns current state of the Flags registry in a form of mapping from flag
47// name to a flag reflection handle.
48absl::flat_hash_map<absl::string_view, absl::CommandLineFlag*> GetAllFlags();
49
50//------------------------------------------------------------------------------
51// FlagSaver
52//------------------------------------------------------------------------------
53//
54// A FlagSaver object stores the state of flags in the scope where the FlagSaver
55// is defined, allowing modification of those flags within that scope and
56// automatic restoration of the flags to their previous state upon leaving the
57// scope.
58//
59// A FlagSaver can be used within tests to temporarily change the test
60// environment and restore the test case to its previous state.
61//
62// Example:
63//
64// void MyFunc() {
65// absl::FlagSaver fs;
66// ...
67// absl::SetFlag(&FLAGS_myFlag, otherValue);
68// ...
69// } // scope of FlagSaver left, flags return to previous state
70//
71// This class is thread-safe.
72
73class FlagSaver {
74 public:
75 FlagSaver();
76 ~FlagSaver();
77
78 FlagSaver(const FlagSaver&) = delete;
79 void operator=(const FlagSaver&) = delete;
80
81 private:
82 flags_internal::FlagSaverImpl* impl_;
83};
84
85//-----------------------------------------------------------------------------
86
87ABSL_NAMESPACE_END
88} // namespace absl
89
90#endif // ABSL_FLAGS_REFLECTION_H_