Boost::python Exposing C++ functions using and returning templates
I need to build python bindings for a C++ codebase. I use boost::python and I ran into problems trying to expose classes containing functions using and returning templates. Here is a typical example
class F开发者_JAVA技巧oo
{
public:
Foo();
template<typename T> Foo& setValue(
const string& propertyName, const T& value);
template<typename T> const T& getValue(
const string& propertyName);
};
Typical T are string, double, vector.
After reading the documentation, I tried using thin wrappers for every type used. Here are the wrappers for string and double and the corresponding class declaration.
Foo & (Foo::*setValueDouble)(const std::string&,const double &) =
&Foo::setValue;
const double & (Foo::*getValueDouble)(const std::string&) =
&Foo::getValue;
Foo & (Foo::*setValueString)(const std::string&,const std::string &) =
&Foo::setValue;
const std::string & (Foo::*getValueString)(const std::string&) =
&Foo::getValue;
class_<Foo>("Foo")
.def("setValue",setValueDouble,
return_value_policy<reference_existing_object>())
.def("getValue",getValueDouble,
return_value_policy<copy_const_reference>())
.def("getValue",getValueString,
return_value_policy<copy_const_reference>())
.def("setValue",setValueString,
return_value_policy<reference_existing_object>());
It compiles ok but when I try to use the python bindings, I get a C++ exception.
>>> f = Foo()
>>> f.setValue("key",1.0)
>>> f.getValue("key")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
RuntimeError: unidentifiable C++ exception
Interestingly, when I only expose Foo for double or string value, i.e
class_<Foo>("Foo")
.def("getValue",getValueString,
return_value_policy<copy_const_reference>())
.def("setValue",setValueString,
return_value_policy<reference_existing_object>());
It works fine. Am I missing something?
This may not be related to your problem directly but I would not trust function signature casting with templates like that. I would have wrapped it like this:
class_<Foo>("Foo")
.def("setValue", &Foo::setValue<double>,
return_value_policy<reference_existing_object>())
.def("getValue", &Foo::getValue<double>,
return_value_policy<copy_const_reference>())
.def("getValue", &Foo::getValue<std::string>,
return_value_policy<copy_const_reference>())
.def("setValue", &Foo::setValue<std::string>,
return_value_policy<reference_existing_object>());
If that does not work, you may need to create some shim functions:
Foo& setValueDouble(foo& self, const string& propertyName, const double value)
{
return self.setValue(propertyName, value)
}
...
and export those as thought they were member functions.
Exporting multiple function overloads to the same name is a perfectly valid thing to do in Boost::Python, so I do not think that that is the problem.
I suspect the problem is that boost::python does not know which overload to call for "getValue" - should it call getValueDouble or getValueString? If you bind them explicitly as getValueString and getValueDouble (as the method name) I bet it will work.
What about creating a C++ wrappers for getters/setters that return/take a boost::python::object? You can then simply determine the type you got in your C++ wrapper and wrap/unwrap it into/from boost::python::object.
struct FooWrap : public Foo
{
using boost::python;
Foo& setValueO(const string& propertyName, const object& obj)
{
object value;
if(PyInt_Check(obj.ptr())) {
return setValue<int>(propertyName, extract<int>(obj);
} else if(PyFloat_Check(obj.ptr())) {
return setValue<double>(propertyName, extract<double>(obj);
} else if(PyString_Check(obj.ptr())) {
return setValue<std::string>(propertyName, extract<std::string>(obj);
}
// etc...
}
object getValueO(const std::string& propertyName)
{
if(determineType() == TYPE_INT) { // however you determine the type
return object(getValue<int>(propertyName));
} else if(determineType() == TYPE_DOUBLE) {
return object(getValue<double>(propertyName));
} else if(determineType() == TYPE_STRING) {
return object(getValue<std::string>(propertyName));
}
// etc...
}
};
class_<Foo>("Foo")
.def("setValue", &FooWrap::setValueO,
return_value_policy<reference_existing_object>())
.def("getValue", &FooWrap::getValueO,
return_value_policy<copy_const_reference>())
精彩评论