Loading...
Searching...
No Matches
buffer_fixed.hpp
Go to the documentation of this file.
#ifndef PROTOZERO_BUFFER_FIXED_HPP
#define PROTOZERO_BUFFER_FIXED_HPP
/*****************************************************************************
protozero - Minimalistic protocol buffer decoder and encoder in C++.
This file is from https://github.com/mapbox/protozero where you can find more
documentation.
*****************************************************************************/
#include "buffer_tmpl.hpp"
#include "config.hpp"
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <stdexcept>
namespace protozero {
class fixed_size_buffer_adaptor {
char* m_data;
std::size_t m_capacity;
std::size_t m_size = 0;
public:
using size_type = std::size_t;
using value_type = char;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
using iterator = pointer;
using const_iterator = const_pointer;
fixed_size_buffer_adaptor(char* data, std::size_t capacity) noexcept :
m_data(data),
m_capacity(capacity) {
}
template <typename T>
explicit fixed_size_buffer_adaptor(T& container) :
m_data(container.data()),
m_capacity(container.size()) {
}
const char* data() const noexcept {
return m_data;
}
char* data() noexcept {
return m_data;
}
std::size_t capacity() const noexcept {
return m_capacity;
}
std::size_t size() const noexcept {
return m_size;
}
char* begin() noexcept {
return m_data;
}
const char* begin() const noexcept {
return m_data;
}
const char* cbegin() const noexcept {
return m_data;
}
char* end() noexcept {
return m_data + m_size;
}
const char* end() const noexcept {
return m_data + m_size;
}
const char* cend() const noexcept {
return m_data + m_size;
}
// Do not rely on anything beyond this point
void append(const char* data, std::size_t count) {
if (m_size + count > m_capacity) {
throw std::length_error{"fixed size data store exhausted"};
}
std::copy_n(data, count, m_data + m_size);
m_size += count;
}
void append_zeros(std::size_t count) {
if (m_size + count > m_capacity) {
throw std::length_error{"fixed size data store exhausted"};
}
std::fill_n(m_data + m_size, count, '\0');
m_size += count;
}
void resize(std::size_t size) {
protozero_assert(size < m_size);
if (size > m_capacity) {
throw std::length_error{"fixed size data store exhausted"};
}
m_size = size;
}
void erase_range(std::size_t from, std::size_t to) {
protozero_assert(from <= m_size);
protozero_assert(to <= m_size);
protozero_assert(from < to);
std::copy(m_data + to, m_data + m_size, m_data + from);
m_size -= (to - from);
}
char* at_pos(std::size_t pos) {
protozero_assert(pos <= m_size);
return m_data + pos;
}
void push_back(char ch) {
if (m_size >= m_capacity) {
throw std::length_error{"fixed size data store exhausted"};
}
m_data[m_size++] = ch;
}
}; // class fixed_size_buffer_adaptor
template <>
struct buffer_customization<fixed_size_buffer_adaptor> {
static std::size_t size(const fixed_size_buffer_adaptor* buffer) noexcept {
return buffer->size();
}
static void append(fixed_size_buffer_adaptor* buffer, const char* data, std::size_t count) {
buffer->append(data, count);
}
static void append_zeros(fixed_size_buffer_adaptor* buffer, std::size_t count) {
buffer->append_zeros(count);
}
static void resize(fixed_size_buffer_adaptor* buffer, std::size_t size) {
buffer->resize(size);
}
static void reserve_additional(fixed_size_buffer_adaptor* /*buffer*/, std::size_t /*size*/) {
/* nothing to be done for fixed-size buffers */
}
static void erase_range(fixed_size_buffer_adaptor* buffer, std::size_t from, std::size_t to) {
buffer->erase_range(from, to);
}
static char* at_pos(fixed_size_buffer_adaptor* buffer, std::size_t pos) {
return buffer->at_pos(pos);
}
static void push_back(fixed_size_buffer_adaptor* buffer, char ch) {
buffer->push_back(ch);
}
};
} // namespace protozero
#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