sparrow 0.3.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 <cmath>
18#include <ranges>
19#include <type_traits>
20
23
24namespace sparrow
25{
26
27 namespace detail
28 {
29 template <class T>
30 class holder
31 {
32 public:
33
34 template <class... Args>
35 holder(Args&&... args)
36 : value(std::forward<Args>(args)...)
37 {
38 }
39
41
42 [[nodiscard]] T extract_storage() &&
43 {
44 return std::move(value);
45 }
46
47 [[nodiscard]] const T& storage() const
48 {
49 return value;
50 }
51
52 [[nodiscard]] T& storage()
53 {
54 return value;
55 }
56
57 void assign(T&& other)
58 {
59 value = std::move(other);
60 }
61 };
62 }
63
64 // like buffer<T> but for any type T, nut always use buffer<std::uint8_t> as storage
65 // This internal storage can be extracted
66 template <class T>
67 class u8_buffer : private detail::holder<buffer<std::uint8_t>>,
68 public buffer_adaptor<T, buffer<std::uint8_t>&>
69 {
70 public:
71
75
77 u8_buffer(const u8_buffer& other);
78 u8_buffer& operator=(u8_buffer&& other) = delete;
79 u8_buffer& operator=(u8_buffer& other) = delete;
80
81 u8_buffer(std::size_t n, const T& val = T{});
82 template <std::ranges::input_range R>
83 requires(!std::same_as<u8_buffer<T>, std::decay_t<R>> && std::convertible_to<std::ranges::range_value_t<R>, T>)
84 u8_buffer(R&& range);
85 u8_buffer(std::initializer_list<T> ilist);
86 };
87
88 template <class T>
94
95 template <class T>
101
102 template <class T>
103 u8_buffer<T>::u8_buffer(std::size_t n, const T& val)
104 : holder_type{n * sizeof(T)}
106 {
107 std::fill(this->begin(), this->end(), val);
108 }
109
110 template <class T>
111 template <std::ranges::input_range R>
112 requires(!std::same_as<u8_buffer<T>, std::decay_t<R>>
113 && std::convertible_to<std::ranges::range_value_t<R>, T>)
115 : holder_type{range_size(range) * sizeof(T)}
116 , buffer_adaptor_type(holder_type::value)
117 {
118 std::ranges::copy(range, this->begin());
119 }
120
121 template <class T>
122 u8_buffer<T>::u8_buffer(std::initializer_list<T> ilist)
123 : holder_type{ilist.size() * sizeof(T)}
125 {
126 std::copy(ilist.begin(), ilist.end(), this->begin());
127 }
128}
constexpr size_type size() const noexcept
void assign(T &&other)
Definition u8_buffer.hpp:57
holder(Args &&... args)
Definition u8_buffer.hpp:35
const T & storage() const
Definition u8_buffer.hpp:47
buffer_adaptor< T, buffer< std::uint8_t > & > buffer_adaptor_type
Definition u8_buffer.hpp:73
u8_buffer & operator=(u8_buffer &&other)=delete
u8_buffer & operator=(u8_buffer &other)=delete
buffer< std::uint8_t > extract_storage() &&
Definition u8_buffer.hpp:42
detail::holder< buffer< std::uint8_t > > holder_type
Definition u8_buffer.hpp:72
u8_buffer(const u8_buffer &other)
Definition u8_buffer.hpp:96
u8_buffer(R &&range)
u8_buffer(u8_buffer &&other)
Definition u8_buffer.hpp:89
u8_buffer(std::initializer_list< T > ilist)
u8_buffer(std::size_t n, const T &val=T{})
std::size_t range_size(R &&r)
Definition ranges.hpp:31