• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

{Debate Thread} Operators and Textmacros

Status
Not open for further replies.
I messed around with operators and textmacros, and ended up making some sort of pseudo-variables.

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 :).
 
What does it do exactly?

Sorry for the late reply,

Operator function that I wrote above:
It simulates a variable array's behavior, only that it has the benefit of extending beyond (theoretically) 8192 instances, but is slower because of using a hashtable lookup.

Method function:
That part is mine to keep..

:)
 
Status
Not open for further replies.
Top