Apollo 10.0
自动驾驶开放平台
msg_converter.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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 <functional>
20#include <memory>
21#include <string>
22#include <vector>
23
24#include "cyber/cyber.h"
25
26namespace apollo {
27namespace perception {
28
30 public:
31 template <class U, class V>
32 using Callback = bool(*)(const std::shared_ptr<U> &, V *);
33
34 public:
35 MsgConverter() = default;
36 explicit MsgConverter(const std::shared_ptr<cyber::Node> &node)
37 : node_(node) {}
38
39 virtual ~MsgConverter() = default;
40
41 template <class From, class To>
42 bool Add(const std::string &from_topic, const std::string &to_topic,
43 Callback<From, To> convert);
44
45 private:
46 std::vector<std::shared_ptr<cyber::ReaderBase>> readers_;
47 std::vector<std::shared_ptr<cyber::WriterBase>> writers_;
48
49 std::shared_ptr<cyber::Node> node_;
50};
51
52template <class From, class To>
53bool MsgConverter::Add(const std::string &from_topic,
54 const std::string &to_topic,
55 Callback<From, To> convert) {
56 AINFO << "Convert: " << from_topic << " to " << to_topic;
57 auto writer = node_->CreateWriter<To>(to_topic);
58 auto reader = node_->CreateReader<From>(
59 from_topic, [=](const std::shared_ptr<From> &from) {
60 std::shared_ptr<To> to(new To());
61 bool res = convert(from, to.get());
62 if (res) {
63 writer->Write(to);
64 }
65 });
66
67 writers_.push_back(writer);
68 readers_.push_back(reader);
69 return true;
70}
71
72} // namespace perception
73} // namespace apollo
bool Add(const std::string &from_topic, const std::string &to_topic, Callback< From, To > convert)
MsgConverter(const std::shared_ptr< cyber::Node > &node)
bool(*)(const std::shared_ptr< U > &, V *) Callback
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37