buffer.hpp 1.8 KB

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