It would be cool if PB would have the possibility to register callback procedures to be able to cleanul stuff in the end of execution.
This is especially true for includes one uses which need/want to do some stuff like cleanin up temp files and the like.
Callbacks for final cleanups
- Didelphodon
- PureBasic Expert
- Posts: 450
- Joined: Sat Dec 18, 2004 11:56 am
- Location: Vienna - Austria
- Contact:
Callbacks for final cleanups
Go, tell it on the mountains.
Re: Callbacks for final cleanups
Where is the sense of something like that ?
If you have to write the callback-procedure that frees up anything you've done,
so you can call your own "free" procedure... it's already written...
If you have to write the callback-procedure that frees up anything you've done,
so you can call your own "free" procedure... it's already written...
- Didelphodon
- PureBasic Expert
- Posts: 450
- Joined: Sat Dec 18, 2004 11:56 am
- Location: Vienna - Austria
- Contact:
Re: Callbacks for final cleanups
A scenario I'm having is that I have a lot of topicly isolated include-files. Some of them would need to do clean-up things on an END statement. If someone else uses one of my includes she wouldn't be aware of the need to have clean-up in the end. Therefore, if the include could register its own clean-up/finalizing procedure to be called on every END (and there can be multiple), that would be very comfortable, imho.
Go, tell it on the mountains.
Re: Callbacks for final cleanups
Good idea, I missed something like this already multiple times. PB would just call user registered functions at end (right before it does its own internal cleanup).
Something like this:
I used 'Quit' instead 'End' here. But the cleanup should also be done internally without using 'End' or 'End errorCode'. It should be automatic.
Something like this:
Code: Select all
Prototype protoCleanupFunction()
Global NewList ___CleanupFunctionPointers.i()
Procedure.i RegisterCleanupFunction(f.protoCleanupFunction)
If f
ResetList( ___CleanupFunctionPointers() )
If AddElement( ___CleanupFunctionPointers() )
___CleanupFunctionPointers() = f
ProcedureReturn #True
EndIf
EndIf
ProcedureReturn #False
EndProcedure
Procedure UnregisterCleanupFunction(f.protoCleanupFunction)
ForEach ___CleanupFunctionPointers()
If ___CleanupFunctionPointers() = f
DeleteElement( ___CleanupFunctionPointers() )
EndIf
Next
EndProcedure
Procedure DoCleanupFunctions()
ForEach ___CleanupFunctionPointers()
f.protoCleanupFunction = ___CleanupFunctionPointers()
If f
f()
EndIf
Next
EndProcedure
Macro Quit ; should be 'End'
DoCleanupFunctions() : End
EndMacro
;------------------------
Procedure myFunc1() : Debug "myFunc1" : EndProcedure
Procedure myFunc2() : Debug "myFunc2" : EndProcedure
RegisterCleanupFunction(@myFunc1())
RegisterCleanupFunction(@myFunc2())
Delay(3000)
Quit ; call user cleanup functions at program end automatically
- Didelphodon
- PureBasic Expert
- Posts: 450
- Joined: Sat Dec 18, 2004 11:56 am
- Location: Vienna - Austria
- Contact:
Re: Callbacks for final cleanups
Exactly what I'm talking about. I also have my own version of this but named it EndProgram.Danilo wrote:Good idea, I missed something like this already multiple times. PB would just call user registered functions at end (right before it does its own internal cleanup).
Something like this:I used 'Quit' instead 'End' here. But the cleanup should also be done internally without using 'End' or 'End errorCode'. It should be automatic.Code: Select all
Prototype protoCleanupFunction() Global NewList ___CleanupFunctionPointers.i() Procedure.i RegisterCleanupFunction(f.protoCleanupFunction) If f ResetList( ___CleanupFunctionPointers() ) If AddElement( ___CleanupFunctionPointers() ) ___CleanupFunctionPointers() = f ProcedureReturn #True EndIf EndIf ProcedureReturn #False EndProcedure Procedure UnregisterCleanupFunction(f.protoCleanupFunction) ForEach ___CleanupFunctionPointers() If ___CleanupFunctionPointers() = f DeleteElement( ___CleanupFunctionPointers() ) EndIf Next EndProcedure Procedure DoCleanupFunctions() ForEach ___CleanupFunctionPointers() f.protoCleanupFunction = ___CleanupFunctionPointers() If f f() EndIf Next EndProcedure Macro Quit ; should be 'End' DoCleanupFunctions() : End EndMacro ;------------------------ Procedure myFunc1() : Debug "myFunc1" : EndProcedure Procedure myFunc2() : Debug "myFunc2" : EndProcedure RegisterCleanupFunction(@myFunc1()) RegisterCleanupFunction(@myFunc2()) Delay(3000) Quit ; call user cleanup functions at program end automatically

Cheers,
Didel.
Go, tell it on the mountains.
Re: Callbacks for final cleanups
And with additional keywords:
Output (without additional code) should be:
So InitProcedure functions would get called automatically by PB after initialization (in order of detection at parsing), before starting any actual PB code, but after init of PB internal libraries.
CleanupProcedure functions would get called at program end (in reverse order of detection at parsing), right before PB internal cleanup code.
It would allow to write includes and modules that have Init/Cleanup functions like PB libraries.
Code: Select all
InitProcedure DoInit()
Debug "DoInit"
EndInitProcedure
CleanupProcedure EndIt()
Debug "EndIt"
EndCleanupProcedure
DeclareModule xyz
EndDeclareModule
Module mod1
InitProcedure DoInit()
Debug "mod1::DoInit"
EndInitProcedure
CleanupProcedure EndIt()
Debug "mod1::EndIt"
EndCleanupProcedure
EndModule
Code: Select all
DoInit
mod1::DoInit
mod1::EndIt
EndIt
CleanupProcedure functions would get called at program end (in reverse order of detection at parsing), right before PB internal cleanup code.
It would allow to write includes and modules that have Init/Cleanup functions like PB libraries.