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