Apollo 10.0
自动驾驶开放平台
single_value_warehouse.cc
浏览该文件的文档.
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
18
19#include "cyber/common/log.h"
20
21namespace apollo {
22namespace cyber {
23namespace service_discovery {
24
25using base::AtomicRWLock;
26using base::ReadLockGuard;
27using base::WriteLockGuard;
28using proto::RoleAttributes;
29
30bool SingleValueWarehouse::Add(uint64_t key, const RolePtr& role,
31 bool ignore_if_exist) {
32 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
33 if (!ignore_if_exist) {
34 if (roles_.find(key) != roles_.end()) {
35 return false;
36 }
37 }
38 roles_[key] = role;
39 return true;
40}
41
43 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
44 roles_.clear();
45}
46
48 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
49 return roles_.size();
50}
51
52void SingleValueWarehouse::Remove(uint64_t key) {
53 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
54 roles_.erase(key);
55}
56
57void SingleValueWarehouse::Remove(uint64_t key, const RolePtr& role) {
58 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
59 auto search = roles_.find(key);
60 if (search == roles_.end()) {
61 return;
62 }
63 if (!search->second->Match(role->attributes())) {
64 return;
65 }
66 roles_.erase(search);
67}
68
70 WriteLockGuard<AtomicRWLock> lock(rw_lock_);
71 for (auto it = roles_.begin(); it != roles_.end();) {
72 auto curr_role = it->second;
73 if (curr_role->Match(target_attr)) {
74 it = roles_.erase(it);
75 } else {
76 ++it;
77 }
78 }
79}
80
81bool SingleValueWarehouse::Search(uint64_t key) {
82 RolePtr role;
83 return Search(key, &role);
84}
85
86bool SingleValueWarehouse::Search(uint64_t key, RolePtr* first_matched_role) {
87 RETURN_VAL_IF_NULL(first_matched_role, false);
88 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
89 auto search = roles_.find(key);
90 if (search == roles_.end()) {
91 return false;
92 }
93 *first_matched_role = search->second;
94 return true;
95}
96
98 RoleAttributes* first_matched_role_attr) {
99 RETURN_VAL_IF_NULL(first_matched_role_attr, false);
100 RolePtr role;
101 if (!Search(key, &role)) {
102 return false;
103 }
104 first_matched_role_attr->CopyFrom(role->attributes());
105 return true;
106}
107
109 std::vector<RolePtr>* matched_roles) {
110 RETURN_VAL_IF_NULL(matched_roles, false);
111 RolePtr role;
112 if (!Search(key, &role)) {
113 return false;
114 }
115 matched_roles->emplace_back(role);
116 return true;
117}
118
120 uint64_t key, std::vector<RoleAttributes>* matched_roles_attr) {
121 RETURN_VAL_IF_NULL(matched_roles_attr, false);
122 RolePtr role;
123 if (!Search(key, &role)) {
124 return false;
125 }
126 matched_roles_attr->emplace_back(role->attributes());
127 return true;
128}
129
131 RolePtr role;
132 return Search(target_attr, &role);
133}
134
136 RolePtr* first_matched_role) {
137 RETURN_VAL_IF_NULL(first_matched_role, false);
138 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
139 for (auto& item : roles_) {
140 if (item.second->Match(target_attr)) {
141 *first_matched_role = item.second;
142 return true;
143 }
144 }
145 return false;
146}
147
149 RoleAttributes* first_matched_role_attr) {
150 RETURN_VAL_IF_NULL(first_matched_role_attr, false);
151 RolePtr role;
152 if (!Search(target_attr, &role)) {
153 return false;
154 }
155 first_matched_role_attr->CopyFrom(role->attributes());
156 return true;
157}
158
160 std::vector<RolePtr>* matched_roles) {
161 RETURN_VAL_IF_NULL(matched_roles, false);
162 bool find = false;
163 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
164 for (auto& item : roles_) {
165 if (item.second->Match(target_attr)) {
166 matched_roles->emplace_back(item.second);
167 find = true;
168 }
169 }
170 return find;
171}
172
174 const RoleAttributes& target_attr,
175 std::vector<RoleAttributes>* matched_roles_attr) {
176 RETURN_VAL_IF_NULL(matched_roles_attr, false);
177 bool find = false;
178 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
179 for (auto& item : roles_) {
180 if (item.second->Match(target_attr)) {
181 matched_roles_attr->emplace_back(item.second->attributes());
182 find = true;
183 }
184 }
185 return find;
186}
187
188void SingleValueWarehouse::GetAllRoles(std::vector<RolePtr>* roles) {
189 RETURN_IF_NULL(roles);
190 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
191 for (auto& item : roles_) {
192 roles->emplace_back(item.second);
193 }
194}
195
197 std::vector<RoleAttributes>* roles_attr) {
198 RETURN_IF_NULL(roles_attr);
199 ReadLockGuard<AtomicRWLock> lock(rw_lock_);
200 for (auto& item : roles_) {
201 roles_attr->emplace_back(item.second->attributes());
202 }
203}
204
205} // namespace service_discovery
206} // namespace cyber
207} // namespace apollo
void GetAllRoles(std::vector< RolePtr > *roles) override
bool Add(uint64_t key, const RolePtr &role, bool ignore_if_exist=true) override
#define RETURN_VAL_IF_NULL(ptr, val)
Definition log.h:98
#define RETURN_IF_NULL(ptr)
Definition log.h:90
std::shared_ptr< RoleBase > RolePtr
Definition role.h:31
class register implement
Definition arena_queue.h:37