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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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