对象序列化 [英] object serialization

查看:83
本文介绍了对象序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个示例,该示例将展示如何使用任何其他api来最简单地序列化c ++

对象。我有一个班级

我想要序列化,然后传递给我的obj-c课程,这样我就可以通过网络发送



我正在寻找如何序列化它,然后将它打包回

另一端。


任何帮助都很多赞赏。

I''m looking for an example that would show how to serialize a c++
object at it''s simplest w/o using any other api''s. I have a class that
I want to serialize and then pass to my obj-c class so I can send it
over the wire.

I''m just looking for how to serialize it, then pack it back up on the
other end.

Any help much appreciated.

推荐答案




我就是这样做的:


从ISerialize类派生每个类。

这个ISerialize类有一个成员接受IArchive即序列化(

IArchive& Archive) 。

ISerialize类有一个虚函数,它返回它的名字(在派生类中重写

以返回类名)。该名称用于

反序列化正确的类。


IArchive函数知道它是否可以读取或写入。

IArchive类有两个虚拟成员读取(char *,长度长)和一个

类似的write()。

IArchive还包含所有常规类型的非虚函数喜欢

string long int(使用模板等来减少这些功能)。

另外这个IArchive可以序列化/反序列化ISerialize派生的

类首先存储名称,然后调用类'''序列化

函数传递自己。在反序列化时,它首先读取类名

(它知道这是一个ISerialize类,而不是像long int等其他类型。

,因为你传递一个指针或引用它。)

然后它创建对象(查找''对象工厂''这是非常标准的

C ++通过名称或id创建对象的方式)并调用序列化成员

对象传递给自己。


ISerialize的Serialize成员通常派生(我的代码片段)


void MCursor :: MCursorInfo :: Serialize(MArchive& Archive)

{

Archive.Serialize(Event);

Archive.Serialize (OffsetX);

Archive.Serialize(OffsetY);

Archive.Serialize(动画);

}

注1,由于存档知道它是在读还是写,所以可以在读或写函数之间选择


注2因为你可以重载读写函数你可以创建

派生函数来轻松存储到内存磁盘,套接字等。你只需要

来重载非常简单的读写函数。

注3由于(de)/序列化是用一个函数完成的,所以总是在

sync(不可能在读写之间造成不匹配)。


当这项工作扩展你的档案以跟踪书面指示

ISerailize对象(因此它只会在反序列化时有一些对象指向一个对象时创建一个对象)。

并添加一些东西来自动保存STL向量/集/地图等

ISerialize等


设置可能需要一些时间,但你拥有它,它确实像

a魅力:-)和很多乐趣,看看它有多么容易。我用它来保存我的2D游戏引擎内部状态(savegame)并在

内存中加载相同的状态(反序列化)。


要学习的东西:

对象工厂

在MSDN上查找microsofts序列化的方式(我从他们那里偷了重复的

指针的想法:-))

确保你有合理的模板

问候,Ron AF Greve

http://www.InformationSuperHighway.eu


" William" < Wi ******* @ gmail.comwrote in message

news:11 ********************** @ d57g2000hsg.googlegr oups.com ...
Hi,

This is how I do it:

Derive every class from an ISerialize class.
This ISerialize class has one member accepting an IArchive i.e Serialize(
IArchive& Archive ).
The ISerialize class has a virtual function that returns it name (overridden
in the derived classes to return the class name). The name is used to
deserialize the correct class.

The IArchive function knows whether it is open for reading or writing.
The IArchive class has two virtual members read( char *, long length ) and a
similar write().
The IArchive also contains non virtual functions for all regular types like
string long int (use templates etc to reduce these to a few functions).
In addition this IArchive can serialize/deserialize ISerialize derived
classes by first storing there name and then calling the class''es Serialize
function passing itself. On deserialization it first reads the class name
(it knows this is a ISerialize class and not another type like long int etc.
because you pass a pointer or reference to it.)
Then it creates the object (lookup ''object factory'' this is pretty standard
C++ way of creating objects by name or id) and calls the serialize member of
the object passing itself.

The Serialize member of a ISerialize derived typically (piece of my code)

void MCursor::MCursorInfo::Serialize( MArchive& Archive )
{
Archive.Serialize ( Event );
Archive.Serialize ( OffsetX );
Archive.Serialize ( OffsetY );
Archive.Serialize ( Animation );
}
Note1 that since the archive knows whether it is reading or writing it can
choose between the read or write function.
Note2 Because you can overload the read and write function you can create
derived function to store to memory disks, sockets etc easily. You only have
to overload the very simplistic read and write function.
Note3 Because (de)/serialization is done with one function it always is in
sync (no possibility to make a mismatch between read and write).

When this works extend your archive to keep track of written pointers to
ISerailize objects (so it only creates one object when it there are several
pointers around to one object on deserialization).
and add some stuff to automatically save STL vectors/sets/maps etc of
ISerialize etc

It might take some time to setup but ones you have it, it really works like
a charm :-) and loads of fun to see how easy it works then. I use it to save
my 2D game engines internal state (savegame) and load the same state back in
memory (deserialization).

Things to study:
Object factories
Lookup microsofts way of serialization on MSDN ( I stole the duplicate
pointer idea from them :-) )
Make sure your are reasonably aquainted with templates
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"William" <Wi*******@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...

我正在寻找一个展示如何序列化c ++

对象的例子在最简单的情况下,不使用任何其他api'。我有一个班级

我想要序列化,然后传递给我的obj-c课程,这样我就可以通过网络发送



我正在寻找如何序列化它,然后将它打包回

另一端。


任何帮助都很多赞赏。
I''m looking for an example that would show how to serialize a c++
object at it''s simplest w/o using any other api''s. I have a class that
I want to serialize and then pass to my obj-c class so I can send it
over the wire.

I''m just looking for how to serialize it, then pack it back up on the
other end.

Any help much appreciated.



4月20日上午8:24,Ron AF Greve < ron @ localhostwrote:
On Apr 20, 8:24 am, "Ron AF Greve" <ron@localhostwrote:

void MCursor :: MCursorInfo :: Serialize(MArchive& Archive)

{

Archive.Serialize(Event);

Archive.Serialize(OffsetX);

Archive.Serialize(OffsetY);

Archive.Serialize (动画);


}
void MCursor::MCursorInfo::Serialize( MArchive& Archive )
{
Archive.Serialize ( Event );
Archive.Serialize ( OffsetX );
Archive.Serialize ( OffsetY );
Archive.Serialize ( Animation );

}



此方法无法解决一个额外的复杂问题。随着文件格式的改变你需要能够用

更新的软件阅读旧版本。


有几个这样做的方法。我过去使用的方式是

,每个对象也会写一个模式编号,然后可以用来确定要使用哪个结构的
。你可能会得到一些看起来更像这样的东西:


void MCursor :: MCursorInfo :: Serialize(MArchive& Archive)

{

int version = Archive.Version< MCursorInfo>(2);

if(version> = 1){

Archive.Serialize(Event);

Archive.Serialise (OffsetX);

Archive.Serialise(OffsetY);

}

if(version> = 2)

Archive.Serialise(动画);

}


您想安排版本返回版本为

使用。通过传递类型(我已经通过模板完成了

专业化,但还有其他方法)它可以存储查询

用于整体文件版本每个架构部分,以便您还可以将
保存为较旧的文件格式。

K

There is one extra complication that this approach doesn''t address. As
the file format changes you need to be able to read old versions with
newer software.

There are a few ways of doing this. The way I''ve used in the past is
that each object also writes a schema number which can then be used to
work out which structure to use. You may get something that looks a
little more like this:

void MCursor::MCursorInfo::Serialize( MArchive& Archive )
{
int version = Archive.Version< MCursorInfo >( 2 );
if ( version >= 1 ) {
Archive.Serialize( Event );
Archive.Serialise( OffsetX );
Archive.Serialise( OffsetY );
}
if ( version >= 2 )
Archive.Serialise( Animation );
}

You want to arrange for Version to return the version that is to be
used. By passing in the type (I''ve done it via a template
specialisation, but there are other ways too) it can store a lookup
for overall file version against each schema part so that you can also
save to older file formats.
K


4月20日凌晨2:49,William< Wizumw ... @ gmail.comwrote:
On Apr 20, 2:49 am, William <Wizumw...@gmail.comwrote:

我正在寻找一个展示如何序列化一个c ++

对象,这是最简单的,没有使用任何其他api'。我有一个班级

我想要序列化,然后传递给我的obj-c课程,这样我就可以通过网络发送它。

I''m looking for an example that would show how to serialize a c++
object at it''s simplest w/o using any other api''s. I have a class that
I want to serialize and then pass to my obj-c class so I can send it
over the wire.


我正在寻找如何序列化它,然后将它打包回

另一端。
I''m just looking for how to serialize it, then pack it back up on the
other end.



遗憾的是,没有简单的答案。基本上,你需要自己定义一个行协议,或者使用一个现有的协议,然后编写你用来符合

行协议。


如果没有其他特殊限制,我会从XDR开始

为低级别类型,并且建立在它上面。我可能会定义一个

oxdrstream和一个ixdrstream,<<和>

基元类型的运算符。 (处理整数很简单。浮点

不那么;取决于你想要编码的可移植性,它甚至可以非常复杂。)更复杂的类型然后输出每个字段。

(还必须考虑可变长度类型,

例如矢量和字符串.XDR有一些基本规则,因为

。如果指向

数据在逻辑上是你对象的一部分,也不要忘记跟踪指针。 (你不能,当然,

序列化一个指针。)


你还需要考虑接收端如何
将知道它的类型。根据协议的不同,

这可能或多或少是隐含的,但大多数情况下,这将是你需要传输这些信息的情况。

well。


-

James Kanze(GABI Software)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l' 'école,法国,+ 33(0)1 30 23 00 34

There are, regretfully, no simple answers. Basically, you''ll
have to either define a line protocol yourself, or use an
existing one, then code every type you use to conform to the
line protocol.

If there are no other particular constraints, I''d start with XDR
for the low level types, and build on it. I''d probably define a
oxdrstream and an ixdrstream, with << and >operators for the
primitive types. (Handling integers is easy. Floating point
less so; depending on how portable you want to code, it can even
be very complex.) More complex types then output each field.
(Some consideration must also be given to variable length types,
e.g. vectors and strings. XDR has some basic rules for these as
well.) And don''t forget to follow pointers, if the pointed to
data is logically part of your object. (You cannot, of course,
serialize a pointer.)

You''ll also have to give some thought to how the receiving end
will know what type it is getting. Depending on the protocol,
this may be more or less implicit, but most of the times, there
will be cases where you''ll have to transmit this information as
well.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于对象序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