Apollo 11.0
自动驾驶开放平台
object_pool.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#pragma once
17
18#include <deque>
19#include <list>
20#include <memory>
21#include <vector>
22
23namespace apollo {
24namespace perception {
25namespace base {
26// @brief general object pool interface
27template <class ObjectType>
29 public:
30 // TODO(All): remove
31 // typedef std::shared_ptr<ObjectType> ObjectTypePtr;
32
33 // @brief default constructor
34 BaseObjectPool() = default;
35 // @brief default destructor
36 virtual ~BaseObjectPool() = default;
37 // @brief pure virtual function to get object smart pointer
38 virtual std::shared_ptr<ObjectType> Get() = 0;
39 // @brief pure virtual function to get batch of smart pointers
40 // @params[IN] num: batch number
41 // @params[OUT] data: vector container to store the pointers
42 virtual void BatchGet(size_t num,
43 std::vector<std::shared_ptr<ObjectType>>* data) = 0;
44 // @brief pure virtual function to get batch of smart pointers
45 // @params[IN] num: batch number
46 // @params[IN] is_front: indicating insert to front or back of the list
47 // @params[OUT] data: list container to store the pointers
48 virtual void BatchGet(size_t num, bool is_front,
49 std::list<std::shared_ptr<ObjectType>>* data) = 0;
50 // @brief pure virtual function to get batch of smart pointers
51 // @params[IN] num: batch number
52 // @params[IN] is_front: indicating insert to front or back of the deque
53 // @params[OUT] data: deque container to store the pointers
54 virtual void BatchGet(size_t num, bool is_front,
55 std::deque<std::shared_ptr<ObjectType>>* data) = 0;
56 // @brief virtual function to set capacity
57 virtual void set_capacity(size_t capacity) {}
58 // @brief capacity getter
59 size_t get_capacity() { return capacity_; }
60 // @brief get remained object number
61 virtual size_t RemainedNum() { return 0; }
62
63 protected:
64 BaseObjectPool(const BaseObjectPool& rhs) = delete;
66 size_t capacity_ = 0;
67}; // class BaseObjectPool
68
69// @brief dummy object pool implementation, not managing memory
70template <class ObjectType>
71class DummyObjectPool : public BaseObjectPool<ObjectType> {
72 public:
73 // @brief Only allow accessing from global instance
75 static DummyObjectPool pool;
76 return pool;
77 }
78 // @brief overrided function to get object smart pointer
79 std::shared_ptr<ObjectType> Get() override {
80 return std::shared_ptr<ObjectType>(new ObjectType);
81 }
82 // @brief overrided function to get batch of smart pointers
83 // @params[IN] num: batch number
84 // @params[OUT] data: vector container to store the pointers
85 void BatchGet(size_t num,
86 std::vector<std::shared_ptr<ObjectType>>* data) override {
87 for (size_t i = 0; i < num; ++i) {
88 data->emplace_back(std::shared_ptr<ObjectType>(new ObjectType));
89 }
90 }
91 // @brief overrided function to get batch of smart pointers
92 // @params[IN] num: batch number
93 // @params[IN] is_front: indicating insert to front or back of the list
94 // @params[OUT] data: list container to store the pointers
95 void BatchGet(size_t num, bool is_front,
96 std::list<std::shared_ptr<ObjectType>>* data) override {
97 for (size_t i = 0; i < num; ++i) {
98 is_front
99 ? data->emplace_front(std::shared_ptr<ObjectType>(new ObjectType))
100 : data->emplace_back(std::shared_ptr<ObjectType>(new ObjectType));
101 }
102 }
103 // @brief overrided function to get batch of smart pointers
104 // @params[IN] num: batch number
105 // @params[IN] is_front: indicating insert to front or back of the deque
106 // @params[OUT] data: deque container to store the pointers
107 void BatchGet(size_t num, bool is_front,
108 std::deque<std::shared_ptr<ObjectType>>* data) override {
109 for (size_t i = 0; i < num; ++i) {
110 is_front
111 ? data->emplace_front(std::shared_ptr<ObjectType>(new ObjectType))
112 : data->emplace_back(std::shared_ptr<ObjectType>(new ObjectType));
113 }
114 }
115
116 protected:
117 // @brief default constructor
118 DummyObjectPool() = default;
119}; // class DummyObjectPool
120
121} // namespace base
122} // namespace perception
123} // namespace apollo
virtual void set_capacity(size_t capacity)
Definition object_pool.h:57
virtual std::shared_ptr< ObjectType > Get()=0
virtual void BatchGet(size_t num, bool is_front, std::list< std::shared_ptr< ObjectType > > *data)=0
BaseObjectPool(const BaseObjectPool &rhs)=delete
virtual void BatchGet(size_t num, std::vector< std::shared_ptr< ObjectType > > *data)=0
BaseObjectPool & operator=(const BaseObjectPool &rhs)=delete
virtual void BatchGet(size_t num, bool is_front, std::deque< std::shared_ptr< ObjectType > > *data)=0
static DummyObjectPool & Instance()
Definition object_pool.h:74
void BatchGet(size_t num, bool is_front, std::deque< std::shared_ptr< ObjectType > > *data) override
void BatchGet(size_t num, bool is_front, std::list< std::shared_ptr< ObjectType > > *data) override
Definition object_pool.h:95
void BatchGet(size_t num, std::vector< std::shared_ptr< ObjectType > > *data) override
Definition object_pool.h:85
std::shared_ptr< ObjectType > Get() override
Definition object_pool.h:79
class register implement
Definition arena_queue.h:37