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