Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/expr/ExVal.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef MARSYAS_EX_VAL_H
00020 #define MARSYAS_EX_VAL_H
00021 
00022 #include <marsyas/sched/TmTimer.h>
00023 #include <marsyas/sched/Scheduler.h>
00024 #include <marsyas/common_header.h>
00025 
00026 #include <string>
00027 #include <iostream>
00028 #include <cmath>
00029 
00030 namespace Marsyas
00031 {
00042 class ExNode;
00043 class ExFun;
00044 class ExNode_List;
00045 
00046 class ExVal {
00047 private:
00048   int kind_;
00049   std::string type_;
00050   std::string string_;
00051   mrs_natural natural_;
00052   mrs_real real_;
00053   mrs_bool bool_;
00054 
00055   ExFun* fun_;
00056   TmTimer** timer_;
00057   Scheduler** scheduler_;
00058   ExNode** list_; // use natural_ as length
00059 protected:
00060   void clear();
00061   void clear_list();
00062   void setKindType(int k, std::string t) { kind_=k; type_=t; }
00063 public:
00064   ExVal()                               {list_=NULL; fun_=NULL; clear();};
00065   ExVal(const std::string x)            {list_=NULL; fun_=NULL; set(x);};
00066   ExVal(double x)                     {list_=NULL; fun_=NULL; set(x);};
00067   ExVal(float x)                     {list_=NULL; fun_=NULL; set(x);};
00068 //     ExVal(mrs_real x)                     {list_=NULL;fun_=NULL;set(x);};
00069   ExVal(mrs_natural x)                  {list_=NULL; fun_=NULL; set(x);};
00070   ExVal(mrs_bool x)                     {list_=NULL; fun_=NULL; set(x);};
00071   ExVal(ExFun* x)                       {list_=NULL; fun_=NULL; set((ExFun*)x);};
00072   ExVal(TmTimer** x)                    {list_=NULL; fun_=NULL; set((TmTimer**)x);};
00073   ExVal(Scheduler** x)                 {list_=NULL; fun_=NULL; set((Scheduler**)x);};
00074   ExVal(mrs_natural len, ExNode** xs, std::string t="")   {list_=NULL; fun_=NULL; set(len,(ExNode**)xs,t);}; // list
00075   ExVal(mrs_natural len, std::string t) {list_=NULL; fun_=NULL; set(len,(std::string)t);}; // empty list
00076   ExVal(const ExVal& x)                 {list_=NULL; fun_=NULL; set((ExVal&)x);};
00077   ExVal& operator=(const ExVal& x) {set(x); return*this;}
00078   virtual ~ExVal();
00079 
00080   std::string getType() const {return type_;};
00081   std::string getBaseType() const;
00082   std::string getElemType() const;
00083   bool is_list() const;
00084   bool is_seq() const;
00085   mrs_natural toNatural() const {return natural_;}
00086   mrs_real toReal() const {return real_;}
00087   mrs_bool toBool() const {return bool_;}
00088   ExFun* toFun() const {return fun_;}
00089   std::string toString() const;
00090   TmTimer** toTimer() const {return timer_;}
00091   Scheduler** toScheduler() const {return scheduler_;}
00092 
00093   ExVal getSeqRange(int lidx, int ridx);
00094   ExVal getSeqElem(int idx);
00095   void setSeqElem(int idx, ExVal v);
00096   ExVal append(const ExVal v) const;
00097 
00098   void set(ExFun* x);
00099   void set(const std::string x);
00100   void set(double x);
00101   void set(float x);
00102   void set(mrs_natural x);
00103   void set(mrs_bool x);
00104   void set(const ExVal& v);
00105   void set(TmTimer** t);
00106   void set(Scheduler** t);
00107   void set(mrs_natural len, ExNode** xs, std::string t="");
00108   void set(mrs_natural len, std::string t); // empty list
00109   static ExVal defaultExValue(std::string type);
00110 
00111 #define LIST_CONCAT                                                 \
00112         if (v1.is_list()&&v2.is_list()) { return v1.append(v2); }
00113 
00114 #define T_BINOP(_T,_VAL,_OP,_CAST) if (v1.type_==_T) { return _CAST(v1._VAL _OP v2._VAL); }
00115 #define S_BOP(_OP,_CAST) T_BINOP("mrs_string",string_,_OP,_CAST)
00116 #define N_BOP(_OP,_CAST) T_BINOP("mrs_natural",natural_,_OP,_CAST)
00117 #define R_BOP(_OP,_CAST) T_BINOP("mrs_real",real_,_OP,_CAST)
00118 #define RMOD_BOP() if (v1.type_=="mrs_real") { return fmod(v1.real_,v2.real_); }
00119 #define B_BOP(_OP,_CAST) T_BINOP("mrs_bool",bool_,_OP,_CAST)
00120 
00121 
00122 
00123 // need to make non-inline version with MRSWARN
00124 
00125 #define VAL_BINOP(_NAME,_WARN,_TESTS)                                   \
00126         friend inline ExVal _NAME(const ExVal& v1, const ExVal& v2)     \
00127         {                                                               \
00128             _TESTS;                                                     \
00129             ((std::string)_WARN+"  Invalid types ~"+v1.getType()+","+v2.getType()); \
00130             return v1;                                                  \
00131         };
00132 
00133   VAL_BINOP(operator==, "ExVal::op==", R_BOP(==,(bool)); N_BOP(==,(bool)); S_BOP(==,(bool)); B_BOP(==,(bool)));
00134   VAL_BINOP(operator!=, "ExVal::op!=", R_BOP(!=,(bool)); N_BOP(!=,(bool)); S_BOP(!=,(bool)); B_BOP(!=,(bool)));
00135   VAL_BINOP(operator<=, "ExVal::op<=", R_BOP(<=,(bool)); N_BOP(<=,(bool)); S_BOP(<=,(bool)); B_BOP(<=,(bool)));
00136   VAL_BINOP(operator< , "ExVal::op<" , R_BOP(< ,(bool)); N_BOP(< ,(bool)); S_BOP(< ,(bool)); B_BOP(< ,(bool)));
00137   VAL_BINOP(operator>=, "ExVal::op>=", R_BOP(>=,(bool)); N_BOP(>=,(bool)); S_BOP(>=,(bool)); B_BOP(>=,(bool)));
00138   VAL_BINOP(operator> , "ExVal::op>" , R_BOP(> ,(bool)); N_BOP(> ,(bool)); S_BOP(> ,(bool)); B_BOP(> ,(bool)));
00139 
00140   VAL_BINOP(operator+, "ExVal::op+", R_BOP(+,(mrs_real)); N_BOP(+,(mrs_natural)); S_BOP(+,(std::string)); LIST_CONCAT;);
00141   VAL_BINOP(operator-, "ExVal::op-", R_BOP(-,(mrs_real)); N_BOP(-,(mrs_natural))          );
00142   VAL_BINOP(operator*, "ExVal::op*", R_BOP(*,(mrs_real)); N_BOP(*,(mrs_natural))          );
00143   VAL_BINOP(operator/, "ExVal::op/", R_BOP(/,(mrs_real)); N_BOP(/,(mrs_natural))          );
00144   VAL_BINOP(operator%, "ExVal::op%", RMOD_BOP();          N_BOP(%,(mrs_natural))          );
00145 
00146   friend inline ExVal operator||(const ExVal& v1, const ExVal& v2) { return v1.bool_ || v2.bool_; };
00147   friend inline ExVal operator&&(const ExVal& v1, const ExVal& v2) { return v1.bool_ && v2.bool_; };
00148   friend std::ostream& operator<<(std::ostream& o, ExVal& v);
00149 
00150 }; //class ExVal
00151 
00152 class ExValTyped : public ExVal {
00153 public:
00154   ExValTyped(int k, std::string t) : ExVal() { setKindType(k,t); };
00155   virtual ~ExValTyped() {}
00156 };
00157 
00158 }//namespace Marsyas
00159 
00160 #endif
00161