Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
proto_tools.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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 {
24
25namespace {
26using ::google::protobuf::Descriptor;
27using ::google::protobuf::FieldDescriptor;
28using ::google::protobuf::Reflection;
29using ::google::protobuf::TextFormat;
30
31void WriteFullProtocolMessage(const google::protobuf::Message& message,
32 int indent_level, std::string* out) {
33 std::string temp_string;
34 const std::string indent(indent_level * 2, ' ');
35 const Descriptor* desc = message.GetDescriptor();
36 const Reflection* refl = message.GetReflection();
37 for (int i = 0; i < desc->field_count(); ++i) {
38 const FieldDescriptor* fd = desc->field(i);
39 const bool repeated = fd->is_repeated();
40 const int start = repeated ? 0 : -1;
41 const int limit = repeated ? refl->FieldSize(message, fd) : 0;
42 for (int j = start; j < limit; ++j) {
43 absl::StrAppend(out, indent, fd->name());
44 if (fd->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
45 absl::StrAppend(out, " {\n");
46 const google::protobuf::Message& nested_message =
47 repeated ? refl->GetRepeatedMessage(message, fd, j)
48 : refl->GetMessage(message, fd);
49 WriteFullProtocolMessage(nested_message, indent_level + 1, out);
50 absl::StrAppend(out, indent, "}\n");
51 } else {
52 TextFormat::PrintFieldValueToString(message, fd, j, &temp_string);
53 absl::StrAppend(out, ": ", temp_string, "\n");
54 }
55 }
56 }
57}
58} // namespace
59
61 const google::protobuf::Message& message, int indent_level) {
62 std::string message_str;
63 WriteFullProtocolMessage(message, indent_level, &message_str);
64 return message_str;
65}
66
67} // namespace operations_research
In SWIG mode, we don't want anything besides these top-level includes.
std::string FullProtocolMessageAsString(const google::protobuf::Message &message, int indent_level)
int64_t start
std::string message
Definition trace.cc:397
int indent
Definition trace.cc:433