Boost logo

Boost-Commit :

From: chris_at_[hidden]
Date: 2008-08-31 07:38:53


Author: chris_kohlhoff
Date: 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
New Revision: 48495
URL: http://svn.boost.org/trac/boost/changeset/48495

Log:
Add class to allow use of arbitrary Windows overlapped I/O operations.

Added:
   trunk/boost/asio/detail/win_iocp_overlapped_ptr.hpp (contents, props changed)
   trunk/boost/asio/windows/overlapped_ptr.hpp (contents, props changed)
   trunk/libs/asio/example/windows/
   trunk/libs/asio/example/windows/Jamfile (contents, props changed)
   trunk/libs/asio/example/windows/Jamfile.v2 (contents, props changed)
   trunk/libs/asio/example/windows/transmit_file.cpp (contents, props changed)
   trunk/libs/asio/test/windows/overlapped_ptr.cpp (contents, props changed)
Text files modified:
   trunk/boost/asio.hpp | 1 +
   trunk/boost/asio/detail/win_iocp_io_service_fwd.hpp | 1 +
   trunk/boost/asio/io_service.hpp | 1 +
   trunk/libs/asio/test/Jamfile | 1 +
   trunk/libs/asio/test/Jamfile.v2 | 2 ++
   5 files changed, 6 insertions(+), 0 deletions(-)

Modified: trunk/boost/asio.hpp
==============================================================================
--- trunk/boost/asio.hpp (original)
+++ trunk/boost/asio.hpp 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -89,6 +89,7 @@
 #include <boost/asio/windows/basic_handle.hpp>
 #include <boost/asio/windows/basic_random_access_handle.hpp>
 #include <boost/asio/windows/basic_stream_handle.hpp>
+#include <boost/asio/windows/overlapped_ptr.hpp>
 #include <boost/asio/windows/random_access_handle.hpp>
 #include <boost/asio/windows/random_access_handle_service.hpp>
 #include <boost/asio/windows/stream_handle.hpp>

Modified: trunk/boost/asio/detail/win_iocp_io_service_fwd.hpp
==============================================================================
--- trunk/boost/asio/detail/win_iocp_io_service_fwd.hpp (original)
+++ trunk/boost/asio/detail/win_iocp_io_service_fwd.hpp 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -37,6 +37,7 @@
 namespace detail {
 
 class win_iocp_io_service;
+class win_iocp_overlapped_ptr;
 
 } // namespace detail
 } // namespace asio

Added: trunk/boost/asio/detail/win_iocp_overlapped_ptr.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/asio/detail/win_iocp_overlapped_ptr.hpp 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -0,0 +1,208 @@
+//
+// win_iocp_overlapped_ptr.hpp
+// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2008 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 BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
+#define BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
+
+#include <boost/asio/detail/push_options.hpp>
+
+#include <boost/asio/detail/win_iocp_io_service_fwd.hpp>
+
+#if defined(BOOST_ASIO_HAS_IOCP)
+
+#include <boost/asio/detail/noncopyable.hpp>
+#include <boost/asio/detail/win_iocp_io_service.hpp>
+
+namespace boost {
+namespace asio {
+namespace detail {
+
+// Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
+class win_iocp_overlapped_ptr
+ : private noncopyable
+{
+public:
+ // Construct an empty win_iocp_overlapped_ptr.
+ win_iocp_overlapped_ptr()
+ : ptr_(0)
+ {
+ }
+
+ // Construct an win_iocp_overlapped_ptr to contain the specified handler.
+ template <typename Handler>
+ explicit win_iocp_overlapped_ptr(
+ boost::asio::io_service& io_service, Handler handler)
+ : ptr_(0)
+ {
+ this->reset(io_service, handler);
+ }
+
+ // Destructor automatically frees the OVERLAPPED object unless released.
+ ~win_iocp_overlapped_ptr()
+ {
+ reset();
+ }
+
+ // Reset to empty.
+ void reset()
+ {
+ if (ptr_)
+ {
+ ptr_->destroy();
+ ptr_ = 0;
+ }
+ }
+
+ // Reset to contain the specified handler, freeing any current OVERLAPPED
+ // object.
+ template <typename Handler>
+ void reset(boost::asio::io_service& io_service, Handler handler)
+ {
+ typedef overlapped_operation<Handler> value_type;
+ typedef handler_alloc_traits<Handler, value_type> alloc_traits;
+ raw_handler_ptr<alloc_traits> raw_ptr(handler);
+ handler_ptr<alloc_traits> ptr(raw_ptr, io_service.impl_, handler);
+ reset();
+ ptr_ = ptr.release();
+ }
+
+ // Get the contained OVERLAPPED object.
+ OVERLAPPED* get()
+ {
+ return ptr_;
+ }
+
+ // Get the contained OVERLAPPED object.
+ const OVERLAPPED* get() const
+ {
+ return ptr_;
+ }
+
+ // Release ownership of the OVERLAPPED object.
+ OVERLAPPED* release()
+ {
+ OVERLAPPED* tmp = ptr_;
+ ptr_ = 0;
+ return tmp;
+ }
+
+ // Post completion notification for overlapped operation. Releases ownership.
+ void complete(const boost::system::error_code& ec,
+ std::size_t bytes_transferred)
+ {
+ if (ptr_)
+ {
+ ptr_->io_service_.post_completion(ptr_, 0, 0);
+ ptr_ = 0;
+ }
+ }
+
+private:
+ struct overlapped_operation_base
+ : public win_iocp_io_service::operation
+ {
+ overlapped_operation_base(win_iocp_io_service& io_service,
+ invoke_func_type invoke_func, destroy_func_type destroy_func)
+ : win_iocp_io_service::operation(io_service, invoke_func, destroy_func),
+ io_service_(io_service)
+ {
+ io_service_.work_started();
+ }
+
+ ~overlapped_operation_base()
+ {
+ io_service_.work_finished();
+ }
+
+ win_iocp_io_service& io_service_;
+ boost::system::error_code ec_;
+ };
+
+ template <typename Handler>
+ struct overlapped_operation
+ : public overlapped_operation_base
+ {
+ overlapped_operation(win_iocp_io_service& io_service,
+ Handler handler)
+ : overlapped_operation_base(io_service,
+ &overlapped_operation<Handler>::do_completion_impl,
+ &overlapped_operation<Handler>::destroy_impl),
+ handler_(handler)
+ {
+ }
+
+ private:
+ // Prevent copying and assignment.
+ overlapped_operation(const overlapped_operation&);
+ void operator=(const overlapped_operation&);
+
+ static void do_completion_impl(operation* op,
+ DWORD last_error, size_t bytes_transferred)
+ {
+ // Take ownership of the operation object.
+ typedef overlapped_operation<Handler> op_type;
+ op_type* handler_op(static_cast<op_type*>(op));
+ typedef handler_alloc_traits<Handler, op_type> alloc_traits;
+ handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
+
+ // Make a copy of the handler and error_code so that the memory can be
+ // deallocated before the upcall is made.
+ Handler handler(handler_op->handler_);
+ boost::system::error_code ec(handler_op->ec_);
+ if (last_error)
+ ec = boost::system::error_code(last_error,
+ boost::asio::error::get_system_category());
+
+ // Free the memory associated with the handler.
+ ptr.reset();
+
+ // Make the upcall.
+ boost_asio_handler_invoke_helpers::invoke(
+ bind_handler(handler, ec, bytes_transferred), &handler);
+ }
+
+ static void destroy_impl(operation* op)
+ {
+ // Take ownership of the operation object.
+ typedef overlapped_operation<Handler> op_type;
+ op_type* handler_op(static_cast<op_type*>(op));
+ typedef handler_alloc_traits<Handler, op_type> alloc_traits;
+ handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);
+
+ // A sub-object of the handler may be the true owner of the memory
+ // associated with the handler. Consequently, a local copy of the handler
+ // is required to ensure that any owning sub-object remains valid until
+ // after we have deallocated the memory here.
+ Handler handler(handler_op->handler_);
+ (void)handler;
+
+ // Free the memory associated with the handler.
+ ptr.reset();
+ }
+
+ Handler handler_;
+ };
+
+ overlapped_operation_base* ptr_;
+};
+
+} // namespace detail
+} // namespace asio
+} // namespace boost
+
+#endif // defined(BOOST_ASIO_HAS_IOCP)
+
+#include <boost/asio/detail/pop_options.hpp>
+
+#endif // BOOST_ASIO_DETAIL_WIN_IOCP_OVERLAPPED_PTR_HPP

