OR-Tools  8.1
proto_tools.cc
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 
15 
16 #include "absl/strings/str_cat.h"
17 #include "google/protobuf/descriptor.h"
18 #include "google/protobuf/message.h"
19 #include "google/protobuf/text_format.h"
20 
21 namespace operations_research {
22 
23 using ::google::protobuf::Descriptor;
24 using ::google::protobuf::FieldDescriptor;
25 using ::google::protobuf::Reflection;
26 using ::google::protobuf::TextFormat;
27 
28 namespace {
29 void WriteFullProtocolMessage(const google::protobuf::Message& message,
30  int indent_level, std::string* out) {
31  std::string temp_string;
32  const std::string indent(indent_level * 2, ' ');
33  const Descriptor* desc = message.GetDescriptor();
34  const Reflection* refl = message.GetReflection();
35  for (int i = 0; i < desc->field_count(); ++i) {
36  const FieldDescriptor* fd = desc->field(i);
37  const bool repeated = fd->is_repeated();
38  const int start = repeated ? 0 : -1;
39  const int limit = repeated ? refl->FieldSize(message, fd) : 0;
40  for (int j = start; j < limit; ++j) {
41  absl::StrAppend(out, indent, fd->name());
42  if (fd->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
43  absl::StrAppend(out, " {\n");
44  const google::protobuf::Message& nested_message =
45  repeated ? refl->GetRepeatedMessage(message, fd, j)
46  : refl->GetMessage(message, fd);
47  WriteFullProtocolMessage(nested_message, indent_level + 1, out);
48  absl::StrAppend(out, indent, "}\n");
49  } else {
50  TextFormat::PrintFieldValueToString(message, fd, j, &temp_string);
51  absl::StrAppend(out, ": ", temp_string, "\n");
52  }
53  }
54  }
55 }
56 } // namespace
57 
59  const google::protobuf::Message& message, int indent_level) {
60  std::string message_str;
61  WriteFullProtocolMessage(message, indent_level, &message_str);
62  return message_str;
63 }
64 
65 } // namespace operations_research
message
std::string message
Definition: trace.cc:395
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
proto_tools.h
operations_research::FullProtocolMessageAsString
std::string FullProtocolMessageAsString(const google::protobuf::Message &message, int indent_level)
Definition: proto_tools.cc:58
indent
int indent
Definition: trace.cc:431