Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64db26166cf66e8e7173d721aacb5f75a78a46d8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************************
 * Copyright (c) 2007, 2008 Symbian Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Andrew Ferguson (Symbian) - Initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.index.composite;

import java.util.Comparator;
import java.util.TreeSet;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.internal.core.index.CIndex;
import org.eclipse.cdt.internal.core.index.DefaultFragmentBindingComparator;
import org.eclipse.cdt.internal.core.index.IIndexFragmentBindingComparator;
import org.eclipse.cdt.internal.core.index.IIndexFragmentBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMFragmentBindingComparator;
import org.eclipse.core.runtime.CoreException;

/**
 * Commonality between composite factories
 */
public abstract class AbstractCompositeFactory implements ICompositesFactory {	
	protected IIndex index;
	private Comparator<IIndexFragmentBinding> fragmentComparator;
	
	public AbstractCompositeFactory(IIndex index) {
		this.index= index;
		this.fragmentComparator= new FragmentBindingComparator( 
			new IIndexFragmentBindingComparator[] {
					new PDOMFragmentBindingComparator(), 
					new DefaultFragmentBindingComparator()
			}
		);
	}
	
	/*
	 * @see org.eclipse.cdt.internal.core.index.composite.ICompositesFactory#getCompositeBindings(org.eclipse.cdt.core.index.IIndex, org.eclipse.cdt.internal.core.index.IIndexFragmentBinding[])
	 */
	public final IIndexBinding[] getCompositeBindings(IIndexFragmentBinding[] bindings) {
		IIndexBinding[] result = new IIndexBinding[bindings.length];
		for(int i=0; i<result.length; i++)
			result[i] = getCompositeBinding(bindings[i]);
		return result;
	}

	/* 
	 * @see org.eclipse.cdt.internal.core.index.composite.cpp.ICompositesFactory#getComposites(org.eclipse.cdt.core.index.IIndex, org.eclipse.cdt.internal.core.index.IIndexFragmentBinding[][])
	 */
	public final IIndexBinding[] getCompositeBindings(IIndexFragmentBinding[][] fragmentBindings) {
		return getCompositeBindings(mergeBindingArrays(fragmentBindings));
	}
 
	/**
	 * Convenience method for taking a group of binding arrays, and returning a single array
	 * with the each binding appearing once
	 * @param fragmentBindings
	 * @return an array of unique bindings
	 */
	protected IIndexFragmentBinding[] mergeBindingArrays(IIndexFragmentBinding[][] fragmentBindings) {
		TreeSet<IIndexFragmentBinding> ts = new TreeSet<IIndexFragmentBinding>(fragmentComparator);
		for(int i=0; i<fragmentBindings.length; i++)
			for(int j=0; j<fragmentBindings[i].length; j++)
				ts.add(fragmentBindings[i][j]);
		return ts.toArray(new IIndexFragmentBinding[ts.size()]);
	}
	
	/**
	 * Convenience method for finding a binding with a definition in the specified index
	 * context, which is equivalent to the specified binding, or null if no definitions were
     * found.
	 * @param index
	 * @param binding
	 * @return
	 */
	protected IIndexFragmentBinding findOneDefinition(IBinding binding) {
		try{
			CIndex cindex = (CIndex) index;
			IIndexFragmentBinding[] ibs = cindex.findEquivalentBindings(binding);
			IBinding def = ibs.length>0 ? ibs[0] : null;
			for(int i=0; i<ibs.length; i++) {
				if(ibs[i].hasDefinition()) {
					def = ibs[i];
				}
			}
			return (IIndexFragmentBinding) def;
		} catch(CoreException ce) {
			CCorePlugin.log(ce);
		}
		throw new CompositingNotImplementedError();
	}
	
	private static class FragmentBindingComparator implements Comparator<IIndexFragmentBinding> {
		private IIndexFragmentBindingComparator[] comparators;
		
		FragmentBindingComparator(IIndexFragmentBindingComparator[] comparators) {
			this.comparators= comparators;
		}
		
		public int compare(IIndexFragmentBinding f1, IIndexFragmentBinding f2) {
			for(int i=0; i<comparators.length; i++) {
				int cmp= comparators[i].compare(f1, f2);
				if(cmp!=Integer.MIN_VALUE) {
					return cmp;
				}
			}
			throw new IllegalArgumentException();
		}
	}
}

Back to the top