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 :
21 : #include <limits.h>
22 :
23 : #include <svl/itempool.hxx>
24 : #include <svl/itemset.hxx>
25 : #include <svl/poolcach.hxx>
26 :
27 : // STATIC DATA -----------------------------------------------------------
28 :
29 : DBG_NAME(SfxItemPoolCache)
30 :
31 : //------------------------------------------------------------------------
32 :
33 540 : SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
34 : const SfxPoolItem *pPutItem ):
35 : pPool(pItemPool),
36 540 : pCache(new SfxItemModifyArr_Impl),
37 : pSetToPut( 0 ),
38 1080 : pItemToPut( &pItemPool->Put(*pPutItem) )
39 : {
40 : DBG_CTOR(SfxItemPoolCache, 0);
41 : DBG_ASSERT(pItemPool, "kein Pool angegeben");
42 540 : }
43 :
44 : //------------------------------------------------------------------------
45 :
46 17646 : SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
47 : const SfxItemSet *pPutSet ):
48 : pPool(pItemPool),
49 17646 : pCache(new SfxItemModifyArr_Impl),
50 : pSetToPut( pPutSet ),
51 35292 : pItemToPut( 0 )
52 : {
53 : DBG_CTOR(SfxItemPoolCache, 0);
54 : DBG_ASSERT(pItemPool, "kein Pool angegeben");
55 17646 : }
56 :
57 : //------------------------------------------------------------------------
58 :
59 18186 : SfxItemPoolCache::~SfxItemPoolCache()
60 : {
61 : DBG_DTOR(SfxItemPoolCache, 0);
62 36385 : for ( size_t nPos = 0; nPos < pCache->size(); ++nPos ) {
63 18199 : pPool->Remove( *(*pCache)[nPos].pPoolItem );
64 18199 : pPool->Remove( *(*pCache)[nPos].pOrigItem );
65 : }
66 18186 : delete pCache; pCache = 0;
67 :
68 18186 : if ( pItemToPut )
69 540 : pPool->Remove( *pItemToPut );
70 18186 : }
71 :
72 : //------------------------------------------------------------------------
73 :
74 18280 : const SfxSetItem& SfxItemPoolCache::ApplyTo( const SfxSetItem &rOrigItem, sal_Bool bNew )
75 : {
76 : DBG_CHKTHIS(SfxItemPoolCache, 0);
77 : DBG_ASSERT( pPool == rOrigItem.GetItemSet().GetPool(), "invalid Pool" );
78 : DBG_ASSERT( IsDefaultItem( &rOrigItem ) || IsPooledItem( &rOrigItem ),
79 : "original not in pool" );
80 :
81 : // Find whether this Transformations ever occurred
82 18324 : for ( size_t nPos = 0; nPos < pCache->size(); ++nPos )
83 : {
84 125 : SfxItemModifyImpl &rMapEntry = (*pCache)[nPos];
85 125 : if ( rMapEntry.pOrigItem == &rOrigItem )
86 : {
87 : // aendert sich ueberhaupt etwas?
88 81 : if ( rMapEntry.pPoolItem != &rOrigItem )
89 : {
90 81 : rMapEntry.pPoolItem->AddRef(2); // einen davon fuer den Cache
91 81 : if ( bNew )
92 81 : pPool->Put( rOrigItem ); //! AddRef??
93 : }
94 81 : return *rMapEntry.pPoolItem;
95 : }
96 : }
97 :
98 : // die neue Attributierung in einem neuen Set eintragen
99 18199 : SfxSetItem *pNewItem = (SfxSetItem *)rOrigItem.Clone();
100 18199 : if ( pItemToPut )
101 : {
102 540 : pNewItem->GetItemSet().PutDirect( *pItemToPut );
103 : DBG_ASSERT( &pNewItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
104 : "wrong item in temporary set" );
105 : }
106 : else
107 17659 : pNewItem->GetItemSet().Put( *pSetToPut );
108 18199 : const SfxSetItem* pNewPoolItem = (const SfxSetItem*) &pPool->Put( *pNewItem );
109 : DBG_ASSERT( pNewPoolItem != pNewItem, "Pool: rein == raus?" );
110 18199 : delete pNewItem;
111 :
112 : // Refernzzaehler anpassen, je einen davon fuer den Cache
113 18199 : pNewPoolItem->AddRef( pNewPoolItem != &rOrigItem ? 2 : 1 );
114 18199 : if ( bNew )
115 18199 : pPool->Put( rOrigItem ); //! AddRef??
116 :
117 : // die Transformation im Cache eintragen
118 : SfxItemModifyImpl aModify;
119 18199 : aModify.pOrigItem = &rOrigItem;
120 18199 : aModify.pPoolItem = (SfxSetItem*) pNewPoolItem;
121 18199 : pCache->push_back( aModify );
122 :
123 : DBG_ASSERT( !pItemToPut ||
124 : &pNewPoolItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
125 : "wrong item in resulting set" );
126 :
127 18199 : return *pNewPoolItem;
128 : }
129 :
130 :
131 :
132 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|