michael@0: /* michael@0: * Copyright (c) 2008-2010 Mozilla Foundation michael@0: * michael@0: * Permission is hereby granted, free of charge, to any person obtaining a michael@0: * copy of this software and associated documentation files (the "Software"), michael@0: * to deal in the Software without restriction, including without limitation michael@0: * the rights to use, copy, modify, merge, publish, distribute, sublicense, michael@0: * and/or sell copies of the Software, and to permit persons to whom the michael@0: * Software is furnished to do so, subject to the following conditions: michael@0: * michael@0: * The above copyright notice and this permission notice shall be included in michael@0: * all copies or substantial portions of the Software. michael@0: * michael@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR michael@0: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, michael@0: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL michael@0: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER michael@0: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING michael@0: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER michael@0: * DEALINGS IN THE SOFTWARE. michael@0: */ michael@0: michael@0: #ifndef jArray_h michael@0: #define jArray_h michael@0: michael@0: #include "mozilla/NullPtr.h" michael@0: #include "nsDebug.h" michael@0: michael@0: template michael@0: struct staticJArray { michael@0: const T* arr; michael@0: const L length; michael@0: operator T*() { return arr; } michael@0: T& operator[] (L const index) { return ((T*)arr)[index]; } michael@0: L binarySearch(T const elem) { michael@0: L lo = 0; michael@0: L hi = length - 1; michael@0: while (lo <= hi) { michael@0: L mid = (lo + hi) / 2; michael@0: if (arr[mid] > elem) { michael@0: hi = mid - 1; michael@0: } else if (arr[mid] < elem) { michael@0: lo = mid + 1; michael@0: } else { michael@0: return mid; michael@0: } michael@0: } michael@0: return -1; michael@0: } michael@0: }; michael@0: michael@0: template michael@0: struct jArray { michael@0: T* arr; michael@0: L length; michael@0: static jArray newJArray(L const len) { michael@0: NS_ASSERTION(len >= 0, "Bad length."); michael@0: jArray newArray = { new T[len], len }; michael@0: return newArray; michael@0: } michael@0: operator T*() { return arr; } michael@0: T& operator[] (L const index) { return arr[index]; } michael@0: void operator=(staticJArray& other) { michael@0: arr = (T*)other.arr; michael@0: length = other.length; michael@0: } michael@0: }; michael@0: michael@0: template michael@0: class autoJArray { michael@0: private: michael@0: T* arr; michael@0: public: michael@0: L length; michael@0: autoJArray() michael@0: : arr(0) michael@0: , length(0) michael@0: { michael@0: } michael@0: autoJArray(const jArray& other) michael@0: : arr(other.arr) michael@0: , length(other.length) michael@0: { michael@0: } michael@0: ~autoJArray() michael@0: { michael@0: delete[] arr; michael@0: } michael@0: operator T*() { return arr; } michael@0: T& operator[] (L const index) { return arr[index]; } michael@0: operator jArray() { michael@0: // WARNING! This makes it possible to goof with buffer ownership! michael@0: // This is needed for the getStack and getListOfActiveFormattingElements michael@0: // methods to work sensibly. michael@0: jArray newArray = { arr, length }; michael@0: return newArray; michael@0: } michael@0: void operator=(const jArray& other) { michael@0: delete[] arr; michael@0: arr = other.arr; michael@0: length = other.length; michael@0: } michael@0: void operator=(mozilla::NullptrT n) { michael@0: // Make assigning null to an array in Java delete the buffer in C++ michael@0: MOZ_ASSERT(n == nullptr); michael@0: delete[] arr; michael@0: arr = nullptr; michael@0: length = 0; michael@0: } michael@0: }; michael@0: michael@0: #endif // jArray_h