marshalling.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include <string>
  165. #include <vector>
  166. #include "absl/base/config.h"
  167. #include "absl/strings/string_view.h"
  168. namespace absl {
  169. ABSL_NAMESPACE_BEGIN
  170. namespace flags_internal {
  171. // Overloads of `AbslParseFlag()` and `AbslUnparseFlag()` for fundamental types.
  172. bool AbslParseFlag(absl::string_view, bool*, std::string*);
  173. bool AbslParseFlag(absl::string_view, short*, std::string*); // NOLINT
  174. bool AbslParseFlag(absl::string_view, unsigned short*, std::string*); // NOLINT
  175. bool AbslParseFlag(absl::string_view, int*, std::string*); // NOLINT
  176. bool AbslParseFlag(absl::string_view, unsigned int*, std::string*); // NOLINT
  177. bool AbslParseFlag(absl::string_view, long*, std::string*); // NOLINT
  178. bool AbslParseFlag(absl::string_view, unsigned long*, std::string*); // NOLINT
  179. bool AbslParseFlag(absl::string_view, long long*, std::string*); // NOLINT
  180. bool AbslParseFlag(absl::string_view, unsigned long long*, // NOLINT
  181. std::string*);
  182. bool AbslParseFlag(absl::string_view, float*, std::string*);
  183. bool AbslParseFlag(absl::string_view, double*, std::string*);
  184. bool AbslParseFlag(absl::string_view, std::string*, std::string*);
  185. bool AbslParseFlag(absl::string_view, std::vector<std::string>*, std::string*);
  186. template <typename T>
  187. bool InvokeParseFlag(absl::string_view input, T* dst, std::string* err) {
  188. // Comment on next line provides a good compiler error message if T
  189. // does not have AbslParseFlag(absl::string_view, T*, std::string*).
  190. return AbslParseFlag(input, dst, err); // Is T missing AbslParseFlag?
  191. }
  192. // Strings and std:: containers do not have the same overload resolution
  193. // considerations as fundamental types. Naming these 'AbslUnparseFlag' means we
  194. // can avoid the need for additional specializations of Unparse (below).
  195. std::string AbslUnparseFlag(absl::string_view v);
  196. std::string AbslUnparseFlag(const std::vector<std::string>&);
  197. template <typename T>
  198. std::string Unparse(const T& v) {
  199. // Comment on next line provides a good compiler error message if T does not
  200. // have UnparseFlag.
  201. return AbslUnparseFlag(v); // Is T missing AbslUnparseFlag?
  202. }
  203. // Overloads for builtin types.
  204. std::string Unparse(bool v);
  205. std::string Unparse(short v); // NOLINT
  206. std::string Unparse(unsigned short v); // NOLINT
  207. std::string Unparse(int v); // NOLINT
  208. std::string Unparse(unsigned int v); // NOLINT
  209. std::string Unparse(long v); // NOLINT
  210. std::string Unparse(unsigned long v); // NOLINT
  211. std::string Unparse(long long v); // NOLINT
  212. std::string Unparse(unsigned long long v); // NOLINT
  213. std::string Unparse(float v);
  214. std::string Unparse(double v);
  215. } // namespace flags_internal
  216. // ParseFlag()
  217. //
  218. // Parses a string value into a flag value of type `T`. Do not add overloads of
  219. // this function for your type directly; instead, add an `AbslParseFlag()`
  220. // free function as documented above.
  221. //
  222. // Some implementations of `AbslParseFlag()` for types which consist of other,
  223. // constituent types which already have Abseil flag support, may need to call
  224. // `absl::ParseFlag()` on those consituent string values. (See above.)
  225. template <typename T>
  226. inline bool ParseFlag(absl::string_view input, T* dst, std::string* error) {
  227. return flags_internal::InvokeParseFlag(input, dst, error);
  228. }
  229. // UnparseFlag()
  230. //
  231. // Unparses a flag value of type `T` into a string value. Do not add overloads
  232. // of this function for your type directly; instead, add an `AbslUnparseFlag()`
  233. // free function as documented above.
  234. //
  235. // Some implementations of `AbslUnparseFlag()` for types which consist of other,
  236. // constituent types which already have Abseil flag support, may want to call
  237. // `absl::UnparseFlag()` on those constituent types. (See above.)
  238. template <typename T>
  239. inline std::string UnparseFlag(const T& v) {
  240. return flags_internal::Unparse(v);
  241. }
  242. // Overloads for `absl::LogSeverity` can't (easily) appear alongside that type's
  243. // definition because it is layered below flags. See proper documentation in
  244. // base/log_severity.h.
  245. enum class LogSeverity : int;
  246. bool AbslParseFlag(absl::string_view, absl::LogSeverity*, std::string*);
  247. std::string AbslUnparseFlag(absl::LogSeverity);
  248. ABSL_NAMESPACE_END
  249. } // namespace absl
  250. #endif // ABSL_FLAGS_MARSHALLING_H_