diff options
author | Abseil Team <absl-team@google.com> | 2022-03-30 10:48:56 -0700 |
---|---|---|
committer | rogeeff <rogeeff@google.com> | 2022-03-30 14:17:04 -0400 |
commit | cfccbd2eb5b051f3c33f1a5bb441ec3029cb0a24 (patch) | |
tree | ec74dd17754cf4a10e1350e3a1fc58bbf54399b6 /absl/flags/marshalling.h | |
parent | 3204cc0625230e9876f0310a6dea0014210ab325 (diff) |
Export of internal Abseil changes
--
fb671efb2a70f452f17a884b17cf18817b977a8f by Abseil Team <absl-team@google.com>:
Remove extra semicolon in ABSL_INTERNAL_ASSERT_IS_FULL macro
To fix compilation when empty statement warning is treated as error.
PiperOrigin-RevId: 438342663
Change-Id: I3067fbeffa2691888f37554e88f229f24fb55ecc
--
a58c9396f1d88d11347aed36ef2e1b633071363c by Martijn Vels <mvels@google.com>:
Fix kMaxHeight bounds to kMaxDepth for CordrepBtreeNavigator
Added unit test (confirmed failure mode with old code) and extra assertion in the implementation.
PiperOrigin-RevId: 438327463
Change-Id: I32242c86b0c879b8a42cb9a92075e537d588e09f
--
f348e85dbfc9187ef59085fa2b999374f1670338 by Jorge Gorbe Moya <jgorbe@google.com>:
Make the flags enum in `RefcountAndFlags` a named enum to workaround an lldb
issue (https://github.com/llvm/llvm-project/issues/54602).
PiperOrigin-RevId: 438146097
Change-Id: Ibc2ee26489d99de515a779a903b6458dd0befef7
--
a960a3e9fb2a2e3418f806178e73d8566b78bc85 by Gennadiy Rozental <rogeeff@google.com>:
Introduce support for std::optional<T>/absl::optional<T> flag types.
PiperOrigin-RevId: 438129500
Change-Id: I3d925c0a7f9ce9f857277fac3b0bf664ccd3a95c
GitOrigin-RevId: fb671efb2a70f452f17a884b17cf18817b977a8f
Diffstat (limited to 'absl/flags/marshalling.h')
-rw-r--r-- | absl/flags/marshalling.h | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/absl/flags/marshalling.h b/absl/flags/marshalling.h index 7cbc136d..0f63cdc5 100644 --- a/absl/flags/marshalling.h +++ b/absl/flags/marshalling.h @@ -162,14 +162,27 @@ #ifndef ABSL_FLAGS_MARSHALLING_H_ #define ABSL_FLAGS_MARSHALLING_H_ +#include "absl/base/config.h" + +#if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL) +#include <optional> +#endif #include <string> #include <vector> -#include "absl/base/config.h" #include "absl/strings/string_view.h" +#include "absl/types/optional.h" namespace absl { ABSL_NAMESPACE_BEGIN + +// Forward declaration to be used inside composable flag parse/unparse +// implementations +template <typename T> +inline bool ParseFlag(absl::string_view input, T* dst, std::string* error); +template <typename T> +inline std::string UnparseFlag(const T& v); + namespace flags_internal { // Overloads of `AbslParseFlag()` and `AbslUnparseFlag()` for fundamental types. @@ -189,6 +202,36 @@ bool AbslParseFlag(absl::string_view, std::string*, std::string*); bool AbslParseFlag(absl::string_view, std::vector<std::string>*, std::string*); template <typename T> +bool AbslParseFlag(absl::string_view text, absl::optional<T>* f, + std::string* err) { + if (text.empty()) { + *f = absl::nullopt; + return true; + } + T value; + if (!absl::ParseFlag(text, &value, err)) return false; + + *f = std::move(value); + return true; +} + +#if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL) +template <typename T> +bool AbslParseFlag(absl::string_view text, std::optional<T>* f, + std::string* err) { + if (text.empty()) { + *f = std::nullopt; + return true; + } + T value; + if (!absl::ParseFlag(text, &value, err)) return false; + + *f = std::move(value); + return true; +} +#endif + +template <typename T> bool InvokeParseFlag(absl::string_view input, T* dst, std::string* err) { // Comment on next line provides a good compiler error message if T // does not have AbslParseFlag(absl::string_view, T*, std::string*). @@ -202,6 +245,18 @@ std::string AbslUnparseFlag(absl::string_view v); std::string AbslUnparseFlag(const std::vector<std::string>&); template <typename T> +std::string AbslUnparseFlag(const absl::optional<T>& f) { + return f.has_value() ? absl::UnparseFlag(*f) : ""; +} + +#if defined(ABSL_HAVE_STD_OPTIONAL) && !defined(ABSL_USES_STD_OPTIONAL) +template <typename T> +std::string AbslUnparseFlag(const std::optional<T>& f) { + return f.has_value() ? absl::UnparseFlag(*f) : ""; +} +#endif + +template <typename T> std::string Unparse(const T& v) { // Comment on next line provides a good compiler error message if T does not // have UnparseFlag. |