Our group is developing a numerical structure using C ++. Now we would like to wrap up the main parts of our infrastructure that will be available in Python. Our weapon of choice is Boost.Python, as we are already Boost for other purposes. We use smart_ptrs to support polymorphism. The following snippet is a simple example of how we apply a strategy template:
#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;
};
If I wrap it all with Boost.Python like this
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 );
}
during compilation the following warning appears
C:/boost/include/boost-1_51/boost/python/object/instance.hpp:14:36: warning: type attributes ignored after type is already defined [-Wattributes]
When I try to use wrappers in python, I get the following errors
>>> 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>)
What is not enough for all this to work without changing our structure? Of course, wrappers can be freely adapted.