buffers_pair.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_BUFFERS_PAIR_HPP
  10. #define BOOST_BEAST_DETAIL_BUFFERS_PAIR_HPP
  11. #include <boost/asio/buffer.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/config/workaround.hpp>
  14. #include <type_traits>
  15. namespace boost {
  16. namespace beast {
  17. namespace detail {
  18. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  19. # pragma warning (push)
  20. # pragma warning (disable: 4521) // multiple copy constructors specified
  21. # pragma warning (disable: 4522) // multiple assignment operators specified
  22. #endif
  23. template<bool isMutable>
  24. class buffers_pair
  25. {
  26. public:
  27. // VFALCO: This type is public otherwise
  28. // asio::buffers_iterator won't compile.
  29. using value_type = typename
  30. std::conditional<isMutable,
  31. net::mutable_buffer,
  32. net::const_buffer>::type;
  33. using const_iterator = value_type const*;
  34. buffers_pair() = default;
  35. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  36. buffers_pair(buffers_pair const& other)
  37. : buffers_pair(
  38. *other.begin(), *(other.begin() + 1))
  39. {
  40. }
  41. buffers_pair&
  42. operator=(buffers_pair const& other)
  43. {
  44. b_[0] = *other.begin();
  45. b_[1] = *(other.begin() + 1);
  46. return *this;
  47. }
  48. #else
  49. buffers_pair(buffers_pair const& other) = default;
  50. buffers_pair& operator=(buffers_pair const& other) = default;
  51. #endif
  52. template<
  53. bool isMutable_ = isMutable,
  54. class = typename std::enable_if<
  55. ! isMutable_>::type>
  56. buffers_pair(buffers_pair<true> const& other)
  57. : buffers_pair(
  58. *other.begin(), *(other.begin() + 1))
  59. {
  60. }
  61. template<
  62. bool isMutable_ = isMutable,
  63. class = typename std::enable_if<
  64. ! isMutable_>::type>
  65. buffers_pair&
  66. operator=(buffers_pair<true> const& other)
  67. {
  68. b_[0] = *other.begin();
  69. b_[1] = *(other.begin() + 1);
  70. return *this;
  71. }
  72. buffers_pair(value_type b0, value_type b1)
  73. : b_{b0, b1}
  74. {
  75. }
  76. const_iterator
  77. begin() const noexcept
  78. {
  79. return &b_[0];
  80. }
  81. const_iterator
  82. end() const noexcept
  83. {
  84. if(b_[1].size() > 0)
  85. return &b_[2];
  86. return &b_[1];
  87. }
  88. private:
  89. value_type b_[2];
  90. };
  91. #if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
  92. # pragma warning (pop)
  93. #endif
  94. } // detail
  95. } // beast
  96. } // boost
  97. #endif