tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
buffer_const.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_NET_BUFFER_CONST_HPP
4#define TETL_NET_BUFFER_CONST_HPP
5
6#include <etl/version.hpp>
7
8#include <etl/array.hpp>
9#include <etl/cstddef.hpp>
10
11namespace etl::experimental::net {
14 const_buffer() noexcept = default;
15
17 const_buffer(void const* data, etl::size_t size)
18 : _data{data}
19 , _size{size}
20 {
21 }
22
24 [[nodiscard]] auto data() const noexcept -> void const* { return _data; }
25
27 [[nodiscard]] auto size() const noexcept -> etl::size_t { return _size; }
28
30 auto operator+=(etl::size_t n) noexcept -> const_buffer&
31 {
32 auto const offset = n < _size ? n : _size;
33 _data = static_cast<char const*>(_data) + offset;
34 _size -= offset;
35 return *this;
36 }
37
38private:
39 void const* _data = nullptr;
40 etl::size_t _size = 0;
41};
42
46inline auto operator+(const_buffer const& b, etl::size_t const n) noexcept -> const_buffer
47{
48 auto offset = n < b.size() ? n : b.size();
49 auto const* data = static_cast<char const*>(b.data()) + offset;
50 auto size = b.size() - offset;
51 return const_buffer{data, size};
52}
53
57inline auto operator+(etl::size_t const n, const_buffer const& b) noexcept -> const_buffer { return b + n; }
58
59} // namespace etl::experimental::net
60
61#endif // TETL_NET_BUFFER_CONST_HPP
constexpr auto size(C const &c) noexcept(noexcept(c.size())) -> decltype(c.size())
Returns the size of the given container c or array array. Returns c.size(), converted to the return t...
Definition size.hpp:18
Definition buffer.hpp:14
Definition adjacent_find.hpp:8
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
auto operator+(const_buffer const &b, etl::size_t const n) noexcept -> const_buffer
Create a new modifiable buffer that is offset from the start of another.
Definition buffer_const.hpp:46
auto operator+=(etl::size_t n) noexcept -> const_buffer &
Move the start of the buffer by the specified number of bytes.
Definition buffer_const.hpp:30
const_buffer() noexcept=default
Construct an empty buffer.
auto operator+(etl::size_t const n, const_buffer const &b) noexcept -> const_buffer
Create a new modifiable buffer that is offset from the start of another.
Definition buffer_const.hpp:57
auto data() const noexcept -> void const *
Get a pointer to the beginning of the memory range.
Definition buffer_const.hpp:24
auto size() const noexcept -> etl::size_t
Get the size of the memory range.
Definition buffer_const.hpp:27