Loading...
Searching...
No Matches
constfor.h
Go to the documentation of this file.
1#ifndef _BBM_CONSTFOR_H_
2#define _BBM_CONSTFOR_H_
3
4/***********************************************************************/
5/*! \file constfor.h
6 \brief Complile-time for loop
7
8 Expects the body to be passed as a templated lambda that takes a size_t
9 literal as template parameter, and no arguments.
10
11 Example:
12 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
13 constfor<4>( []<size_t IDX>()
14 {
15 std::cerr << IDX << std::endl;
16 });
17 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 Prints values from 0 to 3.
19
20 A helper macro simplifies the call:
21 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
22 CONSTFOR(IDX, 4,
23 {
24 std::cerr << IDX << std::endl;
25 });
26 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27
28*************************************************************************/
29
30namespace bbm {
31
32 /********************************************************************/
33 /*! \brief Concept to check if a lambda function meets the required
34 signature
35 *********************************************************************/
36 template<typename F>
37 concept has_constfor_lambda = requires(F&& f) {
38 f.template operator()<size_t(0)>();
39 };
40
41 /*********************************************************************/
42 /*! \brief constfor given an index sequence of indexes.
43 *********************************************************************/
44 template<typename F, size_t... IDX> requires (sizeof...(IDX) == 0 || has_constfor_lambda<F>)
45 inline constexpr void constfor(F&& f, std::index_sequence<IDX...>)
46 {
47 (f.template operator()<IDX>(), ...);
48 }
49
50 /*********************************************************************/
51 /*! \brief constfor given the number of iterations
52 *********************************************************************/
53 template<size_t NumItr, typename F> requires (NumItr == 0 || has_constfor_lambda<F>)
54 inline constexpr void constfor(F&& f)
55 {
56 constfor(std::forward<F>(f), std::make_index_sequence<NumItr>{});
57 }
58
59
60 /*********************************************************************/
61 /*! \brief HELPER MACRO
62 *********************************************************************/
63#define CONSTFOR(IDX, NUMITR, ...) bbm::constfor<NUMITR>( [&]<size_t IDX>() { __VA_ARGS__; } );
64
65} // end bbm namespace
66
67
68#endif /* _BBM_CONSTFOR_H_ */
Concept to check if a lambda function meets the required signature.
Definition: constfor.h:37
Definition: aggregatebsdf.h:29
constexpr void constfor(F &&f, std::index_sequence< IDX... >)
constfor given an index sequence of indexes.
Definition: constfor.h:45