Browse Source

Added TypeInfo and TypeOf classes to type traits.

TypeOf converts Type enum value to type, TypeInfo provides some
of the information provided by TypeTraits at runtime.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
1ff99b0413
  1. 31
      src/TypeTraits.cpp
  2. 55
      src/TypeTraits.h

31
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<TypeOf<Type::type>::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;
}
}
}

55
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<TypeOf<Type::UnsignedByte>::Type>::TextureType data;
TypeTraits<GLubyte>::TextureType data;
@endcode
*/
template<Type T> 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<TypeOf<Type::UnsignedByte>::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<Type::UnsignedByte> { typedef GLubyte Type; };
template<> struct TypeOf<Type::Byte> { typedef GLbyte Type; };
template<> struct TypeOf<Type::UnsignedShort> { typedef GLushort Type; };
template<> struct TypeOf<Type::Short> { typedef GLshort Type; };
template<> struct TypeOf<Type::UnsignedInt> { typedef GLuint Type; };
template<> struct TypeOf<Type::Int> { typedef GLint Type; };
template<> struct TypeOf<Type::Float> { typedef GLfloat Type; };
template<> struct TypeOf<Type::Double> { typedef GLdouble Type; };
template<> struct TypeTraits<GLubyte>: public Math::TypeTraits<unsigned char> {
typedef GLubyte IndexType;
typedef GLubyte TextureType;

Loading…
Cancel
Save