Browse Source

Improved usability of type capability checking in TypeTraits.

Checking whether given type can be used for mesh indices or image data
was always done like this, because it was also needed to get OpenGL type
ID for the type:

    TypeTraits<typename TypeTraits<T>::IndexType>::glType()

This was cumbersome, now the check is done using function, which
returns the OpenGL type ID directly:

    TypeTraits<T>::indexType()

Also replaced TextureType with imageType() and renamed glType() to just
type().
pull/279/head
Vladimír Vondruš 14 years ago
parent
commit
84e4fd1581
  1. 2
      src/BufferedImage.h
  2. 4
      src/Image.h
  3. 2
      src/Mesh.h
  4. 2
      src/MeshTools/CompressIndices.h
  5. 2
      src/Trade/ImageData.h
  6. 86
      src/TypeTraits.h

2
src/BufferedImage.h

@ -83,7 +83,7 @@ template<size_t imageDimensions> class BufferedImage: public AbstractImage {
* in constructor. * in constructor.
*/ */
template<class T> void setData(const T* data) { template<class T> void setData(const T* data) {
if(TypeTraits<typename TypeTraits<T>::TextureType>::glType() != _type) { if(TypeTraits<T>::imageType() != _type) {
Corrade::Utility::Error() << "BufferedImage: Passed data have wrong type"; Corrade::Utility::Error() << "BufferedImage: Passed data have wrong type";
return; return;
} }

4
src/Image.h

@ -45,7 +45,7 @@ template<size_t imageDimensions> class Image: public AbstractImage {
* Note that the image data are not copied on construction, but they * Note that the image data are not copied on construction, but they
* are deleted on class destruction. * are deleted on class destruction.
*/ */
template<class T> inline Image(ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, T* data): AbstractImage(colorFormat, TypeTraits<typename TypeTraits<T>::TextureType>::glType()), _dimensions(dimensions), _data(data) {} template<class T> inline Image(ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, T* data): AbstractImage(colorFormat, TypeTraits<T>::imageType()), _dimensions(dimensions), _data(data) {}
/** /**
* @brief Constructor * @brief Constructor
@ -88,7 +88,7 @@ template<size_t imageDimensions> class Image: public AbstractImage {
* constructor. * constructor.
*/ */
template<class T> void setData(const T* data) { template<class T> void setData(const T* data) {
if(TypeTraits<typename TypeTraits<T>::TextureType>::glType() != _type) { if(TypeTraits<T>::imageType() != _type) {
Corrade::Utility::Error() << "Image: Passed data have wrong type"; Corrade::Utility::Error() << "Image: Passed data have wrong type";
return; return;
} }

2
src/Mesh.h

@ -172,7 +172,7 @@ class MAGNUM_EXPORT Mesh {
* function does nothing. * function does nothing.
*/ */
template<class Attribute> inline void bindAttribute(Buffer* buffer) { template<class Attribute> inline void bindAttribute(Buffer* buffer) {
bindAttribute(buffer, Attribute::Location, TypeTraits<typename Attribute::Type>::count(), TypeTraits<typename Attribute::Type>::glType()); bindAttribute(buffer, Attribute::Location, TypeTraits<typename Attribute::Type>::count(), TypeTraits<typename Attribute::Type>::type());
} }
/** /**

2
src/MeshTools/CompressIndices.h

@ -94,7 +94,7 @@ class CompressIndices {
memcpy(buffer+i*sizeof(IndexType), reinterpret_cast<const char*>(&index), sizeof(IndexType)); memcpy(buffer+i*sizeof(IndexType), reinterpret_cast<const char*>(&index), sizeof(IndexType));
} }
return Result{indices.size(), TypeTraits<IndexType>::glType(), buffer}; return Result{indices.size(), TypeTraits<IndexType>::indexType(), buffer};
} }
}; };

2
src/Trade/ImageData.h

@ -43,7 +43,7 @@ template<size_t imageDimensions> class ImageData: public AbstractImage {
* @attention Note that the image data are not copied on construction, * @attention Note that the image data are not copied on construction,
* but they are deleted on class destruction. * but they are deleted on class destruction.
*/ */
template<class T> inline ImageData(ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, const T* data): AbstractImage(colorFormat, TypeTraits<typename TypeTraits<T>::TextureType>::glType()), _dimensions(dimensions), _data(reinterpret_cast<const char*>(data)) {} template<class T> inline ImageData(ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, const T* data): AbstractImage(colorFormat, TypeTraits<T>::imageType()), _dimensions(dimensions), _data(reinterpret_cast<const char*>(data)) {}
/** @brief Destructor */ /** @brief Destructor */
inline virtual ~ImageData() { delete[] _data; } inline virtual ~ImageData() { delete[] _data; }

86
src/TypeTraits.h

@ -34,29 +34,29 @@ traits.
#ifdef DOXYGEN_GENERATING_OUTPUT #ifdef DOXYGEN_GENERATING_OUTPUT
template<class T> struct TypeTraits: public Math::TypeTraits<T> { template<class T> struct TypeTraits: public Math::TypeTraits<T> {
/** /**
* @brief Type which can be used for indices * @brief OpenGL plain type ID
* *
* Implemented only in types which can be used for vertex indices (all * Returns e.g. Type::UnsignedInt for GLuint.
* unsigned types). This typedef is not present for types unusable for
* vertex indices, like GLfloat or GLint.
*/ */
typedef T IndexType; constexpr inline static Type type();
/** /**
* @brief Type which can be used in textures * @brief OpenGL type ID for indices
* *
* Implemented only in types which can be used for texture data, like * Implemented only in types which can be used for vertex indices (all
* GLubyte. This typedef is not present for types unusable for texture data, * unsigned types). This function is not present for types unusable for
* like GLdouble and Matrix3. * vertex indices, like GLfloat or GLint.
*/ */
typedef T TextureType; constexpr inline static Type indexType();
/** /**
* @brief OpenGL plain type ID * @brief OpenGL type ID for images
* *
* Returns e.g. Type::UnsignedInt for GLuint. * Implemented only in types which can be used for image data, like
* GLubyte. This function is not present for types unusable for image data,
* like GLdouble and Matrix3.
*/ */
constexpr inline static Type glType(); constexpr inline static Type imageType();
/** /**
* @brief Size of plain OpenGL type * @brief Size of plain OpenGL type
@ -96,8 +96,8 @@ 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 use this class to convert it into type, for example these two statements
are equivalent: are equivalent:
@code @code
TypeTraits<TypeOf<Type::UnsignedByte>::Type>::TextureType data; type = TypeTraits<TypeOf<Type::UnsignedByte>::Type>::imageType();
TypeTraits<GLubyte>::TextureType data; type = TypeTraits<GLubyte>::imageType();
@endcode @endcode
*/ */
template<Type T> class TypeOf { template<Type T> class TypeOf {
@ -147,82 +147,73 @@ template<> struct TypeOf<Type::Float> { typedef GLfloat Type; };
template<> struct TypeOf<Type::Double> { typedef GLdouble Type; }; template<> struct TypeOf<Type::Double> { typedef GLdouble Type; };
template<> struct TypeTraits<GLubyte>: public Math::TypeTraits<unsigned char> { template<> struct TypeTraits<GLubyte>: public Math::TypeTraits<unsigned char> {
typedef GLubyte IndexType; inline constexpr static Type type() { return Type::UnsignedByte; }
typedef GLubyte TextureType; inline constexpr static Type indexType() { return Type::UnsignedByte; }
inline constexpr static Type imageType() { return Type::UnsignedByte; }
inline constexpr static Type glType() { return Type::UnsignedByte; }
inline constexpr static size_t size() { return sizeof(GLubyte); } inline constexpr static size_t size() { return sizeof(GLubyte); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<> struct TypeTraits<GLbyte>: public Math::TypeTraits<char> { template<> struct TypeTraits<GLbyte>: public Math::TypeTraits<char> {
inline constexpr static Type type() { return Type::Byte; }
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLbyte TextureType; inline constexpr static Type imageType() { return Type::Byte; }
inline constexpr static Type glType() { return Type::Byte; }
inline constexpr static size_t size() { return sizeof(GLbyte); } inline constexpr static size_t size() { return sizeof(GLbyte); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<> struct TypeTraits<GLushort>: public Math::TypeTraits<unsigned short> { template<> struct TypeTraits<GLushort>: public Math::TypeTraits<unsigned short> {
typedef GLushort IndexType; inline constexpr static Type type() { return Type::UnsignedShort; }
typedef GLushort TextureType; inline constexpr static Type indexType() { return Type::UnsignedShort; }
inline constexpr static Type imageType() { return Type::UnsignedShort; }
inline constexpr static Type glType() { return Type::UnsignedShort; }
inline constexpr static size_t size() { return sizeof(GLushort); } inline constexpr static size_t size() { return sizeof(GLushort); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<> struct TypeTraits<GLshort>: public Math::TypeTraits<short> { template<> struct TypeTraits<GLshort>: public Math::TypeTraits<short> {
inline constexpr static Type type() { return Type::Short; }
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLshort TextureType; inline constexpr static Type imageType() { return Type::Short; }
inline constexpr static Type glType() { return Type::Short; }
inline constexpr static size_t size() { return sizeof(GLshort); } inline constexpr static size_t size() { return sizeof(GLshort); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<> struct TypeTraits<GLuint>: public Math::TypeTraits<unsigned int> { template<> struct TypeTraits<GLuint>: public Math::TypeTraits<unsigned int> {
typedef GLuint IndexType; inline constexpr static Type type() { return Type::UnsignedInt; }
typedef GLuint TextureType; inline constexpr static Type indexType() { return Type::UnsignedInt; }
inline constexpr static Type imageType() { return Type::UnsignedInt; }
inline constexpr static Type glType() { return Type::UnsignedInt; }
inline constexpr static size_t size() { return sizeof(GLuint); } inline constexpr static size_t size() { return sizeof(GLuint); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<> struct TypeTraits<GLint>: public Math::TypeTraits<int> { template<> struct TypeTraits<GLint>: public Math::TypeTraits<int> {
inline constexpr static Type type() { return Type::Int; }
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLint TextureType; inline constexpr static Type imageType() { return Type::Int; }
inline constexpr static Type glType() { return Type::Int; }
inline constexpr static size_t size() { return sizeof(GLint); } inline constexpr static size_t size() { return sizeof(GLint); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<> struct TypeTraits<GLfloat>: public Math::TypeTraits<float> { template<> struct TypeTraits<GLfloat>: public Math::TypeTraits<float> {
inline constexpr static Type type() { return Type::Float; }
/* Can not be used for indices */ /* Can not be used for indices */
typedef GLfloat TextureType; inline constexpr static Type imageType() { return Type::Float; }
inline constexpr static Type glType() { return Type::Float; }
inline constexpr static size_t size() { return sizeof(GLfloat); } inline constexpr static size_t size() { return sizeof(GLfloat); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<> struct TypeTraits<GLdouble>: public Math::TypeTraits<double> { template<> struct TypeTraits<GLdouble>: public Math::TypeTraits<double> {
inline constexpr static Type type() { return Type::Double; }
/* Can not be used for indices */ /* Can not be used for indices */
/* Can not be used for textures */ /* Can not be used for images */
inline constexpr static Type glType() { return Type::Double; }
inline constexpr static size_t size() { return sizeof(GLdouble); } inline constexpr static size_t size() { return sizeof(GLdouble); }
inline constexpr static size_t count() { return 1; } inline constexpr static size_t count() { return 1; }
}; };
template<class T, size_t vectorSize> struct TypeTraits<Math::Vector<T, vectorSize>> { template<class T, size_t vectorSize> struct TypeTraits<Math::Vector<T, vectorSize>> {
inline constexpr static Type type() { return TypeTraits<T>::type(); }
/* Can not be used for indices */ /* Can not be used for indices */
/* Can not be used for textures */ /* Can not be used for images */
inline constexpr static Type glType() { return TypeTraits<T>::glType(); }
inline constexpr static size_t size() { return sizeof(T); } inline constexpr static size_t size() { return sizeof(T); }
inline constexpr static size_t count() { return vectorSize; } inline constexpr static size_t count() { return vectorSize; }
}; };
@ -232,10 +223,9 @@ template<class T> struct TypeTraits<Math::Vector3<T>>: public TypeTraits<Math::V
template<class T> struct TypeTraits<Math::Vector4<T>>: public TypeTraits<Math::Vector<T, 4>> {}; template<class T> struct TypeTraits<Math::Vector4<T>>: public TypeTraits<Math::Vector<T, 4>> {};
template<class T, size_t matrixSize> struct TypeTraits<Math::Matrix<T, matrixSize>> { template<class T, size_t matrixSize> struct TypeTraits<Math::Matrix<T, matrixSize>> {
inline constexpr static Type type() { return TypeTraits<T>::type(); }
/* Can not be used for indices */ /* Can not be used for indices */
/* Can not be used for textures, obviously */ /* Can not be used for images */
inline constexpr static Type glType() { return TypeTraits<T>::glType(); }
inline constexpr static size_t size() { return sizeof(T); } inline constexpr static size_t size() { return sizeof(T); }
inline constexpr static size_t count() { return matrixSize*matrixSize; } inline constexpr static size_t count() { return matrixSize*matrixSize; }
}; };

Loading…
Cancel
Save