#ifndef AUDIOGRAPHER_TMP_FILE_H #define AUDIOGRAPHER_TMP_FILE_H #include #include #include #include #include "sndfile_writer.h" #include "sndfile_reader.h" namespace AudioGrapher { /// A temporary file deleted after this class is destructed template class TmpFile : public SndfileWriter, public SndfileReader { public: /// \a filename_template must match the requirements for mkstemp, i.e. end in "XXXXXX" TmpFile (char * filename_template, int format, ChannelCount channels, framecnt_t samplerate) : SndfileHandle (g_mkstemp(filename_template), true, SndfileBase::ReadWrite, format, channels, samplerate) , filename (filename_template) {} TmpFile (int format, ChannelCount channels, framecnt_t samplerate) : SndfileHandle (fileno (tmpfile()), true, SndfileBase::ReadWrite, format, channels, samplerate) {} TmpFile (TmpFile const & other) : SndfileHandle (other) {} using SndfileHandle::operator=; ~TmpFile() { /* explicitly close first, some OS (yes I'm looking at you windows) * cannot delet files that are still open */ if (!filename.empty()) { SndfileBase::close(); std::remove(filename.c_str()); } } private: std::string filename; }; } // namespace #endif // AUDIOGRAPHER_TMP_FILE_H