Re: Creating properties in Lua-only classes
"Kitten, Nicholas" <[email protected]>
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Message-ID | <[email protected]> |
Wow, I just realized I was overthinking this way too much. If I have the
following forwarding class using wrap_base:
struct WindowWrapper : Window, luabind::wrap_base
{
WindowWrapper()
: base()
{}
virtual void SetHeight(int h)
{
call<void>("SetHeight", h);
}
static void BaseSetHeight(Window* ptr, int h)
{
return ptr->Window::SetHeight(h);
}
};
Then simply overriding SetHeight( ) in Lua will automatically work, because
win.height = 3
-- calls Window::SetHeight( )
-- which calls Lua's version of SetHeight( ), in this case
SubWindow:SetHeight( )
So as long as I maintain a consistent naming convention (property "height"
corresponds to "GetHeight" and "SetHeight"), then clients are free to
override the getters and setters in Lua.
On Mon, Feb 21, 2011 at 3:33 PM, Kitten, Nicholas
<[email protected]>wrote:
> I'll rephrase my question, with code this time:
>
> I have a C++ class, bound to Lua along these lines:
>
> luabind::class_<Window>( "Window" )
>
> .def( luabind::constructor<>( ) )
>
> .property( "height", &Window::GetHeight, &Window::SetHeight )
>
> .property( "width", &Window::GetWidth, &Window::SetWidth );
>
> /** wrap_base forwarding class omitted **/
>
>
> Of course, in Lua this means I can do the following:
>
> win = Window()
> win.width = 320 -- calls setter
> print( win.width ) -- calls getter
>
> Now, I would like to allow a client to subclass Window from within Lua,
> overriding the setters and/or getters with Lua functions, something like
> this:
>
> class 'SubWindow' (Window)
>
> function SubWindow:SetHeight( newHeight )
>
> Window.SetHeight( self, newHeight )
> -- other stuff
>
> end
>
> function SubWindow:SetWidth( newWidth )
>
> Window.SetWidth( self, newWidth )
> -- other stuff
>
> end
>
>
> function SubWindow:__init( )
>
> Window.__init(self)
> --[[
> magically bind derived SetHeight( ) and SetWidth( ) to __newindex
> metamethod
> --]]
>
> end
>
> win = SubWindow()
> win.width = 320 -- calls SubWindow's setter
> print( win.width ) -- calls Window's getter
>
>
> As you can see, the only way I can think to do this would be by overriding
> the __index and __newindex metamethods on the derived class, but because
> Luabind classes are userdata, this seems impossible at the moment. I know
> the last comment I read from this list on the subject said it might possibly
> be implemented with version .9 (even though firm plans were only for 1.0) -
> has that happened?
>
> If not, is there a distinct mechanism within Luabind to override properties
> in derived classes? I remember seeing a comment in the source saying that
> properties should "always be overridden, not overloaded", but I couldn't
> discern how the overriding would happen in the first place.
>
> Thanks,
> -Nick
>
>
>
> On Tue, Feb 8, 2011 at 12:06 PM, Kitten, Nicholas <[email protected]
> > wrote:
>
>> Does anyone know how I would go about creating a property within a
>> Lua-created class (using Lua functions for getters and setters)? I'm
>> guessing there's support in the metatables for doing it, but I've never seen
>> an implementation.
>>
>> Thanks,
>> -Nick
>>
>
>
------------------------------------------------------------------------------
Index, Search & Analyze Logs and other IT data in Real-Time with Splunk
Collect, index and harness all the fast moving IT data generated by your
applications, servers and devices whether physical, virtual or in the cloud.
Deliver compliance at lower cost and gain new business insights.
Free Software Download: http://p.sf.net/sfu/splunk-dev2dev
_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user