From fdbd86f1ead93f0bfa328466de9895e43fc2b131 Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Tue, 1 Dec 2015 15:59:56 +0000 Subject: [PATCH] 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 --- include/PartitionVector.h | 2 ++ src/PartitionVector.cc | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/include/PartitionVector.h b/include/PartitionVector.h index f1d6c33e..1f0f67a7 100644 --- a/include/PartitionVector.h +++ b/include/PartitionVector.h @@ -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 v; diff --git a/src/PartitionVector.cc b/src/PartitionVector.cc index f05bedfd..c35ebf9d 100644 --- a/src/PartitionVector.cc +++ b/src/PartitionVector.cc @@ -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