mirror of
https://github.com/ZDoom/qzdoom.git
synced 2024-11-12 07:34:50 +00:00
Add generic integer setters and getters for PInt and PFloat types
This commit is contained in:
parent
6198c000af
commit
e50c00c856
2 changed files with 138 additions and 0 deletions
126
src/dobjtype.cpp
126
src/dobjtype.cpp
|
@ -206,6 +206,29 @@ PType::~PType()
|
|||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// PType :: SetValue
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void PType::SetValue(void *addr, int val)
|
||||
{
|
||||
assert(0 && "Cannot set value for this type");
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// PType :: GetValue
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int PType::GetValueInt(void *addr)
|
||||
{
|
||||
assert(0 && "Cannot get value for this type");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// PType :: IsMatch
|
||||
|
@ -360,6 +383,69 @@ PInt::PInt(unsigned int size, bool unsign)
|
|||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// PInt :: SetValue
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void PInt::SetValue(void *addr, int val)
|
||||
{
|
||||
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
|
||||
if (Size == 4)
|
||||
{
|
||||
*(int *)addr = val;
|
||||
}
|
||||
else if (Size == 1)
|
||||
{
|
||||
*(BYTE *)addr = val;
|
||||
}
|
||||
else if (Size == 2)
|
||||
{
|
||||
*(WORD *)addr = val;
|
||||
}
|
||||
else if (Size == 8)
|
||||
{
|
||||
*(QWORD *)addr = val;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(0 && "Unhandled integer size");
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// PInt :: GetValueInt
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int PInt::GetValueInt(void *addr)
|
||||
{
|
||||
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
|
||||
if (Size == 4)
|
||||
{
|
||||
return *(int *)addr;
|
||||
}
|
||||
else if (Size == 1)
|
||||
{
|
||||
return Unsigned ? *(BYTE *)addr : *(SBYTE *)addr;
|
||||
}
|
||||
else if (Size == 2)
|
||||
{
|
||||
return Unsigned ? *(WORD *)addr : *(SWORD *)addr;
|
||||
}
|
||||
else if (Size == 8)
|
||||
{ // truncated output
|
||||
return (int)*(QWORD *)addr;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(0 && "Unhandled integer size");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* PFloat *****************************************************************/
|
||||
|
||||
IMPLEMENT_CLASS(PFloat)
|
||||
|
@ -386,6 +472,46 @@ PFloat::PFloat(unsigned int size)
|
|||
{
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// PFloat :: SetValue
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
void PFloat::SetValue(void *addr, int val)
|
||||
{
|
||||
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
|
||||
if (Size == 4)
|
||||
{
|
||||
*(float *)addr = (float)val;
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(Size == 8);
|
||||
*(double *)addr = val;
|
||||
}
|
||||
}
|
||||
|
||||
//==========================================================================
|
||||
//
|
||||
// PFloat :: GetValueInt
|
||||
//
|
||||
//==========================================================================
|
||||
|
||||
int PFloat::GetValueInt(void *addr)
|
||||
{
|
||||
assert(((intptr_t)addr & (Align - 1)) == 0 && "unaligned address");
|
||||
if (Size == 4)
|
||||
{
|
||||
return xs_ToInt(*(float *)addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(Size == 8);
|
||||
return xs_ToInt(*(double *)addr);
|
||||
}
|
||||
}
|
||||
|
||||
/* PString ****************************************************************/
|
||||
|
||||
IMPLEMENT_CLASS(PString)
|
||||
|
|
|
@ -167,6 +167,12 @@ public:
|
|||
PType(unsigned int size, unsigned int align);
|
||||
virtual ~PType();
|
||||
|
||||
// Sets the value of a variable of this type at (addr)
|
||||
virtual void SetValue(void *addr, int val);
|
||||
|
||||
// Gets the value of a variable of this type at (addr)
|
||||
virtual int GetValueInt(void *addr);
|
||||
|
||||
// Returns true if this type matches the two identifiers. Referring to the
|
||||
// above table, any type is identified by at most two characteristics. Each
|
||||
// type that implements this function will cast these to the appropriate type.
|
||||
|
@ -219,6 +225,9 @@ class PInt : public PBasicType
|
|||
public:
|
||||
PInt(unsigned int size, bool unsign);
|
||||
|
||||
virtual void SetValue(void *addr, int val);
|
||||
virtual int GetValueInt(void *addr);
|
||||
|
||||
bool Unsigned;
|
||||
protected:
|
||||
PInt();
|
||||
|
@ -229,6 +238,9 @@ class PFloat : public PBasicType
|
|||
DECLARE_CLASS(PFloat, PBasicType);
|
||||
public:
|
||||
PFloat(unsigned int size);
|
||||
|
||||
virtual void SetValue(void *addr, int val);
|
||||
virtual int GetValueInt(void *addr);
|
||||
protected:
|
||||
PFloat();
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue