In my C++ code I have a class Foo with many methods taking a Bar type variable as an argument:
class Foo {
public:
void do_this(Bar b);
void do_that(Bar b);
/* ... */
};
Bar has a number of constructors that create a new object from many common types such as int, std::string, float, etc:
class Bar {
public:
Bar(int i);
Bar(float f);
Bar(std::string s);
/* ... */
};
I wrapped this with Boost::Python and I'm now able to call my Foo methods using Python literals directly, as they get implicitly converted to Bar objects.
f = Foo()
f.do_this(5)
f.do_that("hello")
Now, I would like to be able to use also other Python types, such as tuples, like this:
f.do_that((1,2,3))
But I don't want to touch the original Bar definition, and I don't want to pollute my C++ library with Boost::Python stuff. I want to write a wrapper function in my binding code, but I just can't understand if this is possible and what is the correct way for doing this.
In other words: can I register a factory function to be used as an automatic conversion in Python?