Modified: trunk/boost/asio/io_service.hpp
==============================================================================
--- trunk/boost/asio/io_service.hpp (original)
+++ trunk/boost/asio/io_service.hpp 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -135,6 +135,7 @@
   // The type of the platform-specific implementation.
 #if defined(BOOST_ASIO_HAS_IOCP)
   typedef detail::win_iocp_io_service impl_type;
+ friend class detail::win_iocp_overlapped_ptr;
 #elif defined(BOOST_ASIO_HAS_EPOLL)
   typedef detail::task_io_service<detail::epoll_reactor<false> > impl_type;
 #elif defined(BOOST_ASIO_HAS_KQUEUE)

Added: trunk/boost/asio/windows/overlapped_ptr.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/asio/windows/overlapped_ptr.hpp 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -0,0 +1,120 @@
+//
+// overlapped_ptr.hpp
+// ~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2008 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 BOOST_ASIO_WINDOWS_OVERLAPPED_PTR_HPP
+#define BOOST_ASIO_WINDOWS_OVERLAPPED_PTR_HPP
+
+#if defined(_MSC_VER) && (_MSC_VER >= 1200)
+# pragma once
+#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
+
+#include <boost/asio/detail/push_options.hpp>
+
+#include <boost/asio/io_service.hpp>
+#include <boost/asio/detail/noncopyable.hpp>
+#include <boost/asio/detail/win_iocp_overlapped_ptr.hpp>
+
+#if !defined(BOOST_ASIO_DISABLE_WINDOWS_OVERLAPPED_PTR)
+# if defined(BOOST_ASIO_HAS_IOCP)
+# define BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR 1
+# endif // defined(BOOST_ASIO_HAS_IOCP)
+#endif // !defined(BOOST_ASIO_DISABLE_WINDOWS_OVERLAPPED_PTR)
+
+#if defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR) \
+ || defined(GENERATING_DOCUMENTATION)
+
+namespace boost {
+namespace asio {
+namespace windows {
+
+/// Wraps a handler to create an OVERLAPPED object for use with overlapped I/O.
+/**
+ * A special-purpose smart pointer used to wrap an application handler so that
+ * it can be passed as the LPOVERLAPPED argument to overlapped I/O functions.
+ *
+ * @par Thread Safety
+ * @e Distinct @e objects: Safe._at_n
+ * @e Shared @e objects: Unsafe.
+ */
+class overlapped_ptr
+ : private noncopyable
+{
+public:
+ /// Construct an empty overlapped_ptr.
+ overlapped_ptr()
+ : impl_()
+ {
+ }
+
+ /// Construct an overlapped_ptr to contain the specified handler.
+ template <typename Handler>
+ explicit overlapped_ptr(boost::asio::io_service& io_service, Handler handler)
+ : impl_(io_service, handler)
+ {
+ }
+
+ /// Destructor automatically frees the OVERLAPPED object unless released.
+ ~overlapped_ptr()
+ {
+ }
+
+ /// Reset to empty.
+ void reset()
+ {
+ impl_.reset();
+ }
+
+ /// Reset to contain the specified handler, freeing any current OVERLAPPED
+ /// object.
+ template <typename Handler>
+ void reset(boost::asio::io_service& io_service, Handler handler)
+ {
+ impl_.reset(io_service, handler);
+ }
+
+ /// Get the contained OVERLAPPED object.
+ OVERLAPPED* get()
+ {
+ return impl_.get();
+ }
+
+ /// Get the contained OVERLAPPED object.
+ const OVERLAPPED* get() const
+ {
+ return impl_.get();
+ }
+
+ /// Release ownership of the OVERLAPPED object.
+ OVERLAPPED* release()
+ {
+ return impl_.release();
+ }
+
+ /// Post completion notification for overlapped operation. Releases ownership.
+ void complete(const boost::system::error_code& ec,
+ std::size_t bytes_transferred)
+ {
+ impl_.complete(ec, bytes_transferred);
+ }
+
+private:
+ detail::win_iocp_overlapped_ptr impl_;
+};
+
+} // namespace windows
+} // namespace asio
+} // namespace boost
+
+#endif // defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
+ // || defined(GENERATING_DOCUMENTATION)
+
+#include <boost/asio/detail/pop_options.hpp>
+
+#endif // BOOST_ASIO_WINDOWS_OVERLAPPED_PTR_HPP

