HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

LOL TEMPLATES

11-08-2006, 01:23 PM#1
karukef
Collapse JASS:
//! require preprocessor TemplateParser

function GCL_sizekey takes string key returns string
    return "size__" + key
endfunction

function GCL_listkey takes string key, integer index returns string
    if index == 0 then
        return key
    endif
    return key + Jlib_QuickI2S(index)
endfunction

function GCL_Size takes gamecache gc, string mkey, string key returns integer
    return GetStoredInteger(gc, mkey, GCL_sizekey(key))
endfunction

////////////////
//TEMPLATE FUN//
////////////////

//! template GCL_FUNCS
//longtype i.e Integer or Real etc
//realtype i.e integer or real etc
function GCL_<longtype>Set takes gamecache gc, string mkey, string key, integer index, <realtype> value returns nothing
    //Does not update the size: use only on elements that actually exists.
    call Store<longtype>(gc, mkey, GCL_listkey(key, index), value)
endfunction

function GCL_<longtype>Get takes gamecache gc, string mkey, string key, integer index returns <realtype>
    return GetStored<longtype>(gc, mkey, GCL_listkey(key, index))
endfunction

function GCL_<longtype>RemoveIndex takes gamecache gc, string mkey, string key, integer index returns nothing
    local integer size = GCL_Size(gc, mkey, key)
    //If the index is not at the end of the list, copy the end of the list to
    //index's position
    if index != size-1 then
        call GCL_<longtype>Set(gc, mkey, key, index, GCL_<longtype>Get(gc, mkey, key, size-1))
    endif
    //Clear the tail
    call FlushStored<longtype>(gc, mkey, GCL_listkey(key, size-1))
    //Decrement size
    call Jlib_CacheMM(gc, mkey, GCL_sizekey(key))
endfunction

function GCL_<longtype>RemoveValue takes gamecache gc, string mkey, string key, <realtype> value returns nothing
    local integer size = GCL_Size(gc, mkey, key)
    local integer i = 0
    loop
        exitwhen i == size
        if GCL_<longtype>Get(gc, mkey, key, i) == value then
            call GCL_<longtype>RemoveIndex(gc, mkey, key, i)
            return
        endif
        set i = i + 1
    endloop
    call Jlib_ErrorMsg("GcList", "Attempt to remove by value failed")
endfunction

function GCL_<longtype>Push takes gamecache gc, string mkey, string key, <realtype> value returns nothing
    //Increase the number of items for this key (The SizeKey)
    //and store the index of the to-be-pushed item
    local integer list_pos = Jlib_CachePP(gc, mkey, GCL_sizekey(key))
    //Place the value in the ListKey of the new pos
    call Store<longtype>(gc, mkey, GCL_listkey(key, list_pos), value)
endfunction

function GCL_<longtype>Pop takes gamecache gc, string mkey, string key returns <realtype>
    local <realtype> value
    local string listkey
    //If there are no items, show an error
    if GCL_Size(gc, mkey, key) < 1 then
        call Jlib_ErrorMsg("GcList", "No items to pop from list")
        return 0
    endif
    //CacheMM decrements and returns the decremented value, so listkey for the old value is +1
    set listkey = GCL_listkey(key, Jlib_CacheMM(gc, mkey, GCL_sizekey(key)) + 1)
    //Get and store the value
    set value = GetStored<longtype>(gc, mkey, listkey)
    //Flush the old entry
    call FlushStored<longtype>(gc, mkey, listkey)
    
    return value
endfunction
//! endtemplate

//! usetemplate GCL_FUNCS longtype Integer;Real;String;Boolean realtype integer;real;string;boolean

//! template GCL_HANDLE_FUNCS
function GCL_<longtype>Set takes gamecache gc, string mkey, string key, integer index, <realtype> value returns nothing
    call GCL_IntegerSet(gc, mkey, key, index, Jlib_H2Int(value))
endfunction

function GCL_<longtype>Get takes gamecache gc, string mkey, string key, integer index returns <realtype>
    return GCL_IntegerGet(gc, mkey, key, index)
    return null
endfunction

function GCL_<longtype>RemoveIndex takes gamecache gc, string mkey, string key, integer index returns nothing
    call GCL_IntegerRemoveIndex(gc, mkey, key, index)
endfunction

function GCL_<longtype>RemoveValue takes gamecache gc, string mkey, string key, <realtype> value returns nothing
    call GCL_IntegerRemoveValue(gc, mkey, key, Jlib_H2Int(value))
endfunction

