| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others |
| 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 | |
| 15 | import java.util.LinkedHashMap; |
| 16 | import java.util.List; |
| 17 | import java.util.ListIterator; |
| 18 | import java.util.Map; |
| 19 | |
| 20 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
| 21 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
| 22 | |
| 23 | /** |
| 24 | * A CTF structure definition (similar to a C structure). |
| 25 | * |
| 26 | * A structure is similar to a C structure, it is a compound data type that |
| 27 | * contains other datatypes in fields. they are stored in an hashmap and indexed |
| 28 | * by names which are strings. |
| 29 | * |
| 30 | * @version 1.0 |
| 31 | * @author Matthew Khouzam |
| 32 | * @author Simon Marchi |
| 33 | */ |
| 34 | public class StructDefinition extends Definition implements IDefinitionScope { |
| 35 | |
| 36 | // ------------------------------------------------------------------------ |
| 37 | // Attributes |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | private final StructDeclaration declaration; |
| 41 | private final Map<String, Definition> definitions = new LinkedHashMap<>(); |
| 42 | |
| 43 | // ------------------------------------------------------------------------ |
| 44 | // Constructors |
| 45 | // ------------------------------------------------------------------------ |
| 46 | |
| 47 | /** |
| 48 | * Constructor |
| 49 | * |
| 50 | * @param declaration |
| 51 | * the parent declaration |
| 52 | * @param definitionScope |
| 53 | * the parent scope |
| 54 | * @param structFieldName |
| 55 | * the field name |
| 56 | */ |
| 57 | public StructDefinition(StructDeclaration declaration, |
| 58 | IDefinitionScope definitionScope, String structFieldName) { |
| 59 | super(definitionScope, structFieldName); |
| 60 | |
| 61 | this.declaration = declaration; |
| 62 | |
| 63 | for (String fName : declaration.getFieldsList()) { |
| 64 | IDeclaration fieldDecl = declaration.getFields().get(fName); |
| 65 | assert (fieldDecl != null); |
| 66 | |
| 67 | Definition def = fieldDecl.createDefinition(this, fName); |
| 68 | definitions.put(fName, def); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // ------------------------------------------------------------------------ |
| 73 | // Getters/Setters/Predicates |
| 74 | // ------------------------------------------------------------------------ |
| 75 | |
| 76 | /** |
| 77 | * @return The definitions of all the fields |
| 78 | * @since 2.0 |
| 79 | */ |
| 80 | public Map<String, Definition> getDefinitions() { |
| 81 | return definitions; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public StructDeclaration getDeclaration() { |
| 86 | return declaration; |
| 87 | } |
| 88 | |
| 89 | // ------------------------------------------------------------------------ |
| 90 | // Operations |
| 91 | // ------------------------------------------------------------------------ |
| 92 | |
| 93 | @Override |
| 94 | public void read(BitBuffer input) throws CTFReaderException { |
| 95 | alignRead(input, this.declaration); |
| 96 | final List<String> fieldList = declaration.getFieldsList(); |
| 97 | for (String fName : fieldList) { |
| 98 | Definition def = definitions.get(fName); |
| 99 | assert (def != null); |
| 100 | def.read(input); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public Definition lookupDefinition(String lookupPath) { |
| 106 | /* |
| 107 | * The fields are created in order of appearance, so if a variant or |
| 108 | * sequence refers to a field that is after it, the field's definition |
| 109 | * will not be there yet in the hashmap. |
| 110 | */ |
| 111 | Definition retVal = definitions.get(lookupPath); |
| 112 | if (retVal == null) { |
| 113 | retVal = definitions.get("_" + lookupPath); //$NON-NLS-1$ |
| 114 | } |
| 115 | return retVal; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Lookup an array in a struct. If the name returns a non-array (like an |
| 120 | * int) than the method returns null |
| 121 | * |
| 122 | * @param name |
| 123 | * the name of the array |
| 124 | * @return the array or null. |
| 125 | */ |
| 126 | public ArrayDefinition lookupArray(String name) { |
| 127 | Definition def = lookupDefinition(name); |
| 128 | return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Lookup an enum in a struct. If the name returns a non-enum (like an int) |
| 133 | * than the method returns null |
| 134 | * |
| 135 | * @param name |
| 136 | * the name of the enum |
| 137 | * @return the enum or null. |
| 138 | */ |
| 139 | public EnumDefinition lookupEnum(String name) { |
| 140 | Definition def = lookupDefinition(name); |
| 141 | return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Lookup an integer in a struct. If the name returns a non-integer (like an |
| 146 | * float) than the method returns null |
| 147 | * |
| 148 | * @param name |
| 149 | * the name of the integer |
| 150 | * @return the integer or null. |
| 151 | */ |
| 152 | public IntegerDefinition lookupInteger(String name) { |
| 153 | Definition def = lookupDefinition(name); |
| 154 | return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def |
| 155 | : null); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Lookup a sequence in a struct. If the name returns a non-sequence (like |
| 160 | * an int) than the method returns null |
| 161 | * |
| 162 | * @param name |
| 163 | * the name of the sequence |
| 164 | * @return the sequence or null. |
| 165 | */ |
| 166 | public SequenceDefinition lookupSequence(String name) { |
| 167 | Definition def = lookupDefinition(name); |
| 168 | return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def |
| 169 | : null); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Lookup a string in a struct. If the name returns a non-string (like |
| 174 | * an int) than the method returns null |
| 175 | * |
| 176 | * @param name |
| 177 | * the name of the string |
| 178 | * @return the string or null. |
| 179 | */ |
| 180 | public StringDefinition lookupString(String name) { |
| 181 | Definition def = lookupDefinition(name); |
| 182 | return (StringDefinition) ((def instanceof StringDefinition) ? def |
| 183 | : null); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Lookup a struct in a struct. If the name returns a non-struct (like |
| 188 | * an int) than the method returns null |
| 189 | * |
| 190 | * @param name |
| 191 | * the name of the struct |
| 192 | * @return the struct or null. |
| 193 | */ |
| 194 | public StructDefinition lookupStruct(String name) { |
| 195 | Definition def = lookupDefinition(name); |
| 196 | return (StructDefinition) ((def instanceof StructDefinition) ? def |
| 197 | : null); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Lookup a variant in a struct. If the name returns a non-variant (like |
| 202 | * an int) than the method returns null |
| 203 | * |
| 204 | * @param name |
| 205 | * the name of the variant |
| 206 | * @return the variant or null. |
| 207 | */ |
| 208 | public VariantDefinition lookupVariant(String name) { |
| 209 | Definition def = lookupDefinition(name); |
| 210 | return (VariantDefinition) ((def instanceof VariantDefinition) ? def |
| 211 | : null); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public String toString() { |
| 216 | StringBuilder builder = new StringBuilder(); |
| 217 | |
| 218 | builder.append("{ "); //$NON-NLS-1$ |
| 219 | |
| 220 | ListIterator<String> listIterator = this.declaration.getFieldsList() |
| 221 | .listIterator(); |
| 222 | |
| 223 | while (listIterator.hasNext()) { |
| 224 | String field = listIterator.next(); |
| 225 | |
| 226 | builder.append(field); |
| 227 | builder.append(" = "); //$NON-NLS-1$ |
| 228 | builder.append(lookupDefinition(field).toString()); |
| 229 | |
| 230 | if (listIterator.hasNext()) { |
| 231 | builder.append(", "); //$NON-NLS-1$ |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | builder.append(" }"); //$NON-NLS-1$ |
| 236 | |
| 237 | return builder.toString(); |
| 238 | } |
| 239 | } |