ctf: move ArrayDefinition.isString to be part of the declaration
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / ArrayDeclaration.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 /**
16 * A CTF array declaration
17 *
18 * Arrays are fixed-length. Their length is declared in the type declaration
19 * within the meta-data. They contain an array of "inner type" elements, which
20 * can refer to any type not containing the type of the array being declared (no
21 * circular dependency). The length is the number of elements in an array.
22 *
23 * @version 1.0
24 * @author Matthew Khouzam
25 * @author Simon Marchi
26 */
27 public class ArrayDeclaration implements IDeclaration {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 private final int length;
34 private final IDeclaration elemType;
35
36 // ------------------------------------------------------------------------
37 // Constructors
38 // ------------------------------------------------------------------------
39
40 /**
41 * Constructor
42 *
43 * @param length
44 * how many elements in the array
45 * @param elemType
46 * what type of element is in the array
47 */
48 public ArrayDeclaration(int length, IDeclaration elemType) {
49 this.length = length;
50 this.elemType = elemType;
51 }
52
53 // ------------------------------------------------------------------------
54 // Getters/Setters/Predicates
55 // ------------------------------------------------------------------------
56
57 /**
58 *
59 * @return the type of element in the array
60 */
61 public IDeclaration getElementType() {
62 return elemType;
63 }
64
65 /**
66 *
67 * @return how many elements in the array
68 */
69 public int getLength() {
70 return length;
71 }
72
73 /**
74 * Sometimes, strings are encoded as an array of 1-byte integers (each one
75 * being an UTF-8 byte).
76 *
77 * @return true if this array is in fact an UTF-8 string. false if it's a
78 * "normal" array of generic Definition's.
79 * @since 3.0
80 */
81 public boolean isString() {
82 if (elemType instanceof IntegerDeclaration) {
83 /*
84 * If the first byte is a "character", we'll consider the whole
85 * array a character string.
86 */
87 IntegerDeclaration elemInt = (IntegerDeclaration) elemType;
88 if (elemInt.isCharacter()) {
89 return true;
90 }
91 }
92 return false;
93 }
94
95 @Override
96 public long getAlignment() {
97 return getElementType().getAlignment();
98 }
99
100 // ------------------------------------------------------------------------
101 // Operations
102 // ------------------------------------------------------------------------
103
104 @Override
105 public ArrayDefinition createDefinition(IDefinitionScope definitionScope,
106 String fieldName) {
107 return new ArrayDefinition(this, definitionScope, fieldName);
108 }
109
110 @Override
111 public String toString() {
112 /* Only used for debugging */
113 return "[declaration] array[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
114 }
115
116 }
This page took 0.049953 seconds and 6 git commands to generate.