Google OR-Tools v9.14
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_PORT_PROTO_UTILS_H_
15#define OR_TOOLS_PORT_PROTO_UTILS_H_
16
17#include <string>
18
19#include "absl/log/log.h"
20#include "absl/strings/ascii.h"
21#include "absl/strings/str_cat.h"
22#include "absl/strings/string_view.h"
23#include "google/protobuf/message.h"
24#include "google/protobuf/message_lite.h"
25#include "google/protobuf/text_format.h"
27
28namespace operations_research {
29
30template <class P>
31std::string ProtobufDebugString(const P& message) {
32 if constexpr (std::is_base_of_v<google::protobuf::Message, P>) {
33 std::string output;
34 google::protobuf::TextFormat::PrintToString(message, &output);
35 absl::StripTrailingAsciiWhitespace(&output);
36 return output;
37 } else if constexpr (std::is_base_of_v<google::protobuf::MessageLite, P>) {
38 return std::string(message.GetTypeName());
39 } else {
40 LOG(FATAL) << "Unsupported type";
41 return "";
42 }
43}
44
45template <class P>
46std::string ProtobufShortDebugString(const P& message) {
47 if constexpr (std::is_base_of_v<google::protobuf::Message, P>) {
48 std::string output;
49 google::protobuf::TextFormat::Printer printer;
50 printer.SetSingleLineMode(true);
51 printer.PrintToString(message, &output);
52 absl::StripTrailingAsciiWhitespace(&output);
53 return output;
54 } else if constexpr (std::is_base_of_v<google::protobuf::MessageLite, P>) {
55 return std::string(message.GetTypeName());
56 } else {
57 LOG(FATAL) << "Unsupported type";
58 return "";
59 }
60}
61
62template <typename ProtoEnumType>
63std::string ProtoEnumToString(ProtoEnumType enum_value) {
64#if defined(__PORTABLE_PLATFORM__)
65 return absl::StrCat(enum_value);
66#else // defined(__PORTABLE_PLATFORM__)
67 auto enum_descriptor = google::protobuf::GetEnumDescriptor<ProtoEnumType>();
68 auto enum_value_descriptor = enum_descriptor->FindValueByNumber(enum_value);
69 if (enum_value_descriptor == nullptr) {
70 return absl::StrCat(
71 "Invalid enum value of: ", enum_value, " for enum type: ",
72 google::protobuf::GetEnumDescriptor<ProtoEnumType>()->name());
73 }
74 return std::string(enum_value_descriptor->name());
75#endif // !defined(__PORTABLE_PLATFORM__)
76}
77
78template <typename ProtoType>
79bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string,
80 ProtoType* proto) {
81 if constexpr (std::is_base_of_v<google::protobuf::Message, ProtoType>) {
82 return google::protobuf::TextFormat::MergeFromString(
83 std::string(proto_text_string), proto);
84 } else if constexpr (std::is_base_of_v<google::protobuf::MessageLite,
85 ProtoType>) {
86 return false;
87 } else {
88 LOG(FATAL) << "Unsupported type";
89 return false;
90 }
91}
92
93// Tries to parse `text` as a text format proto. On a success, stores the result
94// in `message_out` and returns true, otherwise, returns `false` with an
95// explanation in `error_out`.
96//
97// When compiled with lite protos, any nonempty `text` will result in an error,
98// as lite protos do not support parsing from text format.
99//
100// NOTE: this API is optimized for implementing AbslParseFlag(). The error
101// message will be multiline and is designed to be easily read when printed.
102template <typename ProtoType>
103bool ProtobufParseTextProtoForFlag(absl::string_view text,
104 ProtoType* message_out,
105 std::string* error_out) {
106 if constexpr (std::is_base_of_v<google::protobuf::Message, ProtoType>) {
107 return ParseTextProtoForFlag(text, message_out, error_out);
108 } else if constexpr (std::is_base_of_v<google::protobuf::MessageLite,
109 ProtoType>) {
110 if (text.empty()) {
111 *message_out = ProtoType();
112 return true;
113 }
114 *error_out =
115 "cannot parse text protos on this platform (platform uses lite protos "
116 "do not support parsing text protos)";
117 return false;
118 } else {
119 LOG(FATAL) << "Unsupported type";
120 return false;
121 }
122}
123
124// Prints the input proto to a string on a single line in a format compatible
125// with ProtobufParseTextProtoForFlag().
127 const google::protobuf::Message& proto);
128
129// Prints an error message when compiling with lite protos.
131 const google::protobuf::MessageLite& proto);
132
133} // namespace operations_research
134
135#endif // OR_TOOLS_PORT_PROTO_UTILS_H_
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)
std::string ProtoEnumToString(ProtoEnumType enum_value)
Definition proto_utils.h:63
std::string ProtobufShortDebugString(const P &message)
Definition proto_utils.h:46
std::string ProtobufDebugString(const P &message)
Definition proto_utils.h:31
bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string, ProtoType *proto)
Definition proto_utils.h:79