sparrow 2.3.1
C++20 idiomatic APIs for the Apache Arrow Columnar Format
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
25
26namespace sparrow
27{
28 namespace copy_tracker
29 {
30 template <typename T>
31 std::string key_u8_buffer()
32 {
33 return "u8_buffer<" + std::string(typeid(T).name()) + ">";
34 }
35 }
36
37 namespace detail
38 {
44 template <class T>
45 class holder
46 {
47 public:
48
49 using inner_type = T;
50
57 template <class... Args>
58 constexpr holder(Args&&... args) noexcept
59 : value(std::forward<Args>(args)...)
60 {
61 }
62
64
70 [[nodiscard]] constexpr T extract_storage() && noexcept
71 {
72 return std::move(value);
73 }
74
80 [[nodiscard]] constexpr const T& storage() const noexcept
81 {
82 return value;
83 }
84
90 [[nodiscard]] constexpr T& storage() noexcept
91 {
92 return value;
93 }
94
100 constexpr void assign(T&& other)
101 {
102 value = std::move(other);
103 }
104 };
105 }
106
113 template <class T>
114 class u8_buffer : private detail::holder<buffer<std::uint8_t>>,
115 public buffer_adaptor<T, buffer<std::uint8_t>&>
116 {
117 public:
118
124
130 constexpr u8_buffer(u8_buffer&& other) noexcept;
131
137 constexpr u8_buffer(const u8_buffer& other);
138
142 u8_buffer& operator=(u8_buffer&& other) = delete;
143
147 u8_buffer& operator=(u8_buffer& other) = delete;
148
152 ~u8_buffer() = default;
153
159 constexpr explicit u8_buffer(std::size_t n);
160
167 constexpr u8_buffer(std::size_t n, const T& val);
168
177 template <std::ranges::input_range R>
178 requires(
179 !std::same_as<u8_buffer<T>, std::decay_t<R>>
180 && std::convertible_to<std::ranges::range_value_t<R>, T>
181 )
182 constexpr explicit u8_buffer(R&& range);
183
189 constexpr u8_buffer(std::initializer_list<T> ilist);
190
216 template <allocator A>
217 constexpr u8_buffer(T* data_ptr, std::size_t count, const A& a);
218 };
219
220 template <class T>
221 constexpr u8_buffer<T>::u8_buffer(u8_buffer&& other) noexcept
222 : holder_type(std::move(other).extract_storage())
224 {
225 }
226
227 template <class T>
234
235 template <class T>
236 constexpr u8_buffer<T>::u8_buffer(std::size_t n)
237 : holder_type{n * sizeof(T), typename buffer_type::default_allocator{}}
239 {
240 }
241
242 template <class T>
243 constexpr u8_buffer<T>::u8_buffer(std::size_t n, const T& val)
244 : u8_buffer(n)
245 {
246 std::fill(this->begin(), this->end(), val);
247 }
248
249 template <class T>
250 template <std::ranges::input_range R>
251 requires(
252 !std::same_as<u8_buffer<T>, std::decay_t<R>> && std::convertible_to<std::ranges::range_value_t<R>, T>
253 )
254 constexpr u8_buffer<T>::u8_buffer(R&& range)
255 : u8_buffer(range_size(range))
256 {
257 sparrow::ranges::copy(range, this->begin());
258 }
259
260 template <class T>
261 constexpr u8_buffer<T>::u8_buffer(std::initializer_list<T> ilist)
262 : u8_buffer(ilist.size())
263 {
264 std::copy(ilist.begin(), ilist.end(), this->begin());
265 }
266
267 template <class T>
268 template <allocator A>
269 constexpr u8_buffer<T>::u8_buffer(T* data_ptr, std::size_t count, const A& a)
270 : holder_type{reinterpret_cast<uint8_t*>(data_ptr), count * sizeof(T), a}
272 {
273 }
274}
constexpr size_type size() const noexcept(!SPARROW_CONTRACTS_THROW_ON_FAILURE)
Object that owns a piece of contiguous memory.
Definition buffer.hpp:131
xsimd::aligned_allocator< std::uint8_t > default_allocator
Definition buffer.hpp:144
A holder class that wraps a value and provides storage management.
Definition u8_buffer.hpp:46
constexpr void assign(T &&other)
Assigns a new value to the storage.
constexpr T extract_storage() &&noexcept
Extracts the storage by moving the wrapped value.
Definition u8_buffer.hpp:70
constexpr T & storage() noexcept
Gets a reference to the storage.
Definition u8_buffer.hpp:90
constexpr holder(Args &&... args) noexcept
Constructs a holder with the given arguments forwarded to the wrapped value.
Definition u8_buffer.hpp:58
constexpr const T & storage() const noexcept
Gets a constant reference to the storage.
Definition u8_buffer.hpp:80
u8_buffer & operator=(u8_buffer &&other)=delete
Move assignment operator (deleted).
buffer_type::default_allocator default_allocator
constexpr u8_buffer(std::size_t n)
Constructs a buffer with n uninitialized elements.
constexpr u8_buffer(std::initializer_list< T > ilist)
Constructs a buffer with the elements of the initializer list ilist.
u8_buffer & operator=(u8_buffer &other)=delete
Copy assignment operator (deleted).
constexpr u8_buffer(T *data_ptr, std::size_t count, const A &a)
Constructs a buffer by taking ownership of the storage pointed to by data_ptr.
constexpr u8_buffer(const u8_buffer &other)
Copy constructor.
constexpr buffer_type extract_storage() &&noexcept
Extracts the storage by moving the wrapped value.
Definition u8_buffer.hpp:70
constexpr u8_buffer(R &&range)
Constructs a buffer with the elements of the range range.
detail::holder< buffer_type > holder_type
constexpr u8_buffer(std::size_t n, const T &val)
Constructs a buffer with n elements, each initialized to val.
buffer_adaptor< IT, buffer_type & > buffer_adaptor_type
~u8_buffer()=default
Destructor.
constexpr u8_buffer(u8_buffer &&other) noexcept
Move constructor.
buffer< std::uint8_t > buffer_type
std::string key_u8_buffer()
Definition u8_buffer.hpp:31
SPARROW_API void increase(const std::string &key)
constexpr std::ranges::copy_result< std::ranges::borrowed_iterator_t< R >, O > copy(R &&r, O result)
Definition ranges.hpp:132
constexpr std::size_t range_size(R &&r)
Definition ranges.hpp:35