Ruby 仿 C 结构体:CStruct 的一些例子

1. [代码]最简单的例子     
# CStruct Examples
require 'cstruct'
 
# example:
# struct Point in C\C++        (32-bit platform): 
#
# struct Point
# {
#    int x;
#    int y;
# }; 
 
# struct Point in Ruby: 
class Point < CStruct
  int32:x
  int32:y
end
 
# create a Point's instance
point   = Point.new
 
# assign like as C language
point.x = 10
point.y = 20
 
puts "point.x = #{point.x},point.y = #{point.y}"
 
 
 
# struct PointF in C\C++        (32-bit platform): 
#
# struct PointF
# {
#    double x;
#    double y;
# }; 
 
# struct PointF in Ruby: 
class PointF < CStruct
  double:x
  double:y
end
 
# create a PointF's instance
# use 'block' to initialize the fields
point2   = PointF.new do |st|
        st.x = 10.56
        st.y = 20.78
end
 
puts "sizeof(PointF) = #{PointF.size}"
puts "point2.x = #{point2.x},point2.y = #{point2.y}"
2. [代码]数组成员     

# CStruct Examples
require 'cstruct'
 
# example:
# struct T in C\C++ (32-bit platform): 

# struct T
# {
#    int element[8];
# }; 
 
# struct T in Ruby: 
class T < CStruct
  int32:elements,[8]
end
 
# create a T's instance
t_array = T.new
 
(0..7).each do |i|
        t_array.elements[i] = i  # assign like as C language
endhttp://www.enterdesk.com/special/huangguantp/
 
# output皇冠图片
(0..7).each {|i| puts "t_array.elements[#{i}] = #{t_array.elements[i]}" }
 
 
# Actually,t_array.elements.class is Array. So..
t_array.elements.each {|element| puts element }
3. [代码]结构体嵌套     
# CStruct Examples
require 'cstruct'
# example:
# struct A in C\C++ (32-bit platform): 
# struct A{
#     struct Inner
#     {
#       int v1; 
#       int v2;
#     };
#     Inner inner;
# };
 
# struct A in Ruby:
class A < CStruct
    class Inner < CStruct
        int32 :v1
        int32 :v2
    end
        Inner :inner
end
 
a = A.new
a.inner.v1 = 1
a.inner.v2 = 2
 
puts a.inner.v1 
puts a.inner.v2 
4. [代码]匿名结构体     

# CStruct Examples
require 'cstruct'
 
# example:
# struct Window in C\C++ (32-bit platform): 

# struct Window
# {
#    int style;
#    struct{
#       int x;
#       int y;
#    }position; /* position is anonymous struct's instance */
# }; 
 
 
# struct Window in Ruby: 
class Window < CStruct
    int32:style
    struct :position do
        int32:x
        int32:y       
    end
end
# or like this (use brace):
# class Window < CStruct
#    int32:style
#    struct (:position) {
#        int32:x
#        int32:y        
#    }
# end
 
# create a Window's instance
window = Window.new
 
# assign like as C language
window.style = 1
window.position.x = 10
window.position.y = 10
 
puts "sizeof(Window) = #{Window.__size__}" # "__size__" is alias of "size"
puts "window.style = #{window.style},window.position.x = #{window.position.x},window.position.y = #{window.position.y}"
5. [代码]命名空间     

# CStruct Examples
require 'cstruct'
 
# example:
module NS1    #namespace
    class A < CStruct
        uint32:handle
    end
   
    module NS2
        class B < CStruct
            A:a        # directly use A
        end
    end
 
    class C < CStruct
      A     :a
      NS2_B :b        # Meaning of the 'NS2_B' is NS2::B
    end       
end
 
class D < CStruct
    NS1_NS2_B:b        # Meaning of the 'NS1_NS2_B' is NS1::NS2::B
end       
 
v = D.new
v.b.a.handle = 120
p D.__size__
p v.b.a.handle

posted @ 2014-09-05 14:43  虚空之眼  阅读(842)  评论(0编辑  收藏  举报