Boost python export singleton

泪湿孤枕 提交于 2019-12-10 18:41:33

问题


I have a singleton (from boost::serialization):

class LogManager : public boost::serialization::singleton<LogManager> { ... };

And wrapper for getting instance:

inline LogManager &logManager() { return LogManager::get_mutable_instance(); }

What's the right way to bind this into boost.python module?

I tried:

class_< LogManager, boost::serialization::singleton<LogManager> >("LogManager", no_init)
    ...
;

As a result - a lot of ugly error text in console. What's wrong?


回答1:


In addition to using bases<...> in the second argument as Autopulated pointed out, I think you also want to specifiy boost::noncopyable as the third template argument, e.g.

bp::class_<LogManager, bp::bases<boost::serialization::singleton<LogManager> >, boost::noncopyable>("LogManager", bp::no_init)

Edit: Also, you need have a class declaration for any base classes listed, e.g.

bp::class_<boost::serialization::singleton<LogManager>, boost::noncopyable>("Singleton", bp::no_init)

Or, if you don't need access to the base class and won't be exporting any other children of boost::serialization::singleton<LogManager>, then you can omit specifying the base classes in the first place. That is, the following declaration is just fine if all you want to do is expose the LogManager class:

bp::class_<LogManager, boost::noncopyable>("LogManager", bp::no_init)



回答2:


You want bp::bases< boost::serialization::singleton<LogManager> > as the second template parameter instead.



来源:https://stackoverflow.com/questions/5299179/boost-python-export-singleton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!