This HowTo shows how to serialise data structures with cereal. Every SeqAn data structure which is is marked as cerealisable can be used.
Difficulty | Easy |
---|---|
Duration | 15 min |
Prerequisite tutorials | No prerequisites |
Recommended reading |
Storing and loading data, for example a FM-Index, is a common use case. Thanks to the cereal library doing so is incredible easy. This page will show you how to use cereal in SeqAn. As an example data structure we will use a std::vector
, but as already mentioned, any SeqAn data structure that is documented as cerealisable can be used.
Storing a data structure is as easy as using the cereal::BinaryOutputArchive
. In order to use it, you need to include
Note that stl data types, like std::vector
, are not automatically usable with cereal. You need to include the respective header, e.g.
In SeqAn, our data structures have integrated cereal support so there is no need to include an extra header.
Loading a data structure is as easy as using the cereal::BinaryInputArchive
. In order to use it, you need to include
In the example above loading and storing was encapsulated in separated functions. It is possible to use cereal::BinaryInputArchive
and cereal::BinaryOutputArchive
in one function, but then it is necessary to encapsulate each in an individual scope (using extra braces{}
). The reason for this is that the output/input stream handle of an archive is closed on deconstruction and only then is the file properly written an accessible by another filehandle.