c022b013d599b1dd71142f99e611c876ad3a8f08
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / EnumDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
16
17 /**
18 * A CTF enum definition.
19 *
20 * The definition of a enum point basic data type. It will take the data
21 * from a trace and store it (and make it fit) as an integer and a string.
22 *
23 * @version 1.0
24 * @author Matthew Khouzam
25 * @author Simon Marchi
26 */
27 public class EnumDefinition extends SimpleDatatypeDefinition {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 private final EnumDeclaration declaration;
34
35 private final IntegerDefinition integerValue;
36
37 private String value;
38
39 // ------------------------------------------------------------------------
40 // Constructors
41 // ------------------------------------------------------------------------
42
43 /**
44 * Constructor
45 * @param declaration the parent declaration
46 * @param definitionScope the parent scope
47 * @param fieldName the field name
48 */
49 public EnumDefinition(EnumDeclaration declaration,
50 IDefinitionScope definitionScope, String fieldName) {
51 super(definitionScope, fieldName);
52
53 this.declaration = declaration;
54
55 integerValue = declaration.getContainerType().createDefinition(
56 definitionScope, fieldName);
57 value = declaration.query(integerValue.getValue());
58 }
59
60 // ------------------------------------------------------------------------
61 // Getters/Setters/Predicates
62 // ------------------------------------------------------------------------
63
64 /**
65 * Gets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will return "DAY"
66 * @return the value of the enum.
67 */
68 public String getValue() {
69 return value;
70 }
71
72 @Override
73 public String getStringValue(){
74 return getValue();
75 }
76
77 /**
78 * Gets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will return 0
79 * @return the value of the enum.
80 */
81 @Override
82 public Long getIntegerValue() {
83 return integerValue.getValue();
84 }
85
86 /**
87 * Sets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will set 0
88 * @param value The value of the enum.
89 */
90 public void setIntegerValue(long value) {
91 integerValue.setValue(value);
92 this.value = declaration.query(value);
93 }
94
95 @Override
96 public EnumDeclaration getDeclaration() {
97 return declaration;
98 }
99
100 // ------------------------------------------------------------------------
101 // Operations
102 // ------------------------------------------------------------------------
103
104 @Override
105 public void read(BitBuffer input) {
106 alignRead(input, this.declaration);
107 integerValue.read(input);
108 long val = integerValue.getValue();
109
110 // TODO: what to do if the integer value maps to no string for this
111 // integer ?
112 value = declaration.query(val);
113 }
114
115 @Override
116 public String toString() {
117 return "{ value = " + getValue() + //$NON-NLS-1$
118 ", container = " + integerValue.toString() + //$NON-NLS-1$
119 " }"; //$NON-NLS-1$
120 }
121 }
This page took 0.038783 seconds and 4 git commands to generate.