Apollo 9.0
自动驾驶开放平台
history.h
浏览该文件的文档.
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
17#ifndef CYBER_TRANSPORT_MESSAGE_HISTORY_H_
18#define CYBER_TRANSPORT_MESSAGE_HISTORY_H_
19
20#include <cstdint>
21#include <list>
22#include <memory>
23#include <mutex>
24#include <vector>
25
29
30namespace apollo {
31namespace cyber {
32namespace transport {
33
34template <typename MessageT>
35class History {
36 public:
37 using MessagePtr = std::shared_ptr<MessageT>;
39 CachedMessage(const MessagePtr& message, const MessageInfo& message_info)
40 : msg(message), msg_info(message_info) {}
41
44 };
45
46 explicit History(const HistoryAttributes& attr);
47 virtual ~History();
48
49 void Enable() { enabled_ = true; }
50 void Disable() { enabled_ = false; }
51
52 void Add(const MessagePtr& msg, const MessageInfo& msg_info);
53 void Clear();
54 void GetCachedMessage(std::vector<CachedMessage>* msgs) const;
55 size_t GetSize() const;
56
57 uint32_t depth() const { return depth_; }
58 uint32_t max_depth() const { return max_depth_; }
59
60 private:
61 bool enabled_;
62 uint32_t depth_;
63 uint32_t max_depth_;
64 std::list<CachedMessage> msgs_;
65 mutable std::mutex msgs_mutex_;
66};
67
68template <typename MessageT>
70 : enabled_(false), max_depth_(1000) {
71 auto& global_conf = common::GlobalData::Instance()->Config();
72 if (global_conf.has_transport_conf() &&
73 global_conf.transport_conf().has_resource_limit()) {
74 max_depth_ =
75 global_conf.transport_conf().resource_limit().max_history_depth();
76 }
77
79 depth_ = max_depth_;
80 } else {
81 depth_ = attr.depth;
82 if (depth_ > max_depth_) {
83 depth_ = max_depth_;
84 }
85 }
86}
87
88template <typename MessageT>
92
93template <typename MessageT>
95 const MessageInfo& msg_info) {
96 if (!enabled_) {
97 return;
98 }
99 std::lock_guard<std::mutex> lock(msgs_mutex_);
100 msgs_.emplace_back(msg, msg_info);
101 while (msgs_.size() > depth_) {
102 msgs_.pop_front();
103 }
104}
105
106template <typename MessageT>
108 std::lock_guard<std::mutex> lock(msgs_mutex_);
109 msgs_.clear();
110}
111
112template <typename MessageT>
114 std::vector<CachedMessage>* msgs) const {
115 if (msgs == nullptr) {
116 return;
117 }
118
119 std::lock_guard<std::mutex> lock(msgs_mutex_);
120 msgs->reserve(msgs_.size());
121 msgs->insert(msgs->begin(), msgs_.begin(), msgs_.end());
122}
123
124template <typename MessageT>
126 std::lock_guard<std::mutex> lock(msgs_mutex_);
127 return msgs_.size();
128}
129
130} // namespace transport
131} // namespace cyber
132} // namespace apollo
133
134#endif // CYBER_TRANSPORT_MESSAGE_HISTORY_H_
History(const HistoryAttributes &attr)
Definition history.h:69
std::shared_ptr< MessageT > MessagePtr
Definition history.h:37
void GetCachedMessage(std::vector< CachedMessage > *msgs) const
Definition history.h:113
void Add(const MessagePtr &msg, const MessageInfo &msg_info)
Definition history.h:94
void Clear()
Definition init.cc:154
class register implement
CachedMessage(const MessagePtr &message, const MessageInfo &message_info)
Definition history.h:39