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