From 8139222e27a997466c1a79e104690ca573bed6f2 Mon Sep 17 00:00:00 2001 From: kervala Date: Tue, 22 Nov 2016 12:14:32 +0100 Subject: [PATCH] Fixed: Strict aliasing warnings --- code/nel/include/nel/gui/interface_property.h | 4 +-- code/nel/include/nel/misc/cdb_leaf.h | 22 +++++++-------- code/nel/src/gui/interface_property.cpp | 27 ++++++++++++++++--- code/ryzom/server/src/pd_lib/pd_messages.h | 6 +++-- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/code/nel/include/nel/gui/interface_property.h b/code/nel/include/nel/gui/interface_property.h index 4ed6e701e..b506ec7a9 100644 --- a/code/nel/include/nel/gui/interface_property.h +++ b/code/nel/include/nel/gui/interface_property.h @@ -72,8 +72,8 @@ namespace NLGUI bool link( NLMISC::CCDBNodeBranch *dbNode, const std::string &leafId, NLMISC::CCDBNodeLeaf *defaultLeaf = NULL ); /// float operations - void setDouble (double value) {setSInt64((sint64&) value);} - double getDouble () const {sint64 i = getSInt64(); return (double &) i; } + void setDouble(double value); + double getDouble() const; void readDouble (const char* value, const std::string& id); /// sint32 operations diff --git a/code/nel/include/nel/misc/cdb_leaf.h b/code/nel/include/nel/misc/cdb_leaf.h index e3df6f8b3..c80ec903a 100644 --- a/code/nel/include/nel/misc/cdb_leaf.h +++ b/code/nel/include/nel/misc/cdb_leaf.h @@ -40,20 +40,20 @@ public: /// Return the value of the property. - inline sint64 getValue64() { return _Property; } + inline sint64 getValue64() const { return _Property; } /// Set the value of the property (set '_Changed' flag with 'true'). void setValue64 (sint64 prop); - inline sint32 getValue32() { return *((sint32*)&_Property); } + inline sint32 getValue32() const { return (sint32)(_Property & 0xffffffff); } void setValue32 (sint32 prop); - inline sint16 getValue16() { return *((sint16*)&_Property); } + inline sint16 getValue16() const { return (sint16)(_Property & 0xffff); } void setValue16 (sint16 prop); - inline sint8 getValue8() { return *((sint8*)&_Property); } + inline sint8 getValue8() const { return (sint8)(_Property & 0xff); } void setValue8 (sint8 prop); - inline bool getValueBool() { return (_Property!=(sint64)0 ); } + inline bool getValueBool() const { return (_Property!=(sint64)0 ); } void setValueBool (bool prop); - inline CRGBA getValueRGBA() + inline CRGBA getValueRGBA() const { CRGBA col; col.R = (uint8)(_Property&0xff); @@ -65,11 +65,11 @@ public: void setValueRGBA (const CRGBA &color); /// Return the value of the property before the database change - inline sint64 getOldValue64() { return _oldProperty; } - inline sint32 getOldValue32() { return *((sint32*)&_oldProperty); } - inline sint16 getOldValue16() { return *((sint16*)&_oldProperty); } - inline sint8 getOldValue8() { return *((sint8*)&_oldProperty); } - inline bool getOldValueBool() { return (_oldProperty!=(sint64)0 ); } + inline sint64 getOldValue64() const { return _oldProperty; } + inline sint32 getOldValue32() const { return (sint32)(_oldProperty & 0xffffffff); } + inline sint16 getOldValue16() const { return (sint16)(_oldProperty & 0xffff); } + inline sint8 getOldValue8() const { return (sint8)(_oldProperty & 0xff); } + inline bool getOldValueBool() const { return (_oldProperty!=(sint64)0 ); } /// Return the type of the property. diff --git a/code/nel/src/gui/interface_property.cpp b/code/nel/src/gui/interface_property.cpp index 694bd6c36..8715b0f2f 100644 --- a/code/nel/src/gui/interface_property.cpp +++ b/code/nel/src/gui/interface_property.cpp @@ -26,6 +26,12 @@ using namespace std; namespace NLGUI { + // helper to convert double <> sint64 + union C64BitsParts + { + sint64 i64; + double d; + }; bool CInterfaceProperty::link( CCDBNodeLeaf *dbNode ) { @@ -66,6 +72,20 @@ namespace NLGUI } + void CInterfaceProperty::setDouble(double value) + { + C64BitsParts parts; + parts.d = value; + setSInt64(parts.i64); + } + + double CInterfaceProperty::getDouble() const + { + C64BitsParts parts; + parts.i64 = getSInt64(); + return parts.d; + } + // ***************** // sint64 operations // ***************** @@ -104,10 +124,9 @@ namespace NLGUI if ( isdigit(*ptr) || *ptr=='-') { _VolatileValue = NLGUI::CDBManager::getInstance()->getDbProp(id); - double buf; - fromString(ptr, buf); - sint64 i = *(sint64*)&buf; - _VolatileValue->setValue64( i ); + C64BitsParts buf; + fromString(ptr, buf.d); + _VolatileValue->setValue64(buf.i64); } else { diff --git a/code/ryzom/server/src/pd_lib/pd_messages.h b/code/ryzom/server/src/pd_lib/pd_messages.h index 25ea12c4b..41e2c2db2 100644 --- a/code/ryzom/server/src/pd_lib/pd_messages.h +++ b/code/ryzom/server/src/pd_lib/pd_messages.h @@ -578,8 +578,8 @@ public: sint16 asSint16() const { return (sint16)_Value1[0]; } sint32 asSint32() const { return (sint32)_Value2[0]; } sint64 asSint64() const { return (sint64)_Value3[0]; } - float asFloat() const { return *(float*)(&_Value2[0]); } - double asDouble() const { return *(double*)(&_Value3[0]); } + float asFloat() const { return (float)_ValueFloat[0]; } + double asDouble() const { return (double)_ValueDouble[0]; } const NLMISC::CSheetId& asSheetId() const { return *(NLMISC::CSheetId*)(&_Value2[0]); } const NLMISC::CEntityId& asEntityId() const { return *(NLMISC::CEntityId*)(&_Value3[0]); } @@ -668,6 +668,8 @@ private: uint16 _Value1[4]; uint32 _Value2[2]; uint64 _Value3[1]; + float _ValueFloat[2]; + double _ValueDouble[1]; }; bool _ObjectIdPresent;