type_erased.cc
Go to the documentation of this file.
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 
17 
19 #include "absl/flags/config.h"
21 #include "absl/strings/str_cat.h"
22 
23 namespace absl {
24 namespace flags_internal {
25 
27  if (name.empty()) return false;
28  assert(value);
29 
31  if (flag == nullptr || flag->IsRetired()) {
32  return false;
33  }
34 
36  *value = flag->CurrentValue();
37  return true;
38 }
39 
41  CommandLineFlagInfo* OUTPUT) {
42  if (name.empty()) return false;
43 
45  if (flag == nullptr || flag->IsRetired()) {
46  return false;
47  }
48 
49  assert(OUTPUT);
50  FillCommandLineFlagInfo(flag, OUTPUT);
51  return true;
52 }
53 
56  if (!GetCommandLineFlagInfo(name, &info)) {
57  ABSL_INTERNAL_LOG(FATAL, absl::StrCat("Flag '", name, "' does not exist"));
58  }
59  return info;
60 }
61 
62 // --------------------------------------------------------------------
63 
65  return SetCommandLineOptionWithMode(name, value,
67 }
68 
71  FlagSettingMode set_mode) {
73 
74  if (!flag || flag->IsRetired()) return false;
75 
76  std::string error;
77  if (!flag->SetFromString(value, set_mode, kProgrammaticChange, &error)) {
78  // Errors here are all of the form: the provided name was a recognized
79  // flag, but the value was invalid (bad type, or validation failed).
81  return false;
82  }
83 
84  return true;
85 }
86 
87 // --------------------------------------------------------------------
88 
91  if (flag == nullptr) {
92  return false;
93  }
94 
95  if (flag->IsRetired()) {
96  return true;
97  }
98 
99  // No need to lock the flag since we are not mutating it.
100  void* obj = Clone(flag->op, flag->def);
101  std::string ignored_error;
102  const bool result =
103  flags_internal::Parse(flag->marshalling_op, value, obj, &ignored_error) &&
104  Validate(flag, obj);
105  Delete(flag->op, obj);
106  return result;
107 }
108 
109 // --------------------------------------------------------------------
110 
113  if (flag != nullptr && !flag->IsRetired()) {
115  return flag->IsSpecifiedOnCommandLine();
116  }
117  return false;
118 }
119 
120 } // namespace flags_internal
121 } // namespace absl
void Delete(FlagOpFn op, const void *obj)
bool GetCommandLineOption(absl::string_view name, std::string *value)
Definition: type_erased.cc:26
bool Parse(FlagMarshallingOpFn op, absl::string_view text, void *dst, std::string *error)
absl::Mutex * InitFlagIfNecessary(CommandLineFlag *flag) LOCK_RETURNED(flag-> locks->primary_mu)
bool Validate(CommandLineFlag *, const void *)
bool SetCommandLineOption(absl::string_view name, absl::string_view value)
Definition: type_erased.cc:64
void * Clone(FlagOpFn op, const void *obj)
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: str_cat.cc:98
bool IsValidFlagValue(absl::string_view name, absl::string_view value)
Definition: type_erased.cc:89
Definition: algorithm.h:29
bool SetCommandLineOptionWithMode(absl::string_view name, absl::string_view value, FlagSettingMode set_mode)
Definition: type_erased.cc:69
bool GetCommandLineFlagInfo(absl::string_view name, CommandLineFlagInfo *OUTPUT)
Definition: type_erased.cc:40
bool SetFromString(absl::string_view value, flags_internal::FlagSettingMode set_mode, flags_internal::ValueSource source, std::string *error)
#define ABSL_INTERNAL_LOG(severity, message)
Definition: raw_logging.h:73
CommandLineFlagInfo GetCommandLineFlagInfoOrDie(absl::string_view name)
Definition: type_erased.cc:54
size_t value
CommandLineFlag * FindCommandLineFlag(absl::string_view name)
Definition: registry.cc:427
char name[1]
Definition: mutex.cc:296
constexpr bool empty() const noexcept
Definition: string_view.h:277
void FillCommandLineFlagInfo(CommandLineFlag *flag, CommandLineFlagInfo *result)
Definition: registry.cc:408
void ReportUsageError(absl::string_view msg, bool is_fatal)
bool SpecifiedOnCommandLine(absl::string_view name)
Definition: type_erased.cc:111
const FlagMarshallingOpFn marshalling_op


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