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