marshalling.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. // * `std::optional<T>`
  37. // * `absl::LogSeverity` (provided natively for layering reasons)
  38. //
  39. // Note that support for integral types is implemented using overloads for
  40. // variable-width fundamental types (`short`, `int`, `long`, etc.). However,
  41. // you should prefer the fixed-width integral types (`int32_t`, `uint64_t`,
  42. // etc.) we've noted above within flag definitions.
  43. //
  44. // In addition, several Abseil libraries provide their own custom support for
  45. // Abseil flags. Documentation for these formats is provided in the type's
  46. // `AbslParseFlag()` definition.
  47. //
  48. // The Abseil time library provides the following support for civil time values:
  49. //
  50. // * `absl::CivilSecond`
  51. // * `absl::CivilMinute`
  52. // * `absl::CivilHour`
  53. // * `absl::CivilDay`
  54. // * `absl::CivilMonth`
  55. // * `absl::CivilYear`
  56. //
  57. // and also provides support for the following absolute time values:
  58. //
  59. // * `absl::Duration`
  60. // * `absl::Time`
  61. //
  62. // Additional support for Abseil types will be noted here as it is added.
  63. //
  64. // You can also provide your own custom flags by adding overloads for
  65. // `AbslParseFlag()` and `AbslUnparseFlag()` to your type definitions. (See
  66. // below.)
  67. //
  68. // -----------------------------------------------------------------------------
  69. // Optional Flags
  70. // -----------------------------------------------------------------------------
  71. //
  72. // The Abseil flags library supports flags of type `std::optional<T>` where
  73. // `T` is a type of one of the supported flags. We refer to this flag type as
  74. // an "optional flag." An optional flag is either "valueless", holding no value
  75. // of type `T` (indicating that the flag has not been set) or a value of type
  76. // `T`. The valueless state in C++ code is represented by a value of
  77. // `std::nullopt` for the optional flag.
  78. //
  79. // Using `std::nullopt` as an optional flag's default value allows you to check
  80. // whether such a flag was ever specified on the command line:
  81. //
  82. // if (absl::GetFlag(FLAGS_foo).has_value()) {
  83. // // flag was set on command line
  84. // } else {
  85. // // flag was not passed on command line
  86. // }
  87. //
  88. // Using an optional flag in this manner avoids common workarounds for
  89. // indicating such an unset flag (such as using sentinel values to indicate this
  90. // state).
  91. //
  92. // An optional flag also allows a developer to pass a flag in an "unset"
  93. // valueless state on the command line, allowing the flag to later be set in
  94. // binary logic. An optional flag's valueless state is indicated by the special
  95. // notation of passing the value as an empty string through the syntax `--flag=`
  96. // or `--flag ""`.
  97. //
  98. // $ binary_with_optional --flag_in_unset_state=
  99. // $ binary_with_optional --flag_in_unset_state ""
  100. //
  101. // Note: as a result of the above syntax requirements, an optional flag cannot
  102. // be set to a `T` of any value which unparses to the empty string.
  103. //
  104. // -----------------------------------------------------------------------------
  105. // Adding Type Support for Abseil Flags
  106. // -----------------------------------------------------------------------------
  107. //
  108. // To add support for your user-defined type, add overloads of `AbslParseFlag()`
  109. // and `AbslUnparseFlag()` as free (non-member) functions to your type. If `T`
  110. // is a class type, these functions can be friend function definitions. These
  111. // overloads must be added to the same namespace where the type is defined, so
  112. // that they can be discovered by Argument-Dependent Lookup (ADL).
  113. //
  114. // Example:
  115. //
  116. // namespace foo {
  117. //
  118. // enum OutputMode { kPlainText, kHtml };
  119. //
  120. // // AbslParseFlag converts from a string to OutputMode.
  121. // // Must be in same namespace as OutputMode.
  122. //
  123. // // Parses an OutputMode from the command line flag value `text`. Returns
  124. // // `true` and sets `*mode` on success; returns `false` and sets `*error`
  125. // // on failure.
  126. // bool AbslParseFlag(absl::string_view text,
  127. // OutputMode* mode,
  128. // std::string* error) {
  129. // if (text == "plaintext") {
  130. // *mode = kPlainText;
  131. // return true;
  132. // }
  133. // if (text == "html") {
  134. // *mode = kHtml;
  135. // return true;
  136. // }
  137. // *error = "unknown value for enumeration";
  138. // return false;
  139. // }
  140. //
  141. // // AbslUnparseFlag converts from an OutputMode to a string.
  142. // // Must be in same namespace as OutputMode.
  143. //
  144. // // Returns a textual flag value corresponding to the OutputMode `mode`.
  145. // std::string AbslUnparseFlag(OutputMode mode) {
  146. // switch (mode) {
  147. // case kPlainText: return "plaintext";
  148. // case kHtml: return "html";
  149. // }
  150. // return absl::StrCat(mode);
  151. // }
  152. //
  153. // Notice that neither `AbslParseFlag()` nor `AbslUnparseFlag()` are class
  154. // members, but free functions. `AbslParseFlag/AbslUnparseFlag()` overloads
  155. // for a type should only be declared in the same file and namespace as said
  156. // type. The proper `AbslParseFlag/AbslUnparseFlag()` implementations for a
  157. // given type will be discovered via Argument-Dependent Lookup (ADL).
  158. //
  159. // `AbslParseFlag()` may need, in turn, to parse simpler constituent types
  160. // using `absl::ParseFlag()`. For example, a custom struct `MyFlagType`
  161. // consisting of a `std::pair<int, std::string>` would add an `AbslParseFlag()`
  162. // overload for its `MyFlagType` like so:
  163. //
  164. // Example:
  165. //
  166. // namespace my_flag_type {
  167. //
  168. // struct MyFlagType {
  169. // std::pair<int, std::string> my_flag_data;
  170. // };
  171. //
  172. // bool AbslParseFlag(absl::string_view text, MyFlagType* flag,
  173. // std::string* err);
  174. //
  175. // std::string AbslUnparseFlag(const MyFlagType&);
  176. //
  177. // // Within the implementation, `AbslParseFlag()` will, in turn invoke
  178. // // `absl::ParseFlag()` on its constituent `int` and `std::string` types
  179. // // (which have built-in Abseil flag support).
  180. //
  181. // bool AbslParseFlag(absl::string_view text, MyFlagType* flag,
  182. // std::string* err) {
  183. // std::pair<absl::string_view, absl::string_view> tokens =
  184. // absl::StrSplit(text, ',');
  185. // if (!absl::ParseFlag(tokens.first, &flag->my_flag_data.first, err))
  186. // return false;
  187. // if (!absl::ParseFlag(tokens.second, &flag->my_flag_data.second, err))
  188. // return false;
  189. // return true;
  190. // }
  191. //
  192. // // Similarly, for unparsing, we can simply invoke `absl::UnparseFlag()` on
  193. // // the constituent types.
  194. // std::string AbslUnparseFlag(const MyFlagType& flag) {
  195. // return absl::StrCat(absl::UnparseFlag(flag.my_flag_data.first),
  196. // ",",
  197. // absl::UnparseFlag(flag.my_flag_data.second));
  198. // }
  199. #ifndef ABSL_FLAGS_MARSHALLING_H_
  200. #define ABSL_FLAGS_MARSHALLING_H_
  201. #include "absl/base/config.h"
  202. #include "absl/numeric/int128.h"
  203. #if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL)
  204. #include <optional>
  205. #endif
  206. #include <string>
  207. #include <vector>
  208. #include "absl/strings/string_view.h"
  209. #include "absl/types/optional.h"
  210. namespace absl {
  211. ABSL_NAMESPACE_BEGIN
  212. // Forward declaration to be used inside composable flag parse/unparse
  213. // implementations
  214. template <typename T>
  215. inline bool ParseFlag(absl::string_view input, T* dst, std::string* error);
  216. template <typename T>
  217. inline std::string UnparseFlag(const T& v);
  218. namespace flags_internal {
  219. // Overloads of `AbslParseFlag()` and `AbslUnparseFlag()` for fundamental types.
  220. bool AbslParseFlag(absl::string_view, bool*, std::string*);
  221. bool AbslParseFlag(absl::string_view, short*, std::string*); // NOLINT
  222. bool AbslParseFlag(absl::string_view, unsigned short*, std::string*); // NOLINT
  223. bool AbslParseFlag(absl::string_view, int*, std::string*); // NOLINT
  224. bool AbslParseFlag(absl::string_view, unsigned int*, std::string*); // NOLINT
  225. bool AbslParseFlag(absl::string_view, long*, std::string*); // NOLINT
  226. bool AbslParseFlag(absl::string_view, unsigned long*, std::string*); // NOLINT
  227. bool AbslParseFlag(absl::string_view, long long*, std::string*); // NOLINT
  228. bool AbslParseFlag(absl::string_view, unsigned long long*, // NOLINT
  229. std::string*);
  230. bool AbslParseFlag(absl::string_view, absl::int128*, std::string*); // NOLINT
  231. bool AbslParseFlag(absl::string_view, absl::uint128*, std::string*); // NOLINT
  232. bool AbslParseFlag(absl::string_view, float*, std::string*);
  233. bool AbslParseFlag(absl::string_view, double*, std::string*);
  234. bool AbslParseFlag(absl::string_view, std::string*, std::string*);
  235. bool AbslParseFlag(absl::string_view, std::vector<std::string>*, std::string*);
  236. template <typename T>
  237. bool AbslParseFlag(absl::string_view text, absl::optional<T>* f,
  238. std::string* err) {
  239. if (text.empty()) {
  240. *f = absl::nullopt;
  241. return true;
  242. }
  243. T value;
  244. if (!absl::ParseFlag(text, &value, err)) return false;
  245. *f = std::move(value);
  246. return true;
  247. }
  248. #if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL)
  249. template <typename T>
  250. bool AbslParseFlag(absl::string_view text, std::optional<T>* f,
  251. std::string* err) {
  252. if (text.empty()) {
  253. *f = std::nullopt;
  254. return true;
  255. }
  256. T value;
  257. if (!absl::ParseFlag(text, &value, err)) return false;
  258. *f = std::move(value);
  259. return true;
  260. }
  261. #endif
  262. template <typename T>
  263. bool InvokeParseFlag(absl::string_view input, T* dst, std::string* err) {
  264. // Comment on next line provides a good compiler error message if T
  265. // does not have AbslParseFlag(absl::string_view, T*, std::string*).
  266. return AbslParseFlag(input, dst, err); // Is T missing AbslParseFlag?
  267. }
  268. // Strings and std:: containers do not have the same overload resolution
  269. // considerations as fundamental types. Naming these 'AbslUnparseFlag' means we
  270. // can avoid the need for additional specializations of Unparse (below).
  271. std::string AbslUnparseFlag(absl::string_view v);
  272. std::string AbslUnparseFlag(const std::vector<std::string>&);
  273. template <typename T>
  274. std::string AbslUnparseFlag(const absl::optional<T>& f) {
  275. return f.has_value() ? absl::UnparseFlag(*f) : "";
  276. }
  277. #if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL)
  278. template <typename T>
  279. std::string AbslUnparseFlag(const std::optional<T>& f) {
  280. return f.has_value() ? absl::UnparseFlag(*f) : "";
  281. }
  282. #endif
  283. template <typename T>
  284. std::string Unparse(const T& v) {
  285. // Comment on next line provides a good compiler error message if T does not
  286. // have UnparseFlag.
  287. return AbslUnparseFlag(v); // Is T missing AbslUnparseFlag?
  288. }
  289. // Overloads for builtin types.
  290. std::string Unparse(bool v);
  291. std::string Unparse(short v); // NOLINT
  292. std::string Unparse(unsigned short v); // NOLINT
  293. std::string Unparse(int v); // NOLINT
  294. std::string Unparse(unsigned int v); // NOLINT
  295. std::string Unparse(long v); // NOLINT
  296. std::string Unparse(unsigned long v); // NOLINT
  297. std::string Unparse(long long v); // NOLINT
  298. std::string Unparse(unsigned long long v); // NOLINT
  299. std::string Unparse(absl::int128 v);
  300. std::string Unparse(absl::uint128 v);
  301. std::string Unparse(float v);
  302. std::string Unparse(double v);
  303. } // namespace flags_internal
  304. // ParseFlag()
  305. //
  306. // Parses a string value into a flag value of type `T`. Do not add overloads of
  307. // this function for your type directly; instead, add an `AbslParseFlag()`
  308. // free function as documented above.
  309. //
  310. // Some implementations of `AbslParseFlag()` for types which consist of other,
  311. // constituent types which already have Abseil flag support, may need to call
  312. // `absl::ParseFlag()` on those consituent string values. (See above.)
  313. template <typename T>
  314. inline bool ParseFlag(absl::string_view input, T* dst, std::string* error) {
  315. return flags_internal::InvokeParseFlag(input, dst, error);
  316. }
  317. // UnparseFlag()
  318. //
  319. // Unparses a flag value of type `T` into a string value. Do not add overloads
  320. // of this function for your type directly; instead, add an `AbslUnparseFlag()`
  321. // free function as documented above.
  322. //
  323. // Some implementations of `AbslUnparseFlag()` for types which consist of other,
  324. // constituent types which already have Abseil flag support, may want to call
  325. // `absl::UnparseFlag()` on those constituent types. (See above.)
  326. template <typename T>
  327. inline std::string UnparseFlag(const T& v) {
  328. return flags_internal::Unparse(v);
  329. }
  330. // Overloads for `absl::LogSeverity` can't (easily) appear alongside that type's
  331. // definition because it is layered below flags. See proper documentation in
  332. // base/log_severity.h.
  333. enum class LogSeverity : int;
  334. bool AbslParseFlag(absl::string_view, absl::LogSeverity*, std::string*);
  335. std::string AbslUnparseFlag(absl::LogSeverity);
  336. ABSL_NAMESPACE_END
  337. } // namespace absl
  338. #endif // ABSL_FLAGS_MARSHALLING_H_