[C++-sig] How to correctly expose back shared_ptr-ed Python object to C++ (original) (raw)

Nodir GULYAMOV gelvaos at gmail.com
Mon Aug 10 21:43:57 CEST 2009


Hi Renato, I've really appreciated your answer. It's solved my issue. I will never find that I need to call base constructor explicitly.

Thank you, Nodir

Renato Araujo <renatox at gmail.com> writes:

Hi Nodir,

I saw a problem in your exemple. But I did not know if is this your problem, but try call the base constructor in your "PyTestPlugin" constructor, like that: class PyTestPlugin(IBasePlugins): def init(self): IBasePlugins.init(self) <<<<<<<<<<<<<<< here this need be called explicitly self.handletype = 2 def canHandle(self, type): return type == handletype def handle(self): print "handle() from python plugin" BR Renato Araujo Oliveira Filho

On Fri, Aug 7, 2009 at 3:54 PM, Nodir GULYAMOV<gelvaos at gmail.com> wrote: Hello All, I am trying to implement plug-ins system using BOOST Python but a little stuck. I've read documentation a couple of times and have feeling that missed something pretty simple. Below simplified version what I am trying to do: // abstract Base interface class class IBasePlugins { public:  virtual ~IBasePlugins() {};  virtual bool canHandle(int type) = 0;  virtual void handle() = 0; }; //some C++ internal plugin (no need to expose to python) class IntrPlugin : public IBasePlugins { public:  IntrPlugin() : handletype(1) {}  bool canHandle(int type) { return type == handletype;  }  void handle() { std::cout << "handle() from internal plugin" << std::endl;  } private:  int handletype; };

// class to manage plug-ins typedef boost::sharedptr PluginsPtrType; typedef vector VPluginsType; class PluginsMgr { public:  static void register(PluginsPtrType plugin) {  vplugins.puchback(plugin);  }  static void run(int type) { VPluginsType::iterator it; for(it = vplugins.begin(); it != vplugins.end(); it++) if( (*it)->canHandle(type) ) (*it)->handle();  } private: VPluginsType vplugins; }; // export to python struct IBasePluginsWrap : public IBasePlugins {  IBasePluginsWrap(PyObject* self): self(self) {}  bool canHandle(int type) {  return callmethod< bool >(self, "canHandle", type);  }  void handle() {  callmethod< void >(self, "handle");  }  PyObject* self; }; BOOSTPYTHONMODULE(pyPlugins) {  class< IBasePlugins, IBasePluginsWrap, boost::noncopyable >("IBasePlugins")  .def("canHandle", purevirtual(&IBasePlugins::canHandle))  .def("handle", purevirtual(&IBasePlugins::handle))  ;  class< PluginsMgr >("PluginsMgr")  .def("registerPlugin", &PlgMgr::registerPlugin)  .staticmethod("registerPlugin")  .def("run", &PlgMgr::run)  .staticmethod("run")  ;  registerptrtopython(); } Then I am using it from python as follows: from pyPlugins import * class PyTestPlugin(IBasePlugins):  def init(self):  self.handletype = 2  def canHandle(self, type):  return type == handletype  def handle(self):  print "handle() from python plugin" pm = PluginsMgr() testplg = PyTestPlugin() pm.registerPlugin(testplg) And have error that C++ signature mismatch. Thank you in advance. I will really appreciate any help. Kind regards, Nodir


Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig


Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig



More information about the Cplusplus-sig mailing list