constructor for packets
This commit is contained in:
@@ -33,54 +33,5 @@ constexpr int get_tuple_index<T, std::tuple<T, Rest...>> = 0;
|
||||
template <typename T, typename First, typename... Rest>
|
||||
constexpr int get_tuple_index<T, std::tuple<First, Rest...>> = 1 + get_tuple_index<T, std::tuple<Rest...>>;
|
||||
|
||||
// Template black magic to loop at compile time
|
||||
template <std::size_t... indices, class LoopBody>
|
||||
void loop_impl(std::index_sequence<indices...>, LoopBody&& loop_body) {
|
||||
(loop_body(std::integral_constant<std::size_t, indices>{}), ...);
|
||||
}
|
||||
|
||||
template <std::size_t N, class LoopBody>
|
||||
void loop(LoopBody&& loop_body) {
|
||||
loop_impl(std::make_index_sequence<N>{}, std::forward<LoopBody>(loop_body));
|
||||
}
|
||||
|
||||
namespace details {
|
||||
template <std::size_t TRem>
|
||||
class TupleForEachHelper {
|
||||
public:
|
||||
template <typename TTuple, typename TFunc>
|
||||
static void exec(TTuple&& tuple, TFunc&& func) {
|
||||
using Tuple = typename std::decay<TTuple>::type;
|
||||
static const std::size_t TupleSize = std::tuple_size<Tuple>::value;
|
||||
static_assert(TRem <= TupleSize, "Incorrect parameters");
|
||||
|
||||
// Invoke function with current element
|
||||
static const std::size_t Idx = TupleSize - TRem;
|
||||
func(std::get<Idx>(std::forward<TTuple>(tuple)));
|
||||
|
||||
// Compile time recursion - invoke function with the remaining elements
|
||||
TupleForEachHelper<TRem - 1>::exec(std::forward<TTuple>(tuple), std::forward<TFunc>(func));
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
class TupleForEachHelper<0> {
|
||||
public:
|
||||
// Stop compile time recursion
|
||||
template <typename TTuple, typename TFunc>
|
||||
static void exec(TTuple&& tuple, TFunc&& func) {
|
||||
static_cast<void>(tuple);
|
||||
static_cast<void>(func);
|
||||
}
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
template <typename TTuple, typename TFunc>
|
||||
void tupleForEach(TTuple&& tuple, TFunc&& func) {
|
||||
using Tuple = typename std::decay<TTuple>::type;
|
||||
static const std::size_t TupleSize = std::tuple_size<Tuple>::value;
|
||||
|
||||
details::TupleForEachHelper<TupleSize>::exec(std::forward<TTuple>(tuple), std::forward<TFunc>(func));
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
|
||||
Reference in New Issue
Block a user