@@ -20,6 +20,33 @@ def CUnion.entity_class
20
20
end
21
21
end
22
22
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
+
23
50
# Used to construct C classes (CUnion, CStruct, etc)
24
51
#
25
52
# Fiddle::Importer#struct and Fiddle::Importer#union wrap this functionality in an
@@ -191,7 +218,7 @@ def [](*args)
191
218
if ( ty . is_a? ( Integer ) && ( ty < 0 ) )
192
219
return unsigned_value ( val , ty )
193
220
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 )
195
222
else
196
223
return val
197
224
end
0 commit comments