Add FileStream wrapper
This commit is contained in:
parent
dbc81c3548
commit
f23402dacd
@ -173,6 +173,7 @@ template class SizeChecker<UInt8, 1>;
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
@ -6,7 +6,6 @@
|
||||
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
||||
|
||||
#include "File.h"
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
#ifdef _WIN32
|
||||
#include <share.h> // for _SH_DENYWRITE
|
||||
@ -710,3 +709,29 @@ void cFile::Flush(void)
|
||||
{
|
||||
fflush(m_File);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <class StreamType>
|
||||
FileStream<StreamType>::FileStream(const std::string & Path) :
|
||||
StreamType()
|
||||
{
|
||||
// Except on failbit, which is what open sets on failure:
|
||||
FileStream::exceptions(FileStream::failbit | FileStream::badbit);
|
||||
|
||||
// Open the file:
|
||||
FileStream::open(Path);
|
||||
|
||||
// Only subsequently except on serious errors, and not on conditions like EOF or malformed input:
|
||||
FileStream::exceptions(FileStream::badbit);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Instantiate the templated wrapper for input and output:
|
||||
template class FileStream<std::ifstream>;
|
||||
template class FileStream<std::ofstream>;
|
||||
|
@ -185,3 +185,19 @@ private:
|
||||
|
||||
|
||||
|
||||
|
||||
/** A wrapper for file streams that enables exceptions. */
|
||||
template <class StreamType>
|
||||
class FileStream final : public StreamType
|
||||
{
|
||||
public:
|
||||
|
||||
FileStream(const std::string & Path);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
using InputFileStream = FileStream<std::ifstream>;
|
||||
using OutputFileStream = FileStream<std::ofstream>;
|
||||
|
Loading…
Reference in New Issue
Block a user