MillicastSDK 2.0.0
Loading...
Searching...
No Matches
span.h
Go to the documentation of this file.
1#ifndef MILLICAST_API_SPAN_H
2#define MILLICAST_API_SPAN_H
3#include <cstddef>
4#include <iterator>
5
6namespace millicast {
7
8namespace detail {
9
10template <typename T, typename E, typename = void>
11// NOLINTNEXTLINE(readability-identifier-naming)
12struct is_container_element_type_compatible : std::false_type {};
13
14template <typename T, typename E>
16 T,
17 E,
18 typename std::enable_if<
19 !std::is_same<typename std::remove_cv<decltype(std::data(
20 std::declval<T>()))>::type,
21 void>::value &&
22 std::is_convertible<
23 std::remove_pointer_t<decltype(std::declval<T>().data())> (*)[],
24 E (*)[]>::value>::type> : std::true_type {};
25} // namespace detail
26
27template <typename T>
28class Span {
29 public:
30 using element_type = T;
33
34 constexpr Span() noexcept = default;
35 constexpr Span(pointer ptr, size_t size) noexcept : _ptr(ptr), _size(size) {}
36
37 template <size_t N>
38 constexpr Span(T (&arr)[N]) noexcept : Span(arr, N) {}
39 constexpr Span(const Span&) noexcept = default;
40
41 template <
42 typename C,
43 typename std::enable_if<
44 std::is_same_v<typename C::value_type, T> ||
46 int>::type = 0>
47 constexpr Span(const C& container) noexcept
48 : _ptr(std::data(container)), _size(std::size(container)) {}
49
50 template <typename C,
51 typename std::enable_if<
52 std::is_same_v<typename C::value_type, T> ||
54 int>::type = 0>
55 constexpr Span(C& container) noexcept
56 : _ptr(std::data(container)), _size(std::size(container)) {}
57
58 template <
59 typename OtherElementType,
60 typename std::enable_if<!std::is_same_v<OtherElementType, T> &&
63 T>::value,
64 int>::type = 0>
65 constexpr Span(const Span<OtherElementType>& other) noexcept
66 : _ptr(other._ptr), _size(other._size) {}
67
68 constexpr T* data() const { return _ptr; }
69 constexpr T* begin() const { return data(); }
70 constexpr T* end() const { return data() + size(); }
71
72 [[nodiscard]] constexpr size_t size() const { return _size; }
73 [[nodiscard]] constexpr size_t size_bytes() const {
74 return size() * sizeof(T);
75 }
76 [[nodiscard]] constexpr bool empty() const { return size() == 0; }
77
78 private:
79 T* _ptr{nullptr};
80 size_t _size{};
81};
82} // namespace millicast
83#endif // MILLICAST_API_SPAN_H
Definition span.h:28
constexpr Span(const C &container) noexcept
Definition span.h:47
constexpr Span(const Span &) noexcept=default
constexpr bool empty() const
Definition span.h:76
constexpr Span() noexcept=default
element_type & reference
Definition span.h:32
constexpr Span(C &container) noexcept
Definition span.h:55
constexpr T * data() const
Definition span.h:68
constexpr Span(const Span< OtherElementType > &other) noexcept
Definition span.h:65
constexpr T * begin() const
Definition span.h:69
element_type * pointer
Definition span.h:31
constexpr size_t size() const
Definition span.h:72
constexpr size_t size_bytes() const
Definition span.h:73
constexpr Span(T(&arr)[N]) noexcept
Definition span.h:38
T element_type
Definition span.h:30
constexpr T * end() const
Definition span.h:70
Definition capabilities.h:15
Definition error.h:209