00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef MARSYAS_EX_VAL_H
00020 #define MARSYAS_EX_VAL_H
00021
00022 #include <string>
00023 #include <iostream>
00024 #include <cmath>
00025 #include "common_header.h"
00026 #include "TmTimer.h"
00027 #include "Scheduler.h"
00028
00029 namespace Marsyas
00030 {
00041 class ExNode;
00042 class ExFun;
00043 class ExNode_List;
00044
00045 class ExVal {
00046 private:
00047 int kind_;
00048 std::string type_;
00049 std::string string_;
00050 mrs_natural natural_;
00051 mrs_real real_;
00052 mrs_bool bool_;
00053
00054 ExFun* fun_;
00055 TmTimer** timer_;
00056 Scheduler** scheduler_;
00057 ExNode** list_;
00058 protected:
00059 void clear();
00060 void clear_list();
00061 void setKindType(int k, std::string t) { kind_=k; type_=t; }
00062 public:
00063 ExVal() {list_=NULL;fun_=NULL;clear();};
00064 ExVal(const std::string x) {list_=NULL;fun_=NULL;set(x);};
00065 ExVal(double x) {list_=NULL;fun_=NULL;set(x);};
00066 ExVal(float x) {list_=NULL;fun_=NULL;set(x);};
00067
00068 ExVal(mrs_natural x) {list_=NULL;fun_=NULL;set(x);};
00069 ExVal(mrs_bool x) {list_=NULL;fun_=NULL;set(x);};
00070 ExVal(ExFun* x) {list_=NULL;fun_=NULL;set((ExFun*)x);};
00071 ExVal(TmTimer** x) {list_=NULL;fun_=NULL;set((TmTimer**)x);};
00072 ExVal(Scheduler** x) {list_=NULL;fun_=NULL;set((Scheduler**)x);};
00073 ExVal(mrs_natural len, ExNode** xs, std::string t="") {list_=NULL;fun_=NULL;set(len,(ExNode**)xs,t);};
00074 ExVal(mrs_natural len, std::string t) {list_=NULL;fun_=NULL;set(len,(std::string)t);};
00075 ExVal(const ExVal& x) {list_=NULL;fun_=NULL;set((ExVal&)x);};
00076 ExVal& operator=(const ExVal& x){set(x);return*this;}
00077 virtual ~ExVal();
00078
00079 std::string getType() const {return type_;};
00080 std::string getBaseType() const;
00081 std::string getElemType() const;
00082 bool is_list() const;
00083 bool is_seq() const;
00084 mrs_natural toNatural() const {return natural_;}
00085 mrs_real toReal() const {return real_;}
00086 mrs_bool toBool() const {return bool_;}
00087 ExFun* toFun() const {return fun_;}
00088 std::string toString() const;
00089 TmTimer** toTimer() const {return timer_;}
00090 Scheduler** toScheduler() const {return scheduler_;}
00091
00092 ExVal getSeqRange(int lidx, int ridx);
00093 ExVal getSeqElem(int idx);
00094 void setSeqElem(int idx, ExVal v);
00095 ExVal append(const ExVal v) const;
00096
00097 void set(ExFun* x);
00098 void set(const std::string x);
00099 void set(double x);
00100 void set(float x);
00101 void set(mrs_natural x);
00102 void set(mrs_bool x);
00103 void set(const ExVal& v);
00104 void set(TmTimer** t);
00105 void set(Scheduler** t);
00106 void set(mrs_natural len, ExNode** xs, std::string t="");
00107 void set(mrs_natural len, std::string t);
00108 static ExVal defaultExValue(std::string type);
00109
00110 #define LIST_CONCAT \
00111 if (v1.is_list()&&v2.is_list()) { return v1.append(v2); }
00112
00113 #define T_BINOP(_T,_VAL,_OP,_CAST) if (v1.type_==_T) { return _CAST(v1._VAL _OP v2._VAL); }
00114 #define S_BOP(_OP,_CAST) T_BINOP("mrs_string",string_,_OP,_CAST)
00115 #define N_BOP(_OP,_CAST) T_BINOP("mrs_natural",natural_,_OP,_CAST)
00116 #define R_BOP(_OP,_CAST) T_BINOP("mrs_real",real_,_OP,_CAST)
00117 #define RMOD_BOP() if (v1.type_=="mrs_real") { return fmod(v1.real_,v2.real_); }
00118 #define B_BOP(_OP,_CAST) T_BINOP("mrs_bool",bool_,_OP,_CAST)
00119
00120
00121
00122
00123
00124 #define VAL_BINOP(_NAME,_WARN,_TESTS) \
00125 friend inline ExVal _NAME(const ExVal& v1, const ExVal& v2) \
00126 { \
00127 _TESTS; \
00128 ((std::string)_WARN+" Invalid types ~"+v1.getType()+","+v2.getType()); \
00129 return v1; \
00130 };
00131
00132 VAL_BINOP(operator==, "ExVal::op==", R_BOP(==,(bool)); N_BOP(==,(bool)); S_BOP(==,(bool));B_BOP(==,(bool)));
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
00139 VAL_BINOP(operator+, "ExVal::op+", R_BOP(+,(mrs_real)); N_BOP(+,(mrs_natural)); S_BOP(+,(std::string)); LIST_CONCAT;);
00140 VAL_BINOP(operator-, "ExVal::op-", R_BOP(-,(mrs_real)); N_BOP(-,(mrs_natural)) );
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%", RMOD_BOP(); N_BOP(%,(mrs_natural)) );
00144
00145 friend inline ExVal operator||(const ExVal& v1, const ExVal& v2) { return v1.bool_ || v2.bool_; };
00146 friend inline ExVal operator&&(const ExVal& v1, const ExVal& v2) { return v1.bool_ && v2.bool_; };
00147 friend std::ostream& operator<<(std::ostream& o, ExVal& v);
00148
00149 };
00150
00151 class ExValTyped : public ExVal {
00152 public:
00153 ExValTyped(int k, std::string t) : ExVal() { setKindType(k,t); };
00154 virtual ~ExValTyped(){}
00155 };
00156
00157 }
00158
00159 #endif
00160