Encapsulate operation merging inside the Operation* classes (#755214)

Win_GParted::Merge_Operations() method was modifying the internals of
Operation* objects; in particular the partition_new member variable.
This is breaking data hiding and encapsulation tenant of object oriented
programming.

Implement exactly the same operation merge semantics, but hide the
manipulation of the internals of the Operation* objects within the
Operation* classes themselves.

Bug 755214 - Refactor operation merging
This commit is contained in:
Mike Fleetwood 2015-09-12 14:59:40 +01:00 committed by Curtis Gedak
parent 29a1f20688
commit 7f4ffd28d5
20 changed files with 109 additions and 77 deletions

View file

@ -45,6 +45,7 @@ public:
virtual void apply_to_visual( std::vector<Partition> & partitions ) = 0 ;
virtual void create_description() = 0 ;
virtual bool merge_operations( const Operation & candidate ) = 0;
//public variables
Device device ;

View file

@ -34,6 +34,7 @@ void apply_to_visual( std::vector<Partition> & partitions ) ;
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
} ;
} //GParted

View file

@ -31,6 +31,7 @@ public:
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
} ;
} //GParted

View file

@ -36,6 +36,7 @@ public:
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
} ;
} //GParted

View file

@ -33,6 +33,7 @@ public:
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
} ;
} //GParted

View file

@ -31,6 +31,7 @@ public:
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
void remove_original_and_adjacent_unallocated( std::vector<Partition> & partitions, int index_orig ) ;
} ;

View file

@ -33,6 +33,7 @@ public:
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
} ;
} //GParted

View file

@ -33,6 +33,7 @@ public:
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
} ;
} //GParted

View file

@ -33,6 +33,7 @@ public:
private:
void create_description();
bool merge_operations( const Operation & candidate );
};
} //GParted

View file

@ -33,7 +33,8 @@ public:
private:
void create_description() ;
bool merge_operations( const Operation & candidate );
void apply_normal_to_visual( std::vector<Partition> & partitions ) ;
void apply_extended_to_visual( std::vector<Partition> & partitions ) ;

View file

@ -69,4 +69,23 @@ void OperationChangeUUID::create_description()
}
}
bool OperationChangeUUID::merge_operations( const Operation & candidate )
{
if ( candidate.type == OPERATION_CHANGE_UUID &&
partition_new == candidate.partition_original )
{
// Changing half the UUID must not override changing all of it
if ( candidate.partition_new.uuid != UUID_RANDOM_NTFS_HALF )
{
partition_new.uuid = candidate.partition_new.uuid;
create_description();
}
// Else no change required to this operation to merge candidate
return true;
}
return false;
}
} //GParted

View file

@ -39,5 +39,14 @@ void OperationCheck::create_description()
partition_original .get_path() ) ;
}
} //GParted
bool OperationCheck::merge_operations( const Operation & candidate )
{
if ( candidate.type == OPERATION_CHECK &&
partition_original == candidate.partition_original )
// No steps required to merge this operation
return true;
return false;
}
} //GParted

View file

@ -90,5 +90,9 @@ void OperationCopy::create_description()
}
}
} //GParted
bool OperationCopy::merge_operations( const Operation & candidate )
{
return false; // Never merge copy operations
}
} //GParted

View file

@ -92,5 +92,9 @@ void OperationCreate::create_description()
device .get_path() ) ;
}
} //GParted
bool OperationCreate::merge_operations( const Operation & candidate )
{
return false; // Never merge create operations
}
} //GParted

View file

@ -84,7 +84,12 @@ void OperationDelete::create_description()
Utils::format_size( partition_original .get_sector_length(), partition_original .sector_size ),
partition_original .device_path ) ;
}
bool OperationDelete::merge_operations( const Operation & candidate )
{
return false; // Can't merge with an already deleted partition
}
void OperationDelete::remove_original_and_adjacent_unallocated( std::vector<Partition> & partitions, int index_orig )
{
//remove unallocated space following the original partition
@ -101,4 +106,3 @@ void OperationDelete::remove_original_and_adjacent_unallocated( std::vector<Part
}
} //GParted

View file

@ -74,5 +74,17 @@ void OperationFormat::create_description()
Utils::get_filesystem_string( partition_new .filesystem ) ) ;
}
} //GParted
bool OperationFormat::merge_operations( const Operation & candidate )
{
if ( candidate.type == OPERATION_FORMAT &&
partition_new == candidate.partition_original )
{
partition_new = candidate.partition_new;
create_description();
return true;
}
return false;
}
} //GParted

