[C++-sig] Automatic argument conversion

Eric Jardim ericjardim at gmail.com
Mon Jul 25 02:23:43 CEST 2005


Hi,
this may be easy to do, but I don't know how to acomplish it.

If i have a function like:

void
 SomeClass::my_print(std::string s)
{
 std::cout << s << std::endl;
}

And if I declare inside the module:

class_<SomeClass>("SomeClas")
.def ("my_print", my_print)

it will work correctly, because Boost.Python know how (somehow) to convert 
"std::string"s to Python "str" strings and vice-versa.
So in python shell:
>>> import m
>>> c = m.SomeClass()
>>> c.my_print('Eric')
Eric

But if I have a function, like that one, but instead of a known type I have 
another string type:

void
SomeClass::my_print(const QString& s)
{
...
}

How do I say to Boost.Python that everytime it sees a "QString" argument, 
and it have a Python "str", I can apply a simple converter, just like 
"std::string"?

By now, all I can do is write wrappers to method and do the converstion 
manually.
Ex:

void
 SomeClassWrapper::my_print(std::string s)
{
 SomeClass::my_print(QString::fromStdString(s));
 ...
}

Besides, I did not find in the rerefence manual a way to it. I was looking 
at the Boost.Python include directories for this kind of conversion and 
found a interesting named dir:
/usr/include/boost/python/converter/

But then, I realised that these headers are not all documented. Is it right, 
or I did not searched correctly?

Thanks for any help.

[Eric Jardim]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20050724/e41c8640/attachment.htm>


More information about the Cplusplus-sig mailing list