| 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_COMMANDLINEFLAG_H_ |
| 17 | #define ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_ |
| 18 |
| 19 | #include <stddef.h> |
| 20 | #include <stdint.h> |
| 21 |
| 22 | #include <memory> |
| 23 | #include <string> |
| 24 | #include <typeinfo> |
| 25 |
| 26 | #include "absl/base/config.h" |
| 27 | #include "absl/base/internal/fast_type_id.h" |
| 28 | #include "absl/base/macros.h" |
| 29 | #include "absl/flags/config.h" |
| 30 | #include "absl/flags/marshalling.h" |
| 31 | #include "absl/strings/string_view.h" |
| 32 | #include "absl/types/optional.h" |
| 33 |
| 34 | namespace absl { |
| 35 | ABSL_NAMESPACE_BEGIN |
| 36 | namespace flags_internal { |
| 37 |
| 38 | // An alias for flag fast type id. This value identifies the flag value type |
| 39 | // simialarly to typeid(T), without relying on RTTI being available. In most |
| 40 | // cases this id is enough to uniquely identify the flag's value type. In a few |
| 41 | // cases we'll have to resort to using actual RTTI implementation if it is |
| 42 | // available. |
| 43 | using FlagFastTypeId = base_internal::FastTypeIdType; |
| 44 |
| 45 | // Options that control SetCommandLineOptionWithMode. |
| 46 | enum FlagSettingMode { |
| 47 | // update the flag's value unconditionally (can call this multiple times). |
| 48 | SET_FLAGS_VALUE, |
| 49 | // update the flag's value, but *only if* it has not yet been updated |
| 50 | // with SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef". |
| 51 | SET_FLAG_IF_DEFAULT, |
| 52 | // set the flag's default value to this. If the flag has not been updated |
| 53 | // yet (via SET_FLAGS_VALUE, SET_FLAG_IF_DEFAULT, or "FLAGS_xxx = nondef") |
| 54 | // change the flag's current value to the new default value as well. |
| 55 | SET_FLAGS_DEFAULT |
| 56 | }; |
| 57 |
| 58 | // Options that control ParseFrom: Source of a value. |
| 59 | enum ValueSource { |
| 60 | // Flag is being set by value specified on a command line. |
| 61 | kCommandLine, |
| 62 | // Flag is being set by value specified in the code. |
| 63 | kProgrammaticChange, |
| 64 | }; |
| 65 |
| 66 | // Handle to FlagState objects. Specific flag state objects will restore state |
| 67 | // of a flag produced this flag state from method CommandLineFlag::SaveState(). |
| 68 | class FlagStateInterface { |
| 69 | public: |
| 70 | virtual ~FlagStateInterface(); |
| 71 |
| 72 | // Restores the flag originated this object to the saved state. |
| 73 | virtual void Restore() const = 0; |
| 74 | }; |
| 75 |
| 76 | // Holds all information for a flag. |
| 77 | class CommandLineFlag { |
| 78 | public: |
| 79 | constexpr CommandLineFlag() = default; |
| 80 |
| 81 | // Not copyable/assignable. |
| 82 | CommandLineFlag(const CommandLineFlag&) = delete; |
| 83 | CommandLineFlag& operator=(const CommandLineFlag&) = delete; |
| 84 |
| 85 | // Non-polymorphic access methods. |
| 86 |
| 87 | // Return true iff flag has type T. |
| 88 | template <typename T> |
| 89 | inline bool IsOfType() const { |
| 90 | return TypeId() == base_internal::FastTypeId<T>(); |
| 91 | } |
| 92 |
| 93 | // Attempts to retrieve the flag value. Returns value on success, |
| 94 | // absl::nullopt otherwise. |
| 95 | template <typename T> |
| 96 | absl::optional<T> Get() const { |
| 97 | if (IsRetired() || !IsOfType<T>()) { |
| 98 | return absl::nullopt; |
| 99 | } |
| 100 |
| 101 | // Implementation notes: |
| 102 | // |
| 103 | // We are wrapping a union around the value of `T` to serve three purposes: |
| 104 | // |
| 105 | // 1. `U.value` has correct size and alignment for a value of type `T` |
| 106 | // 2. The `U.value` constructor is not invoked since U's constructor does |
| 107 | // not do it explicitly. |
| 108 | // 3. The `U.value` destructor is invoked since U's destructor does it |
| 109 | // explicitly. This makes `U` a kind of RAII wrapper around non default |
| 110 | // constructible value of T, which is destructed when we leave the |
| 111 | // scope. We do need to destroy U.value, which is constructed by |
| 112 | // CommandLineFlag::Read even though we left it in a moved-from state |
| 113 | // after std::move. |
| 114 | // |
| 115 | // All of this serves to avoid requiring `T` being default constructible. |
| 116 | union U { |
| 117 | T value; |
| 118 | U() {} |
| 119 | ~U() { value.~T(); } |
| 120 | }; |
| 121 | U u; |
| 122 |
| 123 | Read(&u.value); |
| 124 | return std::move(u.value); |
| 125 | } |
| 126 |
| 127 | // Polymorphic access methods |
| 128 |
| 129 | // Returns name of this flag. |
| 130 | virtual absl::string_view Name() const = 0; |
| 131 | // Returns name of the file where this flag is defined. |
| 132 | virtual std::string Filename() const = 0; |
| 133 | // Returns name of the flag's value type for some built-in types or empty |
| 134 | // string. |
| 135 | virtual absl::string_view Typename() const = 0; |
| 136 | // Returns help message associated with this flag. |
| 137 | virtual std::string Help() const = 0; |
| 138 | // Returns true iff this object corresponds to retired flag. |
| 139 | virtual bool IsRetired() const; |
| 140 | // Returns true iff this is a handle to an Abseil Flag. |
| 141 | virtual bool IsAbseilFlag() const; |
| 142 | // Returns id of the flag's value type. |
| 143 | virtual FlagFastTypeId TypeId() const = 0; |
| 144 | virtual bool IsModified() const = 0; |
| 145 | virtual bool IsSpecifiedOnCommandLine() const = 0; |
| 146 | virtual std::string DefaultValue() const = 0; |
| 147 | virtual std::string CurrentValue() const = 0; |
| 148 |
| 149 | // Interfaces to operate on validators. |
| 150 | virtual bool ValidateInputValue(absl::string_view value) const = 0; |
| 151 |
| 152 | // Interface to save flag to some persistent state. Returns current flag state |
| 153 | // or nullptr if flag does not support saving and restoring a state. |
| 154 | virtual std::unique_ptr<FlagStateInterface> SaveState() = 0; |
| 155 |
| 156 | // Sets the value of the flag based on specified string `value`. If the flag |
| 157 | // was successfully set to new value, it returns true. Otherwise, sets `error` |
| 158 | // to indicate the error, leaves the flag unchanged, and returns false. There |
| 159 | // are three ways to set the flag's value: |
| 160 | // * Update the current flag value |
| 161 | // * Update the flag's default value |
| 162 | // * Update the current flag value if it was never set before |
| 163 | // The mode is selected based on `set_mode` parameter. |
| 164 | virtual bool ParseFrom(absl::string_view value, |
| 165 | flags_internal::FlagSettingMode set_mode, |
| 166 | flags_internal::ValueSource source, |
| 167 | std::string* error) = 0; |
| 168 |
| 169 | // Checks that flags default value can be converted to string and back to the |
| 170 | // flag's value type. |
| 171 | virtual void CheckDefaultValueParsingRoundtrip() const = 0; |
| 172 |
| 173 | protected: |
| 174 | ~CommandLineFlag() = default; |
| 175 |
| 176 | private: |
| 177 | // Copy-construct a new value of the flag's type in a memory referenced by |
| 178 | // the dst based on the current flag's value. |
| 179 | virtual void Read(void* dst) const = 0; |
| 180 | }; |
| 181 |
| 182 | // This macro is the "source of truth" for the list of supported flag built-in |
| 183 | // types. |
| 184 | #define ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(A) \ |
| 185 | A(bool) \ |
| 186 | A(short) \ |
| 187 | A(unsigned short) \ |
| 188 | A(int) \ |
| 189 | A(unsigned int) \ |
| 190 | A(long) \ |
| 191 | A(unsigned long) \ |
| 192 | A(long long) \ |
| 193 | A(unsigned long long) \ |
| 194 | A(double) \ |
| 195 | A(float) \ |
| 196 | A(std::string) \ |
| 197 | A(std::vector<std::string>) |
| 198 |
| 199 | } // namespace flags_internal |
| 200 | ABSL_NAMESPACE_END |
| 201 | } // namespace absl |
| 202 |
| 203 | #endif // ABSL_FLAGS_INTERNAL_COMMANDLINEFLAG_H_ |
| 204 |