Google OR-Tools: ortools/port/proto_utils.h Source File
14#ifndef ORTOOLS_PORT_PROTO_UTILS_H_
15#define ORTOOLS_PORT_PROTO_UTILS_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());
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());
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());
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,
102template <typename ProtoType>
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)";
127 const google::protobuf::Message& proto);
131 const google::protobuf::MessageLite& proto);
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:103
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