Implenting OOP by macro in MASM
Thomas Bleeker and NaN have done a great work on this. I have written a small game using their OOP model. I found it useful in expressing idea and writting more clear code. However, some improvment was need:
1.there is a lot work to do if you want to use it. For example, the functions' proto, and the functions' long name. The obj creator do help you do this annoying things, but there's some problem in the creator, also.
2.Uncessary overhead was introduced, all method was virtual, so a method call have to be indirect via pointer.
3.Some function should be strengthen, for example, SUPER can only super one level in inheritance.
I have asked Thomas about this. As he said, the OOP model was kept relativly simple in the beginning, and the model's design imitates the Java's way. There's a long time since the last update of the model. I want to continue the two's work and NEED YOUR OPINION AND HELP.
I have serval idea and have tried a little. I have confidence to give a simple and powerful solution to this. Although the model may be more like a game to a significant work, I've decided to do this. I am a student in Beijing China, I am not a experienced programmer in asm, but more familiar to C++ and pascal. I have translated the win32asm tutorial written by Thomas Bleeker into chinese, and analysed the old OOP model in detail one month ago, and have known the C++ and Object Pascal's way, too. Recently, I tried the Masm's macro power and found it powerful enough to do some work that a complier can do.
I have searched the net for a description about the old OOP model, but a little result was found. So, I do this myself, and learned a lot from the design. Thank you Thomas Bleeker, Thank you NaN, Your work is so good that help me grasp the essence behind OOP. I want to write a little about the old OOP model first:
A first step into macro OOP model
As well known, OOP has three key concept. That is encapsulate, inheritance, polymorphic. To achieve these targets, there are serval model, macro OOP model follows the one table model which is the simplest.
OOP is just a idea, ALL language can use it. You can use OOP manually in asm, put data in a struct and put function ptr in the struct too, or using name mangling to write non-virtual function. But it is hard and seems unnecessary comparing the work done and benefit gained. Macro is the tool we can use to change this, You can use Macro to do this for you. The power of macro is more and more knowned, C++'s template is also a macro actually. MASM's macro can accept VARARG, can do string parsing, can be nested. Too powerful to be true.
A class is a struct, and a virtual function is a function which stores its pointer in the struct, a non-virtual function is a function whose name is manglinged to "be" a class's method, inheritance just contain the base's struct in the derive's struct, override just move new ptr to the struct, abstract base class just set the function ptr to a special function which raise a error when you try to call it. Essence is simple and straight forward, but you have to do some dirty work in the macro and do a lot work under the hood.
Thomas Bleeker and NaN used following macro to do this:
The five was the most important, others are relatively simple.
CLASS Shape, Shap
CMETHOD destructor
CMETHOD setColor
Color dd ?
Shape ENDS
define a struct and the init val was set in the .data, a label was used to label the beginning pos, a textequ was used to record the init size. all the init work was done by BEGIN_INIT, END_INIT. SET_CLASS was called in the ctor, and move the init val to the struct, and do inheritance if INHERITS presents, something confusing in this macro, due to there is only one dtor and the pos was the first entry in the struct. OVERRIDE have to be done manually which is inconvenient. CMETHOD assume the Shape_getAreaPto ... was defined prev.
A fully analysz will be written in the next post, stop here. I will demonstrate my opinion in sample code:
METHOD getArea
METHOD setColor
VAR DWORD, Color, 0
no begin_init or end_init or proto was required, it's possible by textequ. The work macro done is more like a complier, symbolic table was created.
CLASS Circle : Shape
METHOD getArea
do the inheritance, the same name getArea cause override.
instance a object can be done only via new before, I want to
some_funct PROC
local i,j
VAR one_circle:Circle
some_funct ENDP
can be possbile via option prologue and option epilogue and textequs.
OPATTR was used to test if the function was a virtual or non-virtual, I have tested it and it works well.
A fully doable demonstration was in progress.
Posted on 2003-04-24 22:16:05 by taowen2002

It is better to post question of a general nature in the main forum as a lot more people will get to see it and be able to answer you if they understand the question.

This forum is more for MASM32 specific questions and it does not get as many people looking at it so I am moving this thread to the main forum so it will be seen by more people.

Posted on 2003-04-24 22:28:43 by hutch--
Oh, sorry, didn't notice.
I will take care next time
Posted on 2003-04-25 00:01:28 by taowen2002