Chris@50: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@50: // Licensed under the MIT License: Chris@50: // Chris@50: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@50: // of this software and associated documentation files (the "Software"), to deal Chris@50: // in the Software without restriction, including without limitation the rights Chris@50: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@50: // copies of the Software, and to permit persons to whom the Software is Chris@50: // furnished to do so, subject to the following conditions: Chris@50: // Chris@50: // The above copyright notice and this permission notice shall be included in Chris@50: // all copies or substantial portions of the Software. Chris@50: // Chris@50: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@50: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@50: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@50: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@50: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@50: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@50: // THE SOFTWARE. Chris@50: Chris@50: #ifndef KJ_ARRAY_H_ Chris@50: #define KJ_ARRAY_H_ Chris@50: Chris@50: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@50: #pragma GCC system_header Chris@50: #endif Chris@50: Chris@50: #include "common.h" Chris@50: #include Chris@50: #include Chris@50: Chris@50: namespace kj { Chris@50: Chris@50: // ======================================================================================= Chris@50: // ArrayDisposer -- Implementation details. Chris@50: Chris@50: class ArrayDisposer { Chris@50: // Much like Disposer from memory.h. Chris@50: Chris@50: protected: Chris@50: // Do not declare a destructor, as doing so will force a global initializer for Chris@50: // HeapArrayDisposer::instance. Chris@50: Chris@50: virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, Chris@50: size_t capacity, void (*destroyElement)(void*)) const = 0; Chris@50: // Disposes of the array. `destroyElement` invokes the destructor of each element, or is nullptr Chris@50: // if the elements have trivial destructors. `capacity` is the amount of space that was Chris@50: // allocated while `elementCount` is the number of elements that were actually constructed; Chris@50: // these are always the same number for Array but may be different when using ArrayBuilder. Chris@50: Chris@50: public: Chris@50: Chris@50: template Chris@50: void dispose(T* firstElement, size_t elementCount, size_t capacity) const; Chris@50: // Helper wrapper around disposeImpl(). Chris@50: // Chris@50: // Callers must not call dispose() on the same array twice, even if the first call throws Chris@50: // an exception. Chris@50: Chris@50: private: Chris@50: template Chris@50: struct Dispose_; Chris@50: }; Chris@50: Chris@50: class ExceptionSafeArrayUtil { Chris@50: // Utility class that assists in constructing or destroying elements of an array, where the Chris@50: // constructor or destructor could throw exceptions. In case of an exception, Chris@50: // ExceptionSafeArrayUtil's destructor will call destructors on all elements that have been Chris@50: // constructed but not destroyed. Remember that destructors that throw exceptions are required Chris@50: // to use UnwindDetector to detect unwind and avoid exceptions in this case. Therefore, no more Chris@50: // than one exception will be thrown (and the program will not terminate). Chris@50: Chris@50: public: Chris@50: inline ExceptionSafeArrayUtil(void* ptr, size_t elementSize, size_t constructedElementCount, Chris@50: void (*destroyElement)(void*)) Chris@50: : pos(reinterpret_cast(ptr) + elementSize * constructedElementCount), Chris@50: elementSize(elementSize), constructedElementCount(constructedElementCount), Chris@50: destroyElement(destroyElement) {} Chris@50: KJ_DISALLOW_COPY(ExceptionSafeArrayUtil); Chris@50: Chris@50: inline ~ExceptionSafeArrayUtil() noexcept(false) { Chris@50: if (constructedElementCount > 0) destroyAll(); Chris@50: } Chris@50: Chris@50: void construct(size_t count, void (*constructElement)(void*)); Chris@50: // Construct the given number of elements. Chris@50: Chris@50: void destroyAll(); Chris@50: // Destroy all elements. Call this immediately before ExceptionSafeArrayUtil goes out-of-scope Chris@50: // to ensure that one element throwing an exception does not prevent the others from being Chris@50: // destroyed. Chris@50: Chris@50: void release() { constructedElementCount = 0; } Chris@50: // Prevent ExceptionSafeArrayUtil's destructor from destroying the constructed elements. Chris@50: // Call this after you've successfully finished constructing. Chris@50: Chris@50: private: Chris@50: byte* pos; Chris@50: size_t elementSize; Chris@50: size_t constructedElementCount; Chris@50: void (*destroyElement)(void*); Chris@50: }; Chris@50: Chris@50: class DestructorOnlyArrayDisposer: public ArrayDisposer { Chris@50: public: Chris@50: static const DestructorOnlyArrayDisposer instance; Chris@50: Chris@50: void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, Chris@50: size_t capacity, void (*destroyElement)(void*)) const override; Chris@50: }; Chris@50: Chris@50: class NullArrayDisposer: public ArrayDisposer { Chris@50: // An ArrayDisposer that does nothing. Can be used to construct a fake Arrays that doesn't Chris@50: // actually own its content. Chris@50: Chris@50: public: Chris@50: static const NullArrayDisposer instance; Chris@50: Chris@50: void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, Chris@50: size_t capacity, void (*destroyElement)(void*)) const override; Chris@50: }; Chris@50: Chris@50: // ======================================================================================= Chris@50: // Array Chris@50: Chris@50: template Chris@50: class Array { Chris@50: // An owned array which will automatically be disposed of (using an ArrayDisposer) in the Chris@50: // destructor. Can be moved, but not copied. Much like Own, but for arrays rather than Chris@50: // single objects. Chris@50: Chris@50: public: Chris@50: inline Array(): ptr(nullptr), size_(0), disposer(nullptr) {} Chris@50: inline Array(decltype(nullptr)): ptr(nullptr), size_(0), disposer(nullptr) {} Chris@50: inline Array(Array&& other) noexcept Chris@50: : ptr(other.ptr), size_(other.size_), disposer(other.disposer) { Chris@50: other.ptr = nullptr; Chris@50: other.size_ = 0; Chris@50: } Chris@50: inline Array(Array>&& other) noexcept Chris@50: : ptr(other.ptr), size_(other.size_), disposer(other.disposer) { Chris@50: other.ptr = nullptr; Chris@50: other.size_ = 0; Chris@50: } Chris@50: inline Array(T* firstElement, size_t size, const ArrayDisposer& disposer) Chris@50: : ptr(firstElement), size_(size), disposer(&disposer) {} Chris@50: Chris@50: KJ_DISALLOW_COPY(Array); Chris@50: inline ~Array() noexcept { dispose(); } Chris@50: Chris@50: inline operator ArrayPtr() { Chris@50: return ArrayPtr(ptr, size_); Chris@50: } Chris@50: inline operator ArrayPtr() const { Chris@50: return ArrayPtr(ptr, size_); Chris@50: } Chris@50: inline ArrayPtr asPtr() { Chris@50: return ArrayPtr(ptr, size_); Chris@50: } Chris@50: inline ArrayPtr asPtr() const { Chris@50: return ArrayPtr(ptr, size_); Chris@50: } Chris@50: Chris@50: inline size_t size() const { return size_; } Chris@50: inline T& operator[](size_t index) const { Chris@50: KJ_IREQUIRE(index < size_, "Out-of-bounds Array access."); Chris@50: return ptr[index]; Chris@50: } Chris@50: Chris@50: inline const T* begin() const { return ptr; } Chris@50: inline const T* end() const { return ptr + size_; } Chris@50: inline const T& front() const { return *ptr; } Chris@50: inline const T& back() const { return *(ptr + size_ - 1); } Chris@50: inline T* begin() { return ptr; } Chris@50: inline T* end() { return ptr + size_; } Chris@50: inline T& front() { return *ptr; } Chris@50: inline T& back() { return *(ptr + size_ - 1); } Chris@50: Chris@50: inline ArrayPtr slice(size_t start, size_t end) { Chris@50: KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice()."); Chris@50: return ArrayPtr(ptr + start, end - start); Chris@50: } Chris@50: inline ArrayPtr slice(size_t start, size_t end) const { Chris@50: KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice()."); Chris@50: return ArrayPtr(ptr + start, end - start); Chris@50: } Chris@50: Chris@50: inline ArrayPtr asBytes() const { return asPtr().asBytes(); } Chris@50: inline ArrayPtr> asBytes() { return asPtr().asBytes(); } Chris@50: inline ArrayPtr asChars() const { return asPtr().asChars(); } Chris@50: inline ArrayPtr> asChars() { return asPtr().asChars(); } Chris@50: Chris@50: inline Array> releaseAsBytes() { Chris@50: // Like asBytes() but transfers ownership. Chris@50: static_assert(sizeof(T) == sizeof(byte), Chris@50: "releaseAsBytes() only possible on arrays with byte-size elements (e.g. chars)."); Chris@50: Array> result( Chris@50: reinterpret_cast*>(ptr), size_, *disposer); Chris@50: ptr = nullptr; Chris@50: size_ = 0; Chris@50: return result; Chris@50: } Chris@50: inline Array> releaseAsChars() { Chris@50: // Like asChars() but transfers ownership. Chris@50: static_assert(sizeof(T) == sizeof(PropagateConst), Chris@50: "releaseAsChars() only possible on arrays with char-size elements (e.g. bytes)."); Chris@50: Array> result( Chris@50: reinterpret_cast*>(ptr), size_, *disposer); Chris@50: ptr = nullptr; Chris@50: size_ = 0; Chris@50: return result; Chris@50: } Chris@50: Chris@50: inline bool operator==(decltype(nullptr)) const { return size_ == 0; } Chris@50: inline bool operator!=(decltype(nullptr)) const { return size_ != 0; } Chris@50: Chris@50: inline Array& operator=(decltype(nullptr)) { Chris@50: dispose(); Chris@50: return *this; Chris@50: } Chris@50: Chris@50: inline Array& operator=(Array&& other) { Chris@50: dispose(); Chris@50: ptr = other.ptr; Chris@50: size_ = other.size_; Chris@50: disposer = other.disposer; Chris@50: other.ptr = nullptr; Chris@50: other.size_ = 0; Chris@50: return *this; Chris@50: } Chris@50: Chris@50: private: Chris@50: T* ptr; Chris@50: size_t size_; Chris@50: const ArrayDisposer* disposer; Chris@50: Chris@50: inline void dispose() { Chris@50: // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly Chris@50: // dispose again. Chris@50: T* ptrCopy = ptr; Chris@50: size_t sizeCopy = size_; Chris@50: if (ptrCopy != nullptr) { Chris@50: ptr = nullptr; Chris@50: size_ = 0; Chris@50: disposer->dispose(ptrCopy, sizeCopy, sizeCopy); Chris@50: } Chris@50: } Chris@50: Chris@50: template Chris@50: friend class Array; Chris@50: }; Chris@50: Chris@50: namespace _ { // private Chris@50: Chris@50: class HeapArrayDisposer final: public ArrayDisposer { Chris@50: public: Chris@50: template Chris@50: static T* allocate(size_t count); Chris@50: template Chris@50: static T* allocateUninitialized(size_t count); Chris@50: Chris@50: static const HeapArrayDisposer instance; Chris@50: Chris@50: private: Chris@50: static void* allocateImpl(size_t elementSize, size_t elementCount, size_t capacity, Chris@50: void (*constructElement)(void*), void (*destroyElement)(void*)); Chris@50: // Allocates and constructs the array. Both function pointers are null if the constructor is Chris@50: // trivial, otherwise destroyElement is null if the constructor doesn't throw. Chris@50: Chris@50: virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount, Chris@50: size_t capacity, void (*destroyElement)(void*)) const override; Chris@50: Chris@50: template Chris@50: struct Allocate_; Chris@50: }; Chris@50: Chris@50: } // namespace _ (private) Chris@50: Chris@50: template Chris@50: inline Array heapArray(size_t size) { Chris@50: // Much like `heap()` from memory.h, allocates a new array on the heap. Chris@50: Chris@50: return Array(_::HeapArrayDisposer::allocate(size), size, Chris@50: _::HeapArrayDisposer::instance); Chris@50: } Chris@50: Chris@50: template Array heapArray(const T* content, size_t size); Chris@50: template Array heapArray(ArrayPtr content); Chris@50: template Array heapArray(ArrayPtr content); Chris@50: template Array heapArray(Iterator begin, Iterator end); Chris@50: template Array heapArray(std::initializer_list init); Chris@50: // Allocate a heap array containing a copy of the given content. Chris@50: Chris@50: template Chris@50: Array heapArrayFromIterable(Container&& a) { return heapArray(a.begin(), a.end()); } Chris@50: template Chris@50: Array heapArrayFromIterable(Array&& a) { return mv(a); } Chris@50: Chris@50: // ======================================================================================= Chris@50: // ArrayBuilder Chris@50: Chris@50: template Chris@50: class ArrayBuilder { Chris@50: // Class which lets you build an Array specifying the exact constructor arguments for each Chris@50: // element, rather than starting by default-constructing them. Chris@50: Chris@50: public: Chris@50: ArrayBuilder(): ptr(nullptr), pos(nullptr), endPtr(nullptr) {} Chris@50: ArrayBuilder(decltype(nullptr)): ptr(nullptr), pos(nullptr), endPtr(nullptr) {} Chris@50: explicit ArrayBuilder(RemoveConst* firstElement, size_t capacity, Chris@50: const ArrayDisposer& disposer) Chris@50: : ptr(firstElement), pos(firstElement), endPtr(firstElement + capacity), Chris@50: disposer(&disposer) {} Chris@50: ArrayBuilder(ArrayBuilder&& other) Chris@50: : ptr(other.ptr), pos(other.pos), endPtr(other.endPtr), disposer(other.disposer) { Chris@50: other.ptr = nullptr; Chris@50: other.pos = nullptr; Chris@50: other.endPtr = nullptr; Chris@50: } Chris@50: KJ_DISALLOW_COPY(ArrayBuilder); Chris@50: inline ~ArrayBuilder() noexcept(false) { dispose(); } Chris@50: Chris@50: inline operator ArrayPtr() { Chris@50: return arrayPtr(ptr, pos); Chris@50: } Chris@50: inline operator ArrayPtr() const { Chris@50: return arrayPtr(ptr, pos); Chris@50: } Chris@50: inline ArrayPtr asPtr() { Chris@50: return arrayPtr(ptr, pos); Chris@50: } Chris@50: inline ArrayPtr asPtr() const { Chris@50: return arrayPtr(ptr, pos); Chris@50: } Chris@50: Chris@50: inline size_t size() const { return pos - ptr; } Chris@50: inline size_t capacity() const { return endPtr - ptr; } Chris@50: inline T& operator[](size_t index) const { Chris@50: KJ_IREQUIRE(index < implicitCast(pos - ptr), "Out-of-bounds Array access."); Chris@50: return ptr[index]; Chris@50: } Chris@50: Chris@50: inline const T* begin() const { return ptr; } Chris@50: inline const T* end() const { return pos; } Chris@50: inline const T& front() const { return *ptr; } Chris@50: inline const T& back() const { return *(pos - 1); } Chris@50: inline T* begin() { return ptr; } Chris@50: inline T* end() { return pos; } Chris@50: inline T& front() { return *ptr; } Chris@50: inline T& back() { return *(pos - 1); } Chris@50: Chris@50: ArrayBuilder& operator=(ArrayBuilder&& other) { Chris@50: dispose(); Chris@50: ptr = other.ptr; Chris@50: pos = other.pos; Chris@50: endPtr = other.endPtr; Chris@50: disposer = other.disposer; Chris@50: other.ptr = nullptr; Chris@50: other.pos = nullptr; Chris@50: other.endPtr = nullptr; Chris@50: return *this; Chris@50: } Chris@50: ArrayBuilder& operator=(decltype(nullptr)) { Chris@50: dispose(); Chris@50: return *this; Chris@50: } Chris@50: Chris@50: template Chris@50: T& add(Params&&... params) { Chris@50: KJ_IREQUIRE(pos < endPtr, "Added too many elements to ArrayBuilder."); Chris@50: ctor(*pos, kj::fwd(params)...); Chris@50: return *pos++; Chris@50: } Chris@50: Chris@50: template Chris@50: void addAll(Container&& container) { Chris@50: addAll(container.begin(), container.end()); Chris@50: } Chris@50: Chris@50: template Chris@50: void addAll(Iterator start, Iterator end); Chris@50: Chris@50: void removeLast() { Chris@50: KJ_IREQUIRE(pos > ptr, "No elements present to remove."); Chris@50: kj::dtor(*--pos); Chris@50: } Chris@50: Chris@50: Array finish() { Chris@50: // We could safely remove this check if we assume that the disposer implementation doesn't Chris@50: // need to know the original capacity, as is thes case with HeapArrayDisposer since it uses Chris@50: // operator new() or if we created a custom disposer for ArrayBuilder which stores the capacity Chris@50: // in a prefix. But that would make it hard to write cleverer heap allocators, and anyway this Chris@50: // check might catch bugs. Probably people should use Vector if they want to build arrays Chris@50: // without knowing the final size in advance. Chris@50: KJ_IREQUIRE(pos == endPtr, "ArrayBuilder::finish() called prematurely."); Chris@50: Array result(reinterpret_cast(ptr), pos - ptr, *disposer); Chris@50: ptr = nullptr; Chris@50: pos = nullptr; Chris@50: endPtr = nullptr; Chris@50: return result; Chris@50: } Chris@50: Chris@50: inline bool isFull() const { Chris@50: return pos == endPtr; Chris@50: } Chris@50: Chris@50: private: Chris@50: T* ptr; Chris@50: RemoveConst* pos; Chris@50: T* endPtr; Chris@50: const ArrayDisposer* disposer; Chris@50: Chris@50: inline void dispose() { Chris@50: // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly Chris@50: // dispose again. Chris@50: T* ptrCopy = ptr; Chris@50: T* posCopy = pos; Chris@50: T* endCopy = endPtr; Chris@50: if (ptrCopy != nullptr) { Chris@50: ptr = nullptr; Chris@50: pos = nullptr; Chris@50: endPtr = nullptr; Chris@50: disposer->dispose(ptrCopy, posCopy - ptrCopy, endCopy - ptrCopy); Chris@50: } Chris@50: } Chris@50: }; Chris@50: Chris@50: template Chris@50: inline ArrayBuilder heapArrayBuilder(size_t size) { Chris@50: // Like `heapArray()` but does not default-construct the elements. You must construct them Chris@50: // manually by calling `add()`. Chris@50: Chris@50: return ArrayBuilder(_::HeapArrayDisposer::allocateUninitialized>(size), Chris@50: size, _::HeapArrayDisposer::instance); Chris@50: } Chris@50: Chris@50: // ======================================================================================= Chris@50: // Inline Arrays Chris@50: Chris@50: template Chris@50: class FixedArray { Chris@50: // A fixed-width array whose storage is allocated inline rather than on the heap. Chris@50: Chris@50: public: Chris@50: inline size_t size() const { return fixedSize; } Chris@50: inline T* begin() { return content; } Chris@50: inline T* end() { return content + fixedSize; } Chris@50: inline const T* begin() const { return content; } Chris@50: inline const T* end() const { return content + fixedSize; } Chris@50: Chris@50: inline operator ArrayPtr() { Chris@50: return arrayPtr(content, fixedSize); Chris@50: } Chris@50: inline operator ArrayPtr() const { Chris@50: return arrayPtr(content, fixedSize); Chris@50: } Chris@50: Chris@50: inline T& operator[](size_t index) { return content[index]; } Chris@50: inline const T& operator[](size_t index) const { return content[index]; } Chris@50: Chris@50: private: Chris@50: T content[fixedSize]; Chris@50: }; Chris@50: Chris@50: template Chris@50: class CappedArray { Chris@50: // Like `FixedArray` but can be dynamically resized as long as the size does not exceed the limit Chris@50: // specified by the template parameter. Chris@50: // Chris@50: // TODO(someday): Don't construct elements past currentSize? Chris@50: Chris@50: public: Chris@50: inline KJ_CONSTEXPR() CappedArray(): currentSize(fixedSize) {} Chris@50: inline explicit constexpr CappedArray(size_t s): currentSize(s) {} Chris@50: Chris@50: inline size_t size() const { return currentSize; } Chris@50: inline void setSize(size_t s) { KJ_IREQUIRE(s <= fixedSize); currentSize = s; } Chris@50: inline T* begin() { return content; } Chris@50: inline T* end() { return content + currentSize; } Chris@50: inline const T* begin() const { return content; } Chris@50: inline const T* end() const { return content + currentSize; } Chris@50: Chris@50: inline operator ArrayPtr() { Chris@50: return arrayPtr(content, currentSize); Chris@50: } Chris@50: inline operator ArrayPtr() const { Chris@50: return arrayPtr(content, currentSize); Chris@50: } Chris@50: Chris@50: inline T& operator[](size_t index) { return content[index]; } Chris@50: inline const T& operator[](size_t index) const { return content[index]; } Chris@50: Chris@50: private: Chris@50: size_t currentSize; Chris@50: T content[fixedSize]; Chris@50: }; Chris@50: Chris@50: // ======================================================================================= Chris@50: // KJ_MAP Chris@50: Chris@50: #define KJ_MAP(elementName, array) \ Chris@50: ::kj::_::Mapper(array) * [&](decltype(*(array).begin()) elementName) Chris@50: // Applies some function to every element of an array, returning an Array of the results, with Chris@50: // nice syntax. Example: Chris@50: // Chris@50: // StringPtr foo = "abcd"; Chris@50: // Array bar = KJ_MAP(c, foo) -> char { return c + 1; }; Chris@50: // KJ_ASSERT(str(bar) == "bcde"); Chris@50: Chris@50: namespace _ { // private Chris@50: Chris@50: template Chris@50: struct Mapper { Chris@50: T array; Chris@50: Mapper(T&& array): array(kj::fwd(array)) {} Chris@50: template Chris@50: auto operator*(Func&& func) -> Array { Chris@50: auto builder = heapArrayBuilder(array.size()); Chris@50: for (auto iter = array.begin(); iter != array.end(); ++iter) { Chris@50: builder.add(func(*iter)); Chris@50: } Chris@50: return builder.finish(); Chris@50: } Chris@50: }; Chris@50: Chris@50: } // namespace _ (private) Chris@50: Chris@50: // ======================================================================================= Chris@50: // Inline implementation details Chris@50: Chris@50: template Chris@50: struct ArrayDisposer::Dispose_ { Chris@50: static void dispose(T* firstElement, size_t elementCount, size_t capacity, Chris@50: const ArrayDisposer& disposer) { Chris@50: disposer.disposeImpl(const_cast*>(firstElement), Chris@50: sizeof(T), elementCount, capacity, nullptr); Chris@50: } Chris@50: }; Chris@50: template Chris@50: struct ArrayDisposer::Dispose_ { Chris@50: static void destruct(void* ptr) { Chris@50: kj::dtor(*reinterpret_cast(ptr)); Chris@50: } Chris@50: Chris@50: static void dispose(T* firstElement, size_t elementCount, size_t capacity, Chris@50: const ArrayDisposer& disposer) { Chris@50: disposer.disposeImpl(firstElement, sizeof(T), elementCount, capacity, &destruct); Chris@50: } Chris@50: }; Chris@50: Chris@50: template Chris@50: void ArrayDisposer::dispose(T* firstElement, size_t elementCount, size_t capacity) const { Chris@50: Dispose_::dispose(firstElement, elementCount, capacity, *this); Chris@50: } Chris@50: Chris@50: namespace _ { // private Chris@50: Chris@50: template Chris@50: struct HeapArrayDisposer::Allocate_ { Chris@50: static T* allocate(size_t elementCount, size_t capacity) { Chris@50: return reinterpret_cast(allocateImpl( Chris@50: sizeof(T), elementCount, capacity, nullptr, nullptr)); Chris@50: } Chris@50: }; Chris@50: template Chris@50: struct HeapArrayDisposer::Allocate_ { Chris@50: static void construct(void* ptr) { Chris@50: kj::ctor(*reinterpret_cast(ptr)); Chris@50: } Chris@50: static T* allocate(size_t elementCount, size_t capacity) { Chris@50: return reinterpret_cast(allocateImpl( Chris@50: sizeof(T), elementCount, capacity, &construct, nullptr)); Chris@50: } Chris@50: }; Chris@50: template Chris@50: struct HeapArrayDisposer::Allocate_ { Chris@50: static void construct(void* ptr) { Chris@50: kj::ctor(*reinterpret_cast(ptr)); Chris@50: } Chris@50: static void destruct(void* ptr) { Chris@50: kj::dtor(*reinterpret_cast(ptr)); Chris@50: } Chris@50: static T* allocate(size_t elementCount, size_t capacity) { Chris@50: return reinterpret_cast(allocateImpl( Chris@50: sizeof(T), elementCount, capacity, &construct, &destruct)); Chris@50: } Chris@50: }; Chris@50: Chris@50: template Chris@50: T* HeapArrayDisposer::allocate(size_t count) { Chris@50: return Allocate_::allocate(count, count); Chris@50: } Chris@50: Chris@50: template Chris@50: T* HeapArrayDisposer::allocateUninitialized(size_t count) { Chris@50: return Allocate_::allocate(0, count); Chris@50: } Chris@50: Chris@50: template ()> Chris@50: struct CopyConstructArray_; Chris@50: Chris@50: template Chris@50: struct CopyConstructArray_ { Chris@50: static inline T* apply(T* __restrict__ pos, T* start, T* end) { Chris@50: memcpy(pos, start, reinterpret_cast(end) - reinterpret_cast(start)); Chris@50: return pos + (end - start); Chris@50: } Chris@50: }; Chris@50: Chris@50: template Chris@50: struct CopyConstructArray_ { Chris@50: static inline T* apply(T* __restrict__ pos, const T* start, const T* end) { Chris@50: memcpy(pos, start, reinterpret_cast(end) - reinterpret_cast(start)); Chris@50: return pos + (end - start); Chris@50: } Chris@50: }; Chris@50: Chris@50: template Chris@50: struct CopyConstructArray_ { Chris@50: static inline T* apply(T* __restrict__ pos, Iterator start, Iterator end) { Chris@50: // Since both the copy constructor and assignment operator are trivial, we know that assignment Chris@50: // is equivalent to copy-constructing. So we can make this case somewhat easier for the Chris@50: // compiler to optimize. Chris@50: while (start != end) { Chris@50: *pos++ = *start++; Chris@50: } Chris@50: return pos; Chris@50: } Chris@50: }; Chris@50: Chris@50: template Chris@50: struct CopyConstructArray_ { Chris@50: struct ExceptionGuard { Chris@50: T* start; Chris@50: T* pos; Chris@50: inline explicit ExceptionGuard(T* pos): start(pos), pos(pos) {} Chris@50: ~ExceptionGuard() noexcept(false) { Chris@50: while (pos > start) { Chris@50: dtor(*--pos); Chris@50: } Chris@50: } Chris@50: }; Chris@50: Chris@50: static T* apply(T* __restrict__ pos, Iterator start, Iterator end) { Chris@50: // Verify that T can be *implicitly* constructed from the source values. Chris@50: if (false) implicitCast(*start); Chris@50: Chris@50: if (noexcept(T(*start))) { Chris@50: while (start != end) { Chris@50: ctor(*pos++, *start++); Chris@50: } Chris@50: return pos; Chris@50: } else { Chris@50: // Crap. This is complicated. Chris@50: ExceptionGuard guard(pos); Chris@50: while (start != end) { Chris@50: ctor(*guard.pos, *start++); Chris@50: ++guard.pos; Chris@50: } Chris@50: guard.start = guard.pos; Chris@50: return guard.pos; Chris@50: } Chris@50: } Chris@50: }; Chris@50: Chris@50: template Chris@50: inline T* copyConstructArray(T* dst, Iterator start, Iterator end) { Chris@50: return CopyConstructArray_>::apply(dst, start, end); Chris@50: } Chris@50: Chris@50: } // namespace _ (private) Chris@50: Chris@50: template Chris@50: template Chris@50: void ArrayBuilder::addAll(Iterator start, Iterator end) { Chris@50: pos = _::copyConstructArray(pos, start, end); Chris@50: } Chris@50: Chris@50: template Chris@50: Array heapArray(const T* content, size_t size) { Chris@50: ArrayBuilder builder = heapArrayBuilder(size); Chris@50: builder.addAll(content, content + size); Chris@50: return builder.finish(); Chris@50: } Chris@50: Chris@50: template Chris@50: Array heapArray(T* content, size_t size) { Chris@50: ArrayBuilder builder = heapArrayBuilder(size); Chris@50: builder.addAll(content, content + size); Chris@50: return builder.finish(); Chris@50: } Chris@50: Chris@50: template Chris@50: Array heapArray(ArrayPtr content) { Chris@50: ArrayBuilder builder = heapArrayBuilder(content.size()); Chris@50: builder.addAll(content); Chris@50: return builder.finish(); Chris@50: } Chris@50: Chris@50: template Chris@50: Array heapArray(ArrayPtr content) { Chris@50: ArrayBuilder builder = heapArrayBuilder(content.size()); Chris@50: builder.addAll(content); Chris@50: return builder.finish(); Chris@50: } Chris@50: Chris@50: template Array Chris@50: heapArray(Iterator begin, Iterator end) { Chris@50: ArrayBuilder builder = heapArrayBuilder(end - begin); Chris@50: builder.addAll(begin, end); Chris@50: return builder.finish(); Chris@50: } Chris@50: Chris@50: template Chris@50: inline Array heapArray(std::initializer_list init) { Chris@50: return heapArray(init.begin(), init.end()); Chris@50: } Chris@50: Chris@50: } // namespace kj Chris@50: Chris@50: #endif // KJ_ARRAY_H_