diff options
Diffstat (limited to 'absl/status/status.cc')
-rw-r--r-- | absl/status/status.cc | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/absl/status/status.cc b/absl/status/status.cc index 88e8eda9..bbf2335d 100644 --- a/absl/status/status.cc +++ b/absl/status/status.cc @@ -16,6 +16,7 @@ #include <errno.h> #include <cassert> +#include <utility> #include "absl/base/internal/raw_logging.h" #include "absl/base/internal/strerror.h" @@ -77,15 +78,17 @@ std::ostream& operator<<(std::ostream& os, StatusCode code) { namespace status_internal { -static int FindPayloadIndexByUrl(const Payloads* payloads, - absl::string_view type_url) { - if (payloads == nullptr) return -1; +static absl::optional<size_t> FindPayloadIndexByUrl( + const Payloads* payloads, + absl::string_view type_url) { + if (payloads == nullptr) + return absl::nullopt; for (size_t i = 0; i < payloads->size(); ++i) { if ((*payloads)[i].type_url == type_url) return i; } - return -1; + return absl::nullopt; } // Convert canonical code to a value known to this binary. @@ -119,8 +122,10 @@ absl::StatusCode MapToLocalCode(int value) { absl::optional<absl::Cord> Status::GetPayload( absl::string_view type_url) const { const auto* payloads = GetPayloads(); - int index = status_internal::FindPayloadIndexByUrl(payloads, type_url); - if (index != -1) return (*payloads)[index].payload; + absl::optional<size_t> index = + status_internal::FindPayloadIndexByUrl(payloads, type_url); + if (index.has_value()) + return (*payloads)[index.value()].payload; return absl::nullopt; } @@ -135,10 +140,10 @@ void Status::SetPayload(absl::string_view type_url, absl::Cord payload) { rep->payloads = absl::make_unique<status_internal::Payloads>(); } - int index = + absl::optional<size_t> index = status_internal::FindPayloadIndexByUrl(rep->payloads.get(), type_url); - if (index != -1) { - (*rep->payloads)[index].payload = std::move(payload); + if (index.has_value()) { + (*rep->payloads)[index.value()].payload = std::move(payload); return; } @@ -146,10 +151,11 @@ void Status::SetPayload(absl::string_view type_url, absl::Cord payload) { } bool Status::ErasePayload(absl::string_view type_url) { - int index = status_internal::FindPayloadIndexByUrl(GetPayloads(), type_url); - if (index != -1) { + absl::optional<size_t> index = + status_internal::FindPayloadIndexByUrl(GetPayloads(), type_url); + if (index.has_value()) { PrepareToModify(); - GetPayloads()->erase(GetPayloads()->begin() + index); + GetPayloads()->erase(GetPayloads()->begin() + index.value()); if (GetPayloads()->empty() && message().empty()) { // Special case: If this can be represented inlined, it MUST be // inlined (EqualsSlow depends on this behavior). |