diff --git a/src/apps/debugger/model/ExpressionInfo.cpp b/src/apps/debugger/model/ExpressionInfo.cpp new file mode 100644 index 0000000000..0b2a58846f --- /dev/null +++ b/src/apps/debugger/model/ExpressionInfo.cpp @@ -0,0 +1,57 @@ +/* + * Copyright 2014, Rene Gollent, rene@gollent.com. + * Distributed under the terms of the MIT License. + */ + + +#include "ExpressionInfo.h" + +#include "Type.h" + + +ExpressionInfo::ExpressionInfo() + : + fExpression(), + fResultType(NULL) +{ +} + + +ExpressionInfo::ExpressionInfo(const ExpressionInfo& other) + : + fExpression(other.fExpression), + fResultType(other.fResultType) +{ + if (fResultType != NULL) + fResultType->AcquireReference(); +} + + +ExpressionInfo::~ExpressionInfo() +{ + if (fResultType != NULL) + fResultType->ReleaseReference(); +} + + +ExpressionInfo::ExpressionInfo(const BString& expression, Type* resultType) + : + fExpression(expression), + fResultType(resultType) +{ + if (resultType != NULL) + resultType->AcquireReference(); +} + + +void +ExpressionInfo::SetTo(const BString& expression, Type* resultType) +{ + fExpression = expression; + if (fResultType != NULL) + fResultType->ReleaseReference(); + + fResultType = resultType; + if (fResultType != NULL) + fResultType->AcquireReference(); +} diff --git a/src/apps/debugger/model/ExpressionInfo.h b/src/apps/debugger/model/ExpressionInfo.h new file mode 100644 index 0000000000..8e3411a619 --- /dev/null +++ b/src/apps/debugger/model/ExpressionInfo.h @@ -0,0 +1,37 @@ +/* + * Copyright 2014, Rene Gollent, rene@gollent.com. + * Distributed under the terms of the MIT License. + */ +#ifndef EXPRESSION_INFO_H +#define EXPRESSION_INFO_H + + +#include + +#include + + +class Type; + + +class ExpressionInfo : public BReferenceable { +public: + ExpressionInfo(); + ExpressionInfo(const ExpressionInfo& other); + ExpressionInfo(const BString& expression, + Type* resultType); + virtual ~ExpressionInfo(); + + void SetTo(const BString& expression, + Type* resultType); + + const BString& Expression() const { return fExpression; } + Type* ResultType() const { return fResultType; } + +private: + BString fExpression; + Type* fResultType; +}; + + +#endif // EXPRESSION_INFO_H diff --git a/src/apps/debugger/model/ExpressionValues.cpp b/src/apps/debugger/model/ExpressionValues.cpp new file mode 100644 index 0000000000..c6152a9ee2 --- /dev/null +++ b/src/apps/debugger/model/ExpressionValues.cpp @@ -0,0 +1,192 @@ +/* + * Copyright 2014, Rene Gollent, rene@gollent.com. + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ + + +#include "ExpressionValues.h" + +#include + +#include "FunctionID.h" +#include "StringUtils.h" +#include "Thread.h" + + +struct ExpressionValues::Key { + FunctionID* function; + Thread* thread; + BString expression; + + Key(FunctionID* function, Thread* thread, const BString& expression) + : + function(function), + thread(thread), + expression(expression) + { + } + + uint32 HashValue() const + { + return function->HashValue() ^ thread->ID() + ^ StringUtils::HashValue(expression); + } + + bool operator==(const Key& other) const + { + return *function == *other.function + && thread->ID() == other.thread->ID() + && expression == other.expression; + } +}; + + +struct ExpressionValues::ValueEntry : Key { + BVariant value; + ValueEntry* next; + + ValueEntry(FunctionID* function, Thread* thread, const BString& expression) + : + Key(function, thread, expression) + { + function->AcquireReference(); + thread->AcquireReference(); + } + + ~ValueEntry() + { + function->ReleaseReference(); + thread->ReleaseReference(); + } +}; + + +struct ExpressionValues::ValueEntryHashDefinition { + typedef Key KeyType; + typedef ValueEntry ValueType; + + size_t HashKey(const Key& key) const + { + return key.HashValue(); + } + + size_t Hash(const ValueEntry* value) const + { + return value->HashValue(); + } + + bool Compare(const Key& key, const ValueEntry* value) const + { + return key == *value; + } + + ValueEntry*& GetLink(ValueEntry* value) const + { + return value->next; + } +}; + + +ExpressionValues::ExpressionValues() + : + fValues(NULL) +{ +} + + +ExpressionValues::ExpressionValues(const ExpressionValues& other) + : + fValues(NULL) +{ + try { + // init + if (Init() != B_OK) + throw std::bad_alloc(); + + // clone all values + for (ValueTable::Iterator it = other.fValues->GetIterator(); + ValueEntry* entry = it.Next();) { + if (SetValue(entry->function, entry->thread, entry->expression, + entry->value) != B_OK) { + throw std::bad_alloc(); + } + } + } catch (...) { + _Cleanup(); + throw; + } +} + + +ExpressionValues::~ExpressionValues() +{ + _Cleanup(); +} + + +status_t +ExpressionValues::Init() +{ + fValues = new(std::nothrow) ValueTable; + if (fValues == NULL) + return B_NO_MEMORY; + + return fValues->Init(); +} + + +bool +ExpressionValues::GetValue(FunctionID* function, Thread* thread, + const BString* expression, BVariant& _value) const +{ + ValueEntry* entry = fValues->Lookup(Key(function, thread, *expression)); + if (entry == NULL) + return false; + + _value = entry->value; + return true; +} + + +bool +ExpressionValues::HasValue(FunctionID* function, Thread* thread, + const BString* expression) const +{ + return fValues->Lookup(Key(function, thread, *expression)) != NULL; +} + + +status_t +ExpressionValues::SetValue(FunctionID* function, Thread* thread, + const BString& expression, const BVariant& value) +{ + ValueEntry* entry = fValues->Lookup(Key(function, thread, expression)); + if (entry == NULL) { + entry = new(std::nothrow) ValueEntry(function, thread, expression); + if (entry == NULL) + return B_NO_MEMORY; + fValues->Insert(entry); + } + + entry->value = value; + return B_OK; +} + + +void +ExpressionValues::_Cleanup() +{ + if (fValues != NULL) { + ValueEntry* entry = fValues->Clear(true); + + while (entry != NULL) { + ValueEntry* next = entry->next; + delete entry; + entry = next; + } + + delete fValues; + fValues = NULL; + } +} diff --git a/src/apps/debugger/model/ExpressionValues.h b/src/apps/debugger/model/ExpressionValues.h new file mode 100644 index 0000000000..1498b5267f --- /dev/null +++ b/src/apps/debugger/model/ExpressionValues.h @@ -0,0 +1,82 @@ +/* + * Copyright 2014, Rene Gollent, rene@gollent.com. + * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. + * Distributed under the terms of the MIT License. + */ +#ifndef EXPRESSION_VALUES_H +#define EXPRESSION_VALUES_H + + +#include + +#include +#include +#include + + +class FunctionID; +class Thread; + + +class ExpressionValues : public BReferenceable { +public: + ExpressionValues(); + ExpressionValues(const ExpressionValues& other); + // throws std::bad_alloc + virtual ~ExpressionValues(); + + status_t Init(); + + bool GetValue(FunctionID* function, + Thread* thread, + const BString* expression, + BVariant& _value) const; + inline bool GetValue(FunctionID* function, + Thread* thread, + const BString& expression, + BVariant& _value) const; + bool HasValue(FunctionID* function, + Thread* thread, + const BString* expression) const; + inline bool HasValue(FunctionID* function, + Thread* thread, + const BString& expression) const; + status_t SetValue(FunctionID* function, + Thread* thread, + const BString& expression, + const BVariant& value); + +private: + struct Key; + struct ValueEntry; + struct ValueEntryHashDefinition; + + typedef BOpenHashTable ValueTable; + +private: + ExpressionValues& operator=(const ExpressionValues& other); + + void _Cleanup(); + +private: + ValueTable* fValues; +}; + + +bool +ExpressionValues::GetValue(FunctionID* function, Thread* thread, + const BString& expression, BVariant& _value) const +{ + return GetValue(function, thread, &expression, _value); +} + + +bool +ExpressionValues::HasValue(FunctionID* function, Thread* thread, + const BString& expression) const +{ + return HasValue(function, thread, &expression); +} + + +#endif // EXPRESSION_VALUES_H