Apollo  6.0
Open source self driving car software
shm_receiver.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_TRANSPORT_RECEIVER_SHM_RECEIVER_H_
18 #define CYBER_TRANSPORT_RECEIVER_SHM_RECEIVER_H_
19 
20 #include <functional>
21 
22 #include "cyber/common/log.h"
25 
26 namespace apollo {
27 namespace cyber {
28 namespace transport {
29 
30 template <typename M>
31 class ShmReceiver : public Receiver<M> {
32  public:
33  ShmReceiver(const RoleAttributes& attr,
34  const typename Receiver<M>::MessageListener& msg_listener);
35  virtual ~ShmReceiver();
36 
37  void Enable() override;
38  void Disable() override;
39 
40  void Enable(const RoleAttributes& opposite_attr) override;
41  void Disable(const RoleAttributes& opposite_attr) override;
42 
43  private:
44  ShmDispatcherPtr dispatcher_;
45 };
46 
47 template <typename M>
49  const RoleAttributes& attr,
50  const typename Receiver<M>::MessageListener& msg_listener)
51  : Receiver<M>(attr, msg_listener) {
52  dispatcher_ = ShmDispatcher::Instance();
53 }
54 
55 template <typename M>
57  Disable();
58 }
59 
60 template <typename M>
62  if (this->enabled_) {
63  return;
64  }
65 
66  dispatcher_->AddListener<M>(
67  this->attr_, std::bind(&ShmReceiver<M>::OnNewMessage, this,
68  std::placeholders::_1, std::placeholders::_2));
69  this->enabled_ = true;
70 }
71 
72 template <typename M>
74  if (!this->enabled_) {
75  return;
76  }
77 
78  dispatcher_->RemoveListener<M>(this->attr_);
79  this->enabled_ = false;
80 }
81 
82 template <typename M>
83 void ShmReceiver<M>::Enable(const RoleAttributes& opposite_attr) {
84  dispatcher_->AddListener<M>(
85  this->attr_, opposite_attr,
86  std::bind(&ShmReceiver<M>::OnNewMessage, this, std::placeholders::_1,
87  std::placeholders::_2));
88 }
89 
90 template <typename M>
91 void ShmReceiver<M>::Disable(const RoleAttributes& opposite_attr) {
92  dispatcher_->RemoveListener<M>(this->attr_, opposite_attr);
93 }
94 
95 } // namespace transport
96 } // namespace cyber
97 } // namespace apollo
98 
99 #endif // CYBER_TRANSPORT_RECEIVER_SHM_RECEIVER_H_
void AddListener(const RoleAttributes &self_attr, const MessageListener< MessageT > &listener)
Definition: shm_dispatcher.h:82
bool enabled_
Definition: endpoint.h:45
Definition: receiver.h:32
void RemoveListener(const RoleAttributes &self_attr)
Definition: dispatcher.h:144
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Definition: shm_receiver.h:31
ShmReceiver(const RoleAttributes &attr, const typename Receiver< M >::MessageListener &msg_listener)
Definition: shm_receiver.h:48
Definition: shm_dispatcher.h:45
RoleAttributes attr_
Definition: endpoint.h:47
virtual ~ShmReceiver()
Definition: shm_receiver.h:56
void Disable() override
Definition: shm_receiver.h:73
void Enable() override
Definition: shm_receiver.h:61
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition: receiver.h:36