Multi-instance (also thread-safe) output file device. More...
#include <shared_file.hpp>
Public Member Functions | |
unsigned int | count () |
Denotes how many instances are currently sharing this file. More... | |
const Error & | error () const |
bool | flush () |
Flush the internal buffer. More... | |
bool | open () |
Status flag indicating if the file is open/closed. More... | |
bool | open (const std::string &name, WriteMode mode=New) |
Opens the file for writing. More... | |
SharedFile () | |
Non-RAII style constructor, doesn't open a file. More... | |
SharedFile (const std::string &name, WriteMode mode=New) | |
Either opens a file for writing or a link to an existing shared instance. More... | |
long | write (const char &c) |
Write a character to the buffer. More... | |
long | write (const char *s, unsigned long n) |
Write a character string to the buffer. More... | |
virtual | ~SharedFile () |
Automatic cleaner for shared files. More... | |
Private Attributes | |
devices::CharBuffer | buffer |
devices::SharedFileCommon * | shared_instance |
Multi-instance (also thread-safe) output file device.
This allows multiple instantiations/connections (uniquely identified by string file name) to an OFile device. Each instantiation has its own buffer that protects data from multiple sources getting unnecessarily tangled. It is also thread-safe.
Usage:
Usage is exactly the same as that for regular OFile devices except that no new files are made if the same file name is used (instead, instantiations share this file). Everything else happens under the hood and cleanup occurs in the destructors.
Definition at line 126 of file shared_file.hpp.
|
inline |
Non-RAII style constructor, doesn't open a file.
Sometimes its more convenient to open manually rather than inside constructors. Use this with the open() command to do so. You can check for open status via the open accessor.
Definition at line 140 of file shared_file.hpp.
Either opens a file for writing or a link to an existing shared instance.
If a file by this name is not yet being shared, then it opens it directly. Otherwise it opens a link to the shared instance. Note, you may ignore the second argument (mode) if you are simply opening a second link to the shared instance.
name | : filename. |
mode | : mode for writing (New, Append), this must be the same for all instances. |
StandardException | : throws if the file could not be opened. |
Definition at line 141 of file shared_file.cpp.
|
virtual |
Automatic cleaner for shared files.
Automatically cleans up and closes io handles.
Definition at line 151 of file shared_file.cpp.
|
inline |
Denotes how many instances are currently sharing this file.
Denotes how many instances are currently sharing this file.
Definition at line 188 of file shared_file.hpp.
|
inline |
Definition at line 240 of file shared_file.hpp.
bool ecl::SharedFile::flush | ( | ) |
Flush the internal buffer.
This writes to the shared file. Error handling the same for OFile's write function.
StandardException | : throws from the underlying file if flushing returned an error [debug mode only]. |
Definition at line 200 of file shared_file.cpp.
|
inline |
Status flag indicating if the file is open/closed.
Definition at line 198 of file shared_file.hpp.
Opens the file for writing.
Configures and opens the file for writing in either New or Append mode. Note, that you don't need to do this if you constructed this class with the RAII style constructor as it gets automatically called.
Error handling the same for OFile's open() call.
name | : name of the file to open. |
mode | : mode of writing, either New or Append. |
StandardException | : throws if the connection failed to open. |
Definition at line 161 of file shared_file.cpp.
long ecl::SharedFile::write | ( | const char & | c | ) |
Write a character to the buffer.
Write a character to the buffer. It will automatically flush (write to the shared ofile instance) if the buffer exceeds its capacity.
Error handling the same for OFile's write function.
c | : the character to write. |
StandardException | : throws from the underlying file if writing returned an error [debug mode only]. |
Definition at line 177 of file shared_file.cpp.
long ecl::SharedFile::write | ( | const char * | s, |
unsigned long | n | ||
) |
Write a character string to the buffer.
Write a character string to the buffer. It will automatically flush (write to the shared ofile instance) if the buffer exceeds its capacity.
Error handling the same for OFile's write function.
s | : points to the beginning of the character string. |
n | : the number of characters to write. |
StandardException | : throws from the underlying file if flushing returned an error [debug mode only]. |
Definition at line 187 of file shared_file.cpp.
|
private |
Definition at line 244 of file shared_file.hpp.
|
private |
Definition at line 243 of file shared_file.hpp.