Apollo 10.0
自动驾驶开放平台
general_message.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <algorithm>
20#include <iomanip>
21#include <numeric>
22#include <utility>
23#include <string>
24#include <sstream>
25#include <vector>
26
29
30namespace {
31
35std::vector<int> SortProtobufMapByKeys(
36 const google::protobuf::Message& message,
37 const google::protobuf::FieldDescriptor* field,
38 const google::protobuf::Reflection& reflection, const int size) {
39 std::vector<int> output;
40 if (0 == size) {
41 return output;
42 }
43 if (field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
44 const ::google::protobuf::Message& item =
45 reflection.GetRepeatedMessage(message, field, 0);
46 const ::google::protobuf::FieldDescriptor* item_fd =
47 item.GetDescriptor()->FindFieldByName("key");
48 if (item_fd && field->is_map() &&
49 ::google::protobuf::FieldDescriptor::Type::TYPE_STRING ==
50 item_fd->type()) {
51 std::vector<std::pair<std::string, int>> key_indices;
52 key_indices.reserve(size);
53 for (int i = 0; i < size; ++i) {
54 const ::google::protobuf::Message& item =
55 reflection.GetRepeatedMessage(message, field, i);
56 const ::google::protobuf::FieldDescriptor* item_fd =
57 item.GetDescriptor()->FindFieldByName("key");
58 const std::string key(item.GetReflection()->GetString(item, item_fd));
59 key_indices.emplace_back(key, i);
60 }
61 std::sort(key_indices.begin(), key_indices.end());
62 output.reserve(size);
63 for (const std::pair<std::string, int>& key_index : key_indices) {
64 output.push_back(key_index.second);
65 }
66 }
67 }
68
69 if (output.empty()) {
70 output.resize(size);
71 std::iota(output.begin(), output.end(), 0);
72 }
73 return output;
74}
75} // namespace
76
78 const google::protobuf::Message* msg,
79 const google::protobuf::Reflection* reflection,
80 const google::protobuf::FieldDescriptor* field)
81 : GeneralMessageBase(parent),
82 item_index_(0),
83 is_folded_(true),
84 field_(field),
85 message_ptr_(msg),
86 reflection_ptr_(reflection) {}
87
88int GeneralMessage::Render(const Screen* s, int key) {
90 int line_no = 0;
91 {
92 RenderableMessage* p = this;
93 while (p->parent()->parent()->parent()) {
94 p = p->parent();
95 }
96
97 GeneralChannelMessage* channel_msg_ptr =
98 static_cast<GeneralChannelMessage*>(p->parent());
99 s->AddStr(0, line_no++, "ChannelName: ");
100 s->AddStr(channel_msg_ptr->GetChannelName().c_str());
101
102 s->AddStr(0, line_no++, "MessageType: ");
103 s->AddStr(channel_msg_ptr->message_type().c_str());
104
105 std::ostringstream out_str;
106 out_str << std::fixed << std::setprecision(FrameRatio_Precision)
107 << channel_msg_ptr->frame_ratio();
108 s->AddStr(0, line_no++, "FrameRatio: ");
109 s->AddStr(out_str.str().c_str());
110
111 clear();
112
113 auto channel_msg = channel_msg_ptr->CopyMsgPtr();
114 if (!channel_msg_ptr->raw_msg_class_->ParseFromString(
115 channel_msg->message)) {
116 s->AddStr(0, line_no++,
117 "Cannot Parse the message for Real-Time Updating");
118 return line_no;
119 }
120
121 if (message_ptr_ && reflection_ptr_) {
122 int size = 0;
123 if (field_->is_repeated()) {
124 size = reflection_ptr_->FieldSize(*message_ptr_, field_);
125 } else {
126 if (reflection_ptr_->HasField(*message_ptr_, field_) ||
127 field_->containing_type()->options().map_entry()) {
128 size = 1;
129 }
130 }
131
132 if (size <= item_index_) {
133 out_str.str("");
134 out_str << "The item [" << item_index_ << "] has been empty !!!";
135 s->AddStr(0, line_no++, out_str.str().c_str());
136 return line_no;
137 }
138
139 if (key == ',') {
140 is_folded_ = !is_folded_;
141 } else if (is_folded_) {
142 switch (key) {
143 case 'n':
144 case 'N':
145 ++item_index_;
146 if (item_index_ >= size) {
147 item_index_ = 0;
148 }
149 break;
150
151 case 'm':
152 case 'M':
153 --item_index_;
154 if (item_index_ < 0) {
155 item_index_ = size - 1;
156 }
157 break;
158
159 default: {
160 }
161 }
162 }
163
164 int lcount = LineCountOfField(*message_ptr_, s->Width(), field_,
165 reflection_ptr_, is_folded_);
166 page_item_count_ = s->Height() - line_no - 8;
167 if (page_item_count_ < 1) {
169 }
170 pages_ = lcount / page_item_count_ + 1;
171 SplitPages(key);
172 int jump_lines = page_index_ * page_item_count_;
173 const std::vector<int> indices(
174 SortProtobufMapByKeys(*message_ptr_, field_, *reflection_ptr_, size));
175 if (is_folded_) {
176 GeneralMessageBase::PrintField(this, *message_ptr_, &jump_lines, s,
177 &line_no, 0, reflection_ptr_, field_,
178 indices[item_index_]);
179 } else {
180 for (const int index : indices) {
181 GeneralMessageBase::PrintField(this, *message_ptr_, &jump_lines, s,
182 &line_no, 0, reflection_ptr_, field_,
183 index);
184 }
185 }
186 }
187 }
188
190 return line_no;
191}
const std::string & message_type(void) const
double frame_ratio(void) override
std::string GetChannelName(void) const
static void PrintField(GeneralMessageBase *baseMsg, const google::protobuf::Message &msg, int *jump_lines, const Screen *s, int *line_no, int indent, const google::protobuf::Reflection *ref, const google::protobuf::FieldDescriptor *field, int index)
static int LineCountOfField(const google::protobuf::Message &msg, int screen_width, const google::protobuf::FieldDescriptor *field, const google::protobuf::Reflection *reflection, bool is_folded=true)
GeneralMessage(GeneralMessageBase *parent, const google::protobuf::Message *msg, const google::protobuf::Reflection *reflection, const google::protobuf::FieldDescriptor *field)
int Render(const Screen *s, int key) override
RenderableMessage * parent(void) const
static constexpr int FrameRatio_Precision
void AddStr(int x, int y, ColorPair color, const char *str) const
Definition screen.cc:152
int Height(void) const
Definition screen.cc:122
int Width(void) const
Definition screen.cc:120
void SetCurrentColor(ColorPair color) const
Definition screen.cc:124
void ClearCurrentColor(void) const
Definition screen.cc:145
@ WHITE_BLACK
Definition screen.h:39