diff options
Diffstat (limited to 'deps/icu-small/source/common/cmemory.h')
-rw-r--r-- | deps/icu-small/source/common/cmemory.h | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/deps/icu-small/source/common/cmemory.h b/deps/icu-small/source/common/cmemory.h index a44f9a1902..f98e13efc0 100644 --- a/deps/icu-small/source/common/cmemory.h +++ b/deps/icu-small/source/common/cmemory.h @@ -300,6 +300,14 @@ public: */ ~MaybeStackArray() { releaseArray(); } /** + * Move constructor: transfers ownership or copies the stack array. + */ + MaybeStackArray(MaybeStackArray<T, stackCapacity> &&src) U_NOEXCEPT; + /** + * Move assignment: transfers ownership or copies the stack array. + */ + MaybeStackArray<T, stackCapacity> &operator=(MaybeStackArray<T, stackCapacity> &&src) U_NOEXCEPT; + /** * Returns the array capacity (number of T items). * @return array capacity */ @@ -376,6 +384,11 @@ private: uprv_free(ptr); } } + void resetToStackArray() { + ptr=stackArray; + capacity=stackCapacity; + needToRelease=FALSE; + } /* No comparison operators with other MaybeStackArray's. */ bool operator==(const MaybeStackArray & /*other*/) {return FALSE;} bool operator!=(const MaybeStackArray & /*other*/) {return TRUE;} @@ -399,6 +412,34 @@ private: }; template<typename T, int32_t stackCapacity> +icu::MaybeStackArray<T, stackCapacity>::MaybeStackArray( + MaybeStackArray <T, stackCapacity>&& src) U_NOEXCEPT + : ptr(src.ptr), capacity(src.capacity), needToRelease(src.needToRelease) { + if (src.ptr == src.stackArray) { + ptr = stackArray; + uprv_memcpy(stackArray, src.stackArray, sizeof(T) * src.capacity); + } else { + src.resetToStackArray(); // take ownership away from src + } +} + +template<typename T, int32_t stackCapacity> +inline MaybeStackArray <T, stackCapacity>& +MaybeStackArray<T, stackCapacity>::operator=(MaybeStackArray <T, stackCapacity>&& src) U_NOEXCEPT { + releaseArray(); // in case this instance had its own memory allocated + capacity = src.capacity; + needToRelease = src.needToRelease; + if (src.ptr == src.stackArray) { + ptr = stackArray; + uprv_memcpy(stackArray, src.stackArray, sizeof(T) * src.capacity); + } else { + ptr = src.ptr; + src.resetToStackArray(); // take ownership away from src + } + return *this; +} + +template<typename T, int32_t stackCapacity> inline T *MaybeStackArray<T, stackCapacity>::resize(int32_t newCapacity, int32_t length) { if(newCapacity>0) { #if U_DEBUG && defined(UPRV_MALLOC_COUNT) @@ -447,9 +488,7 @@ inline T *MaybeStackArray<T, stackCapacity>::orphanOrClone(int32_t length, int32 uprv_memcpy(p, ptr, (size_t)length*sizeof(T)); } resultCapacity=length; - ptr=stackArray; - capacity=stackCapacity; - needToRelease=FALSE; + resetToStackArray(); return p; } |