Overridden properties are overwritten by base classes

Chris Byrne <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <A20AC73ECD8EA64480DBC02889EB5F7622B60859@exchange.elementlabs.com>
We just encountered a problem where the base classes property getter/setter is called instead of the overridden bindings:

// bind our classes
struct Base {
	std::string GetBase( ) const { return "Base::Get"; }
	void SetBase( const std::string& ) { throw "Base::Set"; }
};
struct Derived : public Base {
	std::string GetDerived( ) const { return "Derived::Get"; }
	void SetDerived( const std::string& ) { throw "Derived::Set"; }
};

// expose them with luabind
{
	using namespace luabind;
	
	module( lua_state ) [
		class_< Base >( "_Base" )
			.property( "value", &Base::GetBase, &Base::SetBase )
		,
		class_< Derived, Base >( "_Derived" )
			.property( "value", &Derived::GetDerived, &Derived::SetDerived )
	];
}

// bind them as global lua objects
luabind::object obj_globals = luabind::globals( lua_state );

obj_globals[ "base" ] = Base( );
obj_globals[ "derived" ] = Derived( );



-- in lua:

> print( base.value )
Base::Get
> print( derived.value )
Base::Get
-- should be Derived::Get

> base.value = ""
Base::Set
> derived.value = ""
Base::Set
-- should be Derived::Set

========================================================

Problem is due to class_rep::add_base_class being called _after_ the derived properties have been added to m_getters and m_setters. Which is fine except that add_base_class doesn't check if the properties have already been set...

Small diff attached showing how to fix this problem.


Thanks,
 Chris Byrne
 Software Engineer
 Element Labs, Inc.

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
changeset_r3418.diff (application/octet-stream, 928 B)
Index: class_rep.cpp
===================================================================
--- class_rep.cpp	(revision 3417)
+++ class_rep.cpp	(revision 3418)
@@ -937,6 +937,8 @@
 	for (property_map::const_iterator i = bcrep->m_getters.begin(); 
 			i != bcrep->m_getters.end(); ++i)
 	{
+		if ( m_getters.end() != m_getters.find( i->first ) ) continue;
+		
 		callback& m = m_getters[i->first];
 		m.pointer_offset = i->second.pointer_offset + binfo.pointer_offset;
 		m.func = i->second.func;
@@ -951,10 +953,12 @@
 	for (property_map::const_iterator i = bcrep->m_setters.begin(); 
 			i != bcrep->m_setters.end(); ++i)
 	{
+		if ( m_setters.end() != m_setters.find( i->first ) ) continue;
+		
 		callback& m = m_setters[i->first];
 		m.pointer_offset = i->second.pointer_offset + binfo.pointer_offset;
 		m.func = i->second.func;
-
+		
 #ifndef LUABIND_NO_ERROR_CHECKING
 		m.match = i->second.match;
 		m.sig = i->second.sig;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.