Re: Re: [boost] Serialization Formal Review #2

Matthias Troyer
On Apr 13, 2004, at 9:42 PM, Robert Ramey wrote:
Making a new archive class is described in the section "New Archives - Implementation". The model for an archive class is:
////////////////////////////////////////////////////////////////////// /// // class trivial_iarchive - read serialized objects from a input text stream class trivial_iarchive
: public boost::archive::common_iarchive<trivial_iarchive> { // permit serialization system priledged access to permit // implementation of inline templates for maximum speed. friend class boost::serialization::load_access;
// member template for loading primitive types. // Override for any types/templates that special treatment template<class T> void load(T & t);
public: ////////////////////////////////////////////////////////// // public interface used by programs that use the // serialization library
// the >>> operator template<class T> trivial_iarchive & operator>>(T & t){ boost::serialization::load(* This(), t); return * This(); }
// archives are expected to support this function void load_binary(void *address, size_t count);
};
So in theory one has to implement the two undefined functions.
For each primitive data type, int, float, etc. or any type designated as primitive define a "load" template for function to read them from the
file.
For each data type that you want to treat specially - rather than allowing the serialization library to do the job - define an overload for operator.
Just a quick question: what should the default load (I mean template<class T>> void load(T & t);) function do? Do I guess correctly that it should never be called and is thus undefined to cause link-time errors?
It should handle all primitives not otherwise handled specifically. For example the default load for text archives resolves to input_stream >> t While for binary archives it resolves to load_binary(sizeof(t), t).
Looking through the documentation I find references to virtual functions:
And?
class trivial_iarchive
// all input archives must be derived from this base class. This base // class defines the virtual functions necessary for the serialization // library to handle special types for the serialization system.
I guess that these are incorrect comments left over from an old version?
Correct. Robert Ramey

Robert, I'm still fighting with implementing archives to see how I could use your library with our 10 years worth of simulation data. I think it should be doable but am trying to implement a very simple archive. Modifying the trivial archive example I thought that the following should work: #include <boost/archive/common_oarchive.hpp> #include <boost/serialization/vector.hpp> #include <boost/serialization/string.hpp> #include <iostream> class odump_archive : public boost::archive::common_oarchive<odump_archive> { public: odump_archive(std::ostream& dump) : os(dump) {} template<class T> odump_archive& operator<<(const T & t) { boost::serialization::save(* This(), t); return * This(); } // archives are expected to support this function void save_binary(const void *address, size_t count) { // do something later } private: std::ostream& os; friend class boost::archive::save_access; template<class T> void save(const T & t) { os << t;} }; int main() { odump_archive dump(std::cout); std::vector<int> v(4,5); std::string s("Hello, world!"); dump << v << s; } But this gives me the following compile time errors using g++ 3.1 on MacOS X: test.C: In member function `odump_archive& odump_archive::operator<<(const T&) [with T = std::vector<int, std::allocator<int> >]': test.C:33: instantiated from here test.C:14: error: no matching function for call to `save(odump_archive&, const std::vector<int, std::allocator<int> >&)' test.C: In member function `odump_archive& odump_archive::operator<<(const T&) [with T = std::string]': test.C:33: instantiated from here test.C:14: error: no matching function for call to `save(odump_archive&, const std::basic_string<char, std::char_traits<char>, std::allocator<char>
&)'
Can you help me by telling me what I'm doing incorrectly? Matthias

Matthias Troyer <troyer <at> itp.phys.ethz.ch> writes:
template<class T> odump_archive& operator<<(const T & t) { boost::serialization::save(* This(), t); return * This(); }
Try '{ boost::archive::save(* This(), t); return * This(); }' Matt
participants (3)
-
Matthew Vogt
-
Matthias Troyer
-
Robert Ramey