1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004-2017 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#pragma once
25
26#include "CollectionIndexCache.h"
27#include "NodeList.h"
28#include <wtf/IsoMalloc.h>
29#include <wtf/Ref.h>
30
31namespace WebCore {
32
33class ContainerNode;
34
35class EmptyNodeList final : public NodeList {
36 WTF_MAKE_ISO_ALLOCATED(EmptyNodeList);
37public:
38 static Ref<EmptyNodeList> create(Node& owner)
39 {
40 return adoptRef(*new EmptyNodeList(owner));
41 }
42 virtual ~EmptyNodeList();
43
44 Node& ownerNode() { return m_owner; }
45
46private:
47 explicit EmptyNodeList(Node& owner) : m_owner(owner) { }
48
49 unsigned length() const override { return 0; }
50 Node* item(unsigned) const override { return nullptr; }
51 size_t memoryCost() const override { return 0; }
52
53 bool isEmptyNodeList() const override { return true; }
54
55 Ref<Node> m_owner;
56};
57
58class ChildNodeList final : public NodeList {
59 WTF_MAKE_ISO_ALLOCATED(ChildNodeList);
60public:
61 static Ref<ChildNodeList> create(ContainerNode& parent)
62 {
63 return adoptRef(*new ChildNodeList(parent));
64 }
65
66 virtual ~ChildNodeList();
67
68 ContainerNode& ownerNode() { return m_parent; }
69
70 void invalidateCache();
71
72 // For CollectionIndexCache
73 Node* collectionBegin() const;
74 Node* collectionLast() const;
75 Node* collectionEnd() const { return nullptr; }
76 void collectionTraverseForward(Node*&, unsigned count, unsigned& traversedCount) const;
77 void collectionTraverseBackward(Node*&, unsigned count) const;
78 bool collectionCanTraverseBackward() const { return true; }
79 void willValidateIndexCache() const { }
80
81private:
82 explicit ChildNodeList(ContainerNode& parent);
83
84 unsigned length() const override;
85 Node* item(unsigned index) const override;
86 size_t memoryCost() const override
87 {
88 // memoryCost() may be invoked concurrently from a GC thread, and we need to be careful
89 // about what data we access here and how. Accessing m_indexCache is safe because
90 // because it doesn't involve any pointer chasing.
91 return m_indexCache.memoryCost();
92 }
93
94 bool isChildNodeList() const override { return true; }
95
96 Ref<ContainerNode> m_parent;
97 mutable CollectionIndexCache<ChildNodeList, Node*> m_indexCache;
98};
99
100} // namespace WebCore
101