123456789_123456789_123456789_123456789_123456789_

Class: Fiddle::CStruct

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
Defined in: ext/fiddle/lib/fiddle/struct.rb

Overview

A base class for objects representing a C structure

Class Method Summary

Instance Method Summary

Class Method Details

.entity_class

accessor to CStructEntity

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 12

def CStruct.entity_class
  CStructEntity
end

.offsetof(name, members, types)

This method is for internal use only.
[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 16

def self.offsetof(name, members, types) # :nodoc:
  offset = 0
  worklist = name.split('.')
  this_type = self
  while search_name = worklist.shift
    index = 0
    member_index = members.index(search_name)

    unless member_index
      # Possibly a sub-structure
      member_index = members.index { |member_name, _|
        member_name == search_name
      }
      return unless member_index
    end

    types.each { |type, count = 1|
      orig_offset = offset
      if type.respond_to?(:entity_class)
        align = type.alignment
        type_size = type.size
      else
        align = PackInfo::ALIGN_MAP[type]
        type_size = PackInfo::SIZE_MAP[type]
      end

      # Unions shouldn't advance the offset
      if this_type.entity_class == CUnionEntity
        type_size = 0
      end

      offset = PackInfo.align(orig_offset, align)

      if worklist.empty?
        return offset if index == member_index
      else
        if index == member_index
          subtype = types[member_index]
          members = subtype.members
          types = subtype.types
          this_type = subtype
          break
        end
      end

      offset += (type_size * count)
      index += 1
    }
  end
  nil
end

Instance Method Details

#each

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 68

def each
  return enum_for(__function__) unless block_given?

  self.class.members.each do |name,|
    yield(self[name])
  end
end

#each_pair

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 76

def each_pair
  return enum_for(__function__) unless block_given?

  self.class.members.each do |name,|
    yield(name, self[name])
  end
end

#replace(another)

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 92

def replace(another)
  if another.nil?
    self.class.members.each do |name,|
      self[name] = nil
    end
  elsif another.respond_to?(:each_pair)
    another.each_pair do |name, value|
      self[name] = value
    end
  else
    another.each do |name, value|
      self[name] = value
    end
  end
  self
end

#to_h

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 84

def to_h
  hash = {}
  each_pair do |name, value|
    hash[name] = unstruct(value)
  end
  hash
end

#unstruct(value) (private)

[ GitHub ]

  
# File 'ext/fiddle/lib/fiddle/struct.rb', line 110

def unstruct(value)
  case value
  when CStruct
    value.to_h
  when Array
    value.collect do |v|
      unstruct(v)
    end
  else
    value
  end
end