Page 1 of 1

Callbacks for final cleanups

Posted: Thu Sep 05, 2013 7:23 am
by Didelphodon
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.

Re: Callbacks for final cleanups

Posted: Thu Sep 05, 2013 10:11 am
by Bisonte
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...

Re: Callbacks for final cleanups

Posted: Thu Sep 05, 2013 11:33 am
by Didelphodon
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.

Re: Callbacks for final cleanups

Posted: Fri Sep 06, 2013 6:28 am
by Danilo
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:

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
I used 'Quit' instead 'End' here. But the cleanup should also be done internally without using 'End' or 'End errorCode'. It should be automatic.

Re: Callbacks for final cleanups

Posted: Fri Sep 06, 2013 8:12 am
by Didelphodon
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:

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
I used 'Quit' instead 'End' here. But the cleanup should also be done internally without using 'End' or 'End errorCode'. It should be automatic.
Exactly what I'm talking about. I also have my own version of this but named it EndProgram. :-)

Cheers,
Didel.

Re: Callbacks for final cleanups

Posted: Fri Sep 06, 2013 8:21 am
by Danilo
And with additional keywords:

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
Output (without additional code) should be:

Code: Select all

DoInit
mod1::DoInit
mod1::EndIt
EndIt
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.