Coverage Report

Created: 2026-05-30 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/status/internal/status_internal.h
Line
Count
Source
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
42
    Status;
43
#else
44
class ABSL_MUST_USE_RESULT ABSL_ATTRIBUTE_TRIVIAL_ABI
45
    Status;
46
#endif
47
ABSL_NAMESPACE_END
48
}  // namespace absl
49
#endif  // !SWIG
50
51
namespace absl {
52
ABSL_NAMESPACE_BEGIN
53
54
enum class StatusCode : int;
55
enum class StatusToStringMode : int;
56
57
namespace status_internal {
58
59
// Container for status payloads.
60
struct Payload {
61
  std::string type_url;
62
  absl::Cord payload;
63
};
64
65
using Payloads = absl::InlinedVector<Payload, 1>;
66
67
// Reference-counted representation of Status data.
68
class StatusRep {
69
 public:
70
  StatusRep(absl::StatusCode code_arg, absl::string_view message_arg,
71
            std::unique_ptr<status_internal::Payloads> payloads_arg)
72
2.17k
      : ref_(int32_t{1}),
73
2.17k
        code_(code_arg),
74
2.17k
        message_(message_arg),
75
2.17k
        payloads_(std::move(payloads_arg)) {}
76
77
0
  absl::StatusCode code() const { return code_; }
78
0
  const std::string& message() const { return message_; }
79
80
  // Ref and unref are const to allow access through a const pointer, and are
81
  // used during copying operations.
82
0
  void Ref() const { ref_.fetch_add(1, std::memory_order_relaxed); }
83
  void Unref() const;
84
85
  // Payload methods correspond to the same methods in absl::Status.
86
  absl::optional<absl::Cord> GetPayload(absl::string_view type_url) const;
87
  void SetPayload(absl::string_view type_url, absl::Cord payload);
88
  struct EraseResult {
89
    bool erased;
90
    uintptr_t new_rep;
91
  };
92
  EraseResult ErasePayload(absl::string_view type_url);
93
  void ForEachPayload(
94
      absl::FunctionRef<void(absl::string_view, const absl::Cord&)> visitor)
95
      const;
96
97
  std::string ToString(StatusToStringMode mode) const;
98
99
  bool operator==(const StatusRep& other) const;
100
0
  bool operator!=(const StatusRep& other) const { return !(*this == other); }
101
102
  // Returns an equivalent heap allocated StatusRep with refcount 1.
103
  //
104
  // `this` is not safe to be used after calling as it may have been deleted.
105
  StatusRep* absl_nonnull CloneAndUnref() const;
106
107
 private:
108
  mutable std::atomic<int32_t> ref_;
109
  absl::StatusCode code_;
110
111
  // As an internal implementation detail, we guarantee that if status.message()
112
  // is non-empty, then the resulting string_view is null terminated.
113
  // This is required to implement 'StatusMessageAsCStr(...)'
114
  std::string message_;
115
  std::unique_ptr<status_internal::Payloads> payloads_;
116
};
117
118
absl::StatusCode MapToLocalCode(int value);
119
120
// Returns a pointer to a newly-allocated string with the given `prefix`,
121
// suitable for output as an error message in assertion/`CHECK()` failures.
122
//
123
// This is an internal implementation detail for Abseil logging.
124
ABSL_ATTRIBUTE_PURE_FUNCTION
125
const char* absl_nonnull MakeCheckFailString(
126
    const absl::Status* absl_nonnull status, const char* absl_nonnull prefix);
127
128
}  // namespace status_internal
129
130
ABSL_NAMESPACE_END
131
}  // namespace absl
132
133
#endif  // ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_