From 3d5f7b9a3c31002f5239f5dbea683095c0887e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 19 Jul 2019 11:34:16 +0200 Subject: [PATCH] Make r-value [Compressed]Image[Data]::data() return an Array. Instead of them being deleted. This was not possible in the times where GCC 4.7 compatibility was a thing, but now that's long gone. And of course I forgot the l/r-value overloads on CompressedImage :/ --- doc/changelog.dox | 3 +++ src/Magnum/Image.h | 36 ++++++++++++++++++++++--- src/Magnum/Test/ImageTest.cpp | 31 ++++++++++++++++----- src/Magnum/Trade/ImageData.h | 17 ++++++++++-- src/Magnum/Trade/Test/ImageDataTest.cpp | 17 +++++++++--- 5 files changed, 89 insertions(+), 15 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 3d0831756..0bc1661a8 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -213,6 +213,9 @@ See also: @ref ResourceDataState::NotFound resources - Added comparison operators to @ref Resource (see [mosra/magnum#346](https://github.com/mosra/magnum/pull/346)) +- @ref Image::data(), @ref CompressedImage::data() and + @ref Trade::ImageData::data() called on a r-value now return + @ref Corrade::Containers::Array instead of being deleted @subsubsection changelog-latest-changes-animation Animation library diff --git a/src/Magnum/Image.h b/src/Magnum/Image.h index b4b8aad20..8dc9c709f 100644 --- a/src/Magnum/Image.h +++ b/src/Magnum/Image.h @@ -358,11 +358,24 @@ template class Image { * @see @ref release(), @ref pixels() */ Containers::ArrayView data() & { return _data; } - Containers::ArrayView data() && = delete; /**< @overload */ /** @overload */ Containers::ArrayView data() const & { return _data; } - Containers::ArrayView data() const && = delete; /**< @overload */ + + /** + * @brief Raw data from a r-value + * + * Unlike @ref data(), which returns a view, this is equivalent to + * @ref release() to avoid a dangling view when the temporary instance + * goes out of scope. + * @todoc stupid doxygen can't link to & overloads ffs + */ + Containers::Array data() && { return release(); } + + /** @overload + * @todo what to do here?! + */ + Containers::Array data() const && = delete; #ifdef MAGNUM_BUILD_DEPRECATED /** @@ -587,10 +600,25 @@ template class CompressedImage { * * @see @ref release() */ - Containers::ArrayView data() { return _data; } + Containers::ArrayView data() & { return _data; } /** @overload */ - Containers::ArrayView data() const { return _data; } + Containers::ArrayView data() const & { return _data; } + + /** + * @brief Raw data from a r-value + * + * Unlike @ref data(), which returns a view, this is equivalent to + * @ref release() to avoid a dangling view when the temporary instance + * goes out of scope. + * @todoc stupid doxygen can't link to & overloads ffs + */ + Containers::Array data() && { return release(); } + + /** @overload + * @todo what to do here?! + */ + Containers::Array data() const && = delete; #ifdef MAGNUM_BUILD_DEPRECATED /** diff --git a/src/Magnum/Test/ImageTest.cpp b/src/Magnum/Test/ImageTest.cpp index 9eff1d428..769350ecf 100644 --- a/src/Magnum/Test/ImageTest.cpp +++ b/src/Magnum/Test/ImageTest.cpp @@ -61,8 +61,10 @@ struct ImageTest: TestSuite::Tester { void toViewCompressedGeneric(); void toViewCompressedImplementationSpecific(); - void access(); - void accessCompressed(); + void data(); + void dataCompressed(); + void dataRvalue(); + void dataRvalueCompressed(); void dataProperties(); void dataPropertiesCompressed(); @@ -100,8 +102,10 @@ ImageTest::ImageTest() { &ImageTest::toViewCompressedGeneric, &ImageTest::toViewCompressedImplementationSpecific, - &ImageTest::access, - &ImageTest::accessCompressed, + &ImageTest::data, + &ImageTest::dataCompressed, + &ImageTest::dataRvalue, + &ImageTest::dataRvalueCompressed, &ImageTest::dataProperties, &ImageTest::dataPropertiesCompressed, @@ -608,7 +612,7 @@ void ImageTest::toViewCompressedImplementationSpecific() { CORRADE_COMPARE(b.data().size(), 8); } -void ImageTest::access() { +void ImageTest::data() { auto data = new char[4*4]; Image2D a{PixelFormat::RGBA8Unorm, {1, 3}, Containers::Array{data, 4*4}}; const Image2D& ca = a; @@ -616,7 +620,7 @@ void ImageTest::access() { CORRADE_COMPARE(ca.data(), data); } -void ImageTest::accessCompressed() { +void ImageTest::dataCompressed() { auto data = new char[8]; CompressedImage2D a{CompressedPixelFormat::Bc1RGBAUnorm, {4, 4}, Containers::Array{data, 8}}; @@ -625,6 +629,21 @@ void ImageTest::accessCompressed() { CORRADE_COMPARE(ca.data(), data); } +void ImageTest::dataRvalue() { + auto data = new char[4*4]; + Containers::Array released = Image2D{PixelFormat::RGBA8Unorm, {1, 3}, + Containers::Array{data, 4*4}}.data(); + CORRADE_COMPARE(released.data(), data); +} + +void ImageTest::dataRvalueCompressed() { + auto data = new char[8]; + Containers::Array released = CompressedImage2D{ + CompressedPixelFormat::Bc1RGBAUnorm, {4, 4}, + Containers::Array{data, 8}}.data(); + CORRADE_COMPARE(released.data(), data); +} + void ImageTest::dataProperties() { Image3D image{ PixelStorage{} diff --git a/src/Magnum/Trade/ImageData.h b/src/Magnum/Trade/ImageData.h index 0ed1d224c..90f62939a 100644 --- a/src/Magnum/Trade/ImageData.h +++ b/src/Magnum/Trade/ImageData.h @@ -334,11 +334,24 @@ template class ImageData { * @see @ref release(), @ref pixels() */ Containers::ArrayView data() & { return _data; } - Containers::ArrayView data() && = delete; /**< @overload */ /** @overload */ Containers::ArrayView data() const & { return _data; } - Containers::ArrayView data() const && = delete; /**< @overload */ + + /** + * @brief Raw data from a r-value + * + * Unlike @ref data(), which returns a view, this is equivalent to + * @ref release() to avoid a dangling view when the temporary instance + * goes out of scope. + * @todoc stupid doxygen can't link to & overloads ffs + */ + Containers::Array data() && { return release(); } + + /** @overload + * @todo what to do here?! + */ + Containers::Array data() const && = delete; #ifdef MAGNUM_BUILD_DEPRECATED /** diff --git a/src/Magnum/Trade/Test/ImageDataTest.cpp b/src/Magnum/Trade/Test/ImageDataTest.cpp index 77c4a454e..80a900f34 100644 --- a/src/Magnum/Trade/Test/ImageDataTest.cpp +++ b/src/Magnum/Trade/Test/ImageDataTest.cpp @@ -60,7 +60,9 @@ struct ImageDataTest: TestSuite::Tester { void toViewCompressedGeneric(); void toViewCompressedImplementationSpecific(); - void access(); + void data(); + void dataRvalue(); + void dataProperties(); void release(); @@ -96,7 +98,9 @@ ImageDataTest::ImageDataTest() { &ImageDataTest::toViewCompressedGeneric, &ImageDataTest::toViewCompressedImplementationSpecific, - &ImageDataTest::access, + &ImageDataTest::data, + &ImageDataTest::dataRvalue, + &ImageDataTest::dataProperties, &ImageDataTest::release, @@ -552,7 +556,7 @@ void ImageDataTest::toViewCompressedImplementationSpecific() { CORRADE_COMPARE(b.data().size(), 8); } -void ImageDataTest::access() { +void ImageDataTest::data() { auto data = new char[4*4]; ImageData2D a{PixelFormat::RGBA8Unorm, {1, 3}, Containers::Array{data, 4*4}}; const ImageData2D& ca = a; @@ -560,6 +564,13 @@ void ImageDataTest::access() { CORRADE_COMPARE(ca.data(), data); } +void ImageDataTest::dataRvalue() { + auto data = new char[4*4]; + Containers::Array released = ImageData2D{PixelFormat::RGBA8Unorm, + {1, 3}, Containers::Array{data, 4*4}}.data(); + CORRADE_COMPARE(released.data(), data); +} + void ImageDataTest::dataProperties() { ImageData3D image{ PixelStorage{}