AsmGrader 0.0.0
Loading...
Searching...
No Matches
byte_array.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <gsl/assert>
4#include <range/v3/algorithm/copy.hpp>
5#include <range/v3/algorithm/transform.hpp>
6#include <range/v3/iterator/traits.hpp>
7#include <range/v3/range/access.hpp>
8#include <range/v3/range/concepts.hpp>
9#include <range/v3/range/primitives.hpp>
10#include <range/v3/range/traits.hpp>
11
12#include <array>
13#include <cstddef>
14#include <memory>
15#include <span>
16#include <type_traits>
17#include <utility>
18
19namespace asmgrader {
20
21template <std::size_t Size>
23{
24public:
25 // A bunch of aliases for use with algorithm templates that check for them
26 using value_type = std::byte;
27 using allocator_type = std::allocator<std::byte>;
28 using size_type = std::size_t;
29 using difference_type = std::ptrdiff_t;
33 using const_pointer = const value_type*;
34 using iterator = std::array<std::byte, Size>::iterator;
35 using const_iterator = std::array<std::byte, Size>::const_iterator;
36 using reverse_iterator = std::array<std::byte, Size>::reverse_iterator;
37 using const_reverse_iterator = std::array<std::byte, Size>::const_reverse_iterator;
38
39 // // Conversion constructor from other containers holding "byte-like" objects (char, unsigned char)
40 // template <typename T>
41 // requires requires(T::value_type value) {
42 // { static_cast<std::byte>(value) };
43 // }
44 // explicit constexpr from(const T& container)
45 // : ByteArray{container} {
46 // static_assert(container.size() <= Size, "Container is to large to fit into this ByteArray");
47 // }
48 //
49 // template <ranges::range Range>
50 // explicit constexpr ByteArray(Range&& range) {
51 // Expects(range.size() <= Size);
52 //
53 // from_range(std::forward<Range>(range));
54 // }
55 //
56
57 auto begin() { return data.begin(); }
58
59 auto begin() const { return data.begin(); }
60
61 auto cbegin() const { return data.cend(); }
62
63 auto end() { return data.end(); }
64
65 auto end() const { return data.end(); }
66
67 auto cend() const { return data.cend(); }
68
69 std::size_t size() const { return data.size(); }
70
73 template <typename T>
74 requires requires(T rng, std::size_t size, std::byte byte) {
75 { rng.resize(size) };
76 { std::to_integer<T::value_type>(byte) };
77 }
78 constexpr T to() const {
79 T result;
80 result.resize(size());
81
82 ranges::transform(*this, result.begin(), [](std::byte value) { return std::to_integer<T::value_type>(value); });
83
84 return result;
85 }
86
87 // template <typename T>
88 // requires requires(T rng, std::size_t size, std::byte byte) {
89 // { rng.resize(size) };
90 // { static_cast<T::value_type>(byte) };
91 // }
92 // constexpr T to() const {
93 // T result;
94 // result.resize(this->size());
95 //
96 // ranges::transform(*this, result.begin(), [](std::byte value) { return static_cast<T::value_type>(value); });
97 //
98 // return result;
99 // }
100
101 template <ranges::range Range>
102 static constexpr ByteArray bit_cast_range(Range&& range) {
103 auto raw_bytes = std::as_bytes(
104 std::span{ranges::begin(std::forward<Range>(range)), ranges::end(std::forward<Range>(range))});
105
106 ByteArray<Size> res{};
107 res.from_range(raw_bytes);
108 return res;
109 }
110
111 template <typename... Ts>
112 static constexpr ByteArray bit_cast(const Ts&... args) {
113 ByteArray result{};
114
115 auto it = ranges::begin(result);
116
117 (ranges::copy(std::bit_cast<std::array<std::byte, sizeof(Ts)>>(args), std::exchange(it, it + sizeof(Ts))), ...);
118
119 return result;
120 }
121
122 std::array<std::byte, Size> data;
123
124private:
125 template <ranges::range Range>
126 requires requires(ranges::range_value_t<Range> value) {
127 { std::byte{value} };
128 }
129 constexpr void from_range(Range&& range) {
130 // static_assert(get_size_if_constexpr(range) <= Size, "Passed range is too large for this ByteArray");
131 Expects(ranges::size(range) < Size);
132 ranges::transform(std::forward<Range>(range), data.begin(), [](std::byte value) { return std::byte{value}; });
133 }
134
135 static constexpr auto TRY_CONSTEXPR = []<std::size_t>() {};
136
137 // template <ranges::range Range>
138 // requires requires { TRY_CONSTEXPR.template operator()<ranges::size(Range)>(); }
139 // static constexpr std::size_t get_size_if_constexpr(const Range& range) {
140 // constexpr std::size_t NOT_CONSTEXPR = std::numeric_limits<std::size_t>::max();
141 //
142 // if constexpr () {
143 // return ranges::size(range);
144 // }
145 //
146 // return NOT_CONSTEXPR;
147 // }
148};
149
150static_assert(std::is_aggregate_v<ByteArray<1>>);
151
152} // namespace asmgrader
Definition byte_array.hpp:23
std::allocator< std::byte > allocator_type
Definition byte_array.hpp:27
auto begin()
Definition byte_array.hpp:57
constexpr T to() const
T should be a stdlib-compatible container type where std::byte is convertible to T::value_type.
Definition byte_array.hpp:78
static constexpr ByteArray bit_cast(const Ts &... args)
Definition byte_array.hpp:112
std::array< std::byte, Size >::const_iterator const_iterator
Definition byte_array.hpp:35
auto end()
Definition byte_array.hpp:63
std::array< std::byte, Size >::reverse_iterator reverse_iterator
Definition byte_array.hpp:36
std::ptrdiff_t difference_type
Definition byte_array.hpp:29
std::size_t size() const
Definition byte_array.hpp:69
std::array< std::byte, Size > data
Definition byte_array.hpp:122
value_type * pointer
Definition byte_array.hpp:32
auto cbegin() const
Definition byte_array.hpp:61
std::array< std::byte, Size >::const_reverse_iterator const_reverse_iterator
Definition byte_array.hpp:37
value_type & reference
Definition byte_array.hpp:30
auto begin() const
Definition byte_array.hpp:59
const value_type & const_reference
Definition byte_array.hpp:31
auto cend() const
Definition byte_array.hpp:67
const value_type * const_pointer
Definition byte_array.hpp:33
std::array< std::byte, Size >::iterator iterator
Definition byte_array.hpp:34
auto end() const
Definition byte_array.hpp:65
std::byte value_type
Definition byte_array.hpp:26
static constexpr ByteArray bit_cast_range(Range &&range)
Definition byte_array.hpp:102
std::size_t size_type
Definition byte_array.hpp:28
Definition asm_buffer.hpp:19