|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC. |
| 3 | + * |
| 4 | + * Use of this source code is governed by a BSD-style license that can be |
| 5 | + * found in the LICENSE file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "include/private/SkTArray.h" |
| 9 | + |
| 10 | +namespace SkSL { |
| 11 | + |
| 12 | +class IRNode; |
| 13 | + |
| 14 | +// Wraps an SkTArray<std::unique_ptr<Base>> and presents it as a collection of <T> |
| 15 | +// Once the rearchitecture is complete, Base will always be IRNode and thus we can remove that |
| 16 | +// parameter, but right now we have to worry about wrapping both ExpressionArray and StatementArray |
| 17 | +template<typename T, typename Base> |
| 18 | +class NodeArrayWrapper { |
| 19 | +public: |
| 20 | + class iterator { |
| 21 | + public: |
| 22 | + T& operator*() { |
| 23 | + return static_cast<T&>(**fBase); |
| 24 | + } |
| 25 | + |
| 26 | + T* operator->() { |
| 27 | + return static_cast<T*>(fBase->get()); |
| 28 | + } |
| 29 | + |
| 30 | + iterator& operator++() { |
| 31 | + ++fBase; |
| 32 | + return *this; |
| 33 | + } |
| 34 | + |
| 35 | + bool operator==(const iterator& other) const { |
| 36 | + return fBase == other.fBase; |
| 37 | + } |
| 38 | + |
| 39 | + bool operator!=(const iterator& other) const { |
| 40 | + return fBase != other.fBase; |
| 41 | + } |
| 42 | + |
| 43 | + private: |
| 44 | + iterator(const std::unique_ptr<Base>* base) |
| 45 | + : fBase(base) {} |
| 46 | + |
| 47 | + const std::unique_ptr<Base>* fBase; |
| 48 | + |
| 49 | + friend class NodeArrayWrapper; |
| 50 | + }; |
| 51 | + |
| 52 | + class const_iterator { |
| 53 | + public: |
| 54 | + const T& operator*() { |
| 55 | + return static_cast<const T&>(**fBase); |
| 56 | + } |
| 57 | + |
| 58 | + const T* operator->() { |
| 59 | + return static_cast<const T*>(fBase->get()); |
| 60 | + } |
| 61 | + |
| 62 | + const_iterator& operator++() { |
| 63 | + ++fBase; |
| 64 | + return *this; |
| 65 | + } |
| 66 | + |
| 67 | + bool operator==(const const_iterator& other) const { |
| 68 | + return fBase == other.fBase; |
| 69 | + } |
| 70 | + |
| 71 | + bool operator!=(const const_iterator& other) const { |
| 72 | + return fBase != other.fBase; |
| 73 | + } |
| 74 | + |
| 75 | + private: |
| 76 | + const_iterator(const std::unique_ptr<Base>* base) |
| 77 | + : fBase(base) {} |
| 78 | + |
| 79 | + const std::unique_ptr<Base>* fBase; |
| 80 | + |
| 81 | + friend class NodeArrayWrapper; |
| 82 | + }; |
| 83 | + |
| 84 | + NodeArrayWrapper(SkTArray<std::unique_ptr<Base>>* contents) |
| 85 | + : fContents(contents) {} |
| 86 | + |
| 87 | + NodeArrayWrapper(const NodeArrayWrapper& other) |
| 88 | + : fContents(other.fContents) {} |
| 89 | + |
| 90 | + NodeArrayWrapper& operator=(const NodeArrayWrapper& other) { |
| 91 | + fContents = other.fContents; |
| 92 | + } |
| 93 | + |
| 94 | + void reset() { |
| 95 | + fContents->reset(); |
| 96 | + } |
| 97 | + |
| 98 | + void reserve_back(int n) { |
| 99 | + fContents->reserve_back(n); |
| 100 | + } |
| 101 | + |
| 102 | + int count() const { |
| 103 | + return fContents->count(); |
| 104 | + } |
| 105 | + |
| 106 | + bool empty() const { |
| 107 | + return fContents->empty(); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + T& push_back(T* t) { |
| 112 | + return static_cast<T&>(*fContents->emplace_back(t)); |
| 113 | + } |
| 114 | + |
| 115 | + template<class... Args> T& emplace_back(Args&&... args) { |
| 116 | + return static_cast<T&>(*fContents->emplace_back(new T(std::forward<Args>(args)...))); |
| 117 | + } |
| 118 | + |
| 119 | + void pop_back() { |
| 120 | + fContents->pop_back(); |
| 121 | + } |
| 122 | + |
| 123 | + iterator begin() { |
| 124 | + return iterator(fContents->begin()); |
| 125 | + } |
| 126 | + |
| 127 | + iterator end() { |
| 128 | + return iterator(fContents->end()); |
| 129 | + } |
| 130 | + |
| 131 | + const_iterator begin() const { |
| 132 | + return const_iterator(fContents->begin()); |
| 133 | + } |
| 134 | + |
| 135 | + const_iterator end() const { |
| 136 | + return const_iterator(fContents->end()); |
| 137 | + } |
| 138 | + |
| 139 | + T& operator[](int i) { |
| 140 | + SkASSERT(fContents->at(i)); |
| 141 | + return fContents->at(i)->template as<T>(); |
| 142 | + } |
| 143 | + |
| 144 | + const T& operator[](int i) const { |
| 145 | + SkASSERT(fContents->at(i)); |
| 146 | + return fContents->at(i)->template as<T>(); |
| 147 | + } |
| 148 | + |
| 149 | + T& front() { |
| 150 | + return fContents->front()->template as<T>(); |
| 151 | + } |
| 152 | + |
| 153 | + const T& front() const { |
| 154 | + return fContents->front()->template as<T>(); |
| 155 | + } |
| 156 | + |
| 157 | + T& back() { |
| 158 | + return fContents->back()->template as<T>(); |
| 159 | + } |
| 160 | + |
| 161 | + const T& back() const { |
| 162 | + return fContents->back()->template as<T>(); |
| 163 | + } |
| 164 | + |
| 165 | + int capacity() const { |
| 166 | + return fContents->capacity(); |
| 167 | + } |
| 168 | + |
| 169 | +private: |
| 170 | + SkTArray<std::unique_ptr<Base>>* fContents; |
| 171 | +}; |
| 172 | + |
| 173 | +template<typename T, typename Base> |
| 174 | +class ConstNodeArrayWrapper { |
| 175 | +public: |
| 176 | + class iterator { |
| 177 | + public: |
| 178 | + const T& operator*() { |
| 179 | + return static_cast<T&>(**fBase); |
| 180 | + } |
| 181 | + |
| 182 | + const T* operator->() { |
| 183 | + return static_cast<T*>(fBase->get()); |
| 184 | + } |
| 185 | + |
| 186 | + iterator& operator++() { |
| 187 | + ++fBase; |
| 188 | + return *this; |
| 189 | + } |
| 190 | + |
| 191 | + bool operator==(const iterator& other) const { |
| 192 | + return fBase == other.fBase; |
| 193 | + } |
| 194 | + |
| 195 | + bool operator!=(const iterator& other) const { |
| 196 | + return fBase != other.fBase; |
| 197 | + } |
| 198 | + |
| 199 | + private: |
| 200 | + iterator(const std::unique_ptr<Base>* base) |
| 201 | + : fBase(base) {} |
| 202 | + |
| 203 | + const std::unique_ptr<Base>* fBase; |
| 204 | + |
| 205 | + friend class ConstNodeArrayWrapper; |
| 206 | + }; |
| 207 | + |
| 208 | + ConstNodeArrayWrapper(SkTArray<std::unique_ptr<Base>>* contents) |
| 209 | + : fContents(contents) {} |
| 210 | + |
| 211 | + ConstNodeArrayWrapper(const ConstNodeArrayWrapper& other) |
| 212 | + : fContents(other.fContents) {} |
| 213 | + |
| 214 | + int count() const { |
| 215 | + return fContents->count(); |
| 216 | + } |
| 217 | + |
| 218 | + bool empty() const { |
| 219 | + return fContents->empty(); |
| 220 | + } |
| 221 | + |
| 222 | + iterator begin() const { |
| 223 | + return iterator(fContents->begin()); |
| 224 | + } |
| 225 | + |
| 226 | + iterator end() const { |
| 227 | + return iterator(fContents->end()); |
| 228 | + } |
| 229 | + |
| 230 | + T& operator[](int i) { |
| 231 | + return fContents->at(i)->template as<T>(); |
| 232 | + } |
| 233 | + |
| 234 | + const T& operator[](int i) const { |
| 235 | + return fContents->at(i)->template as<T>(); |
| 236 | + } |
| 237 | + |
| 238 | + T& front() { |
| 239 | + return fContents->front()->template as<T>(); |
| 240 | + } |
| 241 | + |
| 242 | + const T& front() const { |
| 243 | + return fContents->front()->template as<T>(); |
| 244 | + } |
| 245 | + |
| 246 | + T& back() { return fContents->back()->template as<T>(); } |
| 247 | + |
| 248 | + const T& back() const { return fContents->back()->template as<T>(); } |
| 249 | + |
| 250 | +private: |
| 251 | + const SkTArray<std::unique_ptr<Base>>* fContents; |
| 252 | +}; |
| 253 | + |
| 254 | +} // namespace SkSL |
0 commit comments