[PATCH] Payoffs fully implemented in a foreign language via SWIG

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

[PATCH] Payoffs fully implemented in a foreign language via SWIG

andrea-110
I would like to post 2 patches (one for QuantLib, the other for QuantLib-SWIG) to allow users to
implement a payoff entirely in a foreign language (e.g. Java).
These patches apply on top of the other patch I have just sent.

By enabling directors the user can implement the "value" function in Java like it was a standard
subclass of a C++ class.

The goal here is not speed :-) but ease of use, by writing a payoff in a language like Java or C#.

There is also a simple example of an option that pays the running average on every fixing date.

Everything works fine with the exception of the following issue. But I thought to post the code
since I could not find a better solution that what I have currently implemented.



Basically I want to expose to Java a hierarchy like that

class PathPayoff
{
        virtual ....... = 0; // this is the payoff interface
};

class PathOption
{
        virtual vector<Date> fixingDates() = 0;

        virtual boost::shared_ptr<PathPayoff> payoff() = 0; <<<<<<<<< PROBLEM
};

No problem for PathPayoff, but huge issues on how to implement PathOption::payoff() in Java.
Basically the problem is how to create in Java a shared_ptr containing an object inherited from
PathPayoff (which is implemented in Java as well).
I know how to create a raw pointer (which is already managed by SWIG/Java), but I don't think this
pointer can be safely stored into a shared_ptr (i.e. there would be double management).

So I've changed and exposed only one class

class ExternalOption
{
        virtual ....... = 0; // this is the payoff interface
        virtual vector<Date> fixingDates() = 0;

        boost::shared_ptr<PathOption> convert(); // helper to convert an ExternalOption into a PathOption
};

this class contains all functions required to describe a contract.
Then there is a function that converts the ExternalOption into a PathOption using a custom wrapper.
The problem now is that the wrapper contains a reference (C++ reference) to the ExternalOption, so
that there could be problems if someone tried to use the Wrapped PathOption after the ExternalOption
has gone out of scope of garbage collected.

I wish I had a shared_ptr<ExternalOption> but I only have a raw pointer or a reference.

It is a problem similar to the original, but I found it is easier to manage. It is all down to the
use of shared_ptr in SWIG.

If anyone has a comment or better solution I would be delighted to hear about it.

Andrea

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev

external.diff (7K) Download Attachment
swig.diff (6K) Download Attachment
JavaOption.java (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Payoffs fully implemented in a foreign language via SWIG

andrea-110
On 17/01/10 21:31, Andrea wrote:

> I would like to post 2 patches (one for QuantLib, the other for QuantLib-SWIG) to allow users to
> implement a payoff entirely in a foreign language (e.g. Java).
> These patches apply on top of the other patch I have just sent.
>
> By enabling directors the user can implement the "value" function in Java like it was a standard
> subclass of a C++ class.
>
> The goal here is not speed :-) but ease of use, by writing a payoff in a language like Java or C#.
>
> There is also a simple example of an option that pays the running average on every fixing date.
>
> Everything works fine with the exception of the following issue. But I thought to post the code
> since I could not find a better solution that what I have currently implemented.

I've actually found a better solution, but it requires a global change in QuantLib-SWIG.

>
> I wish I had a shared_ptr<ExternalOption> but I only have a raw pointer or a reference.
>
> It is a problem similar to the original, but I found it is easier to manage. It is all down to the
> use of shared_ptr in SWIG.
>

In SWIG there is already support for boost::shared_ptr<> that removes distinction between raw
pointer, value, reference and smart pointer making it all easier.
The problem is that QuantLib-SWIG uses a custom definition of boost::shared_ptr<> (in common.i).

SWIG provides a "shared_ptr.i".
Does anybody know why it is not used?

Andrea

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Payoffs fully implemented in a foreign language via SWIG

Luigi Ballabio
On Mon, 2010-01-18 at 22:14 +0000, Andrea wrote:

> On 17/01/10 21:31, Andrea wrote:
> > I would like to post 2 patches (one for QuantLib, the other for
> QuantLib-SWIG) to allow users to
> > implement a payoff entirely in a foreign language (e.g. Java).
> > These patches apply on top of the other patch I have just sent.
>  
> I've actually found a better solution, but it requires a global change
> in QuantLib-SWIG.
>
> In SWIG there is already support for boost::shared_ptr<> that removes
> distinction between raw pointer, value, reference and smart pointer
> making it all easier.
>
> Does anybody know why it is not used?

SWIG didn't provide it when we first wrote the interfaces, and I'm
afraid we haven't keep abreast of the latest developments.  I'll have to
check it out.  Would it result in an interface change as seen from the
host language?

Later,
        Luigi


--

The doctrine of human equality reposes on this: that there is no man
really clever who has not found that he is stupid.
-- Gilbert K. Chesterson



------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Payoffs fully implemented in a foreign language via SWIG

andrea-110
On 20/01/10 11:15, Luigi Ballabio wrote:

> On Mon, 2010-01-18 at 22:14 +0000, Andrea wrote:
>> On 17/01/10 21:31, Andrea wrote:
>>> I would like to post 2 patches (one for QuantLib, the other for
>> QuantLib-SWIG) to allow users to
>>> implement a payoff entirely in a foreign language (e.g. Java).
>>> These patches apply on top of the other patch I have just sent.
>>  
>> I've actually found a better solution, but it requires a global change
>> in QuantLib-SWIG.
>>
>> In SWIG there is already support for boost::shared_ptr<> that removes
>> distinction between raw pointer, value, reference and smart pointer
>> making it all easier.
>>
>> Does anybody know why it is not used?
>
> SWIG didn't provide it when we first wrote the interfaces, and I'm
> afraid we haven't keep abreast of the latest developments.  I'll have to
> check it out.  Would it result in an interface change as seen from the
> host language?
It looks like it makes things a lot easier.
Basically one has to export the raw C++ inheritance.
Then swig will remove all differences between pointers, references, shared_ptr, wrapping them when
needed.
I think it would simplify what Quantlib-SWIG does now, where it needs to expose classes inheriting
from shared_ptr<I> rather than I itself.

I think the idea is to expose things as they are, then SWIG will take care of all the shared_ptr
details.

If you compile the file I have attached, you can see what it looks like.

It is very likely that the user will not notice any difference.

Coming back to my original problem, unfortunately it seems that using shared_ptr and directors does
not work (yet?) (SWIG generates invalid Java code). So for the time being the code I posted at the
top of the thread is still my best solution.

Andrea

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev

p.i (566 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Payoffs fully implemented in a foreign language via SWIG

Luigi Ballabio
On Wed, 2010-01-20 at 21:08 +0000, Andrea wrote:
> It looks like it makes things a lot easier.
> Basically one has to export the raw C++ inheritance.
> Then swig will remove all differences between pointers, references,
> shared_ptr, wrapping them when needed.
> I think it would simplify what Quantlib-SWIG does now, where it needs
> to expose classes inheriting from shared_ptr<I> rather than I itself.

> It is very likely that the user will not notice any difference.

Ok, I see.  Yes, that would simplify the interfaces somewhat.  However,
there's the problem that shared_ptr is not supported in all the
languages we're exporting to.  

> Coming back to my original problem, unfortunately it seems that using
> shared_ptr and directors does not work (yet?) (SWIG generates invalid
> Java code). So for the time being the code I posted at the top of the
> thread is still my best solution.

Ok, I'll look at it. Thanks.

Luigi



--

The nice thing about standards is that there are so many of them to
choose from.
-- Andrew S. Tanenbaum



------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Payoffs fully implemented in a foreign language via SWIG

andrea-110
On 22/01/10 11:06, Luigi Ballabio wrote:

> On Wed, 2010-01-20 at 21:08 +0000, Andrea wrote:
>> It looks like it makes things a lot easier.
>> Basically one has to export the raw C++ inheritance.
>> Then swig will remove all differences between pointers, references,
>> shared_ptr, wrapping them when needed.
>> I think it would simplify what Quantlib-SWIG does now, where it needs
>> to expose classes inheriting from shared_ptr<I> rather than I itself.
>
>> It is very likely that the user will not notice any difference.
>
> Ok, I see.  Yes, that would simplify the interfaces somewhat.  However,
> there's the problem that shared_ptr is not supported in all the
> languages we're exporting to.  
>
>> Coming back to my original problem, unfortunately it seems that using
>> shared_ptr and directors does not work (yet?) (SWIG generates invalid
>> Java code). So for the time being the code I posted at the top of the
>> thread is still my best solution.
>
> Ok, I'll look at it. Thanks.
>
> Luigi
>
>
>

For the problem with directors, feel free to join me here

http://article.gmane.org/gmane.comp.programming.swig/15606

Andrea

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Payoffs fully implemented in a foreign language via SWIG

andrea-110
In reply to this post by Luigi Ballabio
On 22/01/10 11:06, Luigi Ballabio wrote:

> On Wed, 2010-01-20 at 21:08 +0000, Andrea wrote:
>> It looks like it makes things a lot easier.
>> Basically one has to export the raw C++ inheritance.
>> Then swig will remove all differences between pointers, references,
>> shared_ptr, wrapping them when needed.
>> I think it would simplify what Quantlib-SWIG does now, where it needs
>> to expose classes inheriting from shared_ptr<I> rather than I itself.
>
>> It is very likely that the user will not notice any difference.
>
> Ok, I see.  Yes, that would simplify the interfaces somewhat.  However,
> there's the problem that shared_ptr is not supported in all the
> languages we're exporting to.  
>

I tried a bit more to use swig shared_ptr support (just the template definition and not th emore
advanced feature), but that also has problems.

Basically in quantlib-swig a share_ptr is

    template <class T>
    class shared_ptr {
      public:
        T* operator->();
    };


while in SWIG's shared_ptr.i it is

    template <class T>
    class shared_ptr {
    };

QuantLi-SWIG relies massively on the operator -> which force SWIG to add all methods of T to the
shared_ptr class.

So in the end, I am still with the first version, and I will just try to make it safe with some
observer/observable pattern so that I know when the reference held by the wrapper is destroyed (I
hope at least).

Andrea


------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev