Browse Source

Gmt module: Conversion function for GMTPen now using new parameter reading scheme and error reporting.

gmtdatadir
Michael Uleysky 8 years ago
parent
commit
cbfd462ff0
  1. 160
      modules/gmt/modgmt_func.h

160
modules/gmt/modgmt_func.h

@ -174,6 +174,8 @@ using Base2Coord =Base2Struct<struct gmt_coord>;
using Base2Region=Base2Struct<struct gmt_region>; using Base2Region=Base2Struct<struct gmt_region>;
using Base2Proj =Base2Struct<struct gmt_projection>; using Base2Proj =Base2Struct<struct gmt_projection>;
using Base2Color =Base2Struct<struct gmt_color>; using Base2Color =Base2Struct<struct gmt_color>;
using Base2Dash =Base2Struct<struct gmt_dash>;
using Base2Pen =Base2Struct<struct gmt_pen>;
// Simple aliases of convertors with default values // Simple aliases of convertors with default values
using Base2StringD=Base2StructD<std::string>; using Base2StringD=Base2StructD<std::string>;
@ -194,7 +196,37 @@ class Base2CoordD: public DefaultConverter<Base2Coord>
Base2CoordD(double d):DefaultConverter<Base2Coord>(Def(d)) {} Base2CoordD(double d):DefaultConverter<Base2Coord>(Def(d)) {}
}; };
// Convertors for double non-negative value // Convertor with default value for gmt_color. Added constructor for creating default value from double argument
class Base2ColorD: public DefaultConverter<Base2Color>
{
static ValueType Def(double d)
{
ValueType v;
std::string fake;
v.Convert(d,fake);
return v;
}
public:
using ValueType=DefaultConverter<Base2Color>::ValueType;
Base2ColorD(double d):DefaultConverter<Base2Color>(Def(d)) {}
};
// Convertor with default value for gmt_dash. Default value is always no dash.
class Base2DashD: public DefaultConverter<Base2Dash>
{
static ValueType Def()
{
ValueType v;
v.Clear();
return v;
}
public:
using ValueType=DefaultConverter<Base2Dash>::ValueType;
Base2DashD():DefaultConverter<Base2Dash>(Def()) {}
};
// Convertors for double positive value
class Base2Pos: public Base2Double class Base2Pos: public Base2Double
{ {
public: public:
@ -211,6 +243,23 @@ class Base2Pos: public Base2Double
}; };
using Base2PosD=DefaultConverter<Base2Pos>; using Base2PosD=DefaultConverter<Base2Pos>;
// Convertors for double non-negative value
class Base2NonNeg: public Base2Double
{
public:
double Convert(const ObjectBase* ob, bool* res, std::string& err)
{
double t=Base2Double::Convert(ob,res,err);
if(res && t<0)
{
*res=false;
err="Value mast be non-negative";
}
return t;
}
};
using Base2NonNegD=DefaultConverter<Base2NonNeg>;
// Convertors for double values which must be in some interval // Convertors for double values which must be in some interval
template<int32_t Min, int32_t Max> template<int32_t Min, int32_t Max>
class Base2InRange: public Base2Double class Base2InRange: public Base2Double
@ -837,9 +886,6 @@ class Convert2Struct<struct gmt_color, ObjectList>
} }
}; };
// Helper types
typedef SearchParameterWDefO<double,DoubleDefaultVal<>,false,PMin<0> > Base2Width;
typedef SearchParameter<double,false,PMin<0> > BaseM2Width;
// Converting List to GMTPen // Converting List to GMTPen
/* /*
@ -860,91 +906,53 @@ class Convert2Struct<struct gmt_pen, ObjectList>
{ {
public: public:
struct gmt_pen operator()(const ObjectList* input, bool* issuc) const struct gmt_pen operator()(const ObjectList* input, bool* issuc, std::string& err) const
{ {
struct gmt_pen p; struct gmt_pen p;
auto size=input->Size();
if(1==size) // Cases 1, 2 and 4 if(1==input->Size()) // Cases 1, 2 and 4
{ {
Base2Pen pen(input,0); Base2Pen pen;
bool suc=true; bool suc=true;
p=pen(&suc); p=pen.Convert(input->At(0),&suc,err);
if(!suc) goto fail; // Conversion failed if(!suc) goto fail; // Conversion failed
return p; return p;
} }
// Case 3 // Case 5 and 6
{ {
bool casevalid=false; OPosPar<Base2NonNegD> width("width",gmt_pen::default_width);
bool upd; OPosPar<Base2ColorD> color("color",0.0);
{ OPosPar<Base2DashD> dash("dash");
BaseMT2Pen updarg(input,"pen","p");
bool suc=true; ParsePositionalParameters params(input,width,color,dash);
p=updarg(&upd,&suc); if(!params) goto case3; // May be case3 will work
if(upd && !suc) goto fail; // Conversion failed or too many arguments p.color=color;
} p.dash=dash;
p.dash.width=p.width=width;
if(!upd) return p;
{
// default pen is solid black 1mm width
p.width=gmt_pen::default_width;
p.color.Convert(0);
p.dash.Clear();
}
{
BaseM2Width w(input,"w","width");
if(w.Exist())
{
bool suc=true;
p.width=w(&suc);
if(!suc) goto fail; // Parsing error
casevalid=true;
}
}
// Color
{
BaseMT2Color color(input,"color","c");
if(color.Exist())
{
bool suc=true;
p.color=color(&suc);
if(!suc) goto fail; // Parsing error
casevalid=true;
}
}
// Dash
{
BaseMT2Dash dash(input,"dash","d");
if(dash.Exist())
{
bool suc=true;
p.dash=dash(&suc);
if(!suc) goto fail; // Parsing error
p.dash.width=p.width;
casevalid=true;
}
}
if(casevalid || upd) return p; // Pen created or updated
} }
// Case 5 and 6 // Case 3
if(2==size || 3==size) case3:
{ {
Base2Width w(input,0); bool upd;
Base2Color c(input,1); ONFPar<Base2Pen,ObjectGMTPen> pen("pen");
Base2Dash d(input,2); ONFPar<Base2ColorD,ObjectGMTColor> color("color",0.0);
bool suc=true; ONFPar<Base2DashD,ObjectGMTDash> dash("dash");
p.width=w(&suc); ONPar<Base2NonNegD> width("width",gmt_pen::default_width);
p.color=c(&suc);
if(!suc) goto fail; // Something wrong {ParseNamedParameters params(input,color,pen,dash,width); if(!params) {err=params.Error(); goto fail;} } // Fail to parse by variant 3
p.dash=d(&suc,p.width);
if(!suc) goto fail; // Something wrong upd=pen.Exist();
return p; if(upd) p=pen;
if(color.Exist() || !upd) p.color=color;
if(dash.Exist() || !upd) p.dash=dash;
if(width.Exist() || !upd) p.width=width;
p.dash.width=p.width;
if(color.Exist() || dash.Exist() || width.Exist() || upd) return p; // Pen created or updated
err="Specify at least one of color, dash or width";
} }
fail: fail:

Loading…
Cancel
Save