1
0
mirror of https://github.com/sendyne/cppreg.git synced 2025-05-09 23:24:05 +00:00

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

View File

@ -176,26 +176,47 @@ namespace cppreg {
//! Loop method. //! 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). * This will call Op for the range [start, end).
*/ */
template <typename Op, typename T = void> template <typename Func>
inline static void loop( inline static void loop() {
typename std::enable_if<start < end, T>::type* = nullptr Func().template operator()<start>();
) {
Op().template operator()<start>();
if (start < end) if (start < end)
for_loop<start + 1, end>::template loop<Op>(); for_loop<start + 1, end>::template loop<Func>();
}; };
//! Loop method closure. #if __cplusplus >= 201402L
template <typename Op, typename T = void> //! Apply method.
inline static void loop( /**
typename std::enable_if<start >= end, T>::type* = nullptr * @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. //! Template range loop implementation.