All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
atomicOfstreamWrapper.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_TF_ATOMIC_OFSTREAM_WRAPPER_H
8#define PXR_BASE_TF_ATOMIC_OFSTREAM_WRAPPER_H
9
12
13#include "pxr/pxr.h"
14#include "pxr/base/tf/api.h"
15
16#include <fstream>
17#include <string>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
61{
63 TfAtomicOfstreamWrapper& operator=(
64 const TfAtomicOfstreamWrapper&) = delete;
65public:
67 TF_API explicit TfAtomicOfstreamWrapper(const std::string& filePath);
68
71
78 TF_API bool Open(std::string* reason = 0);
79
85 TF_API bool Commit(std::string* reason = 0);
86
88 TF_API bool Cancel(std::string* reason = 0);
89
93 std::ofstream& GetStream() { return _stream; }
94
95private:
96 std::string _filePath;
97 std::string _tmpFilePath;
98 std::ofstream _stream;
99};
100
101PXR_NAMESPACE_CLOSE_SCOPE
102
103#endif // PXR_BASE_TF_ATOMIC_OFSTREAM_WRAPPER_H
A class that wraps a file output stream, providing improved tolerance for write failures.
TF_API bool Commit(std::string *reason=0)
Synchronizes the temporary file contents to disk, and renames the temporary file into the file path p...
TF_API ~TfAtomicOfstreamWrapper()
Destructor. Calls Cancel().
TF_API bool Cancel(std::string *reason=0)
Closes the temporary file and removes it from disk, if it exists.
TF_API TfAtomicOfstreamWrapper(const std::string &filePath)
Constructor.
std::ofstream & GetStream()
Returns the stream.
TF_API bool Open(std::string *reason=0)
Opens the temporary file for writing.