-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_time_while.hpp
68 lines (61 loc) · 1.71 KB
/
compile_time_while.hpp
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
#pragma once
#include<tuple>
#include<utility>
#include<cstddef>
#include<iostream>
namespace mlib
{
template<auto start_value, auto condition_for_val, auto operation_on_val>
struct pack
{
constexpr auto operator()()
{
if constexpr (!condition_for_val(start_value))
{
return 0;
}
else
{
return 1 + pack<operation_on_val(start_value), condition_for_val, operation_on_val>{}();
}
}
};
template<auto Care, auto DontCare>
struct any_first
{
constexpr auto operator()()
{
return Care;
}
};
template<auto start_value, auto condition_for_val, auto operation_on_val, auto operation_to_do>
struct compile_time_while
{
constexpr auto operator()()
{
// first of all, make a value that is how many times the operations are true.
constexpr auto amount_of_iterations = pack<start_value, condition_for_val, operation_on_val>{}();
// so now that we have the number of iterations, we need to make a tuple of size: amount_of_iterations
// and then we need to do a for_each on them! simple!
constexpr auto tuple_sequence = make_tuple_sequence<amount_of_iterations, operation_to_do>();
// now do for_each
for_each_lambda(tuple_sequence);
}
template<auto iterations, auto lambda>
constexpr auto make_tuple_sequence()
{
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
{
return std::tuple{ (any_first<lambda, indexes>{}())... };
}(std::make_index_sequence<iterations>{});
}
template<typename... Ts>
constexpr auto for_each_lambda(std::tuple<Ts...> tup)
{
[&] <std::size_t... indexes>(std::index_sequence<indexes...>)
{
(std::get<indexes>(tup)(), ...);
}(std::make_index_sequence<sizeof...(Ts)>{});
}
};
} // namespace mlib