char_buffer.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_CORE_DETAIL_CHAR_BUFFER_HPP
  10. #define BOOST_BEAST_CORE_DETAIL_CHAR_BUFFER_HPP
  11. #include <boost/config.hpp>
  12. #include <cstddef>
  13. #include <cstring>
  14. #include <cstdint>
  15. namespace boost {
  16. namespace beast {
  17. namespace detail {
  18. template <std::size_t N>
  19. class char_buffer
  20. {
  21. public:
  22. bool try_push_back(char c)
  23. {
  24. if (size_ == N)
  25. return false;
  26. buf_[size_++] = c;
  27. return true;
  28. }
  29. bool try_append(char const* first, char const* last)
  30. {
  31. std::size_t const n = last - first;
  32. if (n > N - size_)
  33. return false;
  34. std::memmove(&buf_[size_], first, n);
  35. size_ += n;
  36. return true;
  37. }
  38. void clear() noexcept
  39. {
  40. size_ = 0;
  41. }
  42. char* data() noexcept
  43. {
  44. return buf_;
  45. }
  46. char const* data() const noexcept
  47. {
  48. return buf_;
  49. }
  50. std::size_t size() const noexcept
  51. {
  52. return size_;
  53. }
  54. bool empty() const noexcept
  55. {
  56. return size_ == 0;
  57. }
  58. private:
  59. std::size_t size_= 0;
  60. char buf_[N];
  61. };
  62. } // detail
  63. } // beast
  64. } // boost
  65. #endif