Browse Source

plGenericVar => plString

- Also fixes a bug in plGenericType::CopyFrom, where doubles would get
  only half copied (and therefore become corrupt)
Michael Hansen 11 years ago
parent
commit
0c1783b2f3
  1. 5
      Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp
  2. 118
      Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.cpp
  3. 65
      Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.h
  4. 6
      Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp
  5. 2
      Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.h

5
Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp

@ -2089,10 +2089,11 @@ bool plPythonFileMod::MsgReceive(plMessage* msg)
case plVaultNotifyMsg::kPublicAgeCreated: case plVaultNotifyMsg::kPublicAgeCreated:
case plVaultNotifyMsg::kPublicAgeRemoved: { case plVaultNotifyMsg::kPublicAgeRemoved: {
if (const char * ageName = vaultNotifyMsg->GetArgs()->GetString(plNetCommon::VaultTaskArgs::kAgeFilename)) { plString ageName = vaultNotifyMsg->GetArgs()->GetString(plNetCommon::VaultTaskArgs::kAgeFilename);
if (!ageName.IsEmpty()) {
Py_DECREF(ptuple); Py_DECREF(ptuple);
ptuple = PyTuple_New(1); ptuple = PyTuple_New(1);
PyTuple_SetItem(ptuple, 0, PyString_FromString(ageName)); PyTuple_SetItem(ptuple, 0, PyString_FromPlString(ageName));
} }
} }
break; break;

118
Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.cpp

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

65
Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.h

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

6
Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp

@ -198,11 +198,11 @@ void plCreatableListHelper::AddDouble( uint16_t id, double value )
AddItem( id, V, true ); AddItem( id, V, true );
} }
const char * plCreatableListHelper::GetString( uint16_t id ) plString plCreatableListHelper::GetString( uint16_t id )
{ {
plCreatableGenericValue * V = plCreatableGenericValue::ConvertNoRef( GetItem( id ) ); plCreatableGenericValue * V = plCreatableGenericValue::ConvertNoRef( GetItem( id ) );
if ( !V ) return nil; if ( !V ) return plString::Null;
return (const char *)V->Value(); return (plString)V->Value();
} }
int32_t plCreatableListHelper::GetInt( uint16_t id ) int32_t plCreatableListHelper::GetInt( uint16_t id )

2
Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.h

@ -125,7 +125,7 @@ public:
// helpers for typed arguments // helpers for typed arguments
void AddString( uint16_t id, const char * value ); void AddString( uint16_t id, const char * value );
void AddString( uint16_t id, std::string & value ); void AddString( uint16_t id, std::string & value );
const char * GetString( uint16_t id ); plString GetString( uint16_t id );
void AddInt( uint16_t id, int32_t value ); void AddInt( uint16_t id, int32_t value );
int32_t GetInt( uint16_t id ); int32_t GetInt( uint16_t id );
void AddDouble( uint16_t id, double value ); void AddDouble( uint16_t id, double value );

Loading…
Cancel
Save