OR-Tools  8.2
port/proto_utils.h
Go to the documentation of this file.
1 // Copyright 2010-2018 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 #ifndef __PORTABLE_PLATFORM__
20 #include "google/protobuf/descriptor.h"
21 #include "google/protobuf/text_format.h"
22 #endif
23 
24 #include "absl/strings/str_cat.h"
25 
26 namespace operations_research {
27 #if defined(__PORTABLE_PLATFORM__)
28 template <class P>
29 std::string ProtobufDebugString(const P& message) {
30  return message.GetTypeName();
31 }
32 
33 template <class P>
34 std::string ProtobufShortDebugString(const P& message) {
35  return message.GetTypeName();
36 }
37 
38 template <typename ProtoEnumType>
39 std::string ProtoEnumToString(ProtoEnumType enum_value) {
40  return absl::StrCat(enum_value);
41 }
42 
43 template <typename ProtoType>
44 bool ProtobufTextFormatMergeFromString(const std::string& proto_text_string
45  ABSL_ATTRIBUTE_UNUSED,
46  ProtoType* proto ABSL_ATTRIBUTE_UNUSED) {
47  return false;
48 }
49 
50 #else // __PORTABLE_PLATFORM__
51 
52 template <class P>
53 std::string ProtobufDebugString(const P& message) {
54  return message.DebugString();
55 }
56 
57 template <class P>
58 std::string ProtobufShortDebugString(const P& message) {
59  return message.ShortDebugString();
60 }
61 
62 template <typename ProtoEnumType>
63 std::string ProtoEnumToString(ProtoEnumType enum_value) {
64  auto enum_descriptor = google::protobuf::GetEnumDescriptor<ProtoEnumType>();
65  auto enum_value_descriptor = enum_descriptor->FindValueByNumber(enum_value);
66  if (enum_value_descriptor == nullptr) {
67  return absl::StrCat(
68  "Invalid enum value of: ", enum_value, " for enum type: ",
69  google::protobuf::GetEnumDescriptor<ProtoEnumType>()->name());
70  }
71  return enum_value_descriptor->name();
72 }
73 
74 template <typename ProtoType>
75 bool ProtobufTextFormatMergeFromString(const std::string& proto_text_string,
76  ProtoType* proto) {
77  return google::protobuf::TextFormat::MergeFromString(proto_text_string,
78  proto);
79 }
80 
81 #endif // !__PORTABLE_PLATFORM__
82 
83 } // namespace operations_research
84 
85 #endif // OR_TOOLS_PORT_PROTO_UTILS_H_
CpModelProto proto
const std::string name
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
bool ProtobufTextFormatMergeFromString(const std::string &proto_text_string, ProtoType *proto)
std::string ProtoEnumToString(ProtoEnumType enum_value)
std::string ProtobufShortDebugString(const P &message)
std::string ProtobufDebugString(const P &message)
std::string message
Definition: trace.cc:395