vg
tools for working with variation graphs
Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
handlegraph::TriviallySerializable Class Reference

#include <trivially_serializable.hpp>

Inheritance diagram for handlegraph::TriviallySerializable:
handlegraph::Serializable

Public Member Functions

 TriviallySerializable ()=default
 
virtual ~TriviallySerializable ()
 
TriviallySerializableoperator= (const TriviallySerializable &other)=delete
 
TriviallySerializableoperator= (TriviallySerializable &&other)=delete
 
 TriviallySerializable (const TriviallySerializable &other)=delete
 
 TriviallySerializable (TriviallySerializable &&other)=delete
 
void dissociate ()
 
void serialize (std::ostream &out) const final
 
void serialize (std::ostream &out) final
 
void serialize (const std::string &filename) const final
 
void serialize (const std::string &filename) final
 
void deserialize (std::istream &in) final
 
void deserialize (const std::string &filename) final
 
void serialize (int fd) const
 
void serialize (int fd)
 
void deserialize (int fd)
 
- Public Member Functions inherited from handlegraph::Serializable
virtual ~Serializable ()=default
 
virtual uint32_t get_magic_number () const =0
 

Protected Member Functions

void serialized_data_resize (size_t bytes)
 
size_t serialized_data_size () const
 
char * serialized_data ()
 
const char * serialized_data () const
 
void serialize_members (std::ostream &out) const final
 
void deserialize_members (std::istream &in) final
 

Private Member Functions

void * serialize_and_get_mapping (int fd) const
 Helper function to set up output mappings to new files. More...
 
int open_fd (const std::string &filename) const
 Helper to open a file descriptor with error checking. More...
 
void close_fd (int fd) const
 Helper to close a file descriptor with error checking. More...
 

Private Attributes

size_t serializedLength = 0
 
void * serializedData = nullptr
 
int serializedFD = -1
 
size_t mappingFileSize = std::numeric_limits<size_t>::max()
 

Static Private Attributes

static constexpr int MAGIC_SIZE = sizeof(uint32_t) / sizeof(char)
 How big is the magic number? More...
 

Detailed Description

Interface for objects that can use identical in-memory and serialized representations.

The representation begins with the serialized 4-byte magic number, followed by user data. Length is implicit in either stream length or file size, and can be grown.

If serialization or deserialization throws, it is safe to destroy the object, but not to do anything else with it.

Modifying a file that an object has been loaded from or saved to, either on disk or through another associated object, is undefined behavior. To prevent modifications to an object from modifying the last file loaded or saved to, use dissociate().

Constructor & Destructor Documentation

◆ TriviallySerializable() [1/3]

handlegraph::TriviallySerializable::TriviallySerializable ( )
default

Make a new TriviallySerializable. Should really only be called by constructors of inheriting types.

◆ ~TriviallySerializable()

handlegraph::TriviallySerializable::~TriviallySerializable ( )
virtual

Destroy a TriviallySerializable object and any associated memory mapping.

◆ TriviallySerializable() [2/3]

handlegraph::TriviallySerializable::TriviallySerializable ( const TriviallySerializable other)
delete

◆ TriviallySerializable() [3/3]

handlegraph::TriviallySerializable::TriviallySerializable ( TriviallySerializable &&  other)
delete

Member Function Documentation

◆ close_fd()

void handlegraph::TriviallySerializable::close_fd ( int  fd) const
private

Helper to close a file descriptor with error checking.

◆ deserialize() [1/3]

void handlegraph::TriviallySerializable::deserialize ( const std::string &  filename)
finalvirtual

Sets the contents of this object to the contents of a serialized object from a file. The serialized object must be from the same implementation of this interface as is calling deserialize(). Can only be called on an empty object. If the file is a normal writeable file, future modifications to the object will affect the file until dissociate() is called or another normal file is associated.

Reimplemented from handlegraph::Serializable.

◆ deserialize() [2/3]

void handlegraph::TriviallySerializable::deserialize ( int  fd)

Sets the contents of this object to the contents of a serialized object from an open file descriptor. The serialized object must be from the same implementation of this interface as is calling deserialize(). Can only be called on an empty object If the file is a normal writeable file, future modifications to the object will affect the file until dissociate() is called or another normal file is associated.

Assumes that the file entirely belongs to this object.

◆ deserialize() [3/3]

void handlegraph::TriviallySerializable::deserialize ( std::istream &  in)
finalvirtual

Sets the contents of this object to the contents of a serialized object from an istream. The serialized object must be from the same implementation of the interface as is calling deserialize(). Can only be called on an empty object.

Reimplemented from handlegraph::Serializable.

◆ deserialize_members()

void handlegraph::TriviallySerializable::deserialize_members ( std::istream &  in)
finalprotectedvirtual

Final implementation of deserialize_members for streams that loads the data.

Implements handlegraph::Serializable.

◆ dissociate()

void handlegraph::TriviallySerializable::dissociate ( )

Break the write-back link between this object and the file it was loaded from, if any. Future modifications to the object will not affect the file, although future modifications to the file may still affect the object.

After this is called, serialized_data() may return a different address. Old pointers into user data are invalidated.

◆ open_fd()

int handlegraph::TriviallySerializable::open_fd ( const std::string &  filename) const
private

Helper to open a file descriptor with error checking.

◆ operator=() [1/2]

TriviallySerializable& handlegraph::TriviallySerializable::operator= ( const TriviallySerializable other)
delete

◆ operator=() [2/2]

TriviallySerializable& handlegraph::TriviallySerializable::operator= ( TriviallySerializable &&  other)
delete

◆ serialize() [1/6]

void handlegraph::TriviallySerializable::serialize ( const std::string &  filename) const
finalvirtual

Write the contents of this object to a named file. Makes sure to include a leading magic number.

Reimplemented from handlegraph::Serializable.

◆ serialize() [2/6]

void handlegraph::TriviallySerializable::serialize ( const std::string &  filename)
finalvirtual

Write the contents of this object to a named file. Makes sure to include a leading magic number. If the file is nonexistent or a normal file, future modifications to the object will affect the file until dissociate() is called or another normal file is associated.

Reimplemented from handlegraph::Serializable.

◆ serialize() [3/6]

void handlegraph::TriviallySerializable::serialize ( int  fd)

Write the contents of this object to an open file descriptor. Makes sure to include a leading magic number. If the file is a normal file, future modifications to the object will affect the file until dissociate() is called or another normal file is associated.

Assumes that the file entirely belongs to this object.

◆ serialize() [4/6]

void handlegraph::TriviallySerializable::serialize ( int  fd) const

Write the contents of this object to an open file descriptor. Makes sure to include a leading magic number.

Assumes that the file entirely belongs to this object.

◆ serialize() [5/6]

void handlegraph::TriviallySerializable::serialize ( std::ostream &  out) const
finalvirtual

Dump the magic number and user data to the given stream. Does not affect any backing file link.

Reimplemented from handlegraph::Serializable.

◆ serialize() [6/6]

void handlegraph::TriviallySerializable::serialize ( std::ostream &  out)
finalvirtual

Dump the magic number and user data to the given stream. Does not affect any backing file link.

Reimplemented from handlegraph::Serializable.

◆ serialize_and_get_mapping()

void * handlegraph::TriviallySerializable::serialize_and_get_mapping ( int  fd) const
private

Helper function to set up output mappings to new files.

◆ serialize_members()

void handlegraph::TriviallySerializable::serialize_members ( std::ostream &  out) const
finalprotectedvirtual

Final implementation of serialize_members for streams that dumps the data.

Implements handlegraph::Serializable.

◆ serialized_data() [1/2]

char * handlegraph::TriviallySerializable::serialized_data ( )
protected

Get the memory address of our serialized representation. Returns nullptr if we have not ever been loaded or resized.

Should be used by all implementations to get at their data. The magic number is hidden.

◆ serialized_data() [2/2]

const char * handlegraph::TriviallySerializable::serialized_data ( ) const
protected

Get the memory address of our serialized representation. Returns nullptr if we have not ever been loaded or resized.

Should be used by all implementations to get at their data. The magic number is hidden.

◆ serialized_data_resize()

void handlegraph::TriviallySerializable::serialized_data_resize ( size_t  bytes)
protected

Resize our serialized representation to the given number of bytes. Modifies any backing on-disk file.

Should be called by all mutable implementations when more (or fewer) bytes are needed. The magic number size does not count against the total length.

After this is called, serialized_data() may return a different address. Old pointers into user data are invalidated.

Untouched pages will not reserve any memory.

◆ serialized_data_size()

size_t handlegraph::TriviallySerializable::serialized_data_size ( ) const
protected

Get the number of bytes of user data. Does not count the magic number.

Member Data Documentation

◆ MAGIC_SIZE

constexpr int handlegraph::TriviallySerializable::MAGIC_SIZE = sizeof(uint32_t) / sizeof(char)
staticconstexprprivate

How big is the magic number?

◆ mappingFileSize

size_t handlegraph::TriviallySerializable::mappingFileSize = std::numeric_limits<size_t>::max()
private

Even if serializedFD is -1, our mapping may actually be to a file, mapped privately. We can't necessarily safely grow a mapping with mremap if it's to a smaller file. This holds the size of the file our mapping is to, or std::numeric_limits<size_t>::max() if the mapping isn't to a file.

◆ serializedData

void* handlegraph::TriviallySerializable::serializedData = nullptr
private

Where is it in memory? This includes the magic number!

◆ serializedFD

int handlegraph::TriviallySerializable::serializedFD = -1
private

What file descriptor do we have to the file for resizing it? Will be -1 if we don't have one.

◆ serializedLength

size_t handlegraph::TriviallySerializable::serializedLength = 0
private

How many bytes of associated data are there? This includes the magic number! Might not be accuate if serialization/deserialization throws.


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