Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 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 | ||
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 | /** | |
a511da0d | 19 | * A CTF definition |
d37aaa7f | 20 | * |
abec0e9c MK |
21 | * A definition is like an object of a declaration class. It fills the |
22 | * declaration with values. <br> | |
9ac2eb62 MK |
23 | * An example: <br> |
24 | * int i = 0; <br> | |
25 | * <b>int</b> is the declaration.<br> | |
26 | * <b>i</b> is the definition.<br> | |
27 | * <b>0</b> is the value assigned to the definition, not the declaration.<br> | |
28 | * | |
d37aaa7f FC |
29 | * @version 1.0 |
30 | * @author Matthew Khouzam | |
31 | * @author Simon Marchi | |
866e5b51 FC |
32 | */ |
33 | public abstract class Definition { | |
34 | ||
35 | // ------------------------------------------------------------------------ | |
36 | // Attributes | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
0594c61c | 39 | private final String fieldName; |
866e5b51 | 40 | |
07002e0a | 41 | /** The complete path of this field */ |
0594c61c | 42 | private final String path; |
866e5b51 | 43 | |
0594c61c | 44 | private final IDefinitionScope definitionScope; |
866e5b51 FC |
45 | |
46 | // ------------------------------------------------------------------------ | |
47 | // Constructors | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
9ac2eb62 MK |
50 | /** |
51 | * Constructor | |
52 | * | |
53 | * @param definitionScope | |
54 | * the definition is in a scope, (normally a struct) what is it? | |
55 | * @param fieldName | |
56 | * the name of the definition. (it is a field in the parent | |
57 | * scope) | |
58 | */ | |
866e5b51 FC |
59 | public Definition(IDefinitionScope definitionScope, String fieldName) { |
60 | this.definitionScope = definitionScope; | |
61 | this.fieldName = fieldName; | |
62 | if (definitionScope != null) { | |
63 | String parentPath = definitionScope.getPath(); | |
64 | if (parentPath.length() > 0) { | |
65 | path = parentPath + "." + fieldName; //$NON-NLS-1$ | |
66 | } else { | |
67 | path = fieldName; | |
68 | } | |
69 | } else { | |
70 | path = fieldName; | |
71 | } | |
0594c61c | 72 | } |
866e5b51 | 73 | |
0594c61c AM |
74 | // ------------------------------------------------------------------------ |
75 | // Getters | |
76 | // ------------------------------------------------------------------------ | |
77 | ||
78 | /** | |
79 | * Get the field name in its container. | |
80 | * | |
81 | * @return The field name | |
82 | * @since 2.0 | |
83 | */ | |
84 | protected String getFieldName() { | |
85 | return fieldName; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Get the complete path of this field. | |
90 | * | |
91 | * @return The path | |
92 | * @since 2.0 | |
93 | */ | |
94 | public String getPath() { | |
95 | return path; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Get the definition scope in which this definition is found. | |
100 | * | |
101 | * The complete path of a definition is thus the path of the definition | |
102 | * scope DOT the name of the definition (name of the field in its container) | |
103 | * | |
104 | * @return The definition scope | |
105 | * @since 2.0 | |
106 | */ | |
107 | protected IDefinitionScope getDefinitionScope() { | |
108 | return definitionScope; | |
866e5b51 FC |
109 | } |
110 | ||
111 | // ------------------------------------------------------------------------ | |
112 | // Operations | |
113 | // ------------------------------------------------------------------------ | |
114 | ||
9ac2eb62 MK |
115 | /** |
116 | * | |
117 | * @return gets the declaration of a datatype | |
118 | * | |
119 | */ | |
120 | public abstract IDeclaration getDeclaration(); | |
121 | ||
122 | /** | |
123 | * Read the definition from a bitbuffer | |
124 | * | |
125 | * @param input | |
126 | * the bitbuffer containing the data to read. | |
db8e8f7d AM |
127 | * @throws CTFReaderException |
128 | * An error occurred reading the data. If the buffer is reading | |
129 | * beyond its end, this exception will be raised. | |
486efb2e | 130 | * @since 2.0 |
9ac2eb62 | 131 | */ |
db8e8f7d | 132 | public abstract void read(BitBuffer input) throws CTFReaderException; |
866e5b51 | 133 | |
d6205f97 MK |
134 | /** |
135 | * Offset the buffer position wrt the current alignment. | |
136 | * | |
137 | * @param input | |
138 | * The bitbuffer that is being read | |
139 | * @param declaration | |
140 | * The declaration which has an alignment | |
4c67e724 MK |
141 | * @throws CTFReaderException |
142 | * Happens when there is an out of bounds exception | |
d6205f97 MK |
143 | * @since 2.2 |
144 | */ | |
4c67e724 | 145 | protected static void alignRead(BitBuffer input, IDeclaration declaration) throws CTFReaderException{ |
ae46c654 | 146 | long mask = declaration.getAlignment() -1; |
abec0e9c MK |
147 | /* |
148 | * The alignment is a power of 2 | |
149 | */ | |
ae46c654 | 150 | long pos = input.position(); |
576dc674 MK |
151 | if ((pos & mask) == 0) { |
152 | return; | |
153 | } | |
154 | pos = (pos + mask) & ~mask; | |
d6205f97 MK |
155 | input.position(pos); |
156 | } | |
157 | ||
866e5b51 FC |
158 | @Override |
159 | public String toString() { | |
160 | return path + '[' + Integer.toHexString(hashCode()) + ']'; | |
161 | } | |
162 | } |