[serialization] conditional serialization of members and base classes
Hi The logic I want to implement is: * b contains a * when saving b, save a, followed by other data * when loading b, load a into a temporary, if it matches b.m_a, load other data Would you expect the following code to work? class b { a m_a; // a is serializable vector<int> m_v; // other data template<class Archive> void save(Archive& ar, const unsigned version) const { ar & m_a; ar & m_v; } template<class Archive> void load(Archive& ar, const unsigned version) { a tmp; ar & tmp; if(tmp == m_a) ar & m_v; } // ... }; Now, what if b is derived from a instead of containing it? Would this work? class b : public a { vector<int> m_v; // other data template<class Archive> void save(Archive& ar, const unsigned version) const { ar & boost::serialization::base_object<a>(*this); ar & m_v; } template<class Archive> void load(Archive& ar, const unsigned version) { a tmp; ar & tmp; if(tmp == static_cast< const a& >(*this)) ar & m_v; } // ... }; Thanks!
An interesting case. But I don't see how this could be done without enhancing the library in some non-obvious way. . Then general problem is that the seialization library writes a serial stream of bytes. So you can't skip a piece without throwing things out of sync. But you don't know how long something is until it's serialized and then its too late. You might be able to do it for some special cases though Robert Ramey n.torrey.pines wrote:
Hi
The logic I want to implement is: * b contains a * when saving b, save a, followed by other data * when loading b, load a into a temporary, if it matches b.m_a, load other data
Would you expect the following code to work?
class b { a m_a; // a is serializable vector<int> m_v; // other data
template<class Archive> void save(Archive& ar, const unsigned version) const { ar & m_a; ar & m_v; }
template<class Archive> void load(Archive& ar, const unsigned version) { a tmp; ar & tmp; if(tmp == m_a) ar & m_v; }
// ... };
Now, what if b is derived from a instead of containing it? Would this work?
class b : public a { vector<int> m_v; // other data
template<class Archive> void save(Archive& ar, const unsigned version) const { ar & boost::serialization::base_object<a>(*this); ar & m_v; }
template<class Archive> void load(Archive& ar, const unsigned version) { a tmp; ar & tmp; if(tmp == static_cast< const a& >(*this)) ar & m_v; }
// ... };
Thanks!
Hi I'm not actually worried about throwing the rest of the archive out of sync, because the whole file will contain only one instance of "b". When b::m_a does not match what's in the archive, I'll just give up on loading and close the file. However, I'm worried about other issues, like the ones mentioned in the tutorial under "Derived Classes": versioning problems, etc. Can I serialize with boost::serialization::base_object and deserialize with "&" ? Thanks Robert Ramey wrote:
An interesting case. But I don't see how this could be done without enhancing the library in some non-obvious way. .
Then general problem is that the seialization library writes a serial stream of bytes. So you can't skip a piece without throwing things out of sync. But you don't know how long something is until it's serialized and then its too late.
You might be able to do it for some special cases though
Robert Ramey
n.torrey.pines wrote:
Hi
The logic I want to implement is: * b contains a * when saving b, save a, followed by other data * when loading b, load a into a temporary, if it matches b.m_a, load other data
Would you expect the following code to work?
class b { a m_a; // a is serializable vector<int> m_v; // other data
template<class Archive> void save(Archive& ar, const unsigned version) const { ar & m_a; ar & m_v; }
template<class Archive> void load(Archive& ar, const unsigned version) { a tmp; ar & tmp; if(tmp == m_a) ar & m_v; }
// ... };
Now, what if b is derived from a instead of containing it? Would this work?
class b : public a { vector<int> m_v; // other data
template<class Archive> void save(Archive& ar, const unsigned version) const { ar & boost::serialization::base_object<a>(*this); ar & m_v; }
template<class Archive> void load(Archive& ar, const unsigned version) { a tmp; ar & tmp; if(tmp == static_cast< const a& >(*this)) ar & m_v; }
// ... };
Thanks!
n.torrey.pines wrote:
Hi
I'm not actually worried about throwing the rest of the archive out of sync, because the whole file will contain only one instance of "b". When b::m_a does not match what's in the archive, I'll just give up on loading and close the file.
However, I'm worried about other issues, like the ones mentioned in the tutorial under "Derived Classes": versioning problems, etc. Can I serialize with boost::serialization::base_object and deserialize with "&" ?
Now, what if b is derived from a instead of containing it? Would this work?
class b : public a { vector<int> m_v; // other data
template<class Archive> void save(Archive& ar, const unsigned version) const { ar & boost::serialization::base_object<a>(*this); ar & m_v; }
template<class Archive> void load(Archive& ar, const unsigned version) { a tmp; ar & tmp; if(tmp == static_cast< const a& >(*this)) ar & m_v; throw ??? // abort serialization }
// ... };
I would expect the above to do what think you want. Robert Ramey
participants (2)
-
n.torrey.pines
-
Robert Ramey