From 1c6c5f3b57c2634b7666aff000b3aec13208c37e Mon Sep 17 00:00:00 2001 From: Rene Gollent Date: Mon, 27 Oct 2014 11:31:39 -0400 Subject: [PATCH] Debugger: Add Number class for expression usage. - This class abstracts out the underlying type of a value, and handles all the basic mathematical logical operators accordingly. Replaces the MAPM type previously used for these respective operations. --- src/apps/debugger/Jamfile | 1 + src/apps/debugger/types/Number.cpp | 1008 ++++++++++++++++++++++++++++ src/apps/debugger/types/Number.h | 56 ++ 3 files changed, 1065 insertions(+) create mode 100644 src/apps/debugger/types/Number.cpp create mode 100644 src/apps/debugger/types/Number.h diff --git a/src/apps/debugger/Jamfile b/src/apps/debugger/Jamfile index ca763a86b5..3c730066d9 100644 --- a/src/apps/debugger/Jamfile +++ b/src/apps/debugger/Jamfile @@ -219,6 +219,7 @@ Application Debugger : # types ArrayIndexPath.cpp + Number.cpp TargetAddressRangeList.cpp ValueLocation.cpp diff --git a/src/apps/debugger/types/Number.cpp b/src/apps/debugger/types/Number.cpp new file mode 100644 index 0000000000..b8e007a03d --- /dev/null +++ b/src/apps/debugger/types/Number.cpp @@ -0,0 +1,1008 @@ +/* + * Copyright 2014, Rene Gollent, rene@gollent.com. + * Distributed under the terms of the MIT License. + */ + + +#include "Number.h" + +#include + +#include + + +Number::Number() + : + fValue() +{ +} + + +Number::~Number() +{ +} + + +Number::Number(type_code type, const BString& value) +{ + SetTo(type, value); +} + + +Number::Number(const BVariant& value) + : + fValue(value) +{ +} + + +Number::Number(const Number& other) + : + fValue(other.fValue) +{ +} + + +void +Number::SetTo(type_code type, const BString& value, int32 base) +{ + switch (type) { + case B_INT8_TYPE: + { + int8 tempValue = (int8)strtol(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_UINT8_TYPE: + { + uint8 tempValue = (uint8)strtoul(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_INT16_TYPE: + { + int16 tempValue = (int16)strtol(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_UINT16_TYPE: + { + uint16 tempValue = (uint16)strtoul(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_INT32_TYPE: + { + int32 tempValue = (int32)strtol(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_UINT32_TYPE: + { + uint32 tempValue = (int32)strtoul(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_INT64_TYPE: + { + int64 tempValue = (int64)strtoll(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_UINT64_TYPE: + { + uint64 tempValue = (uint64)strtol(value.String(), NULL, base); + fValue.SetTo(tempValue); + break; + } + + case B_FLOAT_TYPE: + { + float tempValue = strtof(value.String(), NULL); + fValue.SetTo(tempValue); + break; + } + + case B_DOUBLE_TYPE: + { + double tempValue = strtod(value.String(), NULL); + fValue.SetTo(tempValue); + break; + } + } +} + + +void +Number::SetTo(const BVariant& value) +{ + fValue = value; +} + + +Number& +Number::operator=(const Number& rhs) +{ + fValue = rhs.fValue; + return *this; +} + + +Number& +Number::operator+=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() + rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() + rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() + rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() + rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() + rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() + rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() + rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() + rhs.fValue.ToUInt64()); + break; + } + + case B_FLOAT_TYPE: + { + fValue.SetTo(fValue.ToFloat() + rhs.fValue.ToFloat()); + break; + } + + case B_DOUBLE_TYPE: + { + fValue.SetTo(fValue.ToDouble() + rhs.fValue.ToDouble()); + break; + } + } + + return *this; +} + + +Number& +Number::operator-=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() - rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() - rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() - rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() - rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() - rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() - rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() - rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() - rhs.fValue.ToUInt64()); + break; + } + + case B_FLOAT_TYPE: + { + fValue.SetTo(fValue.ToFloat() - rhs.fValue.ToFloat()); + break; + } + + case B_DOUBLE_TYPE: + { + fValue.SetTo(fValue.ToDouble() - rhs.fValue.ToDouble()); + break; + } + } + + return *this; +} + + +Number& +Number::operator/=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() / rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() / rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() / rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() / rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() / rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() / rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() / rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() / rhs.fValue.ToUInt64()); + break; + } + + case B_FLOAT_TYPE: + { + fValue.SetTo(fValue.ToFloat() / rhs.fValue.ToFloat()); + break; + } + + case B_DOUBLE_TYPE: + { + fValue.SetTo(fValue.ToDouble() / rhs.fValue.ToDouble()); + break; + } + } + + return *this; +} + + +Number& +Number::operator%=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() % rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() % rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() % rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() % rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() % rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() % rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() % rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() % rhs.fValue.ToUInt64()); + break; + } + } + + return *this; +} + + +Number& +Number::operator*=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() * rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() * rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() * rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() * rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() * rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() * rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() * rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() * rhs.fValue.ToUInt64()); + break; + } + + case B_FLOAT_TYPE: + { + fValue.SetTo(fValue.ToFloat() * rhs.fValue.ToFloat()); + break; + } + + case B_DOUBLE_TYPE: + { + fValue.SetTo(fValue.ToDouble() * rhs.fValue.ToDouble()); + break; + } + } + + return *this; +} + + +Number& +Number::operator&=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() & rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() & rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() & rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() & rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() & rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() & rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() & rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() & rhs.fValue.ToUInt64()); + break; + } + } + + return *this; +} + + +Number& +Number::operator|=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() | rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() | rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() | rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() | rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() | rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() | rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() | rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() | rhs.fValue.ToUInt64()); + break; + } + } + + return *this; +} + + +Number& +Number::operator^=(const Number& rhs) +{ + switch (fValue.Type()) { + case B_INT8_TYPE: + { + fValue.SetTo((int8)(fValue.ToInt8() ^ rhs.fValue.ToInt8())); + break; + } + + case B_UINT8_TYPE: + { + fValue.SetTo((uint8)(fValue.ToUInt8() ^ rhs.fValue.ToUInt8())); + break; + } + + case B_INT16_TYPE: + { + fValue.SetTo((int16)(fValue.ToInt16() ^ rhs.fValue.ToInt16())); + break; + } + + case B_UINT16_TYPE: + { + fValue.SetTo((uint16)(fValue.ToUInt16() ^ rhs.fValue.ToUInt16())); + break; + } + + case B_INT32_TYPE: + { + fValue.SetTo(fValue.ToInt32() ^ rhs.fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + fValue.SetTo(fValue.ToUInt32() ^ rhs.fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + fValue.SetTo(fValue.ToInt64() ^ rhs.fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + fValue.SetTo(fValue.ToUInt64() ^ rhs.fValue.ToUInt64()); + break; + } + } + + return *this; +} + + +Number +Number::operator-() const +{ + Number value(*this); + + switch (fValue.Type()) { + case B_INT8_TYPE: + { + value.fValue.SetTo((int8)-fValue.ToInt8()); + break; + } + + case B_UINT8_TYPE: + { + value.fValue.SetTo((uint8)-fValue.ToUInt8()); + break; + } + + case B_INT16_TYPE: + { + value.fValue.SetTo((int16)-fValue.ToInt16()); + break; + } + + case B_UINT16_TYPE: + { + value.fValue.SetTo((uint16)-fValue.ToUInt16()); + break; + } + + case B_INT32_TYPE: + { + value.fValue.SetTo(-fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + value.fValue.SetTo(-fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + value.fValue.SetTo(-fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + value.fValue.SetTo(-fValue.ToUInt64()); + break; + } + + case B_FLOAT_TYPE: + { + value.fValue.SetTo(-fValue.ToFloat()); + break; + } + + case B_DOUBLE_TYPE: + { + value.fValue.SetTo(-fValue.ToDouble()); + break; + } + } + + return value; +} + + +Number +Number::operator~() const +{ + Number value(*this); + + switch (fValue.Type()) { + case B_INT8_TYPE: + { + value.fValue.SetTo((int8)~fValue.ToInt8()); + break; + } + + case B_UINT8_TYPE: + { + value.fValue.SetTo((uint8)~fValue.ToUInt8()); + break; + } + + case B_INT16_TYPE: + { + value.fValue.SetTo((int16)~fValue.ToInt16()); + break; + } + + case B_UINT16_TYPE: + { + value.fValue.SetTo((uint16)~fValue.ToUInt16()); + break; + } + + case B_INT32_TYPE: + { + value.fValue.SetTo(~fValue.ToInt32()); + break; + } + + case B_UINT32_TYPE: + { + value.fValue.SetTo(~fValue.ToUInt32()); + break; + } + + case B_INT64_TYPE: + { + value.fValue.SetTo(~fValue.ToInt64()); + break; + } + + case B_UINT64_TYPE: + { + value.fValue.SetTo(~fValue.ToUInt64()); + break; + } + } + + return value; +} + + +int +Number::operator<(const Number& rhs) const +{ + int result = 0; + switch (fValue.Type()) { + case B_INT8_TYPE: + { + result = fValue.ToInt8() < rhs.fValue.ToInt8(); + break; + } + + case B_UINT8_TYPE: + { + result = fValue.ToUInt8() < rhs.fValue.ToUInt8(); + break; + } + + case B_INT16_TYPE: + { + result = fValue.ToInt16() < rhs.fValue.ToInt16(); + break; + } + + case B_UINT16_TYPE: + { + result = fValue.ToUInt16() < rhs.fValue.ToUInt16(); + break; + } + + case B_INT32_TYPE: + { + result = fValue.ToInt32() < rhs.fValue.ToInt32(); + break; + } + + case B_UINT32_TYPE: + { + result = fValue.ToUInt32() < rhs.fValue.ToUInt32(); + break; + } + + case B_INT64_TYPE: + { + result = fValue.ToInt64() < rhs.fValue.ToInt64(); + break; + } + + case B_UINT64_TYPE: + { + result = fValue.ToUInt64() < rhs.fValue.ToUInt64(); + break; + } + + case B_FLOAT_TYPE: + { + result = fValue.ToFloat() < rhs.fValue.ToFloat(); + break; + } + + case B_DOUBLE_TYPE: + { + result = fValue.ToDouble() < rhs.fValue.ToDouble(); + break; + } + } + + return result; +} + + +int +Number::operator<=(const Number& rhs) const +{ + return (*this < rhs) || (*this == rhs); +} + + +int +Number::operator>(const Number& rhs) const +{ + int result = 0; + switch (fValue.Type()) { + case B_INT8_TYPE: + { + result = fValue.ToInt8() > rhs.fValue.ToInt8(); + break; + } + + case B_UINT8_TYPE: + { + result = fValue.ToUInt8() > rhs.fValue.ToUInt8(); + break; + } + + case B_INT16_TYPE: + { + result = fValue.ToInt16() > rhs.fValue.ToInt16(); + break; + } + + case B_UINT16_TYPE: + { + result = fValue.ToUInt16() > rhs.fValue.ToUInt16(); + break; + } + + case B_INT32_TYPE: + { + result = fValue.ToInt32() > rhs.fValue.ToInt32(); + break; + } + + case B_UINT32_TYPE: + { + result = fValue.ToUInt32() > rhs.fValue.ToUInt32(); + break; + } + + case B_INT64_TYPE: + { + result = fValue.ToInt64() > rhs.fValue.ToInt64(); + break; + } + + case B_UINT64_TYPE: + { + result = fValue.ToUInt64() > rhs.fValue.ToUInt64(); + break; + } + + case B_FLOAT_TYPE: + { + result = fValue.ToFloat() > rhs.fValue.ToFloat(); + break; + } + + case B_DOUBLE_TYPE: + { + result = fValue.ToDouble() > rhs.fValue.ToDouble(); + break; + } + } + + return result; +} + + +int +Number::operator>=(const Number& rhs) const +{ + return (*this > rhs) || (*this == rhs); +} + + +int +Number::operator==(const Number& rhs) const +{ + int result = 0; + switch (fValue.Type()) { + case B_INT8_TYPE: + { + result = fValue.ToInt8() == rhs.fValue.ToInt8(); + break; + } + + case B_UINT8_TYPE: + { + result = fValue.ToUInt8() == rhs.fValue.ToUInt8(); + break; + } + + case B_INT16_TYPE: + { + result = fValue.ToInt16() == rhs.fValue.ToInt16(); + break; + } + + case B_UINT16_TYPE: + { + result = fValue.ToUInt16() == rhs.fValue.ToUInt16(); + break; + } + + case B_INT32_TYPE: + { + result = fValue.ToInt32() == rhs.fValue.ToInt32(); + break; + } + + case B_UINT32_TYPE: + { + result = fValue.ToUInt32() == rhs.fValue.ToUInt32(); + break; + } + + case B_INT64_TYPE: + { + result = fValue.ToInt64() == rhs.fValue.ToInt64(); + break; + } + + case B_UINT64_TYPE: + { + result = fValue.ToUInt64() == rhs.fValue.ToUInt64(); + break; + } + + case B_FLOAT_TYPE: + { + result = fValue.ToFloat() == rhs.fValue.ToFloat(); + break; + } + + case B_DOUBLE_TYPE: + { + result = fValue.ToDouble() == rhs.fValue.ToDouble(); + break; + } + } + + return result; +} + + +int +Number::operator!=(const Number& rhs) const +{ + return !(*this == rhs); +} + + +BVariant +Number::GetValue() const +{ + return fValue; +} + diff --git a/src/apps/debugger/types/Number.h b/src/apps/debugger/types/Number.h new file mode 100644 index 0000000000..fcbd95f4cf --- /dev/null +++ b/src/apps/debugger/types/Number.h @@ -0,0 +1,56 @@ +/* + * Copyright 2014, Rene Gollent, rene@gollent.com. + * Distributed under the terms of the MIT License. + */ +#ifndef NUMBER_H +#define NUMBER_H + +#include + +#include "Variant.h" + + +class BString; + + +class Number { +public: + Number(); + virtual ~Number(); + Number(type_code type, const BString& value); + Number(const BVariant& value); + Number(const Number& other); + + void SetTo(type_code type, const BString& value, + int32 base = 10); + void SetTo(const BVariant& value); + + Number& operator=(const Number& rhs); + Number& operator+=(const Number& rhs); + Number& operator-=(const Number& rhs); + Number& operator/=(const Number& rhs); + Number& operator*=(const Number& rhs); + Number& operator%=(const Number& rhs); + + Number& operator&=(const Number& rhs); + Number& operator|=(const Number& rhs); + Number& operator^=(const Number& rhs); + + Number operator-() const; + Number operator~() const; + + int operator<(const Number& rhs) const; + int operator<=(const Number& rhs) const; + int operator>(const Number& rhs) const; + int operator>=(const Number& rhs) const; + int operator==(const Number& rhs) const; + int operator!=(const Number& rhs) const; + + BVariant GetValue() const; + +private: + BVariant fValue; +}; + + +#endif // NUMBER_H