type_erased.cc
Go to the documentation of this file.
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 #include "absl/flags/internal/type_erased.h"
00017 
00018 #include "absl/base/internal/raw_logging.h"
00019 #include "absl/flags/config.h"
00020 #include "absl/flags/usage_config.h"
00021 #include "absl/strings/str_cat.h"
00022 
00023 namespace absl {
00024 namespace flags_internal {
00025 
00026 bool GetCommandLineOption(absl::string_view name, std::string* value) {
00027   if (name.empty()) return false;
00028   assert(value);
00029 
00030   CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
00031   if (flag == nullptr || flag->IsRetired()) {
00032     return false;
00033   }
00034 
00035   absl::MutexLock l(InitFlagIfNecessary(flag));
00036   *value = flag->CurrentValue();
00037   return true;
00038 }
00039 
00040 bool GetCommandLineFlagInfo(absl::string_view name,
00041                             CommandLineFlagInfo* OUTPUT) {
00042   if (name.empty()) return false;
00043 
00044   CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
00045   if (flag == nullptr || flag->IsRetired()) {
00046     return false;
00047   }
00048 
00049   assert(OUTPUT);
00050   FillCommandLineFlagInfo(flag, OUTPUT);
00051   return true;
00052 }
00053 
00054 CommandLineFlagInfo GetCommandLineFlagInfoOrDie(absl::string_view name) {
00055   CommandLineFlagInfo info;
00056   if (!GetCommandLineFlagInfo(name, &info)) {
00057     ABSL_INTERNAL_LOG(FATAL, absl::StrCat("Flag '", name, "' does not exist"));
00058   }
00059   return info;
00060 }
00061 
00062 // --------------------------------------------------------------------
00063 
00064 bool SetCommandLineOption(absl::string_view name, absl::string_view value) {
00065   return SetCommandLineOptionWithMode(name, value,
00066                                       flags_internal::SET_FLAGS_VALUE);
00067 }
00068 
00069 bool SetCommandLineOptionWithMode(absl::string_view name,
00070                                   absl::string_view value,
00071                                   FlagSettingMode set_mode) {
00072   CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
00073 
00074   if (!flag || flag->IsRetired()) return false;
00075 
00076   std::string error;
00077   if (!flag->SetFromString(value, set_mode, kProgrammaticChange, &error)) {
00078     // Errors here are all of the form: the provided name was a recognized
00079     // flag, but the value was invalid (bad type, or validation failed).
00080     flags_internal::ReportUsageError(error, false);
00081     return false;
00082   }
00083 
00084   return true;
00085 }
00086 
00087 // --------------------------------------------------------------------
00088 
00089 bool IsValidFlagValue(absl::string_view name, absl::string_view value) {
00090   CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
00091   if (flag == nullptr) {
00092     return false;
00093   }
00094 
00095   if (flag->IsRetired()) {
00096     return true;
00097   }
00098 
00099   // No need to lock the flag since we are not mutating it.
00100   void* obj = Clone(flag->op, flag->def);
00101   std::string ignored_error;
00102   const bool result =
00103       flags_internal::Parse(flag->marshalling_op, value, obj, &ignored_error) &&
00104       Validate(flag, obj);
00105   Delete(flag->op, obj);
00106   return result;
00107 }
00108 
00109 // --------------------------------------------------------------------
00110 
00111 bool SpecifiedOnCommandLine(absl::string_view name) {
00112   CommandLineFlag* flag = flags_internal::FindCommandLineFlag(name);
00113   if (flag != nullptr && !flag->IsRetired()) {
00114     absl::MutexLock l(InitFlagIfNecessary(flag));
00115     return flag->IsSpecifiedOnCommandLine();
00116   }
00117   return false;
00118 }
00119 
00120 }  // namespace flags_internal
00121 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15