// Copyright (C) 2003 Dominic Letourneau (dominic.letourneau@usherbrooke.ca) #include "ObjectRef.h" #include "Vector.h" #include "operators.h" #include "net_types.h" #include "vmethod.h" #include #include "typemap.h" #include "conversion.h" #include "Complex.h" #include #include "Matrix.h" using namespace std; namespace FD { //@implements core TypeMap > &Conversion::conv_table() { static TypeMap > table; return table; } /*This doesn't compile with MSVC++ broken templates*/ #ifndef BROKEN_TEMPLATES ObjectRef ReturnNilObject(ObjectRef in) { return nilObject; } template ObjectRef CTypeConversion(ObjectRef in) { RCPtr ObjectValue = in; return ObjectRef(U::alloc(static_cast(ObjectValue->val()))); } template ObjectRef CTypeStringConversion(ObjectRef in) { ostringstream out; in->prettyPrint(out); return ObjectRef(new U(out.str())); } template ObjectRef StringCTypeConversion(ObjectRef in) { RCPtr ObjectValue = in; istringstream my_stream; my_stream.str(*ObjectValue); typename U::basicType value; my_stream >> value; return ObjectRef(new U(value)); } template ObjectRef CTypeVectorConversion (ObjectRef in) { RCPtr FromCType = in; RCPtr ToVector(U::alloc(1)); (*ToVector)[0] = static_cast(FromCType->val()); return ToVector; } template ObjectRef VectorVectorConversion (ObjectRef in) { RCPtr FromVector = in; RCPtr ToVector(U::alloc(FromVector->size())); for (int i = 0; i < ToVector->size(); i++) { (*ToVector)[i] = static_cast((*FromVector)[i]); } return ToVector; } template ObjectRef MatrixMatrixConversion (ObjectRef in) { RCPtr FromMatrix = in; RCPtr ToMatrix( new U(FromMatrix->nrows(), FromMatrix->ncols())); for (int i = 0; i < ToMatrix->nrows(); i++) { for (int j = 0; j < ToMatrix->ncols(); j++) { (*ToMatrix)(i,j) = static_cast((*FromMatrix)(i,j)); } } return ToMatrix; } template ObjectRef VectorMatrixConversion (ObjectRef in) { RCPtr FromVector = in; RCPtr ToMatrix( new U(1,FromVector->size())); for (int i = 0; i < FromVector->size(); i++) { (*ToMatrix)(0,i) = static_cast((*FromVector)[i]); } return ToMatrix; } template ObjectRef CTypeMatrixConversion (ObjectRef in) { RCPtr FromCType = in; RCPtr ToMatrix(new U(1,1)); (*ToMatrix)(0,0) = static_cast(FromCType->val()); return ToMatrix; } //(DL) 17/02/2004 //Commented conversions that make no sense to be implemented (?) //to Bool conversion REGISTER_CONVERSION_TEMPLATE(Bool, Bool, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Int, Bool, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Float, Bool, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Double, Bool, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(String, Bool, StringCTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Bool, CTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Bool, CTypeConversion); REGISTER_CONVERSION(NilObject, Bool, ReturnNilObject); //to Int conversion REGISTER_CONVERSION_TEMPLATE(Bool, Int, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Int, Int, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Float, Int, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Double, Int, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(String, Int, StringCTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Int, CTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Int, CTypeConversion); REGISTER_CONVERSION(NilObject, Int, ReturnNilObject); //to Float conversion REGISTER_CONVERSION_TEMPLATE(Bool, Float, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Int, Float, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Float, Float, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Double, Float, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(String, Float, StringCTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Float, CTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Float, CTypeConversion); REGISTER_CONVERSION(NilObject, Float, ReturnNilObject); //to Double conversion REGISTER_CONVERSION_TEMPLATE(Bool, Double, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Int, Double, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Float, Double, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Double, Double, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(String, Double, StringCTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Double, CTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Double, CTypeConversion); REGISTER_CONVERSION(NilObject, Double, ReturnNilObject); //to Complex conversion REGISTER_CONVERSION_TEMPLATE(Bool, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Int, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Float, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Double, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(String, Complex, StringCTypeConversion); REGISTER_CONVERSION_TEMPLATE(Complex, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(NetCType >, Complex, CTypeConversion); //REGISTER_CONVERSION_TEMPLATE(Complex, Complex, CTypeConversion); REGISTER_CONVERSION(NilObject, Complex, ReturnNilObject); //to Complex conversion REGISTER_CONVERSION_TEMPLATE(Bool, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Int, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Float, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Double, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(String, Complex, StringCTypeConversion); REGISTER_CONVERSION_TEMPLATE(Complex, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(Complex, Complex, CTypeConversion); REGISTER_CONVERSION_TEMPLATE(NetCType >,Complex,CTypeConversion); REGISTER_CONVERSION_TEMPLATE(NetCType >,Complex,CTypeConversion); REGISTER_CONVERSION(NilObject, Complex, ReturnNilObject); //to String conversion REGISTER_CONVERSION_TEMPLATE(Bool, String, CTypeStringConversion); REGISTER_CONVERSION_TEMPLATE(Int, String, CTypeStringConversion); REGISTER_CONVERSION_TEMPLATE(Float, String, CTypeStringConversion); REGISTER_CONVERSION_TEMPLATE(Double, String, CTypeStringConversion); REGISTER_CONVERSION_TEMPLATE(Complex, String, CTypeStringConversion); REGISTER_CONVERSION_TEMPLATE(Complex, String, CTypeStringConversion); REGISTER_CONVERSION_TEMPLATE(String, String, CTypeStringConversion); REGISTER_CONVERSION(NilObject, String, ReturnNilObject); //CType to Vector conversion //REGISTER_CONVERSION_TEMPLATE(Bool, Vector, CTypeVectorConversion); REGISTER_CONVERSION_TEMPLATE(Int, Vector, CTypeVectorConversion); REGISTER_CONVERSION_TEMPLATE(Float, Vector, CTypeVectorConversion); REGISTER_CONVERSION_TEMPLATE(Double, Vector, CTypeVectorConversion); REGISTER_CONVERSION_TEMPLATE(Complex, Vector >, CTypeVectorConversion); REGISTER_CONVERSION_TEMPLATE(Complex, Vector >, CTypeVectorConversion); //CType to Matrix conversion //REGISTER_CONVERSION_TEMPLATE(Bool, Matrix, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Int, Matrix, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Float, Matrix, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Double, Matrix, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Complex, Matrix >, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Complex, Matrix >, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Float, Matrix >, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Float, Matrix >, CTypeMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Double, Matrix >, CTypeMatrixConversion); //Vector to Vector conversion REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector >, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector >, Vector >, VectorVectorConversion); REGISTER_CONVERSION_TEMPLATE(Vector >, Vector >, VectorVectorConversion); //NilObject to Vector returns nilObject... REGISTER_CONVERSION(NilObject, Vector, ReturnNilObject); REGISTER_CONVERSION(NilObject, Vector, ReturnNilObject); REGISTER_CONVERSION(NilObject, Vector, ReturnNilObject); REGISTER_CONVERSION(NilObject, Vector >, ReturnNilObject); REGISTER_CONVERSION(NilObject, Vector >, ReturnNilObject); //Matrix to Matrix conversion REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix >, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix >, Matrix >, MatrixMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Matrix >, Matrix >, MatrixMatrixConversion); //Vector to Matrix conversion REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector >, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector >, Matrix >, VectorMatrixConversion); REGISTER_CONVERSION_TEMPLATE(Vector >, Matrix >, VectorMatrixConversion); //NilObject to Matrix returns nilObject... REGISTER_CONVERSION(NilObject, Matrix, ReturnNilObject); REGISTER_CONVERSION(NilObject, Matrix, ReturnNilObject); REGISTER_CONVERSION(NilObject, Matrix, ReturnNilObject); REGISTER_CONVERSION(NilObject, Matrix >, ReturnNilObject); REGISTER_CONVERSION(NilObject, Matrix >, ReturnNilObject); //(DL) 17/02/2004 //OLD conversion (to be removed someday) //Int conversion template ObjectRef IntCTypeConversion(ObjectRef in) { typedef typename T::basicType BaseType; BaseType f=dereference_cast (in); return ObjectRef(Int::alloc((int)f)); } ObjectRef IntStringConversion(ObjectRef in) { String my_string = object_cast(in); return ObjectRef(Int::alloc(atoi(my_string.c_str()))); } REGISTER_VTABLE0(toInt, Int, IntCTypeConversion, 1); REGISTER_VTABLE0(toInt, Bool, IntCTypeConversion, 2); REGISTER_VTABLE0(toInt, Float, IntCTypeConversion, 3); REGISTER_VTABLE0(toInt, Double, IntCTypeConversion, 4); REGISTER_VTABLE0(toInt, String, IntStringConversion, 5); REGISTER_VTABLE0(toInt, NilObject, ReturnNilObject, 6); //DL 09/06/2004 //Convenient way to transform a Vector to Vector with the toInt subnet static int dummy_template_vtable_init_for_vector_int_to_vector_int = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toInt"); static int dummy_template_vtable_init_for_vector_float_to_vector_int = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toInt"); static int dummy_template_vtable_init_for_vector_double_to_vector_int = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toInt"); //Bool conversion template ObjectRef BoolCTypeConversion(ObjectRef in) { typedef typename T::basicType BaseType; BaseType f=dereference_cast (in); return ObjectRef(Bool::alloc((bool)f)); } ObjectRef BoolStringConversion(ObjectRef in) { String my_string = object_cast(in); return ObjectRef(Bool::alloc((bool)atoi(my_string.c_str()))); } REGISTER_VTABLE0(toBool, Int, BoolCTypeConversion, 1); REGISTER_VTABLE0(toBool, Bool, BoolCTypeConversion, 2); REGISTER_VTABLE0(toBool, Float, BoolCTypeConversion, 3); REGISTER_VTABLE0(toBool, Double, BoolCTypeConversion, 4); REGISTER_VTABLE0(toBool, String, BoolStringConversion, 5); REGISTER_VTABLE0(toBool, NilObject, ReturnNilObject, 6); //Float conversion template ObjectRef FloatCTypeConversion(ObjectRef in) { typedef typename T::basicType BaseType; BaseType f=dereference_cast (in); return ObjectRef(Float::alloc((float)f)); } ObjectRef FloatStringConversion(ObjectRef in) { String my_string = object_cast(in); return ObjectRef(Float::alloc((float)atof(my_string.c_str()))); } REGISTER_VTABLE0(toFloat, Int, FloatCTypeConversion, 1); REGISTER_VTABLE0(toFloat, Bool, FloatCTypeConversion, 2); REGISTER_VTABLE0(toFloat, Float, FloatCTypeConversion, 3); REGISTER_VTABLE0(toFloat, Double, FloatCTypeConversion, 4); REGISTER_VTABLE0(toFloat, String, FloatStringConversion, 5); REGISTER_VTABLE0(toFloat, NilObject, ReturnNilObject, 6); //DL 09/06/2004 //Convenient way to transform a Vector to Vector with the toFloat subnet static int dummy_template_vtable_init_for_vector_int_to_vector_float = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toFloat"); static int dummy_template_vtable_init_for_vector_float_to_vector_float = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toFloat"); static int dummy_template_vtable_init_for_vector_double_to_vector_float = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toFloat"); //Double conversion template ObjectRef DoubleCTypeConversion(ObjectRef in) { typedef typename T::basicType BaseType; BaseType f=dereference_cast (in); return ObjectRef(Double::alloc((double)f)); } ObjectRef DoubleStringConversion(ObjectRef in) { String my_string = object_cast(in); return ObjectRef(Double::alloc((double)atof(my_string.c_str()))); } REGISTER_VTABLE0(toDouble, Int, DoubleCTypeConversion, 1); REGISTER_VTABLE0(toDouble, Bool, DoubleCTypeConversion, 2); REGISTER_VTABLE0(toDouble, Float, DoubleCTypeConversion, 3); REGISTER_VTABLE0(toDouble, Double, DoubleCTypeConversion, 4); REGISTER_VTABLE0(toDouble, String, DoubleStringConversion, 5); REGISTER_VTABLE0(toDouble, NilObject, ReturnNilObject, 6); //DL 09/06/2004 //Convenient way to transform a Vector to Vector with the toDouble subnet static int dummy_template_vtable_init_for_vector_int_to_vector_double = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toDouble"); static int dummy_template_vtable_init_for_vector_float_to_vector_double = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toDouble"); static int dummy_template_vtable_init_for_vector_double_to_vector_double = \ vmethod()->registerFunct0(VectorVectorConversion,Vector >,&typeid(Vector),"toDouble"); //String conversion template ObjectRef StringCTypeConversion(ObjectRef in) { typedef typename T::basicType BaseType; BaseType f=dereference_cast (in); ostringstream my_stream; my_stream<(in); return ObjectRef(new String(my_string)); } REGISTER_VTABLE0(toString, Int, StringCTypeConversion, 1); REGISTER_VTABLE0(toString, Bool, StringCTypeConversion, 2); REGISTER_VTABLE0(toString, Float, StringCTypeConversion, 3); REGISTER_VTABLE0(toString, Double, StringCTypeConversion, 4); REGISTER_VTABLE0(toString, String, StringStringConversion, 5); REGISTER_VTABLE0(toString, NilObject, ReturnNilObject, 6); //Conversion to vector from a basic C type template ObjectRef VectorCTypeConversion(ObjectRef in) { typedef typename T::basicType BaseType; BaseType value=dereference_cast (in); //create vector Vector *vect = new Vector(1,value); //return vector return ObjectRef(vect); } //Conversion to vector from a string ObjectRef VectorStringConversion(ObjectRef in) { String &value = object_cast(in); //create vector Vector *vect = new Vector(1,value); //return vector return ObjectRef(vect); } REGISTER_VTABLE0(toVect, Int, VectorCTypeConversion, 1); //REGISTER_VTABLE0(toVect, Bool, VectorCTypeConversion, 2); REGISTER_VTABLE0(toVect, Float, VectorCTypeConversion, 3); REGISTER_VTABLE0(toVect, Double, VectorCTypeConversion, 4); REGISTER_VTABLE0(toVect, String, VectorStringConversion, 5); REGISTER_VTABLE0(toVect, NilObject, ReturnNilObject, 6); }//namespace FD #endif //BROKEN_TEMPLATES