unilink  0.4.3
A simple C++ library for unified async communication
serial_builder.hpp
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 
17 #pragma once
18 
19 #include <chrono>
20 #include <cstdint>
21 #include <string>
22 
26 
27 namespace unilink {
28 namespace builder {
29 
33 #ifdef _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable : 4251)
36 #endif
37 class UNILINK_API SerialBuilder : public BuilderInterface<wrapper::Serial, SerialBuilder> {
38  public:
39  SerialBuilder(const std::string& device, uint32_t baud_rate);
40 
41  // Delete copy, allow move
42  SerialBuilder(const SerialBuilder&) = delete;
46 
47  std::unique_ptr<wrapper::Serial> build() override;
48 
49  SerialBuilder& auto_manage(bool auto_manage = true) override;
50 
51  // Modernized event handlers
52  SerialBuilder& on_data(std::function<void(const wrapper::MessageContext&)> handler) override;
53  SerialBuilder& on_connect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
54  SerialBuilder& on_disconnect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
55  SerialBuilder& on_error(std::function<void(const wrapper::ErrorContext&)> handler) override;
56 
60  SerialBuilder& data_bits(int bits);
61 
65  SerialBuilder& stop_bits(int bits);
66 
70  SerialBuilder& parity(const std::string& p);
71 
75  SerialBuilder& flow_control(const std::string& flow);
76 
80  SerialBuilder& retry_interval(uint32_t milliseconds);
81 
85  SerialBuilder& use_independent_context(bool use_independent = true);
86 
87  private:
88  std::string device_;
89  uint32_t baud_rate_;
90  bool auto_manage_;
91  bool use_independent_context_;
92 
93  // Configuration
94  int data_bits_;
95  int stop_bits_;
96  std::string parity_;
97  std::string flow_control_;
98  std::chrono::milliseconds retry_interval_;
99 
100  // Callbacks
101  std::function<void(const wrapper::MessageContext&)> on_data_;
102  std::function<void(const wrapper::ConnectionContext&)> on_connect_;
103  std::function<void(const wrapper::ConnectionContext&)> on_disconnect_;
104  std::function<void(const wrapper::ErrorContext&)> on_error_;
105 };
106 
107 #ifdef _MSC_VER
108 #pragma warning(pop)
109 #endif
110 
111 } // namespace builder
112 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37