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:
    d948cbcb91
    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
This commit is contained in:
Mike Fleetwood 2021-11-01 14:12:58 +00:00 committed by Curtis Gedak
parent 17e451294a
commit 21dd6fe00b
6 changed files with 8 additions and 6 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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;
}

View File

@ -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 ) );

View File

@ -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).

View File

@ -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,