1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
28 #include <o3tl/sorted_vector.hxx>
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
);
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
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
;
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
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?" );
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
);
88 if (it
== maSortablePoolItems
.end())
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
)
112 for (const SfxPoolItem
* p
: maPoolItemSet
)
118 void erase(o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator 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
);
129 if (sortIt
== maSortablePoolItems
.end())
131 assert(false && "did not find item?");
134 if (*pNeedle
< **sortIt
)
136 assert(false && "did not find item?");
139 // need to compare by pointer here, since we might have duplicates
140 if (*sortIt
== pNeedle
)
142 maSortablePoolItems
.erase(sortIt
);
148 maPoolItemSet
.erase(it
);
152 struct SfxItemPool_Impl
155 std::vector
<SfxPoolItemArray_Impl
> maPoolItemArrays
;
157 std::vector
<SfxPoolItem
*> maPoolDefaults
;
158 std::vector
<SfxPoolItem
*>* mpStaticDefaults
;
159 SfxItemPool
* mpMaster
;
160 rtl::Reference
<SfxItemPool
> mpSecondary
;
161 WhichRangesContainer mpPoolRanges
;
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)
174 , eDefMetric(MapUnit::MapCM
)
176 DBG_ASSERT(mnStart
, "Start-Which-Id must be greater 0" );
186 maPoolItemArrays
.clear();
187 maPoolDefaults
.clear();
188 mpPoolRanges
.reset();
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: */