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 » U++ Library support » U++ Core » S_type and ValueMap
S_type and ValueMap [message #52517] Tue, 15 October 2019 11:40 Go to next message
Giorgio is currently offline  Giorgio
Messages: 184
Registered: August 2015
Experienced Member
Hi there,
I have the following code: the idea is to get data from db, put them in a vector of ValueMap and then put them in a S_type structure.

std::vector<ValueMap> * results=this->dao->QueryTable(table, vmfields); //ValueMap(s) here represent lines from db
for(auto it = results->begin(); it != results->end(); ++it)

  S_FILLER s(*it); //S_FILLER is defined in a .sch file
  //Do something with s
}


S_FILLER is defined as follows:

TABLE_ (FILLER)
  STRING   (KMP,8) PRIMARY_KEY REFERENCES(ITEM.KMP)
  STRING_  (DESCRIPTION,50)
  STRING   (STATUS,8)
  INT_     (NOELEMENTS)
  DOUBLE_  (DENSITY)
END_TABLE


The problem is that the code crashes if in the S_type structure there are numerical types, if they are just strings the code runs perfectly.
Is there a way to force some kind of "automatic" conversion from String to the correct type?
Thanks,
gio
Re: S_type and ValueMap [message #52518 is a reply to message #52517] Tue, 15 October 2019 12:00 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12105
Registered: November 2005
Ultimate Member
Giorgio wrote on Tue, 15 October 2019 11:40
Hi there,
I have the following code: the idea is to get data from db, put them in a vector of ValueMap and then put them in a S_type structure.

std::vector<ValueMap> * results=this->dao->QueryTable(table, vmfields); //ValueMap(s) here represent lines from db
for(auto it = results->begin(); it != results->end(); ++it)

  S_FILLER s(*it); //S_FILLER is defined in a .sch file
  //Do something with s
}


S_FILLER is defined as follows:

TABLE_ (FILLER)
  STRING   (KMP,8) PRIMARY_KEY REFERENCES(ITEM.KMP)
  STRING_  (DESCRIPTION,50)
  STRING   (STATUS,8)
  INT_     (NOELEMENTS)
  DOUBLE_  (DENSITY)
END_TABLE


The problem is that the code crashes if in the S_type structure there are numerical types, if they are just strings the code runs perfectly.
Is there a way to force some kind of "automatic" conversion from String to the correct type?
Thanks,
gio


No. I believe that would be dangerous.

I think you can do this by using S_type introspection and doing the conversion yourself. E.g. use GetRef to get a reference to column attribute, then you can use 'Is' to check for its type. Create a template function to do the conversion to accomodate various S_tables.

Mirek
Re: S_type and ValueMap [message #52519 is a reply to message #52518] Tue, 15 October 2019 13:54 Go to previous messageGo to next message
Giorgio is currently offline  Giorgio
Messages: 184
Registered: August 2015
Experienced Member
Hi,
thank you for your suggestion.

This is what I came up with, I post it here for reference and improvements.


[...]
		//S_FILLER s(*vm); Crashes if it contains int or double
		S_FILLER s;
		S_convert(&s,vm);

[...]

	template <class T>
	void S_convert(T * s, ValueMap * vm){
	
		for(int i=0;i<vm->GetCount();i++){
			
			Ref r=s->GetRef(SqlId(vm->GetKey(i)));
			if(r.Is<double>())
				s->Set(SqlId(vm->GetKey(i)),StrDbl(AsString(vm->GetValue(i))));
				
			if(r.Is<int>())
				s->Set(SqlId(vm->GetKey(i)),StrInt(AsString(vm->GetValue(i))));
				
			if(r.Is<String>())
				s->Set(SqlId(vm->GetKey(i)),AsString(vm->GetValue(i)));
		}
		
	}


Re: S_type and ValueMap [message #52520 is a reply to message #52519] Tue, 15 October 2019 14:39 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12105
Registered: November 2005
Ultimate Member
Giorgio wrote on Tue, 15 October 2019 13:54
Hi,
thank you for your suggestion.

This is what I came up with, I post it here for reference and improvements.


[...]
		//S_FILLER s(*vm); Crashes if it contains int or double
		S_FILLER s;
		S_convert(&s,vm);

[...]

	template <class T>
	void S_convert(T * s, ValueMap * vm){
	
		for(int i=0;i<vm->GetCount();i++){
			
			Ref r=s->GetRef(SqlId(vm->GetKey(i)));
			if(r.Is<double>())
				s->Set(SqlId(vm->GetKey(i)),StrDbl(AsString(vm->GetValue(i))));
				
			if(r.Is<int>())
				s->Set(SqlId(vm->GetKey(i)),StrInt(AsString(vm->GetValue(i))));
				
			if(r.Is<String>())
				s->Set(SqlId(vm->GetKey(i)),AsString(vm->GetValue(i)));
		}
		
	}




Why not


template <class T>
void S_convert(T& s, const ValueMap& vm){

?

Mirek
Re: S_type and ValueMap [message #52535 is a reply to message #52520] Mon, 21 October 2019 09:31 Go to previous message
Giorgio is currently offline  Giorgio
Messages: 184
Registered: August 2015
Experienced Member
You are right, in my specific case I already have the ValueMap as a pointer, but in general your solution is better.

Also, I added a continue instruction after evry set (all in brackets of course) to improve a little the execution time.
Previous Topic: Doubt about container
Next Topic: [SOLVED] Vector Bug
Goto Forum:
  


Current Time: Tue Nov 19 06:15:42 CET 2019

Total time taken to generate the page: 0.01681 seconds