sparrow 0.6.0
Loading...
Searching...
No Matches
u8_buffer.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <ranges>
18#include <type_traits>
19
24
25namespace sparrow
26{
27
28 namespace detail
29 {
30
31 template <class T>
32 class holder
33 {
34 public:
35
36 using inner_type = T;
37
38 template <class... Args>
39 holder(Args&&... args)
40 : value(std::forward<Args>(args)...)
41 {
42 }
43
45
46 [[nodiscard]] T extract_storage() &&
47 {
48 return std::move(value);
49 }
50
51 [[nodiscard]] const T& storage() const
52 {
53 return value;
54 }
55
56 [[nodiscard]] T& storage()
57 {
58 return value;
59 }
60
61 void assign(T&& other)
62 {
63 value = std::move(other);
64 }
65 };
66 }
67
72 template <class T>
73 class u8_buffer : private detail::holder<buffer<std::uint8_t>>,
74 public buffer_adaptor<T, buffer<std::uint8_t>&>
75 {
76 public:
77
81
83 u8_buffer(const u8_buffer& other);
84 u8_buffer& operator=(u8_buffer&& other) = delete;
85 u8_buffer& operator=(u8_buffer& other) = delete;
86 ~u8_buffer() = default;
87
93 u8_buffer(std::size_t n, const T& val = T{});
94
100 template <std::ranges::input_range R>
101 requires(
102 !std::same_as<u8_buffer<T>, std::decay_t<R>>
103 && std::convertible_to<std::ranges::range_value_t<R>, T>
104 )
105 u8_buffer(R&& range);
106
111 u8_buffer(std::initializer_list<T> ilist);
112
118 template <allocator A = std::allocator<T>>
119 u8_buffer(T* data_ptr, std::size_t count, const A& a = A());
120 };
121
122 template <class T>
128
129 template <class T>
135
136 template <class T>
137 u8_buffer<T>::u8_buffer(std::size_t n, const T& val)
138 : holder_type{n * sizeof(T)}
140 {
141 std::fill(this->begin(), this->end(), val);
142 }
143
144 template <class T>
145 template <std::ranges::input_range R>
146 requires(!std::same_as<u8_buffer<T>, std::decay_t<R>>
147 && std::convertible_to<std::ranges::range_value_t<R>, T>)
149 : holder_type{range_size(range) * sizeof(T)}
150 , buffer_adaptor_type(holder_type::value)
151 {
152 sparrow::ranges::copy(range, this->begin());
153 }
154
155 template <class T>
156 u8_buffer<T>::u8_buffer(std::initializer_list<T> ilist)
157 : holder_type{ilist.size() * sizeof(T)}
159 {
160 std::copy(ilist.begin(), ilist.end(), this->begin());
161 }
162
163 template <class T>
164 template <allocator A>
165 u8_buffer<T>::u8_buffer(T* data_ptr, std::size_t count, const A& a)
166 : holder_type{reinterpret_cast<uint8_t*>(data_ptr), count * sizeof(T), a}
168 {
169 }
170}
constexpr size_type size() const noexcept
void assign(T &&other)
Definition u8_buffer.hpp:61
holder(Args &&... args)
Definition u8_buffer.hpp:39
const T & storage() const
Definition u8_buffer.hpp:51
buffer_adaptor< T, buffer< std::uint8_t > & > buffer_adaptor_type
Definition u8_buffer.hpp:79
u8_buffer & operator=(u8_buffer &&other)=delete
u8_buffer(T *data_ptr, std::size_t count, const A &a=A())
Constructs a buffer by taking ownership of the storage pointed to by data_ptr.
u8_buffer & operator=(u8_buffer &other)=delete
buffer< std::uint8_t > extract_storage() &&
Definition u8_buffer.hpp:46
detail::holder< buffer< std::uint8_t > > holder_type
Definition u8_buffer.hpp:78
u8_buffer(R &&range)
Constructs a buffer with the elements of the range range.
u8_buffer(const u8_buffer &other)
~u8_buffer()=default
u8_buffer(u8_buffer &&other)
u8_buffer(std::initializer_list< T > ilist)
Constructs a buffer with the elements of the initializer list ilist.
u8_buffer(std::size_t n, const T &val=T{})
Constructs a buffer with n elements, each initialized to val.
constexpr std::ranges::copy_result< std::ranges::borrowed_iterator_t< R >, O > copy(R &&r, O result)
Definition ranges.hpp:118
std::size_t range_size(R &&r)
Definition ranges.hpp:33