Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
status.h
Go to the documentation of this file.
1// Copyright 2010-2025 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 ORTOOLS_GLOP_STATUS_H_
15#define ORTOOLS_GLOP_STATUS_H_
16
17#include <string>
18
19namespace operations_research {
20namespace glop {
21
22// Return type for the solver functions that return "Did that work?".
23// It should only be used for unrecoverable errors.
24class Status {
25 public:
26 // Possible kinds of errors.
27 enum ErrorCode {
28 // Not an error. Returned on success.
30
31 // The LU factorization of the current basis couldn't be computed.
33
34 // The current variable values are out of their bound modulo the tolerance.
36
37 // A pointer argument was NULL when it shouldn't be.
39
40 // The linear program is invalid or it does not have the required format.
42 };
43
44 // Creates a "successful" status.
45 Status();
46
47 // Creates a status with the specified error code and error message.
48 // If "code == 0", error_message is ignored and a Status object identical
49 // to Status::OK is constructed.
51
52 // Improves readability but identical to 0-arg constructor.
53 static Status OK() { return Status(); }
54
55 // Accessors.
56 ErrorCode error_code() const { return error_code_; }
57 const std::string& error_message() const { return error_message_; }
58 bool ok() const { return error_code_ == GLOP_OK; }
59
60 private:
61 ErrorCode error_code_;
62 std::string error_message_;
63};
64
65// Returns the string representation of the ErrorCode enum.
66std::string GetErrorCodeString(Status::ErrorCode error_code);
67
68// Macro to simplify error propagation between function returning Status.
69#define GLOP_RETURN_IF_ERROR(function_call) \
70 do { \
71 Status return_status = function_call; \
72 if (!return_status.ok()) return return_status; \
73 } while (false)
74
75// Macro to simplify the creation of an error.
76#define GLOP_RETURN_AND_LOG_ERROR(error_code, message) \
77 do { \
78 std::string error_message = message; \
79 LOG(ERROR) << GetErrorCodeString(error_code) << ": " << error_message; \
80 return Status(error_code, error_message); \
81 } while (false)
82
83// Macro to check that a pointer argument is not null.
84#define GLOP_RETURN_ERROR_IF_NULL(arg) \
85 if (arg == nullptr) { \
86 const std::string variable_name = #arg; \
87 std::string error_message = variable_name + " must not be null."; \
88 LOG(DFATAL) << error_message; \
89 return Status(Status::ERROR_NULL, error_message); \
90 }
91
92} // namespace glop
93} // namespace operations_research
94
95#endif // ORTOOLS_GLOP_STATUS_H_
ErrorCode error_code() const
Definition status.h:56
const std::string & error_message() const
Definition status.h:57
std::string GetErrorCodeString(Status::ErrorCode error_code)
Definition status.cc:30
OR-Tools root namespace.