[vlc-commits] [Git][videolan/vlc][master] modules: add type strictness to cat/subcat types

Hugo Beauzée-Luyssen (@chouquette) gitlab at videolan.org
Thu Jan 20 07:59:48 UTC 2022



Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC


Commits:
44318673 by Lyndon Brown at 2022-01-20T07:45:45+00:00
modules: add type strictness to cat/subcat types

with cats and subcats both being plain integers it's very easy to
make a mistake mixing them up, storing one in a variable for another,
or passing the wrong variable to the wrong function.

switching to enums adds type strictness to help catch such mistakes.

in switching to enums there is of course additional opportunity to
exploit sequential numbering to achieve certain optimisations that
can be gained from this, but that it not done here. here we're simply
interested in catching mistakes through type strictness.

note that we simply retain the same cat/subcat numbering as before
here.

- - - - -


5 changed files:

- include/vlc_config_cat.h
- include/vlc_plugin.h
- modules/gui/macosx/preferences/prefs.m
- modules/gui/qt/dialogs/preferences/complete_preferences.cpp
- modules/gui/qt/dialogs/preferences/complete_preferences.hpp


Changes:

=====================================
include/vlc_config_cat.h
=====================================
@@ -192,15 +192,15 @@
 
 struct config_category_t
 {
-    int         id;
-    int         general_subcat;
+    enum vlc_config_cat id;
+    enum vlc_config_subcat general_subcat;
     const char *help;
 };
 
 struct config_subcategory_t
 {
-    int         id;
-    int         cat;
+    enum vlc_config_subcat id;
+    enum vlc_config_cat cat;
     const char *name;
     const char *help;
 };
@@ -263,7 +263,7 @@ static const struct config_subcategory_t subcategories_array[] =
 
 /** Get the table index for the given category entry. */
 VLC_USED
