Apollo  6.0
Open source self driving car software
data_notifier.h
Go to the documentation of this file.
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 
30 namespace apollo {
31 namespace cyber {
32 namespace data {
33 
37 
38 struct Notifier {
39  std::function<void()> callback;
40 };
41 
42 class DataNotifier {
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 
59 inline 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 
73 inline 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_
::apollo::cyber::Time Time
Definition: racobit_radar_message_manager.h:41
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: data_notifier.h:38
~DataNotifier()
Definition: data_notifier.h:45
#define DECLARE_SINGLETON(classname)
Definition: macros.h:52
A implementation of lock-free fixed size hash map.
Definition: atomic_hash_map.h:40
Definition: data_notifier.h:42
std::vector< std::shared_ptr< Notifier > > NotifyVector
Definition: data_notifier.h:44
std::function< void()> callback
Definition: data_notifier.h:39
Definition: perf_event_cache.h:36
void AddNotifier(uint64_t channel_id, const std::shared_ptr< Notifier > &notifier)
Definition: data_notifier.h:61
bool Notify(const uint64_t channel_id)
Definition: data_notifier.h:73