Public Member Functions | Static Public Attributes | Protected Types | Protected Attributes | Static Protected Attributes | List of all members
vsl_b_ostream Class Reference

A binary output adaptor for any std::ostream. More...

#include <vsl_binary_io.h>

Inheritance diagram for vsl_b_ostream:
Inheritance graph
[legend]

Public Member Functions

 vsl_b_ostream (std::ostream *os)
 Create this adaptor using an existing stream. More...
 
std::ostream & os () const
 A reference to the adaptor's stream. More...
 
virtual ~vsl_b_ostream ()=default
 Virtual destructor. More...
 
bool operator! () const
 Returns true if the underlying stream has its fail bit set. More...
 
virtual void clear_serialisation_records ()
 Clear the stream's record of any serialisation operations. More...
 
virtual unsigned long add_serialisation_record (void *pointer, int other_data=0)
 Adds an object pointer to the serialisation records. More...
 
virtual unsigned long get_serial_number (void *pointer) const
 Returns a unique identifier for the object. More...
 
virtual int get_serialisation_other_data (void *pointer) const
 Set the user-defined data associated with the object. More...
 
virtual int set_serialisation_other_data (void *pointer, int other_data)
 Modify the user-defined data associated with the object. More...
 

Static Public Attributes

static constexpr std::streamoff header_length = 6
 The length of the b_stream header. More...
 

Protected Types

typedef std::map< void *, std::pair< unsigned long, int >, std::less< void * > > serialisation_records_type
 The type of the serialisation records. More...
 

Protected Attributes

std::ostream * os_
 The member stream. More...
 
serialisation_records_type serialisation_records_
 The serialisation records. More...
 

Static Protected Attributes

static constexpr unsigned short version_no_ = 1
 The version number of the IO scheme. More...
 

Detailed Description

A binary output adaptor for any std::ostream.

Currently the main use of this is to encourage streams to be opened in binary mode (ie. without CR/LF conversion)

This class also provide basic support for serialisation. This allows an object which has multiple pointers to it to be saved only once. During reloading, the pointers can be all set up again to point to the single object. vsl_b_ostream does not do the serialisation itself, but instead keeps records of unique identifiers to allow the user's code to perform serialisation safely. For instance, a smart pointer type object will have to know how to safely save whatever it is pointing to.

Definition at line 37 of file vsl_binary_io.h.

Member Typedef Documentation

◆ serialisation_records_type

typedef std::map<void *, std::pair<unsigned long, int>, std::less<void *> > vsl_b_ostream::serialisation_records_type
protected

The type of the serialisation records.

Definition at line 112 of file vsl_binary_io.h.

Constructor & Destructor Documentation

◆ vsl_b_ostream()

vsl_b_ostream::vsl_b_ostream ( std::ostream *  o_s)

Create this adaptor using an existing stream.

The stream (os) must be open (i.e. ready to receive insertions) so that the IO version and magic number can be written by this constructor. User is responsible for deleting os after deleting the adaptor

The stream (os) must be open (i.e. ready to be written to) so that the IO version number can be written by this constructor. User is responsible for deleting os after deleting the adaptor

Definition at line 208 of file vsl_binary_io.cxx.

◆ ~vsl_b_ostream()

virtual vsl_b_ostream::~vsl_b_ostream ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ add_serialisation_record()

unsigned long vsl_b_ostream::add_serialisation_record ( void *  pointer,
int  other_data = 0 
)
virtual

Adds an object pointer to the serialisation records.

Returns a unique identifier for the object.

pointer must be non-null, so you should handle null pointers separately.

You can optionally add some user-defined integer with each record If error checking is on, and the object pointer is null or already in the records, this function will abort()

Definition at line 248 of file vsl_binary_io.cxx.

◆ clear_serialisation_records()

void vsl_b_ostream::clear_serialisation_records ( )
virtual

Clear the stream's record of any serialisation operations.

Calling this function while outputting serialisable things to stream, will mean that a second copy of an object may get stored to the stream. Calling this function may be required if you change the state of your program whilst you are writing to the same vsl_b_ostream. If the program is at any risk of reallocating the same memory to two different objects controlled by a smart pointer, then calling this function between writing then will prevent them being confused.

Calling this function while outputting serialisable things to stream, will mean that a second copy of an object may get stored to the stream.

Definition at line 233 of file vsl_binary_io.cxx.

◆ get_serial_number()

unsigned long vsl_b_ostream::get_serial_number ( void *  pointer) const
virtual

Returns a unique identifier for the object.

Returns 0 if there is no record of the object.

Definition at line 260 of file vsl_binary_io.cxx.

◆ get_serialisation_other_data()

int vsl_b_ostream::get_serialisation_other_data ( void *  pointer) const
virtual

Set the user-defined data associated with the object.

If there is no record of the object, this function will return 0. However a retval of 0 does not necessarily imply that the object is unrecorded.

Definition at line 278 of file vsl_binary_io.cxx.

◆ operator!()

bool vsl_b_ostream::operator! ( ) const

Returns true if the underlying stream has its fail bit set.

Definition at line 224 of file vsl_binary_io.cxx.

◆ os()

std::ostream & vsl_b_ostream::os ( ) const

A reference to the adaptor's stream.

Definition at line 217 of file vsl_binary_io.cxx.

◆ set_serialisation_other_data()

int vsl_b_ostream::set_serialisation_other_data ( void *  pointer,
int  other_data 
)
virtual

Modify the user-defined data associated with the object.

If there is no record of the object, this function will abort.

Definition at line 295 of file vsl_binary_io.cxx.

Member Data Documentation

◆ header_length

constexpr std::streamoff vsl_b_ostream::header_length = 6
static

The length of the b_stream header.

You can move to this offset from the start of the file to get to the first real data item.

Definition at line 93 of file vsl_binary_io.h.

◆ os_

std::ostream* vsl_b_ostream::os_
protected

The member stream.

Definition at line 97 of file vsl_binary_io.h.

◆ serialisation_records_

serialisation_records_type vsl_b_ostream::serialisation_records_
protected

The serialisation records.

Records a pointer, a unique identifier, and an integer (user_defined data.)

Definition at line 117 of file vsl_binary_io.h.

◆ version_no_

constexpr unsigned short vsl_b_ostream::version_no_ = 1
staticprotected

The version number of the IO scheme.

Definition at line 120 of file vsl_binary_io.h.


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