diff options
author | sinisterchipmunk <sinisterchipmunk@gmail.com> | 2020-01-22 02:55:16 -0500 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-05-23 14:29:16 +0900 |
commit | 4a835621ced099316850a16f2a13534dea62a0b8 (patch) | |
tree | 5cd9aa7b8d9f2c9f76874176bfe1aa802f608f03 /ext/fiddle | |
parent | aa1d3c7d2c020ec927acaa487e8593172fb64bb0 (diff) | |
download | ruby-4a835621ced099316850a16f2a13534dea62a0b8.tar.gz |
[ruby/fiddle] Make array access override compatible with base class (#25)
* Allow access to a struct's underlying memory with `struct[offset, length]`.
* Make accessing a struct's underlying memory more convenient.
* refactor memory access unit tests for improved clarity
https://github.com/ruby/fiddle/commit/c082c81bb5
Diffstat (limited to 'ext/fiddle')
-rw-r--r-- | ext/fiddle/lib/fiddle/struct.rb | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/ext/fiddle/lib/fiddle/struct.rb b/ext/fiddle/lib/fiddle/struct.rb index 7c0dedb39f..f5385e548c 100644 --- a/ext/fiddle/lib/fiddle/struct.rb +++ b/ext/fiddle/lib/fiddle/struct.rb @@ -54,6 +54,8 @@ module Fiddle @entity = klass.entity_class.new(addr, types) @entity.assign_names(members) } + define_method(:[]) { |*args| @entity.send(:[], *args) } + define_method(:[]=) { |*args| @entity.send(:[]=, *args) } define_method(:to_ptr){ @entity } define_method(:to_i){ @entity.to_i } members.each{|name| @@ -148,8 +150,21 @@ module Fiddle @size = PackInfo.align(offset, max_align) end - # Fetch struct member +name+ - def [](name) + # Fetch struct member +name+ if only one argument is specified. If two + # arguments are specified, the first is an offset and the second is a + # length and this method returns the string of +length+ bytes beginning at + # +offset+. + # + # Examples: + # + # my_struct = struct(['int id']).malloc + # my_struct.id = 1 + # my_struct['id'] # => 1 + # my_struct[0, 4] # => "\x01\x00\x00\x00".b + # + def [](*args) + return super(*args) if args.size > 1 + name = args[0] idx = @members.index(name) if( idx.nil? ) raise(ArgumentError, "no such member: #{name}") @@ -182,8 +197,20 @@ module Fiddle end end - # Set struct member +name+, to value +val+ - def []=(name, val) + # Set struct member +name+, to value +val+. If more arguments are + # specified, writes the string of bytes to the memory at the given + # +offset+ and +length+. + # + # Examples: + # + # my_struct = struct(['int id']).malloc + # my_struct['id'] = 1 + # my_struct[0, 4] = "\x01\x00\x00\x00".b + # my_struct.id # => 1 + # + def []=(*args) + return super(*args) if args.size > 2 + name, val = *args idx = @members.index(name) if( idx.nil? ) raise(ArgumentError, "no such member: #{name}") |