Prefer to use std::array when it is available.

This commit is contained in:
Christopher Kohlhoff 2011-03-18 15:37:30 +11:00
parent 272435f775
commit 957ccd6177
9 changed files with 95 additions and 14 deletions

View File

@ -20,7 +20,6 @@
#if !defined(BOOST_NO_IOSTREAM)
#include <streambuf>
#include <boost/array.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/repetition/enum_binary_params.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
@ -28,6 +27,7 @@
#include <boost/utility/base_from_member.hpp>
#include "asio/basic_socket.hpp"
#include "asio/deadline_timer_service.hpp"
#include "asio/detail/array.hpp"
#include "asio/detail/throw_error.hpp"
#include "asio/io_service.hpp"
#include "asio/stream_socket_service.hpp"
@ -505,8 +505,8 @@ private:
enum { putback_max = 8 };
enum { buffer_size = 512 };
boost::array<char, buffer_size> get_buffer_;
boost::array<char, buffer_size> put_buffer_;
asio::detail::array<char, buffer_size> get_buffer_;
asio::detail::array<char, buffer_size> put_buffer_;
bool unbuffered_;
asio::error_code ec_;
std::size_t bytes_transferred_;

View File

@ -0,0 +1,38 @@
//
// detail/array.hpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2011 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_ARRAY_HPP
#define ASIO_DETAIL_ARRAY_HPP
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
#include "asio/detail/config.hpp"
#if defined(ASIO_HAS_STD_ARRAY)
# include <array>
#else // defined(ASIO_HAS_STD_ARRAY)
# include <boost/array.hpp>
#endif // defined(ASIO_HAS_STD_ARRAY)
namespace asio {
namespace detail {
#if defined(ASIO_HAS_STD_ARRAY)
using std::array;
#else // defined(ASIO_HAS_STD_ARRAY)
using boost::array;
#endif // defined(ASIO_HAS_STD_ARRAY)
} // namespace detail
} // namespace asio
#endif // ASIO_DETAIL_ARRAY_HPP

View File

@ -17,7 +17,7 @@
#include "asio/detail/config.hpp"
#include <string>
#include <boost/array.hpp>
#include "asio/detail/array.hpp"
#include "asio/detail/socket_types.hpp"
#include "asio/detail/winsock_init.hpp"
#include "asio/error_code.hpp"
@ -44,7 +44,15 @@ class address_v4
{
public:
/// The type used to represent an address as an array of bytes.
typedef boost::array<unsigned char, 4> bytes_type;
/**
* @note This type is defined in terms of the C++0x template @c std::array
* when it is available. Otherwise, it uses @c boost:array.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef array<unsigned char, 4> bytes_type;
#else
typedef asio::detail::array<unsigned char, 4> bytes_type;
#endif
/// Default constructor.
address_v4()

View File

@ -17,7 +17,7 @@
#include "asio/detail/config.hpp"
#include <string>
#include <boost/array.hpp>
#include "asio/detail/array.hpp"
#include "asio/detail/socket_types.hpp"
#include "asio/detail/winsock_init.hpp"
#include "asio/error_code.hpp"
@ -45,7 +45,15 @@ class address_v6
{
public:
/// The type used to represent an address as an array of bytes.
typedef boost::array<unsigned char, 16> bytes_type;
/**
* @note This type is defined in terms of the C++0x template @c std::array
* when it is available. Otherwise, it uses @c boost:array.
*/
#if defined(GENERATING_DOCUMENTATION)
typedef array<unsigned char, 16> bytes_type;
#else
typedef asio::detail::array<unsigned char, 16> bytes_type;
#endif
/// Default constructor.
ASIO_DECL address_v6();

View File

@ -90,7 +90,7 @@ endpoint::endpoint(const asio::ip::address& addr,
data_.v6.sin6_flowinfo = 0;
asio::ip::address_v6 v6_addr = addr.to_v6();
asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
memcpy(data_.v6.sin6_addr.s6_addr, bytes.elems, 16);
memcpy(data_.v6.sin6_addr.s6_addr, bytes.data(), 16);
data_.v6.sin6_scope_id = v6_addr.scope_id();
}
}
@ -144,7 +144,11 @@ asio::ip::address endpoint::address() const
else
{
asio::ip::address_v6::bytes_type bytes;
#if defined(ASIO_HAS_STD_ARRAY)
memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
#else // defined(ASIO_HAS_STD_ARRAY)
memcpy(bytes.elems, data_.v6.sin6_addr.s6_addr, 16);
#endif // defined(ASIO_HAS_STD_ARRAY)
return asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
}
}

