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



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



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

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

Instance Method Details

#[](index) ⇒ Object



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.



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



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

def element_size
    @ptr.type_size
end

#offset(index) ⇒ MemoryPointer



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

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

#sizeInteger



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

def size
    @size
end