Added: trunk/libs/asio/example/windows/Jamfile
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/windows/Jamfile 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -0,0 +1,33 @@
+#
+# Copyright (c) 2003-2008 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)
+#
+
+subproject libs/asio/example/windows ;
+
+project boost : $(BOOST_ROOT) ;
+
+if $(UNIX)
+{
+ switch $(JAMUNAME)
+ {
+ case SunOS* :
+ {
+ SOCKET_LIBS = <find-library>socket <find-library>nsl ;
+ }
+ }
+}
+
+exe transmit_file
+ : <lib>@boost/libs/system/build/boost_system
+ transmit_file.cpp
+ : <include>$(BOOST_ROOT)
+ <include>../../../..
+ <define>BOOST_ALL_NO_LIB=1
+ <threading>multi
+ <mingw><*><find-library>ws2_32
+ <mingw><*><find-library>mswsock
+ $(SOCKET_LIBS)
+ ;

Added: trunk/libs/asio/example/windows/Jamfile.v2
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/windows/Jamfile.v2 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -0,0 +1,38 @@
+#
+# Copyright (c) 2003-2008 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)
+#
+
+import os ;
+
+if [ os.name ] = SOLARIS
+{
+ lib socket ;
+ lib nsl ;
+}
+else if [ os.name ] = NT
+{
+ lib ws2_32 ;
+ lib mswsock ;
+}
+else if [ os.name ] = HPUX
+{
+ lib ipv6 ;
+}
+
+exe transmit_file
+ : transmit_file.cpp
+ /boost/system//boost_system
+ : <define>BOOST_ALL_NO_LIB=1
+ <threading>multi
+ <os>SOLARIS:<library>socket
+ <os>SOLARIS:<library>nsl
+ <os>NT:<define>_WIN32_WINNT=0x0501
+ <os>NT,<toolset>gcc:<library>ws2_32
+ <os>NT,<toolset>gcc:<library>mswsock
+ <os>NT,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS
+ <os>HPUX,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED
+ <os>HPUX:<library>ipv6
+ ;

