value.cpp

Go to the documentation of this file.
00001 #include <qpainter.h>
00002 #include <qimage.h>
00003 #include <typeinfo>
00004 #include "value.h"
00005 #include "vars.h"
00006 #include "refs.h"
00007 #include "text.h"
00008 #include "image.h"
00009 #include "ranges.h"
00010 #include "line.h"
00011 #include "box.h"
00012 
00013 bool Value::equals(Value *other)
00014 {
00015   if (typeid(*other)==typeid(*this))
00016     return equals_same_type(other);
00017   return false;
00018 }
00019 
00020 QString Expression::toString()
00021 {
00022   QString result("(");
00023   result+=operat;
00024   for(vector<Value*>::iterator it=values.begin(); it!=values.end() ; it++)
00025     {
00026       result+=" ";
00027       result+=(*it)->toString();
00028     }
00029   return result+")";
00030 }
00031 
00032 bool Expression::equals_same_type(Value * other) 
00033 {
00034   Expression * o = (Expression*)other;
00035   if (o->operat!=operat) return false;
00036   if (values.size() != o->values.size()) return false;
00037   for(unsigned int i = 0 ; i < values.size() ; i++)
00038     if (!values[i]->equals(o->values[i])) 
00039       return false;
00040   return true;
00041 }
00042 
00043 Data Expression::getSaveDescription(QString prefix)
00044 {
00045   Token E;
00046   E["type"]=Symbol("expression");
00047   E["operator"]=String(operat);
00048   Array<1,Data> V(values.size());
00049   E["values"]=V;
00050   for(unsigned int i = 0 ; i < values.size() ; i++)
00051     V[i]=values[i]->getSaveDescription(prefix);
00052   return E;
00053 }
00054 
00055 Expression::Expression(Token data)
00056 {
00057   operat = (String)data["operator"];
00058   Array<1,Data> arguments = data["values"];
00059   for(Array<1,Data>::ordered argument(arguments) ; argument.more() ; ++argument)
00060     values.push_back(Value::load(argument));
00061 }
00062 
00063 Value * ValueCopier::copyExpr(Expression * expr)
00064 {
00065   Expression * result = new Expression(expr->op());
00066   for(int i = 0 ; i < expr->argc(); i ++)
00067     result->add(copy(expr->argv(i)));
00068   return result;
00069 }
00070 
00071 Value * ValueCopier::copyRange(Range * r)
00072 {
00073   return new Range(copyRef(r->from),copyRef(r->to));
00074 }
00075 
00076 Value * ValueCopier::copyText(TextValue*vv)
00077 {
00078   return new TextValue(vv->getText());
00079 }
00080 
00081 Value * ValueCopier::copy(Value * what)
00082 {
00083   if (!what) return NULL;
00084   if (typeid(*what)==typeid(TextValue))
00085     return copyText((TextValue*)what);
00086   //  if (typeid(*what)==typeid(ImageValue))
00087   //    return what;
00088   //  if (typeid(*what)==typeid(Variable))
00089   //    return copyVar((Variable*)what);
00090   if (typeid(*what)==typeid(Reference))
00091     return copyRef((Reference*)what);
00092   if (typeid(*what)==typeid(Expression))
00093     return copyExpr((Expression*)what);
00094   if (typeid(*what)==typeid(Range))
00095     return copyRange((Range*)what);
00096   assert(0);
00097   return NULL;
00098 }
00099 
00100 Value * Value::load(Data from)
00101 {
00102   if (from.type()==Symbol::type())
00103     return new Variable((Symbol)from);
00104   if (from.type()==String::type())
00105     return new TextValue((String)from);
00106   if (from.type()==Token::type())
00107     {
00108       Token compound = from;
00109       Symbol type = compound["type"];
00110       if (type==Symbol("expression"))
00111         return new Expression(compound);
00112       if (type==Symbol("image"))
00113         return new ImageValue(compound);
00114       if (type==Symbol("box"))
00115         return new BoxValue(compound);
00116       if (type==Symbol("point"))
00117         return new PointValue(compound);
00118       if (type==Symbol("line"))
00119         return new LineValue(compound);
00120       if (type==Symbol("reference"))
00121         return new Reference(compound);
00122       if (type==Symbol("range"))
00123         return new Range(compound);
00124     }
00125   printf("Cannot load value with the following text\n");
00126   DataTexter::write(from,stdout);
00127   assert(0);
00128 }
00129 
00130 QString ValueArray::toString()
00131 {
00132   return "<Array>";
00133   //  QString result;
00134   //  for(int y = 0 ; y < sy ; y++)
00135   //    {
00136   //      result+="(array ";
00137   //      for(int x = 0 ; x < sx ; x++)
00138   //        {
00139   //        result+=" ";
00140   //        result+=values[x+y*sx]->toString();
00141   //        }
00142   //      result+=")";
00143   //    }
00144   //  return result+")";
00145 }
00146 
00147 bool ValueArray::equals_same_type(Value * other) 
00148 {
00149   ValueArray * o = (ValueArray*)other;
00150   if (sx!=o->sx) return false;
00151   if (sy!=o->sy) return false;
00152   for(int y = 0 ; y < sy ; y++)
00153     for(int x = 0 ; x < sx ; x++)
00154       if (!values[x+y*sx]->equals(o->values[x+y*sy])) 
00155         return false;
00156   return true;
00157 }
00158 
00159 Data ValueArray::getSaveDescription(QString prefix)
00160 {
00161   Token E;
00162   E["type"]=Symbol("array");
00163   Array<2,Data> V(sx,sy);
00164   E["values"]=V;
00165   for(Array<2,Data>::positions xy(V) ; xy.more(); ++xy)
00166     xy=values[xy[0]+xy[1]*sx]->getSaveDescription(prefix);
00167   return E;
00168 }
00169 
00170 void ValueArray::set(int x, int y, Value *v)
00171 {
00172   assert(values);
00173   values[x+y*sx]=v;
00174 }
00175 
00176 ValueArray::ValueArray(Token data)
00177 {
00178   Array<2,Data> arguments = data["values"];
00179   sx = arguments.size(0);
00180   sy = arguments.size(1);
00181   values = new Value*[sx*sy];
00182   for(Array<2,Data>::values a(arguments) ; a.more() ; ++a)
00183     values[a[0]+a[1]*sx]=Value::load(a);
00184 }
00185 
00186 ValueArray::ValueArray(int x, int y)
00187 {
00188   sx = x;
00189   sy = y;
00190   values = new Value*[sx*sy];
00191 }

Generated on Mon Jun 5 22:08:42 2006 for iis by  doxygen 1.4.6