I want to do it in a right way. I've seen exposing boost::serialization::singleton here Boost python export singleton but I don't want to use that. I want to use simple meyers singleton instead.
This code below works, but documentation says that using http://www.boost.org/doc/libs/1_43_0/libs/python/doc/v2/reference_existing_object.html#reference_existing_object-spec/ is dangerous.
Code:
class Singleton
{
private:
Singleton(){};
public:
static Singleton & getInstance()
{
static Singleton instance;
return instance;
}
int getNumber() { return 5; }
};
And in module:
class_<Singleton>("singleton", no_init)
.def("getInstance", &Singleton::getInstance, return_value_policy<reference_existing_object>()).staticmethod("getInstance")
.def("getNumber", &Singleton::getNumber)
;
What is good way to do it? Using return_internal_reference<>()
resulted in error while executing python code.