3

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?

Max Frai
  • 61,946
  • 78
  • 197
  • 306

2 Answers2

4

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)
Ray
  • 4,531
  • 1
  • 23
  • 32
  • Hm, now it compiles. But what does 3d argument do? – Max Frai Mar 14 '11 at 14:16
  • But when I import the module I get: `RuntimeError: extension class wrapper for base class boost::serialization::singleton has not been created yet` – Max Frai Mar 14 '11 at 14:21
  • As I understand it, it tells boost::python not to make a converter for functions which would return an instance of the wrapped class by value. – Ray Mar 14 '11 at 14:23
1

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

James
  • 24,676
  • 13
  • 84
  • 130
  • ps: `bp::class_< LogManager, bp::bases< boost::serialization::singleton > >("LogManager", bp::no_init)` – Max Frai Mar 14 '11 at 13:50
  • Ah, yes; well see Ray's answer :) Those errors are because it's trying to expose it as copy-constructible, when you want it to be non-copyable. – James Mar 14 '11 at 17:33