| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#pragma once |
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
#include <cstdint> |
| 33 |
#include <memory> |
| 34 |
#include <type_traits> |
| 35 |
|
| 36 |
namespace epee |
| 37 |
{ |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
template<typename T> |
| 54 |
class span |
| 55 |
{ |
| 56 |
template<typename U> |
| 57 |
static constexpr bool safe_conversion() noexcept |
| 58 |
{ |
| 59 |
|
| 60 |
using with_const = typename std::add_const<U>::type; |
| 61 |
return std::is_same<T, U>() || |
| 62 |
(std::is_const<T>() && std::is_same<T, with_const>()); |
| 63 |
} |
| 64 |
|
| 65 |
public: |
| 66 |
using value_type = T; |
| 67 |
using size_type = std::size_t; |
| 68 |
using difference_type = std::ptrdiff_t; |
| 69 |
using pointer = T*; |
| 70 |
using const_pointer = const T*; |
| 71 |
using reference = T&; |
| 72 |
using const_reference = const T&; |
| 73 |
using iterator = pointer; |
| 74 |
using const_iterator = const_pointer; |
| 75 |
|
| 76 |
constexpr span() noexcept : ptr(nullptr), len(0) {} |
| 77 |
constexpr span(std::nullptr_t) noexcept : span() {} |
| 78 |
|
| 79 |
|
| 80 |
template<typename U, typename = typename std::enable_if<safe_conversion<U>()>::type> |
| 81 |
constexpr span(U* const src_ptr, const std::size_t count) noexcept |
| 82 |
: ptr(src_ptr), len(count) {} |
| 83 |
|
| 84 |
|
| 85 |
template<std::size_t N> |
| 86 |
constexpr span(T (&src)[N]) noexcept : span(src, N) {} |
| 87 |
|
| 88 |
constexpr span(const span&) noexcept = default; |
| 89 |
span& operator=(const span&) noexcept = default; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
std::size_t remove_prefix(std::size_t amount) noexcept |
| 94 |
{ |
| 95 |
amount = std::min(len, amount); |
| 96 |
ptr += amount; |
| 97 |
len -= amount; |
| 98 |
return amount; |
| 99 |
} |
| 100 |
|
| 101 |
constexpr iterator begin() const noexcept { return ptr; } |
| 102 |
constexpr const_iterator cbegin() const noexcept { return ptr; } |
| 103 |
|
| 104 |
constexpr iterator end() const noexcept { return begin() + size(); } |
| 105 |
constexpr const_iterator cend() const noexcept { return cbegin() + size(); } |
| 106 |
|
| 107 |
constexpr bool empty() const noexcept { return size() == 0; } |
| 108 |
constexpr pointer data() const noexcept { return ptr; } |
| 109 |
constexpr std::size_t size() const noexcept { return len; } |
| 110 |
constexpr std::size_t size_bytes() const noexcept { return size() * sizeof(value_type); } |
| 111 |
|
| 112 |
T &operator[](size_t idx) noexcept { return ptr[idx]; } |
| 113 |
const T &operator[](size_t idx) const noexcept { return ptr[idx]; } |
| 114 |
|
| 115 |
private: |
| 116 |
T* ptr; |
| 117 |
std::size_t len; |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
template<typename T> |
| 122 |
constexpr span<const typename T::value_type> to_span(const T& src) |
| 123 |
{ |
| 124 |
|
| 125 |
return {src.data(), src.size()}; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
template<typename T> |
| 130 |
constexpr span<typename T::value_type> to_mut_span(T& src) |
| 131 |
{ |
| 132 |
|
| 133 |
return {src.data(), src.size()}; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
template<typename T> |
| 138 |
span<const std::uint8_t> to_byte_span(const span<const T> src) noexcept |
| 139 |
{ |
| 140 |
static_assert(!std::is_empty<T>(), "empty value types will not work -> sizeof == 1"); |
| 141 |
static_assert(std::is_standard_layout<T>(), "type must have standard layout"); |
| 142 |
static_assert(std::is_trivially_copyable<T>(), "type must be trivially copyable"); |
| 143 |
static_assert(alignof(T) == 1, "type may have padding"); |
| 144 |
return {reinterpret_cast<const std::uint8_t*>(src.data()), src.size_bytes()}; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
template<typename T> |
| 149 |
constexpr span<std::uint8_t> to_mut_byte_span(T& src) |
| 150 |
{ |
| 151 |
using value_type = typename T::value_type; |
| 152 |
static_assert(!std::is_empty<value_type>(), "empty value types will not work -> sizeof == 1"); |
| 153 |
static_assert(std::is_standard_layout<value_type>(), "value type must have standard layout"); |
| 154 |
static_assert(std::is_trivially_copyable<value_type>(), "value type must be trivially copyable"); |
| 155 |
static_assert(alignof(value_type) == 1, "value type may have padding"); |
| 156 |
return {reinterpret_cast<std::uint8_t*>(src.data()), src.size() * sizeof(value_type)}; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
template<typename T> |
| 161 |
span<const std::uint8_t> as_byte_span(const T& src) noexcept |
| 162 |
{ |
| 163 |
static_assert(!std::is_empty<T>(), "empty types will not work -> sizeof == 1"); |
| 164 |
static_assert(std::is_standard_layout<T>(), "type must have standard layout"); |
| 165 |
static_assert(std::is_trivially_copyable<T>(), "type must be trivially copyable"); |
| 166 |
static_assert(alignof(T) == 1, "type may have padding"); |
| 167 |
return {reinterpret_cast<const std::uint8_t*>(std::addressof(src)), sizeof(T)}; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
template<typename T> |
| 172 |
span<std::uint8_t> as_mut_byte_span(T& src) noexcept |
| 173 |
{ |
| 174 |
static_assert(!std::is_empty<T>(), "empty types will not work -> sizeof == 1"); |
| 175 |
static_assert(std::is_standard_layout<T>(), "type must have standard layout"); |
| 176 |
static_assert(std::is_trivially_copyable<T>(), "type must be trivially copyable"); |
| 177 |
static_assert(alignof(T) == 1, "type may have padding"); |
| 178 |
return {reinterpret_cast<std::uint8_t*>(std::addressof(src)), sizeof(T)}; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
template<typename T, typename U> |
| 183 |
span<const T> strspan(const U&s) noexcept |
| 184 |
{ |
| 185 |
static_assert(std::is_same<typename U::value_type, char>(), "unexpected source type"); |
| 186 |
static_assert(std::is_same<T, char>() || std::is_same<T, unsigned char>() || std::is_same<T, int8_t>() || std::is_same<T, uint8_t>(), "Unexpected destination type"); |
| 187 |
return {reinterpret_cast<const T*>(s.data()), s.size()}; |
| 188 |
} |
| 189 |
} |