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



commit fdbd86f1ead93f0bfa328466de9895e43fc2b131
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Tue Dec 1 15:59:56 2015 +0000

    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(+), 0 deletions(-)
---
diff --git a/include/PartitionVector.h b/include/PartitionVector.h
index f1d6c33..1f0f67a 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<Partition *> v;
diff --git a/src/PartitionVector.cc b/src/PartitionVector.cc
index f05bedf..c35ebf9 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


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