- Joined
- May 9, 2014
- Messages
- 1,820
I messed around with operators and textmacros, and ended up making some sort of pseudo-variables.
Posted this one just for fun too
.
JASS:
library Hashtable uses/*
*/ optional Globals
struct MainHash extends array
readonly static constant hashtable HASH = InitHashtable()
private static integer Var_Key = 0
private method operator recNext takes nothing returns thistype
return LoadInteger(HASH, 0, this)
endmethod
private method operator recNext= takes thistype recycle returns nothing
call SaveInteger(HASH, 0, this, recycle)
endmethod
static method operator genKey takes nothing returns integer
local thistype this = 0
if thistype(0).recNext == 0 then
set Var_Key = Var_Key + 1
return Var_Key
endif
set this = thistype(0).recNext
set thistype(0).recNext = thistype(0).recNext.recNext
return this
endmethod
endstruct
function GetHash takes nothing returns hashtable
return MainHash.HASH
endfunction
function GenerateKey takes nothing returns integer
return MainHash.genKey
endfunction
//! textmacro generate_operators takes operatorname, type, typeid, var, nativename, HashVar
method operator $operatorname$ takes nothing returns $type$
return Load$nativename$($HashVar$, $var$, this)
endmethod
method operator $operatorname$= takes $type$ $typeid$ returns nothing
call Save$nativename$($HashVar$, $var$, this, $typeid$)
endmethod
//! endtextmacro
//! textmacro generate_methods takes methodname, handle, handleid, var, nativename, HashVar, value, valueid
static method get$methodname$ takes $handle$ $handleid$ returns $value$
return Load$nativename$($HashVar$, $var$, GetHandleId($handleid$))
endmethod
static method set$methodname$ takes $handle$ $handleid$, $value$ $valueid$ returns nothing
call Save$nativename$($HashVar$, $var$, GetHandleId($handleid$), $valueid$)
endmethod
//! endtextmacro
module MyCustomAlloc
private static integer count = 0
private static MainHash RecNext = 0 //MainHash keys function exactly like variables, I've just noticed.
//! runtextmacro generate_operators("recNext", "thistype", "that", "RecNext", "Integer", "GetHash()")
private static method onInit takes nothing returns nothing
set RecNext = GenerateKey()
endmethod
static method create takes nothing returns thistype
local thistype this
if thistype(0).recNext == 0 then
set count = count + 1
set this = count
else
set this = thistype(0).recNext
set thistype(0).recNext = thistype(0).recNext.recNext
endif
return this
endmethod
method destroy takes nothing returns nothing
set recNext = thistype(0).recNext
set thistype(0).recNext = this
endmethod
endmodule
endlibrary
Posted this one just for fun too