Commits

Bjoern Michaelsen  committed cdb3b9a

itemsets: using ::std::for_each where possible

  • Participants
  • Parent commits 37df708
  • Branches new_itemsets

Comments (0)

Files changed (1)

File svtools/source/items1/itemset.cxx

                 { return m_bExists; }
     };
 
+    static bool lcl_InRange(const vector<sal_uInt16>& rWhichRanges, const sal_uInt16 nWhichId)
+    {
+        if(rWhichRanges.empty())
+            return false;
+        vector<sal_uInt16>::const_iterator pInsertPos = lower_bound(
+            rWhichRanges.begin(),
+            rWhichRanges.end()-1,
+            nWhichId);
+        return ((pInsertPos-rWhichRanges.begin())%2) || *pInsertPos == nWhichId;
+    };
+
+    class SVL_DLLPRIVATE ItemPutter
+    {
+        public:
+            ItemPutter(ItemVector* const pItems, SfxItemPool* const pPool, const vector<sal_uInt16>* const pWhichRanges, const bool bInvalidAsDefault)
+                : m_pItems(pItems), m_pPool(pPool), m_pWhichRanges(pWhichRanges), m_bInvalidAsDefault(bInvalidAsDefault), m_pLastModification(m_pItems->begin()), m_isModified(false)
+            {};
+            void operator()(const ItemEntry& rItemEntry)
+            {
+                if(!lcl_InRange(*m_pWhichRanges, rItemEntry.GetWhich()))
+                    return;
+                ItemPosition aItemPosition(m_pItems, rItemEntry.GetWhich(), m_pLastModification, m_pItems->end());
+                SfxPoolItem* pSourceItem = const_cast<SfxPoolItem*>(rItemEntry.GetItem());
+                if(aItemPosition.DoesExist())
+                {
+                    if(IsInvalidItem(rItemEntry.GetItem()))
+                    {
+                        if(m_bInvalidAsDefault)
+                        {
+                            m_pLastModification = aItemPosition.ReleaseAndErase(m_pPool);
+                            m_isModified = true;
+                        }
+                        else
+                            aItemPosition.Reset(lcl_kInvalidItem, m_pPool);
+                    }
+                    else if(aItemPosition.GetItem() != pSourceItem
+                        && *aItemPosition.GetItem() != *pSourceItem)
+                    {
+                        m_pLastModification = aItemPosition.Put(pSourceItem, m_pPool);
+                        m_isModified = true;
+                    }
+                }
+                else
+                {
+                    m_pLastModification = aItemPosition.Create(pSourceItem, m_pPool);
+                    m_isModified = true;
+                }
+            };
+            bool DidModify() { return m_isModified; };
+        private:
+            ItemVector* const m_pItems;
+            SfxItemPool* const m_pPool;
+            const vector<sal_uInt16>* const m_pWhichRanges;
+            const bool m_bInvalidAsDefault;
+            ItemVector::iterator m_pLastModification;
+            bool m_isModified;
+    };
+
     class SVL_DLLPRIVATE InvalidItemClearer
     {
         public:
             InvalidItemClearer(ItemVector* pItems, SfxItemPool* pPool, const bool bHardDefault)
                 : m_pItems(pItems), m_pPool(pPool), m_bHardDefault(bHardDefault)
             {};
-            void operator()(const ItemEntry& rItemEntry)
+            void operator()(const ItemEntry& rItemEntry) const
             {
                 if(IsInvalidItem(rItemEntry.GetItem()))
                 {
             const bool m_bHardDefault;
     };
 
-    static bool lcl_InRange(const vector<sal_uInt16>& rWhichRanges, const sal_uInt16 nWhichId)
+    class SVL_DLLPRIVATE SetCandidatePutter
     {
-        if(rWhichRanges.empty())
-            return false;
-        vector<sal_uInt16>::const_iterator pInsertPos = lower_bound(
-            rWhichRanges.begin(),
-            rWhichRanges.end()-1,
-            nWhichId);
-        return ((pInsertPos-rWhichRanges.begin())%2) || *pInsertPos == nWhichId;
+        public:
+            SetCandidatePutter(ItemVector* pItems, SfxItemPool* pPool, const vector<sal_uInt16>* const pWhichRanges, const bool bAllCandidatesInRange)
+                : m_pItems(pItems), m_pPool(pPool), m_pWhichRanges(pWhichRanges), m_bAllCandidatesInRange(bAllCandidatesInRange), m_isModified(false)
+            {};
+            void operator()(const ItemEntry& rItemEntry)
+            {
+                SfxPoolItem* const pItem = const_cast<SfxPoolItem*>(rItemEntry.GetItem());
+                if(!IsInvalidItem(pItem) && pItem->Which()
+                    && (m_bAllCandidatesInRange || lcl_InRange(*m_pWhichRanges, rItemEntry.GetWhich())))
+                {
+                    ItemEntry aItemEntry(rItemEntry.GetWhich());
+                    aItemEntry.SetItem(pItem, m_pPool);
+                    m_pItems->push_back(aItemEntry);
+                    m_isModified = true;
+                }
+            };
+            bool DidModify() { return m_isModified; };
+        private:
+            ItemVector* const m_pItems;
+            SfxItemPool* const m_pPool;
+            const vector<sal_uInt16>* const m_pWhichRanges;
+            const bool m_bAllCandidatesInRange;
+            bool m_isModified;
     };
 
 #if DEBUG
 
 bool SfxItemSet::Put(const SfxItemSet& rSet, bool bInvalidAsDefault)
 {
-    bool isModified = false;
-    ItemVector::iterator pLastModification = m_pItems->begin();
-    for(ItemVector::const_iterator pItemEntry = rSet.m_pItems->begin();
-        pItemEntry != rSet.m_pItems->end();
-        ++pItemEntry)
-    {
-        if(!lcl_InRange(m_vWhichRanges, pItemEntry->GetWhich()))
-            continue;
-        ItemPosition aItemPosition(m_pItems.get(), pItemEntry->GetWhich(), pLastModification, m_pItems->end());
-        SfxPoolItem* pSourceItem = const_cast<SfxPoolItem*>(pItemEntry->GetItem());
-        if(aItemPosition.DoesExist())
-        {
-            if(IsInvalidItem(pItemEntry->GetItem()))
-            {
-                if(bInvalidAsDefault)
-                {
-                    pLastModification = aItemPosition.ReleaseAndErase(m_pPool);
-                    isModified = true;
-                }
-                else
-                    aItemPosition.Reset(lcl_kInvalidItem, m_pPool);
-            }
-            else if(aItemPosition.GetItem() != pSourceItem
-                && *aItemPosition.GetItem() != *pSourceItem)
-            {
-                pLastModification = aItemPosition.Put(pSourceItem, m_pPool);
-                isModified = true;
-            }
-        }
-        else
-        {
-            pLastModification = aItemPosition.Create(pSourceItem, m_pPool);
-            isModified = true;
-        }
-    }
-    return isModified;
+    ItemPutter aPutter(m_pItems.get(), m_pPool, &m_vWhichRanges, bInvalidAsDefault);
+    for_each(rSet.m_pItems->begin(), rSet.m_pItems->end(), aPutter);
+    return aPutter.DidModify();
 }
 
 void SfxItemSet::PutExtended(const SfxItemSet& rSet, SfxItemState eDontCareAs, SfxItemState eDefaultAs)
             ItemEntry(*(m_vWhichRanges.end()-2)));
     }
     ClearItem();
-    bool isModified = false;
-    for(ItemVector::const_iterator pItemEntry = pCandidatesBegin;
-        pItemEntry != pCandidatesEnd;
-        ++pItemEntry)
-    {
-        SfxPoolItem* const pItem = const_cast<SfxPoolItem*>(pItemEntry->GetItem());
-        if(!IsInvalidItem(pItem) && pItem->Which()
-            && (bAllCandidatesInRange || lcl_InRange(m_vWhichRanges, pItemEntry->GetWhich())))
-        {
-            ItemEntry aItemEntry(pItemEntry->GetWhich());
-            aItemEntry.SetItem(pItem, m_pPool);
-            m_pItems->push_back(aItemEntry);
-            isModified = true;
-        }
-    }
-    return isModified;
+    SetCandidatePutter aPutter(m_pItems.get(), m_pPool, &m_vWhichRanges, bAllCandidatesInRange);
+    for_each(pCandidatesBegin, pCandidatesEnd, aPutter);
+    return aPutter.DidModify();
 }
 
 const SfxPoolItem* SfxItemSet::GetItem(sal_uInt16 nId, bool bSrchInParent, TypeId aItemType) const