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