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 <memory> 18 #include <string> 19 #include <utility> 20 21 #include "absl/base/attributes.h" 22 #include "absl/container/inlined_vector.h" 23 #include "absl/strings/cord.h" 24 25 #ifndef SWIG 26 // Disabled for SWIG as it doesn't parse attributes correctly. 27 namespace absl { 28 ABSL_NAMESPACE_BEGIN 29 // Returned Status objects may not be ignored. Codesearch doesn't handle ifdefs 30 // as part of a class definitions (b/6995610), so we use a forward declaration. 31 // 32 // TODO(b/176172494): ABSL_MUST_USE_RESULT should expand to the more strict 33 // [[nodiscard]]. For now, just use [[nodiscard]] directly when it is available. 34 #if ABSL_HAVE_CPP_ATTRIBUTE(nodiscard) 35 class [[nodiscard]] Status; 36 #else 37 class ABSL_MUST_USE_RESULT Status; 38 #endif 39 ABSL_NAMESPACE_END 40 } // namespace absl 41 #endif // !SWIG 42 43 namespace absl { 44 ABSL_NAMESPACE_BEGIN 45 46 enum class StatusCode : int; 47 48 namespace status_internal { 49 50 // Container for status payloads. 51 struct Payload { 52 std::string type_url; 53 absl::Cord payload; 54 }; 55 56 using Payloads = absl::InlinedVector<Payload, 1>; 57 58 // Reference-counted representation of Status data. 59 struct StatusRep { StatusRepStatusRep60 StatusRep(absl::StatusCode code_arg, absl::string_view message_arg, 61 std::unique_ptr<status_internal::Payloads> payloads_arg) 62 : ref(int32_t{1}), 63 code(code_arg), 64 message(message_arg), 65 payloads(std::move(payloads_arg)) {} 66 67 std::atomic<int32_t> ref; 68 absl::StatusCode code; 69 std::string message; 70 std::unique_ptr<status_internal::Payloads> payloads; 71 }; 72 73 absl::StatusCode MapToLocalCode(int value); 74 75 // Returns a pointer to a newly-allocated string with the given `prefix`, 76 // suitable for output as an error message in assertion/`CHECK()` failures. 77 // 78 // This is an internal implementation detail for Abseil logging. 79 std::string* MakeCheckFailString(const absl::Status* status, 80 const char* prefix); 81 82 } // namespace status_internal 83 84 ABSL_NAMESPACE_END 85 } // namespace absl 86 87 #endif // ABSL_STATUS_INTERNAL_STATUS_INTERNAL_H_ 88