Apollo 10.0
自动驾驶开放平台
intra_receiver.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_TRANSPORT_RECEIVER_INTRA_RECEIVER_H_
18#define CYBER_TRANSPORT_RECEIVER_INTRA_RECEIVER_H_
19
20#include "cyber/common/log.h"
23
24namespace apollo {
25namespace cyber {
26namespace transport {
27
28template <typename M>
29class IntraReceiver : public Receiver<M> {
30 public:
31 IntraReceiver(const RoleAttributes& attr,
32 const typename Receiver<M>::MessageListener& msg_listener);
33 virtual ~IntraReceiver();
34
35 void Enable() override;
36 void Disable() override;
37
38 void Enable(const RoleAttributes& opposite_attr) override;
39 void Disable(const RoleAttributes& opposite_attr) override;
40
41 private:
42 IntraDispatcherPtr dispatcher_;
43};
44
45template <typename M>
47 const RoleAttributes& attr,
48 const typename Receiver<M>::MessageListener& msg_listener)
49 : Receiver<M>(attr, msg_listener) {
50 dispatcher_ = IntraDispatcher::Instance();
51}
52
53template <typename M>
57
58template <typename M>
60 if (this->enabled_) {
61 return;
62 }
63
64 dispatcher_->AddListener<M>(
65 this->attr_, std::bind(&IntraReceiver<M>::OnNewMessage, this,
66 std::placeholders::_1, std::placeholders::_2));
67 this->enabled_ = true;
68}
69
70template <typename M>
72 if (!this->enabled_) {
73 return;
74 }
75
76 dispatcher_->RemoveListener<M>(this->attr_);
77 this->enabled_ = false;
78}
79
80template <typename M>
81void IntraReceiver<M>::Enable(const RoleAttributes& opposite_attr) {
82 dispatcher_->AddListener<M>(
83 this->attr_, opposite_attr,
84 std::bind(&IntraReceiver<M>::OnNewMessage, this, std::placeholders::_1,
85 std::placeholders::_2));
86}
87
88template <typename M>
89void IntraReceiver<M>::Disable(const RoleAttributes& opposite_attr) {
90 dispatcher_->RemoveListener<M>(this->attr_, opposite_attr);
91}
92
93} // namespace transport
94} // namespace cyber
95} // namespace apollo
96
97#endif // CYBER_TRANSPORT_RECEIVER_INTRA_RECEIVER_H_
IntraReceiver(const RoleAttributes &attr, const typename Receiver< M >::MessageListener &msg_listener)
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition receiver.h:36
class register implement
Definition arena_queue.h:37