Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
a4fa4e36 MK |
15 | import org.eclipse.jdt.annotation.NonNull; |
16 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
866e5b51 FC |
17 | |
18 | /** | |
d37aaa7f | 19 | * A CTF variant definition (similar to a C union). |
486efb2e | 20 | * |
d37aaa7f FC |
21 | * A variant is similar to a C union, only taking the minimum size of the types, |
22 | * it is a compound data type that contains other datatypes in fields. they are | |
23 | * stored in an hashmap and indexed by names which are strings. | |
24 | * | |
25 | * @version 1.0 | |
26 | * @author Matthew Khouzam | |
27 | * @author Simon Marchi | |
866e5b51 | 28 | */ |
163354ef | 29 | public final class VariantDefinition extends ScopedDefinition { |
866e5b51 FC |
30 | |
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
a4fa4e36 MK |
35 | private final Definition fDefinition; |
36 | private final String fCurrentField; | |
37 | private final String fFieldName; | |
866e5b51 FC |
38 | |
39 | // ------------------------------------------------------------------------ | |
40 | // Constructors | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
9ac2eb62 MK |
43 | /** |
44 | * Constructor | |
a4fa4e36 MK |
45 | * |
46 | * @param declaration | |
47 | * the parent declaration | |
48 | * @param definitionScope | |
49 | * the parent scope | |
50 | * @param selectedField | |
51 | * the selected field | |
52 | * @param fieldName | |
53 | * the field name | |
54 | * @param fieldValue | |
55 | * the field value | |
56 | * @since 3.0 | |
9ac2eb62 | 57 | */ |
a4fa4e36 MK |
58 | public VariantDefinition(@NonNull VariantDeclaration declaration, |
59 | IDefinitionScope definitionScope, String selectedField, @NonNull String fieldName, Definition fieldValue) { | |
60 | super(declaration, definitionScope, fieldName); | |
866e5b51 | 61 | |
a4fa4e36 MK |
62 | fFieldName = fieldName; |
63 | fCurrentField = selectedField; | |
64 | fDefinition = fieldValue; | |
866e5b51 | 65 | |
866e5b51 FC |
66 | } |
67 | ||
68 | // ------------------------------------------------------------------------ | |
69 | // Getters/Setters/Predicates | |
70 | // ------------------------------------------------------------------------ | |
71 | ||
9ac2eb62 | 72 | @Override |
866e5b51 | 73 | public VariantDeclaration getDeclaration() { |
a4fa4e36 | 74 | return (VariantDeclaration) super.getDeclaration(); |
866e5b51 FC |
75 | } |
76 | ||
9ac2eb62 MK |
77 | /** |
78 | * Get the current field name | |
a4fa4e36 | 79 | * |
9ac2eb62 MK |
80 | * @return the current field name |
81 | */ | |
82 | public String getCurrentFieldName() { | |
a4fa4e36 | 83 | return fCurrentField; |
9ac2eb62 MK |
84 | } |
85 | ||
86 | /** | |
87 | * Get the current field | |
a4fa4e36 | 88 | * |
9ac2eb62 MK |
89 | * @return the current field |
90 | */ | |
91 | public Definition getCurrentField() { | |
a4fa4e36 | 92 | return fDefinition; |
9ac2eb62 MK |
93 | } |
94 | ||
866e5b51 FC |
95 | // ------------------------------------------------------------------------ |
96 | // Operations | |
97 | // ------------------------------------------------------------------------ | |
98 | ||
866e5b51 FC |
99 | @Override |
100 | public Definition lookupDefinition(String lookupPath) { | |
a4fa4e36 MK |
101 | if (lookupPath == null) { |
102 | return null; | |
103 | } | |
104 | if (lookupPath.equals(fFieldName)) { | |
105 | return fDefinition; | |
106 | } | |
107 | return getDefinitionScope().lookupDefinition(lookupPath); | |
866e5b51 FC |
108 | } |
109 | ||
419f09a8 SM |
110 | @Override |
111 | public String toString() { | |
112 | return "{ " + getCurrentFieldName() + //$NON-NLS-1$ | |
113 | " = " + getCurrentField() + //$NON-NLS-1$ | |
114 | " }"; //$NON-NLS-1$ | |
115 | } | |
866e5b51 | 116 | } |