[ObjectHandler-cvs] ObjectHandler/ohxl conversions.hpp,1.1,1.2
Brought to you by:
ericehlers,
nando
From: Ferdinando A. <na...@us...> - 2006-05-23 12:08:53
|
Update of /cvsroot/objecthandler/ObjectHandler/ohxl In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25818/ohxl Modified Files: conversions.hpp Log Message: assorted fixes Index: conversions.hpp =================================================================== RCS file: /cvsroot/objecthandler/ObjectHandler/ohxl/conversions.hpp,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** conversions.hpp 19 May 2006 15:12:41 -0000 1.1 --- conversions.hpp 23 May 2006 12:08:48 -0000 1.2 *************** *** 91,115 **** template < class T > ! void fpToVector(std::vector < T > &ret, const FP *fpVector) { ! for (int i=0; i<fpVector->rows * fpVector->columns; i++) ! ret.push_back(fpVector->array[i]); } template < class T > ! void operToVector(std::vector < T > &ret, const OPER *xVector) { OPER xTemp; bool needToFree = false; try { ! if (xVector->xltype & xltypeErr) throw Exception("input value is #NULL (xltypeErr)"); ! if (xVector->xltype & (xltypeMissing | xltypeNil)) ! return; const OPER *xMulti; ! if (xVector->xltype == xltypeMulti) ! xMulti = xVector; else { ! Excel(xlCoerce, &xTemp, 2, xVector, TempInt(xltypeMulti)); xMulti = &xTemp; needToFree = true; --- 91,118 ---- template < class T > ! std::vector < T > fpToVector(const FP &fpVector) { ! std::vector < T > ret; ! for (int i=0; i<fpVector.rows * fpVector.columns; i++) ! ret.push_back(fpVector.array[i]); ! return ret; } template < class T > ! std::vector < T > operToVector(const OPER &xVector) { OPER xTemp; bool needToFree = false; try { ! std::vector < T > ret; ! if (xVector.xltype & xltypeErr) throw Exception("input value is #NULL (xltypeErr)"); ! if (xVector.xltype & (xltypeMissing | xltypeNil)) ! return ret; const OPER *xMulti; ! if (xVector.xltype == xltypeMulti) ! xMulti = &xVector; else { ! Excel(xlCoerce, &xTemp, 2, &xVector, TempInt(xltypeMulti)); xMulti = &xTemp; needToFree = true; *************** *** 125,128 **** --- 128,132 ---- Excel(xlFree, 0, 1, &xTemp); + return ret; } catch (const std::exception &e) { if (needToFree) *************** *** 135,167 **** template < class T > ! void fpToMatrix( ! std::vector < std::vector < T > > &ret, ! const FP *fpMatrix) { ! for (int i=0; i<fpMatrix->rows; i++) { std::vector < T > row; ! for (int j=0; j<fpMatrix->columns; j++) ! row.push_back(fpMatrix->array[i * fpMatrix->columns + j]); ret.push_back(row); } } template < class T > ! void operToMatrix( ! std::vector < std::vector < T > > &ret, ! const OPER *xMatrix) { OPER xTemp; bool needToFree = false; try { ! if (xMatrix->xltype & xltypeErr) throw Exception("input value is #NULL (xltypeErr)"); ! if (xMatrix->xltype & (xltypeMissing | xltypeNil)) ! return; const OPER *xMulti; ! if (xMatrix->xltype == xltypeMulti) ! xMulti = xMatrix; else { ! Excel(xlCoerce, &xTemp, 2, xMatrix, TempInt(xltypeMulti)); xMulti = &xTemp; needToFree = true; --- 139,170 ---- template < class T > ! std::vector < std::vector < T > > fpToMatrix(const FP &fpMatrix) { ! std::vector < std::vector < T > > ret; ! for (int i=0; i<fpMatrix.rows; i++) { std::vector < T > row; ! for (int j=0; j<fpMatrix.columns; j++) ! row.push_back(fpMatrix.array[i * fpMatrix.columns + j]); ret.push_back(row); } + return ret; } template < class T > ! std::vector < std::vector < T > > operToMatrix(const OPER &xMatrix) { OPER xTemp; bool needToFree = false; try { ! std::vector < std::vector < T > > ret, ! if (xMatrix.xltype & xltypeErr) throw Exception("input value is #NULL (xltypeErr)"); ! if (xMatrix.xltype & (xltypeMissing | xltypeNil)) ! return ret; const OPER *xMulti; ! if (xMatrix.xltype == xltypeMulti) ! xMulti = &xMatrix; else { ! Excel(xlCoerce, &xTemp, 2, &xMatrix, TempInt(xltypeMulti)); xMulti = &xTemp; needToFree = true; *************** *** 181,184 **** --- 184,188 ---- Excel(xlFree, 0, 1, &xTemp); + return ret; } catch (const std::exception &e) { if (needToFree) *************** *** 190,194 **** --- 194,307 ---- } + // convert excel datatypes to datatypes of underlying library + + template < typename T > + T operToScalarLibrary(const long &arg) { + if (arg) + return T(arg); + else + return T(); + } + + template < class T > + T operToScalarLibrary(const OPER &xArg, const long &defaultValue=0) { + long arg; + operToScalar(arg, xArg, defaultValue); + return operToScalarLibrary<T>(arg); + } + + template < class T > + std::vector < T > operToVectorLibrary(const OPER &xVector) { + OPER xTemp; + bool needToFree = false; + try { + std::vector < T > ret; + if (xVector.xltype & xltypeErr) + throw Exception("input value is #NULL (xltypeErr)"); + if (xVector.xltype & (xltypeMissing | xltypeNil)) + return ret; + + const OPER *xMulti; + + if (xVector.xltype == xltypeMulti) + xMulti = &xVector; + else { + Excel(xlCoerce, &xTemp, 2, &xVector, TempInt(xltypeMulti)); + xMulti = &xTemp; + needToFree = true; + } + + for (int i=0; i<xMulti->val.array.rows * xMulti->val.array.columns; i++) + ret.push_back(operToScalarLibrary<T>(xMulti->val.array.lparray[i])); + + if (needToFree) + Excel(xlFree, 0, 1, &xTemp); + + return ret; + } catch (const std::exception &e) { + if (needToFree) + Excel(xlFree, 0, 1, &xTemp); + std::ostringstream msg; + msg << "operToVectorLibrary: " << e.what(); + throw Exception(msg.str().c_str()); + } + } + + template < class T > + std::vector < T > operToVectorLibrary(const FP &fpVector) { + std::vector < T > ret; + for (int i=0; i<fpVector.rows * fpVector.columns; i++) + ret.push_back(operToScalarLibrary<T>(fpVector.array[i])); + return ret; + } + + // convert excel types to enumerations + + template < typename T, typename EnumRegistry > + T operToScalarEnum(const OPER &xScalar, const std::string &defaultValue="") { + std::string id; + operToScalar(id, xScalar, defaultValue); + return EnumRegistry()(id); + } + + template < typename T, typename EnumRegistry > + std::vector < T > operToVectorEnum(const OPER &xVector) { + std::vector < T > ret; + OPER xTemp; + bool needToFree = false; + try { + if (xVector.xltype & xltypeErr) + throw Exception("input value is #NULL (xltypeErr)"); + if (xVector.xltype & (xltypeMissing | xltypeNil)) + return ret; + + const OPER *xMulti; + + if (xVector.xltype == xltypeMulti) + xMulti = &xVector; + else { + Excel(xlCoerce, &xTemp, 2, &xVector, TempInt(xltypeMulti)); + xMulti = &xTemp; + needToFree = true; + } + + for (int i=0; i<xMulti->val.array.rows * xMulti->val.array.columns; i++) + ret.push_back(operToScalarEnum<T, EnumRegistry>(xMulti->val.array.lparray[i])); + + if (needToFree) + Excel(xlFree, 0, 1, &xTemp); + + return ret; + } catch (const std::exception &e) { + if (needToFree) + Excel(xlFree, 0, 1, &xTemp); + std::ostringstream msg; + msg << "operToVector: " << e.what(); + throw Exception(msg.str().c_str()); + } + } + } #endif + |