class RubyVM::RJIT::CPointer::Union

注意:我们希望避免使用字母方法名称,以避免与成员方法冲突。 to_i 被视为例外。

公共类方法

define(sizeof, members) 点击以切换源代码

@param sizeof [Integer] @param members [Hash{ Symbol => RubyVM::RJIT::CType::* }]

调用父类方法
# File ruby_vm/rjit/c_pointer.rb, line 132
def self.define(sizeof, members)
  Class.new(self) do
    # Return the size of this type
    define_singleton_method(:sizeof) { sizeof }

    # Part of Struct's offsetof implementation
    define_singleton_method(:offsetof) do |field, *fields|
      member = members.fetch(field)
      offset = 0
      unless fields.empty?
        offset += member.offsetof(*fields)
      end
      offset
    end

    define_method(:initialize) do |addr|
      super(addr, sizeof, members)
    end

    members.each do |member, type|
      # Intelligent API that does automatic dereference
      define_method(member) do
        value = type.new(@addr)
        if value.respond_to?(:*)
          value = value.*
        end
        value
      end
    end
  end
end
new(addr, sizeof, members) 点击以切换源代码

@param _name [String] 当它开始定义联合指针类时使用 @param sizeof [Integer] @param members [Hash{ Symbol => RubyVM::RJIT::CType::* }]

# File ruby_vm/rjit/c_pointer.rb, line 107
def initialize(addr, sizeof, members)
  @addr = addr
  @sizeof = sizeof
  @members = members
end

公共实例方法

+(index) 点击以切换源代码

移动地址以像访问数组一样访问此指针

# File ruby_vm/rjit/c_pointer.rb, line 119
def +(index)
  raise ArgumentError unless index.is_a?(Integer)
  self.class.new(@addr + index * @sizeof)
end
-(union) 点击以切换源代码

Pointer 差异

# File ruby_vm/rjit/c_pointer.rb, line 125
def -(union)
  raise ArgumentError if self.class != union.class
  (@addr - union.instance_variable_get(:@addr)) / @sizeof
end
to_i() 点击以切换源代码

获取原始地址

# File ruby_vm/rjit/c_pointer.rb, line 114
def to_i
  @addr
end