annotate win64-msvc/include/kj/array.h @ 169:223a55898ab9 tip default

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