Using boost python I need create nested namespace.
Assume I have following cpp class structure:
namespace a
{
class A{...}
namespace b
{
class B{...}
}
}
Obvious solution not work:
BOOST_PYTHON_MODULE( a ) {
boost::python::class_<a::A>("A")
...
;
BOOST_PYTHON_MODULE(b){
boost::python::class_<a::b::B>("B")
...
;
}
}
It causes compile-time error: linkage specification must be at global scope
Is there any way to declare class B that would be accessed from Python as a.b.B
?
The trick with dummy classes is quite fine, but doesn't allow:
So, instead, use PyImport_AddModule(). You may find full featured examples in the following article: Packages in Python extension modules, by Vadim Macagon.
In short:
What you want is a boost::python::scope.
Python has no concept of 'namespaces', but you can use a class very much like a namespace:
Then in python, you have:
All
a
,a.A
,a.b
anda.b.B
are actually classes, but you can treata
anda.b
just like namespaces - and never actually instantiate them