source: calamares/trunk/fuentes/src/modules/partition/core/PartitionIterator.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 16 months ago

sync with github

File size: 3.4 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Aurélien Gâteau <agateau@kde.org>
4 *   Copyright 2015, Teo Mrnjavac <teo@kde.org>
5 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
6 *
7 *   Calamares is free software: you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation, either version 3 of the License, or
10 *   (at your option) any later version.
11 *
12 *   Calamares is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <core/PartitionIterator.h>
22
23// KPMcore
24#include <kpmcore/core/device.h>
25#include <kpmcore/core/partition.h>
26#include <kpmcore/core/partitiontable.h>
27
28PartitionIterator::PartitionIterator( PartitionTable* table )
29    : m_table( table )
30{}
31
32Partition*
33PartitionIterator::operator*() const
34{
35    return m_current;
36}
37
38void
39PartitionIterator::operator++()
40{
41    if ( !m_current )
42        return;
43    if ( m_current->hasChildren() )
44    {
45        // Go to the first child
46        m_current = static_cast< Partition* >( m_current->children().first() );
47        return;
48    }
49    PartitionNode* parent = m_current->parent();
50    Partition* successor = parent->successor( *m_current );
51    if ( successor )
52    {
53        // Go to the next sibling
54        m_current = successor;
55        return;
56    }
57    if ( parent->isRoot() )
58    {
59        // We reached the end
60        m_current = nullptr;
61        return;
62    }
63    // Try to go to the next sibling of our parent
64
65    PartitionNode* grandParent = parent->parent();
66    Q_ASSERT( grandParent );
67    // If parent is not root, then it's not a PartitionTable but a
68    // Partition, we can static_cast it.
69    m_current = grandParent->successor( *static_cast< Partition* >( parent ) );
70}
71
72bool
73PartitionIterator::operator==( const PartitionIterator& other ) const
74{
75    return m_table == other.m_table && m_current == other.m_current;
76}
77
78bool
79PartitionIterator::operator!=( const PartitionIterator& other ) const
80{
81    return ! ( *this == other );
82}
83
84PartitionIterator
85PartitionIterator::begin( Device* device )
86{
87    if ( !device )
88        return PartitionIterator( nullptr );
89    Q_ASSERT(device);
90    PartitionTable* table = device->partitionTable();
91    if ( !table )
92        return PartitionIterator( nullptr );
93    return PartitionIterator::begin( table );
94}
95
96PartitionIterator
97PartitionIterator::begin( PartitionTable* table )
98{
99    auto it = PartitionIterator( table );
100    QList< Partition* > children = table->children();
101    // Does not usually happen, but it did happen on a 10MB disk with an MBR
102    // partition table.
103    if ( children.isEmpty() )
104        return it;
105    it.m_current = children.first();
106    return it;
107}
108
109PartitionIterator
110PartitionIterator::end( Device* device )
111{
112    if ( !device )
113        return PartitionIterator( nullptr );
114    PartitionTable* table = device->partitionTable();
115    if ( !table )
116        return PartitionIterator( nullptr );
117
118    return PartitionIterator::end( table );
119}
120
121PartitionIterator
122PartitionIterator::end( PartitionTable* table )
123{
124    return PartitionIterator( table );
125}
Note: See TracBrowser for help on using the repository browser.