temporary_buffer.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // Copyright (c) 2019 Damian Jarek(damian.jarek93@gmail.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_TEMPORARY_BUFFER_HPP
  10. #define BOOST_BEAST_DETAIL_TEMPORARY_BUFFER_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/string.hpp>
  13. #include <memory>
  14. namespace boost {
  15. namespace beast {
  16. namespace detail {
  17. struct temporary_buffer
  18. {
  19. temporary_buffer() = default;
  20. temporary_buffer(temporary_buffer const&) = delete;
  21. temporary_buffer& operator=(temporary_buffer const&) = delete;
  22. ~temporary_buffer() noexcept
  23. {
  24. deallocate(data_);
  25. }
  26. BOOST_BEAST_DECL
  27. void
  28. append(string_view s);
  29. BOOST_BEAST_DECL
  30. void
  31. append(string_view s1, string_view s2);
  32. string_view
  33. view() const noexcept
  34. {
  35. return {data_, size_};
  36. }
  37. bool
  38. empty() const noexcept
  39. {
  40. return size_ == 0;
  41. }
  42. private:
  43. BOOST_BEAST_DECL
  44. void
  45. unchecked_append(string_view s);
  46. BOOST_BEAST_DECL
  47. void
  48. grow(std::size_t n);
  49. void
  50. deallocate(char* data) noexcept
  51. {
  52. if (data != buffer_)
  53. delete[] data;
  54. }
  55. char buffer_[4096];
  56. char* data_ = buffer_;
  57. std::size_t capacity_ = sizeof(buffer_);
  58. std::size_t size_ = 0;
  59. };
  60. } // detail
  61. } // beast
  62. } // boost
  63. #ifdef BOOST_BEAST_HEADER_ONLY
  64. #include <boost/beast/core/detail/impl/temporary_buffer.ipp>
  65. #endif
  66. #endif