[Debian-astro-commits] [gyoto] 177/221: Python: expose normal pointers (with reference counting) rather than SmartPointers.

Thibaut Jean-Claude Paumard thibaut at moszumanska.debian.org
Fri May 22 20:52:44 UTC 2015


This is an automated email from the git hooks/post-receive script.

thibaut pushed a commit to branch master
in repository gyoto.

commit e8ba9a5ebfdd7b2987213d5c468dd8e024135d2d
Author: Thibaut Paumard <paumard at users.sourceforge.net>
Date:   Wed Jan 7 14:01:05 2015 +0100

    Python: expose normal pointers (with reference counting) rather than SmartPointers.
---
 include/GyotoDirectionalDisk.h  |   2 +-
 include/GyotoDynamicalDisk.h    |   2 +-
 include/GyotoDynamicalDisk3D.h  |   2 +-
 include/GyotoPageThorneDisk.h   |   2 +-
 include/GyotoPatternDisk.h      |   2 +-
 include/GyotoPatternDiskBB.h    |   2 +-
 include/GyotoStarTrace.h        |   2 +-
 include/GyotoThinDiskIronLine.h |   2 +-
 include/GyotoThinDiskPL.h       |   2 +-
 python/example.py               |  64 ++++++----
 python/gyoto.i                  | 265 +++++++++++++++++++++++++++++++---------
 python/gyoto_std.i              |  42 +++++--
 12 files changed, 288 insertions(+), 101 deletions(-)

diff --git a/include/GyotoDirectionalDisk.h b/include/GyotoDirectionalDisk.h
index e21d035..72010ba 100644
--- a/include/GyotoDirectionalDisk.h
+++ b/include/GyotoDirectionalDisk.h
@@ -154,7 +154,7 @@ class Gyoto::Astrobj::DirectionalDisk : public Astrobj::ThinDisk {
  public:
   using ThinDisk::emission;
   virtual double emission(double nu_em, double dsem,
-			  double c_ph[8], double c_obj[8]) const;
+			  double c_ph[8], double c_obj[8]=NULL) const;
 
 };
 
diff --git a/include/GyotoDynamicalDisk.h b/include/GyotoDynamicalDisk.h
index 71532b6..9dcbb9a 100644
--- a/include/GyotoDynamicalDisk.h
+++ b/include/GyotoDynamicalDisk.h
@@ -115,7 +115,7 @@ class Gyoto::Astrobj::DynamicalDisk : public Astrobj::PatternDiskBB {
 
   using PatternDiskBB::emission;
   virtual double emission(double nu_em, double dsem,
-			  double c_ph[8], double c_obj[8]) const;
+			  double c_ph[8], double c_obj[8]=NULL) const;
 
   void getVelocity(double const pos[4], double vel[4]);
   double const * getVelocity() const;
