Re: [Cppcms-users] Plugins and templates
Brought to you by:
artyom-beilis
From: Artyom <art...@ya...> - 2010-08-19 10:35:16
|
> class IPlugin { > public: > virtual content::master handleRequest() const = 0; > }; > class HelloPlugin: public IPlugin { > public: > virtual content::master handleRequest() { > content::hello_plugin hp; > hp.title = "Hello"; > hp.message = "World"; > return hp; > } > } ---------------------------------------------------- In C++, objects are not references! They are values. ---------------------------------------------------- What happens there that content::master is copties from content::hello_plugin and this forgets all about hello_plugin. For handling polymorphic classes you need to use pointers or references. > > // This is the function that dispatches to plugin > void myapp::dispatcher(std::string module) { > IPlugin p = plugcontainer.getPluginByModule(module); > content::master m = p.handleRequest(); > render("hello_plugin",m); > } And in this point m is type of content::master so when in plugin it tryes to cast to the content::hello_pluging it fails for very good reason :-) What you need is something like this with smart pointers: class IPlugin { public: virtual booster::shared_ptr<content::master> handleRequest() const = 0; }; class HelloPlugin: public IPlugin { public: virtual booster::shared_ptr<content::master> handleRequest() { booster::shared_ptr<content::hello_plugin> hp(new content::hello_plugin); hp->title = "Hello"; hp->message = "World"; return hp; } } void myapp::dispatcher(std::string module) { IPlugin p = plugcontainer.getPluginByModule(module); booster::shared_ptr<content::master> m = p.handleRequest(); render("hello_plugin",*m); } This should work Best, Artyom |