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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include <array>
#include <type_traits>
#include "caf/byte.hpp"
#include "caf/detail/type_traits.hpp"
namespace caf {
/// A C++11/14 drop-in replacement for C++20's `std::span` without support for
/// static extents.
template <class T>
class span {
public:
// -- member types -----------------------------------------------------------
using element_type = T;
using value_type = typename std::remove_cv<T>::type;
using index_type = size_t;
using difference_type = ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = T&;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
// -- constructors, destructors, and assignment operators --------------------
constexpr span() noexcept : begin_(nullptr), size_(0) {
// nop
}
constexpr span(pointer ptr, size_t size) : begin_(ptr), size_(size) {
// nop
}
constexpr span(pointer first, pointer last)
: begin_(first), size_(static_cast<size_t>(last - first)) {
// nop
}
template <size_t Size>
constexpr span(element_type (&arr)[Size]) noexcept
: begin_(arr), size_(Size) {
// nop
}
template <class C,
class E = detail::enable_if_t<detail::has_data_member<C>::value>>
span(C& xs) noexcept : begin_(xs.data()), size_(xs.size()) {
// nop
}
template <class C,
class E = detail::enable_if_t<detail::has_data_member<C>::value>>
span(const C& xs) noexcept : begin_(xs.data()), size_(xs.size()) {
// nop
}
constexpr span(const span&) noexcept = default;
span& operator=(const span&) noexcept = default;
// -- iterators --------------------------------------------------------------
constexpr iterator begin() const noexcept {
return begin_;
}
constexpr const_iterator cbegin() const noexcept {
return begin_;
}
constexpr iterator end() const noexcept {
return begin() + size_;
}
constexpr const_iterator cend() const noexcept {
return cbegin() + size_;
}
constexpr reverse_iterator rbegin() const noexcept {
return reverse_iterator{end()};
}
constexpr const_reverse_iterator crbegin() const noexcept {
return const_reverse_iterator{end()};
}
constexpr reverse_iterator rend() const noexcept {
return reverse_iterator{begin()};
}
constexpr const_reverse_iterator crend() const noexcept {
return const_reverse_iterator{begin()};
}
// -- element access ---------------------------------------------------------
constexpr reference operator[](size_t index) const noexcept {
return begin_[index];
}
constexpr reference front() const noexcept {
return *begin_;
}
constexpr reference back() const noexcept {
return (*this)[size_ - 1];
}
// -- properties -------------------------------------------------------------
constexpr size_t size() const noexcept {
return size_;
}
constexpr size_t size_bytes() const noexcept {
return size_ * sizeof(element_type);
}
constexpr bool empty() const noexcept {
return size_ == 0;
}
constexpr pointer data() const noexcept {
return begin_;
}
// -- subviews ---------------------------------------------------------------
constexpr span subspan(size_t offset, size_t num_bytes) const {
return {begin_ + offset, num_bytes};
}
constexpr span first(size_t num_bytes) const {
return {begin_, num_bytes};
}
constexpr span last(size_t num_bytes) const {
return subspan(size_ - num_bytes, num_bytes);
}
private:
// -- member variables -------------------------------------------------------
/// Points to the first element in the contiguous memory block.
pointer begin_;
/// Stores the number of elements in the contiguous memory block.
size_t size_;
};
template <class T>
auto begin(const span<T>& xs) -> decltype(xs.begin()) {
return xs.begin();
}
template <class T>
auto cbegin(const span<T>& xs) -> decltype(xs.cbegin()) {
return xs.cbegin();
}
template <class T>
auto end(const span<T>& xs) -> decltype(xs.end()) {
return xs.end();
}
template <class T>
auto cend(const span<T>& xs) -> decltype(xs.cend()) {
return xs.cend();
}
template <class T>
span<const byte> as_bytes(span<T> xs) {
return {reinterpret_cast<const byte*>(xs.data()), xs.size_bytes()};
}
template <class T>
span<byte> as_writable_bytes(span<T> xs) {
return {reinterpret_cast<byte*>(xs.data()), xs.size_bytes()};
}
/// Convenience function to make using `caf::span` more convenient without the
/// deduction guides.
template <class T>
auto make_span(T& xs) -> span<detail::remove_reference_t<decltype(xs[0])>> {
return {xs.data(), xs.size()};
}
/// Convenience function to make using `caf::span` more convenient without the
/// deduction guides.
template <class T, size_t N>
span<T> make_span(T (&xs)[N]) {
return {xs, N};
}
/// Convenience function to make using `caf::span` more convenient without the
/// deduction guides.
template <class T>
span<T> make_span(T* first, size_t size) {
return {first, size};
}
/// Convenience function to make using `caf::span` more convenient without the
/// deduction guides.
template <class T>
span<T> make_span(T* first, T* last) {
return {first, last};
}
} // namespace caf
|