Skip to content

Commit

Permalink
Rework the tests for empty glist check.
Browse files Browse the repository at this point in the history
Introduce a macro g_list_is_empty() and used it.

It is clearer to read:
   if(g_list_is_empty(list))
than
   if(!list)

Also change some check for singleton by g_list_is_singleton().
  • Loading branch information
TurboGit committed Apr 29, 2024
1 parent 73fb641 commit c4be9ed
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 112 deletions.
6 changes: 3 additions & 3 deletions src/common/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ static gchar *get_query_string(const dt_collection_properties_t property, const
g_free(name); // free the original filename
}

if(list) subquery = dt_util_glist_to_str(" OR ", list);
if(!g_list_is_empty(list)) subquery = dt_util_glist_to_str(" OR ", list);
g_list_free_full(list, g_free); // free the SQL clauses as well as the list
}
if(g_strv_length(elems) > 1)
Expand Down Expand Up @@ -2031,7 +2031,7 @@ static gchar *get_query_string(const dt_collection_properties_t property, const
g_free(name); // free the original filename
}

if(list)
if(!g_list_is_empty(list))
{
if(subquery)
{
Expand Down Expand Up @@ -2518,7 +2518,7 @@ void dt_collection_update_query(const dt_collection_t *collection,

if(!collection->clone)
{
if(list)
if(!g_list_is_empty(list))
{
// for changing offsets, thumbtable needs to know the first
// untouched imageid after the list we do this here
Expand Down
5 changes: 3 additions & 2 deletions src/common/colorlabels.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,11 @@ void dt_colorlabels_set_labels(const GList *img,
const gboolean clear_on,
const gboolean undo_on)
{
if(img)
if(!g_list_is_empty(img))
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_COLORLABELS);
if(undo_on)
dt_undo_start_group(darktable.undo, DT_UNDO_COLORLABELS);

_colorlabels_execute(img, labels, &undo, undo_on, clear_on ? DT_CA_SET : DT_CA_ADD);

Expand Down
23 changes: 23 additions & 0 deletions src/common/darktable.h
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ static inline void copy_pixel(float *const __restrict__ out,

// a few macros and helper functions to speed up certain frequently-used GLib operations
#define g_list_is_singleton(list) ((list) && (!(list)->next))
#define g_list_is_empty(list) (!list)

static inline gboolean g_list_shorter_than(const GList *list,
unsigned len)
Expand Down Expand Up @@ -833,6 +834,28 @@ static inline const GList *g_list_prev_wraparound(const GList *list)
return g_list_previous(list) ? g_list_previous(list) : g_list_last((GList*)list);
}

// returns true if the two GLists have the same length
static inline gboolean dt_list_length_equal(GList *l1, GList *l2)
{
while (l1 && l2)
{
l1 = g_list_next(l1);
l2 = g_list_next(l2);
}
return !l1 && !l2;
}

// returns true if the two GSLists have the same length
static inline gboolean dt_slist_length_equal(GSList *l1, GSList *l2)
{
while (l1 && l2)
{
l1 = g_slist_next(l1);
l2 = g_slist_next(l2);
}
return !l1 && !l2;
}

// checks internally for DT_DEBUG_MEMORY
void dt_print_mem_usage();

Expand Down
18 changes: 12 additions & 6 deletions src/common/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,10 @@ static void _metadata_execute(const GList *imgs, const GList *metadata, GList **
}
}

void dt_metadata_set(const dt_imgid_t imgid, const char *key, const char *value, const gboolean undo_on)
void dt_metadata_set(const dt_imgid_t imgid,
const char *key,
const char *value,
const gboolean undo_on)
{
if(!key || !imgid) return;

Expand All @@ -606,7 +609,7 @@ void dt_metadata_set(const dt_imgid_t imgid, const char *key, const char *value,
imgs = dt_act_on_get_images(TRUE, TRUE, FALSE);
else
imgs = g_list_prepend(imgs, GINT_TO_POINTER(imgid));
if(imgs)
if(!g_list_is_empty(imgs))
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_METADATA);
Expand Down Expand Up @@ -650,7 +653,7 @@ void dt_metadata_set_import(const dt_imgid_t imgid, const char *key, const char
{
GList *imgs = NULL;
imgs = g_list_prepend(imgs, GINT_TO_POINTER(imgid));
if(imgs)
if(!g_list_is_empty(imgs))
{
GList *undo = NULL;

Expand Down Expand Up @@ -705,7 +708,8 @@ void dt_metadata_set_list(const GList *imgs, GList *key_value, const gboolean un

if(undo_on)
{
dt_undo_record(darktable.undo, NULL, DT_UNDO_METADATA, undo, _pop_undo, _metadata_undo_data_free);
dt_undo_record(darktable.undo, NULL,
DT_UNDO_METADATA, undo, _pop_undo, _metadata_undo_data_free);
dt_undo_end_group(darktable.undo);
}

Expand Down Expand Up @@ -751,10 +755,12 @@ void dt_metadata_clear(const GList *imgs, const gboolean undo_on)
}
}

void dt_metadata_set_list_id(const GList *img, const GList *metadata, const gboolean clear_on,
void dt_metadata_set_list_id(const GList *img,
const GList *metadata,
const gboolean clear_on,
const gboolean undo_on)
{
if(img)
if(!g_list_is_empty(img))
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_METADATA);
Expand Down
5 changes: 2 additions & 3 deletions src/common/metadata_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ void dt_lib_export_metadata_set_conf(const char *metadata_presets)
int i = 0;
char *conf_keyword = NULL;
char *nameformula = NULL;
if(list)
if(!g_list_is_empty(list))
{
char *flags_hexa = list->data;
dt_conf_set_string(flags_keyword, flags_hexa);
list = g_list_remove(list, flags_hexa);
g_free(flags_hexa);
if(list)
if(!g_list_is_empty(list))
{
for(GList *tags = list; tags; tags = g_list_next(tags))
{
Expand Down Expand Up @@ -120,4 +120,3 @@ void dt_lib_export_metadata_set_conf(const char *metadata_presets)
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
// clang-format on

31 changes: 22 additions & 9 deletions src/common/ratings.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ static void _ratings_undo_data_free(gpointer data)
}

// wrapper that does some precalculation to deal with toggle effects and rating increase/decrease
static void _ratings_apply(const GList *imgs, const int rating, GList **undo, const gboolean undo_on)
static void _ratings_apply(const GList *imgs,
const int rating,
GList **undo,
const gboolean undo_on)
{
// REJECTION and SINGLE_STAR rating can have a toggle effect
// but we only toggle off if ALL images have that rating
Expand Down Expand Up @@ -177,9 +180,11 @@ static void _ratings_apply(const GList *imgs, const int rating, GList **undo, co
}
}

void dt_ratings_apply_on_list(const GList *img, const int rating, const gboolean undo_on)
void dt_ratings_apply_on_list(const GList *img,
const int rating,
const gboolean undo_on)
{
if(img)
if(!g_list_is_empty(img))
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_RATINGS);
Expand All @@ -195,15 +200,19 @@ void dt_ratings_apply_on_list(const GList *img, const int rating, const gboolean
}
}

void dt_ratings_apply_on_image(const dt_imgid_t imgid, const int rating, const gboolean single_star_toggle,
const gboolean undo_on, const gboolean group_on)
void dt_ratings_apply_on_image(const dt_imgid_t imgid,
const int rating,
const gboolean single_star_toggle,
const gboolean undo_on,
const gboolean group_on)
{
GList *imgs = NULL;
int new_rating = rating;

if(dt_is_valid_imgid(imgid)) imgs = g_list_prepend(imgs, GINT_TO_POINTER(imgid));
if(dt_is_valid_imgid(imgid))
imgs = g_list_prepend(imgs, GINT_TO_POINTER(imgid));

if(imgs)
if(!g_list_is_empty(imgs))
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_RATINGS);
Expand All @@ -213,7 +222,8 @@ void dt_ratings_apply_on_image(const dt_imgid_t imgid, const int rating, const g

if(undo_on)
{
dt_undo_record(darktable.undo, NULL, DT_UNDO_RATINGS, undo, _pop_undo, _ratings_undo_data_free);
dt_undo_record(darktable.undo, NULL,
DT_UNDO_RATINGS, undo, _pop_undo, _ratings_undo_data_free);
dt_undo_end_group(darktable.undo);
}
g_list_free(imgs);
Expand All @@ -229,7 +239,10 @@ enum
DT_ACTION_EFFECT_DOWNGRADE = DT_ACTION_EFFECT_DEFAULT_DOWN,
};

static float _action_process_rating(gpointer target, dt_action_element_t element, dt_action_effect_t effect, float move_size)
static float _action_process_rating(gpointer target,
dt_action_element_t element,
dt_action_effect_t effect,
float move_size)
{
float return_value = DT_ACTION_NOT_VALID;

Expand Down
6 changes: 3 additions & 3 deletions src/common/styles.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,17 +703,17 @@ void dt_multiple_styles_apply_to_list(GList *styles,
if(dt_view_get_current() == DT_VIEW_DARKROOM)
dt_dev_write_history(darktable.develop);

if(!styles && !list)
if(g_list_is_empty(styles) && g_list_is_empty(list))
{
dt_control_log(_("no images nor styles selected!"));
return;
}
else if(!styles)
else if(g_list_is_empty(styles))
{
dt_control_log(_("no styles selected!"));
return;
}
else if(!list)
else if(g_list_is_empty(list))
{
dt_control_log(_("no image selected!"));
return;
Expand Down
14 changes: 8 additions & 6 deletions src/common/tags.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,13 @@ gboolean dt_tag_attach_images(const guint tagid,
const GList *img,
const gboolean undo_on)
{
if(!img) return FALSE;
if(g_list_is_empty(img)) return FALSE;
GList *undo = NULL;
GList *tags = NULL;

tags = g_list_prepend(tags, GINT_TO_POINTER(tagid));
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_TAGS);
if(undo_on)
dt_undo_start_group(darktable.undo, DT_UNDO_TAGS);

const gboolean res = _tag_execute(tags, img, &undo, undo_on, DT_TA_ATTACH);

Expand Down Expand Up @@ -518,10 +519,11 @@ gboolean dt_tag_set_tags(const GList *tags,
const gboolean clear_on,
const gboolean undo_on)
{
if(img)
if(!g_list_is_empty(img))
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_TAGS);
if(undo_on)
dt_undo_start_group(darktable.undo, DT_UNDO_TAGS);

const gboolean res = _tag_execute
(tags, img, &undo, undo_on,
Expand Down Expand Up @@ -563,7 +565,7 @@ gboolean dt_tag_attach_string_list(const gchar *tags,
}

// attach newly created tags
if(img)
if(!g_list_is_empty(img))
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_TAGS);
Expand All @@ -587,7 +589,7 @@ gboolean dt_tag_detach_images(const guint tagid,
const GList *img,
const gboolean undo_on)
{
if(img)
if(!g_list_is_empty(img))
{
GList *tags = NULL;
tags = g_list_prepend(tags, GINT_TO_POINTER(tagid));
Expand Down
23 changes: 0 additions & 23 deletions src/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,6 @@ gboolean dt_is_scene_referred(void);
// returns true if current settings is display-referred
gboolean dt_is_display_referred(void);

// returns true if the two GLists have the same length
static inline gboolean dt_list_length_equal(GList *l1, GList *l2)
{
while (l1 && l2)
{
l1 = g_list_next(l1);
l2 = g_list_next(l2);
}
return !l1 && !l2;
}

// returns true if the two GSLists have the same length
static inline gboolean dt_slist_length_equal(GSList *l1, GSList *l2)
{
while (l1 && l2)
{
l1 = g_slist_next(l1);
l2 = g_slist_next(l2);
}
return !l1 && !l2;
}


G_END_DECLS

// clang-format off
Expand Down
3 changes: 2 additions & 1 deletion src/develop/masks/brush.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ static void _brush_catmull_to_bezier(const float x1,
static void _brush_init_ctrl_points(dt_masks_form_t *form)
{
// if we have less than 2 points, what to do ??
if(g_list_shorter_than(form->points, 2)) return;
if(g_list_shorter_than(form->points, 2))
return;

// we need extra points to deal with curve ends
dt_masks_point_brush_t start_point[2], end_point[2];
Expand Down
17 changes: 12 additions & 5 deletions src/dtgtk/range.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,24 @@ static void _range_select_destroy(GtkWidget *widget)

DT_DEBUG_CONTROL_SIGNAL_DISCONNECT(darktable.signals, G_CALLBACK(_dt_pref_changed), range);

if(range->markers) g_list_free_full(range->markers, g_free);
if(!g_list_is_empty(range->markers))
g_list_free_full(range->markers, g_free);
range->markers = NULL;
if(range->blocks) g_list_free_full(range->blocks, g_free);

if(!g_list_is_empty(range->blocks))
g_list_free_full(range->blocks, g_free);
range->blocks = NULL;
if(range->icons) g_list_free_full(range->icons, g_free);

if(!g_list_is_empty(range->icons))
g_list_free_full(range->icons, g_free);
range->icons = NULL;

if(range->surface) cairo_surface_destroy(range->surface);
if(range->surface)
cairo_surface_destroy(range->surface);
range->surface = NULL;

if(range->cur_help) g_free(range->cur_help);
if(range->cur_help)
g_free(range->cur_help);
range->cur_help = NULL;

GTK_WIDGET_CLASS(dtgtk_range_select_parent_class)->destroy(widget);
Expand Down
5 changes: 2 additions & 3 deletions src/libs/export_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ char *dt_lib_export_metadata_configuration_dialog(char *metadata_presets, const
d->taglist = (GList *)dt_exif_get_exiv2_taglist();
GList *list = dt_util_str_to_glist("\1", metadata_presets);
int32_t flags = 0;
if(list)
if(!g_list_is_empty(list))
{
char *flags_hexa = list->data;
flags = strtol(flags_hexa, NULL, 16);
list = g_list_remove(list, flags_hexa);
g_free(flags_hexa);
if(list)
if(!g_list_is_empty(list))
{
for(GList *tags = list; tags; tags = g_list_next(tags))
{
Expand Down Expand Up @@ -484,4 +484,3 @@ char *dt_lib_export_metadata_configuration_dialog(char *metadata_presets, const
// vim: shiftwidth=2 expandtab tabstop=2 cindent
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
// clang-format on

Loading

0 comments on commit c4be9ed

Please sign in to comment.