From 912766c2e11aab2cecd916bdc2027cc14087c4a0 Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Tue, 12 Jul 2016 16:47:18 +0100 Subject: [PATCH] Switch to a static interface for Proc_Partitions_Info The Proc_Partitions_Info has a pseudo multi-object interface and uses the constructor to load the cache. However all the data in the class is static. A Proc_Partitions_Info object doesn't contain any member variables, yet was needed just to call the member functions. Make all the member functions static removing the need to use any Proc_Partitions_Info objects and provide and explicit load_cache() method. --- include/Proc_Partitions_Info.h | 9 +++---- src/GParted_Core.cc | 4 +-- src/Proc_Partitions_Info.cc | 46 +++++++++++++--------------------- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/include/Proc_Partitions_Info.h b/include/Proc_Partitions_Info.h index 8084492d..23b4348a 100644 --- a/include/Proc_Partitions_Info.h +++ b/include/Proc_Partitions_Info.h @@ -33,13 +33,12 @@ namespace GParted class Proc_Partitions_Info { public: - Proc_Partitions_Info() ; - Proc_Partitions_Info( bool do_refresh ) ; - ~Proc_Partitions_Info() ; - std::vector get_device_paths() ; + static void load_cache(); + static const std::vector & get_device_paths(); private: - void load_proc_partitions_info_cache() ; + static void initialize_if_required(); + static void load_proc_partitions_info_cache(); static bool proc_partitions_info_cache_initialized ; static std::vector device_paths_cache ; }; diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc index 38d4d9c2..441614f3 100644 --- a/src/GParted_Core.cc +++ b/src/GParted_Core.cc @@ -169,7 +169,7 @@ void GParted_Core::set_devices_thread( std::vector * pdevices ) BlockSpecial::clear_cache(); // MUST BE FIRST. Cache of name to major, minor // numbers incrementally loaded when BlockSpecial // objects are created in the following caches. - Proc_Partitions_Info pp_info( true ) ; // SHOULD BE SECOND. Caches /proc/partitions and + Proc_Partitions_Info::load_cache(); // SHOULD BE SECOND. Caches /proc/partitions and // pre-populates BlockSpecial cache. FS_Info fs_info( true ) ; //Refresh cache of file system information DMRaid dmraid( true ) ; //Refresh cache of dmraid device information @@ -191,7 +191,7 @@ void GParted_Core::set_devices_thread( std::vector * pdevices ) // http://parted.alioth.debian.org/cgi-bin/trac.cgi/ticket/194 // //try to find all available devices if devices exist in /proc/partitions - std::vector temp_devices = pp_info .get_device_paths() ; + std::vector temp_devices = Proc_Partitions_Info::get_device_paths(); if ( ! temp_devices .empty() ) { //Try to find all devices in /proc/partitions diff --git a/src/Proc_Partitions_Info.cc b/src/Proc_Partitions_Info.cc index fd16fb61..4262ba39 100644 --- a/src/Proc_Partitions_Info.cc +++ b/src/Proc_Partitions_Info.cc @@ -30,41 +30,29 @@ namespace GParted bool Proc_Partitions_Info::proc_partitions_info_cache_initialized = false ; std::vector Proc_Partitions_Info::device_paths_cache ; -Proc_Partitions_Info::Proc_Partitions_Info() +void Proc_Partitions_Info::load_cache() +{ + load_proc_partitions_info_cache(); + proc_partitions_info_cache_initialized = true; +} + +const std::vector & Proc_Partitions_Info::get_device_paths() +{ + initialize_if_required(); + return device_paths_cache; +} + +// Private Methods + +void Proc_Partitions_Info::initialize_if_required() { - //Ensure that cache has been loaded at least once if ( ! proc_partitions_info_cache_initialized ) { - proc_partitions_info_cache_initialized = true ; - load_proc_partitions_info_cache() ; + load_proc_partitions_info_cache(); + proc_partitions_info_cache_initialized = true; } } -Proc_Partitions_Info::Proc_Partitions_Info( bool do_refresh ) -{ - //Ensure that cache has been loaded at least once - if ( ! proc_partitions_info_cache_initialized ) - { - proc_partitions_info_cache_initialized = true ; - if ( do_refresh == false ) - load_proc_partitions_info_cache() ; - } - - if ( do_refresh ) - load_proc_partitions_info_cache() ; -} - -Proc_Partitions_Info::~Proc_Partitions_Info() -{ -} - -std::vector Proc_Partitions_Info::get_device_paths() -{ - return device_paths_cache ; -} - -//Private Methods - void Proc_Partitions_Info::load_proc_partitions_info_cache() { device_paths_cache .clear() ;