|
|
Home » Developing U++ » U++ Developers corner » Xmlize values
Xmlize values [message #25441] |
Wed, 24 February 2010 00:12 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
Here the sample code for few types :
template<> void Xmlize(XmlIO xml, Value& var)
{
dword type;
if(xml.IsLoading())
{
xml.Attr("type", type);
switch(type)
{
case INT_V:
{
int i;
xml.Attr("value", i);
var = i;
break;
}
case DOUBLE_V:
{
double d;
xml.Attr("value", d);
var = d;
break;
}
case STRING_V:
{
String s;
xml.Attr("value", s);
var = s;
break;
}
case BOOL_V:
{
bool b;
xml.Attr("value", b);
var = b;
break;
}
case WSTRING_V:
case DATE_V:
case TIME_V:
case VALUE_V:
case VALUEARRAY_V:
case INT64_V:
case VOID_V:
case ERROR_V:
case VALUEMAP_V:
case UNKNOWN_V:
default:
NEVER();
break;
}
}
else
{
type = var.GetType();
xml.Attr("type", type);
switch(type)
{
case INT_V:
{
int i = var;
xml.Attr("value", i);
break;
}
case DOUBLE_V:
{
double d = var;
xml.Attr("value", d);
break;
}
case STRING_V:
{
String s = var;
xml.Attr("value", s);
break;
}
case BOOL_V:
{
bool b = var;
xml.Attr("value", b);
break;
}
case WSTRING_V:
case DATE_V:
case TIME_V:
case VALUE_V:
case VALUEARRAY_V:
case INT64_V:
case VOID_V:
case ERROR_V:
case VALUEMAP_V:
case UNKNOWN_V:
default:
NEVER();
break;
}
}
}
Ciao
Max
|
|
|
|
|
|
Re: Xmlize values [message #25501 is a reply to message #25496] |
Fri, 26 February 2010 12:27 |
|
koldo
Messages: 3361 Registered: August 2008
|
Senior Veteran |
|
|
luzr wrote on Fri, 26 February 2010 11:57 | I was thinking about Xmlize Value problem and I think this should be quite a fine solution:
First, Xmlize itself should know all Values that are in the Core (I guess we are there already, right?).
Then, add registering mechanism for unknown Values. Anyway, I think it would be better not to register any values automatically, because that would link all XML code into any application. So
Last but not least, for non-registered Values, use binary serialization and put a hex string there. Important - this should be signalled in the serialization so that even if type is registered later, old XML files can still be loaded.
What do you think?
Mirek
|
Hello Mirek
This is out of my knowledge. I am not strong in templates but, it would be great if Value Xmlize would use automatically the Xmlize method for every class.
I mean, this is a detail of actual implementation:
template<> void Xmlize(XmlIO xml, Value& var) {
dword t;
if(xml.IsLoading()) {
xml.Attr("type", t);
switch(t) {
case INT_V:
int i;
xml.Attr("value", i);
var = i;
break;
case DOUBLE_V:
double d;
xml.Attr("value", d);
var = d;
break;
... the same for all Value types
However it would be great to have something like this (pseudocode):
template<> void Xmlize(XmlIO xml, Value& var) {
dword t;
if(xml.IsLoading()) {
xml.Attr("type", t);
xml.Attr("value", var->v as type t); // force var to be as its type
} else {
...
Best regards
Iñaki
|
|
|
Re: Xmlize values [message #25503 is a reply to message #25496] |
Fri, 26 February 2010 12:37 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
luzr wrote on Fri, 26 February 2010 11:57 | I was thinking about Xmlize Value problem and I think this should be quite a fine solution:
First, Xmlize itself should know all Values that are in the Core (I guess we are there already, right?).
Then, add registering mechanism for unknown Values. Anyway, I think it would be better not to register any values automatically, because that would link all XML code into any application. So
Last but not least, for non-registered Values, use binary serialization and put a hex string there. Important - this should be signalled in the serialization so that even if type is registered later, old XML files can still be loaded.
What do you think?
Mirek
|
That would be the best, besides (maybe) some problems that can arise in binary serialization of unknown types, if types are non-POD ones (don't know if Value can store such types).
About the non-auto registering of all value types.... fine, but if you forget to register it you'll have it binary serialized, not very nice.
Maybe a conditional code part that does the registering only if XML code is included would be fine.
Ciao
Max
|
|
|
Re: Xmlize values [message #25504 is a reply to message #25503] |
Fri, 26 February 2010 13:12 |
|
mirek
Messages: 13980 Registered: November 2005
|
Ultimate Member |
|
|
mdelfede wrote on Fri, 26 February 2010 06:37 |
luzr wrote on Fri, 26 February 2010 11:57 | I was thinking about Xmlize Value problem and I think this should be quite a fine solution:
First, Xmlize itself should know all Values that are in the Core (I guess we are there already, right?).
Then, add registering mechanism for unknown Values. Anyway, I think it would be better not to register any values automatically, because that would link all XML code into any application. So
Last but not least, for non-registered Values, use binary serialization and put a hex string there. Important - this should be signalled in the serialization so that even if type is registered later, old XML files can still be loaded.
What do you think?
Mirek
|
That would be the best, besides (maybe) some problems that can arise in binary serialization of unknown types, if types are non-POD ones (don't know if Value can store such types).
|
Oh, I have meant only types that have binary serialization support in Value, of course...
Quote: |
About the non-auto registering of all value types.... fine, but if you forget to register it you'll have it binary serialized, not very nice.
|
True, but XML being backward compatible gives you oportunity to fix that later...
Quote: |
Maybe a conditional code part that does the registering only if XML code is included would be fine.
|
Maybe. Perhaps needs more thinking...
Ciao
Max
[/quote]
|
|
|
|
|
|
|
|
|
|
|
Re: Xmlize values [message #25551 is a reply to message #25549] |
Mon, 01 March 2010 10:53 |
Mindtraveller
Messages: 917 Registered: August 2007 Location: Russia, Moscow rgn.
|
Experienced Contributor |
|
|
luzr wrote on Mon, 01 March 2010 12:20 | But for plain text, it just seemed too weird to put it with attr:)
| May be not, if xmlizing text will require different call. Value may represent different types with the same interface, so should value's xmlization too. This will be right solution IMO.
[Updated on: Mon, 01 March 2010 10:53] Report message to a moderator
|
|
|
Re: Xmlize values [message #25553 is a reply to message #25549] |
Mon, 01 March 2010 11:08 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
luzr wrote on Mon, 01 March 2010 10:20 |
Frankly, there is no hard reason, except maybe estetics.
Well, maybe we should actually do without "value" attribute (putting all values as text between two tags). That would be more code in Xmlize.cpp.. (because for basic types, we like to have them as attributes too).
But for plain text, it just seemed too weird to put it with attr:)
Mirek
|
Ah, no need to change (please, don't do ! ), I was just curious.
Ciao
Max
|
|
|
Re: Xmlize values [message #25710 is a reply to message #25553] |
Tue, 09 March 2010 00:12 |
Mindtraveller
Messages: 917 Registered: August 2007 Location: Russia, Moscow rgn.
|
Experienced Contributor |
|
|
Recently I had runtime exception with this code when trying to load from XML file:
class XXXXX: public XXXXParent
{
public:
virtual void Xmlize(XmlIO &xml)
{
XXXXParent::Xmlize(xml);
xml.Attr("id", id);
xml ("v", v); // <-- exception here!!
}
private:
String id;
Value v;
};
file contained these tags:
Quote: | <element type="2" i="5404" x="24" y="17" l="20" dir="0" dir2="-1" input0="4587" input1="0" link0="5405" link1="0" id="asd">
<v type="String">234234234234234</v>
</element>
|
[Updated on: Tue, 09 March 2010 00:47] Report message to a moderator
|
|
|
Re: Xmlize values [message #25716 is a reply to message #25710] |
Tue, 09 March 2010 09:35 |
|
mirek
Messages: 13980 Registered: November 2005
|
Ultimate Member |
|
|
Mindtraveller wrote on Mon, 08 March 2010 18:12 | Recently I had runtime exception with this code when trying to load from XML file:
class XXXXX: public XXXXParent
{
public:
virtual void Xmlize(XmlIO &xml)
{
XXXXParent::Xmlize(xml);
xml.Attr("id", id);
xml ("v", v); // <-- exception here!!
}
private:
String id;
Value v;
};
file contained these tags:
Quote: | <element type="2" i="5404" x="24" y="17" l="20" dir="0" dir2="-1" input0="4587" input1="0" link0="5405" link1="0" id="asd">
<v type="String">234234234234234</v>
</element>
|
|
What about to tell us about XXXParent::Xmlize? Or kind of runtime exception?
Full testcase would be highly appreciated!
Mirek
[Updated on: Tue, 09 March 2010 09:37] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Wed May 15 04:43:05 CEST 2024
Total time taken to generate the page: 0.03098 seconds
|
|
|