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
19
namespace
operations_research
{
20
namespace
glop
{
21
22
// Return type for the solver functions that return "Did that work?".
23
// It should only be used for unrecoverable errors.
24
class
Status
{
25
public
:
26
// Possible kinds of errors.
27
enum
ErrorCode
{
28
// Not an error. Returned on success.
29
GLOP_OK
= 0,
30
31
// The LU factorization of the current basis couldn't be computed.
32
ERROR_LU
= 1,
33
34
// The current variable values are out of their bound modulo the tolerance.
35
ERROR_BOUND
= 2,
36
37
// A pointer argument was NULL when it shouldn't be.
38
ERROR_NULL
= 3,
39
40
// The linear program is invalid or it does not have the required format.
41
ERROR_INVALID_PROBLEM
= 4,
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.
50
Status
(
ErrorCode
error_code
, std::string
error_message
);
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.
66
std::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_
operations_research::glop::Status::error_code
ErrorCode error_code() const
Definition
status.h:56
operations_research::glop::Status::OK
static Status OK()
Definition
status.h:53
operations_research::glop::Status::error_message
const std::string & error_message() const
Definition
status.h:57
operations_research::glop::Status::ErrorCode
ErrorCode
Definition
status.h:27
operations_research::glop::Status::GLOP_OK
@ GLOP_OK
Definition
status.h:29
operations_research::glop::Status::ERROR_INVALID_PROBLEM
@ ERROR_INVALID_PROBLEM
Definition
status.h:41
operations_research::glop::Status::ERROR_NULL
@ ERROR_NULL
Definition
status.h:38
operations_research::glop::Status::ERROR_LU
@ ERROR_LU
Definition
status.h:32
operations_research::glop::Status::ERROR_BOUND
@ ERROR_BOUND
Definition
status.h:35
operations_research::glop::Status::Status
Status()
Definition
status.cc:24
operations_research::glop::Status::ok
bool ok() const
Definition
status.h:58
operations_research::glop
Definition
basis_representation.cc:25
operations_research::glop::GetErrorCodeString
std::string GetErrorCodeString(Status::ErrorCode error_code)
Definition
status.cc:30
operations_research
OR-Tools root namespace.
Definition
binary_indexed_tree.h:21
ortools
glop
status.h
Generated by
1.15.0