| 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_TYPE_ERASED_H_ |
| 17 | #define ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_ |
| 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include "absl/flags/internal/commandlineflag.h" |
| 22 | #include "absl/flags/internal/registry.h" |
| 23 | |
| 24 | // -------------------------------------------------------------------- |
| 25 | // Registry interfaces operating on type erased handles. |
| 26 | |
| 27 | namespace absl { |
| 28 | namespace flags_internal { |
| 29 | |
| 30 | // If a flag named "name" exists, store its current value in *OUTPUT |
| 31 | // and return true. Else return false without changing *OUTPUT. |
| 32 | // Thread-safe. |
| 33 | bool GetCommandLineOption(absl::string_view name, std::string* value); |
| 34 | |
| 35 | // If a flag named "name" exists, store its information in *OUTPUT |
| 36 | // and return true. Else return false without changing *OUTPUT. |
| 37 | // Thread-safe. |
| 38 | bool GetCommandLineFlagInfo(absl::string_view name, |
| 39 | CommandLineFlagInfo* OUTPUT); |
| 40 | |
| 41 | // Returns the CommandLineFlagInfo of the flagname. exit() with an |
| 42 | // error code if name not found. |
| 43 | // Thread-safe. |
| 44 | CommandLineFlagInfo GetCommandLineFlagInfoOrDie(absl::string_view name); |
| 45 | |
| 46 | // Set the value of the flag named "name" to value. If successful, |
| 47 | // returns true. If not successful (e.g., the flag was not found or |
| 48 | // the value is not a valid value), returns false. |
| 49 | // Thread-safe. |
| 50 | bool SetCommandLineOption(absl::string_view name, absl::string_view value); |
| 51 | |
| 52 | bool SetCommandLineOptionWithMode(absl::string_view name, |
| 53 | absl::string_view value, |
| 54 | FlagSettingMode set_mode); |
| 55 | |
| 56 | //----------------------------------------------------------------------------- |
| 57 | |
| 58 | // Returns true iff all of the following conditions are true: |
| 59 | // (a) "name" names a registered flag |
| 60 | // (b) "value" can be parsed succesfully according to the type of the flag |
| 61 | // (c) parsed value passes any validator associated with the flag |
| 62 | bool IsValidFlagValue(absl::string_view name, absl::string_view value); |
| 63 | |
| 64 | //----------------------------------------------------------------------------- |
| 65 | |
| 66 | // Returns true iff a flag named "name" was specified on the command line |
| 67 | // (either directly, or via one of --flagfile or --fromenv or --tryfromenv). |
| 68 | // |
| 69 | // Any non-command-line modification of the flag does not affect the |
| 70 | // result of this function. So for example, if a flag was passed on |
| 71 | // the command line but then reset via SET_FLAGS_DEFAULT, this |
| 72 | // function will still return true. |
| 73 | bool SpecifiedOnCommandLine(absl::string_view name); |
| 74 | |
| 75 | //----------------------------------------------------------------------------- |
| 76 | |
| 77 | // If a flag with specified "name" exists and has type T, store |
| 78 | // its current value in *dst and return true. Else return false |
| 79 | // without touching *dst. T must obey all of the requirements for |
| 80 | // types passed to DEFINE_FLAG. |
| 81 | template <typename T> |
| 82 | inline bool GetByName(absl::string_view name, T* dst) { |
| 83 | CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name); |
| 84 | if (!flag) return false; |
| 85 | |
| 86 | if (auto val = flag->Get<T>()) { |
| 87 | *dst = *val; |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | } // namespace flags_internal |
| 95 | } // namespace absl |
| 96 | |
| 97 | #endif // ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_ |
| 98 | |