diff --git a/include/GyotoDynamicalDisk3D.h b/include/GyotoDynamicalDisk3D.h
index 101777e..3f5cb6b 100644
--- a/include/GyotoDynamicalDisk3D.h
+++ b/include/GyotoDynamicalDisk3D.h
@@ -133,7 +133,7 @@ class Gyoto::Astrobj::DynamicalDisk3D : public Astrobj::Disk3D {
   using Disk3D::emission;
   /// Interpolate emission between grid dates.
   virtual double emission(double nu_em, double dsem,
-			  double c_ph[8], double c_obj[8]) const;
+			  double c_ph[8], double c_obj[8]=NULL) const;
 
   /// Compute transmission at one grid date.
   double transmission1date(double nu_em, double dsem,
diff --git a/include/GyotoPageThorneDisk.h b/include/GyotoPageThorneDisk.h
index 646a67c..f9a24ad 100644
--- a/include/GyotoPageThorneDisk.h
+++ b/include/GyotoPageThorneDisk.h
@@ -117,7 +117,7 @@ class Gyoto::Astrobj::PageThorneDisk
    * Throws a Gyoto::Error
    */
   virtual double emission(double nu_em, double dsem,
-			  double c_ph[8], double c_obj[8]) const;
+			  double c_ph[8], double c_obj[8]=NULL) const;
 
   /**
    * \brief Bolometric emission
diff --git a/include/GyotoPatternDisk.h b/include/GyotoPatternDisk.h
index 7f7f5ac..3d17cda 100644
--- a/include/GyotoPatternDisk.h
+++ b/include/GyotoPatternDisk.h
@@ -270,7 +270,7 @@ class Gyoto::Astrobj::PatternDisk : public Astrobj::ThinDisk {
  public:
   using ThinDisk::emission;
   virtual double emission(double nu_em, double dsem,
-			  double c_ph[8], double c_obj[8]) const;
+			  double c_ph[8], double c_obj[8]=NULL) const;
   virtual double transmission(double nu_em, double dsem, double coord[8]) const;
 
   virtual void getVelocity(double const pos[4], double vel[4])  ;
diff --git a/include/GyotoPatternDiskBB.h b/include/GyotoPatternDiskBB.h
index 6de130b..ee55e3c 100644
--- a/include/GyotoPatternDiskBB.h
+++ b/include/GyotoPatternDiskBB.h
@@ -89,7 +89,7 @@ class Gyoto::Astrobj::PatternDiskBB : public Astrobj::PatternDisk {
  public:
   using PatternDisk::emission;
   double emission(double nu_em, double dsem,
-		  double c_ph[8], double c_obj[8]) const;
+		  double c_ph[8], double c_obj[8]=NULL) const;
   
   double const * getVelocity() const ;
   void getVelocity(double const pos[4], double vel[4])  ;
diff --git a/include/GyotoStarTrace.h b/include/GyotoStarTrace.h
index b3ca6da..6fe7093 100644
--- a/include/GyotoStarTrace.h
+++ b/include/GyotoStarTrace.h
@@ -122,7 +122,7 @@ class Gyoto::Astrobj::StarTrace :
   void computeXYZ(); ///< Compute (and cache) x_, y_ and z_
 
   using Star::setInitCoord;
-  virtual void setInitCoord(const double coord[8], int dir);
+  virtual void setInitCoord(const double coord[8], int dir = 0);
 
   using Generic::metric;
   virtual void metric(SmartPointer<Metric::Generic> gg);
diff --git a/include/GyotoThinDiskIronLine.h b/include/GyotoThinDiskIronLine.h
index 91f29d9..7b770a8 100644
--- a/include/GyotoThinDiskIronLine.h
+++ b/include/GyotoThinDiskIronLine.h
@@ -25,7 +25,7 @@ class Gyoto::Astrobj::ThinDiskIronLine
 
   using ThinDisk::emission;
   virtual double emission(double nu_em, double dsem,
-			  double c_ph[8], double c_obj[8]) const;
+			  double c_ph[8], double c_obj[8]=NULL) const;
   void getVelocity(double const pos[4], double vel[4]);
 
 
diff --git a/include/GyotoThinDiskPL.h b/include/GyotoThinDiskPL.h
index 324c33d..2322dc6 100644
--- a/include/GyotoThinDiskPL.h
+++ b/include/GyotoThinDiskPL.h
@@ -86,7 +86,7 @@ class Gyoto::Astrobj::ThinDiskPL : public Astrobj::ThinDisk {
  public:
   using ThinDisk::emission;
   virtual double emission(double nu_em, double dsem,
-			  double c_ph[8],double c_obj[8]) const;
+			  double c_ph[8],double c_obj[8]=NULL) const;
 
  private:
   /**
diff --git a/python/example.py b/python/example.py
index 417b72a..7e8c461 100644
--- a/python/example.py
+++ b/python/example.py
@@ -44,8 +44,9 @@ plt.plot(t2, r2)
 plt.show()
 
 # Trace and plot timelike geodesic
+# We need to cast the object to a gyoto_std.Star:
 
-wl=gyoto.castToWorldline(sc.astrobj());
+wl=gyoto_std.Star(sc.astrobj())
 wl.xFill(1000)
 
 n=wl.get_nelements()
@@ -96,6 +97,7 @@ plt.show()
 # Another Scenery, with spectrum
 
 sc=gyoto.Factory("../doc/examples/example-polish-doughnut.xml").getScenery()
+sc.screen().resolution(32)
 res=sc.screen().resolution()
 ns=sc.screen().spectrometer().nSamples()
 spectrum=numpy.zeros((ns, res, res), dtype=float)
@@ -195,34 +197,48 @@ t=numpy.clip(ipct[:,0,0], a_min=-200, a_max=0)
 plt.plot(t)
 plt.show()
 
-# Instanciate and manipulate derived classes from the standard
-# plug-in: Note that objects from gyoto_std needs to be downcast to
-# the relevant base before the can be used in gyoto:
-# e.g. AstrobjPtr(obj.__deref__()). The opposite is not (yet)
-# possible: you can't cast the Astrobj from sc.astrobj() up to a
-# StarPtr or TorusPtr. That's where the Object/Value/Property
-# mechanism comes into play, since you don't need to cast an object to
-# set a property:
-
-# By creating an object from gyoto_std, we have full access to its methods:
-tr=gyoto_std.Torus()
-tr.smallRadius(0.1)
-
-# Before feeding it to a function that expects a SmartPointer<Astrobj>,
-# we nee to cast it:
-tt=gyoto.AstrobjPtr(tr.__deref__())
-
-# We can still use the Torus methods on tr, but not on tt.  However,
-# the Property mechanism gives access to the members that can be set
-# from XML:
+# Any derived class can be instanciated from its name, as soon as the
+# corresponding plug-in has been loaded into Gyoto. The standard
+# plug-in is normally loaded automatically, but this can also be
+# forced with gyoto.loadPlugin():
+gyoto.loadPlugin('stdplug')
+tt=gyoto.Astrobj('Torus')
+kerr=gyoto.Metric('KerrBL')
+
+# Most properties that can be set in an XML file can also be accessed
+# from Python using the Property/Value mechanism:
 p=tt.property("SmallRadius")
 tt.set(p, gyoto.Value(0.2))
+tt.get(p).toDouble() == 0.2
+
+p=kerr.property("Spin")
+kerr.set(p, gyoto.Value(0.95))
+kerr.get(p).toDouble() == 0.95
+
+# However, we also have Python extensions around the standard Gyoto
+# plug-ins. Beware that the plug-in must be loaded into Gyoto before
+# importing the corresponding Python extension:
+gyoto.loadPlugin('stdplug')
+import gyoto_std
+# And if the lorene plug-in has been compiled:
+# gyoto.loadPlugin('lorene')
+# import gyoto_lorene
+
+# It then becomes possible to access the methods specific to derived
+# classes. They can be instanciated directly from the gyoto_* extension:
+tr2=gyoto_std.Torus()
+# and we can cast a generic pointer (from the gyoto extension) to a
+# derived class:
+tr=gyoto_std.Torus(tt)
+p=tt.property("SmallRadius")
 tt.get(p).toDouble() == tr.smallRadius()
 
-# Etc:
+# Another example: using a complex (i.e. compound) Astrobj:
 cplx=gyoto_std.ComplexAstrobj()
-cplx.append(gyoto.AstrobjPtr(tr.__deref__()))
+cplx.append(tr)
 cplx.append(sc.astrobj())
-sc.astrobj(gyoto.AstrobjPtr(cplx.__deref__()))
+sc.astrobj(cplx)
 
 cplx.rMax(100)
+
+print("All done, exiting")
diff --git a/python/gyoto.i b/python/gyoto.i
index 5698459..fe846d8 100644
--- a/python/gyoto.i
+++ b/python/gyoto.i
@@ -1,63 +1,182 @@
+/*
+    Copyright 2014 Thibaut Paumard
+
+    This file is part of Gyoto.
+
+    Gyoto is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    Gyoto is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with Gyoto.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+    This is a Swig interface file. It is currently meant to provide
+    Python bindings only, but it should ne be too difficult to provide
+    bindings for java, Tcl or whatever other language Swig supports.
+ */
+
+// ******** SOME INITIALIZATION ********* //
+
+// Define the module name, with a docstring
 %module(docstring="The General relativitY Orbit Tracer of paris Observatory") gyoto
