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 // -----------------------------------------------------------------------------
17 // File: marshalling.h
18 // -----------------------------------------------------------------------------
19 //
20 // This header file defines the API for extending Abseil flag support to
21 // custom types, and defines the set of overloads for fundamental types.
22 //
23 // Out of the box, the Abseil flags library supports the following types:
24 //
25 // * `bool`
26 // * `int16_t`
27 // * `uint16_t`
28 // * `int32_t`
29 // * `uint32_t`
30 // * `int64_t`
31 // * `uint64_t`
32 // * `float`
33 // * `double`
34 // * `std::string`
35 // * `std::vector<std::string>`
36 // * `absl::LogSeverity` (provided natively for layering reasons)
37 //
38 // Note that support for integral types is implemented using overloads for
39 // variable-width fundamental types (`short`, `int`, `long`, etc.). However,
40 // you should prefer the fixed-width integral types (`int32_t`, `uint64_t`,
41 // etc.) we've noted above within flag definitions.
42 //
43 // In addition, several Abseil libraries provide their own custom support for
44 // Abseil flags. Documentation for these formats is provided in the type's
45 // `AbslParseFlag()` definition.
46 //
47 // The Abseil time library provides the following support for civil time values:
48 //
49 // * `absl::CivilSecond`
50 // * `absl::CivilMinute`
51 // * `absl::CivilHour`
52 // * `absl::CivilDay`
53 // * `absl::CivilMonth`
54 // * `absl::CivilYear`
55 //
56 // and also provides support for the following absolute time values:
57 //
58 // * `absl::Duration`
59 // * `absl::Time`
60 //
61 // Additional support for Abseil types will be noted here as it is added.
62 //
63 // You can also provide your own custom flags by adding overloads for
64 // `AbslParseFlag()` and `AbslUnparseFlag()` to your type definitions. (See
65 // below.)
66 //
67 // -----------------------------------------------------------------------------
68 // Adding Type Support for Abseil Flags
69 // -----------------------------------------------------------------------------
70 //
71 // To add support for your user-defined type, add overloads of `AbslParseFlag()`
72 // and `AbslUnparseFlag()` as free (non-member) functions to your type. If `T`
73 // is a class type, these functions can be friend function definitions. These
74 // overloads must be added to the same namespace where the type is defined, so
75 // that they can be discovered by Argument-Dependent Lookup (ADL).
76 //
77 // Example:
78 //
79 // namespace foo {
80 //
81 // enum OutputMode { kPlainText, kHtml };
82 //
83 // // AbslParseFlag converts from a string to OutputMode.
84 // // Must be in same namespace as OutputMode.
85 //
86 // // Parses an OutputMode from the command line flag value `text`. Returns
87 // // `true` and sets `*mode` on success; returns `false` and sets `*error`
88 // // on failure.
89 // bool AbslParseFlag(absl::string_view text,
90 // OutputMode* mode,
91 // std::string* error) {
92 // if (text == "plaintext") {
93 // *mode = kPlainText;
94 // return true;
95 // }
96 // if (text == "html") {
97 // *mode = kHtml;
98 // return true;
99 // }
100 // *error = "unknown value for enumeration";
101 // return false;
102 // }
103 //
104 // // AbslUnparseFlag converts from an OutputMode to a string.
105 // // Must be in same namespace as OutputMode.
106 //
107 // // Returns a textual flag value corresponding to the OutputMode `mode`.
108 // std::string AbslUnparseFlag(OutputMode mode) {
109 // switch (mode) {
110 // case kPlainText: return "plaintext";
111 // case kHtml: return "html";
112 // }
113 // return absl::StrCat(mode);
114 // }
115 //
116 // Notice that neither `AbslParseFlag()` nor `AbslUnparseFlag()` are class
117 // members, but free functions. `AbslParseFlag/AbslUnparseFlag()` overloads
118 // for a type should only be declared in the same file and namespace as said
119 // type. The proper `AbslParseFlag/AbslUnparseFlag()` implementations for a
120 // given type will be discovered via Argument-Dependent Lookup (ADL).
121 //
122 // `AbslParseFlag()` may need, in turn, to parse simpler constituent types
123 // using `absl::ParseFlag()`. For example, a custom struct `MyFlagType`
124 // consisting of a `std::pair<int, std::string>` would add an `AbslParseFlag()`
125 // overload for its `MyFlagType` like so:
126 //
127 // Example:
128 //
129 // namespace my_flag_type {
130 //
131 // struct MyFlagType {
132 // std::pair<int, std::string> my_flag_data;
133 // };
134 //
135 // bool AbslParseFlag(absl::string_view text, MyFlagType* flag,
136 // std::string* err);
137 //
138 // std::string AbslUnparseFlag(const MyFlagType&);
139 //
140 // // Within the implementation, `AbslParseFlag()` will, in turn invoke
141 // // `absl::ParseFlag()` on its constituent `int` and `std::string` types
142 // // (which have built-in Abseil flag support).
143 //
144 // bool AbslParseFlag(absl::string_view text, MyFlagType* flag,
145 // std::string* err) {
146 // std::pair<absl::string_view, absl::string_view> tokens =
147 // absl::StrSplit(text, ',');
148 // if (!absl::ParseFlag(tokens.first, &flag->my_flag_data.first, err))
149 // return false;
150 // if (!absl::ParseFlag(tokens.second, &flag->my_flag_data.second, err))
151 // return false;
152 // return true;
153 // }
154 //
155 // // Similarly, for unparsing, we can simply invoke `absl::UnparseFlag()` on
156 // // the constituent types.
157 // std::string AbslUnparseFlag(const MyFlagType& flag) {
158 // return absl::StrCat(absl::UnparseFlag(flag.my_flag_data.first),
159 // ",",
160 // absl::UnparseFlag(flag.my_flag_data.second));
161 // }
162 #ifndef ABSL_FLAGS_MARSHALLING_H_
163 #define ABSL_FLAGS_MARSHALLING_H_
164
165 #include <string>
166 #include <vector>
167
168 #include "absl/base/config.h"
169 #include "absl/strings/string_view.h"
170
171 namespace absl {
172 ABSL_NAMESPACE_BEGIN
173 namespace flags_internal {
174
175 // Overloads of `AbslParseFlag()` and `AbslUnparseFlag()` for fundamental types.
176 bool AbslParseFlag(absl::string_view, bool*, std::string*);
177 bool AbslParseFlag(absl::string_view, short*, std::string*); // NOLINT
178 bool AbslParseFlag(absl::string_view, unsigned short*, std::string*); // NOLINT
179 bool AbslParseFlag(absl::string_view, int*, std::string*); // NOLINT
180 bool AbslParseFlag(absl::string_view, unsigned int*, std::string*); // NOLINT
181 bool AbslParseFlag(absl::string_view, long*, std::string*); // NOLINT
182 bool AbslParseFlag(absl::string_view, unsigned long*, std::string*); // NOLINT
183 bool AbslParseFlag(absl::string_view, long long*, std::string*); // NOLINT
184 bool AbslParseFlag(absl::string_view, unsigned long long*, // NOLINT
185 std::string*);
186 bool AbslParseFlag(absl::string_view, float*, std::string*);
187 bool AbslParseFlag(absl::string_view, double*, std::string*);
188 bool AbslParseFlag(absl::string_view, std::string*, std::string*);
189 bool AbslParseFlag(absl::string_view, std::vector<std::string>*, std::string*);
190
191 template <typename T>
InvokeParseFlag(absl::string_view input,T * dst,std::string * err)192 bool InvokeParseFlag(absl::string_view input, T* dst, std::string* err) {
193 // Comment on next line provides a good compiler error message if T
194 // does not have AbslParseFlag(absl::string_view, T*, std::string*).
195 return AbslParseFlag(input, dst, err); // Is T missing AbslParseFlag?
196 }
197
198 // Strings and std:: containers do not have the same overload resolution
199 // considerations as fundamental types. Naming these 'AbslUnparseFlag' means we
200 // can avoid the need for additional specializations of Unparse (below).
201 std::string AbslUnparseFlag(absl::string_view v);
202 std::string AbslUnparseFlag(const std::vector<std::string>&);
203
204 template <typename T>
Unparse(const T & v)205 std::string Unparse(const T& v) {
206 // Comment on next line provides a good compiler error message if T does not
207 // have UnparseFlag.
208 return AbslUnparseFlag(v); // Is T missing AbslUnparseFlag?
209 }
210
211 // Overloads for builtin types.
212 std::string Unparse(bool v);
213 std::string Unparse(short v); // NOLINT
214 std::string Unparse(unsigned short v); // NOLINT
215 std::string Unparse(int v); // NOLINT
216 std::string Unparse(unsigned int v); // NOLINT
217 std::string Unparse(long v); // NOLINT
218 std::string Unparse(unsigned long v); // NOLINT
219 std::string Unparse(long long v); // NOLINT
220 std::string Unparse(unsigned long long v); // NOLINT
221 std::string Unparse(float v);
222 std::string Unparse(double v);
223
224 } // namespace flags_internal
225
226 // ParseFlag()
227 //
228 // Parses a string value into a flag value of type `T`. Do not add overloads of
229 // this function for your type directly; instead, add an `AbslParseFlag()`
230 // free function as documented above.
231 //
232 // Some implementations of `AbslParseFlag()` for types which consist of other,
233 // constituent types which already have Abseil flag support, may need to call
234 // `absl::ParseFlag()` on those consituent string values. (See above.)
235 template <typename T>
ParseFlag(absl::string_view input,T * dst,std::string * error)236 inline bool ParseFlag(absl::string_view input, T* dst, std::string* error) {
237 return flags_internal::InvokeParseFlag(input, dst, error);
238 }
239
240 // UnparseFlag()
241 //
242 // Unparses a flag value of type `T` into a string value. Do not add overloads
243 // of this function for your type directly; instead, add an `AbslUnparseFlag()`
244 // free function as documented above.
245 //
246 // Some implementations of `AbslUnparseFlag()` for types which consist of other,
247 // constituent types which already have Abseil flag support, may want to call
248 // `absl::UnparseFlag()` on those constituent types. (See above.)
249 template <typename T>
UnparseFlag(const T & v)250 inline std::string UnparseFlag(const T& v) {
251 return flags_internal::Unparse(v);
252 }
253
254 // Overloads for `absl::LogSeverity` can't (easily) appear alongside that type's
255 // definition because it is layered below flags. See proper documentation in
256 // base/log_severity.h.
257 enum class LogSeverity : int;
258 bool AbslParseFlag(absl::string_view, absl::LogSeverity*, std::string*);
259 std::string AbslUnparseFlag(absl::LogSeverity);
260
261 ABSL_NAMESPACE_END
262 } // namespace absl
263
264 #endif // ABSL_FLAGS_MARSHALLING_H_
265