• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef BUFFER_H
26 #define BUFFER_H
27 
28 #include "nghttp2_config.h"
29 
30 #include <cstring>
31 #include <algorithm>
32 #include <array>
33 
34 namespace nghttp2 {
35 
36 template <size_t N> struct Buffer {
BufferBuffer37   Buffer() : pos(std::begin(buf)), last(pos) {}
38   // Returns the number of bytes to read.
rleftBuffer39   size_t rleft() const { return last - pos; }
40   // Returns the number of bytes this buffer can store.
wleftBuffer41   size_t wleft() const { return std::end(buf) - last; }
42   // Writes up to min(wleft(), |count|) bytes from buffer pointed by
43   // |src|.  Returns number of bytes written.
writeBuffer44   size_t write(const void *src, size_t count) {
45     count = std::min(count, wleft());
46     auto p = static_cast<const uint8_t *>(src);
47     last = std::copy_n(p, count, last);
48     return count;
49   }
writeBuffer50   size_t write(size_t count) {
51     count = std::min(count, wleft());
52     last += count;
53     return count;
54   }
55   // Drains min(rleft(), |count|) bytes from start of the buffer.
drainBuffer56   size_t drain(size_t count) {
57     count = std::min(count, rleft());
58     pos += count;
59     return count;
60   }
drain_resetBuffer61   size_t drain_reset(size_t count) {
62     count = std::min(count, rleft());
63     std::copy(pos + count, last, std::begin(buf));
64     last = std::begin(buf) + (last - (pos + count));
65     pos = std::begin(buf);
66     return count;
67   }
resetBuffer68   void reset() { pos = last = std::begin(buf); }
beginBuffer69   uint8_t *begin() { return std::begin(buf); }
70   uint8_t &operator[](size_t n) { return buf[n]; }
71   const uint8_t &operator[](size_t n) const { return buf[n]; }
72   std::array<uint8_t, N> buf;
73   uint8_t *pos, *last;
74 };
75 
76 } // namespace nghttp2
77 
78 #endif // BUFFER_H
79