class Novika::FFI::StructLayout

Overview

Allows to describe structs which can then be constructed, retrieved, read, and written to.

point_s = StructLayout.new("Point")
point_s.add("x", F64)
point_s.add("y", F64)

rect_s = StructLayout.new("Rect")
rect_s.add("origin", point_s.reference)
rect_s.add("corner", point_s.inline)

origin = point_s.reference.make!
origin["x"] = F64.new(123)
origin["y"] = F64.new(456)

corner = point_s.inline.make!
corner["x"] = F64.new(234)
corner["y"] = F64.new(567)

rect = rect_s.reference.make!
rect["origin"] = origin
rect["corner"] = corner

Defined in:

novika/ffi.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

Creates an empty struct layout.


Instance Method Detail

def ==(other : self) #

Returns whether this and other layouts are the same layout. Uses reference equality (like same?) rather than deep equality.


def add(id, type : ForeignType) #

Appends a field called id, of the given type, to this struct's list of fields.

Similar to struct ids, id is irrelevant to FFI and is simply one of the ways to access struct fields.


def alignment : UInt64 #

Returns the alignment of this struct layout.


def desc(index : Int32) #

Retrieves field description given the field's index.


def desc(id : String) #

Retrieves field description given the field's identifier. Raises if no such field exists.


def desc?(id : String) #

Retrieves field description given the field's identifier. Returns nil if no such field exists.


def each_desc_with_index(&) #

Yields field descriptions and their indices to the block.


def field_count #

Returns the amount of fields in this struct layout.


def has_field?(id : String) #

Returns whether this layout contains a field with the given identifier.


def hash(hasher) #

Returns whether this and other layouts are the same layout. Uses reference equality (like same?) rather than deep equality.


def index(id : String) #

Returns the index of a field with the given identifier. Dies if there is no such field.


def index?(id : String) #

Returns the index of a field with the given identifier, or nil if there is no such field.


def inline #

Returns an inline struct type layed out according to this struct layout. You can then use it in your struct field / argument types.

Note: this method costs nothing. Feel free to spam .inline instead of saving it in a variable and using that variable.


def map_desc_with_index(&) #

Yields field descriptions and their indices to the block. Returns an array of block results.


def max_field_size : UInt64 #

Returns the maximum field size in this struct.


def padded_size : UInt64 #

Returns the padded size of this struct. Simply put, this is how much bytes you'd need to allocate for this struct layout.


def reference #

Returns a struct reference type layed out according to this struct layout. You can then use it in your struct field / argument types.

Note: this method costs nothing. Feel free to spam .reference instead of saving it in a variable and using that variable.


def to_s(io) #

def union #

Returns a union type layed out according to this struct layout. You can then use it in your struct field / argument types.

Note: this method costs nothing. Feel free to spam .union instead of saving it in a variable and using that variable.