Skip to content

Commit

Permalink
Add converting move construction/assignment to serial ports.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskohlhoff committed Jun 30, 2022
1 parent 322f311 commit 67bdae5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
49 changes: 49 additions & 0 deletions asio/include/asio/basic_serial_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,55 @@ class basic_serial_port
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(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)

/// Destroys the serial port.
Expand Down
8 changes: 6 additions & 2 deletions asio/src/tests/unit/serial_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,17 @@ void test()

#if defined(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(ASIO_HAS_MOVE)

// basic_serial_port operators.

#if defined(ASIO_HAS_MOVE)
port1 = serial_port(ioc);
port1 = std::move(port2);
port1 = std::move(port8);
#endif // defined(ASIO_HAS_MOVE)

// basic_io_object functions.
Expand All @@ -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");
Expand Down

0 comments on commit 67bdae5

Please sign in to comment.