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 17 #include <atomic> 18 #include <cstdint> 19 #include <memory> 20 #include <string> 21 #include <utility> 22 23 #include "absl/base/attributes.h" 24 #include "absl/base/config.h" 25 #include "absl/base/nullability.h" 26 #include "absl/container/inlined_vector.h" 27 #include "absl/strings/cord.h" 28 #include "absl/strings/string_view.h" 29 #include "absl/types/optional.h" 30 31 #ifndef SWIG 32 // Disabled for SWIG as it doesn't parse attributes correctly. 33 namespace absl { 34 ABSL_NAMESPACE_BEGIN 35 // Returned Status objects may not be ignored. Codesearch doesn't handle ifdefs 36 // as part of a class definitions (b/6995610), so we use a forward declaration. 37 // 38 // TODO(b/176172494): ABSL_MUST_USE_RESULT should expand to the more strict 39 // [[nodiscard]]. For now, just use [[nodiscard]] directly when it is available. 40 #if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) 41 class [[nodiscard]] ABSL_ATTRIBUTE_TRIVIAL_ABI Status; 42 #else 43 class ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_TRIVIAL_ABI Status; 44 #endif 45 ABSL_NAMESPACE_END 46 } // namespace absl 47 #endif // !SWIG 48 49 namespace absl { 50 ABSL_NAMESPACE_BEGIN 51 52 enum class StatusCode : int; 53 enum class StatusToStringMode : int; 54 55 namespace status_internal { 56 57 // Container for status payloads. 58 struct Payload { 59 std::string type_url; 60 absl::Cord payload; 61 }; 62 63 using Payloads = absl::InlinedVector<Payload, 1>; 64 65 // Reference-counted representation of Status data. 66 class StatusRep { 67 public: StatusRep(absl::StatusCode code_arg,absl::string_view message_arg,std::unique_ptr<status_internal::Payloads> payloads_arg)68 StatusRep(absl::StatusCode code_arg, absl::string_view message_arg, 69 std::unique_ptr<status_internal::Payloads> payloads_arg) 70 : ref_(int32_t{1}), 71 code_(code_arg), 72 message_(message_arg), 73 payloads_(std::move(payloads_arg)) {} 74 code()75 absl::StatusCode code() const { return code_; } message()76 const std::string& message() const { return message_; } 77 78 // Ref and unref are const to allow access through a const pointer, and are 79 // used during copying operations. Ref()80 void Ref() const { ref_.fetch_add(1, std::memory_order_relaxed); } 81 void Unref() const; 82 83 // Payload methods correspond to the same methods in absl::Status. 84 absl::optional<absl::Cord> GetPayload(absl::string_view type_url) const; 85 void SetPayload(absl::string_view type_url, absl::Cord payload); 86 struct EraseResult { 87 bool erased; 88 uintptr_t new_rep; 89 }; 90 EraseResult ErasePayload(absl::string_view type_url); 91 void ForEachPayload( 92 absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor) 93 const; 94 95 std::string ToString(StatusToStringMode mode) const; 96 97 bool operator==(const StatusRep& other) const; 98 bool operator!=(const StatusRep& other) const { return !(*this == other); } 99 100 // Returns an equivalent heap allocated StatusRep with refcount 1. 101 // 102 // `this` is not safe to be used after calling as it may have been deleted. 103 absl::Nonnull<StatusRep*> CloneAndUnref() const; 104 105 private: 106 mutable std::atomic<int32_t> ref_; 107 absl::StatusCode code_; 108 109 // As an internal implementation detail, we guarantee that if status.message() 110 // is non-empty, then the resulting string_view is null terminated. 111 // This is required to implement 'StatusMessageAsCStr(...)' 112 std::string message_; 113 std::unique_ptr<status_internal::Payloads> payloads_; 114 }; 115 116 absl::StatusCode MapToLocalCode(int value); 117 118 // Returns a pointer to a newly-allocated string with the given `prefix`, 119 // suitable for output as an error message in assertion/`CHECK()` failures. 120 // 121 // This is an internal implementation detail for Abseil logging. 122 ABSL_ATTRIBUTE_PURE_FUNCTION 123 absl::Nonnull<std::string*> MakeCheckFailString( 124 absl::Nonnull<const absl::Status*> status, 125 absl::Nonnull<const char*> prefix); 126 127 } // namespace status_internal 128 129 ABSL_NAMESPACE_END 130 } // namespace absl 131 132 #endif // ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_ 133