@@ -20,6 +20,33 @@ def CUnion.entity_class
2020 end
2121 end
2222
23+ # Wrapper for arrays within a struct
24+ class StructArray < Array
25+ include ValueUtil
26+
27+ def initialize ( ptr , type , initial_values )
28+ @ptr = ptr
29+ @type = type
30+ @align = PackInfo ::ALIGN_MAP [ type ]
31+ @size = Fiddle ::PackInfo ::SIZE_MAP [ type ]
32+ @pack_format = Fiddle ::PackInfo ::PACK_MAP [ type ]
33+ super ( initial_values . collect { |v | unsigned_value ( v , type ) } )
34+ end
35+
36+ def to_ptr
37+ @ptr
38+ end
39+
40+ def []=( index , value )
41+ if index < 0 || index >= size
42+ raise IndexError , 'index %d outside of array bounds 0...%d' % [ index , size ]
43+ end
44+
45+ to_ptr [ index * @size , @size ] = [ value ] . pack ( @pack_format )
46+ super ( index , value )
47+ end
48+ end
49+
2350 # Used to construct C classes (CUnion, CStruct, etc)
2451 #
2552 # Fiddle::Importer#struct and Fiddle::Importer#union wrap this functionality in an
@@ -191,7 +218,7 @@ def [](*args)
191218 if ( ty . is_a? ( Integer ) && ( ty < 0 ) )
192219 return unsigned_value ( val , ty )
193220 elsif ( ty . is_a? ( Array ) && ( ty [ 0 ] < 0 ) )
194- return val . collect { | v | unsigned_value ( v , ty [ 0 ] ) }
221+ return StructArray . new ( self + @offset [ idx ] , ty [ 0 ] , val )
195222 else
196223 return val
197224 end
0 commit comments