io_object_impl.hpp
4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// io_object_impl.hpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef ASIO_DETAIL_IO_OBJECT_IMPL_HPP
#define ASIO_DETAIL_IO_OBJECT_IMPL_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include <new>
#include "asio/detail/config.hpp"
#include "asio/detail/type_traits.hpp"
#include "asio/execution/executor.hpp"
#include "asio/execution/context.hpp"
#include "asio/io_context.hpp"
#include "asio/query.hpp"
#include "asio/detail/push_options.hpp"
namespace asio {
namespace detail {
template <typename IoObjectService,
typename Executor = io_context::executor_type>
class io_object_impl
{
public:
// The type of the service that will be used to provide I/O operations.
typedef IoObjectService service_type;
// The underlying implementation type of I/O object.
typedef typename service_type::implementation_type implementation_type;
// The type of the executor associated with the object.
typedef Executor executor_type;
// Construct an I/O object using an executor.
explicit io_object_impl(const executor_type& ex)
: service_(&asio::use_service<IoObjectService>(
io_object_impl::get_context(ex))),
executor_(ex)
{
service_->construct(implementation_);
}
// Construct an I/O object using an execution context.
template <typename ExecutionContext>
explicit io_object_impl(ExecutionContext& context,
typename enable_if<is_convertible<
ExecutionContext&, execution_context&>::value>::type* = 0)
: service_(&asio::use_service<IoObjectService>(context)),
executor_(context.get_executor())
{
service_->construct(implementation_);
}
#if defined(ASIO_HAS_MOVE)
// Move-construct an I/O object.
io_object_impl(io_object_impl&& other)
: service_(&other.get_service()),
executor_(other.get_executor())
{
service_->move_construct(implementation_, other.implementation_);
}
// Perform a converting move-construction of an I/O object.
template <typename IoObjectService1, typename Executor1>
io_object_impl(io_object_impl<IoObjectService1, Executor1>&& other)
: service_(&asio::use_service<IoObjectService>(
io_object_impl::get_context(other.get_executor()))),
executor_(other.get_executor())
{
service_->converting_move_construct(implementation_,
other.get_service(), other.get_implementation());
}
#endif // defined(ASIO_HAS_MOVE)
// Destructor.
~io_object_impl()
{
service_->destroy(implementation_);
}
#if defined(ASIO_HAS_MOVE)
// Move-assign an I/O object.
io_object_impl& operator=(io_object_impl&& other)
{
if (this != &other)
{
service_->move_assign(implementation_,
*other.service_, other.implementation_);
executor_.~executor_type();
new (&executor_) executor_type(
std::move(other.executor_));
service_ = other.service_;
}
return *this;
}
#endif // defined(ASIO_HAS_MOVE)
// Get the executor associated with the object.
const executor_type& get_executor() ASIO_NOEXCEPT
{
return executor_;
}
// Get the service associated with the I/O object.
service_type& get_service()
{
return *service_;
}
// Get the service associated with the I/O object.
const service_type& get_service() const
{
return *service_;
}
// Get the underlying implementation of the I/O object.
implementation_type& get_implementation()
{
return implementation_;
}
// Get the underlying implementation of the I/O object.
const implementation_type& get_implementation() const
{
return implementation_;
}
private:
// Helper function to get an executor's context.
template <typename T>
static execution_context& get_context(const T& t,
typename enable_if<execution::is_executor<T>::value>::type* = 0)
{
return asio::query(t, execution::context);
}
// Helper function to get an executor's context.
template <typename T>
static execution_context& get_context(const T& t,
typename enable_if<!execution::is_executor<T>::value>::type* = 0)
{
return t.context();
}
// Disallow copying and copy assignment.
io_object_impl(const io_object_impl&);
io_object_impl& operator=(const io_object_impl&);
// The service associated with the I/O object.
service_type* service_;
// The underlying implementation of the I/O object.
implementation_type implementation_;
// The associated executor.
executor_type executor_;
};
} // namespace detail
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_DETAIL_IO_OBJECT_IMPL_HPP