OSB_LIB::CloseFile Class Reference

Collaboration diagram for OSB_LIB::CloseFile:

Collaboration graph
[legend]
List of all members.

Detailed Description

Automatically close a file if the object goes out of scope.

Typical usage:

      void foo()
      {
          FILE* in = fopen(...);
          if (0 == in) { ... }  // error handling
          CloseFile guard(in);
          ...                   // do something that might throw
          guard.release();      // take responsibility to close `in'
          in.close();
      }

Remarks:
If doClose_ is false the class will never access the member file_.

Definition at line 69 of file osbfileutil.h.

Public Member Functions

Private Member Functions

Private Attributes


Constructor & Destructor Documentation

OSB_LIB::CloseFile::CloseFile ( FILE *  file,
bool  doClose = true 
)

Constructor with file to protected and closure flag.

OSB_LIB::CloseFile::CloseFile ( const CloseFile  )  [private]

Prevent copying: not implemented.

OSB_LIB::CloseFile::~CloseFile (  ) 

Close file_.

If doClose() returns true the destructor closes the protected file and sets it to 0.


Member Function Documentation

bool OSB_LIB::CloseFile::doClose (  )  const [inline]

Query if file would be closed.

Definition at line 99 of file osbfileutil.h.

References doClose_, and file_.

bool OSB_LIB::CloseFile::release (  ) 

Revoke responsiblity to close file_.

Returns:
Value of doClose_ before the function is called.
Remarks:
The function should always be called before a regular return by the owner of the guard. Besides cleaner source code this avoids compiler warnings about unused variables.


Member Data Documentation

bool OSB_LIB::CloseFile::doClose_ [private]

Flag to close the file.

Definition at line 102 of file osbfileutil.h.

Referenced by doClose().

FILE* OSB_LIB::CloseFile::file_ [private]

Reference to protected file.

Definition at line 104 of file osbfileutil.h.

Referenced by doClose().


The documentation for this class was generated from the following file:
Generated on Sat Sep 2 14:12:07 2006 for OSB Library by  doxygen 1.4.7