View file

@ -65,4 +65,17 @@ void OperationLabelFileSystem::create_description()
}
}
bool OperationLabelFileSystem::merge_operations( const Operation & candidate )
{
if ( candidate.type == OPERATION_LABEL_FILESYSTEM &&
partition_new == candidate.partition_original )
{
partition_new.set_filesystem_label( candidate.partition_new.get_filesystem_label() );
create_description();
return true;
}
return false;
}
} //GParted

View file

@ -68,4 +68,17 @@ void OperationNamePartition::create_description()
}
}
bool OperationNamePartition::merge_operations( const Operation & candidate )
{
if ( candidate.type == OPERATION_NAME_PARTITION &&
partition_new == candidate.partition_original )
{
partition_new.name = candidate.partition_new.name;
create_description();
return true;
}
return false;
}
} //GParted

View file

@ -208,4 +208,17 @@ void OperationResizeMove::remove_adjacent_unallocated( std::vector<Partition> &
partitions .erase( partitions .begin() + ( index_orig -1 ) ) ;
}
bool OperationResizeMove::merge_operations( const Operation & candidate )
{
if ( candidate.type == OPERATION_RESIZE_MOVE &&
partition_new == candidate.partition_original )
{
partition_new = candidate.partition_new;
create_description();
return true;
}
return false;
}
} //GParted

View file

@ -746,78 +746,9 @@ bool Win_GParted::Merge_Operations( unsigned int first, unsigned int second )
if( first >= operations .size() || second >= operations .size() )
return false;
// Two resize operations of the same partition
if ( operations[ first ]->type == OPERATION_RESIZE_MOVE &&
operations[ second ]->type == OPERATION_RESIZE_MOVE &&
operations[ first ]->partition_new == operations[ second ]->partition_original
)
{
operations[ first ]->partition_new = operations[ second ]->partition_new;
operations[ first ]->create_description() ;
remove_operation( second );
return true;
}
// Two label change operations on the same partition
else if ( operations[ first ]->type == OPERATION_LABEL_FILESYSTEM &&
operations[ second ]->type == OPERATION_LABEL_FILESYSTEM &&
operations[ first ]->partition_new == operations[ second ]->partition_original
)
{
operations[ first ]->partition_new.set_filesystem_label(
operations[ second ]->partition_new.get_filesystem_label() );
operations[ first ]->create_description() ;
remove_operation( second );
return true;
}
// Two name change operations on the same partition
else if ( operations[first]->type == OPERATION_NAME_PARTITION &&
operations[second]->type == OPERATION_NAME_PARTITION &&
operations[first]->partition_new == operations[second]->partition_original
)
{
operations[first]->partition_new.name = operations[second]->partition_new.name;
operations[first]->create_description();
remove_operation( second );
return true;
}
// Two change-uuid change operations on the same partition
else if ( operations[ first ]->type == OPERATION_CHANGE_UUID &&
operations[ second ]->type == OPERATION_CHANGE_UUID &&
operations[ first ]->partition_new == operations[ second ]->partition_original
)
{
// Changing half the UUID should not override changing all of it
if ( operations[ first ]->partition_new.uuid == UUID_RANDOM_NTFS_HALF ||
operations[ second ]->partition_new.uuid == UUID_RANDOM )
operations[ first ]->partition_new.uuid = operations[ second ]->partition_new.uuid;
operations[ first ]->create_description() ;
remove_operation( second );
return true;
}
// Two check operations of the same partition
else if ( operations[ first ]->type == OPERATION_CHECK &&
operations[ second ]->type == OPERATION_CHECK &&
operations[ first ]->partition_original == operations[ second ]->partition_original
)
if ( operations[first]->merge_operations( *operations[second] ) )
{
remove_operation( second );
return true;
}
// Two format operations of the same partition
else if ( operations[ first ]->type == OPERATION_FORMAT &&
operations[ second ]->type == OPERATION_FORMAT &&
operations[ first ]->partition_new == operations[ second ]->partition_original
)
{
operations[ first ]->partition_new = operations[ second ]->partition_new;
operations[ first ]->create_description() ;
remove_operation( second );
return true;
}