dd8cb67ed2f435b63943ba56f4d99421925ce8ed
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / Definition.java
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
15 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
16
17 /**
18 * A CTF definition
19 *
20 * A definition is like an object of a declaration class. It fills the
21 * declaration with values. <br>
22 * An example: <br>
23 * int i = 0; <br>
24 * <b>int</b> is the declaration.<br>
25 * <b>i</b> is the definition.<br>
26 * <b>0</b> is the value assigned to the definition, not the declaration.<br>
27 *
28 * @version 1.0
29 * @author Matthew Khouzam
30 * @author Simon Marchi
31 */
32 public abstract class Definition {
33
34 // ------------------------------------------------------------------------
35 // Attributes
36 // ------------------------------------------------------------------------
37
38 private final String fieldName;
39
40 /** The complete path of this field */
41 private final String path;
42
43 private final IDefinitionScope definitionScope;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * Constructor
51 *
52 * @param definitionScope
53 * the definition is in a scope, (normally a struct) what is it?
54 * @param fieldName
55 * the name of the definition. (it is a field in the parent
56 * scope)
57 */
58 public Definition(IDefinitionScope definitionScope, String fieldName) {
59 this.definitionScope = definitionScope;
60 this.fieldName = fieldName;
61 if (definitionScope != null) {
62 String parentPath = definitionScope.getPath();
63 if (parentPath.length() > 0) {
64 path = parentPath + "." + fieldName; //$NON-NLS-1$
65 } else {
66 path = fieldName;
67 }
68 } else {
69 path = fieldName;
70 }
71 }
72
73 // ------------------------------------------------------------------------
74 // Getters
75 // ------------------------------------------------------------------------
76
77 /**
78 * Get the field name in its container.
79 *
80 * @return The field name
81 * @since 2.0
82 */
83 protected String getFieldName() {
84 return fieldName;
85 }
86
87 /**
88 * Get the complete path of this field.
89 *
90 * @return The path
91 * @since 2.0
92 */
93 public String getPath() {
94 return path;
95 }
96
97 /**
98 * Get the definition scope in which this definition is found.
99 *
100 * The complete path of a definition is thus the path of the definition
101 * scope DOT the name of the definition (name of the field in its container)
102 *
103 * @return The definition scope
104 * @since 2.0
105 */
106 protected IDefinitionScope getDefinitionScope() {
107 return definitionScope;
108 }
109
110 // ------------------------------------------------------------------------
111 // Operations
112 // ------------------------------------------------------------------------
113
114 /**
115 *
116 * @return gets the declaration of a datatype
117 *
118 */
119 public abstract IDeclaration getDeclaration();
120
121 /**
122 * Read the definition from a bitbuffer
123 *
124 * @param input
125 * the bitbuffer containing the data to read.
126 * @since 2.0
127 */
128 public abstract void read(BitBuffer input);
129
130 /**
131 * Offset the buffer position wrt the current alignment.
132 *
133 * @param input
134 * The bitbuffer that is being read
135 * @param declaration
136 * The declaration which has an alignment
137 * @since 2.2
138 */
139 protected static void alignRead(BitBuffer input, IDeclaration declaration){
140 long mask = declaration.getAlignment() -1;
141 /*
142 * The alignment is a power of 2
143 */
144 long pos = input.position();
145 if ((pos & mask) == 0) {
146 return;
147 }
148 pos = (pos + mask) & ~mask;
149
150 input.position(pos);
151 }
152
153 @Override
154 public String toString() {
155 return path + '[' + Integer.toHexString(hashCode()) + ']';
156 }
157 }
This page took 0.034092 seconds and 4 git commands to generate.