2 * Copyright 2020 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 class CircularBuffer {
31 explicit CircularBuffer(size_t size);
33 // Push one item to the circular buffer
35 // Take a snapshot of the circular buffer and return it as a vector
36 std::vector<T> Pull() const;
37 // Drain everything from the circular buffer and return them as a vector
38 std::vector<T> Drain();
43 mutable std::mutex mutex_;
48 virtual long long GetTimestamp() const = 0;
49 virtual ~Timestamper() {}
52 class TimestamperInMilliseconds : public Timestamper {
54 long long GetTimestamp() const override {
55 return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
58 virtual ~TimestamperInMilliseconds() {}
62 struct TimestampedEntry {
68 class TimestampedCircularBuffer : public CircularBuffer<TimestampedEntry<T>> {
70 explicit TimestampedCircularBuffer(
71 size_t size, std::unique_ptr<Timestamper> timestamper = std::make_unique<TimestamperInMilliseconds>());
74 std::vector<TimestampedEntry<T>> Pull() const;
75 std::vector<TimestampedEntry<T>> Drain();
78 std::unique_ptr<Timestamper> timestamper_{std::make_unique<TimestamperInMilliseconds>()};
82 } // namespace bluetooth
85 bluetooth::common::CircularBuffer<T>::CircularBuffer(size_t size) : size_(size) {}
88 void bluetooth::common::CircularBuffer<T>::Push(const T item) {
89 std::unique_lock<std::mutex> lock(mutex_);
90 queue_.push_back(item);
91 while (queue_.size() > size_) {
97 std::vector<T> bluetooth::common::CircularBuffer<T>::Pull() const {
98 std::unique_lock<std::mutex> lock(mutex_);
99 return std::vector<T>(queue_.cbegin(), queue_.cend());
102 template <typename T>
103 std::vector<T> bluetooth::common::CircularBuffer<T>::Drain() {
104 std::unique_lock<std::mutex> lock(mutex_);
105 std::vector<T> items(std::make_move_iterator(queue_.begin()), std::make_move_iterator(queue_.end()));
110 template <typename T>
111 bluetooth::common::TimestampedCircularBuffer<T>::TimestampedCircularBuffer(
112 size_t size, std::unique_ptr<Timestamper> timestamper)
113 : CircularBuffer<TimestampedEntry<T>>(size), timestamper_(std::move(timestamper)) {}
115 template <typename T>
116 void bluetooth::common::TimestampedCircularBuffer<T>::Push(const T item) {
117 TimestampedEntry<T> timestamped_entry{timestamper_->GetTimestamp(), item};
118 bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Push(timestamped_entry);
121 template <typename T>
122 std::vector<struct bluetooth::common::TimestampedEntry<T>> bluetooth::common::TimestampedCircularBuffer<T>::Pull()
124 return bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Pull();
127 template <typename T>
128 std::vector<struct bluetooth::common::TimestampedEntry<T>> bluetooth::common::TimestampedCircularBuffer<T>::Drain() {
129 return bluetooth::common::CircularBuffer<TimestampedEntry<T>>::Drain();