Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
status_builder.h
Go to the documentation of this file.
1// Copyright 2010-2024 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_STATUS_BUILDER_H_
15#define OR_TOOLS_BASE_STATUS_BUILDER_H_
16
17#include <ios>
18#include <sstream>
19#include <string>
20#include <utility>
21
22#include "absl/status/status.h"
23#include "absl/strings/str_cat.h"
24
25namespace util {
26
28 public:
29 explicit StatusBuilder(const absl::StatusCode code)
30 : base_status_(code, /*msg=*/{}) {}
31
32 explicit StatusBuilder(const absl::Status status)
33 : base_status_(std::move(status)) {}
34
36 : base_status_(other.base_status_),
37 ss_(other.ss_.str(), std::ios_base::app) {}
39 base_status_ = other.base_status_;
40 ss_ = std::ostringstream(other.ss_.str(), std::ios_base::app);
41 return *this;
42 }
43
45 : base_status_(std::exchange(other.base_status_, absl::OkStatus())),
46 ss_(std::move(other.ss_)) {}
48 base_status_ = std::exchange(other.base_status_, absl::OkStatus());
49 ss_ = std::move(other.ss_);
50 return *this;
51 }
52
53 operator absl::Status() const { // NOLINT
54 const std::string annotation = ss_.str();
55 if (annotation.empty()) {
56 return base_status_;
57 }
58 if (base_status_.message().empty()) {
59 return absl::Status(base_status_.code(), annotation);
60 }
61 const std::string annotated_message =
62 absl::StrCat(base_status_.message(), "; ", annotation);
63 return absl::Status(base_status_.code(), annotated_message);
64 }
65
66 template <class T>
68 ss_ << t;
69 return *this;
70 }
71
72 StatusBuilder& SetAppend() { return *this; }
73
74 private:
75 absl::Status base_status_;
76 std::ostringstream ss_;
77};
78
80 return StatusBuilder(absl::StatusCode::kAborted);
81}
82
84 return StatusBuilder(absl::StatusCode::kAlreadyExists);
85}
86
88 return StatusBuilder(absl::StatusCode::kCancelled);
89}
90
92 return StatusBuilder(absl::StatusCode::kDataLoss);
93}
94
96 return StatusBuilder(absl::StatusCode::kDeadlineExceeded);
97}
98
100 return StatusBuilder(absl::StatusCode::kFailedPrecondition);
101}
102
104 return StatusBuilder(absl::StatusCode::kInternal);
105}
106
108 return StatusBuilder(absl::StatusCode::kInvalidArgument);
109}
110
112 return StatusBuilder(absl::StatusCode::kNotFound);
113}
114
116 return StatusBuilder(absl::StatusCode::kOutOfRange);
117}
118
120 return StatusBuilder(absl::StatusCode::kPermissionDenied);
121}
122
124 return StatusBuilder(absl::StatusCode::kUnauthenticated);
125}
126
128 return StatusBuilder(absl::StatusCode::kResourceExhausted);
129}
130
132 return StatusBuilder(absl::StatusCode::kUnavailable);
133}
134
136 return StatusBuilder(absl::StatusCode::kUnimplemented);
137}
138
140 return StatusBuilder(absl::StatusCode::kUnknown);
141}
142
143} // namespace util
144
145#endif // OR_TOOLS_BASE_STATUS_BUILDER_H_
StatusBuilder(const absl::Status status)
StatusBuilder & operator<<(const T &t)
StatusBuilder & operator=(StatusBuilder &&other)
StatusBuilder(const StatusBuilder &other)
StatusBuilder & SetAppend()
StatusBuilder & operator=(const StatusBuilder &other)
StatusBuilder(const absl::StatusCode code)
StatusBuilder(StatusBuilder &&other)
absl::Status status
Definition g_gurobi.cc:44
STL namespace.
A collections of i/o utilities for the Graph classes in ./graph.h.
StatusBuilder ResourceExhaustedErrorBuilder()
StatusBuilder UnknownErrorBuilder()
StatusBuilder InternalErrorBuilder()
StatusBuilder UnimplementedErrorBuilder()
StatusBuilder UnavailableErrorBuilder()
StatusBuilder DataLossErrorBuilder()
StatusBuilder UnauthenticatedErrorBuilder()
StatusBuilder AbortedErrorBuilder()
StatusBuilder AlreadyExistsErrorBuilder()
StatusBuilder OutOfRangeErrorBuilder()
StatusBuilder CancelledErrorBuilder()
StatusBuilder PermissionDeniedErrorBuilder()
StatusBuilder InvalidArgumentErrorBuilder()
StatusBuilder DeadlineExceededErrorBuilder()
StatusBuilder FailedPreconditionErrorBuilder()
StatusBuilder NotFoundErrorBuilder()