View File

@ -18,6 +18,7 @@
#include "asio/detail/config.hpp"
#include <cstddef>
#include <cstring>
#include <stdexcept>
#include <boost/throw_exception.hpp>
#include "asio/detail/socket_ops.hpp"
#include "asio/detail/socket_types.hpp"
@ -399,7 +400,7 @@ public:
using namespace std; // For memcpy.
asio::ip::address_v6 ipv6_address = multicast_address.to_v6();
asio::ip::address_v6::bytes_type bytes = ipv6_address.to_bytes();
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.elems, 16);
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
ipv6_value_.ipv6mr_interface = 0;
}
else
@ -437,7 +438,7 @@ public:
using namespace std; // For memcpy.
asio::ip::address_v6::bytes_type bytes =
multicast_address.to_bytes();
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.elems, 16);
memcpy(ipv6_value_.ipv6mr_multiaddr.s6_addr, bytes.data(), 16);
ipv6_value_.ipv6mr_interface = network_interface;
}

View File

@ -41,7 +41,7 @@ address_v4::address_v4(const address_v4::bytes_type& bytes)
#endif // UCHAR_MAX > 0xFF
using namespace std; // For memcpy.
memcpy(&addr_.s_addr, bytes.elems, 4);
memcpy(&addr_.s_addr, bytes.data(), 4);
}
address_v4::address_v4(unsigned long addr)
@ -61,7 +61,11 @@ address_v4::bytes_type address_v4::to_bytes() const
{
using namespace std; // For memcpy.
bytes_type bytes;
#if defined(ASIO_HAS_STD_ARRAY)
memcpy(bytes.data(), &addr_.s_addr, 4);
#else // defined(ASIO_HAS_STD_ARRAY)
memcpy(bytes.elems, &addr_.s_addr, 4);
#endif // defined(ASIO_HAS_STD_ARRAY)
return bytes;
}

View File

@ -52,7 +52,7 @@ address_v6::address_v6(const address_v6::bytes_type& bytes,
#endif // UCHAR_MAX > 0xFF
using namespace std; // For memcpy.
memcpy(addr_.s6_addr, bytes.elems, 16);
memcpy(addr_.s6_addr, bytes.data(), 16);
}
address_v6::address_v6(const address_v6& other)
@ -72,7 +72,11 @@ address_v6::bytes_type address_v6::to_bytes() const
{
using namespace std; // For memcpy.
bytes_type bytes;
#if defined(ASIO_HAS_STD_ARRAY)
memcpy(bytes.data(), addr_.s6_addr, 16);
#else // defined(ASIO_HAS_STD_ARRAY)
memcpy(bytes.elems, addr_.s6_addr, 16);
#endif // defined(ASIO_HAS_STD_ARRAY)
return bytes;
}

View File

@ -55806,7 +55806,14 @@ Obtain an address object that represents the broadcast address that corresponds
The type used to represent an address as an array of bytes.
typedef boost::array< unsigned char, 4 > bytes_type;
typedef array< unsigned char, 4 > bytes_type;
[heading Remarks]
This type is defined in terms of the C++0x template `std::array` when it is available. Otherwise, it uses `boost:array`.
@ -56588,7 +56595,14 @@ Obtain an address object that represents any address.
The type used to represent an address as an array of bytes.
typedef boost::array< unsigned char, 16 > bytes_type;
typedef array< unsigned char, 16 > bytes_type;
[heading Remarks]
This type is defined in terms of the C++0x template `std::array` when it is available. Otherwise, it uses `boost:array`.