Page 1 of 1

SimpleOOP method addresses

Posted: Thu Oct 18, 2012 1:14 pm
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 :)

Re: SimpleOOP method addresses

Posted: Thu Oct 18, 2012 4:51 pm
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! :)

Re: SimpleOOP method addresses

Posted: Fri Oct 19, 2012 5:11 am
by chris319
Example, please?

Re: SimpleOOP method addresses

Posted: Sun Oct 21, 2012 3:57 am
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)

Re: SimpleOOP method addresses

Posted: Mon Nov 05, 2012 2:56 pm
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.