status_internal.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Y_ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
  15. #define Y_ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
  16. #include <atomic>
  17. #include <cstdint>
  18. #include <memory>
  19. #include <util/generic/string.h>
  20. #include <utility>
  21. #include "y_absl/base/attributes.h"
  22. #include "y_absl/base/config.h"
  23. #include "y_absl/base/nullability.h"
  24. #include "y_absl/container/inlined_vector.h"
  25. #include "y_absl/strings/cord.h"
  26. #include "y_absl/strings/string_view.h"
  27. #include "y_absl/types/optional.h"
  28. #ifndef SWIG
  29. // Disabled for SWIG as it doesn't parse attributes correctly.
  30. namespace y_absl {
  31. Y_ABSL_NAMESPACE_BEGIN
  32. class Y_ABSL_ATTRIBUTE_TRIVIAL_ABI Status;
  33. Y_ABSL_NAMESPACE_END
  34. } // namespace y_absl
  35. #endif // !SWIG
  36. namespace y_absl {
  37. Y_ABSL_NAMESPACE_BEGIN
  38. enum class StatusCode : int;
  39. enum class StatusToStringMode : int;
  40. namespace status_internal {
  41. // Container for status payloads.
  42. struct Payload {
  43. TString type_url;
  44. y_absl::Cord payload;
  45. };
  46. using Payloads = y_absl::InlinedVector<Payload, 1>;
  47. // Reference-counted representation of Status data.
  48. class StatusRep {
  49. public:
  50. StatusRep(y_absl::StatusCode code_arg, y_absl::string_view message_arg,
  51. std::unique_ptr<status_internal::Payloads> payloads_arg)
  52. : ref_(int32_t{1}),
  53. code_(code_arg),
  54. message_(message_arg),
  55. payloads_(std::move(payloads_arg)) {}
  56. y_absl::StatusCode code() const { return code_; }
  57. const TString& message() const { return message_; }
  58. // Ref and unref are const to allow access through a const pointer, and are
  59. // used during copying operations.
  60. void Ref() const { ref_.fetch_add(1, std::memory_order_relaxed); }
  61. void Unref() const;
  62. // Payload methods correspond to the same methods in y_absl::Status.
  63. y_absl::optional<y_absl::Cord> GetPayload(y_absl::string_view type_url) const;
  64. void SetPayload(y_absl::string_view type_url, y_absl::Cord payload);
  65. struct EraseResult {
  66. bool erased;
  67. uintptr_t new_rep;
  68. };
  69. EraseResult ErasePayload(y_absl::string_view type_url);
  70. void ForEachPayload(
  71. y_absl::FunctionRef<void(y_absl::string_view, const y_absl::Cord&)> visitor)
  72. const;
  73. TString ToString(StatusToStringMode mode) const;
  74. bool operator==(const StatusRep& other) const;
  75. bool operator!=(const StatusRep& other) const { return !(*this == other); }
  76. // Returns an equivalent heap allocated StatusRep with refcount 1.
  77. //
  78. // `this` is not safe to be used after calling as it may have been deleted.
  79. y_absl::Nonnull<StatusRep*> CloneAndUnref() const;
  80. private:
  81. mutable std::atomic<int32_t> ref_;
  82. y_absl::StatusCode code_;
  83. // As an internal implementation detail, we guarantee that if status.message()
  84. // is non-empty, then the resulting string_view is null terminated.
  85. // This is required to implement 'StatusMessageAsCStr(...)'
  86. TString message_;
  87. std::unique_ptr<status_internal::Payloads> payloads_;
  88. };
  89. y_absl::StatusCode MapToLocalCode(int value);
  90. // Returns a pointer to a newly-allocated string with the given `prefix`,
  91. // suitable for output as an error message in assertion/`CHECK()` failures.
  92. //
  93. // This is an internal implementation detail for Abseil logging.
  94. Y_ABSL_ATTRIBUTE_PURE_FUNCTION
  95. y_absl::Nonnull<TString*> MakeCheckFailString(
  96. y_absl::Nonnull<const y_absl::Status*> status,
  97. y_absl::Nonnull<const char*> prefix);
  98. } // namespace status_internal
  99. Y_ABSL_NAMESPACE_END
  100. } // namespace y_absl
  101. #endif // Y_ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_