Class: SGC::Memory::Buffer
- Inherits:
-
Object
- Object
- SGC::Memory::Buffer
- Includes:
- IBuffer
- Defined in:
- lib/memory/buffer.rb
Overview
A memory buffer class which implements IBuffer interface.
Class Method Summary collapse
-
.element_size(type) ⇒ Integer
The size of an element of type.
Instance Method Summary collapse
-
#[](index) ⇒ Object
The element at index of this buffer.
-
#[]=(index, value) ⇒ Object
Set the element at index of this buffer to value.
-
#element_size ⇒ Integer
The size of an element in this buffer in bytes.
-
#initialize(type, size) ⇒ Object
constructor
A buffer with size elements of type.
-
#offset(index) ⇒ MemoryPointer
A memory pointer pointing to the index element.
-
#size ⇒ Integer
The number of elements in this buffer.
Constructor Details
#initialize(type, size) ⇒ Object
Returns A buffer with size elements of type.
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.
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.
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_size ⇒ Integer
Returns 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.
100 101 102 103 |
# File 'lib/memory/buffer.rb', line 100 def offset(index) assert_index(index) MemoryPointer.new(@ptr[index]) end |
#size ⇒ Integer
Returns The number of elements in this buffer.
75 76 77 |
# File 'lib/memory/buffer.rb', line 75 def size @size end |