[前][次][番号順一覧][スレッド一覧]

ruby-changes:23806

From: drbrain <ko1@a...>
Date: Fri, 1 Jun 2012 07:07:27 +0900 (JST)
Subject: [ruby-changes:23806] drbrain:r35857 (trunk): * ext/dl/lib/dl/struct.rb (DL::CStructEntity#set_ctypes): Refactored

drbrain	2012-06-01 07:07:09 +0900 (Fri, 01 Jun 2012)

  New Revision: 35857

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35857

  Log:
    * ext/dl/lib/dl/struct.rb (DL::CStructEntity#set_ctypes):  Refactored
      #set_ctypes using newer ruby features to simplify its implementation.
    * test/dl/test_c_struct_entry.rb (class DL):  Test to verify
      refactoring.
    
    Reviewed by Aaron Patterson.

  Modified files:
    trunk/ChangeLog
    trunk/ext/dl/lib/dl/struct.rb
    trunk/test/dl/test_c_struct_entry.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 35856)
+++ ChangeLog	(revision 35857)
@@ -1,3 +1,10 @@
+Fri Jun  1 06:57:10 2012  Eric Hodel  <drbrain@s...>
+
+	* ext/dl/lib/dl/struct.rb (DL::CStructEntity#set_ctypes):  Refactored
+	  #set_ctypes using newer ruby features to simplify its implementation.
+	* test/dl/test_c_struct_entry.rb (class DL):  Test to verify
+	  refactoring.
+
 Fri Jun  1 06:40:25 2012  Eric Hodel  <drbrain@s...>
 
 	* object.c (Init_Object):  Restored Kernel documentation based on
Index: ext/dl/lib/dl/struct.rb
===================================================================
--- ext/dl/lib/dl/struct.rb	(revision 35856)
+++ ext/dl/lib/dl/struct.rb	(revision 35857)
@@ -127,27 +127,20 @@
       @ctypes = types
       @offset = []
       offset = 0
-      max_align = 0
-      types.each_with_index{|t,i|
+
+      max_align = types.map { |type, count = 1|
         orig_offset = offset
-        if( t.is_a?(Array) )
-          align = ALIGN_MAP[t[0]]
-        else
-          align = ALIGN_MAP[t]
-        end
+        align = ALIGN_MAP[type]
         offset = PackInfo.align(orig_offset, align)
-        @offset[i] = offset
-        if( t.is_a?(Array) )
-          offset += (SIZE_MAP[t[0]] * t[1])
-        else
-          offset += SIZE_MAP[t]
-        end
-        if (max_align < align)
-          max_align = align
-        end
-      }
-      offset = PackInfo.align(offset, max_align)
-      @size = offset
+
+        @offset << offset
+
+        offset += (SIZE_MAP[type] * count)
+
+        align
+      }.max
+
+      @size = PackInfo.align(offset, max_align)
     end
 
     # Fetch struct member +name+
Index: test/dl/test_c_struct_entry.rb
===================================================================
--- test/dl/test_c_struct_entry.rb	(revision 35856)
+++ test/dl/test_c_struct_entry.rb	(revision 35857)
@@ -37,5 +37,17 @@
 
     assert_equal expected, size
   end
+
+  def test_set_ctypes
+    union = DL::CStructEntity.malloc [DL::TYPE_INT, DL::TYPE_LONG]
+    union.assign_names %w[int long]
+
+    # this test is roundabout because the stored ctypes are not accessible
+    union['long'] = 1
+    union['int'] = 2
+
+    assert_equal 1, union['long']
+    assert_equal 2, union['int']
+  end
 end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]