unilink  0.4.3
A simple C++ library for unified async communication
io_context_manager.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 <boost/asio/executor_work_guard.hpp>
20 #include <boost/asio/io_context.hpp>
21 #include <memory>
22 
24 
25 namespace unilink {
26 namespace concurrency {
27 
33  public:
34  using IoContext = boost::asio::io_context;
35  using WorkGuard = boost::asio::executor_work_guard<IoContext::executor_type>;
36 
37  static IoContextManager& instance();
38 
40  explicit IoContextManager(std::shared_ptr<IoContext> external_context);
41  explicit IoContextManager(IoContext& external_context);
43 
44  // Move semantics
46  IoContextManager& operator=(IoContextManager&&) noexcept;
47 
48  // Non-copyable
50  IoContextManager& operator=(const IoContextManager&) = delete;
51 
52  IoContext& get_context();
53  void start();
54  void stop();
55  bool is_running() const;
56  std::unique_ptr<IoContext> create_independent_context();
57 
58  private:
59  struct Impl;
60  const Impl* get_impl() const { return impl_.get(); }
61  Impl* get_impl() { return impl_.get(); }
62  std::unique_ptr<Impl> impl_;
63 };
64 
65 } // namespace concurrency
66 
67 namespace common {
69 } // namespace common
70 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37