From: Nitro <ni...@dr...> - 2008-07-12 12:04:21
|
Am 12.07.2008, 13:44 Uhr, schrieb Kai Sterker <kai...@gm...>: > I'm using SWIG to generate Python wrappers and have run into the > following issue. > > I have a C++ base class and derived from that several other classes, > each with its own distinct set of methods. Another class stores > instances of the derived classes and has a method get_base() that > returns a specific instance. > > Now, on Python side, I am getting a base instance by calling > get_base(), but would like to use it to invoke methods of derived_a. > However, I see now way to cast the base instance into a derived_a > instance. > > What I did as a workaround is adding the following to my interface file: > > %extend xyz { > derived_a* get_derived_a (const std::string & name) > { > return dynamic_cast<derived_a*> (self->get_base (name)); > } > }; > > However, I would have to do that for every derived_<x> which is not > very elegant. > > Does SWIG provide a better way to handle something like this? There's the swig/lib/typemaps/factory.swg file which does more or less what you suggest. To use it write %include factory.i . -Matthias |