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