Version: SMASH-3.1
smash::RenamingFilePtr Class Reference

#include <file.h>

A RAII type to replace std::FILE *.

While open, the file name ends with ".unfinished".

Automatically closes and renames the file to the original when it goes out of scope. If the object is destroyed because of stack unwinding, no renaming is done.

Definition at line 72 of file file.h.

Public Member Functions

 RenamingFilePtr (const std::filesystem::path &filename, const std::string &mode)
 Construct a RenamingFilePtr. More...
 
FILE * get ()
 Get the underlying FILE* pointer. More...
 
 ~RenamingFilePtr ()
 Close the file and rename it. More...
 

Private Attributes

FILE * file_
 Internal file pointer. More...
 
std::filesystem::path filename_
 Path of the finished file. More...
 
std::filesystem::path filename_unfinished_
 Path of the unfinished file. More...
 
int uncaught_exceptions_ {std::uncaught_exceptions()}
 Number of uncaught exceptions at the time when the object is created. More...
 

Constructor & Destructor Documentation

◆ RenamingFilePtr()

smash::RenamingFilePtr::RenamingFilePtr ( const std::filesystem::path &  filename,
const std::string &  mode 
)

Construct a RenamingFilePtr.

Parameters
[in]filenamePath to the file.
[in]modeThe mode in which the file should be opened (see std::fopen).
Returns
The constructed object.

Definition at line 19 of file file.cc.

20  {
21  filename_ = filename;
22  filename_unfinished_ = filename;
23  filename_unfinished_ += ".unfinished";
24  file_ = std::fopen(filename_unfinished_.c_str(), mode.c_str());
25 }
std::filesystem::path filename_unfinished_
Path of the unfinished file.
Definition: file.h:95
std::filesystem::path filename_
Path of the finished file.
Definition: file.h:93
FILE * file_
Internal file pointer.
Definition: file.h:91
FilePtr fopen(const std::filesystem::path &filename, const std::string &mode)
Open a file with given mode.
Definition: file.cc:14

◆ ~RenamingFilePtr()

smash::RenamingFilePtr::~RenamingFilePtr ( )

Close the file and rename it.

Definition at line 29 of file file.cc.

29  {
30  std::fclose(file_);
31  // we rename the output file only if we are not unwinding the stack
32  // because of an exception
33  if (std::uncaught_exceptions() == uncaught_exceptions_) {
34  std::filesystem::rename(filename_unfinished_, filename_);
35  }
36 }
int uncaught_exceptions_
Number of uncaught exceptions at the time when the object is created.
Definition: file.h:102

Member Function Documentation

◆ get()

FILE * smash::RenamingFilePtr::get ( )

Get the underlying FILE* pointer.

Definition at line 27 of file file.cc.

27 { return file_; }

Member Data Documentation

◆ file_

FILE* smash::RenamingFilePtr::file_
private

Internal file pointer.

Definition at line 91 of file file.h.

◆ filename_

std::filesystem::path smash::RenamingFilePtr::filename_
private

Path of the finished file.

Definition at line 93 of file file.h.

◆ filename_unfinished_

std::filesystem::path smash::RenamingFilePtr::filename_unfinished_
private

Path of the unfinished file.

Definition at line 95 of file file.h.

◆ uncaught_exceptions_

int smash::RenamingFilePtr::uncaught_exceptions_ {std::uncaught_exceptions()}
private

Number of uncaught exceptions at the time when the object is created.

If it has not changed when the object is destroyed, we suppose that the contents of the file are reliable and in the destructor we can safely rename the file and remove the _.unfinished_ suffix.

Definition at line 102 of file file.h.


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