14#ifndef OR_TOOLS_PORT_PROTO_UTILS_H_
15#define OR_TOOLS_PORT_PROTO_UTILS_H_
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"
32 if constexpr (std::is_base_of_v<google::protobuf::Message, P>) {
34 google::protobuf::TextFormat::PrintToString(message, &output);
35 absl::StripTrailingAsciiWhitespace(&output);
37 }
else if constexpr (std::is_base_of_v<google::protobuf::MessageLite, P>) {
38 return std::string(message.GetTypeName());
40 LOG(FATAL) <<
"Unsupported type";
47 if constexpr (std::is_base_of_v<google::protobuf::Message, P>) {
49 google::protobuf::TextFormat::Printer printer;
50 printer.SetSingleLineMode(
true);
51 printer.PrintToString(message, &output);
52 absl::StripTrailingAsciiWhitespace(&output);
54 }
else if constexpr (std::is_base_of_v<google::protobuf::MessageLite, P>) {
55 return std::string(message.GetTypeName());
57 LOG(FATAL) <<
"Unsupported type";
62template <
typename ProtoEnumType>
64#if defined(__PORTABLE_PLATFORM__)
65 return absl::StrCat(enum_value);
67 auto enum_descriptor = google::protobuf::GetEnumDescriptor<ProtoEnumType>();
68 auto enum_value_descriptor = enum_descriptor->FindValueByNumber(enum_value);
69 if (enum_value_descriptor ==
nullptr) {
71 "Invalid enum value of: ", enum_value,
" for enum type: ",
72 google::protobuf::GetEnumDescriptor<ProtoEnumType>()->name());
74 return std::string(enum_value_descriptor->name());
78template <
typename ProtoType>
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,
88 LOG(FATAL) <<
"Unsupported type";
102template <
typename ProtoType>
104 ProtoType* message_out,
105 std::string* error_out) {
106 if constexpr (std::is_base_of_v<google::protobuf::Message, ProtoType>) {
108 }
else if constexpr (std::is_base_of_v<google::protobuf::MessageLite,
111 *message_out = ProtoType();
115 "cannot parse text protos on this platform (platform uses lite protos "
116 "do not support parsing text protos)";
119 LOG(FATAL) <<
"Unsupported type";
127 const google::protobuf::Message& proto);
131 const google::protobuf::MessageLite& proto);
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)
std::string ProtobufShortDebugString(const P &message)
std::string ProtobufDebugString(const P &message)
bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string, ProtoType *proto)