types/description: add support for coordinate types

This commit is contained in:
Danny Robson 2019-05-17 10:55:49 +10:00
parent 50f38c21af
commit 6bc13544dd
3 changed files with 86 additions and 7 deletions

View File

@ -3,11 +3,10 @@
* 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 2012-2017 Danny Robson <danny@nerdcruft.net>
* Copyright 2012-2019 Danny Robson <danny@nerdcruft.net>
*/
#ifndef CRUFT_UTIL_COORD_TRAITS_HPP
#define CRUFT_UTIL_COORD_TRAITS_HPP
#pragma once
#include "fwd.hpp"
@ -197,12 +196,14 @@ namespace cruft {
template <typename,typename=void> struct arity {};
//-------------------------------------------------------------------------
template <typename T>
struct arity<T,std::enable_if_t<std::is_arithmetic_v<T>,void>
> :std::integral_constant<std::size_t, 1>
{ };
//-------------------------------------------------------------------------
template <typename T>
struct arity<
T,std::enable_if_t<is_coord_v<T>,void>
@ -210,9 +211,46 @@ namespace cruft {
{ };
///////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------
template <typename T>
constexpr auto arity_v = arity<T>::value;
///////////////////////////////////////////////////////////////////////////
template <typename, typename = void> struct value_trait {};
//-------------------------------------------------------------------------
template <size_t S, typename T>
struct value_trait<cruft::point<S,T>>
{ using value_type = T; };
//-------------------------------------------------------------------------
template <typename OuterT>
using value_trait_t = typename value_trait<OuterT>::value_type;
}
#endif
#include "../types/description.hpp"
namespace cruft::types {
template <typename CoordT>
struct arity_trait<
CoordT,
std::enable_if_t<
::cruft::is_coord_v<CoordT>
>
> : public ::cruft::arity<CoordT> { };
template <typename CoordT>
struct category_traits<
CoordT,
std::enable_if_t<
::cruft::is_coord_v<CoordT>
>
> : public category_traits<
::cruft::value_trait_t<CoordT>
> { };
}

View File

@ -130,3 +130,22 @@ static auto indices (ContainerT const &obj)
return view {obj};
}
#include "types/description.hpp"
namespace cruft::types {
template <typename TagT, typename ValueT>
struct arity_trait<
::cruft::strongdef::index<TagT, ValueT>
> :
arity_trait<ValueT>
{ ; };
template <typename TagT, typename ValueT>
struct category_traits<
::cruft::strongdef::index<TagT,ValueT>
> :
public category_traits<ValueT>
{};
}

View File

@ -10,7 +10,7 @@
#include "../std.hpp"
#include "../types.hpp"
#include "../debug.hpp"
#include "../debug/panic.hpp"
#include <cstddef>
#include <type_traits>
@ -43,6 +43,17 @@ namespace cruft::types {
> { };
//-------------------------------------------------------------------------
// Punt the handling of any enum to the underlying type.
template <typename EnumT>
struct arity_trait<
EnumT,
std::enable_if_t<std::is_enum_v<EnumT>>
> : public arity_trait<
std::underlying_type_t<EnumT>
> { };
//-------------------------------------------------------------------------
template <typename T>
constexpr auto arity_v = arity_trait<T>::value;
@ -99,7 +110,7 @@ namespace cruft::types {
//-------------------------------------------------------------------------
template <typename T>
template <typename T, typename = void>
struct category_traits;
template <> struct category_traits<u08> : public std::integral_constant<category,category::UNSIGNED> {};
@ -117,6 +128,17 @@ namespace cruft::types {
template <> struct category_traits<bool> : public std::integral_constant<category,category::BOOL> {};
template <typename EnumT>
struct category_traits<
EnumT,
std::enable_if_t<
std::is_enum_v<EnumT>
>
> : public category_traits<std::underlying_type_t<EnumT>>
{ };
template <typename T>
constexpr auto category_traits_v = category_traits<T>::value;