Apollo 11.0
自动驾驶开放平台
indexed_list.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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
21#pragma once
22
23#include <unordered_map>
24#include <vector>
25
26#include <boost/thread/shared_mutex.hpp>
27
28#include "cyber/common/log.h"
30
31namespace apollo {
32namespace planning {
33
34template <typename I, typename T>
36 public:
45 T* Add(const I id, const T& object) {
46 auto obs = Find(id);
47 if (obs) {
48 AWARN << "object " << id << " is already in container";
49 *obs = object;
50 return obs;
51 } else {
52 object_dict_.insert({id, object});
53 auto* ptr = &object_dict_.at(id);
54 object_list_.push_back(ptr);
55 return ptr;
56 }
57 }
58
65 T* Find(const I id) {
66 return apollo::common::util::FindOrNull(object_dict_, id);
67 }
68
75 const T* Find(const I id) const {
76 return apollo::common::util::FindOrNull(object_dict_, id);
77 }
78
83 const std::vector<const T*>& Items() const { return object_list_; }
84
89 const std::unordered_map<I, T>& Dict() const { return object_dict_; }
90
95 this->object_list_.clear();
96 this->object_dict_.clear();
97 for (const auto& item : other.Dict()) {
98 Add(item.first, item.second);
99 }
100 return *this;
101 }
102
103 private:
104 std::vector<const T*> object_list_;
105 std::unordered_map<I, T> object_dict_;
106};
107
108template <typename I, typename T>
109class ThreadSafeIndexedList : public IndexedList<I, T> {
110 public:
111 T* Add(const I id, const T& object) {
112 boost::unique_lock<boost::shared_mutex> writer_lock(mutex_);
113 return IndexedList<I, T>::Add(id, object);
114 }
115
116 T* Find(const I id) {
117 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
118 return IndexedList<I, T>::Find(id);
119 }
120
121 std::vector<const T*> Items() const {
122 boost::shared_lock<boost::shared_mutex> reader_lock(mutex_);
124 }
125
126 private:
127 mutable boost::shared_mutex mutex_;
128};
129
130} // namespace planning
131} // namespace apollo
const std::unordered_map< I, T > & Dict() const
List all the items in the container.
T * Find(const I id)
Find object by id in the container
T * Add(const I id, const T &object)
copy object into the container.
const T * Find(const I id) const
Find object by id in the container
const std::vector< const T * > & Items() const
List all the items in the container.
IndexedList & operator=(const IndexedList &other)
Copy the container with objects.
T * Add(const I id, const T &object)
std::vector< const T * > Items() const
Planning module main class.
#define AWARN
Definition log.h:43
Some map util functions.
class register implement
Definition arena_queue.h:37