Replace single value enums with constexpr <type>
authorColin Clark <colin.clark@cclark.uk>
Sat, 9 Mar 2024 15:57:26 +0000 (15:57 +0000)
committerColin Clark <colin.clark@cclark.uk>
Sat, 9 Mar 2024 15:57:26 +0000 (15:57 +0000)
Commit a4ce95bc clang-tidy: modernize-macro-to-enum, resulted in some
enums containing a single value.

These are now replaced by a constexpr <type> statement.

The script used to identify these single value enum statements is now
included as a meson test.

21 files changed:
meson.build
scripts/enum-check.sh [new file with mode: 0755]
src/advanced-exif.cc
src/bar-gps.cc
src/cache-maint.cc
src/cache.cc
src/cellrenderericon.cc
src/histogram.cc
src/image-load-psd.cc
src/image-overlay.cc
src/layout-util.cc
src/pan-view/pan-calendar.cc
src/pan-view/pan-view.cc
src/pixbuf-renderer.cc
src/preferences.cc
src/print.cc
src/remote.cc
src/renderer-tiles.cc
src/ui-bookmark.cc
src/ui-pathsel.cc
src/utilops.cc

index 294f9d5..ea644b4 100644 (file)
@@ -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 (executable)
index 0000000..8c9a8c7
--- /dev/null
@@ -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
+
index e271c42..736f7d9 100644 (file)
 #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
index f5e036f..a692944 100644 (file)
 #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
 
 /*
  *-------------------------------------------------------------------
index 227fae1..b6f68c5 100644 (file)
 #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 *-----------------------------------------------------------------------------
index 22b8949..6390d22 100644 (file)
  * 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;
index 6064e11..d839b72 100644 (file)
 #include <cairo.h>
 #include <gdk/gdk.h>
 
-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,
index e8aa293..6f8ad84 100644 (file)
  *----------------------------------------------------------------------------
  */
 
-enum {
-       HISTMAP_SIZE = 256
-};
+namespace
+{
+
+constexpr gint HISTMAP_SIZE = 256;
+
+} // namespace
 
 struct HistMap {
        gulong r[HISTMAP_SIZE];
index 8b27383..b9dbf8f 100644 (file)
@@ -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
 {
index 2dec638..ec1bc2c 100644 (file)
 #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
index d1d619a..5dd2e1e 100644 (file)
 #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);
index 558f79b..f348140 100644 (file)
 #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
 
index 13a0839..26c9723 100644 (file)
 #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"
index c3f8786..ced53db 100644 (file)
@@ -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 {
index ab391c8..2d17a9b 100644 (file)
 #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;
index f24f02b..d4abcea 100644 (file)
 /* 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;
index c7ce752..7d47b17 100644 (file)
 #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);
index aa364c0..bb85977 100644 (file)
@@ -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;
index ed4fb82..69a8f49 100644 (file)
 #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<gchar *>("x-url/http"),    0, TARGET_X_URL },
        { const_cast<gchar *>("_NETSCAPE_URL"), 0, TARGET_X_URL }
 };
-enum {
-       bookmark_drop_types_n = 3
-};
 
 static GtkTargetEntry bookmark_drag_types[] = {
        { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
        { const_cast<gchar *>("text/plain"),    0, TARGET_TEXT_PLAIN }
 };
-enum {
-       bookmark_drag_types_n = 2
-};
-
 
 static GList *bookmark_widget_list = nullptr;
 static GList *bookmark_default_list = nullptr;
index c802919..bc3fc04 100644 (file)
 #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<gchar *>("text/uri-list"), 0, TARGET_URI_LIST },
        { const_cast<gchar *>("text/plain"),    0, TARGET_TEXT_PLAIN }
 };
-enum {
-       dest_drag_types_n = 2
-};
-
 
 static void dest_dnd_set_data(GtkWidget *view, GdkDragContext *,
                                  GtkSelectionData *selection_data,
index b50741a..daf8b72 100644 (file)
 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;