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_PORT_PROTO_UTILS_H_
15#define OR_TOOLS_PORT_PROTO_UTILS_H_
16
17#include <string>
18
19#if !defined(__PORTABLE_PLATFORM__)
21#endif // !defined(__PORTABLE_PLATFORM__)
22
23#include "absl/strings/str_cat.h"
24#include "absl/strings/string_view.h"
25#include "google/protobuf/message.h"
26#include "google/protobuf/message_lite.h"
27#include "google/protobuf/text_format.h"
28
29namespace operations_research {
30
31template <class P>
32std::string ProtobufDebugString(const P& message) {
33#if defined(__PORTABLE_PLATFORM__)
34 return message.GetTypeName();
35#else // defined(__PORTABLE_PLATFORM__)
36 return message.DebugString();
37#endif // !defined(__PORTABLE_PLATFORM__)
38}
39
40template <class P>
41std::string ProtobufShortDebugString(const P& message) {
42#if defined(__PORTABLE_PLATFORM__)
43 return message.GetTypeName();
44#else // defined(__PORTABLE_PLATFORM__)
45 return message.ShortDebugString();
46#endif // !defined(__PORTABLE_PLATFORM__)
47}
48
49template <typename ProtoEnumType>
50std::string ProtoEnumToString(ProtoEnumType enum_value) {
51#if defined(__PORTABLE_PLATFORM__)
52 return absl::StrCat(enum_value);
53#else // defined(__PORTABLE_PLATFORM__)
54 auto enum_descriptor = google::protobuf::GetEnumDescriptor<ProtoEnumType>();
55 auto enum_value_descriptor = enum_descriptor->FindValueByNumber(enum_value);
56 if (enum_value_descriptor == nullptr) {
57 return absl::StrCat(
58 "Invalid enum value of: ", enum_value, " for enum type: ",
59 google::protobuf::GetEnumDescriptor<ProtoEnumType>()->name());
60 }
61 return enum_value_descriptor->name();
62#endif // !defined(__PORTABLE_PLATFORM__)
63}
64
65template <typename ProtoType>
66bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string,
67 ProtoType* proto) {
68#if defined(__PORTABLE_PLATFORM__)
69 return false;
70#else // !defined(__PORTABLE_PLATFORM__)
71 return google::protobuf::TextFormat::MergeFromString(
72 std::string(proto_text_string), proto);
73#endif // !defined(__PORTABLE_PLATFORM__)
74}
75
76// Tries to parse `text` as a text format proto. On a success, stores the result
77// in `message_out` and returns true, otherwise, returns `false` with an
78// explanation in `error_out`.
79//
80// When compiled with lite protos, any nonempty `text` will result in an error,
81// as lite protos do not support parsing from text format.
82//
83// NOTE: this API is optimized for implementing AbslParseFlag(). The error
84// message will be multiline and is designed to be easily read when printed.
85template <typename ProtoType>
86bool ProtobufParseTextProtoForFlag(absl::string_view text,
87 ProtoType* message_out,
88 std::string* error_out) {
89#if defined(__PORTABLE_PLATFORM__)
90 if (text.empty()) {
91 *message_out = ProtoType();
92 return true;
93 }
94 *error_out =
95 "cannot parse text protos on this platform (platform uses lite protos do "
96 "not support parsing text protos)";
97 return false;
98#else // defined(__PORTABLE_PLATFORM__)
99 return ParseTextProtoForFlag(text, message_out, error_out);
100#endif // !defined(__PORTABLE_PLATFORM__)
101}
102
103// Prints the input proto to a string on a single line in a format compatible
104// with ProtobufParseTextProtoForFlag().
106 const google::protobuf::Message& proto);
107
108// Prints an error message when compiling with lite protos.
110 const google::protobuf::MessageLite& proto);
111
112} // namespace operations_research
113
114#endif // OR_TOOLS_PORT_PROTO_UTILS_H_
CpModelProto proto
The output proto.
const std::string name
A name for logging purposes.
In SWIG mode, we don't want anything besides these top-level includes.
bool ParseTextProtoForFlag(const absl::string_view text, google::protobuf::Message *const message_out, std::string *const error_out)
std::string ProtobufTextFormatPrintToStringForFlag(const google::protobuf::Message &proto)
bool ProtobufParseTextProtoForFlag(absl::string_view text, ProtoType *message_out, std::string *error_out)
Definition proto_utils.h:86
std::string ProtoEnumToString(ProtoEnumType enum_value)
Definition proto_utils.h:50
std::string ProtobufShortDebugString(const P &message)
Definition proto_utils.h:41
std::string ProtobufDebugString(const P &message)
Definition proto_utils.h:32
bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string, ProtoType *proto)
Definition proto_utils.h:66
std::string message
Definition trace.cc:397