Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » Xmlize values
Xmlize values [message #25441] Wed, 24 February 2010 00:12 Go to next message
mdelfede is currently offline  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 #25442 is a reply to message #25441] Wed, 24 February 2010 00:20 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
For Koldo, a sample ArrayCtrl xmlized with some processing added on load/store.... just a small part of my app.

void GenericLoads::Xmlize(XmlIO xml)
{
	Vector<Vector<Value> >vc;
	Vector<Vector<Value> >vl;
	xml("DAN", DANName);
	if(xml.IsStoring())
	{
		for(int iCond = 0; iCond < conds.GetCount(); iCond++)
			vc.Add(conds.ReadRow(iCond));
		xml("Conds", vc);
		for(int iLoad = 0; iLoad < loads.GetCount(); iLoad++)
			vl.Add(ReadLoadLine(iLoad));
		xml("Loads", vl);
	}
	else
	{
		// setup measurement units reading them
		// from global settings
		distribUM = globalSettings().GetUnitaMisura().Distribuiti;
		concentrUM = globalSettings().GetUnitaMisura().Concentrati;
		lengthUM = globalSettings().GetUnitaMisura().Lunghezze;
		
		xml("Conds", vc);
		xml("Loads", vl);
		conds.SetCount(vc.GetCount());
		loads.SetCount(vl.GetCount());
		PropagateDAN(DANName);
		for(int iCond = 0; iCond < vc.GetCount(); iCond++)
			conds.Set(iCond, vc[iCond]);
		SyncConds();
		for(int iLoad = 0; iLoad < vl.GetCount(); iLoad++)
			WriteLoadLine(iLoad, vl[iLoad]);
		
		// synchronize labels
		SyncLoads();
	}
}
Re: Xmlize values [message #25449 is a reply to message #25442] Wed, 24 February 2010 12:14 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Great Massimo

It is true that Value Xmlize implementation can be improved, but it is outside my knowledge Sad.

Based in your code I have prepared Xmlize for GridCtrl so now it is possible to serialize a class with a GridCtrl inside with just this:

void MyClass::Xmlize(XmlIO xml) {
	xml
		("grid", myGrid)
	;
}


It is very simple Smile

Unodgs: Could you include this en GridCtrl ?. From inside the class it would be much more efficient.


Best regards
Iñaki
Re: Xmlize values [message #25496 is a reply to message #25449] Fri, 26 February 2010 11:57 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
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
Re: Xmlize values [message #25501 is a reply to message #25496] Fri, 26 February 2010 12:27 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
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 Go to previous messageGo to next message
mdelfede is currently offline  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 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
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 #25534 is a reply to message #25504] Sun, 28 February 2010 15:08 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK, all is implemented now, including a new reference/XmlizeCustomValue example.

As for the problem of registering non-Core Values, I have found only one Value compatible type where this is a sort of problem: Font. I think I can live with that for now...

Mirek
Re: Xmlize values [message #25535 is a reply to message #25534] Sun, 28 February 2010 15:44 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
luzr wrote on Sun, 28 February 2010 15:08

OK, all is implemented now, including a new reference/XmlizeCustomValue example.

As for the problem of registering non-Core Values, I have found only one Value compatible type where this is a sort of problem: Font. I think I can live with that for now...

Mirek

Hello Mirek

Thank you for including additional basic types.

Value .xml implementation has been changed so I have lost some data but I will recover it Smile.

Are you going to add new Xmlize functions for CtrlLib classes ?


Best regards
Iñaki
Re: Xmlize values [message #25536 is a reply to message #25535] Sun, 28 February 2010 17:03 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
koldo wrote on Sun, 28 February 2010 09:44

luzr wrote on Sun, 28 February 2010 15:08

OK, all is implemented now, including a new reference/XmlizeCustomValue example.

As for the problem of registering non-Core Values, I have found only one Value compatible type where this is a sort of problem: Font. I think I can live with that for now...

Mirek

Hello Mirek

Thank you for including additional basic types.

Value .xml implementation has been changed so I have lost some data but I will recover it Smile.



Actually, strange - I was coming from your sources. What has changed?

Quote:


Are you going to add new Xmlize functions for CtrlLib classes ?



There is one generic Ctrl::Xmlize.

Mirek
Re: Xmlize values [message #25537 is a reply to message #25536] Sun, 28 February 2010 17:32 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello Mirek

Quote:

Actually, strange - I was coming from your sources. What has changed?

Before there was a separate "type" and a "value" for all types.

Now it is not exactly the same, but no problem Smile :

<item type="String">This is a text</item>
<item type="Time" value="20020101T00:00:00"/>


Quote:

There is one generic Ctrl::Xmlize


Where is it ?


Best regards
Iñaki
Re: Xmlize values [message #25539 is a reply to message #25534] Sun, 28 February 2010 20:41 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
luzr wrote on Sun, 28 February 2010 15:08

OK, all is implemented now, including a new reference/XmlizeCustomValue example.

As for the problem of registering non-Core Values, I have found only one Value compatible type where this is a sort of problem: Font. I think I can live with that for now...

Mirek


Perfect Smile
I removed my quick-and-dirty value xmlizer and used yours in my app. Different file format, indeed, so I'm happy it happened before deploying my app Smile

Ciao

Max
Re: Xmlize values [message #25541 is a reply to message #25537] Sun, 28 February 2010 23:09 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
koldo wrote on Sun, 28 February 2010 11:32

Hello Mirek

Quote:

Actually, strange - I was coming from your sources. What has changed?

Before there was a separate "type" and a "value" for all types.

Now it is not exactly the same, but no problem Smile :

<item type="String">This is a text</item>
<item type="Time" value="20020101T00:00:00"/>


Quote:

There is one generic Ctrl::Xmlize


Where is it ?


In Ctrl::Xmlize? Smile

Mirek
Re: Xmlize values [message #25547 is a reply to message #25541] Mon, 01 March 2010 09:53 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
Btw, I was wondering why for string type the 'value' tag is missing..... or why it is present for other value types.

Max

[Updated on: Mon, 01 March 2010 09:53]

Report message to a moderator

Re: Xmlize values [message #25549 is a reply to message #25547] Mon, 01 March 2010 10:20 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mdelfede wrote on Mon, 01 March 2010 03:53

Btw, I was wondering why for string type the 'value' tag is missing..... or why it is present for other value types.

Max



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
Re: Xmlize values [message #25551 is a reply to message #25549] Mon, 01 March 2010 10:53 Go to previous messageGo to next message
Mindtraveller is currently offline  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 Go to previous messageGo to next message
mdelfede is currently offline  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 ! Smile ), I was just curious.

Ciao

Max
Re: Xmlize values [message #25710 is a reply to message #25553] Tue, 09 March 2010 00:12 Go to previous messageGo to next message
Mindtraveller is currently offline  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 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
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

Re: Xmlize values [message #25729 is a reply to message #25716] Tue, 09 March 2010 13:40 Go to previous messageGo to previous message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Finally the problem is with de-xmlizing Value set to some integer value with stored something like String.

#include <Core/Core.h>
using namespace Upp;

struct A
{
	void Xmlize(XmlIO &xml) { xml ("v",v); } //<-- exception on 2nd call

	Value v;
};

CONSOLE_APP_MAIN
{
	A a;
	a.v = "test";
	StoreAsXMLFile(a, "XmlizeTest", "xmlizeTtest");
	a.v = 0;
	LoadFromXMLFile(a, "xmlizeTtest");
}
Previous Topic: Strange Win7 issue with large fonts...
Next Topic: libpng1.4
Goto Forum:
  


Current Time: Fri Mar 29 00:21:49 CET 2024

Total time taken to generate the page: 0.01174 seconds