1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import org
.eclipse
.jdt
.annotation
.NonNull
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
19 * A CTF variant definition (similar to a C union).
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.
26 * @author Matthew Khouzam
27 * @author Simon Marchi
29 public final class VariantDefinition
extends Definition
implements IDefinitionScope
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 private final Definition fDefinition
;
36 private final String fCurrentField
;
37 private final String fFieldName
;
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
47 * the parent declaration
48 * @param definitionScope
50 * @param selectedField
58 public VariantDefinition(@NonNull VariantDeclaration declaration
,
59 IDefinitionScope definitionScope
, String selectedField
, @NonNull String fieldName
, Definition fieldValue
) {
60 super(declaration
, definitionScope
, fieldName
);
62 fFieldName
= fieldName
;
63 fCurrentField
= selectedField
;
64 fDefinition
= fieldValue
;
68 // ------------------------------------------------------------------------
69 // Getters/Setters/Predicates
70 // ------------------------------------------------------------------------
73 public VariantDeclaration
getDeclaration() {
74 return (VariantDeclaration
) super.getDeclaration();
78 * Get the current field name
80 * @return the current field name
82 public String
getCurrentFieldName() {
87 * Get the current field
89 * @return the current field
91 public Definition
getCurrentField() {
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 public Definition
lookupDefinition(String lookupPath
) {
101 if (lookupPath
== null) {
104 if (lookupPath
.equals(fFieldName
)) {
107 return getDefinitionScope().lookupDefinition(lookupPath
);
111 * Lookup an array in a struct. if the name returns a non-array (like an
112 * int) than the method returns null
115 * the name of the array
116 * @return the array or null.
118 public ArrayDefinition
lookupArray(String name
) {
119 Definition def
= lookupDefinition(name
);
120 return (ArrayDefinition
) ((def
instanceof ArrayDefinition
) ? def
: null);
124 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
125 * than the method returns null
128 * the name of the enum
129 * @return the enum or null.
131 public EnumDefinition
lookupEnum(String name
) {
132 Definition def
= lookupDefinition(name
);
133 return (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
137 * Lookup an integer in a struct. if the name returns a non-integer (like an
138 * float) than the method returns null
141 * the name of the integer
142 * @return the integer or null.
144 public IntegerDefinition
lookupInteger(String name
) {
145 Definition def
= lookupDefinition(name
);
146 return (IntegerDefinition
) ((def
instanceof IntegerDefinition
) ? def
151 * Lookup a sequence in a struct. if the name returns a non-sequence (like
152 * an int) than the method returns null
155 * the name of the sequence
156 * @return the sequence or null.
158 public SequenceDefinition
lookupSequence(String name
) {
159 Definition def
= lookupDefinition(name
);
160 return (SequenceDefinition
) ((def
instanceof SequenceDefinition
) ? def
165 * Lookup a string in a struct. if the name returns a non-string (like an
166 * int) than the method returns null
169 * the name of the string
170 * @return the string or null.
172 public StringDefinition
lookupString(String name
) {
173 Definition def
= lookupDefinition(name
);
174 return (StringDefinition
) ((def
instanceof StringDefinition
) ? def
179 * Lookup a struct in a struct. if the name returns a non-struct (like an
180 * int) than the method returns null
183 * the name of the struct
184 * @return the struct or null.
186 public StructDefinition
lookupStruct(String name
) {
187 Definition def
= lookupDefinition(name
);
188 return (StructDefinition
) ((def
instanceof StructDefinition
) ? def
193 * Lookup a variant in a struct. if the name returns a non-variant (like an
194 * int) than the method returns null
197 * the name of the variant
198 * @return the variant or null.
200 public VariantDefinition
lookupVariant(String name
) {
201 Definition def
= lookupDefinition(name
);
202 return (VariantDefinition
) ((def
instanceof VariantDefinition
) ? def
207 public String
toString() {
208 return "{ " + getCurrentFieldName() + //$NON-NLS-1$
209 " = " + getCurrentField() + //$NON-NLS-1$