From 6ec7e50758f51319245653cbae786026cd22e856302ab06a249095d6d4639c18 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Sat, 9 Aug 2025 05:27:43 -0400 Subject: [PATCH] `instanceOf` now uses RTTI and checks its answer. Next I'll just remove it, but this commit lets me have something in history such that I can fall back to check for bugs that might get introduced. --- dw/oofawarewidget.cc | 8 ++++---- dw/oofposrelmgr.cc | 2 +- dw/table.cc | 2 +- dw/textblock.cc | 10 +++++----- lout/identity.cc | 2 +- lout/identity.hh | 16 +++++++++++++++- src/html.cc | 2 +- 7 files changed, 28 insertions(+), 14 deletions(-) diff --git a/dw/oofawarewidget.cc b/dw/oofawarewidget.cc index d92bff4..5ed5b64 100644 --- a/dw/oofawarewidget.cc +++ b/dw/oofawarewidget.cc @@ -131,7 +131,7 @@ bool OOFAwareWidget::isOOFContainer (Widget *widget, int oofmIndex) switch (oofmIndex) { case OOFM_FLOATS: - return widget->instanceOf (OOFAwareWidget::CLASS_ID) && + return widget->instanceOf () && (// For floats, only some OOF aware widgets are considered as // containers. ((OOFAwareWidget*)widget)->isPossibleOOFContainer (OOFM_FLOATS) && @@ -145,7 +145,7 @@ bool OOFAwareWidget::isOOFContainer (Widget *widget, int oofmIndex) // is also a text block, so possible float container) // within a table widget, which is not a suitable float // container parent). - !(widget->getParent()->instanceOf (OOFAwareWidget::CLASS_ID) && + !(widget->getParent()->instanceOf () && ((OOFAwareWidget*)widget->getParent()) ->isPossibleOOFContainerParent (OOFM_FLOATS)) || // Inline blocks are containing blocks, too. @@ -163,7 +163,7 @@ bool OOFAwareWidget::isOOFContainer (Widget *widget, int oofmIndex) case OOFM_RELATIVE: case OOFM_ABSOLUTE: - return widget->instanceOf (OOFAwareWidget::CLASS_ID) && + return widget->instanceOf () && (widget->getParent() == NULL || OOFAwareWidget::testWidgetPositioned (widget)); @@ -192,7 +192,7 @@ void OOFAwareWidget::notifySetParent () widget != NULL && oofContainer[oofmIndex] == NULL; widget = widget->getParent ()) if (isOOFContainer (widget, oofmIndex)) { - assert (widget->instanceOf (OOFAwareWidget::CLASS_ID)); + assert (widget->instanceOf ()); oofContainer[oofmIndex] = (OOFAwareWidget*)widget; } diff --git a/dw/oofposrelmgr.cc b/dw/oofposrelmgr.cc index f5c5062..f4f89ef 100644 --- a/dw/oofposrelmgr.cc +++ b/dw/oofposrelmgr.cc @@ -66,7 +66,7 @@ void OOFPosRelMgr::calcWidgetRefSize (Widget *widget, Requisition *size) // (Notice also that Widget::sizeRequest has to be called in all // cases.) - if (widget->instanceOf (OOFAwareWidget::CLASS_ID)) + if (widget->instanceOf ()) *size = *((OOFAwareWidget*)widget)->getRequisitionWithoutOOF (); diff --git a/dw/table.cc b/dw/table.cc index 14213d8..03723a5 100644 --- a/dw/table.cc +++ b/dw/table.cc @@ -607,7 +607,7 @@ AlignedTableCell *Table::getCellRef () int n = curCol + row * numCols; if (childDefined (n)) { child = children->get(n)->cell.widget; - if (child->instanceOf (AlignedTableCell::CLASS_ID)) + if (child->instanceOf ()) return (AlignedTableCell*)child; } } diff --git a/dw/textblock.cc b/dw/textblock.cc index 78b1a9b..9c0ac6e 100644 --- a/dw/textblock.cc +++ b/dw/textblock.cc @@ -2523,7 +2523,7 @@ void Textblock::addWidget (core::Widget *widget, core::style::Style *style) widget->setParent (this); // TODO Replace (perhaps) later "textblock" by "OOF aware widget". - if (widget->instanceOf (Textblock::CLASS_ID)) { + if (widget->instanceOf ()) { for (int i = 0; i < NUM_OOFM; i++) searchOutOfFlowMgr(i)->addWidgetInFlow ((Textblock*)widget, this, words->size ()); @@ -2765,7 +2765,7 @@ void Textblock::addParbreak (int space, core::style::Style *style) consider normal flow, no floats etc.) */ for (Widget *widget = this; widget->getParent() != NULL && - widget->getParent()->instanceOf (Textblock::CLASS_ID) && + widget->getParent()->instanceOf () && !isWidgetOOF (widget); widget = widget->getParent ()) { Textblock *textblock2 = (Textblock*)widget->getParent (); @@ -2959,7 +2959,7 @@ void Textblock::handOverBreak (core::style::Style *style) Line *lastLine = lines->getRef (lines->size () - 1); if (lastLine->breakSpace != 0 && (parent = getParent()) && - parent->instanceOf (Textblock::CLASS_ID) && + parent->instanceOf () && parent->getStyle()->display != core::style::DISPLAY_BLOCK) { Textblock *textblock2 = (Textblock*) parent; textblock2->addParbreak(lastLine->breakSpace, style); @@ -3193,7 +3193,7 @@ int Textblock::getGeneratorWidth () // We only examine instances of dw::Textblock, since they are relevant // for floats, for which this method is only called. if(word->content.type == core::Content::WIDGET_IN_FLOW && - word->content.widget->instanceOf(Textblock::CLASS_ID)) { + word->content.widget->instanceOf()) { Textblock *tbChild = (Textblock*)word->content.widget; if(tbChild->findSizeRequestReference(this, &xRel, NULL)) wChild = max(wChild, xRel + tbChild->getGeneratorWidth()); @@ -3291,7 +3291,7 @@ RegardingBorder *Textblock::getWidgetRegardingBorderForLine (int firstWord, if (word->content.type == core::Content::WIDGET_IN_FLOW) { Widget *widget = word->content.widget; - if (widget->instanceOf (RegardingBorder::CLASS_ID) && + if (widget->instanceOf () && // Exclude cases where a textblock constitutes a new floats // container. !isOOFContainer (widget, OOFM_FLOATS)) diff --git a/lout/identity.cc b/lout/identity.cc index db63f1a..8953b3b 100644 --- a/lout/identity.cc +++ b/lout/identity.cc @@ -99,7 +99,7 @@ void IdentifiableObject::registerName (const char *className, const std::type_in * \brief Returns, whether this class is an instance of the class, given by * \em otherClassId, or of a sub class of this class. */ -bool IdentifiableObject::instanceOf (std::intptr_t otherClassId) +bool IdentifiableObject::instanceOf_impl (std::intptr_t otherClassId) { if (otherClassId == -1) // Other class has not been registered yet, while it should have been, diff --git a/lout/identity.hh b/lout/identity.hh index aea9952..0b20bbb 100644 --- a/lout/identity.hh +++ b/lout/identity.hh @@ -3,6 +3,8 @@ #include #include +#include +#include #include #include @@ -143,7 +145,19 @@ public: */ const char *getClassName() { return classesById.at( classId )->className; } - bool instanceOf (std::intptr_t otherClassId); + template< typename T > + [[deprecated]] + inline bool + instanceOf() + { + const bool expected= dynamic_cast< T * >( this ); + const bool witness= instanceOf_impl( T::CLASS_ID ); + if( witness != expected ) throw std::logic_error{ "RTTI based `instanceOf` disagrees with Dillo's lout:: impl." }; + return expected; + } + + private: + bool instanceOf_impl (std::intptr_t otherClassId); }; } // namespace identity diff --git a/src/html.cc b/src/html.cc index 1e79358..c7417f2 100644 --- a/src/html.cc +++ b/src/html.cc @@ -798,7 +798,7 @@ bool DilloHtml::HtmlLinkReceiver::click (Widget *widget, int link, int img, /* Change the link color to "visited" as visual feedback */ for (Widget *w = widget; w; w = w->getParent()) { _MSG(" ->%s\n", w->getClassName()); - if (w->instanceOf(dw::Textblock::CLASS_ID)) { + if (w->instanceOf()) { ((Textblock*)w)->changeLinkColor (link, html->visited_color); break; }