merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / items / poolcach.cxx
blobe8978b70f268393853d3fe9de81c9780c4dfb7d4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: poolcach.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include <limits.h>
36 #ifndef GCC
37 #endif
39 #include <svtools/itempool.hxx>
40 #include <svtools/itemset.hxx>
41 #include "poolcach.hxx"
43 // STATIC DATA -----------------------------------------------------------
45 DBG_NAME(SfxItemPoolCache)
48 //------------------------------------------------------------------------
50 struct SfxItemModifyImpl
52 const SfxSetItem *pOrigItem;
53 SfxSetItem *pPoolItem;
56 SV_DECL_VARARR( SfxItemModifyArr_Impl, SfxItemModifyImpl, 8, 8 )
57 SV_IMPL_VARARR( SfxItemModifyArr_Impl, SfxItemModifyImpl);
59 //------------------------------------------------------------------------
61 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
62 const SfxPoolItem *pPutItem ):
63 pPool(pItemPool),
64 pCache(new SfxItemModifyArr_Impl),
65 pSetToPut( 0 ),
66 pItemToPut( &pItemPool->Put(*pPutItem) )
68 DBG_CTOR(SfxItemPoolCache, 0);
69 DBG_ASSERT(pItemPool, "kein Pool angegeben");
72 //------------------------------------------------------------------------
74 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
75 const SfxItemSet *pPutSet ):
76 pPool(pItemPool),
77 pCache(new SfxItemModifyArr_Impl),
78 pSetToPut( pPutSet ),
79 pItemToPut( 0 )
81 DBG_CTOR(SfxItemPoolCache, 0);
82 DBG_ASSERT(pItemPool, "kein Pool angegeben");
85 //------------------------------------------------------------------------
87 SfxItemPoolCache::~SfxItemPoolCache()
89 DBG_DTOR(SfxItemPoolCache, 0);
90 for ( USHORT nPos = 0; nPos < pCache->Count(); ++nPos ) {
91 pPool->Remove( *(*pCache)[nPos].pPoolItem );
92 pPool->Remove( *(*pCache)[nPos].pOrigItem );
94 delete pCache; pCache = 0;
96 if ( pItemToPut )
97 pPool->Remove( *pItemToPut );
100 //------------------------------------------------------------------------
102 const SfxSetItem& SfxItemPoolCache::ApplyTo( const SfxSetItem &rOrigItem, BOOL bNew )
104 DBG_CHKTHIS(SfxItemPoolCache, 0);
105 DBG_ASSERT( pPool == rOrigItem.GetItemSet().GetPool(), "invalid Pool" );
106 DBG_ASSERT( IsDefaultItem( &rOrigItem ) || IsPooledItem( &rOrigItem ),
107 "original not in pool" );
109 // Suchen, ob diese Transformations schon einmal vorkam
110 for ( USHORT nPos = 0; nPos < pCache->Count(); ++nPos )
112 SfxItemModifyImpl &rMapEntry = (*pCache)[nPos];
113 if ( rMapEntry.pOrigItem == &rOrigItem )
115 // aendert sich ueberhaupt etwas?
116 if ( rMapEntry.pPoolItem != &rOrigItem )
118 rMapEntry.pPoolItem->AddRef(2); // einen davon fuer den Cache
119 if ( bNew )
120 pPool->Put( rOrigItem ); //! AddRef??
122 return *rMapEntry.pPoolItem;
126 // die neue Attributierung in einem neuen Set eintragen
127 SfxSetItem *pNewItem = (SfxSetItem *)rOrigItem.Clone();
128 if ( pItemToPut )
130 pNewItem->GetItemSet().PutDirect( *pItemToPut );
131 DBG_ASSERT( &pNewItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
132 "wrong item in temporary set" );
134 else
135 pNewItem->GetItemSet().Put( *pSetToPut );
136 const SfxSetItem* pNewPoolItem = (const SfxSetItem*) &pPool->Put( *pNewItem );
137 DBG_ASSERT( pNewPoolItem != pNewItem, "Pool: rein == raus?" );
138 delete pNewItem;
140 // Refernzzaehler anpassen, je einen davon fuer den Cache
141 pNewPoolItem->AddRef( pNewPoolItem != &rOrigItem ? 2 : 1 );
142 if ( bNew )
143 pPool->Put( rOrigItem ); //! AddRef??
145 // die Transformation im Cache eintragen
146 SfxItemModifyImpl aModify;
147 aModify.pOrigItem = &rOrigItem;
148 aModify.pPoolItem = (SfxSetItem*) pNewPoolItem;
149 pCache->Insert( aModify, pCache->Count() );
151 DBG_ASSERT( !pItemToPut ||
152 &pNewPoolItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
153 "wrong item in resulting set" );
155 return *pNewPoolItem;