/*******************************************************************************
- * 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
import java.util.HashMap;
import java.util.Map;
-import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
+import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
/**
- * <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 {
private VariantDeclaration declaration;
private EnumDefinition tagDefinition;
- private HashMap<String, Definition> definitions = new HashMap<String, Definition>();
+ private Map<String, Definition> definitions = new HashMap<>();
private String currentField;
// ------------------------------------------------------------------------
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()) {
/**
* Get the definitions in the variant
* @return the definitions
+ * @since 2.0
*/
- public HashMap<String, Definition> getDefinitions() {
+ public Map<String, Definition> getDefinitions() {
return definitions;
}
/**
* Set the definitions in a variant
* @param definitions the definitions
+ * @since 2.0
*/
- public void setDefinitions(HashMap<String, Definition> definitions) {
+ public void setDefinitions(Map<String, Definition> definitions) {
this.definitions = definitions;
}
this.currentField = currentField;
}
- @Override
- public String getPath() {
- return path;
- }
-
/**
* Get the current field name
* @return the current field name
// ------------------------------------------------------------------------
@Override
- public void read(BitBuffer input) {
+ public void read(BitBuffer input) throws CTFReaderException {
currentField = tagDefinition.getValue();
Definition field = definitions.get(currentField);
-
+ if (field == null) {
+ throw new CTFReaderException("Variant was not defined for: "+ currentField); //$NON-NLS-1$
+ }
field.read(input);
}
: null);
}
+ @Override
+ public String toString() {
+ return "{ " + getCurrentFieldName() + //$NON-NLS-1$
+ " = " + getCurrentField() + //$NON-NLS-1$
+ " }"; //$NON-NLS-1$
+ }
}