11 #ifndef ASIO_BUFFER_HPP 12 #define ASIO_BUFFER_HPP 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 25 #if defined(ASIO_MSVC) 26 # if defined(_HAS_ITERATOR_DEBUGGING) && (_HAS_ITERATOR_DEBUGGING != 0) 27 # if !defined(ASIO_DISABLE_BUFFER_DEBUGGING) 28 # define ASIO_ENABLE_BUFFER_DEBUGGING 29 # endif // !defined(ASIO_DISABLE_BUFFER_DEBUGGING) 30 # endif // defined(_HAS_ITERATOR_DEBUGGING) 31 #endif // defined(ASIO_MSVC) 34 # if defined(_GLIBCXX_DEBUG) 35 # if !defined(ASIO_DISABLE_BUFFER_DEBUGGING) 36 # define ASIO_ENABLE_BUFFER_DEBUGGING 37 # endif // !defined(ASIO_DISABLE_BUFFER_DEBUGGING) 38 # endif // defined(_GLIBCXX_DEBUG) 39 #endif // defined(__GNUC__) 41 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 43 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 45 #if defined(ASIO_HAS_BOOST_WORKAROUND) 46 # include <boost/detail/workaround.hpp> 47 # if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) \ 48 || BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590)) 49 # define ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND 50 # endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) 52 #endif // defined(ASIO_HAS_BOOST_WORKAROUND) 54 #if defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) 56 #endif // defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) 108 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 110 asio::detail::function<
void()> debug_check)
113 debug_check_(debug_check)
117 const asio::detail::function<void()>& get_debug_check()
const 121 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 132 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 133 asio::detail::function<void()> debug_check_;
134 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 141 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 142 if (b.size_ && b.debug_check_)
144 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 186 const_iterator
end()
const 232 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
233 , debug_check_(b.get_debug_check())
238 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 240 asio::detail::function<
void()> debug_check)
243 debug_check_(debug_check)
247 const asio::detail::function<void()>& get_debug_check()
const 251 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 262 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 263 asio::detail::function<void()> debug_check_;
264 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 271 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 272 if (b.size_ && b.debug_check_)
274 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 316 const_iterator
end()
const 340 const_iterator
end()
const 385 template <
typename BufferSequence>
388 std::size_t total_buffer_size = 0;
390 typename BufferSequence::const_iterator iter = b.begin();
391 typename BufferSequence::const_iterator
end = b.end();
392 for (; iter !=
end; ++iter)
395 return total_buffer_size;
423 template <
typename Po
interToPodType>
430 template <
typename Po
interToPodType>
449 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
450 , b.get_debug_check()
451 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 466 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
467 , b.get_debug_check()
468 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 480 const char* new_data =
buffer_cast<
const char*>(
b) + start;
483 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
484 , b.get_debug_check()
485 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 497 const char* new_data =
buffer_cast<
const char*>(
b) + start;
500 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
501 , b.get_debug_check()
502 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 506 #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) 509 template <
typename Iterator>
510 class buffer_debug_check
513 buffer_debug_check(Iterator iter)
518 ~buffer_debug_check()
520 #if defined(ASIO_MSVC) && (ASIO_MSVC == 1400) 525 #endif // defined(ASIO_MSVC) && (ASIO_MSVC == 1400) 538 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 719 std::size_t max_size_in_bytes)
725 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
726 , b.get_debug_check()
727 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 748 std::size_t max_size_in_bytes)
754 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
755 , b.get_debug_check()
756 #endif // ASIO_ENABLE_BUFFER_DEBUGGING 774 std::size_t size_in_bytes)
786 template <
typename PodType, std::
size_t N>
799 template <
typename PodType, std::
size_t N>
801 std::size_t max_size_in_bytes)
805 N *
sizeof(PodType) < max_size_in_bytes
806 ? N *
sizeof(PodType) : max_size_in_bytes));
816 template <
typename PodType, std::
size_t N>
829 template <
typename PodType, std::
size_t N>
831 std::size_t max_size_in_bytes)
835 N *
sizeof(PodType) < max_size_in_bytes
836 ? N *
sizeof(PodType) : max_size_in_bytes));
839 #if defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) 855 template <
bool IsConst>
856 struct buffer_types_base;
859 struct buffer_types_base<false>
866 struct buffer_types_base<true>
872 template <
typename PodType>
874 :
public buffer_types_base<is_const<PodType>::value>
880 template <
typename PodType, std::
size_t N>
881 inline typename detail::buffer_types<PodType>::container_type
884 typedef typename asio::detail::buffer_types<PodType>::buffer_type
886 typedef typename asio::detail::buffer_types<PodType>::container_type
888 return container_type(
889 buffer_type(data.c_array(), data.size() *
sizeof(PodType)));
892 template <
typename PodType, std::
size_t N>
893 inline typename detail::buffer_types<PodType>::container_type
896 typedef typename asio::detail::buffer_types<PodType>::buffer_type
898 typedef typename asio::detail::buffer_types<PodType>::container_type
900 return container_type(
901 buffer_type(data.c_array(),
902 data.size() *
sizeof(PodType) < max_size_in_bytes
903 ? data.size() *
sizeof(PodType) : max_size_in_bytes));
906 #else // defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) 915 template <
typename PodType, std::
size_t N>
929 template <
typename PodType, std::
size_t N>
931 std::size_t max_size_in_bytes)
935 data.size() *
sizeof(PodType) < max_size_in_bytes
936 ? data.size() *
sizeof(PodType) : max_size_in_bytes));
946 template <
typename PodType, std::
size_t N>
950 const_buffer(data.data(), data.size() *
sizeof(PodType)));
960 template <
typename PodType, std::
size_t N>
962 std::size_t max_size_in_bytes)
966 data.size() *
sizeof(PodType) < max_size_in_bytes
967 ? data.size() *
sizeof(PodType) : max_size_in_bytes));
970 #endif // defined(ASIO_ENABLE_ARRAY_BUFFER_WORKAROUND) 979 template <
typename PodType, std::
size_t N>
983 const_buffer(data.data(), data.size() *
sizeof(PodType)));
993 template <
typename PodType, std::
size_t N>
995 std::size_t max_size_in_bytes)
999 data.size() *
sizeof(PodType) < max_size_in_bytes
1000 ? data.size() *
sizeof(PodType) : max_size_in_bytes));
1003 #if defined(ASIO_HAS_STD_ARRAY) || defined(GENERATING_DOCUMENTATION) 1012 template <
typename PodType, std::
size_t N>
1026 template <
typename PodType, std::
size_t N>
1028 std::size_t max_size_in_bytes)
1032 data.size() *
sizeof(PodType) < max_size_in_bytes
1033 ? data.size() *
sizeof(PodType) : max_size_in_bytes));
1043 template <
typename PodType, std::
size_t N>
1047 const_buffer(data.data(), data.size() *
sizeof(PodType)));
1057 template <
typename PodType, std::
size_t N>
1059 std::size_t max_size_in_bytes)
1063 data.size() *
sizeof(PodType) < max_size_in_bytes
1064 ? data.size() *
sizeof(PodType) : max_size_in_bytes));
1074 template <
typename PodType, std::
size_t N>
1078 const_buffer(data.data(), data.size() *
sizeof(PodType)));
1088 template <
typename PodType, std::
size_t N>
1090 std::size_t max_size_in_bytes)
1094 data.size() *
sizeof(PodType) < max_size_in_bytes
1095 ? data.size() *
sizeof(PodType) : max_size_in_bytes));
1098 #endif // defined(ASIO_HAS_STD_ARRAY) || defined(GENERATING_DOCUMENTATION) 1110 template <
typename PodType,
typename Allocator>
1114 mutable_buffer(data.size() ? &data[0] : 0, data.size() *
sizeof(PodType)
1115 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
1116 , detail::buffer_debug_check<
1117 typename std::vector<PodType, Allocator>::iterator
1133 template <
typename PodType,
typename Allocator>
1135 std::size_t max_size_in_bytes)
1139 data.size() *
sizeof(PodType) < max_size_in_bytes
1140 ? data.size() *
sizeof(PodType) : max_size_in_bytes
1141 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
1142 , detail::buffer_debug_check<
1143 typename std::vector<PodType, Allocator>::iterator
1159 template <
typename PodType,
typename Allocator>
1161 const std::vector<PodType, Allocator>& data)
1164 const_buffer(data.size() ? &data[0] : 0, data.size() *
sizeof(PodType)
1165 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
1166 , detail::buffer_debug_check<
1167 typename std::vector<PodType, Allocator>::const_iterator
1183 template <
typename PodType,
typename Allocator>
1185 const std::vector<PodType, Allocator>& data, std::size_t max_size_in_bytes)
1189 data.size() *
sizeof(PodType) < max_size_in_bytes
1190 ? data.size() *
sizeof(PodType) : max_size_in_bytes
1191 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
1192 , detail::buffer_debug_check<
1193 typename std::vector<PodType, Allocator>::const_iterator
1206 template <
typename Elem,
typename Traits,
typename Allocator>
1208 const std::basic_string<Elem, Traits, Allocator>& data)
1211 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
1212 , detail::buffer_debug_check<
1213 typename std::basic_string<Elem, Traits, Allocator>::const_iterator
1229 template <
typename Elem,
typename Traits,
typename Allocator>
1231 const std::basic_string<Elem, Traits, Allocator>& data,
1232 std::size_t max_size_in_bytes)
1236 data.size() *
sizeof(Elem) < max_size_in_bytes
1237 ? data.size() *
sizeof(Elem) : max_size_in_bytes
1238 #
if defined(ASIO_ENABLE_BUFFER_DEBUGGING)
1239 , detail::buffer_debug_check<
1240 typename std::basic_string<Elem, Traits, Allocator>::const_iterator
1298 using namespace std;
1301 std::size_t n = target_size < source_size ? target_size : source_size;
1302 memcpy(buffer_cast<void*>(target), buffer_cast<const void*>(source), n);
1328 return buffer_copy(target, static_cast<const const_buffer&>(source));
1402 template <
typename ConstBufferSequence>
1404 const ConstBufferSequence& source)
1406 std::size_t total_bytes_copied = 0;
1408 typename ConstBufferSequence::const_iterator source_iter = source.begin();
1409 typename ConstBufferSequence::const_iterator source_end = source.end();
1412 buffer_size(target_buffer) && source_iter != source_end; ++source_iter)
1415 std::size_t bytes_copied =
buffer_copy(target_buffer, source_buffer);
1416 total_bytes_copied += bytes_copied;
1417 target_buffer = target_buffer + bytes_copied;
1420 return total_bytes_copied;
1445 return buffer_copy(static_cast<const mutable_buffer&>(target), source);
1470 return buffer_copy(static_cast<const mutable_buffer&>(target),
1471 static_cast<const const_buffer&>(source));
1497 return buffer_copy(static_cast<const mutable_buffer&>(target),
1524 return buffer_copy(static_cast<const mutable_buffer&>(target),
1547 template <
typename ConstBufferSequence>
1549 const ConstBufferSequence& source)
1551 return buffer_copy(static_cast<const mutable_buffer&>(target), source);
1573 template <
typename MutableBufferSequence>
1577 std::size_t total_bytes_copied = 0;
1579 typename MutableBufferSequence::const_iterator target_iter = target.begin();
1580 typename MutableBufferSequence::const_iterator target_end = target.end();
1583 buffer_size(source_buffer) && target_iter != target_end; ++target_iter)
1586 std::size_t bytes_copied =
buffer_copy(target_buffer, source_buffer);
1587 total_bytes_copied += bytes_copied;
1588 source_buffer = source_buffer + bytes_copied;
1591 return total_bytes_copied;
1613 template <
typename MutableBufferSequence>
1617 return buffer_copy(target, static_cast<const const_buffer&>(source));
1640 template <
typename MutableBufferSequence>
1667 template <
typename MutableBufferSequence>
1693 template <
typename MutableBufferSequence,
typename ConstBufferSequence>
1695 const ConstBufferSequence& source)
1697 std::size_t total_bytes_copied = 0;
1699 typename MutableBufferSequence::const_iterator target_iter = target.begin();
1700 typename MutableBufferSequence::const_iterator target_end = target.end();
1701 std::size_t target_buffer_offset = 0;
1703 typename ConstBufferSequence::const_iterator source_iter = source.begin();
1704 typename ConstBufferSequence::const_iterator source_end = source.end();
1705 std::size_t source_buffer_offset = 0;
1707 while (target_iter != target_end && source_iter != source_end)
1715 std::size_t bytes_copied =
buffer_copy(target_buffer, source_buffer);
1716 total_bytes_copied += bytes_copied;
1721 target_buffer_offset = 0;
1724 target_buffer_offset += bytes_copied;
1729 source_buffer_offset = 0;
1732 source_buffer_offset += bytes_copied;
1735 return total_bytes_copied;
1762 const const_buffer& source, std::size_t max_bytes_to_copy)
1880 template <
typename ConstBufferSequence>
1882 const ConstBufferSequence& source, std::size_t max_bytes_to_copy)
1911 const const_buffer& source, std::size_t max_bytes_to_copy)
2029 template <
typename ConstBufferSequence>
2031 const ConstBufferSequence& source, std::size_t max_bytes_to_copy)
2060 template <
typename MutableBufferSequence>
2062 const const_buffer& source, std::size_t max_bytes_to_copy)
2091 template <
typename MutableBufferSequence>
2123 template <
typename MutableBufferSequence>
2155 template <
typename MutableBufferSequence>
2186 template <
typename MutableBufferSequence,
typename ConstBufferSequence>
2188 const ConstBufferSequence& source, std::size_t max_bytes_to_copy)
2190 std::size_t total_bytes_copied = 0;
2192 typename MutableBufferSequence::const_iterator target_iter = target.begin();
2193 typename MutableBufferSequence::const_iterator target_end = target.end();
2194 std::size_t target_buffer_offset = 0;
2196 typename ConstBufferSequence::const_iterator source_iter = source.begin();
2197 typename ConstBufferSequence::const_iterator source_end = source.end();
2198 std::size_t source_buffer_offset = 0;
2200 while (total_bytes_copied != max_bytes_to_copy
2201 && target_iter != target_end && source_iter != source_end)
2209 std::size_t bytes_copied =
buffer_copy(target_buffer,
2210 source_buffer, max_bytes_to_copy - total_bytes_copied);
2211 total_bytes_copied += bytes_copied;
2216 target_buffer_offset = 0;
2219 target_buffer_offset += bytes_copied;
2224 source_buffer_offset = 0;
2227 source_buffer_offset += bytes_copied;
2230 return total_bytes_copied;
2239 #endif // ASIO_BUFFER_HPP mutable_buffer(void *data, std::size_t size)
Construct a buffer to represent a given memory range.
const_iterator end() const
Get a random-access iterator for one past the last element.
mutable_buffers_1(const mutable_buffer &b)
Construct to represent a single modifiable buffer.
std::size_t buffer_size_helper(const mutable_buffer &)
mutable_buffer operator+(std::size_t start, const mutable_buffer &b)
Create a new modifiable buffer that is offset from the start of another.
Holds a buffer that cannot be modified.
SocketService Iterator begin
mutable_buffer value_type
The type for each element in the list of buffers.
const_buffer()
Construct an empty buffer.
mutable_buffers_1 buffer(const mutable_buffer &b)
Create a new modifiable buffer from an existing buffer.
const_buffer value_type
The type for each element in the list of buffers.
mutable_buffer()
Construct an empty buffer.
const_iterator begin() const
Get a random-access iterator to the first element.
const_buffer operator+(std::size_t start, const const_buffer &b)
Create a new non-modifiable buffer that is offset from the start of another.
void * buffer_cast_helper(const mutable_buffer &)
mutable_buffer operator+(const mutable_buffer &b, std::size_t start)
Create a new modifiable buffer that is offset from the start of another.
asio::basic_streambuf< Allocator > & b
const mutable_buffer * const_iterator
A random-access iterator type that may be used to read elements.
std::size_t buffer_size(const mutable_buffer &b)
Get the number of bytes in a modifiable buffer.
PointerToPodType buffer_cast(const mutable_buffer &b)
Cast a non-modifiable buffer to a specified pointer to POD type.
const_iterator begin() const
Get a random-access iterator to the first element.
mutable_buffers_1(void *data, std::size_t size)
Construct to represent a given memory range.
const_buffers_1(const void *data, std::size_t size)
Construct to represent a given memory range.
const_iterator begin() const
Get a random-access iterator to the first element.
Holds a buffer that can be modified.
const_iterator end() const
Get a random-access iterator for one past the last element.
const_buffer(const mutable_buffer &b)
Construct a non-modifiable buffer from a modifiable one.
mutable_buffer value_type
The type for each element in the list of buffers.
const_buffer(const void *data, std::size_t size)
Construct a buffer to represent a given memory range.
SocketService Iterator Iterator end
const mutable_buffer * const_iterator
A random-access iterator type that may be used to read elements.
const const_buffer * const_iterator
A random-access iterator type that may be used to read elements.
const_iterator end() const
Get a random-access iterator for one past the last element.
std::size_t buffer_copy(const mutable_buffer &target, const const_buffer &source)
Copies bytes from a source buffer to a target buffer.
const_buffers_1(const const_buffer &b)
Construct to represent a single non-modifiable buffer.
const_buffer operator+(const const_buffer &b, std::size_t start)
Create a new non-modifiable buffer that is offset from the start of another.