So I understand you have something like this (that's not really a 3D array, post the exact code you want to serialise if I misread you):
struct Tile {
int id, passability;
};
std::vector<Tile> tileList;
Because std::vector
is already serialisable through standard Boost.Serialization facilities, you only need to make Tile
serialisable. The easiest way is to add a serialize
member that does both the loading and saving.
struct Tile {
int id, passability;
template <typename Archive>
void serialize(Archive& ar, const unsigned version) {
ar & id;
ar & passability;
}
};
Aand that's about it, your type is now serialisable. Special things to be considered include class members being private — you need to add friend declaration for Boost.Serialization in this case, i.e.
class Tile {
int id, passability;
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive& ar, const unsigned version) {
ar & id;
ar & passability;
}
public:
Tile(int, int);
};
serialize
member have to be a template (well, not really; but until you know the library a bit better, it have to be), so its entire body must be included within the class declaration (unless you use explicit instantiation, but that's also not something for beginners).
You can also split it into save
and load
members, but that's not useful until you start using versioning. It would look like this:
struct Tile {
int id, passability;
BOOST_SERIALIZATION_SPLIT_MEMBER()
template <typename Archive>
void save(Archive& ar, const unsigned version) { ... }
template <typename Archive>
void load(Archive& ar, const unsigned version) { ... }
};
That makes the type held by the vector serialisable. Serialising a vector itself is just archive & tileList
.