Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 204b73c5ea3bf36835f31c1c858b815efaa907cb (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (c) 2013, 2015 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.eclipse.cdt.internal.qt.core.pdom;

import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.cdt.internal.core.pdom.db.IString;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.qt.core.Activator;
import org.eclipse.cdt.internal.qt.core.index.IQProperty;
import org.eclipse.core.runtime.CoreException;

@SuppressWarnings("restriction")
public class QtPDOMProperty extends QtPDOMBinding {

	private static int offsetInitializer = QtPDOMBinding.Field.Last.offset;

	protected static enum Field {
		Type(Database.PTR_SIZE), Attributes(Database.PTR_SIZE), Last(0);

		public final int offset;

		private Field(int sizeof) {
			this.offset = offsetInitializer;
			offsetInitializer += sizeof;
		}

		public long getRecord(long baseRec) {
			return baseRec + offset;
		}
	}

	public QtPDOMProperty(QtPDOMLinkage linkage, long record) {
		super(linkage, record);
	}

	public QtPDOMProperty(QtPDOMLinkage linkage, PDOMBinding parent, QtPropertyName qtName) throws CoreException {
		super(linkage, parent, qtName);

		setType(qtName.getType());

		if (parent instanceof QtPDOMQObject)
			((QtPDOMQObject) parent).addChild(this);
	}

	@Override
	protected int getRecordSize() {
		return Field.Last.offset;
	}

	@Override
	public int getNodeType() {
		return QtPDOMNodeType.QProperty.Type;
	}

	public void delete() throws CoreException {
		long fieldRec = getDB().getRecPtr(Field.Type.getRecord(record));
		if (fieldRec != 0)
			getDB().getString(fieldRec).delete();
		getDB().putRecPtr(Field.Type.getRecord(record), 0);
	}

	public void setType(String type) throws CoreException {
		long rec = getDB().getRecPtr(Field.Type.getRecord(record));
		if (rec != 0) {
			IString typeStr = getDB().getString(rec);
			if (type == null) {
				typeStr.delete();
				return;
			}

			// There is nothing to do if the database already stores the same name.
			if (type.equals(typeStr.getString()))
				return;
		}

		getDB().putRecPtr(Field.Type.getRecord(record), getDB().newString(type).getRecord());
	}

	// IType?
	public String getType() throws CoreException {
		long rec = getDB().getRecPtr(Field.Type.getRecord(record));
		if (rec == 0)
			return null;

		return getDB().getString(rec).getString();
	}

	public void setAttributes(Attribute[] attributes) throws CoreException {
		long rec = getDB().getRecPtr(Field.Attributes.getRecord(record));
		QtPDOMArray<Attribute> pdomArray = new QtPDOMArray<>(getQtLinkage(), Attribute.Codec, rec);
		rec = pdomArray.set(attributes);
		getDB().putRecPtr(Field.Attributes.getRecord(record), rec);
	}

	public Attribute[] getAttributes() throws CoreException {
		long rec = getDB().getRecPtr(Field.Attributes.getRecord(record));
		QtPDOMArray<Attribute> pdomArray = new QtPDOMArray<>(getQtLinkage(), Attribute.Codec, rec);
		return pdomArray.get();
	}

	public static class Attribute {
		public final IQProperty.Attribute attr;
		public final String value;
		public final long cppRecord;

		public Attribute(IQProperty.Attribute attr, String value) {
			this.attr = attr;
			this.value = value;
			this.cppRecord = 0;
		}

		public Attribute(IQProperty.Attribute attr, String value, PDOMBinding cppBinding) {
			this.attr = attr;
			this.value = value;
			this.cppRecord = cppBinding == null ? 0 : cppBinding.getRecord();
		}

		private Attribute(IQProperty.Attribute attr, String value, long cppRecord) {
			this.attr = attr;
			this.value = value;
			this.cppRecord = cppRecord;
		}

		private static final IQtPDOMCodec<Attribute> Codec = new IQtPDOMCodec<Attribute>() {
			@Override
			public int getElementSize() {
				return 1 + Database.PTR_SIZE + Database.PTR_SIZE;
			}

			@Override
			public Attribute[] allocArray(int count) {
				return new Attribute[count];
			}

			@Override
			public Attribute decode(QtPDOMLinkage linkage, long record) throws CoreException {
				byte attrId = linkage.getDB().getByte(record);
				long valRec = linkage.getDB().getRecPtr(record + 1);
				long cppRec = linkage.getDB().getRecPtr(record + 1 + Database.PTR_SIZE);

				if (attrId < 0 || attrId >= IQProperty.Attribute.values().length)
					throw Activator.coreException("invalid QProperty attribute id read from datbase, was " + attrId);

				IQProperty.Attribute attr = IQProperty.Attribute.values()[attrId];

				String val = valRec == 0 ? "" : linkage.getDB().getString(valRec).getString();
				return new Attribute(attr, val, cppRec);
			}

			@Override
			public void encode(QtPDOMLinkage linkage, long record, Attribute element) throws CoreException {
				linkage.getDB().putByte(record, (byte) element.attr.ordinal());

				// Delete the existing strings then create and store new ones.
				long rec = linkage.getDB().getRecPtr(record + 1);
				if (rec != 0)
					linkage.getDB().getString(rec).delete();

				if (element == null || element.value == null)
					linkage.getDB().putRecPtr(record + 1, 0);
				else
					linkage.getDB().putRecPtr(record + 1, linkage.getDB().newString(element.value).getRecord());

				linkage.getDB().putRecPtr(record + 1 + Database.PTR_SIZE, element.cppRecord);
			}
		};
	}
}

Back to the top