LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / svl / source / inc / poolio.hxx
blobb2819cc46bd57328c5c113cc2faed96df4d1f796
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>
30 class SfxPoolItem;
31 class SfxItemPoolUser;
33 const sal_uInt32 SFX_ITEMS_DEFAULT = 0xfffffffe;
35 static bool CompareSortablePoolItems(SfxPoolItem const* lhs, SfxPoolItem const* rhs)
37 return (*lhs) < (*rhs);
39 /**
40 * This array contains a set of SfxPoolItems, if those items are
41 * poolable then each item has a unique set of properties, and we
42 * often search linearly to ensure uniqueness. If they are
43 * non-poolable we maintain an (often large) list of pointers.
45 struct SfxPoolItemArray_Impl
47 private:
48 o3tl::sorted_vector<SfxPoolItem*> maPoolItemSet;
49 // In some cases, e.g. subclasses of NameOrIndex, the parent class (NameOrIndex) is sortable,
50 // but the subclasses do not define an operator<, which means that we don't get an ordering
51 // strong enough to enforce uniqueness purely with operator<, which means we need to do
52 // a partial scan with operator==
53 std::vector<SfxPoolItem*> maSortablePoolItems;
54 public:
55 o3tl::sorted_vector<SfxPoolItem*>::const_iterator begin() const { return maPoolItemSet.begin(); }
56 o3tl::sorted_vector<SfxPoolItem*>::const_iterator end() const { return maPoolItemSet.end(); }
57 /// clear array of PoolItem variants after all PoolItems are deleted
58 /// or all ref counts are decreased
59 void clear();
60 size_t size() const {return maPoolItemSet.size();}
61 bool empty() const {return maPoolItemSet.empty();}
62 o3tl::sorted_vector<SfxPoolItem*>::const_iterator find(SfxPoolItem* pItem) const { return maPoolItemSet.find(pItem); }
63 void insert(SfxPoolItem* pItem)
65 bool bInserted = maPoolItemSet.insert(pItem).second;
66 assert( bInserted && "duplicate item?" );
67 (void)bInserted;
69 if (pItem->IsSortable())
71 // bail early if someone modified one of these things underneath me
72 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
74 auto it = std::lower_bound(maSortablePoolItems.begin(), maSortablePoolItems.end(), pItem, CompareSortablePoolItems);
75 maSortablePoolItems.insert(maSortablePoolItems.begin() + (it - maSortablePoolItems.begin()), pItem);
78 const SfxPoolItem* findByLessThan(const SfxPoolItem* pNeedle) const
80 // bail early if someone modified one of these things underneath me
81 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
82 assert( maPoolItemSet.empty() || maPoolItemSet.front()->IsSortable() );
84 auto it = std::lower_bound(maSortablePoolItems.begin(), maSortablePoolItems.end(), pNeedle, CompareSortablePoolItems);
85 for (;;)
87 if (it == maSortablePoolItems.end())
88 return nullptr;
89 if (**it < *pNeedle)
90 return nullptr;
91 if (*pNeedle == **it)
92 return *it;
93 ++it;
96 std::vector<const SfxPoolItem*> findSurrogateRange(const SfxPoolItem* pNeedle) const
98 std::vector<const SfxPoolItem*> rv;
99 if (!maSortablePoolItems.empty())
101 // bail early if someone modified one of these things underneath me
102 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
104 auto range = std::equal_range(maSortablePoolItems.begin(), maSortablePoolItems.end(), pNeedle, CompareSortablePoolItems);
105 rv.reserve(std::distance(range.first, range.second));
106 for (auto it = range.first; it != range.second; ++it)
107 rv.push_back(*it);
109 else
111 for (const SfxPoolItem* p : maPoolItemSet)
112 if (*pNeedle == *p)
113 rv.push_back(p);
115 return rv;
117 void erase(o3tl::sorted_vector<SfxPoolItem*>::const_iterator it)
119 auto pNeedle = *it;
120 if ((*it)->IsSortable())
122 // bail early if someone modified one of these things underneath me
123 assert( std::is_sorted_until(maSortablePoolItems.begin(), maSortablePoolItems.end(), CompareSortablePoolItems) == maSortablePoolItems.end());
125 auto sortIt = std::lower_bound(maSortablePoolItems.begin(), maSortablePoolItems.end(), pNeedle, CompareSortablePoolItems);
126 for (;;)
128 if (sortIt == maSortablePoolItems.end())
130 assert(false && "did not find item?");
131 break;
133 if (**sortIt < *pNeedle)
135 assert(false && "did not find item?");
136 break;
138 // need to compare by pointer here, since we might have duplicates
139 if (*sortIt == pNeedle)
141 maSortablePoolItems.erase(sortIt);
142 break;
144 ++sortIt;
147 maPoolItemSet.erase(it);
151 struct SfxItemPool_Impl
153 SfxBroadcaster aBC;
154 std::vector<SfxPoolItemArray_Impl> maPoolItemArrays;
155 OUString aName;
156 std::vector<SfxPoolItem*> maPoolDefaults;
157 std::vector<SfxPoolItem*>* mpStaticDefaults;
158 SfxItemPool* mpMaster;
159 rtl::Reference<SfxItemPool> mpSecondary;
160 WhichRangesContainer mpPoolRanges;
161 sal_uInt16 mnStart;
162 sal_uInt16 mnEnd;
163 MapUnit eDefMetric;
165 SfxItemPool_Impl( SfxItemPool* pMaster, const OUString& rName, sal_uInt16 nStart, sal_uInt16 nEnd )
166 : maPoolItemArrays(nEnd - nStart + 1)
167 , aName(rName)
168 , maPoolDefaults(nEnd - nStart + 1)
169 , mpStaticDefaults(nullptr)
170 , mpMaster(pMaster)
171 , mnStart(nStart)
172 , mnEnd(nEnd)
173 , eDefMetric(MapUnit::MapCM)
175 DBG_ASSERT(mnStart, "Start-Which-Id must be greater 0" );
178 ~SfxItemPool_Impl()
180 DeleteItems();
183 void DeleteItems()
185 maPoolItemArrays.clear();
186 maPoolDefaults.clear();
187 mpPoolRanges.reset();
190 // unit testing
191 friend class PoolItemTest;
192 static SfxItemPool_Impl *GetImpl(SfxItemPool const *pPool) { return pPool->pImpl.get(); }
196 #define SFX_ITEMPOOL_VER_MAJOR sal_uInt8(2)
197 #define SFX_ITEMPOOL_VER_MINOR sal_uInt8(0)
199 #define SFX_ITEMPOOL_TAG_STARTPOOL_4 sal_uInt16(0x1111)
200 #define SFX_ITEMPOOL_TAG_STARTPOOL_5 sal_uInt16(0xBBBB)
201 #define SFX_ITEMPOOL_TAG_TRICK4OLD sal_uInt16(0xFFFF)
203 #define SFX_ITEMPOOL_REC sal_uInt8(0x01)
204 #define SFX_ITEMPOOL_REC_HEADER sal_uInt8(0x10)
205 #define SFX_ITEMPOOL_REC_VERSIONMAP sal_uInt16(0x0020)
206 #define SFX_ITEMPOOL_REC_WHICHIDS sal_uInt16(0x0030)
207 #define SFX_ITEMPOOL_REC_ITEMS sal_uInt16(0x0040)
208 #define SFX_ITEMPOOL_REC_DEFAULTS sal_uInt16(0x0050)
210 #endif // INCLUDED_SVL_SOURCE_INC_POOLIO_HXX
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */