System Dynamic Struct Storage - DSS

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:
////////////////////////////////////////////////////////////////////
//      DSS -> Dynamic Struct Storage
//              v 2.0.1
//
//  ============
//  What's DSS?
//  ============
//  DSS is a struct attachment system that can make
//  attaching easier. Getting tired with Table/ABC?
//  Use this! =D
//
//  ==================
//  How to implement?
//  ==================
//  1) Copy this library to your map.
//  2) Done.
//
//  ==================
//  How to use?
//  ==================
//  For timer attachment : implement DSS_vJassTimerAttachment.
//  For trigger attachment : implement DSS_vJasTriggerAttachment.
//
//  ===============
//  How to attach?
//  ===============
//  set <structName>[timer] = struct instance.
//  <structName>[timer] -> return attached struct
//  
//  ===========================================
//  How to clear the attached timers/triggers?
//  ===========================================
//  call struct instance.flush()
//
//  =====================
//  Details :
//  =====================
//  1) DSS uses hashtable to store attached data.
//  2) DSS will automatically remove attached data
//     when the struct is destroyed.
//  3) Want unit attachment? DSS is not aimed to that. =)
//  4) The speed of this is as fast as Table.
//
//  ================
//  Requirements :
//  ================
//  Jasshelper 0.A.2.B or later.
//
//////////////////////////////////////////////////////////////////
library DSS

    globals
        public hashtable hasht = InitHashtable()
        public integer keyz = 0
    endglobals
    
    public module vJassTimerAttachment
        private static integer dss_keyz
        private timer attached_timer
        static method operator [] takes timer whichTimer returns thistype
            return LoadInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTimer))
        endmethod
        static method operator []= takes timer whichTimer, thistype whichData returns nothing
            call whichData.flush()
            call SaveInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTimer),whichData)
            set whichData.attached_timer = whichTimer
        endmethod
        method flush takes nothing returns nothing
            call RemoveSavedInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(.attached_timer))
            set .attached_timer = null
        endmethod
        method destroy takes nothing returns nothing
            call deallocate()
        endmethod
        private static method onInit takes nothing returns nothing
            set DSS_keyz = DSS_keyz + 1
            set thistype.dss_keyz = DSS_keyz
        endmethod
    endmodule
    
    public module vJassTriggerAttachment
        private static integer dss_keyz
        private trigger attached_trigger
        static method operator [] takes trigger whichTrigger returns thistype
            return LoadInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTrigger))
        endmethod
        static method operator []= takes trigger whichTrigger, thistype whichData returns nothing
            call whichData.flush()
            call SaveInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTrigger),whichData)
            set whichData.attached_trigger = whichTrigger
        endmethod
        method flush takes nothing returns nothing
            call RemoveSavedInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(.attached_trigger))
            set .attached_trigger = null
        endmethod
        method destroy takes nothing returns nothing
            call .flush()
            call deallocate()
        endmethod
        private static method onInit takes nothing returns nothing
            set DSS_keyz = DSS_keyz + 1
            set thistype.dss_keyz = DSS_keyz
        endmethod
    endmodule
    
endlibrary


