473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problem with serialization

Can someone take a look at this code and figure out why
Serializable_base::add_serializer throws std::bad_alloc. The problem
seems to be the compiler because msvc++ 7.1 says
c:\LibSerial\LibSerial.cpp(83) : warning C4584: 'serial::E' :
base-class 'serial::A' is already a base-class of 'serial::C'
c:\LibSerial\LibSerial.cpp(10) : see declaration of
'serial::A'
c:\LibSerial\LibSerial.cpp(48) : see declaration of
'serial::C'
#include <vector>
#include <iostream>

//#include "Archive.h"
//
// Archive is an abstract class that defines write(int), write(float)
etc.
// But for our purposes it can be just defined to be
//
namespace serial {

class Archive { };

template<class T>
inline Archive& operator&=(Archive& ar, T& x)
{
return ar;
}

}

namespace serial {

class Serializable_base;
class Serial_holder;

// Serialize_base will store a vector of
// Serialize_action_base objects which
// will then be called back in predefined order
// at serialization
class Serialize_action_base
{
public:
virtual void serialize(Archive& ar) = 0;
};

// This should call the serialize method of a class T
template<typename T>
class Serialize_action : public Serialize_action_base
{
T* sc;
public:
Serialize_action() : sc(0)
{ }

void set_obj(T* serial)
{
sc = serial;
}

void serialize(Archive& ar)
{
if (sc != 0)
{
sc->T::serialize(ar);
}
}
};

// Serializable virtually inherits from this class to retrieve
// a unique id
class Serializable_base
{
friend class Serial_holder;
public:
Serializable_base()
{
std::cout << "Serializable_base()" << std::endl;
}

virtual ~Serializable_base()
{
std::cout << "Serializable_base destroyed" << std::endl;
}
protected:
// this is where std::bad_alloc is thrown
void add_serializer(Serialize_action_base* serializer)
{
serializers.push_back(serializer);
}
private:
typedef std::vector<Serialize_action_base*> serializer_actions;
serializer_actions serializers;
void serialize_all(Archive& ar)
{
std::cout << "num serializers = " << serializers.size() <<
std::endl;
for (size_t i = 0; i < serializers.size(); ++i)
serializers[i]->serialize(ar);
}
};
// T is the class that inherits from this class
template<typename T>
class Serializable : public virtual Serializable_base
{
typedef T Base;

friend class Serialize_action;

// This serialize method function should be invoked within the
// Serialize_action object. It should just call the class
// who derives from this class serialize method, which
// means that any class that we should be able to make a
// given class Serializable by writing
//
// class C : public Serializable<C>
// {
// void serialize(Archive& ar) { }
// };
void serialize(Archive& ar)
{
Base* base = dynamic_cast<Base*>(this);
if (base)
base->serialize(ar);
}
public:
Serializable()
: Serializable_base()
{
std::cout << "Serializable()" << std::endl;
sa.set_obj(this);
add_serializer(&sa);
}
private:
Serialize_action<Serializable<T> > sa;
};

class Serial_holder
{
public:
Serial_holder(Serializable_base* r)
: root(r)
{ }
void serialize(Archive& ar)
{
root->serialize_all(ar);
}
private:
Serializable_base* root;
};

}

#endif

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

// Do some tests of the serialize classes
#include "LibSerial.h"
#include <iostream>
using namespace serial;
class A : public Serializable<A>
{
int a;
int b;
int c;
public:
A() : a(1), b(2), c(3) { }

~A()
{
std::cout << "A destroyed" << std::endl;
}

void serialize(Archive& ar)
{
std::cout << "Serializing A" << std::endl;
ar &= a;
ar &= b;
ar &= c;
}
};

class B : public Serializable<B>
{
int a;
int b;
public:
B() : a(4), b(5) { }

void serialize(Archive& ar)
{
std::cout << "Serializing B" << std::endl;
ar &= a;
ar &= b;
}

};

class C : public A, public B, public Serializable<C>
{
int c;
int d;
public:
C() : c(6), d(7) { }

void serialize(Archive& ar)
{
std::cout << "Serializing C" << std::endl;
ar &= c;
ar &= d;
}
};

class D : public B, public A, public Serializable<D>
{
int e;
int f;
int g;
public:
D() : e(4), f(5), g(6)
{

}

void serialize(Archive& ar)
{
std::cout << "Serializing D" << std::endl;
ar &= e;
ar &= f;
ar &= g;
}
};

class E : public A, public C, public Serializable<E>
{
int e;
public:
E() : e(5) { }

void serialize(Archive& ar)
{
std::cout << "Serializing E" << std::endl;
ar &= e;
}
};

/// T is the class that were're testing
template<class T>
void do_test(serial::Archive& ar)
{
T c;

std::cout << "testing " << typeid(c).name() << std::endl;

Serial_holder sh(&c);
sh.serialize(ar);
}

int main()
{
serial::Archive ar;
do_test<E>(ar);

return 0;
}
Jul 22 '05 #1
0 1469

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David K | last post by:
we are having a problem when trying to serilalize and deserialize DataTable When replacing the container to be ListArray or HashTable everything works fine The code is very straight forward (...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
4
by: james | last post by:
I upgraded my 2002 project to 2003 and I am getting several of there errors on my forms xxxForm.resx Resource transformation for file 'xxxForm.resx' failed. Possible Version mismatch. Type...
2
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have...
0
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem...
0
by: ZippyV | last post by:
Hello everybody, I'm trying to convert a given xsd scheme to vb.net code with the help of xsd.exe. I've had no problems during conversion but the problem is that I can't build the code in vs.net....
2
by: yqlu | last post by:
I hava developed a client in C# that is connected to a 3-party XML Web Services developed in Java based on the AXIS 1.1. Most methods call are successful except for one method named "findObjects"...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
2
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.