From 8d5eed3c82fa6f59c4f9d860f1d5c6bbdd837d80 Mon Sep 17 00:00:00 2001 From: Robert Schumacher Date: Wed, 8 Jul 2020 18:07:50 -0700 Subject: Convert Fl_Group::array_ to union to better represent its behavior Amended by Albrecht: - rename union member variables as discussed - add comments to new array_ union members Fixes #96 Signed-off-by: Albrecht Schlosser --- FL/Fl_Group.H | 5 ++++- src/Fl_Group.cxx | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/FL/Fl_Group.H b/FL/Fl_Group.H index 26baf9726..61ae51093 100644 --- a/FL/Fl_Group.H +++ b/FL/Fl_Group.H @@ -41,7 +41,10 @@ class Fl_Rect; */ class FL_EXPORT Fl_Group : public Fl_Widget { - Fl_Widget** array_; + union { + Fl_Widget** array_; // used if group has two or more children or NULL + Fl_Widget* child1_; // used if group has one child or NULL + }; Fl_Widget* savedfocus_; Fl_Widget* resizable_; int children_; diff --git a/src/Fl_Group.cxx b/src/Fl_Group.cxx index 4a614e1e4..769d28600 100644 --- a/src/Fl_Group.cxx +++ b/src/Fl_Group.cxx @@ -38,7 +38,7 @@ Fl_Group* Fl_Group::current_; is added or removed. */ Fl_Widget*const* Fl_Group::array() const { - return children_ <= 1 ? (Fl_Widget**)(&array_) : array_; + return children_ <= 1 ? &child1_ : array_; } /** @@ -473,9 +473,9 @@ void Fl_Group::insert(Fl_Widget &o, int index) { } o.parent_ = this; if (children_ == 0) { // use array pointer to point at single child - array_ = (Fl_Widget**)&o; + child1_ = &o; } else if (children_ == 1) { // go from 1 to 2 children - Fl_Widget* t = (Fl_Widget*)array_; + Fl_Widget* t = child1_; array_ = (Fl_Widget**)malloc(2*sizeof(Fl_Widget*)); if (index) {array_[0] = t; array_[1] = &o;} else {array_[0] = &o; array_[1] = t;} @@ -520,7 +520,7 @@ void Fl_Group::remove(int index) { if (children_ == 1) { // go from 2 to 1 child Fl_Widget *t = array_[!index]; free((void*)array_); - array_ = (Fl_Widget**)t; + child1_ = t; } else if (children_ > 1) { // delete from array for (; index < children_; index++) array_[index] = array_[index+1]; } -- cgit v1.2.3