sc/Library_sc.mk                          |    1 
 sc/inc/SparklineList.hxx                  |   61 +----------------------
 sc/source/core/data/document.cxx          |    3 +
 sc/source/ui/sparklines/SparklineList.cxx |   79 ++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+), 57 deletions(-)

New commits:
commit c27b48bd741d9c519fbcf1213d50e64c627997aa
Author:     Tomaž Vajngerl <[email protected]>
AuthorDate: Sun Apr 3 13:57:36 2022 +0900
Commit:     Tomaž Vajngerl <[email protected]>
CommitDate: Sun Apr 3 13:57:36 2022 +0900

    sc: prevent a crash when deleting a sparkline
    
    Change-Id: Idf89d4bbdc2bd29ce55cc3a8fd6707ece345869c

diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx
index 5016e5188a8a..047ec6886d68 100644
--- a/sc/source/core/data/document.cxx
+++ b/sc/source/core/data/document.cxx
@@ -6596,6 +6596,9 @@ std::shared_ptr<sc::SparklineGroup> 
ScDocument::SearchSparklineGroup(tools::Guid
 {
     for (auto const& rTable : maTabs)
     {
+        if (!rTable)
+            continue;
+
         auto& rSparklineList = rTable->GetSparklineList();
 
         for (auto const& pSparklineGroup : rSparklineList.getSparklineGroups())
diff --git a/sc/source/ui/sparklines/SparklineList.cxx 
b/sc/source/ui/sparklines/SparklineList.cxx
index 744a58bce66e..7ee52ac74e27 100644
--- a/sc/source/ui/sparklines/SparklineList.cxx
+++ b/sc/source/ui/sparklines/SparklineList.cxx
@@ -31,7 +31,8 @@ std::vector<std::shared_ptr<SparklineGroup>> 
SparklineList::getSparklineGroups()
 
     for (auto iterator = m_aSparklineGroups.begin(); iterator != 
m_aSparklineGroups.end();)
     {
-        if (auto pSparklineGroup = iterator->lock())
+        auto pWeakGroup = *iterator;
+        if (auto pSparklineGroup = pWeakGroup.lock())
         {
             toReturn.push_back(pSparklineGroup);
             iterator++;
commit d1574914458243d5e9c5fc7a114d873ba492803e
Author:     Tomaž Vajngerl <[email protected]>
AuthorDate: Sun Apr 3 13:21:59 2022 +0900
Commit:     Tomaž Vajngerl <[email protected]>
CommitDate: Sun Apr 3 13:22:52 2022 +0900

    sc: put SparklineList implementation into SparklineList.cxx
    
    Change-Id: I309087a27ea0bc297c9bea9b2b8945579e055f4d

diff --git a/sc/Library_sc.mk b/sc/Library_sc.mk
index 743125bfef10..519daa881530 100644
--- a/sc/Library_sc.mk
+++ b/sc/Library_sc.mk
@@ -517,6 +517,7 @@ $(eval $(call gb_Library_add_exception_objects,sc,\
     sc/source/ui/sparklines/SparklineAttributes \
     sc/source/ui/sparklines/SparklineData \
     sc/source/ui/sparklines/SparklineGroup \
+    sc/source/ui/sparklines/SparklineList \
     sc/source/ui/StatisticsDialogs/AnalysisOfVarianceDialog \
     sc/source/ui/StatisticsDialogs/CorrelationDialog \
     sc/source/ui/StatisticsDialogs/CovarianceDialog \
diff --git a/sc/inc/SparklineList.hxx b/sc/inc/SparklineList.hxx
index 2c51ab296a84..ae9b95ffaaef 100644
--- a/sc/inc/SparklineList.hxx
+++ b/sc/inc/SparklineList.hxx
@@ -14,7 +14,6 @@
 #include <memory>
 #include <map>
 
-#include "rangelst.hxx"
 #include <Sparkline.hxx>
 #include <SparklineGroup.hxx>
 
@@ -34,66 +33,14 @@ private:
         m_aSparklineGroupMap;
 
 public:
-    SparklineList() {}
+    SparklineList();
 
-    void addSparkline(std::shared_ptr<Sparkline> const& pSparkline)
-    {
-        auto pWeakGroup = 
std::weak_ptr<SparklineGroup>(pSparkline->getSparklineGroup());
+    void addSparkline(std::shared_ptr<Sparkline> const& pSparkline);
 
-        auto[iterator, bInserted]
-            = m_aSparklineGroupMap.try_emplace(pWeakGroup, 
std::vector<std::weak_ptr<Sparkline>>());
-        iterator->second.push_back(std::weak_ptr<Sparkline>(pSparkline));
-        if (bInserted)
-            m_aSparklineGroups.push_back(pWeakGroup);
-    }
-
-    std::vector<std::shared_ptr<SparklineGroup>> getSparklineGroups()
-    {
-        std::vector<std::shared_ptr<SparklineGroup>> toReturn;
-
-        for (auto iterator = m_aSparklineGroups.begin(); iterator != 
m_aSparklineGroups.end();)
-        {
-            if (auto pSparklineGroup = iterator->lock())
-            {
-                toReturn.push_back(pSparklineGroup);
-                iterator++;
-            }
-            else
-            {
-                iterator = m_aSparklineGroups.erase(iterator);
-            }
-        }
-        return toReturn;
-    }
+    std::vector<std::shared_ptr<SparklineGroup>> getSparklineGroups();
 
     std::vector<std::shared_ptr<Sparkline>>
-    getSparklinesFor(std::shared_ptr<SparklineGroup> const& pSparklineGroup)
-    {
-        std::vector<std::shared_ptr<Sparkline>> toReturn;
-
-        std::weak_ptr<SparklineGroup> pWeakGroup(pSparklineGroup);
-        auto iteratorGroup = m_aSparklineGroupMap.find(pWeakGroup);
-
-        if (iteratorGroup == m_aSparklineGroupMap.end())
-            return toReturn;
-
-        auto& rWeakSparklines = iteratorGroup->second;
-
-        for (auto iterator = rWeakSparklines.begin(); iterator != 
rWeakSparklines.end();)
-        {
-            if (auto aSparkline = iterator->lock())
-            {
-                toReturn.push_back(aSparkline);
-                iterator++;
-            }
-            else
-            {
-                iterator = rWeakSparklines.erase(iterator);
-            }
-        }
-
-        return toReturn;
-    }
+    getSparklinesFor(std::shared_ptr<SparklineGroup> const& pSparklineGroup);
 };
 
 } // end sc
diff --git a/sc/source/ui/sparklines/SparklineList.cxx 
b/sc/source/ui/sparklines/SparklineList.cxx
new file mode 100644
index 000000000000..744a58bce66e
--- /dev/null
+++ b/sc/source/ui/sparklines/SparklineList.cxx
@@ -0,0 +1,78 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ */
+
+#include <SparklineList.hxx>
+
+namespace sc
+{
+SparklineList::SparklineList() = default;
+
+void SparklineList::addSparkline(std::shared_ptr<Sparkline> const& pSparkline)
+{
+    auto pWeakGroup = 
std::weak_ptr<SparklineGroup>(pSparkline->getSparklineGroup());
+
+    auto[iterator, bInserted]
+        = m_aSparklineGroupMap.try_emplace(pWeakGroup, 
std::vector<std::weak_ptr<Sparkline>>());
+    iterator->second.push_back(std::weak_ptr<Sparkline>(pSparkline));
+    if (bInserted)
+        m_aSparklineGroups.push_back(pWeakGroup);
+}
+
+std::vector<std::shared_ptr<SparklineGroup>> 
SparklineList::getSparklineGroups()
+{
+    std::vector<std::shared_ptr<SparklineGroup>> toReturn;
+
+    for (auto iterator = m_aSparklineGroups.begin(); iterator != 
m_aSparklineGroups.end();)
+    {
+        if (auto pSparklineGroup = iterator->lock())
+        {
+            toReturn.push_back(pSparklineGroup);
+            iterator++;
+        }
+        else
+        {
+            iterator = m_aSparklineGroups.erase(iterator);
+        }
+    }
+    return toReturn;
+}
+
+std::vector<std::shared_ptr<Sparkline>>
+SparklineList::getSparklinesFor(std::shared_ptr<SparklineGroup> const& 
pSparklineGroup)
+{
+    std::vector<std::shared_ptr<Sparkline>> toReturn;
+
+    std::weak_ptr<SparklineGroup> pWeakGroup(pSparklineGroup);
+    auto iteratorGroup = m_aSparklineGroupMap.find(pWeakGroup);
+
+    if (iteratorGroup == m_aSparklineGroupMap.end())
+        return toReturn;
+
+    auto& rWeakSparklines = iteratorGroup->second;
+
+    for (auto iterator = rWeakSparklines.begin(); iterator != 
rWeakSparklines.end();)
+    {
+        if (auto aSparkline = iterator->lock())
+        {
+            toReturn.push_back(aSparkline);
+            iterator++;
+        }
+        else
+        {
+            iterator = rWeakSparklines.erase(iterator);
+        }
+    }
+
+    return toReturn;
+}
+
+} // end sc
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Reply via email to