From 6d05779dd20d27c7fae6297a0d57a51907da897a5d491331c33a120dec705d33 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Sun, 2 Mar 2025 17:28:31 -0500 Subject: [PATCH] Removed some manual management. Apparently, the layout engine is written in something resembling either Java or Google's G+. It's "C++", but there's no exceptions, manual memory management, etc. But it's also attempting to recreate a subset of the Java Collections framework in C++. It's a common pattern in both beginner and older C++. I'll completely replace all of this with standard C++ library types. --- dw/layout.cc | 32 ++++++++++++-------------------- dw/layout.hh | 9 +++++++-- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/dw/layout.cc b/dw/layout.cc index 7fe101e..82b0feb 100644 --- a/dw/layout.cc +++ b/dw/layout.cc @@ -287,9 +287,6 @@ Layout::Layout (Platform *platform, bool limit) scrollIdleId = -1; scrollIdleNotInterrupted = false; - anchorsTable = - new container::typed::HashTable (true, true); - resizeIdleId = -1; textZone = new misc::ZoneAllocator (16 * 1024); @@ -339,7 +336,6 @@ Layout::~Layout () delete queueResizeList; delete platform; delete view; - delete anchorsTable; delete textZone; if (requestedAnchor) @@ -755,26 +751,24 @@ char *Layout::addAnchor (Widget *widget, const char* name) char *Layout::addAnchor (Widget *widget, const char* name, int y) { - String key (name); - if (anchorsTable->contains (&key)) + if (anchorsTable.contains (name)) return NULL; else { - Anchor *anchor = new Anchor (); - anchor->name = dStrdup (name); + auto anchor = std::make_unique< Anchor >(); + char *const rv= anchor->name = dStrdup (name); anchor->widget = widget; anchor->y = y; - anchorsTable->put (new String (name), anchor); + anchorsTable[ name ]= std::move( anchor ); updateAnchor (); - return anchor->name; + return rv; } } void Layout::changeAnchor (Widget *widget, char* name, int y) { - String key (name); - Anchor *anchor = anchorsTable->get (&key); + Anchor *const anchor = anchorsTable.at( name ).get(); assert (anchor); assert (anchor->widget == widget); anchor->y = y; @@ -783,18 +777,16 @@ void Layout::changeAnchor (Widget *widget, char* name, int y) void Layout::removeAnchor (Widget *widget, char* name) { - String key (name); - anchorsTable->remove (&key); + anchorsTable.erase( name ); } void Layout::updateAnchor () { - Anchor *anchor; - if (requestedAnchor) { - String key (requestedAnchor); - anchor = anchorsTable->get (&key); - } else - anchor = NULL; + Anchor *anchor= nullptr; + if (requestedAnchor and anchorsTable.contains( requestedAnchor ) ) + { + anchor= anchorsTable[ requestedAnchor ].get(); + } if (anchor == NULL) { /** \todo Copy comment from old docs. */ diff --git a/dw/layout.hh b/dw/layout.hh index f6ae736..8389b3a 100644 --- a/dw/layout.hh +++ b/dw/layout.hh @@ -5,6 +5,10 @@ # error Do not include this file directly, use "core.hh" instead. #endif +#include +#include +#include + namespace dw { namespace core { @@ -181,8 +185,9 @@ private: bool scrollIdleNotInterrupted; /* Anchors of the widget tree */ - lout::container::typed::HashTable - *anchorsTable; + //lout::container::typed::HashTable + //*anchorsTable; + std::unordered_map< std::string, std::unique_ptr< Anchor > > anchorsTable; SelectionState selectionState; FindtextState findtextState;