Move PartitionIterator to its own file

This commit is contained in:
Aurélien Gâteau 2014-07-22 14:25:21 +02:00
parent ed0fd6d207
commit 7763587c01
4 changed files with 153 additions and 80 deletions

View File

@ -31,6 +31,7 @@ calamares_add_plugin( partition
FormatPartitionJob.cpp FormatPartitionJob.cpp
PartitionCoreModule.cpp PartitionCoreModule.cpp
PartitionInfo.cpp PartitionInfo.cpp
PartitionIterator.cpp
PartitionJob.cpp PartitionJob.cpp
PartitionModel.cpp PartitionModel.cpp
PartitionPage.cpp PartitionPage.cpp

View File

@ -24,6 +24,7 @@
#include <FormatPartitionJob.h> #include <FormatPartitionJob.h>
#include <DeviceModel.h> #include <DeviceModel.h>
#include <PartitionInfo.h> #include <PartitionInfo.h>
#include <PartitionIterator.h>
#include <PartitionModel.h> #include <PartitionModel.h>
#include <PMUtils.h> #include <PMUtils.h>
#include <Typedefs.h> #include <Typedefs.h>
@ -39,86 +40,6 @@
// Qt // Qt
#include <QStandardItemModel> #include <QStandardItemModel>
class PartitionIterator
{
public:
Partition* operator*() const
{
return m_current;
}
void operator++()
{
if ( !m_current )
return;
if ( m_current->hasChildren() )
{
// Go to the first child
m_current = static_cast< Partition* >( m_current->children().first() );
return;
}
PartitionNode* parent = m_current->parent();
Partition* successor = parent->successor( *m_current );
if ( successor )
{
// Go to the next sibling
m_current = successor;
return;
}
if ( parent->isRoot() )
{
// We reached the end
m_current = nullptr;
return;
}
// Try to go to the next sibling of our parent
PartitionNode* grandParent = parent->parent();
Q_ASSERT( grandParent );
// If parent is not root, then it's not a PartitionTable but a
// Partition, we can static_cast it.
m_current = grandParent->successor( *static_cast< Partition* >( parent ) );
}
bool operator==( const PartitionIterator& other ) const
{
return m_device == other.m_device && m_current == other.m_current;
}
bool operator!=( const PartitionIterator& other ) const
{
return ! ( *this == other );
}
static PartitionIterator begin( Device* device )
{
auto it = PartitionIterator( device );
PartitionTable* table = device->partitionTable();
if ( !table )
return it;
QList< Partition* > children = table->children();
// Does not usually happen, but it did happen on a 10MB disk with an MBR
// partition table.
if ( children.isEmpty() )
return it;
it.m_current = children.first();
return it;
}
static PartitionIterator end( Device* device )
{
return PartitionIterator( device );
}
private:
PartitionIterator( Device* device )
: m_device( device )
{}
Device* m_device;
Partition* m_current = nullptr;
};
//- DeviceInfo --------------------------------------------- //- DeviceInfo ---------------------------------------------
PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device ) PartitionCoreModule::DeviceInfo::DeviceInfo( Device* _device )
: device( _device ) : device( _device )

View File

@ -0,0 +1,102 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
*
* Calamares 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 3 of the License, or
* (at your option) any later version.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#include <PartitionIterator.h>
// CalaPM
#include <core/device.h>
#include <core/partition.h>
#include <core/partitiontable.h>
PartitionIterator::PartitionIterator( Device* device )
: m_device( device )
{}
Partition*
PartitionIterator::operator*() const
{
return m_current;
}
void
PartitionIterator::operator++()
{
if ( !m_current )
return;
if ( m_current->hasChildren() )
{
// Go to the first child
m_current = static_cast< Partition* >( m_current->children().first() );
return;
}
PartitionNode* parent = m_current->parent();
Partition* successor = parent->successor( *m_current );
if ( successor )
{
// Go to the next sibling
m_current = successor;
return;
}
if ( parent->isRoot() )
{
// We reached the end
m_current = nullptr;
return;
}
// Try to go to the next sibling of our parent
PartitionNode* grandParent = parent->parent();
Q_ASSERT( grandParent );
// If parent is not root, then it's not a PartitionTable but a
// Partition, we can static_cast it.
m_current = grandParent->successor( *static_cast< Partition* >( parent ) );
}
bool
PartitionIterator::operator==( const PartitionIterator& other ) const
{
return m_device == other.m_device && m_current == other.m_current;
}
bool
PartitionIterator::operator!=( const PartitionIterator& other ) const
{
return ! ( *this == other );
}
PartitionIterator
PartitionIterator::begin( Device* device )
{
auto it = PartitionIterator( device );
PartitionTable* table = device->partitionTable();
if ( !table )
return it;
QList< Partition* > children = table->children();
// Does not usually happen, but it did happen on a 10MB disk with an MBR
// partition table.
if ( children.isEmpty() )
return it;
it.m_current = children.first();
return it;
}
PartitionIterator
PartitionIterator::end( Device* device )
{
return PartitionIterator( device );
}

View File

@ -0,0 +1,49 @@
/* === This file is part of Calamares - <http://github.com/calamares> ===
*
* Copyright 2014, Aurélien Gâteau <agateau@kde.org>
*
* Calamares 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 3 of the License, or
* (at your option) any later version.
*
* Calamares 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 Calamares. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PARTITIONITERATOR_H
#define PARTITIONITERATOR_H
class Device;
class Partition;
/**
* A forward-only iterator to go through the partitions of a device,
* independently of whether they are primary, logical or extended.
*/
class PartitionIterator
{
public:
Partition* operator*() const;
void operator++();
bool operator==( const PartitionIterator& other ) const;
bool operator!=( const PartitionIterator& other ) const;
static PartitionIterator begin( Device* device );
static PartitionIterator end( Device* device );
private:
PartitionIterator( Device* device );
Device* m_device;
Partition* m_current = nullptr;
};
#endif /* PARTITIONITERATOR_H */