KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Member Functions | List of all members
kjb::File_Ptr Class Reference

RAII wrapper on stdio.h FILE pointers (use a derived class though). More...

#include <l_stdio_wrap.h>

Inheritance diagram for kjb::File_Ptr:
kjb::File_Ptr_Append kjb::File_Ptr_Read kjb::File_Ptr_Read_Plus kjb::File_Ptr_Smart_Read kjb::File_Ptr_Write kjb::Temporary_File

Public Member Functions

virtual int close ()
 close file before destruction; rarely needed; safe to do twice. More...
 
virtual ~File_Ptr ()
 dtor can simply call close() because closing a null ptr is OK. More...
 
 operator FILE * () const
 transparently use this object wherever a FILE* is used! More...
 

Protected Member Functions

void throw_io_error (const std::string &pathname, const char *mode)
 this is what we do when things go terribly wrong More...
 
 File_Ptr (const std::string &pathname, const char *mode)
 ctor needs the pathname and a mode; mode is set by derived class More...
 
 File_Ptr ()
 default ctor wraps a null pointer (to set up a later swap maybe) More...
 
void swap (File_Ptr &that)
 swap the contents of two wrappers More...
 

Detailed Description

RAII wrapper on stdio.h FILE pointers (use a derived class though).

If you have the misfortune of needing to use stdio.h routines for file IO, consider using one of the derived classes of this class as an RAII wrapper on the pointer object. When done correctly, it means never having to call the close function yourself, because the dtor will do it for you.

This is a base class not meant to be instantiated directly. Instead, use one of the following derived classes (list not necessarily exhaustive):

Constructor & Destructor Documentation

kjb::File_Ptr::File_Ptr ( const std::string &  pathname,
const char *  mode 
)
inlineprotected

ctor needs the pathname and a mode; mode is set by derived class

Exceptions
kjb::IO_errorif the underlying fopen call fails
kjb::File_Ptr::File_Ptr ( )
inlineprotected

default ctor wraps a null pointer (to set up a later swap maybe)

virtual kjb::File_Ptr::~File_Ptr ( )
inlinevirtual

dtor can simply call close() because closing a null ptr is OK.

Member Function Documentation

virtual int kjb::File_Ptr::close ( )
inlinevirtual

close file before destruction; rarely needed; safe to do twice.

Reimplemented in kjb::Temporary_File.

kjb::File_Ptr::operator FILE * ( ) const
inline

transparently use this object wherever a FILE* is used!

void kjb::File_Ptr::swap ( File_Ptr that)
inlineprotected

swap the contents of two wrappers

void kjb::File_Ptr::throw_io_error ( const std::string &  pathname,
const char *  mode 
)
inlineprotected

this is what we do when things go terribly wrong

Exceptions
kjb::IO_errorin all cases, echoing the parameters.

The documentation for this class was generated from the following file: