2013-01-22 6 views
5

私たちのグループは、C++を使用して数値フレームワークを開発しています。 Pythonで利用できるようにするために、フレームワークの基本部分をラップしたいと思っています。我々の武器はBoost.Pythonです。私たちは既に他の目的のためにBoostしているからです。ポリモフィズムをサポートするためにsmart_ptrsを単独で使用しています。私は次の警告は、コンパイルshared_ptrを使用してboost.pythonの所有者として抽象基盤を抽象化

時にポップアップするので

#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を使用して全体をラップする場合

#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; 
}; 

:次のスニペットは、我々は戦略パターンを適用する方法の簡単な例です。

C:/boost/include/boost-1_51/boost/python/object/instance.hpp:14:36: warning: type attributes ignored after type is already defined [-Wattributes] 

私はPythonでラッパーを使用しようとすると、私は次のエラー

を取得

私たちのフレームワークを変更せずにすべてを動作させるには、何が欠けていますか?ラッパーはもちろん自由に適応させることができます。

+0

ここで答えが得られない場合は、Boost.Pythonメーリングリストgmane.comp.python.C++を試してみてください。 – user763305

答えて

4

私は解決策を見つけました。以下に示すように、モジュール宣言でimplicitly_convertibleを使用しなければなりません。

#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); 

    implicitly_convertible<boost::shared_ptr<Foo> , StrategyPtr>(); 
    implicitly_convertible<boost::shared_ptr<Bar> , StrategyPtr>(); 
} 
関連する問題