protozero: include/protozero/buffer_fixed.hpp Source File

protozero 1.8.2 Minimalistic protocol buffer decoder and encoder in C++.

Loading...
Searching...
No Matches
buffer_fixed.hpp
Go to the documentation of this file.
1#ifndef PROTOZERO_BUFFER_FIXED_HPP
2#define PROTOZERO_BUFFER_FIXED_HPP
3
4/*****************************************************************************
5
6protozero - Minimalistic protocol buffer decoder and encoder in C++.
7
8This file is from https://github.com/mapbox/protozero where you can find more
9documentation.
10
11*****************************************************************************/
12
18
19#include "buffer_tmpl.hpp"
20#include "config.hpp"
21
22#include <algorithm>
23#include <cstddef>
24#include <iterator>
25#include <stdexcept>
26
27namespace protozero {
28
34 class fixed_size_buffer_adaptor {
35
36 char* m_data;
37 std::size_t m_capacity;
38 std::size_t m_size = 0;
39
40public:
41
43
44 using size_type = std::size_t;
45
46 using value_type = char;
47 using reference = value_type&;
48 using const_reference = const value_type&;
49 using pointer = value_type*;
50 using const_pointer = const value_type*;
51
52 using iterator = pointer;
53 using const_iterator = const_pointer;
54
56
63 fixed_size_buffer_adaptor(char* data, std::size_t capacity) noexcept :
64 m_data(data),
65 m_capacity(capacity) {
66 }
67
74 template <typename T>
75 explicit fixed_size_buffer_adaptor(T& container) :
76 m_data(container.data()),
77 m_capacity(container.size()) {
78 }
79
81 const char* data() const noexcept {
82 return m_data;
83 }
84
86 char* data() noexcept {
87 return m_data;
88 }
89
91 std::size_t capacity() const noexcept {
92 return m_capacity;
93 }
94
96 std::size_t size() const noexcept {
97 return m_size;
98 }
99
101 char* begin() noexcept {
102 return m_data;
103 }
104
106 const char* begin() const noexcept {
107 return m_data;
108 }
109
111 const char* cbegin() const noexcept {
112 return m_data;
113 }
114
116 char* end() noexcept {
117 return m_data + m_size;
118 }
119
121 const char* end() const noexcept {
122 return m_data + m_size;
123 }
124
126 const char* cend() const noexcept {
127 return m_data + m_size;
128 }
129
131
132 // Do not rely on anything beyond this point
133
134 void append(const char* data, std::size_t count) {
135 if (m_size + count > m_capacity) {
136 throw std::length_error{"fixed size data store exhausted"};
137 }
138 std::copy_n(data, count, m_data + m_size);
139 m_size += count;
140 }
141
142 void append_zeros(std::size_t count) {
143 if (m_size + count > m_capacity) {
144 throw std::length_error{"fixed size data store exhausted"};
145 }
146 std::fill_n(m_data + m_size, count, '\0');
147 m_size += count;
148 }
149
150 void resize(std::size_t size) {
151 protozero_assert(size < m_size);
152 if (size > m_capacity) {
153 throw std::length_error{"fixed size data store exhausted"};
154 }
155 m_size = size;
156 }
157
158 void erase_range(std::size_t from, std::size_t to) {
159 protozero_assert(from <= m_size);
160 protozero_assert(to <= m_size);
161 protozero_assert(from < to);
162 std::copy(m_data + to, m_data + m_size, m_data + from);
163 m_size -= (to - from);
164 }
165
166 char* at_pos(std::size_t pos) {
167 protozero_assert(pos <= m_size);
168 return m_data + pos;
169 }
170
171 void push_back(char ch) {
172 if (m_size >= m_capacity) {
173 throw std::length_error{"fixed size data store exhausted"};
174 }
175 m_data[m_size++] = ch;
176 }
178
179}; // class fixed_size_buffer_adaptor
180
182template <>
183struct buffer_customization<fixed_size_buffer_adaptor> {
184
185 static std::size_t size(const fixed_size_buffer_adaptor* buffer) noexcept {
186 return buffer->size();
187 }
188
189 static void append(fixed_size_buffer_adaptor* buffer, const char* data, std::size_t count) {
190 buffer->append(data, count);
191 }
192
193 static void append_zeros(fixed_size_buffer_adaptor* buffer, std::size_t count) {
194 buffer->append_zeros(count);
195 }
196
197 static void resize(fixed_size_buffer_adaptor* buffer, std::size_t size) {
198 buffer->resize(size);
199 }
200
201 static void reserve_additional(fixed_size_buffer_adaptor* /*buffer*/, std::size_t /*size*/) {
202 /* nothing to be done for fixed-size buffers */
203 }
204
205 static void erase_range(fixed_size_buffer_adaptor* buffer, std::size_t from, std::size_t to) {
206 buffer->erase_range(from, to);
207 }
208
209 static char* at_pos(fixed_size_buffer_adaptor* buffer, std::size_t pos) {
210 return buffer->at_pos(pos);
211 }
212
213 static void push_back(fixed_size_buffer_adaptor* buffer, char ch) {
214 buffer->push_back(ch);
215 }
216
217};
219
220} // namespace protozero
221
222#endif // PROTOZERO_BUFFER_FIXED_HPP
buffer_tmpl.hpp
Contains the customization points for buffer implementations.
protozero::fixed_size_buffer_adaptor
Definition buffer_fixed.hpp:34
protozero::fixed_size_buffer_adaptor::fixed_size_buffer_adaptor
fixed_size_buffer_adaptor(T &container)
Definition buffer_fixed.hpp:75
protozero::fixed_size_buffer_adaptor::size
std::size_t size() const noexcept
The number of bytes used in the buffer. Always <= capacity().
Definition buffer_fixed.hpp:96
protozero::fixed_size_buffer_adaptor::end
const char * end() const noexcept
Return iterator to end of data.
Definition buffer_fixed.hpp:121
protozero::fixed_size_buffer_adaptor::cbegin
const char * cbegin() const noexcept
Return iterator to beginning of data.
Definition buffer_fixed.hpp:111
protozero::fixed_size_buffer_adaptor::fixed_size_buffer_adaptor
fixed_size_buffer_adaptor(char *data, std::size_t capacity) noexcept
Definition buffer_fixed.hpp:63
protozero::fixed_size_buffer_adaptor::end
char * end() noexcept
Return iterator to end of data.
Definition buffer_fixed.hpp:116
protozero::fixed_size_buffer_adaptor::capacity
std::size_t capacity() const noexcept
The capacity this buffer was created with.
Definition buffer_fixed.hpp:91
protozero::fixed_size_buffer_adaptor::begin
const char * begin() const noexcept
Return iterator to beginning of data.
Definition buffer_fixed.hpp:106
protozero::fixed_size_buffer_adaptor::data
char * data() noexcept
Returns a pointer to the data in the buffer.
Definition buffer_fixed.hpp:86
protozero::fixed_size_buffer_adaptor::begin
char * begin() noexcept
Return iterator to beginning of data.
Definition buffer_fixed.hpp:101
protozero::fixed_size_buffer_adaptor::data
const char * data() const noexcept
Returns a pointer to the data in the buffer.
Definition buffer_fixed.hpp:81
protozero::fixed_size_buffer_adaptor::cend
const char * cend() const noexcept
Return iterator to end of data.
Definition buffer_fixed.hpp:126
config.hpp
Contains macro checks for different configurations.
protozero
All parts of the protozero header-only library are in this namespace.
Definition basic_pbf_builder.hpp:24