types/description: add visit dispatcher for descriptions

This commit is contained in:
Danny Robson 2019-03-16 18:19:12 +11:00
parent 6bf1853637
commit 28e0a1afea

View File

@ -9,9 +9,14 @@
#pragma once #pragma once
#include "../std.hpp" #include "../std.hpp"
#include "../types.hpp"
#include "../debug.hpp"
#include <cstddef> #include <cstddef>
#include <type_traits> #include <type_traits>
#include <functional>
#include <utility>
namespace cruft::types { namespace cruft::types {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
@ -24,7 +29,10 @@ namespace cruft::types {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
struct description { struct description {
/// The signed, realness, or voidness of the type.
enum category category; enum category category;
/// The number of bytes for an individual instance of this type.
std::size_t width; std::size_t width;
}; };
@ -60,6 +68,93 @@ namespace cruft::types {
.width = sizeof (T) .width = sizeof (T)
}; };
} }
/// Call a functor with the supplied arguments and a type_tag for the
/// described native type.
///
/// If the type does not exist then throw and invalid_argument exception.
template <typename FunctionT, typename ...Args>
decltype(auto)
visit (description const &descriminator, FunctionT &&func, Args&&...args)
{
switch (descriminator.category) {
case category ::NONE:
return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<void> {}
);
case category::REAL:
switch (descriminator.width) {
case 4: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<f32> {}
);
case 8: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<f64> {}
);
default:
throw std::invalid_argument ("Unsupported floating point width");
}
case category::SIGNED:
switch (descriminator.width) {
case 1: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<i08> {}
);
case 2: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<i16> {}
);
case 4: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<i32> {}
);
case 8: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<i64> {}
);
default:
throw std::invalid_argument ("Unsupported unsigned width");
}
case category::UNSIGNED:
switch (descriminator.width) {
case 1: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<u08> {}
);
case 2: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<u16> {}
);
case 4: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<u32> {}
);
case 8: return std::invoke (
std::forward<FunctionT> (func),
std::forward<Args> (args)...,
type_tag<u64> {}
);
}
}
unhandled (descriminator.category);
}
} }
#include <iosfwd> #include <iosfwd>