From 8baef999da28d8a51734994219514e20fe92a2d2 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 22 Feb 2017 16:38:02 -0500 Subject: switch to C++ --- Makefile | 2 +- cpp_layer/curve.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ setup.py | 9 ++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 cpp_layer/curve.hpp diff --git a/Makefile b/Makefile index c0eb427..82696cb 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ build: python setup.py build_ext --inplace clean: - find pyisda \( -name *.c -o -name \*.h -o -name \*.so \) -exec rm {} \; + find pyisda \( -name *.c -o -name *.h -o -name *.so -o -name *.cpp -o -name *.html \) -exec rm {} \; rm -rf build install: diff --git a/cpp_layer/curve.hpp b/cpp_layer/curve.hpp new file mode 100644 index 0000000..8e54216 --- /dev/null +++ b/cpp_layer/curve.hpp @@ -0,0 +1,42 @@ +#include +typedef long TDate; + +class CurveObject { +public: + CurveObject(TDate baseDate, std::vector dates, std::vector rates, + double basis, long dayCountConv) { + _ptr = JpmcdsMakeTCurve(baseDate, dates.data(), rates.data(), dates.size(), + basis, dayCountConv); + } + CurveObject(TCurve* ptr) { + _ptr = ptr; + } + CurveObject(const CurveObject& other) { + _ptr = JpmcdsCopyCurve(other._ptr); + } + CurveObject(CurveObject&& other) : _ptr(other._ptr) { + other._ptr = nullptr; + } + CurveObject& operator=(const CurveObject& other) { + if( this != &other) { + JpmcdsFreeTCurve(_ptr); + _ptr = JpmcdsCopyCurve(other._ptr); + } + return *this; + } + CurveObject& operator=(CurveObject&& other) { + if( this != &other) { + JpmcdsFreeTCurve(_ptr); + _ptr = other._ptr; + other._ptr = nullptr; + } + return *this; + } + ~CurveObject() { + if(_ptr != nullptr) { + JpmcdsFreeTCurve(_ptr); + } + } +private: + TCurve* _ptr; +}; diff --git a/setup.py b/setup.py index dd76e0d..da9a315 100644 --- a/setup.py +++ b/setup.py @@ -4,13 +4,16 @@ from Cython.Build import cythonize import numpy all_extensions = Extension("*", ["pyisda/*.pyx"], - include_dirs = ['c_layer', numpy.get_include()], - libraries = ["cds"]) + include_dirs = ['cpp_layer', 'c_layer', numpy.get_include()], + libraries = ["cds"], + language = 'c++') c_extension = Extension("pyisda.flat_hazard", include_dirs = ['c_layer', numpy.get_include()], sources = ['pyisda/flat_hazard.pyx', 'c_layer/cdsbootstrap.c'], - libraries = ['cds']) + libraries = ['cds'], + language = 'c++') + all_extensions = cythonize([c_extension, all_extensions], nthreads = 4, compiler_directives={'embedsignature':True}) -- cgit v1.2.3-70-g09d2