Apollo  6.0
Open source self driving car software
history.h
Go to the documentation of this file.
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 
30 namespace apollo {
31 namespace cyber {
32 namespace transport {
33 
34 template <typename MessageT>
35 class History {
36  public:
37  using MessagePtr = std::shared_ptr<MessageT>;
38  struct CachedMessage {
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 
68 template <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 
78  if (attr.history_policy == proto::QosHistoryPolicy::HISTORY_KEEP_ALL) {
79  depth_ = max_depth_;
80  } else {
81  depth_ = attr.depth;
82  if (depth_ > max_depth_) {
83  depth_ = max_depth_;
84  }
85  }
86 }
87 
88 template <typename MessageT>
90  Clear();
91 }
92 
93 template <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 
106 template <typename MessageT>
108  std::lock_guard<std::mutex> lock(msgs_mutex_);
109  msgs_.clear();
110 }
111 
112 template <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 
124 template <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_
uint32_t max_depth() const
Definition: history.h:58
CachedMessage(const MessagePtr &message, const MessageInfo &message_info)
Definition: history.h:39
Definition: history_attributes.h:28
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
size_t GetSize() const
Definition: history.h:125
void GetCachedMessage(std::vector< CachedMessage > *msgs) const
Definition: history.h:113
void Enable()
Definition: history.h:49
Definition: history.h:35
void Disable()
Definition: history.h:50
proto::QosHistoryPolicy history_policy
Definition: history_attributes.h:36
History(const HistoryAttributes &attr)
Definition: history.h:69
uint32_t depth
Definition: history_attributes.h:37
virtual ~History()
Definition: history.h:89
Definition: message_info.h:30
uint32_t depth() const
Definition: history.h:57
std::shared_ptr< MessageT > MessagePtr
Definition: history.h:37
MessagePtr msg
Definition: history.h:42
void Clear()
Definition: history.h:107
MessageInfo msg_info
Definition: history.h:43
void Add(const MessagePtr &msg, const MessageInfo &msg_info)
Definition: history.h:94