function GCL_<longtype>Push takes gamecache gc, string mkey, string key, <realtype> value returns nothing
    call GCL_IntegerPush(gc, mkey, key, Jlib_H2Int(value))
endfunction

function GCL_<longtype>Pop takes gamecache gc, string mkey, string key returns <realtype>
    return GCL_IntegerPop(gc, mkey, key)
    return null
endfunction
//! endtemplate

//! usetemplate GCL_HANDLE_FUNCS longtype Handle;AttackType;Destructable;Effect;Group;Image;Item;Lightning;Loc;Player;Rect;Region;Sound;TimerDialog;Trigger;Ubersplat;Unit;Widget;WeaponType realtype handle;attacktype;destructable;effect;group;image;item;lightning;location;player;rect;region;sound;timerdialog;trigger;ubersplat;unit;widget;weapontype

This is not a joke. I just spent the last hour and a half writing the template parser for WeWarlock and it worked perfectly the first time I ran it. The above code translates into an 800-line suite for treating any key in GC as a list/stack. I'll release new WeWarlock with the TemplateParser and this GcList library shortly (Haven't tested it, the GcList lib might contain bugs)

But seriously. HAHA TEMPLATES!
11-08-2006, 02:05 PM#2
Fulla
What does this do sorry?
11-08-2006, 03:13 PM#3
Vexorian
I don't think I love the syntax, but it is all right, it might as well work for some other things, does it just do a blind replacing of tokens inside <>? or has more fixed rules?

--
Edits:

What about using {} instead of <>?

I suggest:

Collapse JASS:
//! template GCL_HANDLE_FUNCS(longtype,realtype)
function GCL_<longtype>Set takes gamecache gc, string mkey, string key, integer index, <realtype> value returns nothing
    call GCL_IntegerSet(gc, mkey, key, index, Jlib_H2Int(value))
endfunction

function GCL_<longtype>Get takes gamecache gc, string mkey, string key, integer index returns <realtype>
    return GCL_IntegerGet(gc, mkey, key, index)
    return null
endfunction

function GCL_<longtype>RemoveIndex takes gamecache gc, string mkey, string key, integer index returns nothing
    call GCL_IntegerRemoveIndex(gc, mkey, key, index)
endfunction

function GCL_<longtype>RemoveValue takes gamecache gc, string mkey, string key, <realtype> value returns nothing
    call GCL_IntegerRemoveValue(gc, mkey, key, Jlib_H2Int(value))
endfunction

function GCL_<longtype>Push takes gamecache gc, string mkey, string key, <realtype> value returns nothing
    call GCL_IntegerPush(gc, mkey, key, Jlib_H2Int(value))
endfunction

function GCL_<longtype>Pop takes gamecache gc, string mkey, string key returns <realtype>
    return GCL_IntegerPop(gc, mkey, key)
    return null
endfunction
//! endtemplate

//! usetemplate GCL_FUNCS(Integer,integer)
//! usetemplate GCL_FUNCS(Real,real)
//! usetemplate GCL_FUNCS(String,string)
//! usetemplate GCL_FUNCS(Boolean,boolean)
11-08-2006, 08:56 PM#4
karukef
Quote:
Originally Posted by Fulla
What does this do sorry?

This post does not have the intention of making any sense.

As for Vexorian's completely trivial suggestions for syntax change, all I can say is that templates are already such insanely dodgy practice in JASS that brittle syntax contributes nothing to make it less flaky, and those specific suggestions just make it more awkward to use.
11-08-2006, 09:18 PM#5
Chuckle_Brother
Quote:
Originally Posted by Fulla
What does this do sorry?

Well a template in C++/etc was a shorter way of writing code:

Code:
template <class fType>
fType RetNeg (fType iNum)
{
   return -iNum;
}

Thats a VERY simple example of what a basic template is, basically it lets you model a function that could work for any given type(assuming of course that you have the proper overloads and if it makes sense to apply whatever actions you are taking on it).


Anyway, this is neat, and could potentially be exceedingly useful.
11-09-2006, 10:21 AM#6
karukef
Quote:
Originally Posted by Chuckle_Brother
Well a template in C/C++/etc was a shorter way of writing code

...

Anyway, this is neat, and could potentially be exceedingly useful.

First, there are no templates in C, only C++.

Personally I find templates to be a truly horrible way to program. But like in C++, we have in JASS a programming language with such lack of dynamic ability that templates does have some use despite it's awfulness. It's sort of a poor-mans object-orientedness.
11-09-2006, 01:05 PM#7
Chuckle_Brother
Really? Hmm, wonder what my prof was babbling about then, oh wells, fixed.