Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding strong type assertions to AoSoA #203

Merged
merged 2 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/valied/valid/

Also from that description I cannot tell how this differs from is_member_type

*/
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" );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only built-in types will have a specialization of is_arithmetic with a value that is true and all are trivial as far as I know.


// 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