1
/*******************************************************************************
2
* Copyright (c) 2008, 2009 Wind River Systems, Inc. and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Markus Schorn - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.core.pdom.dom.cpp;
13
import java.util.HashMap;
15
import org.eclipse.cdt.core.CCorePlugin;
16
import org.eclipse.cdt.core.dom.ast.DOMException;
17
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
18
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
19
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance;
20
import org.eclipse.cdt.internal.core.index.IndexCPPSignatureUtil;
21
import org.eclipse.cdt.internal.core.pdom.PDOM;
22
import org.eclipse.cdt.internal.core.pdom.dom.NamedNodeCollector;
23
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
24
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNamedNode;
25
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
26
import org.eclipse.core.runtime.CoreException;
28
public class PDOMInstanceCache {
30
public static PDOMInstanceCache getCache(PDOMBinding binding) {
31
final PDOM pdom= binding.getPDOM();
32
final long record= binding.getRecord();
33
final Long key = record+PDOMCPPLinkage.CACHE_INSTANCES;
34
Object cache= pdom.getCachedResult(key);
35
if (cache instanceof PDOMInstanceCache) {
36
return (PDOMInstanceCache) cache;
39
PDOMInstanceCache newCache= new PDOMInstanceCache();
41
newCache.populate(binding);
42
} catch (CoreException e) {
46
newCache= (PDOMInstanceCache) pdom.putCachedResult(key, newCache, false);
50
private final HashMap<String, ICPPTemplateInstance> fMap;
51
private ICPPDeferredClassInstance fDeferredInstance;
53
public PDOMInstanceCache() {
54
fMap= new HashMap<String, ICPPTemplateInstance>();
57
synchronized public final void addInstance(ICPPTemplateArgument[] arguments, ICPPTemplateInstance instance) {
59
String key= IndexCPPSignatureUtil.getTemplateArgString(arguments, true);
60
fMap.put(key, instance);
61
} catch (CoreException e) {
63
} catch (DOMException e) {
67
synchronized public final ICPPTemplateInstance getInstance(ICPPTemplateArgument[] arguments) {
69
String key= IndexCPPSignatureUtil.getTemplateArgString(arguments, true);
71
} catch (CoreException e) {
73
} catch (DOMException e) {
78
private void populate(PDOMBinding binding) throws CoreException {
79
PDOMNode parent= binding.getParentNode();
81
parent= binding.getLinkage();
83
NamedNodeCollector nn= new NamedNodeCollector(binding.getLinkage(), binding.getNameCharArray());
85
PDOMNamedNode[] nodes= nn.getNodes();
86
for (PDOMNamedNode node : nodes) {
87
if (node instanceof ICPPTemplateInstance) {
88
ICPPTemplateInstance inst= (ICPPTemplateInstance) node;
89
if (binding.equals(inst.getTemplateDefinition())) {
90
ICPPTemplateArgument[] args= inst.getTemplateArguments();
91
addInstance(args, inst);
97
synchronized public ICPPTemplateInstance[] getAllInstances() {
98
return fMap.values().toArray(new ICPPTemplateInstance[fMap.size()]);
101
public ICPPDeferredClassInstance getDeferredInstance() {
102
return fDeferredInstance;
105
public void putDeferredInstance(ICPPDeferredClassInstance dci) {
106
fDeferredInstance= dci;