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