Skip to content

Instantly share code, notes, and snippets.

@Korsar13
Created July 20, 2022 12:31
Show Gist options
  • Save Korsar13/5c171d0cd0b47d62276cac07b6b6a184 to your computer and use it in GitHub Desktop.
Save Korsar13/5c171d0cd0b47d62276cac07b6b6a184 to your computer and use it in GitHub Desktop.
magic_enum -- clang: OK, msvc: FAIL
#include <iostream>
#include <typeinfo>
#include "magic_enum.hpp"
enum DataID { DType1, DType2, DType3 };
template< DataID c > struct DataType;
template<> struct DataType<DType1> { using type = char; };
template<> struct DataType<DType2> { using type = int; };
template<> struct DataType<DType3> { using type = float; };
template< DataID c >
using DataType_t = typename DataType<c>::type;
template< typename > struct DataId;
template<> struct DataId< typename DataType<DType1>::type > { static inline constexpr DataID value = DType1; };
template<> struct DataId< typename DataType<DType2>::type > { static inline constexpr DataID value = DType2; };
template<> struct DataId< typename DataType<DType3>::type > { static inline constexpr DataID value = DType3; };
template< typename T >
constexpr DataID DataId_v = DataId<T>::value;
template< typename TSrc, typename TDst >
void convert_tmpl( [[maybe_unused]] const TSrc* src, [[maybe_unused]] TDst *dst, [[maybe_unused]] size_t count )
{
std::cout << "convert from " << typeid(TSrc).name() << " to " << typeid(TDst).name() << std::endl;
}
void do_convert( DataID srcId, DataID dstId, const void *src, void *dst, size_t count )
{
magic_enum::enum_switch([dstId,src,dst,count](auto valSrc){
constexpr DataID sId = valSrc;
using TypeSrc = DataType_t<sId>;
magic_enum::enum_switch([src,dst,count](auto valDst){
constexpr DataID dId = valDst;
using TypeDst = DataType_t<dId>;
convert_tmpl<TypeSrc,TypeDst>( static_cast<const TypeSrc*>(src), static_cast<TypeDst*>(dst), count );
}, dstId );
}, srcId );
}
int main()
{
magic_enum::enum_for_each<DataID>([](auto val1){
magic_enum::enum_for_each<DataID>([val1](auto val2){
do_convert( val1, val2, nullptr, nullptr, 0 );
});
});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment