Box is basically object from Java. "Useful" if you need to create indexs out of anything and don't even remotely care about performance. Or if you want to store multiple types in a collection (you should probably use a struct). Or if you are horrendously lazy and want a type cast library and are also willing to suffer performance penalties.
Box can also be used as a union/typedef in C if you enable a flag. I warn you though that this is even more horrible then the Javesque object purpose. Doing something like box.as_integer = 5; box.as_real = 46; is a real abuse of me being generous enough to include a Static switcher.
Supports::
integer -- set,get,test
real -- set,get,test
boolean -- set,get,test
string -- set,get, test
agent -- set,test
and all agent subtypes. -- get
Meaning you can only get a box.as_unit you have to do box.as_agent to save a unit, or box.is_agent if it has a unit.
The as_T on failure returns null for reference types, or 0 for value types.
Some syntax examples::
Box can also be used as a union/typedef in C if you enable a flag. I warn you though that this is even more horrible then the Javesque object purpose. Doing something like box.as_integer = 5; box.as_real = 46; is a real abuse of me being generous enough to include a Static switcher.
Supports::
integer -- set,get,test
real -- set,get,test
boolean -- set,get,test
string -- set,get, test
agent -- set,test
and all agent subtypes. -- get
Meaning you can only get a box.as_unit you have to do box.as_agent to save a unit, or box.is_agent if it has a unit.
The as_T on failure returns null for reference types, or 0 for value types.
JASS:
//! zinc
// Box is a generic type like Java in Object. Like the name suggests box is
// well a boxing type. It puts whatever you want into a box and then lets you cast out.
// box must be destroyed. This is a pretty horrible thing, so unless you really need it
// you are probably better off never creating one.
// property index
// ============
// as_T = obj
// changes the type of the box to T, and stores the value of obj in the box.
// as_T -> T
// returns the content of the box, if it is a T otherwise returns the default value for T.
// is_T -> booleans
// returns if the box is a T.
// usage::
library Box
{
hashtable Table = InitHashtable();
constant boolean ACT_AS_UNION = false;
public struct Box
{
//! textmacro Box_T takes T,TypeName,BaseName
method operator as_$T$=($T$ obj)
{
static if(!ACT_AS_UNION)
{
FlushChildHashtable(Table,integer(this));
}
Save$TypeName$(Table,integer(this),0,obj);
}
method operator is_$T$() -> boolean
{
return HaveSaved$BaseName$(Table,integer(this),0);
}
//! endtextmacro
//! runtextmacro Box_T("integer","Integer","Integer")
//! runtextmacro Box_T("boolean","Boolean","Boolean")
//! runtextmacro Box_T("string","Str","String")
//! runtextmacro Box_T("real","Real","Real")
//! runtextmacro Box_T("agent","AgentHandle","Handle")
//! textmacro Unbox_T takes TypeName,T
method operator as_$T$() -> $T$
{
return Load$TypeName$(Table,integer(this),0);
}
//! endtextmacro
//! runtextmacro Unbox_T("Integer","integer")
//! runtextmacro Unbox_T("Boolean","boolean")
//! runtextmacro Unbox_T("Str","string")
//! runtextmacro Unbox_T("Real","real")
//! runtextmacro Unbox_T("PlayerHandle","player")
//! runtextmacro Unbox_T("WidgetHandle","widget")
//! runtextmacro Unbox_T("DestructableHandle","destructable")
//! runtextmacro Unbox_T("ItemHandle","item")
//! runtextmacro Unbox_T("UnitHandle","unit")
//! runtextmacro Unbox_T("AbilityHandle","ability")
//! runtextmacro Unbox_T("TimerHandle","timer")
//! runtextmacro Unbox_T("TriggerHandle","trigger")
//! runtextmacro Unbox_T("TriggerConditionHandle","triggercondition")
//! runtextmacro Unbox_T("TriggerEventHandle","event")
//! runtextmacro Unbox_T("ForceHandle","force")
//! runtextmacro Unbox_T("GroupHandle","group")
//! runtextmacro Unbox_T("LocationHandle","location")
//! runtextmacro Unbox_T("RectHandle","rect")
//! runtextmacro Unbox_T("BooleanExprHandle","boolexpr")
//! runtextmacro Unbox_T("SoundHandle","sound")
//! runtextmacro Unbox_T("EffectHandle","effect")
//! runtextmacro Unbox_T("QuestHandle","quest")
//! runtextmacro Unbox_T("QuestItemHandle","questitem")
//! runtextmacro Unbox_T("DefeatConditionHandle","defeatcondition")
//! runtextmacro Unbox_T("TimerDialogHandle","timerdialog")
//! runtextmacro Unbox_T("LeaderboardHandle","leaderboard")
//! runtextmacro Unbox_T("MultiboardHandle","multiboard")
//! runtextmacro Unbox_T("MultiboardItemHandle","multiboarditem")
//! runtextmacro Unbox_T("TrackableHandle","trackable")
//! runtextmacro Unbox_T("DialogHandle","dialog")
//! runtextmacro Unbox_T("ButtonHandle","button")
//! runtextmacro Unbox_T("RegionHandle","region")
//! runtextmacro Unbox_T("FogModifierHandle","fogmodifier")
//! runtextmacro Unbox_T("HashtableHandle","hashtable")
}
}
//! endzinc
Some syntax examples::
JASS:
Box box = Box.create();
box.as_integer = 5;
box.as_real = 5.0; // switched to real.
box.as_agent = CreateUnit(Player(0),039;hfoo039;,0,0,0); // set the box to a unit.
box.is_integer // test if an integer
unit u = box.as_unit // returns the result as a unit null on failure.
widget w = box.as_widget // returns the result as a widget.