Re: [pygccxml-development] Indexing suite and Python containers
Brought to you by:
mbaas,
roman_yakovenko
From: Roman Y. <rom...@gm...> - 2010-01-19 19:57:37
|
On Tue, Jan 19, 2010 at 10:37 AM, Berserker <ber...@ho...> wrote: > I'm exposing some containers with indexing suite 2, in particular: > std::map<std::string, std::string>, std::vector<std::string> and > std::list<std::string> > The manipulation of the containers works fine, I mean get the container from > C++ > and work on it. The only "problem" I have found is when I need to pass a > container to > a C++ function, for example: > > void foo(const std::map<std::string, std::string> &m); > > In Python I expected that I could write: > > m = { "1":"2", "3":"4"} > foo(m) > > The above code doesn't works because I need to write: > > m = std_map_std_string_to_std_string() > m["1"] = "2" > m["3"] = "4" > foo(m) In many cases containers are used as "in-out" arguments. > My question is: do I need to write a specific converter for each C++ > container > or is there a quicker solution to enable the automatic conversion from > Python > containers to C++ containers? Did you consider the performance penalties? At least in my cases such "marshaling" is very expensive and time consuming operation. If your case is different you can solve the problem in a few ways: * give a better name to the class "std_map_std_string_to_std_string" * create "factory" : def cppdict( type_ ): if type_ == ( str, str ): return std_map_std_string_to_std_string() ... m = cppdict( (str,str) ) * expose the functions under different name and create a function wrapper in Python: from <your module> import foo_impl def foo( x ): y = x if isinstance( y, dict ): y = std_map_std_string_to_std_string() y.update( x ) return foo_impl( x ) Basically, if you have many functions like that you can create a function decorator, which converts arguments from one type to another. > Shouldn't indexing_suite do automatically this job? Strong no :-) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |