PageRenderTime 41ms CodeModel.GetById 23ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/src/contrib/boost/asio/detail/buffer_sequence_adapter.hpp

http://pythonocc.googlecode.com/
C++ Header | 254 lines | 203 code | 40 blank | 11 comment | 20 complexity | 8aa1f39f6c6450c24075b6594c8d645e MD5 | raw file
  1//
  2// buffer_sequence_adapter.hpp
  3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6//
  7// Distributed under the Boost Software License, Version 1.0. (See accompanying
  8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9//
 10
 11#ifndef BOOST_ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP
 12#define BOOST_ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP
 13
 14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
 15# pragma once
 16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 17
 18#include <boost/asio/detail/push_options.hpp>
 19
 20#include <boost/asio/buffer.hpp>
 21
 22namespace boost {
 23namespace asio {
 24namespace detail {
 25
 26class buffer_sequence_adapter_base
 27{
 28protected:
 29#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 30  typedef WSABUF native_buffer_type;
 31
 32  static void init_native_buffer(WSABUF& buf,
 33      const boost::asio::mutable_buffer& buffer)
 34  {
 35    buf.buf = boost::asio::buffer_cast<char*>(buffer);
 36    buf.len = boost::asio::buffer_size(buffer);
 37  }
 38
 39  static void init_native_buffer(WSABUF& buf,
 40      const boost::asio::const_buffer& buffer)
 41  {
 42    buf.buf = const_cast<char*>(boost::asio::buffer_cast<const char*>(buffer));
 43    buf.len = boost::asio::buffer_size(buffer);
 44  }
 45#else // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 46  typedef iovec native_buffer_type;
 47
 48  static void init_iov_base(void*& base, void* addr)
 49  {
 50    base = addr;
 51  }
 52
 53  template <typename T>
 54  static void init_iov_base(T& base, void* addr)
 55  {
 56    base = static_cast<T>(addr);
 57  }
 58
 59  static void init_native_buffer(iovec& iov,
 60      const boost::asio::mutable_buffer& buffer)
 61  {
 62    init_iov_base(iov.iov_base, boost::asio::buffer_cast<void*>(buffer));
 63    iov.iov_len = boost::asio::buffer_size(buffer);
 64  }
 65
 66  static void init_native_buffer(iovec& iov,
 67      const boost::asio::const_buffer& buffer)
 68  {
 69    init_iov_base(iov.iov_base, const_cast<void*>(
 70          boost::asio::buffer_cast<const void*>(buffer)));
 71    iov.iov_len = boost::asio::buffer_size(buffer);
 72  }
 73#endif // defined(BOOST_WINDOWS) || defined(__CYGWIN__)
 74};
 75
 76// Helper class to translate buffers into the native buffer representation.
 77template <typename Buffer, typename Buffers>
 78class buffer_sequence_adapter
 79  : buffer_sequence_adapter_base
 80{
 81public:
 82  explicit buffer_sequence_adapter(const Buffers& buffers)
 83    : count_(0), total_buffer_size_(0)
 84  {
 85    typename Buffers::const_iterator iter = buffers.begin();
 86    typename Buffers::const_iterator end = buffers.end();
 87    for (; iter != end && count_ < max_buffers; ++iter, ++count_)
 88    {
 89      Buffer buffer(*iter);
 90      init_native_buffer(buffers_[count_], buffer);
 91      total_buffer_size_ += boost::asio::buffer_size(buffer);
 92    }
 93  }
 94
 95  native_buffer_type* buffers()
 96  {
 97    return buffers_;
 98  }
 99
100  std::size_t count() const
101  {
102    return count_;
103  }
104
105  bool all_empty() const
106  {
107    return total_buffer_size_ == 0;
108  }
109
110  static bool all_empty(const Buffers& buffers)
111  {
112    typename Buffers::const_iterator iter = buffers.begin();
113    typename Buffers::const_iterator end = buffers.end();
114    std::size_t i = 0;
115    for (; iter != end && i < max_buffers; ++iter, ++i)
116      if (boost::asio::buffer_size(Buffer(*iter)) > 0)
117        return false;
118    return true;
119  }
120
121  static void validate(const Buffers& buffers)
122  {
123    typename Buffers::const_iterator iter = buffers.begin();
124    typename Buffers::const_iterator end = buffers.end();
125    for (; iter != end; ++iter)
126    {
127      Buffer buffer(*iter);
128      boost::asio::buffer_cast<const void*>(buffer);
129    }
130  }
131
132  static Buffer first(const Buffers& buffers)
133  {
134    typename Buffers::const_iterator iter = buffers.begin();
135    typename Buffers::const_iterator end = buffers.end();
136    for (; iter != end; ++iter)
137    {
138      Buffer buffer(*iter);
139      if (boost::asio::buffer_size(buffer) != 0)
140        return buffer;
141    }
142    return Buffer();
143  }
144
145private:
146  // The maximum number of buffers to support in a single operation.
147  enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len };
148
149  native_buffer_type buffers_[max_buffers];
150  std::size_t count_;
151  std::size_t total_buffer_size_;
152};
153
154template <typename Buffer>
155class buffer_sequence_adapter<Buffer, boost::asio::mutable_buffers_1>
156  : buffer_sequence_adapter_base
157{
158public:
159  explicit buffer_sequence_adapter(
160      const boost::asio::mutable_buffers_1& buffers)
161  {
162    init_native_buffer(buffer_, buffers);
163    total_buffer_size_ = boost::asio::buffer_size(buffers);
164  }
165
166  native_buffer_type* buffers()
167  {
168    return &buffer_;
169  }
170
171  std::size_t count() const
172  {
173    return 1;
174  }
175
176  bool all_empty() const
177  {
178    return total_buffer_size_ == 0;
179  }
180
181  static bool all_empty(const boost::asio::mutable_buffers_1& buffers)
182  {
183    return boost::asio::buffer_size(buffers) == 0;
184  }
185
186  static void validate(const boost::asio::mutable_buffers_1& buffers)
187  {
188    boost::asio::buffer_cast<const void*>(buffers);
189  }
190
191  static Buffer first(const boost::asio::mutable_buffers_1& buffers)
192  {
193    return Buffer(buffers);
194  }
195
196private:
197  native_buffer_type buffer_;
198  std::size_t total_buffer_size_;
199};
200
201template <typename Buffer>
202class buffer_sequence_adapter<Buffer, boost::asio::const_buffers_1>
203  : buffer_sequence_adapter_base
204{
205public:
206  explicit buffer_sequence_adapter(
207      const boost::asio::const_buffers_1& buffers)
208  {
209    init_native_buffer(buffer_, buffers);
210    total_buffer_size_ = boost::asio::buffer_size(buffers);
211  }
212
213  native_buffer_type* buffers()
214  {
215    return &buffer_;
216  }
217
218  std::size_t count() const
219  {
220    return 1;
221  }
222
223  bool all_empty() const
224  {
225    return total_buffer_size_ == 0;
226  }
227
228  static bool all_empty(const boost::asio::const_buffers_1& buffers)
229  {
230    return boost::asio::buffer_size(buffers) == 0;
231  }
232
233  static void validate(const boost::asio::const_buffers_1& buffers)
234  {
235    boost::asio::buffer_cast<const void*>(buffers);
236  }
237
238  static Buffer first(const boost::asio::const_buffers_1& buffers)
239  {
240    return Buffer(buffers);
241  }
242
243private:
244  native_buffer_type buffer_;
245  std::size_t total_buffer_size_;
246};
247
248} // namespace detail
249} // namespace asio
250} // namespace boost
251
252#include <boost/asio/detail/pop_options.hpp>
253
254#endif // BOOST_ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP