Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 1 | // |
| 2 | // Copyright 2019 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 | #ifndef ABSL_FLAGS_INTERNAL_REGISTRY_H_ |
| 17 | #define ABSL_FLAGS_INTERNAL_REGISTRY_H_ |
| 18 | |
| 19 | #include <functional> |
| 20 | #include <map> |
| 21 | #include <string> |
| 22 | |
| 23 | #include "absl/base/macros.h" |
| 24 | #include "absl/flags/internal/commandlineflag.h" |
| 25 | |
| 26 | // -------------------------------------------------------------------- |
| 27 | // Global flags registry API. |
| 28 | |
| 29 | namespace absl { |
| 30 | namespace flags_internal { |
| 31 | |
| 32 | CommandLineFlag* FindCommandLineFlag(absl::string_view name); |
| 33 | CommandLineFlag* FindRetiredFlag(absl::string_view name); |
| 34 | |
| 35 | // Executes specified visitor for each non-retired flag in the registry. |
| 36 | // Requires the caller hold the registry lock. |
| 37 | void ForEachFlagUnlocked(std::function<void(CommandLineFlag*)> visitor); |
| 38 | // Executes specified visitor for each non-retired flag in the registry. While |
| 39 | // callback are executed, the registry is locked and can't be changed. |
| 40 | void ForEachFlag(std::function<void(CommandLineFlag*)> visitor); |
| 41 | |
| 42 | //----------------------------------------------------------------------------- |
| 43 | |
| 44 | bool RegisterCommandLineFlag(CommandLineFlag*); |
| 45 | |
| 46 | //----------------------------------------------------------------------------- |
| 47 | // Retired registrations: |
| 48 | // |
| 49 | // Retired flag registrations are treated specially. A 'retired' flag is |
| 50 | // provided only for compatibility with automated invocations that still |
| 51 | // name it. A 'retired' flag: |
| 52 | // - is not bound to a C++ FLAGS_ reference. |
| 53 | // - has a type and a value, but that value is intentionally inaccessible. |
| 54 | // - does not appear in --help messages. |
| 55 | // - is fully supported by _all_ flag parsing routines. |
| 56 | // - consumes args normally, and complains about type mismatches in its |
| 57 | // argument. |
| 58 | // - emits a complaint but does not die (e.g. LOG(ERROR)) if it is |
| 59 | // accessed by name through the flags API for parsing or otherwise. |
| 60 | // |
| 61 | // The registrations for a flag happen in an unspecified order as the |
| 62 | // initializers for the namespace-scope objects of a program are run. |
| 63 | // Any number of weak registrations for a flag can weakly define the flag. |
| 64 | // One non-weak registration will upgrade the flag from weak to non-weak. |
| 65 | // Further weak registrations of a non-weak flag are ignored. |
| 66 | // |
| 67 | // This mechanism is designed to support moving dead flags into a |
| 68 | // 'graveyard' library. An example migration: |
| 69 | // |
| 70 | // 0: Remove references to this FLAGS_flagname in the C++ codebase. |
| 71 | // 1: Register as 'retired' in old_lib. |
| 72 | // 2: Make old_lib depend on graveyard. |
| 73 | // 3: Add a redundant 'retired' registration to graveyard. |
| 74 | // 4: Remove the old_lib 'retired' registration. |
| 75 | // 5: Eventually delete the graveyard registration entirely. |
| 76 | // |
| 77 | |
| 78 | // Retire flag with name "name" and type indicated by ops. |
| 79 | bool Retire(const char* name, FlagOpFn ops, |
| 80 | FlagMarshallingOpFn marshalling_ops); |
| 81 | |
| 82 | // Registered a retired flag with name 'flag_name' and type 'T'. |
| 83 | template <typename T> |
| 84 | inline bool RetiredFlag(const char* flag_name) { |
| 85 | return flags_internal::Retire(flag_name, flags_internal::FlagOps<T>, |
| 86 | flags_internal::FlagMarshallingOps<T>); |
| 87 | } |
| 88 | |
| 89 | // If the flag is retired, returns true and indicates in |*type_is_bool| |
| 90 | // whether the type of the retired flag is a bool. |
| 91 | // Only to be called by code that needs to explicitly ignore retired flags. |
| 92 | bool IsRetiredFlag(absl::string_view name, bool* type_is_bool); |
| 93 | |
| 94 | //----------------------------------------------------------------------------- |
| 95 | // Saves the states (value, default value, whether the user has set |
| 96 | // the flag, registered validators, etc) of all flags, and restores |
| 97 | // them when the FlagSaver is destroyed. |
| 98 | // |
| 99 | // This class is thread-safe. However, its destructor writes to |
| 100 | // exactly the set of flags that have changed value during its |
| 101 | // lifetime, so concurrent _direct_ access to those flags |
| 102 | // (i.e. FLAGS_foo instead of {Get,Set}CommandLineOption()) is unsafe. |
| 103 | |
| 104 | class FlagSaver { |
| 105 | public: |
| 106 | FlagSaver(); |
| 107 | ~FlagSaver(); |
| 108 | |
| 109 | FlagSaver(const FlagSaver&) = delete; |
| 110 | void operator=(const FlagSaver&) = delete; |
| 111 | |
| 112 | // Prevents saver from restoring the saved state of flags. |
| 113 | void Ignore(); |
| 114 | |
| 115 | private: |
| 116 | class FlagSaverImpl* impl_; // we use pimpl here to keep API steady |
| 117 | }; |
| 118 | |
| 119 | } // namespace flags_internal |
| 120 | } // namespace absl |
| 121 | |
| 122 | #endif // ABSL_FLAGS_INTERNAL_REGISTRY_H_ |