ADD MORE FUNCTIONALITY TO THE TEMPLATE FOR LOOP IMPLEMENTATION

This adds a C++14 apply method that can be used with polymorphic lambdas.
This commit is contained in:
Nicolas Clauvelin 2018-03-13 17:29:15 -04:00
parent 8ce22c666d
commit 7b9a843e2d
1 changed files with 33 additions and 12 deletions

View File

@ -176,26 +176,47 @@ namespace cppreg {
//! Loop method.
/**
* @tparam Op Operation to be called at each iteration.
* @tparam Func Function to be called at each iteration.
*
* This will call Op for the range [start, end).
*/
template <typename Op, typename T = void>
inline static void loop(
typename std::enable_if<start < end, T>::type* = nullptr
) {
Op().template operator()<start>();
template <typename Func>
inline static void loop() {
Func().template operator()<start>();
if (start < end)
for_loop<start + 1, end>::template loop<Op>();
for_loop<start + 1, end>::template loop<Func>();
};
//! Loop method closure.
template <typename Op, typename T = void>
inline static void loop(
typename std::enable_if<start >= end, T>::type* = nullptr
) {};
#if __cplusplus >= 201402L
//! Apply method.
/**
* @tparam Op Operator type to be called.
*
* This is only available with C++14 and up as this requires polymorphic
* lambdas to be used in a somewhat useful manner.
*
* Typical example:
* use lambda [](auto index) { index.value will be the loop index};
*/
template <typename Op>
inline static void apply(Op& f) {
if (start < end) {
f(std::integral_constant<std::size_t, start>{});
for_loop<start + 1, end>::apply(f);
};
};
#endif // __cplusplus 201402L
};
template <std::size_t end>
struct for_loop<end, end> {
template <typename Func>
inline static void loop() {};
#if __cplusplus >= 201402L
template <typename Op>
inline static void apply(Op& f) {};
#endif // __cplusplus 201402L
};
//! Template range loop implementation.