[gparted] Refactor get_filesystem_object()



commit 9c35d91453c9b3250d0b6b9e4cf4abdb4b7acd25
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Mon Sep 10 18:58:44 2018 +0100

    Refactor get_filesystem_object()
    
    The function was using std::map::count() [1] to test if the file system
    entry existed in the map before looking up the value using
    std::map::operator[] to avoid having operator[] inserting elements which
    don't exist [2].
    
    Rewrite using std::map::find() [3] so that map is only searched once,
    and so that it is more obvious what is happening without having to know
    the subtleties of std::map::count() and ::operator[].
    
    [1] std::map::count()
        http://www.cplusplus.com/reference/map/map/count/
    
        "Searches the container for elements with a key equivalent to k and
        returns the number of matches.
    
        Because all elements in a map container are unique, the function can
        only return 1 (if the element is found) or zero (otherwise).
        "
    
    [2] std::map::operator[]
        http://www.cplusplus.com/reference/map/map/operator[]/
    
        "If k does not match the key of any element in the container, the
        function inserts a new element with that key and returns a reference
        to its mapped value.  Notice that this always increases the
        container size by one, even if no mapped value is assigned to the
        element (the element is constructed using its default constructor).
        "
    
    [3] std::map::find
        http://www.cplusplus.com/reference/map/map/find/
    
        "Searches the container for an element with a key equivalent to k
        and returns an iterator to it if found, otherwise it returns an
        iterator to map::end.
        "

 include/GParted_Core.h | 2 +-
 src/GParted_Core.cc    | 9 +++++----
 2 files changed, 6 insertions(+), 5 deletions(-)
---
diff --git a/include/GParted_Core.h b/include/GParted_Core.h
index e3a923b0..94d204b5 100644
--- a/include/GParted_Core.h
+++ b/include/GParted_Core.h
@@ -65,7 +65,7 @@ public:
        Glib::ustring get_libparted_version() ;
        Glib::ustring get_thread_status_message() ;
 
-       static FileSystem * get_filesystem_object( FSType filesystem );
+       static FileSystem * get_filesystem_object( FSType fstype );
        static bool supported_filesystem( FSType fstype );
        static FS_Limits get_filesystem_limits( FSType fstype, const Partition & partition );
        static bool filesystem_resize_disallowed( const Partition & partition ) ;
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index 4b9d6854..8b33eed3 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -3786,12 +3786,13 @@ bool GParted_Core::update_dmraid_entry( const Partition & partition, OperationDe
        return success;
 }
 
-FileSystem * GParted_Core::get_filesystem_object( FSType filesystem )
+FileSystem * GParted_Core::get_filesystem_object( FSType fstype )
 {
-       if ( FILESYSTEM_MAP .count( filesystem ) )
-           return FILESYSTEM_MAP[ filesystem ] ;
+       std::map<FSType, FileSystem *>::const_iterator fs_iter = FILESYSTEM_MAP.find( fstype );
+       if ( fs_iter == FILESYSTEM_MAP.end() )
+               return NULL;
        else
-           return NULL ;
+               return fs_iter->second;
 }
 
 // Return true for file systems with an implementation class, false otherwise


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]