forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_MemBlk.ahk
97 lines (88 loc) · 2.89 KB
/
class_MemBlk.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
/* Class: MemBlk
* - Minimal struct wrapper. Wraps VarSetCapacity-NumPut/NumGet routines.
* License:
* WTFPL[http://wtfpl.net/]
* CONSTRUCTION:
* buf := new MemBlk( size [, fill := 0 ] )
* Parameter(s):
* size [in] - size of the memory block in bytes
* fill [in, opt] - same as VarSetCapacity's 'FillByte' parameter
* PROPERTIES:
* __Ptr [read-only] - returns the address of the buffer
* Size [read-only] - returns the size of the buffer in bytes
* METHODS:
* PutNumType( num [, at ] ) - Similar to NumPut(). Where 'NumType' can be
* one of the following specified directly as
* part of the method name: UInt, Int, Int64,
* Short, UShort, Char, UChar, Double, or Float.
* Parameters 'num' and 'at' is similar to that
* of NumPut's 'num' and 'offset' respectively.
* An address may be specified for 'at'. If 'at'
* is omitted, the object's internal pointer/pos
* is used. Each call to this method advances the
* internal pointer/pos to allow omission of 'at'
* for subsequent calls, simulates nested NumPuts.
* Return value is the same with NumPut.
* GetNumType( at := 0 ) - Similar to NumGet(). Unlike PutNumType's 'at'
* parameter, 'at' here must be an offset only,
* defaults to 0.
*/
class MemBlk
{
__New(size, fill:=0)
{
ObjSetCapacity(this, "_Buf", size)
this.__Pos := (ptr := this.__Ptr) + 0
;// Initialize
if (fill >= 0 && fill <= 255)
DllCall("RtlFillMemory", "Ptr", ptr, "UPtr", size, "UChar", fill)
}
__Get(key:="", args*)
{
if !key
return ObjGetAddress(this, "_Buf")
}
__Call(method, args*)
{
if (method = "Put" || method = "Get")
method .= "UPtr"
if (method ~= "i)^Put(U?(Int|Short|Char|Ptr)|Int64|Double|Float)$")
{
num := args[1], at := Round(Abs(args[2]) != "" ? args[2] : this.__Pos)
return this._PutAt(num, at, SubStr(method, 4))
}
else if (method ~= "i)^Get(U?(Int|Short|Char|Ptr)|Int64|Double|Float)$")
{
at := Round(args[1])
return this._GetAt(at, SubStr(method, 4))
}
}
Size {
get {
return ObjGetCapacity(this, "_Buf")
}
set {
return value
}
}
__Ptr {
get {
return ObjGetAddress(this, "_Buf")
}
set {
return value
}
}
_PutAt(num, at:=0, type:="UPtr")
{
ptr := this.__Ptr
addr := (at >= ptr && at < ptr + this.Size) ? at : ptr + at
return this.__Pos := NumPut(num, addr + 0, type)
}
_GetAt(at:=0, type:="UPtr")
{
ptr := this.__Ptr
addr := (at >= ptr && at < ptr + this.Size) ? at : ptr + at
return NumGet(addr + 0, type)
}
}