2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-19 16:12:09 +00:00

Add converting move construction/assignment to serial ports.

This commit is contained in:
Christopher Kohlhoff
2022-06-30 12:17:39 +10:00
parent ca858d3ecd
commit 4ef2cd0054
2 changed files with 55 additions and 2 deletions

View File

@@ -292,6 +292,55 @@ public:
impl_ = std::move(other.impl_);
return *this;
}
// All serial ports have access to each other's implementations.
template <typename Executor1>
friend class basic_serial_port;
/// Move-construct a basic_serial_port from a serial port of another executor
/// type.
/**
* This constructor moves a serial port from one object to another.
*
* @param other The other basic_serial_port object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_serial_port(const executor_type&)
* constructor.
*/
template <typename Executor1>
basic_serial_port(basic_serial_port<Executor1>&& other,
typename constraint<
is_convertible<Executor1, Executor>::value,
defaulted_constraint
>::type = defaulted_constraint())
: impl_(std::move(other.impl_))
{
}
/// Move-assign a basic_serial_port from a serial port of another executor
/// type.
/**
* This assignment operator moves a serial port from one object to another.
*
* @param other The other basic_serial_port object from which the move will
* occur.
*
* @note Following the move, the moved-from object is in the same state as if
* constructed using the @c basic_serial_port(const executor_type&)
* constructor.
*/
template <typename Executor1>
typename constraint<
is_convertible<Executor1, Executor>::value,
basic_serial_port&
>::type operator=(basic_serial_port<Executor1>&& other)
{
basic_serial_port tmp(std::move(other));
impl_ = std::move(tmp.impl_);
return *this;
}
#endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Destroys the serial port.

View File

@@ -85,6 +85,9 @@ void test()
#if defined(BOOST_ASIO_HAS_MOVE)
serial_port port7(std::move(port6));
basic_serial_port<io_context::executor_type> port8(ioc);
serial_port port9(std::move(port8));
#endif // defined(BOOST_ASIO_HAS_MOVE)
// basic_serial_port operators.
@@ -92,6 +95,7 @@ void test()
#if defined(BOOST_ASIO_HAS_MOVE)
port1 = serial_port(ioc);
port1 = std::move(port2);
port1 = std::move(port8);
#endif // defined(BOOST_ASIO_HAS_MOVE)
// basic_io_object functions.
@@ -104,8 +108,8 @@ void test()
serial_port::lowest_layer_type& lowest_layer = port1.lowest_layer();
(void)lowest_layer;
const serial_port& port8 = port1;
const serial_port::lowest_layer_type& lowest_layer2 = port8.lowest_layer();
const serial_port& port10 = port1;
const serial_port::lowest_layer_type& lowest_layer2 = port10.lowest_layer();
(void)lowest_layer2;
port1.open("null");