annotate osx/include/kj/array.h @ 79:91c729825bca pa_catalina

Update build for AUDIO_COMPONENT_FIX
author Chris Cannam
date Wed, 30 Oct 2019 12:40:34 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@62 2 // Licensed under the MIT License:
cannam@62 3 //
cannam@62 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@62 5 // of this software and associated documentation files (the "Software"), to deal
cannam@62 6 // in the Software without restriction, including without limitation the rights
cannam@62 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@62 8 // copies of the Software, and to permit persons to whom the Software is
cannam@62 9 // furnished to do so, subject to the following conditions:
cannam@62 10 //
cannam@62 11 // The above copyright notice and this permission notice shall be included in
cannam@62 12 // all copies or substantial portions of the Software.
cannam@62 13 //
cannam@62 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@62 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@62 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@62 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@62 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@62 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@62 20 // THE SOFTWARE.
cannam@62 21
cannam@62 22 #ifndef KJ_ARRAY_H_
cannam@62 23 #define KJ_ARRAY_H_
cannam@62 24
cannam@62 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@62 26 #pragma GCC system_header
cannam@62 27 #endif
cannam@62 28
cannam@62 29 #include "common.h"
cannam@62 30 #include <string.h>
cannam@62 31 #include <initializer_list>
cannam@62 32
cannam@62 33 namespace kj {
cannam@62 34
cannam@62 35 // =======================================================================================
cannam@62 36 // ArrayDisposer -- Implementation details.
cannam@62 37
cannam@62 38 class ArrayDisposer {
cannam@62 39 // Much like Disposer from memory.h.
cannam@62 40
cannam@62 41 protected:
cannam@62 42 // Do not declare a destructor, as doing so will force a global initializer for
cannam@62 43 // HeapArrayDisposer::instance.
cannam@62 44
cannam@62 45 virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
cannam@62 46 size_t capacity, void (*destroyElement)(void*)) const = 0;
cannam@62 47 // Disposes of the array. `destroyElement` invokes the destructor of each element, or is nullptr
cannam@62 48 // if the elements have trivial destructors. `capacity` is the amount of space that was
cannam@62 49 // allocated while `elementCount` is the number of elements that were actually constructed;
cannam@62 50 // these are always the same number for Array<T> but may be different when using ArrayBuilder<T>.
cannam@62 51
cannam@62 52 public:
cannam@62 53
cannam@62 54 template <typename T>
cannam@62 55 void dispose(T* firstElement, size_t elementCount, size_t capacity) const;
cannam@62 56 // Helper wrapper around disposeImpl().
cannam@62 57 //
cannam@62 58 // Callers must not call dispose() on the same array twice, even if the first call throws
cannam@62 59 // an exception.
cannam@62 60
cannam@62 61 private:
cannam@62 62 template <typename T, bool hasTrivialDestructor = __has_trivial_destructor(T)>
cannam@62 63 struct Dispose_;
cannam@62 64 };
cannam@62 65
cannam@62 66 class ExceptionSafeArrayUtil {
cannam@62 67 // Utility class that assists in constructing or destroying elements of an array, where the
cannam@62 68 // constructor or destructor could throw exceptions. In case of an exception,
cannam@62 69 // ExceptionSafeArrayUtil's destructor will call destructors on all elements that have been
cannam@62 70 // constructed but not destroyed. Remember that destructors that throw exceptions are required
cannam@62 71 // to use UnwindDetector to detect unwind and avoid exceptions in this case. Therefore, no more
cannam@62 72 // than one exception will be thrown (and the program will not terminate).
cannam@62 73
cannam@62 74 public:
cannam@62 75 inline ExceptionSafeArrayUtil(void* ptr, size_t elementSize, size_t constructedElementCount,
cannam@62 76 void (*destroyElement)(void*))
cannam@62 77 : pos(reinterpret_cast<byte*>(ptr) + elementSize * constructedElementCount),
cannam@62 78 elementSize(elementSize), constructedElementCount(constructedElementCount),
cannam@62 79 destroyElement(destroyElement) {}
cannam@62 80 KJ_DISALLOW_COPY(ExceptionSafeArrayUtil);
cannam@62 81
cannam@62 82 inline ~ExceptionSafeArrayUtil() noexcept(false) {
cannam@62 83 if (constructedElementCount > 0) destroyAll();
cannam@62 84 }
cannam@62 85
cannam@62 86 void construct(size_t count, void (*constructElement)(void*));
cannam@62 87 // Construct the given number of elements.
cannam@62 88
cannam@62 89 void destroyAll();
cannam@62 90 // Destroy all elements. Call this immediately before ExceptionSafeArrayUtil goes out-of-scope
cannam@62 91 // to ensure that one element throwing an exception does not prevent the others from being
cannam@62 92 // destroyed.
cannam@62 93
cannam@62 94 void release() { constructedElementCount = 0; }
cannam@62 95 // Prevent ExceptionSafeArrayUtil's destructor from destroying the constructed elements.
cannam@62 96 // Call this after you've successfully finished constructing.
cannam@62 97
cannam@62 98 private:
cannam@62 99 byte* pos;
cannam@62 100 size_t elementSize;
cannam@62 101 size_t constructedElementCount;
cannam@62 102 void (*destroyElement)(void*);
cannam@62 103 };
cannam@62 104
cannam@62 105 class DestructorOnlyArrayDisposer: public ArrayDisposer {
cannam@62 106 public:
cannam@62 107 static const DestructorOnlyArrayDisposer instance;
cannam@62 108
cannam@62 109 void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
cannam@62 110 size_t capacity, void (*destroyElement)(void*)) const override;
cannam@62 111 };
cannam@62 112
cannam@62 113 class NullArrayDisposer: public ArrayDisposer {
cannam@62 114 // An ArrayDisposer that does nothing. Can be used to construct a fake Arrays that doesn't
cannam@62 115 // actually own its content.
cannam@62 116
cannam@62 117 public:
cannam@62 118 static const NullArrayDisposer instance;
cannam@62 119
cannam@62 120 void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
cannam@62 121 size_t capacity, void (*destroyElement)(void*)) const override;
cannam@62 122 };
cannam@62 123
cannam@62 124 // =======================================================================================
cannam@62 125 // Array
cannam@62 126
cannam@62 127 template <typename T>
cannam@62 128 class Array {
cannam@62 129 // An owned array which will automatically be disposed of (using an ArrayDisposer) in the
cannam@62 130 // destructor. Can be moved, but not copied. Much like Own<T>, but for arrays rather than
cannam@62 131 // single objects.
cannam@62 132
cannam@62 133 public:
cannam@62 134 inline Array(): ptr(nullptr), size_(0), disposer(nullptr) {}
cannam@62 135 inline Array(decltype(nullptr)): ptr(nullptr), size_(0), disposer(nullptr) {}
cannam@62 136 inline Array(Array&& other) noexcept
cannam@62 137 : ptr(other.ptr), size_(other.size_), disposer(other.disposer) {
cannam@62 138 other.ptr = nullptr;
cannam@62 139 other.size_ = 0;
cannam@62 140 }
cannam@62 141 inline Array(Array<RemoveConstOrDisable<T>>&& other) noexcept
cannam@62 142 : ptr(other.ptr), size_(other.size_), disposer(other.disposer) {
cannam@62 143 other.ptr = nullptr;
cannam@62 144 other.size_ = 0;
cannam@62 145 }
cannam@62 146 inline Array(T* firstElement, size_t size, const ArrayDisposer& disposer)
cannam@62 147 : ptr(firstElement), size_(size), disposer(&disposer) {}
cannam@62 148
cannam@62 149 KJ_DISALLOW_COPY(Array);
cannam@62 150 inline ~Array() noexcept { dispose(); }
cannam@62 151
cannam@62 152 inline operator ArrayPtr<T>() {
cannam@62 153 return ArrayPtr<T>(ptr, size_);
cannam@62 154 }
cannam@62 155 inline operator ArrayPtr<const T>() const {
cannam@62 156 return ArrayPtr<T>(ptr, size_);
cannam@62 157 }
cannam@62 158 inline ArrayPtr<T> asPtr() {
cannam@62 159 return ArrayPtr<T>(ptr, size_);
cannam@62 160 }
cannam@62 161 inline ArrayPtr<const T> asPtr() const {
cannam@62 162 return ArrayPtr<T>(ptr, size_);
cannam@62 163 }
cannam@62 164
cannam@62 165 inline size_t size() const { return size_; }
cannam@62 166 inline T& operator[](size_t index) const {
cannam@62 167 KJ_IREQUIRE(index < size_, "Out-of-bounds Array access.");
cannam@62 168 return ptr[index];
cannam@62 169 }
cannam@62 170
cannam@62 171 inline const T* begin() const { return ptr; }
cannam@62 172 inline const T* end() const { return ptr + size_; }
cannam@62 173 inline const T& front() const { return *ptr; }
cannam@62 174 inline const T& back() const { return *(ptr + size_ - 1); }
cannam@62 175 inline T* begin() { return ptr; }
cannam@62 176 inline T* end() { return ptr + size_; }
cannam@62 177 inline T& front() { return *ptr; }
cannam@62 178 inline T& back() { return *(ptr + size_ - 1); }
cannam@62 179
cannam@62 180 inline ArrayPtr<T> slice(size_t start, size_t end) {
cannam@62 181 KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice().");
cannam@62 182 return ArrayPtr<T>(ptr + start, end - start);
cannam@62 183 }
cannam@62 184 inline ArrayPtr<const T> slice(size_t start, size_t end) const {
cannam@62 185 KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice().");
cannam@62 186 return ArrayPtr<const T>(ptr + start, end - start);
cannam@62 187 }
cannam@62 188
cannam@62 189 inline ArrayPtr<const byte> asBytes() const { return asPtr().asBytes(); }
cannam@62 190 inline ArrayPtr<PropagateConst<T, byte>> asBytes() { return asPtr().asBytes(); }
cannam@62 191 inline ArrayPtr<const char> asChars() const { return asPtr().asChars(); }
cannam@62 192 inline ArrayPtr<PropagateConst<T, char>> asChars() { return asPtr().asChars(); }
cannam@62 193
cannam@62 194 inline Array<PropagateConst<T, byte>> releaseAsBytes() {
cannam@62 195 // Like asBytes() but transfers ownership.
cannam@62 196 static_assert(sizeof(T) == sizeof(byte),
cannam@62 197 "releaseAsBytes() only possible on arrays with byte-size elements (e.g. chars).");
cannam@62 198 Array<PropagateConst<T, byte>> result(
cannam@62 199 reinterpret_cast<PropagateConst<T, byte>*>(ptr), size_, *disposer);
cannam@62 200 ptr = nullptr;
cannam@62 201 size_ = 0;
cannam@62 202 return result;
cannam@62 203 }
cannam@62 204 inline Array<PropagateConst<T, char>> releaseAsChars() {
cannam@62 205 // Like asChars() but transfers ownership.
cannam@62 206 static_assert(sizeof(T) == sizeof(PropagateConst<T, char>),
cannam@62 207 "releaseAsChars() only possible on arrays with char-size elements (e.g. bytes).");
cannam@62 208 Array<PropagateConst<T, char>> result(
cannam@62 209 reinterpret_cast<PropagateConst<T, char>*>(ptr), size_, *disposer);
cannam@62 210 ptr = nullptr;
cannam@62 211 size_ = 0;
cannam@62 212 return result;
cannam@62 213 }
cannam@62 214
cannam@62 215 inline bool operator==(decltype(nullptr)) const { return size_ == 0; }
cannam@62 216 inline bool operator!=(decltype(nullptr)) const { return size_ != 0; }
cannam@62 217
cannam@62 218 inline Array& operator=(decltype(nullptr)) {
cannam@62 219 dispose();
cannam@62 220 return *this;
cannam@62 221 }
cannam@62 222
cannam@62 223 inline Array& operator=(Array&& other) {
cannam@62 224 dispose();
cannam@62 225 ptr = other.ptr;
cannam@62 226 size_ = other.size_;
cannam@62 227 disposer = other.disposer;
cannam@62 228 other.ptr = nullptr;
cannam@62 229 other.size_ = 0;
cannam@62 230 return *this;
cannam@62 231 }
cannam@62 232
cannam@62 233 private:
cannam@62 234 T* ptr;
cannam@62 235 size_t size_;
cannam@62 236 const ArrayDisposer* disposer;
cannam@62 237
cannam@62 238 inline void dispose() {
cannam@62 239 // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly
cannam@62 240 // dispose again.
cannam@62 241 T* ptrCopy = ptr;
cannam@62 242 size_t sizeCopy = size_;
cannam@62 243 if (ptrCopy != nullptr) {
cannam@62 244 ptr = nullptr;
cannam@62 245 size_ = 0;
cannam@62 246 disposer->dispose(ptrCopy, sizeCopy, sizeCopy);
cannam@62 247 }
cannam@62 248 }
cannam@62 249
cannam@62 250 template <typename U>
cannam@62 251 friend class Array;
cannam@62 252 };
cannam@62 253
cannam@62 254 static_assert(!canMemcpy<Array<char>>(), "canMemcpy<>() is broken");
cannam@62 255
cannam@62 256 namespace _ { // private
cannam@62 257
cannam@62 258 class HeapArrayDisposer final: public ArrayDisposer {
cannam@62 259 public:
cannam@62 260 template <typename T>
cannam@62 261 static T* allocate(size_t count);
cannam@62 262 template <typename T>
cannam@62 263 static T* allocateUninitialized(size_t count);
cannam@62 264
cannam@62 265 static const HeapArrayDisposer instance;
cannam@62 266
cannam@62 267 private:
cannam@62 268 static void* allocateImpl(size_t elementSize, size_t elementCount, size_t capacity,
cannam@62 269 void (*constructElement)(void*), void (*destroyElement)(void*));
cannam@62 270 // Allocates and constructs the array. Both function pointers are null if the constructor is
cannam@62 271 // trivial, otherwise destroyElement is null if the constructor doesn't throw.
cannam@62 272
cannam@62 273 virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
cannam@62 274 size_t capacity, void (*destroyElement)(void*)) const override;
cannam@62 275
cannam@62 276 template <typename T, bool hasTrivialConstructor = __has_trivial_constructor(T),
cannam@62 277 bool hasNothrowConstructor = __has_nothrow_constructor(T)>
cannam@62 278 struct Allocate_;
cannam@62 279 };
cannam@62 280
cannam@62 281 } // namespace _ (private)
cannam@62 282
cannam@62 283 template <typename T>
cannam@62 284 inline Array<T> heapArray(size_t size) {
cannam@62 285 // Much like `heap<T>()` from memory.h, allocates a new array on the heap.
cannam@62 286
cannam@62 287 return Array<T>(_::HeapArrayDisposer::allocate<T>(size), size,
cannam@62 288 _::HeapArrayDisposer::instance);
cannam@62 289 }
cannam@62 290
cannam@62 291 template <typename T> Array<T> heapArray(const T* content, size_t size);
cannam@62 292 template <typename T> Array<T> heapArray(ArrayPtr<T> content);
cannam@62 293 template <typename T> Array<T> heapArray(ArrayPtr<const T> content);
cannam@62 294 template <typename T, typename Iterator> Array<T> heapArray(Iterator begin, Iterator end);
cannam@62 295 template <typename T> Array<T> heapArray(std::initializer_list<T> init);
cannam@62 296 // Allocate a heap array containing a copy of the given content.
cannam@62 297
cannam@62 298 template <typename T, typename Container>
cannam@62 299 Array<T> heapArrayFromIterable(Container&& a) { return heapArray<T>(a.begin(), a.end()); }
cannam@62 300 template <typename T>
cannam@62 301 Array<T> heapArrayFromIterable(Array<T>&& a) { return mv(a); }
cannam@62 302
cannam@62 303 // =======================================================================================
cannam@62 304 // ArrayBuilder
cannam@62 305
cannam@62 306 template <typename T>
cannam@62 307 class ArrayBuilder {
cannam@62 308 // Class which lets you build an Array<T> specifying the exact constructor arguments for each
cannam@62 309 // element, rather than starting by default-constructing them.
cannam@62 310
cannam@62 311 public:
cannam@62 312 ArrayBuilder(): ptr(nullptr), pos(nullptr), endPtr(nullptr) {}
cannam@62 313 ArrayBuilder(decltype(nullptr)): ptr(nullptr), pos(nullptr), endPtr(nullptr) {}
cannam@62 314 explicit ArrayBuilder(RemoveConst<T>* firstElement, size_t capacity,
cannam@62 315 const ArrayDisposer& disposer)
cannam@62 316 : ptr(firstElement), pos(firstElement), endPtr(firstElement + capacity),
cannam@62 317 disposer(&disposer) {}
cannam@62 318 ArrayBuilder(ArrayBuilder&& other)
cannam@62 319 : ptr(other.ptr), pos(other.pos), endPtr(other.endPtr), disposer(other.disposer) {
cannam@62 320 other.ptr = nullptr;
cannam@62 321 other.pos = nullptr;
cannam@62 322 other.endPtr = nullptr;
cannam@62 323 }
cannam@62 324 KJ_DISALLOW_COPY(ArrayBuilder);
cannam@62 325 inline ~ArrayBuilder() noexcept(false) { dispose(); }
cannam@62 326
cannam@62 327 inline operator ArrayPtr<T>() {
cannam@62 328 return arrayPtr(ptr, pos);
cannam@62 329 }
cannam@62 330 inline operator ArrayPtr<const T>() const {
cannam@62 331 return arrayPtr(ptr, pos);
cannam@62 332 }
cannam@62 333 inline ArrayPtr<T> asPtr() {
cannam@62 334 return arrayPtr(ptr, pos);
cannam@62 335 }
cannam@62 336 inline ArrayPtr<const T> asPtr() const {
cannam@62 337 return arrayPtr(ptr, pos);
cannam@62 338 }
cannam@62 339
cannam@62 340 inline size_t size() const { return pos - ptr; }
cannam@62 341 inline size_t capacity() const { return endPtr - ptr; }
cannam@62 342 inline T& operator[](size_t index) const {
cannam@62 343 KJ_IREQUIRE(index < implicitCast<size_t>(pos - ptr), "Out-of-bounds Array access.");
cannam@62 344 return ptr[index];
cannam@62 345 }
cannam@62 346
cannam@62 347 inline const T* begin() const { return ptr; }
cannam@62 348 inline const T* end() const { return pos; }
cannam@62 349 inline const T& front() const { return *ptr; }
cannam@62 350 inline const T& back() const { return *(pos - 1); }
cannam@62 351 inline T* begin() { return ptr; }
cannam@62 352 inline T* end() { return pos; }
cannam@62 353 inline T& front() { return *ptr; }
cannam@62 354 inline T& back() { return *(pos - 1); }
cannam@62 355
cannam@62 356 ArrayBuilder& operator=(ArrayBuilder&& other) {
cannam@62 357 dispose();
cannam@62 358 ptr = other.ptr;
cannam@62 359 pos = other.pos;
cannam@62 360 endPtr = other.endPtr;
cannam@62 361 disposer = other.disposer;
cannam@62 362 other.ptr = nullptr;
cannam@62 363 other.pos = nullptr;
cannam@62 364 other.endPtr = nullptr;
cannam@62 365 return *this;
cannam@62 366 }
cannam@62 367 ArrayBuilder& operator=(decltype(nullptr)) {
cannam@62 368 dispose();
cannam@62 369 return *this;
cannam@62 370 }
cannam@62 371
cannam@62 372 template <typename... Params>
cannam@62 373 T& add(Params&&... params) {
cannam@62 374 KJ_IREQUIRE(pos < endPtr, "Added too many elements to ArrayBuilder.");
cannam@62 375 ctor(*pos, kj::fwd<Params>(params)...);
cannam@62 376 return *pos++;
cannam@62 377 }
cannam@62 378
cannam@62 379 template <typename Container>
cannam@62 380 void addAll(Container&& container) {
cannam@62 381 addAll<decltype(container.begin()), !isReference<Container>()>(
cannam@62 382 container.begin(), container.end());
cannam@62 383 }
cannam@62 384
cannam@62 385 template <typename Iterator, bool move = false>
cannam@62 386 void addAll(Iterator start, Iterator end);
cannam@62 387
cannam@62 388 void removeLast() {
cannam@62 389 KJ_IREQUIRE(pos > ptr, "No elements present to remove.");
cannam@62 390 kj::dtor(*--pos);
cannam@62 391 }
cannam@62 392
cannam@62 393 void truncate(size_t size) {
cannam@62 394 KJ_IREQUIRE(size <= this->size(), "can't use truncate() to expand");
cannam@62 395
cannam@62 396 T* target = ptr + size;
cannam@62 397 if (__has_trivial_destructor(T)) {
cannam@62 398 pos = target;
cannam@62 399 } else {
cannam@62 400 while (pos > target) {
cannam@62 401 kj::dtor(*--pos);
cannam@62 402 }
cannam@62 403 }
cannam@62 404 }
cannam@62 405
cannam@62 406 void resize(size_t size) {
cannam@62 407 KJ_IREQUIRE(size <= capacity(), "can't resize past capacity");
cannam@62 408
cannam@62 409 T* target = ptr + size;
cannam@62 410 if (target > pos) {
cannam@62 411 // expand
cannam@62 412 if (__has_trivial_constructor(T)) {
cannam@62 413 pos = target;
cannam@62 414 } else {
cannam@62 415 while (pos < target) {
cannam@62 416 kj::ctor(*pos++);
cannam@62 417 }
cannam@62 418 }
cannam@62 419 } else {
cannam@62 420 // truncate
cannam@62 421 if (__has_trivial_destructor(T)) {
cannam@62 422 pos = target;
cannam@62 423 } else {
cannam@62 424 while (pos > target) {
cannam@62 425 kj::dtor(*--pos);
cannam@62 426 }
cannam@62 427 }
cannam@62 428 }
cannam@62 429 }
cannam@62 430
cannam@62 431 Array<T> finish() {
cannam@62 432 // We could safely remove this check if we assume that the disposer implementation doesn't
cannam@62 433 // need to know the original capacity, as is thes case with HeapArrayDisposer since it uses
cannam@62 434 // operator new() or if we created a custom disposer for ArrayBuilder which stores the capacity
cannam@62 435 // in a prefix. But that would make it hard to write cleverer heap allocators, and anyway this
cannam@62 436 // check might catch bugs. Probably people should use Vector if they want to build arrays
cannam@62 437 // without knowing the final size in advance.
cannam@62 438 KJ_IREQUIRE(pos == endPtr, "ArrayBuilder::finish() called prematurely.");
cannam@62 439 Array<T> result(reinterpret_cast<T*>(ptr), pos - ptr, *disposer);
cannam@62 440 ptr = nullptr;
cannam@62 441 pos = nullptr;
cannam@62 442 endPtr = nullptr;
cannam@62 443 return result;
cannam@62 444 }
cannam@62 445
cannam@62 446 inline bool isFull() const {
cannam@62 447 return pos == endPtr;
cannam@62 448 }
cannam@62 449
cannam@62 450 private:
cannam@62 451 T* ptr;
cannam@62 452 RemoveConst<T>* pos;
cannam@62 453 T* endPtr;
cannam@62 454 const ArrayDisposer* disposer;
cannam@62 455
cannam@62 456 inline void dispose() {
cannam@62 457 // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly
cannam@62 458 // dispose again.
cannam@62 459 T* ptrCopy = ptr;
cannam@62 460 T* posCopy = pos;
cannam@62 461 T* endCopy = endPtr;
cannam@62 462 if (ptrCopy != nullptr) {
cannam@62 463 ptr = nullptr;
cannam@62 464 pos = nullptr;
cannam@62 465 endPtr = nullptr;
cannam@62 466 disposer->dispose(ptrCopy, posCopy - ptrCopy, endCopy - ptrCopy);
cannam@62 467 }
cannam@62 468 }
cannam@62 469 };
cannam@62 470
cannam@62 471 template <typename T>
cannam@62 472 inline ArrayBuilder<T> heapArrayBuilder(size_t size) {
cannam@62 473 // Like `heapArray<T>()` but does not default-construct the elements. You must construct them
cannam@62 474 // manually by calling `add()`.
cannam@62 475
cannam@62 476 return ArrayBuilder<T>(_::HeapArrayDisposer::allocateUninitialized<RemoveConst<T>>(size),
cannam@62 477 size, _::HeapArrayDisposer::instance);
cannam@62 478 }
cannam@62 479
cannam@62 480 // =======================================================================================
cannam@62 481 // Inline Arrays
cannam@62 482
cannam@62 483 template <typename T, size_t fixedSize>
cannam@62 484 class FixedArray {
cannam@62 485 // A fixed-width array whose storage is allocated inline rather than on the heap.
cannam@62 486
cannam@62 487 public:
cannam@62 488 inline size_t size() const { return fixedSize; }
cannam@62 489 inline T* begin() { return content; }
cannam@62 490 inline T* end() { return content + fixedSize; }
cannam@62 491 inline const T* begin() const { return content; }
cannam@62 492 inline const T* end() const { return content + fixedSize; }
cannam@62 493
cannam@62 494 inline operator ArrayPtr<T>() {
cannam@62 495 return arrayPtr(content, fixedSize);
cannam@62 496 }
cannam@62 497 inline operator ArrayPtr<const T>() const {
cannam@62 498 return arrayPtr(content, fixedSize);
cannam@62 499 }
cannam@62 500
cannam@62 501 inline T& operator[](size_t index) { return content[index]; }
cannam@62 502 inline const T& operator[](size_t index) const { return content[index]; }
cannam@62 503
cannam@62 504 private:
cannam@62 505 T content[fixedSize];
cannam@62 506 };
cannam@62 507
cannam@62 508 template <typename T, size_t fixedSize>
cannam@62 509 class CappedArray {
cannam@62 510 // Like `FixedArray` but can be dynamically resized as long as the size does not exceed the limit
cannam@62 511 // specified by the template parameter.
cannam@62 512 //
cannam@62 513 // TODO(someday): Don't construct elements past currentSize?
cannam@62 514
cannam@62 515 public:
cannam@62 516 inline KJ_CONSTEXPR() CappedArray(): currentSize(fixedSize) {}
cannam@62 517 inline explicit constexpr CappedArray(size_t s): currentSize(s) {}
cannam@62 518
cannam@62 519 inline size_t size() const { return currentSize; }
cannam@62 520 inline void setSize(size_t s) { KJ_IREQUIRE(s <= fixedSize); currentSize = s; }
cannam@62 521 inline T* begin() { return content; }
cannam@62 522 inline T* end() { return content + currentSize; }
cannam@62 523 inline const T* begin() const { return content; }
cannam@62 524 inline const T* end() const { return content + currentSize; }
cannam@62 525
cannam@62 526 inline operator ArrayPtr<T>() {
cannam@62 527 return arrayPtr(content, currentSize);
cannam@62 528 }
cannam@62 529 inline operator ArrayPtr<const T>() const {
cannam@62 530 return arrayPtr(content, currentSize);
cannam@62 531 }
cannam@62 532
cannam@62 533 inline T& operator[](size_t index) { return content[index]; }
cannam@62 534 inline const T& operator[](size_t index) const { return content[index]; }
cannam@62 535
cannam@62 536 private:
cannam@62 537 size_t currentSize;
cannam@62 538 T content[fixedSize];
cannam@62 539 };
cannam@62 540
cannam@62 541 // =======================================================================================
cannam@62 542 // KJ_MAP
cannam@62 543
cannam@62 544 #define KJ_MAP(elementName, array) \
cannam@62 545 ::kj::_::Mapper<KJ_DECLTYPE_REF(array)>(array) * \
cannam@62 546 [&](typename ::kj::_::Mapper<KJ_DECLTYPE_REF(array)>::Element elementName)
cannam@62 547 // Applies some function to every element of an array, returning an Array of the results, with
cannam@62 548 // nice syntax. Example:
cannam@62 549 //
cannam@62 550 // StringPtr foo = "abcd";
cannam@62 551 // Array<char> bar = KJ_MAP(c, foo) -> char { return c + 1; };
cannam@62 552 // KJ_ASSERT(str(bar) == "bcde");
cannam@62 553
cannam@62 554 namespace _ { // private
cannam@62 555
cannam@62 556 template <typename T>
cannam@62 557 struct Mapper {
cannam@62 558 T array;
cannam@62 559 Mapper(T&& array): array(kj::fwd<T>(array)) {}
cannam@62 560 template <typename Func>
cannam@62 561 auto operator*(Func&& func) -> Array<decltype(func(*array.begin()))> {
cannam@62 562 auto builder = heapArrayBuilder<decltype(func(*array.begin()))>(array.size());
cannam@62 563 for (auto iter = array.begin(); iter != array.end(); ++iter) {
cannam@62 564 builder.add(func(*iter));
cannam@62 565 }
cannam@62 566 return builder.finish();
cannam@62 567 }
cannam@62 568 typedef decltype(*kj::instance<T>().begin()) Element;
cannam@62 569 };
cannam@62 570
cannam@62 571 template <typename T, size_t s>
cannam@62 572 struct Mapper<T(&)[s]> {
cannam@62 573 T* array;
cannam@62 574 Mapper(T* array): array(array) {}
cannam@62 575 template <typename Func>
cannam@62 576 auto operator*(Func&& func) -> Array<decltype(func(*array))> {
cannam@62 577 auto builder = heapArrayBuilder<decltype(func(*array))>(s);
cannam@62 578 for (size_t i = 0; i < s; i++) {
cannam@62 579 builder.add(func(array[i]));
cannam@62 580 }
cannam@62 581 return builder.finish();
cannam@62 582 }
cannam@62 583 typedef decltype(*array)& Element;
cannam@62 584 };
cannam@62 585
cannam@62 586 } // namespace _ (private)
cannam@62 587
cannam@62 588 // =======================================================================================
cannam@62 589 // Inline implementation details
cannam@62 590
cannam@62 591 template <typename T>
cannam@62 592 struct ArrayDisposer::Dispose_<T, true> {
cannam@62 593 static void dispose(T* firstElement, size_t elementCount, size_t capacity,
cannam@62 594 const ArrayDisposer& disposer) {
cannam@62 595 disposer.disposeImpl(const_cast<RemoveConst<T>*>(firstElement),
cannam@62 596 sizeof(T), elementCount, capacity, nullptr);
cannam@62 597 }
cannam@62 598 };
cannam@62 599 template <typename T>
cannam@62 600 struct ArrayDisposer::Dispose_<T, false> {
cannam@62 601 static void destruct(void* ptr) {
cannam@62 602 kj::dtor(*reinterpret_cast<T*>(ptr));
cannam@62 603 }
cannam@62 604
cannam@62 605 static void dispose(T* firstElement, size_t elementCount, size_t capacity,
cannam@62 606 const ArrayDisposer& disposer) {
cannam@62 607 disposer.disposeImpl(firstElement, sizeof(T), elementCount, capacity, &destruct);
cannam@62 608 }
cannam@62 609 };
cannam@62 610
cannam@62 611 template <typename T>
cannam@62 612 void ArrayDisposer::dispose(T* firstElement, size_t elementCount, size_t capacity) const {
cannam@62 613 Dispose_<T>::dispose(firstElement, elementCount, capacity, *this);
cannam@62 614 }
cannam@62 615
cannam@62 616 namespace _ { // private
cannam@62 617
cannam@62 618 template <typename T>
cannam@62 619 struct HeapArrayDisposer::Allocate_<T, true, true> {
cannam@62 620 static T* allocate(size_t elementCount, size_t capacity) {
cannam@62 621 return reinterpret_cast<T*>(allocateImpl(
cannam@62 622 sizeof(T), elementCount, capacity, nullptr, nullptr));
cannam@62 623 }
cannam@62 624 };
cannam@62 625 template <typename T>
cannam@62 626 struct HeapArrayDisposer::Allocate_<T, false, true> {
cannam@62 627 static void construct(void* ptr) {
cannam@62 628 kj::ctor(*reinterpret_cast<T*>(ptr));
cannam@62 629 }
cannam@62 630 static T* allocate(size_t elementCount, size_t capacity) {
cannam@62 631 return reinterpret_cast<T*>(allocateImpl(
cannam@62 632 sizeof(T), elementCount, capacity, &construct, nullptr));
cannam@62 633 }
cannam@62 634 };
cannam@62 635 template <typename T>
cannam@62 636 struct HeapArrayDisposer::Allocate_<T, false, false> {
cannam@62 637 static void construct(void* ptr) {
cannam@62 638 kj::ctor(*reinterpret_cast<T*>(ptr));
cannam@62 639 }
cannam@62 640 static void destruct(void* ptr) {
cannam@62 641 kj::dtor(*reinterpret_cast<T*>(ptr));
cannam@62 642 }
cannam@62 643 static T* allocate(size_t elementCount, size_t capacity) {
cannam@62 644 return reinterpret_cast<T*>(allocateImpl(
cannam@62 645 sizeof(T), elementCount, capacity, &construct, &destruct));
cannam@62 646 }
cannam@62 647 };
cannam@62 648
cannam@62 649 template <typename T>
cannam@62 650 T* HeapArrayDisposer::allocate(size_t count) {
cannam@62 651 return Allocate_<T>::allocate(count, count);
cannam@62 652 }
cannam@62 653
cannam@62 654 template <typename T>
cannam@62 655 T* HeapArrayDisposer::allocateUninitialized(size_t count) {
cannam@62 656 return Allocate_<T, true, true>::allocate(0, count);
cannam@62 657 }
cannam@62 658
cannam@62 659 template <typename Element, typename Iterator, bool move, bool = canMemcpy<Element>()>
cannam@62 660 struct CopyConstructArray_;
cannam@62 661
cannam@62 662 template <typename T, bool move>
cannam@62 663 struct CopyConstructArray_<T, T*, move, true> {
cannam@62 664 static inline T* apply(T* __restrict__ pos, T* start, T* end) {
cannam@62 665 memcpy(pos, start, reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start));
cannam@62 666 return pos + (end - start);
cannam@62 667 }
cannam@62 668 };
cannam@62 669
cannam@62 670 template <typename T>
cannam@62 671 struct CopyConstructArray_<T, const T*, false, true> {
cannam@62 672 static inline T* apply(T* __restrict__ pos, const T* start, const T* end) {
cannam@62 673 memcpy(pos, start, reinterpret_cast<const byte*>(end) - reinterpret_cast<const byte*>(start));
cannam@62 674 return pos + (end - start);
cannam@62 675 }
cannam@62 676 };
cannam@62 677
cannam@62 678 template <typename T, typename Iterator, bool move>
cannam@62 679 struct CopyConstructArray_<T, Iterator, move, true> {
cannam@62 680 static inline T* apply(T* __restrict__ pos, Iterator start, Iterator end) {
cannam@62 681 // Since both the copy constructor and assignment operator are trivial, we know that assignment
cannam@62 682 // is equivalent to copy-constructing. So we can make this case somewhat easier for the
cannam@62 683 // compiler to optimize.
cannam@62 684 while (start != end) {
cannam@62 685 *pos++ = *start++;
cannam@62 686 }
cannam@62 687 return pos;
cannam@62 688 }
cannam@62 689 };
cannam@62 690
cannam@62 691 template <typename T, typename Iterator>
cannam@62 692 struct CopyConstructArray_<T, Iterator, false, false> {
cannam@62 693 struct ExceptionGuard {
cannam@62 694 T* start;
cannam@62 695 T* pos;
cannam@62 696 inline explicit ExceptionGuard(T* pos): start(pos), pos(pos) {}
cannam@62 697 ~ExceptionGuard() noexcept(false) {
cannam@62 698 while (pos > start) {
cannam@62 699 dtor(*--pos);
cannam@62 700 }
cannam@62 701 }
cannam@62 702 };
cannam@62 703
cannam@62 704 static T* apply(T* __restrict__ pos, Iterator start, Iterator end) {
cannam@62 705 // Verify that T can be *implicitly* constructed from the source values.
cannam@62 706 if (false) implicitCast<T>(*start);
cannam@62 707
cannam@62 708 if (noexcept(T(*start))) {
cannam@62 709 while (start != end) {
cannam@62 710 ctor(*pos++, *start++);
cannam@62 711 }
cannam@62 712 return pos;
cannam@62 713 } else {
cannam@62 714 // Crap. This is complicated.
cannam@62 715 ExceptionGuard guard(pos);
cannam@62 716 while (start != end) {
cannam@62 717 ctor(*guard.pos, *start++);
cannam@62 718 ++guard.pos;
cannam@62 719 }
cannam@62 720 guard.start = guard.pos;
cannam@62 721 return guard.pos;
cannam@62 722 }
cannam@62 723 }
cannam@62 724 };
cannam@62 725
cannam@62 726 template <typename T, typename Iterator>
cannam@62 727 struct CopyConstructArray_<T, Iterator, true, false> {
cannam@62 728 // Actually move-construct.
cannam@62 729
cannam@62 730 struct ExceptionGuard {
cannam@62 731 T* start;
cannam@62 732 T* pos;
cannam@62 733 inline explicit ExceptionGuard(T* pos): start(pos), pos(pos) {}
cannam@62 734 ~ExceptionGuard() noexcept(false) {
cannam@62 735 while (pos > start) {
cannam@62 736 dtor(*--pos);
cannam@62 737 }
cannam@62 738 }
cannam@62 739 };
cannam@62 740
cannam@62 741 static T* apply(T* __restrict__ pos, Iterator start, Iterator end) {
cannam@62 742 // Verify that T can be *implicitly* constructed from the source values.
cannam@62 743 if (false) implicitCast<T>(kj::mv(*start));
cannam@62 744
cannam@62 745 if (noexcept(T(kj::mv(*start)))) {
cannam@62 746 while (start != end) {
cannam@62 747 ctor(*pos++, kj::mv(*start++));
cannam@62 748 }
cannam@62 749 return pos;
cannam@62 750 } else {
cannam@62 751 // Crap. This is complicated.
cannam@62 752 ExceptionGuard guard(pos);
cannam@62 753 while (start != end) {
cannam@62 754 ctor(*guard.pos, kj::mv(*start++));
cannam@62 755 ++guard.pos;
cannam@62 756 }
cannam@62 757 guard.start = guard.pos;
cannam@62 758 return guard.pos;
cannam@62 759 }
cannam@62 760 }
cannam@62 761 };
cannam@62 762
cannam@62 763 } // namespace _ (private)
cannam@62 764
cannam@62 765 template <typename T>
cannam@62 766 template <typename Iterator, bool move>
cannam@62 767 void ArrayBuilder<T>::addAll(Iterator start, Iterator end) {
cannam@62 768 pos = _::CopyConstructArray_<RemoveConst<T>, Decay<Iterator>, move>::apply(pos, start, end);
cannam@62 769 }
cannam@62 770
cannam@62 771 template <typename T>
cannam@62 772 Array<T> heapArray(const T* content, size_t size) {
cannam@62 773 ArrayBuilder<T> builder = heapArrayBuilder<T>(size);
cannam@62 774 builder.addAll(content, content + size);
cannam@62 775 return builder.finish();
cannam@62 776 }
cannam@62 777
cannam@62 778 template <typename T>
cannam@62 779 Array<T> heapArray(T* content, size_t size) {
cannam@62 780 ArrayBuilder<T> builder = heapArrayBuilder<T>(size);
cannam@62 781 builder.addAll(content, content + size);
cannam@62 782 return builder.finish();
cannam@62 783 }
cannam@62 784
cannam@62 785 template <typename T>
cannam@62 786 Array<T> heapArray(ArrayPtr<T> content) {
cannam@62 787 ArrayBuilder<T> builder = heapArrayBuilder<T>(content.size());
cannam@62 788 builder.addAll(content);
cannam@62 789 return builder.finish();
cannam@62 790 }
cannam@62 791
cannam@62 792 template <typename T>
cannam@62 793 Array<T> heapArray(ArrayPtr<const T> content) {
cannam@62 794 ArrayBuilder<T> builder = heapArrayBuilder<T>(content.size());
cannam@62 795 builder.addAll(content);
cannam@62 796 return builder.finish();
cannam@62 797 }
cannam@62 798
cannam@62 799 template <typename T, typename Iterator> Array<T>
cannam@62 800 heapArray(Iterator begin, Iterator end) {
cannam@62 801 ArrayBuilder<T> builder = heapArrayBuilder<T>(end - begin);
cannam@62 802 builder.addAll(begin, end);
cannam@62 803 return builder.finish();
cannam@62 804 }
cannam@62 805
cannam@62 806 template <typename T>
cannam@62 807 inline Array<T> heapArray(std::initializer_list<T> init) {
cannam@62 808 return heapArray<T>(init.begin(), init.end());
cannam@62 809 }
cannam@62 810
cannam@62 811 } // namespace kj
cannam@62 812
cannam@62 813 #endif // KJ_ARRAY_H_