OR-Tools  9.2
proto_tools.cc
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 
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 namespace {
23 using ::google::protobuf::Descriptor;
24 using ::google::protobuf::FieldDescriptor;
25 using ::google::protobuf::Reflection;
26 using ::google::protobuf::TextFormat;
27 
28 void WriteFullProtocolMessage(const google::protobuf::Message& message,
29  int indent_level, std::string* out) {
30  std::string temp_string;
31  const std::string indent(indent_level * 2, ' ');
32  const Descriptor* desc = message.GetDescriptor();
33  const Reflection* refl = message.GetReflection();
34  for (int i = 0; i < desc->field_count(); ++i) {
35  const FieldDescriptor* fd = desc->field(i);
36  const bool repeated = fd->is_repeated();
37  const int start = repeated ? 0 : -1;
38  const int limit = repeated ? refl->FieldSize(message, fd) : 0;
39  for (int j = start; j < limit; ++j) {
40  absl::StrAppend(out, indent, fd->name());
41  if (fd->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
42  absl::StrAppend(out, " {\n");
43  const google::protobuf::Message& nested_message =
44  repeated ? refl->GetRepeatedMessage(message, fd, j)
45  : refl->GetMessage(message, fd);
46  WriteFullProtocolMessage(nested_message, indent_level + 1, out);
47  absl::StrAppend(out, indent, "}\n");
48  } else {
49  TextFormat::PrintFieldValueToString(message, fd, j, &temp_string);
50  absl::StrAppend(out, ": ", temp_string, "\n");
51  }
52  }
53  }
54 }
55 } // namespace
56 
58  const google::protobuf::Message& message, int indent_level) {
59  std::string message_str;
60  WriteFullProtocolMessage(message, indent_level, &message_str);
61  return message_str;
62 }
63 
64 } // namespace operations_research
int indent
Definition: trace.cc:434
std::string message
Definition: trace.cc:398
Collection of objects used to extend the Constraint Solver library.
std::string FullProtocolMessageAsString(const google::protobuf::Message &message, int indent_level)
Definition: proto_tools.cc:57