Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-08-23 10:55:12


Among the default traits for primitive types is no tracking.

So the best way to handle your situation is to use
BOOST_STRONG_TYPEDEF to assign a special
type to your special kind of integer. Then specify
serialization functions for that type. Something like

BOOST_STRONG_TYPEDEF(int, my_int)

template<class Archive>
serialize(Archive &ar, my_int & i, const unsigned int version){
    ar & i;
}

vector<my_int *> v;
ar & v;

Hope this is helpful.

Robert Ramey

Cheok Yan Cheng wrote:
> hello,
>
> i have an int pointer, which pointer to the starting
> address of an array. currently, i need to serialize
> the array elements one by one, at the same time
> serialize the size of the array too.
>
> i try to serialize the dynamic allocated array, just
> like what i did on vector, but fail.
>
> vector<int> v;
> ar & vector; /* no problem. */
>
> int *i = new int[10];
> ar & i; /* compile error. */
>
> i was understand that the serialization library had no
> idea on the size of dynamic allocated array either
> during run-time or compiled time.
>
> i just want to know whether there is any better way to
> serialize on the above mentioned data?
>
> here is the error i get from vc++ 2003.
>
> C:\Boost\include\boost-1_33_1\boost\serialization\access.hpp(109):
> error C2228: left of '.serialize' must have
> class/struct/union type
>
> the below are my full source code. thanks!
>
> #include <fstream>
>
> // include headers that implement a archive in simple
> text format
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
>
> /////////////////////////////////////////////////////////////
> // gps coordinate
> //
> // illustrates serialization for a simple type
> //
> class gps_position
> {
> private:
> friend class boost::serialization::access;
> // When the class Archive corresponds to an output
> archive, the
> // & operator is defined similar to <<. Likewise,
> when the class Archive
> // is a type of input archive the & operator is
> defined similar to >>.
> template<class Archive>
> void serialize(Archive & ar, const unsigned int
> version)
> {
> ar & degrees;
> ar & minutes;
> ar & seconds;
> ar & data;
> }
> int degrees;
> int minutes;
> float seconds;
> int* data;
>
> public:
> gps_position(){};
> gps_position(int d, int m, float s) :
> degrees(d), minutes(m), seconds(s)
> {data = new int[5];}
> };
>
> int main() {
> // create and open a character archive for output
> std::ofstream ofs("filename");
>
> // create class instance
> const gps_position g(35, 59, 24.567f);
>
> // save data to archive
> {
> boost::archive::text_oarchive oa(ofs);
> // write class instance to archive
> oa << g;
> // archive and stream closed when destructors are
> called
> }
>
> // ... some time later restore the class instance
> to its orginal state
> gps_position newg;
> {
> // create and open an archive for input
> std::ifstream ifs("filename",
> std::ios::binary);
> boost::archive::text_iarchive ia(ifs);
> // read class state from archive
> ia >> newg;
> // archive and stream closed when destructors
> are called
> }
> return 0;
> }
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net