Google OR-Tools v9.12
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_utils.h
Go to the documentation of this file.
1// Copyright 2010-2025 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_LINEAR_SOLVER_PROTO_SOLVER_PROTO_UTILS_H_
15#define OR_TOOLS_LINEAR_SOLVER_PROTO_SOLVER_PROTO_UTILS_H_
16
17#include <string>
18#include <type_traits>
19#include <utility>
20
21#include "absl/log/check.h"
22#include "absl/status/statusor.h"
23#include "google/protobuf/message.h"
25#include "ortools/linear_solver/linear_solver.pb.h"
27
28namespace operations_research {
29
30using google::protobuf::Message;
31
32// Some SolveWithProto() returns a StatusOr<MPModelResponse>, this utility
33// just convert bad absl::StatusOr to a proper error in MPModelResponse.
34//
35// TODO(user): All SolveWithProto() should just fill the appropriate response
36// instead.
37inline MPSolutionResponse ConvertStatusOrMPSolutionResponse(
38 bool log_error, absl::StatusOr<MPSolutionResponse> response) {
39 if (!response.ok()) {
40 if (log_error) {
41 LOG(ERROR) << "Error status: " << response.status();
42 }
43 MPSolutionResponse error_response;
44 error_response.set_status(MPSolverResponseStatus::MPSOLVER_ABNORMAL);
45 error_response.set_status_str(response.status().ToString());
46 return error_response;
47 }
48 return std::move(response).value();
49}
50
51// Returns a string that should be used in MPModelRequest's
52// solver_specific_parameters field to encode the glop parameters.
53//
54// The returned string's content depends on the version of the proto library
55// that is linked in the binary.
56//
57// By default it will contain the textual representation of the input proto.
58// But when the proto-lite is used, it will contain the binary stream of the
59// proto instead since it is not possible to build the textual representation in
60// that case.
61//
62// This function will test if the proto-lite is used and expect a binary stream
63// when it is the case. So in order for your code to be portable, you should
64// always use this function to set the specific parameters.
65//
66// Proto-lite disables some features of protobufs and messages inherit from
67// MessageLite directly instead of inheriting from Message (which is itself a
68// specialization of MessageLite).
69// See https://protobuf.dev/reference/cpp/cpp-generated/#message for details.
70template <typename P>
71std::string EncodeParametersAsString(const P& parameters) {
72 if constexpr (!std::is_base_of<Message, P>::value) {
73 // Here we use SerializeToString() instead of SerializeAsString() since the
74 // later ignores errors and returns an empty string instead (which can be a
75 // valid value when no fields are set).
76 std::string bytes;
77 CHECK(parameters.SerializeToString(&bytes));
78 return bytes;
79 }
80
81 return ProtobufShortDebugString(parameters);
82}
83
84} // namespace operations_research
85
86#endif // OR_TOOLS_LINEAR_SOLVER_PROTO_SOLVER_PROTO_UTILS_H_
In SWIG mode, we don't want anything besides these top-level includes.
std::string EncodeParametersAsString(const P &parameters)
Definition proto_utils.h:71
std::string ProtobufShortDebugString(const P &message)
Definition proto_utils.h:41
MPSolutionResponse ConvertStatusOrMPSolutionResponse(bool log_error, absl::StatusOr< MPSolutionResponse > response)
Definition proto_utils.h:37