libcruft-util/types/description.hpp

171 lines
5.9 KiB
C++
Raw Normal View History

/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2018 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include "../std.hpp"
#include "../types.hpp"
#include "../debug.hpp"
#include <cstddef>
#include <type_traits>
#include <functional>
#include <utility>
namespace cruft::types {
//-------------------------------------------------------------------------
enum class category {
NONE,
UNSIGNED,
SIGNED,
REAL,
};
//-------------------------------------------------------------------------
struct description {
/// The signed, realness, or voidness of the type.
enum category category;
/// The number of bytes for an individual instance of this type.
std::size_t width;
};
constexpr bool
operator== (description const &a, description const &b) noexcept
{
return a.category == b.category &&
a.width == b.width;
}
//-------------------------------------------------------------------------
template <typename T>
struct category_traits;
template <> struct category_traits<u08> : public std::integral_constant<category,category::UNSIGNED> {};
template <> struct category_traits<u16> : public std::integral_constant<category,category::UNSIGNED> {};
template <> struct category_traits<u32> : public std::integral_constant<category,category::UNSIGNED> {};
template <> struct category_traits<u64> : public std::integral_constant<category,category::UNSIGNED> {};
template <> struct category_traits<i08> : public std::integral_constant<category,category::SIGNED> {};
template <> struct category_traits<i16> : public std::integral_constant<category,category::SIGNED> {};
template <> struct category_traits<i32> : public std::integral_constant<category,category::SIGNED> {};
template <> struct category_traits<i64> : public std::integral_constant<category,category::SIGNED> {};
template <> struct category_traits<f32> : public std::integral_constant<category,category::REAL> {};
template <> struct category_traits<f64> : public std::integral_constant<category,category::REAL> {};
template <typename T>
constexpr auto category_traits_v = category_traits<T>::value;
//-------------------------------------------------------------------------
template <typename T>
constexpr description
make_description (void) noexcept
{
return {
.category = category_traits_v<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>
namespace cruft::types {
std::ostream& operator<< (std::ostream&, category);
std::ostream& operator<< (std::ostream&, description);
}