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.
    "
This commit is contained in:
Mike Fleetwood 2018-09-10 18:58:44 +01:00 committed by Curtis Gedak
parent d3ef32096a
commit 9c35d91453
2 changed files with 6 additions and 5 deletions

View File

@ -65,7 +65,7 @@ public:
Glib::ustring get_libparted_version() ; Glib::ustring get_libparted_version() ;
Glib::ustring get_thread_status_message() ; 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 bool supported_filesystem( FSType fstype );
static FS_Limits get_filesystem_limits( FSType fstype, const Partition & partition ); static FS_Limits get_filesystem_limits( FSType fstype, const Partition & partition );
static bool filesystem_resize_disallowed( const Partition & partition ) ; static bool filesystem_resize_disallowed( const Partition & partition ) ;

View File

@ -3786,12 +3786,13 @@ bool GParted_Core::update_dmraid_entry( const Partition & partition, OperationDe
return success; return success;
} }
FileSystem * GParted_Core::get_filesystem_object( FSType filesystem ) FileSystem * GParted_Core::get_filesystem_object( FSType fstype )
{ {
if ( FILESYSTEM_MAP .count( filesystem ) ) std::map<FSType, FileSystem *>::const_iterator fs_iter = FILESYSTEM_MAP.find( fstype );
return FILESYSTEM_MAP[ filesystem ] ; if ( fs_iter == FILESYSTEM_MAP.end() )
return NULL;
else else
return NULL ; return fs_iter->second;
} }
// Return true for file systems with an implementation class, false otherwise // Return true for file systems with an implementation class, false otherwise