OR-Tools  9.6
port/proto_utils.h
Go to the documentation of this file.
1 // Copyright 2010-2022 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__)
20 #include "google/protobuf/generated_enum_reflection.h"
21 #include "google/protobuf/text_format.h"
23 #endif // !defined(__PORTABLE_PLATFORM__)
24 
25 #include "absl/strings/str_cat.h"
26 #include "absl/strings/string_view.h"
27 
28 namespace operations_research {
29 
30 template <class P>
31 std::string ProtobufDebugString(const P& message) {
32 #if defined(__PORTABLE_PLATFORM__)
33  return message.GetTypeName();
34 #else // defined(__PORTABLE_PLATFORM__)
35  return message.DebugString();
36 #endif // !defined(__PORTABLE_PLATFORM__)
37 }
38 
39 template <class P>
40 std::string ProtobufShortDebugString(const P& message) {
41 #if defined(__PORTABLE_PLATFORM__)
42  return message.GetTypeName();
43 #else // defined(__PORTABLE_PLATFORM__)
44  return message.ShortDebugString();
45 #endif // !defined(__PORTABLE_PLATFORM__)
46 }
47 
48 template <typename ProtoEnumType>
49 std::string ProtoEnumToString(ProtoEnumType enum_value) {
50 #if defined(__PORTABLE_PLATFORM__)
51  return absl::StrCat(enum_value);
52 #else // defined(__PORTABLE_PLATFORM__)
53  auto enum_descriptor = google::protobuf::GetEnumDescriptor<ProtoEnumType>();
54  auto enum_value_descriptor = enum_descriptor->FindValueByNumber(enum_value);
55  if (enum_value_descriptor == nullptr) {
56  return absl::StrCat(
57  "Invalid enum value of: ", enum_value, " for enum type: ",
58  google::protobuf::GetEnumDescriptor<ProtoEnumType>()->name());
59  }
60  return enum_value_descriptor->name();
61 #endif // !defined(__PORTABLE_PLATFORM__)
62 }
63 
64 template <typename ProtoType>
65 bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string,
66  ProtoType* proto) {
67 #if defined(__PORTABLE_PLATFORM__)
68  return false;
69 #else // !defined(__PORTABLE_PLATFORM__)
70  return google::protobuf::TextFormat::MergeFromString(
71  std::string(proto_text_string), proto);
72 #endif // !defined(__PORTABLE_PLATFORM__)
73 }
74 
75 // Tries to parse `text` as a text format proto. On a success, stores the result
76 // in `message_out` and returns true, otherwise, returns `false` with an
77 // explanation in `error_out`.
78 //
79 // When compiled with lite protos, any nonempty `text` will result in an error,
80 // as lite protos do not support parsing from text format.
81 //
82 // NOTE: this API is optimized for implementing AbslParseFlag(). The error
83 // message will be multiline and is designed to be easily read when printed.
84 template <typename ProtoType>
85 bool ProtobufParseTextProtoForFlag(absl::string_view text,
86  ProtoType* message_out,
87  std::string* error_out) {
88 #if defined(__PORTABLE_PLATFORM__)
89  if (text.empty()) {
90  *message_out = ProtoType();
91  return true;
92  }
93  *error_out =
94  "cannot parse text protos on this platform (platform uses lite protos do "
95  "not support parsing text protos)";
96  return false;
97 #else // defined(__PORTABLE_PLATFORM__)
98  return ParseTextProtoForFlag(text, message_out, error_out);
99 #endif // !defined(__PORTABLE_PLATFORM__)
100 }
101 
102 template <typename ProtoType>
103 std::string ProtobufTextFormatPrintToString(const ProtoType proto) {
104 #if defined(__PORTABLE_PLATFORM__)
105  return absl::StrCat(
106  "<text protos not supported with lite protobuf, cannot print proto "
107  "message of type ",
108  proto.GetTypeName(), ">");
109 #else // defined(__PORTABLE_PLATFORM__)
110  std::string result;
111  google::protobuf::TextFormat::PrintToString(proto, &result);
112  return result;
113 #endif // !defined(__PORTABLE_PLATFORM__)
114 }
115 
116 } // namespace operations_research
117 
118 #endif // OR_TOOLS_PORT_PROTO_UTILS_H_
CpModelProto proto
const std::string name
Collection of objects used to extend the Constraint Solver library.
std::string ProtobufTextFormatPrintToString(const ProtoType proto)
bool ParseTextProtoForFlag(const absl::string_view text, google::protobuf::Message *const message_out, std::string *const error_out)
Definition: parse_proto.cc:77
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)
std::string message
Definition: trace.cc:399