Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
9.0KB

  1. //
  2. // detail/buffer_sequence_adapter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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. #ifndef ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP
  11. #define ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/buffer.hpp"
  17. #include "asio/detail/array_fwd.hpp"
  18. #include "asio/detail/socket_types.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. namespace detail {
  22. class buffer_sequence_adapter_base
  23. {
  24. protected:
  25. #if defined(ASIO_WINDOWS_RUNTIME)
  26. // The maximum number of buffers to support in a single operation.
  27. enum { max_buffers = 1 };
  28. typedef Windows::Storage::Streams::IBuffer^ native_buffer_type;
  29. ASIO_DECL static void init_native_buffer(
  30. native_buffer_type& buf,
  31. const asio::mutable_buffer& buffer);
  32. ASIO_DECL static void init_native_buffer(
  33. native_buffer_type& buf,
  34. const asio::const_buffer& buffer);
  35. #elif defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  36. // The maximum number of buffers to support in a single operation.
  37. enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len };
  38. typedef WSABUF native_buffer_type;
  39. static void init_native_buffer(WSABUF& buf,
  40. const asio::mutable_buffer& buffer)
  41. {
  42. buf.buf = static_cast<char*>(buffer.data());
  43. buf.len = static_cast<ULONG>(buffer.size());
  44. }
  45. static void init_native_buffer(WSABUF& buf,
  46. const asio::const_buffer& buffer)
  47. {
  48. buf.buf = const_cast<char*>(static_cast<const char*>(buffer.data()));
  49. buf.len = static_cast<ULONG>(buffer.size());
  50. }
  51. #else // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  52. // The maximum number of buffers to support in a single operation.
  53. enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len };
  54. typedef iovec native_buffer_type;
  55. static void init_iov_base(void*& base, void* addr)
  56. {
  57. base = addr;
  58. }
  59. template <typename T>
  60. static void init_iov_base(T& base, void* addr)
  61. {
  62. base = static_cast<T>(addr);
  63. }
  64. static void init_native_buffer(iovec& iov,
  65. const asio::mutable_buffer& buffer)
  66. {
  67. init_iov_base(iov.iov_base, buffer.data());
  68. iov.iov_len = buffer.size();
  69. }
  70. static void init_native_buffer(iovec& iov,
  71. const asio::const_buffer& buffer)
  72. {
  73. init_iov_base(iov.iov_base, const_cast<void*>(buffer.data()));
  74. iov.iov_len = buffer.size();
  75. }
  76. #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  77. };
  78. // Helper class to translate buffers into the native buffer representation.
  79. template <typename Buffer, typename Buffers>
  80. class buffer_sequence_adapter
  81. : buffer_sequence_adapter_base
  82. {
  83. public:
  84. explicit buffer_sequence_adapter(const Buffers& buffer_sequence)
  85. : count_(0), total_buffer_size_(0)
  86. {
  87. typename Buffers::const_iterator iter = buffer_sequence.begin();
  88. typename Buffers::const_iterator end = buffer_sequence.end();
  89. for (; iter != end && count_ < max_buffers; ++iter, ++count_)
  90. {
  91. Buffer buffer(*iter);
  92. init_native_buffer(buffers_[count_], buffer);
  93. total_buffer_size_ += buffer.size();
  94. }
  95. }
  96. native_buffer_type* buffers()
  97. {
  98. return buffers_;
  99. }
  100. std::size_t count() const
  101. {
  102. return count_;
  103. }
  104. bool all_empty() const
  105. {
  106. return total_buffer_size_ == 0;
  107. }
  108. static bool all_empty(const Buffers& buffer_sequence)
  109. {
  110. typename Buffers::const_iterator iter = buffer_sequence.begin();
  111. typename Buffers::const_iterator end = buffer_sequence.end();
  112. std::size_t i = 0;
  113. for (; iter != end && i < max_buffers; ++iter, ++i)
  114. if (Buffer(*iter).size() > 0)
  115. return false;
  116. return true;
  117. }
  118. static void validate(const Buffers& buffer_sequence)
  119. {
  120. typename Buffers::const_iterator iter = buffer_sequence.begin();
  121. typename Buffers::const_iterator end = buffer_sequence.end();
  122. for (; iter != end; ++iter)
  123. {
  124. Buffer buffer(*iter);
  125. buffer.data();
  126. }
  127. }
  128. static Buffer first(const Buffers& buffer_sequence)
  129. {
  130. typename Buffers::const_iterator iter = buffer_sequence.begin();
  131. typename Buffers::const_iterator end = buffer_sequence.end();
  132. for (; iter != end; ++iter)
  133. {
  134. Buffer buffer(*iter);
  135. if (buffer.size() != 0)
  136. return buffer;
  137. }
  138. return Buffer();
  139. }
  140. private:
  141. native_buffer_type buffers_[max_buffers];
  142. std::size_t count_;
  143. std::size_t total_buffer_size_;
  144. };
  145. template <typename Buffer>
  146. class buffer_sequence_adapter<Buffer, asio::mutable_buffers_1>
  147. : buffer_sequence_adapter_base
  148. {
  149. public:
  150. explicit buffer_sequence_adapter(
  151. const asio::mutable_buffers_1& buffer_sequence)
  152. {
  153. init_native_buffer(buffer_, Buffer(buffer_sequence));
  154. total_buffer_size_ = buffer_sequence.size();
  155. }
  156. native_buffer_type* buffers()
  157. {
  158. return &buffer_;
  159. }
  160. std::size_t count() const
  161. {
  162. return 1;
  163. }
  164. bool all_empty() const
  165. {
  166. return total_buffer_size_ == 0;
  167. }
  168. static bool all_empty(const asio::mutable_buffers_1& buffer_sequence)
  169. {
  170. return buffer_sequence.size() == 0;
  171. }
  172. static void validate(const asio::mutable_buffers_1& buffer_sequence)
  173. {
  174. buffer_sequence.data();
  175. }
  176. static Buffer first(const asio::mutable_buffers_1& buffer_sequence)
  177. {
  178. return Buffer(buffer_sequence);
  179. }
  180. private:
  181. native_buffer_type buffer_;
  182. std::size_t total_buffer_size_;
  183. };
  184. template <typename Buffer>
  185. class buffer_sequence_adapter<Buffer, asio::const_buffers_1>
  186. : buffer_sequence_adapter_base
  187. {
  188. public:
  189. explicit buffer_sequence_adapter(
  190. const asio::const_buffers_1& buffer_sequence)
  191. {
  192. init_native_buffer(buffer_, Buffer(buffer_sequence));
  193. total_buffer_size_ = buffer_sequence.size();
  194. }
  195. native_buffer_type* buffers()
  196. {
  197. return &buffer_;
  198. }
  199. std::size_t count() const
  200. {
  201. return 1;
  202. }
  203. bool all_empty() const
  204. {
  205. return total_buffer_size_ == 0;
  206. }
  207. static bool all_empty(const asio::const_buffers_1& buffer_sequence)
  208. {
  209. return buffer_sequence.size() == 0;
  210. }
  211. static void validate(const asio::const_buffers_1& buffer_sequence)
  212. {
  213. buffer_sequence.data();
  214. }
  215. static Buffer first(const asio::const_buffers_1& buffer_sequence)
  216. {
  217. return Buffer(buffer_sequence);
  218. }
  219. private:
  220. native_buffer_type buffer_;
  221. std::size_t total_buffer_size_;
  222. };
  223. template <typename Buffer, typename Elem>
  224. class buffer_sequence_adapter<Buffer, boost::array<Elem, 2> >
  225. : buffer_sequence_adapter_base
  226. {
  227. public:
  228. explicit buffer_sequence_adapter(
  229. const boost::array<Elem, 2>& buffer_sequence)
  230. {
  231. init_native_buffer(buffers_[0], Buffer(buffer_sequence[0]));
  232. init_native_buffer(buffers_[1], Buffer(buffer_sequence[1]));
  233. total_buffer_size_ = buffer_sequence[0].size() + buffer_sequence[1].size();
  234. }
  235. native_buffer_type* buffers()
  236. {
  237. return buffers_;
  238. }
  239. std::size_t count() const
  240. {
  241. return 2;
  242. }
  243. bool all_empty() const
  244. {
  245. return total_buffer_size_ == 0;
  246. }
  247. static bool all_empty(const boost::array<Elem, 2>& buffer_sequence)
  248. {
  249. return buffer_sequence[0].size() == 0 && buffer_sequence[1].size() == 0;
  250. }
  251. static void validate(const boost::array<Elem, 2>& buffer_sequence)
  252. {
  253. buffer_sequence[0].data();
  254. buffer_sequence[1].data();
  255. }
  256. static Buffer first(const boost::array<Elem, 2>& buffer_sequence)
  257. {
  258. return Buffer(buffer_sequence[0].size() != 0
  259. ? buffer_sequence[0] : buffer_sequence[1]);
  260. }
  261. private:
  262. native_buffer_type buffers_[2];
  263. std::size_t total_buffer_size_;
  264. };
  265. #if defined(ASIO_HAS_STD_ARRAY)
  266. template <typename Buffer, typename Elem>
  267. class buffer_sequence_adapter<Buffer, std::array<Elem, 2> >
  268. : buffer_sequence_adapter_base
  269. {
  270. public:
  271. explicit buffer_sequence_adapter(
  272. const std::array<Elem, 2>& buffer_sequence)
  273. {
  274. init_native_buffer(buffers_[0], Buffer(buffer_sequence[0]));
  275. init_native_buffer(buffers_[1], Buffer(buffer_sequence[1]));
  276. total_buffer_size_ = buffer_sequence[0].size() + buffer_sequence[1].size();
  277. }
  278. native_buffer_type* buffers()
  279. {
  280. return buffers_;
  281. }
  282. std::size_t count() const
  283. {
  284. return 2;
  285. }
  286. bool all_empty() const
  287. {
  288. return total_buffer_size_ == 0;
  289. }
  290. static bool all_empty(const std::array<Elem, 2>& buffer_sequence)
  291. {
  292. return buffer_sequence[0].size() == 0 && buffer_sequence[1].size() == 0;
  293. }
  294. static void validate(const std::array<Elem, 2>& buffer_sequence)
  295. {
  296. buffer_sequence[0].data();
  297. buffer_sequence[1].data();
  298. }
  299. static Buffer first(const std::array<Elem, 2>& buffer_sequence)
  300. {
  301. return Buffer(buffer_sequence[0].size() != 0
  302. ? buffer_sequence[0] : buffer_sequence[1]);
  303. }
  304. private:
  305. native_buffer_type buffers_[2];
  306. std::size_t total_buffer_size_;
  307. };
  308. #endif // defined(ASIO_HAS_STD_ARRAY)
  309. } // namespace detail
  310. } // namespace asio
  311. #include "asio/detail/pop_options.hpp"
  312. #if defined(ASIO_HEADER_ONLY)
  313. # include "asio/detail/impl/buffer_sequence_adapter.ipp"
  314. #endif // defined(ASIO_HEADER_ONLY)
  315. #endif // ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP