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; |
866e5b51 FC |
16 | |
17 | /** | |
d37aaa7f | 18 | * A CTF enum definition. |
21fb02fa | 19 | * |
d37aaa7f FC |
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 | |
866e5b51 | 26 | */ |
21fb02fa | 27 | public class EnumDefinition extends SimpleDatatypeDefinition { |
866e5b51 FC |
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 | ||
9ac2eb62 MK |
43 | /** |
44 | * Constructor | |
45 | * @param declaration the parent declaration | |
46 | * @param definitionScope the parent scope | |
47 | * @param fieldName the field name | |
48 | */ | |
866e5b51 FC |
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); | |
a54572d8 | 57 | value = declaration.query(integerValue.getValue()); |
866e5b51 FC |
58 | } |
59 | ||
60 | // ------------------------------------------------------------------------ | |
61 | // Getters/Setters/Predicates | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
9ac2eb62 MK |
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 | */ | |
866e5b51 FC |
68 | public String getValue() { |
69 | return value; | |
70 | } | |
71 | ||
21fb02fa MK |
72 | @Override |
73 | public String getStringValue(){ | |
74 | return getValue(); | |
75 | } | |
76 | ||
9ac2eb62 MK |
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 | */ | |
21fb02fa MK |
81 | @Override |
82 | public Long getIntegerValue() { | |
866e5b51 FC |
83 | return integerValue.getValue(); |
84 | } | |
85 | ||
9ac2eb62 MK |
86 | /** |
87 | * Sets the value of the enum in string format so "Enum a{DAY="0", NIGHT="1"}; will set 0 | |
0594c61c | 88 | * @param value The value of the enum. |
9ac2eb62 | 89 | */ |
0594c61c AM |
90 | public void setIntegerValue(long value) { |
91 | integerValue.setValue(value); | |
92 | this.value = declaration.query(value); | |
866e5b51 FC |
93 | } |
94 | ||
9ac2eb62 MK |
95 | @Override |
96 | public EnumDeclaration getDeclaration() { | |
97 | return declaration; | |
98 | } | |
99 | ||
866e5b51 FC |
100 | // ------------------------------------------------------------------------ |
101 | // Operations | |
102 | // ------------------------------------------------------------------------ | |
103 | ||
104 | @Override | |
105 | public void read(BitBuffer input) { | |
d6205f97 | 106 | alignRead(input, this.declaration); |
866e5b51 FC |
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 | ||
419f09a8 SM |
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 | } | |
866e5b51 | 121 | } |