status_internal.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
  15. #define ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
  16. #include <atomic>
  17. #include <cstdint>
  18. #include <memory>
  19. #include <string>
  20. #include <utility>
  21. #include "absl/base/attributes.h"
  22. #include "absl/base/config.h"
  23. #include "absl/base/nullability.h"
  24. #include "absl/container/inlined_vector.h"
  25. #include "absl/strings/cord.h"
  26. #include "absl/strings/string_view.h"
  27. #include "absl/types/optional.h"
  28. #ifndef SWIG
  29. // Disabled for SWIG as it doesn't parse attributes correctly.
  30. namespace absl {
  31. ABSL_NAMESPACE_BEGIN
  32. // Returned Status objects may not be ignored. Codesearch doesn't handle ifdefs
  33. // as part of a class definitions (b/6995610), so we use a forward declaration.
  34. //
  35. // TODO(b/176172494): ABSL_MUST_USE_RESULT should expand to the more strict
  36. // [[nodiscard]]. For now, just use [[nodiscard]] directly when it is available.
  37. #if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
  38. class [[nodiscard]] ABSL_ATTRIBUTE_TRIVIAL_ABI Status;
  39. #else
  40. class ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_TRIVIAL_ABI Status;
  41. #endif
  42. ABSL_NAMESPACE_END
  43. } // namespace absl
  44. #endif // !SWIG
  45. namespace absl {
  46. ABSL_NAMESPACE_BEGIN
  47. enum class StatusCode : int;
  48. enum class StatusToStringMode : int;
  49. namespace status_internal {
  50. // Container for status payloads.
  51. struct Payload {
  52. std::string type_url;
  53. absl::Cord payload;
  54. };
  55. using Payloads = absl::InlinedVector<Payload, 1>;
  56. // Reference-counted representation of Status data.
  57. class StatusRep {
  58. public:
  59. StatusRep(absl::StatusCode code_arg, absl::string_view message_arg,
  60. std::unique_ptr<status_internal::Payloads> payloads_arg)
  61. : ref_(int32_t{1}),
  62. code_(code_arg),
  63. message_(message_arg),
  64. payloads_(std::move(payloads_arg)) {}
  65. absl::StatusCode code() const { return code_; }
  66. const std::string& message() const { return message_; }
  67. // Ref and unref are const to allow access through a const pointer, and are
  68. // used during copying operations.
  69. void Ref() const { ref_.fetch_add(1, std::memory_order_relaxed); }
  70. void Unref() const;
  71. // Payload methods correspond to the same methods in absl::Status.
  72. absl::optional<absl::Cord> GetPayload(absl::string_view type_url) const;
  73. void SetPayload(absl::string_view type_url, absl::Cord payload);
  74. struct EraseResult {
  75. bool erased;
  76. uintptr_t new_rep;
  77. };
  78. EraseResult ErasePayload(absl::string_view type_url);
  79. void ForEachPayload(
  80. absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor)
  81. const;
  82. std::string ToString(StatusToStringMode mode) const;
  83. bool operator==(const StatusRep& other) const;
  84. bool operator!=(const StatusRep& other) const { return !(*this == other); }
  85. // Returns an equivalent heap allocated StatusRep with refcount 1.
  86. //
  87. // `this` is not safe to be used after calling as it may have been deleted.
  88. absl::Nonnull<StatusRep*> CloneAndUnref() const;
  89. private:
  90. mutable std::atomic<int32_t> ref_;
  91. absl::StatusCode code_;
  92. // As an internal implementation detail, we guarantee that if status.message()
  93. // is non-empty, then the resulting string_view is null terminated.
  94. // This is required to implement 'StatusMessageAsCStr(...)'
  95. std::string message_;
  96. std::unique_ptr<status_internal::Payloads> payloads_;
  97. };
  98. absl::StatusCode MapToLocalCode(int value);
  99. // Returns a pointer to a newly-allocated string with the given `prefix`,
  100. // suitable for output as an error message in assertion/`CHECK()` failures.
  101. //
  102. // This is an internal implementation detail for Abseil logging.
  103. ABSL_ATTRIBUTE_PURE_FUNCTION
  104. absl::Nonnull<std::string*> MakeCheckFailString(
  105. absl::Nonnull<const absl::Status*> status,
  106. absl::Nonnull<const char*> prefix);
  107. } // namespace status_internal
  108. ABSL_NAMESPACE_END
  109. } // namespace absl
  110. #endif // ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_