Merge branch 'master' into cairocanvas
[ardour.git] / libs / audiographer / audiographer / sndfile / tmp_file.h
1 #ifndef AUDIOGRAPHER_TMP_FILE_H
2 #define AUDIOGRAPHER_TMP_FILE_H
3
4 #include <cstdio>
5 #include <string>
6
7 #include "sndfile_writer.h"
8 #include "sndfile_reader.h"
9
10 namespace AudioGrapher
11 {
12
13 /// A temporary file deleted after this class is destructed
14 template<typename T = DefaultSampleType>
15 class TmpFile : public SndfileWriter<T>, public SndfileReader<T>
16 {
17   public:
18
19         /// \a filename_template must match the requirements for mkstemp, i.e. end in "XXXXXX"
20         TmpFile (char * filename_template, int format, ChannelCount channels, framecnt_t samplerate)
21                 : SndfileHandle (mkstemp(filename_template), true, SndfileBase::ReadWrite, format, channels, samplerate)
22                 , filename (filename_template)
23         {}
24
25         TmpFile (int format, ChannelCount channels, framecnt_t samplerate)
26           : SndfileHandle (fileno (tmpfile()), true, SndfileBase::ReadWrite, format, channels, samplerate)
27         {}
28
29         TmpFile (TmpFile const & other) : SndfileHandle (other) {}
30         using SndfileHandle::operator=;
31
32         ~TmpFile()
33         {
34                 if (!filename.empty()) {
35                         std::remove(filename.c_str());
36                 }
37         }
38
39   private:
40         std::string filename;
41 };
42
43 } // namespace
44
45 #endif // AUDIOGRAPHER_TMP_FILE_H