मुझे सी ++ कोडबेस के लिए पायथन बाइंडिंग बनाने की आवश्यकता है। मैं बूस्ट :: पायथन का उपयोग करता हूं और मैं टेम्पलेट्स का उपयोग करके और लौटने वाले कार्यों वाले वर्गों को बेनकाब करने की कोशिश करने में समस्याओं में भाग गया। यहां एक सामान्य उदाहरणबूस्ट :: पायथन एक्सपोज़िंग सी ++ फ़ंक्शंस का उपयोग करके और लौटने वाले फ़ंक्शन
class Foo
{
public:
Foo();
template<typename T> Foo& setValue(
const string& propertyName, const T& value);
template<typename T> const T& getValue(
const string& propertyName);
};
विशिष्ट टी स्ट्रिंग, डबल, वेक्टर हैं।
documentation पढ़ने के बाद, मैंने हर प्रकार के इस्तेमाल के लिए पतले रैपर का उपयोग करने की कोशिश की। स्ट्रिंग और डबल और इसी श्रेणी की घोषणा के लिए रैपर हैं।
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>());
यह ठीक संकलित करता है लेकिन जब मैं पाइथन बाइंडिंग का उपयोग करने का प्रयास करता हूं, तो मुझे सी ++ अपवाद मिलता है।
>>> f = Foo()
>>> f.setValue("key",1.0)
>>> f.getValue("key")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
RuntimeError: unidentifiable C++ exception
दिलचस्प बात यह है कि जब मैं केवल डबल या स्ट्रिंग मान के लिए फू का पर्दाफाश, यानी
class_<Foo>("Foo")
.def("getValue",getValueString,
return_value_policy<copy_const_reference>())
.def("setValue",setValueString,
return_value_policy<reference_existing_object>());
यह ठीक काम करता है। क्या मुझे कुछ याद आ रही है?
आप http://mail.python.org/pipermail/cplusplus-sig/2006-February/009990.html पर धागा (जो एक समान समस्या tackles को देखने के लिए चाहते हो सकता है: रैपिंग बढ़ावा :: किसी भी) – lijie