tmf: keep event fields ordering as per ctf metadata
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDefinition.java
index e29a6d0d680211f7b548509b9d17c5330a7c640f..b7603497b473c06ab4ee7b00a0b90cc3b61c3b7d 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
 
 package org.eclipse.linuxtools.ctf.core.event.types;
 
-import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.ListIterator;
+import java.util.Map;
 
-import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
 
 /**
- * <b><u>StructDefinition</u></b>
+ * A CTF structure definition (similar to a C structure).
+ *
+ * A structure is similar to a C structure, it is a compound data type that
+ * contains other datatypes in fields. they are stored in an hashmap and indexed
+ * by names which are strings.
+ *
+ * @version 1.0
+ * @author Matthew Khouzam
+ * @author Simon Marchi
  */
 public class StructDefinition extends Definition implements IDefinitionScope {
 
@@ -27,12 +37,22 @@ public class StructDefinition extends Definition implements IDefinitionScope {
     // ------------------------------------------------------------------------
 
     private final StructDeclaration declaration;
-    private final HashMap<String, Definition> definitions = new HashMap<String, Definition>();
+    private final Map<String, Definition> definitions = new LinkedHashMap<String, Definition>();
 
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
 
+    /**
+     * Constructor
+     *
+     * @param declaration
+     *            the parent declaration
+     * @param definitionScope
+     *            the parent scope
+     * @param structFieldName
+     *            the field name
+     */
     public StructDefinition(StructDeclaration declaration,
             IDefinitionScope definitionScope, String structFieldName) {
         super(definitionScope, structFieldName);
@@ -57,10 +77,15 @@ public class StructDefinition extends Definition implements IDefinitionScope {
         return path;
     }
 
-    public HashMap<String, Definition> getDefinitions() {
+    /**
+     * @return The definitions of all the fields
+     * @since 2.0
+     */
+    public Map<String, Definition> getDefinitions() {
         return definitions;
     }
 
+    @Override
     public StructDeclaration getDeclaration() {
         return declaration;
     }
@@ -71,10 +96,14 @@ public class StructDefinition extends Definition implements IDefinitionScope {
 
     @Override
     public void read(BitBuffer input) {
-        for (String fName : declaration.getFieldsList()) {
+        final int align = (int) declaration.getAlignment();
+        int pos = input.position()
+                + ((align - (input.position() % align)) % align);
+        input.position(pos);
+        final List<String> fieldList = declaration.getFieldsList();
+        for (String fName : fieldList) {
             Definition def = definitions.get(fName);
             assert (def != null);
-
             def.read(input);
         }
     }
@@ -93,40 +122,96 @@ public class StructDefinition extends Definition implements IDefinitionScope {
         return retVal;
     }
 
+    /**
+     * Lookup an array in a struct. if the name returns a non-array (like an
+     * int) than the method returns null
+     *
+     * @param name
+     *            the name of the array
+     * @return the array or null.
+     */
     public ArrayDefinition lookupArray(String name) {
         Definition def = lookupDefinition(name);
         return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
     }
 
+    /**
+     * Lookup an enum in a struct. if the name returns a non-enum (like an int)
+     * than the method returns null
+     *
+     * @param name
+     *            the name of the enum
+     * @return the enum or null.
+     */
     public EnumDefinition lookupEnum(String name) {
         Definition def = lookupDefinition(name);
         return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
     }
 
+    /**
+     * Lookup an integer in a struct. if the name returns a non-integer (like an
+     * float) than the method returns null
+     *
+     * @param name
+     *            the name of the integer
+     * @return the integer or null.
+     */
     public IntegerDefinition lookupInteger(String name) {
         Definition def = lookupDefinition(name);
         return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
                 : null);
     }
 
+    /**
+     * Lookup a sequence in a struct. if the name returns a non-sequence (like
+     * an int) than the method returns null
+     *
+     * @param name
+     *            the name of the sequence
+     * @return the sequence or null.
+     */
     public SequenceDefinition lookupSequence(String name) {
         Definition def = lookupDefinition(name);
         return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
                 : null);
     }
 
+    /**
+     * Lookup a string in a struct. if the name returns a non-string (like
+     * an int) than the method returns null
+     *
+     * @param name
+     *            the name of the string
+     * @return the string or null.
+     */
     public StringDefinition lookupString(String name) {
         Definition def = lookupDefinition(name);
         return (StringDefinition) ((def instanceof StringDefinition) ? def
                 : null);
     }
 
+    /**
+     * Lookup a struct in a struct. if the name returns a non-struct (like
+     * an int) than the method returns null
+     *
+     * @param name
+     *            the name of the struct
+     * @return the struct or null.
+     */
     public StructDefinition lookupStruct(String name) {
         Definition def = lookupDefinition(name);
         return (StructDefinition) ((def instanceof StructDefinition) ? def
                 : null);
     }
 
+    /**
+     * Lookup a variant in a struct. if the name returns a non-variant (like
+     * an int) than the method returns null
+     *
+     * @param name
+     *            the name of the variant
+     * @return the variant or null.
+     */
     public VariantDefinition lookupVariant(String name) {
         Definition def = lookupDefinition(name);
         return (VariantDefinition) ((def instanceof VariantDefinition) ? def
@@ -137,27 +222,24 @@ public class StructDefinition extends Definition implements IDefinitionScope {
     public String toString() {
         StringBuilder builder = new StringBuilder();
 
-        int size = this.declaration.getFieldsList().size();
-        int n = 0;
+        builder.append("{ "); //$NON-NLS-1$
 
-        if (size > 1) {
-            builder.append("{ "); //$NON-NLS-1$
-        }
-
-        ListIterator<String> listIterator = this.declaration.getFieldsList().listIterator();
+        ListIterator<String> listIterator = this.declaration.getFieldsList()
+                .listIterator();
 
         while (listIterator.hasNext()) {
             String field = listIterator.next();
+
+            builder.append(field);
+            builder.append(" = "); //$NON-NLS-1$
             builder.append(lookupDefinition(field).toString());
-            n++;
-            if (n != size) {
+
+            if (listIterator.hasNext()) {
                 builder.append(", "); //$NON-NLS-1$
             }
         }
 
-        if (size > 1) {
-            builder.append(" }"); //$NON-NLS-1$
-        }
+        builder.append(" }"); //$NON-NLS-1$
 
         return builder.toString();
     }
This page took 0.02672 seconds and 5 git commands to generate.