Skip to content

Commit

Permalink
Refactor asTupleImpl and asFlatTupleImpl
Browse files Browse the repository at this point in the history
This should improve readability and make the return type more clearly visible.
  • Loading branch information
bernhardmgruber committed Sep 14, 2022
1 parent a6a4da3 commit 1158bc2
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions include/llama/RecordRef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,30 +195,35 @@ namespace llama
}
};

template<typename ProxyReference, typename T>
LLAMA_FN_HOST_ACC_INLINE auto asTupleImpl(ProxyReference&& leaf, T)
-> std::enable_if_t<!isRecordRef<std::decay_t<ProxyReference>>, ProxyReference>
template<
typename ProxyReference,
typename T,
std::enable_if_t<!isRecordRef<std::decay_t<ProxyReference>>, int> = 0>
LLAMA_FN_HOST_ACC_INLINE auto asTupleImpl(ProxyReference&& leaf, T) -> ProxyReference
{
return leaf;
}

template<typename TWithOptionalConst, typename T>
LLAMA_FN_HOST_ACC_INLINE auto asTupleImpl(TWithOptionalConst& leaf, T) -> std::
enable_if_t<!isRecordRef<std::decay_t<TWithOptionalConst>>, std::reference_wrapper<TWithOptionalConst>>
template<
typename TWithOptionalConst,
typename T,
std::enable_if_t<!isRecordRef<std::decay_t<TWithOptionalConst>>, int> = 0>
LLAMA_FN_HOST_ACC_INLINE auto asTupleImpl(TWithOptionalConst& leaf, T)
-> std::reference_wrapper<TWithOptionalConst>
{
return leaf;
}

template<typename RecordRef, typename T, std::size_t N, std::size_t... Is>
LLAMA_FN_HOST_ACC_INLINE auto asTupleImplArr(RecordRef&& vd, T (&&)[N], std::index_sequence<Is...>)
LLAMA_FN_HOST_ACC_INLINE auto asTupleImplForArray(RecordRef&& vd, T (&&)[N], std::index_sequence<Is...>)
{
return std::make_tuple(asTupleImpl(vd(RecordCoord<Is>{}), T{})...);
}

template<typename RecordRef, typename T, std::size_t N>
LLAMA_FN_HOST_ACC_INLINE auto asTupleImpl(RecordRef&& vd, T (&&a)[N])
{
return asTupleImplArr(std::forward<RecordRef>(vd), std::move(a), std::make_index_sequence<N>{});
return asTupleImplForArray(std::forward<RecordRef>(vd), std::move(a), std::make_index_sequence<N>{});
}

template<typename RecordRef, typename... Fields>
Expand All @@ -227,30 +232,35 @@ namespace llama
return std::make_tuple(asTupleImpl(vd(GetFieldTag<Fields>{}), GetFieldType<Fields>{})...);
}

template<typename ProxyReference, typename T>
LLAMA_FN_HOST_ACC_INLINE auto asFlatTupleImpl(ProxyReference&& leaf, T)
-> std::enable_if_t<!isRecordRef<std::decay_t<ProxyReference>>, std::tuple<ProxyReference>>
template<
typename ProxyReference,
typename T,
std::enable_if_t<!isRecordRef<std::decay_t<ProxyReference>>, int> = 0>
LLAMA_FN_HOST_ACC_INLINE auto asFlatTupleImpl(ProxyReference&& leaf, T) -> std::tuple<ProxyReference>
{
return {std::move(leaf)};
static_assert(!std::is_reference_v<ProxyReference>);
return {std::move(leaf)}; // NOLINT(bugprone-move-forwarding-reference)
}

template<typename TWithOptionalConst, typename T>
LLAMA_FN_HOST_ACC_INLINE auto asFlatTupleImpl(TWithOptionalConst& leaf, T)
-> std::enable_if_t<!isRecordRef<std::decay_t<TWithOptionalConst>>, std::tuple<TWithOptionalConst&>>
template<
typename TWithOptionalConst,
typename T,
std::enable_if_t<!isRecordRef<std::decay_t<TWithOptionalConst>>, int> = 0>
LLAMA_FN_HOST_ACC_INLINE auto asFlatTupleImpl(TWithOptionalConst& leaf, T) -> std::tuple<TWithOptionalConst&>
{
return {leaf};
}

template<typename RecordRef, typename T, std::size_t N, std::size_t... Is>
LLAMA_FN_HOST_ACC_INLINE auto asFlatTupleImplArr(RecordRef&& vd, T (&&)[N], std::index_sequence<Is...>)
LLAMA_FN_HOST_ACC_INLINE auto asFlatTupleImplForArray(RecordRef&& vd, T (&&)[N], std::index_sequence<Is...>)
{
return std::tuple_cat(asFlatTupleImpl(vd(RecordCoord<Is>{}), T{})...);
}

template<typename RecordRef, typename T, std::size_t N>
LLAMA_FN_HOST_ACC_INLINE auto asFlatTupleImpl(RecordRef&& vd, T (&&a)[N])
{
return asFlatTupleImplArr(std::forward<RecordRef>(vd), std::move(a), std::make_index_sequence<N>{});
return asFlatTupleImplForArray(std::forward<RecordRef>(vd), std::move(a), std::make_index_sequence<N>{});
}

template<typename RecordRef, typename... Fields>
Expand Down Expand Up @@ -302,7 +312,7 @@ namespace llama
LLAMA_FN_HOST_ACC_INLINE auto makeFromTuple(Tuple&& src, std::index_sequence<Is...>)
{
using std::get;
return T{get<Is>(std::forward<Tuple>(src))...};
return T{get<Is>(src)...}; // no forward of src, since we call get multiple times on it
}

template<typename T, typename SFINAE, typename... Args>
Expand Down

0 comments on commit 1158bc2

Please sign in to comment.