Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_SVL_SOURCE_INC_POOLIO_HXX
21 : #define INCLUDED_SVL_SOURCE_INC_POOLIO_HXX
22 :
23 : #include <svl/SfxBroadcaster.hxx>
24 : #include <boost/shared_ptr.hpp>
25 : #include <deque>
26 : #include <unordered_map>
27 : #include <vector>
28 :
29 : class SfxPoolItem;
30 : class SfxItemPoolUser;
31 :
32 : #ifndef DELETEZ
33 : #define DELETEZ(pPtr) { delete pPtr; pPtr = 0; }
34 : #endif
35 :
36 : static const sal_uInt32 SFX_ITEMS_DIRECT = 0xffffffff;
37 : static const sal_uInt32 SFX_ITEMS_NULL = 0xfffffff0; // instead StoreSurrogate
38 : static const sal_uInt32 SFX_ITEMS_DEFAULT = 0xfffffffe;
39 :
40 : struct SfxPoolVersion_Impl
41 : {
42 : sal_uInt16 _nVer;
43 : sal_uInt16 _nStart, _nEnd;
44 : const sal_uInt16* _pMap;
45 :
46 374459 : SfxPoolVersion_Impl( sal_uInt16 nVer, sal_uInt16 nStart, sal_uInt16 nEnd,
47 : const sal_uInt16 *pMap )
48 : : _nVer( nVer ),
49 : _nStart( nStart ),
50 : _nEnd( nEnd ),
51 374459 : _pMap( pMap )
52 374459 : {}
53 222 : SfxPoolVersion_Impl( const SfxPoolVersion_Impl &rOrig )
54 : : _nVer( rOrig._nVer ),
55 : _nStart( rOrig._nStart ),
56 : _nEnd( rOrig._nEnd ),
57 222 : _pMap( rOrig._pMap )
58 222 : {}
59 : };
60 :
61 : typedef std::vector<SfxPoolItem*> SfxPoolItemArrayBase_Impl;
62 :
63 : typedef boost::shared_ptr< SfxPoolVersion_Impl > SfxPoolVersion_ImplPtr;
64 : typedef std::deque< SfxPoolVersion_ImplPtr > SfxPoolVersionArr_Impl;
65 :
66 : /**
67 : * This array contains a set of SfxPoolItems, if those items are
68 : * poolable then each item has a unique set of properties, and we
69 : * often search linearly to ensure uniqueness. If they are
70 : * non-poolable we maintain an (often large) list of pointers.
71 : */
72 516297 : struct SfxPoolItemArray_Impl: public SfxPoolItemArrayBase_Impl
73 : {
74 : typedef std::vector<sal_uInt32> FreeList;
75 : typedef std::unordered_map<SfxPoolItem*,sal_uInt32> Hash;
76 :
77 : public:
78 : /// Track list of indices into our array that contain an empty slot
79 : FreeList maFree;
80 : /// Hash of SfxPoolItem pointer to index into our array that contains that slot
81 : Hash maHash;
82 :
83 613733 : SfxPoolItemArray_Impl () {}
84 :
85 : /// re-build the list of free slots and hash from clean
86 : void SVL_DLLPUBLIC ReHash();
87 : };
88 :
89 : struct SfxItemPool_Impl
90 : {
91 : SfxBroadcaster aBC;
92 : std::vector<SfxPoolItemArray_Impl*> maPoolItems;
93 : std::vector<SfxItemPoolUser*> maSfxItemPoolUsers; /// ObjectUser section
94 : OUString aName;
95 : SfxPoolItem** ppPoolDefaults;
96 : SfxPoolItem** ppStaticDefaults;
97 : SfxItemPool* mpMaster;
98 : SfxItemPool* mpSecondary;
99 : sal_uInt16* mpPoolRanges;
100 : SfxPoolVersionArr_Impl aVersions;
101 : sal_uInt16 mnStart;
102 : sal_uInt16 mnEnd;
103 : sal_uInt16 mnFileFormatVersion;
104 : sal_uInt16 nVersion;
105 : sal_uInt16 nLoadingVersion;
106 : sal_uInt16 nInitRefCount; // 1, during load, may be 2
107 : sal_uInt16 nVerStart, nVerEnd; // WhichRange in versions
108 : sal_uInt16 nStoringStart, nStoringEnd; // Range to be saved
109 : sal_uInt8 nMajorVer, nMinorVer; // The Pool itself
110 : SfxMapUnit eDefMetric;
111 : bool bInSetItem;
112 : bool bStreaming; // in Load() or Store()
113 : bool mbPersistentRefCounts;
114 :
115 65966 : SfxItemPool_Impl( SfxItemPool* pMaster, const OUString& rName, sal_uInt16 nStart, sal_uInt16 nEnd )
116 65966 : : maPoolItems(nEnd - nStart + 1, static_cast<SfxPoolItemArray_Impl*>(NULL))
117 : , aName(rName)
118 65966 : , ppPoolDefaults(new SfxPoolItem* [nEnd - nStart + 1])
119 : , ppStaticDefaults(0)
120 : , mpMaster(pMaster)
121 : , mpSecondary(NULL)
122 : , mpPoolRanges(NULL)
123 : , mnStart(nStart)
124 : , mnEnd(nEnd)
125 : , mnFileFormatVersion(0)
126 : , nVersion(0)
127 : , nLoadingVersion(0)
128 : , nInitRefCount(0)
129 : , nVerStart(0)
130 : , nVerEnd(0)
131 : , nStoringStart(0)
132 : , nStoringEnd(0)
133 : , nMajorVer(0)
134 : , nMinorVer(0)
135 : , eDefMetric(SFX_MAPUNIT_CM)
136 : , bInSetItem(false)
137 : , bStreaming(false)
138 197898 : , mbPersistentRefCounts(false)
139 : {
140 : DBG_ASSERT(mnStart, "Start-Which-Id must be greater 0" );
141 65966 : memset( ppPoolDefaults, 0, sizeof( SfxPoolItem* ) * (nEnd - nStart + 1));
142 65966 : }
143 :
144 57027 : ~SfxItemPool_Impl()
145 57027 : {
146 57027 : DeleteItems();
147 57027 : }
148 :
149 114054 : void DeleteItems()
150 : {
151 114054 : std::vector<SfxPoolItemArray_Impl*>::iterator itr = maPoolItems.begin(), itrEnd = maPoolItems.end();
152 4438108 : for (; itr != itrEnd; ++itr)
153 4324054 : delete *itr;
154 114054 : maPoolItems.clear();
155 :
156 114054 : delete[] mpPoolRanges;
157 114054 : mpPoolRanges = NULL;
158 114054 : delete[] ppPoolDefaults;
159 114054 : ppPoolDefaults = NULL;
160 114054 : }
161 :
162 : void readTheItems(SvStream & rStream, sal_uInt32 nCount, sal_uInt16 nVersion,
163 : SfxPoolItem * pDefItem, SfxPoolItemArray_Impl ** pArr);
164 :
165 : // unit testing
166 : friend class PoolItemTest;
167 1 : static SfxItemPool_Impl *GetImpl(SfxItemPool *pPool) { return pPool->pImp; }
168 : };
169 :
170 :
171 : #define CHECK_FILEFORMAT( rStream, nTag ) \
172 : { sal_uInt16 nFileTag; \
173 : rStream.ReadUInt16( nFileTag ); \
174 : if ( nTag != nFileTag ) \
175 : { \
176 : OSL_FAIL( #nTag ); /*! s.u. */ \
177 : /*! Set error code and evaluate! */ \
178 : (rStream).SetError(SVSTREAM_FILEFORMAT_ERROR); \
179 : pImp->bStreaming = false; \
180 : return rStream; \
181 : } \
182 : }
183 :
184 : #define CHECK_FILEFORMAT_RELEASE( rStream, nTag, pPointer ) \
185 : { sal_uInt16 nFileTag; \
186 : rStream.ReadUInt16( nFileTag ); \
187 : if ( nTag != nFileTag ) \
188 : { \
189 : OSL_FAIL( #nTag ); /*! s.u. */ \
190 : /*! Set error code and evaluate! */ \
191 : (rStream).SetError(SVSTREAM_FILEFORMAT_ERROR); \
192 : pImp->bStreaming = false; \
193 : delete pPointer; \
194 : return rStream; \
195 : } \
196 : }
197 :
198 : #define CHECK_FILEFORMAT2( rStream, nTag1, nTag2 ) \
199 : { sal_uInt16 nFileTag; \
200 : rStream.ReadUInt16( nFileTag ); \
201 : if ( nTag1 != nFileTag && nTag2 != nFileTag ) \
202 : { \
203 : OSL_FAIL( #nTag1 ); /*! s.u. */ \
204 : /*! Set error code and evaluate! */ \
205 : (rStream).SetError(SVSTREAM_FILEFORMAT_ERROR); \
206 : pImp->bStreaming = false; \
207 : return rStream; \
208 : } \
209 : }
210 :
211 : #define SFX_ITEMPOOL_VER_MAJOR sal_uInt8(2)
212 : #define SFX_ITEMPOOL_VER_MINOR sal_uInt8(0)
213 :
214 : #define SFX_ITEMPOOL_TAG_STARTPOOL_4 sal_uInt16(0x1111)
215 : #define SFX_ITEMPOOL_TAG_STARTPOOL_5 sal_uInt16(0xBBBB)
216 : #define SFX_ITEMPOOL_TAG_TRICK4OLD sal_uInt16(0xFFFF)
217 :
218 : #define SFX_ITEMPOOL_REC sal_uInt8(0x01)
219 : #define SFX_ITEMPOOL_REC_HEADER sal_uInt8(0x10)
220 : #define SFX_ITEMPOOL_REC_VERSIONMAP sal_uInt16(0x0020)
221 : #define SFX_ITEMPOOL_REC_WHICHIDS sal_uInt16(0x0030)
222 : #define SFX_ITEMPOOL_REC_ITEMS sal_uInt16(0x0040)
223 : #define SFX_ITEMPOOL_REC_DEFAULTS sal_uInt16(0x0050)
224 :
225 :
226 :
227 : /** Read in a Unicode string from a streamed byte string representation.
228 :
229 : @param rStream Some (input) stream. Its Stream/TargetCharSets must
230 : be set to correct values!
231 :
232 : @return On success, returns the reconstructed Unicode string.
233 : */
234 : OUString readByteString(SvStream & rStream);
235 :
236 : /** Write a byte string representation of a Unicode string into a stream.
237 :
238 : @param rStream Some (output) stream. Its Stream/TargetCharSets must
239 : be set to correct values!
240 :
241 : @param rString Some Unicode string.
242 : */
243 : void writeByteString(SvStream & rStream, const OUString& rString);
244 :
245 : /** Read in a Unicode string from either a streamed Unicode or byte string
246 : representation.
247 :
248 : @param rStream Some (input) stream. If bUnicode is false, its
249 : Stream/TargetCharSets must be set to correct values!
250 :
251 : @param bUnicode Whether to read in a stream Unicode (true) or byte
252 : string (false) representation.
253 :
254 : @return On success, returns the reconstructed Unicode string.
255 : */
256 : OUString readUnicodeString(SvStream & rStream, bool bUnicode);
257 :
258 : /** Write a Unicode string representation of a Unicode string into a
259 : stream.
260 :
261 : @param rStream Some (output) stream.
262 :
263 : @param rString Some Unicode string.
264 : */
265 : void writeUnicodeString(SvStream & rStream, const OUString& rString);
266 :
267 :
268 : #endif // INCLUDED_SVL_SOURCE_INC_POOLIO_HXX
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|