Apollo 10.0
自动驾驶开放平台
cache_buffer.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_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
25namespace apollo {
26namespace cyber {
27namespace data {
28
29template <typename T>
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
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_
const T & at(const uint64_t &pos) const
void SetFusionCallback(const FusionCallback &callback)
std::function< void(const T &)> FusionCallback
T & operator[](const uint64_t &pos)
CacheBuffer(const CacheBuffer &rhs)
class register implement
Definition arena_queue.h:37