Belle II Software development
FileSystem::TemporaryFile Class Reference

Helper file to create a temporary file and ensure deletion if object goes out of scope. More...

#include <FileSystem.h>

Inheritance diagram for FileSystem::TemporaryFile:

Public Member Functions

 TemporaryFile (std::ios_base::openmode mode=std::ios_base::trunc|std::ios_base::out)
 construct a new temporary file
 
 TemporaryFile (const TemporaryFile &)=delete
 prevent assignment
 
 TemporaryFile (TemporaryFile &&)=delete
 and no move construction as basic_ios doesn't allow that
 
TemporaryFileoperator= (const TemporaryFile &)=delete
 prevent copy constructor
 
 ~TemporaryFile ()
 close file and delete on destruction
 
std::string getName () const
 get filename of the temporary file
 

Private Attributes

std::string m_filename
 filename of the temporary file
 

Detailed Description

Helper file to create a temporary file and ensure deletion if object goes out of scope.

Definition at line 128 of file FileSystem.h.

Constructor & Destructor Documentation

◆ TemporaryFile()

TemporaryFile ( std::ios_base::openmode  mode = std::ios_base::trunc | std::ios_base::out)
explicit

construct a new temporary file

Definition at line 223 of file FileSystem.cc.

223 : std::fstream()
224{
225 char* temporaryFileName = strdup((std::filesystem::temp_directory_path() / "basf2_XXXXXX").c_str());
226 int fileDescriptor = mkstemp(temporaryFileName);
227 if (fileDescriptor == -1) {
228 B2ERROR("Cannot create temporary file: " << strerror(errno));
229 free(temporaryFileName);
230 return;
231 }
232 m_filename = std::string(temporaryFileName);
233 open(temporaryFileName, mode);
234 if (!is_open()) {
235 B2ERROR("Cannot open temporary file: " << strerror(errno));
236 }
237 free(temporaryFileName);
238 ::close(fileDescriptor);
239}
std::string m_filename
filename of the temporary file
Definition: FileSystem.h:144

◆ ~TemporaryFile()

close file and delete on destruction

Definition at line 241 of file FileSystem.cc.

242{
243 close();
244 fs::remove(m_filename);
245}

Member Function Documentation

◆ getName()

std::string getName ( ) const
inline

get filename of the temporary file

Definition at line 141 of file FileSystem.h.

141{ return m_filename; }

Member Data Documentation

◆ m_filename

std::string m_filename
private

filename of the temporary file

Definition at line 144 of file FileSystem.h.


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