unilink  0.4.3
A simple C++ library for unified async communication
channel_factory.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
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 
18 
25 
26 namespace unilink {
27 namespace factory {
28 
29 std::shared_ptr<interface::Channel> ChannelFactory::create(const ChannelOptions& options,
30  std::shared_ptr<boost::asio::io_context> external_ioc) {
31  return std::visit(
32  [&external_ioc](const auto& config) -> std::shared_ptr<interface::Channel> {
33  using T = std::decay_t<decltype(config)>;
34 
35  if constexpr (std::is_same_v<T, config::TcpClientConfig>) {
36  return create_tcp_client(config, external_ioc);
37  } else if constexpr (std::is_same_v<T, config::TcpServerConfig>) {
38  return create_tcp_server(config, external_ioc);
39  } else if constexpr (std::is_same_v<T, config::SerialConfig>) {
40  return create_serial(config, external_ioc);
41  } else if constexpr (std::is_same_v<T, config::UdpConfig>) {
42  return create_udp(config, external_ioc);
43  } else {
44  static_assert(std::is_same_v<T, void>, "Unsupported config type");
45  return nullptr;
46  }
47  },
48  options);
49 }
50 
51 std::shared_ptr<interface::Channel> ChannelFactory::create_tcp_server(
52  const config::TcpServerConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
53  if (external_ioc) {
54  auto acceptor = std::make_unique<transport::BoostTcpAcceptor>(*external_ioc);
55  return transport::TcpServer::create(cfg, std::move(acceptor), *external_ioc);
56  }
57  return transport::TcpServer::create(cfg);
58 }
59 
60 std::shared_ptr<interface::Channel> ChannelFactory::create_tcp_client(
61  const config::TcpClientConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
62  if (external_ioc) {
63  return transport::TcpClient::create(cfg, *external_ioc);
64  }
65  return transport::TcpClient::create(cfg);
66 }
67 
68 std::shared_ptr<interface::Channel> ChannelFactory::create_serial(
69  const config::SerialConfig& cfg, std::shared_ptr<boost::asio::io_context> external_ioc) {
70  if (external_ioc) {
71  return transport::Serial::create(cfg, std::make_unique<transport::BoostSerialPort>(*external_ioc), *external_ioc);
72  }
73  return transport::Serial::create(cfg);
74 }
75 
76 std::shared_ptr<interface::Channel> ChannelFactory::create_udp(const config::UdpConfig& cfg,
77  std::shared_ptr<boost::asio::io_context> external_ioc) {
78  if (external_ioc) {
79  return transport::UdpChannel::create(cfg, *external_ioc);
80  }
82 }
83 
84 } // namespace factory
85 } // namespace unilink