| 1 |
#ifndef SPAN_HPP |
| 2 |
#define SPAN_HPP |
| 3 |
|
| 4 |
#include "utils/utils.hpp" |
| 5 |
|
| 6 |
#include <cstddef> |
| 7 |
#include <iterator> |
| 8 |
#include <memory> |
| 9 |
#include <type_traits> |
| 10 |
#include <utility> |
| 11 |
|
| 12 |
CPPTRACE_BEGIN_NAMESPACE |
| 13 |
namespace detail { |
| 14 |
// basic span implementation |
| 15 |
// I haven't implemented most members because I don't need them, more will be added as needed |
| 16 |
|
| 17 |
template<typename T> |
| 18 |
class span { |
| 19 |
T* ptr; |
| 20 |
std::size_t count; |
| 21 |
|
| 22 |
public: |
| 23 |
using element_type = T; |
| 24 |
using value_type = typename std::remove_cv<T>::type; |
| 25 |
using size_type = std::size_t; |
| 26 |
using difference_type = std::ptrdiff_t; |
| 27 |
using pointer = T*; |
| 28 |
using const_pointer = const T*; |
| 29 |
using reference = T&; |
| 30 |
using const_reference = const T&; |
| 31 |
using iterator = T*; |
| 32 |
using const_iterator = const T*; |
| 33 |
using reverse_iterator = std::reverse_iterator<iterator>; |
| 34 |
using const_reverse_iterator = std::reverse_iterator<reverse_iterator>; |
| 35 |
using i_am_span = void; |
| 36 |
|
| 37 |
span() : ptr(nullptr), count(0) {} |
| 38 |
span(T* ptr, std::size_t count) : ptr(ptr), count(count) {} |
| 39 |
template<typename It> |
| 40 |
span(It begin, It end) : ptr(std::addressof(*begin)), count(end - begin) {} |
| 41 |
|
| 42 |
T* data() const noexcept { |
| 43 |
return ptr; |
| 44 |
} |
| 45 |
std::size_t size() const noexcept { |
| 46 |
return count; |
| 47 |
} |
| 48 |
bool empty() const noexcept { |
| 49 |
return count == 0; |
| 50 |
} |
| 51 |
|
| 52 |
iterator begin() noexcept { |
| 53 |
return ptr; |
| 54 |
} |
| 55 |
iterator end() noexcept { |
| 56 |
return ptr + count; |
| 57 |
} |
| 58 |
const_iterator begin() const noexcept { |
| 59 |
return ptr; |
| 60 |
} |
| 61 |
const_iterator end() const noexcept { |
| 62 |
return ptr + count; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
using bspan = span<char>; |
| 67 |
using cbspan = span<const char>; |
| 68 |
|
| 69 |
template<typename T, typename = int> |
| 70 |
struct is_span : std::false_type {}; |
| 71 |
|
| 72 |
template<typename T> |
| 73 |
struct is_span<T, void_t<typename T::i_am_span>> : std::true_type {}; |
| 74 |
|
| 75 |
template<typename It> |
| 76 |
auto make_span(It begin, It end) -> span<typename std::remove_reference<decltype(*begin)>::type> { |
| 77 |
return {begin, end}; |
| 78 |
} |
| 79 |
|
| 80 |
template<typename It> |
| 81 |
auto make_span(It begin, std::size_t count) -> span<typename std::remove_reference<decltype(*begin)>::type> { |
| 82 |
return {begin, count}; |
| 83 |
} |
| 84 |
|
| 85 |
template< |
| 86 |
typename T, |
| 87 |
typename std::enable_if< |
| 88 |
std::is_standard_layout<T>::value && is_trivially_copyable<T>::value && !is_span<T>::value, |
| 89 |
int |
| 90 |
>::type = 0 |
| 91 |
> |
| 92 |
span<char> make_bspan(T& object) { |
| 93 |
return span<char>(reinterpret_cast<char*>(std::addressof(object)), sizeof(object)); |
| 94 |
} |
| 95 |
} |
| 96 |
CPPTRACE_END_NAMESPACE |
| 97 |
|
| 98 |
#endif |