Files
ortools-clone/ortools/glop/status.h

96 lines
3.4 KiB
C
Raw Permalink Normal View History

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.
2025-11-05 11:34:49 +01:00
#ifndef ORTOOLS_GLOP_STATUS_H_
#define ORTOOLS_GLOP_STATUS_H_
#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:
// 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.
2023-05-24 15:33:13 +02:00
static Status OK() { return Status(); }
// 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_; }
bool ok() const { return error_code_ == GLOP_OK; }
2020-10-22 23:36:58 +02:00
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.
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; \
} 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); \
} 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); \
}
2020-10-22 23:36:58 +02:00
} // namespace glop
} // namespace operations_research
2025-11-05 11:34:49 +01:00
#endif // ORTOOLS_GLOP_STATUS_H_