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 » ConvertInt > templatable Convert<T>
Re: ConvertInt > templatable Convert<T> [message #22709 is a reply to message #22708] Fri, 07 August 2009 18:03 Go to previous messageGo to previous message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
I'm not really sure what problem you're trying to solve with this, though I think I see what you're trying to do.

I think there are simpler ways of achieving it though, for instance:
template <class DataType>
class ConvertT : public Convert
{
private:
	DataType minval;
	DataType maxval;
	bool notnull;
	
public:
	ConvertT() : minval(Null), maxval(Null), notnull(false) {}

	virtual Value Scan(const Value& text) const {
		if (IsNotNull() && IsNull(text)) return NotNullError();
		Value v;
		if( 	(typeid(DataType) == typeid(int))
			 || (typeid(DataType) == typeid(unsigned int))
			 || (typeid(DataType) == typeid(short))
			 || (typeid(DataType) == typeid(unsigned short))
			 || (typeid(DataType) == typeid(char))
			 || (typeid(DataType) == typeid(unsigned char))
			)
		{
			v = StdConvertInt().Scan(text);
		}
		else if( (typeid(DataType) == typeid(double))
			 || (typeid(DataType) == typeid(float))
			)
		{
			v = StdConvertDouble().Scan(text);
		}
		else if( (typeid(DataType) == typeid(uint64))
			 || (typeid(DataType) == typeid(int64))
			)
		{
			v = ConvertInt64().Scan(text); // No StdConvert64
		}
		else 
			v = StdConvert().Scan(text);
			
		if (!IsNull(v) && !IsNull(minval) && !IsNull(maxval)) {
			DataType m = DataType(v);
			if(m >= minval && m <= maxval) 
				return v;
			return ErrorValue(UPP::Format(t_("Number must be between %s and %s."), Format(minval), Format(maxval)));
		}
		return v;
	}
	virtual Value Format(const Value& q) const {
                // You may want to check the type of q here to ensure it matches DataType
		return StdConvert().Format(q);
	}
	
	ConvertT<DataType>& MinMax(DataType _min, DataType _max) { minval = _min; maxval = _max; return *this; }
	ConvertT<DataType>& Min(DataType _min)                { minval = _min; return *this; }
	ConvertT<DataType>& Max(DataType _max)                { maxval = _max; return *this; }
	ConvertT<DataType>& NotNull(bool b = true)            { notnull = b; return *this; }
	ConvertT<DataType>& NoNotNull()                       { return NotNull(false); }
	DataType         	GetMin() const                    { return minval; }
	DataType         	GetMax() const                    { return maxval; }
	bool        		IsNotNull() const                 { return notnull; }
};

And then use templated edit ctrls like so:
template <class T>
struct EditMinMaxT : public EditMinMax<T, ConvertT<T> >
{ };

 
Read Message
Read Message
Read Message
Read Message icon14.gif
Read Message
Previous Topic: GZDecompress bug
Next Topic: compressed serialization
Goto Forum:
  


Current Time: Tue May 14 14:46:50 CEST 2024

Total time taken to generate the page: 0.01224 seconds