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.
This commit is contained in:
2025-08-22 13:58:05 -04:00
parent f6841f59e0
commit e4f1102e32
8 changed files with 38 additions and 32 deletions

View File

@ -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 return
this == otherIt || 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 = "); sb->append ("{ widget = ");
//widget->intoStringBuffer (sb); //widget->intoStringBuffer (sb);
@ -68,7 +70,7 @@ void Iterator::intoStringBuffer(misc::StringBuffer *sb)
Content::maskIntoStringBuffer (mask, sb); Content::maskIntoStringBuffer (mask, sb);
sb->append (", content = "); sb->append (", content = ");
Content::intoStringBuffer (&content, sb); Content::intoStringBuffer (const_cast< Content * >( &content ), sb);
sb->append (" }"); sb->append (" }");
} }

View File

@ -30,8 +30,8 @@ private:
Content::Type mask; Content::Type mask;
public: public:
bool equals (Object *other); bool equals (const Object *other) const override;
void intoStringBuffer(lout::misc::StringBuffer *sb); void intoStringBuffer(lout::misc::StringBuffer *sb) const override;
inline Widget *getWidget () { return widget; } inline Widget *getWidget () { return widget; }
inline Content *getContent () { return &content; } inline Content *getContent () { return &content; }

View File

@ -155,7 +155,7 @@ bool StyleAttrs::sizeDiffs (StyleAttrs *otherStyle)
return false; return false;
} }
bool StyleAttrs::equals (object::Object *other) { bool StyleAttrs::equals (const object::Object *other) const {
StyleAttrs *otherAttrs = (StyleAttrs *) other; StyleAttrs *otherAttrs = (StyleAttrs *) other;
return this == otherAttrs || 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; FontAttrs *otherAttrs = (FontAttrs*)other;
return 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; ColorAttrs *oc = (ColorAttrs*)other;
return this == oc || (color == oc->color); return this == oc || (color == oc->color);

View File

@ -508,7 +508,7 @@ public:
int top, right, bottom, left; int top, right, bottom, left;
inline void setVal(int val) { top = right = bottom = left = val; } 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 && return top == other->top &&
right == other->right && right == other->right &&
bottom == other->bottom && bottom == other->bottom &&
@ -603,7 +603,7 @@ public:
inline bool hasBackground () inline bool hasBackground ()
{ return backgroundColor != NULL || backgroundImage != NULL; } { return backgroundColor != NULL || backgroundImage != NULL; }
bool equals (lout::object::Object *other); bool equals (const lout::object::Object *other) const override;
int hashValue (); int hashValue ();
}; };
@ -688,7 +688,7 @@ public:
FontVariant fontVariant; FontVariant fontVariant;
FontStyle style; FontStyle style;
bool equals(lout::object::Object *other); bool equals(const lout::object::Object *other) const override;
int hashValue(); int hashValue();
}; };
@ -742,7 +742,7 @@ public:
inline int getColor () { return color; } inline int getColor () { return color; }
bool equals(lout::object::Object *other); bool equals(const lout::object::Object *other) const override;
int hashValue(); int hashValue();
}; };

View File

