unilink  0.4.3
A simple C++ library for unified async communication
ibuilder.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 <functional>
20 #include <memory>
21 #include <string>
22 #include <string_view>
23 #include <vector>
24 
31 
32 namespace unilink {
33 namespace builder {
34 
44 template <typename T, typename Derived>
46  public:
47  virtual ~BuilderInterface() = default;
48  BuilderInterface() = default;
53 
58  virtual std::unique_ptr<T> build() = 0;
59 
65  virtual Derived& auto_manage(bool auto_manage = true) = 0;
66 
72  virtual Derived& on_data(std::function<void(const wrapper::MessageContext&)> handler) = 0;
73 
82  template <typename U, typename F>
83  Derived& on_data(U* obj, F method) {
84  return on_data([obj, method](const wrapper::MessageContext& ctx) { (obj->*method)(ctx); });
85  }
86 
92  virtual Derived& on_connect(std::function<void(const wrapper::ConnectionContext&)> handler) = 0;
93 
102  template <typename U, typename F>
103  Derived& on_connect(U* obj, F method) {
104  return on_connect([obj, method](const wrapper::ConnectionContext& ctx) { (obj->*method)(ctx); });
105  }
106 
112  virtual Derived& on_disconnect(std::function<void(const wrapper::ConnectionContext&)> handler) = 0;
113 
122  template <typename U, typename F>
123  Derived& on_disconnect(U* obj, F method) {
124  return on_disconnect([obj, method](const wrapper::ConnectionContext& ctx) { (obj->*method)(ctx); });
125  }
126 
132  virtual Derived& on_error(std::function<void(const wrapper::ErrorContext&)> handler) = 0;
133 
142  template <typename U, typename F>
143  Derived& on_error(U* obj, F method) {
144  return on_error([obj, method](const wrapper::ErrorContext& ctx) { (obj->*method)(ctx); });
145  }
146 
147  // Framing Support (remains focused on raw data for now, can be evolved)
148 
156  Derived& use_line_framer(std::string_view delimiter = "\n", bool include_delimiter = false,
157  size_t max_length = 65536) {
158  framer_ = std::make_unique<framer::LineFramer>(delimiter, include_delimiter, max_length);
159  return static_cast<Derived&>(*this);
160  }
161 
169  Derived& use_packet_framer(const std::vector<uint8_t>& start_pattern, const std::vector<uint8_t>& end_pattern,
170  size_t max_length) {
171  framer_ = std::make_unique<framer::PacketFramer>(start_pattern, end_pattern, max_length);
172  return static_cast<Derived&>(*this);
173  }
174 
180  Derived& on_message(std::function<void(memory::ConstByteSpan)> handler) {
181  on_message_ = std::move(handler);
182  return static_cast<Derived&>(*this);
183  }
184 
193  template <typename U, typename F>
194  Derived& on_message(U* obj, F method) {
195  return on_message([obj, method](memory::ConstByteSpan msg) { (obj->*method)(msg); });
196  }
197 
198  protected:
199  std::unique_ptr<framer::IFramer> framer_;
200  std::function<void(memory::ConstByteSpan)> on_message_;
201 };
202 
203 } // namespace builder
204 } // namespace unilink