AsmGrader 0.0.0
Loading...
Searching...
No Matches
to_static_range.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <libassert/assert.hpp>
6#include <range/v3/algorithm/copy.hpp>
7#include <range/v3/iterator/access.hpp>
8#include <range/v3/range/access.hpp>
9#include <range/v3/range/concepts.hpp>
10#include <range/v3/range/conversion.hpp>
11#include <range/v3/range/primitives.hpp>
12#include <range/v3/range/traits.hpp>
13#include <range/v3/view/view.hpp>
14
15#include <array>
16#include <cstddef>
17#include <utility>
18
19namespace asmgrader {
20
23template <ranges::range Container>
24 requires(HasStaticSize<Container>)
26{
27 template <ranges::input_range Rng>
28 requires(ranges::sized_range<Rng>)
29 constexpr auto operator()(Rng&& rng) const {
30 // Ensure the range has exactly N elements in debug builds
31 DEBUG_ASSERT(ranges::size(rng) == get_static_size<Container>());
32 // In non-debug builds, be more lax and check that the container can fit within N elements
33 ASSERT(ranges::size(rng) <= get_static_size<Container>());
34
35 Container result{};
36 ranges::copy(std::forward<Rng>(rng), result.begin());
37 return result;
38 }
39};
40
41template <std::size_t N, template <typename, std::size_t> typename ArrayLike>
43{
44 template <ranges::input_range Rng>
45 requires(ranges::sized_range<Rng>)
46 constexpr auto operator()(Rng&& rng) const {
47 using ContainedT = ranges::range_value_t<Rng>;
48
49 using ContainerT = ArrayLike<ContainedT, N>;
50
51 return to_static_container_fn<ContainerT>{}(std::forward<Rng>(rng));
52 }
53};
54
57template <ranges::range StaticallySizedContainer>
58 requires(HasStaticSize<StaticallySizedContainer>)
59constexpr auto static_to() {
60 return ranges::views::view_closure<to_static_container_fn<StaticallySizedContainer>>{};
61};
62
65template <std::size_t N, template <typename, std::size_t> typename ArrayLike>
66constexpr auto static_to() {
67 return ranges::views::view_closure<to_array_like_fn<N, ArrayLike>>{};
68};
69
71template <std::size_t N>
72constexpr auto to_array() {
74};
75
76namespace detail {
77
78template <ranges::range Container>
80
81template <ranges::range Container>
84{
85 using type = decltype(static_to<Container>());
86};
87
88template <ranges::range Container>
91{
92 using type = decltype(ranges::to<Container>());
93};
94
95} // namespace detail
96
99template <ranges::range Container>
100constexpr auto maybe_static_to() {
102}
103
104} // namespace asmgrader
Definition static_size.hpp:13
Definition asm_buffer.hpp:20
consteval std::size_t get_static_size()
Definition static_size.hpp:19
constexpr auto maybe_static_to()
A pipeable adaptor to convert to any container, statically or dynamically sized. Internally uses stat...
Definition to_static_range.hpp:100
constexpr auto to_array()
A pipeable adaptor to a std::array.
Definition to_static_range.hpp:72
constexpr auto static_to()
A pipeable adaptor to convert to any static container. Just like ranges::to, but statically sized.
Definition to_static_range.hpp:59
decltype(static_to< Container >()) type
Definition to_static_range.hpp:85
Definition to_static_range.hpp:79
Definition to_static_range.hpp:43
Range adaptor function to convert a range to a static (sized at compile time) container,...
Definition to_static_range.hpp:26