flat_static_buffer.ipp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_IMPL_FLAT_STATIC_BUFFER_IPP
  10. #define BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP
  11. #include <boost/beast/core/flat_static_buffer.hpp>
  12. #include <boost/throw_exception.hpp>
  13. #include <algorithm>
  14. #include <cstring>
  15. #include <iterator>
  16. #include <memory>
  17. #include <stdexcept>
  18. namespace boost {
  19. namespace beast {
  20. /* Layout:
  21. begin_ in_ out_ last_ end_
  22. |<------->|<---------->|<---------->|<------->|
  23. | readable | writable |
  24. */
  25. void
  26. flat_static_buffer_base::
  27. clear() noexcept
  28. {
  29. in_ = begin_;
  30. out_ = begin_;
  31. last_ = begin_;
  32. }
  33. auto
  34. flat_static_buffer_base::
  35. prepare(std::size_t n) ->
  36. mutable_buffers_type
  37. {
  38. if(n <= dist(out_, end_))
  39. {
  40. last_ = out_ + n;
  41. return {out_, n};
  42. }
  43. auto const len = size();
  44. if(n > capacity() - len)
  45. BOOST_THROW_EXCEPTION(std::length_error{
  46. "buffer overflow"});
  47. if(len > 0)
  48. std::memmove(begin_, in_, len);
  49. in_ = begin_;
  50. out_ = in_ + len;
  51. last_ = out_ + n;
  52. return {out_, n};
  53. }
  54. void
  55. flat_static_buffer_base::
  56. consume(std::size_t n) noexcept
  57. {
  58. if(n >= size())
  59. {
  60. in_ = begin_;
  61. out_ = in_;
  62. return;
  63. }
  64. in_ += n;
  65. }
  66. void
  67. flat_static_buffer_base::
  68. reset(void* p, std::size_t n) noexcept
  69. {
  70. begin_ = static_cast<char*>(p);
  71. in_ = begin_;
  72. out_ = begin_;
  73. last_ = begin_;
  74. end_ = begin_ + n;
  75. }
  76. } // beast
  77. } // boost
  78. #endif