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 <sal/log.hxx>
24 #include <svl/itempool.hxx>
25 #include <svl/SfxBroadcaster.hxx>
26 #include <tools/debug.hxx>
29 #include <o3tl/sorted_vector.hxx>
32 class SfxItemPoolUser
;
34 static 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 maPoolItemSet
.insert(pItem
);
67 if (pItem
->IsSortable())
69 // bail early if someone modified one of these things underneath me
70 assert( std::is_sorted_until(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), CompareSortablePoolItems
) == maSortablePoolItems
.end());
72 auto it
= std::lower_bound(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), pItem
, CompareSortablePoolItems
);
73 maSortablePoolItems
.insert(maSortablePoolItems
.begin() + (it
- maSortablePoolItems
.begin()), pItem
);
76 const SfxPoolItem
* findByLessThan(const SfxPoolItem
* pNeedle
) const
78 // bail early if someone modified one of these things underneath me
79 assert( std::is_sorted_until(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), CompareSortablePoolItems
) == maSortablePoolItems
.end());
80 assert( maPoolItemSet
.empty() || maPoolItemSet
.front()->IsSortable() );
82 auto it
= std::lower_bound(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), pNeedle
, CompareSortablePoolItems
);
85 if (it
== maSortablePoolItems
.end())
94 std::vector
<const SfxPoolItem
*> findSurrogateRange(const SfxPoolItem
* pNeedle
) const
96 std::vector
<const SfxPoolItem
*> rv
;
97 if (!maSortablePoolItems
.empty())
99 // bail early if someone modified one of these things underneath me
100 assert( std::is_sorted_until(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), CompareSortablePoolItems
) == maSortablePoolItems
.end());
102 auto range
= std::equal_range(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), pNeedle
, CompareSortablePoolItems
);
103 rv
.reserve(std::distance(range
.first
, range
.second
));
104 for (auto it
= range
.first
; it
!= range
.second
; ++it
)
109 for (const SfxPoolItem
* p
: maPoolItemSet
)
115 void erase(o3tl::sorted_vector
<SfxPoolItem
*>::const_iterator it
)
118 if ((*it
)->IsSortable())
120 // bail early if someone modified one of these things underneath me
121 assert( std::is_sorted_until(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), CompareSortablePoolItems
) == maSortablePoolItems
.end());
123 auto sortIt
= std::lower_bound(maSortablePoolItems
.begin(), maSortablePoolItems
.end(), pNeedle
, CompareSortablePoolItems
);
126 if (sortIt
== maSortablePoolItems
.end())
128 assert(false && "did not find item?");
131 if (**sortIt
< *pNeedle
)
133 assert(false && "did not find item?");
136 // need to compare by pointer here, since we might have duplicates
137 if (*sortIt
== pNeedle
)
139 maSortablePoolItems
.erase(sortIt
);
145 maPoolItemSet
.erase(it
);
149 struct SfxItemPool_Impl
152 std::vector
<SfxPoolItemArray_Impl
> maPoolItemArrays
;
153 std::vector
<SfxItemPoolUser
*> maSfxItemPoolUsers
; /// ObjectUser section
155 std::vector
<SfxPoolItem
*> maPoolDefaults
;
156 std::vector
<SfxPoolItem
*>* mpStaticDefaults
;
157 SfxItemPool
* mpMaster
;
158 SfxItemPool
* mpSecondary
;
159 std::unique_ptr
<sal_uInt16
[]> mpPoolRanges
;
164 SfxItemPool_Impl( SfxItemPool
* pMaster
, const OUString
& rName
, sal_uInt16 nStart
, sal_uInt16 nEnd
)
165 : maPoolItemArrays(nEnd
- nStart
+ 1)
167 , maPoolDefaults(nEnd
- nStart
+ 1)
168 , mpStaticDefaults(nullptr)
170 , mpSecondary(nullptr)
173 , eDefMetric(MapUnit::MapCM
)
175 DBG_ASSERT(mnStart
, "Start-Which-Id must be greater 0" );
185 maPoolItemArrays
.clear();
186 maPoolDefaults
.clear();
187 mpPoolRanges
.reset();
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: */