alias D_Static_Array(char,3) to char[3];
alias D_Static_Array(char,3,4) to char[3][4];
# this will catch all char[3] char[3][4]
# brings out the aliases from inside of the data template
import D_Static_Array( 'char', / char [ \[ $d := (\d+) \] ]+ / );
# this will catch all static array types
import D_Static_Array( /
# please note that increasing the value of .length at runtime
# won't allocate more memory it will just make the program think it
# has more data to work with. So it wouldn't be a good idea to do
# that in most cases.
data D_Static_Array(type $ident,uint $size = 0){
# uint{0} refers to a uint starting at byte offset 0
# readonly means that only this and anything
# that subclasses this can change it (could also be spelled "r").
readonly uint{0} .length = $size;
# you could also spell ".length" as "->length" if you prefer Perl5
# that would mean you would have to rename all instances of ".length".
# the reason you can do this is because all you need is a word boundary
# to be created between the "data" and ".length"
# ".data" will grow and shrink depending on what
# the value of ".length" is at the time you ask for it
# because of the bind ":=" in the definition.
# This also won't allow you to change the length of ".data"
# outside of this class because ".data.length" is bound to ".length".
rw $ident[ _ := .length ]{.length.sizeof} .data;
# this is for retrieving the value at $index
INDEX(uint $index){
uint $pos = $index * $ident.sizeof + length.sizeof;
return $ident[$pos];
}
# this is for determining out of bounds errors
SIZE{
# the size grows and shrink depending on
return (.length + .length.sizeof) * $ident.sizeof;
# or
return (uint{0} + uint.sizeof) * $ident.sizeof;
}
}
data D_Static_Array($ident,@sizes){
# note this "data" will keep another variable around
# to know how long the array ".length" is.
readonly uint[@sizes.length]{0} .length = @sizes;
# note the bind goes recursively through mult because
# it is a keyword.
uint .data_size := mult( @{.length} );
# ".data_size" needs to be bound because the variable it depends
# on can be changed, and there should be a way to propogate it on
# to ".data", it is not enforced, but highly recommended.
readwrite $ident[ _ := .data_size ]{ _ := .length.sizeof } .data;
INDEX( ){
}
SIZE{
return .length.sizeof + .data_size;
}
}
this is what it would do
# array of four arrays of 3 char each.
D_Static_Array(char,3,4)
{
.length = [4,3];
.data = [char,char,char, char,char,char, char,char,char, char,char,char];
}