I was starting work on my windowing system, and the first class I built was a Rectangle, which I plan to use to pass dimensions around with (since WindowNew(int x, int y, int w, int h) is rather messy, especially if you need more arguments.) My first thought was that it would still be annoying to remember to free the temporary rects I'm passing into my window class.
So my idea was to combine my ClassBuilder with the scheduling lib I released yesterday, and do this:
Class:TempRectangle
Inherit:Rectangle
Comment: Deletes itself after the current VC execution has finished.
Init:int TempRectangleNew(int X, int Y, int W, int H)
InitCode:
EventSetupOnce(0, 'TempRectangleDispose', this);
EndCode
Code:
void TempRectangleDispose() {
Log('Auto-freeing temporary rectangle.');
TempRectangleFree(EventData);
}
EndCode
This gives me a Rectangle (that I can use all the Rectangle* functions I defined, like RectangleIntersect() and RectangleGetOverlap()) that will be deleted as soon as the current VC execution thread is finished. I wrote a little test to see if it was working properly; instantiating a few temporary rectangles, waiting for 10 seconds in a loop, doing a map transition, waiting a further 10 seconds, and then allowing things to stop so the events could be processed. The log file shows that the temporary rectangles were deleted after the map startup script finished executing, which was what I intended.
Obviously it's not something you'd want to use all the time, but I think it will be a useful technique for some of you...now if only I could think of a good way to recreate the auto_ptr class from C++'s STL. ;P