Login  Register

Re: QuantLib-SWIG, the observer pattern and destructor call during update

Posted by Bojan Nikolic on Mar 07, 2012; 9:40am
URL: http://quantlib.414.s1.nabble.com/Re-QuantLib-SWIG-the-observer-pattern-and-destructor-call-during-update-tp8917p8919.html


Bojan Nikolic <[hidden email]> writes:

> I haven't fully worked this one out, but I think that it is possible to
> some progress on this by making the destructor of the Observer class
> private and then creating some helper classes (probably inheriting from
> shared_pointer) that are friends of Observer and define proper clean
> order, i.e., disconnect the Observables before starting the main
> destruction.

I thought some more about this and realised that private/protected
constructors nor overloading operator new would really help. As far as I
can see the only way to fully solve this type of problem in C++ is to
have control over the storage/object lifetime of Observer objects which
would mean restricting their creation to within object factories only.

This would be a big change for the C++ code base. However, the nice
thing is that SWIG, when creating the interface layer, essentially does
this control of object storage. So, by suitable use of SWIG directives I
think it is in fact possible to have a completely clean destruction
procedure. Something like:

%typemap(javadestruct,
         methodname="delete",
         methodmodifiers="public synchronized")  
{
        // Replace Observable with correct QuantLib/Java name
        if (this instanceof Observable) {
        // Translate to Java
        for (iterator i=observables_.begin(); i!=observables_.end(); ++i)
            (*i)->unregisterObserver(this);            
        }
       
      // This is the original javafdestruct code
      if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        $jnicall;
      }
      swigCPtr = 0;
    }
}

should do the trick nicely. Sorry, haven't got around to trying this
yet.

Best,
Bojan

--
Bojan Nikolic          ||          http://www.bnikolic.co.uk

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev