|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r70095 - in trunk/boost/asio: . detail
From: chris_at_[hidden]
Date: 2011-03-17 20:17:35
Author: chris_kohlhoff
Date: 2011-03-17 20:17:32 EDT (Thu, 17 Mar 2011)
New Revision: 70095
URL: http://svn.boost.org/trac/boost/changeset/70095
Log:
Add asio::buffer() overloads for std::array.
Text files modified:
trunk/boost/asio/buffer.hpp | 110 ++++++++++++++++++++++++++++++++++++++-
trunk/boost/asio/detail/array_fwd.hpp | 9 +++
trunk/boost/asio/detail/config.hpp | 20 ++++++
3 files changed, 132 insertions(+), 7 deletions(-)
Modified: trunk/boost/asio/buffer.hpp
==============================================================================
--- trunk/boost/asio/buffer.hpp (original)
+++ trunk/boost/asio/buffer.hpp 2011-03-17 20:17:32 EDT (Thu, 17 Mar 2011)
@@ -557,9 +557,9 @@
* passed to the socket's write function. A buffer created for modifiable
* memory also meets the requirements of the MutableBufferSequence concept.
*
- * An individual buffer may be created from a builtin array, std::vector or
- * boost::array of POD elements. This helps prevent buffer overruns by
- * automatically determining the size of the buffer:
+ * An individual buffer may be created from a builtin array, std::vector,
+ * std::array or boost::array of POD elements. This helps prevent buffer
+ * overruns by automatically determining the size of the buffer:
*
* @code char d1[128];
* size_t bytes_transferred = sock.receive(boost::asio::buffer(d1));
@@ -567,8 +567,11 @@
* std::vector<char> d2(128);
* bytes_transferred = sock.receive(boost::asio::buffer(d2));
*
- * boost::array<char, 128> d3;
- * bytes_transferred = sock.receive(boost::asio::buffer(d3)); @endcode
+ * std::array<char, 128> d3;
+ * bytes_transferred = sock.receive(boost::asio::buffer(d3));
+ *
+ * boost::array<char, 128> d4;
+ * bytes_transferred = sock.receive(boost::asio::buffer(d4)); @endcode
*
* In all three cases above, the buffers created are exactly 128 bytes long.
* Note that a vector is @e never automatically resized when creating or using
@@ -974,6 +977,103 @@
? data.size() * sizeof(PodType) : max_size_in_bytes));
}
+#if defined(BOOST_ASIO_HAS_STD_ARRAY)
+
+/// Create a new modifiable buffer that represents the given POD array.
+/**
+ * @returns A mutable_buffers_1 value equivalent to:
+ * @code mutable_buffers_1(
+ * data.data(),
+ * data.size() * sizeof(PodType)); @endcode
+ */
+template <typename PodType, std::size_t N>
+inline mutable_buffers_1 buffer(std::array<PodType, N>& data)
+{
+ return mutable_buffers_1(
+ mutable_buffer(data.data(), data.size() * sizeof(PodType)));
+}
+
+/// Create a new modifiable buffer that represents the given POD array.
+/**
+ * @returns A mutable_buffers_1 value equivalent to:
+ * @code mutable_buffers_1(
+ * data.data(),
+ * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
+ */
+template <typename PodType, std::size_t N>
+inline mutable_buffers_1 buffer(std::array<PodType, N>& data,
+ std::size_t max_size_in_bytes)
+{
+ return mutable_buffers_1(
+ mutable_buffer(data.data(),
+ data.size() * sizeof(PodType) < max_size_in_bytes
+ ? data.size() * sizeof(PodType) : max_size_in_bytes));
+}
+
+/// Create a new non-modifiable buffer that represents the given POD array.
+/**
+ * @returns A const_buffers_1 value equivalent to:
+ * @code const_buffers_1(
+ * data.data(),
+ * data.size() * sizeof(PodType)); @endcode
+ */
+template <typename PodType, std::size_t N>
+inline const_buffers_1 buffer(std::array<const PodType, N>& data)
+{
+ return const_buffers_1(
+ const_buffer(data.data(), data.size() * sizeof(PodType)));
+}
+
+/// Create a new non-modifiable buffer that represents the given POD array.
+/**
+ * @returns A const_buffers_1 value equivalent to:
+ * @code const_buffers_1(
+ * data.data(),
+ * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
+ */
+template <typename PodType, std::size_t N>
+inline const_buffers_1 buffer(std::array<const PodType, N>& data,
+ std::size_t max_size_in_bytes)
+{
+ return const_buffers_1(
+ const_buffer(data.data(),
+ data.size() * sizeof(PodType) < max_size_in_bytes
+ ? data.size() * sizeof(PodType) : max_size_in_bytes));
+}
+
+/// Create a new non-modifiable buffer that represents the given POD array.
+/**
+ * @returns A const_buffers_1 value equivalent to:
+ * @code const_buffers_1(
+ * data.data(),
+ * data.size() * sizeof(PodType)); @endcode
+ */
+template <typename PodType, std::size_t N>
+inline const_buffers_1 buffer(const std::array<PodType, N>& data)
+{
+ return const_buffers_1(
+ const_buffer(data.data(), data.size() * sizeof(PodType)));
+}
+
+/// Create a new non-modifiable buffer that represents the given POD array.
+/**
+ * @returns A const_buffers_1 value equivalent to:
+ * @code const_buffers_1(
+ * data.data(),
+ * min(data.size() * sizeof(PodType), max_size_in_bytes)); @endcode
+ */
+template <typename PodType, std::size_t N>
+inline const_buffers_1 buffer(const std::array<PodType, N>& data,
+ std::size_t max_size_in_bytes)
+{
+ return const_buffers_1(
+ const_buffer(data.data(),
+ data.size() * sizeof(PodType) < max_size_in_bytes
+ ? data.size() * sizeof(PodType) : max_size_in_bytes));
+}
+
+#endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
+
/// Create a new modifiable buffer that represents the given POD vector.
/**
* @returns A mutable_buffers_1 value equivalent to:
Modified: trunk/boost/asio/detail/array_fwd.hpp
==============================================================================
--- trunk/boost/asio/detail/array_fwd.hpp (original)
+++ trunk/boost/asio/detail/array_fwd.hpp 2011-03-17 20:17:32 EDT (Thu, 17 Mar 2011)
@@ -15,6 +15,8 @@
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
+#include <boost/asio/detail/config.hpp>
+
namespace boost {
template<class T, std::size_t N>
@@ -22,4 +24,11 @@
} // namespace boost
+// Standard library components can't be forward declared, so we'll have to
+// include the array header. Fortunately, it's fairly lightweight and doesn't
+// add significantly to the compile time.
+#if defined(BOOST_ASIO_HAS_STD_ARRAY)
+# include <array>
+#endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
+
#endif // BOOST_ASIO_DETAIL_ARRAY_FWD_HPP
Modified: trunk/boost/asio/detail/config.hpp
==============================================================================
--- trunk/boost/asio/detail/config.hpp (original)
+++ trunk/boost/asio/detail/config.hpp 2011-03-17 20:17:32 EDT (Thu, 17 Mar 2011)
@@ -72,14 +72,30 @@
// Standard library support for system errors.
#if !defined(BOOST_ASIO_DISABLE_STD_SYSTEM_ERROR)
# if defined(__GNUC__)
-# if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ > 4)
+# if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4)
# if defined(__GXX_EXPERIMENTAL_CXX0X__)
# define BOOST_ASIO_HAS_STD_SYSTEM_ERROR
# endif // defined(__GXX_EXPERIMENTAL_CXX0X__)
-# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ > 4)
+# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4)
# endif // defined(__GNUC__)
#endif // !defined(BOOST_ASIO_DISABLE_STD_SYSTEM_ERROR)
+// Standard library support for arrays.
+#if !defined(BOOST_ASIO_DISABLE_STD_ARRAY)
+# if defined(__GNUC__)
+# if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 4)
+# if defined(__GXX_EXPERIMENTAL_CXX0X__)
+# define BOOST_ASIO_HAS_STD_ARRAY
+# endif // defined(__GXX_EXPERIMENTAL_CXX0X__)
+# endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)) || (__GNUC__ > 4)
+# endif // defined(__GNUC__)
+# if defined(BOOST_MSVC)
+# if (_MSC_VER >= 1600)
+# define BOOST_ASIO_HAS_STD_ARRAY
+# endif // (_MSC_VER >= 1600)
+# endif // defined(BOOST_MSVC)
+#endif // !defined(BOOST_ASIO_DISABLE_STD_ARRAY)
+
// Windows: target OS version.
#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
# if !defined(_WIN32_WINNT) && !defined(_WIN32_WINDOWS)
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