MC Protocol Serial C++ 0.2.3
MC protocol serial library for MCU-oriented environments
Loading...
Searching...
No Matches
span_compat.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5
6#if defined(__has_include)
7#if __has_include(<span>)
8#include <span>
9#endif
10#endif
11
12#if !defined(__cpp_lib_span) || (__cpp_lib_span < 202002L)
13namespace std {
14
15constexpr size_t dynamic_extent = static_cast<size_t>(-1);
16
17#if !defined(__cpp_lib_byte) || (__cpp_lib_byte < 201603L)
18enum class byte : unsigned char {};
19#endif
20
21template <typename T>
23 using type = T;
24};
25
26template <typename T>
28 using type = T;
29};
30
31template <typename T>
33 using type = T;
34};
35
36template <typename T>
38 using type = T;
39};
40
41template <typename T, size_t Extent = dynamic_extent>
42class span {
43 public:
44 using element_type = T;
51
52 static constexpr size_type extent = Extent;
53
54 constexpr span() noexcept = default;
55
57
59 : data_(first), size_(static_cast<size_type>(last - first)) {}
60
61 template <size_t N>
62 constexpr span(element_type (&arr)[N]) noexcept : data_(arr), size_(N) {}
63
64 template <typename U, size_t N>
65 constexpr span(array<U, N>& arr) noexcept : data_(arr.data()), size_(N) {}
66
67 template <typename U, size_t N>
68 constexpr span(const array<U, N>& arr) noexcept : data_(arr.data()), size_(N) {}
69
70 template <typename U, size_t OtherExtent>
72 : data_(other.data()), size_(other.size()) {}
73
74 [[nodiscard]] constexpr iterator begin() const noexcept { return data_; }
75 [[nodiscard]] constexpr iterator end() const noexcept { return data_ + size_; }
76 [[nodiscard]] constexpr reference operator[](size_type index) const noexcept { return data_[index]; }
77 [[nodiscard]] constexpr pointer data() const noexcept { return data_; }
78 [[nodiscard]] constexpr size_type size() const noexcept { return size_; }
79 [[nodiscard]] constexpr bool empty() const noexcept { return size_ == 0; }
80
81 [[nodiscard]] constexpr span first(size_type count) const noexcept { return span(data_, count); }
82
83 [[nodiscard]] constexpr span subspan(size_type offset) const noexcept {
84 return span(data_ + offset, size_ - offset);
85 }
86
87 [[nodiscard]] constexpr span subspan(size_type offset, size_type count) const noexcept {
88 return span(data_ + offset, count);
89 }
90
91 private:
92 pointer data_ = nullptr;
93 size_type size_ = 0;
94};
95
96} // namespace std
97#endif
constexpr size_type size() const noexcept
constexpr iterator end() const noexcept
typename mcprotocol_remove_cv< T >::type value_type
constexpr span(const span< U, OtherExtent > &other) noexcept
constexpr pointer data() const noexcept
constexpr span() noexcept=default
constexpr iterator begin() const noexcept
constexpr span(array< U, N > &arr) noexcept
constexpr span subspan(size_type offset) const noexcept
constexpr span(element_type(&arr)[N]) noexcept
constexpr reference operator[](size_type index) const noexcept
static constexpr size_type extent
constexpr span(pointer first, pointer last) noexcept
constexpr span subspan(size_type offset, size_type count) const noexcept
constexpr span(const array< U, N > &arr) noexcept
element_type * pointer
constexpr bool empty() const noexcept
constexpr span first(size_type count) const noexcept
size_t size_type
constexpr size_t dynamic_extent