Skip to content

Commit

Permalink
Merge pull request #203 from sslattery/fix_slice_check
Browse files Browse the repository at this point in the history
Adding strong type assertions to AoSoA
  • Loading branch information
sslattery authored Feb 26, 2020
2 parents 5868b59 + 987d2da commit 0f81bea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/Cabana_AoSoA.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class AoSoA
// Member data types.
static_assert( is_member_types<DataTypes>::value,
"AoSoA data types must be member types" );
static_assert( CheckMemberTypes<DataTypes>::value,
"AoSoA data type failure" );
using member_types = DataTypes;

// Device type.
Expand Down
48 changes: 48 additions & 0 deletions core/src/Cabana_MemberTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,54 @@ struct MemberTypeAtIndex<M, MemberTypes<Types...>>
using type = typename MemberTypeAtIndexImpl<M, Types...>::type;
};

//---------------------------------------------------------------------------//
/*!
\class CheckMemberTypes
\brief Check that member types are valied.
*/
template <std::size_t M, typename T, typename... Types>
struct CheckMemberTypesImpl;

template <typename T, typename... Types>
struct CheckMemberTypesImpl<0, T, Types...>
{
using type = T;
static_assert( std::is_trivial<type>::value,
"Member types must be trivial" );

using value_type = typename std::remove_all_extents<type>::type;
static_assert( std::is_arithmetic<value_type>::value,
"Member value types must be arithmetic" );

// Return true so we get the whole stack to evaluate all the assertions.
static constexpr bool value = true;
};

template <std::size_t M, typename T, typename... Types>
struct CheckMemberTypesImpl
{
using type = T;
static_assert( std::is_trivial<type>::value,
"Member types must be trivial" );

using value_type = typename std::remove_all_extents<type>::type;
static_assert( std::is_arithmetic<value_type>::value,
"Member value types must be arithmetic" );

static constexpr bool value = CheckMemberTypesImpl<M - 1, Types...>::value;
};

template <typename... Types>
struct CheckMemberTypes;

template <typename... Types>
struct CheckMemberTypes<MemberTypes<Types...>>
{
static constexpr int size = MemberTypes<Types...>::size;
static constexpr bool value =
CheckMemberTypesImpl<size - 1, Types...>::value;
};

//---------------------------------------------------------------------------//

} // end namespace Cabana
Expand Down

0 comments on commit 0f81bea

Please sign in to comment.