Callbacks for final cleanups

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 450
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

Callbacks for final cleanups

Post 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.
Go, tell it on the mountains.
User avatar
Bisonte
Addict
Addict
Posts: 1308
Joined: Tue Oct 09, 2007 2:15 am

Re: Callbacks for final cleanups

Post 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...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 450
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

Re: Callbacks for final cleanups

Post 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.
Go, tell it on the mountains.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Callbacks for final cleanups

Post 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.
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 450
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

Re: Callbacks for final cleanups

Post 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.
Go, tell it on the mountains.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Callbacks for final cleanups

Post 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.
Post Reply