include/vcl/BinaryDataContainer.hxx        |    5 +++--
 vcl/qa/cppunit/BinaryDataContainerTest.cxx |   21 ++++++++++++++++++---
 vcl/source/graphic/BinaryDataContainer.cxx |    5 +++++
 3 files changed, 26 insertions(+), 5 deletions(-)

New commits:
commit 8413089d698cf55a7aa14358ab8d5065af331b39
Author:     Tomaž Vajngerl <[email protected]>
AuthorDate: Mon Dec 28 18:34:39 2020 +0900
Commit:     Tomaž Vajngerl <[email protected]>
CommitDate: Tue Jan 19 06:41:25 2021 +0100

    vcl: a way to create data for BinaryDataContainer without copying
    
    Construstor for BinaryDataContainer taking unique_ptr<sal_uInt8>
    represents a way how to create and fill the data for the container
    and make sure that the container is the only one that is managing
    the data after construction.
    Generally we don't want to give the access to the internal
    shared_ptr to the outside, so with the unique_ptr we make sure
    that there is no outside references present (which would be the
    case if the we would take shared_ptr as the constructor) and the
    unique_ptr is cleared (moved) after construction.
    
    Change-Id: I123114c8bee92e342740d94a784b2c16e2564528
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108441
    Tested-by: Tomaž Vajngerl <[email protected]>
    Reviewed-by: Tomaž Vajngerl <[email protected]>

diff --git a/include/vcl/BinaryDataContainer.hxx 
b/include/vcl/BinaryDataContainer.hxx
index f1a3ada45d72..d6c6848c48f6 100644
--- a/include/vcl/BinaryDataContainer.hxx
+++ b/include/vcl/BinaryDataContainer.hxx
@@ -26,8 +26,9 @@ private:
     std::shared_ptr<std::vector<sal_uInt8>> mpData;
 
 public:
-    explicit BinaryDataContainer();
-    explicit BinaryDataContainer(const sal_uInt8* pData, size_t nSize);
+    BinaryDataContainer();
+    BinaryDataContainer(const sal_uInt8* pData, size_t nSize);
+    BinaryDataContainer(std::unique_ptr<std::vector<sal_uInt8>> rData);
 
     BinaryDataContainer(const BinaryDataContainer& rBinaryDataContainer)
         : mpData(rBinaryDataContainer.mpData)
diff --git a/vcl/qa/cppunit/BinaryDataContainerTest.cxx 
b/vcl/qa/cppunit/BinaryDataContainerTest.cxx
index 94e774ec9969..09bb036d8602 100644
--- a/vcl/qa/cppunit/BinaryDataContainerTest.cxx
+++ b/vcl/qa/cppunit/BinaryDataContainerTest.cxx
@@ -19,14 +19,14 @@ namespace
 {
 class BinaryDataContainerTest : public CppUnit::TestFixture
 {
-    void test();
+    void testConstruct();
 
     CPPUNIT_TEST_SUITE(BinaryDataContainerTest);
-    CPPUNIT_TEST(test);
+    CPPUNIT_TEST(testConstruct);
     CPPUNIT_TEST_SUITE_END();
 };
 
-void BinaryDataContainerTest::test()
+void BinaryDataContainerTest::testConstruct()
 {
     {
         BinaryDataContainer aContainer;
@@ -54,6 +54,21 @@ void BinaryDataContainerTest::test()
         CPPUNIT_ASSERT_EQUAL(true, aCopyOfContainer.isEmpty());
         CPPUNIT_ASSERT_EQUAL(size_t(0), aCopyOfContainer.getSize());
     }
+    {
+        // construct a unique_ptr data array
+        std::vector<sal_uInt8> aTestByteArray = { 1, 2, 3, 4 };
+        auto aConstructionByteArray = 
std::make_unique<std::vector<sal_uInt8>>(aTestByteArray);
+
+        // remember for later to compare
+        const sal_uInt8* pInternal = aConstructionByteArray->data();
+
+        BinaryDataContainer aContainer(std::move(aConstructionByteArray));
+
+        // make sure the unique_ptr was moved into BinaryDataContainer
+        CPPUNIT_ASSERT_EQUAL(false, bool(aConstructionByteArray));
+        // make sure we didn't copy data into BinaryDataContainer (pointers 
match)
+        CPPUNIT_ASSERT_EQUAL(pInternal, aContainer.getData());
+    }
 }
 
 } // namespace
diff --git a/vcl/source/graphic/BinaryDataContainer.cxx 
b/vcl/source/graphic/BinaryDataContainer.cxx
index 7576852215b1..ba7a9f597c1f 100644
--- a/vcl/source/graphic/BinaryDataContainer.cxx
+++ b/vcl/source/graphic/BinaryDataContainer.cxx
@@ -19,6 +19,11 @@ BinaryDataContainer::BinaryDataContainer(const sal_uInt8* 
pData, size_t nSize)
     std::copy(pData, pData + nSize, mpData->data());
 }
 
+BinaryDataContainer::BinaryDataContainer(std::unique_ptr<std::vector<sal_uInt8>>
 aData)
+    : mpData(std::shared_ptr<std::vector<sal_uInt8>>(aData.release(), 
aData.get_deleter()))
+{
+}
+
 size_t BinaryDataContainer::calculateHash() const
 {
     size_t nSeed = 0;
_______________________________________________
Libreoffice-commits mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to