I have a variadic template class "InterleavedAttribute" and now I want to want some runtime information ("AttributeTypeInfo") about the types in the template. I wanted to fill a static const array in the template class ("attributeInfo") and later access that array using an index for the individual information at runtime, like object.typeInfo(2), but I can not figure out how. Here's what I have:
#include <iostream>
template <typename TYPE> struct GLTypeInfo;
template <>
struct GLTypeInfo<char> { static constexpr unsigned int glType = 0; };
template <>
struct GLTypeInfo<int> { static constexpr unsigned int glType = 1; };
template <>
struct GLTypeInfo<float> { static constexpr unsigned int glType = 2; };
//---------------------------------------------------------------------------
struct AttributeTypeInfo
{
unsigned int nrOfComponents;
unsigned int glType;
unsigned int bytesPerComponent;
unsigned int bytesPerAttribute;
constexpr AttributeTypeInfo(unsigned int _nrOfComponents, unsigned int _glType, unsigned int _bytesPerComponent, unsigned int _bytesPerAttribute)
: nrOfComponents(_nrOfComponents), glType(_glType), bytesPerComponent(_bytesPerComponent), bytesPerAttribute(_bytesPerAttribute)
{}
};
template <typename T>
struct TypeInfoFactory
{
static unsigned int constexpr extent = std::is_array<T>::value ? std::extent<T>::value : 1;
using type = typename std::conditional<std::is_array<T>::value, typename std::remove_extent<T>::type, T>::type;
static constexpr AttributeTypeInfo typeInfo = AttributeTypeInfo(extent, GLTypeInfo<type>::glType, sizeof(type), sizeof(type) * extent);
};
//---------------------------------------------------------------------------
template <typename TYPE, typename ...P> struct InterleavedAttribute
{
static constexpr unsigned int nrOfAttributes = sizeof...(P)+1;
//static constexpr AttributeTypeInfo attributeInfo[sizeof...(P)+1] = { ??? }; // <-- How do I fill this...
TYPE data;
InterleavedAttribute<P...> next;
InterleavedAttribute() {}
InterleavedAttribute(const TYPE & value, const P &... p) : data(value), next(p...) {}
//static constexpr AttributeTypeInfo typeInfo(unsigned int index) { return attributeInfo[index]; } // ...so I can call this later?
};
template <typename TYPE> struct InterleavedAttribute<TYPE>
{
static constexpr unsigned int nrOfAttributes = 1;
static constexpr AttributeTypeInfo attributeInfo[1] = { TypeInfoFactory<TYPE>::typeInfo };
TYPE data;
InterleavedAttribute() {}
InterleavedAttribute(const TYPE & value) : data(value) {}
static constexpr AttributeTypeInfo typeInfo(unsigned int index) { return attributeInfo[0]; }
};
int main() {
InterleavedAttribute<int> ia1(5);
std::cout << "Numer of attributes: " << ia1.nrOfAttributes << std::endl;
std::cout << "Attribute 0, glType: " << ia1.typeInfo(0).glType << std::endl;
InterleavedAttribute<float, int, char> ia3(1.2, 3, 'a');
std::cout << "Numer of attributes: " << ia3.nrOfAttributes << std::endl;
//std::cout << "Attribute 0, glType: " << ia3.typeInfo(0).glType << std::endl; <-- Trying to get type information here
}
The code is on Coliru here. I've found this answer, and it is close, but I still can not really figure out how to get what I want...