Page 1 of 1

"OOP" - TinyObj

Posted: Sun Feb 16, 2025 6:49 pm
by Mijikai
Not sure where to post this "OOP" thingy :lol:

Code: Select all

EnableExplicit

;TinyObj by Mijikai

;------------------------------------------------------------------ TINY OBJ STRUCTURES

Structure OOP_OBJECT_STRUCT
  Map *method();<- stores the functions of an object
EndStructure

Structure OOP_STRUCT
  Map *object.OOP_OBJECT_STRUCT();<- stores the objects
EndStructure

Global.OOP_STRUCT oop;<- object pool

;------------------------------------------------------------------ TINY OBJ MACROS

Macro objCreate(_object_,_type_,_pointer_)
  _pointer_ = AllocateStructure(_type_):If _pointer_:oop\object(_object_) = _pointer_:EndIf
EndMacro

Macro objMethod(_object_,_name_,_function_)
  oop\object(_object_)\method(_name_) = _function_
EndMacro

Macro objCall(_object_,_method_,_a_ = 0,_b_ = 0,_c_ = 0,_d_ = 0);<- hack prob. unsafe (limited to 4 parameter)!
  CallFunctionFast(oop\object(_object_)\method(_method_),oop\object(_object_),_a_,_b_,_c_,_d_)
EndMacro

Macro objDestroy(_object_)
  FreeStructure(oop\object(_object_)):DeleteMapElement(oop\object(),_object_)
EndMacro

;------------------------------------------------------------------ TINY OBJ EXAMPLE

Structure BALL_STRUCT Extends OOP_OBJECT_STRUCT;<- new ball object
  x.i
  y.i
  name.s
EndStructure

Procedure.i ballAdd(*obj.BALL_STRUCT,x.i,y.i)
  With *obj
    \x + X
    \y + y
  EndWith
EndProcedure

Procedure.i ballSub(*obj.BALL_STRUCT,x.i,y.i)
  With *obj
    \x - x
    \y - y
  EndWith
EndProcedure

Procedure.i ballRename(*obj.BALL_STRUCT,*name)
  With *obj
    \name = PeekS(*name)
  EndWith
EndProcedure

Procedure.i Main()
  Protected.BALL_STRUCT *obj
  With *obj
    objCreate("ball",BALL_STRUCT,*obj);<- create the object
    objMethod("ball","add",@ballAdd());<- add methods for this object
    objMethod("ball","sub",@ballSub())
    objMethod("ball","rename",@ballRename())
    objCall("ball","add",10,20);<- call a method
    Debug Str(\x) + " x " + Str(\y)
    objCall("ball","sub",5,5)
    Debug Str(\x) + " x " + Str(\y)
    objCall("ball","rename",@"Hello World!")
    Debug \name
    objDestroy("ball")
    ProcedureReturn #Null
  EndWith
EndProcedure

End Main()

Re: "OOP" - TinyObj

Posted: Sun Feb 16, 2025 7:03 pm
by Quin
Nice work! Thanks for sharing!

Re: "OOP" - TinyObj

Posted: Mon Feb 17, 2025 2:31 am
by gurj
[ERROR]
Line:48 ;EndProcedure
Invalid memory access.(write error at address 0)

Re: "OOP" - TinyObj

Posted: Sat Feb 22, 2025 6:37 pm
by Zapman
gurj wrote: Mon Feb 17, 2025 2:31 am [ERROR]
Line:48 ;EndProcedure
Invalid memory access.(write error at address 0)
Same error here with PB 6.12 LTS (x86) - Windows 11.
No error with PB 6.20 Beta 4 (x64), but the only thing I get is:

Code: Select all

10 x 20
5 x 15
Hello World!
Could you post a demo for that?

Re: "OOP" - TinyObj

Posted: Sat Feb 22, 2025 7:53 pm
by SMaag
working here! PB6.20!

But a general problem of the code is:

A type mismatch error in the Methode or Object Name will end up in a crash.
Very hard to find if the code will be longer.
You can't use IDE's intellisense for listing the Methodes during programming.
For a good software architecture this in not the way to handle objects!

Better use Module or Interface technology for such stuff!


UPDATE:

I saw in an other Post, you understand all that Object concepts . So my post is useless.
For a proof of concept it's a smart idea to use this 2 Map-Structures

Re: "OOP" - TinyObj

Posted: Sat Feb 22, 2025 8:03 pm
by Kwai chang caine
gurj wrote: Mon Feb 17, 2025 2:31 am [ERROR]
Line:48 ;EndProcedure
Invalid memory access.(write error at address 0)
The same with v6.20B1

Re: "OOP" - TinyObj

Posted: Sun Feb 23, 2025 6:47 am
by miso
Nice example. The best kind, short and easy to understand/expand.
(Works here win7 64 bit PB6.20 still beta1)

Re: "OOP" - TinyObj

Posted: Sun Feb 23, 2025 8:16 am
by gurj
x86win7,Formal edition pb6.20
should Procedure change to Gosub ?

Re: "OOP" - TinyObj

Posted: Sun Feb 23, 2025 11:15 am
by Mijikai
objCall() is limited to 4 parameters.
It would be safer to have a dedicated calls to match the parameters of the method (keep the stack happy).
But then its just not as cute anymore (hence it is the hack it is ) ^.^
I personally would advice against using this in serious projects (if you do so make objCall() safe).
Will add a note to the original post to give a hint :)