Apollo  6.0
Open source self driving car software
cache_buffer.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_DATA_CACHE_BUFFER_H_
18 #define CYBER_DATA_CACHE_BUFFER_H_
19 
20 #include <functional>
21 #include <memory>
22 #include <mutex>
23 #include <vector>
24 
25 namespace apollo {
26 namespace cyber {
27 namespace data {
28 
29 template <typename T>
30 class CacheBuffer {
31  public:
32  using value_type = T;
33  using size_type = std::size_t;
34  using FusionCallback = std::function<void(const T&)>;
35 
36  explicit CacheBuffer(uint64_t size) {
37  capacity_ = size + 1;
38  buffer_.resize(capacity_);
39  }
40 
41  CacheBuffer(const CacheBuffer& rhs) {
42  std::lock_guard<std::mutex> lg(rhs.mutex_);
43  head_ = rhs.head_;
44  tail_ = rhs.tail_;
45  buffer_ = rhs.buffer_;
46  capacity_ = rhs.capacity_;
47  fusion_callback_ = rhs.fusion_callback_;
48  }
49 
50  T& operator[](const uint64_t& pos) { return buffer_[GetIndex(pos)]; }
51  const T& at(const uint64_t& pos) const { return buffer_[GetIndex(pos)]; }
52 
53  uint64_t Head() const { return head_ + 1; }
54  uint64_t Tail() const { return tail_; }
55  uint64_t Size() const { return tail_ - head_; }
56 
57  const T& Front() const { return buffer_[GetIndex(head_ + 1)]; }
58  const T& Back() const { return buffer_[GetIndex(tail_)]; }
59 
60  bool Empty() const { return tail_ == 0; }
61  bool Full() const { return capacity_ - 1 == tail_ - head_; }
62  uint64_t Capacity() const { return capacity_; }
63 
64  void SetFusionCallback(const FusionCallback& callback) {
65  fusion_callback_ = callback;
66  }
67 
68  void Fill(const T& value) {
69  if (fusion_callback_) {
70  fusion_callback_(value);
71  } else {
72  if (Full()) {
73  buffer_[GetIndex(head_)] = value;
74  ++head_;
75  ++tail_;
76  } else {
77  buffer_[GetIndex(tail_ + 1)] = value;
78  ++tail_;
79  }
80  }
81  }
82 
83  std::mutex& Mutex() { return mutex_; }
84 
85  private:
86  CacheBuffer& operator=(const CacheBuffer& other) = delete;
87  uint64_t GetIndex(const uint64_t& pos) const { return pos % capacity_; }
88 
89  uint64_t head_ = 0;
90  uint64_t tail_ = 0;
91  uint64_t capacity_ = 0;
92  std::vector<T> buffer_;
93  mutable std::mutex mutex_;
94  FusionCallback fusion_callback_;
95 };
96 
97 } // namespace data
98 } // namespace cyber
99 } // namespace apollo
100 
101 #endif // CYBER_DATA_CACHE_BUFFER_H_
CacheBuffer(uint64_t size)
Definition: cache_buffer.h:36
const T & Back() const
Definition: cache_buffer.h:58
std::size_t size_type
Definition: cache_buffer.h:33
const T & Front() const
Definition: cache_buffer.h:57
uint64_t Size() const
Definition: cache_buffer.h:55
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void SetFusionCallback(const FusionCallback &callback)
Definition: cache_buffer.h:64
uint64_t Capacity() const
Definition: cache_buffer.h:62
std::mutex & Mutex()
Definition: cache_buffer.h:83
T & operator[](const uint64_t &pos)
Definition: cache_buffer.h:50
std::function< void(const T &)> FusionCallback
Definition: cache_buffer.h:34
const T & at(const uint64_t &pos) const
Definition: cache_buffer.h:51
CacheBuffer(const CacheBuffer &rhs)
Definition: cache_buffer.h:41
void Fill(const T &value)
Definition: cache_buffer.h:68
T value_type
Definition: cache_buffer.h:32
Definition: cache_buffer.h:30
bool Full() const
Definition: cache_buffer.h:61
bool Empty() const
Definition: cache_buffer.h:60
apollo::cyber::base::std value
uint64_t Tail() const
Definition: cache_buffer.h:54
uint64_t Head() const
Definition: cache_buffer.h:53