| 1 |
/* |
| 2 |
* CoreAttributesTreeIterator.h - TaskJuggler |
| 3 |
* |
| 4 |
* SPDX-FileCopyrightText: 2001, 2002, 2003, 2004 Chris Schlaeger <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of version 2 of the GNU General Public License as |
| 8 |
* published by the Free Software Foundation. |
| 9 |
* |
| 10 |
* $Id$ |
| 11 |
*/ |
| 12 |
|
| 13 |
#ifndef _CoreAttributesTreeIterator_h_ |
| 14 |
#define _CoreAttributesTreeIterator_h_ |
| 15 |
|
| 16 |
namespace TJ |
| 17 |
{ |
| 18 |
|
| 19 |
enum IterationMode { leavesOnly = 0, parentAfterLeaves }; |
| 20 |
|
| 21 |
class CoreAttributes; |
| 22 |
|
| 23 |
template <class T> |
| 24 |
class CoreAttributesTreeIteratorT |
| 25 |
{ |
| 26 |
public: |
| 27 |
|
| 28 |
CoreAttributesTreeIteratorT(T* root, IterationMode m = leavesOnly); |
| 29 |
~CoreAttributesTreeIteratorT() { } |
| 30 |
|
| 31 |
T* operator*() { return current; } |
| 32 |
T* operator++(); |
| 33 |
|
| 34 |
protected: |
| 35 |
T* current; |
| 36 |
private: |
| 37 |
IterationMode iMode; |
| 38 |
T* root; |
| 39 |
} ; |
| 40 |
|
| 41 |
template <class T> |
| 42 |
CoreAttributesTreeIteratorT<T>::CoreAttributesTreeIteratorT(T* r, |
| 43 |
IterationMode m) : |
| 44 |
current(r), |
| 45 |
iMode(m), |
| 46 |
root(r) |
| 47 |
{ |
| 48 |
while (current->hasSubs()) |
| 49 |
current = current->getSubList().first(); |
| 50 |
} |
| 51 |
|
| 52 |
template <class T> |
| 53 |
T* |
| 54 |
CoreAttributesTreeIteratorT<T>::operator++() |
| 55 |
{ |
| 56 |
if (current == nullptr) |
| 57 |
return nullptr; |
| 58 |
|
| 59 |
while (current != root) |
| 60 |
{ |
| 61 |
// Find the current CA in the parent's sub list. |
| 62 |
CoreAttributesListIterator |
| 63 |
cli(current->getParent()->getSubListIterator()); |
| 64 |
for (; cli.hasNext() && cli.peekNext() != current; cli.next()) |
| 65 |
; |
| 66 |
// Check if there is another task in the sub list. |
| 67 |
if (cli.hasNext()) |
| 68 |
{ |
| 69 |
// Find the first leaf in this sub list. |
| 70 |
current = cli.next(); |
| 71 |
while (current->hasSubs()) |
| 72 |
current = current->getSubList().first(); |
| 73 |
// This is the new current task. |
| 74 |
return current; |
| 75 |
} |
| 76 |
// End of sub list reached. Try parent node then. |
| 77 |
current = current->getParent(); |
| 78 |
if (iMode == parentAfterLeaves) |
| 79 |
return current; |
| 80 |
} |
| 81 |
return (current = nullptr); |
| 82 |
} |
| 83 |
|
| 84 |
typedef CoreAttributesTreeIteratorT<CoreAttributes> CoreAttributesTreeIterator; |
| 85 |
typedef CoreAttributesTreeIteratorT<const CoreAttributes> |
| 86 |
ConstCoreAttributesTreeIterator; |
| 87 |
|
| 88 |
} // namespace TJ |
| 89 |
|
| 90 |
#endif |