Public Member Functions | Private Attributes | List of all members
ecl::SharedFile Class Reference

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 Errorerror () 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::SharedFileCommonshared_instance
 

Detailed Description

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.

See also
OFile.

Definition at line 126 of file shared_file.hpp.

Constructor & Destructor Documentation

◆ SharedFile() [1/2]

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 140 of file shared_file.hpp.

◆ SharedFile() [2/2]

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.

Parameters
name: filename.
mode: mode for writing (New, Append), this must be the same for all instances.
Exceptions
StandardException: throws if the file could not be opened.

Definition at line 141 of file shared_file.cpp.

◆ ~SharedFile()

ecl::SharedFile::~SharedFile ( )
virtual

Automatic cleaner for shared files.

Automatically cleans up and closes io handles.

Definition at line 151 of file shared_file.cpp.

Member Function Documentation

◆ count()

unsigned int ecl::SharedFile::count ( )
inline

Denotes how many instances are currently sharing this file.

Denotes how many instances are currently sharing this file.

Returns
unsigned int : the number of shared instances.

Definition at line 188 of file shared_file.hpp.

◆ error()

const Error& ecl::SharedFile::error ( ) const
inline

Definition at line 240 of file shared_file.hpp.

◆ flush()

bool ecl::SharedFile::flush ( )

Flush the internal buffer.

This writes to the shared file. Error handling the same for OFile's write function.

Exceptions
StandardException: throws from the underlying file if flushing returned an error [debug mode only].
See also
OFile

Definition at line 200 of file shared_file.cpp.

◆ open() [1/2]

bool ecl::SharedFile::open ( )
inline

Status flag indicating if the file is open/closed.

Returns
bool : true if open, false otherwise.

Definition at line 198 of file shared_file.hpp.

◆ open() [2/2]

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.

Parameters
name: name of the file to open.
mode: mode of writing, either New or Append.
Exceptions
StandardException: throws if the connection failed to open.
See also
OFile

Definition at line 161 of file shared_file.cpp.

◆ write() [1/2]

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.

Parameters
c: the character to write.
Returns
long : the number of bytes written.
Exceptions
StandardException: throws from the underlying file if writing returned an error [debug mode only].
See also
OFile

Definition at line 177 of file shared_file.cpp.

◆ write() [2/2]

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.

Parameters
s: points to the beginning of the character string.
n: the number of characters to write.
Returns
long: the number of bytes written.
Exceptions
StandardException: throws from the underlying file if flushing returned an error [debug mode only].
See also
OFile

Definition at line 187 of file shared_file.cpp.

Member Data Documentation

◆ buffer

devices::CharBuffer ecl::SharedFile::buffer
private

Definition at line 244 of file shared_file.hpp.

◆ shared_instance

devices::SharedFileCommon* ecl::SharedFile::shared_instance
private

Definition at line 243 of file shared_file.hpp.


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


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:45