CASM  1.1.0
A Clusters Approach to Statistical Mechanics
CASM::SafeOfstream Class Reference

#include <SafeOfstream.hh>

Detailed Description

Write to a temporary file to ensure a good write, then rename.

jsonParser json;
// by default, write temporary file "something.json.tmp", or specify your
own extension
// // throws if "something.json.tmp" already exists
file.open("something.json", "tmp");
// write to the underlying stream
json.write(file.ofstream());
// 'close' results in closing "something.json.tmp", checking
fs::ofstream::fail(), and if not failed,
// then removing "something.json", and renaming "something.json.tmp" ->
"something.json" file.close();

Definition at line 31 of file SafeOfstream.hh.

Public Member Functions

 SafeOfstream ()
 
void open (fs::path name, std::string tmp_ext="tmp")
 Opens "file.tmp" for writing, with intended final target "file". More...
 
fs::ofstream & ofstream ()
 Access underlying stream. More...
 
void close ()
 Closes stream, and if not a failed write, removes "file" and renames "file.tmp" to "file". More...
 

Private Attributes

fs::path m_name
 
fs::path m_tmp_name
 
fs::ofstream m_sout
 

Constructor & Destructor Documentation

◆ SafeOfstream()

CASM::SafeOfstream::SafeOfstream ( )
inline

Definition at line 33 of file SafeOfstream.hh.

Member Function Documentation

◆ close()

void CASM::SafeOfstream::close ( )
inline

Closes stream, and if not a failed write, removes "file" and renames "file.tmp" to "file".

Definition at line 84 of file SafeOfstream.hh.

◆ ofstream()

fs::ofstream& CASM::SafeOfstream::ofstream ( )
inline

Access underlying stream.

Definition at line 80 of file SafeOfstream.hh.

◆ open()

void CASM::SafeOfstream::open ( fs::path  name,
std::string  tmp_ext = "tmp" 
)
inline

Opens "file.tmp" for writing, with intended final target "file".

Parameters
nameName of target file
tmp_extString to be used as an extension for the temporary file that is written
Exceptions
ifname.tmp_ext already exists

Example:

jsonParser json;
// By default, open temporary file "something.json.tmp" for writing.
// - throws if "something.json.tmp" already exists
file.open("something.json");
// or specify your own extension, to open temporary file
"something.json.backup"
// - throws if "something.json.backup" already exists
// file.open("something.json", "backup");
// print to the underlying stream
json.print(file.ofstream());
// 'close' results in closing ofstream to "something.json.tmp", checking
fs::ofstream::fail(),
// and if not failed then removing "something.json", and renaming
"something.json.tmp" -> "something.json" file.close();

Definition at line 65 of file SafeOfstream.hh.

Member Data Documentation

◆ m_name

fs::path CASM::SafeOfstream::m_name
private

Definition at line 93 of file SafeOfstream.hh.

◆ m_sout

fs::ofstream CASM::SafeOfstream::m_sout
private

Definition at line 95 of file SafeOfstream.hh.

◆ m_tmp_name

fs::path CASM::SafeOfstream::m_tmp_name
private

Definition at line 94 of file SafeOfstream.hh.


The documentation for this class was generated from the following file: