[gparted] Return and use constant reference from Device::get_path() (!94)



commit 21dd6fe00b89c63b4ca6a054055ead9fae480ed5
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Mon Nov 1 14:12:58 2021 +0000

    Return and use constant reference from Device::get_path() (!94)
    
    A number of GParted methods named get_*() are returning properties and
    are return-by-value.  For objects this implies the returned value is
    copy constructed and later destroyed when it goes out of scope.  Change
    them to use return-by-constant-reference to avoid unnecessary object
    duplication.  Just have to make sure the reference goes out of scope
    before the referenced object is destroyed to avoid having a reference
    (pointer) to freed memory.  Assigning to a local variable instead of
    of a local reference still duplicates the object so can be used when the
    lifetime of the gotten object needs to be longer that the referenced
    object.
    
    Previously done for other getters here:
        d948cbcb9143dc975dd3c66defd7289eb4113b2c
        Make get_custom_text() and get_generic_text() return by reference
    
    This change just makes Device::get_path() return a constant reference
    and updates uses to match to avoid copy constructing the returned value.
    
    Closes !94 - Make more getter methods use return-by-constant-reference

 include/Device.h        | 2 +-
 include/Win_GParted.h   | 2 +-
 src/Device.cc           | 3 ++-
 src/Dialog_Disklabel.cc | 2 +-
 src/GParted_Core.cc     | 2 +-
 src/Win_GParted.cc      | 3 ++-
 6 files changed, 8 insertions(+), 6 deletions(-)
---
diff --git a/include/Device.h b/include/Device.h
index 999e8d79..9d537e3f 100644
--- a/include/Device.h
+++ b/include/Device.h
@@ -37,7 +37,7 @@ public:
 
        Device get_copy_without_partitions() const;
        void set_path( const Glib::ustring & path );
-       Glib::ustring get_path() const ;
+       const Glib::ustring& get_path() const;
        void enable_partition_naming( int length );  // > 0 => enable partition naming support
        bool partition_naming_supported() const;
        int get_max_partition_name_length() const;
diff --git a/include/Win_GParted.h b/include/Win_GParted.h
index 9515357c..481c854e 100644
--- a/include/Win_GParted.h
+++ b/include/Win_GParted.h
@@ -163,7 +163,7 @@ private:
        void menu_gparted_quit();
        void menu_view_harddisk_info();
        void menu_view_operations();
-       void show_disklabel_unrecognized( Glib::ustring device_name );
+       void show_disklabel_unrecognized(const Glib::ustring& device_name);
        void show_resize_readonly( const Glib::ustring & path );
        void show_help(const Glib::ustring & filename, const Glib::ustring & link_id);
        void menu_help_contents();
diff --git a/src/Device.cc b/src/Device.cc
index 719ad5f7..08e116e8 100644
--- a/src/Device.cc
+++ b/src/Device.cc
@@ -66,7 +66,8 @@ void Device::set_path( const Glib::ustring & path )
        this->path = path;
 }
 
-Glib::ustring Device::get_path() const
+
+const Glib::ustring& Device::get_path() const
 {
        return path;
 }
diff --git a/src/Dialog_Disklabel.cc b/src/Dialog_Disklabel.cc
index 1bb3c04a..574fc554 100644
--- a/src/Dialog_Disklabel.cc
+++ b/src/Dialog_Disklabel.cc
@@ -25,7 +25,7 @@ namespace GParted
 
 Dialog_Disklabel::Dialog_Disklabel( const Device & device )
 {
-       const Glib::ustring device_path = device .get_path() ;
+       const Glib::ustring& device_path = device.get_path();
 
        /*TO TRANSLATORS: dialogtitle, looks like Create partition table on /dev/hda */
        this ->set_title( Glib::ustring::compose( _("Create partition table on %1"), device_path ) );
diff --git a/src/GParted_Core.cc b/src/GParted_Core.cc
index 27eb6a1d..b247105a 100644
--- a/src/GParted_Core.cc
+++ b/src/GParted_Core.cc
@@ -474,7 +474,7 @@ bool GParted_Core::apply_operation_to_disk( Operation * operation )
 
 bool GParted_Core::set_disklabel( const Device & device, const Glib::ustring & disklabel )
 {
-       Glib::ustring device_path = device.get_path();
+       const Glib::ustring& device_path = device.get_path();
 
        // FIXME: Should call file system specific removal actions
        // (to remove LVM2 PVs before deleting the partitions).
diff --git a/src/Win_GParted.cc b/src/Win_GParted.cc
index c836529b..a820bd25 100644
--- a/src/Win_GParted.cc
+++ b/src/Win_GParted.cc
@@ -1760,7 +1760,8 @@ void Win_GParted::menu_view_operations()
                close_operationslist() ;
 }
 
-void Win_GParted::show_disklabel_unrecognized ( Glib::ustring device_name )
+
+void Win_GParted::show_disklabel_unrecognized (const Glib::ustring& device_name)
 {
        //Display dialog box indicating that no partition table was found on the device
        Gtk::MessageDialog dialog( *this,


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