-static inline int vlc_config_cat_IndexOf( int cat )
+static inline int vlc_config_cat_IndexOf( enum vlc_config_cat cat )
 {
     int index = -1;
     for( unsigned i = 0; i < ARRAY_SIZE(categories_array); i++ )
@@ -279,7 +279,7 @@ static inline int vlc_config_cat_IndexOf( int cat )
 
 /** Get the table index for the given subcategory entry. */
 VLC_USED
-static inline int vlc_config_subcat_IndexOf( int subcat )
+static inline int vlc_config_subcat_IndexOf( enum vlc_config_subcat subcat )
 {
     int index = -1;
     for( unsigned i = 0; i < ARRAY_SIZE(subcategories_array); i++ )
@@ -309,7 +309,7 @@ static inline int vlc_config_subcat_IndexOf( int subcat )
  * a child node of a subcategory node.
  */
 VLC_USED
-static inline int vlc_config_cat_GetGeneralSubcat( int cat )
+static inline enum vlc_config_subcat vlc_config_cat_GetGeneralSubcat( enum vlc_config_cat cat )
 {
     int i = vlc_config_cat_IndexOf( cat );
     return (i != -1) ? categories_array[i].general_subcat : SUBCAT_UNKNOWN;
@@ -317,7 +317,7 @@ static inline int vlc_config_cat_GetGeneralSubcat( int cat )
 
 /** Get the name for a subcategory. */
 VLC_USED
-static inline const char *vlc_config_subcat_GetName( int subcat )
+static inline const char *vlc_config_subcat_GetName( enum vlc_config_subcat subcat )
 {
     int i = vlc_config_subcat_IndexOf( subcat );
     return (i != -1) ? vlc_gettext(subcategories_array[i].name) : NULL;
@@ -325,7 +325,7 @@ static inline const char *vlc_config_subcat_GetName( int subcat )
 
 /** Get the help text for a subcategory. */
 VLC_USED
-static inline const char *vlc_config_subcat_GetHelp( int subcat )
+static inline const char *vlc_config_subcat_GetHelp( enum vlc_config_subcat subcat )
 {
     int i = vlc_config_subcat_IndexOf( subcat );
     return (i != -1) ? vlc_gettext(subcategories_array[i].help) : NULL;
@@ -333,15 +333,15 @@ static inline const char *vlc_config_subcat_GetHelp( int subcat )
 
 /** Get the name for a category. */
 VLC_USED
-static inline const char *vlc_config_cat_GetName( int cat )
+static inline const char *vlc_config_cat_GetName( enum vlc_config_cat cat )
 {
-    int subcat = vlc_config_cat_GetGeneralSubcat( cat );
+    enum vlc_config_subcat subcat = vlc_config_cat_GetGeneralSubcat( cat );
     return vlc_config_subcat_GetName( subcat );
 }
 
 /** Get the help text for a category. */
 VLC_USED
-static inline const char *vlc_config_cat_GetHelp( int cat )
+static inline const char *vlc_config_cat_GetHelp( enum vlc_config_cat cat )
 {
     int i = vlc_config_cat_IndexOf( cat );
     return (i != -1) ? vlc_gettext(categories_array[i].help) : NULL;
@@ -349,7 +349,7 @@ static inline const char *vlc_config_cat_GetHelp( int cat )
 
 /** Get the parent category for the given subcategory. */
 VLC_USED
-static inline int vlc_config_cat_FromSubcat( int subcat )
+static inline enum vlc_config_cat vlc_config_cat_FromSubcat( enum vlc_config_subcat subcat )
 {
     int i = vlc_config_subcat_IndexOf( subcat );
     return (i != -1) ? subcategories_array[i].cat : CAT_UNKNOWN;
@@ -357,11 +357,11 @@ static inline int vlc_config_cat_FromSubcat( int subcat )
 
 /** Check if the given subcategory is a "general" one. */
 VLC_USED
-static inline bool vlc_config_subcat_IsGeneral( int subcat )
+static inline bool vlc_config_subcat_IsGeneral( enum vlc_config_subcat subcat )
 {
     if( subcat == SUBCAT_UNKNOWN )
         return false;
-    int cat = vlc_config_cat_FromSubcat( subcat );
+    enum vlc_config_cat cat = vlc_config_cat_FromSubcat( subcat );
     return (subcat == vlc_config_cat_GetGeneralSubcat( cat ));
 }
 


=====================================
include/vlc_plugin.h
=====================================
@@ -143,64 +143,75 @@ enum vlc_module_properties
 #define IsConfigFloatType(type) \
     ((type) == CONFIG_ITEM_FLOAT)
 
-/* Hidden categories and subcategories */
-/* Any options under this will be hidden in the GUI preferences, but will be
-   listed in cmdline help output. */
-#define CAT_HIDDEN -1
-#define SUBCAT_HIDDEN -1
-
-/* Unknown/unset/invalid */
-#define CAT_UNKNOWN 0
-#define SUBCAT_UNKNOWN 0
-
-/* Categories and subcategories */
-#define CAT_INTERFACE 1
-#define SUBCAT_INTERFACE_GENERAL 101
-#define SUBCAT_INTERFACE_MAIN 102
-#define SUBCAT_INTERFACE_CONTROL 103
-#define SUBCAT_INTERFACE_HOTKEYS 104
-
-#define CAT_AUDIO 2
-#define SUBCAT_AUDIO_GENERAL 201
-#define SUBCAT_AUDIO_AOUT 202
-#define SUBCAT_AUDIO_AFILTER 203
-#define SUBCAT_AUDIO_VISUAL 204
-#define SUBCAT_AUDIO_RESAMPLER 206
-
-#define CAT_VIDEO 3
-#define SUBCAT_VIDEO_GENERAL 301
-#define SUBCAT_VIDEO_VOUT 302
-#define SUBCAT_VIDEO_VFILTER 303
-#define SUBCAT_VIDEO_SUBPIC 305
-#define SUBCAT_VIDEO_SPLITTER 306
-
-#define CAT_INPUT 4
-#define SUBCAT_INPUT_GENERAL 401
-#define SUBCAT_INPUT_ACCESS 402
-#define SUBCAT_INPUT_DEMUX 403
-#define SUBCAT_INPUT_VCODEC 404
-#define SUBCAT_INPUT_ACODEC 405
-#define SUBCAT_INPUT_SCODEC 406
-#define SUBCAT_INPUT_STREAM_FILTER 407
-
-#define CAT_SOUT 5
-#define SUBCAT_SOUT_GENERAL 501
-#define SUBCAT_SOUT_STREAM 502
-#define SUBCAT_SOUT_MUX 503
-#define SUBCAT_SOUT_ACO 504
-#define SUBCAT_SOUT_PACKETIZER 505
-#define SUBCAT_SOUT_VOD 507
-#define SUBCAT_SOUT_RENDERER 508
-
-#define CAT_ADVANCED 6
-#define SUBCAT_ADVANCED_MISC 602
-#define SUBCAT_ADVANCED_NETWORK 603
-
-#define CAT_PLAYLIST 7
-#define SUBCAT_PLAYLIST_GENERAL 701
-#define SUBCAT_PLAYLIST_SD 702
-#define SUBCAT_PLAYLIST_EXPORT 703
+/* Config category */
+enum vlc_config_cat
+{
+    /* Hidden category.
+       Any options under this will be hidden in the GUI preferences, but will
+       be listed in cmdline help output. */
+    CAT_HIDDEN    = -1,
+
+    CAT_UNKNOWN   = 0,
+
+    CAT_INTERFACE = 1,
+    CAT_AUDIO     = 2,
+    CAT_VIDEO     = 3,
+    CAT_INPUT     = 4,
+    CAT_SOUT      = 5,
+    CAT_ADVANCED  = 6,
+    CAT_PLAYLIST  = 7,
+};
 
+/* Config subcategory */
+enum vlc_config_subcat
+{
+    /* Hidden subcategory.
+       Any options under this will be hidden in the GUI preferences, but will
+       be listed in cmdline help output. */
+    SUBCAT_HIDDEN              = -1,
+
+    SUBCAT_UNKNOWN             = 0,
+
+    SUBCAT_INTERFACE_GENERAL   = 101,
+    SUBCAT_INTERFACE_MAIN      = 102,
+    SUBCAT_INTERFACE_CONTROL   = 103,
+    SUBCAT_INTERFACE_HOTKEYS   = 104,
+
+    SUBCAT_AUDIO_GENERAL       = 201,
+    SUBCAT_AUDIO_AOUT          = 202,
+    SUBCAT_AUDIO_AFILTER       = 203,
+    SUBCAT_AUDIO_VISUAL        = 204,
+    SUBCAT_AUDIO_RESAMPLER     = 206,
+
+    SUBCAT_VIDEO_GENERAL       = 301,
+    SUBCAT_VIDEO_VOUT          = 302,
+    SUBCAT_VIDEO_VFILTER       = 303,
+    SUBCAT_VIDEO_SUBPIC        = 305,
+    SUBCAT_VIDEO_SPLITTER      = 306,
+
+    SUBCAT_INPUT_GENERAL       = 401,
+    SUBCAT_INPUT_ACCESS        = 402,
+    SUBCAT_INPUT_DEMUX         = 403,
+    SUBCAT_INPUT_VCODEC        = 404,
+    SUBCAT_INPUT_ACODEC        = 405,
+    SUBCAT_INPUT_SCODEC        = 406,
+    SUBCAT_INPUT_STREAM_FILTER = 407,
+
+    SUBCAT_SOUT_GENERAL        = 501,
+    SUBCAT_SOUT_STREAM         = 502,
+    SUBCAT_SOUT_MUX            = 503,
+    SUBCAT_SOUT_ACO            = 504,
+    SUBCAT_SOUT_PACKETIZER     = 505,
+    SUBCAT_SOUT_VOD            = 507,
+    SUBCAT_SOUT_RENDERER       = 508,
+
+    SUBCAT_ADVANCED_MISC       = 602,
+    SUBCAT_ADVANCED_NETWORK    = 603,
+
+    SUBCAT_PLAYLIST_GENERAL    = 701,
+    SUBCAT_PLAYLIST_SD         = 702,
+    SUBCAT_PLAYLIST_EXPORT     = 703,
+};
 
 /**
  * Current plugin ABI version


=====================================
modules/gui/macosx/preferences/prefs.m
=====================================
@@ -111,25 +111,25 @@ enum VLCTreeBranchType {
 @interface VLCTreeBranchItem : VLCTreeItem
 {
     enum VLCTreeBranchType _branchType;
-    int _category;
-    int _subcategory;
+    enum vlc_config_cat _category;
+    enum vlc_config_subcat _subcategory;
     /* for plugin type */
     module_config_t * _configItems;
     unsigned int _configSize;
 }
-+ (VLCTreeBranchItem *)newCategoryTreeBranch:(int)category;
-+ (VLCTreeBranchItem *)newSubcategoryTreeBranch:(int)subcategory;
++ (VLCTreeBranchItem *)newCategoryTreeBranch:(enum vlc_config_cat)category;
++ (VLCTreeBranchItem *)newSubcategoryTreeBranch:(enum vlc_config_subcat)subcategory;
 + (VLCTreeBranchItem *)newPluginTreeBranch:(module_t *)plugin;
 
-- (id)initWithCategory:(int)category;
-- (id)initWithSubcategory:(int)subcategory;
+- (id)initWithCategory:(enum vlc_config_cat)category;
+- (id)initWithSubcategory:(enum vlc_config_subcat)subcategory;
 - (id)initWithPlugin:(module_t *)plugin;
 
-- (VLCTreeBranchItem *)childRepresentingSubcategory:(int)category;
+- (VLCTreeBranchItem *)childRepresentingSubcategory:(enum vlc_config_cat)category;
 
 - (enum VLCTreeBranchType)branchType;
-- (int)category;
-- (int)subcategory;
+- (enum vlc_config_cat)category;
+- (enum vlc_config_subcat)subcategory;
 - (module_config_t *)configItems;
 - (unsigned int)configSize;
 @end
@@ -148,7 +148,7 @@ enum VLCTreeBranchType {
 {
     module_config_t * _configItems;
 }
-- (VLCTreeBranchItem *)childRepresentingCategory:(int)category;
+- (VLCTreeBranchItem *)childRepresentingCategory:(enum vlc_config_cat)category;
 @end
 
 #pragma mark -
@@ -446,7 +446,7 @@ enum VLCTreeBranchType {
 #pragma mark -
 @implementation VLCTreeMainItem
 
-- (VLCTreeBranchItem *)childRepresentingCategory:(int)category
+- (VLCTreeBranchItem *)childRepresentingCategory:(enum vlc_config_cat)category
 {
     NSUInteger childrenCount = [[self children] count];
     for (int i = 0; i < childrenCount; i++) {
@@ -476,8 +476,8 @@ enum VLCTreeBranchType {
         VLCTreeBranchItem * subcategoryItem = nil;
         VLCTreeBranchItem * pluginItem = nil;
         module_config_t *p_configs = NULL;
-        int cat = CAT_UNKNOWN;
-        int subcat = SUBCAT_UNKNOWN;
+        enum vlc_config_cat cat = CAT_UNKNOWN;
+        enum vlc_config_subcat subcat = SUBCAT_UNKNOWN;
         bool subcat_is_general = false;
         bool plugin_node_added = false;
         bool pending_tree_node_creation = false;
@@ -503,7 +503,7 @@ enum VLCTreeBranchType {
             int configType = p_configs[j].i_type;
 
             if (configType == CONFIG_SUBCATEGORY) {
-                subcat = (int) p_configs[j].value.i;
+                subcat = (enum vlc_config_subcat) p_configs[j].value.i;
                 cat = vlc_config_cat_FromSubcat(subcat);
                 subcat_is_general = vlc_config_subcat_IsGeneral(subcat);
                 pending_tree_node_creation = true;
@@ -603,16 +603,16 @@ enum VLCTreeBranchType {
 
 #pragma mark -
 @implementation VLCTreeBranchItem
-+ (VLCTreeBranchItem *)newCategoryTreeBranch:(int)category
++ (VLCTreeBranchItem *)newCategoryTreeBranch:(enum vlc_config_cat)category
 {
     if (!vlc_config_cat_GetName(category)) {
-        msg_Err(getIntf(), "failed to get name for category %i", category);
+        msg_Err(getIntf(), "failed to get name for category %i", (int)category);
         return nil;
     }
     return [[[self class] alloc] initWithCategory:category];
 }
 
-+ (VLCTreeBranchItem *)newSubcategoryTreeBranch:(int)subcategory
++ (VLCTreeBranchItem *)newSubcategoryTreeBranch:(enum vlc_config_subcat)subcategory
 {
     if (!vlc_config_subcat_GetName(subcategory))
         return nil;
@@ -624,7 +624,7 @@ enum VLCTreeBranchType {
     return [[[self class] alloc] initWithPlugin:plugin];
 }
 
-- (id)initWithCategory:(int)category
+- (id)initWithCategory:(enum vlc_config_cat)category
 {
     NSString * name = _NS(vlc_config_cat_GetName(category));
     if (self = [super initWithName:name]) {
@@ -638,7 +638,7 @@ enum VLCTreeBranchType {
     return self;
 }
 
-- (id)initWithSubcategory:(int)subcategory
+- (id)initWithSubcategory:(enum vlc_config_subcat)subcategory
 {
     NSString * name = _NS(vlc_config_subcat_GetName(subcategory));
     if (self = [super initWithName:name]) {
@@ -672,7 +672,7 @@ enum VLCTreeBranchType {
         module_config_free(_configItems);
 }
 
-- (VLCTreeBranchItem *)childRepresentingSubcategory:(int)subcategory
+- (VLCTreeBranchItem *)childRepresentingSubcategory:(enum vlc_config_subcat)subcategory
 {
     assert([self isKindOfClass:[VLCTreeBranchItem class]]);
     NSUInteger childrenCount = [[self children] count];
@@ -689,12 +689,12 @@ enum VLCTreeBranchType {
     return _branchType;
 }
 
-- (int)category
+- (enum vlc_config_cat)category
 {
     return _category;
 }
 
-- (int)subcategory
+- (enum vlc_config_subcat)subcategory
 {
     return _subcategory;
 }


=====================================
modules/gui/qt/dialogs/preferences/complete_preferences.cpp
=====================================
@@ -65,8 +65,8 @@ PrefsTree::PrefsTree( qt_intf_t *_p_intf, QWidget *_parent,
     main_module = module_get_main();
     assert( main_module );
 
-    int cat = CAT_UNKNOWN;
-    int subcat = SUBCAT_UNKNOWN;
+    enum vlc_config_cat cat = CAT_UNKNOWN;
+    enum vlc_config_subcat subcat = SUBCAT_UNKNOWN;
     QTreeWidgetItem *cat_item = nullptr;
 
     /* Create base cat/subcat tree from core config set */
@@ -80,7 +80,7 @@ PrefsTree::PrefsTree( qt_intf_t *_p_intf, QWidget *_parent,
         if( p_item->i_type != CONFIG_SUBCATEGORY )
             continue;
 
-        subcat = p_item->value.i;
+        subcat = (enum vlc_config_subcat) p_item->value.i;
         cat = vlc_config_cat_FromSubcat(subcat);
 
         if( cat == CAT_UNKNOWN || cat == CAT_HIDDEN )
@@ -124,7 +124,7 @@ PrefsTree::PrefsTree( qt_intf_t *_p_intf, QWidget *_parent,
 
             if( p_item->i_type == CONFIG_SUBCATEGORY )
             {
-                subcat = p_item->value.i;
+                subcat = (enum vlc_config_subcat) p_item->value.i;
                 cat = vlc_config_cat_FromSubcat( subcat );
                 continue;
             }
@@ -180,9 +180,9 @@ PrefsTree::PrefsTree( qt_intf_t *_p_intf, QWidget *_parent,
     resizeColumnToContents( 0 );
 }
 
-QTreeWidgetItem *PrefsTree::createCatNode( int cat )
+QTreeWidgetItem *PrefsTree::createCatNode( enum vlc_config_cat cat )
 {
-    int general_subcat = vlc_config_cat_GetGeneralSubcat( cat );
+    enum vlc_config_subcat general_subcat = vlc_config_cat_GetGeneralSubcat( cat );
     assert(general_subcat != SUBCAT_UNKNOWN && general_subcat != SUBCAT_HIDDEN);
 
     PrefsTreeItem *item = new PrefsTreeItem( PrefsTreeItem::CATEGORY_NODE );
@@ -212,8 +212,8 @@ QTreeWidgetItem *PrefsTree::createCatNode( int cat )
     item->setIcon( 0, icon );
     //current_item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
 
-    int cat_index = vlc_config_cat_IndexOf( cat );
-    int general_subcat_index = vlc_config_subcat_IndexOf( general_subcat );
+    int cat_index = (int) vlc_config_cat_IndexOf( cat );
+    int general_subcat_index = (int) vlc_config_subcat_IndexOf( general_subcat );
     this->catMap[cat_index] = item;
     this->subcatMap[general_subcat_index] = item;
 
@@ -223,7 +223,7 @@ QTreeWidgetItem *PrefsTree::createCatNode( int cat )
     return item;
 }
 
-QTreeWidgetItem *PrefsTree::createSubcatNode( QTreeWidgetItem * cat, int subcat )
+QTreeWidgetItem *PrefsTree::createSubcatNode( QTreeWidgetItem * cat, enum vlc_config_subcat subcat )
 {
     assert( cat );
 
@@ -238,7 +238,7 @@ QTreeWidgetItem *PrefsTree::createSubcatNode( QTreeWidgetItem * cat, int subcat
     item->setText( 0, item->name );
     //item->setSizeHint( 0, QSize( -1, ITEM_HEIGHT ) );
 
-    int subcat_index = vlc_config_subcat_IndexOf( subcat );
+    int subcat_index = (int) vlc_config_subcat_IndexOf( subcat );
     this->subcatMap[subcat_index] = item;
 
     cat->addChild( item );
@@ -269,13 +269,13 @@ void PrefsTree::createPluginNode( QTreeWidgetItem * parent, module_t *mod )
     parent->addChild( item );
 }
 
-QTreeWidgetItem *PrefsTree::findCatItem( int cat )
+QTreeWidgetItem *PrefsTree::findCatItem( enum vlc_config_cat cat )
 {
     int cat_index = vlc_config_cat_IndexOf( cat );
     return this->catMap[cat_index];
 }
 
-QTreeWidgetItem *PrefsTree::findSubcatItem( int subcat )
+QTreeWidgetItem *PrefsTree::findSubcatItem( enum vlc_config_subcat subcat )
 {
     int subcat_index = vlc_config_subcat_IndexOf( subcat );
     return this->subcatMap[subcat_index];
@@ -461,7 +461,7 @@ PrefsTreeItem::PrefsTreeItem( PrefsTreeItemType ty ) : QTreeWidgetItem(ty)
 bool PrefsTreeItem::contains( const QString &text, Qt::CaseSensitivity cs )
 {
     bool is_core = this->node_type != PrefsTreeItem::PLUGIN_NODE;
-    int id = this->subcat_id;
+    enum vlc_config_subcat id = this->subcat_id;
 
     /* find our module */
     module_t *p_module;


=====================================
modules/gui/qt/dialogs/preferences/complete_preferences.hpp
=====================================
@@ -78,8 +78,8 @@ public:
     AdvPrefsPanel *panel;
     QString name;
     QString help;
-    int cat_id;
-    int subcat_id;
+    enum vlc_config_cat cat_id;
+    enum vlc_config_subcat subcat_id;
     module_t *p_module;
     char *module_name;
     bool module_is_loaded;
@@ -101,11 +101,11 @@ public:
     }
 
 private:
-    QTreeWidgetItem *createCatNode( int cat );
-    QTreeWidgetItem *createSubcatNode( QTreeWidgetItem * cat, int subcat );
+    QTreeWidgetItem *createCatNode( enum vlc_config_cat cat );
+    QTreeWidgetItem *createSubcatNode( QTreeWidgetItem * cat, enum vlc_config_subcat subcat );
     void createPluginNode( QTreeWidgetItem * parent, module_t *mod );
-    QTreeWidgetItem *findCatItem( int cat );
-    QTreeWidgetItem *findSubcatItem( int subcat );
+    QTreeWidgetItem *findCatItem( enum vlc_config_cat cat );
+    QTreeWidgetItem *findSubcatItem( enum vlc_config_subcat subcat );
     bool filterItems( PrefsTreeItem *item, const QString &text, Qt::CaseSensitivity cs );
     bool collapseUnselectedItems( PrefsTreeItem *item );
     void updateLoadedStatus( PrefsTreeItem *item , QSet<QString> *loaded );



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/44318673acac58a0c42760f3014565f78c8793db

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/44318673acac58a0c42760f3014565f78c8793db
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list