From 0c1783b2f35d45f7fac2273137c678418216dbaf Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Sat, 11 Jan 2014 12:10:07 -0800 Subject: [PATCH] plGenericVar => plString - Also fixes a bug in plGenericType::CopyFrom, where doubles would get only half copied (and therefore become corrupt) --- .../FeatureLib/pfPython/plPythonFileMod.cpp | 5 +- .../NucleusLib/pnNetCommon/plGenericVar.cpp | 118 +++--------------- .../NucleusLib/pnNetCommon/plGenericVar.h | 65 +++++----- .../plNetCommon/plNetCommonHelpers.cpp | 6 +- .../plNetCommon/plNetCommonHelpers.h | 2 +- 5 files changed, 54 insertions(+), 142 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp b/Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp index 8d220a22..e3ebe2d6 100644 --- a/Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/plPythonFileMod.cpp @@ -2089,10 +2089,11 @@ bool plPythonFileMod::MsgReceive(plMessage* msg) case plVaultNotifyMsg::kPublicAgeCreated: 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); ptuple = PyTuple_New(1); - PyTuple_SetItem(ptuple, 0, PyString_FromString(ageName)); + PyTuple_SetItem(ptuple, 0, PyString_FromPlString(ageName)); } } break; diff --git a/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.cpp b/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.cpp index 0d71e07c..08a79082 100644 --- a/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.cpp +++ b/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.cpp @@ -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) diff --git a/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.h b/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.h index b3ff533b..14723b89 100644 --- a/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.h +++ b/Sources/Plasma/NucleusLib/pnNetCommon/plGenericVar.h @@ -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; - - void IDeallocString() { if (fType==kString || fType==kAny) {delete [] fS; fS=nil;} } + 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; + 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; } }; diff --git a/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp b/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp index e59d2ccc..64f75e2c 100644 --- a/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp +++ b/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.cpp @@ -198,11 +198,11 @@ void plCreatableListHelper::AddDouble( uint16_t id, double value ) AddItem( id, V, true ); } -const char * plCreatableListHelper::GetString( uint16_t id ) +plString plCreatableListHelper::GetString( uint16_t id ) { plCreatableGenericValue * V = plCreatableGenericValue::ConvertNoRef( GetItem( id ) ); - if ( !V ) return nil; - return (const char *)V->Value(); + if ( !V ) return plString::Null; + return (plString)V->Value(); } int32_t plCreatableListHelper::GetInt( uint16_t id ) diff --git a/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.h b/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.h index 47413798..99b25a42 100644 --- a/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.h +++ b/Sources/Plasma/PubUtilLib/plNetCommon/plNetCommonHelpers.h @@ -125,7 +125,7 @@ public: // helpers for typed arguments void AddString( uint16_t id, const char * 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 ); int32_t GetInt( uint16_t id ); void AddDouble( uint16_t id, double value );