btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / VariantDefinition.java
index 135ff682d021a75981f813302c84e7a174681289..6302f863f6350a239068bf68ad5a119177c83393 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
+ * Copyright (c) 2011, 2014 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.Map;
-
-import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
 
 /**
- * <b><u>VariantDefinition</u></b>
+ * A CTF variant definition (similar to a C union).
+ *
+ * A variant is similar to a C union, only taking the minimum size of the types,
+ * 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 VariantDefinition extends Definition implements IDefinitionScope {
+public final class VariantDefinition extends ScopedDefinition {
 
     // ------------------------------------------------------------------------
     // Attributes
     // ------------------------------------------------------------------------
 
-    private VariantDeclaration declaration;
-
-    private EnumDefinition tagDefinition;
-    private HashMap<String, Definition> definitions = new HashMap<String, Definition>();
-    private String currentField;
+    private final Definition fDefinition;
+    private final String fCurrentField;
+    private final String fFieldName;
 
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
 
-    public VariantDefinition(VariantDeclaration declaration,
-            IDefinitionScope definitionScope, String fieldName) {
-        super(definitionScope, fieldName);
-
-        this.declaration = declaration;
-
-        Definition tagDef = definitionScope.lookupDefinition(declaration.getTag());
-        /*
-         * if (tagDef == null) { throw new
-         * Exception("Variant tag field not found"); }
-         *
-         * if (!(tagDef instanceof EnumDefinition)) { throw new
-         * Exception("Variant tag field not enum"); }
-         */
-        this.tagDefinition = (EnumDefinition) tagDef;
-
-        for (Map.Entry<String, IDeclaration> field : declaration.getFields().entrySet()) {
-            Definition fieldDef = field.getValue().createDefinition(this,
-                    field.getKey());
-            definitions.put(field.getKey(), fieldDef);
-        }
+    /**
+     * Constructor
+     *
+     * @param declaration
+     *            the parent declaration
+     * @param definitionScope
+     *            the parent scope
+     * @param selectedField
+     *            the selected field
+     * @param fieldName
+     *            the field name
+     * @param fieldValue
+     *            the field value
+     * @since 3.0
+     */
+    public VariantDefinition(@NonNull VariantDeclaration declaration,
+            IDefinitionScope definitionScope, String selectedField, @NonNull String fieldName, Definition fieldValue) {
+        super(declaration, definitionScope, fieldName);
+
+        fFieldName = fieldName;
+        fCurrentField = selectedField;
+        fDefinition = fieldValue;
+
     }
 
     // ------------------------------------------------------------------------
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
 
+    @Override
     public VariantDeclaration getDeclaration() {
-        return declaration;
-    }
-
-    public void setDeclaration(VariantDeclaration declaration) {
-        this.declaration = declaration;
-    }
-
-    public EnumDefinition getTagDefinition() {
-        return tagDefinition;
-    }
-
-    public void setTagDefinition(EnumDefinition tagDefinition) {
-        this.tagDefinition = tagDefinition;
-    }
-
-    public HashMap<String, Definition> getDefinitions() {
-        return definitions;
+        return (VariantDeclaration) super.getDeclaration();
     }
 
-    public void setDefinitions(HashMap<String, Definition> definitions) {
-        this.definitions = definitions;
-    }
-
-    public void setCurrentField(String currentField) {
-        this.currentField = currentField;
+    /**
+     * Get the current field name
+     *
+     * @return the current field name
+     */
+    public String getCurrentFieldName() {
+        return fCurrentField;
     }
 
-    @Override
-    public String getPath() {
-        return path;
+    /**
+     * Get the current field
+     *
+     * @return the current field
+     */
+    public Definition getCurrentField() {
+        return fDefinition;
     }
 
     // ------------------------------------------------------------------------
     // Operations
     // ------------------------------------------------------------------------
 
-    @Override
-    public void read(BitBuffer input) {
-        currentField = tagDefinition.getValue();
-
-        Definition field = definitions.get(currentField);
-
-        field.read(input);
-    }
-
     @Override
     public Definition lookupDefinition(String lookupPath) {
-        return definitions.get(lookupPath);
-    }
-
-    public String getCurrentFieldName() {
-        return currentField;
-    }
-
-    public Definition getCurrentField() {
-        return definitions.get(currentField);
-    }
-
-    public ArrayDefinition lookupArray(String name) {
-        Definition def = definitions.get(name);
-        return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
-    }
-
-    public EnumDefinition lookupEnum(String name) {
-        Definition def = definitions.get(name);
-        return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
-    }
-
-    public IntegerDefinition lookupInteger(String name) {
-        Definition def = definitions.get(name);
-        return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
-                : null);
-    }
-
-    public SequenceDefinition lookupSequence(String name) {
-        Definition def = definitions.get(name);
-        return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
-                : null);
-    }
-
-    public StringDefinition lookupString(String name) {
-        Definition def = definitions.get(name);
-        return (StringDefinition) ((def instanceof StringDefinition) ? def
-                : null);
-    }
-
-    public StructDefinition lookupStruct(String name) {
-        Definition def = definitions.get(name);
-        return (StructDefinition) ((def instanceof StructDefinition) ? def
-                : null);
+        if (lookupPath == null) {
+            return null;
+        }
+        if (lookupPath.equals(fFieldName)) {
+            return fDefinition;
+        }
+        return getDefinitionScope().lookupDefinition(lookupPath);
     }
 
-    public VariantDefinition lookupVariant(String name) {
-        Definition def = definitions.get(name);
-        return (VariantDefinition) ((def instanceof VariantDefinition) ? def
-                : null);
+    @Override
+    public String toString() {
+        return "{ " + getCurrentFieldName() + //$NON-NLS-1$
+                " = " + getCurrentField() + //$NON-NLS-1$
+                " }"; //$NON-NLS-1$
     }
-
 }
This page took 0.031107 seconds and 5 git commands to generate.