-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_CaseSensitiveObject.ahk
135 lines (116 loc) · 4.39 KB
/
class_CaseSensitiveObject.ahk
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class CaseSensitiveObject
{
static SuperKey := {}
/*
About SuperKey:
Object keys are allowed to be objects themselfs. (E.g. { []: "123" }).
When an object is used as a key, the only way to get to the contents of that
key is to use the EXACT SAME object, not just an object with identical contents.
By using a specific object key for the contents of all instances of CaseSensitiveObject
we can ensure that the user CANNOT accidentally overwrite or directly get to the contents
of the untranslated object. This is because the only way to get to the actual contents is
to use the full key: CaseSensitiveObject.SuperKey
*/
static _Insert := CaseSensitiveObject.Insert
static _Remove := CaseSensitiveObject.Remove
static _MinIndex := CaseSensitiveObject.MinIndex
static _MaxIndex := CaseSensitiveObject.MaxIndex
static _GetCapacity := CaseSensitiveObject.GetCapacity
static _SetCapacity := CaseSensitiveObject.SetCapacity
static _GetAddress := CaseSensitiveObject.GetAddress
static _Clone := CaseSensitiveObject.Clone
static _HasKey := CaseSensitiveObject.HasKey
static _NewEnum := CaseSensitiveObject.NewEnum
__new(base = "") {
ObjInsert(this, CaseSensitiveObject.SuperKey, IsObject(base) ? {base: base} : {})
}
__get(keys*) {
key := CaseSensitiveObject.FormatKey.(keys[1])
realthis := this[CaseSensitiveObject.SuperKey]
if realthis.HasKey(key) || keys[1] = "SuperKey"
{
keys.remove(1)
if keys.MaxIndex()
return realthis[key][keys*]
else
return realthis[key]
}
}
__set(args*) {
key := CaseSensitiveObject.FormatKey.(args.remove(1))
value := args.remove()
if args.MaxIndex()
return this[CaseSensitiveObject.SuperKey][key][args*] := value
else
return this[CaseSensitiveObject.SuperKey][key] := value
}
__call(target, args*) {
if !ObjHasKey(CaseSensitiveObject,target)
return this[CaseSensitiveObject.SuperKey][CaseSensitiveObject.FormatKey.(target)].(args*)
}
Insert(args*) {
loop % args.MaxIndex() // 2
args[A_Index*2-1] := CaseSensitiveObject.FormatKey.(args[A_Index*2-1]) ;format all keys
return this[CaseSensitiveObject.SuperKey].Insert(args*)
}
Remove(args*) {
loop % args.MaxIndex()
args[A_Index] := CaseSensitiveObject.FormatKey.(args[A_Index])
return this[CaseSensitiveObject.SuperKey].Remove(args*)
}
MinIndex() {
return this[CaseSensitiveObject.SuperKey].MinIndex()
}
MaxIndex() {
return this[CaseSensitiveObject.SuperKey].MaxIndex()
}
GetCapacity(args*) {
return this[CaseSensitiveObject.SuperKey].GetCapacity(args*)
}
SetCapacity(args*) {
if args.MaxIndex() > 1
args[1] := CaseSensitiveObject.FormatKey.(args[1])
return this[CaseSensitiveObject.SuperKey].SetCapacity(args*)
}
GetAddress(key) {
return this[CaseSensitiveObject.SuperKey].GetAddress(CaseSensitiveObject.FormatKey.(key))
}
HasKey(key) {
return this[CaseSensitiveObject.SuperKey].HasKey(CaseSensitiveObject.FormatKey.(key))
}
Clone() {
ret := new CaseSensitiveObject()
ret[CaseSensitiveObject.SuperKey] := this[CaseSensitiveObject.SuperKey].Clone()
return ret
}
FormatKey() {
if !IsObject(this)
if this is not number
{
f := "{1:0" 2*(1+A_IsUnicode) "X}"
loop, parse, this
ret .= Format(f, Asc(A_LoopField))
this := "s" ret
}
return this
}
UnformatKey() {
sz := 2*(1+A_IsUnicode)
this := SubStr(this, 2)
loop % StrLen(this) // sz
ret .= Chr("0x" SubStr(this, (A_Index-1)*sz+1, sz))
return ret
}
NewEnum() {
return new CaseSensitiveObject.Enumerator(this[CaseSensitiveObject.SuperKey])
}
class Enumerator {
__new(obj) {
this.enum := ObjNewEnum(obj)
}
Next(ByRef key, ByRef value = "") {
return this.enum.Next(k, value), key := CaseSensitiveObject.UnFormatKey.(k)
}
}
}