1
0
mirror of https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git synced 2025-07-18 03:09:13 +00:00

plGenericVar => plString

- Also fixes a bug in plGenericType::CopyFrom, where doubles would get
  only half copied (and therefore become corrupt)
This commit is contained in:
2014-01-11 12:10:07 -08:00
parent a783642515
commit 0c1783b2f3
5 changed files with 53 additions and 141 deletions

View File

@ -41,7 +41,6 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
*==LICENSE==*/
#include "hsStream.h"
#include "plGenericVar.h"
#include "hsMemory.h"
//////////////////////////////////////////////////////
// plGenericType
@ -55,116 +54,93 @@ void plGenericType::Reset()
void plGenericType::CopyFrom(const plGenericType& c)
{
IDeallocString();
fType = c.fType;
if (fType==kString || fType==kAny)
{
fS=hsStrcpy(c.fS);
fS = c.fS;
}
else
{
HSMemory::BlockMove((void*)&c.fI, (void*)&fI, 4);
memmove(&fD, &c.fD, sizeof(double));
}
}
//// Conversion Functions ////////////////////////////////////////////////////
const int32_t & plGenericType::IToInt( void ) const
int32_t plGenericType::IToInt( void ) const
{
hsAssert( fType == kInt || fType == kAny, "Trying to use a non-int parameter as an int!" );
static int32_t i;
if( fType == kAny )
{
hsAssert( fS != nil, "Weird parameter during conversion" );
i = atoi( fS );
return i;
return fS.ToInt();
}
return fI;
}
const uint32_t & plGenericType::IToUInt( void ) const
uint32_t plGenericType::IToUInt( void ) const
{
hsAssert( fType == kUInt || fType == kAny, "Trying to use a non-int parameter as an int!" );
static uint32_t i;
if( fType == kAny )
{
hsAssert( fS != nil, "Weird parameter during conversion" );
i = atoi( fS );
return i;
return fS.ToUInt();
}
return fU;
}
const double & plGenericType::IToDouble( void ) const
double plGenericType::IToDouble( void ) const
{
hsAssert( fType == kDouble || fType == kAny, "Trying to use a non-float parameter as a Double!" );
static double d;
if( fType == kAny )
{
hsAssert( fS != nil, "Weird parameter during conversion" );
d = atof( fS );
return d;
return fS.ToDouble();
}
return fD;
}
const float & plGenericType::IToFloat( void ) const
float plGenericType::IToFloat( void ) const
{
hsAssert( fType == kFloat || fType == kAny, "Trying to use a non-float parameter as a float!" );
static float f;
if( fType == kAny )
{
hsAssert( fS != nil, "Weird parameter during conversion" );
f = (float)atof( fS );
return f;
return fS.ToFloat();
}
return fF;
}
const bool & plGenericType::IToBool( void ) const
bool plGenericType::IToBool( void ) const
{
hsAssert( fType == kBool || fType == kAny, "Trying to use a non-bool parameter as a bool!" );
static bool b;
if( fType == kAny )
{
hsAssert( fS != nil, "Weird parameter during conversion" );
if( atoi( fS ) > 0 || stricmp( fS, "true" ) == 0 )
b = true;
else
b = false;
return b;
return (fS.ToInt() > 0 || fS.CompareI("true") == 0);
}
return fB;
}
const plGenericType::CharPtr & plGenericType::IToString( void ) const
plString plGenericType::IToString( void ) const
{
hsAssert( fType == kString || fType == kAny, "Trying to use a non-string parameter as a string!" );
return fS;
}
const char & plGenericType::IToChar( void ) const
char plGenericType::IToChar( void ) const
{
hsAssert( fType == kChar || fType == kAny, "Trying to use a non-char parameter as a char!" );
static char c;
if( fType == kAny )
{
hsAssert( fS != nil, "Weird parameter during conversion" );
c = fS[ 0 ];
return c;
return fS.CharAt(0);
}
return fC;
@ -172,14 +148,13 @@ const char & plGenericType::IToChar( void ) const
void plGenericType::Read(hsStream* s)
{
IDeallocString();
s->ReadLE(&fType);
switch ( fType )
{
case kString:
case kAny:
fS=s->ReadSafeString();
fS=s->ReadSafeString_TEMP();
break;
case kBool:
{int8_t b;
@ -244,8 +219,7 @@ void plGenericType::Write(hsStream* s)
///////////////////////////////////////////////////
void plGenericVar::Read(hsStream* s)
{
delete [] fName;
fName = s->ReadSafeString();
fName = s->ReadSafeString_TEMP();
fValue.Read(s);
}
@ -258,64 +232,6 @@ void plGenericVar::Write(hsStream* s)
//////////////////////////////////
void plGenericType::SetVar(Types t, unsigned int size, void* val)
{
fType = t;
switch (t)
{
case kInt :
{
hsAssert(size <= sizeof(fI), "plGenericType::SetVar size too large for int");
memcpy(&fI, val, size);
break;
}
case kUInt :
{
hsAssert(size <= sizeof(fU), "plGenericType::SetVar size too large for unsigned int");
memcpy(&fU, val, size);
break;
}
case kFloat :
{
hsAssert(size <= sizeof(fF), "plGenericType::SetVar size too large for float");
memcpy(&fF, val, size);
break;
}
case kDouble :
{
hsAssert(size <= sizeof(fD), "plGenericType::SetVar size too large for double");
memcpy(&fD, val, size);
break;
}
case kBool :
{
hsAssert(size <= sizeof(fB), "plGenericType::SetVar size too large for bool");
memcpy(&fB, val, size);
break;
}
case kChar :
{
hsAssert(size <= sizeof(fC), "plGenericType::SetVar size too large for char");
memcpy(&fC, val, size);
break;
}
case kString :
{
delete [] fS;
fS = new char[size+1];
memcpy(fS,val,size);
fS[size] = 0;
break;
}
case kNone :
break;
default:
hsAssert(false,"plGenericType::SetVar unknown type");
}
}
plString plGenericType::GetAsString() const
{
switch (fType)

View File

@ -53,21 +53,18 @@ class hsStream;
//
class plGenericType
{
public:
typedef char* CharPtr;
protected:
union
{
int32_t fI;
uint32_t fU;
int32_t fI;
uint32_t fU;
float fF;
double fD;
bool fB;
CharPtr fS;
char fC;
};
plString fS;
public:
enum Types
@ -86,20 +83,19 @@ public:
protected:
uint8_t fType;
const int32_t &IToInt( void ) const;
const uint32_t &IToUInt( void ) const;
const float &IToFloat( void ) const;
const double &IToDouble( void ) const;
const bool &IToBool( void ) const;
const CharPtr &IToString( void ) const;
const char &IToChar( void ) const;
int32_t IToInt( void ) const;
uint32_t IToUInt( void ) const;
float IToFloat( void ) const;
double IToDouble( void ) const;
bool IToBool( void ) const;
plString IToString( void ) const;
char IToChar( void ) const;
void IDeallocString() { if (fType==kString || fType==kAny) {delete [] fS; fS=nil;} }
public:
plGenericType() : fType(kNone) { Reset(); }
plGenericType(const plGenericType& c) { CopyFrom(c); }
virtual ~plGenericType() { IDeallocString(); }
virtual ~plGenericType() { }
plGenericType& operator=(const plGenericType& c) { CopyFrom(c); return *this; }
@ -110,34 +106,33 @@ public:
operator double() const { return IToDouble(); }
operator float() const { return IToFloat(); }
operator bool() const { return IToBool(); }
operator const CharPtr() const { return IToString(); }
operator plString() const { return IToString(); }
operator char() const { return IToChar(); }
void SetType(Types t) { fType=t; }
uint8_t GetType( void ) const { return fType; }
uint8_t GetType( void ) const { return fType; }
plString GetAsString() const;
// implicit set
void Set( int32_t i ) { fI = i; fType = kInt; }
void Set( uint32_t i ) { fU = i; fType = kUInt; }
void Set( int32_t i ) { fI = i; fType = kInt; }
void Set( uint32_t i ) { fU = i; fType = kUInt; }
void Set( float f ) { fF = f; fType = kFloat; }
void Set( double d ) { fD = d; fType = kDouble; }
void Set( bool b ) { fB = b; fType = kBool; }
void Set( CharPtr s ) { IDeallocString(); fS = hsStrcpy(s); fType = kString; }
void Set( const plString& s ) { fS = s; fType = kString; }
void Set( char c ) { fC = c; fType = kChar; }
// explicit set
void SetInt( int32_t i ) { fI = i; fType = kInt; }
void SetUInt( uint32_t i ) { fU = i; fType = kUInt; }
void SetInt( int32_t i ) { fI = i; fType = kInt; }
void SetUInt( uint32_t i ) { fU = i; fType = kUInt; }
void SetFloat( float f ) { fF = f; fType = kFloat; }
void SetDouble( double d ) { fD = d; fType = kDouble; }
void SetBool( bool b ) { fB = b; fType = kBool; }
void SetString( CharPtr s ) { IDeallocString(); fS = hsStrcpy(s); fType = kString; }
void SetString( const plString& s ) { fS = s; fType = kString; }
void SetChar( char c ) { fC = c; fType = kChar; }
void SetAny( CharPtr s ) { IDeallocString(); fS = hsStrcpy(s); fType = kAny; }
void SetAny( const plString& s ) { fS = s; fType = kAny; }
void SetNone( void ) { fType = kNone; }
void SetVar(Types t, unsigned int size, void* val);
virtual void Read(hsStream* s);
virtual void Write(hsStream* s);
@ -150,17 +145,17 @@ class plGenericVar
{
protected:
plGenericType fValue;
char* fName;
plString fName;
public:
plGenericVar(const plGenericVar &c) : fName(nil) { CopyFrom(c); }
plGenericVar(const char* name=nil) : fName(nil) { SetName(name); }
virtual ~plGenericVar() { delete [] fName; }
plGenericVar(const plGenericVar &c) { CopyFrom(c); }
plGenericVar(const plString& name="") : fName(name) { }
virtual ~plGenericVar() { }
virtual void Reset() { Value().Reset(); } // reset runtime state, not inherent state
plGenericVar& operator=(const plGenericVar &c) { CopyFrom(c); return *this; }
void CopyFrom(const plGenericVar &c) { delete [] fName; fName=hsStrcpy(c.GetName()); fValue=c.Value(); }
const char* GetName() const { return fName; }
void SetName(const char* n) { delete [] fName; fName = hsStrcpy(n); }
void CopyFrom(const plGenericVar &c) { fName=c.GetName(); fValue=c.Value(); }
plString GetName() const { return fName; }
void SetName(const plString& n) { fName = n; }
plGenericType& Value() { return fValue; }
const plGenericType& Value() const { return fValue; }
@ -186,7 +181,7 @@ public:
operator float() const { return (float)fValue; }
operator double() const { return (double)fValue; }
operator bool() const { return (bool)fValue; }
operator const char *() const { return (const char *)fValue; }
operator plString() const { return (plString)fValue; }
operator char() const { return (char)fValue; }
};