xref: /haiku/src/apps/debugger/user_interface/gui/model/VariablesViewState.cpp (revision c0936b5a0384bc6fe654d296ee54222a0f45d2b6)
1 /*
2  * Copyright 2013-2014, Rene Gollent, rene@gollent.com.
3  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "VariablesViewState.h"
9 
10 #include <new>
11 
12 #include "ExpressionValues.h"
13 #include "FunctionID.h"
14 #include "StackFrameValues.h"
15 #include "Type.h"
16 #include "TypeComponentPath.h"
17 
18 
19 // #pragma mark - VariablesViewNodeInfo
20 
21 
22 VariablesViewNodeInfo::VariablesViewNodeInfo()
23 	:
24 	fNodeExpanded(false),
25 	fCastedType(NULL),
26 	fRendererSettings()
27 {
28 }
29 
30 
31 VariablesViewNodeInfo::VariablesViewNodeInfo(const VariablesViewNodeInfo& other)
32 	:
33 	fNodeExpanded(other.fNodeExpanded),
34 	fCastedType(other.fCastedType),
35 	fRendererSettings(other.fRendererSettings)
36 {
37 	if (fCastedType != NULL)
38 		fCastedType->AcquireReference();
39 }
40 
41 
42 VariablesViewNodeInfo::~VariablesViewNodeInfo()
43 {
44 	if (fCastedType != NULL)
45 		fCastedType->ReleaseReference();
46 }
47 
48 
49 VariablesViewNodeInfo&
50 VariablesViewNodeInfo::operator=(const VariablesViewNodeInfo& other)
51 {
52 	fNodeExpanded = other.fNodeExpanded;
53 	SetCastedType(other.fCastedType);
54 	fRendererSettings = other.fRendererSettings;
55 
56 	return *this;
57 }
58 
59 
60 void
61 VariablesViewNodeInfo::SetNodeExpanded(bool expanded)
62 {
63 	fNodeExpanded = expanded;
64 }
65 
66 
67 void
68 VariablesViewNodeInfo::SetCastedType(Type* type)
69 {
70 	if (fCastedType != NULL)
71 		fCastedType->ReleaseReference();
72 
73 	fCastedType = type;
74 	if (fCastedType != NULL)
75 		fCastedType->AcquireReference();
76 }
77 
78 
79 void
80 VariablesViewNodeInfo::SetRendererSettings(const BMessage& settings)
81 {
82 	fRendererSettings = settings;
83 }
84 
85 
86 // #pragma mark - Key
87 
88 
89 struct VariablesViewState::Key {
90 	ObjectID*			variable;
91 	TypeComponentPath*	path;
92 
93 	Key(ObjectID* variable, TypeComponentPath* path)
94 		:
95 		variable(variable),
96 		path(path)
97 	{
98 	}
99 
100 	uint32 HashValue() const
101 	{
102 		return variable->HashValue() ^ path->HashValue();
103 	}
104 
105 	bool operator==(const Key& other) const
106 	{
107 		return *variable == *other.variable && *path == *other.path;
108 	}
109 };
110 
111 
112 // #pragma mark - InfoEntry
113 
114 
115 struct VariablesViewState::InfoEntry : Key, VariablesViewNodeInfo {
116 	InfoEntry*	next;
117 
118 	InfoEntry(ObjectID* variable, TypeComponentPath* path)
119 		:
120 		Key(variable, path)
121 	{
122 		variable->AcquireReference();
123 		path->AcquireReference();
124 	}
125 
126 	~InfoEntry()
127 	{
128 		variable->ReleaseReference();
129 		path->ReleaseReference();
130 	}
131 
132 	void SetInfo(const VariablesViewNodeInfo& info)
133 	{
134 		VariablesViewNodeInfo::operator=(info);
135 	}
136 };
137 
138 
139 struct VariablesViewState::InfoEntryHashDefinition {
140 	typedef Key			KeyType;
141 	typedef	InfoEntry	ValueType;
142 
143 	size_t HashKey(const Key& key) const
144 	{
145 		return key.HashValue();
146 	}
147 
148 	size_t Hash(const InfoEntry* value) const
149 	{
150 		return value->HashValue();
151 	}
152 
153 	bool Compare(const Key& key, const InfoEntry* value) const
154 	{
155 		return key == *value;
156 	}
157 
158 	InfoEntry*& GetLink(InfoEntry* value) const
159 	{
160 		return value->next;
161 	}
162 };
163 
164 
165 VariablesViewState::VariablesViewState()
166 	:
167 	fNodeInfos(NULL),
168 	fValues(NULL),
169 	fExpressionValues(NULL)
170 {
171 }
172 
173 
174 VariablesViewState::~VariablesViewState()
175 {
176 	_Cleanup();
177 }
178 
179 
180 status_t
181 VariablesViewState::Init()
182 {
183 	fNodeInfos = new(std::nothrow) NodeInfoTable;
184 	if (fNodeInfos == NULL)
185 		return B_NO_MEMORY;
186 
187 	return fNodeInfos->Init();
188 }
189 
190 void
191 VariablesViewState::SetValues(StackFrameValues* values)
192 {
193 	if (fValues == values)
194 		return;
195 
196 	if (fValues != NULL)
197 		fValues->ReleaseReference();
198 
199 	fValues = values;
200 
201 	if (fValues != NULL)
202 		fValues->AcquireReference();
203 }
204 
205 
206 void
207 VariablesViewState::SetExpressionValues(ExpressionValues* values)
208 {
209 	if (fExpressionValues == values)
210 		return;
211 
212 	if (fExpressionValues != NULL)
213 		fExpressionValues->ReleaseReference();
214 
215 	fExpressionValues = values;
216 
217 	if (fExpressionValues != NULL)
218 		fExpressionValues->AcquireReference();
219 }
220 
221 
222 const VariablesViewNodeInfo*
223 VariablesViewState::GetNodeInfo(ObjectID* variable,
224 	const TypeComponentPath* path) const
225 {
226 	return fNodeInfos->Lookup(Key(variable, (TypeComponentPath*)path));
227 }
228 
229 
230 status_t
231 VariablesViewState::SetNodeInfo(ObjectID* variable, TypeComponentPath* path,
232 	const VariablesViewNodeInfo& info)
233 {
234 	InfoEntry* entry = fNodeInfos->Lookup(Key(variable, path));
235 	if (entry == NULL) {
236 		entry = new(std::nothrow) InfoEntry(variable, path);
237 		if (entry == NULL)
238 			return B_NO_MEMORY;
239 		fNodeInfos->Insert(entry);
240 	}
241 
242 	entry->SetInfo(info);
243 	return B_OK;
244 }
245 
246 
247 void
248 VariablesViewState::_Cleanup()
249 {
250 	if (fNodeInfos != NULL) {
251 		InfoEntry* entry = fNodeInfos->Clear(true);
252 
253 		while (entry != NULL) {
254 			InfoEntry* next = entry->next;
255 			delete entry;
256 			entry = next;
257 		}
258 
259 		delete fNodeInfos;
260 		fNodeInfos = NULL;
261 	}
262 
263 	SetValues(NULL);
264 	SetExpressionValues(NULL);
265 }
266