|
Boost Users : |
From: Robert Wilkinson (rwilkinson_at_[hidden])
Date: 2008-04-02 13:50:44
Hello
I have the following code, which compiles and runs but fails to
output the elements of the vector to the XML output.
#include <fstream>
#include <iostream>
#include <string>
#include "boost/variant.hpp"
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/variant.hpp>
#include <boost/serialization/vector.hpp>
class my_variant
{
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 & BOOST_SERIALIZATION_NVP(*this);
}
public:
boost::variant<int,std::string> var;
my_variant():var((int)0) {};
my_variant(const int & value):var(value) {}
my_variant(const std::string value):var(value) {}
my_variant(const char* value):var(value){}
~my_variant() {}; // make virtual if planning to use as base
my_variant & operator = (const int &value)
{
std::cerr << "VALUE is " << value << std::endl;
var = (value);
std::cerr << "VAR is " << var << std::endl;
return *this;
}
my_variant & operator = (const char* value)
{
std::cerr << "VALUE is " << value << std::endl;
var = (value);
std::cerr << "VAR is " << var << std::endl;
return *this;
}
friend std::ostream& operator << (std::ostream & os, const my_variant &var);
};
std::ostream & operator << (std::ostream & os, const my_variant &var ) { return os << var.var; }
//class my_variant_vector : public std::vector<my_variant>, my_variant
class my_variant_vector : public std::vector<my_variant>
//class my_variant_vector : public std::vector
//class my_variant_vector
{
private:
friend class boost::serialization::access;
std::vector<my_variant> my_vector;
// 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 & boost::serialization::base_object< std::vector<my_variant> >(*this);
ar & BOOST_SERIALIZATION_NVP(my_vector);
// ar & BOOST_SERIALIZATION_NVP(*this);
// for (unsigned int i = 0; i < my_vector.size(); ++i)
// {
// ar & BOOST_SERIALIZATION_NVP(my_vector[i]);
// }
}
public:
my_variant_vector() {}
my_variant_vector(std::vector<my_variant> tmp)
{
// my_vector = tmp;
for (unsigned int i = 0; i < tmp.size(); ++i)
{
my_vector.push_back(tmp[i]);
}
}
virtual ~my_variant_vector() {}
// void push_back(my_variant tmp) {my_vector.push_back(tmp);}
// int size() {return my_vector.size();}
};
int main() {
//boost::variant<int,std::string> myvar;
//std::vector<my_variant> myvec;
my_variant_vector myvec;
// const my_variant_vector & myvecref = myvec;
my_variant myvar;
myvar=100;
myvec.push_back(myvar);
std::cout << "Raw output " << myvar << std::endl;
for (unsigned int i = 0; i < myvec.size(); ++i)
{
std::cout << "I is " << i << " and Raw output " << myvec[i] << std::endl;
}
std::cout << "output done" << std::endl;
std::string filename;
filename = "integer_variant.xml";
// save data to archive
{
std::ofstream ofs(filename.c_str());
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(myvar.var);
}
myvar="One hundred";
myvec.push_back(myvar);
std::cout << "Raw output " << myvar << std::endl;
for (unsigned int i = 0; i < myvec.size(); ++i)
{
std::cout << "I is " << i << " and Raw output " << myvec[i] << std::endl;
}
filename = "string_variant.xml";
// save data to archive
{
std::ofstream ofs(filename.c_str());
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(myvar.var);
}
std::cout << "output done" << std::endl;
filename = "vector_variant.xml";
// save data to archive
{
std::ofstream ofs(filename.c_str());
assert(ofs.good());
boost::archive::xml_oarchive oa(ofs);
// oa << BOOST_SERIALIZATION_NVP(myvecref);
oa << BOOST_SERIALIZATION_NVP(myvec);
}
}
The XML files are :
bob_at_lapland:~/src/c++/src/testing$ cat integer_variant.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="4">
<myvar.var class_id="0" tracking_level="0" version="0">
<which>0</which>
<value>100</value>
</myvar.var>
</boost_serialization>
bob_at_lapland:~/src/c++/src/testing$ cat string_variant.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="4">
<myvar.var class_id="0" tracking_level="0" version="0">
<which>1</which>
<value>One hundred</value>
</myvar.var>
</boost_serialization>
bob_at_lapland:~/src/c++/src/testing$ cat vector_variant.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="4">
<myvec class_id="0" tracking_level="0" version="0">
<my_vector class_id="1" tracking_level="0" version="0">
<count>0</count>
<item_version>0</item_version>
</my_vector>
</myvec>
</boost_serialization>
bob_at_lapland:~/src/c++/src/testing$
What would I need to do to have the vector elements output in vector_variant.xml?
Bob
-- <hop> kb: I demand integrity and honesty in those who i do business with <hop> i know my demands are unreasonable, but a guy can dream, can't he?
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