SWIG interface files

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

SWIG interface files

Andre Louw-2
Hi,

I have a cpp class in a library of my own that inherits off
QuantLib::Instrument. I now need to create a SWIG file for this.

I assumed that I would only need to "%import Instruments.i" to get the
needed definitions, this leads to all kinds of compile errors.
Using "%include Instruments.i" doesn't hack it either, redoing all the
needed "typedef" statements in my interface file solves the compile errors
but I get weird errors when using my library: accessing
DayCounter.yearFraction(d1,d2) for instance gives me an error saying it
needs 5 arguments (only 3 supplied) - it's as if it doesn't know the last 2
are default arguments.

This is my SWIG file:

#ifndef myinstr_i
#define myinstr_i

%include Instruments.i
%include TermStructures.i
%include String.i

%{
#include <myinstr.hpp>
using MyNameSpace::MyInstr;
typedef RelinkableHandle<QuantLib::TermStructure>
TermStructureRelinkableHandle;
typedef Handle<QuantLib::Instrument> InstrumentHandle;
typedef QuantLib::Handle<MyInstr> MyInstrHandle;
typedef std::string String;
%}

// fake inheritance between handles
%name(MyInstr) class MyInstrHandle
: public InstrumentHandle {
  public:
    // constructor redefined below
    ~MyInstrHandle();
};

%addmethods MyInstrHandle {
    MyInstrHandle(TermStructureRelinkableHandle tStruct,
        String isinCode, String description) {
            return new MyInstrHandle(
               new MyInstr(tStruct, isinCode, description));
    }
    double fairRate() {
       return (*self)->fairRate();
    }
}

#endif

What is the correct way of doing something like this (i.e using SWIG
interface-files from other projects)?

Any help appreciated.

Andre

 
-------------------------------------------------------------------------
This e-mail is intended only for the use of the individual or entity named
above and may contain information that is confidential and privileged,
proprietary to the company and protected by law. If you are not the intended
recipient, you are hereby notified that any dissemination, distribution or
copying of this e-mail is strictly prohibited. Opinions, conclusions and
other information in this message that do not relate to the official
business of our company shall be understood as neither given nor endorsed by
it.


Reply | Threaded
Open this post in threaded view
|

Re: SWIG interface files

Ferdinando M. Ametrano-2
Hi Andre

Luigi is the master of SWIG, but here's a few suggestions while we wait for
his answer

>I have a cpp class in a library of my own that inherits off
>QuantLib::Instrument. I now need to create a SWIG file for this.
>I assumed that I would only need to "%import Instruments.i" to get the
>needed definitions, this leads to all kinds of compile errors.
>Using "%include Instruments.i" doesn't hack it either,
I would "%include ql.i", this will include all the QuantLib interfaces and
this is probably what you want, since you will want your module to include
the whole QuantLib module to use all the QuantLib feautures.

>but I get weird errors when using my library: accessing
>DayCounter.yearFraction(d1,d2) for instance gives me an error saying it
>needs 5 arguments (only 3 supplied) - it's as if it doesn't know the last 2
>are default arguments.
see the file QuantLib-Python\QuantLib\defaults.py for the specification of
default values. Your module will need that file.

Hope this helps

ciao -- Nando

PS Luigi, when we'll settle on the new forthcoming version of SWIG with all
languages consolidated into a single QuantLib-SWIG module, do you plan to
provide an example on how to setup a project based on QuantLib-SWIG?
It would be similar in spirit to our C++ Examples based on QuantLib.