Zinc Plug-in :
JASS:
////////////////////////////////////////////////////////////////////
//      DSS -> Dynamic Struct Storage (Zinc)
//              v 2.0.1         by kingking
//
//  ============
//  What's DSS?
//  ============
//  DSS is a struct attachment system that can make
//  attaching easier. Getting tired with Table/ABC?
//  Use this! =D
//
//  ==================
//  How to implement?
//  ==================
//  1) Copy this library to your map.
//  2) Done.
//
//  ==================
//  How to use?
//  ==================
//  For timer attachment : module DSS_ZincTimerAttachment.
//  For trigger attachment : module DSS_ZincTriggerAttachment.
//
//  ===============
//  How to attach?
//  ===============
//  set <structName>[timer] = struct instance.
//  <structName>[timer] -> return attached struct
//  
//  ===========================================
//  How to clear the attached timers/triggers?
//  ===========================================
//  call struct instance.flush()
//
//  =====================
//  Details :
//  =====================
//  1) DSS uses hashtable to store attached data.
//  2) DSS will automatically remove attached data
//     when the struct is destroyed.
//  3) Want unit attachment? DSS is not aimed to that. =)
//  4) The speed of this is as fast as Table.
//
//  ================
//  Requirements :
//  ================
//  Jasshelper 0.A.2.B or later.
//  Dynamic Struct Storage
//
//////////////////////////////////////////////////////////////////
//! zinc
library DSSZinc requires DSS {

    public module DSS_ZincTimerAttachment {
        private static integer dss_keyz;
        private timer attached_timer;
        
        static method operator [] (timer whichTimer) -> thistype { 
            return LoadInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTimer)); 
        }
        
        static method operator []= (timer whichTimer, thistype whichData) {
            whichData.flush();
            SaveInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTimer),whichData);
            whichData.attached_timer = whichTimer;
        }
        
        method flush () {
            RemoveSavedInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(this.attached_timer));
            this.attached_timer = null;
        }
        
        method destroy () { deallocate(); }
        
        private static method onInit () {
            DSS_keyz += 1;
            thistype.dss_keyz = DSS_keyz;
        }
    }
    
    public module DSS_ZincTriggerAttachment {
        private static integer dss_keyz;
        private trigger attached_trigger;
        
        static method operator [] (trigger whichTrigger) -> thistype { 
            return LoadInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTrigger)); 
        }
        
        static method operator []= (trigger whichTrigger, thistype whichData) {
            whichData.flush();
            SaveInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(whichTrigger),whichData);
            whichData.attached_trigger = whichTrigger;
        }
        
        method flush () {
            RemoveSavedInteger(DSS_hasht,thistype.dss_keyz,GetHandleId(this.attached_trigger));
            this.attached_trigger = null;
        }
        
        method destroy () { deallocate(); }
        
        private static method onInit () {
            DSS_keyz += 1;
            thistype.dss_keyz = DSS_keyz;
        }
    }
}
//! endzinc
 

Jesus4Lyf

Good Idea™
Reaction score
397
Graveyarded for containing H2I-offset in unsafe circumstances.
Edit: My bad, you documented that it should only be used for preloaded handles.
JASS:
            debug static if $TYPE$ == "unit" then
                debug call BJDebugMsg("DSS notification : You are trying to use DSS to attach struct to unit! Please change to unit indexing library instead of DSS for struct attaching!")
            debug endif

Won't compile in debug mode, I'm sure of it...

What's this do that Table doesn't?
Why do you enforce typesafety?
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Looking foward to your answers.
Table requires Table.create to create a key.
DSS does not requires, it will automatically create a key on initialization.

By using DSS, you can use the struct's name directly.
In Table, you need use the key's name instead of struct's name.

GetHandleId() - offset is also a feature that Table does not provide.
For DSS_Struct, it has an advantage over Table. Sometimes when you get attached value from function, jasshelper will give you an error because of the returned value is integer, not struct. In this case, you need to use Data(index). DSS_Struct does not provides trouble, because it returns thistype instead of integer. :thup:

Why do you enforce typesafety?
If I do not do so, some naughty users will use it to attach struct to units. ><
 

Jesus4Lyf

Good Idea™
Reaction score
397
The interface of this is not as clean as [LJASS]hashtable[/LJASS]s and [LJASS]key[/LJASS]s. (This should use keys, btw, which inline.)

Since this serves the exact same functionality of hashtables with keys, but has a worse interface, this is unnecessary, because people can use hashtables with keys. :thup:

Graveyarded.
 

Romek

Super Moderator
Reaction score
963
You honestly don't need to submit everything you make, kingkingyyk3. Most, if not all of your recent submissions seem to be reinventions of what has been made in the past, without any new features or advantages over past systems.

Do you think people will use your submission? If so, I suggest you don't submit things until you realise what people (including yourself) would actually use or find useful. Though I get the feeling that you know people won't use your stuff, and you just submit it for the sake of having submissions. :p

Good system ideas usually come from you needing something yourself, and realising that it hasn't been made before (that other people could also benefit from); not from sitting around thinking of a system to write.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
It provides better functionality. It will automatically remove attached struct from timer/trigger when you called .destroy, which Table can't provide this. :)
 

Romek

Super Moderator
Reaction score
963
He could if he wanted to, though he chooses not to.
 
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