Update of /cvsroot/quantlibaddin/QuantLibAddin/qlo/Conversions
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv23547/qlo/Conversions
Modified Files:
conversions.cpp conversions.hpp
Added Files:
all.hpp enumorobject.hpp handletoobject.hpp objecttohandle.hpp
Log Message:
support for coercion
--- NEW FILE: all.hpp ---
/*
Copyright (C) 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef qlo_conversions_all_hpp
#define qlo_conversions_all_hpp
#include <oh/Conversions/coerce.hpp>
#include <qlo/Conversions/conversions.hpp>
#include <qlo/Conversions/enumorobject.hpp>
#include <qlo/Conversions/handletoobject.hpp>
#include <qlo/Conversions/objecttohandle.hpp>
#endif
Index: conversions.hpp
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/Conversions/conversions.hpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** conversions.hpp 16 Nov 2006 12:39:22 -0000 1.1
--- conversions.hpp 19 Nov 2006 13:08:48 -0000 1.2
***************
*** 17,27 ****
*/
! /*! \file
! \brief Miscellaneous utility functions available to clients
! of %QuantLibAddin i.e. the Addins
! */
!
! #ifndef qla_conversions_hpp
! #define qla_conversions_hpp
#include <qlo/qladdindefines.hpp>
--- 17,22 ----
*/
! #ifndef qlo_conversions_hpp
! #define qlo_conversions_hpp
#include <qlo/qladdindefines.hpp>
***************
*** 29,169 ****
#include <ql/calendar.hpp>
#include <ql/interestrate.hpp>
- #include <ql/handle.hpp>
- #include <oh/objecthandler.hpp>
- #include <qlo/handle.hpp>
- #include <qlo/typefactory.hpp>
#include <vector>
! // Accept an id of an Object in the Repository and return a QuantLib::Handle<qlClass>.
! // 1) If the id is an empty string then return an empty handle
! // 2) If the Object is of class QuantLibAddin::Handle then return the contained QuantLib::Handle<qlClass>
! // 3) If the Object is of class QuantLibAddin::qloClass then convert it to a QuantLib::Handle<qlClass>
! // 4) Otherwise the Object is of an unexpected class so raise an exception
!
! template <class qlClass, class qloClass>
! inline QuantLib::Handle<qlClass> libToHandle(const std::string &id) {
! if (id.empty()) {
! return QuantLib::Handle<qlClass>();
! }
!
! OH_GET_OBJECT(objectPointer, id, ObjHandler::Object)
! boost::shared_ptr<QuantLibAddin::Handle<qlClass> > handlePointer =
! boost::dynamic_pointer_cast<QuantLibAddin::Handle<qlClass> >(objectPointer);
!
! if (handlePointer) {
! return handlePointer->getHandle();
! } else {
! boost::shared_ptr<qloClass> qloPointer =
! boost::dynamic_pointer_cast<qloClass>(objectPointer);
!
! if (qloPointer) {
! boost::shared_ptr<qlClass> ret;
! qloPointer->getLibraryObject(ret);
! return QuantLib::Handle<qlClass>(ret);
! } else {
! std::ostringstream msg;
! msg << "error retrieving object with id '" << id
! << "' from repository - this object could be converted neither to"
! << std::endl << " " << typeid(QuantLib::Handle<qlClass>).name()
! << std::endl << "nor"
! << std::endl << " " << typeid(qloClass).name();
! throw ObjHandler::Exception(msg.str());
! }
! }
! }
!
! // Accept an id of an Object in the Repository and return a boost::shared_ptr<qlClass>.
! // 1) If the Object is of class QuantLibAddin::Handle then return the contained boost::shared_ptr<qlClass>
! // 2) If the Object is of class QuantLibAddin::qloClass then return the contained boost::shared_ptr<qlClass>
! // 3) Otherwise the Object is of an unexpected class so raise an exception
!
! template <class qlClass2, class qlClass, class qloClass>
! inline boost::shared_ptr<qlClass> handleToLib(const std::string &id) {
!
! OH_GET_OBJECT(objectPointer, id, ObjHandler::Object)
! boost::shared_ptr<QuantLibAddin::Handle<qlClass2> > handlePointer =
! boost::dynamic_pointer_cast<QuantLibAddin::Handle<qlClass2> >(objectPointer);
!
! if (handlePointer) {
! boost::shared_ptr<qlClass2> qlPointer2 =
! handlePointer->getHandle().currentLink();
! if (!qlPointer2) {
! std::ostringstream msg;
! msg << "error retrieving object with id '" << id
! << "' from repository - unable to retrieve reference "
! << "contained in handle";
! throw ObjHandler::Exception(msg.str());
! }
! boost::shared_ptr<qlClass> qlPointer1 =
! boost::dynamic_pointer_cast<qlClass>(qlPointer2);
! if (qlPointer1)
! return qlPointer1;
! else {
! std::ostringstream msg;
! msg << "error retrieving object with id '" << id
! << "' from repository - unable to convert class from "
! << " " << typeid(qlClass2).name()
! << std::endl << " to "
! << " " << typeid(qlClass).name();
! throw ObjHandler::Exception(msg.str());
! }
! } else {
! boost::shared_ptr<qloClass> qloPointer =
! boost::dynamic_pointer_cast<qloClass>(objectPointer);
! if (qloPointer) {
! boost::shared_ptr<qlClass> ret;
! qloPointer->getLibraryObject(ret);
! return ret;
! } else {
! std::ostringstream msg;
! msg << "error retrieving object with id '" << id
! << "' from repository - this object could be converted neither "
! << " to " << std::endl << " " << typeid(QuantLib::Handle<qlClass>).name()
! << " nor " << std::endl << " " << typeid(qloClass).name();
! throw ObjHandler::Exception(msg.str());
! }
! }
! }
!
! // enumOrObject - accept a string which is the ID of either
! // and Enumerated Class or an object in the repository,
! // and return the appropriate object
! template <class enumClass, class qlClass, class qloClass>
! inline boost::shared_ptr<qlClass> enumOrObject(
! const std::string &id) {
! QL_REQUIRE(!id.empty(), "attempt to retrieve object with null string as ID");
! if (QuantLibAddin::Create<boost::shared_ptr<enumClass> >().checkType(id)) {
! boost::shared_ptr<enumClass> ret =
! QuantLibAddin::Create<boost::shared_ptr<enumClass> >()(id);
! boost::shared_ptr<qlClass> ret2 =
! boost::dynamic_pointer_cast<qlClass>(ret);
! if (ret2)
! return ret2;
! else
! QL_FAIL("Unable to convert enumerated class with ID " << id <<
! " to class " << typeid(qloClass).name());
! } else {
! OH_GET_REFERENCE(ret, id, qloClass, qlClass)
! return ret;
! }
! }
!
! template <class enumClass, class qlClass, class qloClass>
! inline std::vector<boost::shared_ptr<qlClass> > enumOrObjectVector(
! const std::vector<std::string> &ids) {
! std::vector<boost::shared_ptr<qlClass> > ret;
! for (std::vector<std::string>::const_iterator i = ids.begin();
! i != ids.end(); i++)
! ret.push_back(enumOrObject<enumClass, qlClass, qloClass>(*i));
! return ret;
! }
!
! namespace QuantLibAddin {
!
! /*! conversions
! Conversion of library types to C++ types.
- Called by autogenerated addin code.
- */
double libraryToScalar(const QuantLib::InterestRate&);
double libraryToScalar(const QuantLib::Rate&);
--- 24,31 ----
#include <ql/calendar.hpp>
#include <ql/interestrate.hpp>
#include <vector>
! namespace ObjHandler {
double libraryToScalar(const QuantLib::InterestRate&);
double libraryToScalar(const QuantLib::Rate&);
--- NEW FILE: handletoobject.hpp ---
/*
Copyright (C) 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef qlo_conversions_handletoobject_hpp
#define qlo_conversions_handletoobject_hpp
#include <oh/Conversions/coerce.hpp>
#include <oh/exception.hpp>
#include <qlo/handle.hpp>
namespace ObjHandler {
// Accept an id of an Object in the Repository and return a boost::shared_ptr<qlClass>.
// 1) If the Object is of class QuantLibAddin::Handle then return the contained boost::shared_ptr<qlClass>
// 2) If the Object is of class QuantLibAddin::qloClass then return the contained boost::shared_ptr<qlClass>
// 3) Otherwise the Object is of an unexpected class so raise an exception
template <class qlClass, class qlClass2>
bool referenceFromHandle(
const boost::shared_ptr<ObjHandler::Object> &in,
boost::shared_ptr<qlClass> &out) {
boost::shared_ptr<QuantLibAddin::Handle<qlClass2> > handlePointer =
boost::dynamic_pointer_cast<QuantLibAddin::Handle<qlClass2> >(in);
if (!handlePointer)
return false;
boost::shared_ptr<qlClass2> qlPointer2 =
handlePointer->getHandle().currentLink();
OH_REQUIRE(qlPointer2, "unable to retrieve reference"
<< " contained in handle");
out = boost::dynamic_pointer_cast<qlClass>(qlPointer2);
OH_REQUIRE(out, "unable to convert class from" << std::endl
<< " " << typeid(qlClass2).name()
<< std::endl << "to" << std::endl
<< " " << typeid(qlClass).name());
return true;
}
template <class qlClass, class qloClass>
bool referenceFromObject(
const boost::shared_ptr<ObjHandler::Object> &in,
boost::shared_ptr<qlClass> &out) {
boost::shared_ptr<qloClass> qloPointer =
boost::dynamic_pointer_cast<qloClass>(in);
if (qloPointer) {
qloPointer->getLibraryObject(out);
return true;
} else {
return false;
}
}
template <class qlClass2, class qlClass, class qloClass>
class CoerceHandleToObject : public ObjHandler::Coerce<
boost::shared_ptr<ObjHandler::Object>,
boost::shared_ptr<qlClass> > {
Conversion *getConversions() {
static Conversion conversions[] = {
referenceFromHandle<qlClass, qlClass2>,
referenceFromObject<qlClass, qloClass>,
0
};
return conversions;
};
};
}
#endif
Index: conversions.cpp
===================================================================
RCS file: /cvsroot/quantlibaddin/QuantLibAddin/qlo/Conversions/conversions.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** conversions.cpp 16 Nov 2006 12:39:22 -0000 1.1
--- conversions.cpp 19 Nov 2006 13:08:47 -0000 1.2
***************
*** 22,26 ****
#include <qlo/Conversions/conversions.hpp>
! namespace QuantLibAddin {
double libraryToScalar(const QuantLib::InterestRate &i) {
--- 22,26 ----
#include <qlo/Conversions/conversions.hpp>
! namespace ObjHandler {
double libraryToScalar(const QuantLib::InterestRate &i) {
--- NEW FILE: objecttohandle.hpp ---
/*
Copyright (C) 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef qlo_conversions_objecttohandle_hpp
#define qlo_conversions_objecttohandle_hpp
#include <oh/Conversions/coerce.hpp>
#include <oh/exception.hpp>
#include <qlo/handle.hpp>
namespace ObjHandler {
// Accept an id of an Object in the Repository and return a QuantLib::Handle<qlClass>.
// 1) If the id is an empty string then return an empty handle
// 2) If the Object is of class QuantLibAddin::Handle then return the contained QuantLib::Handle<qlClass>
// 3) If the Object is of class QuantLibAddin::qloClass then convert it to a QuantLib::Handle<qlClass>
// 4) Otherwise the Object is of an unexpected class so raise an exception
template <class qlClass>
bool objectToHandle(
const boost::shared_ptr<ObjHandler::Object> &in,
QuantLib::Handle<qlClass> &out) {
boost::shared_ptr<QuantLibAddin::Handle<qlClass> > handlePointer =
boost::dynamic_pointer_cast<QuantLibAddin::Handle<qlClass> >(in);
if (handlePointer) {
out = handlePointer->getHandle();
return true;
} else {
return false;
}
}
template <class qlClass, class qloClass>
bool wrapObject(
const boost::shared_ptr<ObjHandler::Object> &in,
QuantLib::Handle<qlClass> &out) {
boost::shared_ptr<qloClass> qloPointer =
boost::dynamic_pointer_cast<qloClass>(in);
if (qloPointer) {
boost::shared_ptr<qlClass> ret;
qloPointer->getLibraryObject(ret);
out = QuantLib::Handle<qlClass>(ret);
return true;
} else {
return false;
}
}
template <class qlClass, class qloClass>
class CoerceObjectToHandle : public ObjHandler::Coerce<
boost::shared_ptr<ObjHandler::Object>,
QuantLib::Handle<qlClass> > {
Conversion *getConversions() {
static Conversion conversions[] = {
objectToHandle<qlClass>,
wrapObject<qlClass, qloClass>,
0
};
return conversions;
};
};
}
#endif
--- NEW FILE: enumorobject.hpp ---
/*
Copyright (C) 2006 Eric Ehlers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email qua...@li...
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#ifndef qlo_conversions_enumorobject_hpp
#define qlo_conversions_enumorobject_hpp
#include <oh/Conversions/coerce.hpp>
#include <oh/exception.hpp>
#include <qlo/typefactory.hpp>
namespace ObjHandler {
// enumOrObject - accept a string which is the ID of either
// and Enumerated Class or an object in the repository,
// and return the appropriate object
template <class enumClass, class qlClass>
bool idToEnum(
const std::string &in,
boost::shared_ptr<qlClass> &out) {
if (!QuantLibAddin::Create<boost::shared_ptr<enumClass> >().checkType(in))
return false;
boost::shared_ptr<enumClass> ret =
QuantLibAddin::Create<boost::shared_ptr<enumClass> >()(in);
out = boost::dynamic_pointer_cast<qlClass>(ret);
OH_REQUIRE(out, "Unable to convert enumerated class with ID " << in <<
" to class " << typeid(qlClass).name());
return true;
}
template <class qloClass, class qlClass>
bool idToObject(
const std::string &in,
boost::shared_ptr<qlClass> &out) {
OH_GET_REFERENCE(ret, in, qloClass, qlClass)
out = ret;
return true;
}
template <class enumClass, class qlClass, class qloClass>
class CoerceEnumOrObject : public ObjHandler::Coerce<
const std::string,
boost::shared_ptr<qlClass> > {
Conversion *getConversions() {
static Conversion conversions[] = {
idToEnum<enumClass, qlClass>,
idToObject<qloClass, qlClass>,
0
};
return conversions;
};
};
template <class enumClass, class qlClass, class qloClass>
inline std::vector<boost::shared_ptr<qlClass> > enumOrObjectVector(
const std::vector<std::string> &ids) {
std::vector<boost::shared_ptr<qlClass> > ret;
std::vector<std::string>::const_iterator i;
for (i = ids.begin(); i != ids.end(); i++)
ret.push_back(
CoerceEnumOrObject<enumClass, qlClass, qloClass>()(*i));
return ret;
}
}
#endif
|