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 .
23 #include <svl/itempool.hxx>
24 #include <svl/itemset.hxx>
25 #include <svl/poolcach.hxx>
26 #include <tools/debug.hxx>
28 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool
*pItemPool
,
29 const SfxPoolItem
*pPutItem
):
32 pItemToPut( &pItemPool
->Put(*pPutItem
) )
34 DBG_ASSERT(pItemPool
, "No Pool provided");
38 SfxItemPoolCache::SfxItemPoolCache( SfxItemPool
*pItemPool
,
39 const SfxItemSet
*pPutSet
):
44 DBG_ASSERT(pItemPool
, "No Pool provided");
48 SfxItemPoolCache::~SfxItemPoolCache()
50 for (SfxItemModifyImpl
& rImpl
: m_aCache
) {
51 pPool
->Remove( *rImpl
.pPoolItem
);
52 pPool
->Remove( *rImpl
.pOrigItem
);
56 pPool
->Remove( *pItemToPut
);
60 const SfxSetItem
& SfxItemPoolCache::ApplyTo( const SfxSetItem
&rOrigItem
)
62 DBG_ASSERT( pPool
== rOrigItem
.GetItemSet().GetPool(), "invalid Pool" );
63 DBG_ASSERT( IsDefaultItem( &rOrigItem
) || IsPooledItem( &rOrigItem
),
64 "original not in pool" );
66 // Find whether this Transformations ever occurred
67 for (SfxItemModifyImpl
& rMapEntry
: m_aCache
)
69 if ( rMapEntry
.pOrigItem
== &rOrigItem
)
71 // Did anything change at all?
72 if ( rMapEntry
.pPoolItem
!= &rOrigItem
)
74 rMapEntry
.pPoolItem
->AddRef(2); // One for the cache
75 pPool
->Put( rOrigItem
); //FIXME: AddRef?
77 return *rMapEntry
.pPoolItem
;
81 // Insert the new attributes in a new Set
82 std::unique_ptr
<SfxSetItem
> pNewItem(static_cast<SfxSetItem
*>(rOrigItem
.Clone()));
85 pNewItem
->GetItemSet().PutDirect( *pItemToPut
);
86 DBG_ASSERT( &pNewItem
->GetItemSet().Get( pItemToPut
->Which() ) == pItemToPut
,
87 "wrong item in temporary set" );
90 pNewItem
->GetItemSet().Put( *pSetToPut
);
91 const SfxSetItem
* pNewPoolItem
= &pPool
->Put( std::move(pNewItem
) );
93 // Adapt refcount; one each for the cache
94 pNewPoolItem
->AddRef( pNewPoolItem
!= &rOrigItem
? 2 : 1 );
95 pPool
->Put( rOrigItem
); //FIXME: AddRef?
97 // Add the transformation to the cache
98 SfxItemModifyImpl aModify
;
99 aModify
.pOrigItem
= &rOrigItem
;
100 aModify
.pPoolItem
= const_cast<SfxSetItem
*>(pNewPoolItem
);
101 m_aCache
.push_back( aModify
);
103 DBG_ASSERT( !pItemToPut
||
104 &pNewPoolItem
->GetItemSet().Get( pItemToPut
->Which() ) == pItemToPut
,
105 "wrong item in resulting set" );
107 return *pNewPoolItem
;
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */