473,714 Members | 4,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with serialization

Can someone take a look at this code and figure out why
Serializable_ba se::add_seriali zer throws std::bad_alloc. The problem
seems to be the compiler because msvc++ 7.1 says
c:\LibSerial\Li bSerial.cpp(83) : warning C4584: 'serial::E' :
base-class 'serial::A' is already a base-class of 'serial::C'
c:\LibSerial\Li bSerial.cpp(10) : see declaration of
'serial::A'
c:\LibSerial\Li bSerial.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&=(Arch ive& ar, T& x)
{
return ar;
}

}

namespace serial {

class Serializable_ba se;
class Serial_holder;

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

// This should call the serialize method of a class T
template<typena me T>
class Serialize_actio n : public Serialize_actio n_base
{
T* sc;
public:
Serialize_actio n() : sc(0)
{ }

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

void serialize(Archi ve& ar)
{
if (sc != 0)
{
sc->T::serialize(a r);
}
}
};

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

virtual ~Serializable_b ase()
{
std::cout << "Serializable_b ase destroyed" << std::endl;
}
protected:
// this is where std::bad_alloc is thrown
void add_serializer( Serialize_actio n_base* serializer)
{
serializers.pus h_back(serializ er);
}
private:
typedef std::vector<Ser ialize_action_b ase*> serializer_acti ons;
serializer_acti ons serializers;
void serialize_all(A rchive& ar)
{
std::cout << "num serializers = " << serializers.siz e() <<
std::endl;
for (size_t i = 0; i < serializers.siz e(); ++i)
serializers[i]->serialize(ar );
}
};
// T is the class that inherits from this class
template<typena me T>
class Serializable : public virtual Serializable_ba se
{
typedef T Base;

friend class Serialize_actio n;

// This serialize method function should be invoked within the
// Serialize_actio n 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(Archi ve& ar) { }
// };
void serialize(Archi ve& ar)
{
Base* base = dynamic_cast<Ba se*>(this);
if (base)
base->serialize(ar );
}
public:
Serializable()
: Serializable_ba se()
{
std::cout << "Serializable() " << std::endl;
sa.set_obj(this );
add_serializer( &sa);
}
private:
Serialize_actio n<Serializable< T> > sa;
};

class Serial_holder
{
public:
Serial_holder(S erializable_bas e* r)
: root(r)
{ }
void serialize(Archi ve& ar)
{
root->serialize_all( ar);
}
private:
Serializable_ba se* 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(Archi ve& ar)
{
std::cout << "Serializin g 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(Archi ve& ar)
{
std::cout << "Serializin g 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(Archi ve& ar)
{
std::cout << "Serializin g 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(Archi ve& ar)
{
std::cout << "Serializin g 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(Archi ve& ar)
{
std::cout << "Serializin g 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 1486

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

Similar topics

4
2112
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 ( see below), however when comparing the DataTable before and after the serialize, it is not the same The DataTable after the serialization contains only the string of class type ( Vclass in this class) but not the class itself Any idea ?
1
4361
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 done this before but this time I have no idea why I am getting the error. Any ideas ?? THANKS ! <qcsttatus> <manual sourcerootfolder="" manualname="">
4
1796
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 System.Globalization.DateTimeFormatInfo has 38 members, number of members deserialized is 35. Now, when I open the .resx files I do not find any DateTimeFOrmatInfo members in the XML or in the code.
2
2354
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 a class that inherits from dataset, and I want to serialize it , so I created a serialization constructor that forwards the call to the base class (the dataset) serialization constructor, normally, for this action to succeed I am supposed to put...
0
2498
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 serializing the object but there seems to be a problem serializing an array of objects. Any help will be appreciated "Cannot assign object of type System.Object to an object of type ElectronicWallet.C2PTest.PaymentItem." :...
0
910
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. I keep getting the error: "Reference to a non-shared object requires an object reference". The weird thing is that this problem doesn't occur in every Class in that vb-file. I don't have a clue how to fix this problem. Here is one of the...
2
2756
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" and return a complex type "FieldSearchResult". The error message as following : "Cannot assign object of type System.String to an object of type System.String. There is an error in XML document (23, 97)." By the way,I hava written a client in Java...
5
19596
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
2
1628
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 this. So I wrote the following code to serialize\deserialize the object, but now I have the problem that the object has a generic list and when I'm trying to deserialize it, I get an error. <System.Serializable()Public Class TestClass
2
5564
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 I am trying to read from a binary archive using Boost serialization. I am new to this, and it is possible that I have not understood the usage. In the code below, the string "faultblock" seems to be causing the problem. The code crashes in the ia...
0
8712
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9316
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9179
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5959
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3160
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.