1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <svl/itempool.hxx>
22 #include <svl/itemset.hxx>
23 #include <svl/poolcach.hxx>
24 #include <tools/debug.hxx>
26 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool
*pItemPool
,
27 const SfxPoolItem
*pPutItem
):
30 pItemToPut( &pItemPool
->Put(*pPutItem
) )
32 DBG_ASSERT(pItemPool
, "No Pool provided");
36 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool
*pItemPool
,
37 const SfxItemSet
*pPutSet
):
42 DBG_ASSERT(pItemPool
, "No Pool provided");
46 SfxItemPoolCache::~SfxItemPoolCache()
48 for (const SfxItemModifyImpl
& rImpl
: m_aCache
) {
49 pPool
->Remove( *rImpl
.pPoolItem
);
50 pPool
->Remove( *rImpl
.pOrigItem
);
54 pPool
->Remove( *pItemToPut
);
58 const SfxSetItem
& SfxItemPoolCache::ApplyTo( const SfxSetItem
&rOrigItem
)
60 DBG_ASSERT( pPool
== rOrigItem
.GetItemSet().GetPool(), "invalid Pool" );
61 DBG_ASSERT( IsDefaultItem( &rOrigItem
) || IsPooledItem( &rOrigItem
),
62 "original not in pool" );
64 // Find whether this Transformations ever occurred
65 for (const SfxItemModifyImpl
& rMapEntry
: m_aCache
)
67 if ( rMapEntry
.pOrigItem
== &rOrigItem
)
69 // Did anything change at all?
70 if ( rMapEntry
.pPoolItem
!= &rOrigItem
)
72 rMapEntry
.pPoolItem
->AddRef(2); // One for the cache
73 pPool
->Put( rOrigItem
); //FIXME: AddRef?
75 return *rMapEntry
.pPoolItem
;
79 // Insert the new attributes in a new Set
80 std::unique_ptr
<SfxSetItem
> pNewItem(rOrigItem
.Clone());
83 pNewItem
->GetItemSet().PutDirect( *pItemToPut
);
84 DBG_ASSERT( &pNewItem
->GetItemSet().Get( pItemToPut
->Which() ) == pItemToPut
,
85 "wrong item in temporary set" );
88 pNewItem
->GetItemSet().Put( *pSetToPut
);
89 const SfxSetItem
* pNewPoolItem
= &pPool
->Put( std::move(pNewItem
) );
91 // Adapt refcount; one each for the cache
92 pNewPoolItem
->AddRef( pNewPoolItem
!= &rOrigItem
? 2 : 1 );
93 pPool
->Put( rOrigItem
); //FIXME: AddRef?
95 // Add the transformation to the cache
96 SfxItemModifyImpl aModify
;
97 aModify
.pOrigItem
= &rOrigItem
;
98 aModify
.pPoolItem
= const_cast<SfxSetItem
*>(pNewPoolItem
);
99 m_aCache
.push_back( aModify
);
101 DBG_ASSERT( !pItemToPut
||
102 &pNewPoolItem
->GetItemSet().Get( pItemToPut
->Which() ) == pItemToPut
,
103 "wrong item in resulting set" );
105 return *pNewPoolItem
;
109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */