From b934b115edb64983800d7e3c81945af371f04519b875c203310e0e8d3d224f7c Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Thu, 21 Aug 2025 17:17:43 -0400 Subject: [PATCH] Some const correctness in the Java pseudo core. --- dw/ooffloatsmgr.cc | 12 +++++++++++- dw/ooffloatsmgr.hh | 4 +++- lout/container.cc | 5 +++-- lout/container.hh | 2 +- lout/identity.cc | 3 ++- lout/identity.hh | 4 ++-- lout/misc.hh | 2 +- lout/object.cc | 13 +++++-------- lout/object.hh | 4 ++-- lout/signal.cc | 5 +++-- lout/signal.hh | 4 ++-- 11 files changed, 35 insertions(+), 23 deletions(-) diff --git a/dw/ooffloatsmgr.cc b/dw/ooffloatsmgr.cc index 6a92ca2..a94b056 100644 --- a/dw/ooffloatsmgr.cc +++ b/dw/ooffloatsmgr.cc @@ -67,7 +67,8 @@ OOFFloatsMgr::Float::Float (OOFFloatsMgr *oofm, Widget *widget, } } -void OOFFloatsMgr::Float::intoStringBuffer(StringBuffer *sb) +void +OOFFloatsMgr::Float::intoStringBuffer(StringBuffer *sb) const { sb->append ("{ widget = "); sb->appendPointer (getWidget ()); @@ -1380,6 +1381,15 @@ Widget *OOFFloatsMgr::getWidget (int i) return rightFloats->get(i - leftFloats->size())->getWidget (); } +const Widget * +OOFFloatsMgr::getWidget (int i) const +{ + if (i < leftFloats->size()) + return leftFloats->get(i)->getWidget (); + else + return rightFloats->get(i - leftFloats->size())->getWidget (); +} + } // namespace oof } // namespace dw diff --git a/dw/ooffloatsmgr.hh b/dw/ooffloatsmgr.hh index 4748bad..57b19e9 100644 --- a/dw/ooffloatsmgr.hh +++ b/dw/ooffloatsmgr.hh @@ -39,6 +39,7 @@ private: WidgetInfo (OOFFloatsMgr *oofm, core::Widget *widget); inline core::Widget *getWidget () { return widget; } + inline const core::Widget *getWidget () const { return widget; } }; class Float: public WidgetInfo @@ -78,7 +79,7 @@ private: Float (OOFFloatsMgr *oofm, core::Widget *widget, OOFAwareWidget *generatingBlock, int externalIndex); - void intoStringBuffer(lout::misc::StringBuffer *sb); + void intoStringBuffer(lout::misc::StringBuffer *sb) const override; bool covers (int y, int h); }; @@ -281,6 +282,7 @@ public: int getNumWidgets (); core::Widget *getWidget (int i); + const core::Widget *getWidget (int i) const; }; } // namespace oof diff --git a/lout/container.cc b/lout/container.cc index 4433381..25aaf0b 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -85,11 +85,12 @@ Iterator::~Iterator() // Collection // ---------------- -void Collection::intoStringBuffer(misc::StringBuffer *sb) +void +Collection::intoStringBuffer( misc::StringBuffer *sb ) const { sb->append("{ "); bool first = true; - for (Iterator it = iterator(); it.hasNext(); ) { + for (Iterator it = const_cast< Collection * >( this )->iterator(); it.hasNext(); ) { if (!first) sb->append(", "); it.getNext()->intoStringBuffer(sb); diff --git a/lout/container.hh b/lout/container.hh index 83940e6..c214e1b 100644 --- a/lout/container.hh +++ b/lout/container.hh @@ -135,7 +135,7 @@ public: class Collection: public Collection0 { public: - void intoStringBuffer(misc::StringBuffer *sb); + void intoStringBuffer(misc::StringBuffer *sb) const override; inline Iterator iterator() { Iterator it(createIterator()); return it; } virtual int size() = 0; }; diff --git a/lout/identity.cc b/lout/identity.cc index a69e517..7ca5b95 100644 --- a/lout/identity.cc +++ b/lout/identity.cc @@ -26,7 +26,8 @@ namespace identity { std::map< std::type_index, std::string > IdentifiableObject::classNames; -void IdentifiableObject::intoStringBuffer(misc::StringBuffer *sb) +void +IdentifiableObject::intoStringBuffer(misc::StringBuffer *sb) const { sb->append("appendPointer(this); diff --git a/lout/identity.hh b/lout/identity.hh index b7a1bf0..a5eaffb 100644 --- a/lout/identity.hh +++ b/lout/identity.hh @@ -37,13 +37,13 @@ protected: void registerName (const char *className, std::type_index index); public: - void intoStringBuffer(misc::StringBuffer *sb) override; + void intoStringBuffer(misc::StringBuffer *sb) const override; /** * \brief Return the name, under which the class of this object was * registered. */ - const char *getClassName() { return classNames.at( typeid( *this ) ).c_str(); } + const char *getClassName() const { return classNames.at( typeid( *this ) ).c_str(); } }; } // namespace identity diff --git a/lout/misc.hh b/lout/misc.hh index ebfd95b..c9e30cc 100644 --- a/lout/misc.hh +++ b/lout/misc.hh @@ -581,7 +581,7 @@ public: inline void append(const char *str) { appendNoCopy(dStrdup(str)); } inline void appendInt(int n) { char buf[32]; sprintf (buf, "%d", n); append (buf); } - inline void appendPointer(void *p) + inline void appendPointer(const void *p) { char buf[32]; sprintf (buf, "%p", p); append (buf); } inline void appendBool(bool b) { append (b ? "true" : "false"); } void appendNoCopy(char *str); diff --git a/lout/object.cc b/lout/object.cc index 4b2d3fd..56671e2 100644 --- a/lout/object.cc +++ b/lout/object.cc @@ -75,17 +75,13 @@ Object *Object::clone() /** * \brief Use object::Object::intoStringBuffer to return a textual * representation of the object. - * - * The caller does not have to free the memory, object::Object is responsible - * for this. */ -const char *Object::toString() +std::string +Object::toString() const { - /** \todo garbage! */ misc::StringBuffer sb; intoStringBuffer(&sb); - char *s = dStrdup(sb.getChars()); - return s; + return sb.getChars(); } /** @@ -93,7 +89,8 @@ const char *Object::toString() * * This is used by object::Object::toString. */ -void Object::intoStringBuffer(misc::StringBuffer *sb) +void +Object::intoStringBuffer(misc::StringBuffer *sb) const { sb->append("appendPointer(this); diff --git a/lout/object.hh b/lout/object.hh index da2a5b4..e1a56d1 100644 --- a/lout/object.hh +++ b/lout/object.hh @@ -28,8 +28,8 @@ public: virtual bool equals(Object *other); virtual int hashValue(); virtual Object *clone(); - virtual void intoStringBuffer(misc::StringBuffer *sb); - const char *toString(); + virtual void intoStringBuffer(misc::StringBuffer *sb) const; + std::string toString() const; virtual size_t sizeOf(); }; diff --git a/lout/signal.cc b/lout/signal.cc index c9092a9..2ccee83 100644 --- a/lout/signal.cc +++ b/lout/signal.cc @@ -41,7 +41,8 @@ Emitter::~Emitter () } } -void Emitter::intoStringBuffer(misc::StringBuffer *sb) +void +Emitter::intoStringBuffer(misc::StringBuffer *sb) const { sb->append ("append (""); diff --git a/lout/signal.hh b/lout/signal.hh index 014cada..7de52fe 100644 --- a/lout/signal.hh +++ b/lout/signal.hh @@ -241,7 +241,7 @@ public: Emitter(); ~Emitter(); - void intoStringBuffer(misc::StringBuffer *sb); + void intoStringBuffer(misc::StringBuffer *sb) const override; }; /** @@ -266,7 +266,7 @@ public: Receiver(); ~Receiver(); - void intoStringBuffer(misc::StringBuffer *sb); + void intoStringBuffer(misc::StringBuffer *sb) const override; }; /**