Commit 0a3e5716 authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Merge branch 'feature/map_data_types' into 'master'

Extend the mapDataTypes function to support constraints on the types

See merge request extensions/dune-vtk!13
parents 97d60d81 27381316
dune_add_test(SOURCES test-map-datatypes.cc
LINK_LIBRARIES dunevtk)
dune_add_test(SOURCES test-typededuction.cc dune_add_test(SOURCES test-typededuction.cc
LINK_LIBRARIES dunevtk) LINK_LIBRARIES dunevtk)
......
#include <config.h>
#include <type_traits>
#include <dune/vtk/types.hh>
int main()
{
using namespace Dune;
// unconstrained mapping of DataTypes to real type t
Vtk::mapDataTypes(Vtk::DataTypes::UINT32, [](auto t)
{
using T = typename decltype(t)::type;
VTK_ASSERT(Vtk::Map::type<T>() == Vtk::DataTypes::UINT32);
});
// constrained mapping of DataTypes to real type t
Vtk::mapDataTypes<std::is_integral>(Vtk::DataTypes::UINT32, [](auto t)
{
using T = typename decltype(t)::type;
VTK_ASSERT(Vtk::Map::type<T>() == Vtk::DataTypes::UINT32);
static_assert(std::is_integral_v<T>);
});
Vtk::mapDataTypes<std::is_floating_point>(Vtk::DataTypes::FLOAT32, [](auto t)
{
using T = typename decltype(t)::type;
VTK_ASSERT(Vtk::Map::type<T>() == Vtk::DataTypes::FLOAT32);
static_assert(std::is_floating_point_v<T>);
});
// if the DataType does not fulfill the constraint, the function is never invoked
Vtk::mapDataTypes<std::is_floating_point>(Vtk::DataTypes::UINT32, [](auto t)
{
// This function should never be called
VTK_ASSERT(false);
});
// unconstrained mapping with multiple types
Vtk::mapDataTypes(Vtk::DataTypes::FLOAT32, Vtk::DataTypes::UINT32,
[](auto f, auto h)
{
using F = typename decltype(f)::type;
using H = typename decltype(h)::type;
VTK_ASSERT(Vtk::Map::type<F>() == Vtk::DataTypes::FLOAT32);
VTK_ASSERT(Vtk::Map::type<H>() == Vtk::DataTypes::UINT32);
});
// constrained mapping with multiple types
Vtk::mapDataTypes<std::is_floating_point,std::is_integral>(Vtk::DataTypes::FLOAT32, Vtk::DataTypes::UINT32,
[](auto f, auto h)
{
using F = typename decltype(f)::type;
using H = typename decltype(h)::type;
VTK_ASSERT(Vtk::Map::type<F>() == Vtk::DataTypes::FLOAT32);
VTK_ASSERT(Vtk::Map::type<H>() == Vtk::DataTypes::UINT32);
static_assert(std::is_floating_point_v<F>);
static_assert(std::is_integral_v<H>);
});
}
\ No newline at end of file
...@@ -92,22 +92,23 @@ namespace Dune ...@@ -92,22 +92,23 @@ namespace Dune
static constexpr DataTypes type () { return typeImpl(MetaType<typename FieldTraits<T>::field_type>{}); } static constexpr DataTypes type () { return typeImpl(MetaType<typename FieldTraits<T>::field_type>{}); }
}; };
template <class> struct NoConstraint : std::true_type {};
/// Map a given enum DataType to a type passed to Caller as \ref MetaType /// Map a given enum DataType to a type passed to Caller as \ref MetaType
template <class Caller> template <template <class> class C = NoConstraint, class Caller>
void mapDataTypes (Vtk::DataTypes t, Caller caller) void mapDataTypes (Vtk::DataTypes t, Caller caller)
{ {
switch (t) { switch (t) {
case INT8: caller(MetaType<std::int8_t>{}); break; case INT8: if constexpr(C<std::int8_t>::value) caller(MetaType<std::int8_t>{}); break;
case UINT8: caller(MetaType<std::uint8_t>{}); break; case UINT8: if constexpr(C<std::uint8_t>::value) caller(MetaType<std::uint8_t>{}); break;
case INT16: caller(MetaType<std::int16_t>{}); break; case INT16: if constexpr(C<std::int16_t>::value) caller(MetaType<std::int16_t>{}); break;
case UINT16: caller(MetaType<std::uint16_t>{}); break; case UINT16: if constexpr(C<std::uint16_t>::value) caller(MetaType<std::uint16_t>{}); break;
case INT32: caller(MetaType<std::int32_t>{}); break; case INT32: if constexpr(C<std::int32_t>::value) caller(MetaType<std::int32_t>{}); break;
case UINT32: caller(MetaType<std::uint32_t>{}); break; case UINT32: if constexpr(C<std::uint32_t>::value) caller(MetaType<std::uint32_t>{}); break;
case INT64: caller(MetaType<std::int64_t>{}); break; case INT64: if constexpr(C<std::int64_t>::value) caller(MetaType<std::int64_t>{}); break;
case UINT64: caller(MetaType<std::uint64_t>{}); break; case UINT64: if constexpr(C<std::uint64_t>::value) caller(MetaType<std::uint64_t>{}); break;
case FLOAT32: caller(MetaType<float>{}); break; case FLOAT32: if constexpr(C<float>::value) caller(MetaType<float>{}); break;
case FLOAT64: caller(MetaType<double>{}); break; case FLOAT64: if constexpr(C<double>::value) caller(MetaType<double>{}); break;
default: default:
VTK_ASSERT_MSG(false, "Unsupported type " + to_string(t)); VTK_ASSERT_MSG(false, "Unsupported type " + to_string(t));
break; break;
...@@ -115,23 +116,28 @@ namespace Dune ...@@ -115,23 +116,28 @@ namespace Dune
} }
/// Map two DataTypes as type parameters to the Caller /// Map two DataTypes as type parameters to the Caller
template <class Caller> template <template <class> class Constraint1 = NoConstraint,
template <class> class Constraint2 = NoConstraint,
class Caller>
void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Caller caller) void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Caller caller)
{ {
mapDataTypes(t1, [&](auto type1) { mapDataTypes<Constraint1>(t1, [&](auto type1) {
mapDataTypes(t2, [&](auto type2) { mapDataTypes<Constraint2>(t2, [&](auto type2) {
caller(type1, type2); caller(type1, type2);
}); });
}); });
} }
/// Map three DataTypes as type parameters to the Caller /// Map three DataTypes as type parameters to the Caller
template <class Caller> template <template <class> class Constraint1 = NoConstraint,
template <class> class Constraint2 = NoConstraint,
template <class> class Constraint3 = NoConstraint,
class Caller>
void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Vtk::DataTypes t3, Caller caller) void mapDataTypes (Vtk::DataTypes t1, Vtk::DataTypes t2, Vtk::DataTypes t3, Caller caller)
{ {
mapDataTypes(t1, [&](auto type1) { mapDataTypes<Constraint1>(t1, [&](auto type1) {
mapDataTypes(t2, [&](auto type2) { mapDataTypes<Constraint2>(t2, [&](auto type2) {
mapDataTypes(t3, [&](auto type3) { mapDataTypes<Constraint3>(t3, [&](auto type3) {
caller(type1, type2, type3); caller(type1, type2, type3);
}); });
}); });
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment