From 34fa992ff5a0cb668fc817897e566fe1c268cfd0 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 30 Sep 2010 16:03:04 +0000 Subject: [PATCH] From Chuck Seberino, "Here is a small optimization in osgDB/Serializer that only uses a single accessor call when retrieving serializable values during writing. This is a sizable win for some of my code since the getter() methods are non-trivial. I also removed some explicit namespace qualifiers to be consistent with the rest of the codebase." --- include/osgDB/Serializer | 67 ++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/include/osgDB/Serializer b/include/osgDB/Serializer index 10ca60f89..026427898 100644 --- a/include/osgDB/Serializer +++ b/include/osgDB/Serializer @@ -229,15 +229,16 @@ public: virtual bool write( OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); + const P value = (object.*_getter)(); if ( os.isBinary() ) { - os << (object.*_getter)(); + os << value; } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { os << PROPERTY((ParentType::_name).c_str()); if ( _useHex ) os << std::hex; - os << (object.*_getter)(); + os << value; if ( _useHex ) os << std::dec; os << std::endl; } @@ -286,13 +287,14 @@ public: virtual bool write( OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); + const CP value = (object.*_getter)(); if ( os.isBinary() ) { - os << (object.*_getter)(); + os << value; } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { - os << PROPERTY((ParentType::_name).c_str()) << (object.*_getter)() << std::endl; + os << PROPERTY((ParentType::_name).c_str()) << value << std::endl; } return true; } @@ -335,13 +337,14 @@ public: virtual bool write( OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); + const osg::Matrix& value = (object.*_getter)(); if ( os.isBinary() ) { - os << (object.*_getter)(); + os << value; } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { - os << PROPERTY((ParentType::_name).c_str()) << (object.*_getter)() << std::endl; + os << PROPERTY((ParentType::_name).c_str()) << value << std::endl; } return true; } @@ -398,13 +401,14 @@ public: virtual bool write( OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); + const P value = (object.*_getter)(); if ( os.isBinary() ) { - os << static_cast((object.*_getter)()); + os << static_cast(value); } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { - os << PROPERTY((ParentType::_name).c_str()) << GLENUM((object.*_getter)()) << std::endl; + os << PROPERTY((ParentType::_name).c_str()) << GLENUM(value) << std::endl; } return true; } @@ -448,14 +452,15 @@ public: virtual bool write( OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); + const std::string& value = (object.*_getter)(); if ( os.isBinary() ) { - os << (object.*_getter)(); + os << value; } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { os << PROPERTY((ParentType::_name).c_str()); - os.writeWrappedString( (object.*_getter)() ); + os.writeWrappedString( value ); os << std::endl; } return true; @@ -510,19 +515,20 @@ public: virtual bool write( OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); - bool hasObject = ((object.*_getter)()!=NULL); + const P* value = (object.*_getter)(); + bool hasObject = (value!=NULL); if ( os.isBinary() ) { os << hasObject; - os.writeObject( (object.*_getter)() ); + os.writeObject( value ); } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { os << PROPERTY((ParentType::_name).c_str()) << hasObject; if ( hasObject ) { os << BEGIN_BRACKET << std::endl; - os.writeObject( (object.*_getter)() ); + os.writeObject( value ); os << END_BRACKET; } os << std::endl; @@ -579,19 +585,20 @@ public: virtual bool write( OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); - bool hasObject = ((object.*_getter)()!=NULL); + const P* value = (object.*_getter)(); + bool hasObject = (value!=NULL); if ( os.isBinary() ) { os << hasObject; - os.writeImage( (object.*_getter)() ); + os.writeImage( value ); } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { os << PROPERTY((ParentType::_name).c_str()) << hasObject; if ( hasObject ) { os << BEGIN_BRACKET << std::endl; - os.writeImage( (object.*_getter)() ); + os.writeImage( value ); os << END_BRACKET; } os << std::endl; @@ -617,13 +624,13 @@ public: { ParentType::_defaultValue = def; } void add( const char* str, P value ) - { _lookup.add(str, static_cast(value)); } + { _lookup.add(str, static_cast(value)); } P getValue( const char* str ) { return static_cast

(_lookup.getValue(str)); } const std::string& getString( P value ) - { return _lookup.getString(static_cast(value)); } + { return _lookup.getString(static_cast(value)); } virtual bool read( InputStream& is, osg::Object& obj ) { @@ -646,13 +653,14 @@ public: virtual bool write( osgDB::OutputStream& os, const osg::Object& obj ) { const C& object = OBJECT_CAST(obj); + const P value = (object.*_getter)(); if ( os.isBinary() ) { - os << (osgDB::IntLookup::Value)(object.*_getter)(); + os << (IntLookup::Value)value; } - else if ( ParentType::_defaultValue!=(object.*_getter)() ) + else if ( ParentType::_defaultValue!=value ) { - os << PROPERTY((ParentType::_name).c_str()) << getString((object.*_getter)()) << std::endl; + os << PROPERTY((ParentType::_name).c_str()) << getString(value) << std::endl; } return true; } @@ -672,7 +680,7 @@ public: typedef TemplateSerializer

ParentType; typedef typename P::value_type ValueType; typedef typename P::const_iterator ConstIterator; - typedef const P& (C::*Getter)() const; + typedef const P& (C::*Getter)() const; typedef void (C::*Setter)( const P& ); ListSerializer( const char* name, Getter gf, Setter sf ) @@ -894,7 +902,6 @@ public: #define END_ENUM_SERIALIZER() \ wrapper->addSerializer(serializer.get()); } - } #endif