From b0c0e2d92da9db1cb9ea0da92be2d983cb06e91a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 17 Nov 2013 23:00:26 +0100 Subject: [PATCH] MeshTools: return Containers::Array instead of pointer from compressIndices(). This is source-incompatible change, but this function probably wasn't used much in user code, rather its Mesh/Buffer convenience overload. Actually Containers::Array has _implicit_ conversion operator to char*, but this should not be an issue, as the conversion is not allowed on rvalue references, thus the following code shouldn't compile at all. Not available on GCC < 4.8.1, though. // ... char* data; std::tie(..., data) = MeshTools::compressIndices(...); --- src/MeshTools/CompressIndices.cpp | 18 ++++++++---------- src/MeshTools/CompressIndices.h | 13 ++++--------- src/MeshTools/Test/CompressIndicesTest.cpp | 22 ++++++++-------------- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/MeshTools/CompressIndices.cpp b/src/MeshTools/CompressIndices.cpp index e81c890c9..c2b54c1a7 100644 --- a/src/MeshTools/CompressIndices.cpp +++ b/src/MeshTools/CompressIndices.cpp @@ -38,17 +38,17 @@ template<> constexpr Mesh::IndexType indexType() { return Mesh::In template<> constexpr Mesh::IndexType indexType() { return Mesh::IndexType::UnsignedShort; } template<> constexpr Mesh::IndexType indexType() { return Mesh::IndexType::UnsignedInt; } -template inline std::tuple compress(const std::vector& indices) { - char* buffer = new char[indices.size()*sizeof(T)]; +template inline std::tuple> compress(const std::vector& indices) { + Containers::Array buffer(indices.size()*sizeof(T)); for(std::size_t i = 0; i != indices.size(); ++i) { T index = static_cast(indices[i]); - std::memcpy(buffer+i*sizeof(T), &index, sizeof(T)); + std::memcpy(buffer.begin()+i*sizeof(T), &index, sizeof(T)); } - return std::make_tuple(indices.size(), indexType(), buffer); + return std::make_tuple(indices.size(), indexType(), std::move(buffer)); } -std::tuple compressIndicesInternal(const std::vector& indices, UnsignedInt max) { +std::tuple> compressIndicesInternal(const std::vector& indices, UnsignedInt max) { switch(Math::log(256, max)) { case 0: return compress(indices); @@ -65,7 +65,7 @@ std::tuple compressIndicesInternal(const st } -std::tuple compressIndices(const std::vector& indices) { +std::tuple> compressIndices(const std::vector& indices) { return compressIndicesInternal(indices, *std::max_element(indices.begin(), indices.end())); } @@ -76,14 +76,12 @@ void compressIndices(Mesh& mesh, Buffer& buffer, Buffer::Usage usage, const std: std::size_t indexCount; Mesh::IndexType indexType; - char* data; + Containers::Array data; std::tie(indexCount, indexType, data) = compressIndicesInternal(indices, *minmax.second); mesh.setIndexCount(indices.size()) .setIndexBuffer(buffer, 0, indexType, *minmax.first, *minmax.second); - buffer.setData({data, indexCount*Mesh::indexSize(indexType)}, usage); - - delete[] data; + buffer.setData(data, usage); } }} diff --git a/src/MeshTools/CompressIndices.h b/src/MeshTools/CompressIndices.h index 4acf0ff3b..b233b5eb8 100644 --- a/src/MeshTools/CompressIndices.h +++ b/src/MeshTools/CompressIndices.h @@ -40,28 +40,23 @@ namespace Magnum { namespace MeshTools { /** @brief Compress vertex indices @param indices Index array -@return Index count, type and compressed index array. Deleting the array is - user responsibility. +@return Index count, type and compressed index array This function takes index array and outputs them compressed to smallest possible size. For example when your indices have maximum number 463, it's wasteful to store them in array of 32bit integers, array of 16bit integers is -sufficient. Size of the buffer can be computed from index count and type, as -shown below. Example usage: +sufficient. Example usage: @code std::size_t indexCount; Mesh::IndexType indexType; -char* data; +Containers::Array data; std::tie(indexCount, indexType, data) = MeshTools::compressIndices(indices); -std::size_t dataSize = indexCount*Mesh::indexSize(indexType); -// ... -delete[] data; @endcode See also compressIndices(Mesh*, Buffer*, Buffer::Usage, const std::vector&), which writes the compressed data directly into index buffer of given mesh. */ -std::tuple MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector& indices); +std::tuple> MAGNUM_MESHTOOLS_EXPORT compressIndices(const std::vector& indices); /** @brief Compress vertex indices and write them to index buffer diff --git a/src/MeshTools/Test/CompressIndicesTest.cpp b/src/MeshTools/Test/CompressIndicesTest.cpp index e4a7b0f1d..bee8535d5 100644 --- a/src/MeshTools/Test/CompressIndicesTest.cpp +++ b/src/MeshTools/Test/CompressIndicesTest.cpp @@ -47,48 +47,44 @@ CompressIndicesTest::CompressIndicesTest() { void CompressIndicesTest::compressChar() { std::size_t indexCount; Mesh::IndexType indexType; - char* data; + Containers::Array data; std::tie(indexCount, indexType, data) = MeshTools::compressIndices( std::vector{1, 2, 3, 0, 4}); CORRADE_COMPARE(indexCount, 5); CORRADE_VERIFY(indexType == Mesh::IndexType::UnsignedByte); - CORRADE_COMPARE(std::vector(data, data+indexCount*Mesh::indexSize(indexType)), + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x01, 0x02, 0x03, 0x00, 0x04 })); - - delete[] data; } void CompressIndicesTest::compressShort() { std::size_t indexCount; Mesh::IndexType indexType; - char* data; + Containers::Array data; std::tie(indexCount, indexType, data) = MeshTools::compressIndices( std::vector{1, 256, 0, 5}); CORRADE_COMPARE(indexCount, 4); CORRADE_VERIFY(indexType == Mesh::IndexType::UnsignedShort); if(!Utility::Endianness::isBigEndian()) { - CORRADE_COMPARE(std::vector(data, data+indexCount*Mesh::indexSize(indexType)), + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00 })); } else { - CORRADE_COMPARE(std::vector(data, data+indexCount*Mesh::indexSize(indexType)), + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x05 })); } - - delete[] data; } void CompressIndicesTest::compressInt() { std::size_t indexCount; Mesh::IndexType indexType; - char* data; + Containers::Array data; std::tie(indexCount, indexType, data) = MeshTools::compressIndices( std::vector{65536, 3, 2}); @@ -96,18 +92,16 @@ void CompressIndicesTest::compressInt() { CORRADE_VERIFY(indexType == Mesh::IndexType::UnsignedInt); if(!Utility::Endianness::isBigEndian()) { - CORRADE_COMPARE(std::vector(data, data+indexCount*Mesh::indexSize(indexType)), + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 })); } else { - CORRADE_COMPARE(std::vector(data, data+indexCount*Mesh::indexSize(indexType)), + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02 })); } - - delete[] data; } }}}