On Mon, Feb 08, 2010 at 10:36:31AM -0800, Albrecht Schlosser wrote: > > Hi, developers, > > I propose to add these two virtual methods to Fl_Widget: > > [1] virtual int Fl_Widget::is_group() > [2] virtual int Fl_Widget::is_window() > > Rationale: I'd like to eventually get rid of the "pseudo RTTI" > usage of Fl_Widget::type(), as in: > > [3] if (widget->type() >= FL_WINDOW) ... // > > However, since type() [3] is a fast inline method and the new > proposed methods are virtual, there might be a small performance > penalty if we would replace this all over in the FLTK core. > Therefore I wouldn't suggest to do this in a first step. > > The implementation is trivial, and methods with these names > (and functions) are already in FLTK 2. However, the implementation > in FLTK 2 uses type() and some bit values in type() for GROUP > and WINDOW. The drawback of this approach is trouble as seen in > > http://www.fltk.org/str.php?L1915 > > Thus, I propose to add these two virtual methods and use them > where appropriate. They would also be useful for user code > and the new (work in progress) Fl_Printer support. > > Fl_Widget::is_group() would be useful for traversing a widget > tree (to avoid dynamic_cast). > > Opinions, better proposals, votes please ... I like the idea. I did dynamic_cast() in the past too. However, I assume the next lines of code will come a lot: if (widget->is_group()) { Fl_Group *group = (Fl_Group *)widget; .... } which involves a static cast.
I propose a slightly modified one: virtual Fl_Group* Fl_Widget::as_group() virtual Fl_Window* Fl_Widget::as_window() At the cost of having used a Fl_Group in Fl_Widget.H, all static/dynamic casts are eliminated, as the code needing a cast would now move into Fl_Group, where it is not needed to typecast to Fl_Group. If this looks not too good, I'd still the ::is_group() and ::is_window() are good improvements. > > Thanks > > Albrecht Kurt _______________________________________________ fltk-dev mailing list fltk-dev@easysw.com http://lists.easysw.com/mailman/listinfo/fltk-dev