
On 11/3/07, Emil Dotchevski <emil@revergestudios.com> wrote:
I don't think making file_entry is a lot of work. It's pretty simple and straightforward. And once it's made, you keep it around in your arsenal.
Not to split hairs here, but you could also write:
boost::shared_ptr<FILE> open_file( char const * name, char const * mode ) { if( FILE * f = fopen(name,mode) ) return boost::shared_ptr<FILE>(f,fclose); else throw fopen_error(name,mode); }
and keep that in your "arsenal".
In fact, I think this function would be a good addition to boost so it's part of everyone's arsenal. :)
This free function looks pretty good, indeed. But again, file_sentry works with not only shared_ptr, but auto_ptr or other *_ptr's as well. In reality auto_ptr is lighter and has more applications than shared_ptr. That is, there are in general more applications of auto_ptr<file_sentry> than of shared_ptr<file_sentry>. Now we are talking about templatizing this function and what not. In another dimension, we have other C pointer types similar to FILE*. This is harder to templatize than the type of *_ptr, due to differences in functions such as fopen()/fclose(). In such cases, it seems ideal to have separate thin wrappers to encapsulate the differences.