2025-01-10 11:35:44 +01:00
|
|
|
// Copyright 2010-2025 Google LLC
|
2014-07-08 17:35:15 +00:00
|
|
|
// 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.
|
2014-07-09 15:18:27 +00:00
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#ifndef ORTOOLS_GLOP_STATUS_H_
|
|
|
|
|
#define ORTOOLS_GLOP_STATUS_H_
|
2014-07-08 09:27:02 +00:00
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
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 {
|
2020-10-22 23:36:58 +02:00
|
|
|
public:
|
2014-07-08 09:27:02 +00:00
|
|
|
// Possible kinds of errors.
|
|
|
|
|
enum ErrorCode {
|
|
|
|
|
// Not an error. Returned on success.
|
2018-11-05 16:24:47 +01:00
|
|
|
GLOP_OK = 0,
|
2014-07-08 09:27:02 +00:00
|
|
|
|
|
|
|
|
// 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,
|
2015-10-23 13:45:43 +02:00
|
|
|
|
2016-06-02 13:19:10 +02:00
|
|
|
// The linear program is invalid or it does not have the required format.
|
|
|
|
|
ERROR_INVALID_PROBLEM = 4,
|
2014-07-08 09:27:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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.
|
2023-05-24 15:33:13 +02:00
|
|
|
static Status OK() { return Status(); }
|
2014-07-08 09:27:02 +00:00
|
|
|
|
|
|
|
|
// Accessors.
|
|
|
|
|
ErrorCode error_code() const { return error_code_; }
|
2020-10-29 14:25:39 +01:00
|
|
|
const std::string& error_message() const { return error_message_; }
|
2018-11-05 16:24:47 +01:00
|
|
|
bool ok() const { return error_code_ == GLOP_OK; }
|
2014-07-08 09:27:02 +00:00
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
private:
|
2014-07-08 09:27:02 +00:00
|
|
|
ErrorCode error_code_;
|
|
|
|
|
std::string error_message_;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-20 14:28:11 -08:00
|
|
|
// Returns the string representation of the ErrorCode enum.
|
2014-07-08 09:27:02 +00:00
|
|
|
std::string GetErrorCodeString(Status::ErrorCode error_code);
|
|
|
|
|
|
|
|
|
|
// Macro to simplify error propagation between function returning Status.
|
2020-10-22 23:36:58 +02:00
|
|
|
#define GLOP_RETURN_IF_ERROR(function_call) \
|
|
|
|
|
do { \
|
|
|
|
|
Status return_status = function_call; \
|
|
|
|
|
if (!return_status.ok()) return return_status; \
|
2014-07-08 09:27:02 +00:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
|
// Macro to simplify the creation of an error.
|
2020-10-22 23:36:58 +02:00
|
|
|
#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); \
|
2014-07-08 09:27:02 +00:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
|
|
// Macro to check that a pointer argument is not null.
|
2020-10-22 23:36:58 +02:00
|
|
|
#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); \
|
2014-07-08 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:36:58 +02:00
|
|
|
} // namespace glop
|
|
|
|
|
} // namespace operations_research
|
2014-07-08 09:27:02 +00:00
|
|
|
|
2025-11-05 11:34:49 +01:00
|
|
|
#endif // ORTOOLS_GLOP_STATUS_H_
|