gparted/include/PartitionVector.h
Mike Fleetwood 06b8a3a14a Use pointers to Partitions in PartitionVector class (#759726)
The PartitionVector class is now internally using pointers to Partition
objects and taking on management of their lifetimes.  It therefore has
to implement the Big 3: destructor, copy constructor and copy assignment
operator [1][2].  This is because the implicitly-defined copy
constructor and assignment operator perform memberwise "shallow copying"
and the destructor does nothing.  This not correct for classes which
contain non-class types such as raw pointers.

The semantics of the interface still copies each Partition object into
the PartitionVector when they are added with push_back() and insert().

Note that a PartitionVector object is explicitly copy assigned in
Win_GParted::Refresh_Visual().  They are also implicitly copied when
(1) the implementing vector is resized larger to allow it to hold more
pointers to Partition objects than it previously had capacity for; and
(2) a Partition object is copied including the logicals PartitionVector
member.

[1] The rule of three/five/zero
    http://en.cppreference.com/w/cpp/language/rule_of_three
[2] Rule of Three
    https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00

82 lines
2.8 KiB
C++

/* Copyright (C) 2015 Mike Fleetwood
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/* Minimal implementation of a class with some behaviours like a std::vector<Partition>.
* However internally the class manages pointers to Partition objects allowing for
* Partition object polymorphism.
* Reference:
* C++ Reference to std::vector
* http://www.cplusplus.com/reference/vector/vector/
*/
#ifndef GPARTED_PARTITIONVECTOR_H
#define GPARTED_PARTITIONVECTOR_H
#include "../include/Partition.h"
#include <cstddef>
#include <vector>
namespace GParted
{
class Partition; // Forward declarations as Partition and PartitionVector are
class PartitionVector; // mutually recursive classes.
// References:
// Mutually recursive classes
// http://stackoverflow.com/questions/3410637/mutually-recursive-classes
// recursive definition in CPP
// http://stackoverflow.com/questions/4300420/recursive-definition-in-cpp
class PartitionVector {
public:
typedef size_t size_type;
typedef std::vector<Partition *>::iterator iterator;
PartitionVector() {};
PartitionVector( const PartitionVector & src );
~PartitionVector();
void swap( PartitionVector & other );
PartitionVector & operator=( PartitionVector rhs );
// Iterators
iterator begin() { return v.begin(); };
// Capacity
bool empty() const { return v.empty(); };
// Element access
Partition & operator[]( size_type n ) { return *v[n]; };
const Partition & operator[]( size_type n ) const { return *v[n]; };
size_type size() const { return v.size(); };
const Partition & front() const { return *v.front(); };
const Partition & back() const { return *v.back(); };
// Modifiers
void pop_back();
void erase( const iterator position );
void clear();
void push_back( const Partition & partition );
void insert( iterator position, const Partition & partition );
private:
std::vector<Partition *> v;
};
} //GParted
#endif /* GPARTED_PARTITIONVECTOR_H */