diff --git a/src/MeshTools/Interleave.h b/src/MeshTools/Interleave.h index 27203196c..6d3034ad6 100644 --- a/src/MeshTools/Interleave.h +++ b/src/MeshTools/Interleave.h @@ -42,31 +42,31 @@ namespace Implementation { class Interleave { public: - Interleave(): _attributeCount(0), _stride(0), _data(nullptr) {} + Interleave(): _attributeCount(0), _stride(0) {} - template std::tuple operator()(const T&... attributes) { + template std::tuple> operator()(const T&... attributes) { /* Compute buffer size and stride */ _attributeCount = attributeCount(attributes...); + Containers::Array data; if(_attributeCount && _attributeCount != ~std::size_t(0)) { _stride = stride(attributes...); /* Create output buffer */ - _data = new char[_attributeCount*_stride]; + data = Containers::Array(_attributeCount*_stride); /* Save the data */ - write(_data, attributes...); + write(data.begin(), attributes...); } - return std::make_tuple(_attributeCount, _stride, _data); + return std::make_tuple(_attributeCount, _stride, std::move(data)); } template void operator()(Mesh& mesh, Buffer& buffer, Buffer::Usage usage, const T&... attributes) { - operator()(attributes...); + Containers::Array data; + std::tie(std::ignore, std::ignore, data) = operator()(attributes...); mesh.setVertexCount(_attributeCount); - buffer.setData({_data, _attributeCount*_stride}, usage); - - delete[] _data; + buffer.setData({data, _attributeCount*_stride}, usage); } /* Specialization for only one attribute array */ @@ -126,7 +126,6 @@ class Interleave { std::size_t _attributeCount; std::size_t _stride; - char* _data; }; } @@ -146,11 +145,9 @@ std::vector positions; std::vector textureCoordinates; std::size_t attributeCount; std::size_t stride; -char* data; +Containers::Array data; std::tie(attributeCount, stride, data) = MeshTools::interleave(positions, textureCoordinates); -std::size_t dataSize = attributeCount*stride; // ... -delete[] data; @endcode It's often desirable to align data for one vertex on 32bit boundaries. To @@ -161,7 +158,7 @@ std::vector weights; std::vector> vertexColors; std::size_t attributeCount; std::size_t stride; -char* data; +Containers::Array data; std::tie(attributeCount, stride, data) = MeshTools::interleave(positions, weights, 2, textureCoordinates, 1); @endcode This way vertex stride is 24 bytes, without gaps it would be 21 bytes, causing @@ -178,7 +175,7 @@ See also interleave(Mesh*, Buffer*, Buffer::Usage, const T&...), which writes the interleaved array directly into buffer of given mesh. */ /* enable_if to avoid clash with overloaded function below */ -template inline typename std::enable_if::value, std::tuple>::type interleave(const T& first, const U&... next) { +template inline typename std::enable_if::value, std::tuple>>::type interleave(const T& first, const U&... next) { return Implementation::Interleave()(first, next...); } diff --git a/src/MeshTools/Test/InterleaveTest.cpp b/src/MeshTools/Test/InterleaveTest.cpp index beb57aa35..036fe2e8e 100644 --- a/src/MeshTools/Test/InterleaveTest.cpp +++ b/src/MeshTools/Test/InterleaveTest.cpp @@ -84,7 +84,7 @@ void InterleaveTest::strideGaps() { void InterleaveTest::write() { std::size_t attributeCount; std::size_t stride; - char* data; + Containers::Array data; std::tie(attributeCount, stride, data) = MeshTools::interleave( std::vector{0, 1, 2}, std::vector{3, 4, 5}, @@ -92,28 +92,25 @@ void InterleaveTest::write() { CORRADE_COMPARE(attributeCount, std::size_t(3)); CORRADE_COMPARE(stride, std::size_t(7)); - std::size_t size = attributeCount*stride; if(!Utility::Endianness::isBigEndian()) { - CORRADE_COMPARE(std::vector(data, data+size), (std::vector{ + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x08, 0x00 })); } else { - CORRADE_COMPARE(std::vector(data, data+size), (std::vector{ + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x06, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x07, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08 })); } - - delete[] data; } void InterleaveTest::writeGaps() { std::size_t attributeCount; std::size_t stride; - char* data; + Containers::Array data; std::tie(attributeCount, stride, data) = MeshTools::interleave( std::vector{0, 1, 2}, 3, std::vector{3, 4, 5}, @@ -121,25 +118,22 @@ void InterleaveTest::writeGaps() { CORRADE_COMPARE(attributeCount, std::size_t(3)); CORRADE_COMPARE(stride, std::size_t(12)); - std::size_t size = attributeCount*stride; if(!Utility::Endianness::isBigEndian()) { /* byte, _____________gap, int___________________, short_____, _______gap */ - CORRADE_COMPARE(std::vector(data, data+size), (std::vector{ + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00 })); } else { /* byte, _____________gap, ___________________int, _____short, _______gap */ - CORRADE_COMPARE(std::vector(data, data+size), (std::vector{ + CORRADE_COMPARE(std::vector(data.begin(), data.end()), (std::vector{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x06, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x08, 0x00, 0x00 })); } - - delete[] data; } }}}