-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy patharraylist.rn
100 lines (93 loc) · 2.96 KB
/
arraylist.rn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2021 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
transformer ArrayList(A: Class, B: Class, cascadeDelete:bool = false,
labelA: string = "", labelB: string = "", pluralB = "") {
if pluralB == "" {
pluralB = "$B_s"
}
prependcode A {
self.$labelB$pluralB = arrayof(B)
func resize$labelB$pluralB(self, num$labelB$pluralB: Uint) {
length = self.$labelB$pluralB.length()
if num$labelB$B < length {
// Remove children that fall off the end.
for i in range(num$labelB$pluralB, length) {
child = self.$labelB$pluralB[i]
if !isnull(child) {
self.remove$labelB$B(child!)
}
}
} else {
// Initialize the new values to null.
for i in range(length, num$labelB$pluralB) {
self.$labelB$pluralB[i] = null(B)
}
}
self.$labelB$pluralB.resize(num$labelB$pluralB)
}
func append$labelB$B(self, child: B) {
debug {
if !isnull(child.$labelA$A) {
raise Exception.Internal, "append$labelB$B: Object is already in a relation"
}
}
child.$labelA$A_Index = <u32>self.$labelB$pluralB.length()
self.$labelB$pluralB.append(child)
child.$labelA$A = self
ref child
}
func remove$labelB$B(self, child: B) {
debug {
if child.$labelA$A != self {
raise Exception.Internal, "remove$labelB$B: Object not owned by me"
}
}
self.$labelB$pluralB[child.$labelA$A_Index] = null(child)
child.$labelA$A_Index = 0xffffffffu32
child.$labelA$A = null(self)
unref child
}
}
if cascadeDelete {
// If this is a cascade-delete relationship, destroy children in the destructor.
appendcode A.destroy {
for i in range(self.$labelB$pluralB.length()) {
child$labelB$B = self.$labelB$pluralB[i]
if !isnull(child$labelB$B) {
child$labelB$B.destroy()
}
}
}
} else {
// Remove all children.
prependcode A.destroy {
for i in range(self.$labelB$pluralB.length()) {
child$labelB$B = self.$labelB$pluralB[i]
if !isnull(child$labelB$B) {
self.remove$labelB$B(child$labelB$B!)
}
}
}
}
prependcode B {
self.$labelA$A = null(A)
self.$labelA$A_Index = 0xffffffffu32
}
// Remove self from A on destruction.
prependcode B.destroy {
if !isnull(self.$labelA$A) {
self.$labelA$A.remove$labelB$B(self)
}
}
}