Changeset - r28100:80c8ce218b56
[Not reviewed]
master
0 1 0
Peter Nelson - 13 months ago 2023-11-01 14:16:38
peter1138@openttd.org
Codechange: Don't be lenient with invalid NWidgetPart lists.

Some NWidgetPart combinations were ignored but it was not clear that they do nothing. Instead, assert if NWidgetPart is incorrectly applied.
1 file changed with 28 insertions and 28 deletions:
0 comments (0 inline, 0 general)
src/widget.cpp
Show inline comments
 
@@ -3117,64 +3117,60 @@ static const NWidgetPart *MakeNWidget(co
 

	
 
			case WPT_RESIZE: {
 
				NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
 
				if (nwrb != nullptr) {
 
					assert(nwid_begin->u.xy.x >= 0 && nwid_begin->u.xy.y >= 0);
 
					nwrb->SetResize(nwid_begin->u.xy.x, nwid_begin->u.xy.y);
 
				}
 
				if (unlikely(nwrb == nullptr)) throw std::runtime_error("WPT_RESIZE requires NWidgetResizeBase");
 
				assert(nwid_begin->u.xy.x >= 0 && nwid_begin->u.xy.y >= 0);
 
				nwrb->SetResize(nwid_begin->u.xy.x, nwid_begin->u.xy.y);
 
				break;
 
			}
 

	
 
			case WPT_MINSIZE: {
 
				NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
 
				if (nwrb != nullptr) {
 
					assert(nwid_begin->u.xy.x >= 0 && nwid_begin->u.xy.y >= 0);
 
					nwrb->SetMinimalSize(nwid_begin->u.xy.x, nwid_begin->u.xy.y);
 
				}
 
				if (unlikely(nwrb == nullptr)) throw std::runtime_error("WPT_MINSIZE requires NWidgetResizeBase");
 
				assert(nwid_begin->u.xy.x >= 0 && nwid_begin->u.xy.y >= 0);
 
				nwrb->SetMinimalSize(nwid_begin->u.xy.x, nwid_begin->u.xy.y);
 
				break;
 
			}
 

	
 
			case WPT_MINTEXTLINES: {
 
				NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
 
				if (nwrb != nullptr) {
 
					assert(nwid_begin->u.text_lines.size >= FS_BEGIN && nwid_begin->u.text_lines.size < FS_END);
 
					nwrb->SetMinimalTextLines(nwid_begin->u.text_lines.lines, nwid_begin->u.text_lines.spacing, nwid_begin->u.text_lines.size);
 
				}
 
				if (unlikely(nwrb == nullptr)) throw std::runtime_error("WPT_MINTEXTLINES requires NWidgetResizeBase");
 
				assert(nwid_begin->u.text_lines.size >= FS_BEGIN && nwid_begin->u.text_lines.size < FS_END);
 
				nwrb->SetMinimalTextLines(nwid_begin->u.text_lines.lines, nwid_begin->u.text_lines.spacing, nwid_begin->u.text_lines.size);
 
				break;
 
			}
 

	
 
			case WPT_TEXTSTYLE: {
 
				NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
 
				if (nwc != nullptr) {
 
					nwc->SetTextStyle(nwid_begin->u.text_style.colour, nwid_begin->u.text_style.size);
 
				}
 
				if (unlikely(nwc == nullptr)) throw std::runtime_error("WPT_TEXTSTYLE requires NWidgetCore");
 
				nwc->SetTextStyle(nwid_begin->u.text_style.colour, nwid_begin->u.text_style.size);
 
				break;
 
			}
 

	
 
			case WPT_ALIGNMENT: {
 
				NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
 
				if (nwc != nullptr) {
 
					nwc->SetAlignment(nwid_begin->u.align.align);
 
				}
 
				if (unlikely(nwc == nullptr)) throw std::runtime_error("WPT_ALIGNMENT requires NWidgetCore");
 
				nwc->SetAlignment(nwid_begin->u.align.align);
 
				break;
 
			}
 

	
 
			case WPT_FILL: {
 
				NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
 
				if (nwrb != nullptr) nwrb->SetFill(nwid_begin->u.xy.x, nwid_begin->u.xy.y);
 
				if (unlikely(nwrb == nullptr)) throw std::runtime_error("WPT_FILL requires NWidgetResizeBase");
 
				nwrb->SetFill(nwid_begin->u.xy.x, nwid_begin->u.xy.y);
 
				break;
 
			}
 

	
 
			case WPT_DATATIP: {
 
				NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
 
				if (nwc != nullptr) {
 
					nwc->widget_data = nwid_begin->u.data_tip.data;
 
					nwc->tool_tip = nwid_begin->u.data_tip.tooltip;
 
				}
 
				if (unlikely(nwc == nullptr)) throw std::runtime_error("WPT_DATATIP requires NWidgetCore");
 
				nwc->widget_data = nwid_begin->u.data_tip.data;
 
				nwc->tool_tip = nwid_begin->u.data_tip.tooltip;
 
				break;
 
			}
 

	
 
			case WPT_PADDING:
 
				if (*dest != nullptr) (*dest)->SetPadding(nwid_begin->u.padding);
 
				if (unlikely(*dest == nullptr)) throw std::runtime_error("WPT_PADDING requires NWidgetBase");
 
				(*dest)->SetPadding(nwid_begin->u.padding);
 
				break;
 

	
 
			case WPT_PIPSPACE: {
 
@@ -3183,14 +3179,15 @@ static const NWidgetPart *MakeNWidget(co
 

	
 
				NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
 
				if (nwb != nullptr) nwb->SetPIP(nwid_begin->u.pip.pre, nwid_begin->u.pip.inter, nwid_begin->u.pip.post);
 

	
 
				if (unlikely(nwc == nullptr && nwb == nullptr)) throw std::runtime_error("WPT_PIPSPACE requires NWidgetPIPContainer or NWidgetBackground");
 
				break;
 
			}
 

	
 
			case WPT_SCROLLBAR: {
 
				NWidgetCore *nwc = dynamic_cast<NWidgetCore *>(*dest);
 
				if (nwc != nullptr) {
 
					nwc->scrollbar_index = nwid_begin->u.widget.index;
 
				}
 
				if (unlikely(nwc == nullptr)) throw std::runtime_error("WPT_SCROLLBAR requires NWidgetCore");
 
				nwc->scrollbar_index = nwid_begin->u.widget.index;
 
				break;
 
			}
 

	
 
@@ -3307,7 +3304,10 @@ NWidgetContainer *MakeNWidgets(const NWi
 
	*biggest_index = -1;
 
	if (container == nullptr) container = new NWidgetVertical();
 
	NWidgetBase *cont_ptr = container;
 
	MakeWidgetTree(nwid_begin, nwid_end, &cont_ptr, biggest_index);
 
	[[maybe_unused]] const NWidgetPart *nwid_part = MakeWidgetTree(nwid_begin, nwid_end, &cont_ptr, biggest_index);
 
#ifdef WITH_ASSERT
 
	if (unlikely(nwid_part != nwid_end)) throw std::runtime_error("Did not consume all NWidgetParts");
 
#endif
 
	return container;
 
}
 

	
0 comments (0 inline, 0 general)