Snippet Box

weaaddar

New Member
Reaction score
6
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.
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),'hfoo',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.
 

Nestharus

o-o
Reaction score
84
This is a pretty horrible thing, so unless you really need it you are probably better off never creating one.

Then why did you submit it?

This wouldn't be horrible if you let the users handle the storing and you just provided an index... when you do storing and relations, you kill performance, kill the interface/usability, and just make it horrible as you said.

I know this from experience ^_^.

Another thing you run into when you try to handle storage is crap like this-
JASS:

//! 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")


yea... I think you took a bad direction with this
 

Nestharus

o-o
Reaction score
84
This is a pretty horrible thing, so unless you really need it you are probably better off never creating one.

Considering he said that I'd instant graveyard ><

It's like, you say your resource is junk eh? Well, I know exactly where to put junk! : O
 

Nestharus

o-o
Reaction score
84
I actually wrote something simmilar, called box for creating a generic object. its pretty awful though so I'm not sure why you'd ever use one of these

That's him saying he'd never even use it

Author says it's so bad he'd never use it and that's it's horrible and he suggests other people not to use it.

Why isn't this in graveyard yet?
 

Nestharus

o-o
Reaction score
84
All righty : )

Just couldn't see why not when the author tells people to never use it, lol :eek:.

Oh well up to you guys in moderation team : D
 

Hatebreeder

So many apples
Reaction score
381
I might be able to a custom cooldown system, IF I can typecast library's and scopes into intergers...

Though, then again, it would be messy, and realy does need preformance... Any way you could work on this snipplett to be more efficient?
 

weaaddar

New Member
Reaction score
6
The usage is for basically object storage like in Java.

If you have a bunch of objects you want to store in a collection and aren't willing to create a struct it isn't such a bad thing. The reason to use a box for an integer is uniformity.

As for badness it isn't terrible as every fetch inlines. And if you use the ACT_AS_UNION flag then the setter inlines as well. I've kind of always wanted one, but admittedly the purpose isn't for performance code. Merely for lazy code.

I mean its more or less when you are too lazy to specialize a solution. If you create a reverse look-up, then you have generic struct attachment without too much of the overhead.

e.g::
JASS:
Box mybox = box.create();
box.as_integer = &#039;hfoo&#039;;
Table[&#039;hfoo&#039;] = mybox;
// now you can always look up mybox for when you want to use a struct associated with hfoo


As for the slew of text-macros, it kind of sucks but there is no way to say instance by demand, and if you ever use it inlines. I wish Vex would remove never called functions, but he probably can't because of ExecuteFunc("FuncName"). I suppose you could modularize it, but I'm not sure how much that would help with out a huge implement list which might be worse then this.
Edit:
Actually you can use this as a module and array struct as like an up caster with this bizzaro syntax:: Dewidgetizer[somewidget].as_unit
Which while awful is pretty compact and wouldn't be too expensize. (the static indexer will store it, and then the instance variable will unstore it).
 

Jesus4Lyf

Good Idea™
Reaction score
397
This doesn't need to use hashtables, and should inline to an array read on any get method.
In any situation where you use integers, strings, etc, surely this is useless except for writing overly flexible systems with illogical interfaces.
That's still the only use I can think of for removing typesafety (well, that's what this is basically, right?).

>If you have a bunch of objects you want to store in a collection and aren't willing to create a struct it isn't such a bad thing. The reason to use a box for an integer is uniformity.

You've written an interface designed to avoid typesafety (Box). The reason you need it is because you don't know what type a user wants to retrieve in something like "dictionary". Well, doesn't this suggest a user is better off writing it themselves when they need it? This is back to the classic collections debate that people are always better off writing what they need when they need it, rather than going to a site and trying to figure out which of 100 collections they need...
and which is more efficient or what is the exact difference between dictionary, linked list, hashset, table or singularly linked list or why there are 5 different systems with fancy names and it seems like one is doing what you want it to do but it seems to lack one feature so you need another data structure to tack onto the side to add that feature, and do you have structs to clean up or do they automatically get removed and under what conditions...

Point being, if you understood the difference, you could've written it yourself.

I'm gonna graveyard this, but if we ever decide to afflict ourselves with a lack of typesafety (so we can confuse users with complex collections or something) we will ungraveyard it. Feel free to continue discussion.
 

weaaddar

New Member
Reaction score
6
No, I'm glad to see it grave yarded. If you ever need something like this its probably not worth it.

However, I'm going to probably release a little typecaster as I think this syntax isn't too bad

convert[someInt].as_unit

By using SaveHandleHandle and Load$T$Handle or whatever.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top