Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 09:33:36

0001 // Copyright 2019 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 #ifndef ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
0015 #define ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_
0016 
0017 #include <atomic>
0018 #include <cstdint>
0019 #include <memory>
0020 #include <string>
0021 #include <utility>
0022 
0023 #include "absl/base/attributes.h"
0024 #include "absl/base/config.h"
0025 #include "absl/base/nullability.h"
0026 #include "absl/container/inlined_vector.h"
0027 #include "absl/strings/cord.h"
0028 #include "absl/strings/string_view.h"
0029 #include "absl/types/optional.h"
0030 
0031 #ifndef SWIG
0032 // Disabled for SWIG as it doesn't parse attributes correctly.
0033 namespace absl {
0034 ABSL_NAMESPACE_BEGIN
0035 // Returned Status objects may not be ignored. Codesearch doesn't handle ifdefs
0036 // as part of a class definitions (b/6995610), so we use a forward declaration.
0037 //
0038 // TODO(b/176172494): ABSL_MUST_USE_RESULT should expand to the more strict
0039 // [[nodiscard]]. For now, just use [[nodiscard]] directly when it is available.
0040 #if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard)
0041 class [[nodiscard]] ABSL_ATTRIBUTE_TRIVIAL_ABI Status;
0042 #else
0043 class ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_TRIVIAL_ABI Status;
0044 #endif
0045 ABSL_NAMESPACE_END
0046 }  // namespace absl
0047 #endif  // !SWIG
0048 
0049 namespace absl {
0050 ABSL_NAMESPACE_BEGIN
0051 
0052 enum class StatusCode : int;
0053 enum class StatusToStringMode : int;
0054 
0055 namespace status_internal {
0056 
0057 // Container for status payloads.
0058 struct Payload {
0059   std::string type_url;
0060   absl::Cord payload;
0061 };
0062 
0063 using Payloads = absl::InlinedVector<Payload, 1>;
0064 
0065 // Reference-counted representation of Status data.
0066 class StatusRep {
0067  public:
0068   StatusRep(absl::StatusCode code_arg, absl::string_view message_arg,
0069             std::unique_ptr<status_internal::Payloads> payloads_arg)
0070       : ref_(int32_t{1}),
0071         code_(code_arg),
0072         message_(message_arg),
0073         payloads_(std::move(payloads_arg)) {}
0074 
0075   absl::StatusCode code() const { return code_; }
0076   const std::string& message() const { return message_; }
0077 
0078   // Ref and unref are const to allow access through a const pointer, and are
0079   // used during copying operations.
0080   void Ref() const { ref_.fetch_add(1, std::memory_order_relaxed); }
0081   void Unref() const;
0082 
0083   // Payload methods correspond to the same methods in absl::Status.
0084   absl::optional<absl::Cord> GetPayload(absl::string_view type_url) const;
0085   void SetPayload(absl::string_view type_url, absl::Cord payload);
0086   struct EraseResult {
0087     bool erased;
0088     uintptr_t new_rep;
0089   };
0090   EraseResult ErasePayload(absl::string_view type_url);
0091   void ForEachPayload(
0092       absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor)
0093       const;
0094 
0095   std::string ToString(StatusToStringMode mode) const;
0096 
0097   bool operator==(const StatusRep& other) const;
0098   bool operator!=(const StatusRep& other) const { return !(*this == other); }
0099 
0100   // Returns an equivalent heap allocated StatusRep with refcount 1.
0101   //
0102   // `this` is not safe to be used after calling as it may have been deleted.
0103   absl::Nonnull<StatusRep*> CloneAndUnref() const;
0104 
0105  private:
0106   mutable std::atomic<int32_t> ref_;
0107   absl::StatusCode code_;
0108 
0109   // As an internal implementation detail, we guarantee that if status.message()
0110   // is non-empty, then the resulting string_view is null terminated.
0111   // This is required to implement 'StatusMessageAsCStr(...)'
0112   std::string message_;
0113   std::unique_ptr<status_internal::Payloads> payloads_;
0114 };
0115 
0116 absl::StatusCode MapToLocalCode(int value);
0117 
0118 // Returns a pointer to a newly-allocated string with the given `prefix`,
0119 // suitable for output as an error message in assertion/`CHECK()` failures.
0120 //
0121 // This is an internal implementation detail for Abseil logging.
0122 ABSL_ATTRIBUTE_PURE_FUNCTION
0123 absl::Nonnull<std::string*> MakeCheckFailString(
0124     absl::Nonnull<const absl::Status*> status,
0125     absl::Nonnull<const char*> prefix);
0126 
0127 }  // namespace status_internal
0128 
0129 ABSL_NAMESPACE_END
0130 }  // namespace absl
0131 
0132 #endif  // ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_