attempt to fix macos jenkins hangs - part 3
[LibreOffice.git] / svl / source / inc / poolio.hxx
blobcc406f31e0a09b3dd35281482fa2bfd889d64cb2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_SVL_SOURCE_INC_POOLIO_HXX
21 #define INCLUDED_SVL_SOURCE_INC_POOLIO_HXX
23 #include <rtl/ref.hxx>
24 #include <svl/itempool.hxx>
25 #include <svl/SfxBroadcaster.hxx>
26 #include <tools/debug.hxx>
27 #include <memory>
28 #include <o3tl/sorted_vector.hxx>
29 #include <utility>
31 class SfxPoolItem;
32 class SfxItemPoolUser;
34 const sal_uInt32 SFX_ITEMS_DEFAULT = 0xfffffffe;
36 static bool CompareSortablePoolItems(SfxPoolItem const* lhs, SfxPoolItem const* rhs)
38 return (*lhs) < (*rhs);
40 /**
41 * This array contains a set of SfxPoolItems, if those items are
42 * poolable then each item has a unique set of properties, and we
43 * often search linearly to ensure uniqueness. If they are
44 * non-poolable we maintain an (often large) list of pointers.
46 struct SfxPoolItemArray_Impl
48 private:
49 o3tl::sorted_vector<SfxPoolItem*> maPoolItemSet;
50 // In some cases, e.g. subclasses of NameOrIndex, the parent class (NameOrIndex) is sortable,
51 // but the subclasses do not define an operator<, which means that we don't get an ordering
52 // strong enough to enforce uniqueness purely with operator<, which means we need to do
53 // a partial scan with operator==
54 std::vector<SfxPoolItem*> maSortablePoolItems;
55 public:
56 o3tl::sorted_vector<SfxPoolItem*>::const_iterator begin() const { return maPoolItemSet.begin(); }
57 o3tl::sorted_vector<SfxPoolItem*>::const_iterator end() const { return maPoolItemSet.end(); }
58 /// clear array of PoolItem variants after all PoolItems are deleted
59 /// or all ref counts are decreased
60 void clear();
61 size_t size() const {return maPoolItemSet.size();}
62 bool empty() const {return maPoolItemSet.empty();}
63 o3tl::sorted_vector<SfxPoolItem*>::const_iterator find(SfxPoolItem* pItem) const { return maPoolItemSet.find(pItem); }
64 void insert(SfxPoolItem* pItem)
66 bool bInserted = maPoolItemSet.insert(pItem).second;
67 assert( bInserted && "duplicate item?" );
68 (void)bInserted;
70 if (pItem->IsSortable())
72 // bail early if someone modified one of these things underneath me
73 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
75 auto it = std::lower_bound(maSortablePoolItems.begin(), maSortablePoolItems.end(), pItem, CompareSortablePoolItems);
76 maSortablePoolItems.insert(maSortablePoolItems.begin() + (it - maSortablePoolItems.begin()), pItem);
79 const SfxPoolItem* findByLessThan(const SfxPoolItem* pNeedle) const
81 // bail early if someone modified one of these things underneath me
82 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
83 assert( maPoolItemSet.empty() || maPoolItemSet.front()->IsSortable() );
85 auto it = std::lower_bound(maSortablePoolItems.begin(), maSortablePoolItems.end(), pNeedle, CompareSortablePoolItems);
86 for (;;)
88 if (it == maSortablePoolItems.end())
89 return nullptr;
90 if (*pNeedle < **it)
91 return nullptr;
92 if (*pNeedle == **it)
93 return *it;
94 ++it;
97 std::vector<const SfxPoolItem*> findSurrogateRange(const SfxPoolItem* pNeedle) const
99 std::vector<const SfxPoolItem*> rv;
100 if (!maSortablePoolItems.empty())
102 // bail early if someone modified one of these things underneath me
103 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
105 auto range = std::equal_range(maSortablePoolItems.begin(), maSortablePoolItems.end(), pNeedle, CompareSortablePoolItems);
106 rv.reserve(std::distance(range.first, range.second));
107 for (auto it = range.first; it != range.second; ++it)
108 rv.push_back(*it);
110 else
112 for (const SfxPoolItem* p : maPoolItemSet)
113 if (*pNeedle == *p)
114 rv.push_back(p);
116 return rv;
118 void erase(o3tl::sorted_vector<SfxPoolItem*>::const_iterator it)
120 auto pNeedle = *it;
121 if ((*it)->IsSortable())
123 // bail early if someone modified one of these things underneath me
124 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
126 auto sortIt = std::lower_bound(maSortablePoolItems.begin(), maSortablePoolItems.end(), pNeedle, CompareSortablePoolItems);
127 for (;;)
129 if (sortIt == maSortablePoolItems.end())
131 assert(false && "did not find item?");
132 break;
134 if (*pNeedle < **sortIt)
136 assert(false && "did not find item?");
137 break;
139 // need to compare by pointer here, since we might have duplicates
140 if (*sortIt == pNeedle)
142 maSortablePoolItems.erase(sortIt);
143 break;
145 ++sortIt;
148 maPoolItemSet.erase(it);
152 struct SfxItemPool_Impl
154 SfxBroadcaster aBC;
155 std::vector<SfxPoolItemArray_Impl> maPoolItemArrays;
156 OUString aName;
157 std::vector<SfxPoolItem*> maPoolDefaults;
158 std::vector<SfxPoolItem*>* mpStaticDefaults;
159 SfxItemPool* mpMaster;
160 rtl::Reference<SfxItemPool> mpSecondary;
161 WhichRangesContainer mpPoolRanges;
162 sal_uInt16 mnStart;
163 sal_uInt16 mnEnd;
164 MapUnit eDefMetric;
166 SfxItemPool_Impl( SfxItemPool* pMaster, OUString _aName, sal_uInt16 nStart, sal_uInt16 nEnd )
167 : maPoolItemArrays(nEnd - nStart + 1)
168 , aName(std::move(_aName))
169 , maPoolDefaults(nEnd - nStart + 1)
170 , mpStaticDefaults(nullptr)
171 , mpMaster(pMaster)
172 , mnStart(nStart)
173 , mnEnd(nEnd)
174 , eDefMetric(MapUnit::MapCM)
176 DBG_ASSERT(mnStart, "Start-Which-Id must be greater 0" );
179 ~SfxItemPool_Impl()
181 DeleteItems();
184 void DeleteItems()
186 maPoolItemArrays.clear();
187 maPoolDefaults.clear();
188 mpPoolRanges.reset();
191 // unit testing
192 friend class PoolItemTest;
193 static SfxItemPool_Impl *GetImpl(SfxItemPool const *pPool) { return pPool->pImpl.get(); }
197 #define SFX_ITEMPOOL_VER_MAJOR sal_uInt8(2)
198 #define SFX_ITEMPOOL_VER_MINOR sal_uInt8(0)
200 #define SFX_ITEMPOOL_TAG_STARTPOOL_4 sal_uInt16(0x1111)
201 #define SFX_ITEMPOOL_TAG_STARTPOOL_5 sal_uInt16(0xBBBB)
202 #define SFX_ITEMPOOL_TAG_TRICK4OLD sal_uInt16(0xFFFF)
204 #define SFX_ITEMPOOL_REC sal_uInt8(0x01)
205 #define SFX_ITEMPOOL_REC_HEADER sal_uInt8(0x10)
206 #define SFX_ITEMPOOL_REC_VERSIONMAP sal_uInt16(0x0020)
207 #define SFX_ITEMPOOL_REC_WHICHIDS sal_uInt16(0x0030)
208 #define SFX_ITEMPOOL_REC_ITEMS sal_uInt16(0x0040)
209 #define SFX_ITEMPOOL_REC_DEFAULTS sal_uInt16(0x0050)
211 #endif // INCLUDED_SVL_SOURCE_INC_POOLIO_HXX
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */