2019-05-30 10:43:28 +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-2019 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
2019-06-04 15:42:02 +10:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-30 10:43:28 +10:00
|
|
|
#include "description.hpp"
|
|
|
|
#include "../types.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
namespace cruft::types {
|
|
|
|
/// 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.
|
|
|
|
///
|
|
|
|
/// We split the calls into two here so that there's no possibility of
|
|
|
|
/// recursion (which will blow up the compiler) when we handle the
|
|
|
|
/// dispatch of enums.
|
|
|
|
template <typename FunctionT, typename ...Args>
|
|
|
|
decltype(auto)
|
|
|
|
visit (description const &descriminator, FunctionT &&func, Args&&...args)
|
|
|
|
{
|
2019-05-30 10:51:49 +10:00
|
|
|
#define INVOKE(TAG) \
|
2019-05-30 10:43:28 +10:00
|
|
|
return std::invoke( \
|
|
|
|
std::forward<FunctionT> (func), \
|
|
|
|
std::forward<Args> (args)..., \
|
2019-05-30 10:51:49 +10:00
|
|
|
TAG \
|
2019-05-30 10:43:28 +10:00
|
|
|
);
|
|
|
|
|
|
|
|
switch (descriminator.category) {
|
2019-05-30 10:51:49 +10:00
|
|
|
case category ::NONE: INVOKE(type_tag<void> {})
|
2019-05-30 10:43:28 +10:00
|
|
|
case category::REAL:
|
|
|
|
switch (descriminator.width) {
|
2019-05-30 10:51:49 +10:00
|
|
|
case 4: INVOKE(type_tag<f32> {})
|
|
|
|
case 8: INVOKE(type_tag<f64> {})
|
2019-05-30 10:43:28 +10:00
|
|
|
default:
|
|
|
|
throw std::invalid_argument ("Unsupported floating point width");
|
|
|
|
}
|
|
|
|
|
2019-05-30 10:51:49 +10:00
|
|
|
case category::BOOL: INVOKE(type_tag<bool> {})
|
2019-05-30 10:43:28 +10:00
|
|
|
|
|
|
|
case category::ENUM: {
|
|
|
|
if (descriminator.signedness) {
|
|
|
|
switch (descriminator.width) {
|
2019-05-30 10:51:49 +10:00
|
|
|
case 1: INVOKE(type_tag<unknown_enum_tag<i08>> { descriminator })
|
|
|
|
case 2: INVOKE(type_tag<unknown_enum_tag<i16>> { descriminator })
|
2019-05-30 13:17:10 +10:00
|
|
|
case 4: INVOKE(type_tag<unknown_enum_tag<i32>> { descriminator })
|
|
|
|
case 8: INVOKE(type_tag<unknown_enum_tag<i64>> { descriminator })
|
2019-05-30 10:43:28 +10:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (descriminator.width) {
|
2019-05-30 10:51:49 +10:00
|
|
|
case 1: INVOKE(type_tag<unknown_enum_tag<u08>> { descriminator })
|
|
|
|
case 2: INVOKE(type_tag<unknown_enum_tag<u16>> { descriminator })
|
2019-05-30 13:17:10 +10:00
|
|
|
case 4: INVOKE(type_tag<unknown_enum_tag<u32>> { descriminator })
|
|
|
|
case 8: INVOKE(type_tag<unknown_enum_tag<u64>> { descriminator })
|
2019-05-30 10:43:28 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case category::INTEGER:
|
|
|
|
CHECK_EQ (category::INTEGER, descriminator.category);
|
|
|
|
|
|
|
|
if (descriminator.signedness) {
|
|
|
|
switch (descriminator.width) {
|
2019-05-30 10:51:49 +10:00
|
|
|
case 1: INVOKE(type_tag<i08> {})
|
|
|
|
case 2: INVOKE(type_tag<i16> {})
|
2019-05-30 13:17:10 +10:00
|
|
|
case 4: INVOKE(type_tag<i32> {})
|
|
|
|
case 8: INVOKE(type_tag<i64> {})
|
2019-05-30 10:43:28 +10:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (descriminator.width) {
|
2019-05-30 10:51:49 +10:00
|
|
|
case 1: INVOKE(type_tag<u08> {})
|
|
|
|
case 2: INVOKE(type_tag<u16> {})
|
2019-05-30 13:17:10 +10:00
|
|
|
case 4: INVOKE(type_tag<u32> {})
|
|
|
|
case 8: INVOKE(type_tag<u64> {})
|
2019-05-30 10:43:28 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unhandled (descriminator.category);
|
|
|
|
}
|
|
|
|
|
|
|
|
unhandled (descriminator.category);
|
|
|
|
#undef INVOKE
|
|
|
|
}
|
|
|
|
}
|