tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
buffer_mutable.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_NET_BUFFER_MUTABLE_HPP
5#define TETL_NET_BUFFER_MUTABLE_HPP
6
7#include <etl/version.hpp>
8
9#include <etl/array.hpp>
10#include <etl/cstddef.hpp>
11
12namespace etl::experimental::net {
14 /// \brief Construct an empty buffer.
15 mutable_buffer() noexcept = default;
16
17 /// \brief Construct a buffer to represent a given memory range.
18 mutable_buffer(void* data, etl::size_t size)
19 : _data{data}
20 , _size{size}
21 {
22 }
23
24 /// \brief Get a pointer to the beginning of the memory range.
25 [[nodiscard]] auto data() const noexcept -> void*
26 {
27 return _data;
28 }
29
30 /// \brief Get the size of the memory range.
31 [[nodiscard]] auto size() const noexcept -> etl::size_t
32 {
33 return _size;
34 }
35
36 /// \brief Move the start of the buffer by the specified number of bytes.
37 auto operator+=(etl::size_t n) noexcept -> mutable_buffer&
38 {
39 auto const offset = n < _size ? n : _size;
40 _data = static_cast<char*>(_data) + offset;
41 _size -= offset;
42 return *this;
43 }
44
45private:
46 void* _data = nullptr;
47 etl::size_t _size = 0;
48};
49
50/// \brief Create a new modifiable buffer that is offset from the start of
51/// another.
52/// \relates mutable_buffer
53inline auto operator+(mutable_buffer const& b, etl::size_t const n) noexcept -> mutable_buffer
54{
55 auto offset = n < b.size() ? n : b.size();
56 auto* data = static_cast<char*>(b.data()) + offset;
57 auto size = b.size() - offset;
58 return mutable_buffer{data, size};
59}
60
61/// \brief Create a new modifiable buffer that is offset from the start of
62/// another.
63/// \relates mutable_buffer
64inline auto operator+(etl::size_t const n, mutable_buffer const& b) noexcept -> mutable_buffer
65{
66 return b + n;
67}
68
69} // namespace etl::experimental::net
70
71#endif // TETL_NET_BUFFER_MUTABLE_HPP
Definition buffer.hpp:15
Definition adjacent_find.hpp:9
Definition buffer_mutable.hpp:13
auto operator+=(etl::size_t n) noexcept -> mutable_buffer &
Move the start of the buffer by the specified number of bytes.
Definition buffer_mutable.hpp:37
auto data() const noexcept -> void *
Get a pointer to the beginning of the memory range.
Definition buffer_mutable.hpp:25
mutable_buffer(void *data, etl::size_t size)
Construct a buffer to represent a given memory range.
Definition buffer_mutable.hpp:18
auto operator+(etl::size_t const n, mutable_buffer const &b) noexcept -> mutable_buffer
Create a new modifiable buffer that is offset from the start of another.
Definition buffer_mutable.hpp:64
auto operator+(mutable_buffer const &b, etl::size_t const n) noexcept -> mutable_buffer
Create a new modifiable buffer that is offset from the start of another.
Definition buffer_mutable.hpp:53
mutable_buffer() noexcept=default
Construct an empty buffer.
auto size() const noexcept -> etl::size_t
Get the size of the memory range.
Definition buffer_mutable.hpp:31