summaryrefslogtreecommitdiff
path: root/boost/beast/core/impl/static_buffer.ipp
blob: d93a1e6911a89d9bcd69ae5e874b91d7aba9c47b (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot 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_IMPL_STATIC_BUFFER_IPP
#define BOOST_BEAST_IMPL_STATIC_BUFFER_IPP

#include <boost/beast/core/detail/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/throw_exception.hpp>
#include <algorithm>
#include <cstring>
#include <iterator>
#include <stdexcept>

namespace boost {
namespace beast {

inline
static_buffer_base::
static_buffer_base(void* p, std::size_t size)
    : begin_(reinterpret_cast<char*>(p))
    , capacity_(size)
{
}

inline
auto
static_buffer_base::
data() const ->
    const_buffers_type
{
    using boost::asio::mutable_buffer;
    const_buffers_type result;
    if(in_off_ + in_size_ <= capacity_)
    {
        result[0] = mutable_buffer{begin_ + in_off_, in_size_};
        result[1] = mutable_buffer{begin_, 0};
    }
    else
    {
        result[0] = mutable_buffer{begin_ + in_off_, capacity_ - in_off_};
        result[1] = mutable_buffer{begin_, in_size_ - (capacity_ - in_off_)};
    }
    return result;
}

inline
auto
static_buffer_base::
prepare(std::size_t size) ->
    mutable_buffers_type
{
    using boost::asio::mutable_buffer;
    if(size > capacity_ - in_size_)
        BOOST_THROW_EXCEPTION(std::length_error{
            "buffer overflow"});
    out_size_ = size;
    auto const out_off = (in_off_ + in_size_) % capacity_;
    mutable_buffers_type result;
    if(out_off + out_size_ <= capacity_ )
    {
        result[0] = mutable_buffer{begin_ + out_off, out_size_};
        result[1] = mutable_buffer{begin_, 0};
    }
    else
    {
        result[0] = mutable_buffer{begin_ + out_off, capacity_ - out_off};
        result[1] = mutable_buffer{begin_, out_size_ - (capacity_ - out_off)};
    }
    return result;
}

inline
void
static_buffer_base::
commit(std::size_t size)
{
    in_size_ += (std::min)(size, out_size_);
    out_size_ = 0;
}

inline
void
static_buffer_base::
consume(std::size_t size)
{
    if(size < in_size_)
    {
        in_off_ = (in_off_ + size) % capacity_;
        in_size_ -= size;
    }
    else
    {
        // rewind the offset, so the next call to prepare
        // can have a longer continguous segment. this helps
        // algorithms optimized for larger buffesr.
        in_off_ = 0;
        in_size_ = 0;
    }
}

inline
void
static_buffer_base::
reset(void* p, std::size_t size)
{
    begin_ = reinterpret_cast<char*>(p);
    capacity_ = size;
    in_off_ = 0;
    in_size_ = 0;
    out_size_ = 0;
}

//------------------------------------------------------------------------------

template<std::size_t N>
static_buffer<N>::
static_buffer(static_buffer const& other)
    : static_buffer_base(buf_, N)
{
    using boost::asio::buffer_copy;
    this->commit(buffer_copy(
        this->prepare(other.size()), other.data()));
}

template<std::size_t N>
auto
static_buffer<N>::
operator=(static_buffer const& other) ->
    static_buffer<N>&
{
    using boost::asio::buffer_copy;
    this->consume(this->size());
    this->commit(buffer_copy(
        this->prepare(other.size()), other.data()));
    return *this;
}

} // beast
} // boost

#endif