Class: Win32::CaptureIE::FFI::StructureType
- Defined in:
- lib/win32/capture_ie/ffi/struct.rb
Overview
:nodoc:
Constant Summary collapse
- PACKING_ALIGN =
4
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ruby_class ⇒ Object
readonly
Returns the value of attribute ruby_class.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #align!(r, offset) ⇒ Object
- #calc_offsets ⇒ Object
- #calc_size ⇒ Object
- #define!(ns) ⇒ Object
- #define_ruby_class(ns) ⇒ Object
-
#initialize(name, size = nil) ⇒ StructureType
constructor
A new instance of StructureType.
- #pack(value) ⇒ Object
Constructor Details
#initialize(name, size = nil) ⇒ StructureType
Returns a new instance of StructureType.
52 53 54 55 56 57 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 52 def initialize(name, size=nil) super(name) @ruby_class = nil @fields = [] @size = size end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
51 52 53 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 51 def fields @fields end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
51 52 53 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 51 def name @name end |
#ruby_class ⇒ Object (readonly)
Returns the value of attribute ruby_class.
51 52 53 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 51 def ruby_class @ruby_class end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
51 52 53 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 51 def size @size end |
Instance Method Details
#align!(r, offset) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 97 def align!(r, offset) if offset < r.length raise "buffer over flow: expected offset #{offset}, but was #{r.length}" end if r.length < offset r << "\0" * (offset - r.length) end end |
#calc_offsets ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 59 def calc_offsets offset = 0 @fields.each do |f| align = [f.type.size, PACKING_ALIGN].min offset = (offset.to_f / align).ceil * align size = f.size if f.offset.nil? f.offset = offset offset += size else offset = f.offset offset += size end end end |
#calc_size ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 75 def calc_size return @size if @size if @fields.empty? @size = 0 return end align = [@fields.first.type.size, PACKING_ALIGN].min offset = @fields.last.offset + @fields.last.size @size = (offset.to_f / align).ceil * align end |
#define!(ns) ⇒ Object
106 107 108 109 110 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 106 def define!(ns) calc_offsets calc_size define_ruby_class(ns) end |
#define_ruby_class(ns) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 112 def define_ruby_class(ns) types = self name = @name clazz = Class.new clazz.instance_eval { types.fields.each {|f| attr_accessor f.name } define_method(:initialize) {|*args| types.fields.zip(args).each {|f,arg| instance_variable_set(f.var, arg || f.init) } } define_method(:pack) { types.pack(self) } } clazz.class_eval %Q{ def names [#{types.fields.map{|f| ":#{f.name}"} * ','}] end def values [#{types.fields.map{|f| f.var} * ','}] end } (class <<clazz; self; end).instance_eval { define_method(:packed_size) { CStruct.sizeof(name) } define_method(:c_type) { CStruct.c_type(name) } } @ruby_class = clazz ns.const_set(@name, clazz) end |
#pack(value) ⇒ Object
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/win32/capture_ie/ffi/struct.rb', line 86 def pack(value) packed = @fields.zip(value.values).map{|f,v| [f.offset, f.pack(v)] } r = "" packed.each do |offset,c| align!(r, offset) r << c end align!(r, size) r end |