[prev in list] [next in list] [prev in thread] [next in thread]
List: boost-users
Subject: Re: [Boost-users] [Serialization]Can not export polymorphic types
From: "Robert Ramey" <ramey () rrsd ! com>
Date: 2008-12-30 18:50:47
Message-ID: gjdqh3$lcj$1 () ger ! gmane ! org
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
"Daniel Roberts" <impulsionaudio@gmail.com> wrote in message =
news:14125aaf0812301023u33f8da07qd1f60c7724c707aa@mail.gmail.com...
That works. Some things I notice:
1) The xml file created has an attribute in the object tag called =
"class_name", containing the name of the derived type. This attribute =
does not exist in an xml file created using derived type pointers. I =
guess this is the critical piece of information needed to reconstruct a =
derived type using a base class pointer.
*** If an object is written through a derived type pointer, there is =
really no need for the "class_name" - it's redundant. Note that this =
also means that the "BOOST_CLASS_EXPORT" isn't necessary either. In =
general, archives hold that information - and ONLY that information =
required to (re)load the object.
2) It also works if you create a derived class pointer/object, =
static_cast the derived type pointer to a base class pointer and pass =
the base class pointer to the serialization.
*** correct, casting overrides the C++ typing system - that's the =
purpose of the cast. In general, you can serialized the derived type or =
the base type (which will retrieve the derived type information. What =
you CANNOT do is to save as one type and load as different type.
3) someInXMLArchive >> boost::serialization::make_nvp("", =
someTypePtr); works, which is surprising since I am passing an empty =
string for a tag. It appears serializing in ignores tag names and just =
processes data until the class is constructed or a stream error occurs.
***
The only function of the tag is to permit a name for the html tag. It =
has no purpose as far as serialization itself is concerned. As a useful =
aid in debugging, the starting/ending tags are matched be sure someone =
has munched up the xml file between the time the archive has been saved =
and loaded
4) Calling close on the output file stream (e.g. std::ofstream::close) =
causes the closing boost_serialization tag, </boost_serialization> to =
NOT be written to the file. Is this an issue that needs to be resolved =
or am I doing something else wrong?
***
The archive is "woundup" when it's destructor is called. If the =
underlying stream is closed before that happens, you won't get the last =
tag. What I recommend is to do something like:
{
std::fostream os("filename")
{
boost::archive::xml_archive xa(os);
xa << ....
// xa destructor finishs off the archive
}
// os.close called by os destructor
}
which guarentees that the archive destructor is called before the =
stream is closed.
Thanks a bunch for all the help. Hopefully I'll be running on my own =
now. Please feel free to comment on my observations above, however.
***
You're welcome. If you have a guilty conscience about consuming so =
much time, feel free to submit a "trouble shooting tips and techniques" =
to the serialzation documentation. Your submission can take the form of =
a TRAK item.
Robert Ramey
Regards,
Daniel Roberts
[Attachment #5 (text/html)]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.5730.13" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 \
2px solid; MARGIN-RIGHT: 0px"> <DIV>"Daniel Roberts" <<A
href="mailto:impulsionaudio@gmail.com">impulsionaudio@gmail.com</A>> wrote
in message <A
href="news:14125aaf0812301023u33f8da07qd1f60c7724c707aa@mail.gmail.com">news:14125aaf0812301023u33f8da07qd1f60c7724c707aa@mail.gmail.com</A>...</DIV>
<DIV>That works. Some things I notice:<BR><BR>1) The xml file created has an
attribute in the object tag called "class_name", containing the name of the
derived type. This attribute does not exist in an xml file created using
derived type pointers. I guess this is the critical piece of information
needed to reconstruct a derived type using a base class pointer.</DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>*** If an object is written through a derived
type pointer, there is really no need for the "class_name" - it's
redundant. Note that this also means that the "BOOST_CLASS_EXPORT" isn't
necessary either. In general, archives hold that information - and ONLY
that information required to (re)load the object.</FONT><BR><BR>2) It also
works if you create a derived class pointer/object, static_cast the derived
type pointer to a base class pointer and pass the base class pointer to the
serialization.</DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>*** correct, casting overrides the C++ typing
system - that's the purpose of the cast. In general, you can serialized
the derived type or the base type (which will retrieve the derived type
information. What you CANNOT do is to save as one type and load as
different type.</FONT><BR><BR>3) someInXMLArchive >>
boost::serialization::make_nvp("", someTypePtr); works, which is surprising
since I am passing an empty string for a tag. It appears serializing in
ignores tag names and just processes data until the class is constructed or a
stream error occurs.</DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>***</FONT></DIV>
<DIV><FONT face=Arial size=2>The only function of the tag is to permit a name
for the html tag. It has no purpose as far as serialization itself is
concerned. As a useful aid in debugging, the starting/ending tags are
matched be sure someone has munched up the xml file between the time the
archive has been saved and loaded</FONT><BR><BR>4) Calling close on the output
file stream (e.g. std::ofstream::close) causes the closing boost_serialization
tag, </boost_serialization> to NOT be written to the file. Is this an
issue that needs to be resolved or am I doing something else wrong?</DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>***</FONT></DIV>
<DIV><FONT face=Arial size=2>The archive is "woundup" when it's destructor is
called. If the underlying stream is closed before that happens, you
won't get the last tag. What I recommend is to do something
like:</FONT></DIV><FONT face=Arial size=2></FONT></BLOCKQUOTE>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 \
2px solid; MARGIN-RIGHT: 0px"> <DIV><FONT face=Arial size=2>{</FONT></DIV>
<DIV><FONT face=Arial size=2> std::fostream
os("filename")</FONT></DIV>
<DIV><FONT face=Arial size=2> {</FONT></DIV>
<DIV><FONT face=Arial size=2>
boost::archive::xml_archive xa(os);</FONT></DIV>
<DIV><FONT face=Arial size=2> xa <<
....</FONT></DIV>
<DIV><FONT face=Arial size=2> // xa
destructor finishs off the archive</FONT></DIV>
<DIV><FONT face=Arial size=2> }</FONT></DIV>
<DIV><FONT face=Arial size=2> // os.close called by os
destructor</FONT></DIV>
<DIV><FONT face=Arial size=2>}</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>which guarentees that the archive destructor is
called before the stream is closed.</FONT></DIV><FONT face=Arial
size=2></FONT>
<DIV><BR><BR>Thanks a bunch for all the help. Hopefully I'll be running on my
own now. Please feel free to comment on my observations above, however.</DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>***</FONT></DIV>
<DIV><FONT face=Arial size=2>You're welcome. If you have a guilty
conscience about consuming so much time, feel free to submit a "trouble
shooting tips and techniques" to the serialzation documentation. Your
submission can take the form of a TRAK item.</FONT></DIV><FONT face=Arial
size=2></FONT></BLOCKQUOTE>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 \
2px solid; MARGIN-RIGHT: 0px"> <DIV><FONT face=Arial size=2>Robert \
Ramey</FONT></DIV> <DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><BR><BR>Regards,<BR>Daniel Roberts<BR><BR></DIV></BLOCKQUOTE></BODY></HTML>
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic