diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2011-05-02 23:02:45 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2011-05-02 23:02:45 +0200 |
commit | 15fe513c7237ce4d0ad7b34d34c922a47a4a9811 (patch) | |
tree | 74888a182ff8a5b832defeb143d40d428769cddc | |
parent | f502b8aabf430d2f630bc7c8ceeeac578bc3acb5 (diff) | |
download | ffi-efl-15fe513c7237ce4d0ad7b34d34c922a47a4a9811.zip ffi-efl-15fe513c7237ce4d0ad7b34d34c922a47a4a9811.tar.gz |
eina_hash : remove Hash#from_eina_hash, cleanup REinaHash#initialize and FFI::Autopointer usage, update specs
-rw-r--r-- | lib/efl/eina_hash.rb | 33 | ||||
-rw-r--r-- | spec/eina_hash_spec.rb | 16 |
2 files changed, 15 insertions, 34 deletions
diff --git a/lib/efl/eina_hash.rb b/lib/efl/eina_hash.rb index 9aa8808..59d99e2 100644 --- a/lib/efl/eina_hash.rb +++ b/lib/efl/eina_hash.rb @@ -3,17 +3,6 @@ # require 'efl/ffi/eina_hash' # -class Hash - def self.from_eina_hash o - if o.is_a? Efl::EinaHash::REinaHash - o.to_h - elsif o.is_a? FFI::Pointer - Efl::EinaHash::REinaHash.new(o).to_h - else - raise ArgumentError.new " wrong argument #{o.class.name}" - end - end -end module Efl module EinaHash # @@ -26,27 +15,25 @@ module Efl @ptr = ( case o when NilClass - FFI::AutoPointer.new cstr.call, method(:free) - when self.class - o.to_ptr - when FFI::AutoPointer - o + FFI::AutoPointer.new cstr.call, REinaHash.method(:release) when FFI::Pointer - FFI::AutoPointer.new ( o==FFI::Pointer::NULL ? cstr.call : o ), method(:free) + FFI::AutoPointer.new ( o==FFI::Pointer::NULL ? cstr.call : o ), REinaHash.method(:release) when Hash ptr = cstr.call o.each do |k,v| Efl::EinaHash.eina_hash_add ptr, k, v end - FFI::AutoPointer.new ptr, method(:free) + FFI::AutoPointer.new ptr, REinaHash.method(:release) else raise ArgumentError.new "wrong argument #{o.class.name}" end ) end - def free p=nil - return Efl::EinaHash.eina_hash_free p unless p.nil? - Efl::EinaHash.eina_hash_free @ptr - @ptr.free - @ptr = nil + def self.release p + Efl::EinaHash.eina_hash_free p + end + def del + @ptr.autorelease=false + EinaHash.release @ptr + @ptr=nil end def each data=FFI::Pointer::NULL, &block return if not block_given? diff --git a/spec/eina_hash_spec.rb b/spec/eina_hash_spec.rb index 0376b08..4d1526c 100644 --- a/spec/eina_hash_spec.rb +++ b/spec/eina_hash_spec.rb @@ -37,19 +37,13 @@ describe Efl::EinaHash do end # it "should be able to convert into ruby Hash from NULL pointer" do - h = Hash.from_eina_hash FFI::Pointer::NULL + h = REinaHash.new(FFI::Pointer::NULL).to_h h.empty?.should be_true h.is_a?(Hash).should be_true end # it "should be able to convert into ruby Hash from empty REinaHash" do - h = Hash.from_eina_hash REinaHash.new - h.empty?.should be_true - h.is_a?(Hash).should be_true - end - # - it "should be able to convert into ruby Hash from empty REinaHash pointer" do - h = Hash.from_eina_hash REinaHash.new.to_ptr + h = REinaHash.new(FFI::Pointer::NULL).to_h h.empty?.should be_true h.is_a?(Hash).should be_true end @@ -75,7 +69,7 @@ describe Efl::EinaHash do true } cpt.should == 4 - rh = Hash.from_eina_hash h + rh = h.to_h rh.length.should == 4 rh2 = {} rh.each { |k,v| @@ -102,7 +96,7 @@ describe Efl::EinaHash do h['k1'].read_string.should == "D1" h['k2'].read_string.should == "D2" h['k3'].read_string.should == "D3" - rh = Hash.from_eina_hash h.to_ptr + rh = h.to_h rh.length.should == 4 end # @@ -152,7 +146,7 @@ describe Efl::EinaHash do rh['k3'].read_string.should == "D3" end # - it "should be able to build from ruby Hash" do + it "should be able to build REinaHash from ruby Hash" do rh = {} k0 = FFI::MemoryPointer.from_string "0" k1 = FFI::MemoryPointer.from_string "1" |