00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __PARAMETER_H__
00026 #define __PARAMETER_H__
00027
00028 #include<magic/mstring.h>
00029 #include<magic/mmap.h>
00030
00031
00033
00034
00035
00036
00037
00038
00040
00044 class DynParameter {
00045 public:
00047 DynParameter () {FORBIDDEN}
00048
00050 DynParameter (const String& name,
00051 const String& shortd,
00052 int maxlen,
00053 const String& clsname
00054 ) : mName (name), mShortDescription (shortd), mParamClassName (clsname), mMaxlen(maxlen) {}
00055
00056 virtual ~DynParameter () {}
00057
00059 const String& name () const {return mName;}
00060
00062 const String& shortDesc () const {return mShortDescription;}
00063
00065 const String& paramClass () const {return mParamClassName;}
00066
00068 int maxlen () const {return mMaxlen;}
00069
00073 virtual bool checkValue (const String& value) const {return value.length()<=mMaxlen;}
00074
00075 private:
00076 String mName;
00077 String mShortDescription;
00078 String mParamClassName;
00079 int mMaxlen;
00080 };
00081
00082
00084
00085
00086
00087
00088
00089
00091
00092 class StringParameter : public DynParameter {
00093 public:
00094 virtual bool checkValue (const String& value) const {return DynParameter::checkValue(value);}
00095
00096 protected:
00097 StringParameter (const String& name, const String& shortd, int maxlen) : DynParameter (name, shortd, maxlen, "string") {}
00098 };
00099
00100
00102
00103
00104
00105
00106
00108
00109 class DoubleParameter : public DynParameter {
00110 public:
00111 DoubleParameter (const String& name, const String& shortd, int maxlen, double minv, double maxv, double defv=-345535.234) : DynParameter (name, shortd, maxlen, "float"), mMin(minv), mMax(maxv), mDefault(defv) {}
00112 virtual bool checkValue (const String& value) const {return DynParameter::checkValue(value);}
00113 virtual String defaultString () const {return (mDefault==-345535.234)? String(""):String(mDefault);}
00114
00115 private:
00116 double mMin;
00117 double mMax;
00118 double mDefault;
00119 };
00120
00121 class IntParameter : public DynParameter {
00122 public:
00123 IntParameter (const String& name, const String& shortd, long minv, long maxv, long defv=-3475235) : DynParameter (name, shortd, 15, "int"), mMin(minv), mMax(maxv), mDefault(defv) {}
00124 virtual bool checkValue (const String& value) const {return DynParameter::checkValue(value);}
00125 virtual String defaultString () const {return (mDefault==-3475235)? String(""):String(mDefault);}
00126
00127 private:
00128 int mMin;
00129 int mMax;
00130 int mDefault;
00131 };
00132
00133 class BoolParameter : public DynParameter {
00134 public:
00135 BoolParameter (const String& name, const String& shortd, int defv=-1) : DynParameter (name, shortd, 1, "int"), mDefault(defv) {}
00136 virtual bool checkValue (const String& value) const {return DynParameter::checkValue(value);}
00137 virtual String defaultString () const {return (mDefault==-1)? String(""):String(mDefault);}
00138
00139 private:
00140 int mDefault;
00141 };
00142
00143
00145
00146
00147
00148
00149
00151
00155 class IParameterized {
00156 public:
00160 virtual Array<DynParameter>* parameters () const {MUST_OVERLOAD; return NULL;}
00161
00164 virtual void init (const StringMap& params) {MUST_OVERLOAD}
00165 };
00166
00170 #define INITPARAMS(mapname, initcode) \
00171 try { const_cast<StringMap&>(mapname).failByThrow(); initcode \
00172 } catch (Exception e) {\
00173 String errorstr = e.what();\
00174 errorstr += "\nInitialization parameter missing. Parameters are:\n";\
00175 Array<DynParameter>* pars = parameters ();\
00176 for (int i=0; i<pars->size; i++)\
00177 errorstr += (*pars)[i].name () + " ";\
00178 errorstr += '\n';\
00179 throw Exception (errorstr);\
00180 }
00181
00182 #endif