From e4f1102e325d4f244a9862612b6daf1d80d10624e72c871972f80f5bad87f7d6 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Fri, 22 Aug 2025 13:58:05 -0400 Subject: [PATCH] Some of the `Object::equals` now are const correct. There's more to do. Why make this const-correct, instead of ditch it? Because whatever I replace it with has to be ready for const correctness. --- dw/iterator.cc | 12 +++++++----- dw/iterator.hh | 4 ++-- dw/style.cc | 6 +++--- dw/style.hh | 8 ++++---- lout/container.cc | 2 +- lout/container.hh | 6 +++--- lout/object.cc | 15 ++++++++------- lout/object.hh | 17 ++++++++++------- 8 files changed, 38 insertions(+), 32 deletions(-) diff --git a/dw/iterator.cc b/dw/iterator.cc index fa48a44..1b0c697 100644 --- a/dw/iterator.cc +++ b/dw/iterator.cc @@ -47,15 +47,17 @@ Iterator::~Iterator() { } -bool Iterator::equals (Object *other) +bool +Iterator::equals( const Object *const other ) const { - Iterator *otherIt = (Iterator*)other; + Iterator *const otherIt = const_cast< Iterator * >( dynamic_cast< const Iterator * >( other ) ); return this == otherIt || - (getWidget() == otherIt->getWidget() && compareTo(otherIt) == 0); + (const_cast< Iterator * >( this )->getWidget() == otherIt->getWidget() && const_cast< Iterator * >( this )->compareTo(otherIt) == 0); } -void Iterator::intoStringBuffer(misc::StringBuffer *sb) +void +Iterator::intoStringBuffer(misc::StringBuffer *sb) const { sb->append ("{ widget = "); //widget->intoStringBuffer (sb); @@ -68,7 +70,7 @@ void Iterator::intoStringBuffer(misc::StringBuffer *sb) Content::maskIntoStringBuffer (mask, sb); sb->append (", content = "); - Content::intoStringBuffer (&content, sb); + Content::intoStringBuffer (const_cast< Content * >( &content ), sb); sb->append (" }"); } diff --git a/dw/iterator.hh b/dw/iterator.hh index cee850c..a41ad20 100644 --- a/dw/iterator.hh +++ b/dw/iterator.hh @@ -30,8 +30,8 @@ private: Content::Type mask; public: - bool equals (Object *other); - void intoStringBuffer(lout::misc::StringBuffer *sb); + bool equals (const Object *other) const override; + void intoStringBuffer(lout::misc::StringBuffer *sb) const override; inline Widget *getWidget () { return widget; } inline Content *getContent () { return &content; } diff --git a/dw/style.cc b/dw/style.cc index f6fcead..55d1a60 100644 --- a/dw/style.cc +++ b/dw/style.cc @@ -155,7 +155,7 @@ bool StyleAttrs::sizeDiffs (StyleAttrs *otherStyle) return false; } -bool StyleAttrs::equals (object::Object *other) { +bool StyleAttrs::equals (const object::Object *other) const { StyleAttrs *otherAttrs = (StyleAttrs *) other; return this == otherAttrs || @@ -398,7 +398,7 @@ void Style::copyAttrs (StyleAttrs *attrs) // ---------------------------------------------------------------------- -bool FontAttrs::equals(object::Object *other) +bool FontAttrs::equals(const object::Object *other) const { FontAttrs *otherAttrs = (FontAttrs*)other; return @@ -456,7 +456,7 @@ bool Font::exists (Layout *layout, const char *name) // ---------------------------------------------------------------------- -bool ColorAttrs::equals(object::Object *other) +bool ColorAttrs::equals(const object::Object *other) const { ColorAttrs *oc = (ColorAttrs*)other; return this == oc || (color == oc->color); diff --git a/dw/style.hh b/dw/style.hh index a256658..5b5eb23 100644 --- a/dw/style.hh +++ b/dw/style.hh @@ -508,7 +508,7 @@ public: int top, right, bottom, left; inline void setVal(int val) { top = right = bottom = left = val; } - inline bool equals (Box *other) { + inline bool equals (const Box *other) const { return top == other->top && right == other->right && bottom == other->bottom && @@ -603,7 +603,7 @@ public: inline bool hasBackground () { return backgroundColor != NULL || backgroundImage != NULL; } - bool equals (lout::object::Object *other); + bool equals (const lout::object::Object *other) const override; int hashValue (); }; @@ -688,7 +688,7 @@ public: FontVariant fontVariant; FontStyle style; - bool equals(lout::object::Object *other); + bool equals(const lout::object::Object *other) const override; int hashValue(); }; @@ -742,7 +742,7 @@ public: inline int getColor () { return color; } - bool equals(lout::object::Object *other); + bool equals(const lout::object::Object *other) const override; int hashValue(); }; diff --git a/lout/container.cc b/lout/container.cc index 25aaf0b..10400ae 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -313,7 +313,7 @@ int List::size () return numElements; } -bool List::equals(Object *other) +bool List::equals(const Object *other) const { List *otherList = (List*)other; Node *node1 = first, *node2 = otherList->first; diff --git a/lout/container.hh b/lout/container.hh index c214e1b..076757e 100644 --- a/lout/container.hh +++ b/lout/container.hh @@ -242,7 +242,7 @@ public: List(bool ownerOfObjects); ~List(); - bool equals(Object *other); + bool equals(const Object *other) const override; int hashValue(); int size (); @@ -451,8 +451,8 @@ public: Collection () { this->base = NULL; } ~Collection () { if (this->base) delete this->base; } - bool equals(Object *other) - { return this->base->equals (((Collection*)other)->base); } + bool equals(const Object *other) const override + { return this->base->equals (((const Collection*)other)->base); } int hashValue() { return this->base->hashValue (); } diff --git a/lout/object.cc b/lout/object.cc index 56671e2..6041237 100644 --- a/lout/object.cc +++ b/lout/object.cc @@ -48,7 +48,8 @@ Object::~Object() * this makes casting of "other" safe. Typically, an implementation should * check this == other first, the caller can assume a fast implementation. */ -bool Object::equals(Object *other) +bool +Object::equals( const Object *const other ) const { misc::assertNotReached (); return false; @@ -149,7 +150,7 @@ StandardComparator standardComparator; // Pointer // ------------- -bool Pointer::equals(Object *other) +bool Pointer::equals(const Object *other) const { return value == ((Pointer*)other)->value; } @@ -187,7 +188,7 @@ void Pointer::intoStringBuffer(misc::StringBuffer *sb) // Integer // ------------- -bool Integer::equals(Object *other) +bool Integer::equals(const Object *other) const { return value == ((Integer*)other)->value; } @@ -213,7 +214,7 @@ int Integer::compareTo(Comparable *other) // Boolean // ------------- -bool Boolean::equals(Object *other) +bool Boolean::equals(const Object *other) const { bool value2 = ((Boolean*)other)->value; // TODO Does "==" work? @@ -239,7 +240,7 @@ int Boolean::compareTo(Comparable *other) // ConstString // ----------------- -bool ConstString::equals(Object *other) +bool ConstString::equals(const Object *other) const { ConstString *otherString = (ConstString*)other; return @@ -317,7 +318,7 @@ PairBase::~PairBase() delete second; } -bool PairBase::equals(Object *other) +bool PairBase::equals(const Object *other) const { PairBase *otherPair = (PairBase*)other; @@ -347,7 +348,7 @@ int PairBase::hashValue() return value; } -void PairBase::intoStringBuffer(misc::StringBuffer *sb) +void PairBase::intoStringBuffer(misc::StringBuffer *sb) const { sb->append(" #include +#include + #include "misc.hh" namespace lout { @@ -25,7 +27,8 @@ class Object { public: virtual ~Object(); - virtual bool equals(Object *other); + virtual bool equals(const Object *other) const; + virtual bool equals(Object *other) final { return equals( &std::as_const( *other ) ); } virtual int hashValue(); virtual Object *clone(); virtual void intoStringBuffer(misc::StringBuffer *sb) const; @@ -103,7 +106,7 @@ private: public: Pointer(void *value) { this->value = value; } - bool equals(Object *other); + bool equals(const Object *other) const override; int hashValue(); void intoStringBuffer(misc::StringBuffer *sb); inline void *getValue() { return value; } @@ -129,7 +132,7 @@ class Integer: public Comparable public: Integer(int value) { this->value = value; } - bool equals(Object *other); + bool equals(const Object *other) const override; int hashValue(); void intoStringBuffer(misc::StringBuffer *sb); int compareTo(Comparable *other); @@ -146,7 +149,7 @@ class Boolean: public Comparable public: Boolean(bool value) { this->value = value; } - bool equals(Object *other); + bool equals(const Object *other) const override; int hashValue(); void intoStringBuffer(misc::StringBuffer *sb); int compareTo(Comparable *other); @@ -166,7 +169,7 @@ protected: public: ConstString(const char *str) { this->str = str; } - bool equals(Object *other); + bool equals(const Object *other) const override; int hashValue(); int compareTo(Comparable *other); void intoStringBuffer(misc::StringBuffer *sb); @@ -201,9 +204,9 @@ public: PairBase(Object *first, Object *second); ~PairBase(); - bool equals(Object *other); + bool equals(const Object *other) const override; int hashValue(); - void intoStringBuffer(misc::StringBuffer *sb); + void intoStringBuffer(misc::StringBuffer *sb) const override; size_t sizeOf(); };