2 * Copyright 2014-2016, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
8 #include "ExpressionValues.h"
12 #include "FunctionID.h"
13 #include "model/Thread.h"
14 #include "StringUtils.h"
17 struct ExpressionValues::Key
{
22 Key(FunctionID
* function
, ::Thread
* thread
, const BString
& expression
)
26 expression(expression
)
30 uint32
HashValue() const
32 return function
->HashValue() ^ thread
->ID()
33 ^ StringUtils::HashValue(expression
);
36 bool operator==(const Key
& other
) const
38 return *function
== *other
.function
39 && thread
->ID() == other
.thread
->ID()
40 && expression
== other
.expression
;
45 struct ExpressionValues::ValueEntry
: Key
{
49 ValueEntry(FunctionID
* function
, ::Thread
* thread
,
50 const BString
& expression
)
52 Key(function
, thread
, expression
)
54 function
->AcquireReference();
55 thread
->AcquireReference();
60 function
->ReleaseReference();
61 thread
->ReleaseReference();
66 struct ExpressionValues::ValueEntryHashDefinition
{
68 typedef ValueEntry ValueType
;
70 size_t HashKey(const Key
& key
) const
72 return key
.HashValue();
75 size_t Hash(const ValueEntry
* value
) const
77 return value
->HashValue();
80 bool Compare(const Key
& key
, const ValueEntry
* value
) const
85 ValueEntry
*& GetLink(ValueEntry
* value
) const
92 ExpressionValues::ExpressionValues()
99 ExpressionValues::ExpressionValues(const ExpressionValues
& other
)
106 throw std::bad_alloc();
109 for (ValueTable::Iterator it
= other
.fValues
->GetIterator();
110 ValueEntry
* entry
= it
.Next();) {
111 if (SetValue(entry
->function
, entry
->thread
, entry
->expression
,
112 entry
->value
) != B_OK
) {
113 throw std::bad_alloc();
123 ExpressionValues::~ExpressionValues()
130 ExpressionValues::Init()
132 fValues
= new(std::nothrow
) ValueTable
;
136 return fValues
->Init();
141 ExpressionValues::GetValue(FunctionID
* function
, ::Thread
* thread
,
142 const BString
* expression
, BVariant
& _value
) const
144 ValueEntry
* entry
= fValues
->Lookup(Key(function
, thread
, *expression
));
148 _value
= entry
->value
;
154 ExpressionValues::HasValue(FunctionID
* function
, ::Thread
* thread
,
155 const BString
* expression
) const
157 return fValues
->Lookup(Key(function
, thread
, *expression
)) != NULL
;
162 ExpressionValues::SetValue(FunctionID
* function
, ::Thread
* thread
,
163 const BString
& expression
, const BVariant
& value
)
165 ValueEntry
* entry
= fValues
->Lookup(Key(function
, thread
, expression
));
167 entry
= new(std::nothrow
) ValueEntry(function
, thread
, expression
);
170 fValues
->Insert(entry
);
173 entry
->value
= value
;
179 ExpressionValues::_Cleanup()
181 if (fValues
!= NULL
) {
182 ValueEntry
* entry
= fValues
->Clear(true);
184 while (entry
!= NULL
) {
185 ValueEntry
* next
= entry
->next
;