[V4Rb 2] VDatabase.Close

Ruslan Zasukhin sunshine at public.kherson.ua
Wed Jun 1 21:06:36 CDT 2005


On 6/1/05 8:50 PM, "Charles Yeomans" <yeomans at desuetude.com> wrote:

>> I will not explain advantages of this technique.
>> They are described in the "C++ PL" of Stroustrup.
> 
> Yes; I have read that section very carefully.  The use of the
> constructor for acquisition certainly appeals to symmetry, but the key
> point of the technique is the use of the destructor to release the
> resource.  

But resources allocated by constructor!!!!!!!!!!!
Open in your example is not made by constructor.

Look. For open close exists such simple example, and I have use it in
Valentina kernel

class StFileKeeper
{
        StFileKeeper( File* inFile )
        {
            mpFile = inFile;     // we usually store object
            mpFile.Open();
        }

        ~StFileKeeper( void )
        {
            mpFile.Close();
        }

    
    File*      mpFile;
};    

This simple class now allow me write such code as


Func()
{
     StFileKeeper keeper( f );    // this do open
    
     // do some work...
     f.read()    
}

Without such class I need write


Func()
{
    f.open()
    
    try
    {
        // do some work...
        f.read()   
    }
    catch(...)
    {
        f.close()
    }

    f.close()
}

As you see it save me many lines of code.
Also note in name prefix St, this is form STACK.
Because this technique works only for variable that we create on stack.
So it is good idea underline this in name of class.

Also note important difference!!!

    this technique means that you have one MAIN class as File.
    and one small utility class which have only constructor
    and destructor.

What you suggest is that MAIN class' destructor do this collection of
garbage. And we do this. But again, from point of symmetry the open() should
have close()

> Suppose you're writing some sort of socket class.  Then you
> would not want to attempt to acquire the resource in the constructor,
> because socket connections can fail for many reasons, and generally a
> constructor should raise an exception if it fails (although one could
> instead craft a better definition of "succeed" in this case).  But you
> would still want the destructor to release the port.

Right., if you look on any socket class you will see that exists

    constructor -- create instance
    destructor

    open
    close

Open and close can fail, so RIGT, you should wrap them by try-catch.
And you can use this technique in C++ and in REALbasic to be lazy and write
smaller code.

Btw, I think this is good TIP for all RB users. So I CC to NUG.

 
>> MAY BE btw, this technique can be useful for RB developers!
>> I think 100% can be used. Than more that now RB have exceptions..
> 
> It certainly is useful for Rb; I use it a lot, and so does the Rb
> framework.  Classes like BinaryStream, TextInputStream etc.  use this
> scheme.

-- 
Best regards,

Ruslan Zasukhin
VP Engineering and New Technology
Paradigma Software, Inc

Valentina - Joining Worlds of Information
http://www.paradigmasoft.com

[I feel the need: the need for speed]




More information about the Valentina mailing list