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