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 » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » template + convert problem
template + convert problem [message #23152] Sat, 19 September 2009 14:14 Go to previous message
sergeynikitin is currently offline  sergeynikitin
Messages: 748
Registered: January 2008
Location: Moscow, Russia
Contributor

I make a table in which multiple fields of reference from other tables. The mapping of the field from another table I made with the converter.

Now it looks as follows:

converts.h

struct ConvCompany : Convert
{
	virtual Value Format(const Value& q) const;
};

struct ConvNomencl : Convert
{
	virtual Value Format(const Value& q) const;
};

struct ConvManager : Convert
{
	virtual Value Format(const Value& q) const;
};


converts.cpp:
Value ConvCompany::Format(const Value &q) const
{
	if(q.IsNull()) return Null;
	static VectorMap<int, String> comp;
	static Time lastcleartime;
	if(GetSysTime()-lastcleartime > 600){
		comp.Clear();
		lastcleartime=GetSysTime();
	}
	int f = comp.Find(int(q));
	if(f >= 0){
		return comp[f];
	} else {
		Sql sql;
		sql * Select(COM_NAME).From(COMPANY).Where(COM_ID == q);
		String company;
		if(sql.Fetch()) {
			int sid = int(q);
			company = sql[COM_NAME];
			comp.Add(sid, company);
		} else {
			company = "";
		}
		return company;
	}
}
Value ConvNomencl::Format(const Value& q) const
{
	if(q.IsNull()) return Null;
	static VectorMap<int, String> nom;
	static Time lastcleartime;
	if(GetSysTime()-lastcleartime > 600){
		nom.Clear();
		lastcleartime=GetSysTime();
	}
	int f = nom.Find(int(q));
	if(f >= 0){
		return nom[f];
	} else {
		Sql sql;
		sql * SqlSelect(NOM_ID,NOM_NAME).From(NOMENCL).Where(NOM_ID == q);
		String nomencl;
		if(sql.Fetch()) {
			int sid = sql[NOM_ID];
			nomencl = sql[NOM_NAME];
			nom.Add(sid, nomencl);
		} else {
			nomencl = "";
		}
		return nomencl;
	}
}

Value ConvManager::Format(const Value &q) const
{
	if(q.IsNull()) return Null;
	static VectorMap<int, String> man;
	static Time lastcleartime;
	if(GetSysTime()-lastcleartime > 600){
		man.Clear();
		lastcleartime=GetSysTime();
	}
	int f = man.Find(int(q));
	if(f >= 0){
		return man[f];
	} else {
		Sql sql;
		sql * SqlSelect(USR_REALNAME).From(USER).Where(USR_ID == q);
		String manager;
		if(sql.Fetch()) {
			int sid = int(q);
			manager = sql[USR_REALNAME];
			man.Add(sid, manager);
		} else {
			manager = "";
		}
		return manager;
	}
}


These converters differ only in the name of the table and field names. I want this to be in the form of a template, something like:
template <SqlId TBL ,SqlId ID, SqlId NAME,int CLEARTIME>
struct ConvDict : Convert
//ConvDict<COMPANY,COM_ID,COM_NAME,600>()
{
	virtual Value Format(const Value& q) const {
		if(q.IsNull()) return Null;
		static VectorMap<int, String> dict;
		static Time lastcleartime;
		if(GetSysTime()-lastcleartime > CLEARTIME){
			dict.Clear();
			lastcleartime=GetSysTime();
		}
		int f = dict.Find(int(q));
		if(f >= 0){
			return dict[f];
		} else {
			Sql sql;
			sql * Select(NAME).From(TBL).Where(ID == q);
			String s;
			if(sql.Fetch()) {
				int sid = int(q);
				s = sql[NAME];
				dict.Add(sid, s);
			} else {
				s = "";
			}
			return s;
		}
	}
};


But this code is not compiled, wrote that at this point can not be applied SqlId.

Can someone experienced with this?

How do make a template for the Converter, or Display?


SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}
 
Read Message
Read Message
Read Message
Previous Topic: SAPI example
Next Topic: global Object and static std::map --> Crash
Goto Forum:
  


Current Time: Thu Apr 25 07:58:35 CEST 2024

Total time taken to generate the page: 0.04617 seconds