|
| 1 | + |
| 2 | +Namespace std.syntax |
| 3 | + |
| 4 | +#rem @pro Attributes Minilibrary |
| 5 | +@author iDkP from GaragePixel |
| 6 | +@since 2025-07-04 |
| 7 | +@example |
| 8 | + |
| 9 | + Function myfn(attribs:VAttribs[]) |
| 10 | + Local vars:=Bind(args) |
| 11 | + Local height:=Int(vars.Get("height")) |
| 12 | + Local width:=Float(vars.Get("width")) |
| 13 | + |
| 14 | + Print width+height |
| 15 | + End |
| 16 | + |
| 17 | + Function Main() |
| 18 | + |
| 19 | + myfn(New VAttribs[]( "title" , "myTitle", |
| 20 | + "height", 10, |
| 21 | + "width" , 10.5 )) |
| 22 | + End |
| 23 | +#end |
| 24 | + |
| 25 | +'#Import "../../types/types" |
| 26 | +'#Import "../../system/reflection/reflection" |
| 27 | +'#Import "../primitives/variants/variantcasts" |
| 28 | + |
| 29 | +Using std.types.. |
| 30 | +Using std.reflection |
| 31 | +Using std.syntax.variantcasts |
| 32 | + |
| 33 | +Alias VAttribs:Variant 'Conveniant alias alternative name, usually used in conjonction with a container [] |
| 34 | + 'It should be remplaced by Var, who was reserved by Mark for this usage. |
| 35 | + |
| 36 | +Alias VarAttribs:cVarAttribs<String,TVar> |
| 37 | + |
| 38 | +Function Bind:VarAttribs(v:Variant[]) 'VarAttribs is an array of variant, interleaved as string,variant,string,variant... |
| 39 | + Return New VarAttribs(v,True) 'Then VarAttribs is then used to make a Map<String,Variant> called VarAttribs |
| 40 | +End |
| 41 | + |
| 42 | +Class cVarAttribs<K,V> Extends Map<K,V> Where K=String And V=TVar |
| 43 | + |
| 44 | + 'Should be remplaced by Var, who was reserved by Mark for this usage. |
| 45 | + 'The generic types are locked by the VarAttribs alias. We don't have to try calling |
| 46 | + 'cVarAttribs from another type declaration or alias. Only the VarAttribs call is valid. |
| 47 | + |
| 48 | + Method New( attrs:Variant[], t:Bool=True ) Override |
| 49 | + Assert((attrs.Length Mod 2) = 0,"Pair missing: array length must be even.") |
| 50 | + Local len:=attrs.Length/2 |
| 51 | + |
| 52 | + For Local i:=0 Until len+2 Step 2 |
| 53 | + local tvar:=New TVar() |
| 54 | + tvar.Set(attrs[i+1]) |
| 55 | + tvar.Type=stdlib.reflection.GetType(attrs[i+1]) |
| 56 | + Add(Cast<String>(attrs[i]),tvar) |
| 57 | + End |
| 58 | + End |
| 59 | + |
| 60 | + Method New( attrs:Stack<Variant>, t:Bool=True ) Override |
| 61 | + Assert((attrs.Length Mod 2) = 0,"Pair missing: array length must be even.") |
| 62 | + Local len:=attrs.Length/2 |
| 63 | + |
| 64 | + For Local i:=0 Until len+2 Step 2 |
| 65 | + local tvar:=New TVar() |
| 66 | + tvar.Set(attrs[i+1]) |
| 67 | + tvar.Type=stdlib.reflection.GetType(attrs[i+1]) |
| 68 | + Add(Cast<String>(attrs[i]),tvar) |
| 69 | + End |
| 70 | + End |
| 71 | + |
| 72 | + Method GetT<T>:T( key:K ) |
| 73 | + Local node:=FindNode( key ) |
| 74 | + If node Return node.Value |
| 75 | + Return Null |
| 76 | + End |
| 77 | + |
| 78 | + Method GetBool:Bool( key:K ) |
| 79 | + Return Bool(Get(key)) |
| 80 | + End |
| 81 | + |
| 82 | + Method GetInt:Int( key:K ) |
| 83 | + Return Int(Get(key)) |
| 84 | + End |
| 85 | + |
| 86 | + Method GetFloat:Int( key:K ) |
| 87 | + Return Float(Get(key)) |
| 88 | + End |
| 89 | + |
| 90 | + Method GetString:String( key:K ) |
| 91 | + Return String(Get(key)) |
| 92 | + End |
| 93 | + |
| 94 | + Method GetB:Bool( key:K ) |
| 95 | + Return GetBool(key) |
| 96 | + End |
| 97 | + |
| 98 | + Method GetI:Int( key:K ) |
| 99 | + Return GetInt(key) |
| 100 | + End |
| 101 | + |
| 102 | + Method GetF:Float( key:K ) |
| 103 | + Return GetFloat(key) |
| 104 | + End |
| 105 | + |
| 106 | + Method GetS:String( key:K ) |
| 107 | + Return GetString(key) |
| 108 | + End |
| 109 | + |
| 110 | +End |
0 commit comments