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. | |
const Error & | error () const |
bool | flush () ecl_debug_throw_decl(StandardException) |
Flush the internal buffer. | |
bool | open (const std::string &name, WriteMode mode=New) ecl_throw_decl(StandardException) |
Opens the file for writing. | |
bool | open () |
Status flag indicating if the file is open/closed. | |
SharedFile () | |
Non-RAII style constructor, doesn't open a file. | |
SharedFile (const std::string &name, WriteMode mode=New) ecl_throw_decl(StandardException) | |
Either opens a file for writing or a link to an existing shared instance. | |
long | write (const char &c) ecl_debug_throw_decl(StandardException) |
Write a character to the buffer. | |
long | write (const char *s, unsigned long n) ecl_debug_throw_decl(StandardException) |
Write a character string to the buffer. | |
virtual | ~SharedFile () |
Automatic cleaner for shared files. | |
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 118 of file shared_file.hpp.
ecl::SharedFile::SharedFile | ( | ) | [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 130 of file shared_file.hpp.
ecl::SharedFile::SharedFile | ( | const std::string & | name, |
WriteMode | mode = New |
||
) |
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 135 of file shared_file.cpp.
ecl::SharedFile::~SharedFile | ( | ) | [virtual] |
Automatic cleaner for shared files.
Automatically cleans up and closes io handles.
Definition at line 145 of file shared_file.cpp.
unsigned int ecl::SharedFile::count | ( | ) | [inline] |
Denotes how many instances are currently sharing this file.
Denotes how many instances are currently sharing this file.
Definition at line 178 of file shared_file.hpp.
const Error& ecl::SharedFile::error | ( | ) | const [inline] |
Definition at line 230 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 194 of file shared_file.cpp.
bool ecl::SharedFile::open | ( | const std::string & | name, |
WriteMode | mode = New |
||
) |
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 155 of file shared_file.cpp.
bool ecl::SharedFile::open | ( | ) | [inline] |
Status flag indicating if the file is open/closed.
Definition at line 188 of file shared_file.hpp.
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 171 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 181 of file shared_file.cpp.
devices::CharBuffer ecl::SharedFile::buffer [private] |
Definition at line 234 of file shared_file.hpp.
Definition at line 233 of file shared_file.hpp.