Hello,

I am trying to use Swig to use a C++ implementation of singleton pattern in Python but I do not manage to do it.
I get these messages:
    Warning 401: Base class 'Singleton< Foo >' undefined.
    Warning 401: 'Singleton< Foo >' must be defined before it is used as a base class.

My singleton class is defined by the following header file:
----------------------------------------------------------------------------
singleton.hpp
----------------------------------------------------------------------------
template<class T> class Singleton {
  static T & instance()
  { if ( !m_instance ) m_instance = new T(); return *m_instance; }
protected:
  Singleton(){}
  ~Singleton(){}
private:
  Singleton(Singleton const &);
  Singleton & operator=(Singleton const &);
private:
  static T * m_instance = NULL;
};
----------------------------------------------------------------------------

As usual, a singleton cannot be instantiated but the static method instance() returns the unique instance of the singleton after creating this instance if necessary.
To make a singleton of type Foo, one may define Foo class like this:
--------------------------------------------------------------------------------
foo.hpp
--------------------------------------------------------------------------------
#include "singleton.hpp"
class Foo : public Singleton<Foo>
{
    friend class Singleton<Foo>;
public:
    void my_method()
    { /* implementation */ }
private:
  Foo(){};
  ~Foo(){};
  Foo(const Foo &);
  Foo & operator=(Foo const &);
};
--------------------------------------------------------------------------------

In practice, on uses Foo class by getting the unique instance of it and calling any member method, such as my_method, from it:
-------------------------------------------------------------
main.cpp
-------------------------------------------------------------
#include <cstdlib>
#include "foo.hpp"
int main()
{
    Foo::instance().my_method();
    return EXIT_SUCCESS;
}

To create an interface with Swig, I have a file foo.i which contains the following lines:

--------------------------------------------------------------
foo.i
--------------------------------------------------------------
#include "singleton.hpp"
#include "foo.hpp"

%include "singleton.hpp"
%include "foo.hpp"

%template(SingletonFoo) Singleton<Foo>;
--------------------------------------------------------------

Does anyone know what I am doing wrong?

Thank you very much for your help!

Best regards,

CÚdric Doucet
Inria Paris-Rocquencourt