| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/layers/layer_list_iterator.h" | 5 #include "cc/layers/layer_list_iterator.h" |
| 6 | 6 |
| 7 #include "cc/layers/layer.h" |
| 7 #include "cc/layers/layer_impl.h" | 8 #include "cc/layers/layer_impl.h" |
| 8 | 9 |
| 9 namespace cc { | 10 namespace cc { |
| 10 | 11 |
| 11 LayerListIterator::LayerListIterator(LayerImpl* root_layer) | 12 template <typename LayerType> |
| 13 LayerListIterator<LayerType>::LayerListIterator(LayerType* root_layer) |
| 12 : current_layer_(root_layer) { | 14 : current_layer_(root_layer) { |
| 13 DCHECK(!root_layer || !root_layer->parent()); | 15 DCHECK(!root_layer || !root_layer->parent()); |
| 14 list_indices_.push_back(0); | 16 list_indices_.push_back(0); |
| 15 } | 17 } |
| 16 | 18 |
| 17 LayerListIterator::LayerListIterator(const LayerListIterator& other) = default; | 19 template <typename LayerType> |
| 20 LayerListIterator<LayerType>::LayerListIterator( |
| 21 const LayerListIterator<LayerType>& other) = default; |
| 18 | 22 |
| 19 LayerListIterator::~LayerListIterator() {} | 23 template <typename LayerType> |
| 24 LayerListIterator<LayerType>::~LayerListIterator() {} |
| 20 | 25 |
| 21 LayerListIterator& LayerListIterator::operator++() { | 26 template <typename LayerType> |
| 27 LayerListIterator<LayerType>& LayerListIterator<LayerType>::operator++() { |
| 22 // case 0: done | 28 // case 0: done |
| 23 if (!current_layer_) | 29 if (!current_layer_) |
| 24 return *this; | 30 return *this; |
| 25 | 31 |
| 26 // case 1: descend. | 32 // case 1: descend. |
| 27 const LayerImplList& current_list = current_layer_->children(); | 33 if (!current_layer_->children().empty()) { |
| 28 if (!current_list.empty()) { | 34 current_layer_ = current_layer_->child_at(0); |
| 29 current_layer_ = current_list[0]; | |
| 30 list_indices_.push_back(0); | 35 list_indices_.push_back(0); |
| 31 return *this; | 36 return *this; |
| 32 } | 37 } |
| 33 | 38 |
| 34 for (LayerImpl* parent = current_layer_->parent(); parent; | 39 for (LayerType* parent = current_layer_->parent(); parent; |
| 35 parent = parent->parent()) { | 40 parent = parent->parent()) { |
| 36 // We now try and advance in some list of siblings. | 41 // We now try and advance in some list of siblings. |
| 37 const LayerImplList& sibling_list = parent->children(); | |
| 38 | |
| 39 // case 2: Advance to a sibling. | 42 // case 2: Advance to a sibling. |
| 40 if (list_indices_.back() + 1 < sibling_list.size()) { | 43 if (list_indices_.back() + 1 < parent->children().size()) { |
| 41 ++list_indices_.back(); | 44 ++list_indices_.back(); |
| 42 current_layer_ = sibling_list[list_indices_.back()]; | 45 current_layer_ = parent->child_at(list_indices_.back()); |
| 43 return *this; | 46 return *this; |
| 44 } | 47 } |
| 45 | 48 |
| 46 // We need to ascend. We will pop an index off the stack. | 49 // We need to ascend. We will pop an index off the stack. |
| 47 list_indices_.pop_back(); | 50 list_indices_.pop_back(); |
| 48 } | 51 } |
| 49 | 52 |
| 50 current_layer_ = nullptr; | 53 current_layer_ = nullptr; |
| 51 return *this; | 54 return *this; |
| 52 } | 55 } |
| 53 | 56 |
| 54 LayerListReverseIterator::LayerListReverseIterator(LayerImpl* root_layer) | 57 template <typename LayerType> |
| 55 : LayerListIterator(root_layer) { | 58 LayerListReverseIterator<LayerType>::LayerListReverseIterator( |
| 59 LayerType* root_layer) |
| 60 : LayerListIterator<LayerType>(root_layer) { |
| 56 DescendToRightmostInSubtree(); | 61 DescendToRightmostInSubtree(); |
| 57 } | 62 } |
| 58 | 63 |
| 59 LayerListReverseIterator::~LayerListReverseIterator() {} | 64 template <typename LayerType> |
| 65 LayerListReverseIterator<LayerType>::~LayerListReverseIterator() {} |
| 60 | 66 |
| 61 // We will only support prefix increment. | 67 // We will only support prefix increment. |
| 62 LayerListIterator& LayerListReverseIterator::operator++() { | 68 template <typename LayerType> |
| 69 LayerListIterator<LayerType>& LayerListReverseIterator<LayerType>:: |
| 70 operator++() { |
| 63 // case 0: done | 71 // case 0: done |
| 64 if (!current_layer_) | 72 if (!current_layer()) |
| 65 return *this; | 73 return *this; |
| 66 | 74 |
| 67 // case 1: we're the leftmost sibling. | 75 // case 1: we're the leftmost sibling. |
| 68 if (!list_indices_.back()) { | 76 if (!list_indices().back()) { |
| 69 list_indices_.pop_back(); | 77 list_indices().pop_back(); |
| 70 current_layer_ = current_layer_->parent(); | 78 this->current_layer_ = current_layer()->parent(); |
| 71 return *this; | 79 return *this; |
| 72 } | 80 } |
| 73 | 81 |
| 74 // case 2: we're not the leftmost sibling. In this case, we want to move one | 82 // case 2: we're not the leftmost sibling. In this case, we want to move one |
| 75 // sibling over, and then descend to the rightmost descendant in that subtree. | 83 // sibling over, and then descend to the rightmost descendant in that subtree. |
| 76 CHECK(current_layer_->parent()); | 84 CHECK(current_layer()->parent()); |
| 77 --list_indices_.back(); | 85 --list_indices().back(); |
| 78 const LayerImplList& parent_list = current_layer_->parent()->children(); | 86 this->current_layer_ = |
| 79 current_layer_ = parent_list[list_indices_.back()]; | 87 current_layer()->parent()->child_at(list_indices().back()); |
| 80 DescendToRightmostInSubtree(); | 88 DescendToRightmostInSubtree(); |
| 81 return *this; | 89 return *this; |
| 82 } | 90 } |
| 83 | 91 |
| 84 void LayerListReverseIterator::DescendToRightmostInSubtree() { | 92 template <typename LayerType> |
| 85 if (!current_layer_) | 93 void LayerListReverseIterator<LayerType>::DescendToRightmostInSubtree() { |
| 94 if (!current_layer()) |
| 86 return; | 95 return; |
| 87 | 96 |
| 88 const LayerImplList& current_list = current_layer_->children(); | 97 if (current_layer()->children().empty()) |
| 89 if (current_list.empty()) | |
| 90 return; | 98 return; |
| 91 | 99 |
| 92 size_t last_index = current_list.size() - 1; | 100 size_t last_index = current_layer()->children().size() - 1; |
| 93 current_layer_ = current_list[last_index]; | 101 this->current_layer_ = current_layer()->child_at(last_index); |
| 94 list_indices_.push_back(last_index); | 102 list_indices().push_back(last_index); |
| 95 DescendToRightmostInSubtree(); | 103 DescendToRightmostInSubtree(); |
| 96 } | 104 } |
| 97 | 105 |
| 106 template class LayerListIterator<Layer>; |
| 107 template class LayerListIterator<LayerImpl>; |
| 108 template class LayerListReverseIterator<Layer>; |
| 109 template class LayerListReverseIterator<LayerImpl>; |
| 110 |
| 98 } // namespace cc | 111 } // namespace cc |
| OLD | NEW |