Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Description: This class can be used as a dynamically sized ;
- ; List to store any data type ;
- ; ;
- ; Usage: $List(<ListPtr>,<Params>,...).<Member> ;
- ; Example: var %x = $List ;
- ; Example: var %itemID = $List(%x,$long(500)).add ;
- ; Example: $List(%x,%itemID,$long(499)).set ;
- ; Example: $List(%x,%ItemID).remdel ;
- ; Example: $List(%x).delete ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;;;;;;;;;;;;;;;;;;;;;;
- ; Public Declarations ;
- ;;;;;;;;;;;;;;;;;;;;;;;
- alias List.DELETE.Public
- alias List.Add.PUBLIC
- alias List.Remove.PUBLIC
- alias List.Get.PUBLIC
- alias List.Count.PUBLIC
- alias List.Set.PUBLIC
- alias List.Remdel.PUBLIC
- alias List.Insert.PUBLIC
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Exception Declarations ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- alias List.EXCEPTION.RangeErr {
- var %object $1, %error $2, %message $5, %scriptLine $3, %scriptDir $4
- return Exception Caught on line $+($chr(40),%scriptLine,:,%scriptDir,$chr(41))from Object ( $+ %object $+ : $+ $IsInstance(%object) $+ ): %error $+ - %message
- }
- ;;;;;;;;;;;;;;
- ; List Alias ;
- ;;;;;;;;;;;;;;
- alias List {
- var %Class List
- if (!$prop) {
- var %object $Class
- - $inheritsFrom(%object,%class)
- if ($IsPrivate(%class,INIT)) {
- return $($+($,%class,.INIT,$chr(40),%object,$chr(44),$left($regsubex($str(.,$0),/(.)/g,$\n $+ $chr(44)),-1),$chr(41)),2)
- }
- }
- if ($IsPublic(%class,$prop)) {
- return $($+($,%Class,.,$prop,$chr(40),$left($regsubex($str(.,$0),/(.)/g,$\n $+ $chr(44)),-1),$chr(41)),2)
- }
- return $catch(0,MemberErr,$scriptline, $token($script,-1,92),$qt($prop) is not a public member of %Class)
- }
- ;;;;;;;;;;;;;;;;
- ; List Methods ;
- ;;;;;;;;;;;;;;;;
- alias List.INIT {
- var %instance $1
- ;;;;;;;;;;;;;;;;;;;;;;;;
- ; Do Initializing here ;
- ;;;;;;;;;;;;;;;;;;;;;;;;
- return %instance
- }
- alias List.COUNT {
- return $class($1,COUNT).GetV
- }
- alias List.REMOVE {
- if $2 !isnum 1- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92), $qt($2) is not a valid list index for object $qt($1)) }
- - $Class($1, COUNT, $calc($Class($1,COUNT).GetV - 1)).SetV
- - $Class($1,ITEMS,&items).getb
- var %x $calc(1 + (($2 - 1) * 16)),%y %x + 16,%z $bvar(&items,%x,16).text
- if %y > $bvar(&items,0) { dec %y }
- var %var $bvar(&items,%x,16).text
- bcopy -c &items %x &items %y -1
- - $Class($1,%var).remv
- - $Class($1,ITEMS,&items).setb
- }
- alias List.INSERT {
- if $3 !isnum 0- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92),$qt($3) is not a valid list index for object $qt($1) $chr(40) $+ Valid Ranges: 1- $+ $class($1,COUNT).GetV $+ $chr(41)) }
- - $Class($1, COUNT, $calc($Class($1,COUNT).GetV + 1)).SetV
- - $Class($1, TOTAL, $calc($Class($1,TOTAL).GetV + 1)).SetV
- var %value $base($Class($1,TOTAL).GetV,10,10,16)
- - $Class($1,ITEMS,&items).GetB
- var %x $calc(1 + (($3 - 1) * 16)),%y $calc(%x + 16)
- bcopy &items %y &items %x -1
- bset -at &items %x %value
- - $Class($1,%value,$2).setv
- - $Class($1,ITEMS,&items).setb
- return $$3
- }
- alias List.ADD {
- - $Class($1,COUNT,$calc($Class($1,COUNT).GetV + 1)).SetV
- - $Class($1,TOTAL,$calc($Class($1,TOTAL).GetV + 1)).SetV
- var %x $base($Class($1,TOTAL).GetV,10,10,16)
- - $Class($1,ITEMS,&items).getb
- bset -at &items $calc(1 + (($class($1,COUNT).GetV - 1) * 16)) %x
- - $Class($1,ITEMS,&items).setb
- - $iif($bvar($2,0),$Class($1,%x,$2).setb,$Class($1,%x,$2).setv)
- return $Class($1,Count).GetV
- }
- alias List.GET {
- if $2 !isnum 0- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92),$qt($2) is not a valid list index for object $qt($1) $chr(40) $+ Valid Ranges: 1- $+ $class($1,COUNT).GetV $+ $chr(41)) }
- $iif($2 == 0,return $Class($1,COUNT).GetV)
- - $Class($1,ITEMS,&items).GetB
- var %var2 $bvar(&items,$calc(1 + (($2 - 1) * 16)),16).text
- return $Class($1,%var2).GetV
- }
- alias List.SET {
- if $2 !isnum 0- $+ $Class($1,COUNT).GetV { return $catch($1,RangeErr,$scriptline, $token($script,-1,92),$qt($2) is not a valid list index for object $qt($1) $chr(40) $+ Valid Ranges: 1- $+ $class($1,COUNT).GetV $+ $chr(41)) }
- var %z $bvar(&items,$calc(1 + (($2 - 1) * 16)),16).text
- var %x $Class($1,%z).getv
- if $IsInstance(%x) { - $Object(%x).delete }
- - $Class($1,%z,$3).setv
- }
- alias List.IMPORT {
- var %x $List($1).count
- while %x {
- if $Class($List($1,%x).get).IMPORT {
- + Loaded object $qt($List($1,%x).get) of type $qt($IsInstance($List($1,%x).get))
- }
- dec %x
- }
- if $exists($+(object/,$1.bvr)) {
- bread $+(object/,$1.bvr) 1 $file($+(object/,$1.bvr)).size &data
- - $Class($1,ITEMS,&data).setb
- }
- }
- alias List.EXPORT {
- var %x $List($1).count
- while %x {
- var %y $List($1,%x).get
- if $IsInstance(%y) {
- - $ExportObject(%y)
- + Saved object $qt($List($1,%x).get)
- }
- dec %x
- }
- - $Class($1,ITEMS,&data).getb
- if $exists($+(object/,$1.bvr)) {
- .remove $+(object/,$1.bvr)
- }
- bwrite $+(object/,$1.bvr) 1 -1 &data
- }
- alias List.DELETE {
- ;;;;;;;;;;;;;;;;;;;;;;
- ; Do Destroying here ;
- ;;;;;;;;;;;;;;;;;;;;;;
- - $Class($1,ITEMS).remv
- - $Class($1,TOTAL).remv
- - $Class($1,COUNT).remv
- if $2 {
- while $hget($1,1).item {
- var %y $v1
- var %x $hget($1,%y)
- if $IsInstance(%x) {
- - $Object(%x,1).delete
- - Removed object: $qt(%x) from list $qt($1)
- }
- - $Class($1,%y).remv
- }
- }
- return $Class($1).DELETE
- }
- ;;;;;;;;;;;;
- ; End List ;
- ;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement