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
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
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.
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.
69std::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_
static const Status OK()
Definition: status.h:56
const std::string & error_message() const
Definition: status.h:60
ErrorCode error_code() const
Definition: status.h:59
std::string GetErrorCodeString(Status::ErrorCode error_code)
Definition: status.cc:29
Collection of objects used to extend the Constraint Solver library.