Re: [Cppcms-users] Plugins and templates
Brought to you by:
artyom-beilis
From: Artyom <art...@ya...> - 2010-08-19 07:28:36
|
> Just another question: > The plugins currently return their own content class derived from a > master class containing common properties such as a page title. > Because C++ doesn't allow to redefine return types when overloading > virtual methods, I use a booster::shared_ptr<content::master> for > passing the content class. Actually you can redefine. But not for shared pointer but rather for ordinary pointers and references. i.e. class A{}; class B: public A{} class foo { public: virtual A &get(); }; class bar { public: virtual B &get(); }; Is legal as B derived from A. This is what is called co-variant types. So you may do this. > Example: > Plugin HelloWorld has a content class content::hello_world derived from > content::master which is returned on calling handleRequest() wrapped in > a booster::shared_ptr<content::master>. This content class now has to be > rendered by the main application which does not now which content > subclass is wrapped in the pointer / knows it at runtime only, so that a > hard-compiled conversion via > booster::dynamic_pointer_cast<content::hello_world,content::master> is > not possible, because there could be other plugins returning different > subclasses of the master than hello_world. > The plugin tells the main application to use the view 'hello_world' for > rendering. This view uses content::hello_world, because it needs access > to some fields defined only in the subclass. > > Now, if I call > > render("hello_world", *c.get()); > > where c is the returned shared_ptr, cppcms catches a std::bad_cast > exception occuring in http_context.c, line 132. First of all `render` function always receives `content::master` and dynamic casts after-wards in views to correct content. If cast fails it throws bad_cast. > If I use booster::dynamic_pointer_cast beforehand and pass the casted > shared_ptr, everything works. What do you cast? Do you mean that you cast to `content::hello_world` ? It seems to me this is more shared object issues. Do you compile your application with -rdynamic or --export-dynamic. see: <http://art-blog.no-ip.info/wikipp/en/page/cppcms_1x_templates_bld#Dynamic+Loading+of+Views> Because use it looks like this issue. If you don't compile your applications with -rdynamic, dynamic cast between shared object boundaries may fail. This is very important to remember. Artyom |