| 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 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
| 17 | |
| 18 | /** |
| 19 | * A CTF definition |
| 20 | * |
| 21 | * A definition is like an object of a declaration class. It fills the |
| 22 | * declaration with values. <br> |
| 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 | * |
| 29 | * @version 1.0 |
| 30 | * @author Matthew Khouzam |
| 31 | * @author Simon Marchi |
| 32 | */ |
| 33 | public abstract class Definition { |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | // Attributes |
| 37 | // ------------------------------------------------------------------------ |
| 38 | |
| 39 | private final String fieldName; |
| 40 | |
| 41 | /** The complete path of this field */ |
| 42 | private final String path; |
| 43 | |
| 44 | private final IDefinitionScope definitionScope; |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Constructors |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 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 | */ |
| 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 | } |
| 72 | } |
| 73 | |
| 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; |
| 109 | } |
| 110 | |
| 111 | // ------------------------------------------------------------------------ |
| 112 | // Operations |
| 113 | // ------------------------------------------------------------------------ |
| 114 | |
| 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. |
| 127 | * @throws CTFReaderException |
| 128 | * An error occurred reading the data. If the buffer is reading |
| 129 | * beyond its end, this exception will be raised. |
| 130 | * @since 2.0 |
| 131 | */ |
| 132 | public abstract void read(BitBuffer input) throws CTFReaderException; |
| 133 | |
| 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 |
| 141 | * @throws CTFReaderException |
| 142 | * Happens when there is an out of bounds exception |
| 143 | * @since 2.2 |
| 144 | */ |
| 145 | protected static void alignRead(BitBuffer input, IDeclaration declaration) throws CTFReaderException{ |
| 146 | long mask = declaration.getAlignment() -1; |
| 147 | /* |
| 148 | * The alignment is a power of 2 |
| 149 | */ |
| 150 | long pos = input.position(); |
| 151 | if ((pos & mask) == 0) { |
| 152 | return; |
| 153 | } |
| 154 | pos = (pos + mask) & ~mask; |
| 155 | input.position(pos); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public String toString() { |
| 160 | return path + '[' + Integer.toHexString(hashCode()) + ']'; |
| 161 | } |
| 162 | } |