diff --git a/src/TypeTraits.cpp b/src/TypeTraits.cpp index 0945f7b93..5d0663156 100644 --- a/src/TypeTraits.cpp +++ b/src/TypeTraits.cpp @@ -28,4 +28,35 @@ static_assert(sizeof(GLfloat) == sizeof(float), "GLfloat is not the same as floa static_assert(sizeof(GLdouble) == sizeof(double), "GLdouble is not the same as double"); #endif +size_t TypeInfo::sizeOf(Type type) { + switch(type) { + #define val(type) case Type::type: return TypeTraits::Type>::size(); + val(UnsignedByte) + val(Byte) + val(UnsignedShort) + val(Short) + val(UnsignedInt) + val(Int) + val(Double) + val(Float) + #undef val + + default: return 0; + } +} + +bool TypeInfo::isIntegral(Type type) { + switch(type) { + case Type::UnsignedByte: + case Type::Byte: + case Type::UnsignedShort: + case Type::Short: + case Type::UnsignedInt: + case Type::Int: + return true; + default: + return false; + } +} + } diff --git a/src/TypeTraits.h b/src/TypeTraits.h index 9c8eadff2..85b5a11bf 100644 --- a/src/TypeTraits.h +++ b/src/TypeTraits.h @@ -16,7 +16,7 @@ */ /** @file - * @brief Enum Magnum::Type, class Magnum::TypeTraits + * @brief Enum Magnum::Type, class Magnum::TypeOf, Magnum::TypeInfo, Magnum::TypeTraits */ #include "Magnum.h" @@ -89,10 +89,63 @@ enum class Type: GLenum { Double = GL_DOUBLE /**< Double */ }; +/** +@brief Class for converting Type enum values to types + +When you want to use TypeTraits on type specified only as enum value, you can +use this class to convert it into type, for example these two statements +are equivalent: +@code +TypeTraits::Type>::TextureType data; +TypeTraits::TextureType data; +@endcode +*/ +template class TypeOf { + #ifdef DOXYGEN_GENERATING_OUTPUT + typedef U Type; /**< @brief Type */ + #endif +}; + +/** +@brief Functor for runtime information about given type + +TypeTraits alone allows to get information about given type only at compile +time, this class alows to get some information also at runtime with tiny +performance loss. +*/ +struct MAGNUM_EXPORT TypeInfo { + /** + * @brief Size of given type + * + * These two lines provide the same information, one at compile time, + * one at runtime: + * @code + * size_t size = TypeTraits::size(); + * size_t size = TypeInfo::sizeOf(Type::UnsignedByte); + * @endcode + */ + static size_t sizeOf(Type type); + + /** + * @brief Whether the type is integral + * @return true for (un)signed byte, short and integer, false otherwise. + */ + static bool isIntegral(Type type); +}; + /** @todo Other texture types, referenced in glTexImage2D function manual */ /** @todo Using Vector3 for textures? */ #ifndef DOXYGEN_GENERATING_OUTPUT +template<> struct TypeOf { typedef GLubyte Type; }; +template<> struct TypeOf { typedef GLbyte Type; }; +template<> struct TypeOf { typedef GLushort Type; }; +template<> struct TypeOf { typedef GLshort Type; }; +template<> struct TypeOf { typedef GLuint Type; }; +template<> struct TypeOf { typedef GLint Type; }; +template<> struct TypeOf { typedef GLfloat Type; }; +template<> struct TypeOf { typedef GLdouble Type; }; + template<> struct TypeTraits: public Math::TypeTraits { typedef GLubyte IndexType; typedef GLubyte TextureType;