forked from jeremy-rifkin/cpptrace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspan.hpp
More file actions
98 lines (83 loc) · 2.7 KB
/
span.hpp
File metadata and controls
98 lines (83 loc) · 2.7 KB
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
#ifndef SPAN_HPP
#define SPAN_HPP
#include "utils/utils.hpp"
#include <cstddef>
#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>
CPPTRACE_BEGIN_NAMESPACE
namespace detail {
// basic span implementation
// I haven't implemented most members because I don't need them, more will be added as needed
template<typename T>
class span {
T* ptr;
std::size_t count;
public:
using element_type = T;
using value_type = typename std::remove_cv<T>::type;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = T*;
using const_iterator = const T*;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<reverse_iterator>;
using i_am_span = void;
span() : ptr(nullptr), count(0) {}
span(T* ptr, std::size_t count) : ptr(ptr), count(count) {}
template<typename It>
span(It begin, It end) : ptr(std::addressof(*begin)), count(end - begin) {}
T* data() const noexcept {
return ptr;
}
std::size_t size() const noexcept {
return count;
}
bool empty() const noexcept {
return count == 0;
}
iterator begin() noexcept {
return ptr;
}
iterator end() noexcept {
return ptr + count;
}
const_iterator begin() const noexcept {
return ptr;
}
const_iterator end() const noexcept {
return ptr + count;
}
};
using bspan = span<char>;
using cbspan = span<const char>;
template<typename T, typename = int>
struct is_span : std::false_type {};
template<typename T>
struct is_span<T, void_t<typename T::i_am_span>> : std::true_type {};
template<typename It>
auto make_span(It begin, It end) -> span<typename std::remove_reference<decltype(*begin)>::type> {
return {begin, end};
}
template<typename It>
auto make_span(It begin, std::size_t count) -> span<typename std::remove_reference<decltype(*begin)>::type> {
return {begin, count};
}
template<
typename T,
typename std::enable_if<
std::is_standard_layout<T>::value && is_trivially_copyable<T>::value && !is_span<T>::value,
int
>::type = 0
>
span<char> make_bspan(T& object) {
return span<char>(reinterpret_cast<char*>(std::addressof(object)), sizeof(object));
}
}
CPPTRACE_END_NAMESPACE
#endif