Added: trunk/libs/asio/example/windows/transmit_file.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/example/windows/transmit_file.cpp 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -0,0 +1,170 @@
+//
+// transmit_file.cpp
+// ~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2008 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)
+//
+
+#include <ctime>
+#include <iostream>
+#include <string>
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/asio.hpp>
+
+#if defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
+
+using boost::asio::ip::tcp;
+using boost::asio::windows::overlapped_ptr;
+using boost::asio::windows::random_access_handle;
+
+// A wrapper for the TransmitFile overlapped I/O operation.
+template <typename Handler>
+void transmit_file(tcp::socket& socket,
+ random_access_handle& file, Handler handler)
+{
+ // Construct an OVERLAPPED-derived object to contain the handler.
+ overlapped_ptr overlapped(socket.get_io_service(), handler);
+
+ // Initiate the TransmitFile operation.
+ BOOL ok = ::TransmitFile(socket.native(),
+ file.native(), 0, 0, overlapped.get(), 0, 0);
+ DWORD last_error = ::GetLastError();
+
+ // Check if the operation completed immediately.
+ if (!ok && last_error != ERROR_IO_PENDING)
+ {
+ // The operation completed immediately, so a completion notification needs
+ // to be posted. When complete() is called, ownership of the OVERLAPPED-
+ // derived object passes to the io_service.
+ boost::system::error_code ec(last_error,
+ boost::asio::error::get_system_category());
+ overlapped.complete(ec, 0);
+ }
+ else
+ {
+ // The operation was successfully initiated, so ownership of the
+ // OVERLAPPED-derived object has passed to the io_service.
+ overlapped.release();
+ }
+}
+
+class connection
+ : public boost::enable_shared_from_this<connection>
+{
+public:
+ typedef boost::shared_ptr<connection> pointer;
+
+ static pointer create(boost::asio::io_service& io_service,
+ const std::string& filename)
+ {
+ return pointer(new connection(io_service, filename));
+ }
+
+ tcp::socket& socket()
+ {
+ return socket_;
+ }
+
+ void start()
+ {
+ boost::system::error_code ec;
+ file_.assign(::CreateFile(filename_.c_str(), GENERIC_READ, 0, 0,
+ OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0), ec);
+ if (file_.is_open())
+ {
+ transmit_file(socket_, file_,
+ boost::bind(&connection::handle_write, shared_from_this(),
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
+ }
+ }
+
+private:
+ connection(boost::asio::io_service& io_service, const std::string& filename)
+ : socket_(io_service),
+ filename_(filename),
+ file_(io_service)
+ {
+ }
+
+ void handle_write(const boost::system::error_code& /*error*/,
+ size_t /*bytes_transferred*/)
+ {
+ boost::system::error_code ignored_ec;
+ socket_.shutdown(tcp::socket::shutdown_both, ignored_ec);
+ }
+
+ tcp::socket socket_;
+ std::string filename_;
+ random_access_handle file_;
+};
+
+class server
+{
+public:
+ server(boost::asio::io_service& io_service,
+ unsigned short port, const std::string& filename)
+ : acceptor_(io_service, tcp::endpoint(tcp::v4(), port)),
+ filename_(filename)
+ {
+ start_accept();
+ }
+
+private:
+ void start_accept()
+ {
+ connection::pointer new_connection =
+ connection::create(acceptor_.io_service(), filename_);
+
+ acceptor_.async_accept(new_connection->socket(),
+ boost::bind(&server::handle_accept, this, new_connection,
+ boost::asio::placeholders::error));
+ }
+
+ void handle_accept(connection::pointer new_connection,
+ const boost::system::error_code& error)
+ {
+ if (!error)
+ {
+ new_connection->start();
+ start_accept();
+ }
+ }
+
+ tcp::acceptor acceptor_;
+ std::string filename_;
+};
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ if (argc != 3)
+ {
+ std::cerr << "Usage: transmit_file <port> <filename>\n";
+ return 1;
+ }
+
+ boost::asio::io_service io_service;
+
+ using namespace std; // For atoi.
+ server s(io_service, atoi(argv[1]), argv[2]);
+
+ io_service.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
+
+#else // defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
+# error Overlapped I/O not available on this platform
+#endif // defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)

Modified: trunk/libs/asio/test/Jamfile
==============================================================================
--- trunk/libs/asio/test/Jamfile (original)
+++ trunk/libs/asio/test/Jamfile 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -91,6 +91,7 @@
   [ run windows/basic_handle.cpp <template>asio_unit_test ]
   [ run windows/basic_random_access_handle.cpp <template>asio_unit_test ]
   [ run windows/basic_stream_handle.cpp <template>asio_unit_test ]
+ [ run windows/overlapped_ptr.cpp <template>asio_unit_test ]
   [ run windows/random_access_handle.cpp <template>asio_unit_test ]
   [ run windows/random_access_handle_service.cpp <template>asio_unit_test ]
   [ run windows/stream_handle.cpp <template>asio_unit_test ]

Modified: trunk/libs/asio/test/Jamfile.v2
==============================================================================
--- trunk/libs/asio/test/Jamfile.v2 (original)
+++ trunk/libs/asio/test/Jamfile.v2 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -162,6 +162,8 @@
   [ link windows/basic_random_access_handle.cpp : $(USE_SELECT) : windows_basic_random_access_handle_select ]
   [ link windows/basic_stream_handle.cpp : : windows_basic_stream_handle ]
   [ link windows/basic_stream_handle.cpp : $(USE_SELECT) : windows_basic_stream_handle_select ]
+ [ link windows/overlapped_ptr.cpp : : windows_overlapped_ptr ]
+ [ link windows/overlapped_ptr.cpp : $(USE_SELECT) : windows_overlapped_ptr_select ]
   [ link windows/random_access_handle.cpp : : windows_random_access_handle ]
   [ link windows/random_access_handle.cpp : $(USE_SELECT) : windows_random_access_handle_select ]
   [ link windows/random_access_handle_service.cpp : : windows_random_access_handle_service ]

Added: trunk/libs/asio/test/windows/overlapped_ptr.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/asio/test/windows/overlapped_ptr.cpp 2008-08-31 07:38:52 EDT (Sun, 31 Aug 2008)
@@ -0,0 +1,95 @@
+//
+// overlapped_ptr.cpp
+// ~~~~~~~~~~~~~~~~~~
+//
+// Copyright (c) 2003-2008 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)
+//
+
+// Disable autolinking for unit tests.
+#if !defined(BOOST_ALL_NO_LIB)
+#define BOOST_ALL_NO_LIB 1
+#endif // !defined(BOOST_ALL_NO_LIB)
+
+// Test that header file is self-contained.
+#include <boost/asio/windows/overlapped_ptr.hpp>
+
+#include <boost/asio.hpp>
+#include "../unit_test.hpp"
+
+//------------------------------------------------------------------------------
+
+// windows_overlapped_ptr_compile test
+// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+// The following test checks that all public member functions on the class
+// windows::overlapped_ptr compile and link correctly. Runtime failures are
+// ignored.
+
+namespace windows_overlapped_ptr_compile {
+
+void overlapped_handler_1(const boost::system::error_code&, std::size_t)
+{
+}
+
+struct overlapped_handler_2
+{
+ void operator()(const boost::system::error_code&, std::size_t)
+ {
+ }
+};
+
+void test()
+{
+#if defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
+ using namespace boost::asio;
+ namespace win = boost::asio::windows;
+
+ try
+ {
+ io_service ios;
+
+ // basic_overlapped_ptr constructors.
+
+ win::overlapped_ptr ptr1;
+
+ win::overlapped_ptr ptr2(ios, overlapped_handler_1);
+ win::overlapped_ptr ptr3(ios, overlapped_handler_2());
+
+ // overlapped_ptr functions.
+
+ ptr1.reset();
+
+ ptr2.reset(ios, overlapped_handler_1);
+ ptr3.reset(ios, overlapped_handler_2());
+
+ OVERLAPPED* ov1 = ptr1.get();
+ (void)ov1;
+
+ const win::overlapped_ptr& ptr4(ptr1);
+ const OVERLAPPED* ov2 = ptr4.get();
+ (void)ov2;
+
+ OVERLAPPED* ov3 = ptr1.release();
+ (void)ov3;
+
+ boost::system::error_code ec;
+ std::size_t bytes_transferred = 0;
+ ptr1.complete(ec, bytes_transferred);
+ }
+ catch (std::exception&)
+ {
+ }
+#endif // defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
+}
+
+} // namespace windows_overlapped_ptr_compile
+
+//------------------------------------------------------------------------------
+test_suite* init_unit_test_suite(int, char*[])
+{
+ test_suite* test = BOOST_TEST_SUITE("windows/overlapped_ptr");
+ test->add(BOOST_TEST_CASE(&windows_overlapped_ptr_compile::test));
+ return test;
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk