From 64665872cc0f6d62300eeba6be7befb06edf0944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 22 Apr 2012 02:19:14 +0200 Subject: [PATCH] Use CORRADE_ASSERT() instead of cassert and direct error output. Unlike previous solution this can be compiled without any impact on performance at all. --- src/AbstractShaderProgram.cpp | 17 +++-------------- src/IndexedMesh.cpp | 10 +--------- src/Mesh.cpp | 9 +-------- src/MeshTools/CMakeLists.txt | 6 ++++++ src/MeshTools/CombineIndexedArrays.h | 9 ++------- src/MeshTools/FlipNormals.cpp | 6 +----- src/MeshTools/GenerateFlatNormals.cpp | 6 +----- src/MeshTools/Interleave.h | 15 +++------------ src/MeshTools/Subdivide.h | 5 +---- src/MeshTools/Test/CMakeLists.txt | 7 +++++-- src/MeshTools/Test/CombineIndexedArraysTest.cpp | 2 -- src/MeshTools/Test/InterleaveTest.cpp | 4 ---- src/Primitives/Capsule.cpp | 8 +------- src/Primitives/UVSphere.cpp | 7 +------ 14 files changed, 26 insertions(+), 85 deletions(-) diff --git a/src/AbstractShaderProgram.cpp b/src/AbstractShaderProgram.cpp index 38d8a0574..8e917008b 100644 --- a/src/AbstractShaderProgram.cpp +++ b/src/AbstractShaderProgram.cpp @@ -15,7 +15,6 @@ #include "AbstractShaderProgram.h" -#include #include #define LINKER_MESSAGE_MAX_LENGTH 1024 @@ -51,19 +50,13 @@ bool AbstractShaderProgram::attachShader(Shader* shader) { } void AbstractShaderProgram::bindAttribute(GLuint location, const string& name) { - if(state != Initialized) { - Error() << "AbstractShaderProgram: attribute cannot be bound after linking."; - assert(0); - } + CORRADE_ASSERT(state == Initialized, "AbstractShaderProgram: attribute cannot be bound after linking.", ) glBindAttribLocation(program, location, name.c_str()); } void AbstractShaderProgram::bindFragmentDataLocation(GLuint location, const std::string& name) { - if(state != Initialized) { - Error() << "AbstractShaderProgram: fragment data location cannot be bound after linking."; - assert(0); - } + CORRADE_ASSERT(state == Initialized, "AbstractShaderProgram: fragment data location cannot be bound after linking.", ) glBindFragDataLocation(program, location, name.c_str()); } @@ -102,11 +95,7 @@ void AbstractShaderProgram::link() { } GLint AbstractShaderProgram::uniformLocation(const std::string& name) { - if(state != Linked) { - Error() << "AbstractShaderProgram: uniform location cannot be retrieved before linking."; - assert(0); - return -1; - } + CORRADE_ASSERT(state == Linked, "AbstractShaderProgram: uniform location cannot be retrieved before linking.", -1) GLint location = glGetUniformLocation(program, name.c_str()); if(location == -1) diff --git a/src/IndexedMesh.cpp b/src/IndexedMesh.cpp index 6bc28e515..28823fe5d 100644 --- a/src/IndexedMesh.cpp +++ b/src/IndexedMesh.cpp @@ -15,11 +15,6 @@ #include "IndexedMesh.h" -#include - -using namespace std; -using namespace Corrade::Utility; - namespace Magnum { void IndexedMesh::draw() { @@ -37,10 +32,7 @@ void IndexedMesh::draw() { void IndexedMesh::finalize() { if(isFinalized()) return; - if(!_indexCount) { - Error() << "IndexedMesh: the mesh has zero index count!"; - assert(0); - } + CORRADE_ASSERT(_indexCount, "IndexedMesh: the mesh has zero index count!", ) /* Finalize attribute positions */ Mesh::finalize(); diff --git a/src/Mesh.cpp b/src/Mesh.cpp index dc8a6c98b..3b531a55f 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -16,11 +16,7 @@ #include "Mesh.h" #include "Buffer.h" -#include -#include - using namespace std; -using namespace Corrade::Utility; namespace Magnum { @@ -57,10 +53,7 @@ void Mesh::finalize() { /* Already finalized */ if(finalized) return; - if(!_vertexCount) { - Error() << "Mesh: the mesh has zero vertex count!"; - assert(0); - } + CORRADE_ASSERT(_vertexCount, "Mesh: the mesh has zero vertex count!", ) /* Enable vertex arrays for all attributes */ for(set::const_iterator it = _attributes.begin(); it != _attributes.end(); ++it) diff --git a/src/MeshTools/CMakeLists.txt b/src/MeshTools/CMakeLists.txt index 4ccd61cd4..5b69ab40c 100644 --- a/src/MeshTools/CMakeLists.txt +++ b/src/MeshTools/CMakeLists.txt @@ -16,5 +16,11 @@ install(TARGETS MagnumMeshTools DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR}) if(BUILD_TESTS) enable_testing() + + # Library with graceful assert for testing + add_library(MagnumMeshToolsTestLib SHARED ${MagnumMeshTools_SRCS}) + set_target_properties(MagnumMeshToolsTestLib PROPERTIES COMPILE_FLAGS -DCORRADE_GRACEFUL_ASSERT) + target_link_libraries(MagnumMeshToolsTestLib ${MAGNUM_LIBRARY}) + add_subdirectory(Test) endif() diff --git a/src/MeshTools/CombineIndexedArrays.h b/src/MeshTools/CombineIndexedArrays.h index 069102efa..432643cf9 100644 --- a/src/MeshTools/CombineIndexedArrays.h +++ b/src/MeshTools/CombineIndexedArrays.h @@ -19,7 +19,6 @@ * @brief Function Magnum::MeshTools::combineIndexedArrays() */ -#include #include #include #include @@ -58,12 +57,8 @@ class CombineIndexedArrays { private: template inline static size_t indexCount(const std::vector& first, const std::vector&... next) { - size_t count = indexCount(next...); - if(sizeof...(next) != 0 && count != first.size()) { - Corrade::Utility::Error() << "MeshTools::combineIndexedArrays(): index arrays don't have the same length, nothing done."; - assert(0); - return 0; - } + CORRADE_ASSERT(sizeof...(next) == 0 || indexCount(next...) == first.size(), "MeshTools::combineIndexedArrays(): index arrays don't have the same length, nothing done.", 0) + return first.size(); } diff --git a/src/MeshTools/FlipNormals.cpp b/src/MeshTools/FlipNormals.cpp index 5d3e055b3..8abc87cc2 100644 --- a/src/MeshTools/FlipNormals.cpp +++ b/src/MeshTools/FlipNormals.cpp @@ -16,15 +16,11 @@ #include "FlipNormals.h" using namespace std; -using namespace Corrade::Utility; namespace Magnum { namespace MeshTools { void flipFaceWinding(vector& indices) { - if(indices.size()%3 != 0) { - Error() << "MeshTools::flipNormals(): index count is not divisible by 3!"; - return; - } + CORRADE_ASSERT(!(indices.size()%3), "MeshTools::flipNormals(): index count is not divisible by 3!", ) for(size_t i = 0; i != indices.size(); i += 3) swap(indices[i+1], indices[i+2]); diff --git a/src/MeshTools/GenerateFlatNormals.cpp b/src/MeshTools/GenerateFlatNormals.cpp index ad667dc3a..69d9b7105 100644 --- a/src/MeshTools/GenerateFlatNormals.cpp +++ b/src/MeshTools/GenerateFlatNormals.cpp @@ -18,15 +18,11 @@ #include "MeshTools/Clean.h" using namespace std; -using namespace Corrade::Utility; namespace Magnum { namespace MeshTools { tuple, vector> generateFlatNormals(const std::vector< unsigned int >& indices, const vector< Vector4 >& vertices) { - if(indices.size()%3 != 0) { - Error() << "MeshTools::generateFlatNormals(): index count is not divisible by 3!"; - return tuple, vector>(); - } + CORRADE_ASSERT(!(indices.size()%3), "MeshTools::generateFlatNormals(): index count is not divisible by 3!", (tuple, vector>())) /* Create normal for every triangle (assuming counterclockwise winding) */ vector normalIndices; diff --git a/src/MeshTools/Interleave.h b/src/MeshTools/Interleave.h index 49f44b95a..a873a5403 100644 --- a/src/MeshTools/Interleave.h +++ b/src/MeshTools/Interleave.h @@ -19,7 +19,6 @@ * @brief Function Magnum::MeshTools::interleave() */ -#include #include #include #include @@ -54,11 +53,7 @@ class Interleave { } template void operator()(Mesh* mesh, Buffer* buffer, Buffer::Usage usage, const std::vector&... attributes) { - if(!mesh->isInterleaved(buffer)) { - Corrade::Utility::Error() << "MeshTools::interleave(): the buffer is not interleaved, nothing done"; - assert(0); - return; - } + CORRADE_ASSERT(mesh->isInterleaved(buffer), "MeshTools::interleave(): the buffer is not interleaved, nothing done", ) operator()(attributes...); @@ -69,12 +64,8 @@ class Interleave { } template inline static size_t attributeCount(const std::vector& first, const std::vector&... next) { - size_t count = attributeCount(next...); - if(sizeof...(next) != 0 && count != first.size()) { - Corrade::Utility::Error() << "MeshTools::interleave(): attribute arrays don't have the same length, nothing done."; - assert(0); - return 0; - } + CORRADE_ASSERT(sizeof...(next) == 0 || attributeCount(next...) == first.size(), "MeshTools::interleave(): attribute arrays don't have the same length, nothing done.", 0) + return first.size(); } diff --git a/src/MeshTools/Subdivide.h b/src/MeshTools/Subdivide.h index 47e3a4997..638cce284 100644 --- a/src/MeshTools/Subdivide.h +++ b/src/MeshTools/Subdivide.h @@ -32,10 +32,7 @@ template class Subdivide { inline Subdivide(std::vector& indices, std::vector& vertices): indices(indices), vertices(vertices) {} void operator()(Interpolator interpolator) { - if(indices.size()%3 != 0) { - Corrade::Utility::Error() << "MeshTools::subdivide(): index count is not divisible by 3!"; - return; - } + CORRADE_ASSERT(!(indices.size()%3), "MeshTools::subdivide(): index count is not divisible by 3!", ) size_t indexCount = indices.size(); indices.reserve(indices.size()*4); diff --git a/src/MeshTools/Test/CMakeLists.txt b/src/MeshTools/Test/CMakeLists.txt index b8cc409af..1ebd8120f 100644 --- a/src/MeshTools/Test/CMakeLists.txt +++ b/src/MeshTools/Test/CMakeLists.txt @@ -1,9 +1,12 @@ corrade_add_test(CleanTest CleanTest.h CleanTest.cpp ${CORRADE_UTILITY_LIBRARY}) corrade_add_test(CombineIndexedArraysTest CombineIndexedArraysTest.h CombineIndexedArraysTest.cpp ${CORRADE_UTILITY_LIBRARY}) corrade_add_test(CompressIndicesTest CompressIndicesTest.h CompressIndicesTest.cpp ${CORRADE_UTILITY_LIBRARY} ${MAGNUM_LIBRARY}) -corrade_add_test(FlipNormalsTest FlipNormalsTest.h FlipNormalsTest.cpp ${MAGNUM_MESHTOOLS_LIBRARY}) -corrade_add_test(GenerateFlatNormalsTest GenerateFlatNormalsTest.h GenerateFlatNormalsTest.cpp ${MAGNUM_MESHTOOLS_LIBRARY}) +corrade_add_test(FlipNormalsTest FlipNormalsTest.h FlipNormalsTest.cpp MagnumMeshToolsTestLib) +corrade_add_test(GenerateFlatNormalsTest GenerateFlatNormalsTest.h GenerateFlatNormalsTest.cpp MagnumMeshToolsTestLib) corrade_add_test(InterleaveTest InterleaveTest.h InterleaveTest.cpp ${CORRADE_UTILITY_LIBRARY}) corrade_add_test(SubdivideTest SubdivideTest.h SubdivideTest.cpp ${CORRADE_UTILITY_LIBRARY}) corrade_add_test(SubdivideCleanBenchmark SubdivideCleanBenchmark.h SubdivideCleanBenchmark.cpp ${CORRADE_UTILITY_LIBRARY} ${MAGNUM_PRIMITIVES_LIBRARY}) corrade_add_test(TipsifyTest TipsifyTest.h TipsifyTest.cpp ${MAGNUM_MESHTOOLS_LIBRARY}) + +# Graceful assert for testing +set_target_properties(CombineIndexedArraysTest InterleaveTest SubdivideTest PROPERTIES COMPILE_FLAGS -DCORRADE_GRACEFUL_ASSERT) diff --git a/src/MeshTools/Test/CombineIndexedArraysTest.cpp b/src/MeshTools/Test/CombineIndexedArraysTest.cpp index c79405892..a348e2aae 100644 --- a/src/MeshTools/Test/CombineIndexedArraysTest.cpp +++ b/src/MeshTools/Test/CombineIndexedArraysTest.cpp @@ -15,8 +15,6 @@ #include "CombineIndexedArraysTest.h" -#define NDEBUG - #include #include diff --git a/src/MeshTools/Test/InterleaveTest.cpp b/src/MeshTools/Test/InterleaveTest.cpp index d4d2219c2..acd1c9d21 100644 --- a/src/MeshTools/Test/InterleaveTest.cpp +++ b/src/MeshTools/Test/InterleaveTest.cpp @@ -15,10 +15,6 @@ #include "InterleaveTest.h" -#ifndef NDEBUG -#define NDEBUG -#endif - #include #include diff --git a/src/Primitives/Capsule.cpp b/src/Primitives/Capsule.cpp index 5d0e0da12..9c60682ec 100644 --- a/src/Primitives/Capsule.cpp +++ b/src/Primitives/Capsule.cpp @@ -15,18 +15,12 @@ #include "Capsule.h" -#include - using namespace std; -using namespace Corrade::Utility; namespace Magnum { namespace Primitives { Capsule::Capsule(unsigned int rings, unsigned int segments, GLfloat length, TextureCoords textureCoords): MeshData(Mesh::Primitive::Triangles, new vector, {new vector()}, {new vector()}, textureCoords == TextureCoords::Generate ? vector*>{new vector()} : vector*>()), segments(segments), textureCoords(textureCoords) { - if(rings < 1 || segments < 3) { - Error() << "Capsule must have at least one ring and three segments"; - assert(0); - } + CORRADE_ASSERT(rings >= 1 && segments >= 3, "Capsule must have at least one ring and three segments", ) GLfloat height = 2.0f+length; GLfloat textureCoordsVIncrement = 1.0f/(rings*height); diff --git a/src/Primitives/UVSphere.cpp b/src/Primitives/UVSphere.cpp index 80d36f718..bd3a25a48 100644 --- a/src/Primitives/UVSphere.cpp +++ b/src/Primitives/UVSphere.cpp @@ -13,8 +13,6 @@ GNU Lesser General Public License version 3 for more details. */ -#include - #include "UVSphere.h" using namespace std; @@ -23,10 +21,7 @@ using namespace Corrade::Utility; namespace Magnum { namespace Primitives { UVSphere::UVSphere(unsigned int rings, unsigned int segments, TextureCoords textureCoords): Capsule(segments, textureCoords) { - if(rings < 2 || segments < 3) { - Error() << "UVSphere must have at least two rings and three segments"; - assert(0); - } + CORRADE_ASSERT(rings >= 2 && segments >= 3, "UVSphere must have at least two rings and three segments", ) GLfloat textureCoordsVIncrement = 1.0f/rings; GLfloat ringAngleIncrement = PI/rings;