#include "mlp.h" #include #include #include mlp_value::mlp_value(int val): t(MLP_INT), thrown(false), stringy(NULL){ v.i = val ; } MLP_EXTERN_C mlp_value *mlp_value_new_from_int(int val){ return new mlp_value(val) ; } mlp_value::mlp_value(long val): t(MLP_LONG), thrown(false), stringy(NULL){ v.l = val ; } MLP_EXTERN_C mlp_value *mlp_value_new_from_long(long val){ return new mlp_value(val) ; } mlp_value::mlp_value(double val) : t(MLP_DOUBLE), thrown(false), stringy(NULL){ v.d = val ; } MLP_EXTERN_C mlp_value *mlp_value_new_from_double(double val){ return new mlp_value(val) ; } mlp_value::mlp_value(const char *val) : t(MLP_STRING), thrown(false), stringy(NULL){ v.s = (val == NULL ? NULL : strdup(val)) ; } MLP_EXTERN_C mlp_value *mlp_value_new_from_string(const char *val){ return new mlp_value(val) ; } mlp_value::mlp_value(const mlp_object *val) : t(MLP_OBJECT), thrown(false), stringy(NULL){ v.o = val ; } MLP_EXTERN_C mlp_value *mlp_value_new_from_object(mlp_object *val){ return new mlp_value(val) ; } mlp_value::mlp_value(const mlp_value &val): t(val.t), thrown(val.thrown){ stringy = (val.stringy != NULL ? strdup(val.stringy) : NULL) ; switch (t){ case MLP_INT: v.i = val.v.i ; break ; case MLP_LONG: v.l = val.v.l ; break ; case MLP_DOUBLE: v.d = val.v.d ; break ; case MLP_STRING: v.s = (val.v.s != NULL ? strdup(val.v.s) : NULL) ; break ; case MLP_OBJECT: v.o = val.v.o ; break ; } } mlp_value::~mlp_value(){ switch (t){ case MLP_STRING: if (v.s != NULL){ free(v.s) ; } break ; } if (stringy != NULL){ free(stringy) ; } } MLP_EXTERN_C void mlp_value_delete(mlp_value *val){ delete val ; } int mlp_value::to_int(){ return (int)to_long() ; } MLP_EXTERN_C int mlp_value_to_int(mlp_value *val){ return val->to_int() ; } long mlp_value::to_long(){ switch (t){ case MLP_INT: return (int)v.i ; case MLP_LONG: return v.l ; case MLP_DOUBLE: return (long)v.d ; case MLP_STRING: return (v.s == NULL ? 0L : strtol(v.s, NULL, 10)) ; default: return 0L ; } } MLP_EXTERN_C long mlp_value_to_long(mlp_value *val){ return val->to_long() ; } double mlp_value::to_double(){ switch (t){ case MLP_INT: return (double)v.i ; case MLP_LONG: return (double)v.l ; case MLP_DOUBLE: return v.d ; case MLP_STRING: return (v.s == NULL ? NAN : strtod(v.s, NULL)) ; default: return NAN ; } } MLP_EXTERN_C double mlp_value_to_double(mlp_value *val){ return val->to_double() ; } const char *mlp_value::to_string(){ if (stringy != NULL){ return stringy ; } switch (t){ case MLP_STRING: return v.s ; case MLP_INT: { char num[1024] ; snprintf(num, 1023, "%d", v.i) ; stringy = strdup(num) ; return stringy ; } case MLP_LONG: { char num[1024] ; snprintf(num, 1023, "%ld", v.l) ; stringy = strdup(num) ; return stringy ; } case MLP_DOUBLE: { char num[1024] ; snprintf(num, 1023, "%.15lf", v.d) ; stringy = strdup(num) ; return stringy ; } default: return NULL ; } } MLP_EXTERN_C const char *mlp_value_to_string(mlp_value *val){ return val->to_string() ; } const mlp_object *mlp_value::to_object(){ switch (t){ case MLP_INT: case MLP_LONG: case MLP_DOUBLE: case MLP_STRING: return NULL ; default: return v.o ; } } MLP_EXTERN_C const mlp_object *mlp_value_to_object(mlp_value *val){ return val->to_object() ; } mlp_type mlp_value::get_type(){ return t ; } MLP_EXTERN_C mlp_type mlp_value_get_type(mlp_value *val){ return val->get_type() ; }