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 final { |
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 | WTF_MAKE_FAST_ALLOCATED; |
99 | public: |
100 | iterator() |
101 | : m_node(0) |
102 | { |
103 | } |
104 | |
105 | // This is sort of cheating; it's equivalent to iter == end(). |
106 | bool operator!() const { return !m_node; } |
107 | |
108 | T* operator*() const { return &m_node->m_item; } |
109 | |
110 | iterator& operator++() |
111 | { |
112 | m_node = Node::PtrTraits::unwrap(m_node->m_next); |
113 | return *this; |
114 | } |
115 | |
116 | bool operator==(const iterator& other) const |
117 | { |
118 | return m_node == other.m_node; |
119 | } |
120 | |
121 | bool operator!=(const iterator& other) const |
122 | { |
123 | return !(*this == other); |
124 | } |
125 | |
126 | private: |
127 | template<typename, typename> friend class WTF::Bag; |
128 | Node* m_node; |
129 | }; |
130 | |
131 | iterator begin() |
132 | { |
133 | iterator result; |
134 | result.m_node = unwrappedHead(); |
135 | return result; |
136 | } |
137 | |
138 | const iterator begin() const |
139 | { |
140 | iterator result; |
141 | result.m_node = unwrappedHead(); |
142 | return result; |
143 | } |
144 | |
145 | |
146 | iterator end() const { return iterator(); } |
147 | |
148 | bool isEmpty() const { return !m_head; } |
149 | |
150 | private: |
151 | Node* unwrappedHead() const { return PtrTraits::unwrap(m_head); } |
152 | |
153 | typename PtrTraits::StorageType m_head { nullptr }; |
154 | }; |
155 | |
156 | template<typename T> |
157 | using PackedBag = Bag<T, PackedPtrTraits<T>>; |
158 | |
159 | } // namespace WTF |
160 | |
161 | using WTF::Bag; |
162 | using WTF::PackedBag; |
163 | |