tmf/lttng: Update 2014 copyrights
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / VariantDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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.HashMap;
16 import java.util.Map;
17
18 /**
19 * A CTFC variant declaration.
20 *
21 * A variant is similar to a C union, only taking the minimum size of the types,
22 * it is a compound data type that contains other datatypes in fields. they are
23 * stored in an hashmap and indexed by names which are strings.
24 *
25 * @version 1.0
26 * @author Matthew Khouzam
27 * @author Simon Marchi
28 */
29 public class VariantDeclaration implements IDeclaration {
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
35 private String tag = null;
36 private static final long alignment = 1;
37 private final Map<String, IDeclaration> fields = new HashMap<>();
38
39 // ------------------------------------------------------------------------
40 // Constructors
41 // ------------------------------------------------------------------------
42
43 /**
44 * Constructor
45 */
46 public VariantDeclaration() {
47 }
48
49 // ------------------------------------------------------------------------
50 // Getters/Setters/Predicates
51 // ------------------------------------------------------------------------
52
53 /**
54 * @return Does the variant have a tag
55 */
56 public boolean isTagged() {
57 return tag != null;
58 }
59
60 /**
61 * Lookup if a field exists in the variant
62 * @param fieldTag the field tag name
63 * @return true = field tag exists
64 */
65 public boolean hasField(String fieldTag) {
66 return fields.containsKey(fieldTag);
67 }
68
69 /**
70 * Sets the tag in a variant
71 * @param tag the tag
72 */
73 public void setTag(String tag) {
74 this.tag = tag;
75 }
76
77 /**
78 * Gets current variant tag
79 * @return the variant tag.
80 */
81 public String getTag() {
82 return this.tag;
83 }
84
85 /**
86 * Gets the fields of the variant
87 * @return the fields of the variant
88 * @since 2.0
89 */
90 public Map<String, IDeclaration> getFields() {
91 return this.fields;
92 }
93
94 @Override
95 public long getAlignment() {
96 return alignment;
97 }
98 // ------------------------------------------------------------------------
99 // Operations
100 // ------------------------------------------------------------------------
101
102 @Override
103 public VariantDefinition createDefinition(IDefinitionScope definitionScope,
104 String fieldName) {
105 return new VariantDefinition(this, definitionScope, fieldName);
106 }
107
108
109 /**
110 * Add a field to this CTF Variant
111 *
112 * @param fieldTag
113 * The tag of the new field
114 * @param declaration
115 * The Declaration of this new field
116 */
117 public void addField(String fieldTag, IDeclaration declaration) {
118 fields.put(fieldTag, declaration);
119 }
120
121 @Override
122 public String toString() {
123 /* Only used for debugging */
124 return "[declaration] variant[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
125 }
126
127 }
This page took 0.055924 seconds and 5 git commands to generate.