Commit | Line | Data |
---|---|---|
163354ef MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
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: | |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.ctf.core.event.types; |
163354ef MK |
14 | |
15 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
16 | import org.eclipse.jdt.annotation.Nullable; | |
f357bcd4 | 17 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; |
fbe6fa6f | 18 | import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope; |
163354ef MK |
19 | |
20 | /** | |
21 | * Scoped defintion. a defintion where you can lookup various datatypes | |
22 | * | |
23 | * TODO: replace by default methods and an interface when java 8 is upon us | |
24 | * | |
25 | * @author Matthew Khouzam | |
163354ef MK |
26 | */ |
27 | @NonNullByDefault | |
28 | public abstract class ScopedDefinition extends Definition implements IDefinitionScope { | |
29 | ||
30 | /** | |
31 | * Constructor | |
32 | * | |
33 | * @param declaration | |
34 | * the event declaration | |
35 | * @param definitionScope | |
36 | * the definition is in a scope, (normally a struct) what is it? | |
37 | * @param fieldName | |
38 | * the name of the definition. (it is a field in the parent | |
39 | * scope) | |
40 | */ | |
41 | public ScopedDefinition(IDeclaration declaration, @Nullable IDefinitionScope definitionScope, String fieldName) { | |
42 | super(declaration, definitionScope, fieldName); | |
43 | } | |
44 | ||
70f60307 MK |
45 | /** |
46 | * Constructor This one takes the scope and thus speeds up definition | |
47 | * creation | |
48 | * | |
49 | * @param declaration | |
50 | * the parent declaration | |
51 | * @param definitionScope | |
52 | * the parent scope | |
53 | * @param fieldName | |
54 | * the field name | |
55 | * @param scope | |
56 | * the lexical scope | |
fbe6fa6f | 57 | * @since 1.0 |
70f60307 | 58 | */ |
fbe6fa6f | 59 | public ScopedDefinition(StructDeclaration declaration, @Nullable IDefinitionScope definitionScope, String fieldName, ILexicalScope scope) { |
70f60307 MK |
60 | super(declaration, definitionScope, fieldName, scope); |
61 | } | |
62 | ||
163354ef MK |
63 | /** |
64 | * Lookup an array in a struct. If the name returns a non-array (like an | |
65 | * int) then the method returns null | |
66 | * | |
67 | * @param name | |
68 | * the name of the array | |
7b4f13e6 MK |
69 | * @return the array or null. |
70 | */ | |
70f60307 | 71 | public @Nullable AbstractArrayDefinition lookupArrayDefinition(String name) { |
fa533f33 | 72 | IDefinition def = lookupDefinition(name); |
7b4f13e6 MK |
73 | return (AbstractArrayDefinition) ((def instanceof AbstractArrayDefinition) ? def : null); |
74 | } | |
75 | ||
163354ef MK |
76 | |
77 | /** | |
78 | * Lookup an enum in a struct. If the name returns a non-enum (like an int) | |
79 | * then the method returns null | |
80 | * | |
81 | * @param name | |
82 | * the name of the enum | |
83 | * @return the enum or null if a definition is not found or it does not | |
84 | * match the desired datatype. | |
85 | */ | |
86 | @Nullable | |
87 | public EnumDefinition lookupEnum(String name) { | |
fa533f33 | 88 | IDefinition def = lookupDefinition(name); |
163354ef MK |
89 | return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null); |
90 | } | |
91 | ||
92 | /** | |
93 | * Lookup an integer in a struct. If the name returns a non-integer (like an | |
94 | * float) then the method returns null | |
95 | * | |
96 | * @param name | |
97 | * the name of the integer | |
98 | * @return the integer or null if a definition is not found or it does not | |
99 | * match the desired datatype. | |
100 | */ | |
101 | @Nullable | |
102 | public IntegerDefinition lookupInteger(String name) { | |
fa533f33 | 103 | IDefinition def = lookupDefinition(name); |
163354ef MK |
104 | return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def : null); |
105 | } | |
106 | ||
163354ef MK |
107 | /** |
108 | * Lookup a string in a struct. If the name returns a non-string (like an | |
109 | * int) then the method returns null | |
110 | * | |
111 | * @param name | |
112 | * the name of the string | |
113 | * @return the string or null if a definition is not found or it does not | |
114 | * match the desired datatype. | |
115 | */ | |
116 | @Nullable | |
117 | public StringDefinition lookupString(String name) { | |
fa533f33 | 118 | IDefinition def = lookupDefinition(name); |
163354ef MK |
119 | return (StringDefinition) ((def instanceof StringDefinition) ? def : null); |
120 | } | |
121 | ||
122 | /** | |
123 | * Lookup a struct in a struct. If the name returns a non-struct (like an | |
124 | * int) then the method returns null | |
125 | * | |
126 | * @param name | |
127 | * the name of the struct | |
128 | * @return the struct or null if a definition is not found or it does not | |
129 | * match the desired datatype. | |
130 | */ | |
131 | @Nullable | |
132 | public StructDefinition lookupStruct(String name) { | |
fa533f33 | 133 | IDefinition def = lookupDefinition(name); |
163354ef MK |
134 | return (StructDefinition) ((def instanceof StructDefinition) ? def : null); |
135 | } | |
136 | ||
137 | /** | |
138 | * Lookup a variant in a struct. If the name returns a non-variant (like an | |
139 | * int) then the method returns null | |
140 | * | |
141 | * @param name | |
142 | * the name of the variant | |
143 | * @return the variant or null if a definition is not found or it does not | |
144 | * match the desired datatype. | |
145 | */ | |
146 | @Nullable | |
147 | public VariantDefinition lookupVariant(String name) { | |
fa533f33 | 148 | IDefinition def = lookupDefinition(name); |
163354ef MK |
149 | return (VariantDefinition) ((def instanceof VariantDefinition) ? def : null); |
150 | } | |
151 | } |