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