Class: GRCommons::Fiddley::MemoryPointer

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/gr_commons/fiddley.rb

Constant Summary

Constants included from Utils

Utils::SIZET_FORMAT, Utils::SIZET_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

array2str, str2array, type2size, type2type

Constructor Details

#initialize(type, num = 1) ⇒ MemoryPointer

Returns a new instance of MemoryPointer.


185
186
187
188
189
190
191
192
193
# File 'lib/gr_commons/fiddley.rb', line 185

def initialize(type, num = 1)
  if num.is_a?(Fiddle::Pointer)
    @ptr = num
    @size = @ptr.size
  else
    @size = type2size(type) * num
    @ptr = Fiddle::Pointer.malloc(@size)
  end
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size


195
196
197
# File 'lib/gr_commons/fiddley.rb', line 195

def size
  @size
end

Instance Method Details

#get_bytes(offset, len) ⇒ Object


359
360
361
# File 'lib/gr_commons/fiddley.rb', line 359

def get_bytes(offset, len)
  @ptr[offset, len]
end

#get_string(offset, len = nil) ⇒ Object


379
380
381
# File 'lib/gr_commons/fiddley.rb', line 379

def get_string(offset, len = nil)
  @ptr[offset, len || @size - offset]
end

#put_bytes(offset, str, idx = 0, len = str.bytesize - idx) ⇒ Object


351
352
353
# File 'lib/gr_commons/fiddley.rb', line 351

def put_bytes(offset, str, idx = 0, len = str.bytesize - idx)
  @ptr[offset, len] = str[idx, len]
end

#put_string(offset, str) ⇒ Object


367
368
369
# File 'lib/gr_commons/fiddley.rb', line 367

def put_string(offset, str)
  @ptr[offset, str.bytesize] = str
end

#read_bytes(len) ⇒ Object


363
364
365
# File 'lib/gr_commons/fiddley.rb', line 363

def read_bytes(len)
  get_bytes(0, len)
end

#read_pointerObject

added NOTE: Return Fiddle::Pointer. Not MemoryPointer.


389
390
391
# File 'lib/gr_commons/fiddley.rb', line 389

def read_pointer
  to_ptr.ptr
end

#read_string(len = nil) ⇒ Object


383
384
385
# File 'lib/gr_commons/fiddley.rb', line 383

def read_string(len = nil)
  get_string(0, len)
end

#to_ptrObject


197
198
199
# File 'lib/gr_commons/fiddley.rb', line 197

def to_ptr
  @ptr
end

#write_bytes(str, idx = 0, len = nil) ⇒ Object


355
356
357
# File 'lib/gr_commons/fiddley.rb', line 355

def write_bytes(str, idx = 0, len = nil)
  put_bytes(0, str, idx, len)
end

#write_string(str, len = nil) ⇒ Object


371
372
373
# File 'lib/gr_commons/fiddley.rb', line 371

def write_string(str, len = nil)
  put_string(0, len ? str[0, len] : str)
end

#write_string_length(str, len) ⇒ Object


375
376
377
# File 'lib/gr_commons/fiddley.rb', line 375

def write_string_length(str, len)
  put_string(0, str[0, len])
end