"OOP" - TinyObj

Share your advanced PureBasic knowledge/code with the community.
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

"OOP" - TinyObj

Post 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()
Last edited by Mijikai on Sun Feb 23, 2025 11:16 am, edited 1 time in total.
Quin
Addict
Addict
Posts: 1132
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: "OOP" - TinyObj

Post by Quin »

Nice work! Thanks for sharing!
User avatar
gurj
Enthusiast
Enthusiast
Posts: 693
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: "OOP" - TinyObj

Post by gurj »

[ERROR]
Line:48 ;EndProcedure
Invalid memory access.(write error at address 0)
my pb for chinese:
http://ataorj.ys168.com
User avatar
Zapman
Enthusiast
Enthusiast
Posts: 205
Joined: Tue Jan 07, 2020 7:27 pm

Re: "OOP" - TinyObj

Post 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?
SMaag
Enthusiast
Enthusiast
Posts: 324
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: "OOP" - TinyObj

Post 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
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: "OOP" - TinyObj

Post 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
ImageThe happiness is a road...
Not a destination
miso
Enthusiast
Enthusiast
Posts: 466
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: "OOP" - TinyObj

Post by miso »

Nice example. The best kind, short and easy to understand/expand.
(Works here win7 64 bit PB6.20 still beta1)
User avatar
gurj
Enthusiast
Enthusiast
Posts: 693
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: "OOP" - TinyObj

Post by gurj »

x86win7,Formal edition pb6.20
should Procedure change to Gosub ?
my pb for chinese:
http://ataorj.ys168.com
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: "OOP" - TinyObj

Post 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 :)
Post Reply