123456789_123456789_123456789_123456789_123456789_

Class: Fiddle::Packer

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, PackInfo
Inherits: Object
Defined in: ext/fiddle/lib/fiddle/pack.rb

Constant Summary

PackInfo - Included

ALIGN_MAP, PACK_MAP, SIZE_MAP

Class Method Summary

Instance Method Summary

Constructor Details

.new(types) ⇒ Packer

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/pack.rb', line 73

def initialize(types)
  parse_types(types)
end

Class Method Details

.[](*types)

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/pack.rb', line 69

def self.[](*types)
  new(types)
end

Instance Method Details

#pack(ary)

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/pack.rb', line 81

def pack(ary)
  case SIZEOF_VOIDP
  when SIZEOF_LONG
    ary.pack(@template)
  else
    if defined?(TYPE_LONG_LONG) and
      SIZEOF_VOIDP == SIZEOF_LONG_LONG
      ary.pack(@template)
    else
      raise(RuntimeError, "sizeof(void*)?")
    end
  end
end

#parse_types(types) (private)

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/pack.rb', line 111

def parse_types(types)
  @template = "".dup
  addr     = 0
  types.each{|t|
    orig_addr = addr
    if( t.is_a?(Array) )
      addr = align(orig_addr, ALIGN_MAP[TYPE_VOIDP])
    else
      addr = align(orig_addr, ALIGN_MAP[t])
    end
    d = addr - orig_addr
    if( d > 0 )
      @template << "x#{d}"
    end
    if( t.is_a?(Array) )
      @template << (PACK_MAP[t[0]] * t[1])
      addr += (SIZE_MAP[t[0]] * t[1])
    else
      @template << PACK_MAP[t]
      addr += SIZE_MAP[t]
    end
  }
  addr = align(addr, ALIGN_MAP[TYPE_VOIDP])
  @size = addr
end

#size

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/pack.rb', line 77

def size()
  @size
end

#unpack(ary)

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/pack.rb', line 95

def unpack(ary)
  case SIZEOF_VOIDP
  when SIZEOF_LONG
    ary.join().unpack(@template)
  else
    if defined?(TYPE_LONG_LONG) and
      SIZEOF_VOIDP == SIZEOF_LONG_LONG
      ary.join().unpack(@template)
    else
      raise(RuntimeError, "sizeof(void*)?")
    end
  end
end