Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2669d17a13ee8cac01c3f7b1a006d15723dd3305 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
 * Copyright (c) 2000, 2012 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.mi.core.cdi.model;

import org.eclipse.cdt.debug.core.cdi.CDIException;
import org.eclipse.cdt.debug.core.cdi.model.ICDIValue;
import org.eclipse.cdt.debug.core.cdi.model.ICDIVariable;
import org.eclipse.cdt.debug.core.cdi.model.type.ICDIType;
import org.eclipse.cdt.debug.mi.core.MIException;
import org.eclipse.cdt.debug.mi.core.MISession;
import org.eclipse.cdt.debug.mi.core.cdi.CdiResources;
import org.eclipse.cdt.debug.mi.core.command.CommandFactory;
import org.eclipse.cdt.debug.mi.core.command.MIVarEvaluateExpression;
import org.eclipse.cdt.debug.mi.core.output.MIVarEvaluateExpressionInfo;

/**
 */
public class Value extends CObject implements ICDIValue {

	protected Variable fVariable;

	/**
	 * Indicates whether this Value object is for a C++ reference variable. If
	 * it is, then some decoding is needed on the value string we get from gdb,
	 * since it will contain two things: the address of the variable being
	 * referenced and the value.
	 * @since 6.0
	 */
	protected boolean fIsReference;

	public Value(Variable v) {
		super((Target)v.getTarget());
		fVariable = v;
	}

	protected Variable getVariable() throws CDIException {
		return fVariable;
	}

	/**
	 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIValue#getTypeName()
	 */
	@Override
	public String getTypeName() throws CDIException {
		return getVariable().getTypeName();
	}

	/**
	 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIValue#getValueString()
	 */
	@Override
	public String getValueString() throws CDIException {
		// make sure the variable is updated.
		if (! getVariable().isUpdated()) {
			getVariable().update();
		}

		String result = ""; //$NON-NLS-1$
		MISession mi = ((Target)getTarget()).getMISession();
		CommandFactory factory = mi.getCommandFactory();
		MIVarEvaluateExpression var =
			factory.createMIVarEvaluateExpression(getVariable().getMIVar().getVarName());
		try {
			mi.postCommand(var);
			MIVarEvaluateExpressionInfo info = var.getMIVarEvaluateExpressionInfo();
			if (info == null) {
				throw new CDIException(CdiResources.getString("cdi.Common.No_answer")); //$NON-NLS-1$
			}
			result = info.getValue();
			
			// Reference variables get back a string with two things: the address of the 
			// variable being referenced and the value of the variable. The expected
			// format is, by example (for a float&):	"@0x22cc98: 3.19616001e-39"
			// We need to dig out the latter.
			if (fIsReference) {
				if (result.startsWith("@0x")) { //$NON-NLS-1$
					int index = result.indexOf(':');
					if (index > 0 && ((index + 1) < result.length())) {
						result = result.substring(index+1).trim();
					}
				}
			}
		} catch (MIException e) {
			throw new CDIException(e.getMessage());
		}
		return result;
	}

	/**
	 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIValue#getVariables()
	 */	
	@Override
	public int getChildrenNumber() throws CDIException {
		return getVariable().getMIVar().getNumChild();
	}
	
	/**
	 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIValue#getVariables()
	 */
	@Override
	public boolean hasChildren() throws CDIException {
	/*
		int number = 0;
		MISession mi = getCTarget().getCSession().getMISession();
		CommandFactory factory = mi.getCommandFactory();
		MIVarInfoNumChildren children = 
			factory.createMIVarInfoNumChildren(variable.getMIVar().getVarName());
		try {
			mi.postCommand(children);
			MIVarInfoNumChildrenInfo info = children.getMIVarInfoNumChildrenInfo();
			if (info == null) {
				throw new CDIException("No answer");
			}
			number = info.getChildNumber();
		} catch (MIException e) {
			throw new CDIException(e.getMessage());
		}
		return (number > 0);
	*/
		return (getChildrenNumber() > 0);	
	}

	/**
	 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIValue#getVariables()
	 */
	@Override
	public ICDIVariable[] getVariables() throws CDIException {
		return getVariable().getChildren();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.debug.core.cdi.model.ICDIValue#getType()
	 */
	@Override
	public ICDIType getType() throws CDIException {
		return getVariable().getType();
	}

	/**
	 * Call this after construction with 'true' if the Value is for a reference
	 * variable. See {@link #fIsReference}.
	 * 
	 * Ideally, this property would be passed to the constructor. However
	 * introducing it that way at this point in time would cause a lot of churn
	 * in the codebase, since this class is not directly instantiated, and it
	 * has many subclasses.
	 * @since 6.0
	 */
	public void setIsReference(boolean isReference) {
		fIsReference = isReference;
	}

}

Back to the top