liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Copyright (c) 2019 Damian Jarek(damian.jarek93@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
 
#ifndef BOOST_BEAST_CORE_DETAIL_CHAR_BUFFER_HPP
#define BOOST_BEAST_CORE_DETAIL_CHAR_BUFFER_HPP
 
#include <boost/config.hpp>
#include <cstddef>
#include <cstring>
#include <cstdint>
 
namespace boost {
namespace beast {
namespace detail {
 
template <std::size_t N>
class char_buffer
{
public:
    bool try_push_back(char c)
    {
        if (size_ == N)
            return false;
        buf_[size_++] = c;
        return true;
    }
 
    bool try_append(char const* first, char const* last)
    {
        std::size_t const n = last - first;
        if (n > N - size_)
            return false;
        std::memmove(&buf_[size_], first, n);
        size_ += n;
        return true;
    }
 
    void clear() noexcept
    {
        size_ = 0;
    }
 
    char* data() noexcept
    {
        return buf_;
    }
 
    char const* data() const noexcept
    {
        return buf_;
    }
 
    std::size_t size() const noexcept
    {
        return size_;
    }
 
    bool empty() const noexcept
    {
        return size_ == 0;
    }
 
private:
    std::size_t size_= 0;
    char buf_[N];
};
 
} // detail
} // beast
} // boost
 
#endif