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