Add adoption methods for adding items into a PartitionVector (#759726)

The current code uses push_back() and insert() to copy Partition objects
into the vector of pointers.  This has a few issues:
1) Unnecessary copying of Partition objects;
2) Hides the nature of the PartitionVector class as a manager of
   pointers to Partition objects by providing copy semantics to add
   items.  It is generally better to be explicit;
3) C++ doesn't provide polymorphic copy construction directly, but this
   is easily worked around by following the Virtual Constructor idiom
   [1], which would allow PartitionLUKS derived class objects to be
   copied into the vector.

Add push_back_adopt() and insert_adopt() methods which add a pointer to
a Partition object into the PartitionVector adopting ownership.

[1] Wikibooks: More C++ Idioms / Virtual Constructor
    https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Virtual_Constructor

Bug 759726 - Implement Partition object polymorphism
This commit is contained in:
Mike Fleetwood 2015-12-01 15:59:56 +00:00 committed by Curtis Gedak
parent 06b8a3a14a
commit fdbd86f1ea
2 changed files with 12 additions and 0 deletions

View File

@ -71,6 +71,8 @@ public:
void clear();
void push_back( const Partition & partition );
void insert( iterator position, const Partition & partition );
void push_back_adopt( Partition * partition );
void insert_adopt( iterator position, Partition * partition );
private:
std::vector<Partition *> v;

View File

@ -85,4 +85,14 @@ void PartitionVector::insert( iterator position, const Partition & partition )
v.insert( position, p );
}
void PartitionVector::push_back_adopt( Partition * partition )
{
v.push_back( partition );
}
void PartitionVector::insert_adopt( iterator position, Partition * partition )
{
v.insert( position, partition );
}
} //GParted