From dc3b4ce57c3995f8f4be883e2da0dfec6c200934 Mon Sep 17 00:00:00 2001 From: Colin Clark Date: Sat, 9 Mar 2024 15:57:26 +0000 Subject: [PATCH] Replace single value enums with constexpr Commit a4ce95bc clang-tidy: modernize-macro-to-enum, resulted in some enums containing a single value. These are now replaced by a constexpr statement. The script used to identify these single value enum statements is now included as a meson test. --- meson.build | 13 ++++++++++ scripts/enum-check.sh | 47 ++++++++++++++++++++++++++++++++++++ src/advanced-exif.cc | 11 ++++++--- src/bar-gps.cc | 11 ++++++--- src/cache-maint.cc | 10 +++++--- src/cache.cc | 10 +++++--- src/cellrenderericon.cc | 8 +++--- src/histogram.cc | 9 ++++--- src/image-load-psd.cc | 4 +-- src/image-overlay.cc | 11 ++++++--- src/layout-util.cc | 4 --- src/pan-view/pan-calendar.cc | 10 +++++--- src/pan-view/pan-view.cc | 15 ++++++------ src/pixbuf-renderer.cc | 20 +++++++-------- src/preferences.cc | 15 ++++++------ src/print.cc | 10 +++++--- src/remote.cc | 16 ++++++------ src/renderer-tiles.cc | 11 ++++++--- src/ui-bookmark.cc | 15 ++++++------ src/ui-pathsel.cc | 14 +++++------ src/utilops.cc | 16 +++++------- 21 files changed, 179 insertions(+), 101 deletions(-) create mode 100755 scripts/enum-check.sh diff --git a/meson.build b/meson.build index 294f9d5b..ea644b4f 100644 --- a/meson.build +++ b/meson.build @@ -724,6 +724,19 @@ else summary({'Code Correctness' : ['Test runs:', false]}, section : 'Testing', bool_yn : true) endif +# Single value enum checks +enum_check_sh = find_program('enum-check.sh', dirs : scriptsdir, required : true) +if enum_check_sh.found() + foreach source_file : main_sources + pan_view_sources + view_file_sources + source_file_name = fs.name(source_file) + test('Single Value enum_ ' + source_file_name, enum_check_sh, args : [source_file], timeout : 100) + endforeach + + summary({'Single Value enum' : ['Test runs:', true]}, section : 'Testing', bool_yn : true) +else + summary({'Single Value enum' : ['Test runs:', false]}, section : 'Testing', bool_yn : true) +endif + # Lua test option = get_option('lua') if not option.disabled() diff --git a/scripts/enum-check.sh b/scripts/enum-check.sh new file mode 100755 index 00000000..8c9a8c76 --- /dev/null +++ b/scripts/enum-check.sh @@ -0,0 +1,47 @@ +#!/bin/sh +#********************************************************************** +# Copyright (C) 2024 - The Geeqie Team +# +# Author: Colin Clark +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +#********************************************************************** + +## @file +## @brief Check for single value enums +## +## $1 Source file to check +## +## The expected format is: \n +## enum { \n +## bookmark_drop_types_n = 3 \n +## }; \n +## +## The grep operation prepends line numbers so resulting in: \n +## 123:enum { \n +## 124- bookmark_drop_types_n = 3 \n +## 125-}; +## + +res=$(grep --line-number --after-context=2 'enum\ {$' "$1" | grep --before-context=2 '^[[:digit:]]\+\-};') + +if [ -z "$res" ] +then + exit 0 +else + printf "%s\n" "$res" + exit 1 +fi + diff --git a/src/advanced-exif.cc b/src/advanced-exif.cc index e271c42a..736f7d97 100644 --- a/src/advanced-exif.cc +++ b/src/advanced-exif.cc @@ -44,13 +44,16 @@ #include "ui-misc.h" #include "window.h" +namespace +{ + +constexpr gint ADVANCED_EXIF_DATA_COLUMN_WIDTH = 200; + +} // namespace + struct ExifData; struct ExifItem; -enum { - ADVANCED_EXIF_DATA_COLUMN_WIDTH = 200 -}; - /* *------------------------------------------------------------------- * EXIF window diff --git a/src/bar-gps.cc b/src/bar-gps.cc index f5e036f3..a6929442 100644 --- a/src/bar-gps.cc +++ b/src/bar-gps.cc @@ -51,12 +51,17 @@ #include "ui-utildlg.h" #include "uri-utils.h" +namespace +{ + +constexpr gint THUMB_SIZE = 100; +constexpr int DIRECTION_SIZE = 300; + +} // namespace + #define MARKER_COLOUR 0x00, 0x00, 0xff, 0xff #define TEXT_COLOUR 0x00, 0x00, 0x00, 0xff #define THUMB_COLOUR 0xff, 0xff, 0xff, 0xff -#define THUMB_SIZE 100 - -#define DIRECTION_SIZE 300 /* *------------------------------------------------------------------- diff --git a/src/cache-maint.cc b/src/cache-maint.cc index 227fae13..b6f68c5c 100644 --- a/src/cache-maint.cc +++ b/src/cache-maint.cc @@ -49,6 +49,12 @@ #include "ui-utildlg.h" #include "window.h" +namespace +{ + +constexpr gint PURGE_DIALOG_WIDTH = 400; + +} // namespace struct CMData { @@ -65,10 +71,6 @@ struct CMData gboolean remote; }; -enum { - PURGE_DIALOG_WIDTH = 400 -}; - /* *----------------------------------------------------------------------------- * Command line cache maintenance program functions *----------------------------------------------------------------------------- diff --git a/src/cache.cc b/src/cache.cc index 22b89497..6390d221 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -63,6 +63,12 @@ * Currently SimilarityGrid is always assumed to be 32 x 32 RGB. \n */ +namespace +{ + +constexpr gint CACHE_LOAD_LINE_NOISE = 8; + +} // namespace /* *------------------------------------------------------------------- @@ -409,10 +415,6 @@ static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData return FALSE; } -enum { - CACHE_LOAD_LINE_NOISE = 8 -}; - CacheData *cache_sim_data_load(const gchar *path) { FILE *f; diff --git a/src/cellrenderericon.cc b/src/cellrenderericon.cc index 6064e115..d839b724 100644 --- a/src/cellrenderericon.cc +++ b/src/cellrenderericon.cc @@ -24,10 +24,12 @@ #include #include -enum { - FIXED_ICON_SIZE_MAX = 512 -}; +namespace +{ + +constexpr gint FIXED_ICON_SIZE_MAX = 512; +} // namespace static void gqv_cell_renderer_icon_get_property(GObject *object, guint param_id, diff --git a/src/histogram.cc b/src/histogram.cc index e8aa2937..6f8ad848 100644 --- a/src/histogram.cc +++ b/src/histogram.cc @@ -35,9 +35,12 @@ *---------------------------------------------------------------------------- */ -enum { - HISTMAP_SIZE = 256 -}; +namespace +{ + +constexpr gint HISTMAP_SIZE = 256; + +} // namespace struct HistMap { gulong r[HISTMAP_SIZE]; diff --git a/src/image-load-psd.cc b/src/image-load-psd.cc index 8b27383b..b9dbf8f8 100644 --- a/src/image-load-psd.cc +++ b/src/image-load-psd.cc @@ -92,9 +92,7 @@ struct PsdHeader guint16 color_mode; /* color mode as defined below */ }; -enum { - PSD_HEADER_SIZE = 26 -}; +constexpr guint PSD_HEADER_SIZE = 26; enum PsdColorMode { diff --git a/src/image-overlay.cc b/src/image-overlay.cc index 2dec6382..ec1bc2cf 100644 --- a/src/image-overlay.cc +++ b/src/image-overlay.cc @@ -46,6 +46,13 @@ #include "typedefs.h" #include "ui-fileops.h" +namespace +{ + +constexpr gint IMAGE_OSD_DEFAULT_DURATION = 30; + +} // namespace + struct HistMap; /* @@ -99,10 +106,6 @@ static OSDIcon osd_icons[] = { #define OSD_DATA "overlay-data" -enum { - IMAGE_OSD_DEFAULT_DURATION = 30 -}; - enum { HISTOGRAM_HEIGHT = 140, HISTOGRAM_WIDTH = 256 diff --git a/src/layout-util.cc b/src/layout-util.cc index d1d619a7..5dd2e1e0 100644 --- a/src/layout-util.cc +++ b/src/layout-util.cc @@ -81,10 +81,6 @@ #include "view-file.h" #include "window.h" -enum { - MENU_EDIT_ACTION_OFFSET = 16, -}; - static gboolean layout_bar_enabled(LayoutWindow *lw); static gboolean layout_bar_sort_enabled(LayoutWindow *lw); static void layout_bars_hide_toggle(LayoutWindow *lw); diff --git a/src/pan-view/pan-calendar.cc b/src/pan-view/pan-calendar.cc index 558f79bb..f348140e 100644 --- a/src/pan-view/pan-calendar.cc +++ b/src/pan-view/pan-calendar.cc @@ -36,6 +36,13 @@ #include "pixbuf-util.h" #include "typedefs.h" +namespace +{ + +constexpr guint8 PAN_CAL_DOT_ALPHA = 128; + +} // namespace + #define PAN_CAL_POPUP_COLOR 220, 220, 220 enum { PAN_CAL_POPUP_ALPHA = 255, @@ -70,9 +77,6 @@ enum { PAN_CAL_DOT_GAP = 2 }; #define PAN_CAL_DOT_COLOR 128, 128, 128 -enum { - PAN_CAL_DOT_ALPHA = 128 -}; #define PAN_CAL_DAY_OF_WEEK_COLOR 128, 128, 128 diff --git a/src/pan-view/pan-view.cc b/src/pan-view/pan-view.cc index 13a0839f..26c97239 100644 --- a/src/pan-view/pan-view.cc +++ b/src/pan-view/pan-view.cc @@ -71,21 +71,20 @@ #include "utilops.h" #include "window.h" +namespace +{ + +constexpr gint PAN_TILE_SIZE = 512; +constexpr gint ZOOM_LABEL_WIDTH = 64; + +} // namespace enum { PAN_WINDOW_DEFAULT_WIDTH = 720, PAN_WINDOW_DEFAULT_HEIGHT = 500 }; -enum { - PAN_TILE_SIZE = 512 -}; - #define ZOOM_INCREMENT 1.0 -enum { - ZOOM_LABEL_WIDTH = 64 -}; - #define PAN_PREF_GROUP "pan_view_options" #define PAN_PREF_HIDE_WARNING "hide_performance_warning" diff --git a/src/pixbuf-renderer.cc b/src/pixbuf-renderer.cc index c3f87864..ced53dbf 100644 --- a/src/pixbuf-renderer.cc +++ b/src/pixbuf-renderer.cc @@ -53,20 +53,20 @@ enum ExifOrientationType { }; #endif - -/* default min and max zoom */ -#define PR_ZOOM_MIN (-32.0) -#define PR_ZOOM_MAX 32.0 +namespace +{ /* distance to drag mouse to disable image flip */ -enum { - PR_DRAG_SCROLL_THRESHHOLD = 4 -}; +constexpr gint PR_DRAG_SCROLL_THRESHHOLD = 4; /* increase pan rate when holding down shift */ -enum { - PR_PAN_SHIFT_MULTIPLIER = 6 -}; +constexpr gint PR_PAN_SHIFT_MULTIPLIER = 6; + +} // namespace + +/* default min and max zoom */ +#define PR_ZOOM_MIN (-32.0) +#define PR_ZOOM_MAX 32.0 /* scroller config */ enum { diff --git a/src/preferences.cc b/src/preferences.cc index ab391c8d..2d17a9b4 100644 --- a/src/preferences.cc +++ b/src/preferences.cc @@ -81,6 +81,14 @@ #include "utilops.h" #include "window.h" +namespace +{ + +constexpr gint PRE_FORMATTED_COLUMNS = 5; +constexpr gint KEYWORD_DIALOG_WIDTH = 400; + +} // namespace + struct ZoneDetect; enum { @@ -2501,9 +2509,6 @@ static void config_tab_windows(GtkWidget *notebook) options->fullscreen.disable_saver, &c_options->fullscreen.disable_saver); } -enum { - PRE_FORMATTED_COLUMNS = 5 -}; static void config_tab_osd(GtkWidget *notebook) { GtkWidget *hbox; @@ -3041,10 +3046,6 @@ struct KeywordFindData guint idle_id; /* event source id */ }; -enum { - KEYWORD_DIALOG_WIDTH = 400 -}; - static void keywords_find_folder(KeywordFindData *kfd, FileData *dir_fd) { GList *list_d = nullptr; diff --git a/src/print.cc b/src/print.cc index f24f02ba..d4abcea3 100644 --- a/src/print.cc +++ b/src/print.cc @@ -51,6 +51,13 @@ /* method to use when scaling down image data */ #define PRINT_MAX_INTERP GDK_INTERP_BILINEAR +namespace +{ + +constexpr gint PRE_FORMATTED_COLUMNS = 4; + +} // namespace + /* reverse order is important */ enum TextPosition { FOOTER_2, @@ -356,9 +363,6 @@ static void image_text_template_view_changed_cb(GtkWidget *, gpointer data) gtk_text_buffer_get_text(pTextBuffer, &iStart, &iEnd, TRUE)); } -enum { - PRE_FORMATTED_COLUMNS = 4 -}; static void print_text_menu(GtkWidget *box, PrintWindow *pw) { GtkWidget *group; diff --git a/src/remote.cc b/src/remote.cc index c7ce7522..7d47b178 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -64,19 +64,17 @@ #include "utilops.h" #include "view-file.h" -enum { - SERVER_MAX_CLIENTS = 8 -}; - -enum { - REMOTE_SERVER_BACKLOG = 4 -}; - - #ifndef UNIX_PATH_MAX #define UNIX_PATH_MAX 108 #endif +namespace +{ + +constexpr guint SERVER_MAX_CLIENTS = 8; +constexpr int REMOTE_SERVER_BACKLOG = 4; + +} // namespace static RemoteConnection *remote_client_open(const gchar *path); static gint remote_client_send(RemoteConnection *rc, const gchar *text); diff --git a/src/renderer-tiles.cc b/src/renderer-tiles.cc index aa364c01..bb859776 100644 --- a/src/renderer-tiles.cc +++ b/src/renderer-tiles.cc @@ -60,6 +60,13 @@ enum ExifOrientationType { }; #endif +namespace +{ + +constexpr size_t COLOR_BYTES = 3; /* rgb */ + +} // namespace + struct QueueData; struct ImageTile @@ -887,10 +894,6 @@ static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt) return rt->spare_tile; } -enum { - COLOR_BYTES = 3 /* rgb */ -}; - static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h) { GdkPixbuf *src = *tile; diff --git a/src/ui-bookmark.cc b/src/ui-bookmark.cc index ed4fb82e..69a8f49c 100644 --- a/src/ui-bookmark.cc +++ b/src/ui-bookmark.cc @@ -55,6 +55,14 @@ #define MARKER_PATH "[path]" #define MARKER_ICON "[icon]" +namespace +{ + +constexpr gint bookmark_drop_types_n = 3; +constexpr gint bookmark_drag_types_n = 2; + +} // namespace + struct BookButtonData; struct BookMarkData @@ -106,18 +114,11 @@ static GtkTargetEntry bookmark_drop_types[] = { { const_cast("x-url/http"), 0, TARGET_X_URL }, { const_cast("_NETSCAPE_URL"), 0, TARGET_X_URL } }; -enum { - bookmark_drop_types_n = 3 -}; static GtkTargetEntry bookmark_drag_types[] = { { const_cast("text/uri-list"), 0, TARGET_URI_LIST }, { const_cast("text/plain"), 0, TARGET_TEXT_PLAIN } }; -enum { - bookmark_drag_types_n = 2 -}; - static GList *bookmark_widget_list = nullptr; static GList *bookmark_default_list = nullptr; diff --git a/src/ui-pathsel.cc b/src/ui-pathsel.cc index c802919b..bc3fc044 100644 --- a/src/ui-pathsel.cc +++ b/src/ui-pathsel.cc @@ -46,16 +46,18 @@ #include "uri-utils.h" #include "utilops.h" +namespace +{ + +constexpr gint dest_drag_types_n = 2; + +} // namespace enum { DEST_WIDTH = 250, DEST_HEIGHT = 210 }; -enum { - RENAME_PRESS_DELAY = 333 /* 1/3 second, to allow double clicks */ -}; - #define PATH_SEL_USE_HEADINGS FALSE enum { @@ -331,10 +333,6 @@ static GtkTargetEntry dest_drag_types[] = { { const_cast("text/uri-list"), 0, TARGET_URI_LIST }, { const_cast("text/plain"), 0, TARGET_TEXT_PLAIN } }; -enum { - dest_drag_types_n = 2 -}; - static void dest_dnd_set_data(GtkWidget *view, GdkDragContext *, GtkSelectionData *selection_data, diff --git a/src/utilops.cc b/src/utilops.cc index b50741a3..daf8b72c 100644 --- a/src/utilops.cc +++ b/src/utilops.cc @@ -54,6 +54,11 @@ namespace { +constexpr gint DIALOG_WIDTH = 750; + +/* thumbnail spec has a max depth of 4 (.thumb??/fail/appname/??.png) */ +constexpr gint UTILITY_DELETE_MAX_DEPTH = 5; + struct PixmapErrors { GdkPixbuf *error; @@ -94,11 +99,7 @@ GdkPixbuf *file_util_get_error_icon(FileData *fd, GList *list, GtkWidget *) return pe.apply; } -} - -enum { - DIALOG_WIDTH = 750 -}; +} // namespace enum ClipboardDestination { CLIPBOARD_TEXT_PLAIN = 0, @@ -501,11 +502,6 @@ enum { UTILITY_LIST_MIN_HEIGHT = 150 }; -/* thumbnail spec has a max depth of 4 (.thumb??/fail/appname/??.png) */ -enum { - UTILITY_DELETE_MAX_DEPTH = 5 -}; - static UtilityData *file_util_data_new(UtilityType type) { UtilityData *ud; -- 2.20.1