Apollo 10.0
自动驾驶开放平台
sync_buffering.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 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#pragma once
17
18#include "deque"
19#include "functional"
20#include "memory"
21#include "mutex"
22#include "set"
23#include "unordered_set"
24#include "vector"
25
26#include "cyber/cyber.h"
27
28namespace apollo {
29namespace drivers {
30namespace lidar {
31template <typename T>
33 public:
34 SyncBuffering() = default;
36 std::function<std::shared_ptr<T>(void)> allocator_function,
37 std::function<void(std::shared_ptr<T>&)> cleaner_function) :
38 allocator_function_(allocator_function),
39 cleaner_function_(cleaner_function) {}
40
41 void SetBufferSize(int buffer_size);
42
43 void SetAllocator(
44 std::function<std::shared_ptr<T>(void)> allocator_function);
45
46 void SetCleaner(std::function<void(std::shared_ptr<T>&)> cleaner_function);
47
48 void Init();
49
50 std::shared_ptr<T> GenerateNewObject();
51
52 std::shared_ptr<T> AllocateElement();
53
54 private:
55 int buffer_size_ = 10;
56 std::vector<std::shared_ptr<T>> buffer_;
57
58 std::atomic_int buffer_index_{0};
59
60 std::function<std::shared_ptr<T>(void)> allocator_function_ = nullptr;
61 std::function<void(std::shared_ptr<T>&)> cleaner_function_ = nullptr;
62};
63
64template <typename T>
65void SyncBuffering<T>::SetBufferSize(int buffer_size) {
66 buffer_size_ = buffer_size;
67}
68
69template <typename T>
71 std::function<std::shared_ptr<T>(void)> allocator_function) {
72 allocator_function_ = allocator_function;
73}
74
75template <typename T>
77 std::function<void(std::shared_ptr<T>&)> cleaner_function) {
78 cleaner_function_ = cleaner_function;
79}
80
81template <typename T>
83 for (int i = 0; i < buffer_size_; ++i) {
84 buffer_.push_back(GenerateNewObject());
85 }
86}
87
88template <typename T>
90 std::shared_ptr<T> new_object;
91 if (allocator_function_ == nullptr) {
92 new_object = std::make_shared<T>();
93 } else {
94 new_object = allocator_function_();
95 }
96 return new_object;
97}
98
99template <typename T>
101 int element_idx = buffer_index_.fetch_add(1);
102 std::shared_ptr<T> element = buffer_[element_idx % buffer_size_];
103 if (cleaner_function_) {
104 cleaner_function_(element);
105 }
106 return element;
107}
108
109} // namespace lidar
110} // namespace drivers
111} // namespace apollo
std::shared_ptr< T > GenerateNewObject()
void SetCleaner(std::function< void(std::shared_ptr< T > &)> cleaner_function)
SyncBuffering(std::function< std::shared_ptr< T >(void)> allocator_function, std::function< void(std::shared_ptr< T > &)> cleaner_function)
void SetAllocator(std::function< std::shared_ptr< T >(void)> allocator_function)
class register implement
Definition arena_queue.h:37