Created
April 15, 2020 20:42
-
-
Save jnbrunet/32028bb3b7766f4fa115f163a573cd1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <tuple> | |
template <typename T> | |
struct NameDecoder { | |
static std::string GetDefaultTemplateName() { return "rien"; } | |
}; | |
template<template <typename, typename...> class C, typename T1, typename ...Ts> | |
struct NameDecoder<C<T1, Ts...>> | |
{ | |
private: | |
template<int N> using NthType = | |
typename std::tuple_element<N, std::tuple<Ts...>>::type; | |
public: | |
static std::string GetDefaultTemplateName() { | |
if constexpr (sizeof...(Ts) == 0) { | |
return T1::getName(); | |
} else if constexpr (sizeof...(Ts) == 1) { | |
return T1::getName()+","+NthType<0>::getName(); | |
} else if constexpr (sizeof...(Ts) == 2) { | |
return T1::getName()+","+NthType<0>::getName()+","+NthType<1>::getName(); | |
} else if constexpr (sizeof...(Ts) == 3) { | |
return T1::getName()+","+NthType<0>::getName()+","+NthType<1>::getName()+NthType<2>::getName(); | |
} | |
} | |
}; | |
#define SOFA_TEMPLATE(Class,P1) Class<P1> | |
#define SOFA_TEMPLATE2(Class,P1,P2) Class<P1,P2> | |
#define SOFA_TEMPLATE3(Class,P1,P2,P3) Class<P1,P2,P3> | |
#define SOFA_TEMPLATE4(Class,P1,P2,P3,P4) Class<P1,P2,P3,P4> | |
#define SOFA_CLASS(Class) \ | |
typedef Class MyType; \ | |
public: \ | |
static std::string DefaultTemplateName() { return NameDecoder<MyType>::GetDefaultTemplateName(); } \ | |
std::string getTemplateName(){ return DefaultTemplateName(); } | |
class Class | |
{ | |
SOFA_CLASS(Class) | |
}; | |
template<class DataType> | |
class TemplatedClass | |
{ | |
SOFA_CLASS(SOFA_TEMPLATE(TemplatedClass, DataType)) | |
}; | |
template<class DataType, class DataType2> | |
class TwoTemplatedClass | |
{ | |
SOFA_CLASS(SOFA_TEMPLATE2(TwoTemplatedClass, DataType, DataType2)) | |
}; | |
class ParamF | |
{ | |
public: | |
static std::string getName(){ return "float"; } | |
}; | |
class ParamI | |
{ | |
public: | |
static std::string getName(){ return "int"; } | |
}; | |
int main(int argc, char** argv) | |
{ | |
Class class1; | |
TemplatedClass<ParamF> class2; | |
TemplatedClass<ParamI> class3; | |
TwoTemplatedClass<ParamI, ParamF> class4; | |
std::cout << "class1: " << class1.getTemplateName() << std::endl; | |
std::cout << "class2: " << class2.getTemplateName() << std::endl; | |
std::cout << "class3: " << class3.getTemplateName() << std::endl; | |
std::cout << "class4: " << class4.getTemplateName() << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment