vg
tools for working with variation graphs
|
#include <trivially_serializable.hpp>
Public Member Functions | |
TriviallySerializable ()=default | |
virtual | ~TriviallySerializable () |
TriviallySerializable & | operator= (const TriviallySerializable &other)=delete |
TriviallySerializable & | operator= (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) |
![]() | |
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... | |
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().
|
default |
Make a new TriviallySerializable. Should really only be called by constructors of inheriting types.
|
virtual |
Destroy a TriviallySerializable object and any associated memory mapping.
|
delete |
|
delete |
|
private |
Helper to close a file descriptor with error checking.
|
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.
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.
|
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.
|
finalprotectedvirtual |
Final implementation of deserialize_members for streams that loads the data.
Implements handlegraph::Serializable.
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.
|
private |
Helper to open a file descriptor with error checking.
|
delete |
|
delete |
|
finalvirtual |
Write the contents of this object to a named file. Makes sure to include a leading magic number.
Reimplemented from handlegraph::Serializable.
|
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.
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.
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.
|
finalvirtual |
Dump the magic number and user data to the given stream. Does not affect any backing file link.
Reimplemented from handlegraph::Serializable.
|
finalvirtual |
Dump the magic number and user data to the given stream. Does not affect any backing file link.
Reimplemented from handlegraph::Serializable.
|
private |
Helper function to set up output mappings to new files.
|
finalprotectedvirtual |
Final implementation of serialize_members for streams that dumps the data.
Implements handlegraph::Serializable.
|
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.
|
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.
|
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.
|
protected |
Get the number of bytes of user data. Does not count the magic number.
|
staticconstexprprivate |
How big is the magic number?
|
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.
|
private |
Where is it in memory? This includes the magic number!
|
private |
What file descriptor do we have to the file for resizing it? Will be -1 if we don't have one.
|
private |
How many bytes of associated data are there? This includes the magic number! Might not be accuate if serialization/deserialization throws.