From 152b0cac3474aa1465b703eca93de5901dd1c12d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 3 Sep 2023 21:55:30 +0100 Subject: [PATCH] Codechange: Return update nwid_begin instead of count. This avoids needing to keep track of how many widget parts have been consumed, instead we only to ensure nwid_begin < nwid_end. --- src/widget.cpp | 55 +++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/widget.cpp b/src/widget.cpp index 9eabaa8a3c..d5990c847b 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -3013,31 +3013,29 @@ bool NWidgetLeaf::ButtonHit(const Point &pt) * @param dest Address of pointer to use for returning the composed widget. * @param fill_dest Fill the composed widget with child widgets. * @param biggest_index Pointer to biggest nested widget index in the tree encountered so far. - * @return Number of widget part elements used to compose the widget. + * @return Pointer to remaining nested widget parts. * @pre \c biggest_index != nullptr. */ -static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **dest, bool *fill_dest, int *biggest_index) +static const NWidgetPart *MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **dest, bool *fill_dest, int *biggest_index) { - int num_used = 0; - *dest = nullptr; *fill_dest = false; while (nwid_begin < nwid_end) { switch (nwid_begin->type) { case NWID_SPACER: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; *dest = new NWidgetSpacer(0, 0); break; case NWID_HORIZONTAL: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; *dest = new NWidgetHorizontal(nwid_begin->u.cont_flags); *fill_dest = true; break; case NWID_HORIZONTAL_LTR: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; *dest = new NWidgetHorizontalLTR(nwid_begin->u.cont_flags); *fill_dest = true; break; @@ -3045,20 +3043,20 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en case WWT_PANEL: case WWT_INSET: case WWT_FRAME: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; *dest = new NWidgetBackground(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); *fill_dest = true; break; case NWID_VERTICAL: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; *dest = new NWidgetVertical(nwid_begin->u.cont_flags); *fill_dest = true; break; case NWID_MATRIX: { - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; NWidgetMatrix *nwm = new NWidgetMatrix(); *dest = nwm; *fill_dest = true; @@ -3069,7 +3067,7 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en } case WPT_FUNCTION: { - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; /* Ensure proper functioning even when the called code simply writes its largest index. */ int biggest = -1; *dest = nwid_begin->u.func_ptr(&biggest); @@ -3158,23 +3156,23 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en } case WPT_ENDCONTAINER: - return num_used; + return nwid_begin; case NWID_VIEWPORT: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; *dest = new NWidgetViewport(nwid_begin->u.widget.index); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); break; case NWID_HSCROLLBAR: case NWID_VSCROLLBAR: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; *dest = new NWidgetScrollbar(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); break; case NWID_SELECTION: { - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; NWidgetStacked *nws = new NWidgetStacked(); *dest = nws; *fill_dest = true; @@ -3184,17 +3182,16 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en } default: - if (*dest != nullptr) return num_used; + if (*dest != nullptr) return nwid_begin; assert((nwid_begin->type & WWT_MASK) < WWT_LAST || (nwid_begin->type & WWT_MASK) == NWID_BUTTON_DROPDOWN); *dest = new NWidgetLeaf(nwid_begin->type, nwid_begin->u.widget.colour, nwid_begin->u.widget.index, 0x0, STR_NULL); *biggest_index = std::max(*biggest_index, (int)nwid_begin->u.widget.index); break; } - num_used++; nwid_begin++; } - return num_used; + return nwid_begin; } /** @@ -3203,10 +3200,10 @@ static int MakeNWidget(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_en * @param nwid_end Pointer to ending of nested widget parts. * @param parent Pointer or container to use for storing the child widgets (*parent == nullptr or *parent == container or background widget). * @param biggest_index Pointer to biggest nested widget index in the tree. - * @return Number of widget part elements used to fill the container. + * @return Pointer to remaining nested widget parts. * @post \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used. */ -static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **parent, int *biggest_index) +static const NWidgetPart *MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid_end, NWidgetBase **parent, int *biggest_index) { /* If *parent == nullptr, only the first widget is read and returned. Otherwise, *parent must point to either * a #NWidgetContainer or a #NWidgetBackground object, and parts are added as much as possible. */ @@ -3214,13 +3211,10 @@ static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid NWidgetBackground *nwid_parent = dynamic_cast(*parent); assert(*parent == nullptr || (nwid_cont != nullptr && nwid_parent == nullptr) || (nwid_cont == nullptr && nwid_parent != nullptr)); - int total_used = 0; for (;;) { NWidgetBase *sub_widget = nullptr; bool fill_sub = false; - int num_used = MakeNWidget(nwid_begin, nwid_end, &sub_widget, &fill_sub, biggest_index); - nwid_begin += num_used; - total_used += num_used; + nwid_begin = MakeNWidget(nwid_begin, nwid_end, &sub_widget, &fill_sub, biggest_index); /* Break out of loop when end reached */ if (sub_widget == nullptr) break; @@ -3230,9 +3224,7 @@ static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL || tp == NWID_MATRIX || tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION)) { NWidgetBase *sub_ptr = sub_widget; - num_used = MakeWidgetTree(nwid_begin, nwid_end, &sub_ptr, biggest_index); - nwid_begin += num_used; - total_used += num_used; + nwid_begin = MakeWidgetTree(nwid_begin, nwid_end, &sub_ptr, biggest_index); } /* Add sub_widget to parent container if available, otherwise return the widget to the caller. */ @@ -3240,15 +3232,15 @@ static int MakeWidgetTree(const NWidgetPart *nwid_begin, const NWidgetPart *nwid if (nwid_parent != nullptr) nwid_parent->Add(sub_widget); if (nwid_cont == nullptr && nwid_parent == nullptr) { *parent = sub_widget; - return total_used; + return nwid_begin; } } - if (nwid_begin == nwid_end) return total_used; // Reached the end of the array of parts? + if (nwid_begin == nwid_end) return nwid_begin; // Reached the end of the array of parts? assert(nwid_begin < nwid_end); assert(nwid_begin->type == WPT_ENDCONTAINER); - return total_used + 1; // *nwid_begin is also 'used' + return nwid_begin + 1; // *nwid_begin is also 'used' } /** @@ -3290,9 +3282,8 @@ NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *nwid_begin, const NWi /* Read the first widget recursively from the array. */ NWidgetBase *nwid = nullptr; - int num_used = MakeWidgetTree(nwid_begin, nwid_end, &nwid, biggest_index); + nwid_begin = MakeWidgetTree(nwid_begin, nwid_end, &nwid, biggest_index); assert(nwid != nullptr); - nwid_begin += num_used; NWidgetContainer *root = new NWidgetVertical; root->Add(nwid);