Thread: [pygccxml-development] Template constructors
Brought to you by:
mbaas,
roman_yakovenko
From: Matthias B. <mat...@gm...> - 2008-03-16 21:08:17
|
Hi, after being away from the "wrapping business" for quite a while I'm now back and trying to wrap another library that is heavily template based. As I'm starting from scratch, I'm now using the official module builder instead of my own version. So as a first general question, is there meanwhile some special support for templates? Can I tell Py++ what template arguments to use and it takes care of instantiating it? I'm assuming that's not (yet) the case, so I was instantiating the stuff myself. But I ran into a problem with a template constructor inside a template class. I could only wrap the non-template constructors whereas the other ones don't seem to be reported by gccxml. Here is an example: ///////////////////// File: header.h template<class T> class Spam { T x; T y; public: Spam() : x(0),y(0) {} template<class V> Spam(V a, V b) : x(a),y(b) {} }; // Instantiate the template inline void dummy() { Spam<double> c0(1, 2); } ####################### File: generate.py from pyplusplus.module_builder import module_builder_t mb = module_builder_t(files = ['header.h']) spam = mb.class_("Spam<double>") spam.include() # Print all declarations that are available on the class decls = spam.decls() for decl in decls: print decl mb.build_code_creator(module_name="spammod") mb.split_module(dir_name="bindings") When I run the Python script, the declarations that get printed are the following: Spam<double>::Spam(Spam<double> const & arg0) [copy constructor] Spam<double>::Spam() [constructor] Spam<double>::x [variable] Spam<double>::y [variable] I would also have expected a constructor that takes two ints, but it isn't there and so it isn't wrapped. Is there another way to instantiate the class that would make the constructor visible? I'm using the latest official versions of pygccxml/pyplusplus and gccxml from cvs (and everything is on OSX 10.4.11). Cheers, - Matthias - |
From: Roman Y. <rom...@gm...> - 2008-03-17 12:05:16
|
On Sun, Mar 16, 2008 at 11:08 PM, Matthias Baas <mat...@gm...> wrote: > Hi, Hello. Welcome back. > after being away from the "wrapping business" for quite a while I'm now > back and trying to wrap another library that is heavily template based. > As I'm starting from scratch, I'm now using the official module builder > instead of my own version. > So as a first general question, is there meanwhile some special support > for templates? Can I tell Py++ what template arguments to use and it > takes care of instantiating it? > I'm assuming that's not (yet) the case, so I was instantiating the stuff > myself. > But I ran into a problem with a template constructor inside a template > class. I could only wrap the non-template constructors whereas the other > ones don't seem to be reported by gccxml. > Here is an example: > > ///////////////////// File: header.h > template<class T> > class Spam > { > T x; > T y; > public: > > Spam() : x(0),y(0) {} > template<class V> > Spam(V a, V b) : x(a),y(b) {} > }; > > // Instantiate the template > inline void dummy() > { > Spam<double> c0(1, 2); > } > > ####################### File: generate.py > > from pyplusplus.module_builder import module_builder_t > > mb = module_builder_t(files = ['header.h']) > > spam = mb.class_("Spam<double>") > spam.include() > > # Print all declarations that are available on the class > decls = spam.decls() > for decl in decls: > print decl > > mb.build_code_creator(module_name="spammod") > mb.split_module(dir_name="bindings") > > > When I run the Python script, the declarations that get printed are the > following: > > Spam<double>::Spam(Spam<double> const & arg0) [copy constructor] > Spam<double>::Spam() [constructor] > Spam<double>::x [variable] > Spam<double>::y [variable] > > I would also have expected a constructor that takes two ints, but it > isn't there and so it isn't wrapped. Is there another way to instantiate > the class that would make the constructor visible? Matthias unfortunately the problem is on the GCCXML side I don't see any work around to the problem :-(. It seems that template constructors of template class is a special case, that GCCXML could\does not treat. I think the best solution in this case is to add "constructor declaration" to the classes. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Matthias B. <mat...@gm...> - 2008-03-17 21:12:20
|
Roman Yakovenko wrote: > Matthias unfortunately the problem is on the GCCXML side > > I don't see any work around to the problem :-(. It seems that template > constructors of template class is a special case, that GCCXML > could\does not treat. Yes, unfortunately. I noticed that it doesn't appear in the generated XML file. I was just hoping there's a different way of instantiating the class that might change things. > I think the best solution in this case is to add "constructor > declaration" to the classes. ok, thanks. If only constructors are affected by this then it's not that bad. By the way, here's another (minor) thing I noticed about the warnings that Py++ generates. When you wrap a function that actually takes Boost.Python types as input or returns them as output, then Py++ generates a warning that, for example, boost::python::list is not exposed and this might lead to errors at runtime. But of course, the list object is a built-in Boost.Python type, so this one doesn't need to be wrapped. So it would be nice if Py++ would be aware of the Boost.Python types and could suppress those warnings. At the moment, I don't have many warnings, so it's not such a big issue but I guess as a project grows you might get more and more of those which makes it harder to spot "real" warnings. - Matthias - |
From: Roman Y. <rom...@gm...> - 2008-03-18 07:40:26
|
On Mon, Mar 17, 2008 at 11:12 PM, Matthias Baas <mat...@gm...> wrote: > By the way, here's another (minor) thing I noticed about the warnings > that Py++ generates. When you wrap a function that actually takes > Boost.Python types as input or returns them as output, then Py++ > generates a warning that, for example, boost::python::list is not > exposed and this might lead to errors at runtime. But of course, the > list object is a built-in Boost.Python type, so this one doesn't need to > be wrapped. So it would be nice if Py++ would be aware of the > Boost.Python types and could suppress those warnings. At the moment, I > don't have many warnings, so it's not such a big issue but I guess as a > project grows you might get more and more of those which makes it harder > to spot "real" warnings. Thanks, I will fix this before next release. Py++ has nice interface for warning disabling: http://language-binding.net/pyplusplus/documentation/warnings.html HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |