Skip to content
Snippets Groups Projects
Commit 9f446be7 authored by Nat Goodspeed's avatar Nat Goodspeed
Browse files

DRTVWR-476: Add LLUniqueFile, adding RAII semantics to LLFILE*.

LLUniqueFile wraps an LLFILE* in a move-only class that closes the wrapped
LLFILE* on destruction. It provides conversion operators to permit idiomatic
usage as an LLFILE* value.
parent cc6f1d61
No related branches found
No related tags found
No related merge requests found
...@@ -86,6 +86,69 @@ class LL_COMMON_API LLFile ...@@ -86,6 +86,69 @@ class LL_COMMON_API LLFile
static const char * tmpdir(); static const char * tmpdir();
}; };
/// RAII class
class LLUniqueFile
{
public:
// empty
LLUniqueFile(): mFileHandle(nullptr) {}
// wrap (e.g.) result of LLFile::fopen()
LLUniqueFile(LLFILE* f): mFileHandle(f) {}
// no copy
LLUniqueFile(const LLUniqueFile&) = delete;
// move construction
LLUniqueFile(LLUniqueFile&& other)
{
mFileHandle = other.mFileHandle;
other.mFileHandle = nullptr;
}
// The point of LLUniqueFile is to close on destruction.
~LLUniqueFile()
{
close();
}
// simple assignment
LLUniqueFile& operator=(LLFILE* f)
{
close();
mFileHandle = f;
return *this;
}
// copy assignment deleted
LLUniqueFile& operator=(const LLUniqueFile&) = delete;
// move assignment
LLUniqueFile& operator=(LLUniqueFile&& other)
{
close();
std::swap(mFileHandle, other.mFileHandle);
return *this;
}
// explicit close operation
void close()
{
if (mFileHandle)
{
// in case close() throws, set mFileHandle null FIRST
LLFILE* h{nullptr};
std::swap(h, mFileHandle);
LLFile::close(h);
}
}
// detect whether the wrapped LLFILE is open or not
explicit operator bool() const { return bool(mFileHandle); }
bool operator!() { return ! mFileHandle; }
// LLUniqueFile should be usable for any operation that accepts LLFILE*
// (or FILE* for that matter)
operator LLFILE*() const { return mFileHandle; }
private:
LLFILE* mFileHandle;
};
#if LL_WINDOWS #if LL_WINDOWS
/** /**
* @brief Controlling input for files. * @brief Controlling input for files.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment