From: Cedric D. <ced...@in...> - 2014-06-17 19:26:53
|
I am sorry, there are some typos in reported Swig messages. One should read: Error: Template 'Singleton' undefined. Warning 401: Nothing known about base class 'Singleton< Foo >'. Ignored. Warning 401: Maybe you forgot to instantiate 'Singleton< Foo >' using %template. ------- Simpler question ------------- How do you handle a nested definition like this? template < class T > class Base {}; class Derived : public Base < Derived > {}; Best, Cédric ----- Mail original ----- > De: "Cedric Doucet" <ced...@in...> > À: "William S Fulton" <ws...@fu...> > Cc: swi...@li... > Envoyé: Mardi 17 Juin 2014 21:14:33 > Objet: Re: [Swig-user] Swig and C++ singleton > > > > Thank you very much for your answer William. > > Unfortunately, it does not work with > > #include "singleton.hpp" > %template(SingletonFoo) Singleton<Foo>; > #include "foo.hpp" > > I get the following messages: > > Error: Template 'Singleton' undefined. > Warning 401: Nothing known about base class 'Singleton< FelisceParam >'. > Ignored. > Warning 401: Maybe you forgot to instantiate 'Singleton< FelisceParam >' > using %template. > > I wonder if the problem comes from the fact that Foo inherits from > Singleton<Foo> which should know Foo to be instantiated. > > Thank you very much for your help! > > > > > ----- Mail original ----- > > De: "William S Fulton" <ws...@fu...> > > À: "Cedric Doucet" <ced...@in...> > > Cc: swi...@li... > > Envoyé: Mardi 17 Juin 2014 20:26:04 > > Objet: Re: [Swig-user] Swig and C++ singleton > > > > On 16/06/14 16:11, Cedric Doucet wrote: > > > 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" > > Move the %template below to here. It will then be instantiated in the > > type system before it is used as a base in foo.hpp. > > > > > #include "foo.hpp" > > > > > > %include "singleton.hpp" > > > %include "foo.hpp" > > > > > > %template(SingletonFoo) Singleton<Foo>; > > > -------------------------------------------------------------- > > > > > > > William > > > > ------------------------------------------------------------------------------ > HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions > Find What Matters Most in Your Big Data with HPCC Systems > Open Source. Fast. Scalable. Simple. Ideal for Dirty Data. > Leverages Graph Analysis for Fast Processing & Easy Data Exploration > http://p.sf.net/sfu/hpccsystems > _______________________________________________ > Swig-user mailing list > Swi...@li... > https://lists.sourceforge.net/lists/listinfo/swig-user > |