xref: /aoo41x/main/svl/source/items/poolcach.cxx (revision 40df464e)
1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*40df464eSAndrew Rist  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*40df464eSAndrew Rist  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19*40df464eSAndrew Rist  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <limits.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #ifndef GCC
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <svl/itempool.hxx>
33cdf0e10cSrcweir #include <svl/itemset.hxx>
34cdf0e10cSrcweir #include <svl/poolcach.hxx>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir // STATIC DATA -----------------------------------------------------------
37cdf0e10cSrcweir 
DBG_NAME(SfxItemPoolCache)38cdf0e10cSrcweir DBG_NAME(SfxItemPoolCache)
39cdf0e10cSrcweir 
40cdf0e10cSrcweir //------------------------------------------------------------------------
41cdf0e10cSrcweir 
42cdf0e10cSrcweir SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
43cdf0e10cSrcweir 									const SfxPoolItem *pPutItem ):
44cdf0e10cSrcweir 	pPool(pItemPool),
45cdf0e10cSrcweir 	pCache(new SfxItemModifyArr_Impl),
46cdf0e10cSrcweir 	pSetToPut( 0 ),
47cdf0e10cSrcweir 	pItemToPut( &pItemPool->Put(*pPutItem) )
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	DBG_CTOR(SfxItemPoolCache, 0);
50cdf0e10cSrcweir 	DBG_ASSERT(pItemPool, "kein Pool angegeben");
51cdf0e10cSrcweir }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //------------------------------------------------------------------------
54cdf0e10cSrcweir 
SfxItemPoolCache(SfxItemPool * pItemPool,const SfxItemSet * pPutSet)55cdf0e10cSrcweir SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
56cdf0e10cSrcweir 									const SfxItemSet *pPutSet ):
57cdf0e10cSrcweir 	pPool(pItemPool),
58cdf0e10cSrcweir 	pCache(new SfxItemModifyArr_Impl),
59cdf0e10cSrcweir 	pSetToPut( pPutSet ),
60cdf0e10cSrcweir 	pItemToPut( 0 )
61cdf0e10cSrcweir {
62cdf0e10cSrcweir 	DBG_CTOR(SfxItemPoolCache, 0);
63cdf0e10cSrcweir 	DBG_ASSERT(pItemPool, "kein Pool angegeben");
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir //------------------------------------------------------------------------
67cdf0e10cSrcweir 
~SfxItemPoolCache()68cdf0e10cSrcweir SfxItemPoolCache::~SfxItemPoolCache()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir 	DBG_DTOR(SfxItemPoolCache, 0);
71cdf0e10cSrcweir 	for ( size_t nPos = 0; nPos < pCache->size(); ++nPos ) {
72cdf0e10cSrcweir 		pPool->Remove( *(*pCache)[nPos].pPoolItem );
73cdf0e10cSrcweir 		pPool->Remove( *(*pCache)[nPos].pOrigItem );
74cdf0e10cSrcweir 	}
75cdf0e10cSrcweir 	delete pCache; pCache = 0;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	if ( pItemToPut )
78cdf0e10cSrcweir 		pPool->Remove( *pItemToPut );
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir //------------------------------------------------------------------------
82cdf0e10cSrcweir 
ApplyTo(const SfxSetItem & rOrigItem,sal_Bool bNew)83cdf0e10cSrcweir const SfxSetItem& SfxItemPoolCache::ApplyTo( const SfxSetItem &rOrigItem, sal_Bool bNew )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	DBG_CHKTHIS(SfxItemPoolCache, 0);
86cdf0e10cSrcweir 	DBG_ASSERT( pPool == rOrigItem.GetItemSet().GetPool(), "invalid Pool" );
87cdf0e10cSrcweir 	DBG_ASSERT( IsDefaultItem( &rOrigItem ) || IsPooledItem( &rOrigItem ),
88cdf0e10cSrcweir 				"original not in pool" );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	// Find whether this Transformations ever occurred
91cdf0e10cSrcweir 	for ( size_t nPos = 0; nPos < pCache->size(); ++nPos )
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		SfxItemModifyImpl &rMapEntry = (*pCache)[nPos];
94cdf0e10cSrcweir 		if ( rMapEntry.pOrigItem == &rOrigItem )
95cdf0e10cSrcweir 		{
96cdf0e10cSrcweir 			// aendert sich ueberhaupt etwas?
97cdf0e10cSrcweir 			if ( rMapEntry.pPoolItem != &rOrigItem )
98cdf0e10cSrcweir 			{
99cdf0e10cSrcweir 				rMapEntry.pPoolItem->AddRef(2); // einen davon fuer den Cache
100cdf0e10cSrcweir 				if ( bNew )
101cdf0e10cSrcweir 					pPool->Put( rOrigItem );	//! AddRef??
102cdf0e10cSrcweir 			}
103cdf0e10cSrcweir 			return *rMapEntry.pPoolItem;
104cdf0e10cSrcweir 		}
105cdf0e10cSrcweir 	}
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	// die neue Attributierung in einem neuen Set eintragen
108cdf0e10cSrcweir 	SfxSetItem *pNewItem = (SfxSetItem *)rOrigItem.Clone();
109cdf0e10cSrcweir 	if ( pItemToPut )
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		pNewItem->GetItemSet().PutDirect( *pItemToPut );
112cdf0e10cSrcweir 		DBG_ASSERT( &pNewItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
113cdf0e10cSrcweir 					"wrong item in temporary set" );
114cdf0e10cSrcweir 	}
115cdf0e10cSrcweir 	else
116cdf0e10cSrcweir 		pNewItem->GetItemSet().Put( *pSetToPut );
117cdf0e10cSrcweir 	const SfxSetItem* pNewPoolItem = (const SfxSetItem*) &pPool->Put( *pNewItem );
118cdf0e10cSrcweir 	DBG_ASSERT( pNewPoolItem != pNewItem, "Pool: rein == raus?" );
119cdf0e10cSrcweir 	delete pNewItem;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 	// Refernzzaehler anpassen, je einen davon fuer den Cache
122cdf0e10cSrcweir 	pNewPoolItem->AddRef( pNewPoolItem != &rOrigItem ? 2 : 1 );
123cdf0e10cSrcweir 	if ( bNew )
124cdf0e10cSrcweir 		pPool->Put( rOrigItem );	//! AddRef??
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	// die Transformation im Cache eintragen
127cdf0e10cSrcweir 	SfxItemModifyImpl aModify;
128cdf0e10cSrcweir 	aModify.pOrigItem = &rOrigItem;
129cdf0e10cSrcweir 	aModify.pPoolItem = (SfxSetItem*) pNewPoolItem;
130cdf0e10cSrcweir 	pCache->push_back( aModify );
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	DBG_ASSERT( !pItemToPut ||
133cdf0e10cSrcweir 				&pNewPoolItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
134cdf0e10cSrcweir 				"wrong item in resulting set" );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	return *pNewPoolItem;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141