Google OR-Tools: ortools/port/proto_utils.h Source File

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef ORTOOLS_PORT_PROTO_UTILS_H_

15#define ORTOOLS_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

29

30template <class P>

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>

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>

64#if defined(__PORTABLE_PLATFORM__)

65 return absl::StrCat(enum_value);

66#else

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

76}

77

78template <typename ProtoType>

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

94

95

96

97

98

99

100

101

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,

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

125

127 const google::protobuf::Message& proto);

128

129

131 const google::protobuf::MessageLite& proto);

132

133}

134

135#endif

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