Thread: [pygccxml-development] [py++] vector of vectors trouble
Brought to you by:
mbaas,
roman_yakovenko
From: Maik B. <bec...@go...> - 2009-01-16 13:21:42
|
Hello, I got two problems with vector of vectors 1. nested vector type isn't exported 2. renaming of "vector<vector<> >" doesn't work as expected =============================================== =============================================== 1. nested vector type isn't exported test.hpp (include guards omitted) {{{ #include <vector> void bar(std::vector<std::vector<double> > vv) { ... } }}} This is how I expect it to work in python {{{ from pyMaik import vector_less__double__greater_ as vector_double from pyMaik import vector_less__std_scope_vector_less__double__greater___greater_ as vvector_double from pyMaik import foo v1 = vector_double() v1.append(1) v2 = vector_double() v2.append(2) vv = vvector_double() vv.append(v1) vv.append(v2) foo(vv) }}} However, it doesn't work because std::vector<std::vector<double> > is exported, but std::vector<double> not. Adding a dummy to test.hpp void dummy(std::vector<double> v) { } solves this problem, but I wonder if there is a better way to do this. ================================================ 2. renaming of "vector<vector<> >" doesn't work as expected. To change this {{{ from pyMaik import vector_less__double__greater_ as vector_double from pyMaik import vector_less__std_scope_vector_less__double__greater___greater_ as vvector_double from pyMaik import foo }}} to {{{ from pyMaik import vector_double, vvector_double, foo }}} I've added the following to generate_code.py {{{ std = mb.namespace('std'); # do work: #mb.class_('::std::vector<double>').rename('vector_double') std.class_('vector<double>').rename('vector_double') # doesn't work: #std.class_('vector<vector<double> >').rename('vvector_double') #mb.class_('::std::vector<::std::vector< double > >').rename('vvector_double')# doesn't work #works: mb.class_('::std::vector< std::vector< double > >').rename('vvector_double') }}} As you can see from the comment vector<double> works nicely with, where std.class_('vector<double>').rename('vector_double') is what I prefer. For vector<vector<double> > I tried std.class_('vector<vector<double> >').rename('vvector_double') but it doesn't work. Then I tried mb.class_('::std::vector<::std::vector< double > >').rename('vvector_double') because because ::std::vector<double> worked above, but I it doesn't work either. The only statement I've found to get what I want is mb.class_('::std::vector< std::vector< double > >').rename('vvector_double') What are the rules (give me a RTFM with and a link if I missed it) ? Thanks, -- Maik |
From: Maik B. <bec...@go...> - 2009-01-16 13:44:27
|
Maik Beckmann schrieb am Freitag 16 Januar 2009 um 14:22: > The only statement I've found to get what I want is > vv = mb.class_('::std::vector< std::vector< double > >') > vv.rename('vvector_double') What I actually need for my code is to set an alias for vector<vector<double> const*> But vv = mb.class_('::std::vector< std::vector< double > const* >') fails. How is it done right? Thanks, -- Maik |
From: Roman Y. <rom...@gm...> - 2009-01-16 19:24:06
|
On Fri, Jan 16, 2009 at 3:45 PM, Maik Beckmann <bec...@go...> wrote: > Maik Beckmann schrieb am Freitag 16 Januar 2009 um 14:22: >> The only statement I've found to get what I want is >> vv = mb.class_('::std::vector< std::vector< double > >') >> vv.rename('vvector_double') > > What I actually need for my code is to set an alias for > vector<vector<double> const*> > But > vv = mb.class_('::std::vector< std::vector< double > const* >') > fails. How is it done right? Basically you have to spell the class name right. Another work around could be to define "match" function: def is_my_class( class_ ): if not class_.name.startswith( 'vector' ): return False n = class_.name[ len('vector' ): ] if 'vector' in n and 'double' in n and 'const' in n and '*' in n: return True return False my_class = mb.class_( is_my_class ) But the prefered approach to this problem is described here: http://language-binding.net/pyplusplus/documentation/how_to/hints.html By the way, if you already have define to your class in C++, than using type traits functionality could help: from pygccxml import declarations: mb = module_builder_t( ... ) typedef = mb.global_ns.typedef( '<<<your typedef>>>' ) my_class = declarations.class_traits.get_declaration( typedef ) HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |
From: Roman Y. <rom...@gm...> - 2009-01-16 19:17:14
|
On Fri, Jan 16, 2009 at 3:22 PM, Maik Beckmann <bec...@go...> wrote: > Hello, > > I got two problems with vector of vectors > 1. nested vector type isn't exported > 2. renaming of "vector<vector<> >" doesn't work as expected > > =============================================== > =============================================== > > 1. nested vector type isn't exported > > test.hpp (include guards omitted) > {{{ > #include <vector> > void bar(std::vector<std::vector<double> > vv) > { ... } > }}} > > This is how I expect it to work in python > {{{ > from pyMaik import vector_less__double__greater_ as vector_double > from pyMaik import > vector_less__std_scope_vector_less__double__greater___greater_ as > vvector_double > from pyMaik import foo > > v1 = vector_double() > v1.append(1) > v2 = vector_double() > v2.append(2) > > vv = vvector_double() > vv.append(v1) > vv.append(v2) > > foo(vv) > }}} > However, it doesn't work because > std::vector<std::vector<double> > > is exported, but > std::vector<double> > not. Adding a dummy to test.hpp > void dummy(std::vector<double> v) { } > solves this problem, but I wonder if there is a better way to do this. Not really, all other solutions are variations of the found one. > > ================================================ > > 2. renaming of "vector<vector<> >" doesn't work as expected. > > To change this > {{{ > from pyMaik import vector_less__double__greater_ as vector_double > from pyMaik import > vector_less__std_scope_vector_less__double__greater___greater_ as > vvector_double > from pyMaik import foo > }}} > to > {{{ > from pyMaik import vector_double, vvector_double, foo > }}} > I've added the following to generate_code.py > {{{ > std = mb.namespace('std'); > > # do work: > #mb.class_('::std::vector<double>').rename('vector_double') > std.class_('vector<double>').rename('vector_double') > > # doesn't work: > #std.class_('vector<vector<double> >').rename('vvector_double') > #mb.class_('::std::vector<::std::vector< double > >>').rename('vvector_double')# doesn't work > #works: > mb.class_('::std::vector< std::vector< double > >').rename('vvector_double') > }}} > > As you can see from the comment vector<double> works nicely with, where > std.class_('vector<double>').rename('vector_double') > is what I prefer. For vector<vector<double> > I tried > std.class_('vector<vector<double> >').rename('vvector_double') > but it doesn't work. Then I tried > mb.class_('::std::vector<::std::vector< double > >>').rename('vvector_double') > because because ::std::vector<double> worked above, but I it doesn't work > either. The only statement I've found to get what I want is > mb.class_('::std::vector< std::vector< double > >').rename('vvector_double') > What are the rules (give me a RTFM with and a link if I missed it) ? The RTFM will not help :-). Mainly because you have to specify the exact name of the class as it reports gccxml. Also, I tried very hard to simplify those things, but as you can see there are still problems. The relevant source code link: http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pygccxml_dev/pygccxml/declarations/matchers.py?revision=1428&view=markup Take a look on class declaration_matcher_t. If you will be able to improve this - I will gladly accept your patch. Py++ propose other solution to the same problem - much easier and less error prone: http://language-binding.net/pyplusplus/documentation/how_to/hints.html HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |