Andre Poenitz wrote:
> On Thu, Mar 06, 2003 at 11:37:09AM +0000, Angus Leeming wrote:
>> Can I define a shortcut in FormCitation.C. Something like:
>> typedef getController<ControlCitation> controller;
>
> I think so. What happens if you try?
I can't get the typedef to work. (See class ViewCitation in the
attached file).
> [I would not use macros for that, even if it doesn't work...]
Hmm. I thought you'd say that ;-) Still it works, so I know a little
more.
--
Angus
#include <ostream>
#include <string>
class ControllerBase;
class ViewBase;
class Dialog {
public:
Dialog() : controller_(0), view_(0) {}
void controller(ControllerBase * controller) { c_ = controller; }
void view(ViewBase * view) { v_ = view; }
ControllerBase & controller() { return *c_; }
ViewBase & view() { return *view_; }
private:
ControllerBase * c_;
ViewBase * v_;
};
class ControllerBase {
public:
ControllerBase(Dialog & parent) : parent_(parent) {}
virtual ~ControllerBase() {}
private:
Dialog & parent_;
};
class ViewBase {
public:
ViewBase(Dialog & parent) : parent_(parent) {}
virtual void printcontroller() {}
template<typename Controller>
Controller & getController() const
{ return dynamic_cast<Controller &>(parent_.controller()); }
private:
Dialog & parent_;
};
class ControlCitation : public ControllerBase {
public:
ControlCitation(Dialog & parent) : ControllerBase(parent) {}
std::string const whoami() const { return "ControlCitation"; }
};
#define citationcontroller() getController<ControlCitation>()
class ViewCitation : public ViewBase {
public:
// doesn't like this
// function template "ViewBase::getController" is not a type name
// typedef getController<ControlCitation> controller;
ViewCitation(Dialog & parent) : ViewBase(parent) {}
virtual void printcontroller() {
std::cout << "ViewCitation's controller is "
<< getController<ControlCitation>().whoami()
<< std::endl;
std::cout << "using that macro "
<< citationcontroller().whoami()
<< std::endl;
}
};
int main() {
Dialog dialog;
dialog.controller(new ControlCitation(dialog));
dialog.view(new ViewCitation(dialog));
dialog.view().printcontroller();
return 0;
}