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 2476 : SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
28 : const SfxPoolItem *pPutItem ):
29 : pPool(pItemPool),
30 2476 : pCache(new SfxItemModifyArr_Impl),
31 : pSetToPut( 0 ),
32 4952 : pItemToPut( &pItemPool->Put(*pPutItem) )
33 : {
34 : DBG_ASSERT(pItemPool, "No Pool provided");
35 2476 : }
36 :
37 :
38 113958 : SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
39 : const SfxItemSet *pPutSet ):
40 : pPool(pItemPool),
41 113958 : pCache(new SfxItemModifyArr_Impl),
42 : pSetToPut( pPutSet ),
43 227916 : pItemToPut( 0 )
44 : {
45 : DBG_ASSERT(pItemPool, "No Pool provided");
46 113958 : }
47 :
48 :
49 116434 : SfxItemPoolCache::~SfxItemPoolCache()
50 : {
51 232931 : for ( size_t nPos = 0; nPos < pCache->size(); ++nPos ) {
52 116497 : pPool->Remove( *(*pCache)[nPos].pPoolItem );
53 116497 : pPool->Remove( *(*pCache)[nPos].pOrigItem );
54 : }
55 116434 : delete pCache; pCache = 0;
56 :
57 116434 : if ( pItemToPut )
58 2476 : pPool->Remove( *pItemToPut );
59 116434 : }
60 :
61 :
62 124270 : const SfxSetItem& SfxItemPoolCache::ApplyTo( const SfxSetItem &rOrigItem, bool bNew )
63 : {
64 : DBG_ASSERT( pPool == rOrigItem.GetItemSet().GetPool(), "invalid Pool" );
65 : DBG_ASSERT( IsDefaultItem( &rOrigItem ) || IsPooledItem( &rOrigItem ),
66 : "original not in pool" );
67 :
68 : // Find whether this Transformations ever occurred
69 124370 : for ( size_t nPos = 0; nPos < pCache->size(); ++nPos )
70 : {
71 7873 : SfxItemModifyImpl &rMapEntry = (*pCache)[nPos];
72 7873 : if ( rMapEntry.pOrigItem == &rOrigItem )
73 : {
74 : // Did anything change at all?
75 7773 : if ( rMapEntry.pPoolItem != &rOrigItem )
76 : {
77 6794 : rMapEntry.pPoolItem->AddRef(2); // One for the cache
78 6794 : if ( bNew )
79 6794 : pPool->Put( rOrigItem ); //FIXME: AddRef?
80 : }
81 7773 : return *rMapEntry.pPoolItem;
82 : }
83 : }
84 :
85 : // Insert the new attributes in a new Set
86 116497 : SfxSetItem *pNewItem = static_cast<SfxSetItem *>(rOrigItem.Clone());
87 116497 : if ( pItemToPut )
88 : {
89 2476 : pNewItem->GetItemSet().PutDirect( *pItemToPut );
90 : DBG_ASSERT( &pNewItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
91 : "wrong item in temporary set" );
92 : }
93 : else
94 114021 : pNewItem->GetItemSet().Put( *pSetToPut );
95 116497 : const SfxSetItem* pNewPoolItem = static_cast<const SfxSetItem*>(&pPool->Put( *pNewItem ));
96 : DBG_ASSERT( pNewPoolItem != pNewItem, "Pool: same in and out?" );
97 116497 : delete pNewItem;
98 :
99 : // Adapt refcount; one each for the cache
100 116497 : pNewPoolItem->AddRef( pNewPoolItem != &rOrigItem ? 2 : 1 );
101 116497 : if ( bNew )
102 116497 : pPool->Put( rOrigItem ); //FIXME: AddRef?
103 :
104 : // Add the transformation to the cache
105 : SfxItemModifyImpl aModify;
106 116497 : aModify.pOrigItem = &rOrigItem;
107 116497 : aModify.pPoolItem = const_cast<SfxSetItem*>(pNewPoolItem);
108 116497 : pCache->push_back( aModify );
109 :
110 : DBG_ASSERT( !pItemToPut ||
111 : &pNewPoolItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
112 : "wrong item in resulting set" );
113 :
114 116497 : return *pNewPoolItem;
115 : }
116 :
117 :
118 :
119 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|