Update of /cvsroot/quantlibaddin/QuantLibAddin/qlo/Conversions
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv28036/qlo/Conversions
Added Files:
conversions.cpp conversions.hpp
Log Message:
consolidate conversion functions in preparation for implementation of coercion
--- NEW FILE: conversions.cpp ---
/*
Copyright (C) 2005 Plamen Neykov
Copyright (C) 2004, 2005, 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.
*/
#if defined(HAVE_CONFIG_H) // Dynamically created by configure
#include <qlo/config.hpp>
#endif
#include <qlo/Conversions/conversions.hpp>
namespace QuantLibAddin {
double libraryToScalar(const QuantLib::InterestRate &i) {
return i.rate();
}
double libraryToScalar(const QuantLib::Rate &r) {
return r;
}
long libraryToScalar(const QuantLib::Date &d) {
return d.serialNumber();
}
std::string libraryToScalar(const QuantLib::Period &period) {
std::ostringstream s;
s << period;
return s.str();
}
std::vector<std::string> libraryToVector(const std::vector<QuantLib::Period> &v) {
std::vector<std::string> ret;
for (std::vector<QuantLib::Period>::const_iterator i = v.begin();
i != v.end(); i++)
ret.push_back(libraryToScalar(*i));
return ret;
}
std::vector<long> libraryToVector(const std::vector<QuantLib::Date> &v) {
std::vector<long> ret;
for (std::vector<QuantLib::Date>::const_iterator i = v.begin();
i != v.end(); i++)
ret.push_back(i->serialNumber());
return ret;
}
std::vector<long> libraryToVector(const std::vector<QuantLib::Size> &v) {
std::vector<long> ret;
for (std::vector<QuantLib::Size>::const_iterator i = v.begin();
i != v.end(); i++)
ret.push_back(*i);
return ret;
}
}
--- NEW FILE: conversions.hpp ---
/*
Copyright (C) 2005 Plamen Neykov
Copyright (C) 2004, 2005, 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.
*/
/*! \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>
#include <ql/date.hpp>
#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&);
long libraryToScalar(const QuantLib::Date&);
std::string libraryToScalar(const QuantLib::Period&);
std::vector<long> libraryToVector(const std::vector<QuantLib::Date>&);
std::vector<long> libraryToVector(const std::vector<QuantLib::Size>&);
std::vector<std::string> libraryToVector(const std::vector<QuantLib::Period>&);
}
#endif
|