Boost logo

Boost :

From: Shams (shams_at_[hidden])
Date: 2007-04-09 01:19:19


I totally agree.

I used to think about the lack of the finally keyword in C++ and after
reading, pondering and implementing
I found that RAII is always just a so much better solution and saves you
from having writing so many scoped
blocks of code and local variables to hold state information etc.

For example one can even write a "very simple" RAII class for FILE* and THIS
is the only time I allow approve
usage of an "ugly" public member data BECAUSE this is not a fully fledged
wrapper class and writing one takes
time. This scenario is even more presentful on Windows because of enormous
amount of COM variables that
one may have to wrap...

ifndef utility_h
define utility_h
class File
{
public:
    // YES ugly pubic member data because this is not a fully fledged
class...
     FILE *Handle;

    File()
    {
        Handle = NULL;
    }

    File(const std::string &filename, const std::string &mode)
    {
        Handle = fopen(filename.c_str(), mode.c_str();

        if(ferror(Handle))
        {
            throw std::exception("fopen failed");
        }
    }

    ~File()
    {
        if(Handle)
        {
            fclose(Handle);
        }
    }
};
endif

void OpenFileAfterSelection()
{
    File f("/somefilename", "r+w");

    while(fgets(f.handle, somebuffer, sizeof(somebuffer))
    {
        // do something and maybe throw exceptions....
    }

    // AND regardless of exceptions or success ~f() always runs
}

Thanks
Shams

-- 
"Gregory Dai" <gregory.dai_at_[hidden]> wrote in message 
news:ce347e580704081736k661c2c03vae674ee11490539c_at_mail.gmail.com...
> On 4/1/07, Alexander Nasonov <alnsn_at_[hidden]> wrote:
>>
>> Hans Larsen wrote:
>> > Hello,
>> >
>> > Better is such a big word.  You still have to say in which way it is
>> > better; readability, efficiency, simplicity, something else?  ;-)
>> Readability and simplicity. Compare
>>
>> std::FILE* f = NULL;
>>
>> try
>> {
>>     f = std::fopen("/etc/passwd", "r");
>>     // ...
>> }
>> finally
>> {
>>     if(f)
>>         std::fclose(f);
>> }
>>
>> with
>>
>> std::FILE* f = std::fopen("/etc/passwd", "r");
>> scope(exit)
>> {
>>     if(f)
>>         std::fclose(f);
>> }
>>
>> // ...
>>
>> Even without D's scope(exit) syntax it looks appealing
>>
>> std::FILE* f = std::fopen("/etc/passwd", "r");
>> BOOST_SCOPE_EXIT(f, (f))
>> {
>>     if(f)
>>         std::fclose(f);
>> } BOOST_SCOPE_EXIT_END(f)
>
>
> Well, I'm very late to this thread of discussion, but I'd like to add my
> $0.02.
>
> Why would we go through so much complexity? Remember the simple and basic
> principle of RAII? In situations like this, you just make a simple wrapper
> of the FILE*, with an implementation similar to that of a smart pointer 
> such
> as the auto_ptr, and let's name it auto_cfile, with a "FILE*&()" operator
> member function to expose the FILE* it protects:
>
> auto_cfile f(std::fopen("/etc/passwd","r"));
> .... (uses of  f as if it were a FILE*)
>
> and the FILE* is automatically closed by auto_cfile's dtor when it goes 
> out
> of scope.
>
> No need of a "try ... catch" block at all, much less of "finally," which 
> is
> an unnecessary addition in C#, IMHO.
>
> <snip>
>
> --
>> Alexander Nasonov
>> http://nasonov.blogspot.com
>>
>
> <snip>
>
> -- 
> Thanks,
> Greg
> _______________________________________________
> Unsubscribe & other changes: 
> http://lists.boost.org/mailman/listinfo.cgi/boost
> 

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk