From a691df2a5f60ec65cf8a7cec4d3b850a7003606b Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 27 Jun 2019 19:32:56 +0000 Subject: [PATCH] Drop an indirection This struct is not really useful for just a single hash table, and it gets in the way of moving the guide code to its own file. --- gtk/gtkconstraintlayout.c | 76 ++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 46 deletions(-) diff --git a/gtk/gtkconstraintlayout.c b/gtk/gtkconstraintlayout.c index f928cb8c16..b346cf2f35 100644 --- a/gtk/gtkconstraintlayout.c +++ b/gtk/gtkconstraintlayout.c @@ -72,21 +72,16 @@ #include "gtksizerequest.h" #include "gtkwidgetprivate.h" -typedef struct +struct _GtkConstraintLayoutChild { + GtkLayoutChild parent_instance; + /* HashTable; a hash table of variables, * one for each attribute; we use these to query and suggest the * values for the solver. The string is static and does not need * to be freed. */ GHashTable *bound_attributes; -} ConstraintSolverChildData; - -struct _GtkConstraintLayoutChild -{ - GtkLayoutChild parent_instance; - - ConstraintSolverChildData data; }; typedef enum { @@ -107,7 +102,12 @@ struct _GtkConstraintGuide GtkConstraintLayout *layout; - ConstraintSolverChildData data; + /* HashTable; a hash table of variables, + * one for each attribute; we use these to query and suggest the + * values for the solver. The string is static and does not need + * to be freed. + */ + GHashTable *bound_attributes; GtkConstraintRef *constraints[LAST_GUIDE_VALUE]; }; @@ -185,21 +185,21 @@ get_attribute_name (GtkConstraintAttribute attr) } static GtkConstraintVariable * -get_attribute (ConstraintSolverChildData *self, - GtkConstraintSolver *solver, - const char *prefix, - GtkConstraintAttribute attr) +get_attribute (GHashTable *bound_attributes, + GtkConstraintSolver *solver, + const char *prefix, + GtkConstraintAttribute attr) { const char *attr_name; GtkConstraintVariable *res; attr_name = get_attribute_name (attr); - res = g_hash_table_lookup (self->bound_attributes, attr_name); + res = g_hash_table_lookup (bound_attributes, attr_name); if (res != NULL) return res; res = gtk_constraint_solver_create_variable (solver, prefix, attr_name, 0.0); - g_hash_table_insert (self->bound_attributes, (gpointer) attr_name, res); + g_hash_table_insert (bound_attributes, (gpointer) attr_name, res); /* Some attributes are really constraints computed from other * attributes, to avoid creating additional constraints from @@ -214,8 +214,8 @@ get_attribute (ConstraintSolverChildData *self, GtkConstraintVariable *left, *width; GtkConstraintExpression *expr; - left = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_LEFT); - width = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_WIDTH); + left = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_LEFT); + width = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_WIDTH); gtk_constraint_expression_builder_init (&builder, solver); gtk_constraint_expression_builder_term (&builder, left); @@ -236,8 +236,8 @@ get_attribute (ConstraintSolverChildData *self, GtkConstraintVariable *top, *height; GtkConstraintExpression *expr; - top = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_TOP); - height = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_HEIGHT); + top = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_TOP); + height = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_HEIGHT); gtk_constraint_expression_builder_init (&builder, solver); gtk_constraint_expression_builder_term (&builder, top); @@ -258,8 +258,8 @@ get_attribute (ConstraintSolverChildData *self, GtkConstraintVariable *left, *width; GtkConstraintExpression *expr; - left = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_LEFT); - width = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_WIDTH); + left = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_LEFT); + width = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_WIDTH); gtk_constraint_expression_builder_init (&builder, solver); gtk_constraint_expression_builder_term (&builder, width); @@ -282,8 +282,8 @@ get_attribute (ConstraintSolverChildData *self, GtkConstraintVariable *top, *height; GtkConstraintExpression *expr; - top = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_TOP); - height = get_attribute (self, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_HEIGHT); + top = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_TOP); + height = get_attribute (bound_attributes, solver, prefix, GTK_CONSTRAINT_ATTRIBUTE_HEIGHT); gtk_constraint_expression_builder_init (&builder, solver); gtk_constraint_expression_builder_term (&builder, height); @@ -373,7 +373,7 @@ get_child_attribute (GtkConstraintLayoutChild *self, attr = resolve_direction (attr, widget); - return get_attribute (&self->data, solver, prefix, attr); + return get_attribute (self->bound_attributes, solver, prefix, attr); } static GtkConstraintVariable * @@ -387,26 +387,15 @@ get_guide_attribute (GtkConstraintLayout *layout, attr = resolve_direction (attr, widget); - return get_attribute (&guide->data, solver, "guide", attr); -} - -static void -clear_constraint_solver_data (GtkConstraintSolver *solver, - ConstraintSolverChildData *data) -{ - g_clear_pointer (&data->bound_attributes, g_hash_table_unref); + return get_attribute (guide->bound_attributes, solver, "guide", attr); } static void gtk_constraint_layout_child_finalize (GObject *gobject) { GtkConstraintLayoutChild *self = GTK_CONSTRAINT_LAYOUT_CHILD (gobject); - GtkLayoutManager *manager; - GtkConstraintSolver *solver; - manager = gtk_layout_child_get_layout_manager (GTK_LAYOUT_CHILD (self)); - solver = gtk_constraint_layout_get_solver (GTK_CONSTRAINT_LAYOUT (manager)); - clear_constraint_solver_data (solver, &self->data); + g_clear_pointer (&self->bound_attributes, g_hash_table_unref); G_OBJECT_CLASS (gtk_constraint_layout_child_parent_class)->finalize (gobject); } @@ -422,7 +411,7 @@ gtk_constraint_layout_child_class_init (GtkConstraintLayoutChildClass *klass) static void gtk_constraint_layout_child_init (GtkConstraintLayoutChild *self) { - self->data.bound_attributes = + self->bound_attributes = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) gtk_constraint_variable_unref); @@ -1266,7 +1255,7 @@ G_DEFINE_TYPE_WITH_CODE (GtkConstraintGuide, gtk_constraint_guide, G_TYPE_OBJECT static void gtk_constraint_guide_init (GtkConstraintGuide *guide) { - guide->data.bound_attributes = + guide->bound_attributes = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) gtk_constraint_variable_unref); @@ -1342,7 +1331,7 @@ gtk_constraint_guide_detach (GtkConstraintGuide *guide) guide->constraints[i] = NULL; } - g_hash_table_remove_all (guide->data.bound_attributes); + g_hash_table_remove_all (guide->bound_attributes); } static void @@ -1408,13 +1397,8 @@ static void gtk_constraint_guide_finalize (GObject *object) { GtkConstraintGuide *self = GTK_CONSTRAINT_GUIDE (object); - GtkConstraintSolver *solver; - if (self->layout) - { - solver = gtk_constraint_layout_get_solver (self->layout); - clear_constraint_solver_data (solver, &self->data); - } + g_clear_pointer (&self->bound_attributes, g_hash_table_unref); G_OBJECT_CLASS (gtk_constraint_guide_parent_class)->finalize (object); }