OR-Tools  9.2
port/proto_utils.h
Go to the documentation of this file.
1 // Copyright 2010-2021 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/descriptor.h"
21 #include "google/protobuf/text_format.h"
22 #endif // !defined(__PORTABLE_PLATFORM__)
23 
24 #include "absl/base/attributes.h"
25 #include "absl/strings/str_cat.h"
26 
27 namespace operations_research {
28 template <class P>
29 std::string ProtobufDebugString(const P& message) {
30 #if defined(__PORTABLE_PLATFORM__)
31  return message.GetTypeName();
32 #else // defined(__PORTABLE_PLATFORM__)
33  return message.DebugString();
34 #endif // !defined(__PORTABLE_PLATFORM__)
35 }
36 
37 template <class P>
38 std::string ProtobufShortDebugString(const P& message) {
39 #if defined(__PORTABLE_PLATFORM__)
40  return message.GetTypeName();
41 #else // defined(__PORTABLE_PLATFORM__)
42  return message.ShortDebugString();
43 #endif // !defined(__PORTABLE_PLATFORM__)
44 }
45 
46 template <typename ProtoEnumType>
47 std::string ProtoEnumToString(ProtoEnumType enum_value) {
48 #if defined(__PORTABLE_PLATFORM__)
49  return absl::StrCat(enum_value);
50 #else // defined(__PORTABLE_PLATFORM__)
51  auto enum_descriptor = google::protobuf::GetEnumDescriptor<ProtoEnumType>();
52  auto enum_value_descriptor = enum_descriptor->FindValueByNumber(enum_value);
53  if (enum_value_descriptor == nullptr) {
54  return absl::StrCat(
55  "Invalid enum value of: ", enum_value, " for enum type: ",
56  google::protobuf::GetEnumDescriptor<ProtoEnumType>()->name());
57  }
58  return enum_value_descriptor->name();
59 #endif // !defined(__PORTABLE_PLATFORM__)
60 }
61 
62 template <typename ProtoType>
63 bool ProtobufTextFormatMergeFromString(const std::string& proto_text_string,
64  ProtoType* proto) {
65 #if defined(__PORTABLE_PLATFORM__)
66  return false;
67 #else // defined(__PORTABLE_PLATFORM__)
68  return google::protobuf::TextFormat::MergeFromString(proto_text_string,
69  proto);
70 #endif // !defined(__PORTABLE_PLATFORM__)
71 }
72 
73 } // namespace operations_research
74 
75 #endif // OR_TOOLS_PORT_PROTO_UTILS_H_
const std::string name
bool ProtobufTextFormatMergeFromString(const std::string &proto_text_string, ProtoType *proto)
std::string ProtobufDebugString(const P &message)
CpModelProto proto
std::string ProtoEnumToString(ProtoEnumType enum_value)
std::string message
Definition: trace.cc:398
std::string ProtobufShortDebugString(const P &message)
Collection of objects used to extend the Constraint Solver library.