Class SharedFile

Class Documentation

class SharedFile

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.

Public Functions

inline SharedFile()

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.

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.

Throws

StandardException – : throws if the file could not be opened.

virtual ~SharedFile()

Automatic cleaner for shared files.

Automatically cleans up and closes io handles.

bool 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.

See also

OFile

Parameters
  • name – : name of the file to open.

  • mode – : mode of writing, either New or Append.

Throws

StandardException – : throws if the connection failed to open.

inline unsigned int count()

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.

inline bool open()

Status flag indicating if the file is open/closed.

Returns

bool : true if open, false otherwise.

long 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.

See also

OFile

Parameters

c – : the character to write.

Throws

StandardException – : throws from the underlying file if writing returned an error [debug mode only].

Returns

long : the number of bytes written.

long 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.

See also

OFile

Parameters
  • s – : points to the beginning of the character string.

  • n – : the number of characters to write.

Throws

StandardException – : throws from the underlying file if flushing returned an error [debug mode only].

Returns

long: the number of bytes written.

bool flush()

Flush the internal buffer.

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

See also

OFile

Throws

StandardException – : throws from the underlying file if flushing returned an error [debug mode only].

inline const Error &error() const