@ -313,7 +313,7 @@ int List::size ()
return numElements; return numElements;
} }
bool List::equals(Object *other) bool List::equals(const Object *other) const
{ {
List *otherList = (List*)other; List *otherList = (List*)other;
Node *node1 = first, *node2 = otherList->first; Node *node1 = first, *node2 = otherList->first;

View File

@ -242,7 +242,7 @@ public:
List(bool ownerOfObjects); List(bool ownerOfObjects);
~List(); ~List();
bool equals(Object *other); bool equals(const Object *other) const override;
int hashValue(); int hashValue();
int size (); int size ();
@ -451,8 +451,8 @@ public:
Collection () { this->base = NULL; } Collection () { this->base = NULL; }
~Collection () { if (this->base) delete this->base; } ~Collection () { if (this->base) delete this->base; }
bool equals(Object *other) bool equals(const Object *other) const override
{ return this->base->equals (((Collection<T>*)other)->base); } { return this->base->equals (((const Collection<T>*)other)->base); }
int hashValue() { return this->base->hashValue (); } int hashValue() { return this->base->hashValue (); }

View File

@ -48,7 +48,8 @@ Object::~Object()
* this makes casting of "other" safe. Typically, an implementation should * this makes casting of "other" safe. Typically, an implementation should
* check this == other first, the caller can assume a fast implementation. * 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 (); misc::assertNotReached ();
return false; return false;
@ -149,7 +150,7 @@ StandardComparator standardComparator;
// Pointer // Pointer
// ------------- // -------------
bool Pointer::equals(Object *other) bool Pointer::equals(const Object *other) const
{ {
return value == ((Pointer*)other)->value; return value == ((Pointer*)other)->value;
} }
@ -187,7 +188,7 @@ void Pointer::intoStringBuffer(misc::StringBuffer *sb)
// Integer // Integer
// ------------- // -------------
bool Integer::equals(Object *other) bool Integer::equals(const Object *other) const
{ {
return value == ((Integer*)other)->value; return value == ((Integer*)other)->value;
} }
@ -213,7 +214,7 @@ int Integer::compareTo(Comparable *other)
// Boolean // Boolean
// ------------- // -------------
bool Boolean::equals(Object *other) bool Boolean::equals(const Object *other) const
{ {
bool value2 = ((Boolean*)other)->value; bool value2 = ((Boolean*)other)->value;
// TODO Does "==" work? // TODO Does "==" work?
@ -239,7 +240,7 @@ int Boolean::compareTo(Comparable *other)
// ConstString // ConstString
// ----------------- // -----------------
bool ConstString::equals(Object *other) bool ConstString::equals(const Object *other) const
{ {
ConstString *otherString = (ConstString*)other; ConstString *otherString = (ConstString*)other;
return return
@ -317,7 +318,7 @@ PairBase::~PairBase()
delete second; delete second;
} }
bool PairBase::equals(Object *other) bool PairBase::equals(const Object *other) const
{ {
PairBase *otherPair = (PairBase*)other; PairBase *otherPair = (PairBase*)other;
@ -347,7 +348,7 @@ int PairBase::hashValue()
return value; return value;
} }
void PairBase::intoStringBuffer(misc::StringBuffer *sb) void PairBase::intoStringBuffer(misc::StringBuffer *sb) const
{ {
sb->append("<pair: "); sb->append("<pair: ");

View File

@ -4,6 +4,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <utility>
#include "misc.hh" #include "misc.hh"
namespace lout { namespace lout {
@ -25,7 +27,8 @@ class Object
{ {
public: public:
virtual ~Object(); 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 int hashValue();
virtual Object *clone(); virtual Object *clone();
virtual void intoStringBuffer(misc::StringBuffer *sb) const; virtual void intoStringBuffer(misc::StringBuffer *sb) const;
@ -103,7 +106,7 @@ private:
public: public:
Pointer(void *value) { this->value = value; } Pointer(void *value) { this->value = value; }
bool equals(Object *other); bool equals(const Object *other) const override;
int hashValue(); int hashValue();
void intoStringBuffer(misc::StringBuffer *sb); void intoStringBuffer(misc::StringBuffer *sb);
inline void *getValue() { return value; } inline void *getValue() { return value; }
@ -129,7 +132,7 @@ class Integer: public Comparable
public: public:
Integer(int value) { this->value = value; } Integer(int value) { this->value = value; }
bool equals(Object *other); bool equals(const Object *other) const override;
int hashValue(); int hashValue();
void intoStringBuffer(misc::StringBuffer *sb); void intoStringBuffer(misc::StringBuffer *sb);
int compareTo(Comparable *other); int compareTo(Comparable *other);
@ -146,7 +149,7 @@ class Boolean: public Comparable
public: public:
Boolean(bool value) { this->value = value; } Boolean(bool value) { this->value = value; }
bool equals(Object *other); bool equals(const Object *other) const override;
int hashValue(); int hashValue();
void intoStringBuffer(misc::StringBuffer *sb); void intoStringBuffer(misc::StringBuffer *sb);
int compareTo(Comparable *other); int compareTo(Comparable *other);
@ -166,7 +169,7 @@ protected:
public: public:
ConstString(const char *str) { this->str = str; } ConstString(const char *str) { this->str = str; }
bool equals(Object *other); bool equals(const Object *other) const override;
int hashValue(); int hashValue();
int compareTo(Comparable *other); int compareTo(Comparable *other);
void intoStringBuffer(misc::StringBuffer *sb); void intoStringBuffer(misc::StringBuffer *sb);
@ -201,9 +204,9 @@ public:
PairBase(Object *first, Object *second); PairBase(Object *first, Object *second);
~PairBase(); ~PairBase();
bool equals(Object *other); bool equals(const Object *other) const override;
int hashValue(); int hashValue();
void intoStringBuffer(misc::StringBuffer *sb); void intoStringBuffer(misc::StringBuffer *sb) const override;
size_t sizeOf(); size_t sizeOf();
}; };