Apollo  6.0
Open source self driving car software
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_RECEIVER_H_
18 #define CYBER_TRANSPORT_RECEIVER_RECEIVER_H_
19 
20 #include <functional>
21 #include <memory>
22 
26 
27 namespace apollo {
28 namespace cyber {
29 namespace transport {
30 
31 template <typename M>
32 class Receiver : public Endpoint {
33  public:
34  using MessagePtr = std::shared_ptr<M>;
35  using MessageListener = std::function<void(
36  const MessagePtr&, const MessageInfo&, const RoleAttributes&)>;
37 
38  Receiver(const RoleAttributes& attr, const MessageListener& msg_listener);
39  virtual ~Receiver();
40 
41  virtual void Enable() = 0;
42  virtual void Disable() = 0;
43  virtual void Enable(const RoleAttributes& opposite_attr) = 0;
44  virtual void Disable(const RoleAttributes& opposite_attr) = 0;
45 
46  protected:
47  void OnNewMessage(const MessagePtr& msg, const MessageInfo& msg_info);
48 
50 };
51 
52 template <typename M>
53 Receiver<M>::Receiver(const RoleAttributes& attr,
54  const MessageListener& msg_listener)
55  : Endpoint(attr), msg_listener_(msg_listener) {}
56 
57 template <typename M>
59 
60 template <typename M>
62  const MessageInfo& msg_info) {
63  if (msg_listener_ != nullptr) {
64  msg_listener_(msg, msg_info, attr_);
65  }
66 }
67 
68 } // namespace transport
69 } // namespace cyber
70 } // namespace apollo
71 
72 #endif // CYBER_TRANSPORT_RECEIVER_RECEIVER_H_
Definition: receiver.h:32
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual ~Receiver()
Definition: receiver.h:58
RoleAttributes attr_
Definition: endpoint.h:47
Definition: message_info.h:30
void OnNewMessage(const MessagePtr &msg, const MessageInfo &msg_info)
Definition: receiver.h:61
std::function< void(const MessagePtr &, const MessageInfo &, const RoleAttributes &)> MessageListener
Definition: receiver.h:36
Definition: endpoint.h:36
MessageListener msg_listener_
Definition: receiver.h:49
std::shared_ptr< M > MessagePtr
Definition: receiver.h:34
Receiver(const RoleAttributes &attr, const MessageListener &msg_listener)
Definition: receiver.h:53