1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2025 Mohammad Nejati
3  
// Copyright (c) 2025 Mohammad Nejati
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/cppalliance/http
8  
// Official repository: https://github.com/cppalliance/http
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_HTTP_DETAIL_ARRAY_OF_BUFFERS_HPP
11  
#ifndef BOOST_HTTP_DETAIL_ARRAY_OF_BUFFERS_HPP
12  
#define BOOST_HTTP_DETAIL_ARRAY_OF_BUFFERS_HPP
12  
#define BOOST_HTTP_DETAIL_ARRAY_OF_BUFFERS_HPP
13  

13  

14  
#include <boost/assert.hpp>
14  
#include <boost/assert.hpp>
15  
#include <boost/capy/buffers.hpp>
15  
#include <boost/capy/buffers.hpp>
16  

16  

17  
#include <cstdint>
17  
#include <cstdint>
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace http {
20  
namespace http {
21  
namespace detail {
21  
namespace detail {
22  

22  

23  
class array_of_const_buffers
23  
class array_of_const_buffers
24  
{
24  
{
25  
public:
25  
public:
26  
    using value_type = capy::const_buffer;
26  
    using value_type = capy::const_buffer;
27  
    using iterator = value_type*;
27  
    using iterator = value_type*;
28  
    using const_iterator = iterator;
28  
    using const_iterator = iterator;
29  

29  

30  
    array_of_const_buffers() = default;
30  
    array_of_const_buffers() = default;
31  
    array_of_const_buffers(
31  
    array_of_const_buffers(
32  
        array_of_const_buffers const&) = default;
32  
        array_of_const_buffers const&) = default;
33  
    array_of_const_buffers&
33  
    array_of_const_buffers&
34  
    operator=(array_of_const_buffers const&) = default;
34  
    operator=(array_of_const_buffers const&) = default;
35  

35  

36  
    array_of_const_buffers(
36  
    array_of_const_buffers(
37  
        value_type* p,
37  
        value_type* p,
38  
        std::uint16_t n) noexcept;
38  
        std::uint16_t n) noexcept;
39  

39  

40  
    bool
40  
    bool
41  
    empty() const noexcept
41  
    empty() const noexcept
42  
    {
42  
    {
43  
        return size_ == 0;
43  
        return size_ == 0;
44  
    }
44  
    }
45  

45  

46  
    std::uint16_t
46  
    std::uint16_t
47  
    size() const noexcept
47  
    size() const noexcept
48  
    {
48  
    {
49  
        return size_;
49  
        return size_;
50  
    }
50  
    }
51  

51  

52  
    std::uint16_t
52  
    std::uint16_t
53  
    max_size() const noexcept
53  
    max_size() const noexcept
54  
    {
54  
    {
55  
        return cap_;
55  
        return cap_;
56  
    }
56  
    }
57  

57  

58  
    std::uint16_t
58  
    std::uint16_t
59  
    capacity() const noexcept
59  
    capacity() const noexcept
60  
    {
60  
    {
61  
        return cap_ - size_;
61  
        return cap_ - size_;
62  
    }
62  
    }
63  

63  

64  
    iterator
64  
    iterator
65  
    begin() const noexcept
65  
    begin() const noexcept
66  
    {
66  
    {
67  
        return base_ + pos_;
67  
        return base_ + pos_;
68  
    }
68  
    }
69  

69  

70  
    iterator
70  
    iterator
71  
    end() const noexcept
71  
    end() const noexcept
72  
    {
72  
    {
73  
        return base_ + pos_ + size_;
73  
        return base_ + pos_ + size_;
74  
    }
74  
    }
75  

75  

76  
    value_type&
76  
    value_type&
77  
    operator[](
77  
    operator[](
78  
        std::uint16_t i) const noexcept
78  
        std::uint16_t i) const noexcept
79  
    {
79  
    {
80  
        BOOST_ASSERT(i < cap_ - pos_);
80  
        BOOST_ASSERT(i < cap_ - pos_);
81  
        return base_[i + pos_];
81  
        return base_[i + pos_];
82  
    }
82  
    }
83  

83  

84  
    void
84  
    void
85  
    consume(std::size_t n);
85  
    consume(std::size_t n);
86  

86  

87  
    void
87  
    void
88  
    reset(std::uint16_t n) noexcept;
88  
    reset(std::uint16_t n) noexcept;
89  

89  

90  
    void
90  
    void
91  
    slide_to_front() noexcept;
91  
    slide_to_front() noexcept;
92  

92  

93  
    void append(value_type) noexcept;
93  
    void append(value_type) noexcept;
94  

94  

95  
private:
95  
private:
96  
    value_type* base_ = nullptr;
96  
    value_type* base_ = nullptr;
97  
    std::uint16_t cap_ = 0;
97  
    std::uint16_t cap_ = 0;
98  
    std::uint16_t pos_ = 0;
98  
    std::uint16_t pos_ = 0;
99  
    std::uint16_t size_ = 0;
99  
    std::uint16_t size_ = 0;
100  
};
100  
};
101  

101  

102  
} // detail
102  
} // detail
103  
} // http
103  
} // http
104  
} // boost
104  
} // boost
105  

105  

106  
#endif
106  
#endif