Class: SGC::Memory::Buffer

Inherits:
Object
  • Object
show all
Includes:
IBuffer
Defined in:
lib/memory/buffer.rb

Overview

A memory buffer class which implements IBuffer interface.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, size) ⇒ Object

Returns A buffer with size elements of type.

Parameters:

  • type (Symbol)

    A symbol corresponds to a supported C data type, e.g. :int, :long, :float.

  • size (Integer)

    The number of elements.


45
46
47
48
49
50
51
52
# File 'lib/memory/buffer.rb', line 45

def initialize(type, size)
    @@reads[type] && @@writes[type] or raise "Invalid buffer element type."

    @reader = @@reads[type]
    @writer = @@writes[type]
    @ptr = FFI::MemoryPointer.new(type, size)
    @size = size
end

Class Method Details

.element_size(type) ⇒ Integer

Returns The size of an element of type.

Parameters:

  • type (Symbol)

    A symbol corresponds to a supported C data type, e.g. :int, :long, :float.

Returns:

  • (Integer)

    The size of an element of type.


108
109
110
# File 'lib/memory/buffer.rb', line 108

def self.element_size(type)
    @@sizes[type]
end

Instance Method Details

#[](index) ⇒ Object

Returns The element at index of this buffer.

Parameters:

  • index (Integer)

    The index (0..size-1) of the element to return.

Returns:

  • The element at index of this buffer.


57
58
59
60
# File 'lib/memory/buffer.rb', line 57

def [](index)
    assert_index(index)
    @ptr[index].send(@reader)
end

#[]=(index, value) ⇒ Object

Set the element at index of this buffer to value.

Parameters:

  • index (Integer)

    The index (0..size-1) of the element to set.

  • value (Object)

    The value to set to.

Returns:

  • value.


67
68
69
70
71
# File 'lib/memory/buffer.rb', line 67

def []=(index, value)
    assert_index(index)
    @ptr[index].send(@writer, value)
    value
end

#element_sizeInteger

Returns The size of an element in this buffer in bytes.

Returns:

  • (Integer)

    The size of an element in this buffer in bytes.


81
82
83
# File 'lib/memory/buffer.rb', line 81

def element_size
    @ptr.type_size
end

#offset(index) ⇒ MemoryPointer

Returns A memory pointer pointing to the index element.

Parameters:

  • index (Integer)

    The index to an element in this buffer.

Returns:

  • (MemoryPointer)

    A memory pointer pointing to the index element.


100
101
102
103
# File 'lib/memory/buffer.rb', line 100

def offset(index)
    assert_index(index)
    MemoryPointer.new(@ptr[index])
end

#sizeInteger

Returns The number of elements in this buffer.

Returns:

  • (Integer)

    The number of elements in this buffer.


75
76
77
# File 'lib/memory/buffer.rb', line 75

def size
    @size
end