Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / tool / lookupcache.cxx
blob914b188a851d2a4f2b69832ba8ffcdc525cffbf1
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 "lookupcache.hxx"
22 #include "document.hxx"
23 #include "queryentry.hxx"
25 ScLookupCache::QueryCriteria::QueryCriteria( const ScQueryEntry& rEntry ) :
26 mfVal(0.0), mbAlloc(false), mbString(false)
28 switch (rEntry.eOp)
30 case SC_EQUAL :
31 meOp = EQUAL;
32 break;
33 case SC_LESS_EQUAL :
34 meOp = LESS_EQUAL;
35 break;
36 case SC_GREATER_EQUAL :
37 meOp = GREATER_EQUAL;
38 break;
39 default:
40 meOp = UNKNOWN;
41 SAL_WARN( "sc.core", "ScLookupCache::QueryCriteria not prepared for this ScQueryOp");
44 const ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
45 if (rItem.meType == ScQueryEntry::ByString)
46 setString(rItem.maString.getString());
47 else
48 setDouble(rItem.mfVal);
51 ScLookupCache::QueryCriteria::QueryCriteria( const ScLookupCache::QueryCriteria & r ) :
52 mfVal( r.mfVal),
53 mbAlloc( false),
54 mbString( false),
55 meOp( r.meOp)
57 if (r.mbString && r.mpStr)
59 mpStr = new OUString( *r.mpStr);
60 mbAlloc = mbString = true;
64 ScLookupCache::QueryCriteria::~QueryCriteria()
66 deleteString();
69 ScLookupCache::ScLookupCache( ScDocument * pDoc, const ScRange & rRange ) :
70 maRange( rRange),
71 mpDoc( pDoc)
76 ScLookupCache::~ScLookupCache()
81 ScLookupCache::Result ScLookupCache::lookup( ScAddress & o_rResultAddress,
82 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress ) const
84 QueryMap::const_iterator it( maQueryMap.find( QueryKey( rQueryAddress,
85 rCriteria.getQueryOp())));
86 if (it == maQueryMap.end())
87 return NOT_CACHED;
88 const QueryCriteriaAndResult& rResult = (*it).second;
89 if (!(rResult.maCriteria == rCriteria))
90 return CRITERIA_DIFFERENT;
91 if (rResult.maAddress.Row() < 0 )
92 return NOT_AVAILABLE;
93 o_rResultAddress = rResult.maAddress;
94 return FOUND;
98 bool ScLookupCache::insert( const ScAddress & rResultAddress,
99 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress,
100 const bool bAvailable )
102 QueryKey aKey( rQueryAddress, rCriteria.getQueryOp());
103 QueryCriteriaAndResult aResult( rCriteria, rResultAddress);
104 if (!bAvailable)
105 aResult.maAddress.SetRow(-1);
106 bool bInserted = maQueryMap.insert( ::std::pair< const QueryKey,
107 QueryCriteriaAndResult>( aKey, aResult)).second;
109 return bInserted;
113 void ScLookupCache::Notify( SvtBroadcaster & /* rBC */ , const SfxHint & rHint )
115 if (!mpDoc->IsInDtorClear())
117 const ScHint* p = PTR_CAST( ScHint, &rHint );
118 if (p && (p->GetId() & SC_HINT_DATACHANGED))
120 mpDoc->RemoveLookupCache( *this);
121 delete this;
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */