QuantLib SWIG in Java

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

QuantLib SWIG in Java

Billy Ng-5

What is the difference between these two structures?
Is %template (1) creating a template structure in Java?
whereas (2) is a template for i-file generating different Java Code?

Billy Ng

1.
%template(Callability) boost::shared_ptr<Callability>;
2.
%rename(Name) Name##Ptr;
class Name##Ptr : public boost::shared_ptr<YieldTermStructure>
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: QuantLib SWIG in Java

Luigi Ballabio
On Wed, May 16, 2012 at 8:51 AM, Billy Ng <[hidden email]> wrote:

>
> What is the difference between these two structures?
> Is %template (1) creating a template structure in Java?
> whereas (2) is a template for i-file generating different Java Code?
>
> 1.
> %template(Callability) boost::shared_ptr<Callability>;
> 2.
> %rename(Name) Name##Ptr;
> class Name##Ptr : public boost::shared_ptr<YieldTermStructure>

The first instantiates a template in Java.  If you just give SWIG the
definition of a class template, that's just a template. There's no
actual class to wrap, and as far as I know, it doesn't map to a
generic in Java.  The %template directive tells SWIG to instantiate
the template with the given type and wrap the instantiation.  You can
have a look at the SWIG docs for details.

The second is part of a define macro.  SWIG macros work more or less
like the C preprocessor.  When you call the macro, you pass a value
that replaces the Name parameter, and the above becomes, say,

%rename(SomeCurve) SomeCurvePtr;
class SomeCurvePtr : public boost::shared_ptr<YieldTermStructure> {...};

which declares the class SomeCurvePtr and exports it as SomeCurve.  We
usually do this because we want to hide pointers (which are not
idiomatic in the exported languages), so for instance we camouflage
boost::shared_ptr<YieldTermStructure> as YieldTermStructure in Python.
 But then you have problems if you also export a derived class,
because even if A inherits from B, shared_ptr<A> does not inherit from
shared_ptr<B>.  To solve this, we define SomeCurvePtr as
boost::shared_ptr<YieldTermStructure> (i.e., same as for the base
class), but tell SWIG that it inherits instead.  SWIG generate codes
as if they were parent and child (we told it so) and the code works
because they're actually the very same class.

Luigi

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users