bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / items / poolcach.cxx
blobeb1f955dbece56a72e3717dcc5a1813f684eb171
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <limits.h>
23 #include <svl/itempool.hxx>
24 #include <svl/itemset.hxx>
25 #include <svl/poolcach.hxx>
27 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
28 const SfxPoolItem *pPutItem ):
29 pPool(pItemPool),
30 pCache(new SfxItemModifyArr_Impl),
31 pSetToPut( 0 ),
32 pItemToPut( &pItemPool->Put(*pPutItem) )
34 DBG_ASSERT(pItemPool, "No Pool provided");
38 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool *pItemPool,
39 const SfxItemSet *pPutSet ):
40 pPool(pItemPool),
41 pCache(new SfxItemModifyArr_Impl),
42 pSetToPut( pPutSet ),
43 pItemToPut( 0 )
45 DBG_ASSERT(pItemPool, "No Pool provided");
49 SfxItemPoolCache::~SfxItemPoolCache()
51 for ( size_t nPos = 0; nPos < pCache->size(); ++nPos ) {
52 pPool->Remove( *(*pCache)[nPos].pPoolItem );
53 pPool->Remove( *(*pCache)[nPos].pOrigItem );
55 delete pCache; pCache = 0;
57 if ( pItemToPut )
58 pPool->Remove( *pItemToPut );
62 const SfxSetItem& SfxItemPoolCache::ApplyTo( const SfxSetItem &rOrigItem, bool bNew )
64 DBG_ASSERT( pPool == rOrigItem.GetItemSet().GetPool(), "invalid Pool" );
65 DBG_ASSERT( IsDefaultItem( &rOrigItem ) || IsPooledItem( &rOrigItem ),
66 "original not in pool" );
68 // Find whether this Transformations ever occurred
69 for ( size_t nPos = 0; nPos < pCache->size(); ++nPos )
71 SfxItemModifyImpl &rMapEntry = (*pCache)[nPos];
72 if ( rMapEntry.pOrigItem == &rOrigItem )
74 // Did anything change at all?
75 if ( rMapEntry.pPoolItem != &rOrigItem )
77 rMapEntry.pPoolItem->AddRef(2); // One for the cache
78 if ( bNew )
79 pPool->Put( rOrigItem ); //FIXME: AddRef?
81 return *rMapEntry.pPoolItem;
85 // Insert the new attributes in a new Set
86 SfxSetItem *pNewItem = static_cast<SfxSetItem *>(rOrigItem.Clone());
87 if ( pItemToPut )
89 pNewItem->GetItemSet().PutDirect( *pItemToPut );
90 DBG_ASSERT( &pNewItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
91 "wrong item in temporary set" );
93 else
94 pNewItem->GetItemSet().Put( *pSetToPut );
95 const SfxSetItem* pNewPoolItem = static_cast<const SfxSetItem*>(&pPool->Put( *pNewItem ));
96 DBG_ASSERT( pNewPoolItem != pNewItem, "Pool: same in and out?" );
97 delete pNewItem;
99 // Adapt refcount; one each for the cache
100 pNewPoolItem->AddRef( pNewPoolItem != &rOrigItem ? 2 : 1 );
101 if ( bNew )
102 pPool->Put( rOrigItem ); //FIXME: AddRef?
104 // Add the transformation to the cache
105 SfxItemModifyImpl aModify;
106 aModify.pOrigItem = &rOrigItem;
107 aModify.pPoolItem = const_cast<SfxSetItem*>(pNewPoolItem);
108 pCache->push_back( aModify );
110 DBG_ASSERT( !pItemToPut ||
111 &pNewPoolItem->GetItemSet().Get( pItemToPut->Which() ) == pItemToPut,
112 "wrong item in resulting set" );
114 return *pNewPoolItem;
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */