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
#include
#include
#include
#include
#include
#include
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 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
class my_variant_vector : public std::vector
//class my_variant_vector : public std::vector
//class my_variant_vector
{
private:
friend class boost::serialization::access;
std::vector 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 >(*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 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 myvar;
//std::vector 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@lapland:~/src/c++/src/testing$ cat integer_variant.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<which>0</which>
<value>100</value>
bob@lapland:~/src/c++/src/testing$ cat string_variant.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<which>1</which>
<value>One hundred</value>
bob@lapland:~/src/c++/src/testing$ cat vector_variant.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<myvec class_id="0" tracking_level="0" version="0">
<count>0</count>
0
</myvec>
bob@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?