buffer.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_CORE_DETAIL_BUFFER_HPP
  10. #define BOOST_BEAST_CORE_DETAIL_BUFFER_HPP
  11. #include <boost/beast/core/error.hpp>
  12. #include <boost/asio/buffer.hpp>
  13. #include <boost/optional.hpp>
  14. #include <stdexcept>
  15. namespace boost {
  16. namespace beast {
  17. namespace detail {
  18. template<
  19. class DynamicBuffer,
  20. class ErrorValue>
  21. auto
  22. dynamic_buffer_prepare_noexcept(
  23. DynamicBuffer& buffer,
  24. std::size_t size,
  25. error_code& ec,
  26. ErrorValue ev) ->
  27. boost::optional<typename
  28. DynamicBuffer::mutable_buffers_type>
  29. {
  30. if(buffer.max_size() - buffer.size() < size)
  31. {
  32. // length error
  33. BOOST_BEAST_ASSIGN_EC(ec, ev);
  34. return boost::none;
  35. }
  36. boost::optional<typename
  37. DynamicBuffer::mutable_buffers_type> result;
  38. result.emplace(buffer.prepare(size));
  39. ec = {};
  40. return result;
  41. }
  42. template<
  43. class DynamicBuffer,
  44. class ErrorValue>
  45. auto
  46. dynamic_buffer_prepare(
  47. DynamicBuffer& buffer,
  48. std::size_t size,
  49. error_code& ec,
  50. ErrorValue ev) ->
  51. boost::optional<typename
  52. DynamicBuffer::mutable_buffers_type>
  53. {
  54. #ifndef BOOST_NO_EXCEPTIONS
  55. try
  56. {
  57. boost::optional<typename
  58. DynamicBuffer::mutable_buffers_type> result;
  59. result.emplace(buffer.prepare(size));
  60. ec = {};
  61. return result;
  62. }
  63. catch(std::length_error const&)
  64. {
  65. BOOST_BEAST_ASSIGN_EC(ec, ev);
  66. }
  67. return boost::none;
  68. #else
  69. return dynamic_buffer_prepare_noexcept(
  70. buffer, size, ec, ev);
  71. #endif
  72. }
  73. } // detail
  74. } // beast
  75. } // boost
  76. #endif