Add move support to serial ports.

This commit is contained in:
Christopher Kohlhoff 2011-03-07 12:49:00 +11:00
parent 7bbf2ded95
commit 1853e0eab3
5 changed files with 136 additions and 30 deletions

View File

@ -86,7 +86,7 @@ public:
: basic_io_object<SerialPortService>(io_service)
{
asio::error_code ec;
this->service.open(this->implementation, device, ec);
this->get_service().open(this->get_implementation(), device, ec);
asio::detail::throw_error(ec, "open");
}
@ -106,7 +106,7 @@ public:
: basic_io_object<SerialPortService>(io_service)
{
asio::error_code ec;
this->service.open(this->implementation, device, ec);
this->get_service().open(this->get_implementation(), device, ec);
asio::detail::throw_error(ec, "open");
}
@ -127,10 +127,48 @@ public:
: basic_io_object<SerialPortService>(io_service)
{
asio::error_code ec;
this->service.assign(this->implementation, native_serial_port, ec);
this->get_service().assign(this->get_implementation(),
native_serial_port, ec);
asio::detail::throw_error(ec, "assign");
}
#if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Move-construct a basic_serial_port from another.
/**
* 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 valid operations for the other object are:
* @li Using it as the target of a move assignment.
* @li Destruction.
*/
basic_serial_port(basic_serial_port&& other)
: basic_io_object<SerialPortService>(
ASIO_MOVE_CAST(basic_serial_port)(other))
{
}
/// Move-assign a basic_serial_port from another.
/**
* 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 valid operations for the other object are:
* @li Using it as the target of a move assignment.
* @li Destruction.
*/
basic_serial_port& operator=(basic_serial_port&& other)
{
basic_io_object<SerialPortService>::operator=(
ASIO_MOVE_CAST(basic_serial_port)(other));
return *this;
}
#endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Get a reference to the lowest layer.
/**
* This function returns a reference to the lowest layer in a stack of
@ -170,7 +208,7 @@ public:
void open(const std::string& device)
{
asio::error_code ec;
this->service.open(this->implementation, device, ec);
this->get_service().open(this->get_implementation(), device, ec);
asio::detail::throw_error(ec, "open");
}
@ -186,7 +224,7 @@ public:
asio::error_code open(const std::string& device,
asio::error_code& ec)
{
return this->service.open(this->implementation, device, ec);
return this->get_service().open(this->get_implementation(), device, ec);
}
/// Assign an existing native serial port to the serial port.
@ -200,7 +238,8 @@ public:
void assign(const native_handle_type& native_serial_port)
{
asio::error_code ec;
this->service.assign(this->implementation, native_serial_port, ec);
this->get_service().assign(this->get_implementation(),
native_serial_port, ec);
asio::detail::throw_error(ec, "assign");
}
@ -215,13 +254,14 @@ public:
asio::error_code assign(const native_handle_type& native_serial_port,
asio::error_code& ec)
{
return this->service.assign(this->implementation, native_serial_port, ec);
return this->get_service().assign(this->get_implementation(),
native_serial_port, ec);
}
/// Determine whether the serial port is open.
bool is_open() const
{
return this->service.is_open(this->implementation);
return this->get_service().is_open(this->get_implementation());
}
/// Close the serial port.
@ -235,7 +275,7 @@ public:
void close()
{
asio::error_code ec;
this->service.close(this->implementation, ec);
this->get_service().close(this->get_implementation(), ec);
asio::detail::throw_error(ec, "close");
}
@ -249,7 +289,7 @@ public:
*/
asio::error_code close(asio::error_code& ec)
{
return this->service.close(this->implementation, ec);
return this->get_service().close(this->get_implementation(), ec);
}
/// (Deprecated: Use native_handle().) Get the native serial port
@ -261,7 +301,7 @@ public:
*/
native_type native()
{
return this->service.native_handle(this->implementation);
return this->get_service().native_handle(this->get_implementation());
}
/// Get the native serial port representation.
@ -272,7 +312,7 @@ public:
*/
native_handle_type native_handle()
{
return this->service.native_handle(this->implementation);
return this->get_service().native_handle(this->get_implementation());
}
/// Cancel all asynchronous operations associated with the serial port.
@ -286,7 +326,7 @@ public:
void cancel()
{
asio::error_code ec;
this->service.cancel(this->implementation, ec);
this->get_service().cancel(this->get_implementation(), ec);
asio::detail::throw_error(ec, "cancel");
}
@ -300,7 +340,7 @@ public:
*/
asio::error_code cancel(asio::error_code& ec)
{
return this->service.cancel(this->implementation, ec);
return this->get_service().cancel(this->get_implementation(), ec);
}
/// Send a break sequence to the serial port.
@ -313,7 +353,7 @@ public:
void send_break()
{
asio::error_code ec;
this->service.send_break(this->implementation, ec);
this->get_service().send_break(this->get_implementation(), ec);
asio::detail::throw_error(ec, "send_break");
}
@ -326,7 +366,7 @@ public:
*/
asio::error_code send_break(asio::error_code& ec)
{
return this->service.send_break(this->implementation, ec);
return this->get_service().send_break(this->get_implementation(), ec);
}
/// Set an option on the serial port.
@ -348,7 +388,7 @@ public:
void set_option(const SettableSerialPortOption& option)
{
asio::error_code ec;
this->service.set_option(this->implementation, option, ec);
this->get_service().set_option(this->get_implementation(), option, ec);
asio::detail::throw_error(ec, "set_option");
}
@ -371,7 +411,8 @@ public:
asio::error_code set_option(const SettableSerialPortOption& option,
asio::error_code& ec)
{
return this->service.set_option(this->implementation, option, ec);
return this->get_service().set_option(
this->get_implementation(), option, ec);
}
/// Get an option from the serial port.
@ -394,7 +435,7 @@ public:
void get_option(GettableSerialPortOption& option)
{
asio::error_code ec;
this->service.get_option(this->implementation, option, ec);
this->get_service().get_option(this->get_implementation(), option, ec);
asio::detail::throw_error(ec, "get_option");
}
@ -418,7 +459,8 @@ public:
asio::error_code get_option(GettableSerialPortOption& option,
asio::error_code& ec)
{
return this->service.get_option(this->implementation, option, ec);
return this->get_service().get_option(
this->get_implementation(), option, ec);
}
/// Write some data to the serial port.
@ -452,7 +494,8 @@ public:
std::size_t write_some(const ConstBufferSequence& buffers)
{
asio::error_code ec;
std::size_t s = this->service.write_some(this->implementation, buffers, ec);
std::size_t s = this->get_service().write_some(
this->get_implementation(), buffers, ec);
asio::detail::throw_error(ec, "write_some");
return s;
}
@ -477,7 +520,8 @@ public:
std::size_t write_some(const ConstBufferSequence& buffers,
asio::error_code& ec)
{
return this->service.write_some(this->implementation, buffers, ec);
return this->get_service().write_some(
this->get_implementation(), buffers, ec);
}
/// Start an asynchronous write.
@ -523,8 +567,8 @@ public:
// not meet the documented type requirements for a WriteHandler.
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
this->service.async_write_some(this->implementation, buffers,
ASIO_MOVE_CAST(WriteHandler)(handler));
this->get_service().async_write_some(this->get_implementation(),
buffers, ASIO_MOVE_CAST(WriteHandler)(handler));
}
/// Read some data from the serial port.
@ -559,7 +603,8 @@ public:
std::size_t read_some(const MutableBufferSequence& buffers)
{
asio::error_code ec;
std::size_t s = this->service.read_some(this->implementation, buffers, ec);
std::size_t s = this->get_service().read_some(
this->get_implementation(), buffers, ec);
asio::detail::throw_error(ec, "read_some");
return s;
}
@ -585,7 +630,8 @@ public:
std::size_t read_some(const MutableBufferSequence& buffers,
asio::error_code& ec)
{
return this->service.read_some(this->implementation, buffers, ec);
return this->get_service().read_some(
this->get_implementation(), buffers, ec);
}
/// Start an asynchronous read.
@ -632,8 +678,8 @@ public:
// not meet the documented type requirements for a ReadHandler.
ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
this->service.async_read_some(this->implementation, buffers,
ASIO_MOVE_CAST(ReadHandler)(handler));
this->get_service().async_read_some(this->get_implementation(),
buffers, ASIO_MOVE_CAST(ReadHandler)(handler));
}
};

View File

@ -55,6 +55,22 @@ public:
descriptor_service_.construct(impl);
}
// Move-construct a new serial port implementation.
void move_construct(implementation_type& impl,
implementation_type& other_impl)
{
descriptor_service_.move_construct(impl, other_impl);
}
// Move-assign from another serial port implementation.
void move_assign(implementation_type& impl,
reactive_serial_port_service& other_service,
implementation_type& other_impl)
{
descriptor_service_.move_assign(impl,
other_service.descriptor_service_, other_impl);
}
// Destroy a serial port implementation.
void destroy(implementation_type& impl)
{

View File

@ -53,6 +53,22 @@ public:
handle_service_.construct(impl);
}
// Move-construct a new serial port implementation.
void move_construct(implementation_type& impl,
implementation_type& other_impl)
{
handle_service_.move_construct(impl, other_impl);
}
// Move-assign from another serial port implementation.
void move_assign(implementation_type& impl,
reactive_serial_port_service& other_service,
implementation_type& other_impl)
{
handle_service_.move_assign(impl,
other_service.handle_service_, other_impl);
}
// Destroy a serial port implementation.
void destroy(implementation_type& impl)
{

View File

@ -89,6 +89,23 @@ public:
service_impl_.construct(impl);
}
#if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Move-construct a new serial port implementation.
void move_construct(implementation_type& impl,
implementation_type& other_impl)
{
service_impl_.move_construct(impl, other_impl);
}
/// Move-assign from another serial port implementation.
void move_assign(implementation_type& impl,
serial_port_service& other_service,
implementation_type& other_impl)
{
service_impl_.move_assign(impl, other_service.service_impl_, other_impl);
}
#endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
/// Destroy a serial port implementation.
void destroy(implementation_type& impl)
{

View File

@ -57,6 +57,17 @@ void test()
serial_port::native_handle_type native_port1 = port1.native_handle();
serial_port port3(ios, native_port1);
#if defined(ASIO_HAS_MOVE)
serial_port port4(std::move(port3));
#endif // defined(ASIO_HAS_MOVE)
// basic_serial_port operators.
#if defined(ASIO_HAS_MOVE)
port1 = serial_port(ios);
port1 = std::move(port2);
#endif // defined(ASIO_HAS_MOVE)
// basic_io_object functions.
io_service& ios_ref = port1.get_io_service();
@ -67,8 +78,8 @@ void test()
serial_port::lowest_layer_type& lowest_layer = port1.lowest_layer();
(void)lowest_layer;
const serial_port& port4 = port1;
const serial_port::lowest_layer_type& lowest_layer2 = port4.lowest_layer();
const serial_port& port5 = port1;
const serial_port::lowest_layer_type& lowest_layer2 = port5.lowest_layer();
(void)lowest_layer2;
port1.open("null");