Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when use FFI.typedef :ORITYPE, :TYPEXXX define a type alisname #472

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/ffi/abstractmemory.rb
@@ -0,0 +1,27 @@

module FFI
class AbstractMemory
# for TYPEXXX in FFI::TypeDefs.keys
# add respond_to? [:put_TYPEXXX, :get_TYPEXXX,
# :write_TYPEXXX, :read_TYPEXXX,
# :put_array_of_TYPEXXX, :get_array_of_TYPEXXX,
# :write_array_of_TYPEXXX, :read_array_of_TYPEXXX]
def method_missing method_name, *args, &block
if /^(?<oper>(?:put|get|write|read)_(?:array_of_)?)(?<type_name>[[:word:]]+)$/ =~ method_name.to_s
if builtintype = FFI::TypeDefs[type_name.intern] || FFI::TypeDefs[type_name]
if /FFI\:\:Type\:\:Builtin\:(?<ori_type>[[:word:]]+)/ =~ builtintype.inspect
ori_method1 = "#{oper}#{ori_type}"
ori_method2 = "#{oper}#{ori_type.downcase}"
if respond_to?(ori_method1, true)
return send(ori_method1, *args, &block)
elsif respond_to?(ori_method2, true)
return send(ori_method2, *args, &block)
end
end
end
end

super
end
end
end
1 change: 1 addition & 0 deletions lib/ffi/ffi.rb
Expand Up @@ -28,6 +28,7 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require 'ffi/abstractmemory'
require 'ffi/platform'
require 'ffi/types'
require 'ffi/library'
Expand Down
40 changes: 40 additions & 0 deletions spec/ffi/abstractmemory_spec.rb
@@ -0,0 +1,40 @@

require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))

describe "abstract memory" do
before(:all) do
FFI.typedef :uint64, :TYPEXXX
FFI.typedef :uint16, :TYPEXXY
end

it 'type size' do
a = FFI::Buffer.new :TYPEXXX
b = FFI::Buffer.new :TYPEXXY

expect(a.size).to eq(8)
expect(a.size).to eq(FFI::Buffer.new(:uint64).size)

expect(b.size).to eq(2)
expect(b.size).to eq(FFI::Buffer.new(:uint16).size)
end

it 'method missing' do
v1, v2, v3, v4 = 0xFE01020304050607, 0xFE01020304050608, 0xFE01020304050609, 0xFE0102030405060A

aa = FFI::Buffer.new(:TYPEXXX, 4)

aa.write_TYPEXXX(v1)
expect(aa.read_TYPEXXX).to eq(v1)
expect(aa.read_uint64).to eq(v1)

aa.put_TYPEXXX(8, v2)
expect(aa.get_TYPEXXX(8)).to eq(v2)

aa.write_array_of_TYPEXXX([v1, v2, v4, v3])
expect(aa.read_array_of_TYPEXXX(4)).to eq([v1, v2, v4, v3])

aa.put_array_of_TYPEXXX 16, [v3, v4]
expect(aa.get_array_of_TYPEXXX(0, 4)).to eq([v1, v2, v3, v4])
expect(aa.get_array_of_uint64(0, 4)).to eq([v1, v2, v3, v4])
end
end