/* * Copyright (C) 2013 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef ElementChildIterator_h #define ElementChildIterator_h #include "ElementIterator.h" namespace WebCore { template class ElementChildIterator : public ElementIterator { public: typedef ElementType value_type; typedef ptrdiff_t difference_type; typedef ElementType* pointer; typedef ElementType& reference; typedef std::forward_iterator_tag iterator_category; ElementChildIterator(const ContainerNode& parent); ElementChildIterator(const ContainerNode& parent, ElementType* current); ElementChildIterator& operator--(); ElementChildIterator& operator++(); }; template class ElementChildConstIterator : public ElementConstIterator { public: typedef const ElementType value_type; typedef ptrdiff_t difference_type; typedef const ElementType* pointer; typedef const ElementType& reference; typedef std::forward_iterator_tag iterator_category; ElementChildConstIterator(const ContainerNode& parent); ElementChildConstIterator(const ContainerNode& parent, const ElementType* current); ElementChildConstIterator& operator--(); ElementChildConstIterator& operator++(); }; template class ElementChildIteratorAdapter { public: ElementChildIteratorAdapter(ContainerNode& parent); ElementChildIterator begin(); ElementChildIterator end(); ElementChildIterator beginAt(ElementType&); ElementType* first(); ElementType* last(); private: ContainerNode& m_parent; }; template class ElementChildConstIteratorAdapter { public: ElementChildConstIteratorAdapter(const ContainerNode& parent); ElementChildConstIterator begin() const; ElementChildConstIterator end() const; ElementChildConstIterator beginAt(const ElementType&) const; const ElementType* first() const; const ElementType* last() const; private: const ContainerNode& m_parent; }; template ElementChildIteratorAdapter childrenOfType(ContainerNode&); template ElementChildConstIteratorAdapter childrenOfType(const ContainerNode&); // ElementChildIterator template inline ElementChildIterator::ElementChildIterator(const ContainerNode& parent) : ElementIterator(&parent) { } template inline ElementChildIterator::ElementChildIterator(const ContainerNode& parent, ElementType* current) : ElementIterator(&parent, current) { } template inline ElementChildIterator& ElementChildIterator::operator--() { return static_cast&>(ElementIterator::traversePreviousSibling()); } template inline ElementChildIterator& ElementChildIterator::operator++() { return static_cast&>(ElementIterator::traverseNextSibling()); } // ElementChildConstIterator template inline ElementChildConstIterator::ElementChildConstIterator(const ContainerNode& parent) : ElementConstIterator(&parent) { } template inline ElementChildConstIterator::ElementChildConstIterator(const ContainerNode& parent, const ElementType* current) : ElementConstIterator(&parent, current) { } template inline ElementChildConstIterator& ElementChildConstIterator::operator--() { return static_cast&>(ElementConstIterator::traversePreviousSibling()); } template inline ElementChildConstIterator& ElementChildConstIterator::operator++() { return static_cast&>(ElementConstIterator::traverseNextSibling()); } // ElementChildIteratorAdapter template inline ElementChildIteratorAdapter::ElementChildIteratorAdapter(ContainerNode& parent) : m_parent(parent) { } template inline ElementChildIterator ElementChildIteratorAdapter::begin() { return ElementChildIterator(m_parent, Traversal::firstChild(m_parent)); } template inline ElementChildIterator ElementChildIteratorAdapter::end() { return ElementChildIterator(m_parent); } template inline ElementType* ElementChildIteratorAdapter::first() { return Traversal::firstChild(m_parent); } template inline ElementType* ElementChildIteratorAdapter::last() { return Traversal::lastChild(m_parent); } template inline ElementChildIterator ElementChildIteratorAdapter::beginAt(ElementType& child) { ASSERT(child.parentNode() == &m_parent); return ElementChildIterator(m_parent, &child); } // ElementChildConstIteratorAdapter template inline ElementChildConstIteratorAdapter::ElementChildConstIteratorAdapter(const ContainerNode& parent) : m_parent(parent) { } template inline ElementChildConstIterator ElementChildConstIteratorAdapter::begin() const { return ElementChildConstIterator(m_parent, Traversal::firstChild(m_parent)); } template inline ElementChildConstIterator ElementChildConstIteratorAdapter::end() const { return ElementChildConstIterator(m_parent); } template inline const ElementType* ElementChildConstIteratorAdapter::first() const { return Traversal::firstChild(m_parent); } template inline const ElementType* ElementChildConstIteratorAdapter::last() const { return Traversal::lastChild(m_parent); } template inline ElementChildConstIterator ElementChildConstIteratorAdapter::beginAt(const ElementType& child) const { ASSERT(child.parentNode() == &m_parent); return ElementChildConstIterator(m_parent, &child); } // Standalone functions template inline ElementChildIteratorAdapter childrenOfType(ContainerNode& parent) { return ElementChildIteratorAdapter(parent); } template inline ElementChildConstIteratorAdapter childrenOfType(const ContainerNode& parent) { return ElementChildConstIteratorAdapter(parent); } } #endif