+
+// Let Swig generate some documentation for the overloaded methods
 %feature("autodoc", "1");
 
+// gyoto_doc.i is generated from the doxygen comments using doxy2swig.py
 %import gyoto_doc.i
 
+// Make it possible to detect that a .h file is being processed by
+// Swig rather than CPP. There should be a Swig feature for that, I
+// didn't find it.
 %define GYOTO_SWIGIMPORTED
 %enddef
 
+// Make sure we don't activate the deprecated method names (see
+// GyotoDefs.h): it is better to fail on them to update them now.
 %define GYOTO_NO_DEPRECATED
 %enddef
 
-%define GyotoSmPtrClass(klass)
-%ignore Gyoto:: klass ;
-%include Gyoto ## klass ## .h
-%rename(klass) pyGyoto ## klass;
-%inline {
-Gyoto::SmartPointer<Gyoto::klass> pyGyoto ## klass () {
-  return new Gyoto::klass();
+//  ********** MACRO DEFINITIONS *********** //
+
+// Exposing SmartPointers does not work well: it breaks automatic
+// inheritance and docstring generation. We therefore provide Swig
+// typemaps to convert SmartPointers to normal pointers
+// automatically. We also use the ref/unref features to let Swig
+// handle the reference counting for us mostly automatically (which is
+// what we wouldotherwise loose by not using SmartPointers).
+//
+// Since SmartPointers are templates, we use macros here to provide
+// typemaps for various SmartPointer specializations.
+
+// Typemaps for Gyoto::SmartPointer<Gyoto::klass>
+%define GyotoSmPtrTypeMapClass(klass)
+  GyotoSmPtrTypeMap(Gyoto::klass, SWIGTYPE_p_Gyoto__ ## klass)
+%enddef
+
+// Typemaps for Gyoto::SmartPointer<Gyoto::nspace::Generic>
+%define GyotoSmPtrTypeMapClassGeneric(nspace)
+  GyotoSmPtrTypeMap(Gyoto::nspace::Generic, SWIGTYPE_p_Gyoto__ ## nspace ## __Generic)
+%enddef
+
+// Typemaps for Gyoto::SmartPointer<Gyoto::nspace::klass>
+%define GyotoSmPtrTypeMapClassDerived(nspace, klass)
+   GyotoSmPtrTypeMap(Gyoto::nspace::klass, SWIGTYPE_p_Gyoto__ ## nspace ## __ ## klass)
+%enddef
+
+// Basic macro used in the above: gtype is the Gyoto name of a type,
+// e.g. Gyoto::Metric::KerrBL, while stype is the Swig name for same
+// type, e.g. SWIGTYPE_p_Gyoto__Metric__KerrBL
+%define GyotoSmPtrTypeMap(gtype, stype)
+%typemap(in)
+  Gyoto::SmartPointer<gtype>  (gtype *)
+{
+  int res=0;
+  void *argp=0;
+  res=SWIG_ConvertPtr($input, &argp, stype, 0);
+  if (!SWIG_IsOK(res)) {
+    SWIG_exception_fail(SWIG_ArgError(res), "argument of type '" #gtype "*'");
+  }
+  gtype * kp=reinterpret_cast< gtype * >(argp);
+  $1 = Gyoto::SmartPointer<gtype>(kp);
 }
+%typemap(out)
+  Gyoto::SmartPointer<gtype>  (gtype *)
+{
+  gtype* normal_pointer=(gtype *) (Gyoto::SmartPointer<gtype>(result));
+  normal_pointer->incRefCount();
+  $result = SWIG_NewPointerObj( normal_pointer, stype, SWIG_POINTER_OWN |  0 );
 }
-%template(klass ## Ptr) Gyoto::SmartPointer<Gyoto::klass>;
+%typemap(typecheck) Gyoto::SmartPointer<gtype>, gtype * {
+  void *vptr = 0;
+  int res = SWIG_ConvertPtr($input, &vptr, stype, 0);
+  $1 = SWIG_CheckState(res);
+ }
+%typemap(typecheck) Gyoto::SmartPointer< gtype > = Gyoto::SmartPointer<gtype>;
+%typemap(typecheck) gtype * = Gyoto::SmartPointer<gtype>;
+%typemap(typecheck) gtype const * = Gyoto::SmartPointer<gtype>;
+%enddef
+
+// Include header for a class deriving from SmartPointee, providing
+// the ref and unref features
+%define GyotoSmPtrClass(klass)
+%feature("ref") Gyoto:: klass "$this->incRefCount();";
+%feature("unref") Gyoto:: klass "$this->decRefCount(); if (!$this->getRefCount()) delete $this;";
+%include Gyoto ## klass ## .h
 %enddef
 
+// Include header for a base class (e.g. GyotoMetric.h), provide
+// constructor from kind string (e.g. gyoto.Metric('KerrBL')), provide
+// ref/unref features
 %define GyotoSmPtrClassGeneric(klass)
-%template(klass ## Ptr) Gyoto::SmartPointer<Gyoto::klass::Generic>;
 %rename(klass) klass ## Ptr;
 %ignore Gyoto::klass::Register_;
 %ignore Gyoto::klass::Register;
 %ignore Gyoto::klass::initRegister;
 %ignore Gyoto::klass::getSubcontractor;
-%ignore Gyoto::klass::Generic;
+%rename(klass) Gyoto::klass::Generic;
+%feature("ref") Gyoto:: klass ::Generic"$this->incRefCount();";
+%feature("unref") Gyoto:: klass ::Generic"$this->decRefCount(); if (!$this->getRefCount()) delete $this;";
+%feature("notabstract") Gyoto::klass::Generic;
+%ignore  Gyoto::klass::Generic::Generic(Gyoto::klass::Generic const &);
+%ignore  Gyoto::klass::Generic::Generic(const Generic &);
+%ignore  Gyoto::klass::Generic::Generic(const klass::Generic &);
+%ignore  Gyoto::klass::Generic::Generic();
+%ignore  Gyoto::klass::Generic::Generic(double);
+%ignore  Gyoto::klass::Generic::Generic(kind_t);
+%ignore  Gyoto::klass::Generic::Generic(const std::string);
+%extend Gyoto::klass::Generic {
+  Generic(std::string nm) {
+    Gyoto::SmartPointer<Gyoto::klass::Generic> pres=
+      Gyoto::klass::getSubcontractor(nm.c_str())(NULL);
+    Gyoto::klass::Generic * res = (Gyoto::klass::Generic *)(pres);
+    res -> incRefCount();
+    return res;
+  }
+};
 %include Gyoto ## klass ## .h
-%rename(klass) pyGyoto ## klass;
-%inline {
-Gyoto::SmartPointer<Gyoto::klass::Generic> pyGyoto ## klass (std::string const&s) {
-  return Gyoto::klass::getSubcontractor(s.c_str())(NULL);
-}
-}
 %enddef
 
+// Include header for derived class. Parameters: nspace: namespace
+// (e.g. Astrobj); klass: classname (e.g. Complex); nick: name to use
+// in the flattened namespace (e.g. ComplexAstrobj); hdr: header file
+// (e.g. GyotoComplexAstrobj.h). Extends class with a pseudo
+// constructor for upcasting,
+// e.g. cplx=gyoto_std.ComplexAstrobj(sc.strobj())
 %define GyotoSmPtrClassDerivedPtrHdr(nspace, klass, nick, hdr)
-%template(nick ## Ptr) Gyoto::SmartPointer<Gyoto::nspace::klass>;
-%ignore Gyoto::nspace::klass;
+%rename(nick) Gyoto::nspace::klass;
+%feature("notabstract") Gyoto::nspace::klass;
+%extend  Gyoto::nspace::klass {
+  klass(Gyoto::nspace::Generic * base) {
+    Gyoto::nspace::klass * res = dynamic_cast< Gyoto::nspace::klass * >(base);
+    if (!res) Gyoto::throwError("This pointer cannot be cast to 'Gyoto::" #nspace "::" #klass "*'");
+    return res;
+  }
+ };
 %include hdr
-%rename(nick) pyGyoto ## nick;
-%inline {
-Gyoto::SmartPointer<Gyoto::nspace::klass> pyGyoto ## nick () {
-  return new Gyoto::nspace::klass();
-}
-}
 %enddef
 
+// Simplification of the above when nick == klass
 %define GyotoSmPtrClassDerivedHdr(nspace, klass, hdr)
 GyotoSmPtrClassDerivedPtrHdr(nspace, klass, klass, hdr)
 %enddef
 
+// Simplification of the above when hdr == Gyoto ## klass ## .h
 %define GyotoSmPtrClassDerived(nspace, klass)
 GyotoSmPtrClassDerivedHdr(nspace, klass, Gyoto ## klass ## .h)
 %enddef
 
+
+// ******** INCLUDES ******** //
+// Include any file that is needed to compile the wrappers
 %{
 #define SWIG_FILE_WITH_INIT
   //#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
@@ -84,37 +203,65 @@ using namespace Gyoto;
 
 %}
 
+// ******** INITIALIZATION ******** //
+// This will be called upon extension initialization
+%init {
+  Gyoto::Register::init();
+  import_array();
+ }
+
+// ******** TYPEMAPS ******** //
+// Actually instanciate typemaps using de macros defined above
+
+GyotoSmPtrTypeMapClassGeneric(Metric);
+GyotoSmPtrTypeMapClassGeneric(Astrobj);
+GyotoSmPtrTypeMapClassGeneric(Spectrum);
+GyotoSmPtrTypeMapClassGeneric(Spectrometer);
+
+GyotoSmPtrTypeMapClass(Screen);
+GyotoSmPtrTypeMapClass(Scenery);
+GyotoSmPtrTypeMapClass(Photon);
+
+GyotoSmPtrTypeMapClassDerived(Spectrometer, Complex);
+GyotoSmPtrTypeMapClassDerived(Spectrometer, Uniform);
+
+// Non-Gyoto typemaps:
+// Handle std::string
+%include "std_string.i";
+// Handle std::vector<double> and <unsigned long int>
+%include "std_vector.i";
+%template(vector_double) std::vector<double>;
+%template(vector_unsigned_long) std::vector<unsigned long>;
+// Handle generic C arrays using a class-like interface
+%include "carrays.i"
+%array_class(double, array_double);
+%array_class(double, array_unsigned_long);
+// Handle some arrays as NumPy arrays
+%include "numpy.i";
+%numpy_typemaps(size_t, NPY_ULONG , size_t);
+%numpy_typemaps(double, NPY_DOUBLE, size_t);
+
+// ******** INTERFACE ******** //
+// Here starts the actual pasing of the various header files
+
+// Expose the build-time configuration variables
 %include "GyotoConfig.h"
 
+// Expose the Gyoto::Error class
 %include "GyotoError.h"
 
+// Catch all Gyoto errors and re-throw them as a Python run-time error
 %exception {
 	try {
 	$function
 	}
 	catch (Gyoto::Error e) {
-	 	PyErr_SetString(PyExc_IndexError, e);
+		PyErr_SetString(PyExc_RuntimeError, e);
 		return NULL;
 	}
 }
 
-
-%include "std_string.i" 
-%include "std_vector.i"
-%template(vector_double) std::vector<double>;
-%template(vector_unsigned_long) std::vector<unsigned long>;
-%include "carrays.i"
-%array_class(double, array_double)
-%array_class(double, array_unsigned_long)
-%include "numpy.i"
-%numpy_typemaps(size_t, NPY_ULONG , size_t)
-%numpy_typemaps(double, NPY_DOUBLE, size_t)
-
-%init {
-  Gyoto::Register::init();
-  import_array();
- }
-
+// Expose Gyoto::Register::list as gyoto.listRegister
 %ignore Gyoto::Register::Entry;
 %ignore Gyoto::Register::init;
 %rename(listRegister) Gyoto::Register::list;
@@ -146,10 +293,10 @@ using namespace Gyoto;
   typedef unsigned long unsignedlong;
   %}
 %rename(toVULong) Gyoto::Value::operator std::vector<unsigned long>;
-%rename(toMetricPtr) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Metric::Generic>;
-%rename(toAstrobjPtr) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Astrobj::Generic>;
-%rename(toSpectrumPtr) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Spectrum::Generic>;
-%rename(toSpectrometerPtr) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>;
+%rename(toMetric) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Metric::Generic>;
+%rename(toAstrobj) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Astrobj::Generic>;
+%rename(toSpectrum) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Spectrum::Generic>;
+%rename(toSpectrometer) Gyoto::Value::operator Gyoto::SmartPointer<Gyoto::Spectrometer::Generic>;
 %include "GyotoValue.h"
 %include "GyotoObject.h"
 
@@ -159,16 +306,6 @@ using namespace Gyoto;
 %ignore Gyoto::Worldline::IntegState::Legacy;
 %include "GyotoWorldline.h"
 
-%rename (castToWorldline) pyGyotoCastToWorldline;
-%inline %{
-Gyoto::Worldline * pyGyotoCastToWorldline
-  (Gyoto::SmartPointer<Gyoto::Astrobj::Generic> const_p) {
-  Gyoto::SmartPointee * p=const_cast<Gyoto::Astrobj::Generic*>(const_p());
-  Gyoto::Worldline * res = dynamic_cast<Gyoto::Worldline*>(p);
-  return res;
-}
-%}
-
 GyotoSmPtrClass(Screen)
 GyotoSmPtrClass(Scenery)
 GyotoSmPtrClass(Photon)
@@ -213,13 +350,19 @@ GyotoSmPtrClassGeneric(Metric)
 GyotoSmPtrClassGeneric(Spectrum)
 GyotoSmPtrClassGeneric(Spectrometer)
 
-%rename(__getitem__) Gyoto::Spectrometer::Complex::operator[];
-GyotoSmPtrClassDerivedPtrHdr(Spectrometer, Complex, ComplexSpectrometer, GyotoComplexSpectrometer.h)
-%extend Gyoto::SmartPointer<Gyoto::Spectrometer::Complex> {
-  void __setitem__(int i, Gyoto::SmartPointer<Gyoto::Spectrometer::Generic> p) {
-    (*$self)->operator[](i)=p;
+%extend Gyoto::Spectrometer::Complex {
+  Gyoto::Spectrometer::Generic * __getitem__ (int i) {
+    Gyoto::Spectrometer::Generic * res = ($self)->operator[](i);
+    res -> incRefCount();
+    return res;
+  }
+ };
+%extend Gyoto::Spectrometer::Complex {
+  void __setitem__(int i, Gyoto::Spectrometer::Generic * p) {
+    ($self)->operator[](i)=p;
   }
  };
+GyotoSmPtrClassDerivedPtrHdr(Spectrometer, Complex, ComplexSpectrometer, GyotoComplexSpectrometer.h)
 GyotoSmPtrClassDerivedPtrHdr(Spectrometer, Uniform, UniformSpectrometer, GyotoUniformSpectrometer.h)
 
 %include "GyotoConfig.h"
diff --git a/python/gyoto_std.i b/python/gyoto_std.i
index 3651649..b00ee88 100644
--- a/python/gyoto_std.i
+++ b/python/gyoto_std.i
@@ -48,17 +48,45 @@ using namespace Gyoto;
 %array_class(double, array_double)
 %array_class(double, array_unsigned_long)
 
-%rename(__getitem__) Gyoto::Astrobj::Complex::operator[];
-%rename(ComplexAstrobjPtr) ComplexPtr;
-GyotoSmPtrClassDerivedPtrHdr(Astrobj, Complex, ComplexAstrobj, GyotoComplexAstrobj.h)
+// Typemaps to translate SmartPointer to specific classes
+GyotoSmPtrTypeMapClassDerived(Astrobj, Star)
+GyotoSmPtrTypeMapClassDerived(Astrobj, StarTrace)
+GyotoSmPtrTypeMapClassDerived(Astrobj, FixedStar)
+GyotoSmPtrTypeMapClassDerived(Astrobj, Torus)
+GyotoSmPtrTypeMapClassDerived(Astrobj, PageThorneDisk)
+GyotoSmPtrTypeMapClassDerived(Astrobj, ThinDiskPL)
+GyotoSmPtrTypeMapClassDerived(Astrobj, PolishDoughnut)
+GyotoSmPtrTypeMapClassDerived(Astrobj, ThinDiskIronLine)
+GyotoSmPtrTypeMapClassDerived(Astrobj, PatternDisk)
+GyotoSmPtrTypeMapClassDerived(Astrobj, PatternDiskBB)
+GyotoSmPtrTypeMapClassDerived(Astrobj, DynamicalDisk)
+GyotoSmPtrTypeMapClassDerived(Astrobj, Disk3D)
+GyotoSmPtrTypeMapClassDerived(Astrobj, DynamicalDisk3D)
+GyotoSmPtrTypeMapClassDerived(Astrobj, DirectionalDisk)
+
+GyotoSmPtrTypeMapClassDerived(Metric, KerrBL)
+GyotoSmPtrTypeMapClassDerived(Metric, KerrKS)
+GyotoSmPtrTypeMapClassDerived(Metric, Minkowski)
 
-%extend Gyoto::SmartPointer<Gyoto::Astrobj::Complex> {
-  void __setitem__(int i, Gyoto::SmartPointer<Gyoto::Astrobj::Generic> p) {
-    (*$self)->operator[](i)=p;
+GyotoSmPtrTypeMapClassDerived(Spectrum, PowerLaw)
+GyotoSmPtrTypeMapClassDerived(Spectrum, BlackBody)
+GyotoSmPtrTypeMapClassDerived(Spectrum, ThermalBremsstrahlung)
+
+%extend Gyoto::Astrobj::Complex {
+  Gyoto::Astrobj::Generic * __getitem__ (int i) {
+    Gyoto::Astrobj::Generic * res = ($self)->operator[](i);
+    res -> incRefCount();
+    return res;
   }
  };
+%extend Gyoto::Astrobj::Complex {
+  void __setitem__(int i, Gyoto::Astrobj::Generic * p) {
+    ($self)->operator[](i)=p;
+  }
+ };
+GyotoSmPtrClassDerivedPtrHdr(Astrobj, Complex, ComplexAstrobj, GyotoComplexAstrobj.h)
 
-
+// Declare specific classes
 GyotoSmPtrClassDerived(Astrobj, Star)
 GyotoSmPtrClassDerived(Astrobj, StarTrace)
 GyotoSmPtrClassDerived(Astrobj, FixedStar)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-astro/packages/gyoto.git



More information about the Debian-astro-commits mailing list