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

@ -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;

View File

@ -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<T>*)other)->base); }
bool equals(const Object *other) const override
{ return this->base->equals (((const Collection<T>*)other)->base); }
int hashValue() { return this->base->hashValue (); }

View File

@ -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("<pair: ");

View File

@ -4,6 +4,8 @@
#include <stdlib.h>
#include <string.h>
#include <utility>
#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();
};