के साथ होल्डर के रूप में अमूर्त आधार पर उपयोग करना हमारा समूह C++ का उपयोग करके एक संख्यात्मक ढांचा विकसित कर रहा है। अब हम पाइथन में उपलब्ध होने के लिए हमारे ढांचे के बुनियादी हिस्सों को लपेटना चाहते हैं। पसंद का हमारा हथियार Boost.Python है क्योंकि हम पहले से ही अन्य प्रयोजनों के लिए भी बूस्ट हैं। हम पूरी तरह से polymorphism का समर्थन करने के लिए smart_ptrs का उपयोग करें।boost.pythr को बूस्ट.pypton
#include <boost/shared_ptr.hpp>
struct AbsStrategy
{
virtual std::string talk() = 0;
};
typedef boost::shared_ptr<AbsStrategy> StrategyPtr;
struct Foo : AbsStrategy
{
std::string talk()
{
return "I am a Foo!";
}
};
struct Bar : AbsStrategy
{
std::string talk()
{
return "I am a Bar!";
}
};
struct Client
{
Client(StrategyPtr strategy) :
myStrategy(strategy)
{
}
bool checkStrategy(StrategyPtr strategy)
{
return (strategy == myStrategy);
}
StrategyPtr myStrategy;
};
अगर मैं ऐसा
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(BoostPython)
{
class_<Foo>("Foo")
.def("talk", &Foo::talk);
class_<Bar>("Bar")
.def("talk", &Bar::talk);
class_<Client>("Client", init<StrategyPtr>())
.def("checkStrategy", &Client::checkStrategy);
}
तरह Boost.Python का उपयोग कर पूरी बात लपेट निम्न चेतावनी संकलन
दौरान पॉप अप होता है: निम्नलिखित स्निपेट कैसे हम रणनीति पैटर्न लागू होगा का एक सरल उदाहरण हैC:/boost/include/boost-1_51/boost/python/object/instance.hpp:14:36: warning: type attributes ignored after type is already defined [-Wattributes]
जब मैं अजगर में रैपर इस्तेमाल करने की कोशिश, मैं निम्नलिखित त्रुटियाँ
>>> from BoostPython import *
>>> foo = Foo()
>>> bar = Bar()
>>> client = Client(foo)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Tree.__init__(Tree, Foo)
did not match C++ signature:
__init__(_object*, boost::shared_ptr<AbsStrategy>)
>>> client = Client(bar)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
Tree.__init__(Tree, Bar)
did not match C++ signature:
__init__(_object*, boost::shared_ptr<AbsStrategy>)
हमारी ढांचे को बदलने के बिना पूरी चीज को काम करने के लिए क्या गुम है? रैपर को निश्चित रूप से स्वतंत्र रूप से अनुकूलित किया जा सकता है।
यदि आपको यहां कोई जवाब नहीं मिलता है, तो Boost.Python मेलिंग सूची, gmane.comp.python.C++ को आजमाएं। – user763305