[C++-sig] singleton wrapping

Libor Capak capak at inputwish.com
Mon Apr 24 10:34:00 CEST 2006


hello,

i'm quite new to boost::python. i'd like to ask how you solve
singleton wrapping.
i have simple template:

template<typename T> class Singleton
{
  static T* singleton;
public:
  //.. blabla ..
  static T& instance()
  {
     mustBe(singleton); return *singleton;
  }
  // .. blabla ..
};

and simple class, for example:

class A : public Singleton<A>
{
public:
  void foo();
};

and now to export into python module i'm using wrapper class:

class A_wrap
{
  static void foo() { A.instance().foo(); }
};

BOOST_PYTHON_MODULE(test)
{
  class_<A_wrap>("A", no_init)
    .def("foo", &A_wrap::foo)
    .staticmethod("foo")
    ;
}

is there any trick how to skip writing class A_wrap and still 
use method A::foo() as staticmethod from python?

Libor Capak




More information about the Cplusplus-sig mailing list