SimpleOOP method addresses

Everything else that doesn't fall into one of the other PB categories.
itto
User
User
Posts: 12
Joined: Fri Jul 27, 2012 8:12 pm
Location: Italy
Contact:

SimpleOOP method addresses

Post by itto »

Does anyone know how to get the address of a method in a SimpleOOP class?

I make heavy use of callback functions, using @ to pass around the address of the function I want to call later, but the same doesn't seem to work with SimpleOOP class methods. Any idea how to achieve the same thing with these methods? I would like to be able to register the address of a method to call it later as a callback.

Thanks in advance :)
itto
User
User
Posts: 12
Joined: Fri Jul 27, 2012 8:12 pm
Location: Italy
Contact:

Re: SimpleOOP method addresses

Post by itto »

Ok After studying the SimpleOOP generated code I managed to find a solution, you just need to pass the element whose method you are calling as the first parameter of CallFunctionFast! This really puts a hell of a power into the language! :)
chris319
Enthusiast
Enthusiast
Posts: 782
Joined: Mon Oct 24, 2005 1:05 pm

Re: SimpleOOP method addresses

Post by chris319 »

Example, please?
Alex777
User
User
Posts: 47
Joined: Sun Nov 16, 2008 12:47 am
Location: Cayman Is.
Contact:

Re: SimpleOOP method addresses

Post by Alex777 »

Say you have an object called 'objName'. You wish to call indirectly the 8th method listed in its virtual table. Then this will do it:

Code: Select all

address.i = PeekI(PeekI(objName) + SizeOf(Integer) * 7)                                    

CallFunctionFast(address, objName)
itto
User
User
Posts: 12
Joined: Fri Jul 27, 2012 8:12 pm
Location: Italy
Contact:

Re: SimpleOOP method addresses

Post by itto »

chris319 wrote:Example, please?
For example

Code: Select all

Class foo
	
	Public Method bar()
		
		Debug "hello!"
		
	EndMethod
	
EndClass

*foo.foo = NewObject.foo

CallFunctionFast( *foo\bar, *foo )
the first argument of the method called by CallFunctionFast (*foo), is mandatory, because it will become the *This pointer used by the method. This is needed for the SimpleOOP preprocessor to work correctly, and for the PureBasic compiler to allocate the right amount of memory for the procedures, otherwise memory errors will occur in the program. If the method contains parameters, these can be added after the *foo one.

While this example doesn't really show anything useful, using pointers to reference methods inside objects enable event-driven programming to be used together with SimpleOOP objects and methods. This is really the top of both worlds. I'm easily building a GUI framework and it's working flawlessly.
Post Reply