Apollo 10.0
自动驾驶开放平台
data_notifier.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
17#ifndef CYBER_DATA_DATA_NOTIFIER_H_
18#define CYBER_DATA_DATA_NOTIFIER_H_
19
20#include <memory>
21#include <mutex>
22#include <vector>
23
24#include "cyber/common/log.h"
25#include "cyber/common/macros.h"
28#include "cyber/time/time.h"
29
30namespace apollo {
31namespace cyber {
32namespace data {
33
37
38struct Notifier {
39 std::function<void()> callback;
40};
41
43 public:
44 using NotifyVector = std::vector<std::shared_ptr<Notifier>>;
46
47 void AddNotifier(uint64_t channel_id,
48 const std::shared_ptr<Notifier>& notifier);
49
50 bool Notify(const uint64_t channel_id);
51
52 private:
53 std::mutex notifies_map_mutex_;
55
57};
58
59inline DataNotifier::DataNotifier() {}
60
62 uint64_t channel_id, const std::shared_ptr<Notifier>& notifier) {
63 std::lock_guard<std::mutex> lock(notifies_map_mutex_);
64 NotifyVector* notifies = nullptr;
65 if (notifies_map_.Get(channel_id, &notifies)) {
66 notifies->emplace_back(notifier);
67 } else {
68 NotifyVector new_notify = {notifier};
69 notifies_map_.Set(channel_id, new_notify);
70 }
71}
72
73inline bool DataNotifier::Notify(const uint64_t channel_id) {
74 NotifyVector* notifies = nullptr;
75 if (notifies_map_.Get(channel_id, &notifies)) {
76 for (auto& notifier : *notifies) {
77 if (notifier && notifier->callback) {
78 notifier->callback();
79 }
80 }
81 return true;
82 }
83 return false;
84}
85
86} // namespace data
87} // namespace cyber
88} // namespace apollo
89
90#endif // CYBER_DATA_DATA_NOTIFIER_H_
Cyber has builtin time type Time.
Definition time.h:31
A implementation of lock-free fixed size hash map
bool Notify(const uint64_t channel_id)
void AddNotifier(uint64_t channel_id, const std::shared_ptr< Notifier > &notifier)
std::vector< std::shared_ptr< Notifier > > NotifyVector
#define DECLARE_SINGLETON(classname)
Definition macros.h:52
class register implement
Definition arena_queue.h:37
std::function< void()> callback