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 2528 : SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
34 : const SfxPoolItem *pPutItem ):
35 : pPool(pItemPool),
36 2528 : pCache(new SfxItemModifyArr_Impl),
37 : pSetToPut( 0 ),
38 5056 : pItemToPut( &pItemPool->Put(*pPutItem) )
39 : {
40 : DBG_CTOR(SfxItemPoolCache, 0);
41 : DBG_ASSERT(pItemPool, "kein Pool angegeben");
42 2528 : }
43 :
44 : //------------------------------------------------------------------------
45 :
46 37000 : SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
47 : const SfxItemSet *pPutSet ):
48 : pPool(pItemPool),
49 37000 : pCache(new SfxItemModifyArr_Impl),
50 : pSetToPut( pPutSet ),
51 74000 : pItemToPut( 0 )
52 : {
53 : DBG_CTOR(SfxItemPoolCache, 0);
54 : DBG_ASSERT(pItemPool, "kein Pool angegeben");
55 37000 : }
56 :
57 : //------------------------------------------------------------------------
58 :
59 39528 : SfxItemPoolCache::~SfxItemPoolCache()
60 : {
61 : DBG_DTOR(SfxItemPoolCache, 0);
62 79080 : for ( size_t nPos = 0; nPos < pCache->size(); ++nPos ) {
63 39552 : pPool->Remove( *(*pCache)[nPos].pPoolItem );
64 39552 : pPool->Remove( *(*pCache)[nPos].pOrigItem );
65 : }
66 39528 : delete pCache; pCache = 0;
67 :
68 39528 : if ( pItemToPut )
69 2528 : pPool->Remove( *pItemToPut );
70 39528 : }
71 :
72 : //------------------------------------------------------------------------
73 :
74 39736 : 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 39808 : for ( size_t nPos = 0; nPos < pCache->size(); ++nPos )
83 : {
84 256 : SfxItemModifyImpl &rMapEntry = (*pCache)[nPos];
85 256 : if ( rMapEntry.pOrigItem == &rOrigItem )
86 : {
87 : // aendert sich ueberhaupt etwas?
88 184 : if ( rMapEntry.pPoolItem != &rOrigItem )
89 : {
90 184 : rMapEntry.pPoolItem->AddRef(2); // einen davon fuer den Cache
91 184 : if ( bNew )
92 184 : pPool->Put( rOrigItem ); //! AddRef??
93 : }
94 184 : return *rMapEntry.pPoolItem;
95 : }
96 : }
97 :
98 : // die neue Attributierung in einem neuen Set eintragen
99 39552 : SfxSetItem *pNewItem = (SfxSetItem *)rOrigItem.Clone();
100 39552 : if ( pItemToPut )
101 : {
102 2528 : pNewItem->GetItemSet().PutDirect( *pItemToPut );
103 : DBG_ASSERT( &pNewItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
104 : "wrong item in temporary set" );
105 : }
106 : else
107 37024 : pNewItem->GetItemSet().Put( *pSetToPut );
108 39552 : const SfxSetItem* pNewPoolItem = (const SfxSetItem*) &pPool->Put( *pNewItem );
109 : DBG_ASSERT( pNewPoolItem != pNewItem, "Pool: rein == raus?" );
110 39552 : delete pNewItem;
111 :
112 : // Refernzzaehler anpassen, je einen davon fuer den Cache
113 39552 : pNewPoolItem->AddRef( pNewPoolItem != &rOrigItem ? 2 : 1 );
114 39552 : if ( bNew )
115 39552 : pPool->Put( rOrigItem ); //! AddRef??
116 :
117 : // die Transformation im Cache eintragen
118 : SfxItemModifyImpl aModify;
119 39552 : aModify.pOrigItem = &rOrigItem;
120 39552 : aModify.pPoolItem = (SfxSetItem*) pNewPoolItem;
121 39552 : pCache->push_back( aModify );
122 :
123 : DBG_ASSERT( !pItemToPut ||
124 : &pNewPoolItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
125 : "wrong item in resulting set" );
126 :
127 39552 : return *pNewPoolItem;
128 : }
129 :
130 :
131 :
132 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|