parser/html/jArray.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /*
michael@0 2 * Copyright (c) 2008-2010 Mozilla Foundation
michael@0 3 *
michael@0 4 * Permission is hereby granted, free of charge, to any person obtaining a
michael@0 5 * copy of this software and associated documentation files (the "Software"),
michael@0 6 * to deal in the Software without restriction, including without limitation
michael@0 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
michael@0 8 * and/or sell copies of the Software, and to permit persons to whom the
michael@0 9 * Software is furnished to do so, subject to the following conditions:
michael@0 10 *
michael@0 11 * The above copyright notice and this permission notice shall be included in
michael@0 12 * all copies or substantial portions of the Software.
michael@0 13 *
michael@0 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
michael@0 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
michael@0 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
michael@0 17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
michael@0 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
michael@0 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
michael@0 20 * DEALINGS IN THE SOFTWARE.
michael@0 21 */
michael@0 22
michael@0 23 #ifndef jArray_h
michael@0 24 #define jArray_h
michael@0 25
michael@0 26 #include "mozilla/NullPtr.h"
michael@0 27 #include "nsDebug.h"
michael@0 28
michael@0 29 template<class T, class L>
michael@0 30 struct staticJArray {
michael@0 31 const T* arr;
michael@0 32 const L length;
michael@0 33 operator T*() { return arr; }
michael@0 34 T& operator[] (L const index) { return ((T*)arr)[index]; }
michael@0 35 L binarySearch(T const elem) {
michael@0 36 L lo = 0;
michael@0 37 L hi = length - 1;
michael@0 38 while (lo <= hi) {
michael@0 39 L mid = (lo + hi) / 2;
michael@0 40 if (arr[mid] > elem) {
michael@0 41 hi = mid - 1;
michael@0 42 } else if (arr[mid] < elem) {
michael@0 43 lo = mid + 1;
michael@0 44 } else {
michael@0 45 return mid;
michael@0 46 }
michael@0 47 }
michael@0 48 return -1;
michael@0 49 }
michael@0 50 };
michael@0 51
michael@0 52 template<class T, class L>
michael@0 53 struct jArray {
michael@0 54 T* arr;
michael@0 55 L length;
michael@0 56 static jArray<T,L> newJArray(L const len) {
michael@0 57 NS_ASSERTION(len >= 0, "Bad length.");
michael@0 58 jArray<T,L> newArray = { new T[len], len };
michael@0 59 return newArray;
michael@0 60 }
michael@0 61 operator T*() { return arr; }
michael@0 62 T& operator[] (L const index) { return arr[index]; }
michael@0 63 void operator=(staticJArray<T,L>& other) {
michael@0 64 arr = (T*)other.arr;
michael@0 65 length = other.length;
michael@0 66 }
michael@0 67 };
michael@0 68
michael@0 69 template<class T, class L>
michael@0 70 class autoJArray {
michael@0 71 private:
michael@0 72 T* arr;
michael@0 73 public:
michael@0 74 L length;
michael@0 75 autoJArray()
michael@0 76 : arr(0)
michael@0 77 , length(0)
michael@0 78 {
michael@0 79 }
michael@0 80 autoJArray(const jArray<T,L>& other)
michael@0 81 : arr(other.arr)
michael@0 82 , length(other.length)
michael@0 83 {
michael@0 84 }
michael@0 85 ~autoJArray()
michael@0 86 {
michael@0 87 delete[] arr;
michael@0 88 }
michael@0 89 operator T*() { return arr; }
michael@0 90 T& operator[] (L const index) { return arr[index]; }
michael@0 91 operator jArray<T,L>() {
michael@0 92 // WARNING! This makes it possible to goof with buffer ownership!
michael@0 93 // This is needed for the getStack and getListOfActiveFormattingElements
michael@0 94 // methods to work sensibly.
michael@0 95 jArray<T,L> newArray = { arr, length };
michael@0 96 return newArray;
michael@0 97 }
michael@0 98 void operator=(const jArray<T,L>& other) {
michael@0 99 delete[] arr;
michael@0 100 arr = other.arr;
michael@0 101 length = other.length;
michael@0 102 }
michael@0 103 void operator=(mozilla::NullptrT n) {
michael@0 104 // Make assigning null to an array in Java delete the buffer in C++
michael@0 105 MOZ_ASSERT(n == nullptr);
michael@0 106 delete[] arr;
michael@0 107 arr = nullptr;
michael@0 108 length = 0;
michael@0 109 }
michael@0 110 };
michael@0 111
michael@0 112 #endif // jArray_h

mercurial