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