forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
status.h
97 lines (77 loc) · 3.44 KB
/
status.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef OR_TOOLS_GLOP_STATUS_H_
#define OR_TOOLS_GLOP_STATUS_H_
#include <string>
#include "absl/base/port.h"
namespace operations_research {
namespace glop {
// Return type for the solver functions that return "Did that work?".
// It should only be used for unrecoverable errors.
class Status {
public:
// Possible kinds of errors.
enum ErrorCode {
// Not an error. Returned on success.
GLOP_OK = 0,
// The LU factorization of the current basis couldn't be computed.
ERROR_LU = 1,
// The current variable values are out of their bound modulo the tolerance.
ERROR_BOUND = 2,
// A pointer argument was NULL when it shouldn't be.
ERROR_NULL = 3,
// The linear program is invalid or it does not have the required format.
ERROR_INVALID_PROBLEM = 4,
};
// Creates a "successful" status.
Status();
// Creates a status with the specified error code and error message.
// If "code == 0", error_message is ignored and a Status object identical
// to Status::OK is constructed.
Status(ErrorCode error_code, std::string error_message);
// Improves readability but identical to 0-arg constructor.
static Status OK() { return Status(); }
// Accessors.
ErrorCode error_code() const { return error_code_; }
const std::string& error_message() const { return error_message_; }
bool ok() const { return error_code_ == GLOP_OK; }
private:
ErrorCode error_code_;
std::string error_message_;
};
// Returns the string representation of the ErrorCode enum.
std::string GetErrorCodeString(Status::ErrorCode error_code);
// Macro to simplify error propagation between function returning Status.
#define GLOP_RETURN_IF_ERROR(function_call) \
do { \
Status return_status = function_call; \
if (!return_status.ok()) return return_status; \
} while (false)
// Macro to simplify the creation of an error.
#define GLOP_RETURN_AND_LOG_ERROR(error_code, message) \
do { \
std::string error_message = message; \
LOG(ERROR) << GetErrorCodeString(error_code) << ": " << error_message; \
return Status(error_code, error_message); \
} while (false)
// Macro to check that a pointer argument is not null.
#define GLOP_RETURN_ERROR_IF_NULL(arg) \
if (arg == nullptr) { \
const std::string variable_name = #arg; \
std::string error_message = variable_name + " must not be null."; \
LOG(DFATAL) << error_message; \
return Status(Status::ERROR_NULL, error_message); \
}
} // namespace glop
} // namespace operations_research
#endif // OR_TOOLS_GLOP_STATUS_H_