Apollo  6.0
Open source self driving car software
channel_reader.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 #pragma once
18 
19 #include <memory>
20 #include <string>
21 
22 #include "cyber/cyber.h"
23 
24 class MainWindow;
25 
26 template <typename T>
27 using CyberChannelCallback = std::function<void(const std::shared_ptr<T>&)>;
28 
29 template <typename T>
31  public:
32  CyberChannReader(void) : channel_callback_(nullptr), channel_node_(nullptr) {}
33 
35 
36  void CloseChannel(void) {
37  if (channel_reader_ != nullptr) {
38  channel_reader_.reset();
39  }
40 
41  if (channel_node_ != nullptr) {
42  channel_node_.reset();
43  }
44  }
45 
47  const std::string& channelName,
48  const std::string& nodeName) {
49  return InstallCallback(channelCallback) &&
50  OpenChannel(channelName, nodeName);
51  }
52 
53  bool InstallCallback(CyberChannelCallback<T> channelCallback) {
54  if (channelCallback != nullptr) {
55  channel_callback_ = channelCallback;
56  return true;
57  } else {
58  std::cerr << "Parameter readerCallback is null" << std::endl;
59  return false;
60  }
61  }
62 
63  bool OpenChannel(const std::string& channelName,
64  const std::string& nodeName) {
65  if (channelName.empty() || nodeName.empty()) {
66  std::cerr << "Channel Name or Node Name must be not empty" << std::endl;
67  return false;
68  }
69 
70  if (channel_node_ != nullptr || channel_reader_ != nullptr ||
71  !channel_callback_) {
72  return false;
73  }
74 
75  return CreateChannel(channelName, nodeName);
76  }
77 
78  const std::string& NodeName(void) const { return channel_node_->Name(); }
79 
80  private:
81  bool CreateChannel(const std::string& channelName,
82  const std::string& nodeName) {
83  if (channel_node_ == nullptr) {
84  channel_node_ = apollo::cyber::CreateNode(nodeName);
85  if (channel_node_ == nullptr) {
86  return false;
87  }
88  }
89 
90  channel_reader_ =
91  channel_node_->CreateReader<T>(channelName, channel_callback_);
92 
93  if (channel_reader_ == nullptr) {
94  std::cout << "----------Creat reader failed---------" << std::endl;
95  return false;
96  }
97  return true;
98  }
99 
100  CyberChannelCallback<T> channel_callback_;
101  std::shared_ptr<apollo::cyber::Reader<T>> channel_reader_;
102  std::shared_ptr<apollo::cyber::Node> channel_node_;
103 };
~CyberChannReader()
Definition: channel_reader.h:34
bool InstallCallback(CyberChannelCallback< T > channelCallback)
Definition: channel_reader.h:53
CyberChannReader(void)
Definition: channel_reader.h:32
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space="")
Definition: channel_reader.h:30
const std::string & NodeName(void) const
Definition: channel_reader.h:78
void CloseChannel(void)
Definition: channel_reader.h:36
std::function< void(const std::shared_ptr< T > &)> CyberChannelCallback
Definition: channel_reader.h:27
bool OpenChannel(const std::string &channelName, const std::string &nodeName)
Definition: channel_reader.h:63
Definition: main_window.h:47
bool InstallCallbackAndOpen(CyberChannelCallback< T > channelCallback, const std::string &channelName, const std::string &nodeName)
Definition: channel_reader.h:46