Re: Object-oriented preprocessor
- Posted by jmduro in August
- 1946 views
Let's take an example with win32lib.
Original code:
global procedure drawRectangle( integer id, integer filled, integer x1, integer y1, integer x2, integer y2 ) -- draw a rectangle atom hdc -- get the device context hdc = getDC( id ) -- create a pen createPen( id, hdc ) -- create the brush createBrush( id, filled, hdc ) -- call Rectangle VOID = w32Func( xRectangle, {hdc, x1, y1, x2, y2 } ) -- release the device context releaseDC( id ) end procedure
You would probably define a generic class TRect then specify properties of its instance rect as follows:
--generic class sequence TRect = class("TRect") TRect = addProperty(TRect,"x1",0) TRect = addProperty(TRect,"y1",0) TRect = addProperty(TRect,"x2",0) TRect = addProperty(TRect,"y2",0) -- specific instance: x2 and y2 differ from default values sequence rect = classes:new(TRect) rect = setProperty(rect, "x2", 10) rect = setProperty(rect, "y2", 10)
Class-like code first version (needs to rewrite win32lib):
global procedure drawRectangle( integer id, integer filled, sequence rect ) integer x1 = getProperty(rect, "x1") integer y1 = getProperty(rect, "y1") integer x2 = getProperty(rect, "x2") integer y2 = getProperty(rect, "y2") -- draw a rectangle atom hdc -- get the device context hdc = getDC( id ) -- create a pen createPen( id, hdc ) -- create the brush createBrush( id, filled, hdc ) -- call Rectangle VOID = w32Func( xRectangle, {hdc, x1, y1, x2, y2 } ) -- release the device context releaseDC( id ) end procedure drawRectangle(win, 1, rect )
Class-like code second version (uses original win32lib):
TRect = addMethod(TRect, "drawRectangle", routine_id("drawRectangle")) void = callMethod(rect, "drawRectangle", {id, filled, getProperty(rect, "x1"), getProperty(rect, "y1"), getProperty(rect, "x2"), getProperty(rect, "y2") })
It remains verbose compared to classical Openeuphoria style.
To get something more easy to program, I think the preprocessor version is more adequate (untested).
include win32lib as win class TRect integer x1, y1, x2, y2 procedure drawRectangle(sequence params) win:drawRectangle(params[1], params[2], self.x1, self.y1, self.x2, self.y2 ) end procedure end class sequence rect = new(TRect) rect.x2 = 10 rect.y2 = 10 rect.drawRectangle({win, filled})
Jean-Marc