00001 // 00002 // Copyright 2019 The Abseil Authors. 00003 // 00004 // Licensed under the Apache License, Version 2.0 (the "License"); 00005 // you may not use this file except in compliance with the License. 00006 // You may obtain a copy of the License at 00007 // 00008 // https://www.apache.org/licenses/LICENSE-2.0 00009 // 00010 // Unless required by applicable law or agreed to in writing, software 00011 // distributed under the License is distributed on an "AS IS" BASIS, 00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 // See the License for the specific language governing permissions and 00014 // limitations under the License. 00015 00016 #ifndef ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_ 00017 #define ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_ 00018 00019 #include <string> 00020 00021 #include "absl/flags/internal/commandlineflag.h" 00022 #include "absl/flags/internal/registry.h" 00023 00024 // -------------------------------------------------------------------- 00025 // Registry interfaces operating on type erased handles. 00026 00027 namespace absl { 00028 namespace flags_internal { 00029 00030 // If a flag named "name" exists, store its current value in *OUTPUT 00031 // and return true. Else return false without changing *OUTPUT. 00032 // Thread-safe. 00033 bool GetCommandLineOption(absl::string_view name, std::string* value); 00034 00035 // If a flag named "name" exists, store its information in *OUTPUT 00036 // and return true. Else return false without changing *OUTPUT. 00037 // Thread-safe. 00038 bool GetCommandLineFlagInfo(absl::string_view name, 00039 CommandLineFlagInfo* OUTPUT); 00040 00041 // Returns the CommandLineFlagInfo of the flagname. exit() with an 00042 // error code if name not found. 00043 // Thread-safe. 00044 CommandLineFlagInfo GetCommandLineFlagInfoOrDie(absl::string_view name); 00045 00046 // Set the value of the flag named "name" to value. If successful, 00047 // returns true. If not successful (e.g., the flag was not found or 00048 // the value is not a valid value), returns false. 00049 // Thread-safe. 00050 bool SetCommandLineOption(absl::string_view name, absl::string_view value); 00051 00052 bool SetCommandLineOptionWithMode(absl::string_view name, 00053 absl::string_view value, 00054 FlagSettingMode set_mode); 00055 00056 //----------------------------------------------------------------------------- 00057 00058 // Returns true iff all of the following conditions are true: 00059 // (a) "name" names a registered flag 00060 // (b) "value" can be parsed succesfully according to the type of the flag 00061 // (c) parsed value passes any validator associated with the flag 00062 bool IsValidFlagValue(absl::string_view name, absl::string_view value); 00063 00064 //----------------------------------------------------------------------------- 00065 00066 // Returns true iff a flag named "name" was specified on the command line 00067 // (either directly, or via one of --flagfile or --fromenv or --tryfromenv). 00068 // 00069 // Any non-command-line modification of the flag does not affect the 00070 // result of this function. So for example, if a flag was passed on 00071 // the command line but then reset via SET_FLAGS_DEFAULT, this 00072 // function will still return true. 00073 bool SpecifiedOnCommandLine(absl::string_view name); 00074 00075 //----------------------------------------------------------------------------- 00076 00077 // If a flag with specified "name" exists and has type T, store 00078 // its current value in *dst and return true. Else return false 00079 // without touching *dst. T must obey all of the requirements for 00080 // types passed to DEFINE_FLAG. 00081 template <typename T> 00082 inline bool GetByName(absl::string_view name, T* dst) { 00083 CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name); 00084 if (!flag) return false; 00085 00086 if (auto val = flag->Get<T>()) { 00087 *dst = *val; 00088 return true; 00089 } 00090 00091 return false; 00092 } 00093 00094 } // namespace flags_internal 00095 } // namespace absl 00096 00097 #endif // ABSL_FLAGS_INTERNAL_TYPE_ERASED_H_