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