1 | /* |
2 | * Copyright (C) 2013-2019 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/DumbPtrTraits.h> |
29 | #include <wtf/FastMalloc.h> |
30 | #include <wtf/Noncopyable.h> |
31 | #include <wtf/Packed.h> |
32 | |
33 | namespace WTF { |
34 | |
35 | namespace Private { |
36 | |
37 | template<typename T, typename PassedPtrTraits = DumbPtrTraits<T>> |
38 | class BagNode { |
39 | WTF_MAKE_FAST_ALLOCATED; |
40 | public: |
41 | using PtrTraits = typename PassedPtrTraits::template RebindTraits<BagNode>; |
42 | |
43 | template<typename... Args> |
44 | BagNode(Args&&... args) |
45 | : m_item(std::forward<Args>(args)...) |
46 | { } |
47 | |
48 | T m_item; |
49 | typename PtrTraits::StorageType m_next { nullptr }; |
50 | }; |
51 | |
52 | } // namespace Private |
53 | |
54 | template<typename T, typename PassedPtrTraits = DumbPtrTraits<T>> |
55 | class Bag { |
56 | WTF_MAKE_NONCOPYABLE(Bag); |
57 | WTF_MAKE_FAST_ALLOCATED; |
58 | using Node = Private::BagNode<T, PassedPtrTraits>; |
59 | using PtrTraits = typename PassedPtrTraits::template RebindTraits<Node>; |
60 | |
61 | public: |
62 | Bag() = default; |
63 | |
64 | template<typename U> |
65 | Bag(Bag<T, U>&& other) |
66 | { |
67 | ASSERT(!m_head); |
68 | m_head = other.unwrappedHead(); |
69 | other.m_head = nullptr; |
70 | } |
71 | |
72 | ~Bag() |
73 | { |
74 | clear(); |
75 | } |
76 | |
77 | void clear() |
78 | { |
79 | Node* head = this->unwrappedHead(); |
80 | while (head) { |
81 | Node* current = head; |
82 | head = Node::PtrTraits::unwrap(current->m_next); |
83 | delete current; |
84 | } |
85 | m_head = nullptr; |
86 | } |
87 | |
88 | template<typename... Args> |
89 | T* add(Args&&... args) |
90 | { |
91 | Node* newNode = new Node(std::forward<Args>(args)...); |
92 | newNode->m_next = unwrappedHead(); |
93 | m_head = newNode; |
94 | return &newNode->m_item; |
95 | } |
96 | |
97 | class iterator { |
98 | public: |
99 | iterator() |
100 | : m_node(0) |
101 | { |
102 | } |
103 | |
104 | // This is sort of cheating; it's equivalent to iter == end(). |
105 | bool operator!() const { return !m_node; } |
106 | |
107 | T* operator*() const { return &m_node->m_item; } |
108 | |
109 | iterator& operator++() |
110 | { |
111 | m_node = Node::PtrTraits::unwrap(m_node->m_next); |
112 | return *this; |
113 | } |
114 | |
115 | bool operator==(const iterator& other) const |
116 | { |
117 | return m_node == other.m_node; |
118 | } |
119 | |
120 | bool operator!=(const iterator& other) const |
121 | { |
122 | return !(*this == other); |
123 | } |
124 | |
125 | private: |
126 | template<typename, typename> friend class WTF::Bag; |
127 | Node* m_node; |
128 | }; |
129 | |
130 | iterator begin() |
131 | { |
132 | iterator result; |
133 | result.m_node = unwrappedHead(); |
134 | return result; |
135 | } |
136 | |
137 | const iterator begin() const |
138 | { |
139 | iterator result; |
140 | result.m_node = unwrappedHead(); |
141 | return result; |
142 | } |
143 | |
144 | |
145 | iterator end() const { return iterator(); } |
146 | |
147 | bool isEmpty() const { return !m_head; } |
148 | |
149 | private: |
150 | Node* unwrappedHead() const { return PtrTraits::unwrap(m_head); } |
151 | |
152 | typename PtrTraits::StorageType m_head { nullptr }; |
153 | }; |
154 | |
155 | template<typename T> |
156 | using PackedBag = Bag<T, PackedPtrTraits<T>>; |
157 | |
158 | } // namespace WTF |
159 | |
160 | using WTF::Bag; |
161 | using WTF::PackedBag; |
162 | |