1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.ctf
.core
.event
.types
;
15 import java
.util
.Collection
;
16 import java
.util
.Collections
;
17 import java
.util
.HashMap
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
25 * A CTFC variant declaration.
27 * A variant is similar to a C union, only taking the minimum size of the types,
28 * it is a compound data type that contains other datatypes in fields. they are
29 * stored in an hashmap and indexed by names which are strings.
32 * @author Matthew Khouzam
33 * @author Simon Marchi
35 public class VariantDeclaration
extends Declaration
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private String fTag
= null;
42 private static final long ALIGNMENT
= 1;
43 private final Map
<String
, IDeclaration
> fFields
= Collections
.synchronizedMap(new HashMap
<String
, IDeclaration
>());
44 private EnumDefinition fTagDef
;
45 private IDeclaration fDeclarationToPopulate
;
46 private IDefinitionScope fPrevDefinitionScope
;
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
55 public VariantDeclaration() {
58 // ------------------------------------------------------------------------
59 // Getters/Setters/Predicates
60 // ------------------------------------------------------------------------
63 * @return Does the variant have a tag
65 public boolean isTagged() {
70 * Lookup if a field exists in the variant
74 * @return true = field tag exists
76 public boolean hasField(String fieldTag
) {
77 return fFields
.containsKey(fieldTag
);
81 * Sets the tag in a variant
86 public void setTag(String tag
) {
92 * Gets current variant tag
94 * @return the variant tag.
96 public String
getTag() {
101 * Gets the fields of the variant
103 * @return the fields of the variant
106 public Map
<String
, IDeclaration
> getFields() {
111 public long getAlignment() {
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
123 public VariantDefinition
createDefinition(IDefinitionScope definitionScope
,
124 String fieldName
, BitBuffer input
) throws CTFReaderException
{
126 if (fPrevDefinitionScope
!= definitionScope
) {
128 fPrevDefinitionScope
= definitionScope
;
130 EnumDefinition tagDef
= fTagDef
;
131 if (tagDef
== null) {
132 Definition def
= definitionScope
.lookupDefinition(fTag
);
133 tagDef
= (EnumDefinition
) ((def
instanceof EnumDefinition
) ? def
: null);
135 if (tagDef
== null) {
136 throw new CTFReaderException("Tag is not defined " + fTag
); //$NON-NLS-1$
138 String varFieldName
= tagDef
.getStringValue();
139 fDeclarationToPopulate
= fFields
.get(varFieldName
);
140 if (fDeclarationToPopulate
== null) {
141 throw new CTFReaderException("Unknown enum selector for variant " + //$NON-NLS-1$
142 definitionScope
.getScopePath().toString());
144 Definition fieldValue
= fDeclarationToPopulate
.createDefinition(definitionScope
, fieldName
, input
);
145 return new VariantDefinition(this, definitionScope
, varFieldName
, fieldName
, fieldValue
);
149 * Add a field to this CTF Variant
152 * The tag of the new field
154 * The Declaration of this new field
156 public void addField(String fieldTag
, IDeclaration declaration
) {
157 fFields
.put(fieldTag
, declaration
);
161 * gets the tag definition
163 * @return the fTagDef
166 public EnumDefinition
getTagDef() {
174 public int getMaximumSize() {
175 Collection
<IDeclaration
> values
= fFields
.values();
177 for (IDeclaration field
: values
) {
178 maxSize
= Math
.max(maxSize
, field
.getMaximumSize());
184 public String
toString() {
185 /* Only used for debugging */
186 return "[declaration] variant[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$